读取外部文件

This commit is contained in:
ranchuan
2024-12-30 19:32:43 +08:00
parent dcb6b4b7ec
commit 067f637ab8
3 changed files with 27 additions and 2 deletions

1
flash_data.txt Normal file
View File

@@ -0,0 +1 @@
01 02 03 04 05 06 07 08 09 10

View File

@@ -5,11 +5,13 @@
module test ( module test (
input clk, input clk,
input rst, input rst,
input [7:0] data,
output dout, output dout,
output reg [7:0] data_addr,
// 声明变量 如果不指定变量类型 则默认是wire类型 // 声明变量 如果不指定变量类型 则默认是wire类型
output reg [31:0] sum output reg [31:0] sum
); );
reg [31:0] count;
assign dout = ~clk; assign dout = ~clk;
@@ -18,9 +20,13 @@ module test (
always @(posedge clk or rst) begin always @(posedge clk or rst) begin
if(rst==0) begin if(rst==0) begin
sum <= 0; sum <= 0;
count <= 0;
data_addr <=0;
end end
else begin else begin
sum <= sum+1; count <= count+1;
sum <= data*2;
data_addr <= count[7:0];
end end
end end

View File

@@ -7,8 +7,12 @@
module testbench(); module testbench();
reg clk; reg clk;
reg rst; reg rst;
reg [7:0] data;
wire dout; wire dout;
wire [31:0] sum; wire [31:0] sum;
wire [7:0] data_addr;
// 定义一个存储空间 8位 256个字节
reg [7:0] flash_mem[255:0];
// 指定参数 // 指定参数
parameter CYCLE = 2; parameter CYCLE = 2;
parameter END_TIME = 200; parameter END_TIME = 200;
@@ -17,7 +21,9 @@ module testbench();
test mod( test mod(
.clk(clk), .clk(clk),
.rst(rst), .rst(rst),
.data(data),
.dout(dout), .dout(dout),
.data_addr(data_addr),
.sum(sum) .sum(sum)
); );
@@ -26,8 +32,15 @@ module testbench();
initial begin initial begin
$dumpfile("wave.vcd"); $dumpfile("wave.vcd");
$dumpvars(0,testbench); $dumpvars(0,testbench);
// 把文件中的16进制数据填充到mem
$readmemh("flash_data.txt",flash_mem);
end end
//显示数组的10个值
// initial begin
// $display("yyyyy %d: %h", 0, flash_mem[0]);
// end
// 寄存器初始化只有初始化之后才会产生波形 // 寄存器初始化只有初始化之后才会产生波形
initial begin initial begin
clk = 0; clk = 0;
@@ -42,6 +55,11 @@ module testbench();
#(CYCLE / 2) clk = ~clk; #(CYCLE / 2) clk = ~clk;
end end
always @(posedge clk) begin
data <= flash_mem[data_addr];
$display("yyyyy %d: %h", data_addr, flash_mem[data_addr]);
end
// 在END_TIME个时钟周期之后结束 // 在END_TIME个时钟周期之后结束
initial begin initial begin
#END_TIME; #END_TIME;