fix bcd instructions
[cyclone68000.git] / OpBranch.cpp
1 \r
2 // This file is part of the Cyclone 68000 Emulator\r
3 \r
4 // Copyright (c) 2004,2011 FinalDave (emudave (at) gmail.com)\r
5 // Copyright (c) 2005-2011 GraÅžvydas "notaz" Ignotas (notasas (at) gmail.com)\r
6 \r
7 // This code is licensed under the GNU General Public License version 2.0 and the MAME License.\r
8 // You can choose the license that has the most advantages for you.\r
9 \r
10 // SVN repository can be found at http://code.google.com/p/cyclone68000/\r
11 \r
12 \r
13 #include "app.h"\r
14 \r
15 // in/out address in r0, trashes all temp regs\r
16 static void CheckPc(void)\r
17 {\r
18 #if USE_CHECKPC_CALLBACK\r
19  #ifdef MEMHANDLERS_DIRECT_PREFIX\r
20   ot("  bl %scheckpc ;@ Call checkpc()\n", MEMHANDLERS_DIRECT_PREFIX);\r
21  #else\r
22   ot(";@ Check Memory Base+pc\n");\r
23   ot("  mov lr,pc\n");\r
24   ot("  ldr pc,[r7,#0x64] ;@ Call checkpc()\n");\r
25   ot("\n");\r
26  #endif\r
27 #endif\r
28 }\r
29 \r
30 // Push 32-bit value in r1 - trashes r0-r3,r12,lr\r
31 void OpPush32()\r
32 {\r
33   ot(";@ Push r1 onto stack\n");\r
34   ot("  ldr r0,[r7,#0x3c]\n");\r
35   ot("  sub r0,r0,#4 ;@ Predecrement A7\n");\r
36   ot("  str r0,[r7,#0x3c] ;@ Save A7\n");\r
37   MemHandler(1,2);\r
38   ot("\n");\r
39 }\r
40 \r
41 // Push SR - trashes r0-r3,r12,lr\r
42 void OpPushSr(int high)\r
43 {\r
44   ot(";@ Push SR:\n");\r
45   OpFlagsToReg(high);\r
46   ot("  ldr r0,[r7,#0x3c]\n");\r
47   ot("  sub r0,r0,#2 ;@ Predecrement A7\n");\r
48   ot("  str r0,[r7,#0x3c] ;@ Save A7\n");\r
49   MemHandler(1,1);\r
50   ot("\n");\r
51 }\r
52 \r
53 // Pop SR - trashes r0-r3\r
54 static void PopSr(int high)\r
55 {\r
56   ot(";@ Pop SR:\n");\r
57   ot("  ldr r0,[r7,#0x3c]\n");\r
58   ot("  add r1,r0,#2 ;@ Postincrement A7\n");\r
59   ot("  str r1,[r7,#0x3c] ;@ Save A7\n");\r
60   MemHandler(0,1);\r
61   ot("\n");\r
62   OpRegToFlags(high);\r
63 }\r
64 \r
65 // Pop PC - trashes r0-r3\r
66 static void PopPc()\r
67 {\r
68   ot(";@ Pop PC:\n");\r
69   ot("  ldr r0,[r7,#0x3c]\n");\r
70   ot("  add r1,r0,#4 ;@ Postincrement A7\n");\r
71   ot("  str r1,[r7,#0x3c] ;@ Save A7\n");\r
72   MemHandler(0,2);\r
73   ot("  ldr r1,[r7,#0x60] ;@ Get Memory base\n");\r
74   ot("  add r0,r0,r1 ;@ Memory Base+PC\n");\r
75   ot("\n");\r
76   CheckPc();\r
77 #if EMULATE_ADDRESS_ERRORS_JUMP\r
78   ot("  mov r4,r0\n");\r
79 #else\r
80   ot("  bic r4,r0,#1\n");\r
81 #endif\r
82 }\r
83 \r
84 int OpTrap(int op)\r
85 {\r
86   int use=0;\r
87 \r
88   use=op&~0xf;\r
89   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
90 \r
91   OpStart(op,0x10);\r
92   ot("  and r0,r8,#0xf ;@ Get trap number\n");\r
93   ot("  orr r0,r0,#0x20 ;@ 32+n\n");\r
94   ot("  bl Exception\n");\r
95   ot("\n");\r
96 \r
97   Cycles=38; OpEnd(0x10);\r
98 \r
99   return 0;\r
100 }\r
101 \r
102 // --------------------- Opcodes 0x4e50+ ---------------------\r
103 int OpLink(int op)\r
104 {\r
105   int use=0,reg;\r
106 \r
107   use=op&~7;\r
108   reg=op&7;\r
109   if (reg==7) use=op;\r
110   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
111 \r
112   OpStart(op,0x10);\r
113 \r
114   if(reg!=7) {\r
115     ot(";@ Get An\n");\r
116     EaCalc(11, 7, 8, 2);\r
117     EaRead(11, 1, 8, 2, 7);\r
118   }\r
119 \r
120   ot("  ldr r0,[r7,#0x3c] ;@ Get A7\n");\r
121   ot("  sub r0,r0,#4 ;@ A7-=4\n");\r
122   ot("  mov r8,r0 ;@ abuse r8\n");\r
123   if(reg==7) ot("  mov r1,r0\n");\r
124   ot("\n");\r
125   \r
126   ot(";@ Write An to Stack\n");\r
127   MemHandler(1,2);\r
128 \r
129   ot(";@ Save to An\n");\r
130   if(reg!=7)\r
131     EaWrite(11, 8, 8, 2, 7);\r
132 \r
133   ot(";@ Get offset:\n");\r
134   EaCalc(0,0,0x3c,1);    // abused r8 is ok because of imm EA\r
135   EaRead(0,0,0x3c,1,0);\r
136 \r
137   ot("  add r8,r8,r0 ;@ Add offset to A7\n");\r
138   ot("  str r8,[r7,#0x3c]\n");\r
139   ot("\n");\r
140 \r
141   Cycles=16;\r
142   OpEnd(0x10);\r
143   return 0;\r
144 }\r
145 \r
146 // --------------------- Opcodes 0x4e58+ ---------------------\r
147 int OpUnlk(int op)\r
148 {\r
149   int use=0;\r
150 \r
151   use=op&~7;\r
152   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
153 \r
154   OpStart(op,0x10);\r
155 \r
156   ot(";@ Get An\n");\r
157   EaCalc(11, 0xf, 8, 2);\r
158   EaRead(11,   0, 8, 2, 0xf);\r
159 \r
160   ot("  add r8,r0,#4 ;@ A7+=4, abuse r8\n");\r
161   ot("\n");\r
162   ot(";@ Pop An from stack:\n");\r
163   MemHandler(0,2);\r
164   ot("\n");\r
165   ot("  str r8,[r7,#0x3c] ;@ Save A7\n");\r
166   ot("\n");\r
167   ot(";@ An = value from stack:\n");\r
168   EaWrite(11, 0, 8, 2, 7);\r
169 \r
170   Cycles=12;\r
171   OpEnd(0x10);\r
172   return 0;\r
173 }\r
174 \r
175 // --------------------- Opcodes 0x4e70+ ---------------------\r
176 // 01001110 01110ttt\r
177 int Op4E70(int op)\r
178 {\r
179   int type=0;\r
180 \r
181   type=op&7; // reset/nop/stop/rte/rtd/rts/trapv/rtr\r
182 \r
183   switch (type)\r
184   {\r
185     case 1:  // nop\r
186     OpStart(op);\r
187     Cycles=4;\r
188     OpEnd();\r
189     return 0;\r
190 \r
191     case 3: // rte\r
192     OpStart(op,0x10,0,0,1); Cycles=20;\r
193     PopSr(1);\r
194     PopPc();\r
195     ot("  ldr r1,[r7,#0x44] ;@ reload SR high\n");\r
196     SuperChange(op,1);\r
197 #if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO || EMULATE_HALT\r
198     ot("  ldr r1,[r7,#0x58]\n");\r
199     ot("  bic r1,r1,#0x0c ;@ clear 'not processing instruction' and 'doing addr error' bits\n");\r
200     ot("  str r1,[r7,#0x58]\n");\r
201 #endif\r
202 #if EMULATE_ADDRESS_ERRORS_JUMP\r
203     ot("  tst r4,#1 ;@ address error?\n");\r
204     ot("  bne ExceptionAddressError_r_prg_r4\n");\r
205 #endif\r
206     opend_check_interrupt = 1;\r
207     opend_check_trace = 1;\r
208     OpEnd(0x10,0);\r
209     return 0;\r
210 \r
211     case 5: // rts\r
212     OpStart(op,0x10); Cycles=16;\r
213     PopPc();\r
214 #if EMULATE_ADDRESS_ERRORS_JUMP\r
215     ot("  tst r4,#1 ;@ address error?\n");\r
216     ot("  bne ExceptionAddressError_r_prg_r4\n");\r
217 #endif\r
218     OpEnd(0x10);\r
219     return 0;\r
220 \r
221     case 6: // trapv\r
222     OpStart(op,0x10,0,1); Cycles=4;\r
223     ot("  tst r10,#0x10000000\n");\r
224     ot("  subne r5,r5,#%i\n",34);\r
225     ot("  movne r0,#7 ;@ TRAPV exception\n");\r
226     ot("  blne Exception\n");\r
227     opend_op_changes_cycles = 1;\r
228     OpEnd(0x10,0);\r
229     return 0;\r
230 \r
231     case 7: // rtr\r
232     OpStart(op,0x10); Cycles=20;\r
233     PopSr(0);\r
234     PopPc();\r
235 #if EMULATE_ADDRESS_ERRORS_JUMP\r
236     ot("  tst r4,#1 ;@ address error?\n");\r
237     ot("  bne ExceptionAddressError_r_prg_r4\n");\r
238 #endif\r
239     OpEnd(0x10);\r
240     return 0;\r
241 \r
242     default:\r
243     return 1;\r
244   }\r
245 }\r
246 \r
247 // --------------------- Opcodes 0x4e80+ ---------------------\r
248 // Emit a Jsr/Jmp opcode, 01001110 1meeeeee\r
249 int OpJsr(int op)\r
250 {\r
251   int use=0;\r
252   int sea=0;\r
253 \r
254   sea=op&0x003f;\r
255 \r
256   // See if we can do this opcode:\r
257   if (EaCanRead(sea,-1)==0) return 1;\r
258 \r
259   use=OpBase(op,0);\r
260   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
261 \r
262   OpStart(op,(op&0x40)?0:0x10);\r
263 \r
264   ot("  ldr r11,[r7,#0x60] ;@ Get Memory base\n");\r
265   ot("\n");\r
266   EaCalc(12,0x003f,sea,0);\r
267 \r
268   ot(";@ Jump - Get new PC from r12\n");\r
269   ot("  add r0,r12,r11 ;@ Memory Base + New PC\n");\r
270   ot("\n");\r
271   CheckPc();\r
272   if (!(op&0x40))\r
273   {\r
274     ot("  ldr r2,[r7,#0x3c]\n");\r
275     ot("  sub r1,r4,r11 ;@ r1 = Old PC\n");\r
276   }\r
277 #if EMULATE_ADDRESS_ERRORS_JUMP\r
278   // jsr prefetches next instruction before pushing old PC,\r
279   // according to http://pasti.fxatari.com/68kdocs/68kPrefetch.html\r
280   ot("  mov r4,r0\n");\r
281   ot("  tst r4,#1 ;@ address error?\n");\r
282   ot("  bne ExceptionAddressError_r_prg_r4\n");\r
283 #else\r
284   ot("  bic r4,r0,#1\n");\r
285 #endif\r
286 \r
287   if (!(op&0x40))\r
288   {\r
289     ot(";@ Push old PC onto stack\n");\r
290     ot("  sub r0,r2,#4 ;@ Predecrement A7\n");\r
291     ot("  str r0,[r7,#0x3c] ;@ Save A7\n");\r
292     MemHandler(1,2);\r
293   }\r
294 \r
295   Cycles=(op&0x40) ? 4 : 12;\r
296   Cycles+=Ea_add_ns((op&0x40) ? g_jmp_cycle_table : g_jsr_cycle_table, sea);\r
297 \r
298   OpEnd((op&0x40)?0:0x10);\r
299 \r
300   return 0;\r
301 }\r
302 \r
303 // --------------------- Opcodes 0x50c8+ ---------------------\r
304 \r
305 // ARM version of 68000 condition codes:\r
306 static const char * const Cond[16]=\r
307 {\r
308   "",  "",  "hi","ls","cc","cs","ne","eq",\r
309   "vc","vs","pl","mi","ge","lt","gt","le"\r
310 };\r
311 \r
312 // Emit a Dbra opcode, 0101cccc 11001nnn vv\r
313 int OpDbra(int op)\r
314 {\r
315   const char *cond;\r
316   int use=0;\r
317   int cc=0;\r
318 \r
319   use=op&~7; // Use same handler\r
320   cc=(op>>8)&15;\r
321   \r
322   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
323   OpStart(op);\r
324 \r
325   if (cc>=2)\r
326   {\r
327     ot(";@ Is the condition true?\n");\r
328     cond=TestCond(cc);\r
329     ot(";@ If so, don't dbra\n");\r
330     ot("  b%s DbraTrue\n\n",cond);\r
331   }\r
332 \r
333   if (cc!=0)\r
334   {\r
335     ot(";@ Decrement Dn.w\n");\r
336     ot("  and r1,r8,#0x0007\n");\r
337     ot("  mov r1,r1,lsl #2\n");\r
338     ot("  ldrsh r0,[r7,r1]\n");\r
339     ot("  strb r8,[r7,#0x45] ;@ not polling\n");\r
340     ot("  sub r0,r0,#1\n");\r
341     ot("  strh r0,[r7,r1]\n");\r
342     ot("\n");\r
343 \r
344     ot(";@ Check if Dn.w is -1\n");\r
345     ot("  cmn r0,#1\n");\r
346 \r
347 #if (USE_CHECKPC_CALLBACK && USE_CHECKPC_DBRA) || EMULATE_ADDRESS_ERRORS_JUMP\r
348     ot("  beq DbraMin1\n");\r
349     ot("\n");\r
350 \r
351     ot(";@ Get Branch offset:\n");\r
352     ot("  ldrsh r0,[r4]\n");\r
353     ot("  add r0,r4,r0 ;@ r0 = New PC\n");\r
354     CheckPc();\r
355 #if EMULATE_ADDRESS_ERRORS_JUMP\r
356     ot("  mov r4,r0\n");\r
357     ot("  tst r4,#1 ;@ address error?\n");\r
358     ot("  bne ExceptionAddressError_r_prg_r4\n");\r
359 #else\r
360     ot("  bic r4,r0,#1\n");\r
361 #endif\r
362 #else\r
363     ot("\n");\r
364     ot(";@ Get Branch offset:\n");\r
365     ot("  ldrnesh r0,[r4]\n");\r
366     ot("  addeq r4,r4,#2 ;@ Skip branch offset\n");\r
367     ot("  subeq r5,r5,#4 ;@ additional cycles\n");\r
368     ot("  addne r4,r4,r0 ;@ r4 = New PC\n");\r
369     ot("  bic r4,r4,#1\n"); // we do not emulate address errors\r
370     ot("\n");\r
371 #endif\r
372     Cycles=12-2;\r
373     OpEnd();\r
374   }\r
375   \r
376   //if (cc==0||cc>=2)\r
377   if (op==0x50c8)\r
378   {\r
379     ot(";@ condition true:\n");\r
380     ot("DbraTrue%s\n", ms?"":":");\r
381     ot("  add r4,r4,#2 ;@ Skip branch offset\n");\r
382     ot("\n");\r
383     Cycles=12;\r
384     OpEnd();\r
385   }\r
386 \r
387 #if (USE_CHECKPC_CALLBACK && USE_CHECKPC_DBRA) || EMULATE_ADDRESS_ERRORS_JUMP\r
388   if (op==0x51c8)\r
389   {\r
390     ot(";@ Dn.w is -1:\n");\r
391     ot("DbraMin1%s\n", ms?"":":");\r
392     ot("  add r4,r4,#2 ;@ Skip branch offset\n");\r
393     ot("\n");\r
394     Cycles=12+2;\r
395     OpEnd();\r
396   }\r
397 #endif\r
398 \r
399   return 0;\r
400 }\r
401 \r
402 // --------------------- Opcodes 0x6000+ ---------------------\r
403 // Emit a Branch opcode 0110cccc nn  (cccc=condition)\r
404 int OpBranch(int op)\r
405 {\r
406   int size=0,use=0,checkpc=0;\r
407   int offset=0;\r
408   int cc=0;\r
409   const char *asr_r11="";\r
410   const char *cond;\r
411   int pc_reg=0;\r
412 \r
413   offset=(char)(op&0xff);\r
414   cc=(op>>8)&15;\r
415 \r
416   // Special offsets:\r
417   if (offset==0)  size=1;\r
418   if (offset==-1) size=2;\r
419 \r
420   if (size==2) size=0; // 000 model does not support long displacement\r
421   if (size) use=op; // 16-bit or 32-bit\r
422   else use=(op&0xff01)+2; // Use same opcode for all 8-bit branches\r
423 \r
424   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
425   OpStart(op,size?0x10:0);\r
426   Cycles=10; // Assume branch taken\r
427 \r
428   if (cc>=2)\r
429   {\r
430     cond=TestCond(cc,1);\r
431     ot("  b%s BccDontBranch%i\n\n",cond,8<<size);\r
432   }\r
433 \r
434   if (size) \r
435   {\r
436     if (size<2)\r
437     {\r
438       ot("  ldrsh r11,[r4] ;@ Fetch Branch offset\n");\r
439     }\r
440     else\r
441     {\r
442       ot("  ldrh r2,[r4] ;@ Fetch Branch offset\n");\r
443       ot("  ldrh r11,[r4,#2]\n");\r
444       ot("  orr r11,r11,r2,lsl #16\n");\r
445     }\r
446   }\r
447   else\r
448   {\r
449     ot("  mov r11,r8,asl #24 ;@ Shift 8-bit signed offset up...\n\n");\r
450     asr_r11=",asr #24";\r
451   }\r
452 \r
453   ot(";@ Branch taken - Add on r0 to PC\n");\r
454 \r
455   if (cc==1)\r
456   {\r
457     ot(";@ Bsr - remember old PC\n");\r
458     ot("  ldr r12,[r7,#0x60] ;@ Get Memory base\n");\r
459     ot("  ldr r2,[r7,#0x3c]\n");\r
460     ot("  sub r1,r4,r12 ;@ r1 = Old PC\n");\r
461     if (size) ot("  add r1,r1,#%d\n",1<<size);\r
462     ot("\n");\r
463     ot(";@ Push r1 onto stack\n");\r
464     ot("  sub r0,r2,#4 ;@ Predecrement A7\n");\r
465     ot("  str r0,[r7,#0x3c] ;@ Save A7\n");\r
466     MemHandler(1,2);\r
467     ot("\n");\r
468     Cycles=18; // always 18\r
469   }\r
470 \r
471 #if USE_CHECKPC_CALLBACK && USE_CHECKPC_OFFSETBITS_8\r
472   if (offset!=0 && offset!=-1) checkpc=1;\r
473 #endif\r
474 #if USE_CHECKPC_CALLBACK && USE_CHECKPC_OFFSETBITS_16\r
475   if (offset==0)  checkpc=1;\r
476 #endif\r
477 #if USE_CHECKPC_CALLBACK\r
478   if (offset==-1) checkpc=1;\r
479 #endif\r
480   if (checkpc)\r
481   {\r
482     ot("  add r0,r4,r11%s ;@ New PC\n",asr_r11);\r
483     CheckPc();\r
484     pc_reg=0;\r
485   }\r
486   else\r
487   {\r
488     ot("  add r4,r4,r11%s ;@ r4 = New PC\n",asr_r11);\r
489     pc_reg=4;\r
490   }\r
491 \r
492   if ((op & 1) || size != 0)\r
493   {\r
494 #if EMULATE_ADDRESS_ERRORS_JUMP\r
495     if (pc_reg!=4)\r
496     {\r
497       ot("  mov r4,r%d\n",pc_reg);\r
498       pc_reg=4;\r
499     }\r
500     if (size)\r
501     {\r
502       ot("  tst r4,#1 ;@ address error?\n");\r
503       ot("  bne ExceptionAddressError_r_prg_r4\n");\r
504     }\r
505     else\r
506     {\r
507       ot("  b ExceptionAddressError_r_prg_r4\n");\r
508     }\r
509 #else\r
510     ot("  bic r4,r%d,#1\n",pc_reg);\r
511     pc_reg=4;\r
512 #endif\r
513   }\r
514   if (pc_reg!=4)\r
515     ot("  mov r4,r%d\n",pc_reg);\r
516   ot("\n");\r
517 \r
518   OpEnd(size?0x10:0);\r
519 \r
520   // since all "DontBranch" code is same for every size, output only once\r
521   if (cc>=2&&(op&0xff01)==0x6700)\r
522   {\r
523     ot("BccDontBranch%i%s\n", 8<<size, ms?"":":");\r
524     if (size) ot("  add r4,r4,#%d\n",1<<size);\r
525     Cycles+=(size==1) ? 2 : -2; // Branch not taken\r
526     OpEnd(0);\r
527   }\r
528 \r
529   return 0;\r
530 }\r
531 \r