夏宇闻老师书第12章仿真测试模块的问题
<p>代码:</p><pre>
<code>module fsm(Clock,Reset,A,K2,K1);
input Clock,Reset,A;
output K2,K1;
reg K2,K1;
reg state,nextstate;
reg A1; //键值锁存寄存器,每20ms检测一次键值
parameter
Idle=3'b000,
Start=3'b001,
Stop=3'b010,
Ccb=3'b011,
Clear=3'b100;
reg cnt_20ms; //20ms计数寄存器
always @ (posedge Clock or negedge Reset)
if(!Reset) cnt_20ms <= 20'd0;
else cnt_20ms <= cnt_20ms+1'b1; //不断计数
always @ (posedge Clock or negedge Reset)
if(!Reset) begin
A1 <= 1'b1; //键值寄存器复位,没有键盘按下时键值都为1
end
else if(cnt_20ms == 20'hfffff) begin
A1 <= A; //按键1值锁存
end
always @(posedge Clock)
if(!Reset)
state<=Idle;
else
state<=nextstate;
always @(state or A1)
case(state)
Idle:
if(!A1)
nextstate=Start;
else
nextstate=Idle;
Start:
if(A1)
nextstate=Stop;
else
nextstate=Start;
Stop:
if(!A1)
nextstate=Ccb;
else
nextstate=Stop;
Ccb:
if(A1)
nextstate=Clear;
else
nextstate=Ccb;
Clear:
if(!A1)
nextstate=Idle;
else
nextstate=Clear;
default:
nextstate=3'bxxx;
endcase
always @(state or Reset or A1)
if(!Reset)
K1=0;
else
if(state == Clear && !A1)
K1=1;
else
K1=0;
always @(state or Reset or A1)
if(!Reset)
K2=0;
else
if(state == Stop && A1)
K2=1;
else
K2=0;
endmodule
</code></pre>
<p>现象是按下按键,一个灯反转,再按一次按键,另一个灯翻转,循环往复。</p>
<p>夏老师给了一个仿真测试模块:</p>
<pre>
<code>`timescale 1 ns/ 1 ps
module t;
// constants
// general purpose registers
//reg eachvec;
// test vector input registers
reg A;
reg Clock;
reg Reset;
// wires
wire K1;
wire K2;
// assign statements (if any)
initial
begin
// code that executes only once
// insert code here --> begin
A=0;
Reset=1;
Clock=0;
#22Reset=0;
#100033 Reset=1;
// --> end
end
always #50 Clock=~Clock;
always @(posedge Clock)
begin
#30 A={$random}%2;
#(3*50+12);
end
initial
begin #100000 $stop; end
fsm i1 (
// port map - connection between master ports and signals/registers
.Clock(Clock),
.Reset(Reset),
.A(A),
.K2(K2),
.K1(K1)
);
endmodule
</code></pre>
<p>我仿真了一下,代表按键的那个曲线有变化,可是代表灯的两个曲线一直是低电平。</p>
<p>请问高手,哪里有错?谢谢!</p>
<p>代表按键的那个曲线有变化,软件上的仿真?</p>
Jacktang 发表于 2023-3-31 07:48
代表按键的那个曲线有变化,软件上的仿真?
<p>是的</p>
页:
[1]