1 |
2 |
abhiag |
//----------------------------------------------------------------------//
|
2 |
|
|
// The MIT License
|
3 |
|
|
//
|
4 |
|
|
// Copyright (c) 2008 Abhinav Agarwal, Alfred Man Cheuk Ng
|
5 |
|
|
// Contact: abhiag@gmail.com
|
6 |
|
|
//
|
7 |
|
|
// Permission is hereby granted, free of charge, to any person
|
8 |
|
|
// obtaining a copy of this software and associated documentation
|
9 |
|
|
// files (the "Software"), to deal in the Software without
|
10 |
|
|
// restriction, including without limitation the rights to use,
|
11 |
|
|
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 |
|
|
// copies of the Software, and to permit persons to whom the
|
13 |
|
|
// Software is furnished to do so, subject to the following conditions:
|
14 |
|
|
//
|
15 |
|
|
// The above copyright notice and this permission notice shall be
|
16 |
|
|
// included in all copies or substantial portions of the Software.
|
17 |
|
|
//
|
18 |
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
26 |
|
|
//----------------------------------------------------------------------//
|
27 |
|
|
|
28 |
|
|
import GetPut::*;
|
29 |
|
|
import Vector::*;
|
30 |
|
|
import GFTypes::*;
|
31 |
|
|
import FIFO::*;
|
32 |
|
|
import Assert::*;
|
33 |
|
|
import RegFile::*;
|
34 |
|
|
import mkReedSolomon::*;
|
35 |
|
|
|
36 |
|
|
import "BDPI" function Action loadByteStream ();
|
37 |
|
|
import "BDPI" function ActionValue# (Byte) getNextStreamByte ();
|
38 |
|
|
import "BDPI" function Action storeByteStream ();
|
39 |
|
|
import "BDPI" function Action putNextStreamByte (Byte writeByte);
|
40 |
|
|
import "BDPI" function Action putMACData (Byte n, Byte t);
|
41 |
|
|
import "BDPI" function ActionValue# (Byte) isStreamActive ();
|
42 |
|
|
import "BDPI" function Action closeOutputFile ();
|
43 |
|
|
|
44 |
|
|
//Polynomial primitive_Sypolynomial = 8'b00011101;
|
45 |
|
|
|
46 |
|
|
module mkTestBench ();
|
47 |
|
|
|
48 |
|
|
// Define Primitive polynomial
|
49 |
|
|
// P (x) = x**8 + x**4 + x**3 + x**2 + 1 = 0 ; Ignore highest degree (8)
|
50 |
|
|
IReedSolomon rs <- mkReedSolomon;
|
51 |
|
|
|
52 |
|
|
Reg# (Bool) read_done <- mkReg (False);
|
53 |
|
|
Reg# (int) bytes_in <- mkReg (0);
|
54 |
|
|
Reg# (Bit#(32)) bytes_out <- mkReg (0);
|
55 |
|
|
Reg# (Bit#(32)) last_bytes_out <- mkReg (0);
|
56 |
|
|
Reg# (int) last_bytes_in <- mkReg (0);
|
57 |
|
|
Reg# (int) watchdog <- mkReg (0);
|
58 |
|
|
Reg# (Bool) finished <- mkReg (False);
|
59 |
|
|
Reg# (int) state <- mkReg (0);
|
60 |
|
|
Reg# (Byte) bytes_in_block <- mkReg (0);
|
61 |
|
|
Reg# (Byte) t <- mkReg (0);
|
62 |
|
|
Reg# (Byte) n <- mkReg (0);
|
63 |
|
|
Reg# (Bit#(32)) bytes_out_exp <- mkReg (0);
|
64 |
|
|
|
65 |
|
|
FIFO#(Bit#(32)) ff_bytes_out_exp <- mkSizedFIFO (10);
|
66 |
|
|
FIFO#(Byte) ff_n <- mkSizedFIFO (10);
|
67 |
|
|
FIFO#(Byte) ff_t <- mkSizedFIFO (10);
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
// -------------------------------------------
|
71 |
|
|
rule init (state==0);
|
72 |
|
|
$display ("init\n");
|
73 |
|
|
loadByteStream ();
|
74 |
|
|
storeByteStream ();
|
75 |
|
|
state <= 1;
|
76 |
|
|
endrule
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
// -------------------------------------------
|
80 |
|
|
rule input_control_info (state == 1 && read_done == False && bytes_in_block == n);
|
81 |
|
|
|
82 |
|
|
let n_in <- getNextStreamByte ();
|
83 |
|
|
let t_in <- getNextStreamByte ();
|
84 |
|
|
let not_eof <- isStreamActive ();
|
85 |
|
|
|
86 |
|
|
if (not_eof != 0)
|
87 |
|
|
begin
|
88 |
|
|
n <= n_in;
|
89 |
|
|
t <= t_in;
|
90 |
|
|
bytes_in_block <= 0;
|
91 |
|
|
|
92 |
|
|
rs.rs_t_in.put (t_in);
|
93 |
|
|
rs.rs_k_in.put (n_in - 2*t_in);
|
94 |
|
|
|
95 |
|
|
Byte temp = n_in - 2*t_in;
|
96 |
|
|
bytes_out_exp <= bytes_out_exp + zeroExtend(temp);
|
97 |
|
|
|
98 |
|
|
ff_bytes_out_exp.enq (bytes_out_exp);
|
99 |
|
|
ff_n.enq (n_in);
|
100 |
|
|
ff_t.enq (t_in);
|
101 |
|
|
|
102 |
|
|
$display (" [mac in] n = %d, t = %d, k = %d", n_in, t_in, n_in - 2*t_in);
|
103 |
|
|
end
|
104 |
|
|
else
|
105 |
|
|
begin
|
106 |
|
|
read_done <= True;
|
107 |
|
|
$display (" [reads done] bytes in : %d", bytes_in - 1);
|
108 |
|
|
end
|
109 |
|
|
endrule
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
// -------------------------------------------
|
113 |
|
|
rule input_data (state == 1 && read_done == False && bytes_in_block < n);
|
114 |
|
|
Byte not_eof <- isStreamActive ();
|
115 |
|
|
if (not_eof != 0)
|
116 |
|
|
begin
|
117 |
|
|
Byte datum <- getNextStreamByte ();
|
118 |
|
|
rs.rs_input.put (datum);
|
119 |
|
|
|
120 |
|
|
bytes_in_block <= bytes_in_block + 1;
|
121 |
|
|
|
122 |
|
|
// the way getNextStreamByte operates, we'll get one more character
|
123 |
|
|
// than what is in the input file. So we need to adjust for this in
|
124 |
|
|
// the way we use bytes_in...
|
125 |
|
|
bytes_in <= bytes_in + 1;
|
126 |
|
|
$display (" [bytes in] byte (%d) = %d, block bytes %d", bytes_in, datum, bytes_in_block);
|
127 |
|
|
end
|
128 |
|
|
else
|
129 |
|
|
begin
|
130 |
|
|
read_done <= True;
|
131 |
|
|
$display (" [reads done] bytes in : %d", bytes_in - 1);
|
132 |
|
|
end
|
133 |
|
|
endrule
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
// -------------------------------------------
|
137 |
|
|
rule output_data (state == 1);
|
138 |
|
|
Byte next_byte <- rs.rs_output.get ();
|
139 |
|
|
putNextStreamByte (next_byte);
|
140 |
|
|
bytes_out <= bytes_out + 1;
|
141 |
|
|
$display (" [bytes out] %d / %d", bytes_out, bytes_in - 1);
|
142 |
|
|
endrule
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
// -------------------------------------------
|
146 |
|
|
rule output_mac (state == 1 && bytes_out == ff_bytes_out_exp.first ());
|
147 |
|
|
ff_bytes_out_exp.deq ();
|
148 |
|
|
ff_n.deq ();
|
149 |
|
|
ff_t.deq ();
|
150 |
|
|
putMACData (ff_n.first (), ff_t.first ());
|
151 |
|
|
endrule
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
// -------------------------------------------
|
155 |
|
|
rule print_flag (state == 1);
|
156 |
|
|
Bool cant_correct_flag <- rs.rs_flag.get ();
|
157 |
|
|
$display (" [cant correct flag] %d", cant_correct_flag);
|
158 |
|
|
endrule
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
// -------------------------------------------
|
162 |
|
|
rule watchdog_timer (state == 1);
|
163 |
|
|
if ((last_bytes_out == bytes_out) &&
|
164 |
|
|
(last_bytes_in == bytes_in))
|
165 |
|
|
watchdog <= watchdog + 1;
|
166 |
|
|
else
|
167 |
|
|
begin
|
168 |
|
|
last_bytes_in <= bytes_in;
|
169 |
|
|
last_bytes_out <= bytes_out;
|
170 |
|
|
watchdog <= 0;
|
171 |
|
|
end
|
172 |
|
|
|
173 |
|
|
if (watchdog == 2000)
|
174 |
|
|
begin
|
175 |
|
|
$display ("[WARNING] Watchdog timer expired.");
|
176 |
|
|
$display ("There has been no output in the last 100 clock cycles.");
|
177 |
|
|
$display ("Exiting");
|
178 |
|
|
|
179 |
|
|
closeOutputFile ();
|
180 |
|
|
$finish (0);
|
181 |
|
|
end
|
182 |
|
|
endrule
|
183 |
|
|
|
184 |
|
|
|
185 |
|
|
// -------------------------------------------
|
186 |
|
|
rule exit (read_done == True && (bytes_out_exp) == bytes_out);
|
187 |
|
|
closeOutputFile ();
|
188 |
|
|
$display (" [bytes written] %d", bytes_out);
|
189 |
|
|
$finish (0);
|
190 |
|
|
endrule
|
191 |
|
|
|
192 |
|
|
endmodule
|