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"
23 #include "qemu/error-report.h"
25 #include "hw/core/tcg-cpu-ops.h"
26 #include "exec/exec-all.h"
27 #include "exec/page-protection.h"
28 #include "exec/cpu_ldst.h"
29 #include "exec/address-spaces.h"
30 #include "exec/helper-proto.h"
32 bool avr_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
34 CPUAVRState
*env
= cpu_env(cs
);
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.
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
;
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 */
62 cs
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
70 void avr_cpu_do_interrupt(CPUState
*cs
)
72 CPUAVRState
*env
= cpu_env(cs
);
74 uint32_t ret
= env
->pc_w
;
76 int size
= avr_feature(env
, AVR_FEATURE_JMP_CALL
) ? 2 : 1;
79 if (cs
->exception_index
== EXCP_RESET
) {
81 } else if (env
->intsrc
!= 0) {
82 vector
= ctz64(env
->intsrc
) + 1;
85 if (avr_feature(env
, AVR_FEATURE_3_BYTE_PC
)) {
86 cpu_stb_data(env
, env
->sp
--, (ret
& 0x0000ff));
87 cpu_stb_data(env
, env
->sp
--, (ret
& 0x00ff00) >> 8);
88 cpu_stb_data(env
, env
->sp
--, (ret
& 0xff0000) >> 16);
89 } else if (avr_feature(env
, AVR_FEATURE_2_BYTE_PC
)) {
90 cpu_stb_data(env
, env
->sp
--, (ret
& 0x0000ff));
91 cpu_stb_data(env
, env
->sp
--, (ret
& 0x00ff00) >> 8);
93 cpu_stb_data(env
, env
->sp
--, (ret
& 0x0000ff));
96 env
->pc_w
= base
+ vector
* size
;
97 env
->sregI
= 0; /* clear Global Interrupt Flag */
99 cs
->exception_index
= -1;
102 hwaddr
avr_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
104 return addr
; /* I assume 1:1 address correspondence */
107 bool avr_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
108 MMUAccessType access_type
, int mmu_idx
,
109 bool probe
, uintptr_t retaddr
)
111 int prot
, page_size
= TARGET_PAGE_SIZE
;
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
>= OFFSET_DATA
) {
122 * This should not be possible via any architectural operations.
123 * There is certainly not an exception that we can deliver.
124 * Accept probing that might come from generic code.
129 error_report("execution left flash memory");
133 /* Access to memory. */
134 paddr
= OFFSET_DATA
+ address
;
135 prot
= PAGE_READ
| PAGE_WRITE
;
136 if (address
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
138 * Access to CPU registers, exit and rebuilt this TB to use
139 * full access in case it touches specially handled registers
140 * like SREG or SP. For probing, set page_size = 1, in order
141 * to force tlb_fill to be called for the next access.
146 cpu_env(cs
)->fullacc
= 1;
147 cpu_loop_exit_restore(cs
, retaddr
);
152 tlb_set_page(cs
, address
, paddr
, prot
, mmu_idx
, page_size
);
160 void helper_sleep(CPUAVRState
*env
)
162 CPUState
*cs
= env_cpu(env
);
164 cs
->exception_index
= EXCP_HLT
;
168 void helper_unsupported(CPUAVRState
*env
)
170 CPUState
*cs
= env_cpu(env
);
173 * I count not find what happens on the real platform, so
174 * it's EXCP_DEBUG for meanwhile
176 cs
->exception_index
= EXCP_DEBUG
;
177 if (qemu_loglevel_mask(LOG_UNIMP
)) {
178 qemu_log("UNSUPPORTED\n");
179 cpu_dump_state(cs
, stderr
, 0);
184 void helper_debug(CPUAVRState
*env
)
186 CPUState
*cs
= env_cpu(env
);
188 cs
->exception_index
= EXCP_DEBUG
;
192 void helper_break(CPUAVRState
*env
)
194 CPUState
*cs
= env_cpu(env
);
196 cs
->exception_index
= EXCP_DEBUG
;
200 void helper_wdr(CPUAVRState
*env
)
202 qemu_log_mask(LOG_UNIMP
, "WDG reset (not implemented)\n");
206 * This function implements IN instruction
208 * It does the following
209 * a. if an IO register belongs to CPU, its value is read and returned
210 * b. otherwise io address is translated to mem address and physical memory
212 * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
215 target_ulong
helper_inb(CPUAVRState
*env
, uint32_t port
)
217 target_ulong data
= 0;
220 case 0x38: /* RAMPD */
221 data
= 0xff & (env
->rampD
>> 16);
223 case 0x39: /* RAMPX */
224 data
= 0xff & (env
->rampX
>> 16);
226 case 0x3a: /* RAMPY */
227 data
= 0xff & (env
->rampY
>> 16);
229 case 0x3b: /* RAMPZ */
230 data
= 0xff & (env
->rampZ
>> 16);
232 case 0x3c: /* EIND */
233 data
= 0xff & (env
->eind
>> 16);
236 data
= env
->sp
& 0x00ff;
241 case 0x3f: /* SREG */
242 data
= cpu_get_sreg(env
);
245 /* not a special register, pass to normal memory access */
246 data
= address_space_ldub(&address_space_memory
,
247 OFFSET_IO_REGISTERS
+ port
,
248 MEMTXATTRS_UNSPECIFIED
, NULL
);
255 * This function implements OUT instruction
257 * It does the following
258 * a. if an IO register belongs to CPU, its value is written into the register
259 * b. otherwise io address is translated to mem address and physical memory
261 * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
264 void helper_outb(CPUAVRState
*env
, uint32_t port
, uint32_t data
)
269 case 0x38: /* RAMPD */
270 if (avr_feature(env
, AVR_FEATURE_RAMPD
)) {
271 env
->rampD
= (data
& 0xff) << 16;
274 case 0x39: /* RAMPX */
275 if (avr_feature(env
, AVR_FEATURE_RAMPX
)) {
276 env
->rampX
= (data
& 0xff) << 16;
279 case 0x3a: /* RAMPY */
280 if (avr_feature(env
, AVR_FEATURE_RAMPY
)) {
281 env
->rampY
= (data
& 0xff) << 16;
284 case 0x3b: /* RAMPZ */
285 if (avr_feature(env
, AVR_FEATURE_RAMPZ
)) {
286 env
->rampZ
= (data
& 0xff) << 16;
289 case 0x3c: /* EIDN */
290 env
->eind
= (data
& 0xff) << 16;
293 env
->sp
= (env
->sp
& 0xff00) | (data
);
296 if (avr_feature(env
, AVR_FEATURE_2_BYTE_SP
)) {
297 env
->sp
= (env
->sp
& 0x00ff) | (data
<< 8);
300 case 0x3f: /* SREG */
301 cpu_set_sreg(env
, data
);
304 /* not a special register, pass to normal memory access */
305 address_space_stb(&address_space_memory
, OFFSET_IO_REGISTERS
+ port
,
306 data
, MEMTXATTRS_UNSPECIFIED
, NULL
);
311 * this function implements LD instruction when there is a possibility to read
312 * from a CPU register
314 target_ulong
helper_fullrd(CPUAVRState
*env
, uint32_t addr
)
318 env
->fullacc
= false;
320 if (addr
< NUMBER_OF_CPU_REGISTERS
) {
323 } else if (addr
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
325 data
= helper_inb(env
, addr
- NUMBER_OF_CPU_REGISTERS
);
328 data
= address_space_ldub(&address_space_memory
, OFFSET_DATA
+ addr
,
329 MEMTXATTRS_UNSPECIFIED
, NULL
);
335 * this function implements ST instruction when there is a possibility to write
336 * into a CPU register
338 void helper_fullwr(CPUAVRState
*env
, uint32_t data
, uint32_t addr
)
340 env
->fullacc
= false;
342 /* Following logic assumes this: */
343 assert(OFFSET_CPU_REGISTERS
== OFFSET_DATA
);
344 assert(OFFSET_IO_REGISTERS
== OFFSET_CPU_REGISTERS
+
345 NUMBER_OF_CPU_REGISTERS
);
347 if (addr
< NUMBER_OF_CPU_REGISTERS
) {
350 } else if (addr
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
352 helper_outb(env
, addr
- NUMBER_OF_CPU_REGISTERS
, data
);
355 address_space_stb(&address_space_memory
, OFFSET_DATA
+ addr
, data
,
356 MEMTXATTRS_UNSPECIFIED
, NULL
);