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

Subversion Repositories uart2bus

[/] [uart2bus/] [trunk/] [verilog/] [bench/] [tb_bin_uart2bus_top.v] - Diff between revs 4 and 12

Only display areas with differences | Details | Blame | View Log

Rev 4 Rev 12
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// uart test bench   
// uart test bench   
//
//
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
 
 
`include "timescale.v"
`include "timescale.v"
 
 
module test;
module test;
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// include uart tasks 
// include uart tasks 
`include "uart_tasks.v"
`include "uart_tasks.v"
 
 
// define if simulation should be binary or ascii 
// define if simulation should be binary or ascii 
parameter BINARY_MODE = 1;
parameter BINARY_MODE = 1;
 
 
// internal signal  
// internal signal  
reg clock;              // global clock 
reg clock;              // global clock 
reg reset;              // global reset 
reg reset;              // global reset 
reg [6:0] counter;
reg [6:0] counter;
 
 
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// test bench implementation 
// test bench implementation 
// global signals generation  
// global signals generation  
initial
initial
begin
begin
        counter = 0;
        counter = 0;
        reset = 1;
        reset = 1;
        #40 reset = 0;
        #40 reset = 0;
end
end
 
 
// clock generator - 40MHz clock 
// clock generator - 40MHz clock 
always
always
begin
begin
        #12 clock = 0;
        #12 clock = 0;
        #13 clock = 1;
        #13 clock = 1;
end
end
 
 
// test bench dump variables 
// test bench dump variables 
initial
initial
begin
begin
        $dumpfile("test.vcd");
        $dumpfile("test.vcd");
        //$dumpall;
        //$dumpall;
        $dumpvars(0, test);
        $dumpvars(0, test);
