有时拿到别人的一份代码,真是又长又臭,都不知道该从哪个地方看起,特别是有些状态机很长很长。其实想看懂状态机,个人觉得比较好的方法就是把状态机画出来,至于怎么画呢,这里有两个方法,第一:手画,一边看着代码一边画,这样做有一个好处,思路比较清晰,因为是自己画出来的。第二:用quartus ii画,这个方法的好处就是快,狠,准。第一个方法大家都会,下面就说说第二个方法吧。
第一,编译并综合。
第二,tools -> netlist viewers -> RTL viewer
第三,找到状态机,双击。
第四,完成。

Quartus II中对状态机的设计有一些特别的规定,如果不了解这些规定在设计的时候常常使人产生困惑。
对于Verilog HDL这些规定是:
1.The Always Construct that describes the state machine is clocked.
2.All assignments to the signal or variable that represents the state machine are within the Always Construct.
3.The state machine has more than two states.
4.The state register is not used as an output of the module.
5.The state transition logic does not use arithmetic operators. For example, this condition is not met if the state transition logic uses the expression next_state <= state + 1;, which contains the arithmetic operator +.
其中第5.条是一条很强的约束,从各种程序测试结果来看,状态变量(state和next_state)除了可以用于'case (state)'语句以及条件判别'(state==s1)'和状态参数赋值'next_state=s1'以外,不能用于其它的操作,这就是为什么常常不能综合成状态机的原因。我们可以这样来判断:将状态变量(state和next_state)和状态参数(s1,s2...)看成一些特殊的变量或者标识符,可以用状态参数或者状态变量对另外的状态变量赋值或者判别它们是否相等,但是它们不是'数',一旦将它们看成'数',例如state=s1+1或者state[s1]=1'b1(等数学操作)就不能综合成状态机。因此传统one-hot编码程序风格(位索引方式):
case (1'b1) begin
state[s1]:
...
end
不能综合成状态机。
对于Verilog HDL这些规定是:
1.The Always Construct that describes the state machine is clocked.
2.All assignments to the signal or variable that represents the state machine are within the Always Construct.
3.The state machine has more than two states.
4.The state register is not used as an output of the module.
5.The state transition logic does not use arithmetic operators. For example, this condition is not met if the state transition logic uses the expression next_state <= state + 1;, which contains the arithmetic operator +.
其中第5.条是一条很强的约束,从各种程序测试结果来看,状态变量(state和next_state)除了可以用于'case (state)'语句以及条件判别'(state==s1)'和状态参数赋值'next_state=s1'以外,不能用于其它的操作,这就是为什么常常不能综合成状态机的原因。我们可以这样来判断:将状态变量(state和next_state)和状态参数(s1,s2...)看成一些特殊的变量或者标识符,可以用状态参数或者状态变量对另外的状态变量赋值或者判别它们是否相等,但是它们不是'数',一旦将它们看成'数',例如state=s1+1或者state[s1]=1'b1(等数学操作)就不能综合成状态机。因此传统one-hot编码程序风格(位索引方式):
case (1'b1) begin
state[s1]:
...
end
不能综合成状态机。
4446

被折叠的 条评论
为什么被折叠?



