gpt4 book ai didi

verilog - 'begin/end' 在设计模块中的使用

转载 作者:行者123 更新时间:2023-12-05 08:04:03 30 4
gpt4 key购买 nike

我尝试使用 icarus verilog 模拟在 EDA Playground 上制作 BCD 计数器。在我的第一次尝试中,我在未使用 beginend 关键字的情况下编写了 always block :

module bcdcounter(out, clock, reset);
output [3:0] out;
input clock, reset;

reg [3:0] outstate= 4'd0;
reg [1:0] state= 2'd0;

always@(posedge reset)
case(state)
2'd0: state <=2'd1;
2'd1: state <=2'd2;
2'd2: state <=2'd0;
endcase

always@(posedge clock or state)
if(!state)
outstate <= 4'd0;
if(state == 2'd1)
if(outstate != 4'd9)
outstate <= outstate +4'd1;
else
outstate <= 4'd0;
if(state == 2'd2)
outstate <= outstate;

assign out = outstate;

endmodule

在我检查时生成了以下输出:

design.sv:21: syntax error
design.sv:21: error: Invalid module instantiation
design.sv:23: syntax error
design.sv:23: error: Invalid module instantiation
design.sv:25: syntax error
design.sv:25: error: Invalid module instantiation
Exit code expected: 0, received: 1

但是,一旦我添加了一些 beginend 关键字,它就可以正常工作了:

module bcdcounter(out, clock, reset);
output [3:0] out;
input clock, reset;

reg [3:0] outstate= 4'd0;
reg [1:0] state= 2'd0;

always@(posedge reset)
case(state)
2'd0: state <=2'd1;
2'd1: state <=2'd2;
2'd2: state <=2'd0;
endcase

always@(posedge clock or state)
begin
if(!state)
outstate <=4'd0;
if(state==2'd1)
begin
if(outstate!=4'd9)
outstate<= outstate+4'd1;
else outstate<= 4'd0;
end
if(state==2'd2)
outstate<=outstate;
end

assign out = outstate;

endmodule

我们什么时候需要在设计模块中使用beginend关键字?任何帮助将不胜感激。

最佳答案

当您在 always block 中有多个 语句时,

begin/end 关键字是必需的。

在第一个 always block 中,您不需要 begin/end,因为 case 语句被视为单个语句。

但是,在您的第二个 always block 中,您有 3 个顶级 if 语句,这意味着您需要在 begin/end 中将它们组合在一起。下面是带有不同空格的代码,以清楚地显示单独的语句:

always@(posedge clock or state) begin

// Statement 1
if(!state) outstate <= 4'd0;

// Statement 2
if(state == 2'd1)
if(outstate != 4'd9)
outstate <= outstate +4'd1;
else
outstate <= 4'd0;

// Statement 3
if(state == 2'd2) outstate <= outstate;

end

由于此代码编译时没有语法错误,因此它演示了一些事情:

  1. 显示的开始/结束关键字是强制性的
  2. 不需要其他开始/结束关键字

但是,其他开始/结束关键字是可选的。您可以决定在每个 if 语句中使用它们。这是另一种编写代码的方法:

always@(posedge clock or state) begin

if (!state) begin
outstate <= 4'd0;
end

if (state == 2'd1) begin
if (outstate != 4'd9) begin
outstate <= outstate +4'd1;
end else begin
outstate <= 4'd0;
end
end

if (state == 2'd2) begin
outstate <= outstate;
end

end

相同的规则适用于 initialfinal 程序 block ,就像它们适用于 always block 一样。

关于verilog - 'begin/end' 在设计模块中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70344979/

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