From b1d67364b4b4832b8efed112f7b0ead1a0b3cd3b Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sun, 22 Sep 2024 22:45:18 +0300 Subject: start experimenting with processor grids --- tests/simple_grid/sim.c | 114 +++++++++++++++++++++++++++++++++++++++++ tests/simple_grid/source.mk | 6 +++ tests/simple_grid/test.c | 43 ++++++++++++++++ tests/simple_riscv32/s.asm | 16 ------ tests/simple_riscv32/sim.c | 38 -------------- tests/simple_riscv32/source.mk | 6 --- tests/simple_riscv64/s.asm | 16 ++++++ tests/simple_riscv64/sim.c | 38 ++++++++++++++ tests/simple_riscv64/source.mk | 6 +++ tests/simple_uart/hello.c | 18 +++++++ tests/simple_uart/sim.c | 45 ++++++++++++++++ tests/simple_uart/source.mk | 6 +++ 12 files changed, 292 insertions(+), 60 deletions(-) create mode 100644 tests/simple_grid/sim.c create mode 100644 tests/simple_grid/source.mk create mode 100644 tests/simple_grid/test.c delete mode 100644 tests/simple_riscv32/s.asm delete mode 100644 tests/simple_riscv32/sim.c delete mode 100644 tests/simple_riscv32/source.mk create mode 100644 tests/simple_riscv64/s.asm create mode 100644 tests/simple_riscv64/sim.c create mode 100644 tests/simple_riscv64/source.mk create mode 100644 tests/simple_uart/hello.c create mode 100644 tests/simple_uart/sim.c create mode 100644 tests/simple_uart/source.mk (limited to 'tests') diff --git a/tests/simple_grid/sim.c b/tests/simple_grid/sim.c new file mode 100644 index 0000000..e0c485b --- /dev/null +++ b/tests/simple_grid/sim.c @@ -0,0 +1,114 @@ +/* testcase for a 64x64 grid of processors, that all just spam the first memory + * region due to there being a 'lock' variable there, more or less the worst + * possible program for performance. */ +#include + +#include +#include +#include +#include +#include +#include +#include + +unsigned char test_grid[] = { + 0xb3, 0x67, 0xb5, 0x00, 0x63, 0x9a, 0x07, 0x0a, 0xb7, 0x17, 0x00, 0x00, + 0x23, 0xb0, 0x07, 0x00, 0x1b, 0x57, 0x35, 0x00, 0xb7, 0x27, 0x00, 0x00, + 0x93, 0x06, 0x80, 0x02, 0x13, 0x77, 0x77, 0x00, 0x23, 0x80, 0xd7, 0x00, + 0x13, 0x07, 0x07, 0x03, 0x93, 0x76, 0x75, 0x00, 0x23, 0x80, 0xe7, 0x00, + 0x13, 0x87, 0x06, 0x03, 0x23, 0x80, 0xe7, 0x00, 0x93, 0x06, 0xc0, 0x02, + 0x1b, 0xd7, 0x35, 0x00, 0x23, 0x80, 0xd7, 0x00, 0x13, 0x77, 0x77, 0x00, + 0x93, 0x06, 0x00, 0x02, 0x23, 0x80, 0xd7, 0x00, 0x13, 0x07, 0x07, 0x03, + 0x93, 0xf6, 0x75, 0x00, 0x23, 0x80, 0xe7, 0x00, 0x13, 0x87, 0x06, 0x03, + 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0x90, 0x02, 0x23, 0x80, 0xe7, 0x00, + 0x93, 0x06, 0xa0, 0x00, 0x23, 0x80, 0xd7, 0x00, 0x1b, 0x87, 0x15, 0x00, + 0x93, 0x06, 0xf0, 0x03, 0x93, 0x77, 0xf7, 0x0f, 0x63, 0x98, 0xd5, 0x00, + 0x1b, 0x05, 0x15, 0x00, 0x13, 0x75, 0xf5, 0x0f, 0x93, 0x07, 0x00, 0x00, + 0x13, 0x15, 0x85, 0x00, 0x33, 0x65, 0xf5, 0x00, 0xb7, 0x17, 0x00, 0x00, + 0x23, 0xb0, 0xa7, 0x00, 0xb7, 0x16, 0x00, 0x00, 0x37, 0x47, 0x00, 0x00, + 0x83, 0xb7, 0x06, 0x00, 0xe3, 0x9e, 0xe7, 0xfe, 0x73, 0x00, 0x10, 0x00, + 0x67, 0x80, 0x00, 0x00, 0x13, 0x17, 0x85, 0x00, 0x33, 0x67, 0xb7, 0x00, + 0xb7, 0x16, 0x00, 0x00, 0x83, 0xb7, 0x06, 0x00, 0xe3, 0x9e, 0xe7, 0xfe, + 0x6f, 0xf0, 0x5f, 0xf4 +}; +unsigned int test_grid_len = 208; + +static struct component *get_grid(struct component **grid, int i, int j, uint8_t x, uint8_t y) +{ + if (i < 0) + return NULL; + + if (j < 0) + return NULL; + + if (i >= x) + return NULL; + + if (j >= y) + return NULL; + + return grid[i * x + j]; +} + +static stat build_grid(struct clock_domain *clk, uint8_t x, uint8_t y) +{ + struct component **grid = calloc(x * y, sizeof(struct component *)); + assert(grid); + + struct component **routers = calloc(x * y, sizeof(struct component *)); + assert(routers); + + for (size_t i = 0; i < x; ++i) + for (size_t j = 0; j < y; ++j) { + struct component *imem = create_simple_mem(4096); + init_simple_mem(imem, 0, test_grid_len, test_grid); + + struct component *dmem = create_simple_mem(4096); + struct component *router = create_node_router(0, 0, i, j); + node_router_add(router, dmem, 4096, 4096); + + struct component *rv64 = create_simple_riscv64(0, imem, router); + simple_riscv64_set_reg(rv64, 10, i); /* a0 */ + simple_riscv64_set_reg(rv64, 11, j); /* a1 */ + + clock_domain_add(clk, rv64); + + routers[i * x + j] = router; + grid[i * x + j] = create_grid_node(0, 0, i, j); + } + + struct component *uart = create_simple_uart(); + node_router_add(routers[0], uart, 8192, 1); + + for (int i = 0; i < x; ++i) + for (int j = 0; j < y; ++j) { + struct component *node = grid[i * x + j]; + struct component *lower = routers[i * x + j]; + struct component *left = get_grid(grid, i - 1, j , x, y); + struct component *right = get_grid(grid, i + 1, j , x, y); + struct component *up = get_grid(grid, i , j + 1, x, y); + struct component *down = get_grid(grid, i , j - 1, x, y); + grid_node_connect(node, left, right, up, down, lower, NULL); + node_router_ascend(lower, node); + } + + free(routers); + free(grid); + return OK; +} + +int main() +{ + struct clock_domain *clk = create_clock_domain(NS(1)); + + stat r = build_grid(clk, 64, 64); + assert(r == OK); + + struct gran_root *root = create_root(); + root_add_clock(root, clk); + + r = root_run(root); + assert(r == OK); + + destroy_root(root); +} diff --git a/tests/simple_grid/source.mk b/tests/simple_grid/source.mk new file mode 100644 index 0000000..e2dd442 --- /dev/null +++ b/tests/simple_grid/source.mk @@ -0,0 +1,6 @@ +GRID_TEST_OBJ != ./scripts/gen-deps --sources "tests/simple_grid/sim.c" + +TEST_PROGS += build/tests/simple_grid/sim + +build/tests/simple_grid/sim: $(GRID_TEST_OBJ) $(OBJS) + $(COMPILE) $(GRID_TEST_OBJ) $(OBJS) -o $@ diff --git a/tests/simple_grid/test.c b/tests/simple_grid/test.c new file mode 100644 index 0000000..782062d --- /dev/null +++ b/tests/simple_grid/test.c @@ -0,0 +1,43 @@ +#define X 64 +#define Y 64 + +void _start(unsigned char x, unsigned char y) +{ + volatile unsigned long *counter = (unsigned long *)4096; + volatile char *uart = (char *)8192; + + /* very hacky, not recommended but good enough for testing */ + if (x == 0 && y == 0) + *counter = 0; + else + while (*counter != ((x << 8) | y)) + ; + + *uart = '('; + /* [0 - 63] as two octal numbers */ + *uart = ((x >> 3) & 0x7) + '0'; + *uart = ((x >> 0) & 0x7) + '0'; + + *uart = ','; + *uart = ' '; + + *uart = ((y >> 3) & 0x7) + '0'; + *uart = ((y >> 0) & 0x7) + '0'; + + *uart = ')'; + *uart = '\n'; + + if (y == 63) { + x++; + y = 0; + } + else + y++; + + *counter = (x << 8) | y; + + while (*counter != (64 << 8)) + ; + + asm ("ebreak\n"); +} diff --git a/tests/simple_riscv32/s.asm b/tests/simple_riscv32/s.asm deleted file mode 100644 index d423dd9..0000000 --- a/tests/simple_riscv32/s.asm +++ /dev/null @@ -1,16 +0,0 @@ -.global _start -_start: -/* sum from 0 to 1000000 */ - -li a0, 0 /* index */ -li a1, 1000000 /* top */ -li a2, 0 /* sum */ - -top: -beq a0, a1, done -add a2, a2, a0 -addi a0, a0, 1 -j top - -done: -ebreak diff --git a/tests/simple_riscv32/sim.c b/tests/simple_riscv32/sim.c deleted file mode 100644 index fcf19a1..0000000 --- a/tests/simple_riscv32/sim.c +++ /dev/null @@ -1,38 +0,0 @@ -/* SPDX-License-Identifier: copyleft-next-0.3.1 */ -/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ - -#include - -#include -#include -#include - -unsigned char simple_sum[] = { - 0x13, 0x05, 0x00, 0x00, 0xb7, 0x45, 0x0f, 0x00, 0x93, 0x85, 0x05, 0x24, - 0x13, 0x06, 0x00, 0x00, 0x63, 0x08, 0xb5, 0x00, 0x33, 0x06, 0xa6, 0x00, - 0x13, 0x05, 0x15, 0x00, 0x6f, 0xf0, 0x5f, 0xff, 0x73, 0x00, 0x10, 0x00 -}; - -unsigned int simple_sum_len = 36; - -int main() -{ - const size_t size = 100000; - struct component *imem = create_simple_mem(size); - - /* This works for simple memory, but feels kind of hacky */ - init_simple_mem(imem, 0, simple_sum_len, simple_sum); - - struct component *dmem = create_simple_mem(size); - struct component *rv32 = create_simple_riscv32(0, imem, dmem); - - struct clock_domain *clk = create_clock_domain(NS(1)); - clock_domain_add(clk, rv32); - - struct gran_root *root = create_root(); - root_add_clock(root, clk); - - assert(root_run(root) == OK); - - destroy_root(root); -} diff --git a/tests/simple_riscv32/source.mk b/tests/simple_riscv32/source.mk deleted file mode 100644 index 736c111..0000000 --- a/tests/simple_riscv32/source.mk +++ /dev/null @@ -1,6 +0,0 @@ -RV32_TEST_OBJ != ./scripts/gen-deps --sources "tests/simple_riscv32/sim.c" - -TEST_PROGS += build/tests/simple_riscv32/sim - -build/tests/simple_riscv32/sim: $(RV32_TEST_OBJ) $(TEST_OBJS) $(OBJS) - $(COMPILE) $(RV32_TEST_OBJ) $(TEST_OBJS) $(OBJS) -o $@ diff --git a/tests/simple_riscv64/s.asm b/tests/simple_riscv64/s.asm new file mode 100644 index 0000000..d423dd9 --- /dev/null +++ b/tests/simple_riscv64/s.asm @@ -0,0 +1,16 @@ +.global _start +_start: +/* sum from 0 to 1000000 */ + +li a0, 0 /* index */ +li a1, 1000000 /* top */ +li a2, 0 /* sum */ + +top: +beq a0, a1, done +add a2, a2, a0 +addi a0, a0, 1 +j top + +done: +ebreak diff --git a/tests/simple_riscv64/sim.c b/tests/simple_riscv64/sim.c new file mode 100644 index 0000000..ae3cad4 --- /dev/null +++ b/tests/simple_riscv64/sim.c @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +#include + +#include +#include +#include + +unsigned char simple_sum[] = { + 0x13, 0x05, 0x00, 0x00, 0xb7, 0x45, 0x0f, 0x00, 0x93, 0x85, 0x05, 0x24, + 0x13, 0x06, 0x00, 0x00, 0x63, 0x08, 0xb5, 0x00, 0x33, 0x06, 0xa6, 0x00, + 0x13, 0x05, 0x15, 0x00, 0x6f, 0xf0, 0x5f, 0xff, 0x73, 0x00, 0x10, 0x00 +}; + +unsigned int simple_sum_len = 36; + +int main() +{ + const size_t size = 100000; + struct component *imem = create_simple_mem(size); + + /* This works for simple memory, but feels kind of hacky */ + init_simple_mem(imem, 0, simple_sum_len, simple_sum); + + struct component *dmem = create_simple_mem(size); + struct component *rv64 = create_simple_riscv64(0, imem, dmem); + + struct clock_domain *clk = create_clock_domain(NS(1)); + clock_domain_add(clk, rv64); + + struct gran_root *root = create_root(); + root_add_clock(root, clk); + + assert(root_run(root) == OK); + + destroy_root(root); +} diff --git a/tests/simple_riscv64/source.mk b/tests/simple_riscv64/source.mk new file mode 100644 index 0000000..fbc541d --- /dev/null +++ b/tests/simple_riscv64/source.mk @@ -0,0 +1,6 @@ +RV64_TEST_OBJ != ./scripts/gen-deps --sources "tests/simple_riscv64/sim.c" + +TEST_PROGS += build/tests/simple_riscv64/sim + +build/tests/simple_riscv64/sim: $(RV64_TEST_OBJ) $(TEST_OBJS) $(OBJS) + $(COMPILE) $(RV64_TEST_OBJ) $(TEST_OBJS) $(OBJS) -o $@ diff --git a/tests/simple_uart/hello.c b/tests/simple_uart/hello.c new file mode 100644 index 0000000..30719e0 --- /dev/null +++ b/tests/simple_uart/hello.c @@ -0,0 +1,18 @@ +/* compile with + * riscv64-unknown-elf-gcc -ffreestanding -nostdlib -Wl,-Ttext=0 -march=rv64i -mabi=lp64 -O2 hello.c -o simple_hello + * xxd --include simple_hello + */ +void _start() +{ + /* relies on hard-coded system, good enough for this simple test */ + volatile char *uart = (char *)4096; + *uart = 'H'; + *uart = 'e'; + *uart = 'l'; + *uart = 'l'; + *uart = 'o'; + *uart = '!'; + *uart = '\n'; + + asm ("ebreak\n"); +} diff --git a/tests/simple_uart/sim.c b/tests/simple_uart/sim.c new file mode 100644 index 0000000..762b591 --- /dev/null +++ b/tests/simple_uart/sim.c @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: copyleft-next-0.3.1 */ +/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */ + +#include + +#include +#include +#include +#include +#include + +unsigned char simple_hello[] = { + 0xb7, 0x17, 0x00, 0x00, 0x13, 0x07, 0x80, 0x04, 0x23, 0x80, 0xe7, 0x00, + 0x13, 0x07, 0x50, 0x06, 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0xc0, 0x06, + 0x23, 0x80, 0xe7, 0x00, 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0xf0, 0x06, + 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0x10, 0x02, 0x23, 0x80, 0xe7, 0x00, + 0x13, 0x07, 0xa0, 0x00, 0x23, 0x80, 0xe7, 0x00, 0x73, 0x00, 0x10, 0x00, + 0x67, 0x80, 0x00, 0x00 +}; +unsigned int simple_hello_len = 64; + +int main() +{ + const size_t size = 4096; + struct component *imem = create_simple_mem(size); + init_simple_mem(imem, 0, simple_hello_len, simple_hello); + + struct component *dmem = create_simple_mem(size); + struct component *uart = create_simple_uart(); + struct component *bus = create_simple_bus(); + simple_bus_add(bus, dmem, 0, size); + simple_bus_add(bus, uart, size, 1); + + struct component *rv64 = create_simple_riscv64(0, imem, bus); + + struct clock_domain *clk = create_clock_domain(NS(1)); + clock_domain_add(clk, rv64); + + struct gran_root *root = create_root(); + root_add_clock(root, clk); + + assert(root_run(root) == OK); + + destroy_root(root); +} diff --git a/tests/simple_uart/source.mk b/tests/simple_uart/source.mk new file mode 100644 index 0000000..a42a447 --- /dev/null +++ b/tests/simple_uart/source.mk @@ -0,0 +1,6 @@ +UART_TEST_OBJ != ./scripts/gen-deps --sources "tests/simple_uart/sim.c" + +TEST_PROGS += build/tests/simple_uart/sim + +build/tests/simple_uart/sim: $(UART_TEST_OBJ) $(OBJS) + $(COMPILE) $(UART_TEST_OBJ) $(TEST_OBJS) $(OBJS) -o $@ -- cgit v1.3