-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStateMachineExample.v
More file actions
87 lines (81 loc) · 3.02 KB
/
StateMachineExample.v
File metadata and controls
87 lines (81 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
**************************************************************************************
* File Name : StateMachineExample.v
* Date of create : 2015-04-11
* Author : Ian Jin
* : iancanada.mail@gmail.com
* : Twitter: @iancanadaTT
* Description : Good example of FPGA state macchine (FSM) in one always style
* with indexed states coding
* IEEE Standard Verilog Std 1364-2001
**************************************************************************************
* COPYRIGHT(c) Ian Jin All rights reserved
*/
module SourceSelection(input reset, //system reset
input clk, //system clock
input select, //selection jumper, high as default duo to internal weak pull-up
output reg selectionresult, //slection result after removing vibration
output reg selectionchanged //changed flag, 65536 clocks width indicating selection has just been changed
);
reg[15:0] counter; //count the mclk to run state machin at very low speed
//run 16bit counter,the max loop time for a 49.1520MHz clk is 65535/49.1520M=1.33ms
always@(posedge clk or negedge reset)
begin
if(!reset)
counter<=16'd0;
else
counter<=counter+16'd1;
end
//FSM in one always style with indexed states coding
reg[4:0] state;
parameter[4:0] BEGIN =0,
SELECT1 =1,
CHANGE1 =2,
SELECT0 =3,
CHANGE0 =4;
//FSM in one always style with indexed code
always@(posedge clk or negedge reset)
begin
if(!reset) begin
selectionresult<=0; //power up default is 0
selectionchanged<=0; //power up default is 0
state<=BEGIN;
end
else begin
if(counter==0) begin //run state machine just at moment of counter==0
case(state)
BEGIN: begin
selectionresult<=selectionresult; //keep selection result no change
selectionchanged<=1; //set to 1 in this state
state<=select? SELECT1:SELECT0; //next state will be decided by select input
end
SELECT1: begin
selectionresult<=1;
selectionchanged<=0;
state<=select? SELECT1:CHANGE1;
end
CHANGE1: begin
selectionresult<=1;
selectionchanged<=0;
state<=select? SELECT1:BEGIN;
end
SELECT0: begin
selectionresult<=0;
selectionchanged<=0;
state<=select? CHANGE0:SELECT0;
end
CHANGE0: begin
selectionresult<=0;
selectionchanged<=0;
state<=select? BEGIN:SELECT0;
end
endcase
end
else begin //keep everything no change for all other moment when counter!=0
state<=state;
selectionresult<=selectionresult;
selectionchanged<=selectionchanged;
end
end
end
endmodule