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

Subversion Repositories turbo8051

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

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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