1
2 // This file is part of the Cyclone 68000 Emulator
3
4 // Copyright (c) 2004,2011 FinalDave (emudave (at) gmail.com)
5 // Copyright (c) 2005-2011 Gražvydas "notaz" Ignotas (notasas (at) gmail.com)
6
7 // This code is licensed under the GNU General Public License version 2.0 and the MAME License.
8 // You can choose the license that has the most advantages for you.
9
10 // SVN repository can be found at http://code.google.com/p/cyclone68000/
11
12
13 #include "app.h"
14
15 int opend_op_changes_cycles, opend_check_interrupt, opend_check_trace;
16
17 static unsigned char OpData[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
18
OpRead16(unsigned int a)19 static unsigned short OpRead16(unsigned int a)
20 {
21 return (unsigned short)( (OpData[a&15]<<8) | OpData[(a+1)&15] );
22 }
23
24 // For opcode 'op' use handler 'use'
OpUse(int op,int use)25 void OpUse(int op,int use)
26 {
27 char text[64]="";
28 CyJump[op]=use;
29
30 if (op!=use) return;
31
32 // Disassemble opcode
33 DisaPc=0;
34 DisaText=text;
35 DisaWord=OpRead16;
36
37 DisaGet();
38 ot(";@ ---------- [%.4x] %s uses Op%.4x ----------\n",op,text,use);
39 }
40
OpStart(int op,int sea,int tea,int op_changes_cycles,int supervisor_check)41 void OpStart(int op, int sea, int tea, int op_changes_cycles, int supervisor_check)
42 {
43 int last_op_count=arm_op_count;
44
45 Cycles=0;
46 OpUse(op,op); // This opcode obviously uses this handler
47 ot("Op%.4x%s\n", op, ms?"":":");
48
49 if (supervisor_check)
50 {
51 // checks for supervisor bit, if not set, jumps to SuperEnd()
52 // also sets r11 to SR high value, SuperChange() uses this
53 ot(" ldr r11,[r7,#0x44] ;@ Get SR high\n");
54 }
55 if ((sea >= 0x10 && sea != 0x3c) || (tea >= 0x10 && tea != 0x3c))
56 {
57 #if MEMHANDLERS_NEED_PREV_PC
58 ot(" str r4,[r7,#0x50] ;@ Save prev PC + 2\n");
59 #endif
60 #if MEMHANDLERS_NEED_CYCLES
61 ot(" str r5,[r7,#0x5c] ;@ Save Cycles\n");
62 #endif
63 }
64 if (supervisor_check)
65 {
66 ot(" tst r11,#0x20 ;@ Check we are in supervisor mode\n");
67 ot(" beq WrongPrivilegeMode ;@ No\n");
68 }
69 if ((sea >= 0x10 && sea != 0x3c) || (tea >= 0x10 && tea != 0x3c)) {
70 #if MEMHANDLERS_CHANGE_CYCLES
71 if (op_changes_cycles)
72 ot(" mov r5,#0\n");
73 #endif
74 }
75 if (last_op_count!=arm_op_count)
76 ot("\n");
77 pc_dirty = 1;
78 opend_op_changes_cycles = opend_check_interrupt = opend_check_trace = 0;
79 }
80
OpEnd(int sea,int tea)81 void OpEnd(int sea, int tea)
82 {
83 int did_fetch=0;
84 opend_check_trace = opend_check_trace && EMULATE_TRACE;
85 #if MEMHANDLERS_CHANGE_CYCLES
86 if ((sea >= 0x10 && sea != 0x3c) || (tea >= 0x10 && tea != 0x3c))
87 {
88 if (opend_op_changes_cycles)
89 {
90 ot(" ldr r0,[r7,#0x5c] ;@ Load Cycles\n");
91 ot(" ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
92 ot(" add r5,r0,r5\n");
93 did_fetch=1;
94 }
95 else
96 {
97 ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");
98 }
99 }
100 #endif
101 if (!did_fetch)
102 ot(" ldrh r8,[r4],#2 ;@ Fetch next opcode\n");
103 if (opend_check_trace)
104 ot(" ldr r1,[r7,#0x44]\n");
105 ot(" subs r5,r5,#%d ;@ Subtract cycles\n",Cycles);
106 if (opend_check_trace)
107 {
108 ot(";@ CheckTrace:\n");
109 ot(" tst r1,#0x80\n");
110 ot(" bne CycloneDoTraceWithChecks\n");
111 ot(" cmp r5,#0\n");
112 }
113 if (opend_check_interrupt)
114 {
115 ot(" ble CycloneEnd\n");
116 ot(";@ CheckInterrupt:\n");
117 if (!opend_check_trace)
118 ot(" ldr r1,[r7,#0x44]\n");
119 ot(" movs r0,r1,lsr #24 ;@ Get IRQ level\n"); // same as ldrb r0,[r7,#0x47]
120 ot(" ldreq pc,[r6,r8,asl #2] ;@ Jump to next opcode handler\n");
121 ot(" cmp r0,#6 ;@ irq>6 ?\n");
122 ot(" andle r1,r1,#7 ;@ Get interrupt mask\n");
123 ot(" cmple r0,r1 ;@ irq<=6: Is irq<=mask ?\n");
124 ot(" ldrle pc,[r6,r8,asl #2] ;@ Jump to next opcode handler\n");
125 ot(" b CycloneDoInterruptGoBack\n");
126 }
127 else
128 {
129 ot(" ldrgt pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");
130 ot(" b CycloneEnd\n");
131 }
132 ot("\n");
133 }
134
OpBase(int op,int size,int sepa)135 int OpBase(int op,int size,int sepa)
136 {
137 int ea=op&0x3f; // Get Effective Address
138 if (ea<0x10) return sepa?(op&~0x7):(op&~0xf); // Use 1 handler for d0-d7 and a0-a7
139 if (size==0&&(ea==0x1f || ea==0x27)) return op; // Specific handler for (a7)+ and -(a7)
140 if (ea<0x38) return op&~7; // Use 1 handler for (a0)-(a7), etc...
141 return op;
142 }
143
144 // Get flags, trashes r2
OpGetFlags(int subtract,int xbit,int specialz)145 int OpGetFlags(int subtract,int xbit,int specialz)
146 {
147 if (specialz) ot(" orr r2,r10,#0xb0000000 ;@ for old Z\n");
148
149 ot(" mrs r10,cpsr ;@ r10=flags\n");
150
151 if (specialz) ot(" andeq r10,r10,r2 ;@ fix Z\n");
152
153 if (subtract) ot(" eor r10,r10,#0x20000000 ;@ Invert carry\n");
154
155 if (xbit)
156 {
157 ot(" str r10,[r7,#0x4c] ;@ Save X bit\n");
158 }
159 return 0;
160 }
161
OpGetFlagsNZ(int rd)162 void OpGetFlagsNZ(int rd)
163 {
164 ot(" and r10,r%d,#0x80000000 ;@ r10=N_flag\n",rd);
165 ot(" orreq r10,r10,#0x40000000 ;@ get NZ, clear CV\n");
166 }
167
168 // size 0=8bit, 1=16bit
SignExtend(int rd,int rs,int size)169 void SignExtend(int rd, int rs, int size)
170 {
171 if (size >= 2)
172 {
173 if (rd != rs)
174 ot(" mov r%d,r%d\n", rd, rs);
175 return;
176 }
177 #if defined(HAVE_ARMv6) && (HAVE_ARMv6)
178 if (size == 1)
179 ot(" sxth r%d,r%d ;@ sign extend\n", rd, rs);
180 else
181 ot(" sxtb r%d,r%d ;@ sign extend\n", rd, rs);
182 #else
183 int shift = size ? 16 : 24;
184 ot(" mov r%d,r%d,asl #%d\n", rd, rs, shift);
185 ot(" mov r%d,r%d,asr #%d ;@ sign extend\n", rd, rd, shift);
186 #endif
187 }
188
ZeroExtend(int rd,int rs,int size)189 void ZeroExtend(int rd, int rs, int size)
190 {
191 if (size >= 2)
192 {
193 if (rd != rs)
194 ot(" mov r%d,r%d\n", rd, rs);
195 return;
196 }
197 #if defined(HAVE_ARMv6) && (HAVE_ARMv6)
198 if (size == 1)
199 ot(" uxth r%d,r%d ;@ zero extend\n", rd, rs);
200 else
201 #else
202 if (size == 1)
203 {
204 ot(" mov r%d,r%d,lsl #16\n", rd, rs);
205 ot(" mov r%d,r%d,lsr #16 ;@ zero extend\n", rd, rd);
206 }
207 else
208 #endif
209 {
210 ot(" and r%d,r%d,#0xff ;@ zero extend\n", rd, rs);
211 }
212 }
213
214 // -----------------------------------------------------------------
215
216 int g_op;
217
OpAny(int op)218 void OpAny(int op)
219 {
220 memset(OpData,0x33,sizeof(OpData));
221 OpData[0]=(unsigned char)(op>>8);
222 OpData[1]=(unsigned char)op;
223 g_op=op;
224
225 if ((op&0xf100)==0x0000) OpArith(op); // +
226 if ((op&0xc000)==0x0000) OpMove(op); // +
227 if ((op&0xf5bf)==0x003c) OpArithSr(op); // + Ori/Andi/Eori $nnnn,sr
228 if ((op&0xf100)==0x0100) OpBtstReg(op); // +
229 if ((op&0xf138)==0x0108) OpMovep(op); // +
230 if ((op&0xff00)==0x0800) OpBtstImm(op); // +
231 if ((op&0xf900)==0x4000) OpNeg(op); // +
232 if ((op&0xf140)==0x4100) OpChk(op); // +
233 if ((op&0xf1c0)==0x41c0) OpLea(op); // +
234 if ((op&0xf9c0)==0x40c0) OpMoveSr(op); // +
235 if ((op&0xffc0)==0x4800) OpNbcd(op); // +
236 if ((op&0xfff8)==0x4840) OpSwap(op); // +
237 if ((op&0xffc0)==0x4840) OpPea(op); // +
238 if ((op&0xffb8)==0x4880) OpExt(op); // +
239 if ((op&0xfb80)==0x4880) OpMovem(op); // +
240 if ((op&0xff00)==0x4a00) OpTst(op); // +
241 if ((op&0xffc0)==0x4ac0) OpTas(op); // +
242 if ((op&0xfff0)==0x4e40) OpTrap(op); // +
243 if ((op&0xfff8)==0x4e50) OpLink(op); // +
244 if ((op&0xfff8)==0x4e58) OpUnlk(op); // +
245 if ((op&0xfff0)==0x4e60) OpMoveUsp(op); // +
246 if ((op&0xfff8)==0x4e70) Op4E70(op); // + Reset/Rts etc
247 if ((op&0xfffd)==0x4e70) OpStopReset(op);// +
248 if ((op&0xff80)==0x4e80) OpJsr(op); // +
249 if ((op&0xf000)==0x5000) OpAddq(op); // +
250 if ((op&0xf0c0)==0x50c0) OpSet(op); // +
251 if ((op&0xf0f8)==0x50c8) OpDbra(op); // +
252 if ((op&0xf000)==0x6000) OpBranch(op); // +
253 if ((op&0xf100)==0x7000) OpMoveq(op); // +
254 if ((op&0xa000)==0x8000) OpArithReg(op); // + Or/Sub/And/Add
255 if ((op&0xb1f0)==0x8100) OpAbcd(op); // +
256 if ((op&0xb0c0)==0x80c0) OpMul(op); // +
257 if ((op&0x90c0)==0x90c0) OpAritha(op); // +
258 if ((op&0xb130)==0x9100) OpAddx(op); // +
259 if ((op&0xf000)==0xb000) OpCmpEor(op); // +
260 if ((op&0xf138)==0xb108) OpCmpm(op); // +
261 if ((op&0xf130)==0xc100) OpExg(op); // +
262 if ((op&0xf000)==0xe000) OpAsr(op); // + Asr/l/Ror/l etc
263 if ((op&0xf8c0)==0xe0c0) OpAsrEa(op); // +
264
265 if (op==0xffff)
266 {
267 SuperEnd();
268 }
269 }
270
271