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