OpenCores
URL https://opencores.org/ocsvn/oms8051mini/oms8051mini/trunk

Subversion Repositories oms8051mini

[/] [oms8051mini/] [trunk/] [verif/] [agents/] [uart/] [uart_agent.v] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dinesha
 
2 7 dinesha
`include "tb_defines.v"
3 2 dinesha
 
4
module uart_agent (
5
        test_clk,
6
        sin,
7
        dsr_n,
8
        cts_n,
9
        dcd_n,
10
 
11
        sout,
12
        dtr_n,
13
        rts_n,
14
        out1_n,
15
        out2_n);
16
 
17
input   test_clk;
18
output  sin;
19
output  dsr_n;
20
output  cts_n;
21
output  dcd_n;
22
 
23
input   sout;
24
input   dtr_n;
25
input   rts_n;
26
input   out1_n;
27
input   out2_n;
28
 
29
event   uart_read_done, uart_write_done;
30
event   error_detected,uart_parity_error, uart_stop_error1, uart_stop_error2;
31
event   uart_timeout_error;
32
event   abort;
33
 
34
reg [15:0] rx_count;
35
reg [15:0] tx_count;
36
reg [15:0] par_err_count;
37
reg [15:0] stop_err1_cnt;
38
reg [15:0] stop_err2_cnt;
39
reg [15:0] timeout_err_cnt;
40
reg [15:0] err_cnt;
41
 
42
reg        sin, read, write;
43
reg        dcd_n;
44
reg        dsr_n, cts_n;
45
wire       test_rx_clk;
46
reg        test_tx_clk;
47
reg        stop_err_check;
48
 
49
integer timeout_count;
50
integer data_bit_number;
51 7 dinesha
reg [15:0] clk_count;
52 2 dinesha
 
53
reg      error_ind; // 1 indicate error
54
 
55
initial
56
begin
57
        sin = 1'b1;
58
        dsr_n = 1'b1;
59
        cts_n = 1'b1;
60
        dcd_n = 1'b1;
61
        test_tx_clk = 0;
62
        clk_count = 0;
63
        stop_err_check = 0;
64
  error_ind = 0;
65
end
66
 
67
always @(posedge test_clk)
68
begin
69 7 dinesha
   if (clk_count == 'h0) begin
70
      test_tx_clk = ~test_tx_clk;
71
      clk_count = control_setup.divisor;
72
   end else begin
73
      clk_count = clk_count - 1;
74
   end
75 2 dinesha
end
76
assign test_rx_clk = ~test_tx_clk;
77
 
78
always @(posedge test_clk)
79
begin
80
        timeout_count = timeout_count + 1;
81
        if (timeout_count == (control_setup.maxtime * 16))
82
                -> abort;
83
end
84
 
85
always @uart_read_done
86
        rx_count = rx_count + 1;
87
 
88
always @uart_write_done
89
        tx_count = tx_count + 1;
90
 
91
always @uart_parity_error begin
92
  error_ind = 1;
93
        par_err_count = par_err_count + 1;
94
end
95
 
96
always @uart_stop_error1 begin
97
  error_ind = 1;
98
        stop_err1_cnt = stop_err1_cnt + 1;
99
end
100
 
101
always @uart_stop_error2 begin
102
  error_ind = 1;
103
        stop_err2_cnt = stop_err2_cnt + 1;
104
end
105
 
106
always @uart_timeout_error begin
107
  error_ind = 1;
108
        timeout_err_cnt = timeout_err_cnt + 1;
109
end
110
 
111
 
112
always @error_detected begin
113 7 dinesha
    error_ind = 1;
114
    err_cnt = err_cnt + 1;
115
    `TB_GLBL.test_err;
116 2 dinesha
end
117
 
118
 
119
////////////////////////////////////////////////////////////////////////////////
120
task uart_init;
121
begin
122
  read = 0;
123
  write = 0;
124 7 dinesha
  tx_count = 0;
