FPGA入门笔记003——计数器IP核调用与验证

发布时间 2023-11-20 09:15:51作者: Yamada_Ryo

FPGA设计方式主要有三种:

1、原理图(不推荐);

2、Verilog HDL设计方式;

3、IP核输入方式

计数器IP核调用与验证步骤如下:

1、添加IP核文件

打开Quartus II,新建一个项目,名称为counter_ip。

选择Tools->MegaWizard Plug-In Manager。

微信截图_20231119104141

选择第一个选项。

微信截图_20231119104345

在搜索栏中输入COUNTER,单击LPM_COUNTER。

微信截图_20231119104605

点击'...'按钮,选择prj文件夹中的ip文件夹,输入文件名为counter,并点击打开,最后点击next。

微信截图_20231119104813

接下来进入参数配置界面,设置计数器输出位数为4,并选择Up only(增计数模式),点击Next。

微信截图_20231119105248

选择Modulus,并设置最大计数值为10,再选择Carry-in和Carry-out,点击Next。

选项的具体含义如下:

Plain binary:直接计数模式。

Modulus:计数到一个最大值,再自动清零。

Clock Enable:时钟使能信号。

Count Enable:计数使能信号。

Carry-in:进位输入。

Carry-out:进位输出。

微信截图_20231119110217

点击Next。

微信截图_20231119110459

点击Next。

微信截图_20231119110539

点击Finish,至此ip核文件添加完成。

微信截图_20231119110611

2、生成counter.v文件

右键File,点击Add/Remove File in Project...。

微信截图_20231119110838

添加counter.v文件。

微信截图_20231119111137

点击Add,再点击Apply->OK。

微信截图_20231119111339

至此,counter.v文件已生成。

微信截图_20231119111611

3、测试counter.v文件

右键counter.v文件,点击设置为顶层,再点击设计与综合按钮进行测试。

微信截图_20231119111724

菜单栏中选择Tools->Netlist Viewers->RTL Viewer,查看RTL视图。

微信截图_20231119112041

RTL视图如下图所示:

微信截图_20231119112226

4、对计数器进行仿真测试

新建一个Verilog HDL文件,文件名称为counter_tb,并保存在testbench文件夹中。

设置代码如下:

`timescale 1ns/1ns

`define clock_period 20

module counter_tb;        

	reg cin;	//进位输入
	reg clk;	//计数基准时钟
	
	wire cout;	//进位输出
	wire [3:0] q;
	
	counter counter0(
		.cin(cin),
		.clock(clk),
		.cout(cout),
		.q(q)
	);
	
	initial clk = 1;
	always #(`clock_period / 2)clk = ~clk;
	
	//产生脉冲信号
	initial begin
		repeat(5) begin	//重复5次
			cin = 0;
			#(`clock_period * 5)cin = 1;	//低电平保持5个时钟周期
			#(`clock_period)cin = 0;	//高电平保持10个时钟周期
		end
		#(`clock_period * 200);
		$stop;
	end
	
endmodule

设置仿真链,并点击RTL Simulation按钮,结果如下图所示:

微信截图_20231119121438

5、更改IP核参数

选中IP Components,双击LPM_COUNTER。

微信截图_20231119121936

将模式更改为Plain binary(直接计数模式)。

微信截图_20231119122242

将计数器进行级联

1、设置八位级联计数器

通过两个4位计数器进行级联,生成一个8位的计数器。

原理图如下图所示:

微信图片_20231120082526

新建一个Verilog HDL文件,命名为counter_top。

代码如下:

module counter_top(
	cin,
	clk,
	cout,
	q
	);
	
	input cin;
	input clk;
	
	output cout;
	output [7:0]q;
	
	wire cout0;
	
	counter counter0(
		.cin(cin),
		.clock(clk),
		.cout(cout0),
		.q(q[3:0])
	);
	
	counter counter1(
		.cin(cin),
		.clock(clk),
		.cout(cout),
		.q(q[7:4])
	);

endmodule

将该文件保存在rtl文件夹中,设置为顶层文件,并进行分析与综合。

RTL视图如下图所示:

微信截图_20231120083205

2、对计数器进行仿真测试

新建Verilog HDL文件,命名位counter_top_tb,代码如下:

`timescale 1ns/1ns

`define clock_period 20

module counter_top_tb;

	reg cin;	//进位输入
	reg clk;	//计数基准时钟
	
	wire cout;	//进位输出
	wire [7:0] q;
	
	counter_top counter0(
		.cin(cin),
		.clk(clk),
		.cout(cout),
		.q(q)
	);
	
	initial clk = 1;
	always #(`clock_period / 2)clk = ~clk;
	
	//产生脉冲信号
	initial begin
        repeat(300) begin	//重复3次
			cin = 0;
			#(`clock_period * 5)cin = 1;	//低电平保持5个时钟周期
			#(`clock_period)cin = 0;	//高电平保持10个时钟周期
		end
		#(`clock_period * 200);
		$stop;
	end
	
endmodule

将该文件与仿真工具链接,点击RTL Simulation按钮进行时序仿真。