fpga系列 HDL:全连接层InegrationFCpart.v的权重读取 $readmemh

发布于:2024-09-18 ⋅ 阅读:(57) ⋅ 点赞:(0)

$readmemh

语法

$readmemh("filename", memory_array);//  memory_array为 要存储数据的内存数组
  • 从包含十六进制数据的文件中读取数据,并将这些数据加载到内存中。示例如下:
reg [7:0] memory [0:255];  // 256字节的内存数组
initial begin
    $readmemh("data.hex", memory);
end
  • 地址顺序:数据用换行或空格符分隔,注释用"//"。件中的数据顺序会对应于内存数组中的地址顺序。文件的第一行数据将被加载到内存数组的第一个位置,第二行数据到第二个位置,依此类推。示例:
00 // 3个8bit16进制数
01
02
...

CODE

// https://github.com/omarelhedaby/CNN-FPGA/blob/master/Final%20Code%20Files/Part%205-%20Integration/weightMemory.v
module weightMemory(clk,address,weights);

parameter DATA_WIDTH = 32;
parameter INPUT_NODES = 100;
parameter OUTPUT_NODES = 32;
parameter file = "C:/Users/ahmed/Desktop/ANN/Weight Files/weights1_IEEE.txt";

localparam TOTAL_WEIGHT_SIZE = INPUT_NODES * OUTPUT_NODES;

input clk;
input [7:0] address;
output reg [DATA_WIDTH*OUTPUT_NODES-1:0] weights;

reg [DATA_WIDTH-1:0] memory [0:TOTAL_WEIGHT_SIZE-1];

integer i;

always @ (posedge clk) begin	
	if (address > INPUT_NODES-1 || address < 0) begin
		weights = 0;
	end else begin
		for (i = 0; i < OUTPUT_NODES; i = i + 1) begin
			weights[(OUTPUT_NODES-1-i)*DATA_WIDTH+:DATA_WIDTH] = memory[(address*OUTPUT_NODES)+i];
		end
	end
end

initial begin
	$readmemh(file,memory);
end

endmodule