aboutsummaryrefslogtreecommitdiffstats
path: root/fpga/usrp2/serdes/serdes_tb.v
blob: eb8e019fc4f75fbd6815115f2f8b340007eb55a2 (plain)
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
318
319
320
321
322
323
324
325
326
327
328
// FIXME need to add flow control

module serdes_tb();
   
   reg clk, rst;
   wire ser_rx_clk, ser_tx_clk;
   wire ser_rklsb, ser_rkmsb, ser_tklsb, ser_tkmsb;
   wire [15:0] ser_r, ser_t;
   
   initial clk = 0;
   initial rst = 1;
   initial #1000 rst = 0;
   always #100 clk = ~clk;
   
   // Wishbone
   reg [31:0]  wb_dat_i;
   wire [31:0] wb_dat_o_rx, wb_dat_o_tx;
   reg 	       wb_we, wb_en_rx, wb_en_tx;
   reg [8:0]   wb_adr;

   // Buffer Control
   reg 	       go, clear, read, write;
   reg [3:0]   buf_num;
   wire [31:0] ctrl_word = {buf_num,3'b0,clear,write,read,step,lastline,firstline};
   reg [8:0]   firstline = 0, lastline = 0;
   reg [3:0]   step = 1;
   reg 	       first_tx = 1, first_rx = 1;  // for verif
   
   // TX Side
   reg 	       wb_we_tx;
   wire        en_tx, we_tx;
   wire [8:0]  addr_tx;
   wire [31:0] f2r_tx, r2f_tx;
   wire [31:0] data_tx;
   wire        read_tx, done_tx, error_tx, sop_tx, eop_tx;
   
   wire        fdone_tx, ferror_tx;
   
   reg 	       even;
   reg 	       channel_error = 0;
   
   serdes_tx serdes_tx
     (.clk(clk),.rst(rst),
      .ser_tx_clk(ser_tx_clk),.ser_t(ser_t),.ser_tklsb(ser_tklsb),.ser_tkmsb(ser_tkmsb),
      .rd_dat_i(data_tx),.rd_read_o(read_tx),.rd_done_o(done_tx),
      .rd_error_o(error_tx),.rd_sop_i(sop_tx),.rd_eop_i(eop_tx) );
   
   ram_2port #(.DWIDTH(32),.AWIDTH(9))
     ram_tx(.clka(clk),.ena(wb_en_tx),.wea(wb_we_tx),.addra(wb_adr),.dia(wb_dat_i),.doa(wb_dat_o_tx),
            .clkb(clk),.enb(en_tx),.web(we_tx),.addrb(addr_tx),.dib(f2r_tx),.dob(r2f_tx));
   
   buffer_int #(.BUFF_NUM(1)) buffer_int_tx
     (.clk(clk),.rst(rst),
      .ctrl_word(ctrl_word),.go(go),
      .done(fdone_tx),.error(ferror_tx),
      
      .en_o(en_tx),.we_o(we_tx),.addr_o(addr_tx),.dat_to_buf(f2r_tx),.dat_from_buf(r2f_tx),
      
      .wr_dat_i(0),.wr_write_i(0),.wr_done_i(0),
      .wr_error_i(0),.wr_ready_o(),.wr_full_o(),
      
      .rd_dat_o(data_tx),.rd_read_i(read_tx),.rd_done_i(done_tx),
      .rd_error_i(error_tx),.rd_sop_o(sop_tx),.rd_eop_o(eop_tx) );


   // RX Side
   reg 	       wb_we_rx;
   wire        en_rx, we_rx;
   wire [8:0]  addr_rx;
   wire [31:0] f2r_rx, r2f_rx;
   wire [31:0] data_rx;
   wire        write_rx, done_rx, error_rx, ready_rx, empty_rx;
   
   wire        fdone_rx, ferror_rx;
   
   serdes_rx serdes_rx
     (.clk(clk),.rst(rst),
      .ser_rx_clk(ser_rx_clk),.ser_r(ser_r),.ser_rklsb(ser_rklsb),.ser_rkmsb(ser_rkmsb),
      .wr_dat_o(data_rx),.wr_write_o(write_rx),.wr_done_o(done_rx),
      .wr_error_o(error_rx),.wr_ready_i(ready_rx),.wr_full_i(full_rx) );

   ram_2port #(.DWIDTH(32),.AWIDTH(9))
     ram_rx(.clka(clk),.ena(wb_en_rx),.wea(wb_we_rx),.addra(wb_adr),.dia(wb_dat_i),.doa(wb_dat_o_rx),
            .clkb(clk),.enb(en_rx),.web(we_rx),.addrb(addr_rx),.dib(f2r_rx),.dob(r2f_rx) );
   
   buffer_int #(.BUFF_NUM(0)) buffer_int_rx
     (.clk(clk),.rst(rst),
      .ctrl_word(ctrl_word),.go(go),
      .done(fdone_rx),.error(ferror_rx),
      
      .en_o(en_rx),.we_o(we_rx),.addr_o(addr_rx),.dat_to_buf(f2r_rx),.dat_from_buf(r2f_rx),
      
      .wr_dat_i(data_rx),.wr_write_i(write_rx),.wr_done_i(done_rx),
      .wr_error_i(error_rx),.wr_ready_o(ready_rx),.wr_full_o(full_rx),
      
      .rd_dat_o(),.rd_read_i(0),.rd_done_i(0),
      .rd_error_i(0),.rd_sop_o(),.rd_eop_o() );

   // Simulate the connection
   serdes_model serdes_model
     (.ser_tx_clk(ser_tx_clk), .ser_tkmsb(ser_tkmsb), .ser_tklsb(ser_tklsb), .ser_t(ser_t),
      .ser_rx_clk(ser_rx_clk), .ser_rkmsb(ser_rkmsb), .ser_rklsb(ser_rklsb), .ser_r(ser_r),
      .even(even), .error(channel_error) );
   
   initial begin
      wb_en_rx <= 0;
      wb_en_tx <=0;
      wb_we_tx <= 0;
      wb_we_rx <= 0;
      wb_adr <= 0;
      wb_dat_i <= 0;
      go <= 0;
      even <= 0;
      @(negedge rst);
      @(posedge clk);
      FillTXRAM;
      ClearRXRAM;
      ResetBuffer(0);
      ResetBuffer(1);

      // receive a full buffer
      ReceiveSERDES(0,10);
      SendSERDES(0,10);

      // Receive a partial buffer
      SendSERDES(11,20);
      ReceiveSERDES(11,50);

      // Receive too many for buffer
      SendSERDES(21,100);
      ReceiveSERDES(21,30);

      // Send 3 packets, then wait to receive them, so they stack up in the rx fifo
      SendSERDES(31,40);
      SendSERDES(41,50);
      SendSERDES(51,60);
      repeat (10)
	@(posedge clk);
      ReceiveSERDES(31,40);
      ReceiveSERDES(41,50);
      repeat (1000)
	@(posedge clk);
      ReceiveSERDES(51,60);

      // Overfill the FIFO, should get an error on 3rd packet
      SendSERDES(1,400);
      SendSERDES(1,400);
      
      
      WaitForTX;
      //WaitForRX;
      

      repeat(1000)
	@(posedge clk);
      ReceiveSERDES(101,500);
      ReceiveSERDES(101,500);
      ReadRAM(80);
      $finish;
   end // initial begin

   always @(posedge clk)
     if(write_rx)
       $display("SERDES RX, FIFO WRITE %x, FIFO RDY %d, FIFO FULL %d",data_rx, ready_rx, full_rx);
   
   always @(posedge clk)
     if(read_tx)
       $display("SERDES TX, FIFO READ %x, SOP %d, EOP %d",data_tx, sop_tx, eop_tx);
   
   initial begin
      $dumpfile("serdes_tb.vcd");
      $dumpvars(0,serdes_tb);
   end

   initial #10000000 $finish;

   initial #259300 channel_error <= 1;
   initial #259500 channel_error <= 0;
   
   task FillTXRAM;
      begin
	 wb_adr <= 0;
	 wb_dat_i <= 32'h10802000;
	 wb_we_tx <= 1;
	 wb_en_tx <= 1;
	 @(posedge clk);
	 repeat(511) begin
	    wb_dat_i <= wb_dat_i + 32'h00010001;
	    wb_adr <= wb_adr + 1;
	    @(posedge clk);
	 end // repeat (511)
	 wb_we_tx <= 0;
	 wb_en_tx <= 0;
	 @(posedge clk);
	 $display("Done entering Data into TX RAM\n");
      end
   endtask // FillTXRAM
   
   task ClearRXRAM;
      begin
	 wb_adr <= 0;
	 wb_dat_i <= 0;
	 wb_we_rx <= 1;
	 wb_en_rx <= 1;
	 wb_dat_i <= 0;
	 @(posedge clk);
	 repeat(511) begin
	    wb_adr <= wb_adr + 1;
	    @(posedge clk);
	 end // repeat (511)
	 wb_we_rx <= 0;
	 wb_en_rx <= 0;
	 @(posedge clk);
	 $display("Done clearing RX RAM\n");
      end
   endtask // FillRAM
   
   task ReadRAM;
      input [8:0] lastline;
      begin
	 wb_en_rx <= 1;
	 wb_adr <= 0;
	 @(posedge clk);
	 @(posedge clk);
	 repeat(lastline) begin
	    $display("ADDR: %h  DATA %h", wb_adr, wb_dat_o_rx);
	    wb_adr <= wb_adr + 1;
	    @(posedge clk);
	    @(posedge clk);
	 end // repeat (511)
	 $display("ADDR: %h  DATA %h", wb_adr, wb_dat_o_rx);
	 wb_en_rx <= 0;
	 @(posedge clk);
	 $display("Done reading out RX RAM\n");
      end
   endtask // FillRAM
   
   task ResetBuffer;
      input [3:0] buffer_num;
      begin
	 buf_num <= buffer_num;
	 clear <= 1; read <= 0; write <= 0;
	 go <= 1;
	 @(posedge clk);
	 go <= 0;
	 @(posedge clk);
	 $display("Buffer Reset");
      end
   endtask // ClearBuffer
   
   task SetBufferWrite;
      input [3:0] buffer_num;
      input [8:0] start;
      input [8:0] stop;
      begin
	 buf_num <= buffer_num;
	 clear <= 0; read <= 0; write <= 1;
	 firstline <= start;
	 lastline <= stop;
	 go <= 1;
	 @(posedge clk);
	 go <= 0;
	 @(posedge clk);
	 $display("Buffer Set for Write");
      end
   endtask // SetBufferWrite
   
   task SetBufferRead;
      input [3:0] buffer_num;
      input [8:0] start;
      input [8:0] stop;
      begin
	 buf_num <= buffer_num;
	 clear <= 0; read <= 1; write <= 0;
	 firstline <= start;
	 lastline <= stop;
	 go <= 1;
	 @(posedge clk);
	 go <= 0;
	 @(posedge clk);
	 $display("Buffer Set for Read");
      end
   endtask // SetBufferRead

   task WaitForTX;
      begin
	 while (!(fdone_tx | ferror_tx))
	   @(posedge clk);
      end
   endtask // WaitForTX
   
   task WaitForRX;
      begin
	 while (!(fdone_rx | ferror_rx))
	   @(posedge clk);
      end
   endtask // WaitForRX
   
   task SendSERDES;
      input [8:0] start;
      input [8:0] stop;
      begin
	 if(~first_tx)
	   WaitForTX;
	 else
	   first_tx <= 0;
	 ResetBuffer(1);
	 SetBufferRead(1,start,stop);
	 $display("Here");
      end
   endtask // SendSERDES
   
   task ReceiveSERDES;
      input [8:0] start;
      input [8:0] stop;
      begin
	 if(~first_rx)
	   WaitForRX;
	 else
	   first_rx <= 0;
	 ResetBuffer(0);
	 SetBufferWrite(0,start,stop);
	 $display("Here2");
      end
   endtask // ReceiveSERDES
   
endmodule // serdes_tb