4 static FILE *AsmFile=NULL;
\r
6 static int CycloneVer=0x0099; // Version number of library
\r
7 int *CyJump=NULL; // Jump table
\r
8 int ms=USE_MS_SYNTAX; // If non-zero, output in Microsoft ARMASM format
\r
9 char *Narm[4]={ "b", "h","",""}; // Normal ARM Extensions for operand sizes 0,1,2
\r
10 char *Sarm[4]={"sb","sh","",""}; // Sign-extend ARM Extensions for operand sizes 0,1,2
\r
11 int Cycles; // Current cycles for opcode
\r
12 int pc_dirty; // something changed PC during processing
\r
16 void ot(const char *format, ...)
\r
18 va_list valist=NULL;
\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
26 if (format[0] == ' ' && format[1] == ' ' && format[2] != ' ' && format[2] != '.')
\r
29 va_start(valist,format);
\r
30 if (AsmFile) vfprintf(AsmFile,format,valist);
\r
36 if (ms) ot(" LTORG\n");
\r
37 else ot(" .ltorg\n");
\r
40 #if (CYCLONE_FOR_GENESIS == 2)
\r
41 // r12=ptr to tas in table, trashes r0,r1
\r
42 static void ChangeTAS(int norm)
\r
44 ot(" ldr r0,=Op4ad0%s\n",norm?"_":"");
\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
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
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
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
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
85 #if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO
\r
86 static void AddressErrorWrapper(char rw, char *dataprg, int iw)
\r
88 ot("ExceptionAddressError_%c_%s%s\n", rw, dataprg, ms?"":":");
\r
89 ot(" ldr r1,[r7,#0x44]\n");
\r
90 ot(" mov r6,#0x%02x\n", iw);
\r
91 ot(" mov r11,r0\n");
\r
92 ot(" tst r1,#0x20\n");
\r
93 ot(" orrne r6,r6,#4\n");
\r
94 ot(" b ExceptionAddressError\n");
\r
101 #if MEMHANDLERS_NEED_PC
\r
103 ot(" str r4,[r7,#0x40] ;@ Save PC\n");
\r
108 static void PrintFramework()
\r
110 int state_flags_to_check = 1; // stopped
\r
112 state_flags_to_check |= 2; // tracing
\r
115 state_flags_to_check |= 0x10; // halted
\r
118 ot(";@ --------------------------- Framework --------------------------\n");
\r
119 if (ms) ot("CycloneRun\n");
\r
120 else ot("CycloneRun:\n");
\r
122 ot(" stmdb sp!,{r4-r8,r10,r11,lr}\n");
\r
124 ot(" mov r7,r0 ;@ r7 = Pointer to Cpu Context\n");
\r
125 ot(" ;@ r0-3 = Temporary registers\n");
\r
126 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Flags (NZCV)\n");
\r
127 ot(" ldr r6,=CycloneJumpTab ;@ r6 = Opcode Jump table\n");
\r
128 ot(" ldr r5,[r7,#0x5c] ;@ r5 = Cycles\n");
\r
129 ot(" ldr r4,[r7,#0x40] ;@ r4 = Current PC + Memory Base\n");
\r
130 ot(" ;@ r8 = Current Opcode\n");
\r
131 ot(" ldr r1,[r7,#0x44] ;@ Get SR high T_S__III and irq level\n");
\r
132 ot(" mov r10,r10,lsl #28;@ r10 = Flags 0xf0000000, cpsr format\n");
\r
133 ot(" ;@ r11 = Source value / Memory Base\n");
\r
134 ot(" str r6,[r7,#0x54] ;@ make a copy to avoid literal pools\n");
\r
136 #if (CYCLONE_FOR_GENESIS == 2) || EMULATE_TRACE
\r
137 ot(" mov r2,#0\n");
\r
138 ot(" str r2,[r7,#0x98] ;@ clear custom CycloneEnd\n");
\r
140 ot(";@ CheckInterrupt:\n");
\r
141 ot(" movs r0,r1,lsr #24 ;@ Get IRQ level\n"); // same as ldrb r0,[r7,#0x47]
\r
142 ot(" beq NoInts0\n");
\r
143 ot(" cmp r0,#6 ;@ irq>6 ?\n");
\r
144 ot(" andle r1,r1,#7 ;@ Get interrupt mask\n");
\r
145 ot(" cmple r0,r1 ;@ irq<=6: Is irq<=mask ?\n");
\r
146 ot(" bgt CycloneDoInterrupt\n");
\r
147 ot("NoInts0%s\n", ms?"":":");
\r
149 ot(";@ Check if our processor is in special state\n");
\r
150 ot(";@ and jump to opcode handler if not\n");
\r
151 ot(" ldr r0,[r7,#0x58] ;@ state_flags\n");
\r
152 ot(" ldrh r8,[r4],#2 ;@ Fetch first opcode\n");
\r
153 ot(" tst r0,#0x%02x ;@ special state?\n", state_flags_to_check);
\r
154 ot(" ldreq pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
\r
156 ot("CycloneSpecial%s\n", ms?"":":");
\r
158 ot(" tst r0,#2 ;@ tracing?\n");
\r
159 ot(" bne CycloneDoTrace\n");
\r
161 ot(";@ stopped or halted\n");
\r
162 ot(" mov r5,#0\n");
\r
163 ot(" str r5,[r7,#0x5C] ;@ eat all cycles\n");
\r
164 ot(" ldmia sp!,{r4-r8,r10,r11,pc} ;@ we are stopped, do nothing!\n");
\r
168 ot(";@ We come back here after execution\n");
\r
169 ot("CycloneEnd%s\n", ms?"":":");
\r
170 ot(" sub r4,r4,#2\n");
\r
171 ot("CycloneEndNoBack%s\n", ms?"":":");
\r
172 #if (CYCLONE_FOR_GENESIS == 2) || EMULATE_TRACE
\r
173 ot(" ldr r1,[r7,#0x98]\n");
\r
174 ot(" mov r10,r10,lsr #28\n");
\r
175 ot(" tst r1,r1\n");
\r
176 ot(" bxne r1 ;@ jump to alternative CycloneEnd\n");
\r
178 ot(" mov r10,r10,lsr #28\n");
\r
180 ot(" str r4,[r7,#0x40] ;@ Save Current PC + Memory Base\n");
\r
181 ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");
\r
182 ot(" strb r10,[r7,#0x46] ;@ Save Flags (NZCV)\n");
\r
183 ot(" ldmia sp!,{r4-r8,r10,r11,pc}\n");
\r
188 ot("CycloneInit%s\n", ms?"":":");
\r
189 #if COMPRESS_JUMPTABLE
\r
190 ot(";@ decompress jump table\n");
\r
191 ot(" ldr r12,=CycloneJumpTab\n");
\r
192 ot(" add r0,r12,#0xe000*4 ;@ ctrl code pointer\n");
\r
193 ot(" ldr r1,[r0,#-4]\n");
\r
194 ot(" tst r1,r1\n");
\r
195 ot(" movne pc,lr ;@ already uncompressed\n");
\r
196 ot(" add r3,r12,#0xa000*4 ;@ handler table pointer, r12=dest\n");
\r
197 ot("unc_loop%s\n", ms?"":":");
\r
198 ot(" ldrh r1,[r0],#2\n");
\r
199 ot(" and r2,r1,#0xf\n");
\r
200 ot(" bic r1,r1,#0xf\n");
\r
201 ot(" ldr r1,[r3,r1,lsr #2] ;@ r1=handler\n");
\r
202 ot(" cmp r2,#0xf\n");
\r
203 ot(" addeq r2,r2,#1 ;@ 0xf is really 0x10\n");
\r
204 ot(" tst r2,r2\n");
\r
205 ot(" ldreqh r2,[r0],#2 ;@ counter is in next word\n");
\r
206 ot(" tst r2,r2\n");
\r
207 ot(" beq unc_finish ;@ done decompressing\n");
\r
208 ot(" tst r1,r1\n");
\r
209 ot(" addeq r12,r12,r2,lsl #2 ;@ 0 handler means we should skip those bytes\n");
\r
210 ot(" beq unc_loop\n");
\r
211 ot("unc_loop_in%s\n", ms?"":":");
\r
212 ot(" subs r2,r2,#1\n");
\r
213 ot(" str r1,[r12],#4\n");
\r
214 ot(" bgt unc_loop_in\n");
\r
215 ot(" b unc_loop\n");
\r
216 ot("unc_finish%s\n", ms?"":":");
\r
217 ot(" ldr r12,=CycloneJumpTab\n");
\r
218 ot(" ;@ set a-line and f-line handlers\n");
\r
219 ot(" add r0,r12,#0xa000*4\n");
\r
220 ot(" ldr r1,[r0,#4] ;@ a-line handler\n");
\r
221 ot(" ldr r3,[r0,#8] ;@ f-line handler\n");
\r
222 ot(" mov r2,#0x1000\n");
\r
223 ot("unc_fill3%s\n", ms?"":":");
\r
224 ot(" subs r2,r2,#1\n");
\r
225 ot(" str r1,[r0],#4\n");
\r
226 ot(" bgt unc_fill3\n");
\r
227 ot(" add r0,r12,#0xf000*4\n");
\r
228 ot(" mov r2,#0x1000\n");
\r
229 ot("unc_fill4%s\n", ms?"":":");
\r
230 ot(" subs r2,r2,#1\n");
\r
231 ot(" str r3,[r0],#4\n");
\r
232 ot(" bgt unc_fill4\n");
\r
236 ot(";@ do nothing\n");
\r
242 // 68k: XNZVC, ARM: NZCV
\r
243 ot("CycloneSetSr%s\n", ms?"":":");
\r
244 ot(" mov r2,r1,lsr #8\n");
\r
245 // ot(" ldrb r3,[r0,#0x44] ;@ get SR high\n");
\r
246 // ot(" eor r3,r3,r2\n");
\r
247 // ot(" tst r3,#0x20\n");
\r
249 ot(" and r2,r2,#0xa7 ;@ only defined bits\n");
\r
251 ot(" and r2,r2,#0x27 ;@ only defined bits\n");
\r
253 ot(" strb r2,[r0,#0x44] ;@ set SR high\n");
\r
254 ot(" mov r2,r1,lsl #25\n");
\r
255 ot(" str r2,[r0,#0x4c] ;@ the X flag\n");
\r
256 ot(" bic r2,r1,#0xf3\n");
\r
257 ot(" tst r1,#1\n");
\r
258 ot(" orrne r2,r2,#2\n");
\r
259 ot(" tst r1,#2\n");
\r
260 ot(" orrne r2,r2,#1\n");
\r
261 ot(" strb r2,[r0,#0x46] ;@ flags\n");
\r
266 ot("CycloneGetSr%s\n", ms?"":":");
\r
267 ot(" ldrb r1,[r0,#0x46] ;@ flags\n");
\r
268 ot(" bic r2,r1,#0xf3\n");
\r
269 ot(" tst r1,#1\n");
\r
270 ot(" orrne r2,r2,#2\n");
\r
271 ot(" tst r1,#2\n");
\r
272 ot(" orrne r2,r2,#1\n");
\r
273 ot(" ldr r1,[r0,#0x4c] ;@ the X flag\n");
\r
274 ot(" tst r1,#0x20000000\n");
\r
275 ot(" orrne r2,r2,#0x10\n");
\r
276 ot(" ldrb r1,[r0,#0x44] ;@ the SR high\n");
\r
277 ot(" orr r0,r2,r1,lsl #8\n");
\r
282 ot("CyclonePack%s\n", ms?"":":");
\r
283 ot(" stmfd sp!,{r4,r5,lr}\n");
\r
284 ot(" mov r4,r0\n");
\r
285 ot(" mov r5,r1\n");
\r
286 ot(" mov r3,#16\n");
\r
287 ot(";@ 0x00-0x3f: DA registers\n");
\r
288 ot("c_pack_loop%s\n",ms?"":":");
\r
289 ot(" ldr r1,[r0],#4\n");
\r
290 ot(" subs r3,r3,#1\n");
\r
291 ot(" str r1,[r5],#4\n");
\r
292 ot(" bne c_pack_loop\n");
\r
293 ot(";@ 0x40: PC\n");
\r
294 ot(" ldr r0,[r4,#0x40] ;@ PC + Memory Base\n");
\r
295 ot(" ldr r1,[r4,#0x60] ;@ Memory base\n");
\r
296 ot(" sub r0,r0,r1\n");
\r
297 ot(" str r0,[r5],#4\n");
\r
298 ot(";@ 0x44: SR\n");
\r
299 ot(" mov r0,r4\n");
\r
300 ot(" bl CycloneGetSr\n");
\r
301 ot(" strh r0,[r5],#2\n");
\r
302 ot(";@ 0x46: IRQ level\n");
\r
303 ot(" ldrb r0,[r4,#0x47]\n");
\r
304 ot(" strb r0,[r5],#2\n");
\r
305 ot(";@ 0x48: other SP\n");
\r
306 ot(" ldr r0,[r4,#0x48]\n");
\r
307 ot(" str r0,[r5],#4\n");
\r
308 ot(";@ 0x4c: CPU state flags\n");
\r
309 ot(" ldr r0,[r4,#0x58]\n");
\r
310 ot(" str r0,[r5],#4\n");
\r
311 ot(" ldmfd sp!,{r4,r5,pc}\n");
\r
315 ot("CycloneUnpack%s\n", ms?"":":");
\r
316 ot(" stmfd sp!,{r4,r5,lr}\n");
\r
317 ot(" mov r4,r0\n");
\r
318 ot(" mov r5,r1\n");
\r
319 ot(" mov r3,#16\n");
\r
320 ot(";@ 0x00-0x3f: DA registers\n");
\r
321 ot("c_unpack_loop%s\n",ms?"":":");
\r
322 ot(" ldr r1,[r5],#4\n");
\r
323 ot(" subs r3,r3,#1\n");
\r
324 ot(" str r1,[r0],#4\n");
\r
325 ot(" bne c_unpack_loop\n");
\r
326 ot(";@ 0x40: PC\n");
\r
327 ot(" ldr r0,[r5],#4 ;@ PC\n");
\r
328 #if USE_CHECKPC_CALLBACK
\r
329 ot(" mov r1,#0\n");
\r
330 ot(" str r1,[r4,#0x60] ;@ Memory base\n");
\r
331 ot(" mov lr,pc\n");
\r
332 ot(" ldr pc,[r4,#0x64] ;@ Call checkpc()\n");
\r
334 ot(" ldr r1,[r4,#0x60] ;@ Memory base\n");
\r
335 ot(" add r0,r0,r1 ;@ r0 = Memory Base + New PC\n");
\r
337 ot(" str r0,[r4,#0x40] ;@ PC + Memory Base\n");
\r
338 ot(";@ 0x44: SR\n");
\r
339 ot(" ldrh r1,[r5],#2\n");
\r
340 ot(" mov r0,r4\n");
\r
341 ot(" bl CycloneSetSr\n");
\r
342 ot(";@ 0x46: IRQ level\n");
\r
343 ot(" ldrb r0,[r5],#2\n");
\r
344 ot(" strb r0,[r4,#0x47]\n");
\r
345 ot(";@ 0x48: other SP\n");
\r
346 ot(" ldr r0,[r5],#4\n");
\r
347 ot(" str r0,[r4,#0x48]\n");
\r
348 ot(";@ 0x4c: CPU state flags\n");
\r
349 ot(" ldr r0,[r5],#4\n");
\r
350 ot(" str r0,[r4,#0x58]\n");
\r
351 ot(" ldmfd sp!,{r4,r5,pc}\n");
\r
355 ot("CycloneFlushIrq%s\n", ms?"":":");
\r
356 ot(" ldr r1,[r0,#0x44] ;@ Get SR high T_S__III and irq level\n");
\r
357 ot(" mov r2,r1,lsr #24 ;@ Get IRQ level\n"); // same as ldrb r0,[r7,#0x47]
\r
358 ot(" cmp r2,#6 ;@ irq>6 ?\n");
\r
359 ot(" andle r1,r1,#7 ;@ Get interrupt mask\n");
\r
360 ot(" cmple r2,r1 ;@ irq<=6: Is irq<=mask ?\n");
\r
361 ot(" movle r0,#0\n");
\r
362 ot(" bxle lr ;@ no ints\n");
\r
364 ot(" stmdb sp!,{r4,r5,r7,r8,r10,r11,lr}\n");
\r
365 ot(" mov r7,r0\n");
\r
366 ot(" mov r0,r2\n");
\r
367 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Flags (NZCV)\n");
\r
368 ot(" mov r5,#0\n");
\r
369 ot(" ldr r4,[r7,#0x40] ;@ r4 = Current PC + Memory Base\n");
\r
370 ot(" mov r10,r10,lsl #28 ;@ r10 = Flags 0xf0000000, cpsr format\n");
\r
371 ot(" adr r2,CycloneFlushIrqEnd\n");
\r
372 ot(" str r2,[r7,#0x98] ;@ set custom CycloneEnd\n");
\r
373 ot(" b CycloneDoInterrupt\n");
\r
375 ot("CycloneFlushIrqEnd%s\n", ms?"":":");
\r
376 ot(" rsb r0,r5,#0\n");
\r
377 ot(" str r4,[r7,#0x40] ;@ Save Current PC + Memory Base\n");
\r
378 ot(" strb r10,[r7,#0x46] ;@ Save Flags (NZCV)\n");
\r
379 ot(" ldmia sp!,{r4,r5,r7,r8,r10,r11,lr}\n");
\r
385 ot("CycloneSetRealTAS%s\n", ms?"":":");
\r
386 #if (CYCLONE_FOR_GENESIS == 2)
\r
387 ot(" ldr r12,=CycloneJumpTab\n");
\r
388 ot(" tst r0,r0\n");
\r
389 ot(" add r12,r12,#0x4a00*4\n");
\r
390 ot(" add r12,r12,#0x00d0*4\n");
\r
391 ot(" beq setrtas_off\n");
\r
394 ot("setrtas_off%s\n",ms?"":":");
\r
404 ot(";@ DoInterrupt - r0=IRQ level\n");
\r
405 ot("CycloneDoInterruptGoBack%s\n", ms?"":":");
\r
406 ot(" sub r4,r4,#2\n");
\r
407 ot("CycloneDoInterrupt%s\n", ms?"":":");
\r
408 ot(" bic r8,r8,#0xff000000\n");
\r
409 ot(" orr r8,r8,r0,lsl #29 ;@ abuse r8\n");
\r
411 // Steps are from "M68000 8-/16-/32-BIT MICROPROCESSORS USER'S MANUAL", p. 6-4
\r
412 // but their order is based on http://pasti.fxatari.com/68kdocs/68kPrefetch.html
\r
413 // 1. Make a temporary copy of the status register and set the status register for exception processing.
\r
414 ot(" ldr r2,[r7,#0x58] ;@ state flags\n");
\r
415 ot(" and r0,r0,#7\n");
\r
416 ot(" orr r3,r0,#0x20 ;@ Supervisor mode + IRQ level\n");
\r
417 ot(" bic r2,r2,#3 ;@ clear stopped and trace states\n");
\r
418 #if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO
\r
419 ot(" orr r2,r2,#4 ;@ set activity bit: 'not processing instruction'\n");
\r
421 ot(" str r2,[r7,#0x58]\n");
\r
422 ot(" ldrb r6,[r7,#0x44] ;@ Get old SR high, abuse r6\n");
\r
423 ot(" strb r3,[r7,#0x44] ;@ Put new SR high\n");
\r
426 // 3. Save the current processor context.
\r
427 ot(" ldr r1,[r7,#0x60] ;@ Get Memory base\n");
\r
428 ot(" ldr r11,[r7,#0x3c] ;@ Get A7\n");
\r
429 ot(" tst r6,#0x20\n");
\r
430 ot(";@ get our SP:\n");
\r
431 ot(" ldreq r2,[r7,#0x48] ;@ ...or OSP as our stack pointer\n");
\r
432 ot(" streq r11,[r7,#0x48]\n");
\r
433 ot(" moveq r11,r2\n");
\r
434 ot(";@ Push old PC onto stack\n");
\r
435 ot(" sub r0,r11,#4 ;@ Predecremented A7\n");
\r
436 ot(" sub r1,r4,r1 ;@ r1 = Old PC\n");
\r
438 ot(";@ Push old SR:\n");
\r
439 ot(" ldr r0,[r7,#0x4c] ;@ X bit\n");
\r
440 ot(" mov r1,r10,lsr #28 ;@ ____NZCV\n");
\r
441 ot(" eor r2,r1,r1,ror #1 ;@ Bit 0=C^V\n");
\r
442 ot(" tst r2,#1 ;@ 1 if C!=V\n");
\r
443 ot(" eorne r1,r1,#3 ;@ ____NZVC\n");
\r
444 ot(" and r0,r0,#0x20000000\n");
\r
445 ot(" orr r1,r1,r0,lsr #25 ;@ ___XNZVC\n");
\r
446 ot(" orr r1,r1,r6,lsl #8 ;@ Include old SR high\n");
\r
447 ot(" sub r0,r11,#6 ;@ Predecrement A7\n");
\r
448 ot(" str r0,[r7,#0x3c] ;@ Save A7\n");
\r
449 MemHandler(1,1,0,0); // already checked for address error by prev MemHandler
\r
452 // 2. Obtain the exception vector.
\r
453 ot(" mov r11,r8,lsr #29\n");
\r
454 ot(" mov r0,r11\n");
\r
455 #if USE_INT_ACK_CALLBACK
\r
456 ot(";@ call IrqCallback if it is defined\n");
\r
457 #if INT_ACK_NEEDS_STUFF
\r
458 ot(" str r4,[r7,#0x40] ;@ Save PC\n");
\r
459 ot(" mov r1,r10,lsr #28\n");
\r
460 ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");
\r
461 ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");
\r
463 ot(" ldr r3,[r7,#0x8c] ;@ IrqCallback\n");
\r
464 ot(" add lr,pc,#4*3\n");
\r
465 ot(" tst r3,r3\n");
\r
466 ot(" streqb r3,[r7,#0x47] ;@ just clear IRQ if there is no callback\n");
\r
467 ot(" mvneq r0,#0 ;@ and simulate -1 return\n");
\r
469 #if INT_ACK_CHANGES_CYCLES
\r
470 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");
\r
472 ot(";@ get IRQ vector address:\n");
\r
473 ot(" cmn r0,#1 ;@ returned -1?\n");
\r
474 ot(" addeq r0,r11,#0x18 ;@ use autovector then\n");
\r
475 ot(" cmn r0,#2 ;@ returned -2?\n"); // should be safe as above add should never result in -2
\r
476 ot(" moveq r0,#0x18 ;@ use spurious interrupt then\n");
\r
477 #else // !USE_INT_ACK_CALLBACK
\r
478 ot(";@ Clear irq:\n");
\r
479 ot(" mov r2,#0\n");
\r
480 ot(" strb r2,[r7,#0x47]\n");
\r
481 ot(" add r0,r0,#0x18 ;@ use autovector\n");
\r
483 ot(" mov r0,r0,lsl #2 ;@ get vector address\n");
\r
485 ot(" ldr r11,[r7,#0x60] ;@ Get Memory base\n");
\r
486 ot(";@ Read IRQ Vector:\n");
\r
487 MemHandler(0,2,0,0);
\r
488 ot(" tst r0,r0 ;@ uninitialized int vector?\n");
\r
489 ot(" moveq r0,#0x3c\n");
\r
490 ot(" moveq lr,pc\n");
\r
491 ot(" ldreq pc,[r7,#0x70] ;@ Call read32(r0) handler\n");
\r
492 #if USE_CHECKPC_CALLBACK
\r
493 ot(" add lr,pc,#4\n");
\r
494 ot(" add r0,r0,r11 ;@ r0 = Memory Base + New PC\n");
\r
495 ot(" ldr pc,[r7,#0x64] ;@ Call checkpc()\n");
\r
496 #if EMULATE_ADDRESS_ERRORS_JUMP
\r
497 ot(" mov r4,r0\n");
\r
499 ot(" bic r4,r0,#1\n");
\r
502 ot(" add r4,r0,r11 ;@ r4 = Memory Base + New PC\n");
\r
503 #if EMULATE_ADDRESS_ERRORS_JUMP
\r
504 ot(" bic r4,r4,#1\n");
\r
509 // 4. Obtain a new context and resume instruction processing.
\r
510 // note: the obtain part was already done in previous steps
\r
511 #if EMULATE_ADDRESS_ERRORS_JUMP
\r
512 ot(" tst r4,#1\n");
\r
513 ot(" bne ExceptionAddressError_r_prg_r4\n");
\r
515 ot(" ldr r6,[r7,#0x54]\n");
\r
516 ot(" ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
\r
517 ot(" subs r5,r5,#44 ;@ Subtract cycles\n");
\r
518 ot(" ldrge pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
\r
519 ot(" b CycloneEnd\n");
\r
523 // trashes all temp regs
\r
524 ot("Exception%s\n", ms?"":":");
\r
525 ot(" ;@ Cause an Exception - Vector number in r0\n");
\r
526 ot(" mov r11,lr ;@ Preserve ARM return address\n");
\r
527 ot(" bic r8,r8,#0xff000000\n");
\r
528 ot(" orr r8,r8,r0,lsl #24 ;@ abuse r8\n");
\r
530 // 1. Make a temporary copy of the status register and set the status register for exception processing.
\r
531 ot(" ldr r6,[r7,#0x44] ;@ Get old SR high, abuse r6\n");
\r
532 ot(" ldr r2,[r7,#0x58] ;@ state flags\n");
\r
533 ot(" and r3,r6,#0x27 ;@ clear trace and unused flags\n");
\r
534 ot(" orr r3,r3,#0x20 ;@ set supervisor mode\n");
\r
535 ot(" bic r2,r2,#3 ;@ clear stopped and trace states\n");
\r
536 ot(" str r2,[r7,#0x58]\n");
\r
537 ot(" strb r3,[r7,#0x44] ;@ Put new SR high\n");
\r
540 // 3. Save the current processor context.
\r
541 ot(" ldr r0,[r7,#0x3c] ;@ Get A7\n");
\r
542 ot(" tst r6,#0x20\n");
\r
543 ot(";@ get our SP:\n");
\r
544 ot(" ldreq r2,[r7,#0x48] ;@ ...or OSP as our stack pointer\n");
\r
545 ot(" streq r0,[r7,#0x48]\n");
\r
546 ot(" moveq r0,r2\n");
\r
547 ot(";@ Push old PC onto stack\n");
\r
548 ot(" ldr r1,[r7,#0x60] ;@ Get Memory base\n");
\r
549 ot(" sub r0,r0,#4 ;@ Predecremented A7\n");
\r
550 ot(" str r0,[r7,#0x3c] ;@ Save A7\n");
\r
551 ot(" sub r1,r4,r1 ;@ r1 = Old PC\n");
\r
553 ot(";@ Push old SR:\n");
\r
554 ot(" ldr r0,[r7,#0x4c] ;@ X bit\n");
\r
555 ot(" mov r1,r10,lsr #28 ;@ ____NZCV\n");
\r
556 ot(" eor r2,r1,r1,ror #1 ;@ Bit 0=C^V\n");
\r
557 ot(" tst r2,#1 ;@ 1 if C!=V\n");
\r
558 ot(" eorne r1,r1,#3 ;@ ____NZVC\n");
\r
559 ot(" and r0,r0,#0x20000000\n");
\r
560 ot(" orr r1,r1,r0,lsr #25 ;@ ___XNZVC\n");
\r
561 ot(" ldr r0,[r7,#0x3c] ;@ A7\n");
\r
562 ot(" orr r1,r1,r6,lsl #8 ;@ Include SR high\n");
\r
563 ot(" sub r0,r0,#2 ;@ Predecrement A7\n");
\r
564 ot(" str r0,[r7,#0x3c] ;@ Save A7\n");
\r
565 MemHandler(1,1,0,0);
\r
568 // 2. Obtain the exception vector
\r
569 ot(";@ Read Exception Vector:\n");
\r
570 ot(" mov r0,r8,lsr #24\n");
\r
571 ot(" mov r0,r0,lsl #2\n");
\r
572 MemHandler(0,2,0,0);
\r
573 ot(" ldr r3,[r7,#0x60] ;@ Get Memory base\n");
\r
574 #if USE_CHECKPC_CALLBACK
\r
575 ot(" add lr,pc,#4\n");
\r
576 ot(" add r0,r0,r3 ;@ r0 = Memory Base + New PC\n");
\r
577 ot(" ldr pc,[r7,#0x64] ;@ Call checkpc()\n");
\r
578 #if EMULATE_ADDRESS_ERRORS_JUMP
\r
579 ot(" mov r4,r0\n");
\r
581 ot(" bic r4,r0,#1\n");
\r
584 ot(" add r4,r0,r3 ;@ r4 = Memory Base + New PC\n");
\r
585 #if EMULATE_ADDRESS_ERRORS_JUMP
\r
586 ot(" bic r4,r4,#1\n");
\r
591 // 4. Resume execution.
\r
592 #if EMULATE_ADDRESS_ERRORS_JUMP
\r
593 ot(" tst r4,#1\n");
\r
594 ot(" bne ExceptionAddressError_r_prg_r4\n");
\r
596 ot(" ldr r6,[r7,#0x54]\n");
\r
597 ot(" bx r11 ;@ Return\n");
\r
601 #if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO
\r
602 // first some wrappers: I see no point inlining this code,
\r
603 // as it will be executed in really rare cases.
\r
604 AddressErrorWrapper('r', "data", 0x11);
\r
605 AddressErrorWrapper('r', "prg", 0x12);
\r
606 AddressErrorWrapper('w', "data", 0x01);
\r
607 // there are no program writes
\r
608 // cpu space is only for bus errors?
\r
609 ot("ExceptionAddressError_r_prg_r4%s\n", ms?"":":");
\r
610 ot(" ldr r1,[r7,#0x44]\n");
\r
611 ot(" ldr r3,[r7,#0x60] ;@ Get Memory base\n");
\r
612 ot(" mov r6,#0x12\n");
\r
613 ot(" sub r11,r4,r3\n");
\r
614 ot(" tst r1,#0x20\n");
\r
615 ot(" orrne r6,r6,#4\n");
\r
618 ot("ExceptionAddressError%s\n", ms?"":":");
\r
619 ot(";@ r6 - info word (without instruction/not bit), r11 - faulting address\n");
\r
621 // 1. Make a temporary copy of the status register and set the status register for exception processing.
\r
622 ot(" ldrb r0,[r7,#0x44] ;@ Get old SR high\n");
\r
623 ot(" ldr r2,[r7,#0x58] ;@ state flags\n");
\r
624 ot(" and r3,r0,#0x27 ;@ clear trace and unused flags\n");
\r
625 ot(" orr r3,r3,#0x20 ;@ set supervisor mode\n");
\r
626 ot(" strb r3,[r7,#0x44] ;@ Put new SR high\n");
\r
627 ot(" bic r2,r2,#3 ;@ clear stopped and trace states\n");
\r
628 ot(" tst r2,#4\n");
\r
629 ot(" orrne r6,r6,#8 ;@ complete info word\n");
\r
630 ot(" orr r2,r2,#4 ;@ set activity bit: 'not processing instruction'\n");
\r
632 ot(" tst r2,#8\n");
\r
633 ot(" orrne r2,r2,#0x10 ;@ HALT\n");
\r
634 ot(" orr r2,r2,#8 ;@ processing address error\n");
\r
635 ot(" str r2,[r7,#0x58]\n");
\r
636 ot(" movne r5,#0\n");
\r
637 ot(" bne CycloneEndNoBack ;@ bye bye\n");
\r
639 ot(" str r2,[r7,#0x58]\n");
\r
641 ot(" and r10,r10,#0xf0000000\n");
\r
642 ot(" orr r10,r10,r0,lsl #4 ;@ some preparations for SR push\n");
\r
645 // 3. Save the current processor context + additional information.
\r
646 ot(" ldr r0,[r7,#0x3c] ;@ Get A7\n");
\r
647 ot(" tst r10,#0x200\n");
\r
648 ot(";@ get our SP:\n");
\r
649 ot(" ldreq r2,[r7,#0x48] ;@ ...or OSP as our stack pointer\n");
\r
650 ot(" streq r0,[r7,#0x48]\n");
\r
651 ot(" moveq r0,r2\n");
\r
653 ot(";@ Push old PC onto stack\n");
\r
654 ot(" ldr r1,[r7,#0x60] ;@ Get Memory base\n");
\r
655 ot(" sub r0,r0,#4 ;@ Predecremented A7\n");
\r
656 ot(" sub r1,r4,r1 ;@ r1 = Old PC\n");
\r
657 ot(" str r0,[r7,#0x3c] ;@ Save A7\n");
\r
658 MemHandler(1,2,0,EMULATE_HALT);
\r
660 ot(";@ Push old SR:\n");
\r
661 ot(" ldr r0,[r7,#0x4c] ;@ X bit\n");
\r
662 ot(" mov r1,r10,ror #28 ;@ ____NZCV\n");
\r
663 ot(" eor r2,r1,r1,ror #1 ;@ Bit 0=C^V\n");
\r
664 ot(" tst r2,#1 ;@ 1 if C!=V\n");
\r
665 ot(" eorne r1,r1,#3 ;@ ____NZVC\n");
\r
666 ot(" and r0,r0,#0x20000000\n");
\r
667 ot(" orr r1,r1,r0,lsr #25 ;@ ___XNZVC\n");
\r
668 ot(" ldr r0,[r7,#0x3c] ;@ A7\n");
\r
669 ot(" and r10,r10,#0xf0000000\n");
\r
670 ot(" sub r0,r0,#2 ;@ Predecrement A7\n");
\r
671 ot(" str r0,[r7,#0x3c] ;@ Save A7\n");
\r
672 MemHandler(1,1,0,0);
\r
673 // IR (instruction register)
\r
674 ot(";@ Push IR:\n");
\r
675 ot(" ldr r0,[r7,#0x3c] ;@ A7\n");
\r
676 ot(" mov r1,r8\n");
\r
677 ot(" sub r0,r0,#2 ;@ Predecrement A7\n");
\r
678 ot(" str r0,[r7,#0x3c] ;@ Save A7\n");
\r
679 MemHandler(1,1,0,0);
\r
681 ot(";@ Push address:\n");
\r
682 ot(" ldr r0,[r7,#0x3c] ;@ A7\n");
\r
683 ot(" mov r1,r11\n");
\r
684 ot(" sub r0,r0,#4 ;@ Predecrement A7\n");
\r
685 ot(" str r0,[r7,#0x3c] ;@ Save A7\n");
\r
686 MemHandler(1,2,0,0);
\r
687 // information word
\r
688 ot(";@ Push info word:\n");
\r
689 ot(" ldr r0,[r7,#0x3c] ;@ A7\n");
\r
690 ot(" mov r1,r6\n");
\r
691 ot(" sub r0,r0,#2 ;@ Predecrement A7\n");
\r
692 ot(" str r0,[r7,#0x3c] ;@ Save A7\n");
\r
693 MemHandler(1,1,0,0);
\r
696 // 2. Obtain the exception vector
\r
697 ot(";@ Read Exception Vector:\n");
\r
698 ot(" mov r0,#0x0c\n");
\r
699 MemHandler(0,2,0,0);
\r
700 ot(" ldr r3,[r7,#0x60] ;@ Get Memory base\n");
\r
701 #if USE_CHECKPC_CALLBACK
\r
702 ot(" add lr,pc,#4\n");
\r
703 ot(" add r0,r0,r3 ;@ r0 = Memory Base + New PC\n");
\r
704 ot(" ldr pc,[r7,#0x64] ;@ Call checkpc()\n");
\r
705 ot(" mov r4,r0\n");
\r
707 ot(" add r4,r0,r3 ;@ r4 = Memory Base + New PC\n");
\r
711 #if EMULATE_ADDRESS_ERRORS_JUMP && EMULATE_HALT
\r
712 ot(" tst r4,#1\n");
\r
713 ot(" bne ExceptionAddressError_r_prg_r4\n");
\r
715 ot(" bic r4,r4,#1\n");
\r
718 // 4. Resume execution.
\r
719 ot(" ldr r6,[r7,#0x54]\n");
\r
720 ot(" ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
\r
721 ot(" subs r5,r5,#50 ;@ Subtract cycles\n");
\r
722 ot(" ldrge pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
\r
723 ot(" b CycloneEnd\n");
\r
729 // expects srh and irq level in r1, next opcode already fetched to r8
\r
730 ot("CycloneDoTraceWithChecks%s\n", ms?"":":");
\r
731 ot(" ldr r0,[r7,#0x58]\n");
\r
732 ot(" cmp r5,#0\n");
\r
733 ot(" orr r0,r0,#2 ;@ go to trace mode\n");
\r
734 ot(" str r0,[r7,#0x58]\n");
\r
735 ot(" blt CycloneEnd\n"); // should take care of situation where we come here when already tracing
\r
736 ot(";@ CheckInterrupt:\n");
\r
737 ot(" movs r0,r1,lsr #24 ;@ Get IRQ level\n");
\r
738 ot(" beq CycloneDoTrace\n");
\r
739 ot(" cmp r0,#6 ;@ irq>6 ?\n");
\r
740 ot(" andle r1,r1,#7 ;@ Get interrupt mask\n");
\r
741 ot(" cmple r0,r1 ;@ irq<=6: Is irq<=mask ?\n");
\r
742 ot(" bgt CycloneDoInterruptGoBack\n");
\r
745 // expects next opcode to be already fetched to r8
\r
746 ot("CycloneDoTrace%s\n", ms?"":":");
\r
747 ot(" str r5,[r7,#0x9c] ;@ save cycles\n");
\r
748 ot(" ldr r1,[r7,#0x98]\n");
\r
749 ot(" mov r5,#0\n");
\r
750 ot(" str r1,[r7,#0xa0]\n");
\r
751 ot(" adr r0,TraceEnd\n");
\r
752 ot(" str r0,[r7,#0x98] ;@ store TraceEnd as CycloneEnd hadler\n");
\r
753 ot(" ldr pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
\r
756 ot("TraceEnd%s\n", ms?"":":");
\r
757 ot(" ldr r2,[r7,#0x58]\n");
\r
758 ot(" ldr r0,[r7,#0x9c] ;@ restore cycles\n");
\r
759 ot(" ldr r1,[r7,#0xa0] ;@ old CycloneEnd handler\n");
\r
760 ot(" mov r10,r10,lsl #28\n");
\r
761 ot(" add r5,r0,r5\n");
\r
762 ot(" str r1,[r7,#0x98]\n");
\r
763 ot(";@ still tracing?\n"); // exception might have happend
\r
764 ot(" tst r2,#2\n");
\r
765 ot(" beq TraceDisabled\n");
\r
766 ot(";@ trace exception\n");
\r
767 #if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO
\r
768 ot(" ldr r1,[r7,#0x58]\n");
\r
769 ot(" mov r0,#9\n");
\r
770 ot(" orr r1,r1,#4 ;@ set activity bit: 'not processing instruction'\n");
\r
771 ot(" str r1,[r7,#0x58]\n");
\r
773 ot(" mov r0,#9\n");
\r
775 ot(" bl Exception\n");
\r
776 ot(" ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
\r
777 ot(" subs r5,r5,#34 ;@ Subtract cycles\n");
\r
778 ot(" ldrge pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
\r
779 ot(" b CycloneEnd\n");
\r
781 ot("TraceDisabled%s\n", ms?"":":");
\r
782 ot(" ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
\r
783 ot(" cmp r5,#0\n");
\r
784 ot(" ldrge pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
\r
785 ot(" b CycloneEnd\n");
\r
790 // ---------------------------------------------------------------------------
\r
791 // Call Read(r0), Write(r0,r1) or Fetch(r0)
\r
792 // Trashes r0-r3,r12,lr
\r
793 int MemHandler(int type,int size,int addrreg,int need_addrerr_check)
\r
796 func=0x68+type*0xc+(size<<2); // Find correct offset
\r
798 #if MEMHANDLERS_NEED_FLAGS
\r
799 ot(" mov r3,r10,lsr #28\n");
\r
800 ot(" strb r3,[r7,#0x46] ;@ Save Flags (NZCV)\n");
\r
804 #if (MEMHANDLERS_ADDR_MASK & 0xff000000)
\r
805 ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0xff000000);
\r
808 #if (MEMHANDLERS_ADDR_MASK & 0x00ff0000)
\r
809 ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x00ff0000);
\r
812 #if (MEMHANDLERS_ADDR_MASK & 0x0000ff00)
\r
813 ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x0000ff00);
\r
816 #if (MEMHANDLERS_ADDR_MASK & 0x000000ff)
\r
817 ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x000000ff);
\r
821 #if EMULATE_ADDRESS_ERRORS_IO
\r
822 if (size > 0 && need_addrerr_check)
\r
824 ot(" add lr,pc,#4*%i\n", addrreg==0?2:3); // helps to prevent interlocks
\r
825 if (addrreg != 0) ot(" mov r0,r%i\n", addrreg);
\r
826 ot(" tst r0,#1 ;@ address error?\n");
\r
828 case 0: ot(" bne ExceptionAddressError_r_data\n"); break;
\r
829 case 1: ot(" bne ExceptionAddressError_w_data\n"); break;
\r
830 case 2: ot(" bne ExceptionAddressError_r_prg\n"); break;
\r
837 ot(" add lr,pc,#4\n");
\r
838 ot(" mov r0,r%i\n", addrreg);
\r
841 ot(" mov lr,pc\n");
\r
842 ot(" ldr pc,[r7,#0x%x] ;@ Call ",func);
\r
844 // Document what we are calling:
\r
845 if (type==0) ot("read");
\r
846 if (type==1) ot("write");
\r
847 if (type==2) ot("fetch");
\r
849 if (type==1) ot("%d(r0,r1)",8<<size);
\r
850 else ot("%d(r0)", 8<<size);
\r
853 #if MEMHANDLERS_CHANGE_FLAGS
\r
854 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n");
\r
855 ot(" mov r10,r10,lsl #28\n");
\r
857 #if MEMHANDLERS_CHANGE_PC
\r
858 ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");
\r
864 static void PrintOpcodes()
\r
868 printf("Creating Opcodes: [");
\r
870 ot(";@ ---------------------------- Opcodes ---------------------------\n");
\r
872 // Emit null opcode:
\r
873 ot("Op____%s ;@ Called if an opcode is not recognised\n", ms?"":":");
\r
874 #if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO
\r
875 ot(" ldr r1,[r7,#0x58]\n");
\r
876 ot(" sub r4,r4,#2\n");
\r
877 ot(" orr r1,r1,#4 ;@ set activity bit: 'not processing instruction'\n");
\r
878 ot(" str r1,[r7,#0x58]\n");
\r
880 ot(" sub r4,r4,#2\n");
\r
882 #if USE_UNRECOGNIZED_CALLBACK
\r
883 ot(" str r4,[r7,#0x40] ;@ Save PC\n");
\r
884 ot(" mov r1,r10,lsr #28\n");
\r
885 ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");
\r
886 ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");
\r
887 ot(" ldr r11,[r7,#0x94] ;@ UnrecognizedCallback\n");
\r
888 ot(" tst r11,r11\n");
\r
889 ot(" movne lr,pc\n");
\r
890 ot(" movne pc,r11 ;@ call UnrecognizedCallback if it is defined\n");
\r
891 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n");
\r
892 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");
\r
893 ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");
\r
894 ot(" mov r10,r10,lsl #28\n");
\r
895 ot(" tst r0,r0\n");
\r
896 ot(" moveq r0,#4\n");
\r
897 ot(" bleq Exception\n");
\r
899 ot(" mov r0,#4\n");
\r
900 ot(" bl Exception\n");
\r
906 // Unrecognised a-line and f-line opcodes throw an exception:
\r
907 ot("Op__al%s ;@ Unrecognised a-line opcode\n", ms?"":":");
\r
908 ot(" sub r4,r4,#2\n");
\r
909 #if USE_AFLINE_CALLBACK
\r
910 ot(" str r4,[r7,#0x40] ;@ Save PC\n");
\r
911 ot(" mov r1,r10,lsr #28\n");
\r
912 ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");
\r
913 ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");
\r
914 ot(" ldr r11,[r7,#0x94] ;@ UnrecognizedCallback\n");
\r
915 ot(" tst r11,r11\n");
\r
916 ot(" movne lr,pc\n");
\r
917 ot(" movne pc,r11 ;@ call UnrecognizedCallback if it is defined\n");
\r
918 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n");
\r
919 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");
\r
920 ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");
\r
921 ot(" mov r10,r10,lsl #28\n");
\r
922 ot(" tst r0,r0\n");
\r
923 ot(" moveq r0,#0x0a\n");
\r
924 ot(" bleq Exception\n");
\r
926 ot(" mov r0,#0x0a\n");
\r
927 ot(" bl Exception\n");
\r
933 ot("Op__fl%s ;@ Unrecognised f-line opcode\n", ms?"":":");
\r
934 ot(" sub r4,r4,#2\n");
\r
935 #if USE_AFLINE_CALLBACK
\r
936 ot(" str r4,[r7,#0x40] ;@ Save PC\n");
\r
937 ot(" mov r1,r10,lsr #28\n");
\r
938 ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");
\r
939 ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");
\r
940 ot(" ldr r11,[r7,#0x94] ;@ UnrecognizedCallback\n");
\r
941 ot(" tst r11,r11\n");
\r
942 ot(" movne lr,pc\n");
\r
943 ot(" movne pc,r11 ;@ call UnrecognizedCallback if it is defined\n");
\r
944 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n");
\r
945 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");
\r
946 ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");
\r
947 ot(" mov r10,r10,lsl #28\n");
\r
948 ot(" tst r0,r0\n");
\r
949 ot(" moveq r0,#0x0b\n");
\r
950 ot(" bleq Exception\n");
\r
952 ot(" mov r0,#0x0b\n");
\r
953 ot(" bl Exception\n");
\r
960 for (op=0;op<0x10000;op++)
\r
962 if ((op&0xfff)==0) { printf("%x",op>>12); fflush(stdout); } // Update progress
\r
973 static void ott(const char *str, int par, const char *nl, int nlp, int counter, int size)
\r
976 case 0: if((counter&7)==0) ot(ms?" dcb ":" .byte "); break;
\r
977 case 1: if((counter&7)==0) ot(ms?" dcw ":" .hword "); break;
\r
978 case 2: if((counter&7)==0) ot(ms?" dcd ":" .long "); break;
\r
981 if((counter&7)==7) ot(nl,nlp); else ot(",");
\r
984 static void PrintJumpTable()
\r
986 int i=0,op=0,len=0;
\r
988 ot(";@ -------------------------- Jump Table --------------------------\n");
\r
990 // space for decompressed table
\r
991 ot(ms?" area |.data|, data\n":" .data\n .align 4\n\n");
\r
993 #if COMPRESS_JUMPTABLE
\r
994 int handlers=0,reps=0,*indexes,ip,u,out;
\r
995 // use some weird compression on the jump table
\r
996 indexes=(int *)malloc(0x10000*4);
\r
997 if(!indexes) { printf("ERROR: out of memory\n"); exit(1); }
\r
1000 ot("CycloneJumpTab%s\n", ms?"":":");
\r
1002 for(i = 0; i < 0xa000/8; i++)
\r
1003 ot(" dcd 0,0,0,0,0,0,0,0\n");
\r
1005 ot(" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", 0xa000/8);
\r
1007 // hanlers live in "a-line" part of the table
\r
1008 // first output nop,a-line,f-line handlers
\r
1009 ot(ms?" dcd Op____,Op__al,Op__fl,":" .long Op____,Op__al,Op__fl,");
\r
1012 for(i=0;i<len;i++)
\r
1016 for(u=i-1; u>=0; u--) if(op == CyJump[u]) break; // already done with this op?
\r
1017 if(u==-1 && op >= 0) {
\r
1018 ott("Op%.4x",op," ;@ %.4x\n",i,handlers,2);
\r
1019 indexes[op] = handlers;
\r
1024 fseek(AsmFile, -1, SEEK_CUR); // remove last comma
\r
1025 for(i = 8-(handlers&7); i > 0; i--)
\r
1030 for(i = (0x4000-handlers)/8; i > 0; i--)
\r
1031 ot(" dcd 0,0,0,0,0,0,0,0\n");
\r
1033 ot(ms?"":" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", (0x4000-handlers)/8);
\r
1035 printf("total distinct hanlers: %i\n",handlers);
\r
1037 for(i=0,ip=0; i < 0xf000; i++, ip++) {
\r
1040 // it must skip a-line area, because we keep our data there
\r
1041 ott("0x%.4x", handlers<<4, "\n",0,ip++,1);
\r
1042 ott("0x%.4x", 0x1000, "\n",0,ip,1);
\r
1046 for(reps=1; i < 0xf000; i++, reps++) if(op != CyJump[i+1]) break;
\r
1047 if(op>=0) out=indexes[op]<<4; else out=0; // unrecognised
\r
1048 if(reps <= 0xe || reps==0x10) {
\r
1049 if(reps!=0x10) out|=reps; else out|=0xf; // 0xf means 0x10 (0xf appeared to be unused anyway)
\r
1050 ott("0x%.4x", out, "\n",0,ip,1);
\r
1052 ott("0x%.4x", out, "\n",0,ip++,1);
\r
1053 ott("0x%.4x", reps,"\n",0,ip,1);
\r
1056 if(ip&1) ott("0x%.4x", 0, "\n",0,ip++,1);
\r
1057 if(ip&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma
\r
1059 for(i = 8-(ip&7); i > 0; i--)
\r
1064 for(i = (0x2000-ip/2)/8+1; i > 0; i--)
\r
1065 ot(" dcd 0,0,0,0,0,0,0,0\n");
\r
1067 ot(" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", (0x2000-ip/2)/8+1);
\r
1072 ot("CycloneJumpTab%s\n", ms?"":":");
\r
1073 len=0xfffe; // Hmmm, armasm 2.50.8684 messes up with a 0x10000 long jump table
\r
1074 // notaz: same thing with GNU as 2.9-psion-98r2 (reloc overflow)
\r
1075 // this is due to COFF objects using only 2 bytes for reloc count
\r
1077 for (i=0;i<len;i++)
\r
1081 if(op>=0) ott("Op%.4x",op," ;@ %.4x\n",i-7,i,2);
\r
1082 else if(op==-2) ott("Op__al",0, " ;@ %.4x\n",i-7,i,2);
\r
1083 else if(op==-3) ott("Op__fl",0, " ;@ %.4x\n",i-7,i,2);
\r
1084 else ott("Op____",0, " ;@ %.4x\n",i-7,i,2);
\r
1086 if(i&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma
\r
1089 ot(";@ notaz: we don't want to crash if we run into those 2 missing opcodes\n");
\r
1090 ot(";@ so we leave this pattern to patch it later\n");
\r
1091 ot("%s 0x78563412\n", ms?" dcd":" .long");
\r
1092 ot("%s 0x56341290\n", ms?" dcd":" .long");
\r
1096 static int CycloneMake()
\r
1099 char *name="Cyclone.s";
\r
1100 const char *globl=ms?"export":".global";
\r
1102 // Open the assembly file
\r
1103 if (ms) name="Cyclone.asm";
\r
1104 AsmFile=fopen(name,"wt"); if (AsmFile==NULL) return 1;
\r
1106 printf("Making %s...\n",name);
\r
1108 ot("\n;@ Dave's Cyclone 68000 Emulator v%x.%.3x - Assembler Output\n\n",CycloneVer>>12,CycloneVer&0xfff);
\r
1110 ot(";@ (c) Copyright 2003 Dave, All rights reserved.\n");
\r
1111 ot(";@ some code (c) Copyright 2005-2007 notaz, All rights reserved.\n");
\r
1112 ot(";@ Cyclone 68000 is free for non-commercial use.\n\n");
\r
1113 ot(";@ For commercial use, separate licencing terms must be obtained.\n\n");
\r
1115 CyJump=(int *)malloc(0x40000); if (CyJump==NULL) return 1;
\r
1116 memset(CyJump,0xff,0x40000); // Init to -1
\r
1117 for(i=0xa000; i<0xb000; i++) CyJump[i] = -2; // a-line emulation
\r
1118 for(i=0xf000; i<0x10000; i++) CyJump[i] = -3; // f-line emulation
\r
1120 ot(ms?" area |.text|, code\n":" .text\n .align 4\n\n");
\r
1121 ot(" %s CycloneInit\n",globl);
\r
1122 ot(" %s CycloneRun\n",globl);
\r
1123 ot(" %s CycloneSetSr\n",globl);
\r
1124 ot(" %s CycloneGetSr\n",globl);
\r
1125 ot(" %s CycloneFlushIrq\n",globl);
\r
1126 ot(" %s CyclonePack\n",globl);
\r
1127 ot(" %s CycloneUnpack\n",globl);
\r
1128 ot(" %s CycloneVer\n",globl);
\r
1129 #if (CYCLONE_FOR_GENESIS == 2)
\r
1130 ot(" %s CycloneSetRealTAS\n",globl);
\r
1131 ot(" %s CycloneDoInterrupt\n",globl);
\r
1132 ot(" %s CycloneDoTrace\n",globl);
\r
1133 ot(" %s CycloneJumpTab\n",globl);
\r
1136 ot(ms?"CycloneVer dcd 0x":"CycloneVer: .long 0x");
\r
1137 ot("%.4x\n",CycloneVer);
\r
1143 printf("~%i ARM instructions used for opcode handlers\n", arm_op_count);
\r
1146 if (ms) ot(" END\n");
\r
1148 ot("\n\n;@ vim:filetype=armasm\n");
\r
1150 fclose(AsmFile); AsmFile=NULL;
\r
1153 printf("Assembling...\n");
\r
1154 // Assemble the file
\r
1155 if (ms) system("armasm Cyclone.asm");
\r
1156 else system("as -o Cyclone.o Cyclone.s");
\r
1157 printf("Done!\n\n");
\r
1166 printf("\n Dave's Cyclone 68000 Emulator v%x.%.3x - Core Creator\n\n",CycloneVer>>12,CycloneVer&0xfff);
\r
1168 // Make GAS or ARMASM version
\r