Warning: Cannot modify header information - headers already sent by (output started at /home/abettino/public_html/index.php:2) in /home/abettino/public_html/includes/bootstrap.inc on line 636

Warning: Cannot modify header information - headers already sent by (output started at /home/abettino/public_html/index.php:2) in /home/abettino/public_html/includes/bootstrap.inc on line 637

Warning: Cannot modify header information - headers already sent by (output started at /home/abettino/public_html/index.php:2) in /home/abettino/public_html/includes/bootstrap.inc on line 638

Warning: Cannot modify header information - headers already sent by (output started at /home/abettino/public_html/index.php:2) in /home/abettino/public_html/includes/bootstrap.inc on line 639
4 Bit Subtractor | hdlsnippets
Skip navigation.
Home

4 Bit Subtractor

warning: Cannot modify header information - headers already sent by (output started at /home/abettino/public_html/index.php:2) in /home/abettino/public_html/includes/common.inc on line 148.

This Verilog subtractor uses this Ripple Carry Adder in order to realize a subtracter. It is important to keep in mind that the inputs represent signed two's complement values. The first thing this snippet does take the two's complement of the B input value and then feed it into another RCA.
Subtractor.

// Performs the operation A-B
module sub_4bit
(
 input  [3:0] A,
 input  [3:0] B,
 output [3:0] diff
 );
 
wire [3:0]     B2Comp;
 
rca twocomp
(
 .A(~B),
 .B(4'b1),
 .sum(B2Comp)
 );
 
rca subtract
(
 .A(A),
 .B(B2Comp),
 .sum(diff)
 );
 
endmodule  

Basic RCA.

// Basic full adder module.
module full_adder
(
  input  a,
  input  b,
  input  cin,
  output sum,
  output cout
);
 
assign sum = a ^ b ^ cin;
assign cout = (a & b) | ((a^b) & cin);
 
endmodule
 
 
// Ripple carry adder.
module rca 
(
 input [3:0] A,
 input [3:0] B,
 output [3:0]  sum
 );
 
wire [4:0]       carry_out;
 
full_adder fa0(.a(A[0]),.b(B[0]),.cin(1'b0),
 	       .cout(carry_out[1]),.sum(sum[0]));
full_adder fa1(.a(A[1]),.b(B[1]),.cin(carry_out[1]),
 	       .cout(carry_out[2]),.sum(sum[1]));
full_adder fa2(.a(A[2]),.b(B[2]),.cin(carry_out[2]),
 	       .cout(carry_out[3]),.sum(sum[2]));
full_adder fa3(.a(A[3]),.b(B[3]),.cin(carry_out[3]),
 	       .cout(carry_out[4]),.sum(sum[3]));
 
endmodule

AttachmentSize
sub4bit.v2.04 KB

Great article!

Thanks!