BCD Adder in Verilog

痴心易碎 提交于 2019-12-22 07:39:56

问题


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.


回答1:


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 



回答2:


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 :-)



来源:https://stackoverflow.com/questions/4179330/bcd-adder-in-verilog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!