R9.3.1 One Package for the Opcodes

MediumImplementVerilator + Yosys · serverReview the topic

Write the package an ALU shares with its users. It holds a word type, an opcode enum with a fixed encoding and a 9-bit add function. Then write the ALU.

Hardware context: An 8-bit ALU on a shared package (a word type, an opcode enum with gaps in its encoding, a 9-bit add function); and a packet length counter compiled after another team's type file. Finish alu_pkg and write the ALU that imports it.

An ALU and the blocks around it must agree on the opcodes. The firmware already uses fixed numbers for them, with gaps left for later operations. A second block also needs the adder, with its carry, for an address check.

This is an Implement problem in two parts. First finish alu_pkg: the opcode enum with the encoding of the brief, and the function add9. Then write alu8, which imports the package.

The bench is the package's second user. It reads your labels and type widths by name. It calls add9 itself on every test. It drives the ALU with the numbers of the brief, so a label with another number runs the wrong operation.

Brief: alu8 is combinational, with ports op (op_t), a and b (word_t) in, y (word_t) and carry out. The table in the specification gives each operation. An opcode that is not in the list gives y = 0 and carry = 0. Synthesis check: the design must build on the site's synthesis flow with no latch. The cell and level counts come from an open-source flow and are a proxy for area and depth, not a foundry result.

Signals in the Timeline

SignalWidthRole
op3input of each check
a8input of each check
b8input of each check
Full specification
LabelNumberycarry
OP_PASS0a0
OP_ADD1a + b, low 8 bitsbit 8 of the sum
OP_SUB2a - b, low 8 bits1 when a is at least b (no borrow), else 0
OP_SHL4a shifted left by one; bit 0 is 0the old bit 7 of a
OP_SHR5a shifted right by one; bit 7 is 0the old bit 0 of a
3, 6, 7no label00
Package itemMust be
Wa localparam int, 8
word_ta W-bit vector
op_ta 3-bit enum with the five labels and numbers above
add9(a, b, cin)an automatic function: a + b + cin as a 9-bit value, carry out in bit 8
The module header (keep it)
module alu8
  import alu_pkg::*;
(
  input  op_t   op,
  input  word_t a, b,
  output word_t y,
  output logic  carry
);
  // your decode
endmodule

Result fields: y and carry come from your ALU. pkg_sum is the bench's own call of add9. op_codes holds your five labels, word_bits the width of word_t and add9_bits the width add9 returns.

Examples

Example 1

An Add with a Carry Out
Input
ADD: a=0xf0 b=0x1f; the bench also calls add9(a, b, 0)
Output
y = 0xf, carry = yes, pkg_sum = 0x10f, op_codes = 0x5888, word_bits = 8, add9_bits = 9

f0 + 1f is 10f. y is 0f and carry is 1. The bench's own add9 call gives the same 9-bit sum.

Example 2

A Subtract with No Borrow
Input
SUB: a=0x30 b=0x10; the bench also calls add9(a, b, 1)
Output
y = 0x20, carry = yes, pkg_sum = 0x41, op_codes = 0x5888, word_bits = 8, add9_bits = 9

30 - 10 is 20, and carry is 1 because nothing was borrowed. The bench calls add9(30, 10, 1) at the same time and gets 041.

Example 3

A Subtract That Borrows
Input
SUB: a=0x10 b=0x30; the bench also calls add9(a, b, 0)
Output
y = 0xe0, carry = no, pkg_sum = 0x40, op_codes = 0x5888, word_bits = 8, add9_bits = 9

10 - 30 wraps to e0, and carry is 0.

Constraints and Contract

  • Keep the package name, every item name in it, and the module ports.
  • Inputs are known 0/1 values. Each case applies one set of inputs, lets the ALU settle, calls add9 from the bench, and then reads the outputs.
  • The package holds no variable. On this flow, a package variable that the design writes stops Synthesize, so such a design fails the synthesis check.

Hints

Hint 1

Write a value on every enum label. With no values, the labels count 0, 1, 2, 3, 4 in list order, and the brief has gaps.

Hint 2

Widen both operands by one bit before you add, so the carry has a place in the result. The return type must be 9 bits as well.

Hint 3

A subtract is an add of the inverted b with a carry in of 1. Then add9 serves both operations, and its carry out is the "no borrow" flag.

What the grader checks: Thirteen directed and fifteen seeded cases apply one opcode and two operands each. The ALU's y and carry are compared with an independent model. The bench also reads the five labels and two widths from your package and calls add9 itself. Passing shows the package and the ALU agree with the brief on these cases; the synthesis check adds the latch count.

The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.

28 checks on Verilator + Yosys · server. 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

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

An Add with a Carry Out

Input
ADD: a=0xf0 b=0x1f; the bench also calls add9(a, b, 0)
Expected output
y = 0xf, carry = yes, pkg_sum = 0x10f, op_codes = 0x5888, word_bits = 8, add9_bits = 9

f0 + 1f is 10f. y is 0f and carry is 1. The bench's own add9 call gives the same 9-bit sum.

Run samples executes all 3 public checks. Check all tests runs 28 checks on the server.

Verilator + Yosys · server