gdagis
gdagis

Reputation: 113

Simple Addition in Verilog

I'm relatively new to Verilog so hopefully I can get my thoughts across properly. I'm trying to implement a basic hex calculator. So far I'm able use two 7-segment displays to show the integers to be added (A and B). I'm also able to represent numbers from hex#00 up until hex#10

Below are the important snippets of code I've put together thus far.

Part of my FourBitAdder:

// Four Bit Adder
module FourBitAdder (A, B, Aout1, Aout0, Bout1, Bout0, Cout3, Cout2, Cout1, 
Cout0, Sum, PrintSum, Carry);
output Carry;
output [4:0] Sum;
output [4:0] PrintSum;
input  [3:0] A;             // SW15-SW8
input  [3:0] B;             // SW7-SW0
assign PrintSum = A+B;

I used "Printsum" because it wouldnt let me declare "Sum" twice...

My corresponding FullAdder:

// Full Adder
module FullAdder (A, B, C, Sum, Carry);
output Sum;
output Carry;
input A;
input B;
input C;
assign Sum = A^B^C;   
assign Carry = (A&B) | (B&C) | (C&A);
endmodule

Lastly, my case statements for the sum on my 7-segment display. I thought I could use PrintSum, ranging from 5'b00000 to 5'b11110 (1E, max sum).

// Hex 1 (MSB)
always@*
case(PrintSum)
5'b0???? : Cout1 = 7'b1000000;  //0
5'b1???? : Cout1 = 7'b1111001;  //1 if S[4] = 1
default
Cout1 = 7'b1000000;     //0 by default
endcase 

// Hex 0 (LSB)
always@*
case(PrintSum)
5'b?0000 : Cout0 = 7'b1000000;  //0
...
5'b?1111 : Cout0 = 7'b0001110;  //F
default
Cout0 = 7'b1000000;     //0 by default
endcase 
endmodule

Thanks everyone in advance :)

Upvotes: 1

Views: 19973

Answers (1)

Oldfart
Oldfart

Reputation: 6259

Perhaps I can take the logic values of A[3-0] and B[3-0] and somehow convert that to hex.

Logic values ARE in hex. All you have to do is split the vector in sections of 4 bits, starting with LS going to MS. (You may have less then 4 bits left at the end.).
As to you code: you are working with a high level language not with gates. So to add two numbers you can just use the addition operator: '+'. There is no need to instance a series of full adders:

Sum = A + B;

But you have a problem in that you add two 4 bit numbers which can give a 5 bit result. So Sum should be 5 bits wide. It also means that the MS of your two hex display will only display 0 or 1 (The result is between 0x00 and 0x1E or for mere mortals: 0 to 30)

use case statements similar to the ones I used to show inputs A and B

I can't check your case statement as I don't know which bit controls what segment of the display. But the principle is right.
As extra: You have to decide what you want to display A, B or Sum. A nice next challenge for you might be to display a sequence: A, then B then Sum.

Upvotes: 2

Related Questions