集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 2377|回复: 7

ALtera推荐的两段式状态机的写法

[复制链接]
陈飞龙 发表于 2017-11-1 19:48:20 | 显示全部楼层 |阅读模式
Verilog-2001 State Machine
module verilog_fsm (clk, reset, in_1, in_2, out);
input clk, reset;
input [3:0] in_1, in_2;
output [4:0] out;
parameter state_0 = 3'b000;
parameter state_1 = 3'b001;
parameter state_2 = 3'b010;
parameter state_3 = 3'b011;
parameter state_4 = 3'b100;
reg [4:0] tmp_out_0, tmp_out_1, tmp_out_2;
reg [2:0] state, next_state;
always @ (posedge clk or posedge reset)
begin
if (reset)
state <= state_0;
else
state <= next_state;
end
always @ (*)
begin
tmp_out_0 = in_1 + in_2;
tmp_out_1 = in_1 - in_2;
case (state)
state_0: begin
tmp_out_2 = in_1 + 5'b00001;
next_state = state_1;
end
state_1: begin
if (in_1 < in_2) begin
next_state = state_2;
tmp_out_2 = tmp_out_0;
end
else begin
next_state = state_3;
tmp_out_2 = tmp_out_1;
end
end
state_2: begin
tmp_out_2 = tmp_out_0 - 5'b00001;
next_state = state_3;
end
state_3: begin
tmp_out_2 = tmp_out_1 + 5'b00001;
next_state = state_0;
end
state_4:begin
tmp_out_2 = in_2 + 5'b00001;
next_state = state_0;
end
default:begin
tmp_out_2 = 5'b00000;
next_state = state_0;
end
endcase
end
assign out = tmp_out_2;
endmodule
 楼主| 陈飞龙 发表于 2017-11-1 19:49:36 | 显示全部楼层
An equivalent implementation of this state machine can be achieved by using ‘define instead of
the parameter data type, as follows:
‘define state_0 3'b000
‘define state_1 3'b001
‘define state_2 3'b010
‘define state_3 3'b011
‘define state_4 3'b100
 楼主| 陈飞龙 发表于 2017-11-1 19:50:49 | 显示全部楼层
可以用宏定义的方式代替参数
zhangyukun 发表于 2017-11-2 09:28:35 | 显示全部楼层
ALtera推荐的两段式状态机的写法
芙蓉王 发表于 2017-11-2 09:31:13 | 显示全部楼层
ALtera推荐的两段式状态机的写法
 楼主| 陈飞龙 发表于 2017-11-2 18:35:01 | 显示全部楼层

迷之微笑!!!!!!!!!!!
zxopenljx 发表于 5 天前 | 显示全部楼层
ALtera推荐的两段式状态机的写法
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|手机版|Archiver|fpga论坛|fpga设计论坛 ( 京ICP备20003123号-1 )

GMT+8, 2025-1-26 15:16 , Processed in 0.069444 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表