| 1 |
module top(input clk, output uart_tx, output [4:0] led); |
| 2 |
|
| 3 |
|
| 4 |
reg [18:0] ctr = 1'b0; |
| 5 |
reg tick = 1'b0; |
| 6 |
reg [12:0] message_ctr = 1'b0; |
| 7 |
|
| 8 |
reg [7:0] tx_data; |
| 9 |
reg [7:0] message[8191:0]; |
| 10 |
initial $readmemh("message.hex", message); |
| 11 |
|
| 12 |
always @(posedge clk) begin |
| 13 |
{tick, ctr} <= ctr + 1'b1; |
| 14 |
if (tick) message_ctr <= message_ctr + 1'b1; |
| 15 |
tx_data <= message[message_ctr]; |
| 16 |
end |
| 17 |
|
| 18 |
uart_tx #( |
| 19 |
.baud(115200), |
| 20 |
.clk_freq(50000000) |
| 21 |
) uart_tx_i ( |
| 22 |
.clk(clk), |
| 23 |
.tx_start(tick), |
| 24 |
.tx_data(tx_data), |
| 25 |
.tx(uart_tx) |
| 26 |
); |
| 27 |
|
| 28 |
assign led = {message_ctr, 1'b0}; |
| 29 |
|
| 30 |
endmodule |
| 31 |
|
| 32 |
/* |
| 33 |
* icebreaker examples - Async uart tx module |
| 34 |
* |
| 35 |
* Copyright (C) 2018 Piotr Esden-Tempski <[email protected]> |
| 36 |
* |
| 37 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 38 |
* purpose with or without fee is hereby granted, provided that the above |
| 39 |
* copyright notice and this permission notice appear in all copies. |
| 40 |
* |
| 41 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 42 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 43 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 44 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 45 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 46 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 47 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 48 |
*/ |
| 49 |
|
| 50 |
/*** |
| 51 |
* This module generates a bit baud tick multiplied by the oversampling parameter. |
| 52 |
*/ |
| 53 |
module baud_tick_gen( |
| 54 |
input clk, enable, |
| 55 |
output tick); |
| 56 |
parameter clk_freq = 12000000; |
| 57 |
parameter baud = 115200; |
| 58 |
parameter oversampling = 1; |
| 59 |
|
| 60 |
function integer log2(input integer v); begin log2=0; while(v >> log2) log2 = log2 + 1; end endfunction |
| 61 |
|
| 62 |
localparam acc_width = log2(clk_freq / baud) + 8; // +/- 2% max timing error over a byte |
| 63 |
|
| 64 |
reg [acc_width:0] acc = 0; |
| 65 |
|
| 66 |
localparam shiftlimiter = log2((baud * oversampling) >> (31 - acc_width)); // this makes sure inc calculation doesn't overflow (verilog uses 32bit variables internally) |
| 67 |
localparam inc = ((baud * oversampling << (acc_width - shiftlimiter)) + (clk_freq >> (shiftlimiter + 1))) / (clk_freq >> shiftlimiter); // Calculate accumulate increment |
| 68 |
//initial $display("acc_width %d, shiftlimit %d, inc %d", acc_width, shiftlimiter, inc); |
| 69 |
|
| 70 |
always @(posedge clk) if (enable) acc <= acc[acc_width-1:0] + inc[acc_width:0]; else acc <= inc[acc_width:0]; |
| 71 |
assign tick = acc[acc_width]; |
| 72 |
|
| 73 |
endmodule |
| 74 |
|
| 75 |
|
| 76 |
module uart_tx( |
| 77 |
input clk, |
| 78 |
input tx_start, |
| 79 |
input [7:0] tx_data, |
| 80 |
output tx, |
| 81 |
output tx_busy); |
| 82 |
|
| 83 |
parameter clk_freq = 12000000; |
| 84 |
parameter baud = 115200; |
| 85 |
|
| 86 |
wire bit_tick; |
| 87 |
baud_tick_gen #(clk_freq, baud) tickgen(.clk(clk), .enable(tx_busy), .tick(bit_tick)); |
| 88 |
|
| 89 |
localparam |
| 90 |
IDLE = 4'b0000, // tx = high |
| 91 |
BIT_START = 4'b0100, // tx = low |
| 92 |
BIT0 = 4'b1000, // tx = data bit 0 |
| 93 |
BIT1 = 4'b1001, // tx = data bit 1 |
| 94 |
BIT2 = 4'b1010, // tx = data bit 2 |
| 95 |
BIT3 = 4'b1011, // tx = data bit 3 |
| 96 |
BIT4 = 4'b1100, // tx = data bit 4 |
| 97 |
BIT5 = 4'b1101, // tx = data bit 5 |
| 98 |
BIT6 = 4'b1110, // tx = data bit 6 |
| 99 |
BIT7 = 4'b1111, // tx = data bit 7 |
| 100 |
BIT_STOP1 = 4'b0010, // tx = high |
| 101 |
BIT_STOP2 = 4'b0011; // tx = high |
| 102 |
|
| 103 |
reg [3:0] tx_state = IDLE; |
| 104 |
wire tx_ready = (tx_state == 0); |
| 105 |
assign tx_busy = ~tx_ready; |
| 106 |
|
| 107 |
reg [7:0] tx_shift = 0; |
| 108 |
always @(posedge clk) |
| 109 |
begin |
| 110 |
if (tx_ready & tx_start) |
| 111 |
tx_shift <= tx_data; |
| 112 |
else |
| 113 |
if (tx_state[3] & bit_tick) |
| 114 |
tx_shift <= (tx_shift >> 1); |
| 115 |
|
| 116 |
case (tx_state) |
| 117 |
IDLE: if(tx_start) tx_state <= BIT_START; |
| 118 |
BIT_START: if(bit_tick) tx_state <= BIT0; |
| 119 |
BIT0: if(bit_tick) tx_state <= BIT1; |
| 120 |
BIT1: if(bit_tick) tx_state <= BIT2; |
| 121 |
BIT2: if(bit_tick) tx_state <= BIT3; |
| 122 |
BIT3: if(bit_tick) tx_state <= BIT4; |
| 123 |
BIT4: if(bit_tick) tx_state <= BIT5; |
| 124 |
BIT5: if(bit_tick) tx_state <= BIT6; |
| 125 |
BIT6: if(bit_tick) tx_state <= BIT7; |
| 126 |
BIT7: if(bit_tick) tx_state <= BIT_STOP1; |
| 127 |
BIT_STOP1: if(bit_tick) tx_state <= BIT_STOP2; |
| 128 |
BIT_STOP2: if(bit_tick) tx_state <= IDLE; |
| 129 |
default: if(bit_tick) tx_state <= IDLE; |
| 130 |
endcase |
| 131 |
|
| 132 |
end |
| 133 |
|
| 134 |
// high if state START, STOP1, STOP2 |
| 135 |
// | high if transmitting bits and bit is 1 |
| 136 |
// | | |
| 137 |
// V V |
| 138 |
assign tx = (tx_state < 4) | (tx_state[3] & tx_shift[0]); |
| 139 |
|
| 140 |
endmodule |