125
  rx_count = 0;
126 2 dinesha
  stop_err_check = 0;
127
  par_err_count = 0;
128
  stop_err1_cnt = 0;
129
  stop_err2_cnt = 0;
130
  timeout_err_cnt = 0;
131
  err_cnt = 0;
132
 
133
end
134
endtask
135
 
136
 
137
////////////////////////////////////////////////////////////////////////////////
138
task read_char_chk;
139
input   expected_data;
140
 
141
integer i;
142
reg     [7:0] expected_data;
143
reg     [7:0] data;
144
reg     parity;
145
 
146
begin
147
        data <= 8'h0;
148
        parity <= 1;
149
        timeout_count = 0;
150
 
151
fork
152
   begin : loop_1
153
        @(abort)
154
         $display (">>>>>  Exceed time limit, uart no responce.\n");
155
         ->uart_timeout_error;
156
         disable loop_2;
157
   end
158
 
159
   begin : loop_2
160
 
161
// start cycle
162
        @(negedge sout)
163
         disable loop_1;
164
         read <= 1;
165
 
166
// data cycle
167
        @(posedge test_rx_clk);
168
         for (i = 0; i < data_bit_number; i = i + 1)
169
          begin
170
            @(posedge test_rx_clk)
171
            data[i] <=  sout;
172
            parity <= parity ^ sout;
173
          end
174
 
175
// parity cycle
176
        if(control_setup.parity_en)
177
        begin
178
          @(posedge test_rx_clk);
179
          if ((control_setup.even_odd_parity && (sout == parity)) ||
180
             (!control_setup.even_odd_parity && (sout != parity)))
181
// || (control_setup.stick_parity && (sout == control_setup.even_odd_parity)))
182
             begin
183
                $display (">>>>>  Parity Error");
184
                -> error_detected;
185
                -> uart_parity_error;
186
             end
187
        end
188
 
189
// stop cycle 1
190
        @(posedge test_rx_clk);
191
          if (!sout)
192
             begin
193
                $display (">>>>>  Stop signal 1 Error");
194
                -> error_detected;
195
                -> uart_stop_error1;
196
             end
197
 
198
// stop cycle 2
199
        if (control_setup.stop_bit_number)
200
        begin
201
              @(posedge test_rx_clk);   // stop cycle 2
202
                if (!sout)
203
                  begin
204
                    $display (">>>>>  Stop signal 2 Error");
205
                    -> error_detected;
206
                    -> uart_stop_error2;
207
                  end
208
        end
209
 
210
/*      Who Cares
211
// the stop bits transmitted is one and a half if it is 5-bit
212
        if (data_bit_number == 5)
213
        begin
214
                @(posedge test_rx_clk); // stop cycle for 5-bit/per char
215
                if (!sout)
216
                  begin
217
                    $display (">>>>>  Stop signal 2 Error (5-Bit)");
218
                    -> error_detected;
219
                    -> uart_stop_error2;
220
                  end
221
        end
222
        else
223
*/
224
 
225
// wait another half cycle for tx_done signal
226
                @(negedge test_rx_clk);
227
        read <= 0;
228
        -> uart_read_done;
229
 
230
        if (expected_data != data)
231
        begin
232
                $display ("Error! Data return is %h, expecting %h", data, expected_data);
233
                -> error_detected;
234
        end
235
        else
236
                $display ("(%m) Data match  %h", expected_data);
237
 
238
        $display ("... Read Data from UART done cnt :%d...",rx_count +1);
239
   end
240
join
241
 
242
end
243
 
244
endtask
245
 
246
 
247
////////////////////////////////////////////////////////////////////////////////
248
task write_char;
249
input [7:0] data;
250
 
251
integer i;
252
reg parity;     // 0: odd parity, 1: even parity
253
 
254
begin
255
        parity <=  #1 1;
256
 
257
// start cycle
258
        @(posedge test_tx_clk)
259
         begin
260
                sin <= #1 0;
261
                write <= #1 1;
262
         end
263
 
264
// data cycle
265
        begin
266
           for (i = 0; i < data_bit_number; i = i + 1)
267
           begin
268
                @(posedge test_tx_clk)
269
                    sin <= #1 data[i];
270
                parity <= parity ^ data[i];
271
           end
272
        end
273
 
274
// parity cycle
275
        if (control_setup.parity_en)
276
        begin
277
                @(posedge test_tx_clk)
278
                        sin <= #1
279
//                              control_setup.stick_parity ? ~control_setup.even_odd_parity : 
280
                                control_setup.even_odd_parity ? !parity : parity;
281
        end
282
 
283
// stop cycle 1
284
        @(posedge test_tx_clk)
285
                sin <= #1 stop_err_check ? 0 : 1;
286
 
287
// stop cycle 2
288
        @(posedge test_tx_clk);
289
                sin <= #1 1;
290
        if (data_bit_number == 5)
291
                @(negedge test_tx_clk);
292
        else if (control_setup.stop_bit_number)
293
                @(posedge test_tx_clk);
294
 
295
        write <= #1 0;
296
        $display ("... Write data %h to UART done cnt : %d ...\n", data,tx_count+1);
297
        -> uart_write_done;
298
end
299
endtask
300
 
301
 
302
////////////////////////////////////////////////////////////////////////////////
303
task control_setup;
304
input     [1:0] data_bit_set;
305
input           stop_bit_number;
306
input           parity_en;
307
input           even_odd_parity;
308
input           stick_parity;
309
input    [15:0] maxtime;
310
input    [15:0] divisor;
311
input           fifo_enable;
312
 
313
begin
314 7 dinesha
        clk_count = divisor;
315 2 dinesha
        data_bit_number = data_bit_set + 5;
316
end
317
endtask
318
 
319
 
320
////////////////////////////////////////////////////////////////////////////////
321
task report_status;
322
output  [15:0] rx_nu;
323
output  [15:0] tx_nu;
324
begin
325
        $display ("-------------------- Reporting Configuration --------------------");
326
        $display ("     Data bit number setting is : %0d", data_bit_number);
327
        $display ("     Stop bit number setting is : %0d", control_setup.stop_bit_number + 1);
328
        $display ("     Divisor of Uart clock   is : %0d", control_setup.divisor);
329
        if (control_setup.parity_en)
330
        $display ("     Parity is enable");
331
        else
332
        $display ("     Parity is disable");
333
 
334
        if (control_setup.even_odd_parity)
335
        $display ("     Even parity setting");
336
        else
337
        $display ("     Odd parity setting");
338
 
339
/*
340
        if (control_setup.stick_parity)
341
        $display ("     Parity stick bit is on");
342
        else
343
        $display ("     Parity stick bit is off");
344
*/
345
 
346
        if (control_setup.fifo_enable)
347
        $display ("     FIFO mode is enable");
348
        else
349
        $display ("     FIFO mode is disable");
350
 
351
        $display ("-----------------------------------------------------------------");
352
 
353
        $display ("-------------------- Reporting Status --------------------\n");
354
        $display ("     Number of character received is : %d", rx_count);
355
        $display ("     Number of character sent     is : %d", tx_count);
356
        $display ("     Number of parity error rxd   is : %d", par_err_count);
357
        $display ("     Number of stop1  error rxd   is : %d", stop_err1_cnt);
358
        $display ("     Number of stop2  error rxd   is : %d", stop_err2_cnt);
359
        $display ("     Number of timeout error      is : %d", timeout_err_cnt);
360
        $display ("     Number of error              is : %d", err_cnt);
361
        $display ("-----------------------------------------------------------------");
362
 
363
        rx_nu = rx_count;
364
        tx_nu = tx_count;
365
end
366
endtask
367
 
368
 
369
////////////////////////////////////////////////////////////////////////////////
370
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.