end
end
 
 
//------------------------------------------------------------------
//------------------------------------------------------------------
// test bench transmitter and receiver 
// test bench transmitter and receiver 
// uart transmit - test bench control 
// uart transmit - test bench control 
integer file;           // file handler index 
integer file;           // file handler index 
integer char;           // character read from file 
integer char;           // character read from file 
integer file_len;       // length of binary simulation file 
integer file_len;       // length of binary simulation file 
integer byte_idx;       // byte index in binary mode simulation 
integer byte_idx;       // byte index in binary mode simulation 
integer tx_len;
integer tx_len;
integer rx_len;
integer rx_len;
reg new_rx_data;
reg new_rx_data;
reg [7:0] tx_byte;
reg [7:0] tx_byte;
 
 
initial
initial
begin
begin
        // defualt value of serial output 
        // defualt value of serial output 
        serial_out = 1;
        serial_out = 1;
        // wait for reset to de-assert 
        // wait for reset to de-assert 
        while (reset) @ (posedge clock);
        while (reset) @ (posedge clock);
        // wait for another 100 clock cycles before starting simulation 
        // wait for another 100 clock cycles before starting simulation 
        repeat (100) @ (posedge clock);
        repeat (100) @ (posedge clock);
 
 
        // check simulation mode 
        // check simulation mode 
        if (BINARY_MODE > 0)
        if (BINARY_MODE > 0)
        begin
        begin
                // binary mode simulation 
                // binary mode simulation 
                $display("Starting binary mode simulation");
                $display("Starting binary mode simulation");
                // open binary command file 
                // open binary command file 
                file=$fopen("test.bin", "rb");
                file=$fopen("test.bin", "rb");
                // in binary simulation mode the first two byte contain the file length (MSB first) 
                // in binary simulation mode the first two byte contain the file length (MSB first) 
                file_len = $fgetc(file);
                file_len = $fgetc(file);
                file_len = 256*file_len + $fgetc(file);
                file_len = 256*file_len + $fgetc(file);
                $display("File length: %d", file_len);
                $display("File length: %d", file_len);
 
 
                // send entire file to uart 
                // send entire file to uart 
                byte_idx = 0;
                byte_idx = 0;
                while (byte_idx < file_len)
                while (byte_idx < file_len)
                begin
                begin
                        // each "record" in the binary starts with two bytes: the first is the number 
                        // each "record" in the binary starts with two bytes: the first is the number 
                        // of bytes to transmit and the second is the number of received bytes to wait 
                        // of bytes to transmit and the second is the number of received bytes to wait 
                        // for before transmitting the next command. 
                        // for before transmitting the next command. 
                        tx_len = $fgetc(file);
                        tx_len = $fgetc(file);
                        rx_len = $fgetc(file);
                        rx_len = $fgetc(file);
                        $display("Executing command with %d tx bytes and %d rx bytes", tx_len, rx_len);
                        $display("Executing command with %d tx bytes and %d rx bytes", tx_len, rx_len);
                        byte_idx = byte_idx + 2;
                        byte_idx = byte_idx + 2;
 
 
                        // transmit command 
                        // transmit command 
                        while (tx_len > 0)
                        while (tx_len > 0)
                        begin
                        begin
                                // read next byte from file and transmit it 
                                // read next byte from file and transmit it 
                                char = $fgetc(file);
                                char = $fgetc(file);
                                tx_byte = char;
                                tx_byte = char;
                                byte_idx = byte_idx + 1;
                                byte_idx = byte_idx + 1;
                                send_serial(tx_byte, `BAUD_115200, `PARITY_EVEN, `PARITY_OFF, `NSTOPS_1, `NBITS_8, 0);
                                send_serial(tx_byte, `BAUD_115200, `PARITY_EVEN, `PARITY_OFF, `NSTOPS_1, `NBITS_8, 0);
                                // update tx_len 
                                // update tx_len 
                                tx_len = tx_len - 1;
                                tx_len = tx_len - 1;
                        end
                        end
 
 
                        // wait for received bytes 
                        // wait for received bytes 
                        while (rx_len > 0)
                        while (rx_len > 0)
                        begin
                        begin
                                // one clock delay to allow new_rx_data to update 
                                // one clock delay to allow new_rx_data to update 
                                @(posedge new_rx_data) rx_len = rx_len - 1;
                                @(posedge new_rx_data) rx_len = rx_len - 1;
                                // check if a new byte was received 
                                // check if a new byte was received 
                                if (new_rx_data)
                                if (new_rx_data)
                                        rx_len = rx_len - 1;
                                        rx_len = rx_len - 1;
                        end
                        end
 
 
                        $display("Command finished");
                        $display("Command finished");
                end
                end
        end
        end
        else
        else
        begin
        begin
                // ascii mode simulation 
                // ascii mode simulation 
                // open UART command file 
                // open UART command file 
                file=$fopen("test.txt", "rt");
                file=$fopen("test.txt", "rt");
                // transmit the byte in the command file one by one 
                // transmit the byte in the command file one by one 
                char = $fgetc(file);
                char = $fgetc(file);
                while (char >= 0) begin
                while (char >= 0) begin
                        // transmit byte through UART 
                        // transmit byte through UART 
                        send_serial(char, `BAUD_115200, `PARITY_EVEN, `PARITY_OFF, `NSTOPS_1, `NBITS_8, 0);
                        send_serial(char, `BAUD_115200, `PARITY_EVEN, `PARITY_OFF, `NSTOPS_1, `NBITS_8, 0);
                        #200000;
                        #200000;
                        // read next byte from file 
                        // read next byte from file 
                        char = $fgetc(file);
                        char = $fgetc(file);
                end
                end
        end
        end
 
 
        // close input file 
        // close input file 
        $fclose(file);
        $fclose(file);
 
 
        // delay and finish 
        // delay and finish 
        #500000;
        #500000;
        $finish;
        $finish;
end
end
 
 
// uart receive 
// uart receive 
initial
initial
begin
begin
        // default value for serial receiver and serial input 
        // default value for serial receiver and serial input 
        serial_in = 1;
        serial_in = 1;
        get_serial_data = 0;             // data received from get_serial task 
        get_serial_data = 0;             // data received from get_serial task 
        get_serial_status = 0;           // status of get_serial task  
        get_serial_status = 0;           // status of get_serial task  
