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

Subversion Repositories uart2bus

[/] [uart2bus/] [trunk/] [verilog/] [bench/] [tb_bin_uart2bus_top.v] - Blame information for rev 6

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

Line No. Rev Author Line
1 2 motilito
//---------------------------------------------------------------------------------------
2
// uart test bench   
3
//
4
//---------------------------------------------------------------------------------------
5
 
6
`include "timescale.v"
7
 
8
module test;
9
//---------------------------------------------------------------------------------------
10
// include uart tasks 
11
`include "uart_tasks.v"
12
 
13
// define if simulation should be binary or ascii 
14
parameter BINARY_MODE = 1;
15
 
16
// internal signal  
17
reg clock;              // global clock 
18
reg reset;              // global reset 
19
reg [6:0] counter;
20
 
21
//---------------------------------------------------------------------------------------
22
// test bench implementation 
23
// global signals generation  
24
initial
25
begin
26
        counter = 0;
27
        reset = 1;
28
        #40 reset = 0;
29
end
30
 
31
// clock generator - 40MHz clock 
32
always
33
begin
34
        #12 clock = 0;
35
        #13 clock = 1;
36
end
37
 
38
// test bench dump variables 
39
initial
40
begin
41
        $dumpfile("test.vcd");
42
        //$dumpall;
43
        $dumpvars(0, test);
44
end
45
 
46
//------------------------------------------------------------------
47
// test bench transmitter and receiver 
48
// uart transmit - test bench control 
49
integer file;           // file handler index 
50
integer char;           // character read from file 
51
integer file_len;       // length of binary simulation file 
52
integer byte_idx;       // byte index in binary mode simulation 
53
integer tx_len;
54
integer rx_len;
55
reg new_rx_data;
56 4 motilito
reg [7:0] tx_byte;
57 2 motilito
 
58
initial
59
begin
60
        // defualt value of serial output 
61
        serial_out = 1;
62 4 motilito
        // wait for reset to de-assert 
63
        while (reset) @ (posedge clock);
64
        // wait for another 100 clock cycles before starting simulation 
65
        repeat (100) @ (posedge clock);
66 2 motilito
 
67
        // check simulation mode 
68
        if (BINARY_MODE > 0)
69
        begin
70
                // binary mode simulation 
71
                $display("Starting binary mode simulation");
72
                // open binary command file 
73 4 motilito
                file=$fopen("test.bin", "rb");
74 2 motilito
                // in binary simulation mode the first two byte contain the file length (MSB first) 
75 4 motilito
                file_len = $fgetc(file);
76
                file_len = 256*file_len + $fgetc(file);
77 2 motilito
                $display("File length: %d", file_len);
78
 
79
                // send entire file to uart 
80
                byte_idx = 0;
81
                while (byte_idx < file_len)
82
                begin
83
                        // each "record" in the binary starts with two bytes: the first is the number 
84
                        // of bytes to transmit and the second is the number of received bytes to wait 
85
                        // for before transmitting the next command. 
86
                        tx_len = $fgetc(file);
87
                        rx_len = $fgetc(file);
88
                        $display("Executing command with %d tx bytes and %d rx bytes", tx_len, rx_len);
89
                        byte_idx = byte_idx + 2;
90
 
91
                        // transmit command 
92
                        while (tx_len > 0)
93
                        begin
94
                                // read next byte from file and transmit it 
95
                                char = $fgetc(file);
96 4 motilito
                                tx_byte = char;
97 2 motilito
                                byte_idx = byte_idx + 1;
98 4 motilito
                                send_serial(tx_byte, `BAUD_115200, `PARITY_EVEN, `PARITY_OFF, `NSTOPS_1, `NBITS_8, 0);
99 2 motilito
                                // update tx_len 
100
                                tx_len = tx_len - 1;
101
                        end
102
 
103
                        // wait for received bytes 
104
                        while (rx_len > 0)
105
                        begin
106
                                // one clock delay to allow new_rx_data to update 
107
                                @(posedge new_rx_data) rx_len = rx_len - 1;
108
                                // check if a new byte was received 
109 4 motilito
                                if (new_rx_data)
110
                                        rx_len = rx_len - 1;
111 2 motilito
                        end
112
 
113
                        $display("Command finished");
114
                end
115
        end
116
        else
117
        begin
118
                // ascii mode simulation 
119
                // open UART command file 
120 4 motilito
                file=$fopen("test.txt", "rt");
121 2 motilito
                // transmit the byte in the command file one by one 
122
                char = $fgetc(file);
123
                while (char >= 0) begin
124
                        // transmit byte through UART 
125
                        send_serial(char, `BAUD_115200, `PARITY_EVEN, `PARITY_OFF, `NSTOPS_1, `NBITS_8, 0);
