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

Subversion Repositories turbo8051

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

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

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

powered by: WebSVN 2.1.0

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