集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 10534|回复: 29

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

[复制链接]
VVIC 发表于 2010-6-26 01:39:36 | 显示全部楼层 |阅读模式
本帖最后由 fpgaw 于 2010-11-18 16:10 编辑

借用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
interige 发表于 2010-6-26 01:42:37 | 显示全部楼层
很好呀!!!
longtim 发表于 2010-6-26 02:27:00 | 显示全部楼层
看来我还要好好看看才行啊.怎么就看不懂呢
AAT 发表于 2010-6-26 03:18:49 | 显示全部楼层
这种写法在这个程序中可能看不出优越性来,不过以后大家也许会在做设计的时候突然想起用这种方法更好。<br>
我说其中的一个有点就是只要用一个线网就可以代替一个状态,不需要去写(state = S2)这样的判断了。
inter 发表于 2010-6-26 03:59:22 | 显示全部楼层
今天才知道还有这种写法:<br>
always @(state)<br>
begin<br>
&nbsp; &nbsp; next_state = 8'b0000_0000;<br>
<br>
&nbsp; &nbsp; case(1'b1)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;state[`S1] : next_state[`S2] = 1'b1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;state[`S2] : next_state[`S3] = 1'b1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;state[`S3] : next_state[`S4] = 1'b1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;state[`S4] : next_state[`S5] = 1'b1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;state[`S5] : next_state[`S6] = 1'b1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;state[`S6] : next_state[`S7] = 1'b1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;state[`S7] : next_state[`S8] = 1'b1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;state[`S8] : next_state[`S1] = 1'b1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;default : next_state[`S1] = 1'b1;<br>
&nbsp; &nbsp; endcase<br>
end<br>
这样的prc可综合么.
interige 发表于 2010-6-26 05:10:39 | 显示全部楼层
呵呵 ,可读性好点,不过感觉和ISE提供的标准写法差不多呀,谢谢
interige 发表于 2010-6-26 05:46:48 | 显示全部楼层
有没有vhdl的
usd 发表于 2010-6-26 07:03:33 | 显示全部楼层
请教楼主:&ldquo;/既然是时钟发生器,最好不要用reset,否则复位将导致时钟中断,&rdquo;<br>
没有reset的时候,用这个语句&ldquo;always @(negedge clk)<br>
&nbsp; &nbsp; clk2 &lt;= ~clk2;<br>
<br>
always @(negedge clk)<br>
&nbsp; &nbsp; if (s_s1 | s_s2)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;alu_clk &lt;= ~alu_clk;<br>
<br>
always @(negedge clk)<br>
&nbsp; &nbsp; if (s_s2 | s_s4 | s_s6 | s_s8)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;clk4 &lt;= ~clk4;<br>
<br>
always @(negedge clk)<br>
&nbsp; &nbsp; if (s_s4 | s_s8)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;fetch &lt;= ~fetch;<br>
&rdquo;的话好象得不到时钟啊<br>
clk2,clk4....本来是高阻状态啊,我仿真时,初始化时不复位的话,他们不会产生时钟的。<br>
<br>
还请指教
VVC 发表于 2010-6-26 08:20:45 | 显示全部楼层
以前,有个synopsys的engineer很推崇这种写法,好像还写了一个paper,可是好处在哪里呢?
encounter 发表于 2010-6-26 08:58:09 | 显示全部楼层
很好阿,又学了一招
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

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

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

GMT+8, 2024-11-1 13:22 , Processed in 0.068826 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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