comparison dis.c @ 44:ec71370820f2

Add logic for following control flow based on logic in the translator
author Mike Pavone <pavone@retrodev.com>
date Wed, 12 Dec 2012 20:17:11 -0800
parents f664eeb55cb4
children 4b6c667326a1
comparison
equal deleted inserted replaced
43:3fc57e1a2c56 44:ec71370820f2
1 #include "68kinst.h" 1 #include "68kinst.h"
2 #include <stdio.h> 2 #include <stdio.h>
3 #include <stdlib.h> 3 #include <stdlib.h>
4
5 uint8_t visited[(16*1024*1024)/16];
6
7 void visit(uint32_t address)
8 {
9 address &= 0xFFFFFF;
10 visited[address/16] |= 1 << ((address / 2) % 8);
11 }
12
13 uint8_t is_visited(uint32_t address)
14 {
15 address &= 0xFFFFFF;
16 return visited[address/16] & (1 << ((address / 2) % 8));
17 }
18
19 typedef struct deferred {
20 uint32_t address;
21 struct deferred *next;
22 } deferred;
23
24 deferred * defer(uint32_t address, deferred * next)
25 {
26 if (is_visited(address)) {
27 return next;
28 }
29 deferred * d = malloc(sizeof(deferred));
30 d->address = address;
31 d->next = next;
32 return d;
33 }
34
35 #define SIMPLE 0
4 36
5 int main(int argc, char ** argv) 37 int main(int argc, char ** argv)
6 { 38 {
7 long filesize; 39 long filesize;
8 unsigned short *filebuf; 40 unsigned short *filebuf;
18 fclose(f); 50 fclose(f);
19 for(cur = filebuf; cur - filebuf < (filesize/2); ++cur) 51 for(cur = filebuf; cur - filebuf < (filesize/2); ++cur)
20 { 52 {
21 *cur = (*cur >> 8) | (*cur << 8); 53 *cur = (*cur >> 8) | (*cur << 8);
22 } 54 }
55 uint32_t address = filebuf[2] << 16 | filebuf[3], tmp_addr;
56 #if !SIMPLE
57 uint16_t *encoded, *next;
58 uint32_t size;
59 deferred *def = NULL, *tmpd;
60 def = defer(address, def);
61 def = defer(filebuf[0x68/2] << 16 | filebuf[0x6A/2], def);
62 def = defer(filebuf[0x70/2] << 16 | filebuf[0x72/2], def);
63 def = defer(filebuf[0x78/2] << 16 | filebuf[0x7A/2], def);
64 while(def) {
65 do {
66 encoded = NULL;
67 address = def->address;
68 if (!is_visited(address)) {
69 encoded = filebuf + address/2;
70 }
71 tmpd = def;
72 def = def->next;
73 free(tmpd);
74 } while(def && encoded == NULL);
75 if (!encoded) {
76 break;
77 }
78 for(;;) {
79 visit(instbuf.address);
80 next = m68k_decode(encoded, &instbuf, address);
81 address += (next-encoded)*2;
82 encoded = next;
83 m68k_disasm(&instbuf, disbuf);
84 if (instbuf.op == M68K_ILLEGAL || instbuf.op == M68K_RTS || instbuf.op == M68K_RTE) {
85 break;
86 } else if (instbuf.op == M68K_BCC || instbuf.op == M68K_DBCC || instbuf.op == M68K_BSR) {
87 if (instbuf.op == M68K_BCC && instbuf.extra.cond == COND_TRUE) {
88 address = instbuf.address + 2 + instbuf.src.params.immed;
89 encoded = filebuf + address/2;
90 if (is_visited(address)) {
91 break;
92 }
93 } else {
94 tmp_addr = instbuf.address + 2 + instbuf.src.params.immed;
95 def = defer(tmp_addr, def);
96 }
97 } else if(instbuf.op == M68K_JMP) {
98 if (instbuf.src.addr_mode == MODE_ABSOLUTE || MODE_ABSOLUTE_SHORT) {
99 address = instbuf.src.params.immed;
100 encoded = filebuf + address/2;
101 if (is_visited(address)) {
102 break;
103 }
104 } else {
105 break;
106 }
107 } else if(instbuf.op == M68K_JSR) {
108 if (instbuf.src.addr_mode == MODE_ABSOLUTE || MODE_ABSOLUTE_SHORT) {
109 def = defer(instbuf.src.params.immed, def);
110 }
111 }
112 }
113 }
114 for (address = 0; address < filesize; address+=2) {
115 if (is_visited(address)) {
116 encoded = filebuf + address/2;
117 m68k_decode(encoded, &instbuf, address);
118 m68k_disasm(&instbuf, disbuf);
119 printf("%X: %s\n", instbuf.address, disbuf);
120 }
121 }
122 #else
23 for(cur = filebuf + 0x100; (cur - filebuf) < (filesize/2); ) 123 for(cur = filebuf + 0x100; (cur - filebuf) < (filesize/2); )
24 { 124 {
25 //printf("cur: %p: %x\n", cur, *cur); 125 //printf("cur: %p: %x\n", cur, *cur);
26 unsigned short * start = cur; 126 unsigned short * start = cur;
27 cur = m68k_decode(cur, &instbuf, (start - filebuf)*2); 127 cur = m68k_decode(cur, &instbuf, (start - filebuf)*2);
28 m68k_disasm(&instbuf, disbuf); 128 m68k_disasm(&instbuf, disbuf);
29 printf("%X: %s\n", instbuf.address, disbuf); 129 printf("%X: %s\n", instbuf.address, disbuf);
30 } 130 }
131 #endif
31 return 0; 132 return 0;
32 } 133 }