1 |
3 |
kfleming |
/*
|
2 |
|
|
Copyright (c) 2007 MIT
|
3 |
|
|
|
4 |
|
|
Permission is hereby granted, free of charge, to any person
|
5 |
|
|
obtaining a copy of this software and associated documentation
|
6 |
|
|
files (the "Software"), to deal in the Software without
|
7 |
|
|
restriction, including without limitation the rights to use,
|
8 |
|
|
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
|
|
copies of the Software, and to permit persons to whom the
|
10 |
|
|
Software is furnished to do so, subject to the following
|
11 |
|
|
conditions:
|
12 |
|
|
|
13 |
|
|
The above copyright notice and this permission notice shall be
|
14 |
|
|
included in all copies or substantial portions of the Software.
|
15 |
|
|
|
16 |
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
|
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
|
|
OTHER DEALINGS IN THE SOFTWARE.
|
24 |
|
|
|
25 |
|
|
Author: Kermin Fleming
|
26 |
|
|
*/
|
27 |
|
|
|
28 |
|
|
// Global Imports
|
29 |
|
|
import GetPut::*;
|
30 |
|
|
import FIFO::*;
|
31 |
|
|
import RegFile::*;
|
32 |
|
|
|
33 |
|
|
// Project Imports
|
34 |
|
|
`include "Common.bsv"
|
35 |
|
|
|
36 |
|
|
import PLBMasterWires::*;
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
typedef enum
|
40 |
|
|
{
|
41 |
|
|
Test,
|
42 |
|
|
Sleep,
|
43 |
|
|
Finish
|
44 |
|
|
} TestState
|
45 |
|
|
deriving(Bits, Eq);
|
46 |
|
|
|
47 |
|
|
(* synthesize *)
|
48 |
|
|
module mkPLBMasterMagic (PLBMaster);
|
49 |
|
|
|
50 |
|
|
// state for the actual magic memory hardware
|
51 |
|
|
FIFO#(ComplexWord) wordInfifo <- mkFIFO();
|
52 |
|
|
FIFO#(ComplexWord) wordOutfifo <- mkFIFO();
|
53 |
|
|
FIFO#(PLBMasterCommand) plbMasterCommandInfifo <- mkFIFO();
|
54 |
|
|
|
55 |
|
|
RegFile#(Bit#(20), ComplexWord) matrixA <- mkRegFileFullLoad("matrixA.hex");
|
56 |
|
|
RegFile#(Bit#(20), ComplexWord) matrixB <- mkRegFileFullLoad("matrixB.hex");
|
57 |
|
|
RegFile#(Bit#(20), ComplexWord) matrixC <- mkRegFileFull();
|
58 |
|
|
RegFile#(Bit#(20), ComplexWord) scratch <- mkRegFileFull();
|
59 |
|
|
|
60 |
|
|
Reg#(Bit#(LogBlockElements)) elementCounter <- mkReg(0);
|
61 |
|
|
Reg#(Bit#(LogBlockSize)) rowCounter <- mkReg(0); // 0 -> blocksize
|
62 |
|
|
Reg#(Bit#(LogRowSize)) rowOffset <- mkReg(0);
|
63 |
|
|
Reg#(BlockAddr) addressOffset <- mkReg(0);
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
// State for running the golden test loop
|
67 |
|
|
RegFile#(Bit#(20), ComplexWord) golden <- mkRegFileFullLoad("golden.hex");
|
68 |
|
|
Reg#(Bit#(32)) goldenElementCounter <- mkReg(0);
|
69 |
|
|
Reg#(Bit#(64)) totalTicks <- mkReg(0);
|
70 |
|
|
|
71 |
|
|
rule tick(True);
|
72 |
|
|
totalTicks <= totalTicks +1;
|
73 |
|
|
endrule
|
74 |
|
|
|
75 |
|
|
rule rowSize(plbMasterCommandInfifo.first() matches tagged RowSize .rs);
|
76 |
|
|
debug(plbMasterDebug, $display("PLBMaster: processing RowSize command %d", rs));
|
77 |
|
|
rowOffset <= rs;
|
78 |
|
|
plbMasterCommandInfifo.deq();
|
79 |
|
|
endrule
|
80 |
|
|
|
81 |
|
|
rule loadPage(plbMasterCommandInfifo.first() matches tagged LoadPage .ba);
|
82 |
|
|
elementCounter <= elementCounter + 1;
|
83 |
|
|
if(elementCounter == 0)
|
84 |
|
|
begin
|
85 |
|
|
debug(plbMasterDebug, $display("PLBMaster: processing LoadPage command"));
|
86 |
|
|
end
|
87 |
|
|
if(elementCounter + 1 == 0)
|
88 |
|
|
begin
|
89 |
|
|
debug(plbMasterDebug, $display("PLBMaster: finished LoadPage command"));
|
90 |
|
|
addressOffset <= 0;
|
91 |
|
|
rowCounter <= 0;
|
92 |
|
|
plbMasterCommandInfifo.deq();
|
93 |
|
|
end
|
94 |
|
|
else if(rowCounter + 1 == 0)
|
95 |
|
|
begin //When we get to the end of a row, we need to reset by
|
96 |
|
|
//shifting the Address Offset to 1 row higher =
|
97 |
|
|
rowCounter <= 0;
|
98 |
|
|
addressOffset <= addressOffset + 1 - unpack(fromInteger(1*valueof(BlockSize))) + (1 << rowOffset);
|
99 |
|
|
end
|
100 |
|
|
else
|
101 |
|
|
begin
|
102 |
|
|
addressOffset <= addressOffset + 1;
|
103 |
|
|
rowCounter <= rowCounter + 1;
|
104 |
|
|
end
|
105 |
|
|
|
106 |
|
|
BlockAddr addr = ba + addressOffset;
|
107 |
|
|
// Now that we're done with calculating the address,
|
108 |
|
|
// we can case out our memory space
|
109 |
|
|
//case (addr[23:22])
|
110 |
|
|
// 2'b00: begin $display("PLB: reading matA[%h] => %h" ,addr[21:2], matrixA.sub(addr[21:2])); end
|
111 |
|
|
// 2'b01: begin $display("PLB: reading matB[%h] => %h" ,addr[21:2], matrixB.sub(addr[21:2])); end
|
112 |
|
|
// 2'b10: begin $display("PLB: reading matC[%h] => %h" ,addr[21:2], matrixC.sub(addr[21:2])); end
|
113 |
|
|
// 2'b11: begin $display("PLB: reading scratch[%h] => %h",addr[21:2], scratch.sub(addr[21:2])); end
|
114 |
|
|
//endcase
|
115 |
|
|
|
116 |
|
|
case (addr[21:20])
|
117 |
|
|
2'b00: wordOutfifo.enq(matrixA.sub(addr[19:0]));
|
118 |
|
|
2'b01: wordOutfifo.enq(matrixB.sub(addr[19:0]));
|
119 |
|
|
2'b10: wordOutfifo.enq(matrixC.sub(addr[19:0]));
|
120 |
|
|
2'b11: wordOutfifo.enq(scratch.sub(addr[19:0]));
|
121 |
|
|
endcase
|
122 |
|
|
|
123 |
|
|
endrule
|
124 |
|
|
|
125 |
|
|
rule storePage(plbMasterCommandInfifo.first() matches tagged StorePage .ba);
|
126 |
|
|
elementCounter <= elementCounter + 1;
|
127 |
|
|
if(elementCounter == 0)
|
128 |
|
|
begin
|
129 |
|
|
debug(plbMasterDebug, $display("PLBMaster: processing StorePage command"));
|
130 |
|
|
end
|
131 |
|
|
if(elementCounter + 1 == 0)
|
132 |
|
|
begin
|
133 |
|
|
debug(plbMasterDebug, $display("PLBMaster: finished StorePage command"));
|
134 |
|
|
addressOffset <= 0;
|
135 |
|
|
rowCounter <= 0;
|
136 |
|
|
plbMasterCommandInfifo.deq();
|
137 |
|
|
end
|
138 |
|
|
else if(rowCounter + 1 == 0)
|
139 |
|
|
begin
|
140 |
|
|
addressOffset <= addressOffset + 1 - unpack(fromInteger(valueof(BlockSize))) + (1 << rowOffset);
|
141 |
|
|
rowCounter <= 0;
|
142 |
|
|
end
|
143 |
|
|
else
|
144 |
|
|
begin
|
145 |
|
|
addressOffset <= addressOffset + 1;
|
146 |
|
|
rowCounter <= rowCounter + 1;
|
147 |
|
|
end
|
148 |
|
|
|
149 |
|
|
BlockAddr addr = ba + addressOffset;
|
150 |
|
|
// Now that we're done with calculating the address,
|
151 |
|
|
// we can case out our memory space
|
152 |
|
|
case (addr[21:20])
|
153 |
|
|
2'b00: begin
|
154 |
|
|
debug(plbMasterDebug,$display("PLB: writing to matA %h",addr[19:0]));
|
155 |
|
|
matrixA.upd(addr[19:0],wordInfifo.first());
|
156 |
|
|
end
|
157 |
|
|
2'b01: begin
|
158 |
|
|
debug(plbMasterDebug,$display("PLB: writing to matB %h",addr[19:0]));
|
159 |
|
|
matrixB.upd(addr[19:0],wordInfifo.first());
|
160 |
|
|
end
|
161 |
|
|
2'b10: begin
|
162 |
|
|
debug(plbMasterDebug,$display("PLB: writing to matC %h",addr[19:0]));
|
163 |
|
|
matrixC.upd(addr[19:0],wordInfifo.first());
|
164 |
|
|
let oldval = matrixC.sub(addr[19:0]);
|
165 |
|
|
let goldenval = golden.sub(addr[19:0]);
|
166 |
|
|
|
167 |
|
|
if ((goldenval != oldval) && (goldenval == wordInfifo.first())) // a new correct val
|
168 |
|
|
begin
|
169 |
|
|
goldenElementCounter <= goldenElementCounter +1;
|
170 |
|
|
if (truncate(goldenElementCounter) == 16'hFFFF) // time to announce
|
171 |
|
|
$display("Correct Value Count: %d @ %d", goldenElementCounter+1,totalTicks);
|
172 |
|
|
if (goldenElementCounter + 1 == (1 << (rowOffset<<1)))
|
173 |
|
|
begin
|
174 |
|
|
$display("PASSED @ %d", totalTicks);
|
175 |
|
|
$finish;
|
176 |
|
|
end
|
177 |
|
|
end
|
178 |
|
|
end
|
179 |
|
|
2'b11: begin
|
180 |
|
|
debug(plbMasterDebug,$display("PLB: writing to scratch %h",addr[19:0]));
|
181 |
|
|
scratch.upd(addr[19:0],wordInfifo.first());
|
182 |
|
|
end
|
183 |
|
|
endcase
|
184 |
|
|
wordInfifo.deq();
|
185 |
|
|
endrule
|
186 |
|
|
|
187 |
|
|
rule debugRule (True);
|
188 |
|
|
case (plbMasterCommandInfifo.first()) matches
|
189 |
|
|
tagged LoadPage .i: noAction;
|
190 |
|
|
tagged StorePage .i: noAction;
|
191 |
|
|
tagged RowSize .sz: noAction;
|
192 |
|
|
default:
|
193 |
|
|
$display("PLBMaster: illegal command: %h", plbMasterCommandInfifo.first());
|
194 |
|
|
endcase
|
195 |
|
|
|
196 |
|
|
endrule
|
197 |
|
|
|
198 |
|
|
interface Put wordInput = interface Put;
|
199 |
|
|
method Action put(x);
|
200 |
|
|
wordInfifo.enq(x);
|
201 |
|
|
//$display("PLB: got val %h", x);
|
202 |
|
|
endmethod
|
203 |
|
|
endinterface;
|
204 |
|
|
|
205 |
|
|
interface Get wordOutput = interface Get;
|
206 |
|
|
method get();
|
207 |
|
|
actionvalue
|
208 |
|
|
//$display("PLB: sending val %h", wordOutfifo.first());
|
209 |
|
|
wordOutfifo.deq();
|
210 |
|
|
return wordOutfifo.first();
|
211 |
|
|
endactionvalue
|
212 |
|
|
endmethod
|
213 |
|
|
endinterface;
|
214 |
|
|
interface Put plbMasterCommandInput = fifoToPut(plbMasterCommandInfifo);
|
215 |
|
|
interface PLBMasterWires plbMasterWires = ?;
|
216 |
|
|
endmodule
|