annotate dis.c @ 1470:1e3e0205640f

Add support for writeable ROM and an entry for Game no Kanzume Otokuyou using that support as it expects the cart area to be writable
author Michael Pavone <pavone@retrodev.com>
date Sat, 14 Oct 2017 05:03:38 -0700
parents c4bfbf55d418
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: 197
diff changeset
1 /*
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 197
diff changeset
2 Copyright 2013 Michael Pavone
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
3 This file is part of BlastEm.
467
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 197
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: 197
diff changeset
5 */
2
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include "68kinst.h"
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include <stdio.h>
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 #include <stdlib.h>
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
9 #include <string.h>
805
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
10 #include <stdarg.h>
947
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
11 #include <ctype.h>
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
12 #include "vos_program_module.h"
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
13 #include "tern.h"
947
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
14 #include "util.h"
2
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
16 uint8_t visited[(16*1024*1024)/16];
635
6992a1b07714 Fix logic for automatic label generation.
Michael Pavone <pavone@retrodev.com>
parents: 634
diff changeset
17 uint16_t label[(16*1024*1024)/8];
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
18
805
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
19 void fatal_error(char *format, ...)
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
20 {
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
21 va_list args;
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
22 va_start(args, format);
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
23 vfprintf(stderr, format, args);
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
24 va_end(args);
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
25 exit(1);
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
26 }
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
27
3eced113081c Pre-release cleanup
Michael Pavone <pavone@retrodev.com>
parents: 704
diff changeset
28
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
29 void visit(uint32_t address)
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
30 {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
31 address &= 0xFFFFFF;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
32 visited[address/16] |= 1 << ((address / 2) % 8);
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
33 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
34
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
35 void reference(uint32_t address)
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
36 {
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
37 address &= 0xFFFFFF;
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
38 //printf("referenced: %X\n", address);
635
6992a1b07714 Fix logic for automatic label generation.
Michael Pavone <pavone@retrodev.com>
parents: 634
diff changeset
39 label[address/16] |= 1 << (address % 16);
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
40 }
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
41
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
42 uint8_t is_visited(uint32_t address)
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
43 {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
44 address &= 0xFFFFFF;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
45 return visited[address/16] & (1 << ((address / 2) % 8));
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
46 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
47
635
6992a1b07714 Fix logic for automatic label generation.
Michael Pavone <pavone@retrodev.com>
parents: 634
diff changeset
48 uint16_t is_label(uint32_t address)
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
49 {
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
50 address &= 0xFFFFFF;
635
6992a1b07714 Fix logic for automatic label generation.
Michael Pavone <pavone@retrodev.com>
parents: 634
diff changeset
51 return label[address/16] & (1 << (address % 16));
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
52 }
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
53
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
54 typedef struct {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
55 uint32_t num_labels;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
56 uint32_t storage;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
57 char *labels[];
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
58 } label_names;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
59
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
60 tern_node * add_label(tern_node * head, char * name, uint32_t address)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
61 {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
62 char key[MAX_INT_KEY_SIZE];
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
63 address &= 0xFFFFFF;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
64 reference(address);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
65 tern_int_key(address, key);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
66 label_names * names = tern_find_ptr(head, key);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
67 if (names)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
68 {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
69 if (names->num_labels == names->storage)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
70 {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
71 names->storage = names->storage + (names->storage >> 1);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
72 names = realloc(names, sizeof(label_names) + names->storage * sizeof(char *));
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
73 }
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
74 } else {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
75 names = malloc(sizeof(label_names) + 4 * sizeof(char *));
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
76 names->num_labels = 0;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
77 names->storage = 4;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
78 head = tern_insert_ptr(head, key, names);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
79 }
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
80 names->labels[names->num_labels++] = strdup(name);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
81 return head;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
82 }
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
83
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
84 typedef struct deferred {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
85 uint32_t address;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
86 struct deferred *next;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
87 } deferred;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
88
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
89 deferred * defer(uint32_t address, deferred * next)
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
90 {
639
e263e1ac6038 Ignore odd addresses in calls to defer in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 635
diff changeset
91 if (is_visited(address) || address & 1) {
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
92 return next;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
93 }
111
8b50d2c975b2 Fix decoding of Scc
Mike Pavone <pavone@retrodev.com>
parents: 103
diff changeset
94 //printf("deferring %X\n", address);
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
95 deferred * d = malloc(sizeof(deferred));
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
96 d->address = address;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
97 d->next = next;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
98 return d;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
99 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
100
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
101 void check_reference(m68kinst * inst, m68k_op_info * op)
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
102 {
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
103 switch(op->addr_mode)
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
104 {
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
105 case MODE_PC_DISPLACE:
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
106 reference(inst->address + 2 + op->params.regs.displacement);
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
107 break;
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
108 case MODE_ABSOLUTE:
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
109 case MODE_ABSOLUTE_SHORT:
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
110 reference(op->params.immed);
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
111 break;
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
112 }
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
113 }
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
114
634
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
115 int label_fun(char *dst, uint32_t address, void * data)
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
116 {
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
117 tern_node * labels = data;
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
118 char key[MAX_INT_KEY_SIZE];
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
119 label_names * names = tern_find_ptr(labels, tern_int_key(address & 0xFFFFFF, key));
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
120 if (names)
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
121 {
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
122 return sprintf(dst, "%s", names->labels[0]);
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
123 } else {
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
124 return m68k_default_label_fun(dst, address, NULL);
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
125 }
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
126 }
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
127
947
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
128 char * strip_ws(char * text)
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
129 {
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
130 while (*text && (!isprint(*text) || isblank(*text)))
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
131 {
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
132 text++;
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
133 }
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
134 char * ret = text;
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
135 text = ret + strlen(ret) - 1;
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
136 while (text > ret && (!isprint(*text) || isblank(*text)))
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
137 {
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
138 *text = 0;
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
139 text--;
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
140 }
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
141 return ret;
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
142 }
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
143
2
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
144 int main(int argc, char ** argv)
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
145 {
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 long filesize;
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 unsigned short *filebuf;
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
148 char disbuf[1024];
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
149 m68kinst instbuf;
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 unsigned short * cur;
151
6b593ea0ed90 Implement MULU/MULS and DIVU/DIVS
Mike Pavone <pavone@retrodev.com>
parents: 148
diff changeset
151 deferred *def = NULL, *tmpd;
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
152
632
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
153 uint8_t labels = 0, addr = 0, only = 0, vos = 0, reset = 0;
947
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
154 tern_node * named_labels = NULL;
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
155
1078
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
156 uint32_t address_off = 0, address_end;
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
157 for(uint8_t opt = 2; opt < argc; ++opt) {
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
158 if (argv[opt][0] == '-') {
197
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
159 FILE * address_log;
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
160 switch (argv[opt][1])
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
161 {
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
162 case 'l':
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
163 labels = 1;
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
164 break;
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
165 case 'a':
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
166 addr = 1;
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
167 break;
151
6b593ea0ed90 Implement MULU/MULS and DIVU/DIVS
Mike Pavone <pavone@retrodev.com>
parents: 148
diff changeset
168 case 'o':
6b593ea0ed90 Implement MULU/MULS and DIVU/DIVS
Mike Pavone <pavone@retrodev.com>
parents: 148
diff changeset
169 only = 1;
6b593ea0ed90 Implement MULU/MULS and DIVU/DIVS
Mike Pavone <pavone@retrodev.com>
parents: 148
diff changeset
170 break;
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
171 case 'v':
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
172 vos = 1;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
173 break;
632
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
174 case 'r':
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
175 reset = 1;
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
176 break;
1078
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
177 case 's':
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
178 opt++;
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
179 if (opt >= argc) {
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
180 fputs("-s must be followed by an offset\n", stderr);
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
181 exit(1);
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
182 }
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
183 address_off = strtol(argv[opt], NULL, 0);
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
184 break;
197
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
185 case 'f':
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
186 opt++;
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
187 if (opt >= argc) {
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
188 fputs("-f must be followed by a filename\n", stderr);
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
189 exit(1);
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
190 }
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
191 address_log = fopen(argv[opt], "r");
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
192 if (!address_log) {
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
193 fprintf(stderr, "Failed to open %s for reading\n", argv[opt]);
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
194 exit(1);
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
195 }
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
196 while (fgets(disbuf, sizeof(disbuf), address_log)) {
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
197 if (disbuf[0]) {
947
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
198 char *end;
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
199 uint32_t address = strtol(disbuf, &end, 16);
197
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
200 if (address) {
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
201 def = defer(address, def);
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
202 reference(address);
947
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
203 if (*end == '=') {
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
204 named_labels = add_label(named_labels, strip_ws(end+1), address);
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
205 }
197
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
206 }
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
207 }
7c227a8ec53d Add instruction address logging to translator and support for reading an address log to the disassembler
Mike Pavone <pavone@retrodev.com>
parents: 164
diff changeset
208 }
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
209 }
151
6b593ea0ed90 Implement MULU/MULS and DIVU/DIVS
Mike Pavone <pavone@retrodev.com>
parents: 148
diff changeset
210 } else {
947
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
211 char *end;
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
212 uint32_t address = strtol(argv[opt], &end, 16);
151
6b593ea0ed90 Implement MULU/MULS and DIVU/DIVS
Mike Pavone <pavone@retrodev.com>
parents: 148
diff changeset
213 def = defer(address, def);
6b593ea0ed90 Implement MULU/MULS and DIVU/DIVS
Mike Pavone <pavone@retrodev.com>
parents: 148
diff changeset
214 reference(address);
947
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
215 if (*end == '=') {
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
216 named_labels = add_label(named_labels, end+1, address);
d29722e3359c Allow specification of named labels as command line arguments to the 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 805
diff changeset
217 }
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
218 }
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
219 }
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
220 FILE * f = fopen(argv[1], "rb");
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
221 fseek(f, 0, SEEK_END);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
222 filesize = ftell(f);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
223 fseek(f, 0, SEEK_SET);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
224
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
225 char int_key[MAX_INT_KEY_SIZE];
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
226 if (vos)
2
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
227 {
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
228 vos_program_module header;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
229 vos_read_header(f, &header);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
230 vos_read_alloc_module_map(f, &header);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
231 address_off = header.user_boundary;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
232 address_end = address_off + filesize - 0x1000;
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
233 def = defer(header.main_entry_link.code_address, def);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
234 named_labels = add_label(named_labels, "main_entry_link", header.main_entry_link.code_address);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
235 for (int i = 0; i < header.n_modules; i++)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
236 {
632
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
237 if (!reset || header.module_map_entries[i].code_address != header.user_boundary)
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
238 {
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
239 def = defer(header.module_map_entries[i].code_address, def);
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
240 }
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
241 named_labels = add_label(named_labels, header.module_map_entries[i].name.str, header.module_map_entries[i].code_address);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
242 }
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
243 fseek(f, 0x1000, SEEK_SET);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
244 filebuf = malloc(filesize - 0x1000);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
245 if (fread(filebuf, 2, (filesize - 0x1000)/2, f) != (filesize - 0x1000)/2)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
246 {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
247 fprintf(stderr, "Failure while reading file %s\n", argv[1]);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
248 }
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
249 fclose(f);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
250 for(cur = filebuf; cur - filebuf < ((filesize - 0x1000)/2); ++cur)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
251 {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
252 *cur = (*cur >> 8) | (*cur << 8);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
253 }
632
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
254 if (reset)
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
255 {
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
256 def = defer(filebuf[2] << 16 | filebuf[3], def);
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
257 named_labels = add_label(named_labels, "reset", filebuf[2] << 16 | filebuf[3]);
80e111b48d4b Add -r option to indicate VOS program module contains a 68K reset vector
Michael Pavone <pavone@retrodev.com>
parents: 631
diff changeset
258 }
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
259 } else {
1078
c4bfbf55d418 Added some hacky support for specifying a start offset in 68K disassembler
Michael Pavone <pavone@retrodev.com>
parents: 947
diff changeset
260 address_end = address_off + filesize;
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
261 filebuf = malloc(filesize);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
262 if (fread(filebuf, 2, filesize/2, f) != filesize/2)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
263 {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
264 fprintf(stderr, "Failure while reading file %s\n", argv[1]);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
265 }
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
266 fclose(f);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
267 for(cur = filebuf; cur - filebuf < (filesize/2); ++cur)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
268 {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
269 *cur = (*cur >> 8) | (*cur << 8);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
270 }
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
271 uint32_t start = filebuf[2] << 16 | filebuf[3];
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
272 uint32_t int_2 = filebuf[0x68/2] << 16 | filebuf[0x6A/2];
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
273 uint32_t int_4 = filebuf[0x70/2] << 16 | filebuf[0x72/2];
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
274 uint32_t int_6 = filebuf[0x78/2] << 16 | filebuf[0x7A/2];
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
275 named_labels = add_label(named_labels, "start", start);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
276 named_labels = add_label(named_labels, "int_2", int_2);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
277 named_labels = add_label(named_labels, "int_4", int_4);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
278 named_labels = add_label(named_labels, "int_6", int_6);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
279 if (!def || !only) {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
280 def = defer(start, def);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
281 def = defer(int_2, def);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
282 def = defer(int_4, def);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
283 def = defer(int_6, def);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
284 }
2
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
285 }
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
286 uint16_t *encoded, *next;
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
287 uint32_t size, tmp_addr;
139
cce22fb4c450 Properly support references to odd addresses in label generation in disassembler. Add labels for start and interrupts.
Mike Pavone <pavone@retrodev.com>
parents: 134
diff changeset
288 uint32_t address;
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
289 while(def) {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
290 do {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
291 encoded = NULL;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
292 address = def->address;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
293 if (!is_visited(address)) {
633
a759f4e38488 Fix translation of 68K address to buffer location when address_off != 0
Michael Pavone <pavone@retrodev.com>
parents: 632
diff changeset
294 encoded = filebuf + (address - address_off)/2;
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
295 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
296 tmpd = def;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
297 def = def->next;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
298 free(tmpd);
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
299 } while(def && encoded == NULL);
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
300 if (!encoded) {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
301 break;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
302 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
303 for(;;) {
633
a759f4e38488 Fix translation of 68K address to buffer location when address_off != 0
Michael Pavone <pavone@retrodev.com>
parents: 632
diff changeset
304 if (address > address_end || address < address_off) {
48
0bdda50c7364 Don't try to disassemble addresses beyond the end of the cartridge
Mike Pavone <pavone@retrodev.com>
parents: 47
diff changeset
305 break;
0bdda50c7364 Don't try to disassemble addresses beyond the end of the cartridge
Mike Pavone <pavone@retrodev.com>
parents: 47
diff changeset
306 }
47
4b6c667326a1 Fix bug in address visitation in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 44
diff changeset
307 visit(address);
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
308 next = m68k_decode(encoded, &instbuf, address);
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
309 address += (next-encoded)*2;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
310 encoded = next;
111
8b50d2c975b2 Fix decoding of Scc
Mike Pavone <pavone@retrodev.com>
parents: 103
diff changeset
311 //m68k_disasm(&instbuf, disbuf);
8b50d2c975b2 Fix decoding of Scc
Mike Pavone <pavone@retrodev.com>
parents: 103
diff changeset
312 //printf("%X: %s\n", instbuf.address, disbuf);
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
313 check_reference(&instbuf, &(instbuf.src));
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
314 check_reference(&instbuf, &(instbuf.dst));
148
4a400aec81bb Bail out of disassembly of a particular stream when we hit an invalid instruction
Mike Pavone <pavone@retrodev.com>
parents: 139
diff changeset
315 if (instbuf.op == M68K_ILLEGAL || instbuf.op == M68K_RTS || instbuf.op == M68K_RTE || instbuf.op == M68K_INVALID) {
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
316 break;
164
afbfb0ac0256 Small fix to disassembler
Mike Pavone <pavone@retrodev.com>
parents: 151
diff changeset
317 }
afbfb0ac0256 Small fix to disassembler
Mike Pavone <pavone@retrodev.com>
parents: 151
diff changeset
318 if (instbuf.op == M68K_BCC || instbuf.op == M68K_DBCC || instbuf.op == M68K_BSR) {
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
319 if (instbuf.op == M68K_BCC && instbuf.extra.cond == COND_TRUE) {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
320 address = instbuf.address + 2 + instbuf.src.params.immed;
633
a759f4e38488 Fix translation of 68K address to buffer location when address_off != 0
Michael Pavone <pavone@retrodev.com>
parents: 632
diff changeset
321 encoded = filebuf + (address - address_off)/2;
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
322 reference(address);
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
323 if (is_visited(address)) {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
324 break;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
325 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
326 } else {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
327 tmp_addr = instbuf.address + 2 + instbuf.src.params.immed;
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
328 reference(tmp_addr);
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
329 def = defer(tmp_addr, def);
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
330 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
331 } else if(instbuf.op == M68K_JMP) {
80
7b1e16e981ef Fix bug in disassembler that caused it to disassemble addresses it shouldn't
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
332 if (instbuf.src.addr_mode == MODE_ABSOLUTE || instbuf.src.addr_mode == MODE_ABSOLUTE_SHORT) {
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
333 address = instbuf.src.params.immed;
633
a759f4e38488 Fix translation of 68K address to buffer location when address_off != 0
Michael Pavone <pavone@retrodev.com>
parents: 632
diff changeset
334 encoded = filebuf + (address - address_off)/2;
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
335 if (is_visited(address)) {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
336 break;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
337 }
564
316facea756d Fix PC displacement mode check in disassembler so that jmps involving a register are handled correctly
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
338 } else if (instbuf.src.addr_mode == MODE_PC_DISPLACE) {
114
e821b6fde0e4 Allow jmp/jsr to follow pc-relative addresses in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 111
diff changeset
339 address = instbuf.src.params.regs.displacement + instbuf.address + 2;
633
a759f4e38488 Fix translation of 68K address to buffer location when address_off != 0
Michael Pavone <pavone@retrodev.com>
parents: 632
diff changeset
340 encoded = filebuf + (address - address_off)/2;
114
e821b6fde0e4 Allow jmp/jsr to follow pc-relative addresses in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 111
diff changeset
341 if (is_visited(address)) {
e821b6fde0e4 Allow jmp/jsr to follow pc-relative addresses in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 111
diff changeset
342 break;
e821b6fde0e4 Allow jmp/jsr to follow pc-relative addresses in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 111
diff changeset
343 }
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
344 } else {
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
345 break;
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
346 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
347 } else if(instbuf.op == M68K_JSR) {
80
7b1e16e981ef Fix bug in disassembler that caused it to disassemble addresses it shouldn't
Mike Pavone <pavone@retrodev.com>
parents: 48
diff changeset
348 if (instbuf.src.addr_mode == MODE_ABSOLUTE || instbuf.src.addr_mode == MODE_ABSOLUTE_SHORT) {
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
349 def = defer(instbuf.src.params.immed, def);
164
afbfb0ac0256 Small fix to disassembler
Mike Pavone <pavone@retrodev.com>
parents: 151
diff changeset
350 } else if (instbuf.src.addr_mode == MODE_PC_DISPLACE) {
114
e821b6fde0e4 Allow jmp/jsr to follow pc-relative addresses in disassembler
Mike Pavone <pavone@retrodev.com>
parents: 111
diff changeset
351 def = defer(instbuf.src.params.regs.displacement + instbuf.address + 2, def);
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
352 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
353 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
354 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
355 }
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
356 if (labels) {
640
c806b96b86eb Add equates for address references below address_off
Michael Pavone <pavone@retrodev.com>
parents: 639
diff changeset
357 for (address = 0; address < address_off; address++) {
c806b96b86eb Add equates for address references below address_off
Michael Pavone <pavone@retrodev.com>
parents: 639
diff changeset
358 if (is_label(address)) {
c806b96b86eb Add equates for address references below address_off
Michael Pavone <pavone@retrodev.com>
parents: 639
diff changeset
359 printf("ADR_%X equ $%X\n", address, address);
c806b96b86eb Add equates for address references below address_off
Michael Pavone <pavone@retrodev.com>
parents: 639
diff changeset
360 }
c806b96b86eb Add equates for address references below address_off
Michael Pavone <pavone@retrodev.com>
parents: 639
diff changeset
361 }
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
362 for (address = filesize; address < (16*1024*1024); address++) {
704
1a14f5f6c6a1 Properly print equ for named labels that point outside the cartridge area in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 652
diff changeset
363 char key[MAX_INT_KEY_SIZE];
1a14f5f6c6a1 Properly print equ for named labels that point outside the cartridge area in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 652
diff changeset
364 tern_int_key(address, key);
1a14f5f6c6a1 Properly print equ for named labels that point outside the cartridge area in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 652
diff changeset
365 label_names *names = tern_find_ptr(named_labels, key);
1a14f5f6c6a1 Properly print equ for named labels that point outside the cartridge area in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 652
diff changeset
366 if (names) {
1a14f5f6c6a1 Properly print equ for named labels that point outside the cartridge area in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 652
diff changeset
367 for (int i = 0; i < names->num_labels; i++)
1a14f5f6c6a1 Properly print equ for named labels that point outside the cartridge area in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 652
diff changeset
368 {
1a14f5f6c6a1 Properly print equ for named labels that point outside the cartridge area in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 652
diff changeset
369 printf("%s equ $%X\n", names->labels[i], address);
1a14f5f6c6a1 Properly print equ for named labels that point outside the cartridge area in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 652
diff changeset
370 }
1a14f5f6c6a1 Properly print equ for named labels that point outside the cartridge area in disassembler
Michael Pavone <pavone@retrodev.com>
parents: 652
diff changeset
371 } else if (is_label(address)) {
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
372 printf("ADR_%X equ $%X\n", address, address);
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
373 }
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
374 }
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
375 puts("");
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
376 }
633
a759f4e38488 Fix translation of 68K address to buffer location when address_off != 0
Michael Pavone <pavone@retrodev.com>
parents: 632
diff changeset
377 for (address = address_off; address < address_end; address+=2) {
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
378 if (is_visited(address)) {
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
379 encoded = filebuf + (address-address_off)/2;
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
380 m68k_decode(encoded, &instbuf, address);
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
381 if (labels) {
634
4a6ec64acd79 Better support for labels sourced from VOS program module header
Michael Pavone <pavone@retrodev.com>
parents: 633
diff changeset
382 m68k_disasm_labels(&instbuf, disbuf, label_fun, named_labels);
631
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
383 char keybuf[MAX_INT_KEY_SIZE];
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
384 label_names * names = tern_find_ptr(named_labels, tern_int_key(address, keybuf));
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
385 if (names)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
386 {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
387 for (int i = 0; i < names->num_labels; i++)
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
388 {
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
389 printf("%s:\n", names->labels[i]);
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
390 }
de6f00204fa2 Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
391 } else if (is_label(instbuf.address)) {
134
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
392 printf("ADR_%X:\n", instbuf.address);
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
393 }
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
394 if (addr) {
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
395 printf("\t%s\t;%X\n", disbuf, instbuf.address);
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
396 } else {
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
397 printf("\t%s\n", disbuf);
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
398 }
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
399 } else {
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
400 m68k_disasm(&instbuf, disbuf);
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
401 printf("%X: %s\n", instbuf.address, disbuf);
ab50421b1b7a Improve disassembler
Mike Pavone <pavone@retrodev.com>
parents: 114
diff changeset
402 }
44
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
403 }
ec71370820f2 Add logic for following control flow based on logic in the translator
Mike Pavone <pavone@retrodev.com>
parents: 20
diff changeset
404 }
2
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
405 return 0;
5df303bf72e6 Improve 68K instruction decoding. Add simple disassembler.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
406 }