e126ade51954458fd76c10cd7c4902ed000854d8
[debian/gnuradio] / usrp2 / fpga / opencores / uart16550 / bench / verilog / uart_testbench.v
1 //////////////////////////////////////////////////////////////////////
2 ////                                                              ////
3 ////  uart_testbench.v                                            ////
4 ////                                                              ////
5 ////  This file is part of the "uart16550" project                ////
6 ////  http://www.opencores.org/projects/uart16550/                ////
7 ////                                                              ////
8 ////  Author(s):                                                  ////
9 ////      - tadej@opencores.org (Tadej Markovic)                  ////
10 ////                                                              ////
11 ////  All additional information is avaliable in the README.txt   ////
12 ////  file.                                                       ////
13 ////                                                              ////
14 ////                                                              ////
15 //////////////////////////////////////////////////////////////////////
16 ////                                                              ////
17 //// Copyright (C) 2000 - 2004 authors                            ////
18 ////                                                              ////
19 //// This source file may be used and distributed without         ////
20 //// restriction provided that this copyright statement is not    ////
21 //// removed from the file and that any derivative work contains  ////
22 //// the original copyright notice and the associated disclaimer. ////
23 ////                                                              ////
24 //// This source file is free software; you can redistribute it   ////
25 //// and/or modify it under the terms of the GNU Lesser General   ////
26 //// Public License as published by the Free Software Foundation; ////
27 //// either version 2.1 of the License, or (at your option) any   ////
28 //// later version.                                               ////
29 ////                                                              ////
30 //// This source is distributed in the hope that it will be       ////
31 //// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
32 //// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
33 //// PURPOSE.  See the GNU Lesser General Public License for more ////
34 //// details.                                                     ////
35 ////                                                              ////
36 //// You should have received a copy of the GNU Lesser General    ////
37 //// Public License along with this source; if not, download it   ////
38 //// from http://www.opencores.org/lgpl.shtml                     ////
39 ////                                                              ////
40 //////////////////////////////////////////////////////////////////////
41 //
42 // CVS Revision History
43 //
44 // $Log: uart_testbench.v,v $
45 // Revision 1.1  2004/03/27 03:55:17  tadejm
46 // Testbench with complete selfchecking. BUG is that THRE status is set at the end of last sent bit when TX FIFO is empty instead when only TX FIFO gets empty. This causes testcases not to finish.
47 //
48 //
49 //
50
51
52 `include "uart_defines.v"
53 `include "uart_testbench_defines.v"
54 `include "wb_model_defines.v"
55 `include "timescale.v"
56
57 module testbench;
58
59
60 parameter max_wait_cnt = 20000;
61
62 // INTERNAL signals
63 //#################
64
65   // WB slave signals
66   //#################
67
68     // UART Wishbone Slave signals
69     wire                         wb_int_o;
70     wire [`UART_ADDR_WIDTH-1:0]  wbs_adr_i;
71     wire [`UART_DATA_WIDTH-1:0]  wbs_dat_i;
72     wire [`UART_DATA_WIDTH-1:0]  wbs_dat_o;
73     wire                  [3:0]  wbs_sel_i;
74     wire                         wbs_cyc_i;
75     wire                         wbs_stb_i;
76     wire                  [2:0]  wbs_cti_i;
77     wire                  [1:0]  wbs_bte_i;
78     wire                         wbs_we_i;
79     wire                         wbs_ack_o;
80     wire                         wbs_rty_o = 1'b0;
81     wire                         wbs_err_o = 1'b0;
82
83   // UART signals
84   //#############
85
86     // UART Serial Data I/O signals
87     wire                         stx_pad_o;
88     wire                         srx_pad_i;
89     // UART Modem I/O signals
90     wire                         rts_pad_o;
91     wire                         cts_pad_i;
92     wire                         dtr_pad_o;
93     wire                         dsr_pad_i;
94     wire                         ri_pad_i;
95     wire                         dcd_pad_i;
96   `ifdef UART_HAS_BAUDRATE_OUTPUT
97     wire                         baud_o;
98   `endif
99
100   // System signals
101   //###############
102
103     // WB clock signal
104     reg          wb_clk; // divided device clock with period T_wb_clk_period
105     // WB clock enable signal
106     reg          wb_clk_en = 1'b1;
107     // WB clock period variable
108     real         T_wb_clk_period = 20;
109     // WB reset signal
110     reg          wb_reset;
111     event        reset_aserted;
112     event        reset_released;
113     event        int_aserted;
114     event        int_released;
115     // Error detection event
116     event        error_detected;
117
118   // UART register monitor
119   //#########################
120
121     // Line Status Register
122       // Reading LSR register
123       reg          lsr_reg_read;
124       // Bit 0 - Data Ready
125       reg          lsr_reg_bit0_change_allowed;
126       // Bit 1 - Overrun Error
127       reg          lsr_reg_bit1_change_allowed;
128       // Bit 2 - Parity Error
129       reg          lsr_reg_bit2_change_allowed;
130       reg   [4:0]  rx_fifo_par_rd_pointer;
131       integer      i2;
132       // Bit 3 - Framing Error
133       reg          lsr_reg_bit3_change_allowed;
134       reg   [4:0]  rx_fifo_frm_rd_pointer;
135       integer      i3;
136       // Bit 4 - Break Interrupt
137       reg          lsr_reg_bit4_change_allowed;
138       reg   [4:0]  rx_fifo_brk_rd_pointer;
139       integer      i4;
140       // Bit 5 - Transmitter Holding Register Empty
141       reg          lsr_reg_bit5_change_allowed;
142       // Bit 6 - Transmitter Empty
143       reg          lsr_reg_bit6_change_allowed;
144       // Bit 7 - Error in RX FIFO
145       reg          lsr_reg_bit7_change_allowed;
146
147   // UART transmitter monitor
148   //#########################
149
150     // TX FIFO signals
151     reg   [7:0]  tx_shift_reg;
152     reg          tx_shift_reg_empty;
153     reg          tx_start_bit_edge;
154     reg   [7:0]  tx_fifo [0:31]; 
155     reg   [4:0]  tx_fifo_wr_pointer;
156     reg   [4:0]  tx_fifo_rd_pointer;
157     reg   [4:0]  tx_fifo_status;
158
159   // UART receiver monitor
160   //######################
161
162     // RX FIFO signals
163     reg   [7:0]  rx_shift_reg;
164     reg          rx_shift_reg_full;
165     reg          rx_parity_err;
166     reg          rx_framing_err;
167     reg          rx_framing_glitch;
168     reg          rx_break_int;
169     reg          rx_overrun_err_occured;
170     reg   [7:0]  rx_fifo_data [0:31]; 
171     reg  [31:0]  rx_fifo_par; 
172     reg  [31:0]  rx_fifo_frm; 
173     reg  [31:0]  rx_fifo_brk; 
174     reg   [4:0]  rx_fifo_wr_pointer;
175     reg   [4:0]  rx_fifo_rd_pointer;
176     reg   [4:0]  rx_fifo_status;
177     reg          rx_fifo_read;
178
179   // UART register tracker
180   //######################
181
182     // Registers
183     wire  [7:0]  ier_reg;
184     wire  [7:0]  iir_reg;
185     wire  [7:0]  fcr_reg;
186     wire  [7:0]  lcr_reg;
187     wire  [7:0]  mcr_reg;
188     wire  [7:0]  lsr_reg;
189     wire  [7:0]  msr_reg;
190     wire  [7:0]  dll_reg;
191     wire  [7:0]  dlm_reg;
192     // Events
193     event        ier_reg_changed;
194     event        iir_reg_changed;
195     event        fcr_reg_changed;
196     event        lcr_reg_changed;
197     event        mcr_reg_changed;
198     event        lsr_reg_changed;
199     event        msr_reg_changed;
200     event        dll_reg_changed;
201     event        dlm_reg_changed;
202     // Register access
203     reg   [`UART_ADDR_WIDTH-1:0] reg_adr;
204     reg   [`UART_DATA_WIDTH-1:0] reg_dat;
205     reg          reg_dlab;
206     event        reg_written;
207     event        tx_reg_written;
208     event        reg_read;
209     event        rx_reg_read;
210
211
212
213 uart_top                #(`UART_DATA_WIDTH, `UART_ADDR_WIDTH) i_uart_top
214 (
215     .wb_clk_i           (wb_clk),
216     .wb_rst_i           (wb_reset),
217     .int_o              (wb_int_o),
218 // WB slave signals - 2 address locations for two registers!
219     .wb_cyc_i           (wbs_cyc_i),
220     .wb_stb_i           (wbs_stb_i),
221     .wb_we_i            (wbs_we_i),
222     .wb_sel_i           (wbs_sel_i),
223     .wb_adr_i           (wbs_adr_i),
224     .wb_dat_i           (wbs_dat_i),
225     .wb_dat_o           (wbs_dat_o),
226     .wb_ack_o           (wbs_ack_o),
227 // UART signals
228     .stx_pad_o          (stx_pad_o),
229     .srx_pad_i          (srx_pad_i),
230 // Modem signals
231     .rts_pad_o          (rts_pad_o),
232     .cts_pad_i          (cts_pad_i),
233     .dtr_pad_o          (dtr_pad_o),
234     .dsr_pad_i          (dsr_pad_i),
235     .ri_pad_i           (ri_pad_i),
236     .dcd_pad_i          (dcd_pad_i)
237 `ifdef UART_HAS_BAUDRATE_OUTPUT
238     ,
239     .baud_o             (baud_o)
240 `endif
241 ); 
242
243 uart_device             i_uart_device
244 (
245 // UART signals
246     .stx_i              (stx_pad_o),
247     .srx_o              (srx_pad_i),
248 // Modem signals    
249     .rts_i              (rts_pad_o),
250     .cts_o              (cts_pad_i),
251     .dtr_i              (dtr_pad_o),
252     .dsr_o              (dsr_pad_i),
253     .ri_o               (ri_pad_i),
254     .dcd_o              (dcd_pad_i)
255 );
256
257 wb_master_model         #(`UART_DATA_WIDTH, `UART_ADDR_WIDTH, 4) i_wb_master_model
258 (
259     .wb_rst_i           (wb_reset),
260     .wb_clk_i           (wb_clk),
261     .wbm_cyc_o          (wbs_cyc_i),
262     .wbm_cti_o          (),
263     .wbm_bte_o          (),
264     .wbm_stb_o          (wbs_stb_i),
265     .wbm_we_o           (wbs_we_i),
266     .wbm_adr_o          (wbs_adr_i),
267     .wbm_sel_o          (wbs_sel_i),
268     .wbm_dat_o          (wbs_dat_i),
269     .wbm_dat_i          (wbs_dat_o),
270     .wbm_ack_i          (wbs_ack_o),
271     .wbm_err_i          (wbs_err_o), // inactive (1'b0)
272     .wbm_rty_i          (wbs_rty_o)  // inactive (1'b0)
273 );
274
275
276 initial
277 begin:system
278   // Initial system values
279   wb_reset = 1'b1;
280   wb_clk = 1'b0;
281 end
282
283
284 // WB clock generation (DEVICE clock is generated in uart_device.v)
285 //#################################################################
286
287   // DEVICE's clock generation: 
288   //    ----------------
289   //    // rx_clk rising edge
290   //    always@(posedge rx_clk)
291   //      if (rx_clk_en)
292   //        #(T_clk_period / 2) rx_clk = 1'b0;
293   //    // rx_clk falling edge
294   //    always@(negedge rx_clk)
295   //      if (rx_clk_en)
296   //        #(T_clk_period / 2) rx_clk = 1'b1;
297   //    ----------------
298   // DEVICE's transmit clocks generation: 
299   //    ----------------
300   //    // tx_clk rising edge
301   //    always@(posedge tx_clk)
302   //      if (tx_clk_en)
303   //        #((T_clk_period / 2) * 16 * T_divisor) tx_clk = 1'b0;
304   //    // tx_clk falling edge
305   //    always@(negedge tx_clk)
306   //      if (tx_clk_en)
307   //        #((T_clk_period / 2) * 16 * T_divisor) tx_clk = 1'b1;
308   //    ----------------
309
310   // WB clock
311   always@(posedge wb_clk)
312     if (wb_clk_en)
313       #(T_wb_clk_period / 2) wb_clk = 1'b0;
314   always@(negedge wb_clk)
315     if (wb_clk_en)
316       #(T_wb_clk_period / 2) wb_clk = 1'b1;
317
318
319 // SYSTEM signals tracker
320 //#######################
321
322   // Reset
323   always@(posedge wb_reset)
324     -> reset_aserted;
325   always@(negedge wb_reset)
326     -> reset_released;
327
328   // Interrupt
329   always@(posedge wb_int_o)
330     -> int_aserted;
331   always@(negedge wb_int_o)
332     -> int_released;
333
334
335 // UART register tracker
336 //######################
337
338   // UART registers:
339   //    ----------------
340   //    RBR             (R/  | ADR 0 | DLAB 0)
341   //      [7:0] -RX---- "rxdata" Receiver Buffer Register
342   //    ----------------
343   //    THR             ( /W | ADR 0 | DLAB 0)
344   //      [7:0] ----TX- "txdata" Transmitter Holding Register
345   //    ----------------
346   //    IER             (R/W | ADR 1 | DLAB 0) 
347   //      [0]   -RX---- "1" Received Data Available & Receive Fifo Timeout
348   //      [1]   ----TX- "1" Transmitter Holding Register Empty
349   //      [2]   -RX---- "1" Receiver Line Status
350   //      [3]   -MODEM- "1" Modem Status
351   //    ----------------
352   //    IIR             (R/  | ADR 2)
353   //      [0]   ------- "0" Interrupt is Pending (decreasing priority level in following 3 bits)
354   //      [3:1] -RX---- "011" Receiver Line Status - Overrun, Parity, Framing error or Break int. ---> READ LSR
355   //      [3:1] -RX---- "010" Received Data Available - Fifo Trigger Level Reached ------------------> READ RBR (Fifo lower than trig.)
356   //      [3:1] -RX---- "110" Timeout Indication - Fifo not empty & no Fifo action for 4 char times -> READ RBR
357   //      [3:1] ----TX- "001" Transmitter Holding Register Empty - THR Empty ------------------------> READ IIR | WRITE THR
358   //      [3:1] -MODEM- "000" Modem Status - CTS, DSR, DCD changed or RI changed from '0' to '1' ----> READ MSR
359   //    ----------------
360   //    FCR             ( /W | ADR 2)
361   //      [1]   -RX---- "1" Clear only Receiver Fifo (not shift register)
362   //      [2]   ----TX- "1" Clear only Transmitter Fifo (not shift register)
363   //      [7:6] -RX---- "00"  1 BYTE  Receiver Fifo Interrupt trigger level
364   //      [7:6] -RX---- "01"  4 BYTEs Receiver Fifo Interrupt trigger level
365   //      [7:6] -RX---- "10"  8 BYTEs Receiver Fifo Interrupt trigger level
366   //      [7:6] -RX---- "11" 14 BYTEs Receiver Fifo Interrupt trigger level
367   //    ----------------
368   //    LCR             (R/W | ADR 3)
369   //      [1:0] -RX-TX- "00" 5 bits in each character
370   //      [1:0] -RX-TX- "01" 6 bits in each character
371   //      [1:0] -RX-TX- "10" 7 bits in each character
372   //      [1:0] -RX-TX- "11" 8 bits in each character
373   //      [2]   -RX-TX- "0" 1 stop bit
374   //      [2]   -RX-TX- "1" 1.5 stop bits (when 5 bits of char.) or 2 stop bits (when 6, 7 or 8 bits of char.)
375   //      [3]   -RX-TX- "1" Parity bit enabled
376   //      [5:4] -RX-TX- "00" NO Stick Parity & ODD Parity bit - ODD num. of '1's is transmitted
377   //      [5:4] -RX-TX- "01" NO Stick Parity & EVEN Parity bit - EVEN num. of '1's is transmitted
378   //      [5:4] -RX-TX- "10" Stick Parity bit - Stick '1' as Parity bit
379   //      [5:4] -RX-TX- "11" Stick Parity bit - Stick '0' as Parity bit
380   //      [6]   ----TX- "1" Break Control - Output is forced to '0'
381   //      [7]   ------- "1" DLAB - for access to DLL and DLM
382   //    ----------------
383   //    MCR             ( /W | ADR 4)
384   //      [0]   -MODEM- "1" Force DTR to '0' - in LoopBack connected to DSR input
385   //      [1]   -MODEM- "1" Force RTS to '0' - in LoopBack connected to CTS input
386   //      [2]   -MODEM- "1" Force N.C.1 to '0' - in LoopBack connected to RI input
387   //      [3]   -MODEM- "1" Force N.C.2 to '0' - in LoopBack connected to DCD input
388   //      [4]   -MODEM- "1" LoopBack mode
389   //    ----------------
390   //    LSR             (R/  | ADR 5)
391   //      [0]   -RX---- "1" Data Ready - At least 1 char. received and is in Fifo----------> READ RBR (Fifo empty)
392   //      [1]   -RX---- "1" Overrun Error - Fifo full & 1 char. received in shift reg. ----> READ LSR
393   //      [2]   -RX---- "1" Parity Error - top Fifo char. has invalid parity bit ----------> READ LSR
394   //      [3]   -RX---- "1" Framing Error - top Fifo char. has invalid stop bit -----------> READ LSR
395   //      [4]   -RX---- "1" Break Int. - top Fifo char. bits are '0' and it's ctrl. bits --> READ LSR
396   //      [5]   ----TX- "1" Transmitter Holding Register Empty - transmitter Fifo empty ---> WRITE THR
397   //      [6]   ----TX- "1" Transmitter EMpTy - transmitter Fifo empty & shift reg. empty -> WRITE THR
398   //      [7]   -RX---- "1" At least 1 Parity Error, Framing Error or Break Int. in Fifo --> READ LSR & No More Errors in Fifo
399   //    ----------------
400   //    MSR             (R/  | ADR 6)
401   //      [0]   -MODEM- "1" Delta CTS indicator - CTS has changed it's state --------------> READ MSR
402   //      [1]   -MODEM- "1" Delta DSR indicator - DSR has changed it's state --------------> READ MSR
403   //      [2]   -MODEM- "1" Trailing Edge of RI - RI has changed from '0' to '1' ----------> READ MSR
404   //      [3]   -MODEM- "1" Delta DCD indicator - DCD has changed it's state --------------> READ MSR
405   //      [4]   -MODEM- "x" Complement of CTS input | in LoopBack equal to RTS = MCR[1]
406   //      [5]   -MODEM- "x" Complement of DSR input | in LoopBack equal to DTR = MCR[0]
407   //      [6]   -MODEM- "x" Complement of RI input | in LoopBack equal to N.C.1 = MCR[2]
408   //      [7]   -MODEM- "x" Complement of DCD input | in LoopBack equal to N.C.2 = MCR[3]
409   //    ----------------
410   //    DLL             (R/W | ADR 0 | DLAB 1)
411   //      [7:0] ------- "dl[ 7:0]" LSB of DL Reg. written 2. - dl == '0' disables outputs / dl = 1/(T_wb_clk_period*16*BaudRate)
412   //    ----------------
413   //    DLM             (R/W | ADR 1 | DLAB 1)
414   //      [7:0] ------- "dl[15:8]" MSB of DL Reg. written 1. - dl == '0' disables outputs / dl = 1/(T_wb_clk_period*16*BaudRate)
415   //    ----------------
416
417   // Transparent UART registers
418   assign ier_reg[7:0] = {4'h0, testbench.i_uart_top.regs.ier      };
419   assign iir_reg[7:0] = {4'hC, testbench.i_uart_top.regs.iir      };
420   assign fcr_reg[7:0] = {      testbench.i_uart_top.regs.fcr, 6'h0};
421   assign lcr_reg[7:0] = {      testbench.i_uart_top.regs.lcr      }; // lcr_reg[7] == DLAB !!!
422   assign mcr_reg[7:0] = {3'h0, testbench.i_uart_top.regs.mcr      };
423   assign lsr_reg[7:0] = {      testbench.i_uart_top.regs.lsr      };
424   assign msr_reg[7:0] = {      testbench.i_uart_top.regs.msr      };
425   assign dll_reg[7:0] = {      testbench.i_uart_top.regs.dl[ 7:0] };
426   assign dlm_reg[7:0] = {      testbench.i_uart_top.regs.dl[15:8] };
427
428   // Tracking changes of registers
429   always@(ier_reg)
430   begin
431     -> ier_reg_changed;
432   end
433   always@(iir_reg)
434   begin
435     -> iir_reg_changed;
436   end
437   always@(fcr_reg)
438   begin
439     -> fcr_reg_changed;
440   end
441   always@(lcr_reg)
442   begin
443     -> lcr_reg_changed;
444   end
445   always@(mcr_reg)
446   begin
447     -> mcr_reg_changed;
448   end
449   always@(lsr_reg)
450   begin
451     -> lsr_reg_changed;
452   end
453   always@(msr_reg)
454   begin
455     -> msr_reg_changed;
456   end
457   always@(dll_reg)
458   begin
459     -> dll_reg_changed;
460   end
461   always@(dlm_reg)
462   begin
463     -> dlm_reg_changed;
464   end
465
466   // Tracking read/write access to registers
467   always@(wbs_cyc_i or wbs_stb_i or wbs_we_i or wbs_sel_i or wbs_adr_i or 
468           wbs_dat_i /*or wbs_ack_o*/ /*or posedge wb_clk*/)
469   begin
470     if (wbs_cyc_i && wbs_stb_i)
471     begin
472       if (wbs_we_i /*&& wbs_ack_o*/) // WRITE
473       begin
474         // LOG's example of  detecting of register write:
475         //    ----------------
476         //    case (wbs_adr_i)
477         //    `UART_REG_TR: if (lcr_reg[7]) // lcr_reg[7] == DLAB !!!
478         //                    -> dll_reg_written;
479         //                  else
480         //                    -> thr_reg_written;
481         //    `UART_REG_IE: if (lcr_reg[7]) // lcr_reg[7] == DLAB !!!
482         //                    -> dlm_reg_written;
483         //                  else
484         //                    -> ier_reg_written;
485         //    `UART_REG_FC: -> fcr_reg_written;
486         //    `UART_REG_LC: -> lcr_reg_written; 
487         //    `UART_REG_MC: -> mcr_reg_written; 
488         //    default:      -> erroneous_write_location;
489         //    endcase
490         //    ----------------
491
492         reg_adr = wbs_adr_i;
493         reg_dat = wbs_dat_i;
494         reg_dlab = lcr_reg[7];
495         -> reg_written;
496         if (~reg_dlab && (reg_adr == `UART_REG_TR)) // write to FIFO
497           -> tx_reg_written;
498       end
499     end
500   end
501   always@(wbs_cyc_i or wbs_stb_i or wbs_we_i or wbs_sel_i or wbs_adr_i or 
502           wbs_dat_o or wbs_ack_o /*or posedge wb_clk*/)
503   begin
504     if (wbs_cyc_i && wbs_stb_i)
505     begin
506       if (~wbs_we_i && wbs_ack_o) // READ
507       begin
508         // LOG's example of  detecting of register read:
509         //    ----------------
510         //    case (wbs_adr_i)
511         //    `UART_REG_RB: if (lcr_reg[7]) // lcr_reg[7] == DLAB !!!
512         //                    -> dll_reg_read;
513         //                  else
514         //                    -> rbr_reg_read;
515         //    `UART_REG_IE: if (lcr_reg[7]) // lcr_reg[7] == DLAB !!!
516         //                    -> dlm_reg_read;
517         //                  else
518         //                    -> ier_reg_read;
519         //    `UART_REG_II: -> iir_reg_read;
520         //    `UART_REG_LC: -> lcr_reg_read;
521         //    `UART_REG_LS: -> lsr_reg_read;
522         //    `UART_REG_MS: -> msr_reg_read;
523         //    default:      -> erroneous_read_location;
524         //    endcase 
525         //    ----------------
526
527         reg_adr = wbs_adr_i;
528         reg_dat = wbs_dat_o;
529         reg_dlab = lcr_reg[7];
530         -> reg_read;
531         if (~reg_dlab && (reg_adr == `UART_REG_RB))
532           -> rx_reg_read;
533       end
534     end
535   end
536
537
538 // UART register monitor
539 //#######################
540
541   // Line Status Register
542     // Reading LSR register
543     initial
544     begin
545       lsr_reg_read = 0;
546       forever
547       begin
548         @(reg_read);
549         if (reg_adr == `UART_REG_LS)
550         begin
551           lsr_reg_read = 1'b1;
552           repeat (1) @(posedge wb_clk);
553           lsr_reg_read = 0;
554         end
555       end
556     end
557     // Bit 0 - Data Ready
558     initial
559     begin
560       lsr_reg_bit0_change_allowed = 0;
561       @(reset_released);
562       #10;
563       fork
564       begin: rx_fifo_status_changing
565         forever
566         begin
567           if (rx_fifo_status == 0)
568           begin
569             wait (rx_fifo_status > 0);
570             lsr_reg_bit0_change_allowed = 1'b1;
571             repeat (1) @(posedge wb_clk);
572             #2;
573             lsr_reg_bit0_change_allowed = 0;
574             if (~lsr_reg[0])
575             begin
576               `BENCH_ERROR("Bit 0 of LSR register not '1'!");
577               -> error_detected;
578             end
579           end
580           else
581           begin
582             wait (rx_fifo_status == 0);
583             lsr_reg_bit0_change_allowed = 1'b1;
584             repeat (1) @(posedge wb_clk);
585             #2;
586             lsr_reg_bit0_change_allowed = 0;
587             if (lsr_reg[0])
588             begin
589               `BENCH_ERROR("Bit 0 of LSR register not '0'!");
590               -> error_detected;
591             end
592           end
593         end
594       end
595       begin: lsr_reg_bit0_changing
596         forever
597         begin
598           wait (~lsr_reg_bit0_change_allowed);
599           begin
600             @(lsr_reg[0] or lsr_reg_bit0_change_allowed);
601             if (~lsr_reg_bit0_change_allowed)
602             begin
603               `BENCH_ERROR("Bit 0 of LSR register should not change!");
604               -> error_detected;
605             end
606           end
607         end
608       end
609       join
610     end
611     // Bit 1 - Overrun Error
612     initial
613     begin
614       lsr_reg_bit1_change_allowed = 0;
615       @(reset_released);
616       #10;
617       fork
618       begin: rx_overrun_err_occured_changing
619         forever
620         begin
621           if (~rx_overrun_err_occured)
622           begin
623             wait (rx_overrun_err_occured);
624             lsr_reg_bit1_change_allowed = 1'b1;
625             repeat (1) @(posedge wb_clk);
626             #2;
627             lsr_reg_bit1_change_allowed = 0;
628             if (~lsr_reg[1])
629             begin
630               `BENCH_ERROR("Bit 1 of LSR register not '1'!");
631               -> error_detected;
632             end
633           end
634           else
635           begin
636             wait (lsr_reg_read);
637             lsr_reg_bit1_change_allowed = 1'b1;
638             repeat (1) @(posedge wb_clk);
639             #2;
640             lsr_reg_bit1_change_allowed = 0;
641             rx_overrun_err_occured = 0;
642             if (lsr_reg[1])
643             begin
644               `BENCH_ERROR("Bit 1 of LSR register not '0'!");
645               -> error_detected;
646             end
647           end
648         end
649       end
650       begin: lsr_reg_bit1_changing
651         forever
652         begin
653           wait (~lsr_reg_bit1_change_allowed);
654           begin
655             @(lsr_reg[1] or lsr_reg_bit1_change_allowed);
656             if (~lsr_reg_bit1_change_allowed)
657             begin
658               `BENCH_ERROR("Bit 1 of LSR register should not change!");
659               -> error_detected;
660             end
661           end
662         end
663       end
664       join
665     end
666     // Bit 2 - Parity Error
667     initial
668     begin
669       lsr_reg_bit2_change_allowed = 0;
670       rx_fifo_par_rd_pointer      = 0;
671       @(reset_released);
672       #10;
673       fork
674       begin: rx_parity_err_changing
675         forever
676         begin
677           if (~rx_fifo_par[rx_fifo_par_rd_pointer])
678           begin
679             wait (rx_fifo_read);
680             lsr_reg_bit2_change_allowed = 1'b1;
681             repeat (1) @(posedge wb_clk);
682             #2;
683             lsr_reg_bit2_change_allowed = 0;
684             rx_fifo_par_rd_pointer = rx_fifo_par_rd_pointer + 1'b1;
685             // check bit
686             if (~lsr_reg[2] && rx_fifo_par[rx_fifo_par_rd_pointer])
687             begin
688               `BENCH_ERROR("Bit 2 of LSR register not '1'!");
689               -> error_detected;
690             end
691             else if (lsr_reg[2] && ~rx_fifo_par[rx_fifo_par_rd_pointer])
692             begin
693               `BENCH_ERROR("Bit 2 of LSR register not '0'!");
694               -> error_detected;
695             end
696           end
697           else
698           begin
699             wait (lsr_reg_read);
700             lsr_reg_bit2_change_allowed = 1'b1;
701             repeat (1) @(posedge wb_clk);
702             #2;
703             lsr_reg_bit2_change_allowed = 0;
704             if (rx_fifo_par_rd_pointer < rx_fifo_rd_pointer)
705             begin
706               for (i2 = rx_fifo_par_rd_pointer; i2 <= rx_fifo_rd_pointer; i2 = i2 + 1)
707                 rx_fifo_par[i2] = 0;
708               rx_fifo_par_rd_pointer = rx_fifo_rd_pointer;
709             end
710             else if (rx_fifo_par_rd_pointer > rx_fifo_rd_pointer)
711             begin
712               for (i2 = rx_fifo_par_rd_pointer; i2 <= 31; i2 = i2 + 1)
713                 rx_fifo_par[i2] = 0;
714               for (i2 = 0; i2 <= rx_fifo_rd_pointer; i2 = i2 + 1)
715                 rx_fifo_par[i2] = 0;
716               rx_fifo_par_rd_pointer = rx_fifo_rd_pointer;
717             end
718             else
719             begin
720               rx_fifo_par = 0;
721               rx_fifo_par_rd_pointer = rx_fifo_rd_pointer;
722             end
723             // check bit
724             if (~lsr_reg[2] && rx_fifo_par[rx_fifo_par_rd_pointer])
725             begin
726               `BENCH_ERROR("Bit 2 of LSR register not '1'!");
727               -> error_detected;
728             end
729             else if (lsr_reg[2] && ~rx_fifo_par[rx_fifo_par_rd_pointer])
730             begin
731               `BENCH_ERROR("Bit 2 of LSR register not '0'!");
732               -> error_detected;
733             end
734           end
735         end
736       end
737       begin: lsr_reg_bit2_changing
738         forever
739         begin
740           wait (~lsr_reg_bit2_change_allowed);
741           begin
742             @(lsr_reg[2] or lsr_reg_bit2_change_allowed);
743             if (~lsr_reg_bit2_change_allowed)
744             begin
745               `BENCH_ERROR("Bit 2 of LSR register should not change!");
746               -> error_detected;
747             end
748           end
749         end
750       end
751       join
752     end
753     // Bit 3 - Framing Error
754     initial
755     begin
756       lsr_reg_bit3_change_allowed = 0;
757       rx_fifo_frm_rd_pointer      = 0;
758       @(reset_released);
759       #10;
760       fork
761       begin: rx_framing_err_changing
762         forever
763         begin
764           if (~rx_fifo_frm[rx_fifo_frm_rd_pointer])
765           begin
766             wait (rx_fifo_read);
767             lsr_reg_bit3_change_allowed = 1'b1;
768             repeat (1) @(posedge wb_clk);
769             #2;
770             lsr_reg_bit3_change_allowed = 0;
771             rx_fifo_frm_rd_pointer = rx_fifo_frm_rd_pointer + 1'b1;
772             // check bit
773             if (~lsr_reg[3] && rx_fifo_frm[rx_fifo_frm_rd_pointer])
774             begin
775               `BENCH_ERROR("Bit 3 of LSR register not '1'!");
776               -> error_detected;
777             end
778             else if (lsr_reg[3] && ~rx_fifo_frm[rx_fifo_frm_rd_pointer])
779             begin
780               `BENCH_ERROR("Bit 3 of LSR register not '0'!");
781               -> error_detected;
782             end
783           end
784           else
785           begin
786             wait (lsr_reg_read);
787             lsr_reg_bit3_change_allowed = 1'b1;
788             repeat (1) @(posedge wb_clk);
789             #2;
790             lsr_reg_bit3_change_allowed = 0;
791             if (rx_fifo_frm_rd_pointer < rx_fifo_rd_pointer)
792             begin
793               for (i3 = rx_fifo_frm_rd_pointer; i3 <= rx_fifo_rd_pointer; i3 = i3 + 1)
794                 rx_fifo_frm[i3] = 0;
795               rx_fifo_frm_rd_pointer = rx_fifo_rd_pointer;
796             end
797             else if (rx_fifo_frm_rd_pointer > rx_fifo_rd_pointer)
798             begin
799               for (i3 = rx_fifo_frm_rd_pointer; i3 <= 31; i3 = i3 + 1)
800                 rx_fifo_frm[i3] = 0;
801               for (i3 = 0; i3 <= rx_fifo_rd_pointer; i3 = i3 + 1)
802                 rx_fifo_frm[i3] = 0;
803               rx_fifo_frm_rd_pointer = rx_fifo_rd_pointer;
804             end
805             else
806             begin
807               rx_fifo_frm = 0;
808               rx_fifo_frm_rd_pointer = rx_fifo_rd_pointer;
809             end
810             // check bit
811             if (~lsr_reg[3] && rx_fifo_frm[rx_fifo_frm_rd_pointer])
812             begin
813               `BENCH_ERROR("Bit 3 of LSR register not '1'!");
814               -> error_detected;
815             end
816             else if (lsr_reg[3] && ~rx_fifo_frm[rx_fifo_frm_rd_pointer])
817             begin
818               `BENCH_ERROR("Bit 3 of LSR register not '0'!");
819               -> error_detected;
820             end
821           end
822         end
823       end
824       begin: lsr_reg_bit3_changing
825         forever
826         begin
827           wait (~lsr_reg_bit3_change_allowed);
828           begin
829             @(lsr_reg[3] or lsr_reg_bit3_change_allowed);
830             if (~lsr_reg_bit3_change_allowed)
831             begin
832               `BENCH_ERROR("Bit 3 of LSR register should not change!");
833               -> error_detected;
834             end
835           end
836         end
837       end
838       join
839     end
840     // Bit 4 - Break Interrupt
841     initial
842     begin
843       lsr_reg_bit4_change_allowed = 0;
844       rx_fifo_brk_rd_pointer      = 0;
845       @(reset_released);
846       #10;
847       fork
848       begin: rx_break_int_changing
849         forever
850         begin
851           if (~rx_fifo_brk[rx_fifo_brk_rd_pointer])
852           begin
853             wait (rx_fifo_read);
854             lsr_reg_bit4_change_allowed = 1'b1;
855             repeat (1) @(posedge wb_clk);
856             #2;
857             lsr_reg_bit4_change_allowed = 0;
858             rx_fifo_brk_rd_pointer = rx_fifo_brk_rd_pointer + 1'b1;
859             // check bit
860             if (~lsr_reg[4] && rx_fifo_brk[rx_fifo_brk_rd_pointer])
861             begin
862               `BENCH_ERROR("Bit 4 of LSR register not '1'!");
863               -> error_detected;
864             end
865             else if (lsr_reg[4] && ~rx_fifo_brk[rx_fifo_brk_rd_pointer])
866             begin
867               `BENCH_ERROR("Bit 4 of LSR register not '0'!");
868               -> error_detected;
869             end
870           end
871           else
872           begin
873             wait (lsr_reg_read);
874             lsr_reg_bit4_change_allowed = 1'b1;
875             repeat (1) @(posedge wb_clk);
876             #2;
877             lsr_reg_bit4_change_allowed = 0;
878             if (rx_fifo_brk_rd_pointer < rx_fifo_rd_pointer)
879             begin
880               for (i4 = rx_fifo_brk_rd_pointer; i4 <= rx_fifo_rd_pointer; i4 = i4 + 1)
881                 rx_fifo_brk[i4] = 0;
882               rx_fifo_brk_rd_pointer = rx_fifo_rd_pointer;
883             end
884             else if (rx_fifo_brk_rd_pointer > rx_fifo_rd_pointer)
885             begin
886               for (i4 = rx_fifo_brk_rd_pointer; i4 <= 31; i4 = i4 + 1)
887                 rx_fifo_brk[i4] = 0;
888               for (i4 = 0; i4 <= rx_fifo_rd_pointer; i4 = i4 + 1)
889                 rx_fifo_brk[i4] = 0;
890               rx_fifo_brk_rd_pointer = rx_fifo_rd_pointer;
891             end
892             else
893             begin
894               rx_fifo_brk = 0;
895               rx_fifo_brk_rd_pointer = rx_fifo_rd_pointer;
896             end
897             // check bit
898             if (~lsr_reg[4] && rx_fifo_brk[rx_fifo_brk_rd_pointer])
899             begin
900               `BENCH_ERROR("Bit 4 of LSR register not '1'!");
901               -> error_detected;
902             end
903             else if (lsr_reg[4] && ~rx_fifo_brk[rx_fifo_brk_rd_pointer])
904             begin
905               `BENCH_ERROR("Bit 4 of LSR register not '0'!");
906               -> error_detected;
907             end
908           end
909         end
910       end
911       begin: lsr_reg_bit4_changing
912         forever
913         begin
914           wait (~lsr_reg_bit4_change_allowed);
915           begin
916             @(lsr_reg[4] or lsr_reg_bit4_change_allowed);
917             if (~lsr_reg_bit4_change_allowed)
918             begin
919               `BENCH_ERROR("Bit 4 of LSR register should not change!");
920               -> error_detected;
921             end
922           end
923         end
924       end
925       join
926     end
927     // Bit 5 - Transmitter Holding Register Empty
928     initial
929     begin
930       lsr_reg_bit5_change_allowed = 0;
931       @(reset_released);
932       #10;
933       fork
934       begin: tx_fifo_status_changing
935         forever
936         begin
937           if (tx_fifo_status == 0)
938           begin
939 //            @(tx_reg_written);
940             wait (tx_fifo_status > 0);
941             lsr_reg_bit5_change_allowed = 1'b1;
942             repeat (3) @(posedge wb_clk);
943             #2;
944             lsr_reg_bit5_change_allowed = 0;
945             if (lsr_reg[5])
946             begin
947               `BENCH_ERROR("Bit 5 of LSR register not '0'!");
948               -> error_detected;
949             end
950           end
951           else
952           begin
953             wait (tx_fifo_status == 0);
954             lsr_reg_bit5_change_allowed = 1'b1;
955             repeat (3) @(posedge wb_clk);
956             #2;
957             lsr_reg_bit5_change_allowed = 0;
958             if (~lsr_reg[5])
959             begin
960               `BENCH_ERROR("Bit 5 of LSR register not '1'!");
961               -> error_detected;
962             end
963           end
964         end
965       end
966       begin: lsr_reg_bit5_changing
967         forever
968         begin
969           wait (~lsr_reg_bit5_change_allowed);
970           begin
971             @(lsr_reg[5] or lsr_reg_bit5_change_allowed);
972             if (~lsr_reg_bit5_change_allowed)
973             begin
974               `BENCH_ERROR("Bit 5 of LSR register should not change!");
975               -> error_detected;
976             end
977           end
978         end
979       end
980       join
981     end
982     // Bit 6 - Transmitter Empty
983     initial
984     begin
985       lsr_reg_bit6_change_allowed = 0;
986       @(reset_released);
987       #10;
988       fork
989       begin: tx_fifo_status_and_shift_reg_changing
990         forever
991         begin
992           if ((tx_fifo_status == 0) && tx_shift_reg_empty)
993           begin
994 //            @(tx_reg_written);
995             wait (tx_fifo_status > 0);
996             lsr_reg_bit6_change_allowed = 1'b1;
997             repeat (3) @(posedge wb_clk);
998             #2;
999             lsr_reg_bit6_change_allowed = 0;
1000             if (lsr_reg[6])
1001             begin
1002               `BENCH_ERROR("Bit 6 of LSR register not '0'!");
1003               -> error_detected;
1004             end
1005           end
1006           else
1007           begin
1008             wait ((tx_fifo_status == 0) && tx_shift_reg_empty);
1009             lsr_reg_bit6_change_allowed = 1'b1;
1010             repeat (3) @(posedge wb_clk);
1011             #2;
1012             lsr_reg_bit6_change_allowed = 0;
1013             if (~lsr_reg[6])
1014             begin
1015               `BENCH_ERROR("Bit 6 of LSR register not '1'!");
1016               -> error_detected;
1017             end
1018           end
1019         end
1020       end
1021       begin: lsr_reg_bit6_changing
1022         forever
1023         begin
1024           wait (~lsr_reg_bit6_change_allowed);
1025           begin
1026             @(lsr_reg[6] or lsr_reg_bit6_change_allowed);
1027             if (~lsr_reg_bit6_change_allowed)
1028             begin
1029               `BENCH_ERROR("Bit 6 of LSR register should not change!");
1030               -> error_detected;
1031             end
1032           end
1033         end
1034       end
1035       join
1036     end
1037     // Bit 7 - Error in RX FIFO
1038     initial
1039     begin
1040       lsr_reg_bit7_change_allowed = 0;
1041       @(reset_released);
1042       #10;
1043       fork
1044       begin: error_changing
1045         forever
1046         begin
1047           if ((rx_fifo_par == 0) && (rx_fifo_frm == 0) && (rx_fifo_brk == 0))
1048           begin
1049             wait (rx_parity_err || rx_framing_err || rx_framing_glitch || rx_break_int);
1050             lsr_reg_bit7_change_allowed = 1'b1;
1051             repeat (3) @(posedge wb_clk);
1052             #2;
1053             lsr_reg_bit7_change_allowed = 0;
1054             // check bit
1055             if (~lsr_reg[7])
1056             begin
1057               `BENCH_ERROR("Bit 7 of LSR register not '1'!");
1058               -> error_detected;
1059             end
1060           end
1061           else
1062           begin
1063             wait (lsr_reg_read && (rx_fifo_par == 0) && (rx_fifo_frm == 0) && (rx_fifo_brk == 0));
1064             lsr_reg_bit7_change_allowed = 1'b1;
1065             repeat (2) @(posedge wb_clk);
1066             #2;
1067             lsr_reg_bit7_change_allowed = 0;
1068             // check bit
1069             if (lsr_reg[7])
1070             begin
1071               `BENCH_ERROR("Bit 7 of LSR register not '0'!");
1072               -> error_detected;
1073             end
1074           end
1075         end
1076       end
1077       begin: lsr_reg_bit7_changing
1078         forever
1079         begin
1080           wait (~lsr_reg_bit7_change_allowed);
1081           begin
1082             @(lsr_reg[7] or lsr_reg_bit7_change_allowed);
1083             if (~lsr_reg_bit7_change_allowed)
1084             begin
1085               `BENCH_ERROR("Bit 7 of LSR register should not change!");
1086               -> error_detected;
1087             end
1088           end
1089         end
1090       end
1091       join
1092     end
1093
1094
1095 // UART transmitter monitor
1096 //#########################
1097
1098   // TX FIFO status
1099   always@(tx_fifo_wr_pointer or tx_fifo_rd_pointer)
1100   begin
1101     if (tx_fifo_wr_pointer >= tx_fifo_rd_pointer)
1102       tx_fifo_status = tx_fifo_wr_pointer - tx_fifo_rd_pointer;
1103     else
1104       tx_fifo_status = (5'h1F - tx_fifo_rd_pointer) + tx_fifo_wr_pointer;
1105   end
1106   // TX FIFO and TX data
1107   initial
1108   begin
1109     tx_fifo_wr_pointer = 0;
1110     tx_fifo_rd_pointer = 0;
1111     tx_shift_reg_empty = 1;
1112     tx_fifo_status     = 0;
1113     tx_start_bit_edge  = 1;
1114     fork
1115     begin:write_tx_shift_reg_read_tx_fifo
1116       forever
1117       begin
1118         wait ((tx_fifo_status !== 0) && tx_shift_reg_empty && tx_start_bit_edge && ~stx_pad_o);
1119         tx_start_bit_edge  = 0;
1120         tx_shift_reg = tx_fifo[tx_fifo_rd_pointer];
1121         tx_shift_reg_empty = 0;
1122         @(testbench.i_uart_device.device_received_last_bit);
1123         repeat (16393) @(posedge wb_clk);
1124         tx_fifo_rd_pointer = tx_fifo_rd_pointer + 1'b1;
1125         @(posedge wb_clk);
1126         if (tx_fifo_status == 0)
1127         begin
1128           `BENCH_MSG("TX FIFO is empty!");
1129         end
1130       end
1131     end
1132     begin:write_tx_fifo
1133       forever
1134       begin
1135         @(tx_reg_written); // write to FIFO
1136         repeat (1) @(posedge wb_clk); // delay when writing into registers
1137         if (tx_fifo_status <= 5'h0F)
1138         begin
1139           tx_fifo[tx_fifo_wr_pointer] = reg_dat;
1140           tx_fifo_wr_pointer = tx_fifo_wr_pointer + 1'b1;
1141         end
1142         else // FIFO overflow
1143         begin
1144           `BENCH_WARNING("TX FIFO overflow!");
1145         end
1146       end
1147     end
1148     begin:empty_tx_fifo
1149       forever
1150       begin
1151         wait (fcr_reg[2]);
1152         tx_fifo_wr_pointer = 0;
1153         tx_fifo_rd_pointer = 0;
1154         @(posedge wb_clk);
1155         if (tx_fifo_status == 0)
1156         begin
1157           `BENCH_MSG("TX FIFO is empty!");
1158         end
1159       end
1160     end
1161     begin:read_tx_shift_reg
1162       forever
1163       begin
1164         @(testbench.i_uart_device.device_received_packet);
1165         // Check data
1166         if (tx_shift_reg != testbench.i_uart_device.rx_data)
1167         begin
1168           `BENCH_ERROR("TX data has ERROR!");
1169           -> error_detected;
1170         end
1171         else
1172           `BENCH_MSG("TX data correct!");
1173         if (testbench.i_uart_device.rx_parity_error)
1174         begin
1175           `BENCH_ERROR("TX data has parity ERROR!");
1176           -> error_detected;
1177         end
1178         else
1179           `BENCH_MSG("TX data parity correct!");
1180         if (testbench.i_uart_device.rx_framing_error)
1181         begin
1182           `BENCH_ERROR("TX data has framing ERROR!");
1183           -> error_detected;
1184         end
1185         else
1186           `BENCH_MSG("TX data framing correct!");
1187         // Set TX FIFO read pointer
1188         tx_start_bit_edge  = 1;
1189         repeat (7) @(wb_clk);
1190         if (tx_shift_reg_empty == 0)
1191         begin
1192           tx_shift_reg_empty = 1'b1;
1193         end
1194         else
1195         begin
1196           `BENCH_ERROR("TX shift register empty while transmiting data!");
1197           -> error_detected;
1198         end
1199       end
1200     end
1201     join
1202   end
1203
1204
1205 // UART receiver monitor
1206 //######################
1207
1208   // RX FIFO status
1209   always@(rx_fifo_wr_pointer or rx_fifo_rd_pointer)
1210   begin
1211     if (rx_fifo_wr_pointer >= rx_fifo_rd_pointer)
1212       rx_fifo_status = rx_fifo_wr_pointer - rx_fifo_rd_pointer;
1213     else
1214       rx_fifo_status = (5'h1F - rx_fifo_rd_pointer) + rx_fifo_wr_pointer;
1215   end
1216   // RX FIFO and RX data
1217   initial
1218   begin
1219     rx_parity_err      = 0;
1220     rx_framing_err     = 0;
1221     rx_framing_glitch  = 0;
1222     rx_break_int       = 0;
1223     rx_overrun_err_occured = 0;
1224     rx_fifo_par        = 0;
1225     rx_fifo_frm        = 0;
1226     rx_fifo_brk        = 0;
1227     rx_shift_reg_full  = 0;
1228     rx_fifo_wr_pointer = 0;
1229     rx_fifo_rd_pointer = 0;
1230     rx_fifo_status     = 0;
1231     fork
1232     begin:write_rx_shift_reg
1233       forever
1234       begin
1235         @(testbench.i_uart_device.device_sent_packet);
1236         repeat (1) @(posedge wb_clk);
1237         rx_shift_reg   = testbench.i_uart_device.sent_data;
1238         rx_parity_err  = testbench.i_uart_device.tx_parity_enabled && 
1239                          (testbench.i_uart_device.tx_parity_wrong || 
1240                           (  // sample point is BIT_NUM * 2 - 1 => 3, 5, 7...
1241                            ((testbench.i_uart_device.tx_glitch_num == (3 * 8 * testbench.i_uart_device.T_divisor)) ||
1242                             (testbench.i_uart_device.tx_glitch_num == (5 * 8 * testbench.i_uart_device.T_divisor)) ||
1243                             (testbench.i_uart_device.tx_glitch_num == (7 * 8 * testbench.i_uart_device.T_divisor)) ||
1244                             (testbench.i_uart_device.tx_glitch_num == (9 * 8 * testbench.i_uart_device.T_divisor)) ||
1245                             (testbench.i_uart_device.tx_glitch_num == (11 * 8 * testbench.i_uart_device.T_divisor)) ||
1246                             (testbench.i_uart_device.tx_glitch_num == (13 * 8 * testbench.i_uart_device.T_divisor)) ||
1247                             (testbench.i_uart_device.tx_glitch_num == (15 * 8 * testbench.i_uart_device.T_divisor)) ||
1248                             (testbench.i_uart_device.tx_glitch_num == (17 * 8 * testbench.i_uart_device.T_divisor)) ||
1249                             (testbench.i_uart_device.tx_glitch_num == (19 * 8 * testbench.i_uart_device.T_divisor)) ||
1250                             (testbench.i_uart_device.tx_glitch_num == (21 * 8 * testbench.i_uart_device.T_divisor)) ||
1251                             (testbench.i_uart_device.tx_glitch_num == (23 * 8 * testbench.i_uart_device.T_divisor))) &&
1252                            (testbench.i_uart_device.tx_glitch_num[23:0] < ((testbench.i_uart_device.tx_length + 2'h1) *
1253                              16 * testbench.i_uart_device.T_divisor))
1254                          ));
1255         rx_framing_err = testbench.i_uart_device.tx_framing_wrong;
1256         rx_framing_glitch = (testbench.i_uart_device.tx_glitch_num == ((((testbench.i_uart_device.tx_length + 2'h2 +
1257                                testbench.i_uart_device.tx_parity_enabled) * 
1258                                 2) - 1'b1) * 8 * testbench.i_uart_device.T_divisor));
1259         rx_break_int   = testbench.i_uart_device.tx_break_enable && 
1260                          (testbench.i_uart_device.tx_break_num[15:0] >= ((testbench.i_uart_device.tx_length + 2'h2 +
1261                             testbench.i_uart_device.tx_parity_enabled) * 
1262                            16 * testbench.i_uart_device.T_divisor));
1263         -> testbench.i_uart_device.sent_packet_received;
1264         if (rx_fifo_status > 5'h0F)
1265           rx_overrun_err_occured = 1'b1;
1266         rx_shift_reg_full = 1'b1;
1267       end
1268     end
1269     begin:write_rx_fifo_read_rx_shift_reg
1270       forever
1271       begin
1272         wait (rx_shift_reg_full);
1273         if (rx_fifo_status <= 5'h0F)
1274         begin
1275           rx_fifo_data[rx_fifo_wr_pointer] = testbench.i_uart_device.sent_data;
1276           rx_fifo_par[rx_fifo_wr_pointer]  = rx_parity_err; 
1277           rx_fifo_frm[rx_fifo_wr_pointer]  = rx_framing_err || rx_framing_glitch; 
1278           rx_fifo_brk[rx_fifo_wr_pointer]  = rx_break_int; 
1279           rx_fifo_wr_pointer = rx_fifo_wr_pointer + 1'b1;
1280         end
1281         else // FIFO overflow
1282         begin
1283           `BENCH_WARNING("RX FIFO overflow!");
1284         end
1285         repeat (1) @(posedge wb_clk);
1286         rx_shift_reg_full = 0;
1287       end
1288     end
1289     begin:empty_rx_fifo
1290       forever
1291       begin
1292         wait (fcr_reg[1]);
1293         rx_fifo_wr_pointer = 0;
1294         rx_fifo_rd_pointer = 0;
1295 //        rx_fifo_par        = 0;
1296 //        rx_fifo_frm        = 0;
1297 //        rx_fifo_brk        = 0;
1298         @(posedge wb_clk);
1299         if (rx_fifo_status == 0)
1300         begin
1301           `BENCH_MSG("RX FIFO is empty!");
1302         end
1303       end
1304     end
1305     begin:read_rx_fifo
1306       rx_fifo_read = 0;
1307       forever
1308       begin
1309         @(rx_reg_read);
1310         if (rx_fifo_status > 0)
1311         begin
1312           rx_fifo_read = 1'b1;
1313           // Check data
1314           if (rx_fifo_data[rx_fifo_rd_pointer] != reg_dat)
1315           begin
1316             `BENCH_ERROR("RX data has ERROR!");
1317             -> error_detected;
1318           end
1319           else
1320           begin
1321             `BENCH_MSG("RX data correct!");
1322           end
1323           // Set RX FIFO read pointer
1324           repeat (1) @(posedge wb_clk);
1325           rx_fifo_read = 0;
1326           rx_fifo_rd_pointer = rx_fifo_rd_pointer + 1'b1;
1327         end
1328         else
1329         begin
1330           `BENCH_WARNING("Reading RX FIFO while RX FIFO is empty!");
1331         end
1332
1333
1334         if ((~rx_fifo_frm[rx_fifo_rd_pointer] && lsr_reg[3]) ||
1335             (rx_fifo_frm[rx_fifo_rd_pointer] && ~lsr_reg[3]))
1336         begin
1337           `BENCH_ERROR("RX data has wrong framing ERROR!");
1338           -> error_detected;
1339         end
1340         else
1341           `BENCH_MSG("RX data has correct framing error!");
1342         // Set RX FIFO read pointer
1343         repeat (1) @(posedge wb_clk);
1344         rx_fifo_read = 0;
1345         if (rx_fifo_status > 0)
1346         begin
1347 //          rx_fifo_par[rx_fifo_rd_pointer] = 1'b0; 
1348 //          rx_fifo_frm[rx_fifo_rd_pointer] = 1'b0; 
1349 //          rx_fifo_brk[rx_fifo_rd_pointer] = 1'b0; 
1350           rx_fifo_rd_pointer = rx_fifo_rd_pointer + 1'b1;
1351         end
1352       end
1353     end
1354     join
1355   end
1356
1357
1358 // UART interrupt monitor
1359 //#######################
1360
1361
1362
1363
1364 endmodule
1365
1366