fix bcd instructions
[cyclone68000.git] / OpArith.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 // --------------------- Opcodes 0x0000+ ---------------------\r
16 // Emit an Ori/And/Sub/Add/Eor/Cmp Immediate opcode, 0000ttt0 ssaaaaaa\r
17 int OpArith(int op)\r
18 {\r
19   int type=0,size=0;\r
20   int sea=0,tea=0;\r
21   int use=0;\r
22   const char *shiftstr="";\r
23 \r
24   // Get source and target EA\r
25   type=(op>>9)&7; if (type==4 || type>=7) return 1;\r
26   size=(op>>6)&3; if (size>=3) return 1;\r
27   sea=   0x003c;\r
28   tea=op&0x003f;\r
29 \r
30   // See if we can do this opcode:\r
31   if (EaCanRead(tea,size)==0) return 1;\r
32   if (EaCanWrite(tea)==0 || EaAn(tea)) return 1;\r
33 \r
34   use=OpBase(op,size);\r
35   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
36 \r
37   OpStart(op, sea, tea); Cycles=4;\r
38 \r
39   // imm must be read first\r
40   EaCalcRead(-1,10,sea,size,0,earwt_msb_dont_care);\r
41   EaCalcRead((type!=6)?11:-1,0,tea,size,0x003f,earwt_msb_dont_care);\r
42 \r
43   if (size<2) shiftstr=(char *)(size?",asl #16":",asl #24");\r
44   if (size<2) ot("  mov r10,r10,asl #%i\n",size?16:24);\r
45 \r
46   ot(";@ Do arithmetic:\n");\r
47 \r
48   if (type==0) ot("  orrs r1,r10,r0%s\n",shiftstr);\r
49   if (type==1) ot("  ands r1,r10,r0%s\n",shiftstr);\r
50   if (type==2||type==6)\r
51                ot("  rsbs r1,r10,r0%s ;@ Defines NZCV\n",shiftstr);\r
52   if (type==3) ot("  adds r1,r10,r0%s ;@ Defines NZCV\n",shiftstr);\r
53   if (type==5) ot("  eors r1,r10,r0%s\n",shiftstr);\r
54 \r
55   if (type< 2) OpGetFlagsNZ(1); // Ori/And\r
56   if (type==2) OpGetFlags(1,1); // Sub: Subtract/X-bit\r
57   if (type==3) OpGetFlags(0,1); // Add: X-bit\r
58   if (type==5) OpGetFlagsNZ(1); // Eor\r
59   if (type==6) OpGetFlags(1,0); // Cmp: Subtract\r
60   ot("\n");\r
61 \r
62   if (type!=6)\r
63   {\r
64     EaWrite(11, 1, tea,size,0x003f,earwt_shifted_up);\r
65   }\r
66 \r
67   // Correct cycles:\r
68   if (type==6)\r
69   {\r
70     if (size>=2 && tea<0x10) Cycles+=2;\r
71   }\r
72   else\r
73   {\r
74     if (size>=2) Cycles+=4;\r
75     if (tea>=8)  Cycles+=4;\r
76     if (type==1 && size>=2 && tea<8) Cycles-=2;\r
77   }\r
78 \r
79   OpEnd(sea,tea);\r
80 \r
81   return 0;\r
82 }\r
83 \r
84 // --------------------- Opcodes 0x5000+ ---------------------\r
85 int OpAddq(int op)\r
86 {\r
87   // 0101nnnt xxeeeeee (nnn=#8,1-7 t=addq/subq xx=size, eeeeee=EA)\r
88   int num=0,type=0,size=0,ea=0;\r
89   int use=0;\r
90   char count[16]="";\r
91   int shift=0;\r
92 \r
93   num =(op>>9)&7; if (num==0) num=8;\r
94   type=(op>>8)&1;\r
95   size=(op>>6)&3; if (size>=3) return 1;\r
96   ea  = op&0x3f;\r
97 \r
98   // See if we can do this opcode:\r
99   if (EaCanRead (ea,size)==0) return 1;\r
100   if (EaCanWrite(ea)     ==0) return 1;\r
101   if (size == 0 && EaAn(ea) ) return 1;\r
102 \r
103   use=OpBase(op,size,1);\r
104 \r
105   if (num!=8) use|=0x0e00; // If num is not 8, use same handler\r
106   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
107 \r
108   OpStart(op,ea);\r
109   Cycles=ea<8?4:8;\r
110   if(type==0&&size==1) Cycles=ea<0x10?4:8;\r
111   if(size>=2) Cycles=ea<0x10?8:12;\r
112 \r
113   if (size>0 && (ea&0x38)==0x08) size=2; // addq.w #n,An is also 32-bit\r
114 \r
115   EaCalcRead(11,0,ea,size,0x003f,earwt_msb_dont_care);\r
116 \r
117   shift=32-(8<<size);\r
118 \r
119   if (num!=8)\r
120   {\r
121     int lsr=9-shift;\r
122 \r
123     ot("  and r2,r8,#0x0e00 ;@ Get quick value\n");\r
124 \r
125     if (lsr>=0) sprintf(count,"r2,lsr #%d",  lsr);\r
126     else        sprintf(count,"r2,lsl #%d", -lsr);\r
127 \r
128     ot("\n");\r
129   }\r
130   else\r
131   {\r
132     sprintf(count,"#0x%.4x",8<<shift);\r
133   }\r
134 \r
135   if (size<2)  ot("  mov r0,r0,asl #%d\n\n",size?16:24);\r
136 \r
137   if (type==0) ot("  adds r1,r0,%s\n",count);\r
138   if (type==1) ot("  subs r1,r0,%s\n",count);\r
139 \r
140   if ((ea&0x38)!=0x08) OpGetFlags(type,1);\r
141   ot("\n");\r
142 \r
143   EaWrite(11, 1, ea,size,0x003f,earwt_shifted_up);\r
144 \r
145   OpEnd(ea);\r
146 \r
147   return 0;\r
148 }\r
149 \r
150 // --------------------- Opcodes 0x8000+ ---------------------\r
151 // 1t0tnnnd xxeeeeee (tt=type:or/sub/and/add xx=size, eeeeee=EA)\r
152 int OpArithReg(int op)\r
153 {\r
154   int use=0;\r
155   int type=0,size=0,dir=0,rea=0,ea=0;\r
156   const char *asl="";\r
157   const char *strop=0;\r
158 \r
159   type=(op>>12)&5;\r
160   rea =(op>> 9)&7;\r
161   dir =(op>> 8)&1; // er,re\r
162   size=(op>> 6)&3; if (size>=3) return 1;\r
163   ea  = op&0x3f;\r
164 \r
165   if (dir && ea<0x10) return 1; // addx/subx opcode\r
166 \r
167   // See if we can do this opcode:\r
168   if (dir==0 && EaCanRead (ea,size)==0) return 1;\r
169   if (dir    && EaCanWrite(ea)==0)      return 1;\r
170   if ((size==0||!(type&1))&&EaAn(ea))   return 1;\r
171 \r
172   use=OpBase(op,size);\r
173   use&=~0x0e00; // Use same opcode for Dn\r
174   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
175 \r
176   OpStart(op,ea); Cycles=4;\r
177 \r
178   EaCalcRead(dir?11:-1,0,ea,size,0x003f,earwt_msb_dont_care);\r
179 \r
180   EaCalcRead(dir?-1:11,1,rea,size,0x0e00,earwt_msb_dont_care);\r
181 \r
182   ot(";@ Do arithmetic:\n");\r
183   if (type==0) strop = "orrs";\r
184   if (type==1) strop = (char *) (dir ? "subs" : "rsbs");\r
185   if (type==4) strop = "ands";\r
186   if (type==5) strop = "adds";\r
187 \r
188   if (size==0) asl=",asl #24";\r
189   if (size==1) asl=",asl #16";\r
190 \r
191   if (size<2) ot("  mov r0,r0%s\n",asl);\r
192   ot("  %s r1,r0,r1%s\n",strop,asl);\r
193 \r
194   if (type&1) OpGetFlags(type==1,type&1); // add/subtract\r
195   else        OpGetFlagsNZ(1);\r
196   ot("\n");\r
197 \r
198   ot(";@ Save result:\n");\r
199   if (size<2) ot("  mov r1,r1,asr #%d\n",size?16:24);\r
200   if (dir) EaWrite(11, 1, ea,size,0x003f,earwt_msb_dont_care);\r
201   else     EaWrite(11, 1,rea,size,0x0e00,earwt_msb_dont_care);\r
202 \r
203   if(rea==ea) {\r
204     if(ea<8) Cycles=(size>=2)?8:4; else Cycles+=(size>=2)?26:14;\r
205   } else if(dir) {\r
206     Cycles+=4;\r
207     if(size>=2) Cycles+=4;\r
208   } else {\r
209     if(size>=2) {\r
210       Cycles+=2;\r
211       if(ea<0x10||ea==0x3c) Cycles+=2;\r
212     }\r
213   }\r
214 \r
215   OpEnd(ea);\r
216 \r
217   return 0;\r
218 }\r
219 \r
220 // --------------------- Opcodes 0x80c0+ ---------------------\r
221 int OpMul(int op)\r
222 {\r
223   // Div/Mul: 1m00nnns 11eeeeee (m=Mul, nnn=Register Dn, s=signed, eeeeee=EA)\r
224   int type=0,rea=0,sign=0,ea=0;\r
225   int use=0;\r
226 \r
227   type=(op>>14)&1; // div/mul\r
228   rea =(op>> 9)&7;\r
229   sign=(op>> 8)&1;\r
230   ea  = op&0x3f;\r
231 \r
232   // See if we can do this opcode:\r
233   if (EaCanRead(ea,1)==0||EaAn(ea)) return 1;\r
234 \r
235   use=OpBase(op,1);\r
236   use&=~0x0e00; // Use same for all registers\r
237   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
238 \r
239   OpStart(op,ea);\r
240   if(type) Cycles=54;\r
241   else     Cycles=sign?158:140;\r
242 \r
243   EaCalcRead(-1,0,ea,1,0x003f,earwt_msb_dont_care);\r
244 \r
245   EaCalc(11,0x0e00,rea, 2);\r
246   EaRead(11,     2,rea, 2,0x0e00);\r
247 \r
248   ot("  movs r1,r0,asl #16\n");\r
249 \r
250   if (type==0) // div\r
251   {\r
252     // the manual says C is always cleared, but neither Musashi nor FAME do that\r
253     //ot("  bic r10,r10,#0x20000000 ;@ always clear C\n");\r
254     ot("  beq divzero%.4x ;@ division by zero\n",op);\r
255     ot("\n");\r
256     \r
257     if (sign)\r
258     {\r
259       ot("  mov r12,#0 ;@ r12 = 1 or 2 if the result is negative\n");\r
260       ot("  tst r2,r2\n");\r
261       ot("  orrmi r12,r12,#2\n");\r
262       ot("  rsbmi r2,r2,#0 ;@ Make r2 positive\n");\r
263       ot("\n");\r
264       ot("  movs r0,r1,asr #16\n");\r
265       ot("  orrmi r12,r12,#1\n");\r
266       ot("  rsbmi r0,r0,#0 ;@ Make r0 positive\n");\r
267       ot("\n");\r
268       ot(";@ detect the nasty 0x80000000 / -1 situation\n");\r
269       ot("  mov r3,r2,asr #31\n");\r
270       ot("  eors r3,r3,r1,asr #16\n");\r
271       ot("  beq wrendofop%.4x\n",op);\r
272     }\r
273     else\r
274     {\r
275       ot("  mov r0,r1,lsr #16 ;@ use only 16 bits of divisor\n");\r
276     }\r
277 \r
278     ot("\n");\r
279     ot(";@ Divide r2 by r0\n");\r
280     ot("  mov r3,#0\n");\r
281     ot("  mov r1,r0\n");\r
282     ot("\n");\r
283     ot(";@ Shift up divisor till it's just less than numerator\n");\r
284     ot("Shift%.4x%s\n",op,ms?"":":");\r
285     ot("  cmp r1,r2,lsr #1\n");\r
286     ot("  movls r1,r1,lsl #1\n");\r
287     ot("  bcc Shift%.4x\n",op);\r
288     ot("\n");\r
289 \r
290     ot("Divide%.4x%s\n",op,ms?"":":");\r
291     ot("  cmp r2,r1\n");\r
292     ot("  adc r3,r3,r3 ;@ Double r3 and add 1 if carry set\n");\r
293     ot("  subcs r2,r2,r1\n");\r
294     ot("  teq r1,r0\n");\r
295     ot("  movne r1,r1,lsr #1\n");\r
296     ot("  bne Divide%.4x\n",op);\r
297     ot("\n");\r
298     ot(";@r3==quotient,r2==remainder\n");\r
299 \r
300     if (sign)\r
301     {\r
302       // sign correction\r
303       ot("  and r1,r12,#1\n");\r
304       ot("  teq r1,r12,lsr #1\n");\r
305       ot("  rsbne r3,r3,#0 ;@ negate if quotient is negative\n");\r
306       ot("  tst r12,#2\n");\r
307       ot("  rsbne r2,r2,#0 ;@ negate the remainder if divident was negative\n");\r
308       ot("\n");\r
309 \r
310       // signed overflow check\r
311       ot("  mov r1,r3,asl #16\n");\r
312       ot("  cmp r3,r1,asr #16 ;@ signed overflow?\n");\r
313       ot("  orrne r10,r10,#0x10000000 ;@ set overflow flag\n");\r
314       ot("  bne endofop%.4x ;@ overflow!\n",op);\r
315       ot("\n");\r
316       ot("wrendofop%.4x%s\n",op,ms?"":":");\r
317     }\r
318     else\r
319     {\r
320       // overflow check\r
321       ot("  movs r1,r3,lsr #16 ;@ check for overflow condition\n");\r
322       ot("  orrne r10,r10,#0x10000000 ;@ set overflow flag\n");\r
323       ot("  bne endofop%.4x ;@ overflow!\n",op);\r
324       ot("\n");\r
325     }\r
326 \r
327     ot("  movs r1,r3,lsl #16 ;@ Clip to 16-bits\n");\r
328     OpGetFlagsNZ(1);\r
329 \r
330     ot("  mov r1,r1,lsr #16\n");\r
331     ot("  orr r1,r1,r2,lsl #16 ;@ Insert remainder\n");\r
332   }\r
333 \r
334   if (type==1)\r
335   {\r
336     ot(";@ Get 16-bit signs right:\n");\r
337     ot("  mov r0,r1,%s #16\n",sign?"asr":"lsr");\r
338     ot("  mov r2,r2,lsl #16\n");\r
339     ot("  mov r2,r2,%s #16\n",sign?"asr":"lsr");\r
340     ot("\n");\r
341 \r
342     ot("  muls r1,r2,r0\n");\r
343     OpGetFlagsNZ(1);\r
344   }\r
345   ot("\n");\r
346 \r
347   EaWrite(11, 1,rea, 2,0x0e00,earwt_shifted_up);\r
348 \r
349   if (type==0) ot("endofop%.4x%s\n",op,ms?"":":");\r
350   OpEnd(ea);\r
351 \r
352   if (type==0) // div\r
353   {\r
354     ot("divzero%.4x%s\n",op,ms?"":":");\r
355     ot("  mov r0,#5 ;@ Divide by zero\n");\r
356     ot("  bl Exception\n");\r
357     Cycles+=38;\r
358     OpEnd(ea);\r
359     ot("\n");\r
360   }\r
361 \r
362   return 0;\r
363 }\r
364 \r
365 // Get X Bit into carry - trashes r2\r
366 int GetXBit(int subtract)\r
367 {\r
368   ot(";@ Get X bit:\n");\r
369   ot("  ldr r2,[r7,#0x4c]\n");\r
370   if (subtract) ot("  mvn r2,r2 ;@ Invert it\n");\r
371   ot("  tst r2,r2,lsl #3 ;@ Get into Carry\n");\r
372   ot("\n");\r
373   return 0;\r
374 }\r
375 \r
376 // --------------------- Opcodes 0x8100+ ---------------------\r
377 // 1t00ddd1 0000asss - sbcd/abcd Ds,Dd or -(As),-(Ad)\r
378 int OpAbcd(int op)\r
379 {\r
380   int use=0;\r
381   int type=0,sea=0,mem=0,dea=0;\r
382   \r
383   type=(op>>14)&1; // sbcd/abcd\r
384   dea =(op>> 9)&7;\r
385   mem =(op>> 3)&1;\r
386   sea = op     &7;\r
387 \r
388   if (mem) { sea|=0x20; dea|=0x20; }\r
389 \r
390   use=op&~0x0e07; // Use same opcode for all registers..\r
391   if (sea==0x27) use|=0x0007; // ___x.b -(a7)\r
392   if (dea==0x27) use|=0x0e00; // ___x.b -(a7)\r
393   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
394 \r
395   OpStart(op,sea,dea); Cycles=6;\r
396 \r
397   if (mem)\r
398   {\r
399     ot(";@ Get src/dest EA vals\n");\r
400     EaCalc (0,0x000f, sea,0,earwt_msb_dont_care);\r
401     EaRead (0,     6, sea,0,0x000f,earwt_msb_dont_care);\r
402     EaCalcRead(11,0,dea,0,0x0e00,earwt_msb_dont_care);\r
403   }\r
404   else\r
405   {\r
406     ot(";@ Get src/dest reg vals\n");\r
407     EaCalcRead(-1,6,sea,0,0x0007,earwt_msb_dont_care);\r
408     EaCalcRead(11,0,dea,0,0x0e00,earwt_msb_dont_care);\r
409   }\r
410 \r
411   ot("  bic r10,r10,#0xb1000000 ;@ clear all flags except old Z\n");\r
412 \r
413   ot("  ldr r1,[r7,#0x4c] ;@ Get X bit\n");\r
414   ot("  and r2,r0,#0x0f\n");\r
415   ot("  movs r12,r1,lsl #3 ;@ X into carry\n");\r
416   ot("  and r1,r6,#0x0f\n");\r
417   if (type)\r
418   {\r
419     // abcd\r
420     ot("  adc r1,r2,r1\n");\r
421     ot("  cmp r1,#9\n");\r
422 \r
423     ot("  and r0,r0,#0xf0\n");\r
424     ot("  and r6,r6,#0xf0\n");\r
425     ot("  add r1,r1,r0\n");\r
426     ot("  add r1,r1,r6\n");\r
427     ot("  mov r12,r1\n");\r
428     ot("  addhi r12,#6 ;@ Decimal adjust units\n");\r
429     ot("  tst r1,#0x80\n");\r
430     ot("  orreq r10,r10,#0x10000000 ;@ Undefined V behavior\n");\r
431     ot("  cmp r12,#0x9f\n");\r
432     ot("  orrhi r10,r10,#0x20000000 ;@ C\n");\r
433     ot("  subhi r12,r12,#0xa0\n");\r
434     ot("  movs r0,r12,lsl #24\n");\r
435     ot("  bicpl r10,r10,#0x10000000 ;@ Undefined V behavior part II\n");\r
436   }\r
437   else\r
438   {\r
439     // sbcd\r
440     ot("  mov r12,#0 ;@ corf\n");\r
441     ot("  adc r1,r1,#0\n");\r
442     ot("  sub r1,r2,r1\n");\r
443     ot("  cmp r1,#0x0f\n");\r
444     ot("  movhi r12,#6\n");\r
445 \r
446     ot("  and r0,r0,#0xf0\n");\r
447     ot("  and r6,r6,#0xf0\n");\r
448     ot("  add r1,r1,r0\n");\r
449     ot("  sub r1,r1,r6\n");\r
450     ot("  tst r1,#0x80\n");\r
451     ot("  orrne r10,r10,#0x10000000 ;@ Undefined V behavior\n");\r
452     ot("  cmp r1,r12\n");\r
453     ot("  orrlt r10,r10,#0x20000000 ;@ C\n");\r
454     ot("  cmp r1,#0xff\n");\r
455     ot("  addhi r1,#0xa0\n");\r
456     ot("  sub r12,r1,r12\n");\r
457     ot("  movs r0,r12,lsl #24\n");\r
458     ot("  bicmi r10,r10,#0x10000000 ;@ Undefined V behavior part II\n");\r
459   }\r
460 \r
461   ot("  orrmi r10,r10,#0x80000000 ;@ Undefined N behavior\n");\r
462   ot("  bicne r10,r10,#0x40000000 ;@ Z flag\n");\r
463   ot("  str r10,[r7,#0x4c] ;@ Save X bit\n");\r
464   ot("\n");\r
465 \r
466   EaWrite(11, 0, dea,0,0x0e00,earwt_shifted_up);\r
467 \r
468   ot("  ldr r6,[r7,#0x54]\n");\r
469   OpEnd(sea,dea);\r
470 \r
471   return 0;\r
472 }\r
473 \r
474 // 01001000 00eeeeee - nbcd <ea>\r
475 int OpNbcd(int op)\r
476 {\r
477   int use=0;\r
478   int ea=0;\r
479   \r
480   ea=op&0x3f;\r
481 \r
482   if(EaCanWrite(ea)==0||EaAn(ea)) return 1;\r
483 \r
484   use=OpBase(op,0);\r
485   if(op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
486 \r
487   OpStart(op,ea); Cycles=6;\r
488   if(ea >= 8)  Cycles+=2;\r
489 \r
490   EaCalcRead(11,0,ea,0,0x003f,earwt_zero_extend);\r
491 \r
492   // this is rewrite of Musashi's code\r
493   ot("  ldr r2,[r7,#0x4c]\n");\r
494   ot("  bic r10,r10,#0xb0000000 ;@ clear all flags, except Z\n");\r
495   ot("  and r2,r2,#0x20000000\n");\r
496   ot("  rsb r1,r0,#0 ;@ do arithmetic\n");\r
497   ot("  subs r1,r1,r2,lsr #29 ;@ X\n");\r
498 \r
499   ot("  beq finish%.4x\n",op);\r
500   ot("\n");\r
501 \r
502   ot("  movs r1,r1,lsl #24\n");\r
503   ot("  orrmi r10,r10,#0x10000000 ;@ Undefined V behavior\n");\r
504   ot("  orr r2,r1,r0,lsl #24\n");\r
505   ot("  tst r2,#0x0f000000\n");\r
506   ot("  andeq r1,r1,#0xf0000000\n");\r
507   ot("  orreq r1,r1,#0x06000000\n");\r
508   ot("  adds r1,r1,#0x9a000000\n");\r
509   ot("  bicmi r10,r10,#0x10000000 ;@ Undefined V behavior part II\n");\r
510   ot("  orrmi r10,r10,#0x80000000 ;@ Undefined N behavior\n");\r
511   ot("  bicne r10,r10,#0x40000000 ;@ Z\n");\r
512   ot("  orr r10,r10,#0x20000000 ;@ C\n");\r
513   ot("\n");\r
514 \r
515   EaWrite(11, 1, ea,0,0x3f,earwt_shifted_up);\r
516 \r
517   ot("finish%.4x%s\n",op,ms?"":":");\r
518   ot("  str r10,[r7,#0x4c] ;@ Save X\n");\r
519   ot("\n");\r
520 \r
521   ot("  ldr r6,[r7,#0x54]\n");\r
522   OpEnd(ea);\r
523 \r
524   return 0;\r
525 }\r
526 \r
527 // --------------------- Opcodes 0x90c0+ ---------------------\r
528 // Suba/Cmpa/Adda 1tt1nnnx 11eeeeee (tt=type, x=size, eeeeee=Source EA)\r
529 int OpAritha(int op)\r
530 {\r
531   int use=0;\r
532   int type=0,size=0,sea=0,dea=0;\r
533   const char *asr="";\r
534 \r
535   // Suba/Cmpa/Adda/(invalid):\r
536   type=(op>>13)&3; if (type>=3) return 1;\r
537 \r
538   size=(op>>8)&1; size++;\r
539   dea=(op>>9)&7; dea|=8; // Dest=An\r
540   sea=op&0x003f; // Source\r
541 \r
542   // See if we can do this opcode:\r
543   if (EaCanRead(sea,size)==0) return 1;\r
544 \r
545   use=OpBase(op,size);\r
546   use&=~0x0e00; // Use same opcode for An\r
547   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
548 \r
549   OpStart(op,sea); Cycles=(size==2)?6:8;\r
550   if(size==2&&(sea<0x10||sea==0x3c)) Cycles+=2;\r
551   if(type==1) Cycles=6;\r
552 \r
553   // EA calculation order defines how situations like  suba.w (A0)+, A0 get handled.\r
554   // different emus act differently in this situation, I couldn't fugure which is right behaviour.\r
555   //if (type == 1)\r
556   {\r
557     EaCalcRead(-1,0,sea,size,0x003f,earwt_msb_dont_care);\r
558     EaCalcRead(type!=1?11:-1,1,dea,2,0x0e00,earwt_msb_dont_care);\r
559   }\r
560 #if 0\r
561   else\r
562   {\r
563     EaCalcRead(type!=1?11:-1,1,dea,2,0x0e00,earwt_msb_dont_care);\r
564     EaCalcRead(-1,0,sea,size,0x003f,earwt_msb_dont_care);\r
565   }\r
566 #endif\r
567 \r
568   if (size<2) ot("  mov r0,r0,asl #%d\n\n",size?16:24);\r
569   if (size<2) asr=(char *)(size?",asr #16":",asr #24");\r
570 \r
571   if (type==0) ot("  sub r1,r1,r0%s\n",asr);\r
572   if (type==1) ot("  cmp r1,r0%s ;@ Defines NZCV\n",asr);\r
573   if (type==1) OpGetFlags(1,0); // Get Cmp flags\r
574   if (type==2) ot("  add r1,r1,r0%s\n",asr);\r
575   ot("\n");\r
576 \r
577   if (type!=1) EaWrite(11, 1, dea,2,0x0e00);\r
578 \r
579   OpEnd(sea);\r
580 \r
581   return 0;\r
582 }\r
583 \r
584 // --------------------- Opcodes 0x9100+ ---------------------\r
585 // Emit a Subx/Addx opcode, 1t01ddd1 zz00rsss addx.z Ds,Dd\r
586 int OpAddx(int op)\r
587 {\r
588   int use=0;\r
589   int type=0,size=0,dea=0,sea=0,mem=0;\r
590   const char *asl="";\r
591 \r
592   type=(op>>14)&1;\r
593   dea =(op>> 9)&7;\r
594   size=(op>> 6)&3; if (size>=3) return 1;\r
595   sea = op&7;\r
596   mem =(op>> 3)&1;\r
597 \r
598   // See if we can do this opcode:\r
599   if (EaCanRead(sea,size)==0) return 1;\r
600   if (EaCanWrite(dea)==0) return 1;\r
601 \r
602   if (mem) { sea+=0x20; dea+=0x20; }\r
603 \r
604   use=op&~0x0e07; // Use same opcode for Dn\r
605   if (size==0&&sea==0x27) use|=0x0007; // ___x.b -(a7)\r
606   if (size==0&&dea==0x27) use|=0x0e00; // ___x.b -(a7)\r
607   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
608 \r
609   OpStart(op,sea,dea); Cycles=4;\r
610   if(size>=2)   Cycles+=4;\r
611   if(sea>=0x10) Cycles+=2;\r
612 \r
613   if (mem)\r
614   {\r
615     ot(";@ Get src/dest EA vals\n");\r
616     EaCalc (0,0x000f, sea,size,earwt_shifted_up);\r
617     EaRead (0,     6, sea,size,0x000f,earwt_shifted_up);\r
618     EaCalcRead(11,0,dea,size,0x0e00,earwt_msb_dont_care);\r
619   }\r
620   else\r
621   {\r
622     ot(";@ Get src/dest reg vals\n");\r
623     EaCalcRead(-1,6,sea,size,0x0007,earwt_msb_dont_care);\r
624     EaCalcRead(11,0,dea,size,0x0e00,earwt_msb_dont_care);\r
625     if (size<2) ot("  mov r6,r6,asl #%d\n\n",size?16:24);\r
626   }\r
627 \r
628   if (size<2) asl=(char *)(size?",asl #16":",asl #24");\r
629 \r
630   ot(";@ Do arithmetic:\n");\r
631   GetXBit(type==0);\r
632 \r
633   if (type==1 && size<2)\r
634   {\r
635     ot(";@ Make sure the carry bit will tip the balance:\n");\r
636     ot("  mvn r2,#0\n");\r
637     ot("  orr r6,r6,r2,lsr #%i\n",(size==0)?8:16);\r
638     ot("\n");\r
639   }\r
640 \r
641   if (type==0) ot("  rscs r1,r6,r0%s\n",asl);\r
642   if (type==1) ot("  adcs r1,r6,r0%s\n",asl);\r
643   ot("  orr r3,r10,#0xb0000000 ;@ for old Z\n");\r
644   OpGetFlags(type==0,1,0); // subtract\r
645   if (size<2) {\r
646     ot("  movs r2,r1,lsr #%i\n", size?16:24);\r
647     ot("  orreq r10,r10,#0x40000000 ;@ add potentially missed Z\n");\r
648   }\r
649   ot("  andeq r10,r10,r3 ;@ fix Z\n");\r
650   ot("\n");\r
651 \r
652   ot(";@ Save result:\n");\r
653   EaWrite(11, 1, dea,size,0x0e00,earwt_shifted_up);\r
654 \r
655   ot("  ldr r6,[r7,#0x54]\n");\r
656   OpEnd(sea,dea);\r
657 \r
658   return 0;\r
659 }\r
660 \r
661 // --------------------- Opcodes 0xb000+ ---------------------\r
662 // Emit a Cmp/Eor opcode, 1011rrrt xxeeeeee (rrr=Dn, t=cmp/eor, xx=size extension, eeeeee=ea)\r
663 int OpCmpEor(int op)\r
664 {\r
665   int rea=0,eor=0;\r
666   int size=0,ea=0,use=0;\r
667   const char *asl="";\r
668 \r
669   // Get EA and register EA\r
670   rea=(op>>9)&7;\r
671   eor=(op>>8)&1;\r
672   size=(op>>6)&3; if (size>=3) return 1;\r
673   ea=op&0x3f;\r
674 \r
675   if (eor && (ea>>3) == 1) return 1; // not a valid mode for eor\r
676 \r
677   // See if we can do this opcode:\r
678   if (EaCanRead(ea,size)==0) return 1;\r
679   if (eor && EaCanWrite(ea)==0) return 1;\r
680   if (EaAn(ea)&&(eor||size==0)) return 1;\r
681 \r
682   use=OpBase(op,size);\r
683   use&=~0x0e00; // Use 1 handler for register d0-7\r
684   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
685 \r
686   OpStart(op,ea); Cycles=4;\r
687   if(eor) {\r
688     if(ea>8)     Cycles+=4;\r
689     if(size>=2)  Cycles+=4;\r
690   } else {\r
691     if(size>=2)  Cycles+=2;\r
692   }\r
693 \r
694   ot(";@ Get EA into r11 and value into r0:\n");\r
695   EaCalcRead(eor?11:-1,0,ea,size,0x003f,earwt_msb_dont_care);\r
696 \r
697   ot(";@ Get register operand into r1:\n");\r
698   EaCalcRead(-1,1,rea,size,0x0e00,earwt_msb_dont_care);\r
699 \r
700   if (size<2) ot("  mov r0,r0,asl #%d\n\n",size?16:24);\r
701   if (size<2) asl=(char *)(size?",asl #16":",asl #24");\r
702 \r
703   ot(";@ Do arithmetic:\n");\r
704   if (eor)\r
705   {\r
706     ot("  eors r1,r0,r1%s\n",asl);\r
707     OpGetFlagsNZ(1);\r
708   }\r
709   else\r
710   {\r
711     ot("  rsbs r1,r0,r1%s\n",asl);\r
712     OpGetFlags(1,0); // Cmp like subtract\r
713   }\r
714   ot("\n");\r
715 \r
716   if (eor) EaWrite(11, 1,ea,size,0x003f,earwt_shifted_up);\r
717 \r
718   OpEnd(ea);\r
719   return 0;\r
720 }\r
721 \r
722 // Emit a Cmpm opcode, 1011ddd1 xx001sss (rrr=Adst, xx=size extension, sss=Asrc)\r
723 int OpCmpm(int op)\r
724 {\r
725   int size=0,sea=0,dea=0,use=0;\r
726   const char *asl="";\r
727 \r
728   // get size, get EAs\r
729   size=(op>>6)&3; if (size>=3) return 1;\r
730   sea=(op&7)|0x18;\r
731   dea=(op>>9)&0x3f;\r
732 \r
733   use=op&~0x0e07; // Use 1 handler for all registers..\r
734   if (size==0&&sea==0x1f) use|=0x0007; // ..except (a7)+\r
735   if (size==0&&dea==0x1f) use|=0x0e00;\r
736   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
737 \r
738   OpStart(op,sea); Cycles=4;\r
739 \r
740   ot(";@ Get src operand into r11:\n");\r
741   EaCalc (0,0x0007, sea,size,earwt_shifted_up);\r
742   EaRead (0,    11, sea,size,0x0007,earwt_shifted_up);\r
743 \r
744   ot(";@ Get dst operand into r0:\n");\r
745   EaCalcRead(-1,0,dea,size,0x0e00,earwt_msb_dont_care);\r
746 \r
747   if (size<2) asl=(char *)(size?",asl #16":",asl #24");\r
748 \r
749   ot("  rsbs r0,r11,r0%s\n",asl);\r
750   OpGetFlags(1,0); // Cmp like subtract\r
751   ot("\n");\r
752 \r
753   OpEnd(sea);\r
754   return 0;\r
755 }\r
756 \r
757 \r
758 // Emit a Chk opcode, 0100ddd1 x0eeeeee (rrr=Dn, x=size extension, eeeeee=ea)\r
759 int OpChk(int op)\r
760 {\r
761   int rea=0;\r
762   int size=0,ea=0,use=0;\r
763 \r
764   // Get EA and register EA\r
765   rea=(op>>9)&7;\r
766   if((op>>7)&1)\r
767        size=1; // word operation\r
768   else size=2; // long\r
769   ea=op&0x3f;\r
770 \r
771   if (EaAn(ea)) return 1; // not a valid mode\r
772   if (size!=1)  return 1; // 000 variant only supports word\r
773 \r
774   // See if we can do this opcode:\r
775   if (EaCanRead(ea,size)==0) return 1;\r
776 \r
777   use=OpBase(op,size);\r
778   use&=~0x0e00; // Use 1 handler for register d0-7\r
779   if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
780 \r
781   OpStart(op,ea); Cycles=10;\r
782 \r
783   ot(";@ Get value into r0:\n");\r
784   EaCalcRead(-1,0,ea,size,0x003f,earwt_msb_dont_care);\r
785 \r
786   ot(";@ Get register operand into r1:\n");\r
787   EaCalcRead(-1,1,rea,size,0x0e00,earwt_msb_dont_care);\r
788 \r
789   if (size<2) ot("  mov r0,r0,asl #%d\n",size?16:24);\r
790 \r
791   if (size<2) ot("  movs r1,r1,asl #%d\n\n",size?16:24);\r
792   else        ot("  adds r1,r1,#0 ;@ Define flags\n");\r
793 \r
794   ot(";@ get flags, including undocumented ones\n");\r
795   ot("  and r3,r10,#0x80000000\n");\r
796   OpGetFlagsNZ(1);\r
797 \r
798   ot(";@ is reg negative?\n");\r
799   ot("  bmi chktrap%.4x\n",op);\r
800 \r
801   ot(";@ Do arithmetic:\n");\r
802   ot("  cmp r1,r0\n");\r
803   ot("  bgt chktrap%.4x\n",op);\r
804 \r
805   ot(";@ old N remains\n");\r
806   ot("  orr r10,r10,r3\n");\r
807   OpEnd(ea);\r
808 \r
809   ot("chktrap%.4x%s ;@ CHK exception:\n",op,ms?"":":");\r
810   ot("  mov r0,#6\n");\r
811   ot("  bl Exception\n");\r
812   Cycles+=40;\r
813   OpEnd(ea);\r
814 \r
815   return 0;\r
816 }\r
817 \r
818 // vim:ts=2:sw=2:expandtab\r