annotate zdis.c @ 1913:2c742812bcbb

Fix regression at the very start of The Revenge of Shinobi
author Michael Pavone <pavone@retrodev.com>
date Wed, 01 Apr 2020 23:15:47 -0700
parents 137dbd05ceab
children 92dc3e24f309
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 }
1594
137dbd05ceab Fix some issues identified by cppcheck
Michael Pavone <pavone@retrodev.com>
parents: 818
diff changeset
115 fclose(address_log);
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
116 break;
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
117 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
118 opt++;
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
119 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
120 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
121 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
122 }
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
123 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
124 break;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
125 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
126 } else {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
127 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
128 def = defer(address, def);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
129 reference(address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
130 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
131 }
817
e95978c0988c Added a -s paramter for specifying a start offset in the Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
132 uint16_t start = offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
133 uint8_t *encoded, *next;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134 uint32_t size;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
135 if (!def || !only) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
136 def = defer(start, def);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
137 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
138 uint16_t address;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
139 while(def) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
140 do {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
141 encoded = NULL;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
142 address = def->address;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
143 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
144 encoded = filebuf + address - offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 tmpd = def;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 def = def->next;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
148 free(tmpd);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
149 } while(def && encoded == NULL);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 if (!encoded) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
151 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
152 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
153 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
154 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
155 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
156 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
157 visit(address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
158 next = z80_decode(encoded, &instbuf);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
159 address += (next-encoded);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
160 encoded = next;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
161
258
852b8765bba6 Fix infinite loop in Z80 disassembler
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
162 //z80_disasm(&instbuf, disbuf);
852b8765bba6 Fix infinite loop in Z80 disassembler
Mike Pavone <pavone@retrodev.com>
parents: 199
diff changeset
163 //printf("%X: %s\n", address, disbuf);
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
164 switch (instbuf.op)
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
165 {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
166 case Z80_JR:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
167 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
168 encoded = filebuf + address - offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
169 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
170 case Z80_JRCC:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
171 reference(address + instbuf.immed);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
172 def = defer(address + instbuf.immed, def);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
173 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
174 case Z80_JP:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
175 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
176 encoded = filebuf + address - offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
177 break;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
178 case Z80_JPCC:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
179 case Z80_CALL:
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
180 case Z80_CALLCC:
313
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
181 case Z80_RST:
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
182 reference(instbuf.immed);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
183 def = defer(instbuf.immed, def);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
184 break;
313
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
185 default:
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
186 if (z80_is_terminal(&instbuf)) {
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
187 address = filesize + 1;
a13329645ea3 Fix terminal instruction detection in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 258
diff changeset
188 }
199
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 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
192 if (labels) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
193 for (address = filesize; address < (64*1024); address++) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
194 if (is_label(address)) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
195 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
196 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
197 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
198 puts("");
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
199 }
818
a634ed0a92cf Small fix to start offset support in Z80 disassembler
Michael Pavone <pavone@retrodev.com>
parents: 817
diff changeset
200 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
201 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
202 encoded = filebuf + address - offset;
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
203 z80_decode(encoded, &instbuf);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
204 if (labels) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
205 /*m68k_disasm_labels(&instbuf, disbuf);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
206 if (is_label(instbuf.address)) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
207 printf("ADR_%X:\n", instbuf.address);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
208 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
209 if (addr) {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
210 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
211 } else {
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
212 printf("\t%s\n", disbuf);
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
213 }*/
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
214 } else {
314
54c0e5f22198 Show absolute addresses for JR, JRCC and DJNZ in Z80 disassembler
Mike Pavone <pavone@retrodev.com>
parents: 313
diff changeset
215 z80_disasm(&instbuf, disbuf, address);
199
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
216 printf("%X: %s\n", address, disbuf);
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 }
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
220 return 0;
69585e7d474f Add initial stab at Z80 decoder and disassembler
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
221 }