// Exercise P3: the FIFO adder.  Numbers pushed into FIFO A and FIFO B at
// different times are added in pairs; the sums wait in FIFO C until they
// are popped.  When C is full, A and B keep their words: backpressure.
//
// Generic version that matches schematic 1, not tied to any chip.  Like
// the 4013s of the breadboard, the design samples the buttons at the
// rising edge of clk and acts once per press.  A transfer takes two clock
// edges, as the transfer flip-flop does it:
//
//     go = A has data & B has data & C has room & ~ transfer
//     the edge that sees go:        transfer goes to 1, C takes the sum of
//                                   the heads of A and B
//     the edge after it:            transfer goes to 0, A and B drop the
//                                   pair
//
// The operands are 3 bits wide, so the sum always fits into 4 bits.  rst
// empties the FIFOs and clears the flip-flops at once.  Synthesizable.

module fifo_adder
(
    input  logic       clk,
    input  logic       rst,

    input  logic       push_a,
    input  logic       push_b,
    input  logic       pop_c,
    input  logic [2:0] data,

    output logic [2:0] head_a,
    output logic       dor_a,      // A has data
    output logic [2:0] head_b,
    output logic       dor_b,      // B has data
    output logic [3:0] head_c,
    output logic       dor_c,      // C has data
    output logic       dir_c,      // C has room
    output logic       transfer    // The transfer pulse, T
);

    //------------------------------------------------------------------------
    // The buttons at the last clock edge, like the 4013 flip-flops

    logic push_a_q, push_b_q, pop_c_q;

    wire push_a_now = push_a & ~ push_a_q;
    wire push_b_now = push_b & ~ push_b_q;
    wire pop_c_now  = pop_c  & ~ pop_c_q;

    //------------------------------------------------------------------------
    // Three FIFOs of sixteen words: write and read counters with one bit
    // more than the address, equal when empty, 16 apart when full

    logic [2:0] word_a [0:15], word_b [0:15];
    logic [3:0] word_c [0:15];
    logic [4:0] wr_a, rd_a, wr_b, rd_b, wr_c, rd_c;

    wire dir_a = wr_a != { ~ rd_a [4], rd_a [3:0] };
    wire dir_b = wr_b != { ~ rd_b [4], rd_b [3:0] };

    assign dor_a  = wr_a != rd_a;
    assign dor_b  = wr_b != rd_b;
    assign dor_c  = wr_c != rd_c;
    assign dir_c  = wr_c != { ~ rd_c [4], rd_c [3:0] };

    assign head_a = word_a [rd_a [3:0]];
    assign head_b = word_b [rd_b [3:0]];
    assign head_c = word_c [rd_c [3:0]];

    //------------------------------------------------------------------------
    // The adder and the transfer

    wire [3:0] sum = 4' (head_a) + 4' (head_b);
    wire       go  = dor_a & dor_b & dir_c & ~ transfer;

    always_ff @ (posedge clk or posedge rst)
        if (rst)
        begin
            push_a_q <= 1'b0;
            push_b_q <= 1'b0;
            pop_c_q  <= 1'b0;
            transfer <= 1'b0;
            wr_a     <= '0;
            rd_a     <= '0;
            wr_b     <= '0;
            rd_b     <= '0;
            wr_c     <= '0;
            rd_c     <= '0;
        end
        else
        begin
            push_a_q <= push_a;
            push_b_q <= push_b;
            pop_c_q  <= pop_c;
            transfer <= go;

            if (push_a_now & dir_a)
                wr_a <= wr_a + 5'd1;

            if (push_b_now & dir_b)
                wr_b <= wr_b + 5'd1;

            if (go)
                wr_c <= wr_c + 5'd1;

            if (transfer)
            begin
                rd_a <= rd_a + 5'd1;
                rd_b <= rd_b + 5'd1;
            end

            if (pop_c_now & dor_c)
                rd_c <= rd_c + 5'd1;
        end

    always_ff @ (posedge clk)
    begin
        if (push_a_now & dir_a)
            word_a [wr_a [3:0]] <= data;

        if (push_b_now & dir_b)
            word_b [wr_b [3:0]] <= data;

        if (go)
            word_c [wr_c [3:0]] <= sum;
    end

endmodule
