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