fix compatibility with ancient gas
[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
65044ba9 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
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
3504ce94 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
d9d77995 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
3504ce94 53 if (type==5) ot(" eors r1,r10,r0%s\n",shiftstr);\r
6003a768 54\r
3504ce94 55 if (type< 2) OpGetFlagsNZ(1); // Ori/And\r
6003a768 56 if (type==2) OpGetFlags(1,1); // Sub: Subtract/X-bit\r
57 if (type==3) OpGetFlags(0,1); // Add: X-bit\r
3504ce94 58 if (type==5) OpGetFlagsNZ(1); // Eor\r
6003a768 59 if (type==6) OpGetFlags(1,0); // Cmp: Subtract\r
60 ot("\n");\r
61\r
62 if (type!=6)\r
63 {\r
65044ba9 64 EaWrite(11, 1, tea,size,0x003f,earwt_shifted_up);\r
6003a768 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
d9d77995 75 if (tea>=8) Cycles+=4;\r
76 if (type==1 && size>=2 && tea<8) Cycles-=2;\r
6003a768 77 }\r
78\r
d9d77995 79 OpEnd(sea,tea);\r
6003a768 80\r
81 return 0;\r
82}\r
83\r
84// --------------------- Opcodes 0x5000+ ---------------------\r
85int 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
d9d77995 100 if (EaCanWrite(ea) ==0) return 1;\r
101 if (size == 0 && EaAn(ea) ) return 1;\r
6003a768 102\r
d9d77995 103 use=OpBase(op,size,1);\r
6003a768 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
d9d77995 108 OpStart(op,ea);\r
6003a768 109 Cycles=ea<8?4:8;\r
d9d77995 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
6003a768 114\r
65044ba9 115 EaCalcRead(11,0,ea,size,0x003f,earwt_msb_dont_care);\r
6003a768 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
d9d77995 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
6003a768 127\r
6003a768 128 ot("\n");\r
d9d77995 129 }\r
130 else\r
131 {\r
132 sprintf(count,"#0x%.4x",8<<shift);\r
6003a768 133 }\r
134\r
d9d77995 135 if (size<2) ot(" mov r0,r0,asl #%d\n\n",size?16:24);\r
6003a768 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
65044ba9 143 EaWrite(11, 1, ea,size,0x003f,earwt_shifted_up);\r
6003a768 144\r
d9d77995 145 OpEnd(ea);\r
6003a768 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
152int OpArithReg(int op)\r
153{\r
154 int use=0;\r
155 int type=0,size=0,dir=0,rea=0,ea=0;\r
d9d77995 156 const char *asl="";\r
157 const char *strop=0;\r
6003a768 158\r
159 type=(op>>12)&5;\r
160 rea =(op>> 9)&7;\r
d9d77995 161 dir =(op>> 8)&1; // er,re\r
6003a768 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
d9d77995 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
6003a768 171\r
d9d77995 172 use=OpBase(op,size);\r
6003a768 173 use&=~0x0e00; // Use same opcode for Dn\r
174 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
175\r
d9d77995 176 OpStart(op,ea); Cycles=4;\r
6003a768 177\r
65044ba9 178 EaCalcRead(dir?11:-1,0,ea,size,0x003f,earwt_msb_dont_care);\r
d9d77995 179\r
65044ba9 180 EaCalcRead(dir?-1:11,1,rea,size,0x0e00,earwt_msb_dont_care);\r
6003a768 181\r
182 ot(";@ Do arithmetic:\n");\r
3504ce94 183 if (type==0) strop = "orrs";\r
d9d77995 184 if (type==1) strop = (char *) (dir ? "subs" : "rsbs");\r
3504ce94 185 if (type==4) strop = "ands";\r
d9d77995 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
6003a768 193\r
3504ce94 194 if (type&1) OpGetFlags(type==1,type&1); // add/subtract\r
195 else OpGetFlagsNZ(1);\r
6003a768 196 ot("\n");\r
197\r
198 ot(";@ Save result:\n");\r
d9d77995 199 if (size<2) ot(" mov r1,r1,asr #%d\n",size?16:24);\r
65044ba9 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
d9d77995 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
6003a768 214\r
d9d77995 215 OpEnd(ea);\r
6003a768 216\r
217 return 0;\r
218}\r
219\r
220// --------------------- Opcodes 0x80c0+ ---------------------\r
221int 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
d9d77995 233 if (EaCanRead(ea,1)==0||EaAn(ea)) return 1;\r
6003a768 234\r
d9d77995 235 use=OpBase(op,1);\r
6003a768 236 use&=~0x0e00; // Use same for all registers\r
237 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
238\r
d9d77995 239 OpStart(op,ea);\r
240 if(type) Cycles=54;\r
241 else Cycles=sign?158:140;\r
6003a768 242\r
65044ba9 243 EaCalcRead(-1,0,ea,1,0x003f,earwt_msb_dont_care);\r
6003a768 244\r
d9d77995 245 EaCalc(11,0x0e00,rea, 2);\r
246 EaRead(11, 2,rea, 2,0x0e00);\r
6003a768 247\r
d9d77995 248 ot(" movs r1,r0,asl #16\n");\r
249\r
250 if (type==0) // div\r
6003a768 251 {\r
d9d77995 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
6003a768 255 ot("\n");\r
256 \r
257 if (sign)\r
258 {\r
d9d77995 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
6003a768 263 ot("\n");\r
d9d77995 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
6003a768 267 ot("\n");\r
d9d77995 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
6003a768 276 }\r
277\r
d9d77995 278 ot("\n");\r
279 ot(";@ Divide r2 by r0\n");\r
6003a768 280 ot(" mov r3,#0\n");\r
d9d77995 281 ot(" mov r1,r0\n");\r
6003a768 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
d9d77995 294 ot(" teq r1,r0\n");\r
6003a768 295 ot(" movne r1,r1,lsr #1\n");\r
296 ot(" bne Divide%.4x\n",op);\r
297 ot("\n");\r
d9d77995 298 ot(";@r3==quotient,r2==remainder\n");\r
6003a768 299\r
300 if (sign)\r
301 {\r
d9d77995 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
6003a768 309\r
d9d77995 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
6003a768 326\r
3504ce94 327 ot(" movs r1,r3,lsl #16 ;@ Clip to 16-bits\n");\r
328 OpGetFlagsNZ(1);\r
6003a768 329\r
6003a768 330 ot(" mov r1,r1,lsr #16\n");\r
d9d77995 331 ot(" orr r1,r1,r2,lsl #16 ;@ Insert remainder\n");\r
6003a768 332 }\r
333\r
334 if (type==1)\r
335 {\r
6003a768 336 ot(";@ Get 16-bit signs right:\n");\r
d9d77995 337 ot(" mov r0,r1,%s #16\n",sign?"asr":"lsr");\r
6003a768 338 ot(" mov r2,r2,lsl #16\n");\r
d9d77995 339 ot(" mov r2,r2,%s #16\n",sign?"asr":"lsr");\r
6003a768 340 ot("\n");\r
341\r
3504ce94 342 ot(" muls r1,r2,r0\n");\r
343 OpGetFlagsNZ(1);\r
6003a768 344 }\r
345 ot("\n");\r
346\r
65044ba9 347 EaWrite(11, 1,rea, 2,0x0e00,earwt_shifted_up);\r
6003a768 348\r
d9d77995 349 if (type==0) ot("endofop%.4x%s\n",op,ms?"":":");\r
350 OpEnd(ea);\r
6003a768 351\r
d9d77995 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
6003a768 361\r
362 return 0;\r
363}\r
364\r
365// Get X Bit into carry - trashes r2\r
d9d77995 366int GetXBit(int subtract)\r
6003a768 367{\r
368 ot(";@ Get X bit:\n");\r
d9d77995 369 ot(" ldr r2,[r7,#0x4c]\n");\r
370 if (subtract) ot(" mvn r2,r2 ;@ Invert it\n");\r
cfe17eee 371 ot(" tst r2,r2,lsl #3 ;@ Get into Carry\n");\r
6003a768 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
378int OpAbcd(int op)\r
379{\r
380 int use=0;\r
d9d77995 381 int type=0,sea=0,mem=0,dea=0;\r
6003a768 382 \r
d9d77995 383 type=(op>>14)&1; // sbcd/abcd\r
6003a768 384 dea =(op>> 9)&7;\r
d9d77995 385 mem =(op>> 3)&1;\r
6003a768 386 sea = op &7;\r
387\r
d9d77995 388 if (mem) { sea|=0x20; dea|=0x20; }\r
6003a768 389\r
d9d77995 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
6003a768 393 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
394\r
d9d77995 395 OpStart(op,sea,dea); Cycles=6;\r
6003a768 396\r
d9d77995 397 if (mem)\r
398 {\r
399 ot(";@ Get src/dest EA vals\n");\r
ee2d4225 400 EaCalc (0,0x000f, sea,0,earwt_msb_dont_care);\r
401 EaRead (0, 6, sea,0,0x000f,earwt_msb_dont_care);\r
65044ba9 402 EaCalcRead(11,0,dea,0,0x0e00,earwt_msb_dont_care);\r
d9d77995 403 }\r
404 else\r
405 {\r
406 ot(";@ Get src/dest reg vals\n");\r
65044ba9 407 EaCalcRead(-1,6,sea,0,0x0007,earwt_msb_dont_care);\r
408 EaCalcRead(11,0,dea,0,0x0e00,earwt_msb_dont_care);\r
d9d77995 409 }\r
6003a768 410\r
d9d77995 411 ot(" bic r10,r10,#0xb1000000 ;@ clear all flags except old Z\n");\r
6003a768 412\r
ee2d4225 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
6003a768 417 if (type)\r
418 {\r
ee2d4225 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
94a9d9a8 428 ot(" addhi r12,r12,#6 ;@ Decimal adjust units\n");\r
ee2d4225 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
d9d77995 432 ot(" orrhi r10,r10,#0x20000000 ;@ C\n");\r
ee2d4225 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
6003a768 436 }\r
437 else\r
438 {\r
ee2d4225 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
94a9d9a8 455 ot(" addhi r1,r1,#0xa0\n");\r
ee2d4225 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
6003a768 459 }\r
d9d77995 460\r
ee2d4225 461 ot(" orrmi r10,r10,#0x80000000 ;@ Undefined N behavior\n");\r
462 ot(" bicne r10,r10,#0x40000000 ;@ Z flag\n");\r
d9d77995 463 ot(" str r10,[r7,#0x4c] ;@ Save X bit\n");\r
464 ot("\n");\r
465\r
65044ba9 466 EaWrite(11, 0, dea,0,0x0e00,earwt_shifted_up);\r
d9d77995 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
475int 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
ee2d4225 490 EaCalcRead(11,0,ea,0,0x003f,earwt_zero_extend);\r
d9d77995 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
d9d77995 495 ot(" and r2,r2,#0x20000000\n");\r
ee2d4225 496 ot(" rsb r1,r0,#0 ;@ do arithmetic\n");\r
497 ot(" subs r1,r1,r2,lsr #29 ;@ X\n");\r
d9d77995 498\r
d9d77995 499 ot(" beq finish%.4x\n",op);\r
6003a768 500 ot("\n");\r
501\r
ee2d4225 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
d9d77995 511 ot(" bicne r10,r10,#0x40000000 ;@ Z\n");\r
d9d77995 512 ot(" orr r10,r10,#0x20000000 ;@ C\n");\r
513 ot("\n");\r
514\r
ee2d4225 515 EaWrite(11, 1, ea,0,0x3f,earwt_shifted_up);\r
d9d77995 516\r
517 ot("finish%.4x%s\n",op,ms?"":":");\r
d9d77995 518 ot(" str r10,[r7,#0x4c] ;@ Save X\n");\r
519 ot("\n");\r
6003a768 520\r
d9d77995 521 ot(" ldr r6,[r7,#0x54]\n");\r
522 OpEnd(ea);\r
6003a768 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
529int OpAritha(int op)\r
530{\r
531 int use=0;\r
532 int type=0,size=0,sea=0,dea=0;\r
d9d77995 533 const char *asr="";\r
6003a768 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
d9d77995 545 use=OpBase(op,size);\r
6003a768 546 use&=~0x0e00; // Use same opcode for An\r
547 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
548\r
d9d77995 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
65044ba9 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
d9d77995 559 }\r
560#if 0\r
561 else\r
562 {\r
65044ba9 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
d9d77995 565 }\r
566#endif\r
6003a768 567\r
d9d77995 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
6003a768 570\r
d9d77995 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
6003a768 573 if (type==1) OpGetFlags(1,0); // Get Cmp flags\r
d9d77995 574 if (type==2) ot(" add r1,r1,r0%s\n",asr);\r
6003a768 575 ot("\n");\r
6003a768 576\r
d9d77995 577 if (type!=1) EaWrite(11, 1, dea,2,0x0e00);\r
6003a768 578\r
d9d77995 579 OpEnd(sea);\r
6003a768 580\r
581 return 0;\r
582}\r
583\r
584// --------------------- Opcodes 0x9100+ ---------------------\r
d9d77995 585// Emit a Subx/Addx opcode, 1t01ddd1 zz00rsss addx.z Ds,Dd\r
6003a768 586int OpAddx(int op)\r
587{\r
588 int use=0;\r
d9d77995 589 int type=0,size=0,dea=0,sea=0,mem=0;\r
590 const char *asl="";\r
6003a768 591\r
d9d77995 592 type=(op>>14)&1;\r
6003a768 593 dea =(op>> 9)&7;\r
594 size=(op>> 6)&3; if (size>=3) return 1;\r
d9d77995 595 sea = op&7;\r
596 mem =(op>> 3)&1;\r
6003a768 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
d9d77995 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
6003a768 607 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
608\r
d9d77995 609 OpStart(op,sea,dea); Cycles=4;\r
610 if(size>=2) Cycles+=4;\r
611 if(sea>=0x10) Cycles+=2;\r
6003a768 612\r
d9d77995 613 if (mem)\r
614 {\r
615 ot(";@ Get src/dest EA vals\n");\r
65044ba9 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
d9d77995 619 }\r
620 else\r
621 {\r
622 ot(";@ Get src/dest reg vals\n");\r
65044ba9 623 EaCalcRead(-1,6,sea,size,0x0007,earwt_msb_dont_care);\r
624 EaCalcRead(11,0,dea,size,0x0e00,earwt_msb_dont_care);\r
d9d77995 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
6003a768 629\r
630 ot(";@ Do arithmetic:\n");\r
d9d77995 631 GetXBit(type==0);\r
6003a768 632\r
d9d77995 633 if (type==1 && size<2)\r
6003a768 634 {\r
635 ot(";@ Make sure the carry bit will tip the balance:\n");\r
d9d77995 636 ot(" mvn r2,#0\n");\r
637 ot(" orr r6,r6,r2,lsr #%i\n",(size==0)?8:16);\r
6003a768 638 ot("\n");\r
639 }\r
640\r
d9d77995 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
6003a768 650 ot("\n");\r
651\r
652 ot(";@ Save result:\n");\r
65044ba9 653 EaWrite(11, 1, dea,size,0x0e00,earwt_shifted_up);\r
6003a768 654\r
d9d77995 655 ot(" ldr r6,[r7,#0x54]\n");\r
656 OpEnd(sea,dea);\r
6003a768 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
d9d77995 667 const char *asl="";\r
6003a768 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
d9d77995 675 if (eor && (ea>>3) == 1) return 1; // not a valid mode for eor\r
676\r
6003a768 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
d9d77995 680 if (EaAn(ea)&&(eor||size==0)) return 1;\r
6003a768 681\r
d9d77995 682 use=OpBase(op,size);\r
6003a768 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
d9d77995 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
6003a768 693\r
d9d77995 694 ot(";@ Get EA into r11 and value into r0:\n");\r
65044ba9 695 EaCalcRead(eor?11:-1,0,ea,size,0x003f,earwt_msb_dont_care);\r
6003a768 696\r
697 ot(";@ Get register operand into r1:\n");\r
65044ba9 698 EaCalcRead(-1,1,rea,size,0x0e00,earwt_msb_dont_care);\r
d9d77995 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
6003a768 702\r
703 ot(";@ Do arithmetic:\n");\r
6003a768 704 if (eor)\r
705 {\r
3504ce94 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
6003a768 713 }\r
6003a768 714 ot("\n");\r
715\r
65044ba9 716 if (eor) EaWrite(11, 1,ea,size,0x003f,earwt_shifted_up);\r
d9d77995 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
723int 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
65044ba9 741 EaCalc (0,0x0007, sea,size,earwt_shifted_up);\r
742 EaRead (0, 11, sea,size,0x0007,earwt_shifted_up);\r
d9d77995 743\r
744 ot(";@ Get dst operand into r0:\n");\r
65044ba9 745 EaCalcRead(-1,0,dea,size,0x0e00,earwt_msb_dont_care);\r
d9d77995 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
759int 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
65044ba9 784 EaCalcRead(-1,0,ea,size,0x003f,earwt_msb_dont_care);\r
d9d77995 785\r
786 ot(";@ Get register operand into r1:\n");\r
65044ba9 787 EaCalcRead(-1,1,rea,size,0x0e00,earwt_msb_dont_care);\r
d9d77995 788\r
789 if (size<2) ot(" mov r0,r0,asl #%d\n",size?16:24);\r
3504ce94 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
d9d77995 793\r
794 ot(";@ get flags, including undocumented ones\n");\r
795 ot(" and r3,r10,#0x80000000\n");\r
3504ce94 796 OpGetFlagsNZ(1);\r
d9d77995 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
d9d77995 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
6003a768 808\r
d9d77995 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
6003a768 814\r
6003a768 815 return 0;\r
816}\r
817\r
ee2d4225 818// vim:ts=2:sw=2:expandtab\r