verilog hdl state machine
[复制链接]
01 `include "timescale.v"
02
03 module module_name (
04 clk , rst ,
05 input_i ,
06 output_o ) ;
07
08 input clk ;
09 input rst ;
10 input input_i ;
11 output output_o ;
12
13 wire clk ;
14 wire rst ;
15 wire input_i ;
16 reg output_o ;
17 reg next_output ;
18
19 // diagram signals declarations
20 reg [ 2 : 0 ] i , next_i ;
21 reg [ 5 : 0 ] state_cs ;
22 reg [ 5 : 0 ] state_ns ;
23
24 //----------------------------------
25 // Next State Logic (combinatorial)
26 //----------------------------------
27 always @ ( input_i or i or state_cs )
28 begin : SM_NextState
29 // Set default values for outputs and signals
30 state_ns <= state_cs ;
31 next_i <= i ;
32 next_output <= output_o ;
33
34 case ( state_cs )
35 0 :
36 state_ns <= 1 ;
37
38 1 :
39 begin
40 next_output <= 1'b0 ;
41 state_ns <= 2 ;
42 end
43
44 2 :
45 if ( input_i = 1'b0 )
46 begin
47 state_ns <= 3 ;
48 next_output <= 1'b1 ;
49 end
50 endcase
51 end
52
53 //----------------------------------
54 // Current State Logic (sequential)
55 //----------------------------------
56 always @ ( posedge clk )
57 begin : SM_CurrentState
58 if ( rst )
59 state_cs <= 0 ;
60 else
61 state_cs <= state_ns ;
62 end
63
64 //----------------------------------
65 // Registered outputs logic
66 //----------------------------------
67 always @ ( posedge clk )
68 begin : SM_RegOutput
69 if ( rst )
70 begin
71 i <= 3'h0 ;
72 output_o <= 1'b0 ;
73 end
74 else
75 begin
76 i <= next_i ;
77 output_o <= next_output_o | input_i;
78 end
79 end
80
81 endmodule
|