cc68a136 |
1 | \r |
2 | #include "app.h"\r |
3 | \r |
4 | static FILE *AsmFile=NULL;\r |
5 | \r |
6 | static int CycloneVer=0x0086; // Version number of library\r |
7 | int *CyJump=NULL; // Jump table\r |
8 | int ms=USE_MS_SYNTAX; // If non-zero, output in Microsoft ARMASM format\r |
9 | char *Narm[4]={ "b", "h","",""}; // Normal ARM Extensions for operand sizes 0,1,2\r |
10 | char *Sarm[4]={"sb","sh","",""}; // Sign-extend ARM Extensions for operand sizes 0,1,2\r |
11 | int Cycles; // Current cycles for opcode\r |
12 | \r |
13 | \r |
14 | void ot(const char *format, ...)\r |
15 | {\r |
16 | va_list valist=NULL;\r |
17 | int i, len;\r |
18 | \r |
19 | // notaz: stop me from leaving newlines in the middle of format string\r |
20 | // and generating bad code\r |
21 | for(i=0, len=strlen(format); i < len && format[i] != '\n'; i++);\r |
22 | if(i < len-1 && format[len-1] != '\n') printf("\nWARNING: possible improper newline placement:\n%s\n", format);\r |
23 | \r |
24 | va_start(valist,format);\r |
25 | if (AsmFile) vfprintf(AsmFile,format,valist);\r |
26 | va_end(valist);\r |
27 | }\r |
28 | \r |
29 | void ltorg()\r |
30 | {\r |
31 | if (ms) ot(" LTORG\n");\r |
32 | else ot(" .ltorg\n");\r |
33 | }\r |
34 | \r |
35 | // trashes all temp regs\r |
36 | static void PrintException(int ints)\r |
37 | {\r |
38 | if(!ints) {\r |
39 | ot(" ;@ Cause an Exception - Vector address in r0\n");\r |
40 | ot(" mov r11,r0\n");\r |
41 | }\r |
42 | \r |
43 | ot(";@ swap OSP <-> A7?\n");\r |
44 | ot(" ldr r0,[r7,#0x44] ;@ Get SR high\n");\r |
45 | ot(" tst r0,#0x20\n");\r |
46 | ot(" bne no_sp_swap%i\n",ints);\r |
47 | ot(";@ swap OSP and A7:\n");\r |
48 | ot(" ldr r0,[r7,#0x3C] ;@ Get A7\n");\r |
49 | ot(" ldr r1,[r7,#0x48] ;@ Get OSP\n");\r |
50 | ot(" str r0,[r7,#0x48]\n");\r |
51 | ot(" str r1,[r7,#0x3C]\n");\r |
52 | ot("no_sp_swap%i%s\n",ints,ms?"":":");\r |
53 | \r |
54 | ot(" ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r |
55 | ot(" mov r1,r4,lsl #8\n");\r |
56 | ot(" sub r1,r1,r10,lsl #8 ;@ r1 = Old PC\n");\r |
57 | ot(" mov r1,r1,asr #8 ;@ push sign extended\n");\r |
58 | OpPush32();\r |
59 | OpPushSr(1);\r |
60 | ot(" mov r0,r11\n");\r |
61 | ot(";@ Read IRQ Vector:\n");\r |
62 | MemHandler(0,2);\r |
63 | if(ints) {\r |
64 | ot(" tst r0,r0 ;@ uninitialized int vector?\n");\r |
65 | ot(" moveq r0,#0x3c\n");\r |
66 | ot(" moveq lr,pc\n");\r |
67 | ot(" ldreq pc,[r7,#0x70] ;@ Call read32(r0) handler\n");\r |
68 | }\r |
69 | #if USE_CHECKPC_CALLBACK\r |
70 | ot(" add r0,r0,r10 ;@ r0 = Memory Base + New PC\n");\r |
71 | ot(" mov lr,pc\n");\r |
72 | ot(" ldr pc,[r7,#0x64] ;@ Call checkpc()\n");\r |
73 | ot(" mov r4,r0\n");\r |
74 | #endif\r |
75 | ot("\n");\r |
76 | \r |
77 | if(!ints) {\r |
78 | ot(" ldr r0,[r7,#0x44] ;@ Get SR high\n");\r |
79 | ot(" bic r0,r0,#0xd8 ;@ clear trace and unused flags\n");\r |
80 | ot(" orr r0,r0,#0x20 ;@ set supervisor mode\n");\r |
81 | ot(" strb r0,[r7,#0x44]\n");\r |
82 | }\r |
83 | }\r |
84 | \r |
85 | // Trashes r0,r1\r |
86 | void CheckInterrupt(int op)\r |
87 | {\r |
88 | ot(";@ CheckInterrupt:\n");\r |
89 | ot(" ldr r0,[r7,#0x44]\n"); // same as ldrb r0,[r7,#0x47]\r |
90 | ot(" movs r0,r0,lsr #24 ;@ Get IRQ level (loading word is faster)\n");\r |
91 | ot(" beq NoInts%x\n",op);\r |
92 | ot(" cmp r0,#6 ;@ irq>6 ?\n");\r |
93 | ot(" ldrleb r1,[r7,#0x44] ;@ Get SR high: T_S__III\n");\r |
94 | ot(" andle r1,r1,#7 ;@ Get interrupt mask\n");\r |
95 | ot(" cmple r0,r1 ;@ irq<=6: Is irq<=mask ?\n");\r |
96 | ot(" blgt DoInterrupt\n");\r |
97 | ot("NoInts%x%s\n", op,ms?"":":");\r |
98 | ot("\n");\r |
99 | }\r |
100 | \r |
101 | static void PrintFramework()\r |
102 | {\r |
103 | ot(";@ --------------------------- Framework --------------------------\n");\r |
104 | if (ms) ot("CycloneRun\n");\r |
105 | else ot("CycloneRun:\n");\r |
106 | \r |
107 | ot(" stmdb sp!,{r4-r11,lr}\n");\r |
108 | \r |
109 | ot(" mov r7,r0 ;@ r7 = Pointer to Cpu Context\n");\r |
110 | ot(" ;@ r0-3 = Temporary registers\n");\r |
111 | ot(" ldrb r9,[r7,#0x46] ;@ r9 = Flags (NZCV)\n");\r |
112 | ot(" ldr r6,=JumpTab ;@ r6 = Opcode Jump table\n");\r |
113 | ot(" ldr r5,[r7,#0x5c] ;@ r5 = Cycles\n");\r |
114 | ot(" ldr r4,[r7,#0x40] ;@ r4 = Current PC + Memory Base\n");\r |
115 | ot(" ;@ r8 = Current Opcode\n");\r |
116 | ot(" ldr r0,[r7,#0x44]\n");\r |
117 | ot(" mov r9,r9,lsl #28 ;@ r9 = Flags 0xf0000000, cpsr format\n");\r |
118 | ot(" ;@ r10 = Source value / Memory Base\n");\r |
119 | ot("\n");\r |
120 | ot(";@ CheckInterrupt:\n");\r |
121 | ot(" movs r0,r0,lsr #24 ;@ Get IRQ level\n"); // same as ldrb r0,[r7,#0x47]\r |
122 | ot(" beq NoInts0\n");\r |
123 | ot(" cmp r0,#6 ;@ irq>6 ?\n");\r |
124 | ot(" ldrleb r1,[r7,#0x44] ;@ Get SR high: T_S__III\n");\r |
125 | ot(" andle r1,r1,#7 ;@ Get interrupt mask\n");\r |
126 | ot(" cmple r0,r1 ;@ irq<=6: Is irq<=mask ?\n");\r |
127 | ot(" blgt DoInterrupt\n");\r |
128 | ot(";@ Check if interrupt used up all the cycles:\n");\r |
129 | ot(" subs r5,r5,#0\n");\r |
130 | ot(" blt CycloneEndNoBack\n");\r |
131 | ot("NoInts0%s\n", ms?"":":");\r |
132 | ot("\n");\r |
133 | ot(";@ Check if our processor is in stopped state and jump to opcode handler if not\n");\r |
134 | ot(" ldr r0,[r7,#0x58]\n");\r |
135 | ot(" ldrh r8,[r4],#2 ;@ Fetch first opcode\n");\r |
136 | ot(" tst r0,r0 ;@ stopped?\n");\r |
137 | ot(" bne CycloneStopped\n");\r |
138 | ot(" ldr pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");\r |
139 | ot("\n");\r |
140 | ot("\n");\r |
141 | \r |
142 | ot(";@ We come back here after execution\n");\r |
143 | ot("CycloneEnd%s\n", ms?"":":");\r |
144 | ot(" sub r4,r4,#2\n");\r |
145 | ot("CycloneEndNoBack%s\n", ms?"":":");\r |
146 | ot(" mov r9,r9,lsr #28\n");\r |
147 | ot(" str r4,[r7,#0x40] ;@ Save Current PC + Memory Base\n");\r |
148 | ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");\r |
149 | ot(" strb r9,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r |
150 | ot(" ldmia sp!,{r4-r11,pc}\n");\r |
151 | ot("\n");\r |
152 | ot("CycloneStopped%s\n", ms?"":":");\r |
153 | ot(" mov r5,#0\n");\r |
154 | ot(" str r5,[r7,#0x5C] ;@ eat all cycles\n");\r |
155 | ot(" ldmia sp!,{r4-r11,pc} ;@ we are stopped, do nothing!\n");\r |
156 | ot("\n");\r |
157 | \r |
158 | ltorg();\r |
159 | \r |
160 | #if COMPRESS_JUMPTABLE\r |
161 | ot(";@ uncompress jump table\n");\r |
162 | if (ms) ot("CycloneInit\n");\r |
163 | else ot("CycloneInit:\n");\r |
164 | ot(" ldr r12,=JumpTab\n");\r |
165 | ot(" add r0,r12,#0xe000*4 ;@ ctrl code pointer\n");\r |
166 | ot(" ldr r1,[r0,#-4]\n");\r |
167 | ot(" tst r1,r1\n");\r |
168 | ot(" movne pc,lr ;@ already uncompressed\n");\r |
169 | ot(" add r3,r12,#0xa000*4 ;@ handler table pointer, r12=dest\n");\r |
170 | ot("unc_loop%s\n", ms?"":":");\r |
171 | ot(" ldrh r1,[r0],#2\n");\r |
172 | ot(" and r2,r1,#0xf\n");\r |
173 | ot(" bic r1,r1,#0xf\n");\r |
174 | ot(" ldr r1,[r3,r1,lsr #2] ;@ r1=handler\n");\r |
175 | ot(" cmp r2,#0xf\n");\r |
176 | ot(" addeq r2,r2,#1 ;@ 0xf is really 0x10\n");\r |
177 | ot(" tst r2,r2\n");\r |
178 | ot(" ldreqh r2,[r0],#2 ;@ counter is in next word\n");\r |
179 | ot(" tst r2,r2\n");\r |
180 | ot(" beq unc_finish ;@ done decompressing\n");\r |
181 | ot(" tst r1,r1\n");\r |
182 | ot(" addeq r12,r12,r2,lsl #2 ;@ 0 handler means we should skip those bytes\n");\r |
183 | ot(" beq unc_loop\n");\r |
184 | ot("unc_loop_in%s\n", ms?"":":");\r |
185 | ot(" subs r2,r2,#1\n");\r |
186 | ot(" str r1,[r12],#4\n");\r |
187 | ot(" bgt unc_loop_in\n");\r |
188 | ot(" b unc_loop\n");\r |
189 | ot("unc_finish%s\n", ms?"":":");\r |
190 | ot(" ldr r12,=JumpTab\n");\r |
191 | ot(" ;@ set a-line and f-line handlers\n");\r |
192 | ot(" add r0,r12,#0xa000*4\n");\r |
193 | ot(" ldr r1,[r0,#4] ;@ a-line handler\n");\r |
194 | ot(" ldr r3,[r0,#8] ;@ f-line handler\n");\r |
195 | ot(" mov r2,#0x1000\n");\r |
196 | ot("unc_fill3%s\n", ms?"":":");\r |
197 | ot(" subs r2,r2,#1\n");\r |
198 | ot(" str r1,[r0],#4\n");\r |
199 | ot(" bgt unc_fill3\n");\r |
200 | ot(" add r0,r12,#0xf000*4\n");\r |
201 | ot(" mov r2,#0x1000\n");\r |
202 | ot("unc_fill4%s\n", ms?"":":");\r |
203 | ot(" subs r2,r2,#1\n");\r |
204 | ot(" str r3,[r0],#4\n");\r |
205 | ot(" bgt unc_fill4\n");\r |
206 | ot(" bx lr\n");\r |
207 | ltorg();\r |
208 | ot("\n");\r |
209 | #else\r |
210 | ot(";@ do nothing\n");\r |
211 | if (ms) ot("CycloneInit\n");\r |
212 | else ot("CycloneInit:\n");\r |
213 | ot(" bx lr\n");\r |
214 | ot("\n");\r |
215 | #endif\r |
216 | if (ms) ot("CycloneSetSr\n");\r |
217 | else ot("CycloneSetSr:\n");\r |
218 | ot(" mov r2,r1,lsr #8\n");\r |
219 | ot(" ldrb r3,[r0,#0x44] ;@ get SR high\n");\r |
220 | ot(" eor r3,r3,r2\n");\r |
221 | ot(" tst r3,#0x20\n");\r |
222 | ot(" and r2,r2,#0xa7 ;@ only nonzero bits\n");\r |
223 | ot(" strb r2,[r0,#0x44] ;@ set SR high\n");\r |
224 | ot(" bne setsr_noswap\n");\r |
225 | ot(" ldr r2,[r0,#0x3C] ;@ Get A7\n");\r |
226 | ot(" ldr r3,[r0,#0x48] ;@ Get OSP\n");\r |
227 | ot(" str r3,[r0,#0x3C]\n");\r |
228 | ot(" str r2,[r0,#0x48]\n");\r |
229 | ot("setsr_noswap%s\n",ms?"":":");\r |
230 | ot(" mov r2,r1,lsr #3\n");\r |
231 | ot(" strb r2,[r0,#0x45] ;@ the X flag\n");\r |
232 | ot(" bic r2,r1,#0xf3\n");\r |
233 | ot(" tst r1,#1\n");\r |
234 | ot(" orrne r2,r2,#2\n");\r |
235 | ot(" tst r1,#2\n");\r |
236 | ot(" orrne r2,r2,#1\n");\r |
237 | ot(" strb r2,[r0,#0x46] ;@ flags\n");\r |
238 | ot(" bx lr\n");\r |
239 | ot("\n");\r |
240 | \r |
241 | if (ms) ot("CycloneGetSr\n");\r |
242 | else ot("CycloneGetSr:\n");\r |
243 | ot(" ldrb r1,[r0,#0x46] ;@ flags\n");\r |
244 | ot(" bic r2,r1,#0xf3\n");\r |
245 | ot(" tst r1,#1\n");\r |
246 | ot(" orrne r2,r2,#2\n");\r |
247 | ot(" tst r1,#2\n");\r |
248 | ot(" orrne r2,r2,#1\n");\r |
249 | ot(" ldrb r1,[r0,#0x45] ;@ the X flag\n");\r |
250 | ot(" tst r1,#2\n");\r |
251 | ot(" orrne r2,r2,#0x10\n");\r |
252 | ot(" ldrb r1,[r0,#0x44] ;@ the SR high\n");\r |
253 | ot(" orr r0,r2,r1,lsl #8\n");\r |
254 | ot(" bx lr\n");\r |
255 | ot("\n");\r |
256 | \r |
257 | ot(";@ DoInterrupt - r0=IRQ number\n");\r |
258 | ot("DoInterrupt%s\n", ms?"":":");\r |
259 | ot(" stmdb sp!,{lr} ;@ Push ARM return address\n");\r |
260 | \r |
261 | ot(";@ Get IRQ Vector address:\n");\r |
262 | ot(" mov r0,r0,asl #2\n");\r |
263 | ot(" add r11,r0,#0x60\n");\r |
264 | PrintException(1);\r |
265 | \r |
266 | ot(" ldrb r0,[r7,#0x47] ;@ IRQ\n");\r |
267 | ot(" mov r2,#0\n");\r |
268 | ot(" orr r1,r0,#0x20 ;@ Supervisor mode + IRQ number\n");\r |
269 | ot(" strb r1,[r7,#0x44] ;@ Put SR high\n");\r |
270 | \r |
271 | ot(";@ Clear stopped states:\n");\r |
272 | ot(" str r2,[r7,#0x58]\n");\r |
273 | ot(" sub r5,r5,#%d ;@ Subtract cycles\n",44);\r |
274 | ot("\n");\r |
275 | #if USE_INT_ACK_CALLBACK\r |
276 | #if INT_ACK_NEEDS_STUFF\r |
277 | ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r |
278 | ot(" mov r1,r9,lsr #28\n");\r |
279 | ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r |
280 | ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");\r |
281 | #endif\r |
282 | ot(" ldr r11,[r7,#0x8c] ;@ IrqCallback\n");\r |
283 | ot(" tst r11,r11\n");\r |
284 | ot(" movne lr,pc\n");\r |
285 | ot(" movne pc,r11 ;@ call IrqCallback if it is defined\n");\r |
286 | #if INT_ACK_CHANGES_STUFF\r |
287 | ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r |
288 | ot(" ldrb r9,[r7,#0x46] ;@ r9 = Load Flags (NZCV)\n");\r |
289 | ot(" mov r9,r9,lsl #28\n");\r |
290 | ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r |
291 | #endif\r |
292 | #else // not USE_INT_ACK_CALLBACK\r |
293 | ot(";@ Clear irq:\n");\r |
294 | ot(" strb r1,[r7,#0x47]\n");\r |
295 | #endif\r |
296 | ot(" ldmia sp!,{pc} ;@ Return\n");\r |
297 | ot("\n");\r |
298 | \r |
299 | ot("Exception%s\n", ms?"":":");\r |
300 | ot("\n");\r |
301 | ot(" stmdb sp!,{lr} ;@ Preserve ARM return address\n");\r |
302 | PrintException(0);\r |
303 | ot(" ldmia sp!,{pc} ;@ Return\n");\r |
304 | ot("\n");\r |
305 | }\r |
306 | \r |
307 | // ---------------------------------------------------------------------------\r |
308 | // Call Read(r0), Write(r0,r1) or Fetch(r0)\r |
309 | // Trashes r0-r3,r12,lr\r |
310 | int MemHandler(int type,int size)\r |
311 | {\r |
312 | int func=0;\r |
313 | func=0x68+type*0xc+(size<<2); // Find correct offset\r |
314 | \r |
315 | #if MEMHANDLERS_NEED_PC\r |
316 | ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r |
317 | #endif\r |
318 | #if MEMHANDLERS_NEED_FLAGS\r |
319 | ot(" mov r3,r9,lsr #28\n");\r |
320 | ot(" strb r3,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r |
321 | #endif\r |
322 | #if MEMHANDLERS_NEED_CYCLES\r |
323 | ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");\r |
324 | #endif\r |
325 | \r |
326 | ot(" mov lr,pc\n");\r |
327 | ot(" ldr pc,[r7,#0x%x] ;@ Call ",func);\r |
328 | \r |
329 | // Document what we are calling:\r |
330 | if (type==0) ot("read");\r |
331 | if (type==1) ot("write");\r |
332 | if (type==2) ot("fetch");\r |
333 | \r |
334 | if (type==1) ot("%d(r0,r1)",8<<size);\r |
335 | else ot("%d(r0)", 8<<size);\r |
336 | ot(" handler\n");\r |
337 | \r |
338 | #if MEMHANDLERS_CHANGE_CYCLES\r |
339 | ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r |
340 | #endif\r |
341 | #if MEMHANDLERS_CHANGE_FLAGS\r |
342 | ot(" ldrb r9,[r7,#0x46] ;@ r9 = Load Flags (NZCV)\n");\r |
343 | ot(" mov r9,r9,lsl #28\n");\r |
344 | #endif\r |
345 | #if MEMHANDLERS_CHANGE_PC\r |
346 | ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r |
347 | #endif\r |
348 | \r |
349 | return 0;\r |
350 | }\r |
351 | \r |
352 | static void PrintOpcodes()\r |
353 | {\r |
354 | int op=0;\r |
355 | \r |
356 | printf("Creating Opcodes: [");\r |
357 | \r |
358 | ot(";@ ---------------------------- Opcodes ---------------------------\n");\r |
359 | \r |
360 | // Emit null opcode:\r |
361 | ot("Op____%s ;@ Called if an opcode is not recognised\n", ms?"":":");\r |
362 | ot(" sub r4,r4,#2\n");\r |
363 | #if USE_UNRECOGNIZED_CALLBACK\r |
364 | ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r |
365 | ot(" mov r1,r9,lsr #28\n");\r |
366 | ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r |
367 | ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");\r |
368 | ot(" ldr r11,[r7,#0x94] ;@ UnrecognizedCallback\n");\r |
369 | ot(" tst r11,r11\n");\r |
370 | ot(" movne lr,pc\n");\r |
371 | ot(" movne pc,r11 ;@ call UnrecognizedCallback if it is defined\n");\r |
372 | ot(" ldrb r9,[r7,#0x46] ;@ r9 = Load Flags (NZCV)\n");\r |
373 | ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r |
374 | ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r |
375 | ot(" mov r9,r9,lsl #28\n");\r |
376 | ot(" tst r0,r0\n");\r |
377 | ot(" moveq r0,#0x10\n");\r |
378 | ot(" bleq Exception\n");\r |
379 | #else\r |
380 | ot(" mov r0,#0x10\n");\r |
381 | ot(" bl Exception\n");\r |
382 | #endif\r |
383 | Cycles=34;\r |
384 | OpEnd();\r |
385 | \r |
386 | // Unrecognised a-line and f-line opcodes throw an exception:\r |
387 | ot("Op__al%s ;@ Unrecognised a-line opcode\n", ms?"":":");\r |
388 | ot(" sub r4,r4,#2\n");\r |
389 | #if USE_AFLINE_CALLBACK\r |
390 | ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r |
391 | ot(" mov r1,r9,lsr #28\n");\r |
392 | ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r |
393 | ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");\r |
394 | ot(" ldr r11,[r7,#0x94] ;@ UnrecognizedCallback\n");\r |
395 | ot(" tst r11,r11\n");\r |
396 | ot(" movne lr,pc\n");\r |
397 | ot(" movne pc,r11 ;@ call UnrecognizedCallback if it is defined\n");\r |
398 | ot(" ldrb r9,[r7,#0x46] ;@ r9 = Load Flags (NZCV)\n");\r |
399 | ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r |
400 | ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r |
401 | ot(" mov r9,r9,lsl #28\n");\r |
402 | ot(" tst r0,r0\n");\r |
403 | ot(" moveq r0,#0x28\n");\r |
404 | ot(" bleq Exception\n");\r |
405 | #else\r |
406 | ot(" mov r0,#0x28\n");\r |
407 | ot(" bl Exception\n");\r |
408 | #endif\r |
409 | Cycles=4;\r |
410 | OpEnd();\r |
411 | \r |
412 | ot("Op__fl%s ;@ Unrecognised f-line opcode\n", ms?"":":");\r |
413 | ot(" sub r4,r4,#2\n");\r |
414 | #if USE_AFLINE_CALLBACK\r |
415 | ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r |
416 | ot(" mov r1,r9,lsr #28\n");\r |
417 | ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r |
418 | ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");\r |
419 | ot(" ldr r11,[r7,#0x94] ;@ UnrecognizedCallback\n");\r |
420 | ot(" tst r11,r11\n");\r |
421 | ot(" movne lr,pc\n");\r |
422 | ot(" movne pc,r11 ;@ call UnrecognizedCallback if it is defined\n");\r |
423 | ot(" ldrb r9,[r7,#0x46] ;@ r9 = Load Flags (NZCV)\n");\r |
424 | ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r |
425 | ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r |
426 | ot(" mov r9,r9,lsl #28\n");\r |
427 | ot(" tst r0,r0\n");\r |
428 | ot(" moveq r0,#0x2c\n");\r |
429 | ot(" bleq Exception\n");\r |
430 | #else\r |
431 | ot(" mov r0,#0x2c\n");\r |
432 | ot(" bl Exception\n");\r |
433 | #endif\r |
434 | Cycles=4;\r |
435 | OpEnd();\r |
436 | \r |
437 | \r |
438 | for (op=0;op<0x10000;op++)\r |
439 | {\r |
440 | if ((op&0xfff)==0) { printf("%x",op>>12); fflush(stdout); } // Update progress\r |
441 | \r |
442 | OpAny(op);\r |
443 | }\r |
444 | \r |
445 | ot("\n");\r |
446 | \r |
447 | printf("]\n");\r |
448 | }\r |
449 | \r |
450 | // helper\r |
451 | static void ott(const char *str, int par, const char *nl, int nlp, int counter, int size)\r |
452 | {\r |
453 | switch(size) {\r |
454 | case 0: if((counter&7)==0) ot(ms?" dcb ":" .byte "); break;\r |
455 | case 1: if((counter&7)==0) ot(ms?" dcw ":" .hword "); break;\r |
456 | case 2: if((counter&7)==0) ot(ms?" dcd ":" .long "); break;\r |
457 | }\r |
458 | ot(str, par);\r |
459 | if((counter&7)==7) ot(nl,nlp); else ot(",");\r |
460 | }\r |
461 | \r |
462 | static void PrintJumpTable()\r |
463 | {\r |
464 | int i=0,op=0,len=0;\r |
465 | \r |
466 | ot(";@ -------------------------- Jump Table --------------------------\n");\r |
467 | \r |
468 | #if COMPRESS_JUMPTABLE\r |
469 | int handlers=0,reps=0,*indexes,ip,u,out;\r |
470 | // use some weird compression on the jump table\r |
471 | indexes=(int *)malloc(0x10000*4);\r |
472 | if(!indexes) { printf("ERROR: out of memory\n"); exit(1); }\r |
473 | len=0x10000;\r |
474 | \r |
475 | // space for decompressed table\r |
476 | ot(ms?" area |.data|, data\n":" .data\n .align 4\n\n");\r |
477 | \r |
478 | ot("JumpTab%s\n", ms?"":":");\r |
479 | if(ms) {\r |
480 | for(i = 0; i < 0xa000/8; i++)\r |
481 | ot(" dcd 0,0,0,0,0,0,0,0\n");\r |
482 | } else\r |
483 | ot(" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", 0xa000/8);\r |
484 | \r |
485 | // hanlers live in "a-line" part of the table\r |
486 | // first output nop,a-line,f-line handlers\r |
487 | ot(ms?" dcd Op____,Op__al,Op__fl,":" .long Op____,Op__al,Op__fl,");\r |
488 | handlers=3;\r |
489 | \r |
490 | for(i=0;i<len;i++)\r |
491 | {\r |
492 | op=CyJump[i];\r |
493 | \r |
494 | for(u=i-1; u>=0; u--) if(op == CyJump[u]) break; // already done with this op?\r |
495 | if(u==-1 && op >= 0) {\r |
496 | ott("Op%.4x",op," ;@ %.4x\n",i,handlers,2);\r |
497 | indexes[op] = handlers;\r |
498 | handlers++;\r |
499 | }\r |
500 | }\r |
501 | if(handlers&7) {\r |
502 | fseek(AsmFile, -1, SEEK_CUR); // remove last comma\r |
503 | for(i = 8-(handlers&7); i > 0; i--)\r |
504 | ot(",000000");\r |
505 | ot("\n");\r |
506 | }\r |
507 | if(ms) {\r |
508 | for(i = (0x4000-handlers)/8; i > 0; i--)\r |
509 | ot(" dcd 0,0,0,0,0,0,0,0\n");\r |
510 | } else {\r |
511 | ot(ms?"":" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", (0x4000-handlers)/8);\r |
512 | }\r |
513 | printf("total distinct hanlers: %i\n",handlers);\r |
514 | // output data\r |
515 | for(i=0,ip=0; i < 0xf000; i++, ip++) {\r |
516 | op=CyJump[i];\r |
517 | if(op == -2) {\r |
518 | // it must skip a-line area, because we keep our data there\r |
519 | ott("0x%.4x", handlers<<4, "\n",0,ip++,1);\r |
520 | ott("0x%.4x", 0x1000, "\n",0,ip,1);\r |
521 | i+=0xfff;\r |
522 | continue;\r |
523 | }\r |
524 | for(reps=1; i < 0xf000; i++, reps++) if(op != CyJump[i+1]) break;\r |
525 | if(op>=0) out=indexes[op]<<4; else out=0; // unrecognised\r |
526 | if(reps <= 0xe || reps==0x10) {\r |
527 | if(reps!=0x10) out|=reps; else out|=0xf; // 0xf means 0x10 (0xf appeared to be unused anyway)\r |
528 | ott("0x%.4x", out, "\n",0,ip,1);\r |
529 | } else {\r |
530 | ott("0x%.4x", out, "\n",0,ip++,1);\r |
531 | ott("0x%.4x", reps,"\n",0,ip,1);\r |
532 | }\r |
533 | }\r |
534 | if(ip&1) ott("0x%.4x", 0, "\n",0,ip++,1);\r |
535 | if(ip&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma\r |
536 | ot("\n");\r |
537 | if(ip&7) {\r |
538 | for(i = 8-(ip&7); i > 0; i--)\r |
539 | ot(",0x0000");\r |
540 | ot("\n");\r |
541 | }\r |
542 | if(ms) {\r |
543 | for(i = (0x2000-ip/2)/8+1; i > 0; i--)\r |
544 | ot(" dcd 0,0,0,0,0,0,0,0\n");\r |
545 | } else {\r |
546 | ot(" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", (0x2000-ip/2)/8+1);\r |
547 | }\r |
548 | ot("\n");\r |
549 | free(indexes);\r |
550 | #else\r |
551 | ot("JumpTab%s\n", ms?"":":");\r |
552 | len=0xfffe; // Hmmm, armasm 2.50.8684 messes up with a 0x10000 long jump table\r |
553 | // notaz: same thing with GNU as 2.9-psion-98r2 (reloc overflow)\r |
554 | // this is due to COFF objects using only 2 bytes for reloc count\r |
555 | \r |
556 | for (i=0;i<len;i++)\r |
557 | {\r |
558 | op=CyJump[i];\r |
559 | \r |
560 | if(op>=0) ott("Op%.4x",op," ;@ %.4x\n",i-7,i,2);\r |
561 | else if(op==-2) ott("Op__al",0, " ;@ %.4x\n",i-7,i,2);\r |
562 | else if(op==-3) ott("Op__fl",0, " ;@ %.4x\n",i-7,i,2);\r |
563 | else ott("Op____",0, " ;@ %.4x\n",i-7,i,2);\r |
564 | }\r |
565 | if(i&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma\r |
566 | \r |
567 | ot("\n");\r |
568 | ot(";@ notaz: we don't want to crash if we run into those 2 missing opcodes\n");\r |
569 | ot(";@ so we leave this pattern to patch it later\n");\r |
570 | ot("%s 0x78563412\n", ms?" dcd":" .long");\r |
571 | ot("%s 0x56341290\n", ms?" dcd":" .long");\r |
572 | #endif\r |
573 | }\r |
574 | \r |
575 | static int CycloneMake()\r |
576 | {\r |
577 | int i;\r |
578 | char *name="Cyclone.s";\r |
579 | \r |
580 | // Open the assembly file\r |
581 | if (ms) name="Cyclone.asm";\r |
582 | AsmFile=fopen(name,"wt"); if (AsmFile==NULL) return 1;\r |
583 | \r |
584 | printf("Making %s...\n",name);\r |
585 | \r |
586 | ot("\n;@ Dave's Cyclone 68000 Emulator v%x.%.3x - Assembler Output\n\n",CycloneVer>>12,CycloneVer&0xfff);\r |
587 | \r |
588 | ot(";@ (c) Copyright 2003 Dave, All rights reserved.\n");\r |
589 | ot(";@ some code (c) Copyright 2005-2006 notaz, All rights reserved.\n");\r |
590 | ot(";@ Cyclone 68000 is free for non-commercial use.\n\n");\r |
591 | ot(";@ For commercial use, separate licencing terms must be obtained.\n\n");\r |
592 | \r |
593 | CyJump=(int *)malloc(0x40000); if (CyJump==NULL) return 1;\r |
594 | memset(CyJump,0xff,0x40000); // Init to -1\r |
595 | for(i=0xa000; i<0xb000; i++) CyJump[i] = -2; // a-line emulation\r |
596 | for(i=0xf000; i<0x10000; i++) CyJump[i] = -3; // f-line emulation\r |
597 | \r |
598 | if (ms)\r |
599 | {\r |
600 | ot(" area |.text|, code\n");\r |
601 | ot(" export CycloneInit\n");\r |
602 | ot(" export CycloneRun\n");\r |
603 | ot(" export CycloneSetSr\n");\r |
604 | ot(" export CycloneGetSr\n");\r |
605 | ot(" export CycloneVer\n");\r |
606 | ot("\n");\r |
607 | ot("CycloneVer dcd 0x%.4x\n",CycloneVer);\r |
608 | }\r |
609 | else\r |
610 | {\r |
611 | ot(" .global CycloneInit\n");\r |
612 | ot(" .global CycloneRun\n");\r |
613 | ot(" .global CycloneSetSr\n");\r |
614 | ot(" .global CycloneGetSr\n");\r |
615 | ot(" .global CycloneVer\n");\r |
616 | ot("CycloneVer: .long 0x%.4x\n",CycloneVer);\r |
617 | }\r |
618 | ot("\n");\r |
619 | \r |
620 | PrintFramework();\r |
621 | PrintOpcodes();\r |
622 | PrintJumpTable();\r |
623 | \r |
624 | if (ms) ot(" END\n");\r |
625 | \r |
626 | fclose(AsmFile); AsmFile=NULL;\r |
627 | \r |
628 | #if 0\r |
629 | printf("Assembling...\n");\r |
630 | // Assemble the file\r |
631 | if (ms) system("armasm Cyclone.asm");\r |
632 | else system("as -o Cyclone.o Cyclone.s");\r |
633 | printf("Done!\n\n");\r |
634 | #endif\r |
635 | \r |
636 | free(CyJump);\r |
637 | return 0;\r |
638 | }\r |
639 | \r |
640 | int main()\r |
641 | {\r |
642 | printf("\n Dave's Cyclone 68000 Emulator v%x.%.3x - Core Creator\n\n",CycloneVer>>12,CycloneVer&0xfff);\r |
643 | \r |
644 | // Make GAS or ARMASM version\r |
645 | CycloneMake();\r |
646 | return 0;\r |
647 | }\r |
648 | \r |