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