⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 top_tb.v

📁 全面模仿AVR的UART功能
💻 V
📖 第 1 页 / 共 2 页
字号:

`timescale  1ns/1ns
module top_tb;

reg                 clk;
reg                 rst_n;

wire    [7:0]       wAD;
reg     [7:0]       rAD;
reg     [7:0]       addr;
reg                 wr_n;
reg                 rd_n;
reg                 ale;

reg                 rxd;
wire                txd;
wire                uart_int;
wire                uart_clk;

reg                 rBaudRate;

parameter           CLK             = 10,
                    DIVIDER_FACTOR  = 6;

parameter           UART_ADDR_BASE = 16'h1200;

parameter           UDR     = UART_ADDR_BASE + 0,
                    UCSRA   = UART_ADDR_BASE + 1,
                    UCSRB   = UART_ADDR_BASE + 2,
                    UCSRC   = UART_ADDR_BASE + 3,
                    UBRRH   = UART_ADDR_BASE + 4,
                    UBRRL   = UART_ADDR_BASE + 5,
                    UVER    = UART_ADDR_BASE + 6;

parameter   
    RXC             = 7,                                        //  接收结束标记, 
    TXC             = 6,                                        //  发送结束标记
    UDRE            = 5,                                        //  发送缓冲空, Uart Data Register Empty
    FE              = 4,                                        //  帧错误标记, Frame Error
    DOR             = 3,                                        //  数据举出, Data OverRun
    UPE             = 2,                                        //  检验错误, Parity Error
    U2X             = 1,                                        //  UART 倍速设置
    MPCM            = 0;                                        //  Mutli-Process Communication Mode

parameter
    RXCIE           = 7,                                        //  Rx Completed Interrupt Enable
    TXCIE           = 6,                                        //  Tx Completed Interrupt Enable
    UDRIE           = 5,                                        //  UART Data Register Interrupt Enable
    RXEN            = 4,                                        //  RX Enable
    TXEN            = 3,                                        //  TX Enable
    UCSZ2           = 2,                                        //  UART Char Size[2]
    RXB8            = 1,                                        //  RX Bit[8]
    TXB8            = 0;                                        //  TX Bit[8]

parameter
    UMSEL           = 6,                                        //  UART Mode Select
    UPM1            = 5,                                        //  UART Parity Mode
    UPM0            = 4,
    USBS            = 3,                                        //  UART Stop Bits Select
    UCSZ1           = 2,
    UCSZ0           = 1,
    UCPOL           = 0;

parameter
    CSZ_5BITS       = 5,
    CSZ_6BITS       = 6,
    CSZ_7BITS       = 7,
    CSZ_8BITS       = 8,
    SBS_1BIT        = 0,
    SBS_2BITS       = 1,
    NO_PARITY       = 0,
    EVEN_PARITY     = 2,
    ODD_PARITY      = 3;

reg     [7:0]       rRsl;
reg     [7:0]       rTstDat;
reg     [3:0]       rTstCharSize;
reg                 rTstStopBit;
reg     [1:0]       rTstParity;

initial
begin
    clk     <= 1'b0;
    rst_n   <= 1'b0;

    rd_n    <= 1'b1;
    wr_n    <= 1'b1;
    ale     <= 1'b0;
    
    #100
    rst_n   <= 1'b1;

    #100;


    ebiWriteDat(UBRRL, DIVIDER_FACTOR-1);                       //  set bardrate
    ebiWriteDat(UBRRH, 8'h00);
    
    ebiWriteDat(UCSRB,
        (1 << RXCIE) |
        (1 << RXEN) |
        (1 << TXEN)
    );

    ebiWriteDat(UCSRC, (8'h01<<UCSZ1) | (8'h01<<UCSZ0));

    /*  发送0x51, 5bits, 1 stopbit, no parity   */
    rTstDat = 8'h51;
    rTstCharSize = CSZ_5BITS;
    rTstStopBit = SBS_1BIT;
    rTstParity  = NO_PARITY;
    $display("Frame format is: CharSize=[%d], StopBit=[%d], ParityMode=[%d]", rTstCharSize, rTstStopBit, rTstParity);
    ebiWriteDat(UCSRC,
        (0 << UPM1) |
        (0 << UPM0) |
        (0 << UCSZ1) |
        (0 << UCSZ0)
    );
    rTstDat = rTstDat & 8'h1F;
    $display("start send data: 0x%H", rTstDat, $time);
    txdGenerate(8'h51, rTstCharSize, rTstParity, rTstStopBit);
    if (uart_int) begin
        @(negedge uart_int);                                       //  wait for rxd completed
        $display("Error: failed to receive one frame!");
    end else begin
        $display("Info: success to receive one frame!");
    end

    ebiReadDat(UCSRA, rRsl);
    $display("UCSRA = 0x%H", rRsl);
    ebiReadDat(UCSRB, rRsl);
    $display("UCSRB = 0x%H", rRsl);
    ebiReadDat(UCSRC, rRsl);
    $display("UCSRC = 0x%H", rRsl);
    ebiReadDat(UDR, rRsl);
    if (rRsl == rTstDat) begin
        $display("receive data ok!, UDR = 0x%H", rRsl, $time);
    end else begin
        $display("faile to receive data!", $time);
    end
    $display("==============");
    $stop;

    

    /*  发送0x51, 5bits, 1 stopbit, even parity   */
    rTstDat = 8'h51;
    rTstCharSize = CSZ_5BITS;
    rTstStopBit = SBS_1BIT;
    rTstParity  = EVEN_PARITY;
    $display("Frame format is: CharSize=[%d], StopBit=[%d], ParityMode=[%d]", rTstCharSize, rTstStopBit, rTstParity);

    ebiWriteDat(UCSRC,
        (1 << UPM1) |
        (0 << UPM0) |
        (0 << UCSZ1) |
        (0 << UCSZ0)
    );
    rTstDat = rTstDat & 8'h1F;
    $display("start send data: 0x%H", rTstDat, $time);
    txdGenerate(8'h51, rTstCharSize, rTstParity, rTstStopBit);
    if (uart_int) begin
        @(negedge uart_int);                                       //  wait for rxd completed
        $display("Error: failed to receive one frame!");
    end else begin
        $display("Info: success to receive one frame!");
    end
    ebiReadDat(UCSRA, rRsl);
    $display("UCSRA = 0x%H", rRsl);
    ebiReadDat(UCSRB, rRsl);
    $display("UCSRB = 0x%H", rRsl);
    ebiReadDat(UCSRC, rRsl);
    $display("UCSRC = 0x%H", rRsl);
    ebiReadDat(UDR, rRsl);
    if (rRsl == rTstDat) begin
        $display("receive data ok!, UDR = 0x%H", rRsl, $time);
    end else begin
        $display("faile to receive data!", $time);
    end
    $display("==============");
    $stop;



    /*  发送0x51, 5bits, 1 stopbit, odd parity   */
    rTstDat = 8'h51;
    rTstCharSize = CSZ_5BITS;
    rTstStopBit = SBS_1BIT;
    rTstParity  = ODD_PARITY;
    $display("Frame format is: CharSize=[%d], StopBit=[%d], ParityMode=[%d]", rTstCharSize, rTstStopBit, rTstParity);

    ebiWriteDat(UCSRC,
        (1 << UPM1) |
        (1 << UPM0) |
        (0 << UCSZ1) |
        (0 << UCSZ0)
    );
    rTstDat = rTstDat & 8'h1F;
    $display("start send data: 0x%H", rTstDat, $time);
    txdGenerate(8'h51, rTstCharSize, rTstParity, rTstStopBit);
    if (uart_int) begin
        @(negedge uart_int);                                       //  wait for rxd completed
        $display("Error: failed to receive one frame!");
    end else begin
        $display("Info: success to receive one frame!");
    end

    ebiReadDat(UCSRA, rRsl);
    $display("UCSRA = 0x%H", rRsl);
    ebiReadDat(UCSRB, rRsl);
    $display("UCSRB = 0x%H", rRsl);
    ebiReadDat(UCSRC, rRsl);
    $display("UCSRC = 0x%H", rRsl);
    ebiReadDat(UDR, rRsl);
    if (rRsl == rTstDat) begin
        $display("receive data ok!, UDR = 0x%H", rRsl, $time);
    end else begin
        $display("faile to receive data!", $time);
    end
    $display("==============");
    $stop;



    /*  发送0x51, 6bits, 1 stopbit, no parity   */
    rTstDat = 8'h51;
    rTstCharSize = CSZ_6BITS;
    rTstStopBit = SBS_1BIT;
    rTstParity  = NO_PARITY;
    $display("Frame format is: CharSize=[%d], StopBit=[%d], ParityMode=[%d]", rTstCharSize, rTstStopBit, rTstParity);
    ebiWriteDat(UCSRC,
        (0 << UPM1) |
        (0 << UPM0) |
        (0 << UCSZ1) |
        (1 << UCSZ0)
    );
    rTstDat = rTstDat &  ((1<<rTstCharSize) - 1);
    $display("start send data: 0x%H", rTstDat, $time);
    txdGenerate(8'h51, rTstCharSize, rTstParity, rTstStopBit);
    if (uart_int) begin
        @(negedge uart_int);                                       //  wait for rxd completed
        $display("Error: failed to receive one frame!");
    end else begin
        $display("Info: success to receive one frame!");
    end

    ebiReadDat(UCSRA, rRsl);
    $display("UCSRA = 0x%H", rRsl);
    ebiReadDat(UCSRB, rRsl);
    $display("UCSRB = 0x%H", rRsl);
    ebiReadDat(UCSRC, rRsl);
    $display("UCSRC = 0x%H", rRsl);
    ebiReadDat(UDR, rRsl);
    if (rRsl == rTstDat) begin
        $display("receive data ok!, UDR = 0x%H", rRsl, $time);
    end else begin
        $display("faile to receive data!", $time);
    end
    $display("==============");
    $stop;

    

    /*  发送0x51, 6bits, 1 stopbit, even parity   */
    rTstDat = 8'h51;
    rTstCharSize = CSZ_6BITS;
    rTstStopBit = SBS_1BIT;
    rTstParity  = EVEN_PARITY;
    $display("Frame format is: CharSize=[%d], StopBit=[%d], ParityMode=[%d]", rTstCharSize, rTstStopBit, rTstParity);

    ebiWriteDat(UCSRC,
        (1 << UPM1) |
        (0 << UPM0) |
        (0 << UCSZ1) |
        (1 << UCSZ0)
    );
    rTstDat = rTstDat &  ((1<<rTstCharSize) - 1);
    $display("start send data: 0x%H", rTstDat, $time);
    txdGenerate(8'h51, rTstCharSize, rTstParity, rTstStopBit);
    if (uart_int) begin
        @(negedge uart_int);                                       //  wait for rxd completed
        $display("Error: failed to receive one frame!");
    end else begin
        $display("Info: success to receive one frame!");
    end

    ebiReadDat(UCSRA, rRsl);
    $display("UCSRA = 0x%H", rRsl);
    ebiReadDat(UCSRB, rRsl);
    $display("UCSRB = 0x%H", rRsl);
    ebiReadDat(UCSRC, rRsl);
    $display("UCSRC = 0x%H", rRsl);
    ebiReadDat(UDR, rRsl);
    if (rRsl == rTstDat) begin
        $display("receive data ok!, UDR = 0x%H", rRsl, $time);
    end else begin
        $display("faile to receive data!", $time);
    end
    $display("==============");
    $stop;



    /*  发送0x51, 6bits, 1 stopbit, odd parity   */
    rTstDat = 8'h51;
    rTstCharSize = CSZ_6BITS;
    rTstStopBit = SBS_1BIT;
    rTstParity  = ODD_PARITY;
    $display("Frame format is: CharSize=[%d], StopBit=[%d], ParityMode=[%d]", rTstCharSize, rTstStopBit, rTstParity);

    ebiWriteDat(UCSRC,
        (1 << UPM1) |
        (1 << UPM0) |
        (0 << UCSZ1) |
        (1 << UCSZ0)
    );
    rTstDat = rTstDat &  ((1<<rTstCharSize) - 1);
    $display("start send data: 0x%H", rTstDat, $time);
    txdGenerate(8'h51, rTstCharSize, rTstParity, rTstStopBit);
    if (uart_int) begin
        @(negedge uart_int);                                       //  wait for rxd completed
        $display("Error: failed to receive one frame!");
    end else begin
        $display("Info: success to receive one frame!");
    end

    ebiReadDat(UCSRA, rRsl);
    $display("UCSRA = 0x%H", rRsl);
    ebiReadDat(UCSRB, rRsl);
    $display("UCSRB = 0x%H", rRsl);
    ebiReadDat(UCSRC, rRsl);
    $display("UCSRC = 0x%H", rRsl);
    ebiReadDat(UDR, rRsl);
    if (rRsl == rTstDat) begin
        $display("receive data ok!, UDR = 0x%H", rRsl, $time);
    end else begin
        $display("faile to receive data!", $time);
    end
    $display("==============");
    $stop;



    /*  发送0x51, 7bits, 1 stopbit, no parity   */
    rTstDat = 8'h51;
    rTstCharSize = CSZ_7BITS;
    rTstStopBit = SBS_1BIT;
    rTstParity  = NO_PARITY;
    $display("Frame format is: CharSize=[%d], StopBit=[%d], ParityMode=[%d]", rTstCharSize, rTstStopBit, rTstParity);
    ebiWriteDat(UCSRC,
        (0 << UPM1) |
        (0 << UPM0) |
        (1 << UCSZ1) |
        (0 << UCSZ0)
    );
    rTstDat = rTstDat &  ((1<<rTstCharSize) - 1);
    $display("start send data: 0x%H", rTstDat, $time);
    txdGenerate(8'h51, rTstCharSize, rTstParity, rTstStopBit);
    if (uart_int) begin
        @(negedge uart_int);                                       //  wait for rxd completed
        $display("Error: failed to receive one frame!");
    end else begin
        $display("Info: success to receive one frame!");
    end

    ebiReadDat(UCSRA, rRsl);
    $display("UCSRA = 0x%H", rRsl);
    ebiReadDat(UCSRB, rRsl);
    $display("UCSRB = 0x%H", rRsl);
    ebiReadDat(UCSRC, rRsl);
    $display("UCSRC = 0x%H", rRsl);
    ebiReadDat(UDR, rRsl);
    if (rRsl == rTstDat) begin
        $display("receive data ok!, UDR = 0x%H", rRsl, $time);
    end else begin
        $display("faile to receive data!", $time);
    end
    $display("==============");
    $stop;

    

    /*  发送0x51, 7bits, 1 stopbit, even parity   */
    rTstDat = 8'h51;
    rTstCharSize = CSZ_7BITS;
    rTstStopBit = SBS_1BIT;
    rTstParity  = EVEN_PARITY;
    $display("Frame format is: CharSize=[%d], StopBit=[%d], ParityMode=[%d]", rTstCharSize, rTstStopBit, rTstParity);

    ebiWriteDat(UCSRC,
        (1 << UPM1) |
        (0 << UPM0) |
        (1 << UCSZ1) |
        (0 << UCSZ0)
    );
    rTstDat = rTstDat &  ((1<<rTstCharSize) - 1);
    $display("start send data: 0x%H", rTstDat, $time);
    txdGenerate(8'h51, rTstCharSize, rTstParity, rTstStopBit);
    if (uart_int) begin
        @(negedge uart_int);                                       //  wait for rxd completed
        $display("Error: failed to receive one frame!");
    end else begin
        $display("Info: success to receive one frame!");
    end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -