我正试图为BCD计数秒表编写一个模块。当我检查语法时,会发现错误:
ERROR:HDLCompilers:26 - "../counter.v" line 24 expecting 'end', found 'else'
ERROR:HDLCompilers:26 - "../counter.v" line 25 unexpected token: '='
ERROR:HDLCompilers:26 - "../counter.v" line 25 unexpected token: '+'
ERROR:HDLCompilers:26 - "../counter.v" line 25 expecting 'endmodule', found '1'在我的代码中。我不太清楚错误是从哪里来的,并试图实现更多的开始/结束,但这并不能解决问题。下面是我当前的代码:
module BCDCount(en, clk, rst, direction, cTenths, cSec, cTS, cMin);
input en, clk, rst, direction;
output [3:0] cTenths, cSec, cTS, cMin;
reg [3:0] cTenths, cSec, cTS, cMin;
always @(posedge clk)
if(en)
begin
if(direction == 0)
if(cMin== 4'b 1001)
cMin <= 4'b 0000;
if(cTS == 4'b 0101)
cTS <= 4'b 0000;
cMin = cMin +1;
if(cSec == 4'b 1001)
cSec <= 4'b 0000;
cTS = cTS +1;
if(cTenths == 4'b 1001)
cTenths <= 4'b 0000;
cSec = cSec+1;
else
cTenths = cTenths +1;
else
cSec = cSec+1;
else
cTS = cTS + 1;
if(direction == 1)
if(cMin== 4'b 0000)
cMin <= 4'b 1001;
if(cTS == 4'b 0000)
cTS <= 4'b 1001;
cMin = cMin -1;
if(cSec == 4'b 0000)
cSec <= 4'b 1001;
cTS = cTS -1;
if(cTenths == 4'b 0000)
cTenths <= 4'b 1001;
cSec = cSec-1;
else
cTenths = cTenths -1;
else
cSec = cSec-1;
else
cTS = cTS - 1;
end
always @(posedge rst)
begin
cMin <= 0;
cTS <= 0;
cSec <= 0;
cTenths <= 0;
end
endmodule 发布于 2015-12-10 19:09:14
基于缩进结构,您似乎对此代码期望如此。
...
if(cTS == 4'b 0101)
cTS <= 4'b 0000;
cMin = cMin +1;
if(cTenths == 4'b 1001)
...Cmin = cMin + 1将在cTS == 4'b0101的情况下被执行。但是,在Verilog中,if语句只适用于它们前面的语句(就像C中的那样)。为了使它们适用于多个语句,我们需要将这些语句包装在begin-end块中(就像C中的{} )。
因此,您将看到代码有一个else语句的错误,但是它找不到匹配的if!
您需要使用以下内容:
...
if(cTS == 4'b 0101)
begin
cTS <= 4'b 0000;
cMin = cMin +1;
if(cTenths == 4'b 1001)
...
end
else
...编辑:同样值得注意的是,您将阻塞(=)和非阻塞(<=)分配混合在您的always块中。对于时钟always块,您应该(基本上)始终使用非阻塞赋值。将任何顺序分配移动到他们自己的always@(*)块。
您还将得到信号有多个驱动程序的错误,因为您在多个始终块中分配了一些信号值。
https://stackoverflow.com/questions/34209700
复制相似问题