1 |
dpavlin |
14 |
/* |
2 |
|
|
* Copyright (C) 2005 Anders Gavare. All rights reserved. |
3 |
|
|
* |
4 |
|
|
* Redistribution and use in source and binary forms, with or without |
5 |
|
|
* modification, are permitted provided that the following conditions are met: |
6 |
|
|
* |
7 |
|
|
* 1. Redistributions of source code must retain the above copyright |
8 |
|
|
* notice, this list of conditions and the following disclaimer. |
9 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
|
|
* notice, this list of conditions and the following disclaimer in the |
11 |
|
|
* documentation and/or other materials provided with the distribution. |
12 |
|
|
* 3. The name of the author may not be used to endorse or promote products |
13 |
|
|
* derived from this software without specific prior written permission. |
14 |
|
|
* |
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
|
|
* SUCH DAMAGE. |
26 |
|
|
* |
27 |
|
|
* |
28 |
|
|
* $Id: cpu_avr_instr.c,v 1.3 2005/09/17 22:34:52 debug Exp $ |
29 |
|
|
* |
30 |
|
|
* Atmel AVR (8-bit) instructions. |
31 |
|
|
* |
32 |
|
|
* Individual functions should keep track of cpu->n_translated_instrs. Since |
33 |
|
|
* AVR uses variable length instructions, cpu->cd.avr.next_ic must also be |
34 |
|
|
* increased by the number of "instruction slots" that were executed. (I.e. |
35 |
|
|
* if an instruction occupying 6 bytes was executed, then next_ic should be |
36 |
|
|
* increased by 3.) |
37 |
|
|
* |
38 |
|
|
* (n_translated_instrs is automatically increased by 1 for each function |
39 |
|
|
* call. If no instruction was executed, then it should be decreased. If, say, |
40 |
|
|
* 4 instructions were combined into one function and executed, then it should |
41 |
|
|
* be increased by 3.) |
42 |
|
|
*/ |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
/*****************************************************************************/ |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
/* |
49 |
|
|
* nop: Do nothing. |
50 |
|
|
*/ |
51 |
|
|
X(nop) |
52 |
|
|
{ |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
|
56 |
|
|
/* |
57 |
|
|
* clX: Clear an sreg bit. |
58 |
|
|
*/ |
59 |
|
|
X(clc) { cpu->cd.avr.sreg &= ~AVR_SREG_C; } |
60 |
|
|
X(clz) { cpu->cd.avr.sreg &= ~AVR_SREG_Z; } |
61 |
|
|
X(cln) { cpu->cd.avr.sreg &= ~AVR_SREG_N; } |
62 |
|
|
X(clv) { cpu->cd.avr.sreg &= ~AVR_SREG_V; } |
63 |
|
|
X(cls) { cpu->cd.avr.sreg &= ~AVR_SREG_S; } |
64 |
|
|
X(clh) { cpu->cd.avr.sreg &= ~AVR_SREG_H; } |
65 |
|
|
X(clt) { cpu->cd.avr.sreg &= ~AVR_SREG_T; } |
66 |
|
|
X(cli) { cpu->cd.avr.sreg &= ~AVR_SREG_I; } |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
/* |
70 |
|
|
* ldi: Load immediate. |
71 |
|
|
* |
72 |
|
|
* arg[0]: ptr to register |
73 |
|
|
* arg[1]: byte value |
74 |
|
|
*/ |
75 |
|
|
X(ldi) |
76 |
|
|
{ |
77 |
|
|
*(uint8_t *)(ic->arg[0]) = ic->arg[1]; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
|
81 |
|
|
/* |
82 |
|
|
* mov: Copy register. |
83 |
|
|
* |
84 |
|
|
* arg[0]: ptr to rr |
85 |
|
|
* arg[1]: ptr to rd |
86 |
|
|
*/ |
87 |
|
|
X(mov) |
88 |
|
|
{ |
89 |
|
|
*(uint8_t *)(ic->arg[1]) = *(uint8_t *)(ic->arg[0]); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
/* |
94 |
|
|
* rjmp: Relative jump. |
95 |
|
|
* |
96 |
|
|
* arg[0]: relative offset |
97 |
|
|
*/ |
98 |
|
|
X(rjmp) |
99 |
|
|
{ |
100 |
|
|
uint32_t low_pc; |
101 |
|
|
|
102 |
|
|
cpu->cd.avr.extra_cycles ++; |
103 |
|
|
|
104 |
|
|
/* Calculate new PC from the next instruction + arg[0] */ |
105 |
|
|
low_pc = ((size_t)ic - (size_t)cpu->cd.avr.cur_ic_page) / |
106 |
|
|
sizeof(struct avr_instr_call); |
107 |
|
|
cpu->pc &= ~((AVR_IC_ENTRIES_PER_PAGE-1) |
108 |
|
|
<< AVR_INSTR_ALIGNMENT_SHIFT); |
109 |
|
|
cpu->pc += (low_pc << AVR_INSTR_ALIGNMENT_SHIFT); |
110 |
|
|
cpu->pc += (int32_t)ic->arg[0]; |
111 |
|
|
|
112 |
|
|
/* Find the new physical page and update the translation pointers: */ |
113 |
|
|
avr_pc_to_pointers(cpu); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
|
117 |
|
|
/* |
118 |
|
|
* rjmp_samepage: Relative jump (to within the same translated page). |
119 |
|
|
* |
120 |
|
|
* arg[0] = pointer to new avr_instr_call |
121 |
|
|
*/ |
122 |
|
|
X(rjmp_samepage) |
123 |
|
|
{ |
124 |
|
|
cpu->cd.avr.extra_cycles ++; |
125 |
|
|
cpu->cd.avr.next_ic = (struct avr_instr_call *) ic->arg[0]; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
|
129 |
|
|
/* |
130 |
|
|
* seX: Set an sreg bit. |
131 |
|
|
*/ |
132 |
|
|
X(sec) { cpu->cd.avr.sreg |= AVR_SREG_C; } |
133 |
|
|
X(sez) { cpu->cd.avr.sreg |= AVR_SREG_Z; } |
134 |
|
|
X(sen) { cpu->cd.avr.sreg |= AVR_SREG_N; } |
135 |
|
|
X(sev) { cpu->cd.avr.sreg |= AVR_SREG_V; } |
136 |
|
|
X(ses) { cpu->cd.avr.sreg |= AVR_SREG_S; } |
137 |
|
|
X(seh) { cpu->cd.avr.sreg |= AVR_SREG_H; } |
138 |
|
|
X(set) { cpu->cd.avr.sreg |= AVR_SREG_T; } |
139 |
|
|
X(sei) { cpu->cd.avr.sreg |= AVR_SREG_I; } |
140 |
|
|
|
141 |
|
|
|
142 |
|
|
/* |
143 |
|
|
* swap: Swap nibbles. |
144 |
|
|
* |
145 |
|
|
* arg[0]: ptr to rd |
146 |
|
|
*/ |
147 |
|
|
X(swap) |
148 |
|
|
{ |
149 |
|
|
uint8_t x = *(uint8_t *)(ic->arg[0]); |
150 |
|
|
*(uint8_t *)(ic->arg[0]) = (x >> 4) | (x << 4); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
|
154 |
|
|
/*****************************************************************************/ |
155 |
|
|
|
156 |
|
|
|
157 |
|
|
X(end_of_page) |
158 |
|
|
{ |
159 |
|
|
/* Update the PC: (offset 0, but on the next page) */ |
160 |
|
|
cpu->pc &= ~((AVR_IC_ENTRIES_PER_PAGE-1) << 1); |
161 |
|
|
cpu->pc += (AVR_IC_ENTRIES_PER_PAGE << 1); |
162 |
|
|
|
163 |
|
|
/* Find the new physical page and update the translation pointers: */ |
164 |
|
|
avr_pc_to_pointers(cpu); |
165 |
|
|
|
166 |
|
|
/* end_of_page doesn't count as an executed instruction: */ |
167 |
|
|
cpu->n_translated_instrs --; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
/*****************************************************************************/ |
172 |
|
|
|
173 |
|
|
|
174 |
|
|
/* |
175 |
|
|
* avr_combine_instructions(): |
176 |
|
|
* |
177 |
|
|
* Combine two or more instructions, if possible, into a single function call. |
178 |
|
|
*/ |
179 |
|
|
void avr_combine_instructions(struct cpu *cpu, struct avr_instr_call *ic, |
180 |
|
|
uint32_t addr) |
181 |
|
|
{ |
182 |
|
|
int n_back; |
183 |
|
|
n_back = (addr >> 1) & (AVR_IC_ENTRIES_PER_PAGE-1); |
184 |
|
|
|
185 |
|
|
if (n_back >= 1) { |
186 |
|
|
/* TODO */ |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
/* TODO: Combine forward as well */ |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
|
193 |
|
|
/*****************************************************************************/ |
194 |
|
|
|
195 |
|
|
|
196 |
|
|
/* |
197 |
|
|
* avr_instr_to_be_translated(): |
198 |
|
|
* |
199 |
|
|
* Translate an instruction word into an avr_instr_call. ic is filled in with |
200 |
|
|
* valid data for the translated instruction, or a "nothing" instruction if |
201 |
|
|
* there was a translation failure. The newly translated instruction is then |
202 |
|
|
* executed. |
203 |
|
|
*/ |
204 |
|
|
X(to_be_translated) |
205 |
|
|
{ |
206 |
|
|
int addr, low_pc, rd, rr, main_opcode; |
207 |
|
|
uint16_t iword; |
208 |
|
|
unsigned char *page; |
209 |
|
|
unsigned char ib[2]; |
210 |
|
|
void (*samepage_function)(struct cpu *, struct avr_instr_call *); |
211 |
|
|
|
212 |
|
|
/* Figure out the (virtual) address of the instruction: */ |
213 |
|
|
low_pc = ((size_t)ic - (size_t)cpu->cd.avr.cur_ic_page) |
214 |
|
|
/ sizeof(struct avr_instr_call); |
215 |
|
|
addr = cpu->pc & ~((AVR_IC_ENTRIES_PER_PAGE-1) << |
216 |
|
|
AVR_INSTR_ALIGNMENT_SHIFT); |
217 |
|
|
addr += (low_pc << AVR_INSTR_ALIGNMENT_SHIFT); |
218 |
|
|
cpu->pc = addr; |
219 |
|
|
addr &= ~((1 << AVR_INSTR_ALIGNMENT_SHIFT) - 1); |
220 |
|
|
|
221 |
|
|
addr &= cpu->cd.avr.pc_mask; |
222 |
|
|
|
223 |
|
|
/* Read the instruction word from memory: */ |
224 |
|
|
page = cpu->cd.avr.host_load[addr >> 12]; |
225 |
|
|
|
226 |
|
|
if (page != NULL) { |
227 |
|
|
/* fatal("TRANSLATION HIT!\n"); */ |
228 |
|
|
memcpy(ib, page + (addr & 0xfff), sizeof(ib)); |
229 |
|
|
} else { |
230 |
|
|
/* fatal("TRANSLATION MISS!\n"); */ |
231 |
|
|
if (!cpu->memory_rw(cpu, cpu->mem, addr, ib, |
232 |
|
|
sizeof(ib), MEM_READ, CACHE_INSTRUCTION)) { |
233 |
|
|
fatal("to_be_translated(): " |
234 |
|
|
"read failed: TODO\n"); |
235 |
|
|
goto bad; |
236 |
|
|
} |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
iword = *((uint16_t *)&ib[0]); |
240 |
|
|
|
241 |
|
|
#ifdef HOST_BIG_ENDIAN |
242 |
|
|
iword = ((iword & 0xff) << 8) | |
243 |
|
|
((iword & 0xff00) >> 8); |
244 |
|
|
#endif |
245 |
|
|
|
246 |
|
|
|
247 |
|
|
#define DYNTRANS_TO_BE_TRANSLATED_HEAD |
248 |
|
|
#include "cpu_dyntrans.c" |
249 |
|
|
#undef DYNTRANS_TO_BE_TRANSLATED_HEAD |
250 |
|
|
|
251 |
|
|
|
252 |
|
|
/* |
253 |
|
|
* Translate the instruction: |
254 |
|
|
*/ |
255 |
|
|
main_opcode = iword >> 12; |
256 |
|
|
|
257 |
|
|
switch (main_opcode) { |
258 |
|
|
|
259 |
|
|
case 0x0: |
260 |
|
|
if (iword == 0x0000) { |
261 |
|
|
ic->f = instr(nop); |
262 |
|
|
break; |
263 |
|
|
} |
264 |
|
|
goto bad; |
265 |
|
|
|
266 |
|
|
case 0x2: |
267 |
|
|
if ((iword & 0xfc00) == 0x2c00) { |
268 |
|
|
rd = (iword & 0x1f0) >> 4; |
269 |
|
|
rr = ((iword & 0x200) >> 5) | (iword & 0xf); |
270 |
|
|
ic->f = instr(mov); |
271 |
|
|
ic->arg[0] = (size_t)(&cpu->cd.avr.r[rr]); |
272 |
|
|
ic->arg[1] = (size_t)(&cpu->cd.avr.r[rd]); |
273 |
|
|
break; |
274 |
|
|
} |
275 |
|
|
goto bad; |
276 |
|
|
|
277 |
|
|
case 0x9: |
278 |
|
|
if ((iword & 0xfe0f) == 0x9402) { |
279 |
|
|
rd = (iword >> 4) & 31; |
280 |
|
|
ic->f = instr(swap); |
281 |
|
|
ic->arg[0] = (size_t)(&cpu->cd.avr.r[rd]); |
282 |
|
|
break; |
283 |
|
|
} |
284 |
|
|
if ((iword & 0xff8f) == 0x9408) { |
285 |
|
|
switch ((iword >> 4) & 7) { |
286 |
|
|
case 0: ic->f = instr(sec); break; |
287 |
|
|
case 1: ic->f = instr(sez); break; |
288 |
|
|
case 2: ic->f = instr(sen); break; |
289 |
|
|
case 3: ic->f = instr(sev); break; |
290 |
|
|
case 4: ic->f = instr(ses); break; |
291 |
|
|
case 5: ic->f = instr(seh); break; |
292 |
|
|
case 6: ic->f = instr(set); break; |
293 |
|
|
case 7: ic->f = instr(sei); break; |
294 |
|
|
} |
295 |
|
|
break; |
296 |
|
|
} |
297 |
|
|
if ((iword & 0xff8f) == 0x9488) { |
298 |
|
|
switch ((iword >> 4) & 7) { |
299 |
|
|
case 0: ic->f = instr(clc); break; |
300 |
|
|
case 1: ic->f = instr(clz); break; |
301 |
|
|
case 2: ic->f = instr(cln); break; |
302 |
|
|
case 3: ic->f = instr(clv); break; |
303 |
|
|
case 4: ic->f = instr(cls); break; |
304 |
|
|
case 5: ic->f = instr(clh); break; |
305 |
|
|
case 6: ic->f = instr(clt); break; |
306 |
|
|
case 7: ic->f = instr(cli); break; |
307 |
|
|
} |
308 |
|
|
break; |
309 |
|
|
} |
310 |
|
|
goto bad; |
311 |
|
|
|
312 |
|
|
case 0xc: |
313 |
|
|
ic->f = instr(rjmp); |
314 |
|
|
samepage_function = instr(rjmp_samepage); |
315 |
|
|
ic->arg[0] = (((int16_t)((iword & 0x0fff) << 4)) >> 3) + 2; |
316 |
|
|
/* Special case: branch within the same page: */ |
317 |
|
|
{ |
318 |
|
|
uint32_t mask_within_page = |
319 |
|
|
((AVR_IC_ENTRIES_PER_PAGE-1) << |
320 |
|
|
AVR_INSTR_ALIGNMENT_SHIFT) | |
321 |
|
|
((1 << AVR_INSTR_ALIGNMENT_SHIFT) - 1); |
322 |
|
|
uint32_t old_pc = addr; |
323 |
|
|
uint32_t new_pc = old_pc + (int32_t)ic->arg[0]; |
324 |
|
|
if ((old_pc & ~mask_within_page) == |
325 |
|
|
(new_pc & ~mask_within_page)) { |
326 |
|
|
ic->f = samepage_function; |
327 |
|
|
ic->arg[0] = (size_t) ( |
328 |
|
|
cpu->cd.avr.cur_ic_page + |
329 |
|
|
((new_pc & mask_within_page) >> |
330 |
|
|
AVR_INSTR_ALIGNMENT_SHIFT)); |
331 |
|
|
} |
332 |
|
|
} |
333 |
|
|
break; |
334 |
|
|
|
335 |
|
|
case 0xe: |
336 |
|
|
rd = ((iword >> 4) & 0xf) + 16; |
337 |
|
|
ic->f = instr(ldi); |
338 |
|
|
ic->arg[0] = (size_t)(&cpu->cd.avr.r[rd]); |
339 |
|
|
ic->arg[1] = ((iword >> 4) & 0xf0) | (iword & 0xf); |
340 |
|
|
break; |
341 |
|
|
|
342 |
|
|
default:goto bad; |
343 |
|
|
} |
344 |
|
|
|
345 |
|
|
|
346 |
|
|
#define DYNTRANS_TO_BE_TRANSLATED_TAIL |
347 |
|
|
#include "cpu_dyntrans.c" |
348 |
|
|
#undef DYNTRANS_TO_BE_TRANSLATED_TAIL |
349 |
|
|
} |
350 |
|
|
|