-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMatMulDemo.cpp
103 lines (84 loc) · 2.01 KB
/
MatMulDemo.cpp
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
#include <stdlib.h>
#include <iostream>
#include <verilated.h>
#include <verilated_vcd_c.h>
#include "VMatMul.h"
// These "eval" calls are unique to pure-software simulators
// and aren't needed with SystemVerilog simulators because
// they can intelligently propagate signals (but trying to
// do the same propagation with this kind of simulation would
// be far less efficient)
void step(VMatMul *dut) {
dut->eval();
dut->clk = 1;
dut->eval();
dut->clk = 0;
dut->eval();
}
void load_mtx(VMatMul *dut, int mtx[2][2]) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
dut->mtx_in[i][j] = mtx[i][j];
}
}
}
void load_vec(VMatMul *dut, int vec[2]) {
for (int i = 0; i < 2; i++) {
dut->vec_in[i] = vec[i];
}
}
void run_matmul(VMatMul *dut) {
dut->start = 1;
step(dut);
dut->start = 0;
while (!dut->done) step(dut);
}
void print_result(VMatMul *dut) {
printf("{ ");
for (int i = 0; i < 2; i++) {
printf("%d ", dut->vec_out[i]);
}
printf("}\n");
}
void assert_result(VMatMul *dut, int vec[2]) {
for (int i = 0; i < 2; i++) {
assert(dut->vec_out[i] == vec[i]);
}
}
int main() {
printf("hello\n");
VMatMul *dut = new VMatMul();
dut->start = 0;
dut->reset = 1;
step(dut);
step(dut);
dut->reset = 0;
// Everything abstracted away into utility functions, really easy
// to do in C++
int mtx[2][2] = {
{1, 2},
{3, 4}
};
load_mtx(dut, mtx);
int vec[2] = {1, 2};
load_vec(dut, vec);
run_matmul(dut);
printf("result = ");
print_result(dut);
int expected[2] = {5, 11};
assert_result(dut, expected);
// Do everything one more time :>
int mtx1[2][2] = {
{5, 6},
{7, 8}
};
load_mtx(dut, mtx1);
int vec1[2] = {3, 4};
load_vec(dut, vec1);
run_matmul(dut);
printf("result = ");
print_result(dut);
int expected1[2] = {39, 53};
assert_result(dut, expected1);
return 0;
}