我正在尝试为 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
根据您的缩进结构,您似乎期望此代码
...
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@(*)
块。
您还会遇到信号具有多个驱动程序的错误,因为您在多个始终块中分配了一些信号值。