aboutsummaryrefslogtreecommitdiff
path: root/tests/simple_ideal_alloc/testbench.c
blob: 703c2a8ae65cc76a1d6cbc2fb9ed48084d6da0af (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
#include <gran/if/alloc.h>
#include "testbench.h"

struct reg {
	struct packet pkt;
	bool busy;
};

struct tb {
	struct component component;
	struct component *dut;
	struct reg in;

	enum {
		ADD_REGION,
		QUERY_EXISTING,
		REMOVE_REGION,
		QUERY_NONEXISTING,
		FINAL
	} state;
};

static stat tb_receive(struct tb *tb, struct component *from, struct packet pkt)
{
	assert(tb->dut == from);
	tb->in.pkt = pkt;
	tb->in.busy = true;
	return OK;
}

static stat tb_clock(struct tb *tb)
{
	assert(tb->dut);

	switch (tb->state) {
	case ADD_REGION: {
		struct alloc_if_new n = {.space = 0, .start = 0, .end = 10};
		struct packet pkt = create_packet(0, ALLOC_IF_NEW, sizeof(n), &n, PACKET_WRITE);
		assert(SEND(tb, tb->dut, pkt) == OK);
		tb->state = QUERY_EXISTING;
		break;
	}

	case QUERY_EXISTING: {
		if (!tb->in.busy)
			break;

		struct alloc_if_r *r = (struct alloc_if_r *)&tb->in.pkt.data;
		assert(r->r == ALLOC_IF_OK);

		struct alloc_if_q q = {.space = 0, .addr = 5};
		struct packet pkt = create_packet(0, ALLOC_IF_Q, sizeof(q), &q, PACKET_WRITE);
		assert(SEND(tb, tb->dut, pkt) == OK);
		tb->state = REMOVE_REGION;
		tb->in.busy = false;
		break;
	}

	case REMOVE_REGION: {
		if (!tb->in.busy)
			break;

		struct alloc_if_r *r = (struct alloc_if_r *)&tb->in.pkt.data;
		assert(r->r == ALLOC_IF_OK);
		assert(r->start == 0);
		assert(r->end == 10);

		struct alloc_if_rm rm = {.space = 0, .start = 0, .end = 10};
		struct packet pkt = create_packet(0, ALLOC_IF_RM, sizeof(rm), &rm, PACKET_WRITE);
		assert(SEND(tb, tb->dut, pkt) == OK);
		tb->state = QUERY_NONEXISTING;
		tb->in.busy = false;
		break;
	}

	case QUERY_NONEXISTING: {
		if (!tb->in.busy)
			break;

		struct alloc_if_r *r = (struct alloc_if_r *)&tb->in.pkt.data;
		assert(r->r == ALLOC_IF_OK);

		struct alloc_if_q q = {.space = 0, .addr = 5};
		struct packet pkt = create_packet(0, ALLOC_IF_Q, sizeof(q), &q, PACKET_WRITE);
		assert(SEND(tb, tb->dut, pkt) == OK);
		tb->state = FINAL;
		tb->in.busy = false;
		break;
	}

	case FINAL: {
		if (!tb->in.busy)
			break;

		struct alloc_if_r *r = (struct alloc_if_r *)&tb->in.pkt.data;
		assert(r->r != ALLOC_IF_OK);
		return DONE;
	}

	default: abort();
	}

	return OK;
}

struct component *create_testbench()
{
	struct tb *tb = calloc(1, sizeof(struct tb));
	tb->component.clock = (clock_callback)tb_clock;
	tb->component.receive = (receive_callback)tb_receive;
	return (struct component *)tb;
}

stat testbench_connect(struct component *tb, struct component *dut)
{
	struct tb *t = (struct tb *)tb;
	t->dut = dut;
	return OK;
}