前几天在调试一个项目时想用SignaTap II查看状态机的状态,经过琢磨后发现了使用方法。保存与此,以便以后忘记了再来复习。
附上一个小的状态机测试程序,虽然代码不够完善,但也能达到示范目的。
1
module
statetest(
2 input [ 0 : 0 ] iKEY,
3 input [ 8 : 0 ] iSW,
4 input iCLK_50,
5 output reg [ 7 : 0 ] oLEDG);
6 // wire [7:0] oLEDG;
7 localparam IDLE = 0 ,
8 LED1 = 1 ,
9 LED2 = 2 ,
10 LED3 = 3 ,
11 LED4 = 4 ,
12 LED5 = 5 ,
13 LED6 = 6 ,
14 LED7 = 7 ,
15 LED8 = 8 ;
16
17 reg [ 7 : 0 ] state_reg,state_next;
18
19 always @( posedge iCLK_50, negedge iKEY[ 0 ])
20 if ( ! iKEY[ 0 ])
21 state_reg <= IDLE;
22 else
23 state_reg <= state_next;
24
25
26 always @ *
27 begin
28 state_next = state_reg;
29
30 case (state_reg)
31 IDLE:
32 begin
33 oLEDG = 8 ' b00000000;
34 if (iSW[ 0 ])
35 state_next = LED1;
36 else
37 state_next = IDLE;
38 end
39 LED1:
40 begin
41 oLEDG = 8 ' b00000001;
42 if (iSW[ 1 ])
43 state_next = LED2;
44 else
45 state_next = LED1;
46 end
47 LED2:
48 begin
49 oLEDG = 8 ' b00000010;
50 if (iSW[ 2 ])
51 state_next = LED3;
52 else
53 state_next = LED2;
54 end
55 LED3:
56 begin
57 oLEDG = 8 ' b00000100;
58 if (iSW[ 3 ])
59 state_next = LED4;
60 else
61 state_next = LED3;
62 end
63 LED4:
64 begin
65 oLEDG = 8 ' b00001000;
66 if (iSW[ 4 ])
67 state_next = LED5;
68 else
69 state_next = LED4;
70 end
71 LED5:
72 begin
73 oLEDG = 8 ' b00010000;
74 if (iSW[ 5 ])
75 state_next = LED6;
76 else
77 state_next = LED5;
78 end
79 LED6:
80 begin
81 oLEDG = 8 ' b00100000;
82 if (iSW[ 6 ])
83 state_next = LED7;
84 else
85 state_next = LED6;
86 end
87 LED7:
88 begin
89 oLEDG = 8 ' b01000000;
90 if (iSW[ 7 ])
91 state_next = LED8;
92 else
93 state_next = LED7;
94 end
95 LED8:
96 begin
97 oLEDG = 8 ' b10000000;
98 if (iSW[ 8 ])
99 state_next = IDLE;
100 else
101 state_next = LED8;
102 end
103 default :
104 state_next = IDLE;
105 endcase
106 end
107 endmodule
108
109
2 input [ 0 : 0 ] iKEY,
3 input [ 8 : 0 ] iSW,
4 input iCLK_50,
5 output reg [ 7 : 0 ] oLEDG);
6 // wire [7:0] oLEDG;
7 localparam IDLE = 0 ,
8 LED1 = 1 ,
9 LED2 = 2 ,
10 LED3 = 3 ,
11 LED4 = 4 ,
12 LED5 = 5 ,
13 LED6 = 6 ,
14 LED7 = 7 ,
15 LED8 = 8 ;
16
17 reg [ 7 : 0 ] state_reg,state_next;
18
19 always @( posedge iCLK_50, negedge iKEY[ 0 ])
20 if ( ! iKEY[ 0 ])
21 state_reg <= IDLE;
22 else
23 state_reg <= state_next;
24
25
26 always @ *
27 begin
28 state_next = state_reg;
29
30 case (state_reg)
31 IDLE:
32 begin
33 oLEDG = 8 ' b00000000;
34 if (iSW[ 0 ])
35 state_next = LED1;
36 else
37 state_next = IDLE;
38 end
39 LED1:
40 begin
41 oLEDG = 8 ' b00000001;
42 if (iSW[ 1 ])
43 state_next = LED2;
44 else
45 state_next = LED1;
46 end
47 LED2:
48 begin
49 oLEDG = 8 ' b00000010;
50 if (iSW[ 2 ])
51 state_next = LED3;
52 else
53 state_next = LED2;
54 end
55 LED3:
56 begin
57 oLEDG = 8 ' b00000100;
58 if (iSW[ 3 ])
59 state_next = LED4;
60 else
61 state_next = LED3;
62 end
63 LED4:
64 begin
65 oLEDG = 8 ' b00001000;
66 if (iSW[ 4 ])
67 state_next = LED5;
68 else
69 state_next = LED4;
70 end
71 LED5:
72 begin
73 oLEDG = 8 ' b00010000;
74 if (iSW[ 5 ])
75 state_next = LED6;
76 else
77 state_next = LED5;
78 end
79 LED6:
80 begin
81 oLEDG = 8 ' b00100000;
82 if (iSW[ 6 ])
83 state_next = LED7;
84 else
85 state_next = LED6;
86 end
87 LED7:
88 begin
89 oLEDG = 8 ' b01000000;
90 if (iSW[ 7 ])
91 state_next = LED8;
92 else
93 state_next = LED7;
94 end
95 LED8:
96 begin
97 oLEDG = 8 ' b10000000;
98 if (iSW[ 8 ])
99 state_next = IDLE;
100 else
101 state_next = LED8;
102 end
103 default :
104 state_next = IDLE;
105 endcase
106 end
107 endmodule
108
109