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 "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
)
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
;
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 */
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 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
)
111 MemTxAttrs attrs
= {};
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");
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
;
132 cpu_loop_exit_restore(cs
, retaddr
);
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
);
149 void helper_sleep(CPUAVRState
*env
)
151 CPUState
*cs
= env_cpu(env
);
153 cs
->exception_index
= EXCP_HLT
;
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);
173 void helper_debug(CPUAVRState
*env
)
175 CPUState
*cs
= env_cpu(env
);
177 cs
->exception_index
= EXCP_DEBUG
;
181 void helper_break(CPUAVRState
*env
)
183 CPUState
*cs
= env_cpu(env
);
185 cs
->exception_index
= EXCP_DEBUG
;
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
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;
209 case 0x38: /* RAMPD */
210 data
= 0xff & (env
->rampD
>> 16);
212 case 0x39: /* RAMPX */
213 data
= 0xff & (env
->rampX
>> 16);
215 case 0x3a: /* RAMPY */
216 data
= 0xff & (env
->rampY
>> 16);
218 case 0x3b: /* RAMPZ */
219 data
= 0xff & (env
->rampZ
>> 16);
221 case 0x3c: /* EIND */
222 data
= 0xff & (env
->eind
>> 16);
225 data
= env
->sp
& 0x00ff;
230 case 0x3f: /* SREG */
231 data
= cpu_get_sreg(env
);
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
);
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
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
)
258 case 0x38: /* RAMPD */
259 if (avr_feature(env
, AVR_FEATURE_RAMPD
)) {
260 env
->rampD
= (data
& 0xff) << 16;
263 case 0x39: /* RAMPX */
264 if (avr_feature(env
, AVR_FEATURE_RAMPX
)) {
265 env
->rampX
= (data
& 0xff) << 16;
268 case 0x3a: /* RAMPY */
269 if (avr_feature(env
, AVR_FEATURE_RAMPY
)) {
270 env
->rampY
= (data
& 0xff) << 16;
273 case 0x3b: /* RAMPZ */
274 if (avr_feature(env
, AVR_FEATURE_RAMPZ
)) {
275 env
->rampZ
= (data
& 0xff) << 16;
278 case 0x3c: /* EIDN */
279 env
->eind
= (data
& 0xff) << 16;
282 env
->sp
= (env
->sp
& 0xff00) | (data
);
285 if (avr_feature(env
, AVR_FEATURE_2_BYTE_SP
)) {
286 env
->sp
= (env
->sp
& 0x00ff) | (data
<< 8);
289 case 0x3f: /* SREG */
290 cpu_set_sreg(env
, data
);
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
)
307 env
->fullacc
= false;
309 if (addr
< NUMBER_OF_CPU_REGISTERS
) {
312 } else if (addr
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
314 data
= helper_inb(env
, addr
- NUMBER_OF_CPU_REGISTERS
);
317 data
= address_space_ldub(&address_space_memory
, OFFSET_DATA
+ addr
,
318 MEMTXATTRS_UNSPECIFIED
, NULL
);
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
) {
339 } else if (addr
< NUMBER_OF_CPU_REGISTERS
+ NUMBER_OF_IO_REGISTERS
) {
341 helper_outb(env
, addr
- NUMBER_OF_CPU_REGISTERS
, data
);
344 address_space_stb(&address_space_memory
, OFFSET_DATA
+ addr
, data
,
345 MEMTXATTRS_UNSPECIFIED
, NULL
);