hw/arm: Deprecate various old Arm machine types
[qemu/armbru.git] / target / avr / helper.c
blobfdc9884ea0e475ad78f7ba8a61aeab2d60583135
1 /*
2 * QEMU AVR CPU helpers
4 * Copyright (c) 2016-2020 Michael Rolnik
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see
18 * <http://www.gnu.org/licenses/lgpl-2.1.html>
21 #include "qemu/osdep.h"
22 #include "qemu/log.h"
23 #include "qemu/error-report.h"
24 #include "cpu.h"
25 #include "hw/core/tcg-cpu-ops.h"
26 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
28 #include "exec/address-spaces.h"
29 #include "exec/helper-proto.h"
31 bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
33 AVRCPU *cpu = AVR_CPU(cs);
34 CPUAVRState *env = &cpu->env;
37 * We cannot separate a skip from the next instruction,
38 * as the skip would not be preserved across the interrupt.
39 * Separating the two insn normally only happens at page boundaries.
41 if (env->skip) {
42 return false;
45 if (interrupt_request & CPU_INTERRUPT_RESET) {
46 if (cpu_interrupts_enabled(env)) {
47 cs->exception_index = EXCP_RESET;
48 avr_cpu_do_interrupt(cs);
50 cs->interrupt_request &= ~CPU_INTERRUPT_RESET;
51 return true;
54 if (interrupt_request & CPU_INTERRUPT_HARD) {
55 if (cpu_interrupts_enabled(env) && env->intsrc != 0) {
56 int index = ctz64(env->intsrc);
57 cs->exception_index = EXCP_INT(index);
58 avr_cpu_do_interrupt(cs);
60 env->intsrc &= env->intsrc - 1; /* clear the interrupt */
61 if (!env->intsrc) {
62 cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
64 return true;
67 return false;
70 void avr_cpu_do_interrupt(CPUState *cs)
72 AVRCPU *cpu = AVR_CPU(cs);
73 CPUAVRState *env = &cpu->env;
75 uint32_t ret = env->pc_w;
76 int vector = 0;
77 int size = avr_feature(env, AVR_FEATURE_JMP_CALL) ? 2 : 1;
78 int base = 0;
80 if (cs->exception_index == EXCP_RESET) {
81 vector = 0;
82 } else if (env->intsrc != 0) {
83 vector = ctz64(env->intsrc) + 1;
86 if (avr_feature(env, AVR_FEATURE_3_BYTE_PC)) {
87 cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
88 cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
89 cpu_stb_data(env, env->sp--, (ret & 0xff0000) >> 16);
90 } else if (avr_feature(env, AVR_FEATURE_2_BYTE_PC)) {
91 cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
92 cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
93 } else {
94 cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
97 env->pc_w = base + vector * size;
98 env->sregI = 0; /* clear Global Interrupt Flag */
100 cs->exception_index = -1;
103 hwaddr avr_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
105 return addr; /* I assume 1:1 address correspondence */
108 bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
109 MMUAccessType access_type, int mmu_idx,
110 bool probe, uintptr_t retaddr)
112 int prot, page_size = TARGET_PAGE_SIZE;
113 uint32_t paddr;
115 address &= TARGET_PAGE_MASK;
117 if (mmu_idx == MMU_CODE_IDX) {
118 /* Access to code in flash. */
119 paddr = OFFSET_CODE + address;
120 prot = PAGE_READ | PAGE_EXEC;
121 if (paddr >= OFFSET_DATA) {
123 * This should not be possible via any architectural operations.
124 * There is certainly not an exception that we can deliver.
125 * Accept probing that might come from generic code.
127 if (probe) {
128 return false;
130 error_report("execution left flash memory");
131 abort();
133 } else {
134 /* Access to memory. */
135 paddr = OFFSET_DATA + address;
136 prot = PAGE_READ | PAGE_WRITE;
137 if (address < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
139 * Access to CPU registers, exit and rebuilt this TB to use
140 * full access in case it touches specially handled registers
141 * like SREG or SP. For probing, set page_size = 1, in order
142 * to force tlb_fill to be called for the next access.
144 if (probe) {
145 page_size = 1;
146 } else {
147 AVRCPU *cpu = AVR_CPU(cs);
148 CPUAVRState *env = &cpu->env;
149 env->fullacc = 1;
150 cpu_loop_exit_restore(cs, retaddr);
155 tlb_set_page(cs, address, paddr, prot, mmu_idx, page_size);
156 return true;
160 * helpers
163 void helper_sleep(CPUAVRState *env)
165 CPUState *cs = env_cpu(env);
167 cs->exception_index = EXCP_HLT;
168 cpu_loop_exit(cs);
171 void helper_unsupported(CPUAVRState *env)
173 CPUState *cs = env_cpu(env);
176 * I count not find what happens on the real platform, so
177 * it's EXCP_DEBUG for meanwhile
179 cs->exception_index = EXCP_DEBUG;
180 if (qemu_loglevel_mask(LOG_UNIMP)) {
181 qemu_log("UNSUPPORTED\n");
182 cpu_dump_state(cs, stderr, 0);
184 cpu_loop_exit(cs);
187 void helper_debug(CPUAVRState *env)
189 CPUState *cs = env_cpu(env);
191 cs->exception_index = EXCP_DEBUG;
192 cpu_loop_exit(cs);
195 void helper_break(CPUAVRState *env)
197 CPUState *cs = env_cpu(env);
199 cs->exception_index = EXCP_DEBUG;
200 cpu_loop_exit(cs);
203 void helper_wdr(CPUAVRState *env)
205 qemu_log_mask(LOG_UNIMP, "WDG reset (not implemented)\n");
209 * This function implements IN instruction
211 * It does the following
212 * a. if an IO register belongs to CPU, its value is read and returned
213 * b. otherwise io address is translated to mem address and physical memory
214 * is read.
215 * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
218 target_ulong helper_inb(CPUAVRState *env, uint32_t port)
220 target_ulong data = 0;
222 switch (port) {
223 case 0x38: /* RAMPD */
224 data = 0xff & (env->rampD >> 16);
225 break;
226 case 0x39: /* RAMPX */
227 data = 0xff & (env->rampX >> 16);
228 break;
229 case 0x3a: /* RAMPY */
230 data = 0xff & (env->rampY >> 16);
231 break;
232 case 0x3b: /* RAMPZ */
233 data = 0xff & (env->rampZ >> 16);
234 break;
235 case 0x3c: /* EIND */
236 data = 0xff & (env->eind >> 16);
237 break;
238 case 0x3d: /* SPL */
239 data = env->sp & 0x00ff;
240 break;
241 case 0x3e: /* SPH */
242 data = env->sp >> 8;
243 break;
244 case 0x3f: /* SREG */
245 data = cpu_get_sreg(env);
246 break;
247 default:
248 /* not a special register, pass to normal memory access */
249 data = address_space_ldub(&address_space_memory,
250 OFFSET_IO_REGISTERS + port,
251 MEMTXATTRS_UNSPECIFIED, NULL);
254 return data;
258 * This function implements OUT instruction
260 * It does the following
261 * a. if an IO register belongs to CPU, its value is written into the register
262 * b. otherwise io address is translated to mem address and physical memory
263 * is written.
264 * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
267 void helper_outb(CPUAVRState *env, uint32_t port, uint32_t data)
269 data &= 0x000000ff;
271 switch (port) {
272 case 0x38: /* RAMPD */
273 if (avr_feature(env, AVR_FEATURE_RAMPD)) {
274 env->rampD = (data & 0xff) << 16;
276 break;
277 case 0x39: /* RAMPX */
278 if (avr_feature(env, AVR_FEATURE_RAMPX)) {
279 env->rampX = (data & 0xff) << 16;
281 break;
282 case 0x3a: /* RAMPY */
283 if (avr_feature(env, AVR_FEATURE_RAMPY)) {
284 env->rampY = (data & 0xff) << 16;
286 break;
287 case 0x3b: /* RAMPZ */
288 if (avr_feature(env, AVR_FEATURE_RAMPZ)) {
289 env->rampZ = (data & 0xff) << 16;
291 break;
292 case 0x3c: /* EIDN */
293 env->eind = (data & 0xff) << 16;
294 break;
295 case 0x3d: /* SPL */
296 env->sp = (env->sp & 0xff00) | (data);
297 break;
298 case 0x3e: /* SPH */
299 if (avr_feature(env, AVR_FEATURE_2_BYTE_SP)) {
300 env->sp = (env->sp & 0x00ff) | (data << 8);
302 break;
303 case 0x3f: /* SREG */
304 cpu_set_sreg(env, data);
305 break;
306 default:
307 /* not a special register, pass to normal memory access */
308 address_space_stb(&address_space_memory, OFFSET_IO_REGISTERS + port,
309 data, MEMTXATTRS_UNSPECIFIED, NULL);
314 * this function implements LD instruction when there is a possibility to read
315 * from a CPU register
317 target_ulong helper_fullrd(CPUAVRState *env, uint32_t addr)
319 uint8_t data;
321 env->fullacc = false;
323 if (addr < NUMBER_OF_CPU_REGISTERS) {
324 /* CPU registers */
325 data = env->r[addr];
326 } else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
327 /* IO registers */
328 data = helper_inb(env, addr - NUMBER_OF_CPU_REGISTERS);
329 } else {
330 /* memory */
331 data = address_space_ldub(&address_space_memory, OFFSET_DATA + addr,
332 MEMTXATTRS_UNSPECIFIED, NULL);
334 return data;
338 * this function implements ST instruction when there is a possibility to write
339 * into a CPU register
341 void helper_fullwr(CPUAVRState *env, uint32_t data, uint32_t addr)
343 env->fullacc = false;
345 /* Following logic assumes this: */
346 assert(OFFSET_CPU_REGISTERS == OFFSET_DATA);
347 assert(OFFSET_IO_REGISTERS == OFFSET_CPU_REGISTERS +
348 NUMBER_OF_CPU_REGISTERS);
350 if (addr < NUMBER_OF_CPU_REGISTERS) {
351 /* CPU registers */
352 env->r[addr] = data;
353 } else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
354 /* IO registers */
355 helper_outb(env, addr - NUMBER_OF_CPU_REGISTERS, data);
356 } else {
357 /* memory */
358 address_space_stb(&address_space_memory, OFFSET_DATA + addr, data,
359 MEMTXATTRS_UNSPECIFIED, NULL);