-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.cc
More file actions
77 lines (65 loc) · 1.69 KB
/
counter.cc
File metadata and controls
77 lines (65 loc) · 1.69 KB
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
#include <systemc.h>
#include <cstddef>
#include <cstdlib>
#include <cstdint>
#include <cassert>
/* For every clock signal, increment a counter by one. */
SC_MODULE(Counter) {
static constexpr size_t NUM_BITS = 3;
static constexpr uint64_t MAX = (1 << (NUM_BITS - 1)) | ((1 << (NUM_BITS - 1)) - 1);
static_assert(NUM_BITS <= 64, "Would need larger internal type");
/* Ports: */
sc_in<bool> Clock;
sc_in<bool> Reset;
sc_in<bool> Enable;
sc_out<sc_uint<NUM_BITS>> Result;
/* Internals: */
uint64_t Count;
/* Could be called whatever: */
void tick() {
/* Reset if resetting... */
if (Reset.read()) {
Count = 0;
Result.write(0);
return;
}
/* Skip most stuff if not enabled... */
if (!Enable.read())
return;
if (Count == MAX)
Count = 0;
else
Count += 1;
Result.write(Count);
}
/* SystemC magic: Module setup */
SC_CTOR(Counter) {
/* Register the function as method. */
SC_METHOD(tick);
/* Update on every clock (rising or falling?) edge: */
sensitive << Clock.pos();
}
};
int sc_main(int argc, char **argv) {
(void) argc;
(void) argv;
sc_clock TestClock("test-clock", 1, SC_NS);
sc_signal<bool> Reset("reset", 1);
sc_signal<bool> Enable("enable", 0);
sc_signal<sc_uint<3>> Result("counter", 0xf);
Counter C("4BitCounter");
C.Clock(TestClock);
C.Reset(Reset);
C.Enable(Enable);
C.Result(Result);
sc_start(1, SC_NS);
Enable.write(1);
Reset.write(0);
uint64_t expexted_values[] = { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3 };
for (size_t i = 0; i < (sizeof(expexted_values) / sizeof(expexted_values[9])); i++) {
assert(uint64_t(Result.read()) == expexted_values[i]);
sc_start(1, SC_NS);
}
printf("Simulation successful!\n");
return EXIT_SUCCESS;
}