Build a half adder, sum and carry, from two-input NAND cells only, within a budget of five cells.
Hardware context: A test chip with NAND cells only and a radiation-tolerant controller with NOR cells only. Build a half adder from five two-input NAND cells, the carry sharing a cell with the sum.
A test chip has room for one kind of logic cell: the two-input NAND. A counter on the chip needs a half adder: sum = a XOR b and carry = a AND b.
This is an Optimize problem. Build the half adder from NAND cells of the library cells.sv. The starter is the textbook form: five cells for the sum and two more for the carry. It is correct, but your budget is five cells for both outputs.
Lesson G2.6 builds XNOR from four NOR cells. Find the NAND netlist for the sum, then look for a cell the carry can share.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
row | 2 | input of each check |
Full specification
// cells.sv: the cell library (read-only). Every cell takes 2 time units.
// Ports are (y, inputs...): output first. Each cell adds 1 to its own counter in the bench.
// While the bench sets poison to 1, every cell output is x.
module INV (output wire y, input wire a); wire g; not #2 (g, a); assign y = tb.poison ? 1'bx : g; initial tb.n_inv++; always @(tb.recount) tb.n_inv++; endmodule
module NAND2 (output wire y, input wire a, b); wire g; nand #2 (g, a, b); assign y = tb.poison ? 1'bx : g; initial tb.n_nand2++; always @(tb.recount) tb.n_nand2++; endmodule
module NOR2 (output wire y, input wire a, b); wire g; nor #2 (g, a, b); assign y = tb.poison ? 1'bx : g; initial tb.n_nor2++; always @(tb.recount) tb.n_nor2++; endmodule
module NAND3 (output wire y, input wire a, b, c); wire g; nand #2 (g, a, b, c); assign y = tb.poison ? 1'bx : g; initial tb.n_nand3++; always @(tb.recount) tb.n_nand3++; endmodule
module NOR3 (output wire y, input wire a, b, c); wire g; nor #2 (g, a, b, c); assign y = tb.poison ? 1'bx : g; initial tb.n_nor3++; always @(tb.recount) tb.n_nor3++; endmodule
module XOR2 (output wire y, input wire a, b); wire g; xor #2 (g, a, b); assign y = tb.poison ? 1'bx : g; initial tb.n_xor2++; always @(tb.recount) tb.n_xor2++; endmodule
module half_add (
input logic a,
input logic b,
output wire sum, // a XOR b
output wire carry // a AND b
);
// your NAND2 cells
endmodule
| Rule | Detail |
|---|---|
| Cells | Use only two-input NAND cells from cells.sv. The library has other cells; using one fails the check. |
| Budget | At most 5 cells. No correct netlist of this kind is smaller. |
| Timing | Every cell takes 2 ns. The check reads the outputs after each input change and again once they settle. |
| Cells only | The check refuses assign, always, initial, gate primitives, operators and hierarchical names before it runs. It also forces every cell output to x, and each output must follow. |
| Wires | Declare every wire between two cells. Each cell is one line: the cell name, an instance name, then (output, inputs). |
Each row check reports sum and carry after one input change. The cost checks count your NAND cells and every other cell.
Examples
Example 1
Only b Is 1- Input
ab = 01
- Output
sum = 1, carry = 0, moved_too_early = no
0 + 1 = 1: sum is 1 and carry is 0.
Example 2
Both Inputs Are 1- Input
ab = 11
- Output
sum = 0, carry = 1, moved_too_early = no
1 + 1 = 10 in binary: sum is 0 and carry is 1.
Example 3
Only a Is 1- Input
ab = 10
- Output
sum = 1, carry = 0, moved_too_early = no
From 11 to 10, sum must rise again and carry must fall.
Constraints and Contract
- Keep the module name half_add and its four ports.
- Use cell instances only: no gate primitives, assign or always blocks.
Hints
Hint 1
NAND and NOR are duals. The dual of an XNOR circuit computes XOR.
Hint 2
An XOR from NAND cells can start with t = (ab)'. A NAND of a with t is 0 only when a is 1 and b is 0.
Hint 3
The carry is ab, which is the complement of t. One more NAND, with both inputs tied to t, gives it.
What the grader checks: The check applies twelve input changes and reads both outputs after each one. It counts the cells of each kind, and forces every cell output to x to confirm that the outputs come from cells. It checks function, cell kind and count.
15 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 steps and the final capstone