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