annotate zdis.c @ 951:fec4a59ae5d7

Merge
author Michael Pavone <pavone@retrodev.com>
date Mon, 11 Apr 2016 20:56:54 -0700
parents a634ed0a92cf
children 137dbd05ceab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
467
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 314
diff changeset
1 /*
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 314
diff changeset
2 Copyright 2013 Michael Pavone
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 314
diff changeset
3 This file is part of BlastEm.
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 314
diff changeset
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 314
diff changeset
5 */
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include "z80inst.h"
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include <stdio.h>
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 #include <stdlib.h>
805
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
9 #include <stdarg.h>
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11 uint8_t visited[(64*1024)/8];
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12 uint8_t label[(64*1024)/8];
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
13
805
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
14 void fatal_error(char *format, ...)
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
15 {
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
16 va_list args;
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
17 va_start(args, format);
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
18 vfprintf(stderr, format, args);
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
19 va_end(args);
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
20 exit(1);
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
21 }
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
22
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
23 void visit(uint16_t address)
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
24 {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
25 visited[address/8] |= 1 << (address % 8);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 void reference(uint16_t address)
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30 //printf("referenced: %X\n", address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
31 label[address/8] |= 1 << (address % 8);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 uint8_t is_visited(uint16_t address)
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
36 return visited[address/8] & (1 << (address % 8));
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 uint8_t is_label(uint16_t address)
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 return label[address/8] & (1 << (address % 8));
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
43
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
44 typedef struct deferred {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 uint16_t address;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 struct deferred *next;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 } deferred;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 deferred * defer(uint16_t address, deferred * next)
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 if (is_visited(address)) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 return next;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 //printf("deferring %X\n", address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 deferred * d = malloc(sizeof(deferred));
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 d->address = address;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 d->next = next;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 return d;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 uint8_t labels = 0;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62 uint8_t addr = 0;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 uint8_t only = 0;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 int main(int argc, char ** argv)
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 long filesize;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 uint8_t *filebuf;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 char disbuf[1024];
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70 z80inst instbuf;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 uint8_t * cur;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
72 FILE * f = fopen(argv[1], "rb");
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 fseek(f, 0, SEEK_END);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 filesize = ftell(f);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75 fseek(f, 0, SEEK_SET);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 filebuf = malloc(filesize);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 fread(filebuf, 1, filesize, f);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 fclose(f);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 deferred *def = NULL, *tmpd;
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
80 uint16_t offset = 0;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 for(uint8_t opt = 2; opt < argc; ++opt) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82 if (argv[opt][0] == '-') {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83 FILE * address_log;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 switch (argv[opt][1])
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85 {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 case 'l':
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87 labels = 1;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
88 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89 case 'a':
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 addr = 1;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
91 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 case 'o':
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
93 only = 1;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 case 'f':
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96 opt++;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
97 if (opt >= argc) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
98 fputs("-f must be followed by a filename\n", stderr);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
99 exit(1);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
101 address_log = fopen(argv[opt], "r");
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 if (!address_log) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 fprintf(stderr, "Failed to open %s for reading\n", argv[opt]);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 exit(1);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
106 while (fgets(disbuf, sizeof(disbuf), address_log)) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
107 if (disbuf[0]) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
108 uint16_t address = strtol(disbuf, NULL, 16);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
109 if (address) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
110 def = defer(address, def);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
111 reference(address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
113 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
114 }
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
115 break;
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
116 case 's':
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
117 opt++;
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
118 if (opt >= argc) {
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
119 fputs("-s must be followed by a start offset in hex\n", stderr);
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
120 exit(1);
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
121 }
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
122 offset = strtol(argv[opt], NULL, 16);
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
123 break;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
124 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 } else {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
126 uint16_t address = strtol(argv[opt], NULL, 16);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 def = defer(address, def);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
128 reference(address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 }
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
131 uint16_t start = offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
132 uint8_t *encoded, *next;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
133 uint32_t size;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134 if (!def || !only) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 def = defer(start, def);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
137 uint16_t address;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
138 while(def) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139 do {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140 encoded = NULL;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
141 address = def->address;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
142 if (!is_visited(address)) {
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
143 encoded = filebuf + address - offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
144 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 tmpd = def;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 def = def->next;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 free(tmpd);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
148 } while(def && encoded == NULL);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
149 if (!encoded) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
151 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
152 for(;;) {
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
153 if ((address - offset) > filesize || is_visited(address) || address < offset) {
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
154 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
155 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
156 visit(address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
157 next = z80_decode(encoded, &instbuf);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
158 address += (next-encoded);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
159 encoded = next;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
160
258
852b8765bba6 Fix infinite loop in Z80 disassembler
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
161 //z80_disasm(&instbuf, disbuf);
852b8765bba6 Fix infinite loop in Z80 disassembler
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
162 //printf("%X: %s\n", address, disbuf);
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
163 switch (instbuf.op)
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
165 case Z80_JR:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
166 address += instbuf.immed;
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
167 encoded = filebuf + address - offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
168 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 case Z80_JRCC:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 reference(address + instbuf.immed);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
171 def = defer(address + instbuf.immed, def);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 case Z80_JP:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 address = instbuf.immed;
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
175 encoded = filebuf + address - offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
176 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
177 case Z80_JPCC:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178 case Z80_CALL:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 case Z80_CALLCC:
313
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
180 case Z80_RST:
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
181 reference(instbuf.immed);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
182 def = defer(instbuf.immed, def);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
183 break;
313
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
184 default:
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
185 if (z80_is_terminal(&instbuf)) {
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
186 address = filesize + 1;
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
187 }
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
188 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
189 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
190 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
191 if (labels) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
192 for (address = filesize; address < (64*1024); address++) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 if (is_label(address)) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
194 printf("ADR_%X equ $%X\n", address, address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
195 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
196 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
197 puts("");
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
198 }
818
a634ed0a92cf Small fix to start offset support in Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 817
diff changeset
199 for (address = offset; address < filesize + offset; address++) {
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
200 if (is_visited(address)) {
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
201 encoded = filebuf + address - offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
202 z80_decode(encoded, &instbuf);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
203 if (labels) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
204 /*m68k_disasm_labels(&instbuf, disbuf);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
205 if (is_label(instbuf.address)) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
206 printf("ADR_%X:\n", instbuf.address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
207 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
208 if (addr) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
209 printf("\t%s\t;%X\n", disbuf, instbuf.address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
210 } else {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
211 printf("\t%s\n", disbuf);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
212 }*/
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
213 } else {
314
54c0e5f22198 Show absolute addresses for JR, JRCC and DJNZ in Z80 disassembler
Mike Pavone <pavone@retrodev.com>
parents: 313
diff changeset
214 z80_disasm(&instbuf, disbuf, address);
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
215 printf("%X: %s\n", address, disbuf);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
216 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
217 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
218 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
219 return 0;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
220 }