简体   繁体   English

我有一个 Verilog 代码,问题是我需要与 B_G2 同时将 C 位到 go HIGH 并在 else 语句中完成

[英]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 == 0R == 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_G2R == 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.

相关问题 我有幅度比较器 4 位 Verilog 代码,但输出错误 - I have magnitude comparator 4-bit Verilog code and I have wrong output 对于 verilog 上的加法器代码,如何将 1'b1 添加到 4 位向量 [3:0]A? - For an adder code on verilog, how do I add just 1'b1 to a 4 bit vector [3:0]A? 8 位 ALU 的大问题,程序不会停止,我只需要验证是否在 verilog 中达到了规范 - big issue with an 8-bit ALU, the program won't stop and I just need to verify that the specifications are reached in verilog 我正在尝试在 verilog Quartus 中为 8 位 2x1 MUX 编写代码,但代码中有错误 - am trying to make code in verilog Quartus for 8-bit 2x1 MUX but i have errors in the code 我可以找到Verilog代码的执行时间吗? - Can I find the execution time of verilog code? Verilog代码,相同的结构,相同的样式。 一个怎么起作用,而另一个却不起作用? 我哪里做错了? - Verilog code, same structure, same style. How come one works but the other doesnt? Where did I go wrong? 我需要生成如图所示的波形。 在Verilog代码中 - I need to generate waveform as shown. in verilog code 我在`上在Verilog上定义有错误 - I have error in `define on the Verilog with for 如何在Verilog中将1加到4位线 - How do I add 1 to a 4 bit wire in Verilog 尝试在 Verilog 代码的编译时/运行时更改包含语句 - Attempting to change include statement at compile time/run time for Verilog code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM