2020-02-07 00:20:49 +01:00
|
|
|
/*
|
|
|
|
* QEMU AVR CPU helpers
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2020 Michael Rolnik
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see
|
|
|
|
* <http://www.gnu.org/licenses/lgpl-2.1.html>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2022-02-07 09:27:56 +01:00
|
|
|
#include "qemu/log.h"
|
2020-02-07 00:20:49 +01:00
|
|
|
#include "cpu.h"
|
2021-02-04 17:39:23 +01:00
|
|
|
#include "hw/core/tcg-cpu-ops.h"
|
2020-02-07 00:20:49 +01:00
|
|
|
#include "exec/exec-all.h"
|
2020-01-24 01:51:08 +01:00
|
|
|
#include "exec/address-spaces.h"
|
2020-02-07 00:20:49 +01:00
|
|
|
#include "exec/helper-proto.h"
|
|
|
|
|
|
|
|
bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
|
|
|
|
{
|
|
|
|
AVRCPU *cpu = AVR_CPU(cs);
|
|
|
|
CPUAVRState *env = &cpu->env;
|
|
|
|
|
2022-08-26 22:53:32 +02:00
|
|
|
/*
|
|
|
|
* We cannot separate a skip from the next instruction,
|
|
|
|
* as the skip would not be preserved across the interrupt.
|
|
|
|
* Separating the two insn normally only happens at page boundaries.
|
|
|
|
*/
|
|
|
|
if (env->skip) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:20:49 +01:00
|
|
|
if (interrupt_request & CPU_INTERRUPT_RESET) {
|
|
|
|
if (cpu_interrupts_enabled(env)) {
|
|
|
|
cs->exception_index = EXCP_RESET;
|
2022-08-26 22:32:56 +02:00
|
|
|
avr_cpu_do_interrupt(cs);
|
2020-02-07 00:20:49 +01:00
|
|
|
|
|
|
|
cs->interrupt_request &= ~CPU_INTERRUPT_RESET;
|
2022-08-26 22:35:52 +02:00
|
|
|
return true;
|
2020-02-07 00:20:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (interrupt_request & CPU_INTERRUPT_HARD) {
|
|
|
|
if (cpu_interrupts_enabled(env) && env->intsrc != 0) {
|
|
|
|
int index = ctz32(env->intsrc);
|
|
|
|
cs->exception_index = EXCP_INT(index);
|
2022-08-26 22:32:56 +02:00
|
|
|
avr_cpu_do_interrupt(cs);
|
2020-02-07 00:20:49 +01:00
|
|
|
|
|
|
|
env->intsrc &= env->intsrc - 1; /* clear the interrupt */
|
2021-03-12 17:47:54 +01:00
|
|
|
if (!env->intsrc) {
|
|
|
|
cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
|
|
|
|
}
|
2022-08-26 22:35:52 +02:00
|
|
|
return true;
|
2020-02-07 00:20:49 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-26 22:35:52 +02:00
|
|
|
return false;
|
2020-02-07 00:20:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void avr_cpu_do_interrupt(CPUState *cs)
|
|
|
|
{
|
|
|
|
AVRCPU *cpu = AVR_CPU(cs);
|
|
|
|
CPUAVRState *env = &cpu->env;
|
|
|
|
|
|
|
|
uint32_t ret = env->pc_w;
|
|
|
|
int vector = 0;
|
|
|
|
int size = avr_feature(env, AVR_FEATURE_JMP_CALL) ? 2 : 1;
|
|
|
|
int base = 0;
|
|
|
|
|
|
|
|
if (cs->exception_index == EXCP_RESET) {
|
|
|
|
vector = 0;
|
|
|
|
} else if (env->intsrc != 0) {
|
|
|
|
vector = ctz32(env->intsrc) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avr_feature(env, AVR_FEATURE_3_BYTE_PC)) {
|
|
|
|
cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
|
|
|
|
cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
|
|
|
|
cpu_stb_data(env, env->sp--, (ret & 0xff0000) >> 16);
|
|
|
|
} else if (avr_feature(env, AVR_FEATURE_2_BYTE_PC)) {
|
|
|
|
cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
|
|
|
|
cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
|
|
|
|
} else {
|
|
|
|
cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
|
|
|
|
}
|
|
|
|
|
|
|
|
env->pc_w = base + vector * size;
|
|
|
|
env->sregI = 0; /* clear Global Interrupt Flag */
|
|
|
|
|
|
|
|
cs->exception_index = -1;
|
|
|
|
}
|
2020-02-06 23:08:31 +01:00
|
|
|
|
|
|
|
hwaddr avr_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
|
|
|
|
{
|
2020-10-09 08:44:46 +02:00
|
|
|
return addr; /* I assume 1:1 address correspondence */
|
2020-02-06 23:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
|
|
|
|
MMUAccessType access_type, int mmu_idx,
|
|
|
|
bool probe, uintptr_t retaddr)
|
|
|
|
{
|
2022-08-23 06:57:20 +02:00
|
|
|
int prot, page_size = TARGET_PAGE_SIZE;
|
2020-02-06 23:08:31 +01:00
|
|
|
uint32_t paddr;
|
|
|
|
|
|
|
|
address &= TARGET_PAGE_MASK;
|
|
|
|
|
|
|
|
if (mmu_idx == MMU_CODE_IDX) {
|
2022-08-23 06:57:20 +02:00
|
|
|
/* Access to code in flash. */
|
2020-02-06 23:08:31 +01:00
|
|
|
paddr = OFFSET_CODE + address;
|
|
|
|
prot = PAGE_READ | PAGE_EXEC;
|
2022-08-23 06:57:20 +02:00
|
|
|
if (paddr >= OFFSET_DATA) {
|
|
|
|
/*
|
|
|
|
* This should not be possible via any architectural operations.
|
|
|
|
* There is certainly not an exception that we can deliver.
|
|
|
|
* Accept probing that might come from generic code.
|
|
|
|
*/
|
|
|
|
if (probe) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-06 23:08:31 +01:00
|
|
|
error_report("execution left flash memory");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-23 06:57:20 +02:00
|
|
|
/* Access to memory. */
|
2020-02-06 23:08:31 +01:00
|
|
|
paddr = OFFSET_DATA + address;
|
|
|
|
prot = PAGE_READ | PAGE_WRITE;
|
2022-08-23 06:57:20 +02:00
|
|
|
if (address < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
|
|
|
|
/*
|
|
|
|
* Access to CPU registers, exit and rebuilt this TB to use
|
|
|
|
* full access in case it touches specially handled registers
|
|
|
|
* like SREG or SP. For probing, set page_size = 1, in order
|
|
|
|
* to force tlb_fill to be called for the next access.
|
|
|
|
*/
|
|
|
|
if (probe) {
|
|
|
|
page_size = 1;
|
|
|
|
} else {
|
|
|
|
AVRCPU *cpu = AVR_CPU(cs);
|
|
|
|
CPUAVRState *env = &cpu->env;
|
|
|
|
env->fullacc = 1;
|
|
|
|
cpu_loop_exit_restore(cs, retaddr);
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 23:08:31 +01:00
|
|
|
}
|
|
|
|
|
2022-08-23 06:57:20 +02:00
|
|
|
tlb_set_page(cs, address, paddr, prot, mmu_idx, page_size);
|
2020-02-06 23:08:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
2020-01-24 01:51:08 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
void helper_sleep(CPUAVRState *env)
|
|
|
|
{
|
|
|
|
CPUState *cs = env_cpu(env);
|
|
|
|
|
|
|
|
cs->exception_index = EXCP_HLT;
|
|
|
|
cpu_loop_exit(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void helper_unsupported(CPUAVRState *env)
|
|
|
|
{
|
|
|
|
CPUState *cs = env_cpu(env);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I count not find what happens on the real platform, so
|
|
|
|
* it's EXCP_DEBUG for meanwhile
|
|
|
|
*/
|
|
|
|
cs->exception_index = EXCP_DEBUG;
|
|
|
|
if (qemu_loglevel_mask(LOG_UNIMP)) {
|
|
|
|
qemu_log("UNSUPPORTED\n");
|
|
|
|
cpu_dump_state(cs, stderr, 0);
|
|
|
|
}
|
|
|
|
cpu_loop_exit(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void helper_debug(CPUAVRState *env)
|
|
|
|
{
|
|
|
|
CPUState *cs = env_cpu(env);
|
|
|
|
|
|
|
|
cs->exception_index = EXCP_DEBUG;
|
|
|
|
cpu_loop_exit(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void helper_break(CPUAVRState *env)
|
|
|
|
{
|
|
|
|
CPUState *cs = env_cpu(env);
|
|
|
|
|
|
|
|
cs->exception_index = EXCP_DEBUG;
|
|
|
|
cpu_loop_exit(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void helper_wdr(CPUAVRState *env)
|
|
|
|
{
|
2021-05-02 21:09:00 +02:00
|
|
|
qemu_log_mask(LOG_UNIMP, "WDG reset (not implemented)\n");
|
2020-01-24 01:51:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function implements IN instruction
|
|
|
|
*
|
|
|
|
* It does the following
|
|
|
|
* a. if an IO register belongs to CPU, its value is read and returned
|
|
|
|
* b. otherwise io address is translated to mem address and physical memory
|
|
|
|
* is read.
|
|
|
|
* c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
target_ulong helper_inb(CPUAVRState *env, uint32_t port)
|
|
|
|
{
|
|
|
|
target_ulong data = 0;
|
|
|
|
|
|
|
|
switch (port) {
|
|
|
|
case 0x38: /* RAMPD */
|
|
|
|
data = 0xff & (env->rampD >> 16);
|
|
|
|
break;
|
|
|
|
case 0x39: /* RAMPX */
|
|
|
|
data = 0xff & (env->rampX >> 16);
|
|
|
|
break;
|
|
|
|
case 0x3a: /* RAMPY */
|
|
|
|
data = 0xff & (env->rampY >> 16);
|
|
|
|
break;
|
|
|
|
case 0x3b: /* RAMPZ */
|
|
|
|
data = 0xff & (env->rampZ >> 16);
|
|
|
|
break;
|
|
|
|
case 0x3c: /* EIND */
|
|
|
|
data = 0xff & (env->eind >> 16);
|
|
|
|
break;
|
|
|
|
case 0x3d: /* SPL */
|
|
|
|
data = env->sp & 0x00ff;
|
|
|
|
break;
|
|
|
|
case 0x3e: /* SPH */
|
|
|
|
data = env->sp >> 8;
|
|
|
|
break;
|
|
|
|
case 0x3f: /* SREG */
|
|
|
|
data = cpu_get_sreg(env);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* not a special register, pass to normal memory access */
|
|
|
|
data = address_space_ldub(&address_space_memory,
|
|
|
|
OFFSET_IO_REGISTERS + port,
|
|
|
|
MEMTXATTRS_UNSPECIFIED, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function implements OUT instruction
|
|
|
|
*
|
|
|
|
* It does the following
|
|
|
|
* a. if an IO register belongs to CPU, its value is written into the register
|
|
|
|
* b. otherwise io address is translated to mem address and physical memory
|
|
|
|
* is written.
|
|
|
|
* c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void helper_outb(CPUAVRState *env, uint32_t port, uint32_t data)
|
|
|
|
{
|
|
|
|
data &= 0x000000ff;
|
|
|
|
|
|
|
|
switch (port) {
|
|
|
|
case 0x38: /* RAMPD */
|
|
|
|
if (avr_feature(env, AVR_FEATURE_RAMPD)) {
|
|
|
|
env->rampD = (data & 0xff) << 16;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x39: /* RAMPX */
|
|
|
|
if (avr_feature(env, AVR_FEATURE_RAMPX)) {
|
|
|
|
env->rampX = (data & 0xff) << 16;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x3a: /* RAMPY */
|
|
|
|
if (avr_feature(env, AVR_FEATURE_RAMPY)) {
|
|
|
|
env->rampY = (data & 0xff) << 16;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x3b: /* RAMPZ */
|
|
|
|
if (avr_feature(env, AVR_FEATURE_RAMPZ)) {
|
|
|
|
env->rampZ = (data & 0xff) << 16;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x3c: /* EIDN */
|
|
|
|
env->eind = (data & 0xff) << 16;
|
|
|
|
break;
|
|
|
|
case 0x3d: /* SPL */
|
|
|
|
env->sp = (env->sp & 0xff00) | (data);
|
|
|
|
break;
|
|
|
|
case 0x3e: /* SPH */
|
|
|
|
if (avr_feature(env, AVR_FEATURE_2_BYTE_SP)) {
|
|
|
|
env->sp = (env->sp & 0x00ff) | (data << 8);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x3f: /* SREG */
|
|
|
|
cpu_set_sreg(env, data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* not a special register, pass to normal memory access */
|
|
|
|
address_space_stb(&address_space_memory, OFFSET_IO_REGISTERS + port,
|
|
|
|
data, MEMTXATTRS_UNSPECIFIED, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-10-09 08:44:46 +02:00
|
|
|
* this function implements LD instruction when there is a possibility to read
|
2020-01-24 01:51:08 +01:00
|
|
|
* from a CPU register
|
|
|
|
*/
|
|
|
|
target_ulong helper_fullrd(CPUAVRState *env, uint32_t addr)
|
|
|
|
{
|
|
|
|
uint8_t data;
|
|
|
|
|
|
|
|
env->fullacc = false;
|
|
|
|
|
|
|
|
if (addr < NUMBER_OF_CPU_REGISTERS) {
|
|
|
|
/* CPU registers */
|
|
|
|
data = env->r[addr];
|
|
|
|
} else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
|
|
|
|
/* IO registers */
|
|
|
|
data = helper_inb(env, addr - NUMBER_OF_CPU_REGISTERS);
|
|
|
|
} else {
|
|
|
|
/* memory */
|
|
|
|
data = address_space_ldub(&address_space_memory, OFFSET_DATA + addr,
|
|
|
|
MEMTXATTRS_UNSPECIFIED, NULL);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-10-09 08:44:46 +02:00
|
|
|
* this function implements ST instruction when there is a possibility to write
|
2020-01-24 01:51:08 +01:00
|
|
|
* into a CPU register
|
|
|
|
*/
|
|
|
|
void helper_fullwr(CPUAVRState *env, uint32_t data, uint32_t addr)
|
|
|
|
{
|
|
|
|
env->fullacc = false;
|
|
|
|
|
|
|
|
/* Following logic assumes this: */
|
|
|
|
assert(OFFSET_CPU_REGISTERS == OFFSET_DATA);
|
|
|
|
assert(OFFSET_IO_REGISTERS == OFFSET_CPU_REGISTERS +
|
|
|
|
NUMBER_OF_CPU_REGISTERS);
|
|
|
|
|
|
|
|
if (addr < NUMBER_OF_CPU_REGISTERS) {
|
|
|
|
/* CPU registers */
|
|
|
|
env->r[addr] = data;
|
|
|
|
} else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
|
|
|
|
/* IO registers */
|
|
|
|
helper_outb(env, addr - NUMBER_OF_CPU_REGISTERS, data);
|
|
|
|
} else {
|
|
|
|
/* memory */
|
|
|
|
address_space_stb(&address_space_memory, OFFSET_DATA + addr, data,
|
|
|
|
MEMTXATTRS_UNSPECIFIED, NULL);
|
|
|
|
}
|
|
|
|
}
|