1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "qemu/log.h"
23#include "cpu.h"
24#include "hw/core/tcg-cpu-ops.h"
25#include "exec/exec-all.h"
26#include "exec/address-spaces.h"
27#include "exec/helper-proto.h"
28
29bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
30{
31 AVRCPU *cpu = AVR_CPU(cs);
32 CPUAVRState *env = &cpu->env;
33
34
35
36
37
38
39 if (env->skip) {
40 return false;
41 }
42
43 if (interrupt_request & CPU_INTERRUPT_RESET) {
44 if (cpu_interrupts_enabled(env)) {
45 cs->exception_index = EXCP_RESET;
46 avr_cpu_do_interrupt(cs);
47
48 cs->interrupt_request &= ~CPU_INTERRUPT_RESET;
49 return true;
50 }
51 }
52 if (interrupt_request & CPU_INTERRUPT_HARD) {
53 if (cpu_interrupts_enabled(env) && env->intsrc != 0) {
54 int index = ctz32(env->intsrc);
55 cs->exception_index = EXCP_INT(index);
56 avr_cpu_do_interrupt(cs);
57
58 env->intsrc &= env->intsrc - 1;
59 if (!env->intsrc) {
60 cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
61 }
62 return true;
63 }
64 }
65 return false;
66}
67
68void avr_cpu_do_interrupt(CPUState *cs)
69{
70 AVRCPU *cpu = AVR_CPU(cs);
71 CPUAVRState *env = &cpu->env;
72
73 uint32_t ret = env->pc_w;
74 int vector = 0;
75 int size = avr_feature(env, AVR_FEATURE_JMP_CALL) ? 2 : 1;
76 int base = 0;
77
78 if (cs->exception_index == EXCP_RESET) {
79 vector = 0;
80 } else if (env->intsrc != 0) {
81 vector = ctz32(env->intsrc) + 1;
82 }
83
84 if (avr_feature(env, AVR_FEATURE_3_BYTE_PC)) {
85 cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
86 cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
87 cpu_stb_data(env, env->sp--, (ret & 0xff0000) >> 16);
88 } else if (avr_feature(env, AVR_FEATURE_2_BYTE_PC)) {
89 cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
90 cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
91 } else {
92 cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
93 }
94
95 env->pc_w = base + vector * size;
96 env->sregI = 0;
97
98 cs->exception_index = -1;
99}
100
101hwaddr avr_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
102{
103 return addr;
104}
105
106bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
107 MMUAccessType access_type, int mmu_idx,
108 bool probe, uintptr_t retaddr)
109{
110 int prot, page_size = TARGET_PAGE_SIZE;
111 uint32_t paddr;
112
113 address &= TARGET_PAGE_MASK;
114
115 if (mmu_idx == MMU_CODE_IDX) {
116
117 paddr = OFFSET_CODE + address;
118 prot = PAGE_READ | PAGE_EXEC;
119 if (paddr >= OFFSET_DATA) {
120
121
122
123
124
125 if (probe) {
126 return false;
127 }
128 error_report("execution left flash memory");
129 abort();
130 }
131 } else {
132
133 paddr = OFFSET_DATA + address;
134 prot = PAGE_READ | PAGE_WRITE;
135 if (address < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
136
137
138
139
140
141
142 if (probe) {
143 page_size = 1;
144 } else {
145 AVRCPU *cpu = AVR_CPU(cs);
146 CPUAVRState *env = &cpu->env;
147 env->fullacc = 1;
148 cpu_loop_exit_restore(cs, retaddr);
149 }
150 }
151 }
152
153 tlb_set_page(cs, address, paddr, prot, mmu_idx, page_size);
154 return true;
155}
156
157
158
159
160
161void helper_sleep(CPUAVRState *env)
162{
163 CPUState *cs = env_cpu(env);
164
165 cs->exception_index = EXCP_HLT;
166 cpu_loop_exit(cs);
167}
168
169void helper_unsupported(CPUAVRState *env)
170{
171 CPUState *cs = env_cpu(env);
172
173
174
175
176
177 cs->exception_index = EXCP_DEBUG;
178 if (qemu_loglevel_mask(LOG_UNIMP)) {
179 qemu_log("UNSUPPORTED\n");
180 cpu_dump_state(cs, stderr, 0);
181 }
182 cpu_loop_exit(cs);
183}
184
185void helper_debug(CPUAVRState *env)
186{
187 CPUState *cs = env_cpu(env);
188
189 cs->exception_index = EXCP_DEBUG;
190 cpu_loop_exit(cs);
191}
192
193void helper_break(CPUAVRState *env)
194{
195 CPUState *cs = env_cpu(env);
196
197 cs->exception_index = EXCP_DEBUG;
198 cpu_loop_exit(cs);
199}
200
201void helper_wdr(CPUAVRState *env)
202{
203 qemu_log_mask(LOG_UNIMP, "WDG reset (not implemented)\n");
204}
205
206
207
208
209
210
211
212
213
214
215
216target_ulong helper_inb(CPUAVRState *env, uint32_t port)
217{
218 target_ulong data = 0;
219
220 switch (port) {
221 case 0x38:
222 data = 0xff & (env->rampD >> 16);
223 break;
224 case 0x39:
225 data = 0xff & (env->rampX >> 16);
226 break;
227 case 0x3a:
228 data = 0xff & (env->rampY >> 16);
229 break;
230 case 0x3b:
231 data = 0xff & (env->rampZ >> 16);
232 break;
233 case 0x3c:
234 data = 0xff & (env->eind >> 16);
235 break;
236 case 0x3d:
237 data = env->sp & 0x00ff;
238 break;
239 case 0x3e:
240 data = env->sp >> 8;
241 break;
242 case 0x3f:
243 data = cpu_get_sreg(env);
244 break;
245 default:
246
247 data = address_space_ldub(&address_space_memory,
248 OFFSET_IO_REGISTERS + port,
249 MEMTXATTRS_UNSPECIFIED, NULL);
250 }
251
252 return data;
253}
254
255
256
257
258
259
260
261
262
263
264
265void helper_outb(CPUAVRState *env, uint32_t port, uint32_t data)
266{
267 data &= 0x000000ff;
268
269 switch (port) {
270 case 0x38:
271 if (avr_feature(env, AVR_FEATURE_RAMPD)) {
272 env->rampD = (data & 0xff) << 16;
273 }
274 break;
275 case 0x39:
276 if (avr_feature(env, AVR_FEATURE_RAMPX)) {
277 env->rampX = (data & 0xff) << 16;
278 }
279 break;
280 case 0x3a:
281 if (avr_feature(env, AVR_FEATURE_RAMPY)) {
282 env->rampY = (data & 0xff) << 16;
283 }
284 break;
285 case 0x3b:
286 if (avr_feature(env, AVR_FEATURE_RAMPZ)) {
287 env->rampZ = (data & 0xff) << 16;
288 }
289 break;
290 case 0x3c:
291 env->eind = (data & 0xff) << 16;
292 break;
293 case 0x3d:
294 env->sp = (env->sp & 0xff00) | (data);
295 break;
296 case 0x3e:
297 if (avr_feature(env, AVR_FEATURE_2_BYTE_SP)) {
298 env->sp = (env->sp & 0x00ff) | (data << 8);
299 }
300 break;
301 case 0x3f:
302 cpu_set_sreg(env, data);
303 break;
304 default:
305
306 address_space_stb(&address_space_memory, OFFSET_IO_REGISTERS + port,
307 data, MEMTXATTRS_UNSPECIFIED, NULL);
308 }
309}
310
311
312
313
314
315target_ulong helper_fullrd(CPUAVRState *env, uint32_t addr)
316{
317 uint8_t data;
318
319 env->fullacc = false;
320
321 if (addr < NUMBER_OF_CPU_REGISTERS) {
322
323 data = env->r[addr];
324 } else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
325
326 data = helper_inb(env, addr - NUMBER_OF_CPU_REGISTERS);
327 } else {
328
329 data = address_space_ldub(&address_space_memory, OFFSET_DATA + addr,
330 MEMTXATTRS_UNSPECIFIED, NULL);
331 }
332 return data;
333}
334
335
336
337
338
339void helper_fullwr(CPUAVRState *env, uint32_t data, uint32_t addr)
340{
341 env->fullacc = false;
342
343
344 assert(OFFSET_CPU_REGISTERS == OFFSET_DATA);
345 assert(OFFSET_IO_REGISTERS == OFFSET_CPU_REGISTERS +
346 NUMBER_OF_CPU_REGISTERS);
347
348 if (addr < NUMBER_OF_CPU_REGISTERS) {
349
350 env->r[addr] = data;
351 } else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
352
353 helper_outb(env, addr - NUMBER_OF_CPU_REGISTERS, data);
354 } else {
355
356 address_space_stb(&address_space_memory, OFFSET_DATA + addr, data,
357 MEMTXATTRS_UNSPECIFIED, NULL);
358 }
359}
360