// EP08: word-granularity BRAM copy engine (DMA) for the storage-hierarchy test.
// Reads through port B, writes through port A. Sustains one 32-bit word per
// clock when the CPU keeps off the scratch RAM (the test polls only the CSR).
module bram_dma (
    input  wire        clk,
    input  wire        rst_n,
    input  wire        start_toggle,
    input  wire [12:0] src_word,
    input  wire [12:0] dst_word,
    input  wire [12:0] words,
    input  wire        wr_blocked,

    output reg         busy,
    output reg         done,
    output reg  [31:0] cycles,

    output reg         rd_en,
    output reg  [12:0] rd_addr,
    input  wire [31:0] rd_data,
    input  wire        rd_valid,

    output reg         wr_en,
    output reg  [12:0] wr_addr,
    output wire [31:0] wr_data
);

    reg [2:0]  start_sync;
    reg        start_seen;
    wire       start_pulse = start_sync[2] ^ start_sync[1];

    reg [12:0] rd_idx;      // next source word to issue
    reg [12:0] wr_idx;      // next destination word to write
    reg [31:0] data_q;      // one-word skid buffer
    reg        data_valid;
    reg [31:0] cycle_cnt;

    assign wr_data = data_q;

    always @(posedge clk) begin
        if (!rst_n) begin
            start_sync <= 3'b000;
            busy       <= 1'b0;
            done       <= 1'b0;
            cycles     <= 32'd0;
            rd_idx     <= 13'd0;
            wr_idx     <= 13'd0;
            data_q     <= 32'd0;
            data_valid <= 1'b0;
            cycle_cnt  <= 32'd0;
            rd_en      <= 1'b0;
            rd_addr    <= 13'd0;
            wr_en      <= 1'b0;
            wr_addr    <= 13'd0;
            start_seen <= 1'b0;
        end else begin
            start_sync <= {start_sync[1:0], start_toggle};

            if (busy)
                cycle_cnt <= cycle_cnt + 32'd1;

            if (start_pulse && !start_seen && !busy) begin
                start_seen <= 1'b1;
                busy       <= 1'b1;
                done       <= 1'b0;
                cycles     <= 32'd0;
                rd_idx     <= 13'd0;
                wr_idx     <= 13'd0;
                data_valid <= 1'b0;
                cycle_cnt  <= 32'd0;
                rd_en      <= 1'b1;
                rd_addr    <= src_word;
                wr_en      <= 1'b0;
            end else begin
                if (!start_pulse)
                    start_seen <= 1'b0;

                rd_en <= 1'b0;
                wr_en <= 1'b0;

                if (busy) begin
                    // Write the buffered word. In steady state a new word is
                    // accepted on the same edge, keeping the buffer full.
                    if (data_valid && !wr_blocked) begin
                        wr_en   <= 1'b1;
                        wr_addr <= dst_word + wr_idx;
                        wr_idx  <= wr_idx + 13'd1;
                        if ((wr_idx + 13'd1) == words) begin
                            cycles <= cycle_cnt + 32'd1;
                            done   <= 1'b1;
                            busy   <= 1'b0;
                        end
                    end

                    // Issue the next read whenever the buffer is free now, or
                    // will be freed by the write happening on this edge.
                    if ((rd_idx != words) && (!data_valid || !wr_blocked)) begin
                        rd_en   <= 1'b1;
                        rd_addr <= src_word + rd_idx;
                        rd_idx  <= rd_idx + 13'd1;
                    end

                    // Accept returned data when the buffer is free or draining.
                    if (rd_valid && (!data_valid || (!wr_blocked && data_valid))) begin
                        data_q     <= rd_data;
                        data_valid <= 1'b1;
                    end else if (data_valid && !wr_blocked) begin
                        data_valid <= 1'b0;
                    end
                end
            end
        end
    end

endmodule
