Finite State Machines - Moore/Mealy Diagrams for Verilog

Discussion in 'Embedded Systems and Microcontrollers' started by stupidlogic, Nov 3, 2010.

  1. stupidlogic

    Thread Starter Member

    Aug 10, 2010
    39
    0
    I need to create a finite state machine in Verilog. It's kind of like a mod-10 counter, but with some variations.

    When,
    w1w0 = 00 the count remains the same
    w1w0 = 01 the count increases by one
    w1w0 = 10 the count increases by two
    w1w0 = 11 the count decreases by one

    And then obviously, I want to display the counter on a 7-seg display.

    Before I start to do the Verilog stuff, I want to make a state diagram that describes this machine. My textbook doesn't help me very much and I don't understand how to create transition, state, and state/output tables for the machine.

    Can anyone give me some suggestions on where to start or some good resources to learn more about it? I'm working on it now and will post what I have later this evening, but I'm struggling here. Thank you in advance.
     
  2. stupidlogic

    Thread Starter Member

    Aug 10, 2010
    39
    0
    Maybe it was easier than I thought. I think I have one worked out if anyone cares to comment about its correctness. And I haven't looked at the Verilog portion yet, but I'm sure I could use some pointers with that as this will pretty much be my first FSM in Verilog. Thanks for the help!

    [​IMG]
     
  3. stupidlogic

    Thread Starter Member

    Aug 10, 2010
    39
    0
    Here is my code and I'd like to hear some feedback. I haven't been able to test it on a board yet but does anyone see any problems with it? Thank you.

    Code ( (Unknown Language)):
    1. /*
    2.    Finite State Machine
    3.     - Four input types: Stays, counts up by one,
    4.       counts up by two, and counts down by one
    5.    */
    6.  
    7. module FSM_partIV (SW,KEY,HEX0);
    8.     input [2:0]SW;
    9.     input [0:0]KEY;
    10.     output [6:0]HEX0;
    11.  
    12.     parameter n = 4;
    13.  
    14.     reg [n-1:0]state_reg,next_state;
    15.  
    16.     wire Clk,Resetn;
    17.     wire [1:0]W;
    18.     wire [n-1:0]Z;
    19.  
    20.     assign Clk = KEY;
    21.     assign Resetn = SW[0];
    22.     assign W = SW[2:1];
    23.  
    24.  
    25. // state declaration
    26.     parameter zero = 4'b0000;
    27.     parameter one = 4'b0001;
    28.     parameter two = 4'b0010;
    29.     parameter three = 4'b0011;
    30.     parameter four = 4'b0100;
    31.     parameter five = 4'b0101;
    32.     parameter six = 4'b0110;
    33.     parameter seven = 4'b0111;
    34.     parameter eight = 4'b1000;
    35.     parameter nine = 4'b1001;
    36.  
    37.  
    38. // state register
    39.     always @(posedge Clk or nededge Resetn)
    40.         if(Resetn == 0)
    41.             state_reg <= zero;
    42.         else
    43.             state_reg <= next_state;
    44.  
    45.  
    46. // next state logic
    47.     always @(state_reg or W)
    48.         case (state_reg)
    49.             zero:
    50.                 if (W == 00)
    51.                     next_state = zero;
    52.                 else if (W == 01)
    53.                     next_state = one;
    54.                 else if (W == 10)
    55.                     next_state = two;
    56.                 else if (W == 11)
    57.                     next_state = nine;
    58.                 else
    59.                     next_state = zero;
    60.             one:
    61.                 if (W == 00)
    62.                     next_state = one;
    63.                 else if (W == 01)
    64.                     next_state = two;
    65.                 else if (W == 10)
    66.                     next_state = three;
    67.                 else if (W == 11)
    68.                     next_state = zero;
    69.                 else
    70.                     next_state = zero;
    71.             two:
    72.                 if (W == 00)
    73.                     next_state = two;
    74.                 else if (W == 01)
    75.                     next_state = three;
    76.                 else if (W == 10)
    77.                     next_state = four;
    78.                 else if (W == 11)
    79.                     next_state = one;
    80.                 else
    81.                     next_state = zero;
    82.             three:
    83.                 if (W == 00)
    84.                     next_state = three;
    85.                 else if (W == 01)
    86.                     next_state = four;
    87.                 else if (W == 10)
    88.                     next_state = five;
    89.                 else if (W == 11)
    90.                     next_state = two;
    91.                 else
    92.                     next_state = zero;
    93.             four:
    94.                 if (W == 00)
    95.                     next_state = four;
    96.                 else if (W == 01)
    97.                     next_state = five;
    98.                 else if (W == 10)
    99.                     next_state = six;
    100.                 else if (W == 11)
    101.                     next_state = three;
    102.                 else
    103.                     next_state = zero;
    104.             five:
    105.                 if (W == 00)
    106.                     next_state = five;
    107.                 else if (W == 01)
    108.                     next_state = six;
    109.                 else if (W == 10)
    110.                     next_state = seven;
    111.                 else if (W == 11)
    112.                     next_state = four;
    113.                 else
    114.                     next_state = zero;
    115.             six:
    116.                 if (W == 00)
    117.                     next_state = six;
    118.                 else if (W == 01)
    119.                     next_state = seven;
    120.                 else if (W == 10)
    121.                     next_state = eight;
    122.                 else if (W == 11)
    123.                     next_state = five;
    124.                 else
    125.                     next_state = zero;
    126.             seven:
    127.                 if (W == 00)
    128.                     next_state = seven:
    129.                 else if (W == 01)
    130.                     next_state = eight:
    131.                 else if (W == 10)
    132.                     next_state = nine:
    133.                 else if (W == 11)
    134.                     next_state = six;
    135.                 else
    136.                     next_state = zero;
    137.             eight:
    138.                 if (W == 00)
    139.                     next_state = eight;
    140.                 else if (W == 01)
    141.                     next_state = nine;
    142.                 else if (W == 10)
    143.                     next_state = zero;
    144.                 else if (W == 11)
    145.                     next_state = seven;
    146.                 else
    147.                     next_state = zero;
    148.             nine:
    149.                 if (W == 00)
    150.                     next_state = nine;
    151.                 else if (W == 01)
    152.                     next_state = zero;
    153.                 else if (W == 10)
    154.                     next_state = one;
    155.                 else if (W == 11)
    156.                     next_state = eight;
    157.                 else
    158.                     next_state = zero;
    159.             default:
    160.                 next_state = zero;
    161.         endcase
    162.  
    163.     assign Z = next_state;
    164.  
    165.  
    166. // implement 7-segment decoder
    167.     hex_7seg H0 (Z,HEX0);
    168.  
    169. endmodule
     
Loading...