make Ea* functions easier to work with
[cyclone68000.git] / OpMove.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
d9d77995 15// Pack our flags into r1, in SR/CCR register format\r
16// trashes r0,r2\r
17void OpFlagsToReg(int high)\r
18{\r
19 ot(" ldr r0,[r7,#0x4c] ;@ X bit\n");\r
20 ot(" mov r1,r10,lsr #28 ;@ ____NZCV\n");\r
21 ot(" eor r2,r1,r1,ror #1 ;@ Bit 0=C^V\n");\r
22 ot(" tst r2,#1 ;@ 1 if C!=V\n");\r
23 ot(" eorne r1,r1,#3 ;@ ____NZVC\n");\r
24 ot("\n");\r
25 if (high) ot(" ldrb r2,[r7,#0x44] ;@ Include SR high\n");\r
26 ot(" and r0,r0,#0x20000000\n");\r
27 ot(" orr r1,r1,r0,lsr #25 ;@ ___XNZVC\n");\r
28 if (high) ot(" orr r1,r1,r2,lsl #8\n");\r
29 ot("\n");\r
30}\r
31\r
32// Convert SR/CRR register in r0 to our flags\r
33// trashes r0,r1\r
34void OpRegToFlags(int high, int srh_reg)\r
35{\r
36 ot(" eor r1,r0,r0,ror #1 ;@ Bit 0=C^V\n");\r
37 ot(" mov r2,r0,lsl #25\n");\r
38 ot(" tst r1,#1 ;@ 1 if C!=V\n");\r
39 ot(" eorne r0,r0,#3 ;@ ___XNZCV\n");\r
40 ot(" str r2,[r7,#0x4c] ;@ Store X bit\n");\r
41 ot(" mov r10,r0,lsl #28 ;@ r10=NZCV...\n");\r
42\r
43 if (high)\r
44 {\r
45 int mask=EMULATE_TRACE?0xa7:0x27;\r
46 ot(" mov r%i,r0,ror #8\n",srh_reg);\r
47 ot(" and r%i,r%i,#0x%02x ;@ only take defined bits\n",srh_reg,srh_reg,mask);\r
48 ot(" strb r%i,[r7,#0x44] ;@ Store SR high\n",srh_reg);\r
49 }\r
50 ot("\n");\r
51}\r
52\r
53void SuperEnd(void)\r
54{\r
55 ot(";@ ----------\n");\r
56 ot(";@ tried execute privileged instruction in user mode\n");\r
57 ot("WrongPrivilegeMode%s\n",ms?"":":");\r
58#if EMULATE_ADDRESS_ERRORS_JUMP || EMULATE_ADDRESS_ERRORS_IO\r
59 ot(" ldr r1,[r7,#0x58]\n");\r
60 ot(" sub r4,r4,#2 ;@ last opcode wasn't executed - go back\n");\r
61 ot(" orr r1,r1,#4 ;@ set activity bit: 'not processing instruction'\n");\r
62 ot(" str r1,[r7,#0x58]\n");\r
63#else\r
64 ot(" sub r4,r4,#2 ;@ last opcode wasn't executed - go back\n");\r
65#endif\r
66 ot(" mov r0,#8 ;@ privilege violation\n");\r
67 ot(" bl Exception\n");\r
68 Cycles=34;\r
69 OpEnd(0);\r
70}\r
71\r
72// does OSP and A7 swapping if needed\r
73// new or old SR (not the one already in [r7,#0x44]) should be passed in r11\r
74// uses srh from srh_reg (loads if < 0), trashes r0,r11\r
75void SuperChange(int op,int srh_reg)\r
76{\r
77 ot(";@ A7 <-> OSP?\n");\r
78 if (srh_reg < 0) {\r
79 ot(" ldr r0,[r7,#0x44] ;@ Get other SR high\n");\r
80 srh_reg=0;\r
81 }\r
82 ot(" eor r0,r%i,r11\n",srh_reg);\r
83 ot(" tst r0,#0x20\n");\r
84 ot(" beq no_sp_swap%.4x\n",op);\r
85 ot(" ;@ swap OSP and A7:\n");\r
86 ot(" ldr r11,[r7,#0x3C] ;@ Get A7\n");\r
87 ot(" ldr r0, [r7,#0x48] ;@ Get OSP\n");\r
88 ot(" str r11,[r7,#0x48]\n");\r
89 ot(" str r0, [r7,#0x3C]\n");\r
90 ot("no_sp_swap%.4x%s\n", op, ms?"":":");\r
91}\r
92\r
93\r
94\r
6003a768 95// --------------------- Opcodes 0x1000+ ---------------------\r
96// Emit a Move opcode, 00xxdddd ddssssss\r
97int OpMove(int op)\r
98{\r
99 int sea=0,tea=0;\r
100 int size=0,use=0;\r
101 int movea=0;\r
102\r
103 // Get source and target EA\r
104 sea = op&0x003f;\r
105 tea =(op&0x01c0)>>3;\r
106 tea|=(op&0x0e00)>>9;\r
107\r
108 if (tea>=8 && tea<0x10) movea=1;\r
109\r
110 // Find size extension\r
111 switch (op&0x3000)\r
112 {\r
113 default: return 1;\r
114 case 0x1000: size=0; break;\r
115 case 0x3000: size=1; break;\r
116 case 0x2000: size=2; break;\r
117 }\r
118\r
d9d77995 119 if (size<1 && (movea || EaAn(sea))) return 1; // move.b An,* and movea.b * are invalid\r
6003a768 120\r
121 // See if we can do this opcode:\r
122 if (EaCanRead (sea,size)==0) return 1;\r
123 if (EaCanWrite(tea )==0) return 1;\r
124\r
d9d77995 125 use=OpBase(op,size);\r
6003a768 126 if (tea<0x38) use&=~0x0e00; // Use same handler for register ?0-7\r
127 \r
d9d77995 128 if (tea==0x1f || tea==0x27) use|=0x0e00; // Specific handler for (a7)+ and -(a7)\r
6003a768 129\r
130 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
131\r
d9d77995 132 OpStart(op,sea,tea); Cycles=4;\r
6003a768 133\r
d9d77995 134 if (movea==0)\r
135 {\r
65044ba9 136 EaCalcRead(-1,1,sea,size,0x003f,earwt_sign_extend,1);\r
3504ce94 137 OpGetFlagsNZ(1);\r
d9d77995 138 ot("\n");\r
139 }\r
140 else\r
141 {\r
65044ba9 142 EaCalcRead(-1,1,sea,size,0x003f,earwt_sign_extend);\r
d9d77995 143 size=2; // movea always expands to 32-bits\r
144 }\r
6003a768 145\r
d9d77995 146 eawrite_check_addrerr=1;\r
147#if SPLIT_MOVEL_PD\r
148 if ((tea&0x38)==0x20 && size==2) { // -(An)\r
65044ba9 149 EaCalc (8,0x0e00,tea,size,earwt_msb_dont_care);\r
d9d77995 150 ot(" mov r11,r1\n");\r
151 ot(" add r0,r8,#2\n");\r
65044ba9 152 EaWrite(0, 1,tea,1,0x0e00,earwt_msb_dont_care);\r
153 EaWrite(8, 11,tea,1,0x0e00,earwt_shifted_up);\r
d9d77995 154 }\r
155 else\r
156#endif\r
157 {\r
65044ba9 158 EaCalc (0,0x0e00,tea,size,earwt_msb_dont_care);\r
159 EaWrite(0, 1,tea,size,0x0e00,earwt_msb_dont_care);\r
d9d77995 160 }\r
6003a768 161\r
d9d77995 162#if CYCLONE_FOR_GENESIS && !MEMHANDLERS_CHANGE_CYCLES\r
163 // this is a bit hacky (device handlers might modify cycles)\r
164 if (tea==0x39||((0x10<=tea&&tea<0x30)&&size>=1))\r
165 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
166#endif\r
6003a768 167\r
d9d77995 168 if((tea&0x38)==0x20) Cycles-=2; // less cycles when dest is -(An)\r
6003a768 169\r
d9d77995 170 OpEnd(sea,tea);\r
6003a768 171 return 0;\r
172}\r
173\r
174// --------------------- Opcodes 0x41c0+ ---------------------\r
175// Emit an Lea opcode, 0100nnn1 11aaaaaa\r
176int OpLea(int op)\r
177{\r
178 int use=0;\r
179 int sea=0,tea=0;\r
180\r
181 sea= op&0x003f;\r
182 tea=(op&0x0e00)>>9; tea|=8;\r
183\r
d9d77995 184 if (EaCanRead(sea,-1)==0) return 1; // See if we can do this opcode\r
6003a768 185\r
d9d77995 186 use=OpBase(op,0);\r
6003a768 187 use&=~0x0e00; // Also use 1 handler for target ?0-7\r
188 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
189\r
d9d77995 190 OpStart(op,sea,tea);\r
6003a768 191\r
d9d77995 192 eawrite_check_addrerr=1;\r
6003a768 193 EaCalc (1,0x003f,sea,0); // Lea\r
194 EaCalc (0,0x0e00,tea,2);\r
d9d77995 195 EaWrite(0, 1,tea,2,0x0e00);\r
6003a768 196\r
d9d77995 197 Cycles=Ea_add_ns(g_lea_cycle_table,sea);\r
6003a768 198\r
d9d77995 199 OpEnd(sea,tea);\r
6003a768 200\r
201 return 0;\r
202}\r
203\r
204// --------------------- Opcodes 0x40c0+ ---------------------\r
d9d77995 205// Move SR opcode, 01000tt0 11aaaaaa move SR\r
6003a768 206int OpMoveSr(int op)\r
207{\r
208 int type=0,ea=0;\r
209 int use=0,size=1;\r
210\r
d9d77995 211 type=(op>>9)&3; // from SR, from CCR, to CCR, to SR\r
6003a768 212 ea=op&0x3f;\r
213\r
d9d77995 214 if(EaAn(ea)) return 1; // can't use An regs\r
215\r
6003a768 216 switch(type)\r
217 {\r
d9d77995 218 case 0:\r
6003a768 219 if (EaCanWrite(ea)==0) return 1; // See if we can do this opcode:\r
d9d77995 220 break;\r
6003a768 221\r
d9d77995 222 case 1:\r
223 return 1; // no such op in 68000\r
6003a768 224\r
225 case 2: case 3:\r
226 if (EaCanRead(ea,size)==0) return 1; // See if we can do this opcode:\r
d9d77995 227 break;\r
6003a768 228 }\r
229\r
d9d77995 230 use=OpBase(op,size);\r
6003a768 231 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
232\r
d9d77995 233 // 68000 model allows reading whole SR in user mode (but newer models don't)\r
234 OpStart(op,ea,0,0,type==3);\r
235 Cycles=12;\r
236 if (type==0) Cycles=(ea>=8)?8:6;\r
6003a768 237\r
238 if (type==0 || type==1)\r
239 {\r
d9d77995 240 eawrite_check_addrerr=1;\r
6003a768 241 OpFlagsToReg(type==0);\r
65044ba9 242 EaCalc (0,0x003f,ea,size,earwt_msb_dont_care);\r
243 EaWrite(0, 1,ea,size,0x003f,earwt_msb_dont_care);\r
6003a768 244 }\r
245\r
246 if (type==2 || type==3)\r
247 {\r
65044ba9 248 EaCalcRead(-1,0,ea,size,0x003f,earwt_msb_dont_care);\r
d9d77995 249 OpRegToFlags(type==3,1);\r
250 if (type==3) {\r
251 SuperChange(op,1);\r
252 opend_check_interrupt = 1;\r
253 opend_check_trace = 1;\r
254 OpEnd(ea);\r
255 return 0;\r
256 }\r
6003a768 257 }\r
258\r
d9d77995 259 OpEnd(ea);\r
6003a768 260\r
261 return 0;\r
262}\r
263\r
264\r
265// Ori/Andi/Eori $nnnn,sr 0000t0t0 01111100\r
266int OpArithSr(int op)\r
267{\r
268 int type=0,ea=0;\r
269 int use=0,size=0;\r
d9d77995 270 int sr_mask=EMULATE_TRACE?0xa7:0x27;\r
6003a768 271\r
272 type=(op>>9)&5; if (type==4) return 1;\r
d9d77995 273 size=(op>>6)&1; // ccr or sr?\r
6003a768 274 ea=0x3c;\r
275\r
d9d77995 276 use=OpBase(op,size);\r
6003a768 277 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
278\r
d9d77995 279 OpStart(op,ea,0,0,size!=0); Cycles=16;\r
280\r
65044ba9 281 EaCalcRead(-1,0,ea,size,0x003f,earwt_sign_extend);\r
6003a768 282\r
d9d77995 283 ot(" eor r1,r0,r0,ror #1 ;@ Bit 0=C^V\n");\r
284 ot(" tst r1,#1 ;@ 1 if C!=V\n");\r
285 ot(" eorne r0,r0,#3 ;@ ___XNZCV\n");\r
286 ot(" ldr r2,[r7,#0x4c] ;@ Load old X bit\n");\r
287\r
288 // note: old srh is already in r11 (done by OpStart)\r
289 if (type==0) {\r
290 ot(" orr r10,r10,r0,lsl #28\n");\r
291 ot(" orr r2,r2,r0,lsl #25 ;@ X bit\n");\r
292 if (size!=0) {\r
293 ot(" orr r1,r11,r0,lsr #8\n");\r
294 ot(" and r1,r1,#0x%02x ;@ mask-out unused bits\n",sr_mask);\r
295 }\r
296 }\r
297 if (type==1) {\r
298 ot(" and r10,r10,r0,lsl #28\n");\r
299 ot(" and r2,r2,r0,lsl #25 ;@ X bit\n");\r
300 if (size!=0)\r
301 ot(" and r1,r11,r0,lsr #8\n");\r
302 }\r
303 if (type==5) {\r
304 ot(" eor r10,r10,r0,lsl #28\n");\r
305 ot(" eor r2,r2,r0,lsl #25 ;@ X bit\n");\r
306 if (size!=0) {\r
307 ot(" eor r1,r11,r0,lsr #8\n");\r
308 ot(" and r1,r1,#0x%02x ;@ mask-out unused bits\n",sr_mask);\r
309 }\r
310 }\r
6003a768 311\r
d9d77995 312 ot(" str r2,[r7,#0x4c] ;@ Save X bit\n");\r
313 if (size!=0)\r
314 ot(" strb r1,[r7,#0x44]\n");\r
315 ot("\n");\r
6003a768 316\r
d9d77995 317 // we can't enter supervisor mode, nor unmask irqs just by using OR\r
318 if (size!=0 && type!=0) {\r
319 SuperChange(op,1);\r
320 ot("\n");\r
321 opend_check_interrupt = 1;\r
322 }\r
323 // also can't set trace bit with AND\r
324 if (size!=0 && type!=1)\r
325 opend_check_trace = 1;\r
6003a768 326\r
d9d77995 327 OpEnd(ea);\r
6003a768 328\r
329 return 0;\r
330}\r
331\r
332// --------------------- Opcodes 0x4850+ ---------------------\r
333// Emit an Pea opcode, 01001000 01aaaaaa\r
334int OpPea(int op)\r
335{\r
336 int use=0;\r
337 int ea=0;\r
338\r
339 ea=op&0x003f; if (ea<0x10) return 1; // Swap opcode\r
340 if (EaCanRead(ea,-1)==0) return 1; // See if we can do this opcode:\r
341\r
d9d77995 342 use=OpBase(op,0);\r
6003a768 343 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
344\r
d9d77995 345 OpStart(op,ea);\r
6003a768 346\r
d9d77995 347 ot(" ldr r11,[r7,#0x3c]\n");\r
6003a768 348 EaCalc (1,0x003f, ea,0);\r
349 ot("\n");\r
d9d77995 350 ot(" sub r0,r11,#4 ;@ Predecrement A7\n");\r
6003a768 351 ot(" str r0,[r7,#0x3c] ;@ Save A7\n");\r
352 ot("\n");\r
353 MemHandler(1,2); // Write 32-bit\r
354 ot("\n");\r
355\r
d9d77995 356 Cycles=6+Ea_add_ns(g_pea_cycle_table,ea);\r
357\r
358 OpEnd(ea);\r
6003a768 359\r
360 return 0;\r
361}\r
362\r
363// --------------------- Opcodes 0x4880+ ---------------------\r
364// Emit a Movem opcode, 01001d00 1xeeeeee regmask\r
365int OpMovem(int op)\r
366{\r
367 int size=0,ea=0,cea=0,dir=0;\r
368 int use=0,decr=0,change=0;\r
369\r
d9d77995 370 size=((op>>6)&1)+1; // word, long\r
6003a768 371 ea=op&0x003f;\r
d9d77995 372 dir=(op>>10)&1; // Direction (1==ea2reg)\r
373\r
374 if (dir) {\r
375 if (ea<0x10 || ea>0x3b || (ea&0x38)==0x20) return 1; // Invalid EA\r
376 } else {\r
377 if (ea<0x10 || ea>0x39 || (ea&0x38)==0x18) return 1;\r
378 }\r
6003a768 379\r
6003a768 380 if ((ea&0x38)==0x18 || (ea&0x38)==0x20) change=1;\r
381 if ((ea&0x38)==0x20) decr=1; // -(An), bitfield is decr\r
382\r
6003a768 383 cea=ea; if (change) cea=0x10;\r
384\r
d9d77995 385 use=OpBase(op,size);\r
6003a768 386 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
387\r
d9d77995 388 OpStart(op,ea,0,1);\r
6003a768 389\r
6003a768 390 ot(" ldrh r11,[r4],#2 ;@ r11=register mask\n");\r
6003a768 391 ot("\n");\r
d9d77995 392 ot(";@ Get the address into r6:\n");\r
393 EaCalc(6,0x003f,cea,size);\r
6003a768 394\r
d9d77995 395#if !MEMHANDLERS_NEED_PREV_PC\r
396 // must save PC, need a spare register\r
397 ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r
398#endif\r
399\r
400 ot(";@ r4=Register Index*4:\n");\r
401 if (decr) ot(" mov r4,#0x40 ;@ order reversed for -(An)\n");\r
402 else ot(" mov r4,#-4\n");\r
6003a768 403 \r
404 ot("\n");\r
d9d77995 405 ot(" tst r11,r11\n"); // sanity check\r
406 ot(" beq NoRegs%.4x\n",op);\r
6003a768 407\r
d9d77995 408#if EMULATE_ADDRESS_ERRORS_IO\r
6003a768 409 ot("\n");\r
d9d77995 410 ot(" tst r6,#1 ;@ address error?\n");\r
411 ot(" movne r0,r6\n");\r
412 ot(" bne ExceptionAddressError_%c_data\n",dir?'r':'w');\r
413#endif\r
6003a768 414\r
d9d77995 415 ot("\n");\r
416 ot("Movemloop%.4x%s\n",op, ms?"":":");\r
417 ot(" add r4,r4,#%d ;@ r4=Next Register\n",decr?-4:4);\r
418 ot(" movs r11,r11,lsr #1\n");\r
419 ot(" bcc Movemloop%.4x\n",op);\r
420 ot("\n");\r
421\r
422 if (decr) ot(" sub r6,r6,#%d ;@ Pre-decrement address\n",1<<size);\r
6003a768 423\r
424 if (dir)\r
425 {\r
426 ot(" ;@ Copy memory to register:\n",1<<size);\r
d9d77995 427 earead_check_addrerr=0; // already checked\r
428 EaRead (6,0,ea,size,0x003f);\r
429 ot(" str r0,[r7,r4] ;@ Save value into Dn/An\n");\r
6003a768 430 }\r
431 else\r
432 {\r
433 ot(" ;@ Copy register to memory:\n",1<<size);\r
d9d77995 434 ot(" ldr r1,[r7,r4] ;@ Load value from Dn/An\n");\r
435#if SPLIT_MOVEL_PD\r
436 if (decr && size==2) { // -(An)\r
437 ot(" add r0,r6,#2\n");\r
65044ba9 438 EaWrite(0,1,ea,1,0x003f,earwt_msb_dont_care);\r
d9d77995 439 ot(" ldr r1,[r7,r4] ;@ Load value from Dn/An\n");\r
440 ot(" mov r0,r6\n");\r
65044ba9 441 EaWrite(0,1,ea,1,0x003f,earwt_shifted_up);\r
d9d77995 442 }\r
443 else\r
444#endif\r
445 {\r
446 EaWrite(6,1,ea,size,0x003f);\r
447 }\r
6003a768 448 }\r
449\r
d9d77995 450 if (decr==0) ot(" add r6,r6,#%d ;@ Post-increment address\n",1<<size);\r
6003a768 451\r
452 ot(" sub r5,r5,#%d ;@ Take some cycles\n",2<<size);\r
d9d77995 453 ot(" tst r11,r11\n");\r
454 ot(" bne Movemloop%.4x\n",op);\r
6003a768 455 ot("\n");\r
456\r
457 if (change)\r
458 {\r
459 ot(";@ Write back address:\n");\r
460 EaCalc (0,0x0007,8|(ea&7),2);\r
d9d77995 461 EaWrite(0, 6,8|(ea&7),2,0x0007);\r
6003a768 462 }\r
463\r
d9d77995 464 ot("NoRegs%.4x%s\n",op, ms?"":":");\r
465 ot(" ldr r4,[r7,#0x40]\n");\r
466 ot(" ldr r6,[r7,#0x54] ;@ restore Opcode Jump table\n");\r
6003a768 467 ot("\n");\r
468\r
d9d77995 469 if(dir) { // er\r
470 if (ea==0x3a) Cycles=16; // ($nn,PC)\r
471 else if (ea==0x3b) Cycles=18; // ($nn,pc,Rn)\r
472 else Cycles=12;\r
473 } else {\r
474 Cycles=8;\r
475 }\r
6003a768 476\r
d9d77995 477 Cycles+=Ea_add_ns(g_movem_cycle_table,ea);\r
478\r
479 opend_op_changes_cycles = 1;\r
480 OpEnd(ea);\r
481 ot("\n");\r
6003a768 482\r
483 return 0;\r
484}\r
485\r
486// --------------------- Opcodes 0x4e60+ ---------------------\r
487// Emit a Move USP opcode, 01001110 0110dnnn move An to/from USP\r
488int OpMoveUsp(int op)\r
489{\r
490 int use=0,dir=0;\r
491\r
492 dir=(op>>3)&1; // Direction\r
493 use=op&~0x0007; // Use same opcode for all An\r
494\r
495 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
496\r
d9d77995 497 OpStart(op,0,0,0,1); Cycles=4;\r
6003a768 498\r
499 if (dir)\r
500 {\r
d9d77995 501 eawrite_check_addrerr=1;\r
6003a768 502 ot(" ldr r1,[r7,#0x48] ;@ Get from USP\n\n");\r
65044ba9 503 EaCalc (0,0x000f,8,2);\r
504 EaWrite(0, 1,8,2,0x000f);\r
6003a768 505 }\r
506 else\r
507 {\r
65044ba9 508 EaCalc (0,0x000f,8,2);\r
509 EaRead (0, 0,8,2,0x000f);\r
6003a768 510 ot(" str r0,[r7,#0x48] ;@ Put in USP\n\n");\r
511 }\r
512 \r
513 OpEnd();\r
514\r
6003a768 515 return 0;\r
516}\r
517\r
518// --------------------- Opcodes 0x7000+ ---------------------\r
519// Emit a Move Quick opcode, 0111nnn0 dddddddd moveq #dd,Dn\r
520int OpMoveq(int op)\r
521{\r
522 int use=0;\r
523\r
524 use=op&0xf100; // Use same opcode for all values\r
525 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
526\r
527 OpStart(op); Cycles=4;\r
528\r
529 ot(" movs r0,r8,asl #24\n");\r
530 ot(" and r1,r8,#0x0e00\n");\r
531 ot(" mov r0,r0,asr #24 ;@ Sign extended Quick value\n");\r
3504ce94 532 OpGetFlagsNZ(0);\r
6003a768 533 ot(" str r0,[r7,r1,lsr #7] ;@ Store into Dn\n");\r
534 ot("\n");\r
535\r
536 OpEnd();\r
537\r
538 return 0;\r
539}\r
540\r
541// --------------------- Opcodes 0xc140+ ---------------------\r
542// Emit a Exchange opcode:\r
543// 1100ttt1 01000sss exg ds,dt\r
544// 1100ttt1 01001sss exg as,at\r
545// 1100ttt1 10001sss exg as,dt\r
546int OpExg(int op)\r
547{\r
548 int use=0,type=0;\r
549\r
550 type=op&0xf8;\r
551\r
552 if (type!=0x40 && type!=0x48 && type!=0x88) return 1; // Not an exg opcode\r
553\r
554 use=op&0xf1f8; // Use same opcode for all values\r
555 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler\r
556\r
557 OpStart(op); Cycles=6;\r
558\r
d9d77995 559 ot(" and r2,r8,#0x0e00 ;@ Find T register\n");\r
560 ot(" and r3,r8,#0x000f ;@ Find S register\n");\r
561 if (type==0x48) ot(" orr r2,r2,#0x1000 ;@ T is an address register\n");\r
6003a768 562 ot("\n");\r
d9d77995 563 ot(" ldr r0,[r7,r2,lsr #7] ;@ Get T\n");\r
564 ot(" ldr r1,[r7,r3,lsl #2] ;@ Get S\n");\r
6003a768 565 ot("\n");\r
d9d77995 566 ot(" str r0,[r7,r3,lsl #2] ;@ T->S\n");\r
567 ot(" str r1,[r7,r2,lsr #7] ;@ S->T\n"); \r
6003a768 568 ot("\n");\r
569\r
570 OpEnd();\r
571 \r
572 return 0;\r
573}\r
d9d77995 574\r
575// ------------------------- movep -------------------------------\r
576// 0000ddd1 0z001sss\r
577// 0000sss1 1z001ddd (to mem)\r
578int OpMovep(int op)\r
579{\r
580 int ea=0,rea=0;\r
581 int size=1,use=0,dir,aadd=0;\r
582\r
583 use=op&0xf1f8;\r
584 if (op!=use) { OpUse(op,use); return 0; } // Use existing handler (for all dests, srcs)\r
585\r
586 // Get EA\r
587 ea = (op&0x0007)|0x28;\r
588 rea= (op&0x0e00)>>9;\r
589 dir = (op>>7)&1;\r
590\r
591 // Find size extension\r
592 if(op&0x0040) size=2;\r
593\r
594 OpStart(op,ea);\r
595 \r
596 if(dir) // reg to mem\r
597 {\r
65044ba9 598 EaCalcRead(-1,11,rea,size,0x0e00,earwt_msb_dont_care);\r
d9d77995 599\r
600 EaCalc(8,0x000f,ea,size);\r
601 if(size==2) { // if operand is long\r
602 ot(" mov r1,r11,lsr #24 ;@ first byte\n");\r
603 EaWrite(8,1,ea,0,0x000f); // store first byte\r
604 ot(" add r0,r8,#%i\n",(aadd+=2));\r
605 ot(" mov r1,r11,lsr #16 ;@ second byte\n");\r
606 EaWrite(0,1,ea,0,0x000f); // store second byte\r
607 ot(" add r0,r8,#%i\n",(aadd+=2));\r
608 } else {\r
609 ot(" mov r0,r8\n");\r
610 }\r
611 ot(" mov r1,r11,lsr #8 ;@ first or third byte\n");\r
612 EaWrite(0,1,ea,0,0x000f);\r
613 ot(" add r0,r8,#%i\n",(aadd+=2));\r
614 ot(" and r1,r11,#0xff\n");\r
615 EaWrite(0,1,ea,0,0x000f);\r
616 }\r
617 else // mem to reg\r
618 {\r
65044ba9 619 EaCalc(6,0x000f,ea,size,earwt_shifted_up);\r
620 EaRead(6,11,ea,0,0x000f,earwt_shifted_up); // read first byte\r
d9d77995 621 ot(" add r0,r6,#2\n");\r
65044ba9 622 EaRead(0,1,ea,0,0x000f,earwt_shifted_up); // read second byte\r
d9d77995 623 if(size==2) { // if operand is long\r
624 ot(" orr r11,r11,r1,lsr #8 ;@ second byte\n");\r
625 ot(" add r0,r6,#4\n");\r
65044ba9 626 EaRead(0,1,ea,0,0x000f,earwt_shifted_up);\r
d9d77995 627 ot(" orr r11,r11,r1,lsr #16 ;@ third byte\n");\r
628 ot(" add r0,r6,#6\n");\r
65044ba9 629 EaRead(0,1,ea,0,0x000f,earwt_shifted_up);\r
d9d77995 630 ot(" orr r1,r11,r1,lsr #24 ;@ fourth byte\n");\r
631 } else {\r
632 ot(" orr r1,r11,r1,lsr #8 ;@ second byte\n");\r
633 }\r
634 // store the result\r
65044ba9 635 EaCalc(0,0x0e00,rea,size,earwt_shifted_up);\r
636 EaWrite(0,1,rea,size,0x0e00,earwt_shifted_up);\r
d9d77995 637 ot(" ldr r6,[r7,#0x54]\n");\r
638 }\r
639\r
640 Cycles=(size==2)?24:16;\r
641 OpEnd(ea);\r
642\r
643 return 0;\r
644}\r
645\r
646// Emit a Stop/Reset opcodes, 01001110 011100t0 imm\r
647int OpStopReset(int op)\r
648{\r
649 int type=(op>>1)&1; // stop/reset\r
650\r
651 OpStart(op,0,0,0,1);\r
652\r
653 if(type) {\r
654 // copy immediate to SR, stop the CPU and eat all remaining cycles.\r
655 ot(" ldrh r0,[r4],#2 ;@ Fetch the immediate\n");\r
656 OpRegToFlags(1);\r
657 SuperChange(op,0);\r
658\r
659 ot("\n");\r
660\r
661 ot(" ldr r0,[r7,#0x58]\n");\r
662 ot(" mov r5,#0 ;@ eat cycles\n");\r
663 ot(" orr r0,r0,#1 ;@ stopped\n");\r
664 ot(" str r0,[r7,#0x58]\n");\r
665 ot("\n");\r
666\r
667 Cycles = 4;\r
668 ot("\n");\r
669 }\r
670 else\r
671 {\r
672 Cycles = 132;\r
673#if USE_RESET_CALLBACK\r
674 ot(" str r4,[r7,#0x40] ;@ Save PC\n");\r
675 ot(" mov r1,r10,lsr #28\n");\r
676 ot(" strb r1,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r
677 ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");\r
678 ot(" ldr r11,[r7,#0x90] ;@ ResetCallback\n");\r
679 ot(" tst r11,r11\n");\r
680 ot(" movne lr,pc\n");\r
681 ot(" bxne r11 ;@ call ResetCallback if it is defined\n");\r
682 ot(" ldrb r10,[r7,#0x46] ;@ r10 = Load Flags (NZCV)\n");\r
683 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
684 ot(" ldr r4,[r7,#0x40] ;@ Load PC\n");\r
685 ot(" mov r10,r10,lsl #28\n");\r
686 ot("\n");\r
687#endif\r
688 }\r
689\r
690 OpEnd();\r
691\r
692 return 0;\r
693}\r
694\r