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