实现根据时钟来自增计数器

This commit is contained in:
ranchuan
2024-12-30 16:19:10 +08:00
commit dcb6b4b7ec
5 changed files with 112 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
wave
wave.vcd

5
ReadMe.txt Normal file
View File

@@ -0,0 +1,5 @@
2024.12.30
实现一个改变时钟电平的基本电路
每个时钟周期计数器加1

27
main_module.v Normal file
View File

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

27
make.py Normal file
View File

@@ -0,0 +1,27 @@
import os
import sys
TARGET="wave"
SRC = ["testbench.v"]
INC = []
IVER="iverilog"
VVP="vvp"
GTKWAVE="gtkwave"
def make():
cmd=' '.join([IVER,'-o',TARGET]+SRC)
if(os.system(cmd)):
sys.exit(-1)
cmd=' '.join([VVP,'-n',TARGET,'lxt2'])
if(os.system(cmd)):
sys.exit(-1)
cmd=' '.join([GTKWAVE,TARGET+'.vcd'])
if(os.system(cmd)):
sys.exit(-1)
if __name__ == "__main__":
make()

51
testbench.v Normal file
View File

@@ -0,0 +1,51 @@
// 时间尺度 以1ns为时钟单位 / 1ns为时钟精度
`timescale 1ns/1ns
// 包含文件 main_module.v 编译的时候就不需要指定这个文件了
`include "main_module.v"
// 定义模块 此模块没有输入输出
module testbench();
reg clk;
reg rst;
wire dout;
wire [31:0] sum;
// 指定参数
parameter CYCLE = 2;
parameter END_TIME = 200;
// 实例化模块
test mod(
.clk(clk),
.rst(rst),
.dout(dout),
.sum(sum)
);
// 初始化块 此块用于生成波形文件
// initial 语句为仿真语句通常在testbench中使用
initial begin
$dumpfile("wave.vcd");
$dumpvars(0,testbench);
end
// 寄存器初始化只有初始化之后才会产生波形
initial begin
clk = 0;
rst = 0;
end
initial begin
#5 rst = 1;
end
// 每隔一个时钟周期取反
always begin
#(CYCLE / 2) clk = ~clk;
end
// 在END_TIME个时钟周期之后结束
initial begin
#END_TIME;
$stop;
end
endmodule