Verilog编程练习:
1、根据描述功能用verilog编写一段代码并用状态机来实现该功能
(1)状态机:实现一个测试过程,该过程包括启动准备状态、启动测试、停止测试、查询测试结果、显示测试结果、测试结束返回初始化6个状态;用时间来控制该过程,90秒内完成该过程;
(2)描述状态跳转时间
(3)编码实现
解答过程如下:
(1)状态机:
S0表示初始化状态(initial)
S1表示准备状态:准备20秒钟。
S2 表示测试启动状态(start):经过30秒测试完成。
S3表示测试停止状态(stop):表示测试结束。
S4表示测试查询状态:进行测试结果查询。
S5表示显示查询结果状态(display):显示测试结果。
(2)
C0表示timer = 1秒时跳到S1状态。
C1表示timer =41 秒时,进入S2状态。
C2 表示timer= 71秒,进入S3状态。
C3 表示timer = 72秒,进入S4状态。
C4 表示timer =73秒时,进入S5结果显示状态。
C5 表示timer = 74 秒,返回S0初始化状态。
(3)
reg[5:0] state;
wire[63:0] timer;
always @(posedge clk)
begin
if(reset)
state S0;
else
begin
case(state)
S0 ://inital
begin
if(timer == 1)
state = state;
else
state = S0;
end
S1 :// ready
begin
if(timer == 41)
state = S2;
else
state = state;
end
S2 ://start
begin
if(timer == 71)
state = S3;
else
state = state;
end
S3 ://stop
begin
if(timer == 72)
state = S4;
else
state = state;
end
S4 ://search
begin
if(timer == 73)
state = S5;
else
state = state;
end
S5 ://display
begin
if(timer == 74)
state = S0;
else
state = state;
end
default : state = S0;
end case
end
end
2、写同步、异步复位及置位D触发器的verilog module。(扬智电子笔试)
//带有异步清0、异步置1的D触发器模块描述
module D_trigger(clk,set_n,rst_n,D,Q);
input clk;
input set_n;
input rst_n;
input D;
output Q;
reg Q;
always @(posedge clk or negedge rst_n or negedge set_n)
begin
if(!rst_n)//异步清0 低有效
Q <= 1'b0;
else