4 * Copyright (c) 2019-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 "qapi/error.h"
23 #include "qemu/qemu-print.h"
24 #include "exec/exec-all.h"
26 #include "disas/dis-asm.h"
28 static void avr_cpu_set_pc(CPUState
*cs
, vaddr value
)
30 AVRCPU
*cpu
= AVR_CPU(cs
);
32 cpu
->env
.pc_w
= value
/ 2; /* internally PC points to words */
35 static bool avr_cpu_has_work(CPUState
*cs
)
37 AVRCPU
*cpu
= AVR_CPU(cs
);
38 CPUAVRState
*env
= &cpu
->env
;
40 return (cs
->interrupt_request
& (CPU_INTERRUPT_HARD
| CPU_INTERRUPT_RESET
))
41 && cpu_interrupts_enabled(env
);
44 static void avr_cpu_synchronize_from_tb(CPUState
*cs
, TranslationBlock
*tb
)
46 AVRCPU
*cpu
= AVR_CPU(cs
);
47 CPUAVRState
*env
= &cpu
->env
;
49 env
->pc_w
= tb
->pc
/ 2; /* internally PC points to words */
52 static void avr_cpu_reset(DeviceState
*ds
)
54 CPUState
*cs
= CPU(ds
);
55 AVRCPU
*cpu
= AVR_CPU(cs
);
56 AVRCPUClass
*mcc
= AVR_CPU_GET_CLASS(cpu
);
57 CPUAVRState
*env
= &cpu
->env
;
59 mcc
->parent_reset(ds
);
80 memset(env
->r
, 0, sizeof(env
->r
));
85 static void avr_cpu_disas_set_info(CPUState
*cpu
, disassemble_info
*info
)
87 info
->mach
= bfd_arch_avr
;
88 info
->print_insn
= NULL
;
91 static void avr_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
93 CPUState
*cs
= CPU(dev
);
94 AVRCPUClass
*mcc
= AVR_CPU_GET_CLASS(dev
);
95 Error
*local_err
= NULL
;
97 cpu_exec_realizefn(cs
, &local_err
);
98 if (local_err
!= NULL
) {
99 error_propagate(errp
, local_err
);
105 mcc
->parent_realize(dev
, errp
);
108 static void avr_cpu_set_int(void *opaque
, int irq
, int level
)
110 AVRCPU
*cpu
= opaque
;
111 CPUAVRState
*env
= &cpu
->env
;
112 CPUState
*cs
= CPU(cpu
);
113 uint64_t mask
= (1ull << irq
);
117 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
119 env
->intsrc
&= ~mask
;
120 if (env
->intsrc
== 0) {
121 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
126 static void avr_cpu_initfn(Object
*obj
)
128 AVRCPU
*cpu
= AVR_CPU(obj
);
130 cpu_set_cpustate_pointers(cpu
);
132 /* Set the number of interrupts supported by the CPU. */
133 qdev_init_gpio_in(DEVICE(cpu
), avr_cpu_set_int
,
134 sizeof(cpu
->env
.intsrc
) * 8);
137 static ObjectClass
*avr_cpu_class_by_name(const char *cpu_model
)
141 oc
= object_class_by_name(cpu_model
);
142 if (object_class_dynamic_cast(oc
, TYPE_AVR_CPU
) == NULL
||
143 object_class_is_abstract(oc
)) {
149 static void avr_cpu_dump_state(CPUState
*cs
, FILE *f
, int flags
)
151 AVRCPU
*cpu
= AVR_CPU(cs
);
152 CPUAVRState
*env
= &cpu
->env
;
155 qemu_fprintf(f
, "\n");
156 qemu_fprintf(f
, "PC: %06x\n", env
->pc_w
);
157 qemu_fprintf(f
, "SP: %04x\n", env
->sp
);
158 qemu_fprintf(f
, "rampD: %02x\n", env
->rampD
>> 16);
159 qemu_fprintf(f
, "rampX: %02x\n", env
->rampX
>> 16);
160 qemu_fprintf(f
, "rampY: %02x\n", env
->rampY
>> 16);
161 qemu_fprintf(f
, "rampZ: %02x\n", env
->rampZ
>> 16);
162 qemu_fprintf(f
, "EIND: %02x\n", env
->eind
>> 16);
163 qemu_fprintf(f
, "X: %02x%02x\n", env
->r
[27], env
->r
[26]);
164 qemu_fprintf(f
, "Y: %02x%02x\n", env
->r
[29], env
->r
[28]);
165 qemu_fprintf(f
, "Z: %02x%02x\n", env
->r
[31], env
->r
[30]);
166 qemu_fprintf(f
, "SREG: [ %c %c %c %c %c %c %c %c ]\n",
167 env
->sregI
? 'I' : '-',
168 env
->sregT
? 'T' : '-',
169 env
->sregH
? 'H' : '-',
170 env
->sregS
? 'S' : '-',
171 env
->sregV
? 'V' : '-',
172 env
->sregN
? '-' : 'N', /* Zf has negative logic */
173 env
->sregZ
? 'Z' : '-',
174 env
->sregC
? 'I' : '-');
175 qemu_fprintf(f
, "SKIP: %02x\n", env
->skip
);
177 qemu_fprintf(f
, "\n");
178 for (i
= 0; i
< ARRAY_SIZE(env
->r
); i
++) {
179 qemu_fprintf(f
, "R[%02d]: %02x ", i
, env
->r
[i
]);
182 qemu_fprintf(f
, "\n");
185 qemu_fprintf(f
, "\n");
188 static void avr_cpu_class_init(ObjectClass
*oc
, void *data
)
190 DeviceClass
*dc
= DEVICE_CLASS(oc
);
191 CPUClass
*cc
= CPU_CLASS(oc
);
192 AVRCPUClass
*mcc
= AVR_CPU_CLASS(oc
);
194 mcc
->parent_realize
= dc
->realize
;
195 dc
->realize
= avr_cpu_realizefn
;
197 device_class_set_parent_reset(dc
, avr_cpu_reset
, &mcc
->parent_reset
);
199 cc
->class_by_name
= avr_cpu_class_by_name
;
201 cc
->has_work
= avr_cpu_has_work
;
202 cc
->dump_state
= avr_cpu_dump_state
;
203 cc
->set_pc
= avr_cpu_set_pc
;
204 cc
->disas_set_info
= avr_cpu_disas_set_info
;
205 cc
->tcg_initialize
= avr_cpu_tcg_init
;
206 cc
->synchronize_from_tb
= avr_cpu_synchronize_from_tb
;