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/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.
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 AVRCPU
*cpu
= AVR_CPU(cs
);
73 CPUAVRState
*env
= &cpu
->env
;
75 uint32_t ret
= env
->pc_w
;
77 int size
= avr_feature(env
, AVR_FEATURE_JMP_CALL
) ? 2 : 1;
80 if (cs
->exception_index
== EXCP_RESET
) {
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);
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
;
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.
130 error_report("execution left flash memory");
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.
147 AVRCPU
*cpu
= AVR_CPU(cs
);
148 CPUAVRState
*env
= &cpu
->env
;
150 cpu_loop_exit_restore(cs
, retaddr
);
155 tlb_set_page(cs
, address
, paddr
, prot
, mmu_idx
, page_size
);
163 void helper_sleep(CPUAVRState
*env
)
165 CPUState
*cs
= env_cpu(env
);
167 cs
->exception_index
= EXCP_HLT
;
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);
187 void helper_debug(CPUAVRState
*env
)
189 CPUState
*cs
= env_cpu(env
);
191 cs
->exception_index
= EXCP_DEBUG
;
195 void helper_break(CPUAVRState
*env
)
197 CPUState
*cs
= env_cpu(env
);
199 cs
->exception_index
= EXCP_DEBUG
;
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
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;
223 case 0x38: /* RAMPD */
224 data
= 0xff & (env
->rampD
>> 16);
226 case 0x39: /* RAMPX */
227 data
= 0xff & (env
->rampX
>> 16);
229 case 0x3a: /* RAMPY */
230 data
= 0xff & (env
->rampY
>> 16);
232 case 0x3b: /* RAMPZ */
233 data
= 0xff & (env
->rampZ
>> 16);
235 case 0x3c: /* EIND */
236 data
= 0xff & (env
->eind
>> 16);
239 data
= env
->sp
& 0x00ff;
244 case 0x3f: /* SREG */
245 data
= cpu_get_sreg(env
);
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
);
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
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
)
272 case 0x38: /* RAMPD */
273 if (avr_feature(env
, AVR_FEATURE_RAMPD
)) {
274 env
->rampD
= (data
& 0xff) << 16;
277 case 0x39: /* RAMPX */
278 if (avr_feature(env
, AVR_FEATURE_RAMPX
)) {
279 env
->rampX
= (data
& 0xff) << 16;
282 case 0x3a: /* RAMPY */
283 if (avr_feature(env
, AVR_FEATURE_RAMPY
)) {
284 env
->rampY
= (data
& 0xff) << 16;
287 case 0x3b: /* RAMPZ */
288 if (avr_feature(env
, AVR_FEATURE_RAMPZ
)) {
289 env
->rampZ
= (data
& 0xff) << 16;
292 case 0x3c: /* EIDN */
293 env
->eind
= (data
& 0xff) << 16;
296 env
->sp
= (env
->sp
& 0xff00) | (data
);
299 if (avr_feature(env
, AVR_FEATURE_2_BYTE_SP
)) {
300 env
->sp
= (env
->sp
& 0x00ff) | (data
<< 8);
303 case 0x3f: /* SREG */
304 cpu_set_sreg(env
, data
);
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
)
321 env
->fullacc
= false;
323 if (addr
< NUMBER_OF_CPU_REGISTERS
) {
326 } else if (addr
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
328 data
= helper_inb(env
, addr
- NUMBER_OF_CPU_REGISTERS
);
331 data
= address_space_ldub(&address_space_memory
, OFFSET_DATA
+ addr
,
332 MEMTXATTRS_UNSPECIFIED
, NULL
);
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
) {
353 } else if (addr
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
355 helper_outb(env
, addr
- NUMBER_OF_CPU_REGISTERS
, data
);
358 address_space_stb(&address_space_memory
, OFFSET_DATA
+ addr
, data
,
359 MEMTXATTRS_UNSPECIFIED
, NULL
);