3 `define CLK_DIV `IN_CLK / `OUT_CLK
4 `define DATA_ADDR 16'hFF52
5 `define STAT_ADDR 16'hFF53
13 output reg serial = 1,
16 wire data_decode = (addr == `DATA_ADDR);
17 wire stat_decode = (addr == `STAT_ADDR);
21 reg [7:0] tx_data = 0;
22 reg [15:0] tx_clkdiv = 0;
23 reg [3:0] tx_state = 4'b0000;
25 wire tx_newdata = (wr) && (!tx_busy) && data_decode;
28 reg [15:0] rx_clkdiv = 0;
29 reg [3:0] rx_state = 4'b0000;
32 assign data = (stat_latch) ? {6'b0, rx_hasdata, tx_busy} :
33 (data_latch) ? rx_data :
38 data_latch <= rd && data_decode;
39 stat_latch <= rd && stat_decode;
44 end else if (tx_clkdiv == 0) begin
45 tx_state <= tx_state + 1;
49 4'b0001: serial <= tx_data[0];
50 4'b0010: serial <= tx_data[1];
51 4'b0011: serial <= tx_data[2];
52 4'b0100: serial <= tx_data[3];
53 4'b0101: serial <= tx_data[4];
54 4'b0110: serial <= tx_data[5];
55 4'b0111: serial <= tx_data[6];
56 4'b1000: serial <= tx_data[7];
58 4'b1010: tx_busy <= 0;
63 if ((rx_state == 0) && (serialrx == 0) /*&& (rx_hasdata == 0)*/) /* Kick off. */
65 else if ((rx_state != 4'b0000) && (rx_clkdiv == 0)) begin
66 if (rx_state != 4'b1010)
67 rx_state <= rx_state + 1;
71 4'b0001: begin end /* Twiddle thumbs -- this is the end of the half bit. */
72 4'b0010: rx_data[0] <= serialrx;
73 4'b0011: rx_data[1] <= serialrx;
74 4'b0100: rx_data[2] <= serialrx;
75 4'b0101: rx_data[3] <= serialrx;
76 4'b0110: rx_data[4] <= serialrx;
77 4'b0111: rx_data[5] <= serialrx;
78 4'b1000: rx_data[6] <= serialrx;
79 4'b1001: rx_data[7] <= serialrx;
80 4'b1010: begin end /* Expect a 1 */
84 rx_hasdata <= (rx_hasdata && ~(rd && data_decode)) || ((rx_state == 4'b1010) && (tx_clkdiv == 0));
86 if(tx_newdata || (tx_clkdiv == `CLK_DIV))
89 tx_clkdiv <= tx_clkdiv + 1;
91 if ((rx_state == 0) && (serialrx == 0) /*&& (rx_hasdata == 0)*/) /* Wait half a period before advancing. */
92 rx_clkdiv <= `CLK_DIV / 2 + `CLK_DIV / 4;
93 else if (rx_clkdiv == `CLK_DIV)
96 rx_clkdiv <= rx_clkdiv + 1;