nand2/06_IO_Devices/UartRX.v

56 lines
1.5 KiB
Coq
Raw Normal View History

2023-01-11 11:13:09 +01:00
/**
* UartRX receives bytes over UART
*
* When clear = 1 the chip clears the receive buffer and is ready to receive
* next byte. out[15] is set to 1 to show, that chip is ready to receive next
* byte. When RX goes low the chip starts sampling the RX line. After reading
* of byte completes, chip ouputs the received byte to out[7:0]] with out[15]=0.
*/
`default_nettype none
2025-04-25 21:22:29 +05:30
module UartRX (
input wire clk,
input wire clear,
input wire RX,
2024-10-20 09:49:55 -04:00
output reg [15:0] out
2023-01-11 11:13:09 +01:00
);
2025-04-25 21:22:29 +05:30
reg [7:0] baud_count;
reg [3:0] bit_index;
reg [7:0] rx_shift;
reg rx_active;
initial begin
out <= 16'b0;
rx_active <= 1'b0;
end
2024-10-20 09:49:55 -04:00
always @(posedge clk) begin
2025-04-25 21:22:29 +05:30
out <= (clear | out[15]) ? 16'h8000 : out;
if (!rx_active && !RX) begin
// RX Falling edge: Start bit detected
rx_active <= 1;
rx_shift <= 0;
baud_count <= 1;
bit_index <= 0;
end else if (rx_active) begin
baud_count <= (baud_count == 216) ? 0 : baud_count + 1;
bit_index <= (baud_count == 108) ? bit_index + 1 : bit_index;
if (bit_index >= 1 && bit_index <= 8 && baud_count == 108) begin
rx_shift <= {RX, rx_shift[7:1]};
end
if (bit_index == 10 && baud_count == 216) begin
rx_active <= 0; // Done receiving 8 bits + stop
out <= {8'b0, rx_shift};
2024-10-20 09:49:55 -04:00
end
end
end
2025-04-25 21:22:29 +05:30
2023-01-11 11:13:09 +01:00
endmodule