`timescale 1ns/1ps
// Clocked SCCB/I2C slave model, third revision: ACK asserted at the falling
// edge that ends the 8th data bit and held through the 9th (ACK) clock.
module sccb_slave (
    input  wire clk,
    input  wire rst_n,
    input  wire scl,
    inout  wire sda
);
    localparam [2:0] S_ADDR = 3'd0,
                     S_REGH = 3'd1,
                     S_REGL = 3'd2,
                     S_RXW  = 3'd3,
                     S_TX   = 3'd4;

    reg [2:0] sm      = S_ADDR;
    reg [3:0] bitn    = 4'd0;
    reg [7:0] rx      = 8'd0;
    reg [7:0] tx      = 8'd0;
    reg [7:0] regh    = 8'd0, regl = 8'd0;
    reg       drv     = 1'b0;
    reg       sout    = 1'b0;
    reg       acking  = 1'b0;
    reg       scl_r   = 1'b1, sda_r = 1'b1;

    wire scl_rise = (~scl_r) & scl;
    wire scl_fall = scl_r & (~scl);
    wire sda_fall = sda_r & (~sda);

    assign sda = drv ? sout : 1'bz;

    wire [7:0] txdata = (regh == 8'h30 && regl == 8'h0A) ? 8'h56 :
                        (regh == 8'h30 && regl == 8'h0B) ? 8'h40 : 8'h00;

    always @(posedge clk) begin
        scl_r <= scl;
        sda_r <= sda;
        if (!rst_n) begin
            sm <= S_ADDR; bitn <= 4'd0; drv <= 1'b0; sout <= 1'b0; acking <= 1'b0;
            rx <= 8'd0; tx <= 8'd0; regh <= 8'd0; regl <= 8'd0;
        end else begin
            if (sda_fall && scl) begin          // START / repeated START
                sm     <= S_ADDR;
                bitn   <= 4'd0;
                drv    <= 1'b0;
                acking <= 1'b0;
                rx     <= 8'd0;
            end

            if (scl_rise) begin
                if (sm != S_TX && bitn < 4'd8) begin
                    rx   <= {rx[6:0], sda};
                    bitn <= bitn + 4'd1;
                end
            end

            if (scl_fall) begin
                if (sm == S_TX && bitn < 4'd8) begin
                    sout <= tx[7];
                    tx   <= {tx[6:0], 1'b0};
                    drv  <= 1'b1;
                    bitn <= bitn + 4'd1;
                end else if (sm == S_TX) begin
                    drv <= 1'b0;
                end else if (bitn == 4'd8 && !acking) begin
                    // 8 bits done: drive ACK for the 9th clock
                    if (sm == S_ADDR) begin
                        acking <= (rx[7:1] == 7'h3C);
                        drv    <= (rx[7:1] == 7'h3C);
                        sout   <= 1'b0;
                    end else begin
                        acking <= 1'b1;
                        drv    <= 1'b1;
                        sout   <= 1'b0;
                    end
                end else if (acking) begin
                    // 9th clock finished
                    acking <= 1'b0;
                    drv    <= 1'b0;
                    bitn   <= 4'd0;
                    if (sm != S_ADDR || rx[7:1] == 7'h3C) begin
                        case (sm)
                            S_ADDR: begin
                                if (rx[0]) begin
                                    sm   <= S_TX;
                                    tx   <= {txdata[6:0], 1'b0};
                                    sout <= txdata[7];
                                    drv  <= 1'b1;      // first data bit already in place
                                    bitn <= 4'd1;
                                end else begin
                                    sm <= S_REGH;
                                end
                            end
                            S_REGH: begin regh <= rx; sm <= S_REGL; end
                            S_REGL: begin regl <= rx; sm <= S_RXW;  end
                            S_RXW:  begin sm <= S_ADDR; end
                            default: sm <= S_ADDR;
                        endcase
                    end
                end
            end
        end
    end
endmodule
