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]];
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>>
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>>
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.
The 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 name
Bit range
R/W
Reset value
Desciption
start_addr
[31:0]
R/W
0x0000_0000
start address of the source range.
2.4.3 DMA_DST
Field name
Bit range
R/W
Reset value
Desciption
start_addr
[31:0]
R/W
0x0000_0000
start address of the destination range.
2.4.4 DMA_LEN
Field name
Bit range
R/W
Reset value
Desciption
byte_len
[15:0]
R/W
0x0000
Number of bytes to be transferred from the source to the destination.
2.4.5 DMA_CMD Field
Field name
Bit range
R/W
Reset value
Desciption
start
[0]
W
N/A
Writing 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 name
Bit range
R/W
Reset value
Desciption
done
[0]
R
1
This 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.
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).
// DMA Engine State Machine Logic
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
state <= S_IDLE;
src_addr <= 32'b0;
dst_addr <= 32'b0;
cnt <= 16'b0;
wcnt <= 4'b0;
arvalid <= 1'b0;
rready <= 1'b0;
awvalid <= 1'b0;
wvalid <= 1'b0;
wlast <= 1'b0;
done <= 1'b1;
end else begin
state <= state_n;
src_addr <= src_addr_n;
dst_addr <= dst_addr_n;
cnt <= cnt_n;
wcnt <= wcnt_n;
// Default values for control signals
arvalid <= 1'b0;
rready <= 1'b0;
awvalid <= 1'b0;
wvalid <= 1'b0;
wlast <= 1'b0;
done <= 1'b0;
case (state)
S_IDLE: begin
if (start_i && byte_len_i != 16'b0) begin
state_n <= S_RREQ;
src_addr_n <= src_addr_i;
dst_addr_n <= dst_addr_i;
cnt_n <= byte_len_i;
done <= 1'b0;
end else begin
done <= 1'b1;
end
end
S_RREQ: begin
arvalid <= 1'b1;
if (arready_i) begin
state_n <= S_RDATA;
end
end
S_RDATA: begin
rready <= 1'b1;
if (rvalid_i && rlast_i) begin
state_n <= S_WREQ;
src_addr_n <= src_addr + 32'd4; // Increment src address
wcnt_n <= cnt > 16 ? 4'hF : cnt[3:0] - 1; // Calculate wcnt for the next burst
cnt_n <= cnt - 32'd4; // Decrement cnt by 4 (size of word)
end
end
S_WREQ: begin
awvalid <= 1'b1;
if (awready_i) begin
state_n <= S_WDATA;
end
end
S_WDATA: begin
wvalid <= 1'b1;
wlast <= wcnt == 4'd0; // Set wlast when wcnt reaches 0
if (wready_i && wlast) begin
if (cnt == 32'd0) begin
state_n <= S_IDLE;
end else begin
state_n <= S_WREQ;
dst_addr_n <= dst_addr + 32'd4; // Increment dst address for next burst
wcnt_n <= wcnt - 1'b1; // Decrement wcnt
cnt_n <= cnt - 32'd4; // Decrement cnt by 4 (size of word)
end
end
end
default: state_n <= S_IDLE;
endcase
end
end
This code implements the Direct Memory Access (DMA) Controller's Engine logic using a Finite State Machine (FSM). The FSM transitions through states based on the availability of the ARREADY/ARVALID for read operations and AWREADY/AWVALID for write operations along with other conditions to manage data transfers effectively. Note that this simplified illustration may need adjustments such as focus on data buffering, actual data transfer handling, error checking, and overlap prevention, which requires comprehensive consideration aligned to specific requirements and AXI protocol adherence.
Also, keep in mind start_i, byte_len_i, src_addr_i, dst_addr_i, arready_i, rvalid_i, rlast_i, awready_i, and wready_i are signals that ideally come from configuration registers or interface signals indicating control and status for initiating and managing DMA operations. The code assumes a simplistic behavior model that does not fully cover corner cases or optimizations for efficiency and throughput, such as burst accesses and parallel data processing which might be required in a more robust implementation targeted for production environments.
// DMA 엔진 상태 머신 로직
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
state <= S_IDLE;
src_addr <= 32'b0;
dst_addr <= 32'b0;
cnt <= 16'b0;
wcnt <= 4'b0;
arvalid <= 1'b0;
rready <= 1'b0;
awvalid <= 1'b0;
wvalid <= 1'b0;
wlast <= 1'b0;
done <= 1'b1;
end else begin
state <= state_n;
src_addr <= src_addr_n;
dst_addr <= dst_addr_n;
cnt <= cnt_n;
wcnt <= wcnt_n;
// 제어 신호의 기본 값
arvalid <= 1'b0;
rready <= 1'b0;
awvalid <= 1'b0;
wvalid <= 1'b0;
wlast <= 1'b0;
done <= 1'b0;
case (state)
S_IDLE: begin
if (start_i && byte_len_i != 16'b0) begin
state_n <= S_RREQ;
src_addr_n <= src_addr_i;
dst_addr_n <= dst_addr_i;
cnt_n <= byte_len_i;
done <= 1'b0;
end else begin
done <= 1'b1;
end
end
S_RREQ: begin
arvalid <= 1'b1;
if (arready_i) begin
state_n <= S_RDATA;
end
end
S_RDATA: begin
rready <= 1'b1;
if (rvalid_i && rlast_i) begin
state_n <= S_WREQ;
src_addr_n <= src_addr + 32'd4; // 소스 주소 증가
wcnt_n <= cnt > 16 ? 4'hF : cnt[3:0] - 1; // 다음 버스트를 위한 wcnt 계산
cnt_n <= cnt - 32'd4; // 워드 크기인 4만큼 cnt 감소
end
end
S_WREQ: begin
awvalid <= 1'b1;
if (awready_i) begin
state_n <= S_WDATA;
end
end
S_WDATA: begin
wvalid <= 1'b1;
wlast <= wcnt == 4'd0; // wcnt가 0에 도달하면 wlast 설정
if (wready_i && wlast) begin
if (cnt == 32'd0) begin
state_n <= S_IDLE;
end else begin
state_n <= S_WREQ;
dst_addr_n <= dst_addr + 32'd4; // 다음 버스트를 위한 목적지 주소 증가
wcnt_n <= wcnt - 1'b1; // wcnt 감소
cnt_n <= cnt - 32'd4; // 워드 크기인 4만큼 cnt 감소
end
end
end
default: state_n <= S_IDLE;
endcase
end
end
이 코드는 상태 유한 기계(FSM)을 사용하여 직접 메모리 액세스(DMA) 컨트롤러의 엔진 로직을 구현합니다. FSM은 읽기 작업을 위한 ARREADY/ARVALID와 쓰기 작업을 위한 AWREADY/AWVALID의 가용성과 데이터 전송을 효과적으로 관리하기 위한 기타 조건에 따라 상태 간에 전환됩니다. 이 간단한 예시는 데이터 버퍼링, 실제 데이터 전송 처리, 오류 검사 및 중복 방지와 같은 포괄적 고려 사항과 AXI 프로토콜 준수에 초점을 맞추지 않습니다.
또한, start_i, byte_len_i, src_addr_i, dst_addr_i, arready_i, rvalid_i, rlast_i, awready_i, wready_i와 같은 신호는 DMA 작업을 시작하고 관리하기 위한 제어 및 상태를 나타내는 구성 레지스터 또는 인터페이스 신호에서 오는 것으로 가정합니다. 이 코드는 모서리 케이스 또는 효율성 및 처리량에 대한 최적화를 완전히 다루지 않는 단순화된 동작 모델을 가정합니다.