-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_cpu.cpp
105 lines (80 loc) · 3.3 KB
/
test_cpu.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
104
105
#include <iostream>
#include <memory>
#include "Vgrom_cpu.h"
#include "verilated.h"
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
int main( int argc, char* argv[] )
{
Verilated::commandArgs(argc, argv);
int result = Catch::Session().run( argc, argv );
return ( result < 0xff ? result : 0xff );
}
TEST_CASE("Reset CPU", "CPU")
{
std::unique_ptr<Vgrom_cpu> top = std::make_unique<Vgrom_cpu>();
top->clk = 0;
top->reset = 1;
top->clk ^= 1; top->eval(); // mid clock
REQUIRE(top->hlt == 0);
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_RESET);
top->clk ^= 1; top->eval();
top->reset = 0;
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // one Clock
REQUIRE(top->grom_cpu__DOT__CS == 0);
REQUIRE(top->grom_cpu__DOT__DS == 0);
REQUIRE(top->grom_cpu__DOT__R[0] == 0);
REQUIRE(top->grom_cpu__DOT__R[1] == 0);
REQUIRE(top->grom_cpu__DOT__R[2] == 0);
REQUIRE(top->grom_cpu__DOT__R[3] == 0);
REQUIRE(top->grom_cpu__DOT__SP == 0xfff);
}
TEST_CASE("Fetch instruction", "CPU")
{
std::unique_ptr<Vgrom_cpu> top = std::make_unique<Vgrom_cpu>();
top->clk = 0;
top->data_in = 0x7f;
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_FETCH_PREP
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_FETCH_PREP);
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_FETCH_WAIT
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_FETCH_WAIT);
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_FETCH
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_FETCH);
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_EXECUTE
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_EXECUTE);
REQUIRE(top->grom_cpu__DOT__IR == 0x7f);
}
void executeSimpleInstruction(Vgrom_cpu *top)
{
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_FETCH_PREP
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_FETCH_PREP);
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_FETCH_WAIT
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_FETCH_WAIT);
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_FETCH
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_FETCH);
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_EXECUTE
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_EXECUTE);
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_FETCH_PREP
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_FETCH_PREP);
}
TEST_CASE("HLT instruction", "CPU")
{
std::unique_ptr<Vgrom_cpu> top = std::make_unique<Vgrom_cpu>();
top->clk = 0;
top->data_in = 0x7f; // HLT
executeSimpleInstruction(top.get());
REQUIRE(top->hlt == 1);
}
TEST_CASE("After HLT cpu should remain in loop", "CPU")
{
std::unique_ptr<Vgrom_cpu> top = std::make_unique<Vgrom_cpu>();
top->clk = 0;
top->data_in = 0x7f; // HLT
executeSimpleInstruction(top.get());
REQUIRE(top->hlt == 1);
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_FETCH_PREP);
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_FETCH_WAIT
REQUIRE(top->grom_cpu__DOT__state == top->grom_cpu__DOT__STATE_FETCH_WAIT);
top->clk ^= 1; top->eval(); top->clk ^= 1; top->eval(); // STATE_FETCH
REQUIRE(top->hlt == 1);
}