| 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 |
12 |
motilito |
wire int_req; // bus access request signal
|
| 195 |
|
|
wire int_gnt; // bus access grant signal
|
| 196 |
2 |
motilito |
wire ser_in; // DUT serial input
|
| 197 |
|
|
wire ser_out; // DUT serial output
|
| 198 |
|
|
|
| 199 |
|
|
// DUT instance
|
| 200 |
|
|
uart2bus_top uart2bus1
|
| 201 |
|
|
(
|
| 202 |
4 |
motilito |
.clock(clock),
|
| 203 |
|
|
.reset(reset),
|
| 204 |
|
|
.ser_in(ser_in),
|
| 205 |
|
|
.ser_out(ser_out),
|
| 206 |
|
|
.int_address(int_address),
|
| 207 |
|
|
.int_wr_data(int_wr_data),
|
| 208 |
|
|
.int_write(int_write),
|
| 209 |
|
|
.int_rd_data(int_rd_data),
|
| 210 |
12 |
motilito |
.int_read(int_read),
|
| 211 |
|
|
.int_req(int_req),
|
| 212 |
|
|
.int_gnt(int_gnt)
|
| 213 |
2 |
motilito |
);
|
| 214 |
12 |
motilito |
// bus grant is always active
|
| 215 |
|
|
assign int_gnt = 1'b1;
|
| 216 |
2 |
motilito |
|
| 217 |
|
|
// serial interface to test bench
|
| 218 |
|
|
assign ser_in = serial_out;
|
| 219 |
|
|
always @ (posedge clock) serial_in = ser_out;
|
| 220 |
|
|
|
| 221 |
|
|
// register file model
|
| 222 |
|
|
reg_file_model reg_file1
|
| 223 |
|
|
(
|
| 224 |
4 |
motilito |
.clock(clock),
|
| 225 |
|
|
.reset(reset),
|
| 226 |
|
|
.int_address(int_address[7:0]),
|
| 227 |
|
|
.int_wr_data(int_wr_data),
|
| 228 |
|
|
.int_write(int_write),
|
| 229 |
|
|
.int_rd_data(int_rd_data),
|
| 230 |
|
|
.int_read(int_read)
|
| 231 |
2 |
motilito |
);
|
| 232 |
|
|
|
| 233 |
|
|
endmodule
|
| 234 |
|
|
//---------------------------------------------------------------------------------------
|
| 235 |
|
|
// Th.. Th.. Th.. Thats all folks !!!
|
| 236 |
|
|
//---------------------------------------------------------------------------------------
|