Module Overview
This document describes a Verilog hardware module designed for Xilinx FPGA platforms (ZYNQ7010, XCZU2CG, Kintex7). The module implements a state machine-based data transfer path between an external handshake memory interface and internal Block RAM (BRAM). It supports multiple frame configurations and includes comprehensive error detection capabilities.
Parameter Definitions
| Parameter |
Default |
Description |
| SIM_ENABLE |
0 |
Simulation mode bypass |
| FRAME_TRIG_WIDTH |
3 |
Frame trigger signal width |
| MEM_BASE_ADDR0 |
32'h0000_0000 |
DDR memory base address 0 |
| MEM_BASE_ADDR1 |
32'h0010_0000 |
DDR memory base address 1 |
| MEM_BASE_ADDR2 |
32'h0020_0000 |
DDR memory base address 2 |
| MEM_BASE_ADDR3 |
32'h0020_0000 |
DDR memory base address 3 |
| MEM_BASE_ADDR4 |
32'h0020_0000 |
DDR memory base address 4 |
| MEM_BASE_ADDR5 |
32'h0020_0000 |
DDR memory base address 5 |
| MEM_BASE_ADDR6 |
32'h0020_0000 |
DDR memory base address 6 |
| MEM_BASE_ADDR7 |
32'h0020_0000 |
DDR memory base address 7 |
| FRAME_COUNT_TOTAL |
720 |
Total number of frame transfers |
| HS_DATA_WIDTH |
32 |
Handshake data bus width |
| HS_ADDR_WIDTH |
32 |
Handshake address bus width |
| HS_BURST_SIZE |
2048 |
Handshake burst transfer size |
| WRITE_DELAY_CYCLES |
1000 |
Write operasion delay |
| BRAM_DATA_WIDTH |
32 |
BRAM data path width |
| BRAM_ADDR_WIDTH |
9 |
BRAM address depth |
| BRAM_BE_WIDTH |
1 |
BRAM byte enable width |
| ERR_STATUS_WIDTH |
4 |
Error status field width |
Interface Signals
System Control
| Signal |
Direction |
Width |
Description |
| sys_clk |
input |
1 |
System clock |
| sys_rst_n |
input |
1 |
Active-low reset |
| frame_trig_in |
input |
[FRAME_TRIG_WIDTH-1:0] |
Frame start trigger |
| bram_full_flag |
output |
1 |
BRAM write complete flag |
Error Status Output
| Bit |
Description |
| [0] |
Frame trigger received while not in IDLE state |
| [1] |
Invalid frame triggger encoding |
| [2] |
BRAM write overflow condition detected |
BRAM Master Interface
| Signal |
Direction |
Width |
Description |
| bram_addr |
output |
[BRAM_ADDR_WIDTH-1:0] |
BRAM write address |
| bram_clk |
output |
1 |
BRAM clock (connected to sys_clk) |
| bram_din |
output |
[BRAM_DATA_WIDTH-1:0] |
Data to BRAM |
| bram_dout |
input |
[BRAM_DATA_WIDTH-1:0] |
Data from BRAM (unused) |
| bram_en |
output |
1 |
BRAM enable |
| bram_rst |
output |
1 |
BRAM synchronous reset |
| bram_we |
output |
[BRAM_BE_WIDTH-1:0] |
BRAM write strobe |
Handshake Master Interface
| Signal |
Direction |
Width |
Description |
| hs_req_valid |
output |
1 |
Request valid |
| hs_req_sync |
output |
1 |
Request synchronization |
| hs_req_data |
output |
[HS_DATA_WIDTH-1:0] |
Request data payload |
| hs_req_addr |
output |
[HS_ADDR_WIDTH-1:0] |
Request memory address |
| hs_resp_ready |
input |
1 |
Slave ready indicator |
| hs_resp_sync |
input |
1 |
Response synchronization |
| hs_resp_data |
input |
[HS_DATA_WIDTH-1:0] |
Response data payload |
| hs_resp_addr |
input |
[HS_ADDR_WIDTH-1:0] |
Response address |
Internal Registers
| Register |
Type |
Description |
| frame_select |
reg [FRAME_TRIG_WIDTH-1:0] |
Selected frame configuration |
| base_addr |
wire [HS_DATA_WIDTH-1:0] |
Computed base address |
| bram_full_reg |
reg |
BRAM full flag register |
| bram_addr_reg |
reg [BRAM_ADDR_WIDTH-1:0] |
BRAM write address counter |
| bram_data_reg |
reg [BRAM_DATA_WIDTH-1:0] |
BRAM write data buffer |
| bram_enable_reg |
reg |
BRAM enable register |
| bram_reset_reg |
reg |
BRAM reset register |
| bram_we_reg |
reg [BRAM_BE_WIDTH-1:0] |
BRAM write enable register |
| hs_valid_reg |
reg |
Handshake request valid |
| hs_sync_reg |
reg |
Handshake sync signal |
| hs_data_reg |
reg [HS_DATA_WIDTH-1:0] |
Handshake data register |
| hs_addr_reg |
reg [HS_ADDR_WIDTH-1:0] |
Handshake address register |
| delay_counter |
reg [LOG2(WRITE_DELAY_CYCLES)-1:0] |
Write delay counter |
| current_state |
reg [3:0] |
FSM current state |
| error_status |
reg [ERR_STATUS_WIDTH-1:0] |
Error status register |
State Machine Definition
| State |
Value |
Description |
| IDLE |
0 |
Idle, waiting for frame trigger |
| START |
1 |
Initialize transfer |
| HS_REQUEST |
6 |
Issue memory read request |
| HS_WRITE |
2 |
Write received data to BRAM |
| WRITE_DELAY |
3 |
Insert delay between operations |
| CHECK_NEXT |
4 |
Evaluate if more transfers needed |
| TRANSFER_DONE |
5 |
Transfer complete, return to IDLE |
Helper Functions
function integer ceil_log2(input integer value);
begin
value = value - 1;
for (ceil_log2 = 0; value > 0; ceil_log2 = ceil_log2 + 1)
begin
value = value >> 1;
end
end
endfunction
Derived Parameters
localparam DELAY_CNT_WIDTH = ceil_log2(WRITE_DELAY_CYCLES);
localparam DATA_BYTE_COUNT = HS_DATA_WIDTH / 8;
localparam BRAM_BURST_WORDS = HS_BURST_SIZE / DATA_BYTE_COUNT;
Implementation Code
module ddr_to_bram #(
parameter SIM_ENABLE = 0,
parameter FRAME_TRIG_WIDTH = 3,
parameter MEM_BASE_ADDR0 = 32'h0000_0000,
parameter MEM_BASE_ADDR1 = 32'h0010_0000,
parameter MEM_BASE_ADDR2 = 32'h0020_0000,
parameter MEM_BASE_ADDR3 = 32'h0020_0000,
parameter MEM_BASE_ADDR4 = 32'h0020_0000,
parameter MEM_BASE_ADDR5 = 32'h0020_0000,
parameter MEM_BASE_ADDR6 = 32'h0020_0000,
parameter MEM_BASE_ADDR7 = 32'h0020_0000,
parameter FRAME_COUNT_TOTAL = 720,
parameter HS_DATA_WIDTH = 32,
parameter HS_ADDR_WIDTH = 32,
parameter HS_BURST_SIZE = 2048,
parameter WRITE_DELAY_CYCLES = 1000,
parameter BRAM_DATA_WIDTH = 32,
parameter BRAM_ADDR_WIDTH = 9,
parameter BRAM_BE_WIDTH = 1,
parameter ERR_STATUS_WIDTH = 4
)(
input sys_clk,
input sys_rst_n,
input [FRAME_TRIG_WIDTH-1:0] frame_trig_in,
output bram_full_flag,
output [BRAM_ADDR_WIDTH-1:0] bram_addr,
output bram_clk,
output [BRAM_DATA_WIDTH-1:0] bram_din,
input [BRAM_DATA_WIDTH-1:0] bram_dout,
output bram_en,
output bram_rst,
output [BRAM_BE_WIDTH-1:0] bram_we,
output hs_req_valid,
output hs_req_sync,
output [HS_DATA_WIDTH-1:0] hs_req_data,
output [HS_ADDR_WIDTH-1:0] hs_req_addr,
input hs_resp_ready,
input hs_resp_sync,
input [HS_DATA_WIDTH-1:0] hs_resp_data,
input [HS_ADDR_WIDTH-1:0] hs_resp_addr,
output [ERR_STATUS_WIDTH-1:0] error_status
);
Derived Parameter Calculation
localparam DELAY_CNT_WIDTH = ceil_log2(WRITE_DELAY_CYCLES);
localparam DATA_BYTE_COUNT = HS_DATA_WIDTH / 8;
localparam BRAM_BURST_WORDS = HS_BURST_SIZE / DATA_BYTE_COUNT;
localparam ST_IDLE = 0;
localparam ST_START = 1;
localparam ST_HS_REQ = 6;
localparam ST_HS_WR = 2;
localparam ST_DELAY = 3;
localparam ST_SWITCH = 4;
localparam ST_OVER = 5;
Frame Selection and Address Generation
reg [FRAME_TRIG_WIDTH-1:0] frame_select = 0;
wire [HS_DATA_WIDTH-1:0] base_addr;
reg bram_full_reg = 0;
assign bram_full_flag = bram_full_reg;
always @(posedge sys_clk)
begin
if (current_state == ST_IDLE)
begin
if (frame_trig_in != 0)
begin
case (frame_trig_in)
3'b001: frame_select <= 1;
3'b010: frame_select <= 2;
3'b100: frame_select <= 3;
default: frame_select <= 0;
endcase
end
else
begin
frame_select <= 0;
end
end
end
assign base_addr = (frame_select == 1) ? MEM_BASE_ADDR0 :
(frame_select == 2) ? MEM_BASE_ADDR1 :
(frame_select == 3) ? MEM_BASE_ADDR2 :
(frame_select == 4) ? MEM_BASE_ADDR3 :
(frame_select == 5) ? MEM_BASE_ADDR4 :
(frame_select == 6) ? MEM_BASE_ADDR5 :
(frame_select == 7) ? MEM_BASE_ADDR6 :
(frame_select == 8) ? MEM_BASE_ADDR7 :
32'h0;
BRAM Interface Control
reg [BRAM_ADDR_WIDTH-1:0] bram_addr_reg = 0;
reg [BRAM_DATA_WIDTH-1:0] bram_data_reg = 0;
reg bram_enable_reg = 0;
reg bram_reset_reg = 1;
reg [BRAM_BE_WIDTH-1:0] bram_we_reg = 0;
assign bram_addr = bram_addr_reg;
assign bram_clk = sys_clk;
assign bram_din = bram_data_reg;
assign bram_en = bram_enable_reg;
assign bram_rst = bram_reset_reg;
assign bram_we = bram_we_reg;
always @(posedge sys_clk)
begin
if (current_state == ST_IDLE || current_state == ST_HS_REQ)
begin
bram_addr_reg <= 0;
end
else if (current_state == ST_HS_WR)
begin
if (bram_we_reg[0])
begin
bram_addr_reg <= bram_addr_reg + 1;
end
end
end
always @(posedge sys_clk)
begin
if (current_state == ST_IDLE)
begin
bram_data_reg <= 0;
end
else if (current_state == ST_HS_WR)
begin
bram_data_reg <= hs_resp_data;
end
end
always @(posedge sys_clk)
begin
if (current_state == ST_IDLE || current_state == ST_DELAY)
begin
bram_enable_reg <= 0;
end
else if (current_state == ST_HS_REQ)
begin
bram_enable_reg <= 1;
end
end
always @(posedge sys_clk)
begin
if (current_state == ST_IDLE)
begin
bram_reset_reg <= 1;
end
else if (current_state == ST_HS_REQ)
begin
bram_reset_reg <= 0;
end
end
always @(posedge sys_clk)
begin
if (current_state == ST_IDLE)
begin
bram_we_reg <= 0;
end
else if (current_state == ST_HS_WR)
begin
bram_we_reg <= {BRAM_BE_WIDTH{hs_resp_sync}};
end
end
Handshake Interface Control
reg hs_valid_reg = 0;
reg hs_sync_reg = 0;
reg [HS_DATA_WIDTH-1:0] hs_data_reg = 0;
reg [HS_ADDR_WIDTH-1:0] hs_addr_reg = 0;
assign hs_req_valid = hs_valid_reg;
assign hs_req_sync = hs_sync_reg;
assign hs_req_data = hs_data_reg;
assign hs_req_addr = hs_addr_reg;
always @(posedge sys_clk)
begin
if (current_state == ST_IDLE || current_state == ST_HS_WR)
begin
hs_valid_reg <= 0;
end
else if (current_state == ST_HS_REQ)
begin
hs_valid_reg <= 1;
end
end
always @(posedge sys_clk)
begin
if (current_state == ST_START)
begin
hs_addr_reg <= base_addr;
end
else if (current_state == ST_SWITCH)
begin
hs_addr_reg <= hs_addr_reg + HS_BURST_SIZE;
end
end
Delay Counter
reg [DELAY_CNT_WIDTH-1:0] delay_counter = 0;
always @(posedge sys_clk)
begin
if (current_state == ST_IDLE || current_state == ST_HS_REQ)
begin
delay_counter <= 0;
end
else if (current_state == ST_DELAY)
begin
delay_counter <= delay_counter + 1;
end
end
always @(posedge sys_clk)
begin
if (current_state == ST_IDLE)
begin
bram_full_reg <= 0;
end
else if (current_state == ST_DELAY)
begin
bram_full_reg <= (delay_counter == 0) ? 1 : 0;
end
end
Main State Machine
reg [3:0] current_state = ST_IDLE;
always @(posedge sys_clk)
begin
if (!sys_rst_n)
begin
current_state <= ST_IDLE;
end
else
begin
case (current_state)
ST_IDLE:
begin
if (frame_trig_in != 0)
begin
current_state <= ST_START;
end
end
ST_START:
begin
if (frame_select == 0)
begin
current_state <= ST_OVER;
end
else
begin
current_state <= ST_HS_REQ;
end
end
ST_HS_REQ:
begin
current_state <= ST_HS_WR;
end
ST_HS_WR:
begin
if (hs_resp_ready)
begin
current_state <= ST_DELAY;
end
end
ST_DELAY:
begin
if (delay_counter >= WRITE_DELAY_CYCLES - 1)
begin
current_state <= ST_SWITCH;
end
end
ST_SWITCH:
begin
if (hs_addr_reg >= base_addr + HS_BURST_SIZE * (FRAME_COUNT_TOTAL - 1))
begin
current_state <= ST_OVER;
end
else
begin
current_state <= ST_HS_REQ;
end
end
ST_OVER:
begin
current_state <= ST_IDLE;
end
default:
begin
current_state <= ST_IDLE;
end
endcase
end
end
Error Status Generation
reg [ERR_STATUS_WIDTH-1:0] error_status = 0;
assign error_status = error_status;
always @(posedge sys_clk)
begin
if (!sys_rst_n)
begin
error_status <= 0;
end
else
begin
error_status[0] <= (frame_trig_in != 0) && (current_state != ST_IDLE);
error_status[1] <= (current_state == ST_START) && (frame_select == 0);
error_status[2] <= (current_state == ST_HS_WR) && (bram_addr_reg > BRAM_BURST_WORDS);
end
end
endmodule
Functional Description
The module operates as a bridge between external DDR memory accessed through a handshake protocol and internal FPGA Block RAM. Upon receiving a valid frame trigger, the state machine advances through initialization, memory request, data transfer, delay, and completion phases.
The frame selection logic decodes the incoming trigger to select one of eight possible base addresses in DDR memory. Each frame transfer reads a configurable burst of data (default 2048 bytes) and writes it sequentially to BRAM. The module supports multiple frame transfers (default 720) with configurable inter-frame delays.
Error detection monitors three conditions: premature frame triggers, invalid triggger encoding, and BRAM write overflow. These status bits are exposed through the error_status output for system-level monitoring.