实现mem

This commit is contained in:
ranchuan
2025-03-11 17:54:18 +08:00
parent 067f637ab8
commit 6efb3b0473
5 changed files with 108 additions and 29 deletions

View File

@@ -2,4 +2,6 @@
2024.12.30 2024.12.30
实现一个改变时钟电平的基本电路 实现一个改变时钟电平的基本电路
每个时钟周期计数器加1 每个时钟周期计数器加1
2025.3.11
实现内存模块

15
make.py
View File

@@ -6,14 +6,25 @@ TARGET="wave"
SRC = ["testbench.v"] SRC = ["testbench.v"]
INC = [] INC = ['verilog']
IVER="iverilog" IVER="iverilog"
VVP="vvp" VVP="vvp"
GTKWAVE="gtkwave" GTKWAVE="gtkwave"
for i in range(len(INC)):
tmp=os.path.join(os.path.curdir,INC[i])
tmp=os.path.abspath(tmp).replace('/','\\')
INC[i]='-I '+tmp
for i in range(len(SRC)):
tmp=os.path.join(os.path.curdir,SRC[i])
tmp=os.path.abspath(tmp).replace('/','\\')
SRC[i]=tmp
def make(): def make():
cmd=' '.join([IVER,'-o',TARGET]+SRC) cmd=' '.join([IVER,'-o',TARGET]+INC+SRC)
if(os.system(cmd)): if(os.system(cmd)):
sys.exit(-1) sys.exit(-1)
cmd=' '.join([VVP,'-n',TARGET,'lxt2']) cmd=' '.join([VVP,'-n',TARGET,'lxt2'])

View File

@@ -2,44 +2,44 @@
// 时间尺度 以1ns为时钟单位 / 1ns为时钟精度 // 时间尺度 以1ns为时钟单位 / 1ns为时钟精度
`timescale 1ns/1ns `timescale 1ns/1ns
// 包含文件 main_module.v 编译的时候就不需要指定这个文件了 // 包含文件 main_module.v 编译的时候就不需要指定这个文件了
`include "main_module.v" `include "counter.v"
`include "mem.v"
// 定义模块 此模块没有输入输出 // 定义模块 此模块没有输入输出
module testbench(); module testbench();
// 输入给模块的变量用reg
reg clk; reg clk;
reg rst; reg rst;
reg [7:0] data; reg [7:0] data;
wire dout;
// 接收模块的输出用wire
wire [31:0] sum; wire [31:0] sum;
wire [7:0] data_addr; wire [7:0] read_data;
// 定义一个存储空间 8位 256个字节
reg [7:0] flash_mem[255:0];
// 指定参数 // 指定参数
parameter CYCLE = 2; parameter CYCLE = 2;
parameter END_TIME = 200; parameter END_TIME = 200;
// 实例化模块 // 实例化模块
test mod( counter counter_mod(
.clk(clk), .clk(clk),
.rst(rst), .rst(rst),
.data(data), .count(sum)
.dout(dout),
.data_addr(data_addr),
.sum(sum)
); );
// 初始化块 此块用于生成波形文件 mem mem_mod(
// initial 语句为仿真语句通常在testbench中使用 .clk(clk),
initial begin .rst(rst),
$dumpfile("wave.vcd"); .write_en(1'b1),
$dumpvars(0,testbench); .read_en(1'b0),
// 把文件中的16进制数据填充到mem .data_in(data),
$readmemh("flash_data.txt",flash_mem); .addr(sum[7:0]),
end .data_out(read_data)
);
//显示数组的10个值 always @(posedge clk) begin
// initial begin data <= sum[7:0];
// $display("yyyyy %d: %h", 0, flash_mem[0]); end
// end
// 寄存器初始化只有初始化之后才会产生波形 // 寄存器初始化只有初始化之后才会产生波形
initial begin initial begin
@@ -47,7 +47,7 @@ module testbench();
rst = 0; rst = 0;
end end
initial begin initial begin
#5 rst = 1; #1 rst = 1;
end end
// 每隔一个时钟周期取反 // 每隔一个时钟周期取反
@@ -55,10 +55,10 @@ module testbench();
#(CYCLE / 2) clk = ~clk; #(CYCLE / 2) clk = ~clk;
end end
// 仿真语句 打印变量值
always @(posedge clk) begin always @(posedge clk) begin
data <= flash_mem[data_addr]; $display("yyyyy %d: %h", sum, read_data);
$display("yyyyy %d: %h", data_addr, flash_mem[data_addr]); end
end
// 在END_TIME个时钟周期之后结束 // 在END_TIME个时钟周期之后结束
initial begin initial begin

23
verilog/counter.v Normal file
View File

@@ -0,0 +1,23 @@
module counter (
input clk,
input rst,
// 声明变量 如果不指定变量类型 则默认是wire类型
output reg [31:0] count
);
// 在时钟的上升沿开始计算
// 对时钟上升沿或rst电平敏感
always @(posedge clk or rst) begin
if(rst==0) begin
count <= 0;
end
else begin
count <= count+1;
end
end
endmodule

43
verilog/mem.v Normal file
View File

@@ -0,0 +1,43 @@
module mem(
input clk,
input rst,
input write_en,
input read_en,
input [7:0] data_in,
input [7:0] addr,
output reg [7:0] data_out
);
reg [7:0] mem_array[255:0];
// 初始化块 此块用于生成波形文件
// initial 语句为仿真语句通常在testbench中使用
initial begin
$dumpfile("wave.vcd");
$dumpvars(0,testbench);
// 把文件中的16进制数据填充到mem
$readmemh("flash_data.txt",mem_array);
end
always @(posedge clk or rst) begin
if(rst==0) begin
data_out <= 0;
end
else begin
if(write_en) begin
mem_array[addr] <= data_in;
end
else if(read_en) begin
data_out <= mem_array[addr];
end
end
end
endmodule