1 |
/* |
2 |
* UAE - The Un*x Amiga Emulator |
3 |
* |
4 |
* Read 68000 CPU specs from file "table68k" and build table68k.c |
5 |
* |
6 |
* Copyright 1995,1996 Bernd Schmidt |
7 |
*/ |
8 |
|
9 |
#include <assert.h> |
10 |
#include <ctype.h> |
11 |
#include <stdio.h> |
12 |
|
13 |
#include "sysdeps.h" |
14 |
#include "readcpu.h" |
15 |
|
16 |
static FILE *tablef; |
17 |
static int nextch = 0; |
18 |
|
19 |
static void getnextch(void) |
20 |
{ |
21 |
do { |
22 |
nextch = fgetc(tablef); |
23 |
if (nextch == '%') { |
24 |
do { |
25 |
nextch = fgetc(tablef); |
26 |
} while (nextch != EOF && nextch != '\n'); |
27 |
} |
28 |
} while (nextch != EOF && isspace(nextch)); |
29 |
} |
30 |
|
31 |
static int nextchtohex(void) |
32 |
{ |
33 |
switch (isupper (nextch) ? tolower (nextch) : nextch) { |
34 |
case '0': return 0; |
35 |
case '1': return 1; |
36 |
case '2': return 2; |
37 |
case '3': return 3; |
38 |
case '4': return 4; |
39 |
case '5': return 5; |
40 |
case '6': return 6; |
41 |
case '7': return 7; |
42 |
case '8': return 8; |
43 |
case '9': return 9; |
44 |
case 'a': return 10; |
45 |
case 'b': return 11; |
46 |
case 'c': return 12; |
47 |
case 'd': return 13; |
48 |
case 'e': return 14; |
49 |
case 'f': return 15; |
50 |
default: abort(); |
51 |
} |
52 |
} |
53 |
|
54 |
int main(int argc, char **argv) |
55 |
{ |
56 |
int no_insns = 0; |
57 |
|
58 |
printf ("#include \"sysdeps.h\"\n"); |
59 |
printf ("#include \"readcpu.h\"\n"); |
60 |
printf ("struct instr_def defs68k[] = {\n"); |
61 |
#ifdef WIN32 |
62 |
tablef = fopen(argc > 1 ? argv[1] : "table68k","r"); |
63 |
if (tablef == NULL) { |
64 |
fprintf(stderr, "table68k not found\n"); |
65 |
exit(1); |
66 |
} |
67 |
#else |
68 |
tablef = stdin; |
69 |
#endif |
70 |
getnextch(); |
71 |
while (nextch != EOF) { |
72 |
int cpulevel, plevel, sduse; |
73 |
int i; |
74 |
|
75 |
char patbits[16]; |
76 |
char opcstr[256]; |
77 |
int bitpos[16]; |
78 |
int flagset[5], flaguse[5]; |
79 |
char cflow; |
80 |
|
81 |
unsigned int bitmask,bitpattern; |
82 |
int n_variable; |
83 |
|
84 |
n_variable = 0; |
85 |
bitmask = bitpattern = 0; |
86 |
memset (bitpos, 0, sizeof(bitpos)); |
87 |
for(i=0; i<16; i++) { |
88 |
int currbit; |
89 |
bitmask <<= 1; |
90 |
bitpattern <<= 1; |
91 |
|
92 |
switch (nextch) { |
93 |
case '0': currbit = bit0; bitmask |= 1; break; |
94 |
case '1': currbit = bit1; bitmask |= 1; bitpattern |= 1; break; |
95 |
case 'c': currbit = bitc; break; |
96 |
case 'C': currbit = bitC; break; |
97 |
case 'f': currbit = bitf; break; |
98 |
case 'i': currbit = biti; break; |
99 |
case 'I': currbit = bitI; break; |
100 |
case 'j': currbit = bitj; break; |
101 |
case 'J': currbit = bitJ; break; |
102 |
case 'k': currbit = bitk; break; |
103 |
case 'K': currbit = bitK; break; |
104 |
case 's': currbit = bits; break; |
105 |
case 'S': currbit = bitS; break; |
106 |
case 'd': currbit = bitd; break; |
107 |
case 'D': currbit = bitD; break; |
108 |
case 'r': currbit = bitr; break; |
109 |
case 'R': currbit = bitR; break; |
110 |
case 'z': currbit = bitz; break; |
111 |
case 'E': currbit = bitE; break; |
112 |
case 'p': currbit = bitp; break; |
113 |
default: abort(); |
114 |
} |
115 |
if (!(bitmask & 1)) { |
116 |
bitpos[n_variable] = currbit; |
117 |
n_variable++; |
118 |
} |
119 |
|
120 |
if (nextch == '0' || nextch == '1') |
121 |
bitmask |= 1; |
122 |
if (nextch == '1') |
123 |
bitpattern |= 1; |
124 |
patbits[i] = nextch; |
125 |
getnextch(); |
126 |
} |
127 |
|
128 |
while (isspace(nextch) || nextch == ':') /* Get CPU and privilege level */ |
129 |
getnextch(); |
130 |
|
131 |
switch (nextch) { |
132 |
case '0': cpulevel = 0; break; |
133 |
case '1': cpulevel = 1; break; |
134 |
case '2': cpulevel = 2; break; |
135 |
case '3': cpulevel = 3; break; |
136 |
case '4': cpulevel = 4; break; |
137 |
case '5': cpulevel = 5; break; |
138 |
default: abort(); |
139 |
} |
140 |
getnextch(); |
141 |
|
142 |
switch (nextch) { |
143 |
case '0': plevel = 0; break; |
144 |
case '1': plevel = 1; break; |
145 |
case '2': plevel = 2; break; |
146 |
case '3': plevel = 3; break; |
147 |
default: abort(); |
148 |
} |
149 |
getnextch(); |
150 |
|
151 |
while (isspace(nextch)) /* Get flag set information */ |
152 |
getnextch(); |
153 |
|
154 |
if (nextch != ':') |
155 |
abort(); |
156 |
|
157 |
for(i = 0; i < 5; i++) { |
158 |
getnextch(); |
159 |
switch(nextch){ |
160 |
case '-': flagset[i] = fa_unset; break; |
161 |
case '0': flagset[i] = fa_zero; break; |
162 |
case '1': flagset[i] = fa_one; break; |
163 |
case 'x': flagset[i] = fa_dontcare; break; |
164 |
case '?': flagset[i] = fa_unknown; break; |
165 |
default: flagset[i] = fa_set; break; |
166 |
} |
167 |
} |
168 |
|
169 |
getnextch(); |
170 |
while (isspace(nextch)) |
171 |
getnextch(); |
172 |
|
173 |
if (nextch != ':') /* Get flag used information */ |
174 |
abort(); |
175 |
|
176 |
for(i = 0; i < 5; i++) { |
177 |
getnextch(); |
178 |
switch(nextch){ |
179 |
case '-': flaguse[i] = fu_unused; break; |
180 |
case '?': flaguse[i] = fu_unknown; break; |
181 |
default: flaguse[i] = fu_used; break; |
182 |
} |
183 |
} |
184 |
|
185 |
getnextch(); |
186 |
while (isspace(nextch)) |
187 |
getnextch(); |
188 |
|
189 |
if (nextch != ':') /* Get control flow information */ |
190 |
abort(); |
191 |
|
192 |
cflow = 0; |
193 |
for(i = 0; i < 2; i++) { |
194 |
getnextch(); |
195 |
switch(nextch){ |
196 |
case '-': break; |
197 |
case 'R': cflow |= fl_return; break; |
198 |
case 'B': cflow |= fl_branch; break; |
199 |
case 'J': cflow |= fl_jump; break; |
200 |
case 'T': cflow |= fl_trap; break; |
201 |
default: abort(); |
202 |
} |
203 |
} |
204 |
|
205 |
getnextch(); |
206 |
while (isspace(nextch)) |
207 |
getnextch(); |
208 |
|
209 |
if (nextch != ':') /* Get source/dest usage information */ |
210 |
abort(); |
211 |
|
212 |
getnextch(); |
213 |
sduse = nextchtohex() << 4; |
214 |
getnextch(); |
215 |
sduse |= nextchtohex(); |
216 |
|
217 |
getnextch(); |
218 |
while (isspace(nextch)) |
219 |
getnextch(); |
220 |
|
221 |
if (nextch != ':') |
222 |
abort(); |
223 |
|
224 |
fgets(opcstr, 250, tablef); |
225 |
getnextch(); |
226 |
{ |
227 |
int j; |
228 |
/* Remove superfluous spaces from the string */ |
229 |
char *opstrp = opcstr, *osendp; |
230 |
int slen = 0; |
231 |
|
232 |
while (isspace(*opstrp)) |
233 |
opstrp++; |
234 |
|
235 |
osendp = opstrp; |
236 |
while (*osendp) { |
237 |
if (!isspace (*osendp)) |
238 |
slen = osendp - opstrp + 1; |
239 |
osendp++; |
240 |
} |
241 |
opstrp[slen] = 0; |
242 |
|
243 |
if (no_insns > 0) |
244 |
printf(",\n"); |
245 |
no_insns++; |
246 |
printf("{ %d, %d, {", bitpattern, n_variable); |
247 |
for (j = 0; j < 16; j++) { |
248 |
printf("%d", bitpos[j]); |
249 |
if (j < 15) |
250 |
printf(","); |
251 |
} |
252 |
printf ("}, %d, %d, %d, { ", bitmask, cpulevel, plevel); |
253 |
for(i = 0; i < 5; i++) { |
254 |
printf("{ %d, %d }%c ", flaguse[i], flagset[i], i == 4 ? ' ' : ','); |
255 |
} |
256 |
printf("}, %d, %d, \"%s\"}", cflow, sduse, opstrp); |
257 |
} |
258 |
} |
259 |
printf("};\nint n_defs68k = %d;\n", no_insns); |
260 |
fflush(stdout); |
261 |
return 0; |
262 |
} |