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

Subversion Repositories turbo8051

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

Go to most recent revision | 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
input   expected_data;
136
 
137
integer i;
138
reg     [7:0] expected_data;
139
reg     [7:0] data;
140
reg     parity;
141
 
142
begin
143
        data <= 8'h0;
144
        parity <= 1;
145
        timeout_count = 0;
146
 
147
fork
148
   begin : loop_1
149
        @(abort)
150
         $display (">>>>>  Exceed time limit, uart no responce.\n");
151
         ->uart_timeout_error;
152
         disable loop_2;
153
   end
154
 
155
   begin : loop_2
156
 
157
// start cycle
158
        @(negedge sout)
159
         disable loop_1;
160
         read <= 1;
161
 
162
// data cycle
163
        @(posedge test_rx_clk);
164
         for (i = 0; i < data_bit_number; i = i + 1)
165
          begin
166
            @(posedge test_rx_clk)
167
            data[i] <=  sout;
168
            parity <= parity ^ sout;
169
          end
170
 
171
// parity cycle
172
        if(control_setup.parity_en)
173
        begin
174
          @(posedge test_rx_clk);
175
          if ((control_setup.even_odd_parity && (sout == parity)) ||
176
             (!control_setup.even_odd_parity && (sout != parity)))
177
// || (control_setup.stick_parity && (sout == control_setup.even_odd_parity)))
178
             begin
179
                $display (">>>>>  Parity Error");
180
                -> error_detected;
181
                -> uart_parity_error;
182
             end
183
        end
184
 
185
// stop cycle 1
186
        @(posedge test_rx_clk);
187
          if (!sout)
188
             begin
189
                $display (">>>>>  Stop signal 1 Error");
190
                -> error_detected;
191
                -> uart_stop_error1;
192
             end
193
 
194
// stop cycle 2
195
        if (control_setup.stop_bit_number)
196
        begin
197
              @(posedge test_rx_clk);   // stop cycle 2
198
                if (!sout)
199
                  begin
200
                    $display (">>>>>  Stop signal 2 Error");
201
                    -> error_detected;
202
                    -> uart_stop_error2;
203
                  end
204
        end
205
 
206
/*      Who Cares
207
// the stop bits transmitted is one and a half if it is 5-bit
208
        if (data_bit_number == 5)
209
        begin
210
                @(posedge test_rx_clk); // stop cycle for 5-bit/per char
211
                if (!sout)
212
                  begin
213
                    $display (">>>>>  Stop signal 2 Error (5-Bit)");
214
                    -> error_detected;
215
                    -> uart_stop_error2;
216
                  end
217
        end
218
        else
219
*/
220
 
221
// wait another half cycle for tx_done signal
222
                @(negedge test_rx_clk);
223
        read <= 0;
224
        -> uart_read_done;
225
 
226
        if (expected_data != data)
227
        begin
228
                $display ("Error! Data return is %h, expecting %h", data, expected_data);
229
                -> error_detected;
230
        end
231
        else
232
                $display ("(%m) Data match  %h", expected_data);
233
 
234
        $display ("... Read Data from UART done cnt :%d...",rx_count +1);
235
   end
236
join
237
 
238
end
239
 
240
endtask
241
 
242
 
243
////////////////////////////////////////////////////////////////////////////////
244
task write_char;
245
input [7:0] data;
246
 
247
integer i;
248
reg parity;     // 0: odd parity, 1: even parity
249
 
250
begin
251
        parity <=  #1 1;
252
 
253
// start cycle
254
        @(posedge test_tx_clk)
255
         begin
256
                sin <= #1 0;
257
                write <= #1 1;
258
         end
259
 
260
// data cycle
261
        begin
262
           for (i = 0; i < data_bit_number; i = i + 1)
263
           begin
264
                @(posedge test_tx_clk)
265
                    sin <= #1 data[i];
266
                parity <= parity ^ data[i];
267
           end
268
        end
269
 
270
// parity cycle
271
        if (control_setup.parity_en)
272
        begin
273
                @(posedge test_tx_clk)
274
                        sin <= #1
275
//                              control_setup.stick_parity ? ~control_setup.even_odd_parity : 
276
                                control_setup.even_odd_parity ? !parity : parity;
277
        end
278
 
279
// stop cycle 1
280
        @(posedge test_tx_clk)
281
                sin <= #1 stop_err_check ? 0 : 1;
282
 
283
// stop cycle 2
284
        @(posedge test_tx_clk);
285
                sin <= #1 1;
286
        if (data_bit_number == 5)
287
                @(negedge test_tx_clk);
288
        else if (control_setup.stop_bit_number)
289
                @(posedge test_tx_clk);
290
 
291
        write <= #1 0;
292
        $display ("... Write data %h to UART done cnt : %d ...\n", data,tx_count+1);
293
        -> uart_write_done;
294
end
295
endtask
296
 
297
 
298
////////////////////////////////////////////////////////////////////////////////
299
task control_setup;
300
input     [1:0] data_bit_set;
301
input           stop_bit_number;
302
input           parity_en;
303
input           even_odd_parity;
304
input           stick_parity;
305
input    [15:0] maxtime;
306
input    [15:0] divisor;
307
input           fifo_enable;
308
 
309
begin
310
        data_bit_number = data_bit_set + 5;
311
end
312
endtask
313
 
314
 
315
////////////////////////////////////////////////////////////////////////////////
316
task report_status;
317
output  [15:0] rx_nu;
318
output  [15:0] tx_nu;
319
begin
320
        $display ("-------------------- Reporting Configuration --------------------");
321
        $display ("     Data bit number setting is : %0d", data_bit_number);
322
        $display ("     Stop bit number setting is : %0d", control_setup.stop_bit_number + 1);
323
        $display ("     Divisor of Uart clock   is : %0d", control_setup.divisor);
324
        if (control_setup.parity_en)
325
        $display ("     Parity is enable");
326
        else
327
        $display ("     Parity is disable");
328
 
329
        if (control_setup.even_odd_parity)
330
        $display ("     Even parity setting");
331
        else
332
        $display ("     Odd parity setting");
333
 
334
/*
335
        if (control_setup.stick_parity)
336
        $display ("     Parity stick bit is on");
337
        else
338
        $display ("     Parity stick bit is off");
339
*/
340
 
341
        if (control_setup.fifo_enable)
342
        $display ("     FIFO mode is enable");
343
        else
344
        $display ("     FIFO mode is disable");
345
 
346
        $display ("-----------------------------------------------------------------");
347
 
348
        $display ("-------------------- Reporting Status --------------------\n");
349
        $display ("     Number of character received is : %d", rx_count);
350
        $display ("     Number of character sent     is : %d", tx_count);
351
        $display ("     Number of parity error rxd   is : %d", par_err_count);
352
        $display ("     Number of stop1  error rxd   is : %d", stop_err1_cnt);
353
        $display ("     Number of stop2  error rxd   is : %d", stop_err2_cnt);
354
        $display ("     Number of timeout error      is : %d", timeout_err_cnt);
355
        $display ("     Number of error              is : %d", err_cnt);
356
        $display ("-----------------------------------------------------------------");
357
 
358
        rx_nu = rx_count;
359
        tx_nu = tx_count;
360
end
361
endtask
362
 
363
 
364
////////////////////////////////////////////////////////////////////////////////
365
endmodule

powered by: WebSVN 2.1.0

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