当前位置:首页 > 北航verilog上机实验报告
Verilog上机实验报告
always @(posedge clk_in) begin
if (!reset)
clk_out=1; //原例题中此处为clk_out=0; else
clk_out=~clk_out; end endmodule
2. 测试模块
----------------------------------------文件名 half_clk_tb.v----------------------------------- `timescale 1ns/100ps `define clk_cycle 50 module top; reg clk,reset; wire clk_out;
always #`clk_cycle clk=~clk; initial begin
clk=0; reset=1; #10 reset=0; #110 reset=1; #100000 $stop; end
half_clk m0(.reset(reset),.clk_in(clk),.clk_out(clk_out)); endmodule
六、 仿真波形
七、 总结及对波形的说明
1. 实验结论
从波形中可以看出当clk_out的周期为clk_in的两倍,并且在t=300ns时,为clk_out的下降沿,恰好和例题中的输出信号波形反相,满足题目的要求。
9
Verilog上机实验报告
2. 实验思考(课本P318思考题二)
1) 如果没有reset信号,可以通过在测试文件中的initial块中设置clk_in的初值为
1来使输出反相。
2) 只用clk时钟沿的出发可以通过一个计数器来产生其他分频的时钟,并且通过调
节计数器的值来产生不同占空比的分频时钟,其代码如下: i. 产生任意分频的时钟
----------------------------主程序div.v ----------------------------------------- module div(rst,clk,out,num); input rst, clk;
input [3:0] num; //通过num调节分频数 output out; reg out; reg [3:0] i;
always @(posedge clk) begin if(!rst) begin i<=0; out<=0; end else begin
if (i==((num[3:1])-1)) //num[3:1]代替num/2 begin i<=0;
out=~out; end else begin i<=i+1; out=out; end end end
endmodule
----------------------------测试文件div_tb.v ----------------------------------------- `timescale 1ns/100ps `define clk_cycle 50 module t; reg clk,rst; wire out;
reg [3:0] num;
always #`clk_cycle clk=~clk;
10
Verilog上机实验报告
ii.
initial begin
num=4; clk=0; rst=1; #10 rst=0; #110 rst=1;
#100000 $stop; end
div m(.rst(rst),.clk(clk),.out(out),.num(num)); endmodule
产生占空比不同的分频时钟
----------------------------主程序div_ex.v ----------------------------------------- module div_ex(rst,clk,out,top,down); input rst, clk;
input [3:0] top,down; //通过top,down调节占空比 output out; reg out; reg [3:0] i;
always @(posedge clk) begin if(!rst) begin i<=0; out<=0; end else begin
if (out==1) begin
if (i==(top-1)) begin i<=0;
out=~out; end else begin
i<=i+1; out=out; end end
11
Verilog上机实验报告
else begin
if (i==(down-1)) begin i<=0;
out=~out; end else begin
i<=i+1; out=out; end end end end
endmodule
----------------------------测试文件div_ex_tb.v -----------------------------------------
`timescale 1ns/100ps `define clk_cycle 50 module t; reg clk,rst; wire out;
reg [3:0] top,down;
always #`clk_cycle clk=~clk; initial begin
top=2; down=4; clk=0; rst=1; #10 rst=0; #110 rst=1;
#100000 $stop; end
div_ex m(.rst(rst),.clk(clk),.out(out),.top(top),.down(down)); endmodule
3. 实验总结
12
共分享92篇相关文档