网站首页
IC库存
IC展台
电子资讯
技术资料
PDF文档
我的博客
IC72论坛
ic72 logo
资料首页最新产品 技术参数 电路图 设计应用 解决方案 代理商查询 IC替换 IC厂商 电子辞典
关键字: 技术文章 PDF资料 IC价格 电路图 代理商查询 IC替换 IC厂商 电子辞典

状态机举例

你可以指定状态寄存器和状态机的状态。以下是一个有四种状态的普通状态机。

// These are the symbolic names for states
// 定义状态的符号名称
parameter [1:0]
S0 = 2'h0,
S1 = 2'h1,
S2 = 2'h2,
S3 = 2'h3;

// These are the current state and next state variables
// 定义当前状态和下一状态变量
reg [1:0] state;
reg [1:0] next_state;


// state_vector state
// 状态向量的转移关系
always @ (state or y or x)
begin
next_state = state;
case (state)
S0: begin
if (x) begin
next_state = S1;
end
else begin
next_state = S2;
end
end
S1: begin
if (y) begin
next_state = S2;
end
else begin
next_state = S0;
end
end
S2: begin
if (x & y) begin
next_state = S3;
end
else begin
next_state = S0;
end
end
S3: begin
next_state = S0;
end
endcase
end


always @ (posedge clk or posedge reset)
begin
if (reset) begin
state <= S0;
end
else begin
state <= next_state;
end
end


  同样的状态机也可以用下面的代码以“One hot”编码方式实现。
// These are the symbolic names for states
// 定义状态的符号名称
parameter [1:0]
S0 = 2'h0,
S1 = 2'h1,
S2 = 2'h2,
S3 = 2'h3;

parameter [3:0]
s0 = 4'h1,
s1 = 4'h2,
s2 = 4'h4,
s3 = 4'h8;

// These are the current state and next state variables
// 定义当前状态和下一状态变量
reg [3:0] state;
reg [3:0] next_state;


// state_vector state
// 状态向量的转移关系
always @ (state or y or x)
begin
next_state = state;
case (1)
state[S0]: begin
if (x) begin
next_state = 1 << S1;
end
else begin
next_state = 1 << S2;
end
end
state[S1]: begin
if (y) begin
next_state = 1 << S2;
end
else begin
next_state = 1 << S0;
end
end
state[S2]: begin
if (x & y) begin
next_state = 1 << S3;
end
else begin
next_state = 1 << S0;
end
end
state[S3]: begin
next_state = 1 << S0;
end
endcase
end


always @ (posedge clk or posedge reset)
begin
if (reset) begin
state <= 1 << S0;
end
else begin
state <= next_state;
end
end

热门搜索:PS2408 602-15 BT152-500R/600R TLP606B 6SPDX PDU12IEC TLM825GF 02M1001JF 02T1001JF BT137S-500E PS-415-HG-OEM B3429D SBB830 PS-415-HG PS120406 LS606M CC2544RHBR 2839570 PDU2430 ULTRABLOK B30-7100-PCB LED12-C2 01C1001JF TLP712 2920120
COPYRIGHT:(1998-2010) IC72 达普IC芯片交易网
客户服务:service@IC72.com 库存上载:IC72@IC72.com
(北京)联系方式: 在线QQ咨询:点击这里给我发消息 联系电话:010-82614113 传真:010-82614123
京ICP备06008810号-21 京公网安备 11010802032910 号 企业资质