Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 239735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:36:33+00:00 2026-05-11T20:36:33+00:00

I am trying to design a 4-bit adder subtracter in verilog. This is only

  • 0

I am trying to design a 4-bit adder subtracter in verilog. This is only the second thing I have ever written in verilog, and I don’t know all the correct syntax yet. This is the module I have so far:

module Question3(carryin, X, Y, Z, S, carryout, overflow);
    parameter n = 4;
    input carryin, Z;
    input [n-1:0]X, Y;
    output reg [n-1:0]S;
    output reg carryout, overflow;

    if(Z==0)
    begin
        Y = not(y) + 4'b0001;
    end

    always @(X, Y, carryin)
        begin
            {carryout, S} = X + Y + carryin;
            overflow = carryout ^ X[n-1]^Y[n-1]^S[n-1];
        end

endmodule

My compiler (xilinx 10.1), keeps saying “Syntax error near if.” I have tried many different ways of doing the conversion, including just using a Case that takes Y as an argument, then checks all the possible 4-bit combinations, and converts them to two’s complement.

Z is what determines if the adder does subtraction or addition. If it’s 0, it means subtraction, and I want to convert y to two’s complement, then just do regular addition. I’m sure the rest of the adder is correct, I just do not know what is wrong with the part where I’m trying to convert.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:36:33+00:00Added an answer on May 11, 2026 at 8:36 pm
    reg [n-1:0] Y_compl;
    
    always @( Z, Y, X, carryin ) begin
      Y_ = ( ~Y + 4'b0001 );
      if ( Z == 1'b0 ) begin
        {carryout, S} = X + Y_compl + carryin;
        overflow = carryout ^ X[n-1] ^ Y_compl[n-1] ^ S[n-1];
      end
      else begin
        {carryout, S} = X + Y + carryin;
        overflow = carryout ^ X[n-1] ^ Y[n-1] ^ S[n-1];
      end
    
    end
    

    A couple of important points.

    1. Put the if statement inside the always block. Do not use two always blocks, you’ll create a race condition in the simulator.
    2. I created a new variable, Y_ because using Y, which is an input, remember, on the left hand side of an assignment will probably infer latches or do something else nasty when you synthesize.
    3. I suggest using the bitwise inversion operator ‘~’ to invert Y instead if the ‘not’
      primitive. The synthesis tool has more freedom to optimize your code this way.
    4. Double check for correct results, it’s been awhile since I built an adder.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of web design that I’m trying to solve with efficient
I have a basic 8-bit ALU described in Verilog. I am trying to implement
I'm trying to design my app's interface in IB using a storyboard and have
I am trying to design my Class Diagram. But I get stuck on this
I'm trying to design a pretty simple app and am getting myself a bit
I'm trying to get a bit more in to design patterns and I was
I'm trying to create a design where I have a div for an image,
This question is a bit of a structural/design question as I'm having trouble working
I'm trying to join two SQL tables, parent (which I have complete design control
I'm a bit stuck trying to figure out how to design an Oracle query,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.