0.0088 release
[picodrive.git] / cpu / Cyclone / OpArith.cpp
CommitLineData
cc68a136 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
6int 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
b637c56a 11 char *shiftstr="";\r
cc68a136 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
85a36a57 23 use=OpBase(op,size);\r
cc68a136 24 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
25\r
cfb3dfa0 26 OpStart(op, sea, tea); Cycles=4;\r
cc68a136 27\r
03c5768c 28 // imm must be read first\r
85a36a57 29 EaCalcReadNoSE(-1,10,sea,size,0);\r
03c5768c 30 EaCalcReadNoSE((type!=6)?11:-1,0,tea,size,0x003f);\r
cc68a136 31\r
85a36a57 32 if (size<2) shiftstr=(char *)(size?",asl #16":",asl #24");\r
03c5768c 33 if (size<2) ot(" mov r10,r10,asl #%i\n",size?16:24);\r
b637c56a 34\r
cc68a136 35 ot(";@ Do arithmetic:\n");\r
36\r
03c5768c 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
cc68a136 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
cfb3dfa0 70 OpEnd(sea,tea);\r
cc68a136 71\r
72 return 0;\r
73}\r
74\r
75// --------------------- Opcodes 0x5000+ ---------------------\r
76int 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
85a36a57 94 use=OpBase(op,size,1);\r
cc68a136 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
a6785576 99 OpStart(op,ea);\r
cc68a136 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
85a36a57 106 EaCalcReadNoSE(10,0,ea,size,0x003f);\r
cc68a136 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
3a5e6cf8 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
cc68a136 118\r
cc68a136 119 ot("\n");\r
cc68a136 120 }\r
3a5e6cf8 121 else\r
122 {\r
123 sprintf(count,"#0x%.4x",8<<shift);\r
124 }\r
cc68a136 125\r
85a36a57 126 if (size<2) ot(" mov r0,r0,asl #%d\n\n",size?16:24);\r
127\r
cc68a136 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
cfb3dfa0 136 OpEnd(ea);\r
cc68a136 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
143int OpArithReg(int op)\r
144{\r
145 int use=0;\r
146 int type=0,size=0,dir=0,rea=0,ea=0;\r
85a36a57 147 char *asl="";\r
148 char *strop=0;\r
cc68a136 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
85a36a57 163 use=OpBase(op,size);\r
cc68a136 164 use&=~0x0e00; // Use same opcode for Dn\r
165 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
166\r
a6785576 167 OpStart(op,ea); Cycles=4;\r
cc68a136 168\r
85a36a57 169 EaCalcReadNoSE(dir?10:-1,0,ea,size,0x003f);\r
170\r
171 EaCalcReadNoSE(dir?-1:10,1,rea,size,0x0e00);\r
cc68a136 172\r
173 ot(";@ Do arithmetic:\n");\r
85a36a57 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
cc68a136 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
85a36a57 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
cc68a136 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
a6785576 202 Cycles+=2;\r
203 if(ea<0x10||ea==0x3c) Cycles+=2;\r
204 }\r
cc68a136 205 }\r
206\r
cfb3dfa0 207 OpEnd(ea);\r
cc68a136 208\r
209 return 0;\r
210}\r
211\r
212// --------------------- Opcodes 0x80c0+ ---------------------\r
213int 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
85a36a57 227 use=OpBase(op,1);\r
cc68a136 228 use&=~0x0e00; // Use same for all registers\r
229 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
230\r
a6785576 231 OpStart(op,ea);\r
cc68a136 232 if(type) Cycles=54;\r
233 else Cycles=sign?158:140;\r
234\r
85a36a57 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
cc68a136 239\r
1c88b865 240 ot(" movs r1,r0,asl #16\n");\r
cc68a136 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
cc68a136 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
cc68a136 252 ot(" tst r2,r2\n");\r
253 ot(" orrmi r11,r11,#2\n");\r
254 ot(" rsbmi r2,r2,#0 ;@ Make r2 positive\n");\r
255 ot("\n");\r
1c88b865 256 ot(" movs r0,r1,asr #16\n");\r
257 ot(" orrmi r11,r11,#1\n");\r
258 ot(" rsbmi r0,r0,#0 ;@ Make r0 positive\n");\r
259 ot("\n");\r
260 ot(";@ detect the nasty 0x80000000 / -1 situation\n");\r
261 ot(" mov r3,r2,asr #31\n");\r
262 ot(" eors r3,r3,r1,asr #16\n");\r
263 ot(" beq wrendofop%.4x\n",op);\r
cc68a136 264 }\r
265 else\r
a6785576 266 {\r
1c88b865 267 ot(" mov r0,r1,lsr #16 ;@ use only 16 bits of divisor\n");\r
a6785576 268 }\r
cc68a136 269\r
1c88b865 270 ot("\n");\r
85a36a57 271 ot(";@ Divide r2 by r0\n");\r
cc68a136 272 ot(" mov r3,#0\n");\r
85a36a57 273 ot(" mov r1,r0\n");\r
cc68a136 274 ot("\n");\r
275 ot(";@ Shift up divisor till it's just less than numerator\n");\r
276 ot("Shift%.4x%s\n",op,ms?"":":");\r
277 ot(" cmp r1,r2,lsr #1\n");\r
278 ot(" movls r1,r1,lsl #1\n");\r
279 ot(" bcc Shift%.4x\n",op);\r
280 ot("\n");\r
281\r
282 ot("Divide%.4x%s\n",op,ms?"":":");\r
283 ot(" cmp r2,r1\n");\r
284 ot(" adc r3,r3,r3 ;@ Double r3 and add 1 if carry set\n");\r
285 ot(" subcs r2,r2,r1\n");\r
85a36a57 286 ot(" teq r1,r0\n");\r
cc68a136 287 ot(" movne r1,r1,lsr #1\n");\r
288 ot(" bne Divide%.4x\n",op);\r
289 ot("\n");\r
290 ot(";@r3==quotient,r2==remainder\n");\r
291\r
292 if (sign)\r
293 {\r
a6785576 294 // sign correction\r
cc68a136 295 ot(" and r1,r11,#1\n");\r
296 ot(" teq r1,r11,lsr #1\n");\r
297 ot(" rsbne r3,r3,#0 ;@ negate if quotient is negative\n");\r
298 ot(" tst r11,#2\n");\r
299 ot(" rsbne r2,r2,#0 ;@ negate the remainder if divident was negative\n");\r
300 ot("\n");\r
301\r
a6785576 302 // signed overflow check\r
303 ot(" mov r1,r3,asl #16\n");\r
304 ot(" cmp r3,r1,asr #16 ;@ signed overflow?\n");\r
cc68a136 305 ot(" orrne r9,r9,#0x10000000 ;@ set overflow flag\n");\r
306 ot(" bne endofop%.4x ;@ overflow!\n",op);\r
1c88b865 307 ot("\n");\r
308 ot("wrendofop%.4x%s\n",op,ms?"":":");\r
cc68a136 309 }\r
a6785576 310 else\r
311 {\r
312 // overflow check\r
313 ot(" movs r1,r3,lsr #16 ;@ check for overflow condition\n");\r
cc68a136 314 ot(" orrne r9,r9,#0x10000000 ;@ set overflow flag\n");\r
315 ot(" bne endofop%.4x ;@ overflow!\n",op);\r
1c88b865 316 ot("\n");\r
a6785576 317 }\r
cc68a136 318\r
319 ot(" mov r1,r3,lsl #16 ;@ Clip to 16-bits\n");\r
320 ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
321 OpGetFlags(0,0);\r
322\r
323 ot(" mov r1,r1,lsr #16\n");\r
324 ot(" orr r1,r1,r2,lsl #16 ;@ Insert remainder\n");\r
325 }\r
326\r
327 if (type==1)\r
328 {\r
cc68a136 329 ot(";@ Get 16-bit signs right:\n");\r
1c88b865 330 ot(" mov r0,r1,%s #16\n",sign?"asr":"lsr");\r
cc68a136 331 ot(" mov r2,r2,lsl #16\n");\r
85a36a57 332 ot(" mov r2,r2,%s #16\n",sign?"asr":"lsr");\r
cc68a136 333 ot("\n");\r
334\r
85a36a57 335 ot(" mul r1,r2,r0\n");\r
cc68a136 336 ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
337 OpGetFlags(0,0);\r
338 }\r
339 ot("\n");\r
340\r
85a36a57 341 EaWrite(10, 1,rea, 2,0x0e00,1);\r
cc68a136 342\r
85a36a57 343 if (type==0) ot("endofop%.4x%s\n",op,ms?"":":");\r
cfb3dfa0 344 OpEnd(ea);\r
cc68a136 345\r
85a36a57 346 if (type==0) // div\r
347 {\r
348 ot("divzero%.4x%s\n",op,ms?"":":");\r
0e11c502 349 ot(" mov r0,#5 ;@ Divide by zero\n");\r
85a36a57 350 ot(" bl Exception\n");\r
351 Cycles+=38;\r
352 OpEnd(ea);\r
353 ot("\n");\r
354 }\r
cc68a136 355\r
356 return 0;\r
357}\r
358\r
359// Get X Bit into carry - trashes r2\r
360int GetXBit(int subtract)\r
361{\r
362 ot(";@ Get X bit:\n");\r
85a36a57 363 ot(" ldr r2,[r7,#0x4c]\n");\r
364 if (subtract) ot(" mvn r2,r2 ;@ Invert it\n");\r
cc68a136 365 ot(" msr cpsr_flg,r2 ;@ Get into Carry\n");\r
366 ot("\n");\r
367 return 0;\r
368}\r
369\r
370// --------------------- Opcodes 0x8100+ ---------------------\r
371// 1t00ddd1 0000asss - sbcd/abcd Ds,Dd or -(As),-(Ad)\r
372int OpAbcd(int op)\r
373{\r
374 int use=0;\r
85a36a57 375 int type=0,sea=0,mem=0,dea=0;\r
cc68a136 376 \r
377 type=(op>>14)&1; // sbcd/abcd\r
378 dea =(op>> 9)&7;\r
85a36a57 379 mem =(op>> 3)&1;\r
cc68a136 380 sea = op &7;\r
381\r
85a36a57 382 if (mem) { sea|=0x20; dea|=0x20; }\r
cc68a136 383\r
384 use=op&~0x0e07; // Use same opcode for all registers..\r
85a36a57 385 if (sea==0x27) use|=0x0007; // ___x.b -(a7)\r
386 if (dea==0x27) use|=0x0e00; // ___x.b -(a7)\r
cc68a136 387 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
388\r
cfb3dfa0 389 OpStart(op,sea,dea); Cycles=6;\r
cc68a136 390\r
85a36a57 391 if (mem)\r
392 {\r
393 ot(";@ Get src/dest EA vals\n");\r
394 EaCalc (0,0x000f, sea,0,1);\r
395 EaRead (0, 10, sea,0,0x000f,1);\r
03c5768c 396 EaCalcReadNoSE(11,0,dea,0,0x0e00);\r
85a36a57 397 }\r
398 else\r
399 {\r
400 ot(";@ Get src/dest reg vals\n");\r
401 EaCalcReadNoSE(-1,10,sea,0,0x0007);\r
402 EaCalcReadNoSE(11,0,dea,0,0x0e00);\r
403 ot(" mov r10,r10,asl #24\n");\r
404 }\r
405 ot(" mov r1,r0,asl #24\n\n");\r
cc68a136 406\r
407 ot(" bic r9,r9,#0xb1000000 ;@ clear all flags except old Z\n");\r
408\r
409 if (type)\r
410 {\r
85a36a57 411 ot(" ldr r0,[r7,#0x4c] ;@ Get X bit\n");\r
cc68a136 412 ot(" mov r3,#0x00f00000\n");\r
413 ot(" and r2,r3,r1,lsr #4\n");\r
85a36a57 414 ot(" tst r0,#0x20000000\n");\r
cc68a136 415 ot(" and r0,r3,r10,lsr #4\n");\r
416 ot(" add r0,r0,r2\n");\r
417 ot(" addne r0,r0,#0x00100000\n");\r
418// ot(" tst r0,#0x00800000\n");\r
419// ot(" orreq r9,r9,#0x01000000 ;@ Undefined V behavior\n");\r
420 ot(" cmp r0,#0x00900000\n");\r
421 ot(" addhi r0,r0,#0x00600000 ;@ Decimal adjust units\n");\r
422\r
423 ot(" mov r2,r1,lsr #28\n");\r
424 ot(" add r0,r0,r2,lsl #24\n");\r
425 ot(" mov r2,r10,lsr #28\n");\r
426 ot(" add r0,r0,r2,lsl #24\n");\r
427 ot(" cmp r0,#0x09900000\n");\r
a6785576 428 ot(" orrhi r9,r9,#0x20000000 ;@ C\n");\r
429 ot(" subhi r0,r0,#0x0a000000\n");\r
430// ot(" and r3,r9,r0,lsr #3 ;@ Undefined V behavior part II\n");\r
431// ot(" orr r9,r9,r3,lsl #4 ;@ V\n");\r
cc68a136 432 ot(" movs r0,r0,lsl #4\n");\r
a6785576 433 ot(" orrmi r9,r9,#0x90000000 ;@ Undefined N+V behavior\n"); // this is what Musashi really does\r
434 ot(" bicne r9,r9,#0x40000000 ;@ Z flag\n");\r
cc68a136 435 }\r
436 else\r
437 {\r
85a36a57 438 ot(" ldr r0,[r7,#0x4c] ;@ Get X bit\n");\r
cc68a136 439 ot(" mov r3,#0x00f00000\n");\r
440 ot(" and r2,r3,r10,lsr #4\n");\r
85a36a57 441 ot(" tst r0,#0x20000000\n");\r
cc68a136 442 ot(" and r0,r3,r1,lsr #4\n");\r
443 ot(" sub r0,r0,r2\n");\r
444 ot(" subne r0,r0,#0x00100000\n");\r
445// ot(" tst r0,#0x00800000\n");\r
446// ot(" orreq r9,r9,#0x01000000 ;@ Undefined V behavior\n");\r
447 ot(" cmp r0,#0x00900000\n");\r
448 ot(" subhi r0,r0,#0x00600000 ;@ Decimal adjust units\n");\r
449\r
450 ot(" mov r2,r1,lsr #28\n");\r
451 ot(" add r0,r0,r2,lsl #24\n");\r
452 ot(" mov r2,r10,lsr #28\n");\r
453 ot(" sub r0,r0,r2,lsl #24\n");\r
454 ot(" cmp r0,#0x09900000\n");\r
a6785576 455 ot(" orrhi r9,r9,#0xa0000000 ;@ N and C\n");\r
456 ot(" addhi r0,r0,#0x0a000000\n");\r
457// ot(" and r3,r9,r0,lsr #3 ;@ Undefined V behavior part II\n");\r
458// ot(" orr r9,r9,r3,lsl #4 ;@ V\n");\r
cc68a136 459 ot(" movs r0,r0,lsl #4\n");\r
a6785576 460// ot(" orrmi r9,r9,#0x80000000 ;@ Undefined N behavior\n");\r
461 ot(" bicne r9,r9,#0x40000000 ;@ Z flag\n");\r
cc68a136 462 }\r
463\r
85a36a57 464 ot(" str r9,[r7,#0x4c] ;@ Save X bit\n");\r
465 ot("\n");\r
cc68a136 466\r
467 EaWrite(11, 0, dea,0,0x0e00,1);\r
cfb3dfa0 468 OpEnd(sea,dea);\r
cc68a136 469\r
470 return 0;\r
471}\r
472\r
85a36a57 473// 01001000 00eeeeee - nbcd <ea>\r
cc68a136 474int OpNbcd(int op)\r
475{\r
476 int use=0;\r
477 int ea=0;\r
478 \r
479 ea=op&0x3f;\r
480\r
481 if(EaCanWrite(ea)==0||EaAn(ea)) return 1;\r
482\r
85a36a57 483 use=OpBase(op,0);\r
cc68a136 484 if(op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
485\r
a6785576 486 OpStart(op,ea); Cycles=6;\r
cc68a136 487 if(ea >= 8) Cycles+=2;\r
488\r
85a36a57 489 EaCalcReadNoSE(10,0,ea,0,0x003f);\r
cc68a136 490\r
491 // this is rewrite of Musashi's code\r
85a36a57 492 ot(" ldr r2,[r7,#0x4c]\n");\r
493 ot(" bic r9,r9,#0xb0000000 ;@ clear all flags, except Z\n");\r
494 ot(" mov r0,r0,asl #24\n");\r
495 ot(" and r2,r2,#0x20000000\n");\r
496 ot(" add r2,r0,r2,lsr #5 ;@ add X\n");\r
1c88b865 497 ot(" rsb r11,r2,#0x9a000000 ;@ do arithmetic\n");\r
cc68a136 498\r
1c88b865 499 ot(" cmp r11,#0x9a000000\n");\r
cc68a136 500 ot(" beq finish%.4x\n",op);\r
501 ot("\n");\r
502\r
1c88b865 503 ot(" mvn r3,r11,lsr #31 ;@ Undefined V behavior\n",op);\r
504 ot(" and r2,r11,#0x0f000000\n");\r
cc68a136 505 ot(" cmp r2,#0x0a000000\n");\r
1c88b865 506 ot(" andeq r11,r11,#0xf0000000\n");\r
507 ot(" addeq r11,r11,#0x10000000\n");\r
508 ot(" and r3,r3,r11,lsr #31 ;@ Undefined V behavior part II\n",op);\r
509 ot(" movs r1,r11,asr #24\n");\r
cc68a136 510 ot(" bicne r9,r9,#0x40000000 ;@ Z\n");\r
1c88b865 511 ot(" orr r9,r9,r3,lsl #28 ;@ save V\n",op);\r
cc68a136 512 ot(" orr r9,r9,#0x20000000 ;@ C\n");\r
513 ot("\n");\r
514\r
1c88b865 515 EaWrite(10, 1, ea,0,0x3f,0,0);\r
cc68a136 516\r
517 ot("finish%.4x%s\n",op,ms?"":":");\r
1c88b865 518 ot(" tst r11,r11\n");\r
519 ot(" orrmi r9,r9,#0x80000000 ;@ N\n");\r
85a36a57 520 ot(" str r9,[r7,#0x4c] ;@ Save X\n");\r
1c88b865 521 ot("\n");\r
cc68a136 522\r
cfb3dfa0 523 OpEnd(ea);\r
cc68a136 524\r
525 return 0;\r
526}\r
527\r
528// --------------------- Opcodes 0x90c0+ ---------------------\r
529// Suba/Cmpa/Adda 1tt1nnnx 11eeeeee (tt=type, x=size, eeeeee=Source EA)\r
530int OpAritha(int op)\r
531{\r
532 int use=0;\r
533 int type=0,size=0,sea=0,dea=0;\r
85a36a57 534 char *asr="";\r
cc68a136 535\r
536 // Suba/Cmpa/Adda/(invalid):\r
537 type=(op>>13)&3; if (type>=3) return 1;\r
538\r
539 size=(op>>8)&1; size++;\r
540 dea=(op>>9)&7; dea|=8; // Dest=An\r
541 sea=op&0x003f; // Source\r
542\r
543 // See if we can do this opcode:\r
544 if (EaCanRead(sea,size)==0) return 1;\r
545\r
85a36a57 546 use=OpBase(op,size);\r
cc68a136 547 use&=~0x0e00; // Use same opcode for An\r
548 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
549\r
a6785576 550 OpStart(op,sea); Cycles=(size==2)?6:8;\r
cc68a136 551 if(size==2&&(sea<0x10||sea==0x3c)) Cycles+=2;\r
552 if(type==1) Cycles=6;\r
553\r
1c88b865 554 // EA calculation order defines how situations like suba.w (A0)+, A0 get handled.\r
555 // different emus act differently in this situation, I couldn't fugure which is right behaviour.\r
0e11c502 556 //if (type == 1)\r
1c88b865 557 {\r
558 EaCalcReadNoSE(-1,0,sea,size,0x003f);\r
559 EaCalcReadNoSE(type!=1?10:-1,11,dea,2,0x0e00);\r
560 }\r
0e11c502 561#if 0\r
1c88b865 562 else\r
563 {\r
564 EaCalcReadNoSE(type!=1?10:-1,11,dea,2,0x0e00);\r
565 EaCalcReadNoSE(-1,0,sea,size,0x003f);\r
566 }\r
0e11c502 567#endif\r
cc68a136 568\r
03c5768c 569 if (size<2) ot(" mov r0,r0,asl #%d\n\n",size?16:24);\r
85a36a57 570 if (size<2) asr=(char *)(size?",asr #16":",asr #24");\r
cc68a136 571\r
85a36a57 572 if (type==0) ot(" sub r11,r11,r0%s\n",asr);\r
573 if (type==1) ot(" cmp r11,r0%s ;@ Defines NZCV\n",asr);\r
cc68a136 574 if (type==1) OpGetFlags(1,0); // Get Cmp flags\r
85a36a57 575 if (type==2) ot(" add r11,r11,r0%s\n",asr);\r
cc68a136 576 ot("\n");\r
577 \r
03c5768c 578 if (type!=1) EaWrite(10, 11, dea,2,0x0e00);\r
cc68a136 579\r
cfb3dfa0 580 OpEnd(sea);\r
cc68a136 581\r
582 return 0;\r
583}\r
584\r
585// --------------------- Opcodes 0x9100+ ---------------------\r
586// Emit a Subx/Addx opcode, 1t01ddd1 zz00rsss addx.z Ds,Dd\r
587int OpAddx(int op)\r
588{\r
589 int use=0;\r
590 int type=0,size=0,dea=0,sea=0,mem=0;\r
85a36a57 591 char *asl="";\r
cc68a136 592\r
85a36a57 593 type=(op>>14)&1;\r
cc68a136 594 dea =(op>> 9)&7;\r
595 size=(op>> 6)&3; if (size>=3) return 1;\r
596 sea = op&7;\r
597 mem =(op>> 3)&1;\r
598\r
599 // See if we can do this opcode:\r
600 if (EaCanRead(sea,size)==0) return 1;\r
601 if (EaCanWrite(dea)==0) return 1;\r
602\r
603 if(mem) { sea+=0x20; dea+=0x20; }\r
604\r
605 use=op&~0x0e07; // Use same opcode for Dn\r
85a36a57 606 if (size==0&&sea==0x27) use|=0x0007; // ___x.b -(a7)\r
607 if (size==0&&dea==0x27) use|=0x0e00; // ___x.b -(a7)\r
cc68a136 608 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
609\r
cfb3dfa0 610 OpStart(op,sea,dea); Cycles=4;\r
cc68a136 611 if(size>=2) Cycles+=4;\r
612 if(sea>=0x10) Cycles+=2;\r
613\r
85a36a57 614 if (mem)\r
615 {\r
616 ot(";@ Get src/dest EA vals\n");\r
617 EaCalc (0,0x000f, sea,size,1);\r
618 EaRead (0, 11, sea,size,0x000f,1);\r
03c5768c 619 EaCalcReadNoSE(10,0,dea,size,0x0e00);\r
85a36a57 620 }\r
621 else\r
622 {\r
623 ot(";@ Get src/dest reg vals\n");\r
624 EaCalcReadNoSE(-1,11,sea,size,0x0007);\r
625 EaCalcReadNoSE(10,0,dea,size,0x0e00);\r
626 if (size<2) ot(" mov r11,r11,asl #%d\n\n",size?16:24);\r
627 }\r
628\r
629 if (size<2) asl=(char *)(size?",asl #16":",asl #24");\r
cc68a136 630\r
631 ot(";@ Do arithmetic:\n");\r
85a36a57 632 GetXBit(type==0);\r
cc68a136 633\r
85a36a57 634 if (type==1 && size<2)\r
cc68a136 635 {\r
636 ot(";@ Make sure the carry bit will tip the balance:\n");\r
a6785576 637 ot(" mvn r2,#0\n");\r
cc68a136 638 ot(" orr r11,r11,r2,lsr #%i\n",(size==0)?8:16);\r
639 ot("\n");\r
640 }\r
641\r
85a36a57 642 if (type==0) ot(" rscs r1,r11,r0%s\n",asl);\r
643 if (type==1) ot(" adcs r1,r11,r0%s\n",asl);\r
cc68a136 644 ot(" orr r3,r9,#0xb0000000 ;@ for old Z\n");\r
85a36a57 645 OpGetFlags(type==0,1,0); // subtract\r
cc68a136 646 if (size<2) {\r
647 ot(" movs r2,r1,lsr #%i\n", size?16:24);\r
648 ot(" orreq r9,r9,#0x40000000 ;@ add potentially missed Z\n");\r
649 }\r
650 ot(" andeq r9,r9,r3 ;@ fix Z\n");\r
651 ot("\n");\r
652\r
653 ot(";@ Save result:\n");\r
85a36a57 654 EaWrite(10, 1, dea,size,0x0e00,1);\r
cc68a136 655\r
cfb3dfa0 656 OpEnd(sea,dea);\r
cc68a136 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
663int OpCmpEor(int op)\r
664{\r
665 int rea=0,eor=0;\r
666 int size=0,ea=0,use=0;\r
85a36a57 667 char *asl="";\r
cc68a136 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
85a36a57 682 use=OpBase(op,size);\r
cc68a136 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
a6785576 686 OpStart(op,ea); Cycles=4;\r
cc68a136 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 r10 and value into r0:\n");\r
85a36a57 695 EaCalcReadNoSE(eor?10:-1,0,ea,size,0x003f);\r
cc68a136 696\r
697 ot(";@ Get register operand into r1:\n");\r
85a36a57 698 EaCalcReadNoSE(-1,1,rea,size,0x0e00);\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
cc68a136 702\r
703 ot(";@ Do arithmetic:\n");\r
85a36a57 704 if (eor==0) ot(" rsbs r1,r0,r1%s\n",asl);\r
cc68a136 705 if (eor)\r
706 {\r
85a36a57 707 ot(" eor r1,r0,r1%s\n",asl);\r
cc68a136 708 ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
709 }\r
710\r
711 OpGetFlags(eor==0,0); // Cmp like subtract\r
712 ot("\n");\r
713\r
714 if (eor) EaWrite(10, 1,ea,size,0x003f,1);\r
715\r
cfb3dfa0 716 OpEnd(ea);\r
cc68a136 717 return 0;\r
718}\r
719\r
720// Emit a Cmpm opcode, 1011ddd1 xx001sss (rrr=Adst, xx=size extension, sss=Asrc)\r
721int OpCmpm(int op)\r
722{\r
723 int size=0,sea=0,dea=0,use=0;\r
85a36a57 724 char *asl="";\r
cc68a136 725\r
726 // get size, get EAs\r
727 size=(op>>6)&3; if (size>=3) return 1;\r
728 sea=(op&7)|0x18;\r
729 dea=(op>>9)&0x3f;\r
730\r
731 use=op&~0x0e07; // Use 1 handler for all registers..\r
85a36a57 732 if (size==0&&sea==0x1f) use|=0x0007; // ..except (a7)+\r
733 if (size==0&&dea==0x1f) use|=0x0e00;\r
cc68a136 734 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
735\r
a6785576 736 OpStart(op,sea); Cycles=4;\r
cc68a136 737\r
738 ot(";@ Get src operand into r10:\n");\r
03c5768c 739 EaCalc (0,0x0007, sea,size,1);\r
740 EaRead (0, 10, sea,size,0x0007,1);\r
cc68a136 741\r
742 ot(";@ Get dst operand into r0:\n");\r
03c5768c 743 EaCalcReadNoSE(-1,0,dea,size,0x0e00);\r
cc68a136 744\r
85a36a57 745 if (size<2) asl=(char *)(size?",asl #16":",asl #24");\r
746\r
747 ot(" rsbs r0,r10,r0%s\n",asl);\r
cc68a136 748 OpGetFlags(1,0); // Cmp like subtract\r
85a36a57 749 ot("\n");\r
cc68a136 750\r
cfb3dfa0 751 OpEnd(sea);\r
cc68a136 752 return 0;\r
753}\r
754\r
755\r
756// Emit a Chk opcode, 0100ddd1 x0eeeeee (rrr=Dn, x=size extension, eeeeee=ea)\r
757int OpChk(int op)\r
758{\r
759 int rea=0;\r
760 int size=0,ea=0,use=0;\r
761\r
762 // Get EA and register EA\r
763 rea=(op>>9)&7;\r
764 if((op>>7)&1)\r
765 size=1; // word operation\r
766 else size=2; // long\r
767 ea=op&0x3f;\r
768\r
769 if (EaAn(ea)) return 1; // not a valid mode\r
770 if (size!=1) return 1; // 000 variant only supports word\r
771\r
772 // See if we can do this opcode:\r
773 if (EaCanRead(ea,size)==0) return 1;\r
774\r
85a36a57 775 use=OpBase(op,size);\r
cc68a136 776 use&=~0x0e00; // Use 1 handler for register d0-7\r
777 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
778\r
a6785576 779 OpStart(op,ea); Cycles=10;\r
cc68a136 780\r
781 ot(";@ Get EA into r10 and value into r0:\n");\r
85a36a57 782 EaCalcReadNoSE(-1,0,ea,size,0x003f);\r
cc68a136 783\r
784 ot(";@ Get register operand into r1:\n");\r
85a36a57 785 EaCalcReadNoSE(-1,1,rea,size,0x0e00);\r
786\r
787 if (size<2) ot(" mov r0,r0,asl #%d\n",size?16:24);\r
788 if (size<2) ot(" mov r1,r1,asl #%d\n\n",size?16:24);\r
cc68a136 789\r
790 ot(";@ get flags, including undocumented ones\n");\r
791 ot(" and r3,r9,#0x80000000\n");\r
792 ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
793 OpGetFlags(0,0);\r
794\r
795 ot(";@ is reg negative?\n");\r
796 ot(" bmi chktrap%.4x\n",op);\r
797\r
798 ot(";@ Do arithmetic:\n");\r
85a36a57 799 ot(" bic r9,r9,#0x80000000 ;@ N\n");\r
cc68a136 800 ot(" cmp r1,r0\n");\r
cc68a136 801 ot(" bgt chktrap%.4x\n",op);\r
802\r
803 ot(";@ old N remains\n");\r
cc68a136 804 ot(" orr r9,r9,r3\n");\r
cfb3dfa0 805 OpEnd(ea);\r
cc68a136 806\r
807 ot("chktrap%.4x%s ;@ CHK exception:\n",op,ms?"":":");\r
0e11c502 808 ot(" mov r0,#6\n");\r
cc68a136 809 ot(" bl Exception\n");\r
810 Cycles+=40;\r
cfb3dfa0 811 OpEnd(ea);\r
cc68a136 812\r
813 return 0;\r
814}\r
815\r