target/ppc: Use MMUAccessType in mmu-hash32.c
[qemu/ar7.git] / target / avr / helper.c
blob981c29da45358f4ca0211e4940a1967e2ab53c68
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 "cpu.h"
23 #include "hw/core/tcg-cpu-ops.h"
24 #include "exec/exec-all.h"
25 #include "exec/address-spaces.h"
26 #include "exec/helper-proto.h"
28 bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
30 bool ret = false;
31 CPUClass *cc = CPU_GET_CLASS(cs);
32 AVRCPU *cpu = AVR_CPU(cs);
33 CPUAVRState *env = &cpu->env;
35 if (interrupt_request & CPU_INTERRUPT_RESET) {
36 if (cpu_interrupts_enabled(env)) {
37 cs->exception_index = EXCP_RESET;
38 cc->tcg_ops->do_interrupt(cs);
40 cs->interrupt_request &= ~CPU_INTERRUPT_RESET;
42 ret = true;
45 if (interrupt_request & CPU_INTERRUPT_HARD) {
46 if (cpu_interrupts_enabled(env) && env->intsrc != 0) {
47 int index = ctz32(env->intsrc);
48 cs->exception_index = EXCP_INT(index);
49 cc->tcg_ops->do_interrupt(cs);
51 env->intsrc &= env->intsrc - 1; /* clear the interrupt */
52 if (!env->intsrc) {
53 cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
56 ret = true;
59 return ret;
62 void avr_cpu_do_interrupt(CPUState *cs)
64 AVRCPU *cpu = AVR_CPU(cs);
65 CPUAVRState *env = &cpu->env;
67 uint32_t ret = env->pc_w;
68 int vector = 0;
69 int size = avr_feature(env, AVR_FEATURE_JMP_CALL) ? 2 : 1;
70 int base = 0;
72 if (cs->exception_index == EXCP_RESET) {
73 vector = 0;
74 } else if (env->intsrc != 0) {
75 vector = ctz32(env->intsrc) + 1;
78 if (avr_feature(env, AVR_FEATURE_3_BYTE_PC)) {
79 cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
80 cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
81 cpu_stb_data(env, env->sp--, (ret & 0xff0000) >> 16);
82 } else if (avr_feature(env, AVR_FEATURE_2_BYTE_PC)) {
83 cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
84 cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
85 } else {
86 cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
89 env->pc_w = base + vector * size;
90 env->sregI = 0; /* clear Global Interrupt Flag */
92 cs->exception_index = -1;
95 int avr_cpu_memory_rw_debug(CPUState *cs, vaddr addr, uint8_t *buf,
96 int len, bool is_write)
98 return cpu_memory_rw_debug(cs, addr, buf, len, is_write);
101 hwaddr avr_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
103 return addr; /* I assume 1:1 address correspondence */
106 bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
107 MMUAccessType access_type, int mmu_idx,
108 bool probe, uintptr_t retaddr)
110 int prot = 0;
111 MemTxAttrs attrs = {};
112 uint32_t paddr;
114 address &= TARGET_PAGE_MASK;
116 if (mmu_idx == MMU_CODE_IDX) {
117 /* access to code in flash */
118 paddr = OFFSET_CODE + address;
119 prot = PAGE_READ | PAGE_EXEC;
120 if (paddr + TARGET_PAGE_SIZE > OFFSET_DATA) {
121 error_report("execution left flash memory");
122 abort();
124 } else if (address < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
126 * access to CPU registers, exit and rebuilt this TB to use full access
127 * incase it touches specially handled registers like SREG or SP
129 AVRCPU *cpu = AVR_CPU(cs);
130 CPUAVRState *env = &cpu->env;
131 env->fullacc = 1;
132 cpu_loop_exit_restore(cs, retaddr);
133 } else {
134 /* access to memory. nothing special */
135 paddr = OFFSET_DATA + address;
136 prot = PAGE_READ | PAGE_WRITE;
139 tlb_set_page_with_attrs(cs, address, paddr, attrs, prot,
140 mmu_idx, TARGET_PAGE_SIZE);
142 return true;
146 * helpers
149 void helper_sleep(CPUAVRState *env)
151 CPUState *cs = env_cpu(env);
153 cs->exception_index = EXCP_HLT;
154 cpu_loop_exit(cs);
157 void helper_unsupported(CPUAVRState *env)
159 CPUState *cs = env_cpu(env);
162 * I count not find what happens on the real platform, so
163 * it's EXCP_DEBUG for meanwhile
165 cs->exception_index = EXCP_DEBUG;
166 if (qemu_loglevel_mask(LOG_UNIMP)) {
167 qemu_log("UNSUPPORTED\n");
168 cpu_dump_state(cs, stderr, 0);
170 cpu_loop_exit(cs);
173 void helper_debug(CPUAVRState *env)
175 CPUState *cs = env_cpu(env);
177 cs->exception_index = EXCP_DEBUG;
178 cpu_loop_exit(cs);
181 void helper_break(CPUAVRState *env)
183 CPUState *cs = env_cpu(env);
185 cs->exception_index = EXCP_DEBUG;
186 cpu_loop_exit(cs);
189 void helper_wdr(CPUAVRState *env)
191 qemu_log_mask(LOG_UNIMP, "WDG reset (not implemented)\n");
195 * This function implements IN instruction
197 * It does the following
198 * a. if an IO register belongs to CPU, its value is read and returned
199 * b. otherwise io address is translated to mem address and physical memory
200 * is read.
201 * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
204 target_ulong helper_inb(CPUAVRState *env, uint32_t port)
206 target_ulong data = 0;
208 switch (port) {
209 case 0x38: /* RAMPD */
210 data = 0xff & (env->rampD >> 16);
211 break;
212 case 0x39: /* RAMPX */
213 data = 0xff & (env->rampX >> 16);
214 break;
215 case 0x3a: /* RAMPY */
216 data = 0xff & (env->rampY >> 16);
217 break;
218 case 0x3b: /* RAMPZ */
219 data = 0xff & (env->rampZ >> 16);
220 break;
221 case 0x3c: /* EIND */
222 data = 0xff & (env->eind >> 16);
223 break;
224 case 0x3d: /* SPL */
225 data = env->sp & 0x00ff;
226 break;
227 case 0x3e: /* SPH */
228 data = env->sp >> 8;
229 break;
230 case 0x3f: /* SREG */
231 data = cpu_get_sreg(env);
232 break;
233 default:
234 /* not a special register, pass to normal memory access */
235 data = address_space_ldub(&address_space_memory,
236 OFFSET_IO_REGISTERS + port,
237 MEMTXATTRS_UNSPECIFIED, NULL);
240 return data;
244 * This function implements OUT instruction
246 * It does the following
247 * a. if an IO register belongs to CPU, its value is written into the register
248 * b. otherwise io address is translated to mem address and physical memory
249 * is written.
250 * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
253 void helper_outb(CPUAVRState *env, uint32_t port, uint32_t data)
255 data &= 0x000000ff;
257 switch (port) {
258 case 0x38: /* RAMPD */
259 if (avr_feature(env, AVR_FEATURE_RAMPD)) {
260 env->rampD = (data & 0xff) << 16;
262 break;
263 case 0x39: /* RAMPX */
264 if (avr_feature(env, AVR_FEATURE_RAMPX)) {
265 env->rampX = (data & 0xff) << 16;
267 break;
268 case 0x3a: /* RAMPY */
269 if (avr_feature(env, AVR_FEATURE_RAMPY)) {
270 env->rampY = (data & 0xff) << 16;
272 break;
273 case 0x3b: /* RAMPZ */
274 if (avr_feature(env, AVR_FEATURE_RAMPZ)) {
275 env->rampZ = (data & 0xff) << 16;
277 break;
278 case 0x3c: /* EIDN */
279 env->eind = (data & 0xff) << 16;
280 break;
281 case 0x3d: /* SPL */
282 env->sp = (env->sp & 0xff00) | (data);
283 break;
284 case 0x3e: /* SPH */
285 if (avr_feature(env, AVR_FEATURE_2_BYTE_SP)) {
286 env->sp = (env->sp & 0x00ff) | (data << 8);
288 break;
289 case 0x3f: /* SREG */
290 cpu_set_sreg(env, data);
291 break;
292 default:
293 /* not a special register, pass to normal memory access */
294 address_space_stb(&address_space_memory, OFFSET_IO_REGISTERS + port,
295 data, MEMTXATTRS_UNSPECIFIED, NULL);
300 * this function implements LD instruction when there is a possibility to read
301 * from a CPU register
303 target_ulong helper_fullrd(CPUAVRState *env, uint32_t addr)
305 uint8_t data;
307 env->fullacc = false;
309 if (addr < NUMBER_OF_CPU_REGISTERS) {
310 /* CPU registers */
311 data = env->r[addr];
312 } else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
313 /* IO registers */
314 data = helper_inb(env, addr - NUMBER_OF_CPU_REGISTERS);
315 } else {
316 /* memory */
317 data = address_space_ldub(&address_space_memory, OFFSET_DATA + addr,
318 MEMTXATTRS_UNSPECIFIED, NULL);
320 return data;
324 * this function implements ST instruction when there is a possibility to write
325 * into a CPU register
327 void helper_fullwr(CPUAVRState *env, uint32_t data, uint32_t addr)
329 env->fullacc = false;
331 /* Following logic assumes this: */
332 assert(OFFSET_CPU_REGISTERS == OFFSET_DATA);
333 assert(OFFSET_IO_REGISTERS == OFFSET_CPU_REGISTERS +
334 NUMBER_OF_CPU_REGISTERS);
336 if (addr < NUMBER_OF_CPU_REGISTERS) {
337 /* CPU registers */
338 env->r[addr] = data;
339 } else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
340 /* IO registers */
341 helper_outb(env, addr - NUMBER_OF_CPU_REGISTERS, data);
342 } else {
343 /* memory */
344 address_space_stb(&address_space_memory, OFFSET_DATA + addr, data,
345 MEMTXATTRS_UNSPECIFIED, NULL);