https://blackmesalabs.wordpress.com/2016/10/24/sump2-96-msps-logic-analyzer-for-22/
[BML_sump2] / sump2 / source / mesa_tx_uart.v
1 /* ****************************************************************************\r
2 -- (C) Copyright 2015 Black Mesa Labs\r
3 -- Source file: mesa_tx_uart.v                \r
4 -- Date:        June 1, 2015   \r
5 -- Author:      khubbard\r
6 -- Description: TX only 1/2 of UART for transmitting Wo bytes.\r
7 -- Language:    Verilog-2001 \r
8 -- License:     This project is licensed with the CERN Open Hardware Licence\r
9 --              v1.2.  You may redistribute and modify this project under the\r
10 --              terms of the CERN OHL v.1.2. (http://ohwr.org/cernohl).\r
11 --              This project is distributed WITHOUT ANY EXPRESS OR IMPLIED\r
12 --              WARRANTY, INCLUDING OF MERCHANTABILITY, SATISFACTORY QUALITY\r
13 --              AND FITNESS FOR A PARTICULAR PURPOSE. Please see the CERN OHL\r
14 --              v.1.2 for applicable Conditions.\r
15 --\r
16 -- RXD    \START/<D0><D1><D2><..><D7>/STOP\r
17 -- Design Statistics after Packing\r
18 -- en_clr_lock = 1;\r
19 --    Number of LUTs  : 190 / 384\r
20 --    Number of DFFs  : 110 / 384\r
21 --\r
22 -- Revision History:\r
23 -- Ver#  When      Who      What\r
24 -- ----  --------  -------- ---------------------------------------------------\r
25 -- 0.1   06.01.15  khubbard Creation\r
26 -- ***************************************************************************/\r
27 //`default_nettype none // Strictly enforce all nets to be declared\r
28                                                                                 \r
29 module mesa_tx_uart\r
30 (\r
31   input  wire         reset,\r
32   input  wire         clk,\r
33   input  wire [7:0]   tx_byte,\r
34   input  wire         tx_en,\r
35   output reg          tx_busy,\r
36   output reg          txd,\r
37   input  wire         baud_lock,\r
38   input  wire [15:0]  baud_rate\r
39 ); // module mesa_tx_uart\r
40 \r
41 \r
42   reg  [15:0]   tx_cnt_16b;\r
43   reg  [3:0]    tx_bit_cnt;\r
44   reg  [9:0]    tx_sr;\r
45   reg           txd_loc;\r
46   reg           tx_shift;\r
47   reg           tx_now;\r
48   reg           tx_en_p1;\r
49   reg           baud_lock_p1;\r
50   reg           send_lf;\r
51 \r
52 \r
53 //-----------------------------------------------------------------------------\r
54 // TX : Load 8bits into 10bit SR and shift out at the RX baud rate    \r
55 //-----------------------------------------------------------------------------\r
56 always @ ( posedge clk ) begin : proc_tx \r
57  begin\r
58   tx_shift     <= 0;\r
59   tx_busy      <= 0;\r
60   txd          <= txd_loc;\r
61   txd_loc      <= tx_sr[0];\r
62   tx_now       <= 0;\r
63   tx_en_p1     <= tx_en;\r
64   baud_lock_p1 <= baud_lock;   \r
65   send_lf      <= baud_lock & ~ baud_lock_p1;\r
66 \r
67   // Load a new Byte to send\r
68   if ( ( tx_en == 1 && tx_en_p1 == 0 ) || ( send_lf == 1 )  ) begin\r
69     tx_bit_cnt <= 4'd10;\r
70     tx_cnt_16b <= 16'h0000;\r
71     tx_busy    <= 1;\r
72     tx_shift   <= 1;\r
73     tx_sr[9:0] <= { 1'b1, tx_byte[7:0], 1'b0 };\r
74     if ( send_lf == 1 ) begin\r
75       tx_sr[9:0] <= { 1'b1, 8'h0A , 1'b0 };// Used for autobauding next node\r
76     end \r
77 \r
78   // Shift and send the byte until bit_cnt goes to 0\r
79   end else if ( tx_bit_cnt != 4'd0 ) begin\r
80     tx_cnt_16b <= tx_cnt_16b + 1;\r
81     tx_busy    <= 1;\r
82     if ( tx_now == 1 ) begin\r
83       tx_shift   <= 1;\r
84       tx_bit_cnt <= tx_bit_cnt[3:0] - 1;\r
85       tx_cnt_16b <= 16'h0000;\r
86       tx_sr[9:0] <= { 1'b1, tx_sr[9:1] };\r
87     end\r
88   end else begin\r
89     tx_sr <= 10'H3FF;\r
90   end\r
91 \r
92   if ( tx_cnt_16b[15:0] == baud_rate[15:0] ) begin\r
93     tx_now <= 1;\r
94   end\r
95 \r
96   if ( reset == 1 ) begin\r
97     tx_bit_cnt <= 4'd0;\r
98   end\r
99 \r
100  end\r
101 end // proc_tx\r
102 \r
103 \r
104 endmodule // mesa_tx_uart\r