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

Subversion Repositories turbo8051

[/] [turbo8051/] [trunk/] [verif/] [agents/] [uart/] [uart_agent.v] - Blame information for rev 79

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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