时序逻辑代码与仿真
![图片[1] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客 图片[1] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客](https://pic.imgdb.cn/item/628b30cf094754312932c804.jpg)
代码
//计数器
`timescale 1ns/10ps
module counter(
clk,
res,
y
);
input clk;
input res;
output[7:0] y;
reg[7:0] y;
wire[7:0] sum;//+1运算的结果;
assign sum=y+1;//组合逻辑部分;
always@(posedge clk or negedge res)
if(~res)begin
y<=0;
end
else begin
y<=sum;
end
endmodule
//----testbench of counter----
module counter_tb;
reg clk,res;
wire[7:0] y;
counter counter(
.clk(clk),
.res(res),
.y(y)
);
initial begin
clk<=0;res<=0;
#17 res<=1;
#6000 $stop;
end
always #5 clk<=~clk;
endmodule现象![图片[2] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客 图片[2] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客](https://pic.imgdb.cn/item/628b3afe09475431294e478d.jpg)
模拟显示![图片[3] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客 图片[3] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客](https://pic.imgdb.cn/item/628b3bf509475431295152b4.jpg)
伪随机码发生器
![图片[4] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客 图片[4] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客](https://pic.imgdb.cn/item/628b7cfb0947543129ffb3e8.jpg)
代码
//四级伪随机码发生器;
`timescale 1ns/10ps
module m_gen(
clk,
res,
y
);
input clk;
input res;
output y;
reg[3:0] d;
assign y=d[0];
always@(posedge clk or negedge res)
if(~res)begin
d<=4'b1111;
end
else begin
d[2:0]<=d[3:1];//右移一位;
d[3]<=d[3]+d[0];//模二加;
end
endmodule
//----testbench of m_gen----
module m_gen_tb;
reg clk,res;
wire y;
m_gen m_gen(
.clk(clk),
.res(res),
.y(y)
);
initial begin
clk<=0;res<=0;
#17 res<=1;
#600 $stop;
end
always #5 clk<=~clk;
endmodule现象
![图片[5] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客 图片[5] - 【3】Verilog练习-时序逻辑与伪随机码发生器设计和仿真 - 我的学记|刘航宇的博客](https://pic.imgdb.cn/item/628b8d7409475431292e7827.jpg)