From 28a11976daa0376db37766f807f5b234cc508dfb Mon Sep 17 00:00:00 2001 From: Mateusz Holenko Date: Wed, 10 Jul 2019 10:38:38 +0200 Subject: [PATCH 1/4] Allow to set custom RAM base address for emulator This is needed when loading the emulator to RAM with an offset. --- src/main/c/common/ram.ld | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/c/common/ram.ld b/src/main/c/common/ram.ld index 19dc3d9..2ebf858 100755 --- a/src/main/c/common/ram.ld +++ b/src/main/c/common/ram.ld @@ -4,7 +4,7 @@ ENTRY( _start ) MEMORY { - ram : ORIGIN = 0x80000000, LENGTH = 64k + ram : ORIGIN = DEFINED(__ram_origin) ? __ram_origin : 0x80000000, LENGTH = 64k } From 423355ecbff46ef0dddc5e7a48ae10af2a2d1bea Mon Sep 17 00:00:00 2001 From: Mateusz Holenko Date: Wed, 10 Jul 2019 10:38:58 +0200 Subject: [PATCH 2/4] Allow to set custom DTB/OS_CALL addresses Setting those from command line during compilation allows to create a custom setup without the need of modifying the sources. --- src/main/c/emulator/src/config.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/c/emulator/src/config.h b/src/main/c/emulator/src/config.h index 191ae9f..6e251ad 100644 --- a/src/main/c/emulator/src/config.h +++ b/src/main/c/emulator/src/config.h @@ -3,7 +3,13 @@ //#define QEMU #define SIM + +#ifndef OS_CALL #define OS_CALL 0xC0000000 +#endif + +#ifndef DTB #define DTB 0xC3000000 +#endif #endif From 39c3f408e58bd0eb6b62638071f8de3e2659465b Mon Sep 17 00:00:00 2001 From: Mateusz Holenko Date: Wed, 10 Jul 2019 10:39:47 +0200 Subject: [PATCH 3/4] Create makefile targets Allow to change build target without modifiying the sources. In order to keep compatibilty `sim` target is built by default. --- src/main/c/emulator/makefile | 6 ++++++ src/main/c/emulator/src/config.h | 3 --- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/c/emulator/makefile b/src/main/c/emulator/makefile index e861785..d9d9098 100755 --- a/src/main/c/emulator/makefile +++ b/src/main/c/emulator/makefile @@ -12,6 +12,12 @@ SRCS = $(wildcard src/*.c) \ LDSCRIPT = ${STANDALONE}/common/ram.ld +sim: CFLAGS += -DSIM +sim: all + +qemu: CFLAGS += -DQEMU +qemu: all + include ${STANDALONE}/common/riscv64-unknown-elf.mk include ${STANDALONE}/common/standalone.mk diff --git a/src/main/c/emulator/src/config.h b/src/main/c/emulator/src/config.h index 6e251ad..afce2d5 100644 --- a/src/main/c/emulator/src/config.h +++ b/src/main/c/emulator/src/config.h @@ -1,9 +1,6 @@ #ifndef CONFIG_H #define CONFIG_H -//#define QEMU -#define SIM - #ifndef OS_CALL #define OS_CALL 0xC0000000 #endif From 6a2584b840a172f39b6c7882276a25a76874047c Mon Sep 17 00:00:00 2001 From: Mateusz Holenko Date: Wed, 10 Jul 2019 10:39:26 +0200 Subject: [PATCH 4/4] Add `litex` target Use configuration from the `csr.h` file generated dynamically when building a LiteX platform. --- src/main/c/emulator/makefile | 5 +++ src/main/c/emulator/src/hal.c | 71 ++++++++++++++++++++++++++++++++++ src/main/c/emulator/src/main.c | 2 +- 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/main/c/emulator/makefile b/src/main/c/emulator/makefile index d9d9098..7534d08 100755 --- a/src/main/c/emulator/makefile +++ b/src/main/c/emulator/makefile @@ -18,6 +18,11 @@ sim: all qemu: CFLAGS += -DQEMU qemu: all +litex: CFLAGS += -DLITEX -I${LITEX_BASE}/software/include +litex: | check_litex_base all +check_litex_base: + @[ "${LITEX_BASE}" ] || ( echo ">> LITEX_BASE is not set"; exit 1 ) + include ${STANDALONE}/common/riscv64-unknown-elf.mk include ${STANDALONE}/common/standalone.mk diff --git a/src/main/c/emulator/src/hal.c b/src/main/c/emulator/src/hal.c index 52bb0e4..a9189f2 100644 --- a/src/main/c/emulator/src/hal.c +++ b/src/main/c/emulator/src/hal.c @@ -144,5 +144,76 @@ void halInit(){ #endif +#ifdef LITEX + +// this is copied from LiteX +#define CSR_ACCESSORS_DEFINED +static inline void csr_writeb(uint8_t value, unsigned long addr) +{ + *((volatile uint8_t *)addr) = value; +} + +static inline uint8_t csr_readb(unsigned long addr) +{ + return *(volatile uint8_t *)addr; +} + +static inline void csr_writew(uint16_t value, unsigned long addr) +{ + *((volatile uint16_t *)addr) = value; +} + +static inline uint16_t csr_readw(unsigned long addr) +{ + return *(volatile uint16_t *)addr; +} + +static inline void csr_writel(uint32_t value, unsigned long addr) +{ + *((volatile uint32_t *)addr) = value; +} + +static inline uint32_t csr_readl(unsigned long addr) +{ + return *(volatile uint32_t *)addr; +} + +// this is a file generated by LiteX +#include + +#if !defined(CSR_UART_BASE) || !defined(CSR_CPU_BASE) + #error LiteX configuration with uart and cpu_timer is required. +#endif + +void stopSim(){ + while(1); +} + +void putC(char ch){ + uart_rxtx_write(ch); +} + +int32_t getC(){ + return uart_rxempty_read() + ? -1 + : uart_rxtx_read(); +} + +uint32_t rdtime(){ + return (uint32_t)cpu_timer_time_read(); +} + +uint32_t rdtimeh(){ + return (uint32_t)(cpu_timer_time_read() >> 32); +} + +void setMachineTimerCmp(uint32_t low, uint32_t high){ + cpu_timer_time_cmp_write((((unsigned long long int)high) << 32) | low); +} + +void halInit(){ +} + +#endif diff --git a/src/main/c/emulator/src/main.c b/src/main/c/emulator/src/main.c index 2102818..dc12fb8 100755 --- a/src/main/c/emulator/src/main.c +++ b/src/main/c/emulator/src/main.c @@ -161,7 +161,7 @@ void trap(){ #ifdef SIM uint32_t instruction = csr_read(mbadaddr); #endif -#ifdef QEMU +#if defined(QEMU) || defined(LITEX) uint32_t instruction = 0; uint32_t i; if (mepc & 2) {