-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRRAM_Controller.cpp
More file actions
184 lines (172 loc) · 4.83 KB
/
RRAM_Controller.cpp
File metadata and controls
184 lines (172 loc) · 4.83 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "RRAM_Controller.h"
void RRAM_Controller::enable_read(void)
{
ren = ren_p->read();
if (ren_low == false && ren == SC_LOGIC_0)
{
cout << "Read enable set low at time " << sc_time_stamp() << endl;
ren_low = true;
begin_read_index.notify();
}
else if (ren_low == true && ren == SC_LOGIC_1)
{
cout << "Read enable set high at time " << sc_time_stamp() << endl;
ren_low = false;
}
next_trigger(ren_p->default_event() & clk_in_p->posedge_event());
}
void RRAM_Controller::enable_write(void)
{
wen = wen_p->read();
if (wen_low == false && wen == SC_LOGIC_0)
{
cout << "Write enable set low at time " << sc_time_stamp() << endl;
begin_RRAM_write.notify();
wen_low = true;
}
else if (wen_low == true && wen == SC_LOGIC_1)
{
cout << "Write enable set high at time " << sc_time_stamp() << endl;
wen_low = false;
}
next_trigger(wen_p->default_event() & clk_in_p->posedge_event());
}
void RRAM_Controller::read_index(void)
{
for(;;)
{
int i=0;
wait(SC_ZERO_TIME);
cout << "Reading index byte "<< endl;
index = index_p->read();
cout << "Read index byte successfully as " << index << " at time " << sc_time_stamp() << endl;
begin_RRAM_read.notify();
wait();
}
}
void RRAM_Controller::RRAM_read(void)
{
for(;;)
{
wait(begin_RRAM_read);
cout << "Starting RRAM read operation" << endl;
wait(clk_in_p.posedge_event());
wait(time_RRAM_cs_hold);
cs_p->write(sc_bit('0'));
sc_bv<8> read_ins =RRAM_instruction_read;
for (int i=0;i<8;i++)
{
wait(clk_in_p.posedge_event());
sc_logic r = (sc_logic)read_ins[i];
bit_out_p->write(r);
}
sc_uint<8> index_int = index;
sc_uint<8> para_add_int = para_add;
sc_uint<16> add_int = index_int*para_add_int;
sc_lv<16> address = add_int;
for (int i=0;i<8;i++)
{
wait(clk_in_p->posedge_event());
bit_out_p->write(sc_logic('0'));
}
for (int i=0;i<16;i++)
{
wait(clk_in_p->posedge_event());
sc_logic abit = (sc_logic) address[i];
bit_out_p->write(abit);
}
sc_lv<8> high_impedance = "ZZZZZZZZ";
int num_read_bits = 8*num_read_bytes;
for (int i=0;i<num_read_bits;i++)
{
sc_logic d = sc_logic('0');
wait(clk_in_p->negedge_event());
if (i==0) bit_out_p->write(sc_logic('Z'));
data_valid_p->write(sc_bit('0'));
wait(SC_ZERO_TIME);
d= bit_in_p->read();
cout << "Read bit " << i%8+1 << " of byte " << i/8+1 << " at time " << sc_time_stamp() << " as " << d << endl;
data[i%8] = d;
if (i%8 == 7)
{
cout << "Data valid changing to 1" << endl;
data_valid_p->write(sc_bit('1'));
data_read_p->write(data);
}
else
{
data_read_p->write(high_impedance);
}
}
wait(clk_in_p->posedge_event());
done_p->write(true);
data_read_p->write(high_impedance);
data_valid_p->write(sc_bit('0'));
wait(time_RRAM_cs_hold);
cout << "Chip select changing value at time " << sc_time_stamp() << endl;
cs_p->write(sc_bit('1'));
cout << "Exiting read operation" << endl;
wait(clk_in_p->negedge_event());
done_p->write(false);
}
}
void RRAM_Controller::RRAM_write(void)
{
for(;;)
{
wait(begin_RRAM_write);
cout << "Write enable set low, starting RRAM write operation by opening file" << endl;
fileReader.open("weights.txt");
int num_lines = 0;
int weight = 0;
sc_bv<32> data_sc_bv = 0;
fileReader >> num_lines;
wait(clk_in_p->posedge_event());
wait(5,SC_NS);
cout << "Driving CS low" << endl;
cout << "Write 0: " << sc_time_stamp() << endl;
cs_p->write(sc_bit('0'));
cout << "CS written low by controller" << endl;
sc_bv<8> write_enable_ins = "01100000";
for (int i=0;i<8;i++){
wait();
sc_logic r = (sc_logic)write_enable_ins[i];
bit_out_p->write(r);
}
cout << "Write enable instruction written" << endl;
wait(5,SC_NS);
cout << "Write 1: " << sc_time_stamp() << endl;
cs_p->write(sc_bit('1'));
wait(clk_in_p->posedge_event());
wait(5,SC_NS);
cout << "Write 2: " << sc_time_stamp() << endl;
cs_p->write(sc_bit('0'));
sc_bv<8> write_ins = "01000000";
for (int i=0;i<8;i++){
wait(clk_in_p->posedge_event());
sc_logic r = (sc_logic)write_ins[i];
bit_out_p->write(r);
}
for (int i=0;i<24;i++){
wait(clk_in_p->posedge_event());
bit_out_p->write(sc_logic('0'));
}
cout << "Address written to RRAM" << endl;
for (int i=0; i<num_lines ; i++)
{
fileReader >> weight;
data_sc_bv = weight;
cout << "Weight " << i+1 << " " << data_sc_bv << endl;
for (int j=0;j<32;j++)
{
wait(clk_in_p->posedge_event());
bit_out_p->write(sc_logic(data_sc_bv[j]));
}
cout << "Weight " << i+1 << " written to RRAM" << endl;
}
wait(5,SC_NS);
cout << "Write 3: " << sc_time_stamp() << endl;
cs_p->write(sc_bit('1'));
fileReader.close();
}
}