nand2/06_IO_Devices/UartRX.v
2024-10-25 13:47:27 -04:00

54 lines
1.5 KiB
Verilog

/**
* 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
module UartRX(
input clk,
input clear,
input RX,
output reg [15:0] out
);
// Put your code here:
wire clkdRX;
reg active=0;
reg out_set=0;
reg [7:0] uart;
reg [3:0] nthbit;
reg [9:0] is216;
always @(posedge clk) begin
out_set <= clear ? 1 : out_set;
out <= clear ? 16'h8000 : (out_set ? out : 16'h0000);
if ((active==0) && (RX == 0)) begin
out_set <=1;
active <= 1;
is216 <= 1;
uart <= 0;
nthbit <= 0;
end
else if (active==1) begin
is216 <= (is216 == 216) ? 0 : is216 + 1;
nthbit <= (is216 == 108) ? nthbit + 1 : nthbit;
case (nthbit)
1 : uart[0] <= RX;
2 : uart[1] <= RX;
3 : uart[2] <= RX;
4 : uart[3] <= RX;
5 : uart[4] <= RX;
6 : uart[5] <= RX;
7 : uart[6] <= RX;
8 : uart[7] <= RX;
endcase
if (nthbit == 10 && is216 == 216) begin
active <= 0;
out <= {8'b0, uart};
end
end
end
endmodule