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