Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· OpenAIAi2 WildChat-4.8M (AI2)

다음 아래의 코드는 문제가 없는 것 같은가? <<StartOfFile:DMAC/RTL/DMACCFG.sv>>> module DMACCFG ( input…

  1. User

    다음 아래의 코드는 문제가 없는 것 같은가?

    <StartOfFile:DMAC/RTL/DMAC_CFG.sv>> module DMAC_CFG ( input wire clk, input wire rst_n, // _n means active low

    // AMBA APB interface
    input wire psel_i,
    input wire penable_i,
    input wire [11:0] paddr_i,
    input wire pwrite_i,
    input wire [31:0] pwdata_i,
    output reg pready_o,
    output reg [31:0] prdata_o,
    output reg pslverr_o,
    
    // configuration registers
    output reg [31:0] src_addr_o,
    output reg [31:0] dst_addr_o,
    output reg [15:0] byte_len_o,
    output wire start_o,
    input wire done_i
    

    );

    // Configuration register to read/write
    reg [31:0] src_addr;
    reg [31:0] dst_addr;
    reg [15:0] byte_len;
    
    //----------------------------------------------------------
    // Write
    //----------------------------------------------------------
    // an APB write occurs when PSEL & PENABLE & PWRITE
    // clk : __--__--__--__--__--__--__--__--__--__--
    // psel : ___--------_____________________________
    // penable : _______----_____________________________
    // pwrite : ___--------_____________________________
    // wren : _______----_____________________________
    //
    // DMA start command must be asserted when APB writes 1 to the DMA_CMD
    // register
    // clk : __--__--__--__--__--__--__--__--__--__--
    // psel : ___--------_____________________________
    // penable : _______----_____________________________
    // pwrite : ___--------_____________________________
    // paddr : |DMA_CMD|
    // pwdata : | 1 |
    // start : _______----_____________________________
    
    wire wren = psel_i & penable_i & pwrite_i;
    
    always @(posedge clk) begin
        if (!rst_n) begin
            src_addr <= 32'd0;
            dst_addr <= 32'd0;
            byte_len <= 16'd0;
        end
        else if (wren) begin
            case (paddr_i)
                'h100: src_addr <= pwdata_i[31:0];
                'h104: dst_addr <= pwdata_i[31:0];
                'h108: byte_len <= pwdata_i[15:0];
            endcase
        end
    end
    wire start = wren & (paddr_i=='h10C) & pwdata_i[0];
    
    //----------------------------------------------------------
    // READ
    //----------------------------------------------------------
    // an APB read occurs when PSEL & PENABLE & !PWRITE
    // To make read data a direct output from register,
    // this code shall buffer the muxed read data into a register
    // in the SETUP cycle (PSEL & !PENABLE)
    // clk : __--__--__--__--__--__--__--__--__--__--
    // psel : ___--------_____________________________
    // penable : _______----_____________________________
    // pwrite : ________________________________________
    // reg update : ___----_________________________________
    // prdata : |DATA
    
    reg [31:0] rdata;
    
    always @(posedge clk) begin
        if (!rst_n) begin
            rdata <= 32'd0;
        end
        else if (psel_i & !penable_i & !pwrite_i) begin // in the setup cycle in the APB state diagram
            case (paddr_i)
                'h0: rdata <= 32'h0001_2024;
                'h100: rdata <= src_addr;
                'h104: rdata <= dst_addr;
                'h108: rdata <= {16'd0, byte_len};
                'h110: rdata <= {31'd0, done_i};
                default: rdata <= 32'd0;
            endcase
        end
    end
    
    // output assignments
    assign pready_o = 1'b1;
    assign prdata_o = rdata;
    assign pslverr_o = 1'b0;
    
    assign src_addr_o = src_addr;
    assign dst_addr_o = dst_addr;
    assign byte_len_o = byte_len;
    assign start_o = start;
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_CFG.sv>>

    <<StartOfFile:DMAC/RTL/DMAC_ENGINE.sv>> module DMAC_ENGINE ( input wire clk, input wire rst_n, // _n means active low

    // configuration registers
    input wire [31:0] src_addr_i,
    input wire [31:0] dst_addr_i,
    input wire [15:0] byte_len_i,
    input wire start_i,
    output wire done_o,
    
    // AMBA AXI interface (AW channel)
    output wire [3:0] awid_o,
    output wire [31:0] awaddr_o,
    output wire [3:0] awlen_o,
    output wire [2:0] awsize_o,
    output wire [1:0] awburst_o,
    output wire awvalid_o,
    input wire awready_i,
    
    // AMBA AXI interface (W channel)
    output wire [3:0] wid_o,
    output wire [31:0] wdata_o,
    output wire [3:0] wstrb_o,
    output wire wlast_o,
    output wire wvalid_o,
    input wire wready_i,
    
    // AMBA AXI interface (B channel)
    input wire [3:0] bid_i,
    input wire [1:0] bresp_i,
    input wire bvalid_i,
    output wire bready_o,
    
    // AMBA AXI interface (AR channel)
    output wire [3:0] arid_o,
    output wire [31:0] araddr_o,
    output wire [3:0] arlen_o,
    output wire [2:0] arsize_o,
    output wire [1:0] arburst_o,
    output wire arvalid_o,
    input wire arready_i,
    
    // AMBA AXI interface (R channel)
    input wire [3:0] rid_i,
    input wire [31:0] rdata_i,
    input wire [1:0] rresp_i,
    input wire rlast_i,
    input wire rvalid_i,
    output wire rready_o
    

    );

    // mnemonics for state values
    localparam S_IDLE = 3'd0,
                                S_RREQ = 3'd1,
                                S_RDATA = 3'd2,
                                S_WREQ = 3'd3,
                                S_WDATA = 3'd4;
    
    reg [2:0] state, state_n;
    
    reg [31:0] src_addr, src_addr_n;
    reg [31:0] dst_addr, dst_addr_n;
    reg [15:0] cnt, cnt_n;
    reg [3:0] wcnt, wcnt_n;
    
    reg arvalid,
                                rready,
                                awvalid,
                                wvalid,
                                wlast,
                                done;
    
    wire fifo_full,
                                fifo_empty;
    reg fifo_wren,
                                fifo_rden;
    wire [31:0] fifo_rdata;
    
    
    // it's desirable to code registers in a simple way
    always_ff @(posedge clk)
        if (!rst_n) begin
            state <= S_IDLE;
    
            src_addr <= 32'd0;
            dst_addr <= 32'd0;
            cnt <= 16'd0;
    
            wcnt <= 4'd0;
        end
        else begin
            state <= state_n;
    
            src_addr <= src_addr_n;
            dst_addr <= dst_addr_n;
            cnt <= cnt_n;
    
            wcnt <= wcnt_n;
        end
    
    
    // this block programs output values and next register values
    // based on states.
    always_comb begin
        // ********************** 
        // ********************** 
        // FILL YOUR CODE HERE
        // ********************** 
        // ********************** 
    end
    
    DMAC_FIFO u_fifo
    (
        .clk (clk),
        .rst_n (rst_n),
    
        .full_o (fifo_full),
        .wren_i (fifo_wren),
        .wdata_i (rdata_i),
    
        .empty_o (fifo_empty),
        .rden_i (fifo_rden),
        .rdata_o (fifo_rdata)
    );
    
    // Output assigments
    assign done_o = done;
    
    assign awid_o = 4'd0;
    assign awaddr_o = dst_addr;
    assign awlen_o = (cnt >= 'd64) ? 4'hF: cnt[5:2]-4'h1;
    assign awsize_o = 3'b010; // 4 bytes per transfer
    assign awburst_o = 2'b01; // incremental
    assign awvalid_o = awvalid;
    
    assign wid_o = 4'd0;
    assign wdata_o = fifo_rdata;
    assign wstrb_o = 4'b1111; // all bytes within 4 byte are valid
    assign wlast_o = wlast;
    assign wvalid_o = wvalid;
    
    assign bready_o = 1'b1;
    
    assign arvalid_o = arvalid;
    assign araddr_o = src_addr;
    assign arid_o = 4'd0;
    assign arlen_o = (cnt >= 'd64) ? 4'hF: cnt[5:2]-4'h1;
    assign arsize_o = 3'b010; // 4 bytes per transfer
    assign arburst_o = 2'b01; // incremental
    assign arvalid_o = arvalid;
    
    assign rready_o = rready & !fifo_full;
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_ENGINE.sv>>

    <<StartOfFile:DMAC/RTL/DMAC_FIFO.sv>> module DMAC_FIFO #( parameter DEPTH_LG2 = 4, parameter DATA_WIDTH = 32 ) ( input wire clk, input wire rst_n,

    output wire full_o,
    input wire wren_i,
    input wire [DATA_WIDTH-1:0] wdata_i,
    
    output wire empty_o,
    input wire rden_i,
    output wire [DATA_WIDTH-1:0] rdata_o
    

    );

    localparam FIFO_DEPTH = (1<<DEPTH_LG2);
    
    reg [DATA_WIDTH-1:0] data[FIFO_DEPTH];
    
    reg full, full_n,
                                        empty, empty_n;
    reg [DEPTH_LG2:0] wrptr, wrptr_n,
                                        rdptr, rdptr_n;
    
    // reset entries to all 0s
    always_ff @(posedge clk)
        if (!rst_n) begin
            full <= 1'b0;
            empty <= 1'b1; // empty after as reset
    
            wrptr <= {(DEPTH_LG2+1){1'b0}};
            rdptr <= {(DEPTH_LG2+1){1'b0}};
    
            for (int i=0; i<FIFO_DEPTH; i++) begin
                data[i] <= {DATA_WIDTH{1'b0}};
            end
        end
        else begin
            full <= full_n;
            empty <= empty_n;
    
            wrptr <= wrptr_n;
            rdptr <= rdptr_n;
    
            if (wren_i) begin
                data[wrptr[DEPTH_LG2-1:0]] <= wdata_i;
            end
        end
    
    always_comb begin
        wrptr_n = wrptr;
        rdptr_n = rdptr;
    
        if (wren_i) begin
            wrptr_n = wrptr + 'd1;
        end
    
        if (rden_i) begin
            rdptr_n = rdptr + 'd1;
        end
    
        empty_n = (wrptr_n == rdptr_n);
        full_n = (wrptr_n[DEPTH_LG2]!=rdptr_n[DEPTH_LG2])
                                     &(wrptr_n[DEPTH_LG2-1:0]==rdptr_n[DEPTH_LG2-1:0]);
    
    end
    
    // synthesis translate_off
    always @(posedge clk) begin
        if (full_o & wren_i) begin
            \$display("FIFO overflow");
            @(posedge clk);
            \$finish;
        end
    end
    
    always @(posedge clk) begin
        if (empty_o & rden_i) begin
            \$display("FIFO underflow");
            @(posedge clk);
            \$finish;
        end
    end
    // synthesis translate_on
    
    assign full_o = full;
    assign empty_o = empty;
    assign rdata_o = data[rdptr[DEPTH_LG2-1:0]];
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_FIFO.sv>>

    <<StartOfFile:DMAC/RTL/DMAC_TOP.sv>> module DMAC_TOP ( input wire clk, input wire rst_n, // _n means active low

    // AMBA APB interface
    input wire psel_i,
    input wire penable_i,
    input wire [11:0] paddr_i,
    input wire pwrite_i,
    input wire [31:0] pwdata_i,
    output reg pready_o,
    output reg [31:0] prdata_o,
    output reg pslverr_o,
    
    // AMBA AXI interface (AW channel)
    output wire [3:0] awid_o,
    output wire [31:0] awaddr_o,
    output wire [3:0] awlen_o,
    output wire [2:0] awsize_o,
    output wire [1:0] awburst_o,
    output wire awvalid_o,
    input wire awready_i,
    
    // AMBA AXI interface (AW channel)
    output wire [3:0] wid_o,
    output wire [31:0] wdata_o,
    output wire [3:0] wstrb_o,
    output wire wlast_o,
    output wire wvalid_o,
    input wire wready_i,
    
    // AMBA AXI interface (B channel)
    input wire [3:0] bid_i,
    input wire [1:0] bresp_i,
    input wire bvalid_i,
    output wire bready_o,
    
    // AMBA AXI interface (AR channel)
    output wire [3:0] arid_o,
    output wire [31:0] araddr_o,
    output wire [3:0] arlen_o,
    output wire [2:0] arsize_o,
    output wire [1:0] arburst_o,
    output wire arvalid_o,
    input wire arready_i,
    
    // AMBA AXI interface (R channel)
    input wire [3:0] rid_i,
    input wire [31:0] rdata_i,
    input wire [1:0] rresp_i,
    input wire rlast_i,
    input wire rvalid_i,
    output wire rready_o
    

    );

    wire [31:0] src_addr;
    wire [31:0] dst_addr;
    wire [15:0] byte_len;
    wire start;
    wire done;
    
    DMAC_CFG u_cfg(
        .clk (clk),
        .rst_n (rst_n),
    
        // AMBA APB interface
        .psel_i (psel_i),
        .penable_i (penable_i),
        .paddr_i (paddr_i),
        .pwrite_i (pwrite_i),
        .pwdata_i (pwdata_i),
        .pready_o (pready_o),
        .prdata_o (prdata_o),
        .pslverr_o (pslverr_o),
    
        .src_addr_o (src_addr),
        .dst_addr_o (dst_addr),
        .byte_len_o (byte_len),
        .start_o (start),
        .done_i (done)
    );
    
    DMAC_ENGINE u_engine(
        .clk (clk),
        .rst_n (rst_n),
    
        // configuration registers
        .src_addr_i (src_addr),
        .dst_addr_i (dst_addr),
        .byte_len_i (byte_len),
        .start_i (start),
        .done_o (done),
    
        // AMBA AXI interface (AW channel)
        .awid_o (awid_o),
        .awaddr_o (awaddr_o),
        .awlen_o (awlen_o),
        .awsize_o (awsize_o),
        .awburst_o (awburst_o),
        .awvalid_o (awvalid_o),
        .awready_i (awready_i),
    
        // AMBA AXI interface (W channel)
        .wid_o (wid_o),
        .wdata_o (wdata_o),
        .wstrb_o (wstrb_o),
        .wlast_o (wlast_o),
        .wvalid_o (wvalid_o),
        .wready_i (wready_i),
    
        // AMBA AXI interface (B channel)
        .bid_i (bid_i),
        .bresp_i (bresp_i),
        .bvalid_i (bvalid_i),
        .bready_o (bready_o),
    
        // AMBA AXI interface (AR channel)
        .arid_o (arid_o),
        .araddr_o (araddr_o),
        .arlen_o (arlen_o),
        .arsize_o (arsize_o),
        .arburst_o (arburst_o),
        .arvalid_o (arvalid_o),
        .arready_i (arready_i),
    
        // AMBA AXI interface (R channel)
        .rid_i (rid_i),
        .rdata_i (rdata_i),
        .rresp_i (rresp_i),
        .rlast_i (rlast_i),
        .rvalid_i (rvalid_i),
        .rready_o (rready_o)
    );
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_TOP.sv>>

    <<StartOfFile:DMAC/RTL/filelist.f>> -sverilog $LAB_PATH/RTL/DMAC_TOP.sv -sverilog $LAB_PATH/RTL/DMAC_CFG.sv -sverilog $LAB_PATH/RTL/DMAC_ENGINE.sv -sverilog $LAB_PATH/RTL/DMAC_FIFO.sv <<EndOfFile:DMAC/RTL/filelist.f>>

    <<StartOfFile:DMAC/SIM/TB/AXI_INTF.sv>> `include "../TB/AXI_TYPEDEF.svh"

    interface AXI_AW_CH #( parameter ADDR_WIDTH = AXI_ADDR_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic awvalid; logic awready; logic [ID_WIDTH-1:0] awid; logic [ADDR_WIDTH-1:0] awaddr; logic [3:0] awlen; logic [2:0] awsize; logic [1:0] awburst;

    endinterface

    interface AXI_W_CH #( parameter DATA_WIDTH = AXI_DATA_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic wvalid; logic wready; logic [ID_WIDTH-1:0] wid; logic [DATA_WIDTH-1:0] wdata; logic [DATA_WIDTH/8-1:0] wstrb; logic wlast;

    endinterface

    interface AXI_B_CH #( parameter ID_WIDTH = `AXI_ID_WIDTH ) ( input clk ); logic bvalid; logic bready; logic [ID_WIDTH-1:0] bid; logic [1:0] bresp;

    endinterface

    interface AXI_AR_CH #( parameter ADDR_WIDTH = AXI_ADDR_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic arvalid; logic arready; logic [ID_WIDTH-1:0] arid; logic [ADDR_WIDTH-1:0] araddr; logic [3:0] arlen; logic [2:0] arsize; logic [1:0] arburst;

    endinterface

    interface AXI_R_CH #( parameter DATA_WIDTH = AXI_DATA_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic rvalid; logic rready; logic [ID_WIDTH-1:0] rid; logic [DATA_WIDTH-1:0] rdata; logic [1:0] rresp; logic rlast;

    endinterface

    interface APB ( input clk ); logic psel; logic penable; logic [31:0] paddr; logic pwrite; logic [31:0] pwdata; logic pready; logic [31:0] prdata; logic pslverr;

    modport master (
        input clk,
        input pready, prdata, pslverr,
        output psel, penable, paddr, pwrite, pwdata
    );
    
    task init();
        psel = 1'b0;
        penable = 1'b0;
        paddr = 32'd0;
        pwrite = 1'b0;
        pwdata = 32'd0;
    endtask
    
    task write(input int addr,
               input int data);
        #1
        psel = 1'b1;
        penable = 1'b0;
        paddr = addr;
        pwrite = 1'b1;
        pwdata = data;
        @(posedge clk);
        #1
        penable = 1'b1;
        @(posedge clk);
    
        while (pready==1'b0) begin
            @(posedge clk);
        end
    
        psel = 1'b0;
        penable = 1'b0;
        paddr = 'hX;
        pwrite = 1'bx;
        pwdata = 'hX;
    endtask
    
    task read(input int addr,
              output int data);
        #1
        psel = 1'b1;
        penable = 1'b0;
        paddr = addr;
        pwrite = 1'b0;
        pwdata = 'hX;
        @(posedge clk);
        #1
        penable = 1'b1;
        @(posedge clk);
    
        while (pready==1'b0) begin
            @(posedge clk);
        end
        data = prdata;
    
        psel = 1'b0;
        penable = 1'b0;
        paddr = 'hX;
        pwrite = 1'bx;
        pwdata = 'hX;
    endtask
    

    endinterface <<EndOfFile:DMAC/SIM/TB/AXI_INTF.sv>>

    <<StartOfFile:DMAC/SIM/TB/AXI_SLAVE.sv>> `include "../TB/AXI_TYPEDEF.svh"

    module AXI_SLAVE #( parameter ADDR_WIDTH = 16, parameter DATA_WIDTH = AXI_DATA_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH, parameter AWREADY_DELAY = 1, parameter ARREADY_DELAY = 1, parameter AR2R_DELAY = 50 ) ( input wire clk, input wire rst_n, // _n means active low

    AXI_AW_CH aw_ch,
    AXI_W_CH w_ch,
    AXI_B_CH b_ch,
    AXI_AR_CH ar_ch,
    AXI_R_CH r_ch
    

    );

    localparam DATA_DEPTH = 1<<ADDR_WIDTH;
    
    logic [7:0] mem[DATA_DEPTH];
    
    function void write_byte(int addr, input bit [7:0] wdata);
        mem[addr] = wdata;
    endfunction
    
    function void write_word(int addr, input bit [31:0] wdata);
        for (int i=0; i<4; i++) begin
            write_byte(addr+i, wdata[8*i +: 8]); // [i*8+7:i*8]
        end
    endfunction
    
    function bit [7:0] read_byte(int addr);
        read_byte = mem[addr];
    endfunction
    
    function bit [31:0] read_word(int addr);
        for (int i=0; i<4; i++) begin
            read_word[8*i +: 8] = read_byte(addr+i);// [i*8+7:i*8]
        end
    endfunction
    
    //----------------------------------------------------------
    // write channels (AW, W, B)
    //----------------------------------------------------------
    localparam logic [1:0] S_W_IDLE = 0,
                                S_W_AWREADY = 1,
                                S_W_BURST = 2,
                                S_W_RESP = 3;
    
    logic [1:0] wstate, wstate_n;
    logic [7:0] wcnt, wcnt_n;
    
    logic [ADDR_WIDTH-1:0] waddr, waddr_n;
    logic [ID_WIDTH-1:0] wid, wid_n;
    logic [3:0] wlen, wlen_n;
    
    always_ff @(posedge clk)
        if (!rst_n) begin
            wstate <= S_W_IDLE;
    
            wcnt <= 8'd0;
            waddr <= {ADDR_WIDTH{1'b0}};
            wid <= {ID_WIDTH{1'b0}};
            wlen <= 4'd0;
        end
        else begin
            wstate <= wstate_n;
    
            wcnt <= wcnt_n;
            waddr <= waddr_n;
            wid <= wid_n;
            wlen <= wlen_n;
        end
    
    always @(*) begin
        wstate_n = wstate;
    
        wcnt_n = wcnt;
        waddr_n = waddr;
        wid_n = wid;
        wlen_n = wlen;
    
        aw_ch.awready = 1'b0;
        w_ch.wready = 1'b0;
        b_ch.bvalid = 1'b0;
    
        case (wstate)
            S_W_IDLE: begin
                if (aw_ch.awvalid) begin
                    if (AWREADY_DELAY == 0) begin
                        waddr_n = aw_ch.awaddr;
                        wid_n = aw_ch.awid;
                        wlen_n = aw_ch.awlen;
                        aw_ch.awready = 1'b1;
                        wstate_n = S_W_BURST;
                    end
                    else begin
                        wcnt_n = AWREADY_DELAY-1;
                        wstate_n = S_W_AWREADY;
                    end
                end
            end
            S_W_AWREADY: begin
                if (wcnt==0) begin
                    waddr_n = aw_ch.awaddr;
                    wid_n = aw_ch.awid;
                    wlen_n = aw_ch.awlen;
                    aw_ch.awready = 1'b1;
                    wstate_n = S_W_BURST;
                end
                else begin
                    wcnt_n = wcnt - 8'd1;
                end
            end
            S_W_BURST: begin
                w_ch.wready = 1'b1;
                if (w_ch.wvalid) begin
                    for (int i=0; i<DATA_WIDTH/8; i++) begin
                        write_byte(waddr + i, w_ch.wdata[i*8 +: 8]); // [i*8+7:i*8]
                    end
                    waddr_n = waddr + (DATA_WIDTH/8);
                    if (wlen==4'd0) begin
                        wstate_n = S_W_RESP;
                    end
                    else begin
                        wlen_n = wlen - 4'd1;
                    end
                end
            end
            S_W_RESP: begin
                b_ch.bvalid = 1'b1;
                if (b_ch.bready) begin
                    wstate_n = S_W_IDLE;
                end
            end
        endcase
    end
    
    //----------------------------------------------------------
    // read channel (AR, R)
    //----------------------------------------------------------
    localparam logic [1:0] S_R_IDLE = 0,
                                S_R_ARREADY = 1,
                                S_R_DELAY = 2,
                                S_R_BURST = 3;
    
    logic [1:0] rstate, rstate_n;
    logic [7:0] rcnt, rcnt_n;
    
    logic [ADDR_WIDTH-1:0] raddr, raddr_n;
    logic [ID_WIDTH-1:0] rid, rid_n;
    logic [3:0] rlen, rlen_n;
    
    always_ff @(posedge clk)
        if (!rst_n) begin
            rstate <= S_R_IDLE;
    
            rcnt <= 8'd0;
            raddr <= {ADDR_WIDTH{1'b0}};
            rid <= {ID_WIDTH{1'b0}};
            rlen <= 4'd0;
        end
        else begin
            rstate <= rstate_n;
    
            rcnt <= rcnt_n;
            raddr <= raddr_n;
            rid <= rid_n;
            rlen <= rlen_n;
        end
    
    always_comb begin
        rstate_n = rstate;
    
        rcnt_n = rcnt;
        raddr_n = raddr;
        rid_n = rid;
        rlen_n = rlen;
    
        ar_ch.arready = 1'b0;
        r_ch.rvalid = 1'b0;
        r_ch.rlast = 1'b0;
    
        case (rstate)
            S_R_IDLE: begin
                if (ar_ch.arvalid) begin
                    if (ARREADY_DELAY == 0) begin
                        raddr_n = ar_ch.araddr;
                        rid_n = ar_ch.arid;
                        rlen_n = ar_ch.arlen;
                        ar_ch.arready = 1'b1;
    
                        rcnt_n = AR2R_DELAY - 1;
                        rstate_n = S_R_DELAY;
                    end
                    else begin
                        rcnt_n = ARREADY_DELAY-1;
                        rstate_n = S_R_ARREADY;
                    end
                end
            end
            S_R_ARREADY: begin
                if (rcnt==0) begin
                    raddr_n = ar_ch.araddr;
                    rid_n = ar_ch.arid;
                    rlen_n = ar_ch.arlen;
                    ar_ch.arready = 1'b1;
    
                    rcnt_n = AR2R_DELAY - 1;
                    rstate_n = S_R_DELAY;
                end
                else begin
                    rcnt_n = rcnt - 8'd1;
                end
            end
            S_R_DELAY: begin
                if (rcnt==0) begin
                    rstate_n = S_R_BURST;
                end
                else begin
                    rcnt_n = rcnt - 8'd1;
                end
            end
            S_R_BURST: begin
                r_ch.rvalid = 1'b1;
                r_ch.rlast = (rlen==4'd0);
                for (int i=0; i<DATA_WIDTH/8; i++) begin
                    r_ch.rdata[i*8 +: 8] = read_byte(raddr + i); // [i*8+7:i*8]
                end
                if (r_ch.rready) begin
                    raddr_n = raddr + (DATA_WIDTH/8);
                    if (rlen==4'd0) begin
                        rstate_n = S_R_IDLE;
                    end
                    else begin
                        rlen_n = rlen - 4'd1;
                    end
                end
            end
        endcase
    end
    
    // output assignments
    assign b_ch.bid = wid;
    assign b_ch.bresp = 2'd0;
    assign r_ch.rid = rid;
    assign r_ch.rresp = 2'd0;
    

    endmodule <<EndOfFile:DMAC/SIM/TB/AXI_SLAVE.sv>>

    <<StartOfFile:DMAC/SIM/TB/AXI_TYPEDEF.svh>> ifndef __AXI_TYPEDEF_SVH__ define AXI_TYPEDEF_SVH

    define AXI_ADDR_WIDTH 32 define AXI_DATA_WIDTH 32 `define AXI_ID_WIDTH 4

    `endif /* AXI_TYPEDEF_SVH */ <<EndOfFile:DMAC/SIM/TB/AXI_TYPEDEF.svh>>

    <<StartOfFile:DMAC/SIM/TB/DMAC_TOP_TB.sv>> define IP_VER 32'h000 define SRC_ADDR 32'h100 define DST_ADDR 32'h104 define LEN_ADDR 32'h108 define STAT_ADDR 32'h110 define START_ADDR 32'h10c

    `define TIMEOUT_CYCLE 999999 module DMAC_TOP_TB ();

    reg clk;
    reg rst_n;
    
    // clock generation
    initial begin
        clk = 1'b0;
    
        forever #10 clk = !clk;
    end
    
    // reset generation
    initial begin
        rst_n = 1'b0; // active at time 0
    
        repeat (3) @(posedge clk); // after 3 cycles,
        rst_n = 1'b1; // release the reset
    end
    
    // enable waveform dump
    initial begin
        \$dumpvars(0, u_DUT);
        \$dumpfile("dump.vcd");
    end
    

    // timeout initial begin #`TIMEOUT_CYCLE $display("Timeout!"); $finish; end

    APB apb_if (.clk(clk));
    
    AXI_AW_CH aw_ch (.clk(clk));
    AXI_W_CH w_ch (.clk(clk));
    AXI_B_CH b_ch (.clk(clk));
    AXI_AR_CH ar_ch (.clk(clk));
    AXI_R_CH r_ch (.clk(clk));
    
    task test_init();
        int data;
        apb_if.init();
    
        @(posedge rst_n); // wait for a release of the reset
        repeat (10) @(posedge clk); // wait another 10 cycles
    
        apb_if.read(`IP_VER, data);
        \$display("---------------------------------------------------");
        \$display("IP version: %x", data);
        \$display("---------------------------------------------------");
    
        \$display("---------------------------------------------------");
        \$display("Reset value test");
        \$display("---------------------------------------------------");
        apb_if.read(`SRC_ADDR, data);
        if (data===0)
            \$display("DMA_SRC(pass): %x", data);
        else begin
            \$display("DMA_SRC(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.read(`DST_ADDR, data);
        if (data===0)
            \$display("DMA_DST(pass): %x", data);
        else begin
            \$display("DMA_DST(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.read(`LEN_ADDR, data);
        if (data===0)
            \$display("DMA_LEN(pass): %x", data);
        else begin
            \$display("DMA_LEN(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.read(`STAT_ADDR, data);
        if (data===1)
            \$display("DMA_STATUS(pass): %x", data);
        else begin
            \$display("DMA_STATUS(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
    endtask
    
    task test_dma(input int src, input int dst, input int len);
        int data;
        int word;
        realtime elapsed_time;
    
        \$display("---------------------------------------------------");
        \$display("Load data to memory");
        \$display("---------------------------------------------------");
        for (int i=src; i<(src+len); i=i+4) begin
            word = \$random; 
            u_mem.write_word(i, word);
        end
    
        \$display("---------------------------------------------------");
        \$display("Configuration test");
        \$display("---------------------------------------------------");
        apb_if.write(`SRC_ADDR, src);
        apb_if.read(`SRC_ADDR, data);
        if (data===src)
            \$display("DMA_SRC(pass): %x", data);
        else begin
            \$display("DMA_SRC(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.write(`DST_ADDR, dst);
        apb_if.read(`DST_ADDR, data);
        if (data===dst)
            \$display("DMA_DST(pass): %x", data);
        else begin
            \$display("DMA_DST(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.write(`LEN_ADDR, len);
        apb_if.read(`LEN_ADDR, data);
        if (data===len)
            \$display("DMA_LEN(pass): %x", data);
        else begin
            \$display("DMA_LEN(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
    
        \$display("---------------------------------------------------");
        \$display("DMA start");
        \$display("---------------------------------------------------");
        apb_if.write(`START_ADDR, 32'h1);
        elapsed_time = \$realtime;
    
        \$display("---------------------------------------------------");
        \$display("Wait for a DMA completion");
        \$display("---------------------------------------------------");
        data = 0;
        while (data!=1) begin
            apb_if.read(`STAT_ADDR, data);
            repeat (100) @(posedge clk);
        end
        @(posedge clk);
        elapsed_time = \$realtime - elapsed_time;
        \$timeformat(-9, 0, " ns", 10);
        \$display("Elapsed time for DMA: %t", elapsed_time);
    
        \$display("---------------------------------------------------");
        \$display("DMA completed");
        \$display("---------------------------------------------------");
    
        repeat (len) @(posedge clk); // to make sure data is written
    
        \$display("---------------------------------------------------");
        \$display("verify data");
        \$display("---------------------------------------------------");
        for (int i=0; i<len; i=i+4) begin
            logic [31:0] src_word;
            logic [31:0] dst_word;
            src_word = u_mem.read_word(src+i);
            dst_word = u_mem.read_word(dst+i);
            if (src_word!==dst_word) begin
                \$display("Mismatch! (src:%x @%x, dst:%x @%x", src_word, src+i, dst_word, dst+i);
            end
        end
    endtask
    
    int src,
                    dst,
                    len;
    
    // main
    initial begin
        test_init();
    
        src = 'h0000_1000;
        dst = 'h0000_2000;
        len = 'h0100;
        \$display("===================================================");
        \$display("= 1st trial"); 
        \$display("= Copying %x bytes from %x to %x", len, src, dst);
        \$display("===================================================");
        test_dma(src, dst, len);
    
        src = 'h1234_1234;
        dst = 'hABCD_ABCC;
        len = 'h0F00;
        \$display("===================================================");
        \$display("= 2nd trial (long transfer)");
        \$display("= Copying %x bytes from %x to %x", len, src, dst);
        \$display("===================================================");
        test_dma(src, dst, len);
    
        src = 'h4278_0000;
        dst = 'h4278_1000;
        len = 'h0F10;
        \$display("===================================================");
        \$display("= 3rd trial (long transfer-2)");
        \$display("= Copying %x bytes from %x to %x", len, src, dst);
        \$display("===================================================");
        test_dma(src, dst, len);
        \$finish;
    end
    
    
    AXI_SLAVE u_mem (
        .clk (clk),
        .rst_n (rst_n),
    
        .aw_ch (aw_ch),
        .w_ch (w_ch),
        .b_ch (b_ch),
        .ar_ch (ar_ch),
        .r_ch (r_ch)
    );
    
    DMAC_TOP u_DUT (
        .clk (clk),
        .rst_n (rst_n),
    
        // APB interface
        .psel_i (apb_if.psel),
        .penable_i (apb_if.penable),
        .paddr_i (apb_if.paddr[11:0]),
        .pwrite_i (apb_if.pwrite),
        .pwdata_i (apb_if.pwdata),
        .pready_o (apb_if.pready),
        .prdata_o (apb_if.prdata),
        .pslverr_o (apb_if.pslverr),
    
        // AXI AW channel
        .awid_o (aw_ch.awid),
        .awaddr_o (aw_ch.awaddr),
        .awlen_o (aw_ch.awlen),
        .awsize_o (aw_ch.awsize),
        .awburst_o (aw_ch.awburst),
        .awvalid_o (aw_ch.awvalid),
        .awready_i (aw_ch.awready),
    
        // AXI W channel
        .wid_o (w_ch.wid),
        .wdata_o (w_ch.wdata),
        .wstrb_o (w_ch.wstrb),
        .wlast_o (w_ch.wlast),
        .wvalid_o (w_ch.wvalid),
        .wready_i (w_ch.wready),
    
        // AXI B channel
        .bid_i (b_ch.bid),
        .bresp_i (b_ch.bresp),
        .bvalid_i (b_ch.bvalid),
        .bready_o (b_ch.bready),
    
        // AXI AR channel
        .arid_o (ar_ch.arid),
        .araddr_o (ar_ch.araddr),
        .arlen_o (ar_ch.arlen),
        .arsize_o (ar_ch.arsize),
        .arburst_o (ar_ch.arburst),
        .arvalid_o (ar_ch.arvalid),
        .arready_i (ar_ch.arready),
    
        // AXI R channel
        .rid_i (r_ch.rid),
        .rdata_i (r_ch.rdata),
        .rresp_i (r_ch.rresp),
        .rlast_i (r_ch.rlast),
        .rvalid_i (r_ch.rvalid),
        .rready_o (r_ch.rready)
    );
    

    endmodule <<EndOfFile:DMAC/SIM/TB/DMAC_TOP_TB.sv>>

    <<StartOfFile:DMAC/SIM/TB/filelist.f>> $LAB_PATH/SIM/TB/timescale.v $LAB_PATH/SIM/TB/AXI_INTF.sv $LAB_PATH/SIM/TB/AXI_SLAVE.sv $LAB_PATH/SIM/TB/DMAC_TOP_TB.sv <<EndOfFile:DMAC/SIM/TB/filelist.f>>

    <<StartOfFile:DMAC/SIM/TB/timescale.v>> `timescale 1ns/1ps <<EndOfFile:DMAC/SIM/TB/timescale.v>>

  2. Assistant

    From scanning through the provided files, several aspects of the design and its accompanying test bench capture attention. It's worth noting that the actual functionality of the RTL and the robustness of the test environment can vary widely depending on the underlying requirements, the specific behavior of components involved, and the context in which they are used. Without running and rigorously testing the code in a simulation tool, it's not possible to catch every potential issue, but here are some observations and points of interest:

    Code Structure and Modularity

    • The design is split into multiple components (DMAC_CFG, DMAC_ENGINE, DMAC_FIFO, and DMAC_TOP), which is good for modularity and code organization.
    • The test bench (DMAC_TOP_TB) is designed to interface with the top module of the DMAC, simulating APB and AXI interfaces and utilizing tasks to streamline the verification process.

    Design Specific Observations

    1. Reset Synchronicity: In various modules, asynchronous reset (rst_n) is used. Ensure that the rest of your design conforms to this choice to maintain consistency and prevent potential synchronization issues.
    2. State Machine Design: The DMAC_ENGINE module appears to have a state machine (with states like S_IDLE, S_RREQ, etc.), but the transition logic (always_comb) does not have actual implementation comments. For a functional module, you need to fill this in with real code.
    3. FIFO Module: The DMAC_FIFO module uses always_ff with non-blocking assignments for sequential logic, which is a good practice. However, the full and empty flags are calculated in an always_comb block, which is correct. Be cautious with combinational loops and ensure logic correctness for edge cases, like FIFO full and then an immediate read.
    4. Register and Wire Usage: The choice between reg and wire types seems appropriate. Notably, reg types are used for variables expected to hold values across clock cycles, and wire types for immediate assignments.
    5. Output Assignments: Continuous assignments (assign) are used effectively for output port assignments in the DMAC_CFG and other modules where output depends directly on internal logic state or input values.

    Testbench Specific Observations

    1. Clock and Reset Generation: The approach to provide a clock (clk) and reset (rst_n) within the test bench allows for controlled environment setup. A time-based halt using $finish after a timeout period is a good safety measure for simulation runs.
    2. Interface Usage: The test bench makes good use of interfaces (APB and AXI_XXX_CH) to abstract and simulate the protocol communication effectively.
    3. Test Cases: Through tasks such as test_init and test_dma, structured tests are applied. These demonstrate an understanding of crucial verification concepts, albeit the real robustness of the test cases hinges on their comprehensive coverage of the functionality.

    Potential Concerns or Improvements

    • Commented Code Sections: Some portions of the code (especially within the DMAC_ENGINE) are placeholders or commented out. For a fully functional design, these sections need implementing.
    • Error Handling in Testbench: While the test bench does check for timeouts and basic functionality, more nuanced error handling (checking for erroneous responses, illegal states, etc.) would enhance its reliability.
    • Simulation Directives: The use of simulation directives like $dumpvars for waveform generation and $display for logging is evident. However, ensure you remove or conditionally compile ($ifdef SYNTHESIS ...) any testbench-specific system tasks and functions in synthesis runs.

    In summary, while the overall structure and approach demonstrate good practice in digital design and testbench creation, the effectiveness heavily relies on the completeness of the implementation, specifically within certain blocks and state machines, and the thoroughness of the test scenarios covered.

  3. User

    한글로 번역해서 출력해줘.

  4. Assistant

    제공된 파일들을 검토한 결과, 디자인과 테스트 벤치의 여러 측면이 주목을 받습니다. 기본 요구 사항, 관련 컴포넌트의 구체적인 동작, 사용되는 맥락에 따라 실제 기능과 테스트 환경의 견고함은 크게 달라질 수 있습니다. 시뮬레이션 도구에서 코드를 실행하고 엄격하게 테스트하지 않는 한, 모든 잠재적 문제를 파악하는 것은 불가능하지만, 다음은 일부 관찰 사항과 관심 포인트입니다.

    코드 구조 및 모듈성

    • 디자인은 여러 컴포넌트(DMAC_CFG, DMAC_ENGINE, DMAC_FIFO, 그리고 DMAC_TOP)로 나누어져 있어 모듈성과 코드 조직에 좋습니다.
    • 테스트 벤치(DMAC_TOP_TB)는 DMAC의 탑 모듈과 인터페이스하기 위해 설계되었고, APB와 AXI 인터페이스를 시뮬레이션하며, 테스트 프로세스를 간소화하기 위해 task를 사용합니다.

    디자인 특정 관찰 사항

    1. 리셋 동기성: 여러 모듈에서 비동기 리셋(rst_n)을 사용합니다. 이러한 선택이 나머지 디자인과 일관되게 유지되도록 하여 동기화 문제를 방지해야 합니다.
    2. 상태 머신 디자인: DMAC_ENGINE 모듈은 상태 머신(예: S_IDLE, S_RREQ 등)을 가지고 있으나, 실제 구현 주석이 없는 전환 로직(always_comb)을 가지고 있습니다. 기능적인 모듈을 위해서는 이를 실제 코드로 채워야 합니다.
    3. FIFO 모듈: DMAC_FIFO 모듈은 순차 로직에 non-blocking 할당을 사용하는 always_ff를 사용하는데, 이는 좋은 관행입니다. 단, 완전 및 empty 플래그는 always_comb 블록에서 계산되는데, 이는 일반적으로 올바릅니다. 조합 루프에 주의하고 에지 케이스를 위한 로직의 정확성을 보장하세요.
    4. 레지스터 및 와이어 사용: reg와 wire 타입 사이의 선택이 적절해 보입니다. 일반적으로 reg 타입은 시계 사이클을 거쳐 값을 유지할 변수에, wire 타입은 즉시 할당에 사용됩니다.
    5. 출력 할당: DMAC_CFG 및 다른 모듈에서 출력 포트 할당에 대한 연속 할당(assign)이 효과적으로 사용됩니다. 출력이 내부 로직 상태나 입력 값에 직접 의존할 때 유용합니다.

    테스트벤치 특정 관찰 사항

    1. 클록 및 리셋 생성: 클록(clk)과 리셋(rst_n)을 테스트 벤치 내에서 제공하는 접근 방식은 제어 환경 설정에 좋습니다. 시뮬레이션 실행을 위한 안전 조치로 시간 기반 중단이 좋은 방법입니다.
    2. 인터페이스 사용: 테스트 벤치는 프로토콜 통신을 효과적으로 추상화하고 시뮬레이트하기 위해 인터페이스(APB 및 AXI_XXX_CH)를 잘 활용합니다.
    3. 테스트 케이스: test_init 및 test_dma와 같은 task를 통해 구조화된 테스트가 적용됩니다. 이러한 접근법은 기본적인 검증 개념을 잘 보여주지만, 테스트 케이스의 견고함은 기능의 전반적인 커버리지에 크게 의존합니다.

    잠재적 우려사항 또는 개선점

    • 주석 처리된 코드 섹션: 코드의 일부분(특히 DMAC_ENGINE 내부)은 자리 표시자이거나 주석 처리되어 있습니다. 완전히 기능하는 디자인을 위해서는 이러한 섹션에 실제 구현이 필요합니다.
    • 테스트벤치 내 오류 처리: 테스트 벤치는 타임아웃 및 기본 기능성을 확인하지만, 오류 응답, 불법 상태 등에 대한 더 미묘한 오류 처리가 검증의 신뢰성을 향상시킬 것입니다.
    • 시뮬레이션 지시문: 웨이브폼 생성을 위한 $dumpvars와 로깅을 위한 $display사용이 명시되어 있습니다. 그러나 합성 실행에서는 테스트벤치 특정 시스템 태스크 및 함수를 제거하거나 조건부 컴파일($ifdef SYNTHESIS ...)을 확실히 해야 합니다.

    요약하자면, 전체 구조와 접근 방식이 디지털 디자인 및 테스트벤치 생성에 대한 좋은 관행을 보여주지만, 특정 블록 및 상태 머신의 구현 완성도와 다루는 테스트 시나리오의 포괄성에 크게 의존합니다.

  5. User

    "아래 내용을 통해서 표가 올바른지 확인해주세요."를 영어로 영작해줘.

  6. Assistant

    "Please check if the table below is correct based on the provided information."

  7. User

    Please check if the table below is correct based on the provided information.

    +-------+-------------------------------------------------------------------------------------+------------+-----------------------------------------------------------+-----------------------------------------+ | State | Major outputs | Next State | Next state transition condition | Notes | | +---------+--------+---------------------------------------+-------------------+------+ | | | | | ARVALID | RREADY | AWVALID | WVALID | done | | | | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | IDLE | 0 | 0 | 0 | 0 | 1 | RREQ | (DMA_CMD.start is written as 1) and (DMA_LEN.byte_len!=0) | On moving out, | | | | | | | | | | - Copy DMA_SRC to SRC_ADDR. | | | | | | | | | | - Copy DMA_DST to DST_ADDR | | | | | | | | | | - Copy DMA_LEN to the internal counter | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | RREQ | 1 | 0 | 0 | 0 | 0 | RDATA | ARREADY=1 | On moving out, | | | | | | | | | | - Increment ARADDR by 4 | | | | | | | | | | | | | | | | | | | | ARLEN = (cnt>=64) ? 'hF : cnt[5:2]-4'h1 | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | RDATA | 0 | 1 | 0 | 0 | 0 | WREQ | (RVALID=1) & (RLAST) | Push data to FIFO | | | | | | | +------------+-----------------------------------------------------------+-----------------------------------------+ | | | | | | | RDATA | (RVALID) & (!RLAST) | Push data to FIFO | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | WREQ | 0 | 0 | 1 | 0 | 0 | WDATA | AWREADY=1 | On moving out, | | | | | AWLEN=(cnt>=64) ? 'hF : cnt[5:2]-4'h1 | | | | | - Increment AWADDR by 4 | | | | | | | | | | - Decrement the internal counter by 4 | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | WDATA | 0 | 0 | 0 | 1 | 0 | RREQ | (WREADY=1) (!WLAST) & (counter!=0) | Pop data from FIFO | | | | | | WLAST=(wcnt=='d0) | | | | Decrement wcnt | | | | | | | +------------+-----------------------------------------------------------+-----------------------------------------+ | | | | | | | IDLE | (WREADY=1) (WLAST) & (counter==0) | Pop data from FIFO | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+

    <StartOfFile:DMAC/RTL/DMAC_CFG.sv>> module DMAC_CFG ( input wire clk, input wire rst_n, // _n means active low

    // AMBA APB interface
    input wire psel_i,
    input wire penable_i,
    input wire [11:0] paddr_i,
    input wire pwrite_i,
    input wire [31:0] pwdata_i,
    output reg pready_o,
    output reg [31:0] prdata_o,
    output reg pslverr_o,
    
    // configuration registers
    output reg [31:0] src_addr_o,
    output reg [31:0] dst_addr_o,
    output reg [15:0] byte_len_o,
    output wire start_o,
    input wire done_i
    

    );

    // Configuration register to read/write
    reg [31:0] src_addr;
    reg [31:0] dst_addr;
    reg [15:0] byte_len;
    
    //----------------------------------------------------------
    // Write
    //----------------------------------------------------------
    // an APB write occurs when PSEL & PENABLE & PWRITE
    // clk : __--__--__--__--__--__--__--__--__--__--
    // psel : ___--------_____________________________
    // penable : _______----_____________________________
    // pwrite : ___--------_____________________________
    // wren : _______----_____________________________
    //
    // DMA start command must be asserted when APB writes 1 to the DMA_CMD
    // register
    // clk : __--__--__--__--__--__--__--__--__--__--
    // psel : ___--------_____________________________
    // penable : _______----_____________________________
    // pwrite : ___--------_____________________________
    // paddr : |DMA_CMD|
    // pwdata : | 1 |
    // start : _______----_____________________________
    
    wire wren = psel_i & penable_i & pwrite_i;
    
    always @(posedge clk) begin
        if (!rst_n) begin
            src_addr <= 32'd0;
            dst_addr <= 32'd0;
            byte_len <= 16'd0;
        end
        else if (wren) begin
            case (paddr_i)
                'h100: src_addr <= pwdata_i[31:0];
                'h104: dst_addr <= pwdata_i[31:0];
                'h108: byte_len <= pwdata_i[15:0];
            endcase
        end
    end
    wire start = wren & (paddr_i=='h10C) & pwdata_i[0];
    
    //----------------------------------------------------------
    // READ
    //----------------------------------------------------------
    // an APB read occurs when PSEL & PENABLE & !PWRITE
    // To make read data a direct output from register,
    // this code shall buffer the muxed read data into a register
    // in the SETUP cycle (PSEL & !PENABLE)
    // clk : __--__--__--__--__--__--__--__--__--__--
    // psel : ___--------_____________________________
    // penable : _______----_____________________________
    // pwrite : ________________________________________
    // reg update : ___----_________________________________
    // prdata : |DATA
    
    reg [31:0] rdata;
    
    always @(posedge clk) begin
        if (!rst_n) begin
            rdata <= 32'd0;
        end
        else if (psel_i & !penable_i & !pwrite_i) begin // in the setup cycle in the APB state diagram
            case (paddr_i)
                'h0: rdata <= 32'h0001_2024;
                'h100: rdata <= src_addr;
                'h104: rdata <= dst_addr;
                'h108: rdata <= {16'd0, byte_len};
                'h110: rdata <= {31'd0, done_i};
                default: rdata <= 32'd0;
            endcase
        end
    end
    
    // output assignments
    assign pready_o = 1'b1;
    assign prdata_o = rdata;
    assign pslverr_o = 1'b0;
    
    assign src_addr_o = src_addr;
    assign dst_addr_o = dst_addr;
    assign byte_len_o = byte_len;
    assign start_o = start;
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_CFG.sv>>

    <<StartOfFile:DMAC/RTL/DMAC_ENGINE.sv>> module DMAC_ENGINE ( input wire clk, input wire rst_n, // _n means active low

    // configuration registers
    input wire [31:0] src_addr_i,
    input wire [31:0] dst_addr_i,
    input wire [15:0] byte_len_i,
    input wire start_i,
    output wire done_o,
    
    // AMBA AXI interface (AW channel)
    output wire [3:0] awid_o,
    output wire [31:0] awaddr_o,
    output wire [3:0] awlen_o,
    output wire [2:0] awsize_o,
    output wire [1:0] awburst_o,
    output wire awvalid_o,
    input wire awready_i,
    
    // AMBA AXI interface (W channel)
    output wire [3:0] wid_o,
    output wire [31:0] wdata_o,
    output wire [3:0] wstrb_o,
    output wire wlast_o,
    output wire wvalid_o,
    input wire wready_i,
    
    // AMBA AXI interface (B channel)
    input wire [3:0] bid_i,
    input wire [1:0] bresp_i,
    input wire bvalid_i,
    output wire bready_o,
    
    // AMBA AXI interface (AR channel)
    output wire [3:0] arid_o,
    output wire [31:0] araddr_o,
    output wire [3:0] arlen_o,
    output wire [2:0] arsize_o,
    output wire [1:0] arburst_o,
    output wire arvalid_o,
    input wire arready_i,
    
    // AMBA AXI interface (R channel)
    input wire [3:0] rid_i,
    input wire [31:0] rdata_i,
    input wire [1:0] rresp_i,
    input wire rlast_i,
    input wire rvalid_i,
    output wire rready_o
    

    );

    // mnemonics for state values
    localparam S_IDLE = 3'd0,
                                S_RREQ = 3'd1,
                                S_RDATA = 3'd2,
                                S_WREQ = 3'd3,
                                S_WDATA = 3'd4;
    
    reg [2:0] state, state_n;
    
    reg [31:0] src_addr, src_addr_n;
    reg [31:0] dst_addr, dst_addr_n;
    reg [15:0] cnt, cnt_n;
    reg [3:0] wcnt, wcnt_n;
    
    reg arvalid,
                                rready,
                                awvalid,
                                wvalid,
                                wlast,
                                done;
    
    wire fifo_full,
                                fifo_empty;
    reg fifo_wren,
                                fifo_rden;
    wire [31:0] fifo_rdata;
    
    
    // it's desirable to code registers in a simple way
    always_ff @(posedge clk)
        if (!rst_n) begin
            state <= S_IDLE;
    
            src_addr <= 32'd0;
            dst_addr <= 32'd0;
            cnt <= 16'd0;
    
            wcnt <= 4'd0;
        end
        else begin
            state <= state_n;
    
            src_addr <= src_addr_n;
            dst_addr <= dst_addr_n;
            cnt <= cnt_n;
    
            wcnt <= wcnt_n;
        end
    
    
    // this block programs output values and next register values
    // based on states.
    always_comb begin
        // ********************** 
        // ********************** 
        // FILL YOUR CODE HERE
        // ********************** 
        // ********************** 
    end
    
    DMAC_FIFO u_fifo
    (
        .clk (clk),
        .rst_n (rst_n),
    
        .full_o (fifo_full),
        .wren_i (fifo_wren),
        .wdata_i (rdata_i),
    
        .empty_o (fifo_empty),
        .rden_i (fifo_rden),
        .rdata_o (fifo_rdata)
    );
    
    // Output assigments
    assign done_o = done;
    
    assign awid_o = 4'd0;
    assign awaddr_o = dst_addr;
    assign awlen_o = (cnt >= 'd64) ? 4'hF: cnt[5:2]-4'h1;
    assign awsize_o = 3'b010; // 4 bytes per transfer
    assign awburst_o = 2'b01; // incremental
    assign awvalid_o = awvalid;
    
    assign wid_o = 4'd0;
    assign wdata_o = fifo_rdata;
    assign wstrb_o = 4'b1111; // all bytes within 4 byte are valid
    assign wlast_o = wlast;
    assign wvalid_o = wvalid;
    
    assign bready_o = 1'b1;
    
    assign arvalid_o = arvalid;
    assign araddr_o = src_addr;
    assign arid_o = 4'd0;
    assign arlen_o = (cnt >= 'd64) ? 4'hF: cnt[5:2]-4'h1;
    assign arsize_o = 3'b010; // 4 bytes per transfer
    assign arburst_o = 2'b01; // incremental
    assign arvalid_o = arvalid;
    
    assign rready_o = rready & !fifo_full;
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_ENGINE.sv>>

    <<StartOfFile:DMAC/RTL/DMAC_FIFO.sv>> module DMAC_FIFO #( parameter DEPTH_LG2 = 4, parameter DATA_WIDTH = 32 ) ( input wire clk, input wire rst_n,

    output wire full_o,
    input wire wren_i,
    input wire [DATA_WIDTH-1:0] wdata_i,
    
    output wire empty_o,
    input wire rden_i,
    output wire [DATA_WIDTH-1:0] rdata_o
    

    );

    localparam FIFO_DEPTH = (1<<DEPTH_LG2);
    
    reg [DATA_WIDTH-1:0] data[FIFO_DEPTH];
    
    reg full, full_n,
                                        empty, empty_n;
    reg [DEPTH_LG2:0] wrptr, wrptr_n,
                                        rdptr, rdptr_n;
    
    // reset entries to all 0s
    always_ff @(posedge clk)
        if (!rst_n) begin
            full <= 1'b0;
            empty <= 1'b1; // empty after as reset
    
            wrptr <= {(DEPTH_LG2+1){1'b0}};
            rdptr <= {(DEPTH_LG2+1){1'b0}};
    
            for (int i=0; i<FIFO_DEPTH; i++) begin
                data[i] <= {DATA_WIDTH{1'b0}};
            end
        end
        else begin
            full <= full_n;
            empty <= empty_n;
    
            wrptr <= wrptr_n;
            rdptr <= rdptr_n;
    
            if (wren_i) begin
                data[wrptr[DEPTH_LG2-1:0]] <= wdata_i;
            end
        end
    
    always_comb begin
        wrptr_n = wrptr;
        rdptr_n = rdptr;
    
        if (wren_i) begin
            wrptr_n = wrptr + 'd1;
        end
    
        if (rden_i) begin
            rdptr_n = rdptr + 'd1;
        end
    
        empty_n = (wrptr_n == rdptr_n);
        full_n = (wrptr_n[DEPTH_LG2]!=rdptr_n[DEPTH_LG2])
                                     &(wrptr_n[DEPTH_LG2-1:0]==rdptr_n[DEPTH_LG2-1:0]);
    
    end
    
    // synthesis translate_off
    always @(posedge clk) begin
        if (full_o & wren_i) begin
            \$display("FIFO overflow");
            @(posedge clk);
            \$finish;
        end
    end
    
    always @(posedge clk) begin
        if (empty_o & rden_i) begin
            \$display("FIFO underflow");
            @(posedge clk);
            \$finish;
        end
    end
    // synthesis translate_on
    
    assign full_o = full;
    assign empty_o = empty;
    assign rdata_o = data[rdptr[DEPTH_LG2-1:0]];
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_FIFO.sv>>

    <<StartOfFile:DMAC/RTL/DMAC_TOP.sv>> module DMAC_TOP ( input wire clk, input wire rst_n, // _n means active low

    // AMBA APB interface
    input wire psel_i,
    input wire penable_i,
    input wire [11:0] paddr_i,
    input wire pwrite_i,
    input wire [31:0] pwdata_i,
    output reg pready_o,
    output reg [31:0] prdata_o,
    output reg pslverr_o,
    
    // AMBA AXI interface (AW channel)
    output wire [3:0] awid_o,
    output wire [31:0] awaddr_o,
    output wire [3:0] awlen_o,
    output wire [2:0] awsize_o,
    output wire [1:0] awburst_o,
    output wire awvalid_o,
    input wire awready_i,
    
    // AMBA AXI interface (AW channel)
    output wire [3:0] wid_o,
    output wire [31:0] wdata_o,
    output wire [3:0] wstrb_o,
    output wire wlast_o,
    output wire wvalid_o,
    input wire wready_i,
    
    // AMBA AXI interface (B channel)
    input wire [3:0] bid_i,
    input wire [1:0] bresp_i,
    input wire bvalid_i,
    output wire bready_o,
    
    // AMBA AXI interface (AR channel)
    output wire [3:0] arid_o,
    output wire [31:0] araddr_o,
    output wire [3:0] arlen_o,
    output wire [2:0] arsize_o,
    output wire [1:0] arburst_o,
    output wire arvalid_o,
    input wire arready_i,
    
    // AMBA AXI interface (R channel)
    input wire [3:0] rid_i,
    input wire [31:0] rdata_i,
    input wire [1:0] rresp_i,
    input wire rlast_i,
    input wire rvalid_i,
    output wire rready_o
    

    );

    wire [31:0] src_addr;
    wire [31:0] dst_addr;
    wire [15:0] byte_len;
    wire start;
    wire done;
    
    DMAC_CFG u_cfg(
        .clk (clk),
        .rst_n (rst_n),
    
        // AMBA APB interface
        .psel_i (psel_i),
        .penable_i (penable_i),
        .paddr_i (paddr_i),
        .pwrite_i (pwrite_i),
        .pwdata_i (pwdata_i),
        .pready_o (pready_o),
        .prdata_o (prdata_o),
        .pslverr_o (pslverr_o),
    
        .src_addr_o (src_addr),
        .dst_addr_o (dst_addr),
        .byte_len_o (byte_len),
        .start_o (start),
        .done_i (done)
    );
    
    DMAC_ENGINE u_engine(
        .clk (clk),
        .rst_n (rst_n),
    
        // configuration registers
        .src_addr_i (src_addr),
        .dst_addr_i (dst_addr),
        .byte_len_i (byte_len),
        .start_i (start),
        .done_o (done),
    
        // AMBA AXI interface (AW channel)
        .awid_o (awid_o),
        .awaddr_o (awaddr_o),
        .awlen_o (awlen_o),
        .awsize_o (awsize_o),
        .awburst_o (awburst_o),
        .awvalid_o (awvalid_o),
        .awready_i (awready_i),
    
        // AMBA AXI interface (W channel)
        .wid_o (wid_o),
        .wdata_o (wdata_o),
        .wstrb_o (wstrb_o),
        .wlast_o (wlast_o),
        .wvalid_o (wvalid_o),
        .wready_i (wready_i),
    
        // AMBA AXI interface (B channel)
        .bid_i (bid_i),
        .bresp_i (bresp_i),
        .bvalid_i (bvalid_i),
        .bready_o (bready_o),
    
        // AMBA AXI interface (AR channel)
        .arid_o (arid_o),
        .araddr_o (araddr_o),
        .arlen_o (arlen_o),
        .arsize_o (arsize_o),
        .arburst_o (arburst_o),
        .arvalid_o (arvalid_o),
        .arready_i (arready_i),
    
        // AMBA AXI interface (R channel)
        .rid_i (rid_i),
        .rdata_i (rdata_i),
        .rresp_i (rresp_i),
        .rlast_i (rlast_i),
        .rvalid_i (rvalid_i),
        .rready_o (rready_o)
    );
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_TOP.sv>>

    <<StartOfFile:DMAC/RTL/filelist.f>> -sverilog $LAB_PATH/RTL/DMAC_TOP.sv -sverilog $LAB_PATH/RTL/DMAC_CFG.sv -sverilog $LAB_PATH/RTL/DMAC_ENGINE.sv -sverilog $LAB_PATH/RTL/DMAC_FIFO.sv <<EndOfFile:DMAC/RTL/filelist.f>>

    <<StartOfFile:DMAC/SIM/TB/AXI_INTF.sv>> `include "../TB/AXI_TYPEDEF.svh"

    interface AXI_AW_CH #( parameter ADDR_WIDTH = AXI_ADDR_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic awvalid; logic awready; logic [ID_WIDTH-1:0] awid; logic [ADDR_WIDTH-1:0] awaddr; logic [3:0] awlen; logic [2:0] awsize; logic [1:0] awburst;

    endinterface

    interface AXI_W_CH #( parameter DATA_WIDTH = AXI_DATA_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic wvalid; logic wready; logic [ID_WIDTH-1:0] wid; logic [DATA_WIDTH-1:0] wdata; logic [DATA_WIDTH/8-1:0] wstrb; logic wlast;

    endinterface

    interface AXI_B_CH #( parameter ID_WIDTH = `AXI_ID_WIDTH ) ( input clk ); logic bvalid; logic bready; logic [ID_WIDTH-1:0] bid; logic [1:0] bresp;

    endinterface

    interface AXI_AR_CH #( parameter ADDR_WIDTH = AXI_ADDR_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic arvalid; logic arready; logic [ID_WIDTH-1:0] arid; logic [ADDR_WIDTH-1:0] araddr; logic [3:0] arlen; logic [2:0] arsize; logic [1:0] arburst;

    endinterface

    interface AXI_R_CH #( parameter DATA_WIDTH = AXI_DATA_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic rvalid; logic rready; logic [ID_WIDTH-1:0] rid; logic [DATA_WIDTH-1:0] rdata; logic [1:0] rresp; logic rlast;

    endinterface

    interface APB ( input clk ); logic psel; logic penable; logic [31:0] paddr; logic pwrite; logic [31:0] pwdata; logic pready; logic [31:0] prdata; logic pslverr;

    modport master (
        input clk,
        input pready, prdata, pslverr,
        output psel, penable, paddr, pwrite, pwdata
    );
    
    task init();
        psel = 1'b0;
        penable = 1'b0;
        paddr = 32'd0;
        pwrite = 1'b0;
        pwdata = 32'd0;
    endtask
    
    task write(input int addr,
               input int data);
        #1
        psel = 1'b1;
        penable = 1'b0;
        paddr = addr;
        pwrite = 1'b1;
        pwdata = data;
        @(posedge clk);
        #1
        penable = 1'b1;
        @(posedge clk);
    
        while (pready==1'b0) begin
            @(posedge clk);
        end
    
        psel = 1'b0;
        penable = 1'b0;
        paddr = 'hX;
        pwrite = 1'bx;
        pwdata = 'hX;
    endtask
    
    task read(input int addr,
              output int data);
        #1
        psel = 1'b1;
        penable = 1'b0;
        paddr = addr;
        pwrite = 1'b0;
        pwdata = 'hX;
        @(posedge clk);
        #1
        penable = 1'b1;
        @(posedge clk);
    
        while (pready==1'b0) begin
            @(posedge clk);
        end
        data = prdata;
    
        psel = 1'b0;
        penable = 1'b0;
        paddr = 'hX;
        pwrite = 1'bx;
        pwdata = 'hX;
    endtask
    

    endinterface <<EndOfFile:DMAC/SIM/TB/AXI_INTF.sv>>

    <<StartOfFile:DMAC/SIM/TB/AXI_SLAVE.sv>> `include "../TB/AXI_TYPEDEF.svh"

    module AXI_SLAVE #( parameter ADDR_WIDTH = 16, parameter DATA_WIDTH = AXI_DATA_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH, parameter AWREADY_DELAY = 1, parameter ARREADY_DELAY = 1, parameter AR2R_DELAY = 50 ) ( input wire clk, input wire rst_n, // _n means active low

    AXI_AW_CH aw_ch,
    AXI_W_CH w_ch,
    AXI_B_CH b_ch,
    AXI_AR_CH ar_ch,
    AXI_R_CH r_ch
    

    );

    localparam DATA_DEPTH = 1<<ADDR_WIDTH;
    
    logic [7:0] mem[DATA_DEPTH];
    
    function void write_byte(int addr, input bit [7:0] wdata);
        mem[addr] = wdata;
    endfunction
    
    function void write_word(int addr, input bit [31:0] wdata);
        for (int i=0; i<4; i++) begin
            write_byte(addr+i, wdata[8*i +: 8]); // [i*8+7:i*8]
        end
    endfunction
    
    function bit [7:0] read_byte(int addr);
        read_byte = mem[addr];
    endfunction
    
    function bit [31:0] read_word(int addr);
        for (int i=0; i<4; i++) begin
            read_word[8*i +: 8] = read_byte(addr+i);// [i*8+7:i*8]
        end
    endfunction
    
    //----------------------------------------------------------
    // write channels (AW, W, B)
    //----------------------------------------------------------
    localparam logic [1:0] S_W_IDLE = 0,
                                S_W_AWREADY = 1,
                                S_W_BURST = 2,
                                S_W_RESP = 3;
    
    logic [1:0] wstate, wstate_n;
    logic [7:0] wcnt, wcnt_n;
    
    logic [ADDR_WIDTH-1:0] waddr, waddr_n;
    logic [ID_WIDTH-1:0] wid, wid_n;
    logic [3:0] wlen, wlen_n;
    
    always_ff @(posedge clk)
        if (!rst_n) begin
            wstate <= S_W_IDLE;
    
            wcnt <= 8'd0;
            waddr <= {ADDR_WIDTH{1'b0}};
            wid <= {ID_WIDTH{1'b0}};
            wlen <= 4'd0;
        end
        else begin
            wstate <= wstate_n;
    
            wcnt <= wcnt_n;
            waddr <= waddr_n;
            wid <= wid_n;
            wlen <= wlen_n;
        end
    
    always @(*) begin
        wstate_n = wstate;
    
        wcnt_n = wcnt;
        waddr_n = waddr;
        wid_n = wid;
        wlen_n = wlen;
    
        aw_ch.awready = 1'b0;
        w_ch.wready = 1'b0;
        b_ch.bvalid = 1'b0;
    
        case (wstate)
            S_W_IDLE: begin
                if (aw_ch.awvalid) begin
                    if (AWREADY_DELAY == 0) begin
                        waddr_n = aw_ch.awaddr;
                        wid_n = aw_ch.awid;
                        wlen_n = aw_ch.awlen;
                        aw_ch.awready = 1'b1;
                        wstate_n = S_W_BURST;
                    end
                    else begin
                        wcnt_n = AWREADY_DELAY-1;
                        wstate_n = S_W_AWREADY;
                    end
                end
            end
            S_W_AWREADY: begin
                if (wcnt==0) begin
                    waddr_n = aw_ch.awaddr;
                    wid_n = aw_ch.awid;
                    wlen_n = aw_ch.awlen;
                    aw_ch.awready = 1'b1;
                    wstate_n = S_W_BURST;
                end
                else begin
                    wcnt_n = wcnt - 8'd1;
                end
            end
            S_W_BURST: begin
                w_ch.wready = 1'b1;
                if (w_ch.wvalid) begin
                    for (int i=0; i<DATA_WIDTH/8; i++) begin
                        write_byte(waddr + i, w_ch.wdata[i*8 +: 8]); // [i*8+7:i*8]
                    end
                    waddr_n = waddr + (DATA_WIDTH/8);
                    if (wlen==4'd0) begin
                        wstate_n = S_W_RESP;
                    end
                    else begin
                        wlen_n = wlen - 4'd1;
                    end
                end
            end
            S_W_RESP: begin
                b_ch.bvalid = 1'b1;
                if (b_ch.bready) begin
                    wstate_n = S_W_IDLE;
                end
            end
        endcase
    end
    
    //----------------------------------------------------------
    // read channel (AR, R)
    //----------------------------------------------------------
    localparam logic [1:0] S_R_IDLE = 0,
                                S_R_ARREADY = 1,
                                S_R_DELAY = 2,
                                S_R_BURST = 3;
    
    logic [1:0] rstate, rstate_n;
    logic [7:0] rcnt, rcnt_n;
    
    logic [ADDR_WIDTH-1:0] raddr, raddr_n;
    logic [ID_WIDTH-1:0] rid, rid_n;
    logic [3:0] rlen, rlen_n;
    
    always_ff @(posedge clk)
        if (!rst_n) begin
            rstate <= S_R_IDLE;
    
            rcnt <= 8'd0;
            raddr <= {ADDR_WIDTH{1'b0}};
            rid <= {ID_WIDTH{1'b0}};
            rlen <= 4'd0;
        end
        else begin
            rstate <= rstate_n;
    
            rcnt <= rcnt_n;
            raddr <= raddr_n;
            rid <= rid_n;
            rlen <= rlen_n;
        end
    
    always_comb begin
        rstate_n = rstate;
    
        rcnt_n = rcnt;
        raddr_n = raddr;
        rid_n = rid;
        rlen_n = rlen;
    
        ar_ch.arready = 1'b0;
        r_ch.rvalid = 1'b0;
        r_ch.rlast = 1'b0;
    
        case (rstate)
            S_R_IDLE: begin
                if (ar_ch.arvalid) begin
                    if (ARREADY_DELAY == 0) begin
                        raddr_n = ar_ch.araddr;
                        rid_n = ar_ch.arid;
                        rlen_n = ar_ch.arlen;
                        ar_ch.arready = 1'b1;
    
                        rcnt_n = AR2R_DELAY - 1;
                        rstate_n = S_R_DELAY;
                    end
                    else begin
                        rcnt_n = ARREADY_DELAY-1;
                        rstate_n = S_R_ARREADY;
                    end
                end
            end
            S_R_ARREADY: begin
                if (rcnt==0) begin
                    raddr_n = ar_ch.araddr;
                    rid_n = ar_ch.arid;
                    rlen_n = ar_ch.arlen;
                    ar_ch.arready = 1'b1;
    
                    rcnt_n = AR2R_DELAY - 1;
                    rstate_n = S_R_DELAY;
                end
                else begin
                    rcnt_n = rcnt - 8'd1;
                end
            end
            S_R_DELAY: begin
                if (rcnt==0) begin
                    rstate_n = S_R_BURST;
                end
                else begin
                    rcnt_n = rcnt - 8'd1;
                end
            end
            S_R_BURST: begin
                r_ch.rvalid = 1'b1;
                r_ch.rlast = (rlen==4'd0);
                for (int i=0; i<DATA_WIDTH/8; i++) begin
                    r_ch.rdata[i*8 +: 8] = read_byte(raddr + i); // [i*8+7:i*8]
                end
                if (r_ch.rready) begin
                    raddr_n = raddr + (DATA_WIDTH/8);
                    if (rlen==4'd0) begin
                        rstate_n = S_R_IDLE;
                    end
                    else begin
                        rlen_n = rlen - 4'd1;
                    end
                end
            end
        endcase
    end
    
    // output assignments
    assign b_ch.bid = wid;
    assign b_ch.bresp = 2'd0;
    assign r_ch.rid = rid;
    assign r_ch.rresp = 2'd0;
    

    endmodule <<EndOfFile:DMAC/SIM/TB/AXI_SLAVE.sv>>

    <<StartOfFile:DMAC/SIM/TB/AXI_TYPEDEF.svh>> ifndef __AXI_TYPEDEF_SVH__ define AXI_TYPEDEF_SVH

    define AXI_ADDR_WIDTH 32 define AXI_DATA_WIDTH 32 `define AXI_ID_WIDTH 4

    `endif /* AXI_TYPEDEF_SVH */ <<EndOfFile:DMAC/SIM/TB/AXI_TYPEDEF.svh>>

    <<StartOfFile:DMAC/SIM/TB/DMAC_TOP_TB.sv>> define IP_VER 32'h000 define SRC_ADDR 32'h100 define DST_ADDR 32'h104 define LEN_ADDR 32'h108 define STAT_ADDR 32'h110 define START_ADDR 32'h10c

    `define TIMEOUT_CYCLE 999999 module DMAC_TOP_TB ();

    reg clk;
    reg rst_n;
    
    // clock generation
    initial begin
        clk = 1'b0;
    
        forever #10 clk = !clk;
    end
    
    // reset generation
    initial begin
        rst_n = 1'b0; // active at time 0
    
        repeat (3) @(posedge clk); // after 3 cycles,
        rst_n = 1'b1; // release the reset
    end
    
    // enable waveform dump
    initial begin
        \$dumpvars(0, u_DUT);
        \$dumpfile("dump.vcd");
    end
    

    // timeout initial begin #`TIMEOUT_CYCLE $display("Timeout!"); $finish; end

    APB apb_if (.clk(clk));
    
    AXI_AW_CH aw_ch (.clk(clk));
    AXI_W_CH w_ch (.clk(clk));
    AXI_B_CH b_ch (.clk(clk));
    AXI_AR_CH ar_ch (.clk(clk));
    AXI_R_CH r_ch (.clk(clk));
    
    task test_init();
        int data;
        apb_if.init();
    
        @(posedge rst_n); // wait for a release of the reset
        repeat (10) @(posedge clk); // wait another 10 cycles
    
        apb_if.read(`IP_VER, data);
        \$display("---------------------------------------------------");
        \$display("IP version: %x", data);
        \$display("---------------------------------------------------");
    
        \$display("---------------------------------------------------");
        \$display("Reset value test");
        \$display("---------------------------------------------------");
        apb_if.read(`SRC_ADDR, data);
        if (data===0)
            \$display("DMA_SRC(pass): %x", data);
        else begin
            \$display("DMA_SRC(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.read(`DST_ADDR, data);
        if (data===0)
            \$display("DMA_DST(pass): %x", data);
        else begin
            \$display("DMA_DST(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.read(`LEN_ADDR, data);
        if (data===0)
            \$display("DMA_LEN(pass): %x", data);
        else begin
            \$display("DMA_LEN(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.read(`STAT_ADDR, data);
        if (data===1)
            \$display("DMA_STATUS(pass): %x", data);
        else begin
            \$display("DMA_STATUS(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
    endtask
    
    task test_dma(input int src, input int dst, input int len);
        int data;
        int word;
        realtime elapsed_time;
    
        \$display("---------------------------------------------------");
        \$display("Load data to memory");
        \$display("---------------------------------------------------");
        for (int i=src; i<(src+len); i=i+4) begin
            word = \$random; 
            u_mem.write_word(i, word);
        end
    
        \$display("---------------------------------------------------");
        \$display("Configuration test");
        \$display("---------------------------------------------------");
        apb_if.write(`SRC_ADDR, src);
        apb_if.read(`SRC_ADDR, data);
        if (data===src)
            \$display("DMA_SRC(pass): %x", data);
        else begin
            \$display("DMA_SRC(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.write(`DST_ADDR, dst);
        apb_if.read(`DST_ADDR, data);
        if (data===dst)
            \$display("DMA_DST(pass): %x", data);
        else begin
            \$display("DMA_DST(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.write(`LEN_ADDR, len);
        apb_if.read(`LEN_ADDR, data);
        if (data===len)
            \$display("DMA_LEN(pass): %x", data);
        else begin
            \$display("DMA_LEN(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
    
        \$display("---------------------------------------------------");
        \$display("DMA start");
        \$display("---------------------------------------------------");
        apb_if.write(`START_ADDR, 32'h1);
        elapsed_time = \$realtime;
    
        \$display("---------------------------------------------------");
        \$display("Wait for a DMA completion");
        \$display("---------------------------------------------------");
        data = 0;
        while (data!=1) begin
            apb_if.read(`STAT_ADDR, data);
            repeat (100) @(posedge clk);
        end
        @(posedge clk);
        elapsed_time = \$realtime - elapsed_time;
        \$timeformat(-9, 0, " ns", 10);
        \$display("Elapsed time for DMA: %t", elapsed_time);
    
        \$display("---------------------------------------------------");
        \$display("DMA completed");
        \$display("---------------------------------------------------");
    
        repeat (len) @(posedge clk); // to make sure data is written
    
        \$display("---------------------------------------------------");
        \$display("verify data");
        \$display("---------------------------------------------------");
        for (int i=0; i<len; i=i+4) begin
            logic [31:0] src_word;
            logic [31:0] dst_word;
            src_word = u_mem.read_word(src+i);
            dst_word = u_mem.read_word(dst+i);
            if (src_word!==dst_word) begin
                \$display("Mismatch! (src:%x @%x, dst:%x @%x", src_word, src+i, dst_word, dst+i);
            end
        end
    endtask
    
    int src,
                    dst,
                    len;
    
    // main
    initial begin
        test_init();
    
        src = 'h0000_1000;
        dst = 'h0000_2000;
        len = 'h0100;
        \$display("===================================================");
        \$display("= 1st trial"); 
        \$display("= Copying %x bytes from %x to %x", len, src, dst);
        \$display("===================================================");
        test_dma(src, dst, len);
    
        src = 'h1234_1234;
        dst = 'hABCD_ABCC;
        len = 'h0F00;
        \$display("===================================================");
        \$display("= 2nd trial (long transfer)");
        \$display("= Copying %x bytes from %x to %x", len, src, dst);
        \$display("===================================================");
        test_dma(src, dst, len);
    
        src = 'h4278_0000;
        dst = 'h4278_1000;
        len = 'h0F10;
        \$display("===================================================");
        \$display("= 3rd trial (long transfer-2)");
        \$display("= Copying %x bytes from %x to %x", len, src, dst);
        \$display("===================================================");
        test_dma(src, dst, len);
        \$finish;
    end
    
    
    AXI_SLAVE u_mem (
        .clk (clk),
        .rst_n (rst_n),
    
        .aw_ch (aw_ch),
        .w_ch (w_ch),
        .b_ch (b_ch),
        .ar_ch (ar_ch),
        .r_ch (r_ch)
    );
    
    DMAC_TOP u_DUT (
        .clk (clk),
        .rst_n (rst_n),
    
        // APB interface
        .psel_i (apb_if.psel),
        .penable_i (apb_if.penable),
        .paddr_i (apb_if.paddr[11:0]),
        .pwrite_i (apb_if.pwrite),
        .pwdata_i (apb_if.pwdata),
        .pready_o (apb_if.pready),
        .prdata_o (apb_if.prdata),
        .pslverr_o (apb_if.pslverr),
    
        // AXI AW channel
        .awid_o (aw_ch.awid),
        .awaddr_o (aw_ch.awaddr),
        .awlen_o (aw_ch.awlen),
        .awsize_o (aw_ch.awsize),
        .awburst_o (aw_ch.awburst),
        .awvalid_o (aw_ch.awvalid),
        .awready_i (aw_ch.awready),
    
        // AXI W channel
        .wid_o (w_ch.wid),
        .wdata_o (w_ch.wdata),
        .wstrb_o (w_ch.wstrb),
        .wlast_o (w_ch.wlast),
        .wvalid_o (w_ch.wvalid),
        .wready_i (w_ch.wready),
    
        // AXI B channel
        .bid_i (b_ch.bid),
        .bresp_i (b_ch.bresp),
        .bvalid_i (b_ch.bvalid),
        .bready_o (b_ch.bready),
    
        // AXI AR channel
        .arid_o (ar_ch.arid),
        .araddr_o (ar_ch.araddr),
        .arlen_o (ar_ch.arlen),
        .arsize_o (ar_ch.arsize),
        .arburst_o (ar_ch.arburst),
        .arvalid_o (ar_ch.arvalid),
        .arready_i (ar_ch.arready),
    
        // AXI R channel
        .rid_i (r_ch.rid),
        .rdata_i (r_ch.rdata),
        .rresp_i (r_ch.rresp),
        .rlast_i (r_ch.rlast),
        .rvalid_i (r_ch.rvalid),
        .rready_o (r_ch.rready)
    );
    

    endmodule <<EndOfFile:DMAC/SIM/TB/DMAC_TOP_TB.sv>>

    <<StartOfFile:DMAC/SIM/TB/filelist.f>> $LAB_PATH/SIM/TB/timescale.v $LAB_PATH/SIM/TB/AXI_INTF.sv $LAB_PATH/SIM/TB/AXI_SLAVE.sv $LAB_PATH/SIM/TB/DMAC_TOP_TB.sv <<EndOfFile:DMAC/SIM/TB/filelist.f>>

    <<StartOfFile:DMAC/SIM/TB/timescale.v>> `timescale 1ns/1ps <<EndOfFile:DMAC/SIM/TB/timescale.v>>

    <<StartOfFile:DMAC/SIM/run.compile>> #!/bin/bash

    source ../../scripts/common.sh

    export LAB_PATH="$PWD/../" FILELIST_TB="../TB/filelist.f" FILELIST_RTL="../../RTL/filelist.f"

    echo "Cleaning up the old directory" rm -rf $RUN_DIR echo "Creating a new directory" mkdir -p $RUN_DIR cd $RUN_DIR echo "Compiling" $COMPILE_CMD $COMPILE_OPTIONS -f $FILELIST_TB -f $FILELIST_RTL <<EndOfFile:DMAC/SIM/run.compile>>

    <<StartOfFile:DMAC/SIM/run.compile4cov>> #!/bin/bash

    source ../../scripts/common.sh

    export LAB_PATH="$PWD/../" COV_DIR="$PWD/VDB" COV_OPTIONS="-cm line+cond+fsm+tgl+branch -cm_dir $COV_DIR" FILELIST_TB="../TB/filelist.f" FILELIST_RTL="../../RTL/filelist.f"

    echo "Cleaning up the old directory" rm -rf $RUN_DIR echo "Creating a new directory" mkdir -p $RUN_DIR cd $RUN_DIR echo "Compiling" $COMPILE_CMD $COMPILE_OPTIONS $COV_OPTIONS -f $FILELIST_TB -f $FILELIST_RTL <<EndOfFile:DMAC/SIM/run.compile4cov>>

    <<StartOfFile:DMAC/SIM/run.sim>> #!/bin/bash

    source ../../scripts/common.sh

    if [ -e $RUN_DIR/simv ]; then cd $RUN_DIR ./simv else echo "Compile file does not exist" exit 1 fi <<EndOfFile:DMAC/SIM/run.sim>>

    <<StartOfFile:DMAC/SIM/run.sim4cov>> #!/bin/bash

    source ../../scripts/common.sh

    COV_DIR="$PWD/VDB" COV_OPTIONS="-cm line+cond+fsm+tgl+branch -cm_dir $COV_DIR"

    if [ -e $RUN_DIR/simv ]; then cd $RUN_DIR ./simv $COV_OPTIONS else echo "Compile file does not exist" exit 1 fi <<EndOfFile:DMAC/SIM/run.sim4cov>>

    <<StartOfFile:DMAC/SIM/run.verdi>> #!/bin/bash

    source ../../scripts/common.sh

    export LAB_PATH="$PWD/../" FILELIST_TB="../TB/filelist.f" FILELIST_RTL="../../RTL/filelist.f"

    cd $RUN_DIR $VERDI_CMD $VERDI_OPTIONS -f $FILELIST_TB -f $FILELIST_RTL <<EndOfFile:DMAC/SIM/run.verdi>>

    <<StartOfFile:DMAC/SIM/run.verdi4cov>> verdi -cov -covdir VDB.vdb/ <<EndOfFile:DMAC/SIM/run.verdi4cov>>

    <<StartOfFile:DMAC/SIM/run.waveform>> #!/bin/bash

    source ../../scripts/common.sh

    if [ -e $RUN_DIR/dump.vcd ]; then cd $RUN_DIR $WAVE_CMD $WAVE_OPTIONS -i dump.vcd else echo "Dump file does not exist" exit 1 fi <<EndOfFile:DMAC/SIM/run.waveform>>

    ///

    Direct Memory Access (DMA) Controller Design Document V1.0

    1 Overview This document specifies the design and implementation of a Direct Memory Access Controller (DMAC) as a part of System-on-a-Chip (SoC). The main purpose of this DMAC design is to integrate into SoC for exchange a large volume of data between memory and peripherals at high speed. The proposed DMAC works on ARM’s Advanced Microcontroller Bus Architecture (AMBA) specification. The DMAC provides an AMBA APB interface to configure the IP, and an AMBA AXI interface to transfer data.

    2 Architecture Specification 2.1 General Description Some applications require transferring a volume of data between memory and peripherals without any modification on data. In software, it is commonly served by executing the memcpy library function in C, C++ or other languages. In C, the function has the following interface and copies len bytes from the object pointed by src to the object pointed by dst: void* memcpy(void* dst, const void* src, size_t len). While a pure software-based implementation of memcpy transfers data using CPU instructions, DMA does not use expensive CPU cycles but uses a hardware engine (DMAC) for the transfer. This can significantly speed up data transfers and allows using CPU for other jobs. 2.2 Usage Constraints Below describe constraints in utilizing DMAC v1. -The src and dst addresses are physical addresses. -The src and dst addresses must be a multiple of 4. -The len must be a multiple of 4. -The maximum len is 0xFFFF -Source and destination ranges must not overlap. 2.3 Programming Model Software can use the following sequence to transfer data using DMAC. -1.Write the source address to DMA_SRC register -2.Write the destination address to DMA_DST register -3.Write length to DMA_LEN register -4.Write 1 to bit[0] of DMA_CMD register -5.Wait until DMA_STATUS register has bit[0] as 1.

    2.4 Register Map In order to control DMAC, software can configure the following registers. +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+ | Offset | Reg Name | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+ | 0x00 | DMA_VER | version | +--------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ | 0x04~0xFC | Reserved | +--------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ | 0x100 | DMA_SRC | start_addr | +--------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ | 0x104 | DMA_DST | start_addr | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------------------------------------------------------------------------+ | 0x108 | DMA_LEN | | | | | | | | | | | | | | | | | byte_len | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+ | 0x10C | DMA_CMD | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | start | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+ | 0x110 | DMA_STATUS | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | done | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+

    2.4.1 DMA VERSION

    Field nameBit rangeR/WReset valueDesciption
    version[31:0]R0x0001_2024The version of this DMA controller. The upper 16 bits represent the major version. The lower 16 bits represent the released year of the version. This document describes behaviors of major version 1.

    2.4.2 DMA_SRC

    Field nameBit rangeR/WReset valueDesciption
    start_addr[31:0]R/W0x0000_0000start address of the source range.

    2.4.3 DMA_DST

    Field nameBit rangeR/WReset valueDesciption
    start_addr[31:0]R/W0x0000_0000start address of the destination range.

    2.4.4 DMA_LEN

    Field nameBit rangeR/WReset valueDesciption
    byte_len[15:0]R/W0x0000Number of bytes to be transferred from the source to the destination.

    2.4.5 DMA_CMD Field

    Field nameBit rangeR/WReset valueDesciption
    start[0]WN/AWriting 1 to this field will initiate a DMA transfer based on DMA_SRC, DMA_DST, and DMA_LEN registers. Software must not write 1 when there’s an on-going transfer. Writing 0 to this field does not affect operation

    2.4.6 DMA_STATUS

    Field nameBit rangeR/WReset valueDesciption
    done[0]R1This field is 1 when there’s no on-going DMA transfer. Software must wait this field to be 1 for a completion of a transfer. Software must not initiate a DMA transfer when this field is 0.

    3 Micro-architecture v1.1 Specification This section describes microarchitecture of a simple DMAC. It reads data from memory, buffers the data, and write the data into memory. It repeats this procedure until it completes transferring the specified number of bytes. For simplicity, it read/writes one-cycle data (4 bytes) at a time (in other words, burst-1 transfers). For simplicity, this microarchitecture does not consider write responses from the AXI interface. Later versions will support burst transfers and write responses.

    3.1 External Interface DMAC v1.1 has the following external interfaces to communicate with other hardware IPs. -AMBA APB interface for configuration -AMBA AXI interface for data transfer

    The image you’ve uploaded is a diagram showing the on-chip interconnect of a computer system. Here’s a detailed description: The diagram illustrates how the CPU core, memory, and DMAC (Direct Memory Access Controller) are connected through an on-chip interconnect. The connections also include specific interfaces like Config interface (APB) and Data interface (AXI). “CPU core” is a box on the left side connected to the central “On-chip interconnect” cloud shape with a bidirectional arrow. Below the “CPU core,” there’s another box labeled “Memory,” also connected to the “On-chip interconnect” with a bidirectional arrow. On the right side, there’s a box labeled “DMAC” connected to both “Config interface (APB)” and “Data interface (AXI)” which are in turn connected to the central “On-chip interconnect” with bidirectional arrows. The arrows indicate that data can flow in both directions between these components.

    3.2 Block Diagram DMAC v1.1 has the following blocks inside.

    The diagram is divided into three main blocks labeled “DMAC_TOP,” “DMAC_CFG,” and “DMAC_ENGINE.” “clk” and “rst” are inputs to the “DMAC_TOP” block. An arrow labeled “APB” connects the “DMAC_TOP” block to the “DMAC_CFG” block. Another arrow labeled “AXI” connects both the “DMAC_TOP” and “DMAC_CFG” blocks to the “DMAC_ENGINE” block. Inside the “DMAC_ENGINE” block, there are four internal components labeled as follows: SRC_ADDR DST_ADDR CNT DATA BUF There’s also a small circular graph with nodes labeled 0 to 3 inside this block. This diagram is likely used to illustrate the flow of data or control signals between these components in a Direct Memory Access Controller configuration. Please let me know if you need more information!

    3.3 Configuration Register (lab2) This block receives read/write requests from the APB and configures the registers describes in Section 2.4.

    3.4 Finite State Machine (lab3) DMA engine utilizes the following state machine to control operations.

    The diagram contains five blue circles representing different states: IDLE, RREQ, RDATA, WREQ, and WDATA. Arrows connect these circles indicating the flow from one state to another. Each arrow has text annotations that describe the conditions for transitioning from one state to another. For example, transitioning from IDLE to RREQ requires writing 1 to DMA_CMD & LEN!=0, and copying DMA_SRC/DST/LEN. There are also annotations on the state circles themselves, such as “done=1” on IDLE and “AWVALID=1” on WDATA.

    +-------+--------------------------------------------+------------+-----------------------------------------------------------+----------------------------------------+ | State | Major outputs | Next State | Next state transition condition | Notes | | +---------+--------+---------+--------+------+ | | | | | ARVALID | RREADY | AWVALID | WVALID | done | | | | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | IDLE | 0 | 0 | 0 | 0 | 1 | RREQ | (DMA_CMD.start is written as 1) and (DMA_LEN.byte_len!=0) | On moving out, | | | | | | | | | | - Copy DMA_SRC to SRC_ADDR. | | | | | | | | | | - Copy DMA_DST to DST_ADDR | | | | | | | | | | - Copy DMA_LEN to the internal counter | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | RREQ | 1 | 0 | 0 | 0 | 0 | RDATA | ARREADY=1 | On moving out, | | | | | | | | | | - Increment ARADDR by 4 | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | RDATA | 0 | 1 | 0 | 0 | 0 | WREQ | RVALID=1 | On moving out, | | | | | | | | | | - Buffer RDATA into the data buffer | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | WREQ | 0 | 0 | 1 | 0 | 0 | WDATA | AWREADY=1 | On moving out, | | | | | | | | | | - Increment AWADDR by 4 | | | | | | | | | | - Decrement the internal counter by 4 | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | WDATA | 0 | 0 | 0 | 1 | 0 | RREQ | (WREADY=1) & (counter!=0) | | | | | | | | +------------+-----------------------------------------------------------+----------------------------------------+ | | | | | | | IDLE | (WREADY=1) & (counter==0) | | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+

    { "signal": [ { "name": "clk", "wave": "p....|.........." }, { "name": "state", "wave": "2.3.4|..5.6.2...", "data": ["IDLE", "RREQ", "RDATA", "WREQ", "WDATA", "IDLE"] }, { "name": "write to CMD", "wave": "010..|..........", "data": ["1"] }, {}, [ "AR ch", { "name": "ARVALID(out)", "wave": "0.1.0|..........", "data": ["SRC"] }, { "name": "ARADDR(out)", "wave": "x.3.x|..........", "data": ["SRC"] }, { "name": "ARLEN(out)", "wave": "2....|..........", "data": ["0"] }, { "name": "ARREADY(in)", "wave": "0..10|.........." }, ], [ "R ch", { "name": "RREADY(out)", "wave": "0...1|..0......." }, { "name": "RVALID(in)", "wave": "0....|.10......." }, { "name": "RDATA(in)", "wave": "x....|.4x.......", "data": ["DATA"] }, ], [ "AW ch", { "name": "AWVALID(out)", "wave": "0....|..1.0....." }, { "name": "AWADDR(out)", "wave": "x....|..5.x.....", "data": ["DST"] }, { "name": "AWLEN(out)", "wave": "2....|..........", "data": ["0"] }, { "name": "AWREADY(in)", "wave": "0....|...10....." }, ], [ "W ch", { "name": "WVALID(out)", "wave": "0....|....1.0..." }, { "name": "WDATA(out)", "wave": "x....|....4.x...", "data": ["DATA"] }, { "name": "WREADY(in)", "wave": "0....|.....10..." } ] ], "head" : { "tick" : "0" }, "foot" : { "tick" : "0" } } 그림 1. DMA operation with microarchitecture v1.1

    4 Micro-architecture v1.2 Specification (lab4) A problem with microarchitecture v1.1 is that it reads/writes data one-by-one. As memory read takes some time, DMAC v1.1 will suffer from poor performance with a long memory read latency (그림 2). We will improve the microarchitecture to transfer a burst of data to minimize performance degradation.

    { "signal": [ { "name": "clk", "wave": "p....|.................." }, { "name": "state", "wave": "2.3.4|..5.6.3.4|..5.6.3.", "data": ["IDLE", "RREQ", "RDATA", "WREQ", "WDATA", "RREQ", "RDATA", "WREQ", "WDATA", "RREQ"] }, { "name": "write to CMD", "wave": "010..|.........|........", "data": ["1"] }, {}, [ "AR ch", { "name": "ARVALID(out)", "wave": "0.1.0|......1.0|......1.", "data": ["SRC"] }, { "name": "ARADDR(out)", "wave": "x.3.x|......3.x|......3.", "data": ["SRC", "SRC+4", "SRC+8"] }, { "name": "ARLEN(out)", "wave": "2....|.........|........", "data": ["0"] }, { "name": "ARREADY(in)", "wave": "0..10|.......10|.......1" }, ], [ "R ch", { "name": "RREADY(out)", "wave": "0...1|..0.....1|..0....." }, { "name": "RVALID(in)", "wave": "0....|.10......|.10....." }, { "name": "RDATA(in)", "wave": "x....|.4x......|.4x.....", "data": ["DATA", "DATA"] }, ], [ "AW ch", { "name": "AWVALID(out)", "wave": "0....|..1.0....|..1.0..." }, { "name": "AWADDR(out)", "wave": "x....|..5.x....|..5.x...", "data": ["DST", "DST+4"] }, { "name": "AWLEN(out)", "wave": "2....|.........|........", "data": ["0"] }, { "name": "AWREADY(in)", "wave": "0....|...10....|...10..." }, ], [ "W ch", { "name": "WVALID(out)", "wave": "0....|....1.0..|....1.0." }, { "name": "WDATA(out)", "wave": "x....|....4.x..|....4.x.", "data": ["DATA", "DATA"] }, { "name": "WREADY(in)", "wave": "0....|.....10..|.....10." } ] ], "head" : { "tick" : "0" }, "foot" : { "tick" : "0" } } 그림 2. DMA operation with microarchitecture 1.1. At a time, it transfers single burst of data In Microarchitecture version 2, DMAC transfers up to 16 cycles of data with a single access. This can significantly reduce execution time by transferring data in bursts (그림 3).

    { "signal": [ { "name": "clk", "wave": "p....|....|.......|....." }, { "name": "state", "wave": "2.3.4|....|..5.6..|..3.4", "data": ["IDLE", "RREQ", "RDATA", "WREQ", "WDATA", "RREQ"] }, { "name": "write to CMD", "wave": "010..|....|.......|.....", "data": ["1"] }, {}, [ "AR ch", { "name": "ARVALID(out)", "wave": "0.1.0|....|.......|.....", "data": ["SRC"] }, { "name": "ARADDR(out)", "wave": "x.3.x|....|.......|.....", "data": ["SRC"] }, { "name": "ARLEN(out)", "wave": "2....|....|.......|.....", "data": ["15"] }, { "name": "ARREADY(in)", "wave": "0..10|....|.......|....." }, ], [ "R ch", { "name": "RREADY(out)", "wave": "0...1|....|..0....|....." }, { "name": "RVALID(in)", "wave": "0....|.1..|..0....|....." }, { "name": "RLAST(in)", "wave": "0....|....|.10....|....." }, { "name": "RDATA(in)", "wave": "x....|.444|44x....|.....", "data": ["D0", "D1", "D2", "D14", "D15"] }, ], [ "AW ch", { "name": "AWVALID(out)", "wave": "0....|....|..1.0..|....." }, { "name": "AWADDR(out)", "wave": "x....|....|..5.x..|.....", "data": ["DST"] }, { "name": "AWLEN(out)", "wave": "2....|....|.......|.....", "data": ["15"] }, { "name": "AWREADY(in)", "wave": "0....|....|...10..|....." }, ], [ "W ch", { "name": "WVALID(out)", "wave": "0....|....|....1..|..0.." }, { "name": "WDATA(out)", "wave": "x....|....|....444|44x..", "data": ["D0", "D1", "D2", "D14", "D15"] }, { "name": "WLAST(out)", "wave": "0....|....|.......|.10.." }, { "name": "WREADY(in)", "wave": "0....|....|....1..|..0.." } ] ], "head" : { "tick" : "0" }, "foot" : { "tick" : "0" } } 그림 3. DMA operation with burst transfers. At a time, a request reads/writes 16 cycles of data.

  8. Assistant

    Based on the provided information, the table seems to accurately capture the state machine logic for the DMA operation as described in the design documentation. The table depicts various states (IDLE, RREQ, RDATA, WREQ, WDATA), major outputs for each state, conditions for transitioning to the next state, and additional notes that provide insights into the operation and behavior within each state.

    Given the extracted information from the design document and RTL code snippets concerning the DMAC's operation, including how it interfaces with memory through the AXI bus for data transfers and the APB interface for configurations, the table lays down a concise summary of the essential operational flow of the DMAC.

    Here are several key validations:

    1. State Transitions: The conditions described for moving between states (e.g., from IDLE to RREQ upon a DMA start command and having a nonzero byte length) align with typical DMA operations designed to handle bulk data transfers with minimal CPU intervention.
    2. Functionality Within States: The activities within each state, such as incrementing address pointers (ARADDR, AWADDR) and managing the internal data buffer (e.g., pushing data to FIFO in RDATA and popping in WDATA), match the expected functional behavior described in the documentation for conducting read or write operations.
    3. Output Signals: The depiction of major output signals (e.g., ARVALID, RREADY, AWVALID, WVALID) and their condition based on the state, like asserting ARVALID in RREQ to request a read operation from the source address, is consistent with AXI protocol-based data transfer mechanisms.
    4. Notes/Descriptions: The additional notes provide clarifying context that supports understanding the operational logic, such as copying the DMA source, destination, and length registers upon leaving IDLE, which is a necessary setup action before data transfer begins.

    Overall, given the design and operational descriptions from the DMAC documentation and code, the table elegantly encapsulates the functioning of a DMA controller's state machine with specific focus on its operation under the AXI protocol for data transfer, reinforcing its correctness based on the provided information.

  9. User

    한글로 출력해줘.

  10. Assistant

    제공된 정보를 기반으로 표는 DMAC 작업에 대한 설계 문서에서 설명한 대로 DMA 작업의 상태 머신 논리를 정확하게 포착한 것으로 보입니다. 표는 다양한 상태(IDLE, RREQ, RDATA, WREQ, WDATA), 각 상태에 대한 주요 출력, 다음 상태로 전환하기 위한 조건, 추가 노트 등을 보여줍니다. 이는 DMAC의 작업과 행동에 대한 깊은 통찰력을 제공합니다.

    DMAC의 작업에 대한 설계 문서와 RTL 코드 조각에서 추출된 정보를 고려할 때, 데이터 전송을 위한 AXI 버스와 구성을 위한 APB 인터페이스를 통해 메모리와 인터페이스하는 방식을 포함하여 DMAC의 핵심 작동 흐름을 간결하게 요약한 표입니다.

    여기 몇 가지 주요 검증 사항이 있습니다:

    1. 상태 전환: IDLE에서 RREQ로 이동하는 조건(DMA 시작 명령과 0이 아닌 바이트 길이가 있는 경우)은 CPU 개입을 최소화하면서 대량 데이터 전송을 처리하도록 설계된 일반적인 DMA 작업과 일치합니다.
    2. 상태 내 기능: 각 상태 내에서의 활동(예: 주소 포인터 증가(ARADDR, AWADDR) 및 내부 데이터 버퍼 관리(예: RDATA에서 FIFO로 데이터 푸시 및 WDATA에서 팝))은 소스 주소에서 읽기 작업을 요청하기 위해 ARVALID를 주장하는 것과 같은 AXI 프로토콜 기반 데이터 전송 메커니즘과 일치하는 예상 기능 동작과 일치합니다.
    3. 출력 신호: 주요 출력 신호(예: ARVALID, RREADY, AWVALID, WVALID)와 그 조건이 상태에 따라 달라지는 것(예: RREQ에서 읽기 작업을 요청하기 위해 ARVALID를 활성화)은 DMAC의 작동 방식을 캡슐화하는 데 효과적입니다.
    4. 노트/설명: 추가 노트는 데이터 전송이 시작되기 전에 필요한 설정 작업인 IDLE을 떠날 때 DMA 소스, 목적지 및 길이 레지스터를 복사하는 것과 같은 작동 논리를 이해하는 데 도움이 되는 명확한 컨텍스트를 제공합니다.

    전반적으로, DMAC의 설계 및 작동 설명을 고려할 때, 이 표는 데이터 전송을 위한 AXI 프로토콜 아래에서 DMA 컨트롤러의 상태 머신의 기능을 우아하게 요약하며, 제공된 정보를 기반으로 그 정확성을 강화합니다.

  11. User

    "다음 내용을 기반으로 비어있는 코드를 완성해주세요."를 영작해주세요.

  12. Assistant

    "Please complete the blank code based on the following content."

  13. User

    Please complete the blank code based on the following content.

    +-------+-------------------------------------------------------------------------------------+------------+-----------------------------------------------------------+-----------------------------------------+ | State | Major outputs | Next State | Next state transition condition | Notes | | +---------+--------+---------------------------------------+-------------------+------+ | | | | | ARVALID | RREADY | AWVALID | WVALID | done | | | | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | IDLE | 0 | 0 | 0 | 0 | 1 | RREQ | (DMA_CMD.start is written as 1) and (DMA_LEN.byte_len!=0) | On moving out, | | | | | | | | | | - Copy DMA_SRC to SRC_ADDR. | | | | | | | | | | - Copy DMA_DST to DST_ADDR | | | | | | | | | | - Copy DMA_LEN to the internal counter | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | RREQ | 1 | 0 | 0 | 0 | 0 | RDATA | ARREADY=1 | On moving out, | | | | | | | | | | - Increment ARADDR by 4 | | | | | | | | | | | | | | | | | | | | ARLEN = (cnt>=64) ? 'hF : cnt[5:2]-4'h1 | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | RDATA | 0 | 1 | 0 | 0 | 0 | WREQ | (RVALID=1) & (RLAST) | Push data to FIFO | | | | | | | +------------+-----------------------------------------------------------+-----------------------------------------+ | | | | | | | RDATA | (RVALID) & (!RLAST) | Push data to FIFO | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | WREQ | 0 | 0 | 1 | 0 | 0 | WDATA | AWREADY=1 | On moving out, | | | | | AWLEN=(cnt>=64) ? 'hF : cnt[5:2]-4'h1 | | | | | - Increment AWADDR by 4 | | | | | | | | | | - Decrement the internal counter by 4 | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+ | WDATA | 0 | 0 | 0 | 1 | 0 | RREQ | (WREADY=1) (!WLAST) & (counter!=0) | Pop data from FIFO | | | | | | WLAST=(wcnt=='d0) | | | | Decrement wcnt | | | | | | | +------------+-----------------------------------------------------------+-----------------------------------------+ | | | | | | | IDLE | (WREADY=1) (WLAST) & (counter==0) | Pop data from FIFO | +-------+---------+--------+---------------------------------------+-------------------+------+------------+-----------------------------------------------------------+-----------------------------------------+

    <StartOfFile:DMAC/RTL/DMAC_CFG.sv>> module DMAC_CFG ( input wire clk, input wire rst_n, // _n means active low

    // AMBA APB interface
    input wire psel_i,
    input wire penable_i,
    input wire [11:0] paddr_i,
    input wire pwrite_i,
    input wire [31:0] pwdata_i,
    output reg pready_o,
    output reg [31:0] prdata_o,
    output reg pslverr_o,
    
    // configuration registers
    output reg [31:0] src_addr_o,
    output reg [31:0] dst_addr_o,
    output reg [15:0] byte_len_o,
    output wire start_o,
    input wire done_i
    

    );

    // Configuration register to read/write
    reg [31:0] src_addr;
    reg [31:0] dst_addr;
    reg [15:0] byte_len;
    
    //----------------------------------------------------------
    // Write
    //----------------------------------------------------------
    // an APB write occurs when PSEL & PENABLE & PWRITE
    // clk : __--__--__--__--__--__--__--__--__--__--
    // psel : ___--------_____________________________
    // penable : _______----_____________________________
    // pwrite : ___--------_____________________________
    // wren : _______----_____________________________
    //
    // DMA start command must be asserted when APB writes 1 to the DMA_CMD
    // register
    // clk : __--__--__--__--__--__--__--__--__--__--
    // psel : ___--------_____________________________
    // penable : _______----_____________________________
    // pwrite : ___--------_____________________________
    // paddr : |DMA_CMD|
    // pwdata : | 1 |
    // start : _______----_____________________________
    
    wire wren = psel_i & penable_i & pwrite_i;
    
    always @(posedge clk) begin
        if (!rst_n) begin
            src_addr <= 32'd0;
            dst_addr <= 32'd0;
            byte_len <= 16'd0;
        end
        else if (wren) begin
            case (paddr_i)
                'h100: src_addr <= pwdata_i[31:0];
                'h104: dst_addr <= pwdata_i[31:0];
                'h108: byte_len <= pwdata_i[15:0];
            endcase
        end
    end
    wire start = wren & (paddr_i=='h10C) & pwdata_i[0];
    
    //----------------------------------------------------------
    // READ
    //----------------------------------------------------------
    // an APB read occurs when PSEL & PENABLE & !PWRITE
    // To make read data a direct output from register,
    // this code shall buffer the muxed read data into a register
    // in the SETUP cycle (PSEL & !PENABLE)
    // clk : __--__--__--__--__--__--__--__--__--__--
    // psel : ___--------_____________________________
    // penable : _______----_____________________________
    // pwrite : ________________________________________
    // reg update : ___----_________________________________
    // prdata : |DATA
    
    reg [31:0] rdata;
    
    always @(posedge clk) begin
        if (!rst_n) begin
            rdata <= 32'd0;
        end
        else if (psel_i & !penable_i & !pwrite_i) begin // in the setup cycle in the APB state diagram
            case (paddr_i)
                'h0: rdata <= 32'h0001_2024;
                'h100: rdata <= src_addr;
                'h104: rdata <= dst_addr;
                'h108: rdata <= {16'd0, byte_len};
                'h110: rdata <= {31'd0, done_i};
                default: rdata <= 32'd0;
            endcase
        end
    end
    
    // output assignments
    assign pready_o = 1'b1;
    assign prdata_o = rdata;
    assign pslverr_o = 1'b0;
    
    assign src_addr_o = src_addr;
    assign dst_addr_o = dst_addr;
    assign byte_len_o = byte_len;
    assign start_o = start;
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_CFG.sv>>

    <<StartOfFile:DMAC/RTL/DMAC_ENGINE.sv>> module DMAC_ENGINE ( input wire clk, input wire rst_n, // _n means active low

    // configuration registers
    input wire [31:0] src_addr_i,
    input wire [31:0] dst_addr_i,
    input wire [15:0] byte_len_i,
    input wire start_i,
    output wire done_o,
    
    // AMBA AXI interface (AW channel)
    output wire [3:0] awid_o,
    output wire [31:0] awaddr_o,
    output wire [3:0] awlen_o,
    output wire [2:0] awsize_o,
    output wire [1:0] awburst_o,
    output wire awvalid_o,
    input wire awready_i,
    
    // AMBA AXI interface (W channel)
    output wire [3:0] wid_o,
    output wire [31:0] wdata_o,
    output wire [3:0] wstrb_o,
    output wire wlast_o,
    output wire wvalid_o,
    input wire wready_i,
    
    // AMBA AXI interface (B channel)
    input wire [3:0] bid_i,
    input wire [1:0] bresp_i,
    input wire bvalid_i,
    output wire bready_o,
    
    // AMBA AXI interface (AR channel)
    output wire [3:0] arid_o,
    output wire [31:0] araddr_o,
    output wire [3:0] arlen_o,
    output wire [2:0] arsize_o,
    output wire [1:0] arburst_o,
    output wire arvalid_o,
    input wire arready_i,
    
    // AMBA AXI interface (R channel)
    input wire [3:0] rid_i,
    input wire [31:0] rdata_i,
    input wire [1:0] rresp_i,
    input wire rlast_i,
    input wire rvalid_i,
    output wire rready_o
    

    );

    // mnemonics for state values
    localparam S_IDLE = 3'd0,
                                S_RREQ = 3'd1,
                                S_RDATA = 3'd2,
                                S_WREQ = 3'd3,
                                S_WDATA = 3'd4;
    
    reg [2:0] state, state_n;
    
    reg [31:0] src_addr, src_addr_n;
    reg [31:0] dst_addr, dst_addr_n;
    reg [15:0] cnt, cnt_n;
    reg [3:0] wcnt, wcnt_n;
    
    reg arvalid,
                                rready,
                                awvalid,
                                wvalid,
                                wlast,
                                done;
    
    wire fifo_full,
                                fifo_empty;
    reg fifo_wren,
                                fifo_rden;
    wire [31:0] fifo_rdata;
    
    
    // it's desirable to code registers in a simple way
    always_ff @(posedge clk)
        if (!rst_n) begin
            state <= S_IDLE;
    
            src_addr <= 32'd0;
            dst_addr <= 32'd0;
            cnt <= 16'd0;
    
            wcnt <= 4'd0;
        end
        else begin
            state <= state_n;
    
            src_addr <= src_addr_n;
            dst_addr <= dst_addr_n;
            cnt <= cnt_n;
    
            wcnt <= wcnt_n;
        end
    
    
    // this block programs output values and next register values
    // based on states.
    always_comb begin
        // ********************** 
        // ********************** 
        // FILL YOUR CODE HERE
        // ********************** 
        // ********************** 
    end
    
    DMAC_FIFO u_fifo
    (
        .clk (clk),
        .rst_n (rst_n),
    
        .full_o (fifo_full),
        .wren_i (fifo_wren),
        .wdata_i (rdata_i),
    
        .empty_o (fifo_empty),
        .rden_i (fifo_rden),
        .rdata_o (fifo_rdata)
    );
    
    // Output assigments
    assign done_o = done;
    
    assign awid_o = 4'd0;
    assign awaddr_o = dst_addr;
    assign awlen_o = (cnt >= 'd64) ? 4'hF: cnt[5:2]-4'h1;
    assign awsize_o = 3'b010; // 4 bytes per transfer
    assign awburst_o = 2'b01; // incremental
    assign awvalid_o = awvalid;
    
    assign wid_o = 4'd0;
    assign wdata_o = fifo_rdata;
    assign wstrb_o = 4'b1111; // all bytes within 4 byte are valid
    assign wlast_o = wlast;
    assign wvalid_o = wvalid;
    
    assign bready_o = 1'b1;
    
    assign arvalid_o = arvalid;
    assign araddr_o = src_addr;
    assign arid_o = 4'd0;
    assign arlen_o = (cnt >= 'd64) ? 4'hF: cnt[5:2]-4'h1;
    assign arsize_o = 3'b010; // 4 bytes per transfer
    assign arburst_o = 2'b01; // incremental
    assign arvalid_o = arvalid;
    
    assign rready_o = rready & !fifo_full;
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_ENGINE.sv>>

    <<StartOfFile:DMAC/RTL/DMAC_FIFO.sv>> module DMAC_FIFO #( parameter DEPTH_LG2 = 4, parameter DATA_WIDTH = 32 ) ( input wire clk, input wire rst_n,

    output wire full_o,
    input wire wren_i,
    input wire [DATA_WIDTH-1:0] wdata_i,
    
    output wire empty_o,
    input wire rden_i,
    output wire [DATA_WIDTH-1:0] rdata_o
    

    );

    localparam FIFO_DEPTH = (1<<DEPTH_LG2);
    
    reg [DATA_WIDTH-1:0] data[FIFO_DEPTH];
    
    reg full, full_n,
                                        empty, empty_n;
    reg [DEPTH_LG2:0] wrptr, wrptr_n,
                                        rdptr, rdptr_n;
    
    // reset entries to all 0s
    always_ff @(posedge clk)
        if (!rst_n) begin
            full <= 1'b0;
            empty <= 1'b1; // empty after as reset
    
            wrptr <= {(DEPTH_LG2+1){1'b0}};
            rdptr <= {(DEPTH_LG2+1){1'b0}};
    
            for (int i=0; i<FIFO_DEPTH; i++) begin
                data[i] <= {DATA_WIDTH{1'b0}};
            end
        end
        else begin
            full <= full_n;
            empty <= empty_n;
    
            wrptr <= wrptr_n;
            rdptr <= rdptr_n;
    
            if (wren_i) begin
                data[wrptr[DEPTH_LG2-1:0]] <= wdata_i;
            end
        end
    
    always_comb begin
        wrptr_n = wrptr;
        rdptr_n = rdptr;
    
        if (wren_i) begin
            wrptr_n = wrptr + 'd1;
        end
    
        if (rden_i) begin
            rdptr_n = rdptr + 'd1;
        end
    
        empty_n = (wrptr_n == rdptr_n);
        full_n = (wrptr_n[DEPTH_LG2]!=rdptr_n[DEPTH_LG2])
                                     &(wrptr_n[DEPTH_LG2-1:0]==rdptr_n[DEPTH_LG2-1:0]);
    
    end
    
    // synthesis translate_off
    always @(posedge clk) begin
        if (full_o & wren_i) begin
            \$display("FIFO overflow");
            @(posedge clk);
            \$finish;
        end
    end
    
    always @(posedge clk) begin
        if (empty_o & rden_i) begin
            \$display("FIFO underflow");
            @(posedge clk);
            \$finish;
        end
    end
    // synthesis translate_on
    
    assign full_o = full;
    assign empty_o = empty;
    assign rdata_o = data[rdptr[DEPTH_LG2-1:0]];
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_FIFO.sv>>

    <<StartOfFile:DMAC/RTL/DMAC_TOP.sv>> module DMAC_TOP ( input wire clk, input wire rst_n, // _n means active low

    // AMBA APB interface
    input wire psel_i,
    input wire penable_i,
    input wire [11:0] paddr_i,
    input wire pwrite_i,
    input wire [31:0] pwdata_i,
    output reg pready_o,
    output reg [31:0] prdata_o,
    output reg pslverr_o,
    
    // AMBA AXI interface (AW channel)
    output wire [3:0] awid_o,
    output wire [31:0] awaddr_o,
    output wire [3:0] awlen_o,
    output wire [2:0] awsize_o,
    output wire [1:0] awburst_o,
    output wire awvalid_o,
    input wire awready_i,
    
    // AMBA AXI interface (AW channel)
    output wire [3:0] wid_o,
    output wire [31:0] wdata_o,
    output wire [3:0] wstrb_o,
    output wire wlast_o,
    output wire wvalid_o,
    input wire wready_i,
    
    // AMBA AXI interface (B channel)
    input wire [3:0] bid_i,
    input wire [1:0] bresp_i,
    input wire bvalid_i,
    output wire bready_o,
    
    // AMBA AXI interface (AR channel)
    output wire [3:0] arid_o,
    output wire [31:0] araddr_o,
    output wire [3:0] arlen_o,
    output wire [2:0] arsize_o,
    output wire [1:0] arburst_o,
    output wire arvalid_o,
    input wire arready_i,
    
    // AMBA AXI interface (R channel)
    input wire [3:0] rid_i,
    input wire [31:0] rdata_i,
    input wire [1:0] rresp_i,
    input wire rlast_i,
    input wire rvalid_i,
    output wire rready_o
    

    );

    wire [31:0] src_addr;
    wire [31:0] dst_addr;
    wire [15:0] byte_len;
    wire start;
    wire done;
    
    DMAC_CFG u_cfg(
        .clk (clk),
        .rst_n (rst_n),
    
        // AMBA APB interface
        .psel_i (psel_i),
        .penable_i (penable_i),
        .paddr_i (paddr_i),
        .pwrite_i (pwrite_i),
        .pwdata_i (pwdata_i),
        .pready_o (pready_o),
        .prdata_o (prdata_o),
        .pslverr_o (pslverr_o),
    
        .src_addr_o (src_addr),
        .dst_addr_o (dst_addr),
        .byte_len_o (byte_len),
        .start_o (start),
        .done_i (done)
    );
    
    DMAC_ENGINE u_engine(
        .clk (clk),
        .rst_n (rst_n),
    
        // configuration registers
        .src_addr_i (src_addr),
        .dst_addr_i (dst_addr),
        .byte_len_i (byte_len),
        .start_i (start),
        .done_o (done),
    
        // AMBA AXI interface (AW channel)
        .awid_o (awid_o),
        .awaddr_o (awaddr_o),
        .awlen_o (awlen_o),
        .awsize_o (awsize_o),
        .awburst_o (awburst_o),
        .awvalid_o (awvalid_o),
        .awready_i (awready_i),
    
        // AMBA AXI interface (W channel)
        .wid_o (wid_o),
        .wdata_o (wdata_o),
        .wstrb_o (wstrb_o),
        .wlast_o (wlast_o),
        .wvalid_o (wvalid_o),
        .wready_i (wready_i),
    
        // AMBA AXI interface (B channel)
        .bid_i (bid_i),
        .bresp_i (bresp_i),
        .bvalid_i (bvalid_i),
        .bready_o (bready_o),
    
        // AMBA AXI interface (AR channel)
        .arid_o (arid_o),
        .araddr_o (araddr_o),
        .arlen_o (arlen_o),
        .arsize_o (arsize_o),
        .arburst_o (arburst_o),
        .arvalid_o (arvalid_o),
        .arready_i (arready_i),
    
        // AMBA AXI interface (R channel)
        .rid_i (rid_i),
        .rdata_i (rdata_i),
        .rresp_i (rresp_i),
        .rlast_i (rlast_i),
        .rvalid_i (rvalid_i),
        .rready_o (rready_o)
    );
    

    endmodule <<EndOfFile:DMAC/RTL/DMAC_TOP.sv>>

    <<StartOfFile:DMAC/RTL/filelist.f>> -sverilog $LAB_PATH/RTL/DMAC_TOP.sv -sverilog $LAB_PATH/RTL/DMAC_CFG.sv -sverilog $LAB_PATH/RTL/DMAC_ENGINE.sv -sverilog $LAB_PATH/RTL/DMAC_FIFO.sv <<EndOfFile:DMAC/RTL/filelist.f>>

    <<StartOfFile:DMAC/SIM/TB/AXI_INTF.sv>> `include "../TB/AXI_TYPEDEF.svh"

    interface AXI_AW_CH #( parameter ADDR_WIDTH = AXI_ADDR_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic awvalid; logic awready; logic [ID_WIDTH-1:0] awid; logic [ADDR_WIDTH-1:0] awaddr; logic [3:0] awlen; logic [2:0] awsize; logic [1:0] awburst;

    endinterface

    interface AXI_W_CH #( parameter DATA_WIDTH = AXI_DATA_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic wvalid; logic wready; logic [ID_WIDTH-1:0] wid; logic [DATA_WIDTH-1:0] wdata; logic [DATA_WIDTH/8-1:0] wstrb; logic wlast;

    endinterface

    interface AXI_B_CH #( parameter ID_WIDTH = `AXI_ID_WIDTH ) ( input clk ); logic bvalid; logic bready; logic [ID_WIDTH-1:0] bid; logic [1:0] bresp;

    endinterface

    interface AXI_AR_CH #( parameter ADDR_WIDTH = AXI_ADDR_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic arvalid; logic arready; logic [ID_WIDTH-1:0] arid; logic [ADDR_WIDTH-1:0] araddr; logic [3:0] arlen; logic [2:0] arsize; logic [1:0] arburst;

    endinterface

    interface AXI_R_CH #( parameter DATA_WIDTH = AXI_DATA_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH ) ( input clk ); logic rvalid; logic rready; logic [ID_WIDTH-1:0] rid; logic [DATA_WIDTH-1:0] rdata; logic [1:0] rresp; logic rlast;

    endinterface

    interface APB ( input clk ); logic psel; logic penable; logic [31:0] paddr; logic pwrite; logic [31:0] pwdata; logic pready; logic [31:0] prdata; logic pslverr;

    modport master (
        input clk,
        input pready, prdata, pslverr,
        output psel, penable, paddr, pwrite, pwdata
    );
    
    task init();
        psel = 1'b0;
        penable = 1'b0;
        paddr = 32'd0;
        pwrite = 1'b0;
        pwdata = 32'd0;
    endtask
    
    task write(input int addr,
               input int data);
        #1
        psel = 1'b1;
        penable = 1'b0;
        paddr = addr;
        pwrite = 1'b1;
        pwdata = data;
        @(posedge clk);
        #1
        penable = 1'b1;
        @(posedge clk);
    
        while (pready==1'b0) begin
            @(posedge clk);
        end
    
        psel = 1'b0;
        penable = 1'b0;
        paddr = 'hX;
        pwrite = 1'bx;
        pwdata = 'hX;
    endtask
    
    task read(input int addr,
              output int data);
        #1
        psel = 1'b1;
        penable = 1'b0;
        paddr = addr;
        pwrite = 1'b0;
        pwdata = 'hX;
        @(posedge clk);
        #1
        penable = 1'b1;
        @(posedge clk);
    
        while (pready==1'b0) begin
            @(posedge clk);
        end
        data = prdata;
    
        psel = 1'b0;
        penable = 1'b0;
        paddr = 'hX;
        pwrite = 1'bx;
        pwdata = 'hX;
    endtask
    

    endinterface <<EndOfFile:DMAC/SIM/TB/AXI_INTF.sv>>

    <<StartOfFile:DMAC/SIM/TB/AXI_SLAVE.sv>> `include "../TB/AXI_TYPEDEF.svh"

    module AXI_SLAVE #( parameter ADDR_WIDTH = 16, parameter DATA_WIDTH = AXI_DATA_WIDTH, parameter ID_WIDTH = AXI_ID_WIDTH, parameter AWREADY_DELAY = 1, parameter ARREADY_DELAY = 1, parameter AR2R_DELAY = 50 ) ( input wire clk, input wire rst_n, // _n means active low

    AXI_AW_CH aw_ch,
    AXI_W_CH w_ch,
    AXI_B_CH b_ch,
    AXI_AR_CH ar_ch,
    AXI_R_CH r_ch
    

    );

    localparam DATA_DEPTH = 1<<ADDR_WIDTH;
    
    logic [7:0] mem[DATA_DEPTH];
    
    function void write_byte(int addr, input bit [7:0] wdata);
        mem[addr] = wdata;
    endfunction
    
    function void write_word(int addr, input bit [31:0] wdata);
        for (int i=0; i<4; i++) begin
            write_byte(addr+i, wdata[8*i +: 8]); // [i*8+7:i*8]
        end
    endfunction
    
    function bit [7:0] read_byte(int addr);
        read_byte = mem[addr];
    endfunction
    
    function bit [31:0] read_word(int addr);
        for (int i=0; i<4; i++) begin
            read_word[8*i +: 8] = read_byte(addr+i);// [i*8+7:i*8]
        end
    endfunction
    
    //----------------------------------------------------------
    // write channels (AW, W, B)
    //----------------------------------------------------------
    localparam logic [1:0] S_W_IDLE = 0,
                                S_W_AWREADY = 1,
                                S_W_BURST = 2,
                                S_W_RESP = 3;
    
    logic [1:0] wstate, wstate_n;
    logic [7:0] wcnt, wcnt_n;
    
    logic [ADDR_WIDTH-1:0] waddr, waddr_n;
    logic [ID_WIDTH-1:0] wid, wid_n;
    logic [3:0] wlen, wlen_n;
    
    always_ff @(posedge clk)
        if (!rst_n) begin
            wstate <= S_W_IDLE;
    
            wcnt <= 8'd0;
            waddr <= {ADDR_WIDTH{1'b0}};
            wid <= {ID_WIDTH{1'b0}};
            wlen <= 4'd0;
        end
        else begin
            wstate <= wstate_n;
    
            wcnt <= wcnt_n;
            waddr <= waddr_n;
            wid <= wid_n;
            wlen <= wlen_n;
        end
    
    always @(*) begin
        wstate_n = wstate;
    
        wcnt_n = wcnt;
        waddr_n = waddr;
        wid_n = wid;
        wlen_n = wlen;
    
        aw_ch.awready = 1'b0;
        w_ch.wready = 1'b0;
        b_ch.bvalid = 1'b0;
    
        case (wstate)
            S_W_IDLE: begin
                if (aw_ch.awvalid) begin
                    if (AWREADY_DELAY == 0) begin
                        waddr_n = aw_ch.awaddr;
                        wid_n = aw_ch.awid;
                        wlen_n = aw_ch.awlen;
                        aw_ch.awready = 1'b1;
                        wstate_n = S_W_BURST;
                    end
                    else begin
                        wcnt_n = AWREADY_DELAY-1;
                        wstate_n = S_W_AWREADY;
                    end
                end
            end
            S_W_AWREADY: begin
                if (wcnt==0) begin
                    waddr_n = aw_ch.awaddr;
                    wid_n = aw_ch.awid;
                    wlen_n = aw_ch.awlen;
                    aw_ch.awready = 1'b1;
                    wstate_n = S_W_BURST;
                end
                else begin
                    wcnt_n = wcnt - 8'd1;
                end
            end
            S_W_BURST: begin
                w_ch.wready = 1'b1;
                if (w_ch.wvalid) begin
                    for (int i=0; i<DATA_WIDTH/8; i++) begin
                        write_byte(waddr + i, w_ch.wdata[i*8 +: 8]); // [i*8+7:i*8]
                    end
                    waddr_n = waddr + (DATA_WIDTH/8);
                    if (wlen==4'd0) begin
                        wstate_n = S_W_RESP;
                    end
                    else begin
                        wlen_n = wlen - 4'd1;
                    end
                end
            end
            S_W_RESP: begin
                b_ch.bvalid = 1'b1;
                if (b_ch.bready) begin
                    wstate_n = S_W_IDLE;
                end
            end
        endcase
    end
    
    //----------------------------------------------------------
    // read channel (AR, R)
    //----------------------------------------------------------
    localparam logic [1:0] S_R_IDLE = 0,
                                S_R_ARREADY = 1,
                                S_R_DELAY = 2,
                                S_R_BURST = 3;
    
    logic [1:0] rstate, rstate_n;
    logic [7:0] rcnt, rcnt_n;
    
    logic [ADDR_WIDTH-1:0] raddr, raddr_n;
    logic [ID_WIDTH-1:0] rid, rid_n;
    logic [3:0] rlen, rlen_n;
    
    always_ff @(posedge clk)
        if (!rst_n) begin
            rstate <= S_R_IDLE;
    
            rcnt <= 8'd0;
            raddr <= {ADDR_WIDTH{1'b0}};
            rid <= {ID_WIDTH{1'b0}};
            rlen <= 4'd0;
        end
        else begin
            rstate <= rstate_n;
    
            rcnt <= rcnt_n;
            raddr <= raddr_n;
            rid <= rid_n;
            rlen <= rlen_n;
        end
    
    always_comb begin
        rstate_n = rstate;
    
        rcnt_n = rcnt;
        raddr_n = raddr;
        rid_n = rid;
        rlen_n = rlen;
    
        ar_ch.arready = 1'b0;
        r_ch.rvalid = 1'b0;
        r_ch.rlast = 1'b0;
    
        case (rstate)
            S_R_IDLE: begin
                if (ar_ch.arvalid) begin
                    if (ARREADY_DELAY == 0) begin
                        raddr_n = ar_ch.araddr;
                        rid_n = ar_ch.arid;
                        rlen_n = ar_ch.arlen;
                        ar_ch.arready = 1'b1;
    
                        rcnt_n = AR2R_DELAY - 1;
                        rstate_n = S_R_DELAY;
                    end
                    else begin
                        rcnt_n = ARREADY_DELAY-1;
                        rstate_n = S_R_ARREADY;
                    end
                end
            end
            S_R_ARREADY: begin
                if (rcnt==0) begin
                    raddr_n = ar_ch.araddr;
                    rid_n = ar_ch.arid;
                    rlen_n = ar_ch.arlen;
                    ar_ch.arready = 1'b1;
    
                    rcnt_n = AR2R_DELAY - 1;
                    rstate_n = S_R_DELAY;
                end
                else begin
                    rcnt_n = rcnt - 8'd1;
                end
            end
            S_R_DELAY: begin
                if (rcnt==0) begin
                    rstate_n = S_R_BURST;
                end
                else begin
                    rcnt_n = rcnt - 8'd1;
                end
            end
            S_R_BURST: begin
                r_ch.rvalid = 1'b1;
                r_ch.rlast = (rlen==4'd0);
                for (int i=0; i<DATA_WIDTH/8; i++) begin
                    r_ch.rdata[i*8 +: 8] = read_byte(raddr + i); // [i*8+7:i*8]
                end
                if (r_ch.rready) begin
                    raddr_n = raddr + (DATA_WIDTH/8);
                    if (rlen==4'd0) begin
                        rstate_n = S_R_IDLE;
                    end
                    else begin
                        rlen_n = rlen - 4'd1;
                    end
                end
            end
        endcase
    end
    
    // output assignments
    assign b_ch.bid = wid;
    assign b_ch.bresp = 2'd0;
    assign r_ch.rid = rid;
    assign r_ch.rresp = 2'd0;
    

    endmodule <<EndOfFile:DMAC/SIM/TB/AXI_SLAVE.sv>>

    <<StartOfFile:DMAC/SIM/TB/AXI_TYPEDEF.svh>> ifndef __AXI_TYPEDEF_SVH__ define AXI_TYPEDEF_SVH

    define AXI_ADDR_WIDTH 32 define AXI_DATA_WIDTH 32 `define AXI_ID_WIDTH 4

    `endif /* AXI_TYPEDEF_SVH */ <<EndOfFile:DMAC/SIM/TB/AXI_TYPEDEF.svh>>

    <<StartOfFile:DMAC/SIM/TB/DMAC_TOP_TB.sv>> define IP_VER 32'h000 define SRC_ADDR 32'h100 define DST_ADDR 32'h104 define LEN_ADDR 32'h108 define STAT_ADDR 32'h110 define START_ADDR 32'h10c

    `define TIMEOUT_CYCLE 999999 module DMAC_TOP_TB ();

    reg clk;
    reg rst_n;
    
    // clock generation
    initial begin
        clk = 1'b0;
    
        forever #10 clk = !clk;
    end
    
    // reset generation
    initial begin
        rst_n = 1'b0; // active at time 0
    
        repeat (3) @(posedge clk); // after 3 cycles,
        rst_n = 1'b1; // release the reset
    end
    
    // enable waveform dump
    initial begin
        \$dumpvars(0, u_DUT);
        \$dumpfile("dump.vcd");
    end
    

    // timeout initial begin #`TIMEOUT_CYCLE $display("Timeout!"); $finish; end

    APB apb_if (.clk(clk));
    
    AXI_AW_CH aw_ch (.clk(clk));
    AXI_W_CH w_ch (.clk(clk));
    AXI_B_CH b_ch (.clk(clk));
    AXI_AR_CH ar_ch (.clk(clk));
    AXI_R_CH r_ch (.clk(clk));
    
    task test_init();
        int data;
        apb_if.init();
    
        @(posedge rst_n); // wait for a release of the reset
        repeat (10) @(posedge clk); // wait another 10 cycles
    
        apb_if.read(`IP_VER, data);
        \$display("---------------------------------------------------");
        \$display("IP version: %x", data);
        \$display("---------------------------------------------------");
    
        \$display("---------------------------------------------------");
        \$display("Reset value test");
        \$display("---------------------------------------------------");
        apb_if.read(`SRC_ADDR, data);
        if (data===0)
            \$display("DMA_SRC(pass): %x", data);
        else begin
            \$display("DMA_SRC(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.read(`DST_ADDR, data);
        if (data===0)
            \$display("DMA_DST(pass): %x", data);
        else begin
            \$display("DMA_DST(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.read(`LEN_ADDR, data);
        if (data===0)
            \$display("DMA_LEN(pass): %x", data);
        else begin
            \$display("DMA_LEN(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.read(`STAT_ADDR, data);
        if (data===1)
            \$display("DMA_STATUS(pass): %x", data);
        else begin
            \$display("DMA_STATUS(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
    endtask
    
    task test_dma(input int src, input int dst, input int len);
        int data;
        int word;
        realtime elapsed_time;
    
        \$display("---------------------------------------------------");
        \$display("Load data to memory");
        \$display("---------------------------------------------------");
        for (int i=src; i<(src+len); i=i+4) begin
            word = \$random; 
            u_mem.write_word(i, word);
        end
    
        \$display("---------------------------------------------------");
        \$display("Configuration test");
        \$display("---------------------------------------------------");
        apb_if.write(`SRC_ADDR, src);
        apb_if.read(`SRC_ADDR, data);
        if (data===src)
            \$display("DMA_SRC(pass): %x", data);
        else begin
            \$display("DMA_SRC(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.write(`DST_ADDR, dst);
        apb_if.read(`DST_ADDR, data);
        if (data===dst)
            \$display("DMA_DST(pass): %x", data);
        else begin
            \$display("DMA_DST(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
        apb_if.write(`LEN_ADDR, len);
        apb_if.read(`LEN_ADDR, data);
        if (data===len)
            \$display("DMA_LEN(pass): %x", data);
        else begin
            \$display("DMA_LEN(fail): %x", data);
            @(posedge clk);
            \$finish;
        end
    
        \$display("---------------------------------------------------");
        \$display("DMA start");
        \$display("---------------------------------------------------");
        apb_if.write(`START_ADDR, 32'h1);
        elapsed_time = \$realtime;
    
        \$display("---------------------------------------------------");
        \$display("Wait for a DMA completion");
        \$display("---------------------------------------------------");
        data = 0;
        while (data!=1) begin
            apb_if.read(`STAT_ADDR, data);
            repeat (100) @(posedge clk);
        end
        @(posedge clk);
        elapsed_time = \$realtime - elapsed_time;
        \$timeformat(-9, 0, " ns", 10);
        \$display("Elapsed time for DMA: %t", elapsed_time);
    
        \$display("---------------------------------------------------");
        \$display("DMA completed");
        \$display("---------------------------------------------------");
    
        repeat (len) @(posedge clk); // to make sure data is written
    
        \$display("---------------------------------------------------");
        \$display("verify data");
        \$display("---------------------------------------------------");
        for (int i=0; i<len; i=i+4) begin
            logic [31:0] src_word;
            logic [31:0] dst_word;
            src_word = u_mem.read_word(src+i);
            dst_word = u_mem.read_word(dst+i);
            if (src_word!==dst_word) begin
                \$display("Mismatch! (src:%x @%x, dst:%x @%x", src_word, src+i, dst_word, dst+i);
            end
        end
    endtask
    
    int src,
                    dst,
                    len;
    
    // main
    initial begin
        test_init();
    
        src = 'h0000_1000;
        dst = 'h0000_2000;
        len = 'h0100;
        \$display("===================================================");
        \$display("= 1st trial"); 
        \$display("= Copying %x bytes from %x to %x", len, src, dst);
        \$display("===================================================");
        test_dma(src, dst, len);
    
        src = 'h1234_1234;
        dst = 'hABCD_ABCC;
        len = 'h0F00;
        \$display("===================================================");
        \$display("= 2nd trial (long transfer)");
        \$display("= Copying %x bytes from %x to %x", len, src, dst);
        \$display("===================================================");
        test_dma(src, dst, len);
    
        src = 'h4278_0000;
        dst = 'h4278_1000;
        len = 'h0F10;
        \$display("===================================================");
        \$display("= 3rd trial (long transfer-2)");
        \$display("= Copying %x bytes from %x to %x", len, src, dst);
        \$display("===================================================");
        test_dma(src, dst, len);
        \$finish;
    end
    
    
    AXI_SLAVE u_mem (
        .clk (clk),
        .rst_n (rst_n),
    
        .aw_ch (aw_ch),
        .w_ch (w_ch),
        .b_ch (b_ch),
        .ar_ch (ar_ch),
        .r_ch (r_ch)
    );
    
    DMAC_TOP u_DUT (
        .clk (clk),
        .rst_n (rst_n),
    
        // APB interface
        .psel_i (apb_if.psel),
        .penable_i (apb_if.penable),
        .paddr_i (apb_if.paddr[11:0]),
        .pwrite_i (apb_if.pwrite),
        .pwdata_i (apb_if.pwdata),
        .pready_o (apb_if.pready),
        .prdata_o (apb_if.prdata),
        .pslverr_o (apb_if.pslverr),
    
        // AXI AW channel
        .awid_o (aw_ch.awid),
        .awaddr_o (aw_ch.awaddr),
        .awlen_o (aw_ch.awlen),
        .awsize_o (aw_ch.awsize),
        .awburst_o (aw_ch.awburst),
        .awvalid_o (aw_ch.awvalid),
        .awready_i (aw_ch.awready),
    
        // AXI W channel
        .wid_o (w_ch.wid),
        .wdata_o (w_ch.wdata),
        .wstrb_o (w_ch.wstrb),
        .wlast_o (w_ch.wlast),
        .wvalid_o (w_ch.wvalid),
        .wready_i (w_ch.wready),
    
        // AXI B channel
        .bid_i (b_ch.bid),
        .bresp_i (b_ch.bresp),
        .bvalid_i (b_ch.bvalid),
        .bready_o (b_ch.bready),
    
        // AXI AR channel
        .arid_o (ar_ch.arid),
        .araddr_o (ar_ch.araddr),
        .arlen_o (ar_ch.arlen),
        .arsize_o (ar_ch.arsize),
        .arburst_o (ar_ch.arburst),
        .arvalid_o (ar_ch.arvalid),
        .arready_i (ar_ch.arready),
    
        // AXI R channel
        .rid_i (r_ch.rid),
        .rdata_i (r_ch.rdata),
        .rresp_i (r_ch.rresp),
        .rlast_i (r_ch.rlast),
        .rvalid_i (r_ch.rvalid),
        .rready_o (r_ch.rready)
    );
    

    endmodule <<EndOfFile:DMAC/SIM/TB/DMAC_TOP_TB.sv>>

    <<StartOfFile:DMAC/SIM/TB/filelist.f>> $LAB_PATH/SIM/TB/timescale.v $LAB_PATH/SIM/TB/AXI_INTF.sv $LAB_PATH/SIM/TB/AXI_SLAVE.sv $LAB_PATH/SIM/TB/DMAC_TOP_TB.sv <<EndOfFile:DMAC/SIM/TB/filelist.f>>

    <<StartOfFile:DMAC/SIM/TB/timescale.v>> `timescale 1ns/1ps <<EndOfFile:DMAC/SIM/TB/timescale.v>>

    <<StartOfFile:DMAC/SIM/run.compile>> #!/bin/bash

    source ../../scripts/common.sh

    export LAB_PATH="$PWD/../" FILELIST_TB="../TB/filelist.f" FILELIST_RTL="../../RTL/filelist.f"

    echo "Cleaning up the old directory" rm -rf $RUN_DIR echo "Creating a new directory" mkdir -p $RUN_DIR cd $RUN_DIR echo "Compiling" $COMPILE_CMD $COMPILE_OPTIONS -f $FILELIST_TB -f $FILELIST_RTL <<EndOfFile:DMAC/SIM/run.compile>>

    <<StartOfFile:DMAC/SIM/run.compile4cov>> #!/bin/bash

    source ../../scripts/common.sh

    export LAB_PATH="$PWD/../" COV_DIR="$PWD/VDB" COV_OPTIONS="-cm line+cond+fsm+tgl+branch -cm_dir $COV_DIR" FILELIST_TB="../TB/filelist.f" FILELIST_RTL="../../RTL/filelist.f"

    echo "Cleaning up the old directory" rm -rf $RUN_DIR echo "Creating a new directory" mkdir -p $RUN_DIR cd $RUN_DIR echo "Compiling" $COMPILE_CMD $COMPILE_OPTIONS $COV_OPTIONS -f $FILELIST_TB -f $FILELIST_RTL <<EndOfFile:DMAC/SIM/run.compile4cov>>

    <<StartOfFile:DMAC/SIM/run.sim>> #!/bin/bash

    source ../../scripts/common.sh

    if [ -e $RUN_DIR/simv ]; then cd $RUN_DIR ./simv else echo "Compile file does not exist" exit 1 fi <<EndOfFile:DMAC/SIM/run.sim>>

    <<StartOfFile:DMAC/SIM/run.sim4cov>> #!/bin/bash

    source ../../scripts/common.sh

    COV_DIR="$PWD/VDB" COV_OPTIONS="-cm line+cond+fsm+tgl+branch -cm_dir $COV_DIR"

    if [ -e $RUN_DIR/simv ]; then cd $RUN_DIR ./simv $COV_OPTIONS else echo "Compile file does not exist" exit 1 fi <<EndOfFile:DMAC/SIM/run.sim4cov>>

    <<StartOfFile:DMAC/SIM/run.verdi>> #!/bin/bash

    source ../../scripts/common.sh

    export LAB_PATH="$PWD/../" FILELIST_TB="../TB/filelist.f" FILELIST_RTL="../../RTL/filelist.f"

    cd $RUN_DIR $VERDI_CMD $VERDI_OPTIONS -f $FILELIST_TB -f $FILELIST_RTL <<EndOfFile:DMAC/SIM/run.verdi>>

    <<StartOfFile:DMAC/SIM/run.verdi4cov>> verdi -cov -covdir VDB.vdb/ <<EndOfFile:DMAC/SIM/run.verdi4cov>>

    <<StartOfFile:DMAC/SIM/run.waveform>> #!/bin/bash

    source ../../scripts/common.sh

    if [ -e $RUN_DIR/dump.vcd ]; then cd $RUN_DIR $WAVE_CMD $WAVE_OPTIONS -i dump.vcd else echo "Dump file does not exist" exit 1 fi <<EndOfFile:DMAC/SIM/run.waveform>>

    ///

    Direct Memory Access (DMA) Controller Design Document V1.0

    1 Overview This document specifies the design and implementation of a Direct Memory Access Controller (DMAC) as a part of System-on-a-Chip (SoC). The main purpose of this DMAC design is to integrate into SoC for exchange a large volume of data between memory and peripherals at high speed. The proposed DMAC works on ARM’s Advanced Microcontroller Bus Architecture (AMBA) specification. The DMAC provides an AMBA APB interface to configure the IP, and an AMBA AXI interface to transfer data.

    2 Architecture Specification 2.1 General Description Some applications require transferring a volume of data between memory and peripherals without any modification on data. In software, it is commonly served by executing the memcpy library function in C, C++ or other languages. In C, the function has the following interface and copies len bytes from the object pointed by src to the object pointed by dst: void* memcpy(void* dst, const void* src, size_t len). While a pure software-based implementation of memcpy transfers data using CPU instructions, DMA does not use expensive CPU cycles but uses a hardware engine (DMAC) for the transfer. This can significantly speed up data transfers and allows using CPU for other jobs. 2.2 Usage Constraints Below describe constraints in utilizing DMAC v1. -The src and dst addresses are physical addresses. -The src and dst addresses must be a multiple of 4. -The len must be a multiple of 4. -The maximum len is 0xFFFF -Source and destination ranges must not overlap. 2.3 Programming Model Software can use the following sequence to transfer data using DMAC. -1.Write the source address to DMA_SRC register -2.Write the destination address to DMA_DST register -3.Write length to DMA_LEN register -4.Write 1 to bit[0] of DMA_CMD register -5.Wait until DMA_STATUS register has bit[0] as 1.

    2.4 Register Map In order to control DMAC, software can configure the following registers. +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+ | Offset | Reg Name | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+ | 0x00 | DMA_VER | version | +--------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ | 0x04~0xFC | Reserved | +--------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ | 0x100 | DMA_SRC | start_addr | +--------+------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+ | 0x104 | DMA_DST | start_addr | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------------------------------------------------------------------------+ | 0x108 | DMA_LEN | | | | | | | | | | | | | | | | | byte_len | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+ | 0x10C | DMA_CMD | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | start | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+ | 0x110 | DMA_STATUS | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | done | +--------+------------+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+-------+

    2.4.1 DMA VERSION

    Field nameBit rangeR/WReset valueDesciption
    version[31:0]R0x0001_2024The version of this DMA controller. The upper 16 bits represent the major version. The lower 16 bits represent the released year of the version. This document describes behaviors of major version 1.

    2.4.2 DMA_SRC

    Field nameBit rangeR/WReset valueDesciption
    start_addr[31:0]R/W0x0000_0000start address of the source range.

    2.4.3 DMA_DST

    Field nameBit rangeR/WReset valueDesciption
    start_addr[31:0]R/W0x0000_0000start address of the destination range.

    2.4.4 DMA_LEN

    Field nameBit rangeR/WReset valueDesciption
    byte_len[15:0]R/W0x0000Number of bytes to be transferred from the source to the destination.

    2.4.5 DMA_CMD Field

    Field nameBit rangeR/WReset valueDesciption
    start[0]WN/AWriting 1 to this field will initiate a DMA transfer based on DMA_SRC, DMA_DST, and DMA_LEN registers. Software must not write 1 when there’s an on-going transfer. Writing 0 to this field does not affect operation

    2.4.6 DMA_STATUS

    Field nameBit rangeR/WReset valueDesciption
    done[0]R1This field is 1 when there’s no on-going DMA transfer. Software must wait this field to be 1 for a completion of a transfer. Software must not initiate a DMA transfer when this field is 0.

    3 Micro-architecture v1.1 Specification This section describes microarchitecture of a simple DMAC. It reads data from memory, buffers the data, and write the data into memory. It repeats this procedure until it completes transferring the specified number of bytes. For simplicity, it read/writes one-cycle data (4 bytes) at a time (in other words, burst-1 transfers). For simplicity, this microarchitecture does not consider write responses from the AXI interface. Later versions will support burst transfers and write responses.

    3.1 External Interface DMAC v1.1 has the following external interfaces to communicate with other hardware IPs. -AMBA APB interface for configuration -AMBA AXI interface for data transfer

    The image you’ve uploaded is a diagram showing the on-chip interconnect of a computer system. Here’s a detailed description: The diagram illustrates how the CPU core, memory, and DMAC (Direct Memory Access Controller) are connected through an on-chip interconnect. The connections also include specific interfaces like Config interface (APB) and Data interface (AXI). “CPU core” is a box on the left side connected to the central “On-chip interconnect” cloud shape with a bidirectional arrow. Below the “CPU core,” there’s another box labeled “Memory,” also connected to the “On-chip interconnect” with a bidirectional arrow. On the right side, there’s a box labeled “DMAC” connected to both “Config interface (APB)” and “Data interface (AXI)” which are in turn connected to the central “On-chip interconnect” with bidirectional arrows. The arrows indicate that data can flow in both directions between these components.

    3.2 Block Diagram DMAC v1.1 has the following blocks inside.

    The diagram is divided into three main blocks labeled “DMAC_TOP,” “DMAC_CFG,” and “DMAC_ENGINE.” “clk” and “rst” are inputs to the “DMAC_TOP” block. An arrow labeled “APB” connects the “DMAC_TOP” block to the “DMAC_CFG” block. Another arrow labeled “AXI” connects both the “DMAC_TOP” and “DMAC_CFG” blocks to the “DMAC_ENGINE” block. Inside the “DMAC_ENGINE” block, there are four internal components labeled as follows: SRC_ADDR DST_ADDR CNT DATA BUF There’s also a small circular graph with nodes labeled 0 to 3 inside this block. This diagram is likely used to illustrate the flow of data or control signals between these components in a Direct Memory Access Controller configuration. Please let me know if you need more information!

    3.3 Configuration Register (lab2) This block receives read/write requests from the APB and configures the registers describes in Section 2.4.

    3.4 Finite State Machine (lab3) DMA engine utilizes the following state machine to control operations.

    The diagram contains five blue circles representing different states: IDLE, RREQ, RDATA, WREQ, and WDATA. Arrows connect these circles indicating the flow from one state to another. Each arrow has text annotations that describe the conditions for transitioning from one state to another. For example, transitioning from IDLE to RREQ requires writing 1 to DMA_CMD & LEN!=0, and copying DMA_SRC/DST/LEN. There are also annotations on the state circles themselves, such as “done=1” on IDLE and “AWVALID=1” on WDATA.

    +-------+--------------------------------------------+------------+-----------------------------------------------------------+----------------------------------------+ | State | Major outputs | Next State | Next state transition condition | Notes | | +---------+--------+---------+--------+------+ | | | | | ARVALID | RREADY | AWVALID | WVALID | done | | | | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | IDLE | 0 | 0 | 0 | 0 | 1 | RREQ | (DMA_CMD.start is written as 1) and (DMA_LEN.byte_len!=0) | On moving out, | | | | | | | | | | - Copy DMA_SRC to SRC_ADDR. | | | | | | | | | | - Copy DMA_DST to DST_ADDR | | | | | | | | | | - Copy DMA_LEN to the internal counter | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | RREQ | 1 | 0 | 0 | 0 | 0 | RDATA | ARREADY=1 | On moving out, | | | | | | | | | | - Increment ARADDR by 4 | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | RDATA | 0 | 1 | 0 | 0 | 0 | WREQ | RVALID=1 | On moving out, | | | | | | | | | | - Buffer RDATA into the data buffer | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | WREQ | 0 | 0 | 1 | 0 | 0 | WDATA | AWREADY=1 | On moving out, | | | | | | | | | | - Increment AWADDR by 4 | | | | | | | | | | - Decrement the internal counter by 4 | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+ | WDATA | 0 | 0 | 0 | 1 | 0 | RREQ | (WREADY=1) & (counter!=0) | | | | | | | | +------------+-----------------------------------------------------------+----------------------------------------+ | | | | | | | IDLE | (WREADY=1) & (counter==0) | | +-------+---------+--------+---------+--------+------+------------+-----------------------------------------------------------+----------------------------------------+

    { "signal": [ { "name": "clk", "wave": "p....|.........." }, { "name": "state", "wave": "2.3.4|..5.6.2...", "data": ["IDLE", "RREQ", "RDATA", "WREQ", "WDATA", "IDLE"] }, { "name": "write to CMD", "wave": "010..|..........", "data": ["1"] }, {}, [ "AR ch", { "name": "ARVALID(out)", "wave": "0.1.0|..........", "data": ["SRC"] }, { "name": "ARADDR(out)", "wave": "x.3.x|..........", "data": ["SRC"] }, { "name": "ARLEN(out)", "wave": "2....|..........", "data": ["0"] }, { "name": "ARREADY(in)", "wave": "0..10|.........." }, ], [ "R ch", { "name": "RREADY(out)", "wave": "0...1|..0......." }, { "name": "RVALID(in)", "wave": "0....|.10......." }, { "name": "RDATA(in)", "wave": "x....|.4x.......", "data": ["DATA"] }, ], [ "AW ch", { "name": "AWVALID(out)", "wave": "0....|..1.0....." }, { "name": "AWADDR(out)", "wave": "x....|..5.x.....", "data": ["DST"] }, { "name": "AWLEN(out)", "wave": "2....|..........", "data": ["0"] }, { "name": "AWREADY(in)", "wave": "0....|...10....." }, ], [ "W ch", { "name": "WVALID(out)", "wave": "0....|....1.0..." }, { "name": "WDATA(out)", "wave": "x....|....4.x...", "data": ["DATA"] }, { "name": "WREADY(in)", "wave": "0....|.....10..." } ] ], "head" : { "tick" : "0" }, "foot" : { "tick" : "0" } } 그림 1. DMA operation with microarchitecture v1.1

    4 Micro-architecture v1.2 Specification (lab4) A problem with microarchitecture v1.1 is that it reads/writes data one-by-one. As memory read takes some time, DMAC v1.1 will suffer from poor performance with a long memory read latency (그림 2). We will improve the microarchitecture to transfer a burst of data to minimize performance degradation.

    { "signal": [ { "name": "clk", "wave": "p....|.................." }, { "name": "state", "wave": "2.3.4|..5.6.3.4|..5.6.3.", "data": ["IDLE", "RREQ", "RDATA", "WREQ", "WDATA", "RREQ", "RDATA", "WREQ", "WDATA", "RREQ"] }, { "name": "write to CMD", "wave": "010..|.........|........", "data": ["1"] }, {}, [ "AR ch", { "name": "ARVALID(out)", "wave": "0.1.0|......1.0|......1.", "data": ["SRC"] }, { "name": "ARADDR(out)", "wave": "x.3.x|......3.x|......3.", "data": ["SRC", "SRC+4", "SRC+8"] }, { "name": "ARLEN(out)", "wave": "2....|.........|........", "data": ["0"] }, { "name": "ARREADY(in)", "wave": "0..10|.......10|.......1" }, ], [ "R ch", { "name": "RREADY(out)", "wave": "0...1|..0.....1|..0....." }, { "name": "RVALID(in)", "wave": "0....|.10......|.10....." }, { "name": "RDATA(in)", "wave": "x....|.4x......|.4x.....", "data": ["DATA", "DATA"] }, ], [ "AW ch", { "name": "AWVALID(out)", "wave": "0....|..1.0....|..1.0..." }, { "name": "AWADDR(out)", "wave": "x....|..5.x....|..5.x...", "data": ["DST", "DST+4"] }, { "name": "AWLEN(out)", "wave": "2....|.........|........", "data": ["0"] }, { "name": "AWREADY(in)", "wave": "0....|...10....|...10..." }, ], [ "W ch", { "name": "WVALID(out)", "wave": "0....|....1.0..|....1.0." }, { "name": "WDATA(out)", "wave": "x....|....4.x..|....4.x.", "data": ["DATA", "DATA"] }, { "name": "WREADY(in)", "wave": "0....|.....10..|.....10." } ] ], "head" : { "tick" : "0" }, "foot" : { "tick" : "0" } } 그림 2. DMA operation with microarchitecture 1.1. At a time, it transfers single burst of data In Microarchitecture version 2, DMAC transfers up to 16 cycles of data with a single access. This can significantly reduce execution time by transferring data in bursts (그림 3).

    { "signal": [ { "name": "clk", "wave": "p....|....|.......|....." }, { "name": "state", "wave": "2.3.4|....|..5.6..|..3.4", "data": ["IDLE", "RREQ", "RDATA", "WREQ", "WDATA", "RREQ"] }, { "name": "write to CMD", "wave": "010..|....|.......|.....", "data": ["1"] }, {}, [ "AR ch", { "name": "ARVALID(out)", "wave": "0.1.0|....|.......|.....", "data": ["SRC"] }, { "name": "ARADDR(out)", "wave": "x.3.x|....|.......|.....", "data": ["SRC"] }, { "name": "ARLEN(out)", "wave": "2....|....|.......|.....", "data": ["15"] }, { "name": "ARREADY(in)", "wave": "0..10|....|.......|....." }, ], [ "R ch", { "name": "RREADY(out)", "wave": "0...1|....|..0....|....." }, { "name": "RVALID(in)", "wave": "0....|.1..|..0....|....." }, { "name": "RLAST(in)", "wave": "0....|....|.10....|....." }, { "name": "RDATA(in)", "wave": "x....|.444|44x....|.....", "data": ["D0", "D1", "D2", "D14", "D15"] }, ], [ "AW ch", { "name": "AWVALID(out)", "wave": "0....|....|..1.0..|....." }, { "name": "AWADDR(out)", "wave": "x....|....|..5.x..|.....", "data": ["DST"] }, { "name": "AWLEN(out)", "wave": "2....|....|.......|.....", "data": ["15"] }, { "name": "AWREADY(in)", "wave": "0....|....|...10..|....." }, ], [ "W ch", { "name": "WVALID(out)", "wave": "0....|....|....1..|..0.." }, { "name": "WDATA(out)", "wave": "x....|....|....444|44x..", "data": ["D0", "D1", "D2", "D14", "D15"] }, { "name": "WLAST(out)", "wave": "0....|....|.......|.10.." }, { "name": "WREADY(in)", "wave": "0....|....|....1..|..0.." } ] ], "head" : { "tick" : "0" }, "foot" : { "tick" : "0" } } 그림 3. DMA operation with burst transfers. At a time, a request reads/writes 16 cycles of data.

  14. Assistant

    To complete the blank code based on the given state transition table and descriptions, we need to implement a finite state machine (FSM) in the DMAC_ENGINE module, handling the transitions between the states: IDLE, RREQ, RDATA, WREQ, and WDATA, along with their corresponding actions.

    Here is a refined version of the DMAC_ENGINE module that captures the states, outputs, and transitions described:

    module DMAC_ENGINE
    (
        input wire clk,
        input wire rst_n,
    
        // Configuration registers inputs
        input wire [31:0] src_addr_i,
        input wire [31:0] dst_addr_i,
        input wire [15:0] byte_len_i,
        input wire start_i,
    
        // Status output
        output reg done_o,
    
        // AMBA AXI Interfaces
        output reg arvalid_o,
        input wire arready_i,
        output reg [31:0] araddr_o,
        output reg awvalid_o,
        input wire awready_i,
        output reg [31:0] awaddr_o,
        output reg wvalid_o,
        input wire wready_i,
        output reg [31:0] wdata_o,
        output reg rready_o,
        input wire rvalid_i,
        input wire [31:0] rdata_i,
        input wire rlast_i
    );
    
        // State definition
        typedef enum int {IDLE, RREQ, RDATA, WREQ, WDATA} state_t;
        state_t state, next_state;
    
        // Internal signals
        reg [31:0] src_addr, dst_addr;
        reg [15:0] byte_len, byte_cnt;
        wire transfer_done = (byte_cnt == 0);
        
        // FIFO interface signals might go here
    
        // FSM Logic
        always @(posedge clk)
        begin
            if (!rst_n)
                state <= IDLE;
            else
                state <= next_state;
            
            // Handling state-dependent register updates might go here as well
        end
    
        always_comb
        begin
            next_state = state; // Default to staying in current state
            case(state)
                IDLE: if (start_i && byte_len_i != 0) next_state = RREQ;
                RREQ: if (arready_i) next_state = RDATA;
                RDATA: if (rvalid_i && rlast_i) next_state = WREQ;
                WREQ: if (awready_i) next_state = WDATA;
                WDATA: if (wready_i && transfer_done) next_state = IDLE;
            endcase
    
            // Handling state-dependent signal assignments might go here
        end
    
        // Implement actions that should be taken on state transitions such as
        // updating the `araddr_o`, `awaddr_o`, `wdata_o` and managing byte_count
        // You'll need to add logic for managing the FIFO and handling the data transfer between states
    
        // Additional control and data signal assignments
        
    endmodule
    

    This code snippet initializes an FSM with the states outlined in the provided table. It also initializes key control signals but doesn't include the specifics of managing data through the FIFO, updating address counters, or detailed GPIO configuration for the AXI interfaces. Those will require further implementation based on how data is buffered between reads and writes, and how your AXI interfaces are set up to interact with external memories or peripherals.

    Each state's transitions depend on the input signals and the internal conditions (like ARREADY for RREQ to RDATA move), as specified. You'll need to ensure the AXI signals (arvalid_o, awvalid_o, wvalid_o, and rready_o) are correctly assigned per your AXI transaction requirements within each state.

    Remember to integrate the FIFO for buffering data between read (RDATA) and write (WDATA) operations, considering the actions like "Push data to FIFO" and "Pop data from FIFO" during transitions.

  15. User

    ""FILL YOUR CODE HERE"에 적절한 코드를 출력해주세요."를 영작해줘.

  16. Assistant

    "Please output appropriate code for 'FILL YOUR CODE HERE'."