-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubleq.cpp
143 lines (128 loc) · 4.08 KB
/
subleq.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
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
135
136
137
138
139
140
141
142
143
#include <bitset>
#include <cassert>
#include <cstdint>
#include <fstream>
#include <iostream>
#include "Vsubleq.h"
#include "verilated.h"
#include "common.hpp"
template<
typename ADDRESS_TYPE,
unsigned int ADDRESS_BITS,
typename DATA_TYPE,
unsigned int DATA_BITS,
unsigned int NWORDS = (1 << ADDRESS_BITS),
unsigned int NBITS = (NWORDS * DATA_BITS)
>
class Ram : public std::bitset<NBITS> {
private:
/* http://stackoverflow.com/questions/17857596/how-to-convert-a-range-subset-of-bits-in-a-c-bitset-to-a-number?noredirect=1&lq=1
* http://stackoverflow.com/questions/2177186/in-bitset-can-i-use-to-ulong-for-a-specific-range-of-bits */
DATA_TYPE get_word(ADDRESS_TYPE address) {
DATA_TYPE ret = 0;
DATA_TYPE mask = 1;
auto base = address * DATA_BITS;
for (std::size_t i = 0; i < DATA_BITS; ++i) {
if (this->operator[](base + i))
ret |= mask;
mask <<= 1;
}
return ret;
}
void set_word(ADDRESS_TYPE address, DATA_TYPE data) {
DATA_TYPE ret = 0;
DATA_TYPE mask = 1;
auto base = address * DATA_BITS;
for (std::size_t i = 0; i < DATA_BITS; ++i) {
this->set(address + i, mask & data);
mask <<= 1;
}
}
void load(const char *buffer) {
DATA_TYPE mask = 1;
for (size_t bit = 0; bit < NBITS; ++bit) {
this->set(bit, buffer[bit >> 3] & (1 << (bit % 8)));
}
}
public:
using std::bitset<NBITS>::bitset;
void update(
ADDRESS_TYPE &address,
DATA_TYPE &data,
bool write
) {
if (write) {
this->set_word(address, data);
} else {
data = this->get_word(address);
}
//std::cout
//<< " " << unsigned(address)
//<< " " << unsigned(data)
//<< " " << unsigned(write)
//<< std::endl
//;
}
static constexpr decltype(ADDRESS_BITS) get_nbits() { return NBITS; }
};
template<unsigned int BITS>
class SubleqTestCase : public TestCase<Vsubleq> {
public:
typedef Ram<decltype(Vsubleq::address), BITS, decltype(Vsubleq::data), BITS> ram_t;
SubleqTestCase(
ram_t &ram,
std::string vcd_file_path
) :
TestCase(vcd_file_path),
ram(ram)
{}
virtual bool check() {
return true;
}
virtual void step(bool& finish) {
if (this->time == 0) {
this->dut->reset = 1;
} else if (this->time == 2) {
this->dut->reset = 0;
}
ram.update(
this->dut->address,
this->dut->data,
this->dut->write
);
this->dut->clock = this->clock;
if (this->dut->halt || this->time == 64) {
finish = true;
}
}
static constexpr decltype(ram_t::get_nbits()) get_nbits() {
return ram_t::get_nbits();
}
private:
ram_t &ram;
};
int main(int argc, char **argv) {
Verilated::commandArgs(argc, argv);
constexpr unsigned int BITS = 4;
// Zero input. Immediate infinite loop.
{
SubleqTestCase<BITS>::ram_t ram0, ram;
assert(SubleqTestCase<BITS>(ram, "subleq_zero.cpp.vcd").run());
assert(ram == ram0);
}
// 2 state oscillator. Never halts, but never does anything either.
{
SubleqTestCase<BITS>::ram_t ram0(
"0000" "0000" "0000"
"0011" "0000" "0000"
//"0101" "0100" "0011"
//"0010" "0001" "0000"
//"1111" "1111" "1111"
//"1111" "1111" "1111"
);
auto ram = ram0;
assert(SubleqTestCase<BITS>(ram, "subleq_oscillator.cpp.vcd").run());
assert(ram == ram0);
}
//assert(SubleqTestCase<BITS>(buffer, "subleq_inc.cpp.vcd").run());
}