-
Notifications
You must be signed in to change notification settings - Fork 0
/
hcmpp.v
executable file
·317 lines (259 loc) · 14.1 KB
/
hcmpp.v
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
`timescale 1ns / 1ps
module HCMPP(
input clk,
input [ROWINDEXBITS_HCM-1:0] inputRowToWrite,
input writeRow,
input SSIDIsNew,
input [ROWINDEXBITS_HCM-1:0] inputRowToRead,
input readRow,
input [HITINFOBITS-1:0] inputHitInfo,
input reset,
output reg writeReady,
output reg readReady,
output reg [ROWINDEXBITS_HCM-1:0] rowPassed,
output reg [MAXHITNBITS-1:0] nOldHits,
output reg [MAXHITNBITS-1:0] nNewHits,
output reg [MAXHITNBITS-1:0] readNHits,
output reg [ROWINDEXBITS_HIM-1:0] readHIM_address,
output reg [ROWINDEXBITS_HIM-1:0] HIM_address,
output reg [NCOLS_HIM-1:0] outputNewHitInfo,
output reg newOutput,
output reg readFinished,
output reg busy
);
`include "MyParameters.vh"
//-------------//
// HCM BRAM IP //
//-------------//
reg writeToBRAM = 0;
reg [ROWINDEXBITS_HCM-1:0] rowToWrite;
reg [NCOLS_HCM-1:0] dataToWrite;
reg [ROWINDEXBITS_HCM-1:0] rowToRead;
wire [NCOLS_HCM-1:0] dataRead;
wire [NCOLS_HCM-1:0] dummyRead;
reg [NCOLS_HCM-1:0] dummyWrite = 0;
hcmpp HCM_BRAM (
.clka(clk),
.ena(1'b1),
.wea(writeToBRAM),
.addra(rowToWrite),
.dina(dataToWrite),
.douta(dummyRead),
.clkb(clk),
.enb(1'b1),
.web(1'b0),
.addrb(rowToRead),
.dinb(dummyWrite),
.doutb(dataRead)
);
//-----------------//
// HCM WRITE QUEUE //
//-----------------//
reg [ROWINDEXBITS_HCM-1:0] queueWriteRow [QUEUESIZE-1:0];
reg [MAXHITNBITS-1:0] queueNewNHits [QUEUESIZE-1:0];
reg [NCOLS_HIM-1:0] queueNewHitInfo [QUEUESIZE-1:0];
reg [QUEUESIZE-1:0] queueSSIDIsNew;
reg [QUEUESIZEBITS-1:0] nInWriteQueue = 0;
reg [QUEUESIZE-1:0] waitTimeWriteQueue [2:0];
wire writeQueueShifted;
wire [QUEUESIZEBITS-1:0] nInWriteQueueAfterShift;
assign writeQueueShifted = (nInWriteQueue > 0) && (waitTimeWriteQueue[0] == 0);
assign nInWriteQueueAfterShift = nInWriteQueue - writeQueueShifted;
reg [ROWINDEXBITS_HIM-1:0] nextHIMAddress = 0; // next available address
//----------------//
// HCM READ QUEUE //
//----------------//
reg [ROWINDEXBITS_HCM-1:0] queueReadRow [QUEUESIZE-1:0];
reg [QUEUESIZEBITS-1:0] nInReadQueue = 0;
reg [QUEUESIZE-1:0] waitTimeReadQueue [2:0];
reg [QUEUESIZE-1:0] queueRequestedRead = 0;
wire readQueueShifted;
wire [QUEUESIZEBITS-1:0] nInReadQueueAfterShift;
assign readQueueShifted = (nInReadQueue > 0) && (waitTimeReadQueue[0] == 0);
assign nInReadQueueAfterShift = nInReadQueue - readQueueShifted;
//---------------------//
// COLLISION AVOIDANCE //
//---------------------//
reg [QUEUESIZE-1:0] collisionDetected = 0;
reg [NCOLS_HCM-1:0] dataPreviouslyWritten [QUEUESIZE-1:0];
//-----------------//
// FILL SEQUENTIAL //
//-----------------//
reg [1:0] fillStatus = 2'b00; // 00 = idle; 01 = filling; 10 = fill finished; 11 = ready to go
reg [ROWINDEXBITS_HCM-1:0] fillRow = 0;
reg [3:0] fillDelay = 0; // wait a safe amount of time after testing before resuming read and write
//-----------------//
// STUPID BULLSHIT //
//-----------------//
reg [QUEUESIZEBITS-1:0] queueN = 0; // can't do loop variable declaration
//---------//
// TESTING //
//---------//
(*mark_debug="TRUE"*)
reg [ROWINDEXBITS_HCM-1:0] debugQueueWriteRow [QUEUESIZE-1:0];
(*mark_debug="TRUE"*)
reg [MAXHITNBITS-1:0] debugQueueNewNHits [QUEUESIZE-1:0];
(*mark_debug="TRUE"*)
reg [ROWINDEXBITS_HCM-1:0] debugRowToRead;
(*mark_debug="TRUE"*)
reg [QUEUESIZEBITS-1:0] debugNInReadQueue = 0;
genvar i;
generate
for (i = 0; i < QUEUESIZE; i = i + 1) begin
always @(posedge clk) begin
debugQueueWriteRow[i] <= queueWriteRow[i];
debugQueueNewNHits[i] <= queueNewNHits[i];
debugRowToRead <= rowToRead;
debugNInReadQueue <= nInReadQueue;
end
end
endgenerate
initial begin
//$monitor ("%g\t%b\t%b\t%b", $time, writeToBRAM, rowToWrite, dataToWrite[6:0]);
//$monitor ("%b\t%b\t%b\t%b", debugQueueWriteRow[0], debugQueueNewHitsRow[0], debugRowToRead, debugNInReadQueue);
//$monitor ("%b\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d", writeToBRAM, rowToWrite, rowToWrite[SSIDBITS-1:COLINDEXBITS_HNM], rowToWrite[COLINDEXBITS_HNM-1:0], dataToWrite[MAXHITNBITS-1:0], outputNewHitInfo[ROWINDEXBITS_HCM-1:0], outputNewHitInfo[HITINFOBITS+ROWINDEXBITS_HCM-1:HITINFOBITS], outputNewHitInfo[HITINFOBITS*2+ROWINDEXBITS_HCM:HITINFOBITS*2], outputNewHitInfo[HITINFOBITS*3+ROWINDEXBITS_HCM:HITINFOBITS*3]);
end
always @(posedge clk) begin
if (reset) begin
nextHIMAddress <= 0;
collisionDetected <= 0;
nInReadQueue <= 0;
nInWriteQueue <= 0;
readFinished <= 0;
end
else begin
//----------------//
// READ AND WRITE //
//----------------//
writeToBRAM <= 1'b0; // don't write
newOutput <= 1'b0; // we did not just read something out
readFinished <= 0;
//----------------------------------//
// MOVE QUEUE AND RETURN READ VALUE //
//----------------------------------//
if (nInReadQueue > 0) begin
if (waitTimeReadQueue[0] > 0) begin // nothing to be read yet
for (queueN = 0; queueN < QUEUESIZE; queueN = queueN + 1) begin
waitTimeReadQueue[queueN] <= waitTimeReadQueue[queueN] - 1; // reduce wait times
end
end
else begin // read the first item
for (queueN = 0; queueN < QUEUESIZE - 1; queueN = queueN + 1) begin
waitTimeReadQueue[queueN] <= waitTimeReadQueue[queueN+1] - 1; // pop an item
queueReadRow[queueN] <= queueReadRow[queueN+1]; // pop an item
collisionDetected[queueN] <= collisionDetected[queueN+1];
dataPreviouslyWritten[queueN] <= dataPreviouslyWritten[queueN+1];
queueRequestedRead[queueN] <= queueRequestedRead[queueN+1]; // pop an item
end
nInReadQueue <= nInReadQueue - 1; // reduce number of items in queue
if (queueRequestedRead[0]) begin
readFinished <= 1; // only for requested reads
end
readNHits <= dataRead[MAXHITNBITS-1:0];
readHIM_address <= dataRead[NCOLS_HCM-1:MAXHITNBITS];
if (collisionDetected[0]) begin
//$display("Non-collision result for row %d is %b", queueReadRow[0], dataRead);
//$display("Data previously written for row %d was %b", queueReadRow[0], dataPreviouslyWritten[0]);
readNHits <= dataPreviouslyWritten[0][MAXHITNBITS-1:0];
readHIM_address <= dataPreviouslyWritten[0][NCOLS_HCM-1:MAXHITNBITS];
end
end
end
//------------------------------//
// MOVE QUEUE AND WRITE TO BRAM //
//------------------------------//
if (nInWriteQueue > 0) begin
if (waitTimeWriteQueue[0] > 0) begin // nothing to be written yet
for (queueN = 0; queueN < QUEUESIZE; queueN = queueN + 1) begin
waitTimeWriteQueue[queueN] <= waitTimeWriteQueue[queueN] - 1; // reduce wait times
end
end
else begin // write the first item
if (queueSSIDIsNew[0]) begin
dataToWrite <= queueNewNHits[0] | (nextHIMAddress << MAXHITNBITS);
HIM_address <= nextHIMAddress; // return HIM address
nextHIMAddress <= nextHIMAddress + 1;
//$display("New row %d, address %d, hitN %d", queueWriteRow[0], nextHIMAddress, queueNewNHits[0]);
dataPreviouslyWritten[nInReadQueueAfterShift] <= queueNewNHits[0] | (nextHIMAddress << MAXHITNBITS); // in case of collision
//$display("Setting collision readout data to %b", queueWriteRow[0]);
nOldHits <= 0;
nNewHits <= queueNewNHits[0];
end
else begin
dataToWrite <= dataRead + queueNewNHits[0];
HIM_address <= dataRead[NCOLS_HCM-1:MAXHITNBITS]; // return HIM address
dataPreviouslyWritten[nInReadQueueAfterShift] <= dataRead + queueNewNHits[0]; // in case of collision
nOldHits <= dataRead[MAXHITNBITS-1:0];
if (collisionDetected[0]) begin
dataToWrite <= dataPreviouslyWritten[0] + queueNewNHits[0];
dataPreviouslyWritten[nInReadQueueAfterShift] <= dataPreviouslyWritten[0] + queueNewNHits[0]; // in case of collision
nOldHits <= dataPreviouslyWritten[0][MAXHITNBITS-1:0];
end
//$display("Setting collision readout data to %b", queueWriteRow[0]);
//$display("Existing row %d, address %d, hitN %d", queueWriteRow[0], dataRead[10:3], dataRead[2:0] + queueNewNHits[0]);
nNewHits <= queueNewNHits[0];
end
for (queueN = 0; queueN < QUEUESIZE - 1; queueN = queueN + 1) begin
waitTimeWriteQueue[queueN] <= waitTimeWriteQueue[queueN+1] - 1; // pop an item
queueWriteRow[queueN] <= queueWriteRow[queueN+1]; // pop an item
queueNewNHits[queueN] <= queueNewNHits[queueN+1]; // pop an item
queueSSIDIsNew[queueN] <= queueSSIDIsNew[queueN+1]; // pop an item
queueNewHitInfo[queueN] <= queueNewHitInfo[queueN+1]; // pop an item
end
outputNewHitInfo <= queueNewHitInfo[0]; // return the new hit info
nInWriteQueue <= nInWriteQueue - 1; // reduce number of items in queue
writeToBRAM <= 1'b1;
rowToWrite <= queueWriteRow[0];
rowPassed <= queueWriteRow[0];
newOutput <= 1'b1;
end
end
//--------------------//
// ADD TO WRITE QUEUE //
//--------------------//
if (writeRow == 1'b1) begin // writing requires a row read request first - see below
// add to queue if not already there
queueSSIDIsNew[nInWriteQueueAfterShift] <= SSIDIsNew;
queueNewNHits[nInWriteQueueAfterShift] <= 1;
queueWriteRow[nInWriteQueueAfterShift] <= inputRowToWrite; // place in queue until read completes
waitTimeWriteQueue[nInWriteQueueAfterShift] <= BRAM_READDELAY;
nInWriteQueue <= nInWriteQueueAfterShift + 1; // increase the number of items in queue
queueNewHitInfo[nInWriteQueueAfterShift] <= inputHitInfo;
for (queueN = 0; queueN < nInWriteQueue; queueN = queueN + 1) begin
// if the row was already set to write on the clock edge, don't try to add to that row
if (queueWriteRow[queueN] == inputRowToWrite && waitTimeWriteQueue[queueN] > 0) begin
// undo the new item in queue and add to existing row in queue
queueNewNHits[nInWriteQueueAfterShift] <= 0;
queueNewNHits[queueN - writeQueueShifted] <= queueNewNHits[queueN] + 1;
queueWriteRow[nInWriteQueueAfterShift] <= queueWriteRow[nInWriteQueueAfterShift];
waitTimeWriteQueue[nInWriteQueueAfterShift] <= waitTimeWriteQueue[nInWriteQueueAfterShift];
nInWriteQueue <= nInWriteQueueAfterShift;
queueNewHitInfo[queueN - writeQueueShifted] <= queueNewHitInfo[queueN] | (inputHitInfo << HITINFOBITS*queueNewNHits[queueN]);
queueNewHitInfo[nInWriteQueueAfterShift] <= 0;
end
end
//$display("%d - %d:%d, %d:%d, %d:%d, %d:%d", nInWriteQueue, queueWriteRow[0], queueNewNHits[0], queueWriteRow[1], queueNewNHits[1], queueWriteRow[2], queueNewNHits[2], queueWriteRow[3], queueNewNHits[3]);
end
//-----------------------//
// ADD TO ROW READ QUEUE //
//-----------------------//
if (writeRow == 1'b1 || readRow == 1'b1) begin // read a whole row
rowToRead <= writeRow ? inputRowToWrite : inputRowToRead; // request read for this row
collisionDetected[nInReadQueueAfterShift] <= 0; // no collision
if ((waitTimeWriteQueue[0] == 0) && ((writeRow && (inputRowToWrite == queueWriteRow[0])) || (!writeRow && (inputRowToRead == queueWriteRow[0])))) begin // if we're going to write a row, and it's the same as the row we're reading (a collision)
rowToRead <= (writeRow ? inputRowToWrite : inputRowToRead) + 1; // make it a different row
collisionDetected[nInReadQueueAfterShift] <= 1;
end
queueReadRow[nInReadQueueAfterShift] <= writeRow ? inputRowToWrite : inputRowToRead; // place row in queue until read completes
waitTimeReadQueue[nInReadQueueAfterShift] <= BRAM_READDELAY; // set the wait time
nInReadQueue <= nInReadQueueAfterShift + 1; // increase the number of items in queue
if (readRow) queueRequestedRead[nInReadQueueAfterShift] <= 1; // mark as a silent read
else queueRequestedRead[nInReadQueueAfterShift] <= 0; // regular read (pass along results)
end
//-------------------//
// READ WRITE ENABLE //
//-------------------//
readReady <= (nInWriteQueue == 0); // needs better logic here
end
end
endmodule