updated Cyclone not to use r9
[picodrive.git] / cpu / Cyclone / Main.cpp
CommitLineData
cc68a136 1\r
2#include "app.h"\r
3\r
4static FILE *AsmFile=NULL;\r
5\r
449ecf92 6static int CycloneVer=0x0099; // Version number of library\r
cc68a136 7int *CyJump=NULL; // Jump table\r
8int ms=USE_MS_SYNTAX; // If non-zero, output in Microsoft ARMASM format\r
9char *Narm[4]={ "b", "h","",""}; // Normal ARM Extensions for operand sizes 0,1,2\r
10char *Sarm[4]={"sb","sh","",""}; // Sign-extend ARM Extensions for operand sizes 0,1,2\r
11int Cycles; // Current cycles for opcode\r
cfb3dfa0 12int pc_dirty; // something changed PC during processing\r
ee5e024c 13int arm_op_count;\r
cc68a136 14\r
15\r
16void ot(const char *format, ...)\r
17{\r
18 va_list valist=NULL;\r
19 int i, len;\r
20\r
21 // notaz: stop me from leaving newlines in the middle of format string\r
22 // and generating bad code\r
23 for(i=0, len=strlen(format); i < len && format[i] != '\n'; i++);\r
24 if(i < len-1 && format[len-1] != '\n') printf("\nWARNING: possible improper newline placement:\n%s\n", format);\r
25\r
b637c56a 26 if (format[0] == ' ' && format[1] == ' ' && format[2] != ' ' && format[2] != '.')\r
27 arm_op_count++;\r
28\r
cc68a136 29 va_start(valist,format);\r
30 if (AsmFile) vfprintf(AsmFile,format,valist);\r
31 va_end(valist);\r
32}\r
33\r
34void ltorg()\r
35{\r
36 if (ms) ot(" LTORG\n");\r
37 else ot(" .ltorg\n");\r
38}\r
39\r
a9a5a6e0 40#if (CYCLONE_FOR_GENESIS == 2)\r
c008977e 41// r12=ptr to tas in table, trashes r0,r1\r
42static void ChangeTAS(int norm)\r
43{\r
44 ot(" ldr r0,=Op4ad0%s\n",norm?"_":"");\r
45 ot(" mov r1,#8\n");\r
46 ot("setrtas_loop%i0%s ;@ 4ad0-4ad7\n",norm,ms?"":":");\r
47 ot(" subs r1,r1,#1\n");\r
48 ot(" str r0,[r12],#4\n");\r
49 ot(" bne setrtas_loop%i0\n",norm);\r
50 ot(" ldr r0,=Op4ad8%s\n",norm?"_":"");\r
51 ot(" mov r1,#7\n");\r
52 ot("setrtas_loop%i1%s ;@ 4ad8-4ade\n",norm,ms?"":":");\r
53 ot(" subs r1,r1,#1\n");\r
54 ot(" str r0,[r12],#4\n");\r
55 ot(" bne setrtas_loop%i1\n",norm);\r
56 ot(" ldr r0,=Op4adf%s\n",norm?"_":"");\r
57 ot(" str r0,[r12],#4\n");\r
58 ot(" ldr r0,=Op4ae0%s\n",norm?"_":"");\r
59 ot(" mov r1,#7\n");\r
60 ot("setrtas_loop%i2%s ;@ 4ae0-4ae6\n",norm,ms?"":":");\r
61 ot(" subs r1,r1,#1\n");\r
62 ot(" str r0,[r12],#4\n");\r
63 ot(" bne setrtas_loop%i2\n",norm);\r
64 ot(" ldr r0,=Op4ae7%s\n",norm?"_":"");\r
65 ot(" str r0,[r12],#4\n");\r
66 ot(" ldr r0,=Op4ae8%s\n",norm?"_":"");\r
67 ot(" mov r1,#8\n");\r
68 ot("setrtas_loop%i3%s ;@ 4ae8-4aef\n",norm,ms?"":":");\r
69 ot(" subs r1,r1,#1\n");\r
70 ot(" str r0,[r12],#4\n");\r
71 ot(" bne setrtas_loop%i3\n",norm);\r
72 ot(" ldr r0,=Op4af0%s\n",norm?"_":"");\r
73 ot(" mov r1,#8\n");\r
74 ot("setrtas_loop%i4%s ;@ 4af0-4af7\n",norm,ms?"":":");\r
75 ot(" subs r1,r1,#1\n");\r
76 ot(" str r0,[r12],#4\n");\r
77 ot(" bne setrtas_loop%i4\n",norm);\r
78 ot(" ldr r0,=Op4af8%s\n",norm?"_":"");\r
79 ot(" str r0,[r12],#4\n");\r
80 ot(" ldr r0,=Op4af9%s\n",norm?"_":"");\r
81 ot(" str r0,[r12],#4\n");\r
82}\r
83#endif\r
84\r
0e11c502 85#if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO\r
86static void AddressErrorWrapper(char rw, char *dataprg, int iw)\r
cc68a136 87{\r
0e11c502 88 ot("ExceptionAddressError_%c_%s%s\n", rw, dataprg, ms?"":":");\r
89 ot(" ldr r1,[r7,#0x44]\n");\r
449ecf92 90 ot(" mov r6,#0x%02x\n", iw);\r
0e11c502 91 ot(" mov r11,r0\n");\r
92 ot(" tst r1,#0x20\n");\r
449ecf92 93 ot(" orrne r6,r6,#4\n");\r
0e11c502 94 ot(" b ExceptionAddressError\n");\r
cc68a136 95 ot("\n");\r
cc68a136 96}\r
0e11c502 97#endif\r
cc68a136 98\r
cfb3dfa0 99void FlushPC(void)\r
100{\r
101#if MEMHANDLERS_NEED_PC\r
102 if (pc_dirty)\r
103 ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r
104#endif\r
105 pc_dirty = 0;\r
106}\r
107\r
cc68a136 108static void PrintFramework()\r
109{\r
0e11c502 110 int state_flags_to_check = 1; // stopped\r
111#if EMULATE_TRACE\r
112 state_flags_to_check |= 2; // tracing\r
113#endif\r
114#if EMULATE_HALT\r
115 state_flags_to_check |= 0x10; // halted\r
116#endif\r
117\r
cc68a136 118 ot(";@ --------------------------- Framework --------------------------\n");\r
119 if (ms) ot("CycloneRun\n");\r
120 else ot("CycloneRun:\n");\r
121\r
449ecf92 122 ot(" stmdb sp!,{r4-r8,r10,r11,lr}\n");\r
cc68a136 123\r
124 ot(" mov r7,r0 ;@ r7 = Pointer to Cpu Context\n");\r
125 ot(" ;@ r0-3 = Temporary registers\n");\r
449ecf92 126 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Flags (NZCV)\n");\r
7336a99a 127 ot(" ldr r6,=CycloneJumpTab ;@ r6 = Opcode Jump table\n");\r
cc68a136 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
66fdc0f0 131 ot(" ldr r1,[r7,#0x44] ;@ Get SR high T_S__III and irq level\n");\r
449ecf92 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
cc68a136 135 ot("\n");\r
0e11c502 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
139#endif\r
cc68a136 140 ot(";@ CheckInterrupt:\n");\r
66fdc0f0 141 ot(" movs r0,r1,lsr #24 ;@ Get IRQ level\n"); // same as ldrb r0,[r7,#0x47]\r
cc68a136 142 ot(" beq NoInts0\n");\r
143 ot(" cmp r0,#6 ;@ irq>6 ?\n");\r
cc68a136 144 ot(" andle r1,r1,#7 ;@ Get interrupt mask\n");\r
145 ot(" cmple r0,r1 ;@ irq<=6: Is irq<=mask ?\n");\r
ee5e024c 146 ot(" bgt CycloneDoInterrupt\n");\r
cc68a136 147 ot("NoInts0%s\n", ms?"":":");\r
148 ot("\n");\r
0e11c502 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
cc68a136 152 ot(" ldrh r8,[r4],#2 ;@ Fetch first opcode\n");\r
0e11c502 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
155 ot("\n");\r
156 ot("CycloneSpecial%s\n", ms?"":":");\r
157#if EMULATE_TRACE\r
158 ot(" tst r0,#2 ;@ tracing?\n");\r
159 ot(" bne CycloneDoTrace\n");\r
160#endif\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
449ecf92 164 ot(" ldmia sp!,{r4-r8,r10,r11,pc} ;@ we are stopped, do nothing!\n");\r
cc68a136 165 ot("\n");\r
166 ot("\n");\r
167\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
0e11c502 172#if (CYCLONE_FOR_GENESIS == 2) || EMULATE_TRACE\r
173 ot(" ldr r1,[r7,#0x98]\n");\r
449ecf92 174 ot(" mov r10,r10,lsr #28\n");\r
7336a99a 175 ot(" tst r1,r1\n");\r
176 ot(" bxne r1 ;@ jump to alternative CycloneEnd\n");\r
177#else\r
449ecf92 178 ot(" mov r10,r10,lsr #28\n");\r
7336a99a 179#endif\r
cc68a136 180 ot(" str r4,[r7,#0x40] ;@ Save Current PC + Memory Base\n");\r
181 ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");\r
449ecf92 182 ot(" strb r10,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r
183 ot(" ldmia sp!,{r4-r8,r10,r11,pc}\n");\r
0e11c502 184 ltorg();\r
cc68a136 185 ot("\n");\r
cc68a136 186 ot("\n");\r
187\r
0e11c502 188 ot("CycloneInit%s\n", ms?"":":");\r
cc68a136 189#if COMPRESS_JUMPTABLE\r
0e11c502 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
233 ot(" bx lr\n");\r
234 ltorg();\r
cc68a136 235#else\r
0e11c502 236 ot(";@ do nothing\n");\r
237 ot(" bx lr\n");\r
cc68a136 238#endif\r
0e11c502 239 ot("\n");\r
240\r
241 // --------------\r
85a36a57 242 // 68k: XNZVC, ARM: NZCV\r
0e11c502 243 ot("CycloneSetSr%s\n", ms?"":":");\r
cc68a136 244 ot(" mov r2,r1,lsr #8\n");\r
0e11c502 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
248#if EMULATE_TRACE\r
249 ot(" and r2,r2,#0xa7 ;@ only defined bits\n");\r
250#else\r
251 ot(" and r2,r2,#0x27 ;@ only defined bits\n");\r
252#endif\r
cc68a136 253 ot(" strb r2,[r0,#0x44] ;@ set SR high\n");\r
85a36a57 254 ot(" mov r2,r1,lsl #25\n");\r
255 ot(" str r2,[r0,#0x4c] ;@ the X flag\n");\r
cc68a136 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
262 ot(" bx lr\n");\r
263 ot("\n");\r
264\r
0e11c502 265 // --------------\r
266 ot("CycloneGetSr%s\n", ms?"":":");\r
cc68a136 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
85a36a57 273 ot(" ldr r1,[r0,#0x4c] ;@ the X flag\n");\r
274 ot(" tst r1,#0x20000000\n");\r
cc68a136 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
278 ot(" bx lr\n");\r
279 ot("\n");\r
280\r
0e11c502 281 // --------------\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
312 ot("\n");\r
313\r
314 // --------------\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
333#else\r
334 ot(" ldr r1,[r4,#0x60] ;@ Memory base\n");\r
335 ot(" add r0,r0,r1 ;@ r0 = Memory Base + New PC\n");\r
336#endif\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
352 ot("\n");\r
353\r
354 // --------------\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
363 ot("\n");\r
449ecf92 364 ot(" stmdb sp!,{r4,r5,r7,r8,r10,r11,lr}\n");\r
0e11c502 365 ot(" mov r7,r0\n");\r
366 ot(" mov r0,r2\n");\r
449ecf92 367 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Flags (NZCV)\n");\r
0e11c502 368 ot(" mov r5,#0\n");\r
449ecf92 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
0e11c502 371 ot(" adr r2,CycloneFlushIrqEnd\n");\r
372 ot(" str r2,[r7,#0x98] ;@ set custom CycloneEnd\n");\r
373 ot(" b CycloneDoInterrupt\n");\r
374 ot("\n");\r
375 ot("CycloneFlushIrqEnd%s\n", ms?"":":");\r
376 ot(" rsb r0,r5,#0\n");\r
449ecf92 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
0e11c502 380 ot(" bx lr\n");\r
381 ot("\n");\r
382 ot("\n");\r
383\r
384 // --------------\r
385 ot("CycloneSetRealTAS%s\n", ms?"":":");\r
a9a5a6e0 386#if (CYCLONE_FOR_GENESIS == 2)\r
c008977e 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
392 ChangeTAS(1);\r
393 ot(" bx lr\n");\r
394 ot("setrtas_off%s\n",ms?"":":");\r
395 ChangeTAS(0);\r
396 ot(" bx lr\n");\r
397 ltorg();\r
c008977e 398#else\r
399 ot(" bx lr\n");\r
c008977e 400#endif\r
0e11c502 401 ot("\n");\r
c008977e 402\r
0e11c502 403 // --------------\r
404 ot(";@ DoInterrupt - r0=IRQ level\n");\r
ee5e024c 405 ot("CycloneDoInterruptGoBack%s\n", ms?"":":");\r
406 ot(" sub r4,r4,#2\n");\r
7336a99a 407 ot("CycloneDoInterrupt%s\n", ms?"":":");\r
0e11c502 408 ot(" bic r8,r8,#0xff000000\n");\r
409 ot(" orr r8,r8,r0,lsl #29 ;@ abuse r8\n");\r
410\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
420#endif\r
cc68a136 421 ot(" str r2,[r7,#0x58]\n");\r
449ecf92 422 ot(" ldrb r6,[r7,#0x44] ;@ Get old SR high, abuse r6\n");\r
0e11c502 423 ot(" strb r3,[r7,#0x44] ;@ Put new SR high\n");\r
424 ot("\n");\r
425\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
449ecf92 429 ot(" tst r6,#0x20\n");\r
0e11c502 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
437 MemHandler(1,2);\r
438 ot(";@ Push old SR:\n");\r
439 ot(" ldr r0,[r7,#0x4c] ;@ X bit\n");\r
449ecf92 440 ot(" mov r1,r10,lsr #28 ;@ ____NZCV\n");\r
0e11c502 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
449ecf92 446 ot(" orr r1,r1,r6,lsl #8 ;@ Include old SR high\n");\r
0e11c502 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
cc68a136 450 ot("\n");\r
0e11c502 451\r
452 // 2. Obtain the exception vector.\r
453 ot(" mov r11,r8,lsr #29\n");\r
454 ot(" mov r0,r11\n");\r
cc68a136 455#if USE_INT_ACK_CALLBACK\r
0e11c502 456 ot(";@ call IrqCallback if it is defined\n");\r
cc68a136 457#if INT_ACK_NEEDS_STUFF\r
458 ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r
449ecf92 459 ot(" mov r1,r10,lsr #28\n");\r
cc68a136 460 ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r
461 ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");\r
462#endif\r
0e11c502 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
468 ot(" bxne r3\n");\r
469#if INT_ACK_CHANGES_CYCLES\r
cc68a136 470 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
cc68a136 471#endif\r
0e11c502 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
ee5e024c 477#else // !USE_INT_ACK_CALLBACK\r
cc68a136 478 ot(";@ Clear irq:\n");\r
0e11c502 479 ot(" mov r2,#0\n");\r
ee5e024c 480 ot(" strb r2,[r7,#0x47]\n");\r
0e11c502 481 ot(" add r0,r0,#0x18 ;@ use autovector\n");\r
482#endif\r
483 ot(" mov r0,r0,lsl #2 ;@ get vector address\n");\r
484 ot("\n");\r
449ecf92 485 ot(" ldr r11,[r7,#0x60] ;@ Get Memory base\n");\r
0e11c502 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
449ecf92 494 ot(" add r0,r0,r11 ;@ r0 = Memory Base + New PC\n");\r
0e11c502 495 ot(" ldr pc,[r7,#0x64] ;@ Call checkpc()\n");\r
496 #if EMULATE_ADDRESS_ERRORS_JUMP\r
497 ot(" mov r4,r0\n");\r
498 #else\r
499 ot(" bic r4,r0,#1\n");\r
500 #endif\r
501#else\r
449ecf92 502 ot(" add r4,r0,r11 ;@ r4 = Memory Base + New PC\n");\r
0e11c502 503 #if EMULATE_ADDRESS_ERRORS_JUMP\r
504 ot(" bic r4,r4,#1\n");\r
505 #endif\r
506#endif\r
507 ot("\n");\r
508\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
cc68a136 514#endif\r
449ecf92 515 ot(" ldr r6,[r7,#0x54]\n");\r
ee5e024c 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
cc68a136 520 ot("\n");\r
521 \r
0e11c502 522 // --------------\r
523 // trashes all temp regs\r
cc68a136 524 ot("Exception%s\n", ms?"":":");\r
0e11c502 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
529\r
530 // 1. Make a temporary copy of the status register and set the status register for exception processing.\r
449ecf92 531 ot(" ldr r6,[r7,#0x44] ;@ Get old SR high, abuse r6\n");\r
0e11c502 532 ot(" ldr r2,[r7,#0x58] ;@ state flags\n");\r
449ecf92 533 ot(" and r3,r6,#0x27 ;@ clear trace and unused flags\n");\r
0e11c502 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
538 ot("\n");\r
539\r
540 // 3. Save the current processor context.\r
541 ot(" ldr r0,[r7,#0x3c] ;@ Get A7\n");\r
449ecf92 542 ot(" tst r6,#0x20\n");\r
0e11c502 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
552 MemHandler(1,2);\r
553 ot(";@ Push old SR:\n");\r
554 ot(" ldr r0,[r7,#0x4c] ;@ X bit\n");\r
449ecf92 555 ot(" mov r1,r10,lsr #28 ;@ ____NZCV\n");\r
0e11c502 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
449ecf92 562 ot(" orr r1,r1,r6,lsl #8 ;@ Include SR high\n");\r
0e11c502 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
566 ot("\n");\r
567\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
580 #else\r
581 ot(" bic r4,r0,#1\n");\r
582 #endif\r
583#else\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
587 #endif\r
588#endif\r
589 ot("\n");\r
590\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
595#endif\r
449ecf92 596 ot(" ldr r6,[r7,#0x54]\n");\r
0e11c502 597 ot(" bx r11 ;@ Return\n");\r
598 ot("\n");\r
599\r
600 // --------------\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
449ecf92 612 ot(" mov r6,#0x12\n");\r
0e11c502 613 ot(" sub r11,r4,r3\n");\r
614 ot(" tst r1,#0x20\n");\r
449ecf92 615 ot(" orrne r6,r6,#4\n");\r
0e11c502 616 ot("\n");\r
617\r
618 ot("ExceptionAddressError%s\n", ms?"":":");\r
449ecf92 619 ot(";@ r6 - info word (without instruction/not bit), r11 - faulting address\n");\r
0e11c502 620\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
449ecf92 629 ot(" orrne r6,r6,#8 ;@ complete info word\n");\r
0e11c502 630 ot(" orr r2,r2,#4 ;@ set activity bit: 'not processing instruction'\n");\r
631#if EMULATE_HALT\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
638#else\r
639 ot(" str r2,[r7,#0x58]\n");\r
640#endif\r
449ecf92 641 ot(" and r10,r10,#0xf0000000\n");\r
642 ot(" orr r10,r10,r0,lsl #4 ;@ some preparations for SR push\n");\r
0e11c502 643 ot("\n");\r
644\r
645 // 3. Save the current processor context + additional information.\r
646 ot(" ldr r0,[r7,#0x3c] ;@ Get A7\n");\r
449ecf92 647 ot(" tst r10,#0x200\n");\r
0e11c502 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
652 // PC\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
659 // SR\r
660 ot(";@ Push old SR:\n");\r
661 ot(" ldr r0,[r7,#0x4c] ;@ X bit\n");\r
449ecf92 662 ot(" mov r1,r10,ror #28 ;@ ____NZCV\n");\r
0e11c502 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
449ecf92 669 ot(" and r10,r10,#0xf0000000\n");\r
0e11c502 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
680 // access address\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
449ecf92 690 ot(" mov r1,r6\n");\r
0e11c502 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
694 ot("\n");\r
695\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
706#else\r
707 ot(" add r4,r0,r3 ;@ r4 = Memory Base + New PC\n");\r
708#endif\r
709 ot("\n");\r
710\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
714#else\r
715 ot(" bic r4,r4,#1\n");\r
716#endif\r
717\r
718 // 4. Resume execution.\r
449ecf92 719 ot(" ldr r6,[r7,#0x54]\n");\r
0e11c502 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
724 ot("\n");\r
725#endif\r
726\r
727 // --------------\r
728#if EMULATE_TRACE\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
743 ot("\n");\r
744\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
754 ot("\n");\r
755\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
449ecf92 760 ot(" mov r10,r10,lsl #28\n");\r
0e11c502 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
772#else\r
773 ot(" mov r0,#9\n");\r
774#endif\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
cc68a136 780 ot("\n");\r
0e11c502 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
786 ot("\n");\r
787#endif\r
cc68a136 788}\r
789\r
790// ---------------------------------------------------------------------------\r
791// Call Read(r0), Write(r0,r1) or Fetch(r0)\r
792// Trashes r0-r3,r12,lr\r
0e11c502 793int MemHandler(int type,int size,int addrreg,int need_addrerr_check)\r
cc68a136 794{\r
795 int func=0;\r
796 func=0x68+type*0xc+(size<<2); // Find correct offset\r
797\r
cc68a136 798#if MEMHANDLERS_NEED_FLAGS\r
449ecf92 799 ot(" mov r3,r10,lsr #28\n");\r
cc68a136 800 ot(" strb r3,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r
801#endif\r
cfb3dfa0 802 FlushPC();\r
cc68a136 803\r
a6785576 804#if (MEMHANDLERS_ADDR_MASK & 0xff000000)\r
cfb3dfa0 805 ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0xff000000);\r
806 addrreg=0;\r
a6785576 807#endif\r
808#if (MEMHANDLERS_ADDR_MASK & 0x00ff0000)\r
cfb3dfa0 809 ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x00ff0000);\r
810 addrreg=0;\r
a6785576 811#endif\r
812#if (MEMHANDLERS_ADDR_MASK & 0x0000ff00)\r
cfb3dfa0 813 ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x0000ff00);\r
814 addrreg=0;\r
a6785576 815#endif\r
816#if (MEMHANDLERS_ADDR_MASK & 0x000000ff)\r
cfb3dfa0 817 ot(" bic r0,r%i,#0x%08x\n", addrreg, MEMHANDLERS_ADDR_MASK & 0x000000ff);\r
818 addrreg=0;\r
a6785576 819#endif\r
0e11c502 820\r
821#if EMULATE_ADDRESS_ERRORS_IO\r
822 if (size > 0 && need_addrerr_check)\r
823 {\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
827 switch (type) {\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
831 }\r
832 }\r
833 else\r
834#endif\r
cfb3dfa0 835 if (addrreg != 0)\r
85a36a57 836 {\r
0e11c502 837 ot(" add lr,pc,#4\n");\r
cfb3dfa0 838 ot(" mov r0,r%i\n", addrreg);\r
85a36a57 839 }\r
840 else\r
841 ot(" mov lr,pc\n");\r
cc68a136 842 ot(" ldr pc,[r7,#0x%x] ;@ Call ",func);\r
843\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
848\r
849 if (type==1) ot("%d(r0,r1)",8<<size);\r
850 else ot("%d(r0)", 8<<size);\r
851 ot(" handler\n");\r
852\r
cc68a136 853#if MEMHANDLERS_CHANGE_FLAGS\r
449ecf92 854 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n");\r
855 ot(" mov r10,r10,lsl #28\n");\r
cc68a136 856#endif\r
857#if MEMHANDLERS_CHANGE_PC\r
858 ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r
859#endif\r
860\r
861 return 0;\r
862}\r
863\r
864static void PrintOpcodes()\r
865{\r
866 int op=0;\r
b637c56a 867\r
cc68a136 868 printf("Creating Opcodes: [");\r
869\r
870 ot(";@ ---------------------------- Opcodes ---------------------------\n");\r
871\r
872 // Emit null opcode:\r
873 ot("Op____%s ;@ Called if an opcode is not recognised\n", ms?"":":");\r
0e11c502 874#if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO\r
875 ot(" ldr r1,[r7,#0x58]\n");\r
cc68a136 876 ot(" sub r4,r4,#2\n");\r
0e11c502 877 ot(" orr r1,r1,#4 ;@ set activity bit: 'not processing instruction'\n");\r
878 ot(" str r1,[r7,#0x58]\n");\r
879#else\r
880 ot(" sub r4,r4,#2\n");\r
881#endif\r
cc68a136 882#if USE_UNRECOGNIZED_CALLBACK\r
883 ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r
449ecf92 884 ot(" mov r1,r10,lsr #28\n");\r
cc68a136 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
449ecf92 891 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n");\r
cc68a136 892 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
893 ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r
449ecf92 894 ot(" mov r10,r10,lsl #28\n");\r
cc68a136 895 ot(" tst r0,r0\n");\r
0e11c502 896 ot(" moveq r0,#4\n");\r
cc68a136 897 ot(" bleq Exception\n");\r
898#else\r
0e11c502 899 ot(" mov r0,#4\n");\r
cc68a136 900 ot(" bl Exception\n");\r
901#endif\r
85a36a57 902 ot("\n");\r
cc68a136 903 Cycles=34;\r
904 OpEnd();\r
905\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
449ecf92 911 ot(" mov r1,r10,lsr #28\n");\r
cc68a136 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
449ecf92 918 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n");\r
cc68a136 919 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
920 ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r
449ecf92 921 ot(" mov r10,r10,lsl #28\n");\r
cc68a136 922 ot(" tst r0,r0\n");\r
0e11c502 923 ot(" moveq r0,#0x0a\n");\r
cc68a136 924 ot(" bleq Exception\n");\r
925#else\r
0e11c502 926 ot(" mov r0,#0x0a\n");\r
cc68a136 927 ot(" bl Exception\n");\r
928#endif\r
85a36a57 929 ot("\n");\r
cc68a136 930 Cycles=4;\r
931 OpEnd();\r
932\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
449ecf92 937 ot(" mov r1,r10,lsr #28\n");\r
cc68a136 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
449ecf92 944 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n");\r
cc68a136 945 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
946 ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r
449ecf92 947 ot(" mov r10,r10,lsl #28\n");\r
cc68a136 948 ot(" tst r0,r0\n");\r
0e11c502 949 ot(" moveq r0,#0x0b\n");\r
cc68a136 950 ot(" bleq Exception\n");\r
951#else\r
0e11c502 952 ot(" mov r0,#0x0b\n");\r
cc68a136 953 ot(" bl Exception\n");\r
954#endif\r
85a36a57 955 ot("\n");\r
cc68a136 956 Cycles=4;\r
957 OpEnd();\r
958\r
959\r
960 for (op=0;op<0x10000;op++)\r
961 {\r
962 if ((op&0xfff)==0) { printf("%x",op>>12); fflush(stdout); } // Update progress\r
963\r
964 OpAny(op);\r
965 }\r
966\r
967 ot("\n");\r
968\r
969 printf("]\n");\r
970}\r
971\r
972// helper\r
973static void ott(const char *str, int par, const char *nl, int nlp, int counter, int size)\r
974{\r
975 switch(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
979 }\r
980 ot(str, par);\r
981 if((counter&7)==7) ot(nl,nlp); else ot(",");\r
982}\r
983\r
984static void PrintJumpTable()\r
985{\r
986 int i=0,op=0,len=0;\r
987\r
988 ot(";@ -------------------------- Jump Table --------------------------\n");\r
989\r
c008977e 990 // space for decompressed table\r
991 ot(ms?" area |.data|, data\n":" .data\n .align 4\n\n");\r
992\r
cc68a136 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
a6785576 996 indexes=(int *)malloc(0x10000*4);\r
997 if(!indexes) { printf("ERROR: out of memory\n"); exit(1); }\r
998 len=0x10000;\r
cc68a136 999\r
a6785576 1000 ot("CycloneJumpTab%s\n", ms?"":":");\r
1001 if(ms) {\r
1002 for(i = 0; i < 0xa000/8; i++)\r
1003 ot(" dcd 0,0,0,0,0,0,0,0\n");\r
1004 } else\r
1005 ot(" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", 0xa000/8);\r
cc68a136 1006\r
1007 // hanlers live in "a-line" part of the table\r
a6785576 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
1010 handlers=3;\r
cc68a136 1011\r
a6785576 1012 for(i=0;i<len;i++)\r
cc68a136 1013 {\r
1014 op=CyJump[i];\r
1015\r
a6785576 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
1020 handlers++;\r
cc68a136 1021 }\r
a6785576 1022 }\r
1023 if(handlers&7) {\r
1024 fseek(AsmFile, -1, SEEK_CUR); // remove last comma\r
1025 for(i = 8-(handlers&7); i > 0; i--)\r
1026 ot(",000000");\r
1027 ot("\n");\r
1028 }\r
1029 if(ms) {\r
1030 for(i = (0x4000-handlers)/8; i > 0; i--)\r
1031 ot(" dcd 0,0,0,0,0,0,0,0\n");\r
1032 } else {\r
1033 ot(ms?"":" .rept 0x%x\n .long 0,0,0,0,0,0,0,0\n .endr\n", (0x4000-handlers)/8);\r
1034 }\r
cc68a136 1035 printf("total distinct hanlers: %i\n",handlers);\r
a6785576 1036 // output data\r
1037 for(i=0,ip=0; i < 0xf000; i++, ip++) {\r
cc68a136 1038 op=CyJump[i];\r
a6785576 1039 if(op == -2) {\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
1043 i+=0xfff;\r
1044 continue;\r
1045 }\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
cc68a136 1051 } else {\r
a6785576 1052 ott("0x%.4x", out, "\n",0,ip++,1);\r
1053 ott("0x%.4x", reps,"\n",0,ip,1);\r
1054 }\r
cc68a136 1055 }\r
a6785576 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
a6785576 1058 if(ip&7) {\r
1059 for(i = 8-(ip&7); i > 0; i--)\r
1060 ot(",0x0000");\r
a6785576 1061 }\r
547045e3 1062 ot("\n");\r
a6785576 1063 if(ms) {\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
1066 } else {\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
1068 }\r
1069 ot("\n");\r
1070 free(indexes);\r
cc68a136 1071#else\r
a6785576 1072 ot("CycloneJumpTab%s\n", ms?"":":");\r
cc68a136 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
1076\r
1077 for (i=0;i<len;i++)\r
1078 {\r
1079 op=CyJump[i];\r
1080 \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
1085 }\r
a6785576 1086 if(i&7) fseek(AsmFile, -1, SEEK_CUR); // remove last comma\r
cc68a136 1087\r
1088 ot("\n");\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
1093#endif\r
1094}\r
1095\r
1096static int CycloneMake()\r
1097{\r
1098 int i;\r
1099 char *name="Cyclone.s";\r
0e11c502 1100 const char *globl=ms?"export":".global";\r
cc68a136 1101 \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
1105 \r
1106 printf("Making %s...\n",name);\r
1107\r
1108 ot("\n;@ Dave's Cyclone 68000 Emulator v%x.%.3x - Assembler Output\n\n",CycloneVer>>12,CycloneVer&0xfff);\r
1109\r
1110 ot(";@ (c) Copyright 2003 Dave, All rights reserved.\n");\r
547045e3 1111 ot(";@ some code (c) Copyright 2005-2007 notaz, All rights reserved.\n");\r
cc68a136 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
1114\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
1119\r
0e11c502 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
a9a5a6e0 1129#if (CYCLONE_FOR_GENESIS == 2)\r
0e11c502 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
7336a99a 1134#endif\r
0e11c502 1135 ot("\n");\r
1136 ot(ms?"CycloneVer dcd 0x":"CycloneVer: .long 0x");\r
1137 ot("%.4x\n",CycloneVer);\r
cc68a136 1138 ot("\n");\r
1139\r
1140 PrintFramework();\r
b637c56a 1141 arm_op_count = 0;\r
cc68a136 1142 PrintOpcodes();\r
b637c56a 1143 printf("~%i ARM instructions used for opcode handlers\n", arm_op_count);\r
cc68a136 1144 PrintJumpTable();\r
1145\r
1146 if (ms) ot(" END\n");\r
1147\r
7336a99a 1148 ot("\n\n;@ vim:filetype=armasm\n");\r
1149\r
cc68a136 1150 fclose(AsmFile); AsmFile=NULL;\r
1151\r
1152#if 0\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
1158#endif\r
1159\r
1160 free(CyJump);\r
1161 return 0;\r
1162}\r
1163\r
1164int main()\r
1165{\r
1166 printf("\n Dave's Cyclone 68000 Emulator v%x.%.3x - Core Creator\n\n",CycloneVer>>12,CycloneVer&0xfff);\r
1167\r
1168 // Make GAS or ARMASM version\r
1169 CycloneMake();\r
1170 return 0;\r
1171}\r
1172\r