-2

I am trying to write a FSM in System Verilog that acts as a password detector. The password will be inputted using 4 buttons labeled b1, b2, b3, b4. The correct passcode is b1-b2-b3-b4. This is my logic and code for the first two states:

module FSM(
input b1, b2, b3, b4,
output logic unlock

typedef enum {STA, STB, STC, STD, STE} STATES; 
STATES NS, PS = STA;
always_ff@(posedge clock)
begin
   PS <=NS;
end
always_comb
begin
unlock = 0;
case(PS)

STA:
begin
if (b1 == 1'b1) // If button one is pressed
begin
NS = STB;
else:
NS = STA;

STB:
begin
   if (b2 == 1'b1) // If button two is pressed
    begin
     NS = STC;
     else if (b1 == 1 | b3 == 1 | b4 == 1) // if any other button is pressed go to stateA  
       begin
        NS = STA;
     else  // stay in state B until a button is pressed
       begin
        NS = STB;
       end

My FSM goes from STA to STB fine, but after inputting the correct button to go from STB to STC, the FSM goes back to STA. If I hold all the buttons down in the correct sequence at once, it will go to the last state. Could someone explain the error in my logic or if there is a better way to implement this?

toolic
  • 57,801
  • 17
  • 75
  • 117
  • your code has syntactic errors, so it cannot work at all. Besides that, why all this complication in the second stage? something like that should work: `STB: if (b2 == 1) NS = STC; else NS = STA;` similarly the last stage. – Serge Nov 30 '22 at 01:32
  • I wrote some pseudo code, I don't have syntax errors in my actual code. The logic you are suggesting will not work because as soon as you go from STA-->STB the else statement will instantly bring you back to STA. This is the logic I originally had, but it does not work. – user20638857 Nov 30 '22 at 01:34
  • also, please define what *"does not work"* mean? What exactly do you expect? – Serge Nov 30 '22 at 13:59

1 Answers1

-1

If your pseudo-code is right, then it should work even if you just keep all the buttons pushed. Your first case branch will first look at b1, ignoring all the other buttons, your second will first look at b2, ignoring or others etc; every time, the "correct" NS will be chosen, and the password will be accepted. Presumably, want your if condition for the first branch to be (b1 && !b2 && !b3 && !b4) -- and similar for the rest of the branches.

Further, you probably need a way to ensure that the fsm waits for a button to be unpressed, before looking for the next one. Otherwise, keeping, say, b1 pressed will give you STA->STB->STA.