Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module DEBUG_UART
  2.   #(
  3.     parameter clk_frequency = 50_000_000,
  4.     parameter baud_rate = 921600,
  5.     parameter stop_bits = 2,
  6.     parameter data_width = 16,
  7.     parameter sample_size = 32768
  8.   )
  9.   (
  10.     input wire clk,
  11.     input wire reset,
  12.     input wire [data_width - 1:0] data,
  13.     output wire txd
  14.   );
  15.  
  16.   localparam data_width_bytes = ((data_width - 1) / 8) + 1;
  17.   localparam sample_size_bytes = sample_size * data_width_bytes;
  18.   localparam header_size_bytes = 12;
  19.  
  20.   localparam STATE_RECORD               = 2'h0;
  21.   localparam STATE_TRANSFER_HEADER      = 2'h1;
  22.   localparam STATE_TRANSFER_HEADER_STOP = 2'h2;
  23.   localparam STATE_TRANSFER_DATA        = 2'h3;
  24.  
  25.   wire [7:0] uart_data;
  26.   wire UART_start;
  27.   wire UART_busy;
  28.  
  29.   reg [data_width - 1:0] data_buf [sample_size - 1:0];
  30.   reg [data_width - 1:0] buf_out;
  31.   reg [7:0] header [header_size_bytes - 1:0];
  32.   reg [7:0] header_out;
  33.   reg [1:0] state = STATE_RECORD;
  34.   reg buf_we = 1'b1;
  35.   reg [3:0] header_cnt = 0;
  36.   reg [$clog2(sample_size) - 1:0] buf_cnt = 0;
  37.   reg [$clog2(data_width_bytes) - 1:0] byte_cnt = 0;
  38.   reg [7:0] buf_out_byte = 0;
  39.  
  40.   assign uart_data = ((state == STATE_TRANSFER_HEADER) ||
  41.             (state == STATE_TRANSFER_HEADER_STOP)) ? header_out : buf_out_byte;
  42.   assign UART_start = (state == STATE_TRANSFER_HEADER) ||
  43.               (state == STATE_TRANSFER_HEADER_STOP) ||
  44.               (state == STATE_TRANSFER_DATA);
  45.  
  46.   initial begin
  47.     header[0] = 8'h01; header[1] = 8'h23;
  48.     header[2] = 8'h45; header[3] = 8'h67;
  49.     header[4] = 8'h89; header[5] = 8'hAB;
  50.     header[6] = 8'hCD; header[7] = 8'hEF;
  51.     header[8] = data_width;
  52.     header[9] = sample_size >> 16;
  53.     header[10] = sample_size >> 8;
  54.     header[11] = sample_size;
  55.   end
  56.  
  57.   always @(posedge clk) begin
  58.     if (buf_we)
  59.       data_buf[buf_cnt] <= data;
  60.     buf_out <= data_buf[buf_cnt];
  61.     buf_out_byte <= buf_out >> ((data_width_bytes - 1 - byte_cnt) * 8);
  62.   end
  63.  
  64.   always @(posedge clk) begin
  65.     if (reset) begin
  66.       state <= STATE_RECORD;
  67.       header_cnt <= 0;
  68.       buf_cnt <= 0;
  69.       buf_we <= 1'b1;
  70.     end else begin
  71.       case (state)
  72.         STATE_RECORD: begin
  73.           buf_we <= 1'b1;
  74.           buf_cnt <= buf_cnt + 1;
  75.           if (buf_cnt == sample_size - 1) begin
  76.             header_cnt <= 0;
  77.             buf_cnt <= 0;
  78.             buf_we <= 1'b0;
  79.             header_cnt <= 1;
  80.             header_out <= header[0];
  81.             state <= STATE_TRANSFER_HEADER;
  82.           end
  83.         end
  84.         STATE_TRANSFER_HEADER: begin
  85.           if (!UART_busy) begin
  86.             header_cnt <= header_cnt + 1'b1;
  87.             header_out <= header[header_cnt];
  88.             if (header_cnt == header_size_bytes - 1) begin
  89.               byte_cnt <= 0;
  90.               buf_cnt <= 0;
  91.               state <= STATE_TRANSFER_HEADER_STOP;
  92.             end
  93.           end
  94.         end
  95.         STATE_TRANSFER_HEADER_STOP: begin
  96.           if (!UART_busy) begin
  97.             state <= STATE_TRANSFER_DATA;
  98.           end
  99.         end
  100.         STATE_TRANSFER_DATA: begin
  101.           if (!UART_busy) begin
  102.             if (byte_cnt < data_width_bytes - 1) begin
  103.               byte_cnt <= byte_cnt + 1'b1;
  104.             end else begin
  105.               byte_cnt <= 0;
  106.               buf_cnt <= buf_cnt + 1'b1;
  107.               if (buf_cnt == sample_size - 1) begin
  108.                 buf_cnt <= 0;
  109.                 byte_cnt <= 0;
  110.                 state <= STATE_RECORD;
  111.               end
  112.             end
  113.           end
  114.         end
  115.         default: begin
  116.           state <= STATE_RECORD;
  117.         end
  118.       endcase
  119.     end
  120.   end
  121.  
  122.   UART_TX #(
  123.     .clk_frequency(clk_frequency),
  124.     .baud_rate(baud_rate),
  125.     .stop_bits(stop_bits)
  126.   ) UART_TX_inst (
  127.     .clk(clk),
  128.     .data(uart_data),
  129.     .start(UART_start),
  130.     .busy(UART_busy),
  131.     .txd(txd)
  132.   );
  133. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement