打印

介绍状态机的一种书写方式

[复制链接]
1889|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wangjun88|  楼主 | 2010-8-20 10:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
介绍状态机的一种书写方式
借用zqadam的逻辑改的:
`define S1 0
`define S2 1
`define S3 2
`define S4 3
`define S5 4
`define S6 5
`define S7 6
`define S8 7
module clk_gen2 (clk,reset,clk1,clk2,clk4,fetch,alu_clk);
input clk,reset;
output clk1,clk2,clk4,fetch,alu_clk;
wire clk,reset;
reg clk2,clk4,fetch,alu_clk;
reg[7:0] state,next_state;
wire s_s1 = state[`S1];
wire s_s2 = state[`S2];
wire s_s3 = state[`S3];
wire s_s4 = state[`S4];
wire s_s5 = state[`S5];
wire s_s6 = state[`S6];
wire s_s7 = state[`S7];
wire s_s8 = state[`S8];
assign clk1 = ~clk;
//----------------------状态机-----------------
//状态机的时序逻辑
always @(negedge clk)
state <= next_state;
//状态机的组合逻辑(可能没有实际的组合电路),仅表示状态跳转,
//增强代码的可读性
//既然是时钟发生器,最好不要用reset,否则复位将导致时钟中断,
//特别时钟要输出给其它模块或其它游器件用的时候
always @(state)
begin
next_state = 8'b0000_0000;
case(1'b1)
state[`S1] : next_state[`S2] = 1'b1;
state[`S2] : next_state[`S3] = 1'b1;
state[`S3] : next_state[`S4] = 1'b1;
state[`S4] : next_state[`S5] = 1'b1;
state[`S5] : next_state[`S6] = 1'b1;
state[`S6] : next_state[`S7] = 1'b1;
state[`S7] : next_state[`S8] = 1'b1;
state[`S8] : next_state[`S1] = 1'b1;
default : next_state[`S1] = 1'b1;
endcase
end
//-----------------处理逻辑-------------------------
always @(negedge clk)
clk2 <= ~clk2;
always @(negedge clk)
if (s_s1 | s_s2)
alu_clk <= ~alu_clk;
always @(negedge clk)
if (s_s2 | s_s4 | s_s6 | s_s8)
clk4 <= ~clk4;
always @(negedge clk)
if (s_s4 | s_s8)
fetch <= ~fetch;
endmodule


相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

80

主题

251

帖子

5

粉丝