Verilog 中的 BCD 加法器

发布于 2024-10-02 23:30:01 字数 872 浏览 6 评论 0原文

我正在尝试用 Verilog 编写 BCD 加法器,但其中一个模块遇到问题。具体来说,加法器将两个 BCD 数字相加。所以,如果两位数之和小于或等于九,那么它就是正确的。但是,如果它更大,则必须添加 6 的偏移量。到目前为止,这是我的 Verilog 代码:

module DIGITADD(
    input [3:0] IN_A,
    input [3:0] IN_B,
    input CIN,
    output reg COUT,
    output reg [3:0] SUM
);

wire s2, c2;

always @ ( * ) 
begin
 assign {c2, s2} = IN_A + IN_B + CIN;

 if(s2 <= 9 && c2 == 0) begin
  assign {COUT, SUM} = {c2, s2};
 end
 else if({c2, s2} > 9) begin
  assign {COUT, SUM} = {c2, s2 + 6};
 end
end
endmodule

无论如何,当我尝试在 Xilinx 中综合它时,出现以下错误:

ERROR:HDLCompilers:247 - "DIGITADD.v" line 33 Reference to scalarwire 'c2' is not a legal reg或变量左值

ERROR:HDLCompilers:247 - "DIGITADD.v" line 33 对标量线 's2' 的引用不是合法的寄存器或变量左值

ERROR:HDLCompilers:42 - "DIGITADD.v" line 33 程序的左侧非法 我尝试更改一些内容

,例如将电线更改为reg,但我仍然无法使其工作。任何帮助表示赞赏。

I am trying to write a BCD Adder in Verilog, but I am having trouble with one of the modules. Specifically, the adder that takes two BCD digits and adds them. So, the idea is if the sum of the two digits is less than or equal to nine, then it is correct. However, if it is greater, then an offset of 6 has to be added. Here is my Verilog code so far:

module DIGITADD(
    input [3:0] IN_A,
    input [3:0] IN_B,
    input CIN,
    output reg COUT,
    output reg [3:0] SUM
);

wire s2, c2;

always @ ( * ) 
begin
 assign {c2, s2} = IN_A + IN_B + CIN;

 if(s2 <= 9 && c2 == 0) begin
  assign {COUT, SUM} = {c2, s2};
 end
 else if({c2, s2} > 9) begin
  assign {COUT, SUM} = {c2, s2 + 6};
 end
end
endmodule

Anyways, when I try to synthesize it in Xilinx, I get the following errors:

ERROR:HDLCompilers:247 - "DIGITADD.v" line 33 Reference to scalar wire 'c2' is not a legal reg or variable lvalue

ERROR:HDLCompilers:247 - "DIGITADD.v" line 33 Reference to scalar wire 's2' is not a legal reg or variable lvalue

ERROR:HDLCompilers:42 - "DIGITADD.v" line 33 Illegal left hand side of procedural assign

I tried changing some things like changing wire to reg, but I still can't get it to work. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞美少年 2024-10-09 23:30:02

在纯文本中,不要在程序块中使用连续赋值,例如“分配”语句,即“始终”或“初始”。

记住规则,生活就会美好:-)

In plain text, do not have a continuous assignment like "assign" statement in a procedural block i.e. always or initial.

Remember the rule and life is good :-)

冷清清 2024-10-09 23:30:01

好吧,我明白了,正确的代码如下。基本上,请参阅我对我的问题所做的评论,以获取一些需要记住的提示。有趣的是,与我之前遇到的混乱相比,这要简单得多。

module DIGITADD(
    input [3:0] IN_A,
    input [3:0] IN_B,
    input CIN,
    output COUT,
    output [3:0] SUM
    );

reg [4:0] s2;

assign SUM = s2[3:0];
assign COUT = s2[4];

always @ ( * )
begin
    s2 = IN_A + IN_B + CIN;
    if (s2 > 9)
    begin
        s2 = s2 + 6;
    end
end
endmodule 

Okay, I figured it out, the correct code is below. Basically, see the comment I made on my question for some tips to remember. Its funny how much simpler this is compared to the mess I had earlier.

module DIGITADD(
    input [3:0] IN_A,
    input [3:0] IN_B,
    input CIN,
    output COUT,
    output [3:0] SUM
    );

reg [4:0] s2;

assign SUM = s2[3:0];
assign COUT = s2[4];

always @ ( * )
begin
    s2 = IN_A + IN_B + CIN;
    if (s2 > 9)
    begin
        s2 = s2 + 6;
    end
end
endmodule 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文