gpt4 book ai didi

verilog - Verilog 中的简单状态机是什么样的?

转载 作者:行者123 更新时间:2023-12-05 01:09:19 27 4
gpt4 key购买 nike

我正在尝试简单地转换流程图 state machine进入 Verilog代码。
但是我不知何故被以下内容困住了,因为我对 Verilog 几乎没有任何了解,所以我可能会遗漏一些东西。

状态机检测到 0 and 1 的输入流, 如果计数 1 s 可以被 3 整除(或者简单地说:如果有 3 次数字 1)。

enter image description here

module example (
input clk,
input rst,
input input1,
output output
);

reg state;
reg nextstate;

localparam state2 = 3'd0;
localparam state1 = 3'd1;
localparam state0 = 3'd2;

always @(posedge clk or posedge rst) begin
if (rst)
state <= state0;
else
state <= nextstate;
end

always @* begin
case(state)
state0: begin
if(input1)
nextstate = state1;
end
state2: begin
if(input1)
nextstate = state0;
end
state1: begin
if(input1)
nextstate = state2;
end
default: nextstate = state0;
endcase
end

always @* begin
output1 = state0 & input1;
end

endmodule

我不知道:
  • 我是否必须将输入 + 输出定义为 regwire ?或者是 inputoutput !充足的?
  • 我必须为 reg state, nextstate 提供一个向量维度吗? ?如果是,我如何知道选择哪个维度?
  • 我可以在结尾写这些断言,如 state0 & input1 ?或者我应该使用 state = state0 & input1 = ?? - 是,什么?
  • 最佳答案

    do I have to define the inputs + outputs as reg or wire? Or is input and output! sufficient?



    输入总是连线,尽管这并不重要,因为您没有分配给它们。默认情况下输出是电线,但您也可以声明 output reg如果你想要一个寄存器。

    must I provide a vector dimension for the reg state, nextstate? If yes, how to I know which dimension to pick?



    是的,您必须声明一个维度,否则当 verilog 静默地将您的所有状态截断为 0 时,您的设计将灾难性地失败。或 1 .状态的宽度应该与用于定义状态名称的 localparams 的宽度相同,或者更一般地说,宽度应该是 log2(number of input states) .

    can I write these kind of assertions at the end like state0 & input1?



    我不认为这是你想要的。 State0 只是一个常数。如果想知道状态机是否处于state0,那么就需要将当前状态与常量state0进行比较。此外,您可能不想要按位 AND 在这里,所以使用常规和 && .应该:
    output = (state == state0) && input1;

    关于verilog - Verilog 中的简单状态机是什么样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15932590/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com