[英]I have a Verilog code and the issue is that I need to C bit to go HIGH at the same time as B_G2 and that is done at the else statement
The issue is that I need C bit to go HIGH at the same time as B_G2 and that it is done at the else statement.问题是我需要 C 位到 go HIGH 与 B_G2 同时并在 else 语句中完成。
Here is the code that I have so far:这是我到目前为止的代码:
module lightcontrol(clk, R,C, A_CAR, B_CAR, lightsA, lightsB, cnt);
`define A_G2 3'b000
`define A_Y 3'b001
`define B_G1 3'b010
`define B_G2 3'b110
`define B_Y 3'b101
`define A_G1 3'b100
`define RED 3'b100
`define YELLOW 3'b010
`define GREEN 3'b001
input clk;
input A_CAR;
input B_CAR;
input R ;
output [2:0] lightsA;
output [2:0] lightsB;
output [3:0] cnt;
output C;
reg [3:0] cnt = 0;
reg [2:0] state;
reg C = 0;
always @(posedge clk)
begin
if (R==0 && C==1)
begin
if((state == `B_G2)&&(A_CAR == 1'b1))
state=`B_Y;
C=0;
end
else if ((R==1 &&C==0) | (R==1 && C==1))
state = `A_G2;
else
case(state)
`A_G2: state = B_CAR ? `A_Y : `A_G2;
`A_Y: state = `B_G1;
`B_G1:
begin
state = `B_G2;
C=1;
end
`B_G2: state = A_CAR ? `B_Y : `B_G2 ;
`B_Y: state = `A_G1 ;
`A_G1: state = `A_G2 ;
endcase
end
assign lightsA = ((state == `A_G2)|(state == `A_G1)) ? `GREEN: (state == `A_Y) ? `YELLOW : `RED;
assign lightsB = ((state == `B_G2)|(state == `B_G1)) ? `GREEN: (state == `B_Y) ? `YELLOW : `RED;
endmodule
You should use non-blocking assignments您应该使用非阻塞分配
Instead of initializing variables like you are doing with reg C = 0
you should use a reset.而不是像您使用
reg C = 0
那样初始化变量,您应该使用重置。 Other than this, In order to get the event you need to determine what are the conditions necessary to run that code.除此之外,为了获得事件,您需要确定运行该代码所需的条件。
Immediately you see that it requires C == 0
, R == 0
to run the case (to advance the state).您立即看到它需要
C == 0
, R == 0
来运行案例(推进状态)。 And the event you are interested requires state to be B_G1
, you can go to state == B_G1
from state == A_Y
, and you can reach to state == A_Y
if you are in state A_G2
and the input B_CAR
is set. And the event you are interested requires state to be
B_G1
, you can go to state == B_G1
from state == A_Y
, and you can reach to state == A_Y
if you are in state A_G2
and the input B_CAR
is set. Finally (initially) you go to state A_G2
when R == 1
.最后(最初)你 go 到 state
A_G2
当R == 1
时。 Maybe better to understand in a waveform.也许更好地理解波形。
(powered by wavedrom ) (由wavedrom提供支持)
{signal: [
{name: 'clk', wave: 'p....'},
{name: 'R', wave: '10...'},
{name: 'C', wave: 'x0..1'},
{name: 'state', wave: 'x2345', data:['A_G2', 'A_Y', 'B_G1', 'B_G2']},
{name: 'B_CAR', wave: 'x1xxx'},
]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.