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"
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"
29 bool avr_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
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 avr_cpu_do_interrupt(cs
);
40 cs
->interrupt_request
&= ~CPU_INTERRUPT_RESET
;
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 avr_cpu_do_interrupt(cs
);
51 env
->intsrc
&= env
->intsrc
- 1; /* clear the interrupt */
53 cs
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
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
;
69 int size
= avr_feature(env
, AVR_FEATURE_JMP_CALL
) ? 2 : 1;
72 if (cs
->exception_index
== EXCP_RESET
) {
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);
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 hwaddr
avr_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
97 return addr
; /* I assume 1:1 address correspondence */
100 bool avr_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
101 MMUAccessType access_type
, int mmu_idx
,
102 bool probe
, uintptr_t retaddr
)
104 int prot
, page_size
= TARGET_PAGE_SIZE
;
107 address
&= TARGET_PAGE_MASK
;
109 if (mmu_idx
== MMU_CODE_IDX
) {
110 /* Access to code in flash. */
111 paddr
= OFFSET_CODE
+ address
;
112 prot
= PAGE_READ
| PAGE_EXEC
;
113 if (paddr
>= OFFSET_DATA
) {
115 * This should not be possible via any architectural operations.
116 * There is certainly not an exception that we can deliver.
117 * Accept probing that might come from generic code.
122 error_report("execution left flash memory");
126 /* Access to memory. */
127 paddr
= OFFSET_DATA
+ address
;
128 prot
= PAGE_READ
| PAGE_WRITE
;
129 if (address
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
131 * Access to CPU registers, exit and rebuilt this TB to use
132 * full access in case it touches specially handled registers
133 * like SREG or SP. For probing, set page_size = 1, in order
134 * to force tlb_fill to be called for the next access.
139 AVRCPU
*cpu
= AVR_CPU(cs
);
140 CPUAVRState
*env
= &cpu
->env
;
142 cpu_loop_exit_restore(cs
, retaddr
);
147 tlb_set_page(cs
, address
, paddr
, prot
, mmu_idx
, page_size
);
155 void helper_sleep(CPUAVRState
*env
)
157 CPUState
*cs
= env_cpu(env
);
159 cs
->exception_index
= EXCP_HLT
;
163 void helper_unsupported(CPUAVRState
*env
)
165 CPUState
*cs
= env_cpu(env
);
168 * I count not find what happens on the real platform, so
169 * it's EXCP_DEBUG for meanwhile
171 cs
->exception_index
= EXCP_DEBUG
;
172 if (qemu_loglevel_mask(LOG_UNIMP
)) {
173 qemu_log("UNSUPPORTED\n");
174 cpu_dump_state(cs
, stderr
, 0);
179 void helper_debug(CPUAVRState
*env
)
181 CPUState
*cs
= env_cpu(env
);
183 cs
->exception_index
= EXCP_DEBUG
;
187 void helper_break(CPUAVRState
*env
)
189 CPUState
*cs
= env_cpu(env
);
191 cs
->exception_index
= EXCP_DEBUG
;
195 void helper_wdr(CPUAVRState
*env
)
197 qemu_log_mask(LOG_UNIMP
, "WDG reset (not implemented)\n");
201 * This function implements IN instruction
203 * It does the following
204 * a. if an IO register belongs to CPU, its value is read and returned
205 * b. otherwise io address is translated to mem address and physical memory
207 * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
210 target_ulong
helper_inb(CPUAVRState
*env
, uint32_t port
)
212 target_ulong data
= 0;
215 case 0x38: /* RAMPD */
216 data
= 0xff & (env
->rampD
>> 16);
218 case 0x39: /* RAMPX */
219 data
= 0xff & (env
->rampX
>> 16);
221 case 0x3a: /* RAMPY */
222 data
= 0xff & (env
->rampY
>> 16);
224 case 0x3b: /* RAMPZ */
225 data
= 0xff & (env
->rampZ
>> 16);
227 case 0x3c: /* EIND */
228 data
= 0xff & (env
->eind
>> 16);
231 data
= env
->sp
& 0x00ff;
236 case 0x3f: /* SREG */
237 data
= cpu_get_sreg(env
);
240 /* not a special register, pass to normal memory access */
241 data
= address_space_ldub(&address_space_memory
,
242 OFFSET_IO_REGISTERS
+ port
,
243 MEMTXATTRS_UNSPECIFIED
, NULL
);
250 * This function implements OUT instruction
252 * It does the following
253 * a. if an IO register belongs to CPU, its value is written into the register
254 * b. otherwise io address is translated to mem address and physical memory
256 * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
259 void helper_outb(CPUAVRState
*env
, uint32_t port
, uint32_t data
)
264 case 0x38: /* RAMPD */
265 if (avr_feature(env
, AVR_FEATURE_RAMPD
)) {
266 env
->rampD
= (data
& 0xff) << 16;
269 case 0x39: /* RAMPX */
270 if (avr_feature(env
, AVR_FEATURE_RAMPX
)) {
271 env
->rampX
= (data
& 0xff) << 16;
274 case 0x3a: /* RAMPY */
275 if (avr_feature(env
, AVR_FEATURE_RAMPY
)) {
276 env
->rampY
= (data
& 0xff) << 16;
279 case 0x3b: /* RAMPZ */
280 if (avr_feature(env
, AVR_FEATURE_RAMPZ
)) {
281 env
->rampZ
= (data
& 0xff) << 16;
284 case 0x3c: /* EIDN */
285 env
->eind
= (data
& 0xff) << 16;
288 env
->sp
= (env
->sp
& 0xff00) | (data
);
291 if (avr_feature(env
, AVR_FEATURE_2_BYTE_SP
)) {
292 env
->sp
= (env
->sp
& 0x00ff) | (data
<< 8);
295 case 0x3f: /* SREG */
296 cpu_set_sreg(env
, data
);
299 /* not a special register, pass to normal memory access */
300 address_space_stb(&address_space_memory
, OFFSET_IO_REGISTERS
+ port
,
301 data
, MEMTXATTRS_UNSPECIFIED
, NULL
);
306 * this function implements LD instruction when there is a possibility to read
307 * from a CPU register
309 target_ulong
helper_fullrd(CPUAVRState
*env
, uint32_t addr
)
313 env
->fullacc
= false;
315 if (addr
< NUMBER_OF_CPU_REGISTERS
) {
318 } else if (addr
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
320 data
= helper_inb(env
, addr
- NUMBER_OF_CPU_REGISTERS
);
323 data
= address_space_ldub(&address_space_memory
, OFFSET_DATA
+ addr
,
324 MEMTXATTRS_UNSPECIFIED
, NULL
);
330 * this function implements ST instruction when there is a possibility to write
331 * into a CPU register
333 void helper_fullwr(CPUAVRState
*env
, uint32_t data
, uint32_t addr
)
335 env
->fullacc
= false;
337 /* Following logic assumes this: */
338 assert(OFFSET_CPU_REGISTERS
== OFFSET_DATA
);
339 assert(OFFSET_IO_REGISTERS
== OFFSET_CPU_REGISTERS
+
340 NUMBER_OF_CPU_REGISTERS
);
342 if (addr
< NUMBER_OF_CPU_REGISTERS
) {
345 } else if (addr
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
347 helper_outb(env
, addr
- NUMBER_OF_CPU_REGISTERS
, data
);
350 address_space_stb(&address_space_memory
, OFFSET_DATA
+ addr
, data
,
351 MEMTXATTRS_UNSPECIFIED
, NULL
);