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