+
+`define IN_CLK 25000000
+`define OUT_CLK 57600
+`define CLK_DIV (`IN_CLK / `OUT_CLK)
+
+module SerRX(
+ input pixclk,
+ output reg wr = 0,
+ output reg [11:0] waddr = 0,
+ output reg [7:0] wchar = 0,
+ input serialrx
+);
+
+ reg [15:0] rx_clkdiv = 0;
+ reg [3:0] rx_state = 4'b0000;
+ reg [7:0] rx_data_tmp;
+
+
+ always @(posedge pixclk)
+ begin
+ if ((rx_state == 0) && (serialrx == 0) /*&& (rx_hasdata == 0)*/) /* Kick off. */
+ rx_state <= 4'b0001;
+ else if ((rx_state != 4'b0000) && (rx_clkdiv == 0)) begin
+ if (rx_state != 4'b1010)
+ rx_state <= rx_state + 1;
+ else
+ rx_state <= 0;
+ case (rx_state)
+ 4'b0001: begin end /* Twiddle thumbs -- this is the end of the half bit. */
+ 4'b0010: rx_data_tmp[0] <= serialrx;
+ 4'b0011: rx_data_tmp[1] <= serialrx;
+ 4'b0100: rx_data_tmp[2] <= serialrx;
+ 4'b0101: rx_data_tmp[3] <= serialrx;
+ 4'b0110: rx_data_tmp[4] <= serialrx;
+ 4'b0111: rx_data_tmp[5] <= serialrx;
+ 4'b1000: rx_data_tmp[6] <= serialrx;
+ 4'b1001: rx_data_tmp[7] <= serialrx;
+ 4'b1010: if (serialrx == 1) begin
+ wr <= 1;
+ wchar <= rx_data_tmp;
+ end
+ endcase
+ end
+
+ if (wr) begin
+ wr <= 0;
+ waddr <= waddr + 1;
+ end
+
+ if ((rx_state == 0) && (serialrx == 0) /*&& (rx_hasdata == 0)*/) /* Wait half a period before advancing. */
+ rx_clkdiv <= `CLK_DIV / 2 + `CLK_DIV / 4;
+ else if (rx_clkdiv == `CLK_DIV)
+ rx_clkdiv <= 0;
+ else
+ rx_clkdiv <= rx_clkdiv + 1;
+ end
+
+endmodule