From d133cc70b125efc1d6c992cb49ce1bca41cf580e Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sun, 30 Apr 2023 17:47:00 +0300 Subject: refactor component tree --- include/gran/bus/simple_bus.h | 10 ++++++ include/gran/clock_domain.h | 6 ++-- include/gran/common.h | 6 ++-- include/gran/component.h | 6 ++-- include/gran/components/bus/simple_bus.h | 10 ------ include/gran/components/mem/simple_mem.h | 8 ----- include/gran/cpu/riscv/simple_riscv32.h | 8 +++++ include/gran/mem/simple_mem.h | 8 +++++ include/gran/root.h | 6 ++-- src/components/bus/simple_bus.c | 2 +- src/components/cpu/riscv/simple_riscv32.c | 58 +++++++++++++++++++++++++++++++ src/components/mem/simple_mem.c | 2 +- tests/simple_mem/bus.c | 4 +-- tests/simple_mem/many.c | 2 +- tests/simple_mem/no_bus.c | 2 +- tests/simple_mem/traffic_gen.h | 6 ++-- 16 files changed, 105 insertions(+), 39 deletions(-) create mode 100644 include/gran/bus/simple_bus.h delete mode 100644 include/gran/components/bus/simple_bus.h delete mode 100644 include/gran/components/mem/simple_mem.h create mode 100644 include/gran/cpu/riscv/simple_riscv32.h create mode 100644 include/gran/mem/simple_mem.h create mode 100644 src/components/cpu/riscv/simple_riscv32.c diff --git a/include/gran/bus/simple_bus.h b/include/gran/bus/simple_bus.h new file mode 100644 index 0000000..c569e8a --- /dev/null +++ b/include/gran/bus/simple_bus.h @@ -0,0 +1,10 @@ +#ifndef GRAN_SIMPLE_BUS_H +#define GRAN_SIMPLE_BUS_H + +#include + +struct component *create_simple_bus(); +stat simple_bus_add(struct component *bus, struct component *component, + uintptr_t addr, size_t size); + +#endif /* GRAN_SIMPLE_BUS_H */ diff --git a/include/gran/clock_domain.h b/include/gran/clock_domain.h index 3f960ae..9fb04a4 100644 --- a/include/gran/clock_domain.h +++ b/include/gran/clock_domain.h @@ -1,5 +1,5 @@ -#ifndef EXSIM_CLOCK_DOMAIN_H -#define EXSIM_CLOCK_DOMAIN_H +#ifndef GRAN_CLOCK_DOMAIN_H +#define GRAN_CLOCK_DOMAIN_H #include #include @@ -37,4 +37,4 @@ bool le_time(struct clock_time a, struct clock_time b); struct clock_time max_time(struct clock_time a, struct clock_time b); struct clock_time domain_time(struct clock_domain *); -#endif /* EXSIM_CLOCK_DOMAIN_H */ +#endif /* GRAN_CLOCK_DOMAIN_H */ diff --git a/include/gran/common.h b/include/gran/common.h index c012c49..b4b8978 100644 --- a/include/gran/common.h +++ b/include/gran/common.h @@ -1,5 +1,5 @@ -#ifndef EXSIM_COMMON_H -#define EXSIM_COMMON_H +#ifndef GRAN_COMMON_H +#define GRAN_COMMON_H #include @@ -31,4 +31,4 @@ typedef enum { #define debug(x, ...) #endif -#endif /* EXSIM_COMMON_H */ +#endif /* GRAN_COMMON_H */ diff --git a/include/gran/component.h b/include/gran/component.h index cd21a6b..498d7c0 100644 --- a/include/gran/component.h +++ b/include/gran/component.h @@ -1,5 +1,5 @@ -#ifndef EXSIM_COMPONENT_H -#define EXSIM_COMPONENT_H +#ifndef GRAN_COMPONENT_H +#define GRAN_COMPONENT_H #include #include @@ -104,4 +104,4 @@ static inline stat swap_u8(struct component *component, uintptr_t addr, return swap(component, addr, sizeof(uint8_t), c, sizeof(uint8_t), c); } -#endif /* EXSIM_COMPONENT_H */ +#endif /* GRAN_COMPONENT_H */ diff --git a/include/gran/components/bus/simple_bus.h b/include/gran/components/bus/simple_bus.h deleted file mode 100644 index 3a5f814..0000000 --- a/include/gran/components/bus/simple_bus.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef EXSIM_SIMPLE_BUS_H -#define EXSIM_SIMPLE_BUS_H - -#include - -struct component *create_simple_bus(); -stat simple_bus_add(struct component *bus, struct component *component, - uintptr_t addr, size_t size); - -#endif /* EXSIM_SIMPLE_BUS_H */ diff --git a/include/gran/components/mem/simple_mem.h b/include/gran/components/mem/simple_mem.h deleted file mode 100644 index 8e0845c..0000000 --- a/include/gran/components/mem/simple_mem.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef EXSIM_SIMPLE_MEM_H -#define EXSIM_SIMPLE_MEM_H - -#include - -struct component *create_simple_mem(size_t size); - -#endif /* EXSIM_SIMPLE_MEM_H */ diff --git a/include/gran/cpu/riscv/simple_riscv32.h b/include/gran/cpu/riscv/simple_riscv32.h new file mode 100644 index 0000000..054c76d --- /dev/null +++ b/include/gran/cpu/riscv/simple_riscv32.h @@ -0,0 +1,8 @@ +#ifndef GRAN_SIMPLE_RISCV32 +#define GRAN_SIMPLE_RISCV32 + +#include + +struct component *create_simple_riscv32(uint32_t start_pc, struct component *imem, struct component *dmem); + +#endif /* GRAN_SIMPLE_RISCV */ diff --git a/include/gran/mem/simple_mem.h b/include/gran/mem/simple_mem.h new file mode 100644 index 0000000..984dbf6 --- /dev/null +++ b/include/gran/mem/simple_mem.h @@ -0,0 +1,8 @@ +#ifndef GRAN_SIMPLE_MEM_H +#define GRAN_SIMPLE_MEM_H + +#include + +struct component *create_simple_mem(size_t size); + +#endif /* GRAN_SIMPLE_MEM_H */ diff --git a/include/gran/root.h b/include/gran/root.h index d158b04..32ca1cf 100644 --- a/include/gran/root.h +++ b/include/gran/root.h @@ -1,5 +1,5 @@ -#ifndef EXSIM_ROOT_H -#define EXSIM_ROOT_H +#ifndef GRAN_ROOT_H +#define GRAN_ROOT_H #include #include @@ -11,4 +11,4 @@ stat root_add_clock(struct gran_root *, struct clock_domain *); stat root_run(struct gran_root *); void destroy_root(struct gran_root *); -#endif /* EXSIM_ROOT_H */ +#endif /* GRAN_ROOT_H */ diff --git a/src/components/bus/simple_bus.c b/src/components/bus/simple_bus.c index 23a6bc2..58e4132 100644 --- a/src/components/bus/simple_bus.c +++ b/src/components/bus/simple_bus.c @@ -5,7 +5,7 @@ #include #include -#include +#include struct mem_region { uintptr_t addr; diff --git a/src/components/cpu/riscv/simple_riscv32.c b/src/components/cpu/riscv/simple_riscv32.c new file mode 100644 index 0000000..1f19b1f --- /dev/null +++ b/src/components/cpu/riscv/simple_riscv32.c @@ -0,0 +1,58 @@ +#include + +struct simple_riscv32 { + struct component component; + + struct component *imem; + struct component *dmem; + + /* have to be careful with x0 */ + uint32_t regs[32]; + uint32_t pc; +}; + +static uint32_t get_reg(struct simple_riscv32 *cpu, size_t i) +{ + assert(i < 32); + + if (i == 0) + return 0; + + return cpu->regs[i]; +} + +static void set_reg(struct simple_riscv32 *cpu, size_t i, uint32_t v) +{ + assert(i < 32); + + if (i == 0) + return; + + cpu->regs[i] = v; +} + +static stat simple_riscv32_clock(struct simple_riscv32 *cpu) +{ + uint32_t insn = 0; + stat ret = read(cpu->imem, cpu->pc, sizeof(insn), &insn); + if (ret) + return ret; + + /* @todo instruction decode and execution, not sure if this is too early + * to start thinking about how to modularise stuff */ + return OK; +} + +struct componen *create_simple_riscv32(uint32_t start_pc, struct component *imem, struct component *dmem) +{ + struct component *new = calloc(1, sizeof(simple_riscv32)); + if (!new) + return NULL; + + new->component.clock = (clock_callback)simple_riscv32_clock; + + new->pc = start_pc; + new->imem = imem; + new->dmem = dmem; + return new; +} diff --git a/src/components/mem/simple_mem.c b/src/components/mem/simple_mem.c index bea68ed..52606e2 100644 --- a/src/components/mem/simple_mem.c +++ b/src/components/mem/simple_mem.c @@ -1,7 +1,7 @@ #include #include -#include +#include struct simple_mem { struct component component; diff --git a/tests/simple_mem/bus.c b/tests/simple_mem/bus.c index fa2ffad..67fb53b 100644 --- a/tests/simple_mem/bus.c +++ b/tests/simple_mem/bus.c @@ -2,8 +2,8 @@ #include #include -#include -#include +#include +#include #include "traffic_gen.h" diff --git a/tests/simple_mem/many.c b/tests/simple_mem/many.c index 6754ebb..254b49f 100644 --- a/tests/simple_mem/many.c +++ b/tests/simple_mem/many.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include "traffic_gen.h" diff --git a/tests/simple_mem/no_bus.c b/tests/simple_mem/no_bus.c index df7888e..8a5b05b 100644 --- a/tests/simple_mem/no_bus.c +++ b/tests/simple_mem/no_bus.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include "traffic_gen.h" diff --git a/tests/simple_mem/traffic_gen.h b/tests/simple_mem/traffic_gen.h index 9875bae..1975e48 100644 --- a/tests/simple_mem/traffic_gen.h +++ b/tests/simple_mem/traffic_gen.h @@ -1,9 +1,9 @@ -#ifndef EXSIM_TRAFFIC_GEN_H -#define EXSIM_TRAFFIC_GEN_H +#ifndef GRAN_TRAFFIC_GEN_H +#define GRAN_TRAFFIC_GEN_H #include #include struct component *create_traffic_gen(struct component *, uintptr_t, size_t); -#endif /* EXSIM_TRAFFIC_GEN_H */ +#endif /* GRAN_TRAFFIC_GEN_H */ -- cgit v1.3