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