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