aboutsummaryrefslogtreecommitdiffstats
path: root/sim.cpp
blob: 7edf0bf72141af1555f86c5bb8f3f7209e166e74 (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
#include <memory>
#include <limits>
#include <cstdint>
#include <iostream>
#include <verilated.h>
#include "Vboothmul.h"

using word = int16_t;
using dword = int32_t;

constexpr word minint = std::numeric_limits<word>::min();
constexpr word maxint = std::numeric_limits<word>::max();

uint32_t main_time = 0;

double sc_time_stamp() {
	return main_time;
}

Vboothmul *mod;

static void run_clock() {
	mod->clk = !mod->clk;
	mod->eval();
	main_time++;

	mod->clk = !mod->clk;
	mod->eval();
	main_time++;
}

static void run(word i, word j) {
	// Processor is twos-compliment
	mod->a1 = i;
	mod->a2 = j;
	mod->arm = 1;

	while (!mod->fin)
		run_clock();

	dword expected = (dword) i * (dword) j;
	if (mod->outn != expected) {
		std::cout << i << "*" << j << "=" << expected
		          << "(" << mod->outn << ")" << std::endl;
	}

	mod->arm = 0;
	run_clock();
}

int main(int argc, char **argv) {
	Verilated::commandArgs(argc, argv);
	// Verilated::traceEverOn(true);
	mod = new Vboothmul;

	mod->clk = 0;
	mod->arm = 0;
	run_clock();

	run(minint, minint);
	run(minint, maxint);
	run(maxint, minint);
	run(maxint, maxint);

	for (word i = -20; i < 20; i++) {
		for (word j = - 20; j < 20; j++) {
			run(i, j);
		}
	}

	mod->final();

	delete mod;
	std::cout << "done" << std::endl;

	return 0;
}