1 |
2 |
sinclairrf |
//
|
2 |
|
|
// PERIPHERAL UART_Tx: @NAME@
|
3 |
9 |
sinclairrf |
// Copyright 2013-2015 Sinclair R.F., Inc.
|
4 |
2 |
sinclairrf |
//
|
5 |
|
|
localparam L__OUTFIFO_NBITS = $clog2(@OUTFIFO@);
|
6 |
|
|
localparam L__COUNT = @BAUDMETHOD@-1;
|
7 |
|
|
localparam L__COUNT_NBITS = $clog2(L__COUNT+1);
|
8 |
|
|
localparam L__NTX = 1+8+@NSTOP@-1;
|
9 |
|
|
localparam L__NTX_NBITS = $clog2((L__NTX==0)?1:L__NTX);
|
10 |
|
|
generate
|
11 |
|
|
reg [7:0] s__Tx_data;
|
12 |
6 |
sinclairrf |
wire s__Tx_enabled = @ENABLED@;
|
13 |
2 |
sinclairrf |
reg s__Tx_go;
|
14 |
|
|
reg s__Tx_uart_busy;
|
15 |
|
|
if (@OUTFIFO@ == 0) begin : gen__nooutfifo
|
16 |
9 |
sinclairrf |
always @ (s__Tx_uart_busy, s__Tx_enabled)
|
17 |
6 |
sinclairrf |
s__Tx_busy = s__Tx_uart_busy || !s__Tx_enabled;
|
18 |
2 |
sinclairrf |
always @ (s__Tx)
|
19 |
|
|
s__Tx_data = s__Tx;
|
20 |
|
|
always @ (s__Tx_wr)
|
21 |
|
|
s__Tx_go = s__Tx_wr;
|
22 |
|
|
end else begin : gen__outfifo
|
23 |
|
|
reg [7:0] s__Tx_fifo_mem[@OUTFIFO@-1:0];
|
24 |
|
|
reg [L__OUTFIFO_NBITS:0] s__Tx_fifo_addr_in = {(L__OUTFIFO_NBITS+1){1'b0}};
|
25 |
|
|
always @ (posedge i_clk)
|
26 |
|
|
if (i_rst)
|
27 |
|
|
s__Tx_fifo_addr_in <= {(L__OUTFIFO_NBITS+1){1'b0}};
|
28 |
|
|
else if (s__Tx_wr) begin
|
29 |
|
|
s__Tx_fifo_addr_in <= s__Tx_fifo_addr_in + { {(L__OUTFIFO_NBITS){1'b0}}, 1'b1 };
|
30 |
|
|
s__Tx_fifo_mem[s__Tx_fifo_addr_in[0+:L__OUTFIFO_NBITS]] <= s__Tx;
|
31 |
|
|
end else
|
32 |
|
|
s__Tx_fifo_addr_in <= s__Tx_fifo_addr_in;
|
33 |
|
|
reg [L__OUTFIFO_NBITS:0] s__Tx_fifo_addr_out;
|
34 |
|
|
reg s__Tx_fifo_has_data = 1'b0;
|
35 |
|
|
reg s__Tx_fifo_full = 1'b0;
|
36 |
|
|
always @ (posedge i_clk)
|
37 |
|
|
if (i_rst) begin
|
38 |
|
|
s__Tx_fifo_has_data <= 1'b0;
|
39 |
|
|
s__Tx_fifo_full <= 1'b0;
|
40 |
|
|
end else begin
|
41 |
|
|
s__Tx_fifo_has_data <= (s__Tx_fifo_addr_out != s__Tx_fifo_addr_in);
|
42 |
|
|
s__Tx_fifo_full <= (s__Tx_fifo_addr_out == (s__Tx_fifo_addr_in ^ { 1'b1, {(L__OUTFIFO_NBITS){1'b0}} }));
|
43 |
|
|
end
|
44 |
|
|
initial s__Tx_go = 1'b0;
|
45 |
|
|
always @ (posedge i_clk)
|
46 |
|
|
if (i_rst)
|
47 |
|
|
s__Tx_go <= 1'b0;
|
48 |
6 |
sinclairrf |
else if (s__Tx_enabled && s__Tx_fifo_has_data && !s__Tx_uart_busy && !s__Tx_go)
|
49 |
2 |
sinclairrf |
s__Tx_go <= 1'b1;
|
50 |
|
|
else
|
51 |
|
|
s__Tx_go <= 1'b0;
|
52 |
|
|
initial s__Tx_fifo_addr_out = {(L__OUTFIFO_NBITS+1){1'b0}};
|
53 |
|
|
always @ (posedge i_clk)
|
54 |
|
|
if (i_rst)
|
55 |
|
|
s__Tx_fifo_addr_out <= {(L__OUTFIFO_NBITS+1){1'b0}};
|
56 |
|
|
else if (s__Tx_go)
|
57 |
|
|
s__Tx_fifo_addr_out <= s__Tx_fifo_addr_out + { {(L__OUTFIFO_NBITS){1'b0}}, 1'b1 };
|
58 |
|
|
else
|
59 |
|
|
s__Tx_fifo_addr_out <= s__Tx_fifo_addr_out;
|
60 |
|
|
initial s__Tx_data = 8'd0;
|
61 |
|
|
always @ (posedge i_clk)
|
62 |
|
|
if (i_rst)
|
63 |
|
|
s__Tx_data <= 8'd0;
|
64 |
|
|
else
|
65 |
|
|
s__Tx_data <= s__Tx_fifo_mem[s__Tx_fifo_addr_out[0+:L__OUTFIFO_NBITS]];
|
66 |
|
|
always @ (s__Tx_fifo_full)
|
67 |
|
|
s__Tx_busy = s__Tx_fifo_full;
|
68 |
|
|
end
|
69 |
|
|
// Count the clock cycles to decimate to the desired baud rate.
|
70 |
|
|
reg [L__COUNT_NBITS-1:0] s__Tx_count = {(L__COUNT_NBITS){1'b0}};
|
71 |
|
|
reg s__Tx_count_is_zero = 1'b0;
|
72 |
|
|
always @ (posedge i_clk)
|
73 |
|
|
if (i_rst) begin
|
74 |
|
|
s__Tx_count <= {(L__COUNT_NBITS){1'b0}};
|
75 |
|
|
s__Tx_count_is_zero <= 1'b0;
|
76 |
|
|
end else if (s__Tx_go || s__Tx_count_is_zero) begin
|
77 |
|
|
s__Tx_count <= L__COUNT[0+:L__COUNT_NBITS];
|
78 |
|
|
s__Tx_count_is_zero <= 1'b0;
|
79 |
|
|
end else begin
|
80 |
|
|
s__Tx_count <= s__Tx_count - { {(L__COUNT_NBITS-1){1'b0}}, 1'b1 };
|
81 |
|
|
s__Tx_count_is_zero <= (s__Tx_count == { {(L__COUNT_NBITS-1){1'b0}}, 1'b1 });
|
82 |
|
|
end
|
83 |
|
|
// Latch the bits to output.
|
84 |
|
|
reg [7:0] s__Tx_stream = 8'hFF;
|
85 |
|
|
always @ (posedge i_clk)
|
86 |
|
|
if (i_rst)
|
87 |
|
|
s__Tx_stream <= 8'hFF;
|
88 |
|
|
else if (s__Tx_go)
|
89 |
|
|
s__Tx_stream <= s__Tx_data;
|
90 |
|
|
else if (s__Tx_count_is_zero)
|
91 |
|
|
s__Tx_stream <= { 1'b1, s__Tx_stream[1+:7] };
|
92 |
|
|
else
|
93 |
|
|
s__Tx_stream <= s__Tx_stream;
|
94 |
|
|
// Generate the output bit stream.
|
95 |
|
|
initial @NAME@ = 1'b1;
|
96 |
|
|
always @ (posedge i_clk)
|
97 |
|
|
if (i_rst)
|
98 |
|
|
@NAME@ <= 1'b1;
|
99 |
|
|
else if (s__Tx_go)
|
100 |
|
|
@NAME@ <= 1'b0;
|
101 |
|
|
else if (s__Tx_count_is_zero)
|
102 |
|
|
@NAME@ <= s__Tx_stream[0];
|
103 |
|
|
else
|
104 |
|
|
@NAME@ <= @NAME@;
|
105 |
|
|
// Count down the number of bits.
|
106 |
|
|
reg [L__NTX_NBITS-1:0] s__Tx_n = {(L__NTX_NBITS){1'b0}};
|
107 |
|
|
always @ (posedge i_clk)
|
108 |
|
|
if (i_rst)
|
109 |
|
|
s__Tx_n <= {(L__NTX_NBITS){1'b0}};
|
110 |
|
|
else if (s__Tx_go)
|
111 |
|
|
s__Tx_n <= L__NTX[0+:L__NTX_NBITS];
|
112 |
|
|
else if (s__Tx_count_is_zero)
|
113 |
|
|
s__Tx_n <= s__Tx_n - { {(L__NTX_NBITS-1){1'b0}}, 1'b1 };
|
114 |
|
|
else
|
115 |
|
|
s__Tx_n <= s__Tx_n;
|
116 |
|
|
// The status bit is 1 if the core is busy and 0 otherwise.
|
117 |
|
|
initial s__Tx_uart_busy = 1'b1;
|
118 |
|
|
always @ (posedge i_clk)
|
119 |
|
|
if (i_rst)
|
120 |
|
|
s__Tx_uart_busy <= 1'b0;
|
121 |
|
|
else if (s__Tx_go)
|
122 |
|
|
s__Tx_uart_busy <= 1'b1;
|
123 |
|
|
else if (s__Tx_count_is_zero && (s__Tx_n == {(L__NTX_NBITS){1'b0}}))
|
124 |
|
|
s__Tx_uart_busy <= 1'b0;
|
125 |
|
|
else
|
126 |
|
|
s__Tx_uart_busy <= s__Tx_uart_busy;
|
127 |
|
|
endgenerate
|