FPGA Prototyping By Verilog Examples第五章 状态机FSM设计

上升沿检测电路之Moore型FSM

复制代码
// Listing 5.3
module edge_detect_moore
(
input wire clk, reset,
input wire level,
output reg tick
);

// symbolic state declaration
localparam [1:0]
zero = 2'b00,
edg = 2'b01,
one = 2'b10;

// signal declaration
reg [1:0] state_reg, state_next;

// state register
always @(posedge clk, posedge reset)
if (reset)
state_reg <= zero;
else
state_reg <= state_next;

// next-state logic and output logic
always @*
begin
state_next = state_reg; // default state: the same
tick = 1'b0; // default output: 0
case (state_reg)
zero:
if (level)
state_next = edg;
edg:
begin
tick = 1'b1;
if (level)
state_next = one;
else
state_next = zero;
end
one:
if (~level)
state_next = zero;
default: state_next = zero;
endcase
end
endmodule
复制代码

上升沿检测电路之Mealy型FSM

复制代码
// Listing 5.4
module edge_detect_mealy
(
input wire clk, reset,
input wire level,
output reg tick
);

// symbolic state declaration
localparam zero = 1'b0,
one = 1'b1;

// signal declaration
reg state_reg, state_next;

// state register
always @(posedge clk, posedge reset)
if (reset)
state_reg <= zero;
else
state_reg <= state_next;

// next-state logic and output logic
always @*
begin
state_next = state_reg; // default state: the same
tick = 1'b0; // default output: 0
case (state_reg)
zero:
if (level)
begin
tick = 1'b1;
state_next = one;
end
one:
if (~level)
state_next = zero;
default: state_next = zero;
endcase
end

endmodule
复制代码

上升沿检测电路之基本门电路设计

复制代码
// Listing 5.5
module edge_detect_gate
(
input wire clk, reset,
input wire level,
output wire tick
);

// signal declaration
reg delay_reg;

// delay register
always @(posedge clk, posedge reset)
if (reset)
delay_reg <= 1'b0;
else
delay_reg <= level;

// decoding logic
assign tick = ~delay_reg & level;

endmodule
复制代码

按键消抖电路FSM

复制代码
// Listing 5.6
module db_fsm
(
input wire clk, reset,
input wire sw,
output reg db
);

// symbolic state declaration
localparam [2:0]
zero = 3'b000,
wait1_1 = 3'b001,
wait1_2 = 3'b010,
wait1_3 = 3'b011,
one = 3'b100,
wait0_1 = 3'b101,
wait0_2 = 3'b110,
wait0_3 = 3'b111;

// number of counter bits (2^N * 20ns = 10ms tick)
localparam N =19;

// signal declaration
reg [N-1:0] q_reg;
wire [N-1:0] q_next;
wire m_tick;
reg [2:0] state_reg, state_next;

// body

//=============================================
// counter to generate 10 ms tick
//=============================================
always @(posedge clk)
q_reg <= q_next;
// next-state logic
assign q_next = q_reg + 1;
// output tick
assign m_tick = (q_reg==0) ? 1'b1 : 1'b0;

//=============================================
// debouncing FSM
//=============================================
// state register
always @(posedge clk, posedge reset)
if (reset)
state_reg <= zero;
else
state_reg <= state_next;

// next-state logic and output logic
always @*
begin
state_next = state_reg; // default state: the same
db = 1'b0; // default output: 0
case (state_reg)
zero:
if (sw)
state_next = wait1_1;
wait1_1:
if (~sw)
state_next = zero;
else
if (m_tick)
state_next = wait1_2;
wait1_2:
if (~sw)
state_next = zero;
else
if (m_tick)
state_next = wait1_3;
wait1_3:
if (~sw)
state_next = zero;
else
if (m_tick)
state_next = one;
one:
begin
db = 1'b1;
if (~sw)
state_next = wait0_1;
end
wait0_1:
begin
db = 1'b1;
if (sw)
state_next = one;
else
if (m_tick)
state_next = wait0_2;
end
wait0_2:
begin
db = 1'b1;
if (sw)
state_next = one;
else
if (m_tick)
state_next = wait0_3;
end
wait0_3:
begin
db = 1'b1;
if (sw)
state_next = one;
else
if (m_tick)
state_next = zero;
end
default: state_next = zero;
endcase
end

endmodule
复制代码
路漫漫其修远兮,吾将上下而求索
转载自:http://www.cnblogs.com/qiweiwang/archive/2011/04/12/2013415.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值