§ 2.2. Logic Operations¶
LogicUnit<W> selects one of seven bitwise operations on two W-bit words. The RV32I ALU uses its own logic part, which produces AND, OR and XOR at once.
| Component | Description | Structural | Behavioral |
|---|---|---|---|
LogicUnit<W> |
Selected AND/OR/XOR/NOT/NAND/NOR/XNOR result | Yes | Yes |
W is 8, 16 or 32. The fidelity columns hold at every width: each unit has a structural and a compact form at 8, 16 and 32 bits. The C++ references take W as a template parameter. Construction and selection describes construction.
2.2.1. LogicUnit<W>¶
IO pins¶
| Pin | Direction | Width | Description |
|---|---|---|---|
A |
Input | W | First operand; the only operand used by NOT |
B |
Input | W | Second operand |
OP |
Input | 3 | Local operation selector |
OUT |
Output | W | Selected bitwise result |
Behavior¶
Every output bit depends on the same-position operand bits. Complements are limited to W bits; there is no inter-bit carry.
| Local OP | Operation | OUT | ALU OP |
|---|---|---|---|
| 0 | AND | A AND B | 8 |
| 1 | OR | A OR B | 9 |
| 2 | XOR | A XOR B | 10 |
| 3 | NOT | W-bit complement of A; B ignored | 11 |
| 4 | NAND | W-bit complement of A AND B | 12 |
| 5 | NOR | W-bit complement of A OR B | 13 |
| 6 | XNOR | W-bit complement of A XOR B | 14 |
| 7 | Reserved | Zero | 15, reserved |
The unit has no carry, flags or invalid-operation output. The complete ALU derives flags after result selection.
C++ example¶
This binary reference is parameterized by W = 8, 16 or 32. Inputs must fit their documented pin widths. It omits propagation delays and X/Z values.
#include <cstdint>
template<unsigned W>
std::uint32_t logic_ref(
std::uint32_t a, std::uint32_t b, std::uint32_t op) {
const std::uint64_t x = a, y = b;
std::uint64_t result;
switch (op) {
case 0: result = x & y; break;
case 1: result = x | y; break;
case 2: result = x ^ y; break;
case 3: result = ~x; break;
case 4: result = ~(x & y); break;
case 5: result = ~(x | y); break;
case 6: result = ~(x ^ y); break;
default: result = 0; break;
}
return static_cast<std::uint32_t>(result & WordReference<W>::mask);
}
Unknown inputs¶
AND with zero and OR with one mask unknown inputs. XOR and inversion produce X when their relevant input is X/Z. NOT ignores B. Known reserved OP returns zero. Unknown OP follows the mux equations, including their conservative four-state behavior.
Implementation¶
Structural implementation¶
Word gates AND, OR and XOR (ANDGate<W>, ORGate<W>, XORGate<W>) combine A and B on every bit; four NOTGate<W> word gates (NOT, NAND, NOR, XNOR) produce the inverted candidates. One Mux<8,W> (SELECT, an eight-way mux per bit) chooses the result; a constant zero word supplies the reserved input. These are the same one-bit gates and muxes as a bit-by-bit circuit, grouped as words. The construction rule is identical at W=8, 16 and 32.
Behavioral implementation¶
The compact form, available at W = 8, 16 and 32, computes the settled outputs of these gates in one step. It keeps the gates' Boolean factoring, so X and Z inputs give the same outputs as the structural circuit, and it runs on delays proven on that circuit (delay model).
Examples¶
All 32 recorded rows of LogicUnitWidth8Test, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-gate-v1 / structural (ed03f70afe360dba). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | AInput · 8 bit |
BInput · 8 bit |
OPInput · 3 bit |
OUTExpected output · 8 bit |
|---|---|---|---|---|---|
#0row-0 | 7 | 0x00 |
0x0F |
0x0 |
0x00 |
#1row-1 | 15 | 0x55 |
0x0F |
0x0 |
0x05 |
#2row-2 | 23 | 0x80 |
0x0F |
0x0 |
0x00 |
#3row-3 | 31 | 0xFF |
0x0F |
0x0 |
0x0F |
#4row-4 | 39 | 0x00 |
0x0F |
0x1 |
0x0F |
#5row-5 | 47 | 0x55 |
0x0F |
0x1 |
0x5F |
#6row-6 | 55 | 0x80 |
0x0F |
0x1 |
0x8F |
#7row-7 | 63 | 0xFF |
0x0F |
0x1 |
0xFF |
#8row-8 | 71 | 0x00 |
0x0F |
0x2 |
0x0F |
#9row-9 | 79 | 0x55 |
0x0F |
0x2 |
0x5A |
#10row-10 | 87 | 0x80 |
0x0F |
0x2 |
0x8F |
#11row-11 | 95 | 0xFF |
0x0F |
0x2 |
0xF0 |
#12row-12 | 103 | 0x00 |
0x0F |
0x3 |
0xFF |
#13row-13 | 111 | 0x55 |
0x0F |
0x3 |
0xAA |
#14row-14 | 119 | 0x80 |
0x0F |
0x3 |
0x7F |
#15row-15 | 127 | 0xFF |
0x0F |
0x3 |
0x00 |
#16row-16 | 136 | 0x00 |
0x0F |
0x4 |
0xFF |
#17row-17 | 145 | 0x55 |
0x0F |
0x4 |
0xFA |
#18row-18 | 154 | 0x80 |
0x0F |
0x4 |
0xFF |
#19row-19 | 163 | 0xFF |
0x0F |
0x4 |
0xF0 |
#20row-20 | 172 | 0x00 |
0x0F |
0x5 |
0xF0 |
#21row-21 | 181 | 0x55 |
0x0F |
0x5 |
0xA0 |
#22row-22 | 190 | 0x80 |
0x0F |
0x5 |
0x70 |
#23row-23 | 199 | 0xFF |
0x0F |
0x5 |
0x00 |
#24row-24 | 208 | 0x00 |
0x0F |
0x6 |
0xF0 |
#25row-25 | 217 | 0x55 |
0x0F |
0x6 |
0xA5 |
#26row-26 | 226 | 0x80 |
0x0F |
0x6 |
0x70 |
#27row-27 | 235 | 0xFF |
0x0F |
0x6 |
0x0F |
#28row-28 | 243 | 0x00 |
0x0F |
0x7 |
0x00 |
#29row-29 | 250 | 0x55 |
0x0F |
0x7 |
0x00 |
#30row-30 | 257 | 0x80 |
0x0F |
0x7 |
0x00 |
#31row-31 | 264 | 0xFF |
0x0F |
0x7 |
0x00 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
This recording instantiates W = 8; the C++ reference accepts every supported W. Width/configuration coverage is listed in the tests below.
Embedded simulation¶
The logic-unit-w8 scenario runs LogicUnitWidth8Test: 32 recorded cases covering all eight local selections, with A drawn from 0x00, 0x55, 0x80, and 0xFF, and B = 0x0F. The table above lists every recorded row; use its checkpoint labels to locate the settled outputs.
Open LogicUnit<8> in the simulator
Source and tests¶
| Test | Coverage |
|---|---|
LogicUnitWidth8Test |
32 binary cases: eight local selections × four A values, with B = 0x0F. Checks OUT; this is the embedded scenario. |
ALUContractTest: integrated checks |
Boundary and seeded random cases across all global opcodes, including logic operations. Targeted X/Z cases check NOT's ignored inputs, AND masking, XOR propagation, and reserved global encodings. |
Neither test exhaustively sweeps standalone LogicUnit<8> inputs. The integrated test's larger arithmetic and shifter sweeps do not constitute an exhaustive logic-unit test.