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

📄 uart_device.v

📁 uart协议、实现、验证
💻 V
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////////                                                              ////////  uart_device.v                                               ////////                                                              ////////  This file is part of the "uart16550" project                ////////  http://www.opencores.org/projects/uart16550/                ////////                                                              ////////  Author(s):                                                  ////////      - tadej@opencores.org (Tadej Markovic)                  ////////      - igorm@opencores.org (Igor Mohor)                      ////////                                                              ////////  All additional information is avaliable in the README.txt   ////////  file.                                                       ////////                                                              ////////                                                              //////////////////////////////////////////////////////////////////////////////                                                              //////// Copyright (C) 2000 - 2004 authors                            ////////                                                              //////// This source file may be used and distributed without         //////// restriction provided that this copyright statement is not    //////// removed from the file and that any derivative work contains  //////// the original copyright notice and the associated disclaimer. ////////                                                              //////// This source file is free software; you can redistribute it   //////// and/or modify it under the terms of the GNU Lesser General   //////// Public License as published by the Free Software Foundation; //////// either version 2.1 of the License, or (at your option) any   //////// later version.                                               ////////                                                              //////// This source is distributed in the hope that it will be       //////// useful, but WITHOUT ANY WARRANTY; without even the implied   //////// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //////// PURPOSE.  See the GNU Lesser General Public License for more //////// details.                                                     ////////                                                              //////// You should have received a copy of the GNU Lesser General    //////// Public License along with this source; if not, download it   //////// from http://www.opencores.org/lgpl.shtml                     ////////                                                              ////////////////////////////////////////////////////////////////////////////// CVS Revision History//// $Log: uart_device.v,v $// Revision 1.1  2004/03/27 03:55:16  tadejm// Testbench with complete selfchecking. BUG is that THRE status is set at the end of last sent bit when TX FIFO is empty instead when only TX FIFO gets empty. This causes testcases not to finish.//////`include "uart_testbench_defines.v"`include "timescale.v"module uart_device(    // UART signals    stx_i,    srx_o,    // Modem signals    rts_i,    cts_o,    dtr_i,    dsr_o,    ri_o,    dcd_o);// IN/OUT signals//###############  // UART signals  input           stx_i;  output          srx_o;  // Modem signals  input           rts_i;  output          cts_o;  input           dtr_i;  output          dsr_o;  output          ri_o;  output          dcd_o;// INTERNAL signals//#################  // Clock generation signals  //#########################    // Operational and transmission clock signals    reg          rx_clk;         // RX device clock with period T_clk_period (should be equal to wb_clk_period)    reg          tx_clk;         // TX device clock with period (T_clk_period + T_clk_delay)    reg          tx_clk_divided; // divided TX device clock with period ((T_clk_period + T_clk_delay) * T_divisor * 16)    // Clock enable signals    reg          rx_clk_en = 1'b1;    reg          tx_clk_en = 1'b1;    reg          tx_clk_divided_en = 1'b1;    // Clock period variables    real         T_clk_period = 20;    real         T_clk_delay = 0;    integer      T_divisor = 5;  // IN/OUT assignment signals  //##########################    // Modem signals    wire         rts;    wire         dtr;  // UART receiver signals  //######################    // RX packet control signals    wire         rx;    reg   [3:0]  rx_length;    reg          rx_odd_parity;    reg          rx_even_parity;    reg          rx_stick1_parity;    reg          rx_stick0_parity;    reg          rx_parity_enabled;    reg          rx_stop_bit_1;    reg          rx_stop_bit_1_5;    reg          rx_stop_bit_2;    // RX logic signals    wire  [3:0]  rx_total_length;    wire  [5:0]  rx_break_detection_length;    reg          rx_packet_end;    reg          rx_packet_end_q;    reg          rx_clk_cnt_en;    reg  [31:0]  rx_clk_cnt;    reg          rx_sample_clock;    integer      rx_bit_index;    integer      rx_stop_bit_index;    reg   [7:0]  rx_data;    reg   [1:0]  rx_stop;    reg          rx_framing_error;    reg          rx_parity;    reg          rx_parity_error;    reg          rx_break_detected;    reg          rx_break_detected_q;    reg  [31:0]  rx_break_cnt;    // RX events    event        device_received_packet;    event        device_received_last_bit;    event        device_received_stop_bit;    event        device_detected_rx_break;  // UART transmitter signals  //#########################    // TX packet control signals    reg          tx;    reg   [3:0]  tx_length;    reg          tx_odd_parity;    reg          tx_even_parity;    reg          tx_stick1_parity;    reg          tx_stick0_parity;    reg          tx_parity_enabled;    reg          tx_parity_wrong;    reg          tx_framing_wrong;    // TX logic signals    reg  [23:0]  tx_glitch_num;    reg          start_tx_glitch_cnt;    reg  [31:0]  tx_glitch_cnt;    reg          tx_glitch;    reg          tx_break_enable;    reg  [15:0]  tx_break_num;    reg          start_tx_break_cnt;        reg  [31:0]  tx_break_cnt;    reg          tx_break;    // TX test signals    reg   [7:0]  sent_data;    reg          tx_accept_next_framing_err;    reg          tx_framing_err;    reg          tx_framing_glitch_err;    // TX events    event        device_sent_packet;    event        sent_packet_received;// Clock generation//#################  // Example of TESTBENCH's task for setting UART clock period:  //    ----------------  //    task set_uart_clk_period;  //      input [31:0]  clk_period;  //    begin  //      //@(posedge testbench.uart_device.clk);  //      testbench.uart_device.T_clk_period = clk_period;  //    end  //    endtask // set_uart_clk_period  //    ----------------  // Example of TESTBENCH's task for setting UART clock rising edge   // delayed for time_delay_i after WB clock rising edge:  //    ----------------  //    task uart_clk_follows_wb_clk;  //      input [31:0]  time_delay_i;  //      integer       time_delay;  //    begin  //      time_delay = time_delay_i;  //      @(posedge testbench.uart_device.clk);  //      testbench.uart_device.clk_en = 1'b0;  //      @(posedge wb_clk);  //      #time_delay testbench.uart_device.clk = 1'b1;  //      testbench.uart_device.clk_en = 1'b1;  //    end  //    endtask // uart_clk_follows_wb_clk  //    ----------------  // rx_clk rising edge  always@(posedge rx_clk)    if (rx_clk_en)      #(T_clk_period / 2) rx_clk = 1'b0;  // rx_clk falling edge  always@(negedge rx_clk)    if (rx_clk_en)      #(T_clk_period / 2) rx_clk = 1'b1;  // tx_clk rising edge  always@(posedge tx_clk)    if (tx_clk_en)      #((T_clk_period + T_clk_delay) / 2) tx_clk = 1'b0;  // tx_clk falling edge  always@(negedge tx_clk)    if (tx_clk_en)      #((T_clk_period + T_clk_delay) / 2) tx_clk = 1'b1;  // tx_clk_divided rising edge  always@(posedge tx_clk_divided)    if (tx_clk_divided_en)      #(((T_clk_period + T_clk_delay) / 2) * 16 * T_divisor) tx_clk_divided = 1'b0;  // tx_clk_divided falling edge  always@(negedge tx_clk_divided)    if (tx_clk_divided_en)      #(((T_clk_period + T_clk_delay) / 2) * 16 * T_divisor) tx_clk_divided = 1'b1;  // Inital CLK values  initial  begin:device    rx_clk = 1'b0;    tx_clk = 1'b0;    tx_clk_divided = 1'b0;  end// IN/OUT assignments//###################  // UART output  assign srx_o = (tx ^ tx_glitch) & ~tx_break;  // Modem output  assign cts_o  = 0;  assign dsr_o  = 0;  assign ri_o   = 0;  assign dcd_o  = 0;  // UART input  assign rx = stx_i;  // Modem input  assign rts = rts_i;  assign dtr = dtr_i;// UART receiver//##############  // Initial values for RX  initial  begin    // Default LENGTH    rx_length = 8;    // Default PARITY    rx_odd_parity     = 1'b0;    rx_even_parity    = 1'b0;    rx_stick1_parity  = 1'b0;    rx_stick0_parity  = 1'b0;    rx_parity_enabled = 1'b0;    // Default STOP    rx_stop_bit_1   = 1'b1;    rx_stop_bit_1_5 = 1'b0;    rx_stop_bit_2   = 1'b0;  end  // Total length of RX packet (for proper generation of the rx_packet_end signal):   //   data length + parity + 1 stop bit + second stop bit (when enabled)  assign rx_total_length = rx_length + rx_parity_enabled + 1 + rx_stop_bit_2;  //   +1 is used because start bit was not included in rx_total_length.  assign rx_break_detection_length = rx_total_length + 1;  // Generating rx_clk_cnt_en signal.  always@(posedge rx_clk)  begin    if (~rx_clk_cnt_en)    begin      wait (~rx);    end    rx_clk_cnt_en = 1;    rx_packet_end = 0;    wait (rx_packet_end);    rx_clk_cnt_en = 0;    wait (rx); // Must be high to continue, because of break condition  end  // Counter used in data reception  always@(posedge rx_clk)  begin    if (rx_clk_cnt_en)    begin      if (rx_clk_cnt == (8 * T_divisor - 1) & rx) // False start bit detection        rx_packet_end = 1;      if (rx_clk_cnt_en) // Checking is still enabled after devisor clocks        rx_clk_cnt <= #1 rx_clk_cnt + 1;      else        rx_clk_cnt <= #1 0;    end    else      rx_clk_cnt <= #1 0;  end  // Delayed rx_packet_end signal  always@(posedge rx_clk)    rx_packet_end_q = rx_packet_end;  // Generating sample clock and end of the frame (Received data is sampled with this clock)  always@(posedge rx_clk)  begin    if (rx_clk_cnt == 8 * T_divisor - 1)      rx_bit_index = 0;    else if (rx_clk_cnt == (8 * T_divisor + 16 * T_divisor * (rx_bit_index + 1) - 1))    begin      rx_sample_clock = 1;      rx_bit_index = rx_bit_index + 1;      if (rx_bit_index == rx_total_length)        rx_packet_end = 1;    end    else      rx_sample_clock = 0;  end  // Sampling data (received data)  always@(posedge rx_clk)  begin    if (rx_sample_clock)    begin      if (rx_bit_index <= rx_length) // Sampling data      begin        rx_stop_bit_index <= 0; // Stop bit index reset at the beginning of the data stage//        $display("\t\t\t\t\t\t\t(rx_bit_index = %0d) Reading data bits = %0x", rx_bit_index, rx);        rx_data[rx_bit_index - 1] = rx;        if (rx_bit_index == rx_length)          -> device_received_last_bit;      end      else

⌨️ 快捷键说明

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