end
end
 
 
// serial sniffer loop 
// serial sniffer loop 
always
always
begin
begin
        // clear new_rx_data flag 
        // clear new_rx_data flag 
        new_rx_data = 0;
        new_rx_data = 0;
 
 
        // call serial sniffer 
        // call serial sniffer 
        get_serial(`BAUD_115200, `PARITY_EVEN, `PARITY_OFF, `NSTOPS_1, `NBITS_8);
        get_serial(`BAUD_115200, `PARITY_EVEN, `PARITY_OFF, `NSTOPS_1, `NBITS_8);
 
 
        // check serial receiver status 
        // check serial receiver status 
        // byte received OK 
        // byte received OK 
        if (get_serial_status & `RECEIVE_RESULT_OK)
        if (get_serial_status & `RECEIVE_RESULT_OK)
        begin
        begin
                // check if not a control character (above and including space ascii code)
                // check if not a control character (above and including space ascii code)
                if (get_serial_data >= 8'h20)
                if (get_serial_data >= 8'h20)
                        $display("received byte 0x%h (\"%c\") at %t ns", get_serial_data, get_serial_data, $time);
                        $display("received byte 0x%h (\"%c\") at %t ns", get_serial_data, get_serial_data, $time);
                else
                else
                        $display("received byte 0x%h (\"%c\") at %t ns", get_serial_data, 8'hb0, $time);
                        $display("received byte 0x%h (\"%c\") at %t ns", get_serial_data, 8'hb0, $time);
 
 
                // sign to transmit process that a new byte was received 
                // sign to transmit process that a new byte was received 
                @(posedge clock) new_rx_data = 1;
                @(posedge clock) new_rx_data = 1;
                @(posedge clock) new_rx_data = 0;
                @(posedge clock) new_rx_data = 0;
        end
        end
 
 
        // false start error 
        // false start error 
        if (get_serial_status & `RECEIVE_RESULT_FALSESTART)
        if (get_serial_status & `RECEIVE_RESULT_FALSESTART)
                $display("Error (get_char): false start condition at %t", $realtime);
                $display("Error (get_char): false start condition at %t", $realtime);
 
 
        // bad parity error             
        // bad parity error             
        if (get_serial_status & `RECEIVE_RESULT_BADPARITY)
        if (get_serial_status & `RECEIVE_RESULT_BADPARITY)
                $display("Error (get_char): bad parity condition at %t", $realtime);
                $display("Error (get_char): bad parity condition at %t", $realtime);
 
 
        // bad stop bits sequence       
        // bad stop bits sequence       
        if (get_serial_status & `RECEIVE_RESULT_BADSTOP)
        if (get_serial_status & `RECEIVE_RESULT_BADSTOP)
                $display("Error (get_char): bad stop bits sequence at %t", $realtime);
                $display("Error (get_char): bad stop bits sequence at %t", $realtime);
end
end
 
 
//------------------------------------------------------------------
//------------------------------------------------------------------
// device under test 
// device under test 
// DUT interface 
// DUT interface 
wire    [15:0]   int_address;    // address bus to register file 
wire    [15:0]   int_address;    // address bus to register file 
wire    [7:0]    int_wr_data;    // write data to register file 
wire    [7:0]    int_wr_data;    // write data to register file 
wire                    int_write;              // write control to register file 
wire                    int_write;              // write control to register file 
wire                    int_read;               // read control to register file 
wire                    int_read;               // read control to register file 
wire    [7:0]    int_rd_data;    // data read from register file 
wire    [7:0]    int_rd_data;    // data read from register file 
 
wire                    int_req;                // bus access request signal 
 
wire                    int_gnt;                // bus access grant signal 
wire                    ser_in;                 // DUT serial input 
wire                    ser_in;                 // DUT serial input 
wire                    ser_out;                // DUT serial output 
wire                    ser_out;                // DUT serial output 
 
 
// DUT instance 
// DUT instance 
uart2bus_top uart2bus1
uart2bus_top uart2bus1
(
(
        .clock(clock),
        .clock(clock),
        .reset(reset),
        .reset(reset),
        .ser_in(ser_in),
        .ser_in(ser_in),
        .ser_out(ser_out),
        .ser_out(ser_out),
        .int_address(int_address),
        .int_address(int_address),
        .int_wr_data(int_wr_data),
        .int_wr_data(int_wr_data),
        .int_write(int_write),
        .int_write(int_write),
        .int_rd_data(int_rd_data),
        .int_rd_data(int_rd_data),
        .int_read(int_read)
        .int_read(int_read),
 
        .int_req(int_req),
 
        .int_gnt(int_gnt)
);
);
 
// bus grant is always active 
 
assign int_gnt = 1'b1;
 
 
// serial interface to test bench 
// serial interface to test bench 
assign ser_in = serial_out;
assign ser_in = serial_out;
always @ (posedge clock) serial_in = ser_out;
always @ (posedge clock) serial_in = ser_out;
 
 
// register file model 
// register file model 
reg_file_model reg_file1
reg_file_model reg_file1
(
(
        .clock(clock),
        .clock(clock),
        .reset(reset),
        .reset(reset),
        .int_address(int_address[7:0]),
        .int_address(int_address[7:0]),
        .int_wr_data(int_wr_data),
        .int_wr_data(int_wr_data),
        .int_write(int_write),
        .int_write(int_write),
        .int_rd_data(int_rd_data),
        .int_rd_data(int_rd_data),
        .int_read(int_read)
        .int_read(int_read)
);
);
 
 
endmodule
endmodule
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//                                              Th.. Th.. Th.. Thats all folks !!!
//                                              Th.. Th.. Th.. Thats all folks !!!
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
 
 

powered by: WebSVN 2.1.0

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