126
                        #200000;
127
                        // read next byte from file 
128
                        char = $fgetc(file);
129
                end
130
        end
131
 
132
        // close input file 
133
        $fclose(file);
134
 
135
        // delay and finish 
136
        #500000;
137
        $finish;
138
end
139
 
140
// uart receive 
141
initial
142
begin
143
        // default value for serial receiver and serial input 
144
        serial_in = 1;
145
        get_serial_data = 0;             // data received from get_serial task 
146
        get_serial_status = 0;           // status of get_serial task  
147
end
148
 
149
// serial sniffer loop 
150
always
151
begin
152
        // clear new_rx_data flag 
153
        new_rx_data = 0;
154
 
155
        // call serial sniffer 
156
        get_serial(`BAUD_115200, `PARITY_EVEN, `PARITY_OFF, `NSTOPS_1, `NBITS_8);
157
 
158
        // check serial receiver status 
159
        // byte received OK 
160
        if (get_serial_status & `RECEIVE_RESULT_OK)
161
        begin
162
                // check if not a control character (above and including space ascii code)
163
                if (get_serial_data >= 8'h20)
164
                        $display("received byte 0x%h (\"%c\") at %t ns", get_serial_data, get_serial_data, $time);
165
                else
166
                        $display("received byte 0x%h (\"%c\") at %t ns", get_serial_data, 8'hb0, $time);
167
 
168
                // sign to transmit process that a new byte was received 
169
                @(posedge clock) new_rx_data = 1;
170
                @(posedge clock) new_rx_data = 0;
171
        end
172
 
173
        // false start error 
174
        if (get_serial_status & `RECEIVE_RESULT_FALSESTART)
175
                $display("Error (get_char): false start condition at %t", $realtime);
176
 
177
        // bad parity error             
178
        if (get_serial_status & `RECEIVE_RESULT_BADPARITY)
179
                $display("Error (get_char): bad parity condition at %t", $realtime);
180
 
181
        // bad stop bits sequence       
182
        if (get_serial_status & `RECEIVE_RESULT_BADSTOP)
183
                $display("Error (get_char): bad stop bits sequence at %t", $realtime);
184
end
185
 
186
//------------------------------------------------------------------
187
// device under test 
188
// DUT interface 
189 4 motilito
wire    [15:0]   int_address;    // address bus to register file 
190 2 motilito
wire    [7:0]    int_wr_data;    // write data to register file 
191
wire                    int_write;              // write control to register file 
192
wire                    int_read;               // read control to register file 
193
wire    [7:0]    int_rd_data;    // data read from register file 
194
wire                    ser_in;                 // DUT serial input 
195
wire                    ser_out;                // DUT serial output 
196
 
197
// DUT instance 
198
uart2bus_top uart2bus1
199
(
200 4 motilito
        .clock(clock),
201
        .reset(reset),
202
        .ser_in(ser_in),
203
        .ser_out(ser_out),
204
        .int_address(int_address),
205
        .int_wr_data(int_wr_data),
206
        .int_write(int_write),
207
        .int_rd_data(int_rd_data),
208
        .int_read(int_read)
209 2 motilito
);
210
 
211
// serial interface to test bench 
212
assign ser_in = serial_out;
213
always @ (posedge clock) serial_in = ser_out;
214
 
215
// register file model 
216
reg_file_model reg_file1
217
(
218 4 motilito
        .clock(clock),
219
        .reset(reset),
220
        .int_address(int_address[7:0]),
221
        .int_wr_data(int_wr_data),
222
        .int_write(int_write),
223
        .int_rd_data(int_rd_data),
224
        .int_read(int_read)
225 2 motilito
);
226
 
227
endmodule
228
//---------------------------------------------------------------------------------------
229
//                                              Th.. Th.. Th.. Thats all folks !!!
230
//---------------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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