aboutsummaryrefslogtreecommitdiff
path: root/tests/simple_grid/sim.c
blob: 519599e3007d52afe9f79ec967b71edbd871c1bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 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 <assert.h>

#include <gran/root.h>
#include <gran/mem/simple_mem.h>
#include <gran/bus/simple_bus.h>
#include <gran/uart/simple_uart.h>
#include <gran/grid/node.h>
#include <gran/cpu/riscv/simple_riscv64.h>

unsigned char _tmp_test_bin[] = {
  0x1b, 0x86, 0x05, 0x00, 0x63, 0x18, 0x05, 0x00, 0x93, 0x07, 0x10, 0x00,
  0x1b, 0x86, 0x05, 0x00, 0x63, 0x88, 0xf5, 0x0c, 0x93, 0x16, 0x85, 0x00,
  0x37, 0x17, 0x00, 0x10, 0xb3, 0xe6, 0xb6, 0x00, 0x13, 0x17, 0x47, 0x01,
  0x83, 0x37, 0x07, 0x00, 0xe3, 0x9e, 0xd7, 0xfe, 0x1b, 0x57, 0x35, 0x00,
  0xb7, 0x17, 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, 0x13, 0x07, 0xa0, 0x00, 0x23, 0x80, 0xe7, 0x00,
  0x93, 0x07, 0xf0, 0x03, 0x63, 0x0c, 0xf6, 0x02, 0x9b, 0x85, 0x15, 0x00,
  0x93, 0x95, 0x05, 0x03, 0x93, 0xd5, 0x05, 0x03, 0x13, 0x15, 0x85, 0x00,
  0x37, 0x17, 0x00, 0x10, 0x33, 0x65, 0xb5, 0x00, 0x13, 0x17, 0x47, 0x01,
  0x23, 0x30, 0xa7, 0x00, 0xb7, 0x46, 0x00, 0x00, 0x83, 0x37, 0x07, 0x00,
  0xe3, 0x9e, 0xd7, 0xfe, 0x73, 0x00, 0x10, 0x00, 0x67, 0x80, 0x00, 0x00,
  0x1b, 0x05, 0x15, 0x00, 0x13, 0x15, 0x05, 0x03, 0x13, 0x55, 0x05, 0x03,
  0x93, 0x05, 0x00, 0x00, 0x6f, 0xf0, 0x9f, 0xfc, 0xb7, 0x17, 0x00, 0x10,
  0x93, 0x97, 0x47, 0x01, 0x23, 0xb0, 0x07, 0x00, 0x13, 0x07, 0x00, 0x03,
  0xb7, 0x17, 0x00, 0x00, 0x23, 0x80, 0xe7, 0x00, 0x13, 0x07, 0xa0, 0x00,
  0x23, 0x80, 0xe7, 0x00, 0x6f, 0xf0, 0xdf, 0xf2
};
unsigned int _tmp_test_bin_len = 260;

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 **pes = calloc(x * y, sizeof(struct component *));
	assert(pes);

	for (size_t i = 0; i < x; ++i)
	for (size_t j = 0; j < y; ++j) {
		struct component *node = create_grid_node(i, j);
		clock_domain_add(clk, node);
		grid[i * x + j] = node;

		if (i == 0 && j == 0)
			continue;

		if (i == 1 && j == 1)
			continue;

		struct component *imem = create_simple_mem(4096);
		init_simple_mem(imem, 0, _tmp_test_bin_len, _tmp_test_bin);

		uint64_t rcv = grid_addr(i, j, 0);
		struct component *rv64 = create_simple_riscv64(rcv, 0, imem, node);
		simple_riscv64_set_reg(rv64, 10, i); /* a0 */
		simple_riscv64_set_reg(rv64, 11, j); /* a1 */

		clock_domain_add(clk, rv64);
		clock_domain_add(clk, imem);

		pes[i * x + j] = rv64;
	}

	struct component *uart = create_simple_uart();
	clock_domain_add(clk, uart);
	grid_node_connect(grid[0], NULL, grid[x], grid[1], NULL, uart);

	struct component *dmem = create_simple_mem(4096);
	clock_domain_add(clk, dmem);
	grid_node_connect(grid[x + 1], grid[1], grid[2 * x + 1], grid[x + 2], grid[x], dmem);

	for (int i = 0; i < x; ++i)
	for (int j = 0; j < y; ++j) {
		if (i == 0 && j == 0)
			continue;

		if (i == 1 && j == 1)
			continue;

		struct component *node  = grid[i * x + j];
		struct component *lower = pes[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);
	}

	free(pes);
	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);
}