easy 10 pts Solved

4-Bit Add/Subtract Unit

One adder, two operations: a single control bit selects between a+b and a-b using the standard invert-and-add-one trick.

Verilog problems / Combinational Design

What you must build

One adder, two operations: a single control bit selects between a+b and a-b using the standard invert-and-add-one trick.

Engineers use “4-Bit Add/Subtract Unit” as a building block in combinational design. Interviewers ask for the same ports and the same corner cases this judge covers. Completing it in the browser is the same skill as writing synthesizable RTL at work, minus the EDA license.

Concept: {cout,result} = a + (b ^ {4{sub}}) + sub. When sub=0, b passes through unchanged and this is a plain add. When sub=1, every bit of b is inverted and a 1 is added — the classic two's-complement subtraction trick, reusing the same adder hardware.

Port contract

The judge instantiates exactly these ports. Extra ports or a different module name fail to elaborate.

NameDirWidthDescription
ainput4First operand
binput4Second operand
subinput10 = add, 1 = subtract (a - b)
resultoutput4a+b or a-b, wrapped
coutoutput1Carry out (add) / NOT-borrow (subtract)

How to approach this kata

This is a medium kata: you will need sequential logic or a small FSM. Decide what is registered versus combinational before you type. Reset polarity and clock edge must match the spec; the judge will fail you on the first mismatched cycle.

Hidden tests instantiate top_module, drive the ports, and compare every sample against a golden model. They do not grade coding style. They do grade X/Z, off-by-one counters, and ignoring enables. Sign in only when you want the run saved on the leaderboard — the specification below is public.

Starter shape

Copy this skeleton into the editor (or press Reset starter). Fill the body; do not rename the module.

module top_module(
  input  [3:0] a,
  input  [3:0] b,
  input        sub,
  output [3:0] result,
  output       cout
);

  // Your code here

endmodule

Why this shows up in interviews

4-Bit Add/Subtract Unit sits under Combinational Design (combinational, arithmetic). Concept: {cout,result} = a + (b ^ {4{sub}}) + sub . When sub=0 , b passes through unchanged and this is a plain add. When sub=1 , every bit of b is inverted and a 1 is added — the classic two's-complement subtraction trick, reusing the same adder hardware.

A passing solution is synthesizable intent: no delays in the DUT, no initial blocks inside top_module, and no reference to testbench tasks. Use blocking assignments only in combinational always blocks; use non-blocking for registers clocked by clk.

Related problems

  • 2-to-1 Multiplexer — Select between two 1-bit inputs using sel. When sel=0 output a; when sel=1 output b.
  • 1-Bit Full Adder — Compute sum and carry-out for a + b + cin.
  • 4-to-1 Multiplexer — Select one of four 1-bit inputs using a 2-bit sel. sel=00→in0, 01→in1, 10→in2, 11→in3.
  • 4-to-2 Priority Encoder — Output the index of the highest-priority (MSB-most) set bit in a 4-bit input, plus a valid flag when any bit is set.

FAQ

What does this problem require?

What does the 4-Bit Add/Subtract Unit problem ask for? One adder, two operations: a single control bit selects between a+b and a-b using the standard invert-and-add-one trick. Implement it as Verilog module top_module with the listed ports.

Combinational or sequential?

Is 4-Bit Add/Subtract Unit combinational or sequential? Tags: combinational, arithmetic. Follow the clock/reset ports if they appear in the table; if there is no clock, use continuous assignment or combinational always @(*).

How does the auto-grader work?

How is 4-Bit Add/Subtract Unit graded? A hidden SystemVerilog/Verilog testbench in the EcrioniX judge simulates your module in the browser. You pass when every directed vector matches, including the waveform contract shown on this page.

Write a module named top_module matching the ports below exactly.
Expected waveform
Your solution
Judge output
// Output appears after you run tests.