added copyright line to top of source files next to license information
[cyclone68000.git] / Cyclone / OpArith.cpp
CommitLineData
6003a768 1\r
619b1824 2// This file is part of the Cyclone 68000 Emulator\r
3\r
c41b9b97 4// Copyright (c) 2011 FinalDave (emudave (at) gmail.com)\r
5\r
619b1824 6// This code is licensed under the GNU General Public License version 2.0 and the MAME License.\r
7// You can choose the license that has the most advantages for you.\r
8\r
9// SVN repository can be found at http://code.google.com/p/cyclone68000/\r
10\r
6003a768 11#include "app.h"\r
12\r
13// --------------------- Opcodes 0x0000+ ---------------------\r
14// Emit an Ori/And/Sub/Add/Eor/Cmp Immediate opcode, 0000ttt0 00aaaaaa\r
15int OpArith(int op)\r
16{\r
17 int type=0,size=0;\r
18 int sea=0,tea=0;\r
19 int use=0;\r
20\r
21 // Get source and target EA\r
22 type=(op>>9)&7; if (type==4 || type>=7) return 1;\r
23 size=(op>>6)&3; if (size>=3) return 1;\r
24 sea= 0x003c;\r
25 tea=op&0x003f;\r
26\r
27 // See if we can do this opcode:\r
28 if (EaCanRead(tea,size)==0) return 1;\r
29 if (type!=6 && EaCanWrite(tea)==0) return 1;\r
30\r
31 use=OpBase(op);\r
32 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
33\r
34 OpStart(op); Cycles=4;\r
35\r
36 EaCalc(10,0x0000, sea,size);\r
37 EaRead(10, 10, sea,size,1);\r
38\r
39 EaCalc(11,0x003f, tea,size);\r
40 EaRead(11, 0, tea,size,1);\r
41\r
42 ot(";@ Do arithmetic:\n");\r
43\r
44 if (type==0) ot(" orr r1,r0,r10\n");\r
45 if (type==1) ot(" and r1,r0,r10\n");\r
46 if (type==2) ot(" subs r1,r0,r10 ;@ Defines NZCV\n");\r
47 if (type==3) ot(" adds r1,r0,r10 ;@ Defines NZCV\n");\r
48 if (type==5) ot(" eor r1,r0,r10\n");\r
49 if (type==6) ot(" cmp r0,r10 ;@ Defines NZCV\n");\r
50\r
51 if (type<2 || type==5) ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n"); // 0,1,5\r
52\r
53 if (type< 2) OpGetFlags(0,0); // Ori/And\r
54 if (type==2) OpGetFlags(1,1); // Sub: Subtract/X-bit\r
55 if (type==3) OpGetFlags(0,1); // Add: X-bit\r
56 if (type==5) OpGetFlags(0,0); // Eor\r
57 if (type==6) OpGetFlags(1,0); // Cmp: Subtract\r
58 ot("\n");\r
59\r
60 if (type!=6)\r
61 {\r
62 EaWrite(11, 1, tea,size,1);\r
63 }\r
64\r
65 // Correct cycles:\r
66 if (type==6)\r
67 {\r
68 if (size>=2 && tea<0x10) Cycles+=2;\r
69 }\r
70 else\r
71 {\r
72 if (size>=2) Cycles+=4;\r
73 if (tea>=0x10) Cycles+=4;\r
74 if (Amatch && type==1 && size>=2 && tea<0x10) Cycles-=2;\r
75 }\r
76\r
77 OpEnd();\r
78\r
79 return 0;\r
80}\r
81\r
82// --------------------- Opcodes 0x5000+ ---------------------\r
83int OpAddq(int op)\r
84{\r
85 // 0101nnnt xxeeeeee (nnn=#8,1-7 t=addq/subq xx=size, eeeeee=EA)\r
86 int num=0,type=0,size=0,ea=0;\r
87 int use=0;\r
88 char count[16]="";\r
89 int shift=0;\r
90\r
91 num =(op>>9)&7; if (num==0) num=8;\r
92 type=(op>>8)&1;\r
93 size=(op>>6)&3; if (size>=3) return 1;\r
94 ea = op&0x3f;\r
95\r
96 // See if we can do this opcode:\r
97 if (EaCanRead (ea,size)==0) return 1;\r
98 if (EaCanWrite(ea )==0) return 1;\r
99\r
100 use=op; if (ea<0x38) use&=~7;\r
101 if ((ea&0x38)==0x08) { size=2; use&=~0xc0; } // Every addq #n,An is 32-bit\r
102\r
103 if (num!=8) use|=0x0e00; // If num is not 8, use same handler\r
104 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
105\r
106 OpStart(op);\r
107 Cycles=ea<8?4:8;\r
108 if (size>=2 && ea!=8) Cycles+=4;\r
109\r
110 EaCalc(10,0x003f, ea,size);\r
111 EaRead(10, 0, ea,size,1);\r
112\r
113 shift=32-(8<<size);\r
114\r
115 if (num!=8)\r
116 {\r
117 int lsr=9-shift;\r
118\r
119 if (lsr>=0) ot(" mov r2,r8,lsr #%d ;@ Get quick value\n", lsr);\r
120 else ot(" mov r2,r8,lsl #%d ;@ Get quick value\n",-lsr);\r
121\r
122 ot(" and r2,r2,#0x%.4x\n",7<<shift);\r
123 ot("\n");\r
124 strcpy(count,"r2");\r
125 }\r
126\r
127 if (num==8) sprintf(count,"#0x%.4x",8<<shift);\r
128\r
129 if (type==0) ot(" adds r1,r0,%s\n",count);\r
130 if (type==1) ot(" subs r1,r0,%s\n",count);\r
131\r
132 if ((ea&0x38)!=0x08) OpGetFlags(type,1);\r
133 ot("\n");\r
134\r
135 EaWrite(10, 1, ea,size,1);\r
136\r
137 OpEnd();\r
138\r
139 return 0;\r
140}\r
141\r
142// --------------------- Opcodes 0x8000+ ---------------------\r
143// 1t0tnnnd xxeeeeee (tt=type:or/sub/and/add xx=size, eeeeee=EA)\r
144int OpArithReg(int op)\r
145{\r
146 int use=0;\r
147 int type=0,size=0,dir=0,rea=0,ea=0;\r
148\r
149 type=(op>>12)&5;\r
150 rea =(op>> 9)&7;\r
151 dir =(op>> 8)&1;\r
152 size=(op>> 6)&3; if (size>=3) return 1;\r
153 ea = op&0x3f;\r
154\r
155 if (dir && ea<0x10) return 1; // addx/subx opcode\r
156\r
157 // See if we can do this opcode:\r
158 if (dir==0 && EaCanWrite(rea)==0) return 1;\r
159 if (dir && EaCanWrite( ea)==0) return 1;\r
160\r
161 use=OpBase(op);\r
162 use&=~0x0e00; // Use same opcode for Dn\r
163 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
164\r
165 OpStart(op); Cycles=4;\r
166\r
167 ot(";@ Get r10=EA r11=EA value\n");\r
168 EaCalc(10,0x003f, ea,size);\r
169 EaRead(10, 11, ea,size,1);\r
170 ot(";@ Get r0=Register r1=Register value\n");\r
171 EaCalc( 0,0x0e00,rea,size);\r
172 EaRead( 0, 1,rea,size,1);\r
173\r
174 ot(";@ Do arithmetic:\n");\r
175 if (type==0) ot(" orr ");\r
176 if (type==1) ot(" subs ");\r
177 if (type==4) ot(" and ");\r
178 if (type==5) ot(" adds ");\r
179 if (dir) ot("r1,r11,r1\n");\r
180 else ot("r1,r1,r11\n");\r
181\r
182 if ((type&1)==0) ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
183\r
184 OpGetFlags(type==1,type&1); // 1==subtract\r
185 ot("\n");\r
186\r
187 ot(";@ Save result:\n");\r
188 if (dir) EaWrite(10, 1, ea,size,1);\r
189 else EaWrite( 0, 1,rea,size,1);\r
190\r
191 if (size==1 && ea>=0x10) Cycles+=4;\r
192 if (size>=2) { if (ea<0x10) Cycles+=4; else Cycles+=2; }\r
193\r
194 OpEnd();\r
195\r
196 return 0;\r
197}\r
198\r
199// --------------------- Opcodes 0x80c0+ ---------------------\r
200int OpMul(int op)\r
201{\r
202 // Div/Mul: 1m00nnns 11eeeeee (m=Mul, nnn=Register Dn, s=signed, eeeeee=EA)\r
203 int type=0,rea=0,sign=0,ea=0;\r
204 int use=0;\r
205\r
206 type=(op>>14)&1; // div/mul\r
207 rea =(op>> 9)&7;\r
208 sign=(op>> 8)&1;\r
209 ea = op&0x3f;\r
210\r
211 // See if we can do this opcode:\r
212 if (EaCanRead(ea,1)==0) return 1;\r
213\r
214 use=OpBase(op);\r
215 use&=~0x0e00; // Use same for all registers\r
216 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
217\r
218 OpStart(op); Cycles=type?70:133;\r
219\r
220 EaCalc(10,0x003f, ea, 1);\r
221 EaRead(10, 10, ea, 1);\r
222\r
223 EaCalc (0,0x0e00,rea, 2);\r
224 EaRead (0, 2,rea, 2);\r
225\r
226 if (type==0)\r
227 {\r
228 ot(" cmp r10,#0\n");\r
229 ot(" moveq r10,#1 ;@ Divide by zero\n");\r
230 ot("\n");\r
231 \r
232 if (sign)\r
233 {\r
234 ot(" mov r11,#0 ;@ r11 = 1 if the result is negative\n");\r
235 ot(" eorlt r11,r11,#1\n");\r
236 ot(" rsblt r10,r10,#0 ;@ Make r10 positive\n");\r
237 ot("\n");\r
238 ot(" cmp r2,#0\n");\r
239 ot(" eorlt r11,r11,#1\n");\r
240 ot(" rsblt r2,r2,#0 ;@ Make r2 positive\n");\r
241 ot("\n");\r
242 }\r
243\r
244 ot(";@ Divide r2 by r10\n");\r
245 ot(" mov r3,#0\n");\r
246 ot(" mov r1,r10\n");\r
247 ot("\n");\r
248 ot(";@ Shift up divisor till it's just less than numerator\n");\r
249 ot("Shift%.4x%s\n",op,ms?"":":");\r
250 ot(" cmp r1,r2,lsr #1\n");\r
251 ot(" movls r1,r1,lsl #1\n");\r
252 ot(" bcc Shift%.4x\n",op);\r
253 ot("\n");\r
254\r
255 ot("Divide%.4x%s\n",op,ms?"":":");\r
256 ot(" cmp r2,r1\n");\r
257 ot(" adc r3,r3,r3 ;@ Double r3 and add 1 if carry set\n");\r
258 ot(" subcs r2,r2,r1\n");\r
259 ot(" teq r1,r10\n");\r
260 ot(" movne r1,r1,lsr #1\n");\r
261 ot(" bne Divide%.4x\n",op);\r
262 ot("\n");\r
263\r
264 if (sign)\r
265 {\r
266 ot(" tst r11,r11\n");\r
267 ot(" rsbne r3,r3,#0 ;@ Negate if result is negative\n");\r
268 }\r
269\r
270 ot(" mov r11,r2 ;@ Remainder\n");\r
271\r
272 ot(" adds r1,r3,#0 ;@ Defines NZ, clears CV\n");\r
273 OpGetFlags(0,0);\r
274\r
275 ot(" mov r1,r1,lsl #16 ;@ Clip to 16-bits\n");\r
276 ot(" mov r1,r1,lsr #16\n");\r
277 ot(" orr r1,r1,r11,lsl #16 ;@ Insert remainder\n");\r
278 }\r
279\r
280 if (type==1)\r
281 {\r
282 char *shift="asr";\r
283\r
284 ot(";@ Get 16-bit signs right:\n");\r
285 if (sign==0) { ot(" mov r10,r10,lsl #16\n"); shift="lsr"; }\r
286 ot(" mov r2,r2,lsl #16\n");\r
287\r
288 if (sign==0) ot(" mov r10,r10,lsr #16\n");\r
289 ot(" mov r2,r2,%s #16\n",shift);\r
290 ot("\n");\r
291\r
292 ot(" mul r1,r2,r10\n");\r
293 ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
294 OpGetFlags(0,0);\r
295\r
296 if (Amatch && ea==0x3c) Cycles-=4;\r
297 }\r
298 ot("\n");\r
299\r
300 EaWrite(0, 1,rea, 2);\r
301\r
302\r
303 OpEnd();\r
304\r
305 return 0;\r
306}\r
307\r
308// Get X Bit into carry - trashes r2\r
309static int GetXBit(int subtract)\r
310{\r
311 ot(";@ Get X bit:\n");\r
312 ot(" ldrb r2,[r7,#0x45]\n");\r
313 if (subtract) ot(" mvn r2,r2,lsl #28 ;@ Invert it\n");\r
314 else ot(" mov r2,r2,lsl #28\n");\r
315 ot(" msr cpsr_flg,r2 ;@ Get into Carry\n");\r
316 ot("\n");\r
317 return 0;\r
318}\r
319\r
320// --------------------- Opcodes 0x8100+ ---------------------\r
321// 1t00ddd1 0000asss - sbcd/abcd Ds,Dd or -(As),-(Ad)\r
322int OpAbcd(int op)\r
323{\r
324 int use=0;\r
325 int type=0,sea=0,addr=0,dea=0;\r
326 \r
327 type=(op>>14)&1;\r
328 dea =(op>> 9)&7;\r
329 addr=(op>> 3)&1;\r
330 sea = op &7;\r
331\r
332 if (addr) { sea|=0x20; dea|=0x20; }\r
333\r
334 use=op&~0x0e07; // Use same opcode for all registers\r
335 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
336\r
337 OpStart(op); Cycles=6;\r
338\r
339 EaCalc( 0,0x0007, sea,0);\r
340 EaRead( 0, 10, sea,0,1);\r
341 EaCalc(11,0x0e00, dea,0);\r
342 EaRead(11, 1, dea,0,1);\r
343\r
344 ot(" ldrb r2,[r7,#0x45] ;@ Get X bit\n");\r
345 ot(" tst r2,#2\n");\r
346 ot(" addne r10,r10,#0x01000000 ;@ Add carry bit\n");\r
347\r
348 if (type)\r
349 {\r
350 ot(";@ Add units into r2:\n");\r
351 ot(" and r2,r1, #0x0f000000\n");\r
352 ot(" and r0,r10,#0x0f000000\n");\r
353 ot(" add r2,r2,r0\n");\r
354 ot(" cmp r2,#0x0a000000\n");\r
355 ot(" addpl r1,r1,#0x06000000 ;@ Decimal adjust units\n");\r
356 ot(" add r1,r1,r10 ;@ Add BCD\n");\r
357 ot(" mov r0,r1,lsr #24\n");\r
358 ot(" cmp r0,#0xa0\n");\r
359 ot(" addpl r1,r1,#0x60000000 ;@ Decimal adjust tens\n");\r
360 OpGetFlags(0,1);\r
361 }\r
362 else\r
363 {\r
364 ot(";@ Sub units into r2:\n");\r
365 ot(" and r2,r1, #0x0f000000\n");\r
366 ot(" and r0,r10,#0x0f000000\n");\r
367 ot(" subs r2,r2,r0\n");\r
368 ot(" submi r1,r1,#0x06000000 ;@ Decimal adjust units\n");\r
369 ot(" subs r1,r1,r10 ;@ Subtract BCD\n");\r
370 ot(" submis r1,r1,#0x60000000 ;@ Decimal adjust tens\n");\r
371 OpGetFlags(1,1);\r
372 }\r
373 ot("\n");\r
374\r
375 EaWrite(11, 1, dea,0,1);\r
376\r
377 OpEnd();\r
378\r
379 return 0;\r
380}\r
381\r
382// --------------------- Opcodes 0x90c0+ ---------------------\r
383// Suba/Cmpa/Adda 1tt1nnnx 11eeeeee (tt=type, x=size, eeeeee=Source EA)\r
384int OpAritha(int op)\r
385{\r
386 int use=0;\r
387 int type=0,size=0,sea=0,dea=0;\r
388\r
389 // Suba/Cmpa/Adda/(invalid):\r
390 type=(op>>13)&3; if (type>=3) return 1;\r
391\r
392 size=(op>>8)&1; size++;\r
393 dea=(op>>9)&7; dea|=8; // Dest=An\r
394 sea=op&0x003f; // Source\r
395\r
396 // See if we can do this opcode:\r
397 if (EaCanRead(sea,size)==0) return 1;\r
398\r
399 use=OpBase(op);\r
400 use&=~0x0e00; // Use same opcode for An\r
401 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
402\r
403 OpStart(op); Cycles=4;\r
404 EaCalc ( 0,0x003f, sea,size);\r
405 EaRead ( 0, 10, sea,size);\r
406\r
407 EaCalc ( 0,0x0e00, dea,2);\r
408 EaRead ( 0, 1, dea,2);\r
409\r
410 if (type==0) ot(" sub r1,r1,r10\n");\r
411 if (type==1) ot(" cmp r1,r10 ;@ Defines NZCV\n");\r
412 if (type==1) OpGetFlags(1,0); // Get Cmp flags\r
413 if (type==2) ot(" add r1,r1,r10\n");\r
414 ot("\n");\r
415 \r
416 EaWrite( 0, 1, dea,2);\r
417\r
418 if (Amatch && sea==0x3c) Cycles-=size<2?4:8; // Correct?\r
419 if (size>=2) { if (sea<0x10) Cycles+=4; else Cycles+=2; }\r
420\r
421 OpEnd();\r
422\r
423 return 0;\r
424}\r
425\r
426// --------------------- Opcodes 0x9100+ ---------------------\r
427// Emit a Subx/Addx opcode, 1t01ddd1 zz000sss addx.z Ds,Dd\r
428int OpAddx(int op)\r
429{\r
430 int use=0;\r
431 int type=0,size=0,dea=0,sea=0;\r
432\r
433 type=(op>>12)&5;\r
434 dea =(op>> 9)&7;\r
435 size=(op>> 6)&3; if (size>=3) return 1;\r
436 sea = op&0x3f;\r
437\r
438 // See if we can do this opcode:\r
439 if (EaCanRead(sea,size)==0) return 1;\r
440 if (EaCanWrite(dea)==0) return 1;\r
441\r
442 use=OpBase(op);\r
443 use&=~0x0e00; // Use same opcode for Dn\r
444 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
445\r
446 OpStart(op); Cycles=8;\r
447\r
448 ot(";@ Get r10=EA r11=EA value\n");\r
449 EaCalc( 0,0x003f,sea,size);\r
450 EaRead( 0, 11,sea,size,1);\r
451 ot(";@ Get r0=Register r1=Register value\n");\r
452 EaCalc( 0,0x0e00,dea,size);\r
453 EaRead( 0, 1,dea,size,1);\r
454\r
455 ot(";@ Do arithmetic:\n");\r
456 GetXBit(type==1);\r
457\r
458 if (type==5 && size<2)\r
459 {\r
460 ot(";@ Make sure the carry bit will tip the balance:\n");\r
461 if (size==0) ot(" ldr r2,=0x00ffffff\n");\r
462 else ot(" ldr r2,=0x0000ffff\n");\r
463 ot(" orr r11,r11,r2\n");\r
464 ot("\n");\r
465 }\r
466\r
467 if (type==1) ot(" sbcs r1,r1,r11\n");\r
468 if (type==5) ot(" adcs r1,r1,r11\n");\r
469 OpGetFlags(type==1,1); // subtract\r
470 ot("\n");\r
471\r
472 ot(";@ Save result:\n");\r
473 EaWrite( 0, 1, dea,size,1);\r
474\r
475 OpEnd();\r
476\r
477 return 0;\r
478}\r
479\r
480// --------------------- Opcodes 0xb000+ ---------------------\r
481// Emit a Cmp/Eor opcode, 1011rrrt xxeeeeee (rrr=Dn, t=cmp/eor, xx=size extension, eeeeee=ea)\r
482int OpCmpEor(int op)\r
483{\r
484 int rea=0,eor=0;\r
485 int size=0,ea=0,use=0;\r
486\r
487 // Get EA and register EA\r
488 rea=(op>>9)&7;\r
489 eor=(op>>8)&1;\r
490 size=(op>>6)&3; if (size>=3) return 1;\r
491 ea=op&0x3f;\r
492\r
493 // See if we can do this opcode:\r
494 if (EaCanRead(ea,size)==0) return 1;\r
495 if (eor && EaCanWrite(ea)==0) return 1;\r
496\r
497 use=OpBase(op);\r
498 use&=~0x0e00; // Use 1 handler for register d0-7\r
499 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
500\r
501 OpStart(op); Cycles=eor?8:4;\r
502\r
503 ot(";@ Get EA into r10 and value into r0:\n");\r
504 EaCalc (10,0x003f, ea,size);\r
505 EaRead (10, 0, ea,size,1);\r
506\r
507 ot(";@ Get register operand into r1:\n");\r
508 EaCalc (1 ,0x0e00, rea,size);\r
509 EaRead (1, 1, rea,size,1);\r
510\r
511 ot(";@ Do arithmetic:\n");\r
512 if (eor==0) ot(" cmp r1,r0\n");\r
513 if (eor)\r
514 {\r
515 ot(" eor r1,r0,r1\n");\r
516 ot(" adds r1,r1,#0 ;@ Defines NZ, clears CV\n");\r
517 }\r
518\r
519 OpGetFlags(eor==0,0); // Cmp like subtract\r
520 ot("\n");\r
521\r
522 if (size>=2) Cycles+=4; // Correct?\r
523 if (ea==0x3c) Cycles-=4;\r
524\r
525 if (eor) EaWrite(10, 1,ea,size,1);\r
526\r
527 OpEnd();\r
528 return 0;\r
529}\r
530\r