RHDL provides a complete library of combinational components — from basic logic gates through multiplexers, decoders, encoders, and arithmetic units.

Logic Gates

Basic Gates

All gates support configurable input counts:

gate = RHDL::HDL::AndGate.new(nil, inputs: 3)  # 3-input AND
gate.set_input(:a0, 1)
gate.set_input(:a1, 1)
gate.set_input(:a2, 1)
gate.propagate
gate.get_output(:y)  # => 1
ComponentFunctionPorts
NotGateInversionay
BufferNon-inverting driveray
AndGateANDa0..aNy
OrGateORa0..aNy
XorGateXORa0..aNy
NandGateNANDa0..aNy
NorGateNORa0..aNy
XnorGateXNORa0..aNy
TristateBufferTri-state drivera, eny

Bitwise Operations

Multi-bit logic operations on buses:

and_op = RHDL::HDL::BitwiseAnd.new(nil, width: 8)
and_op.set_input(:a, 0b11110000)
and_op.set_input(:b, 0b10101010)
and_op.propagate
and_op.get_output(:y)  # => 0b10100000
ComponentFunction
BitwiseAndN-bit AND
BitwiseOrN-bit OR
BitwiseXorN-bit XOR
BitwiseNotN-bit inversion

Multiplexers

Select one of several inputs based on a selector signal:

mux = RHDL::HDL::Mux4.new(nil, width: 8)
mux.set_input(:a, 10)
mux.set_input(:b, 20)
mux.set_input(:c, 30)
mux.set_input(:d, 40)
mux.set_input(:sel, 2)
mux.propagate
mux.get_output(:y)  # => 30
ComponentInputsSelector Width
Mux221
Mux442
Mux883
MuxNN (parameterized)log2(N)

Demultiplexers

Route one input to one of several outputs:

ComponentOutputs
Demux22
Demux44

Decoders

Binary to one-hot conversion:

dec = RHDL::HDL::Decoder3to8.new
dec.set_input(:a, 5)
dec.set_input(:en, 1)
dec.propagate
dec.get_output(:y5)  # => 1
dec.get_output(:y0)  # => 0
ComponentInput WidthOutput Width
Decoder2to424
Decoder3to838
DecoderNN (parameterized)2^N

Encoders

Priority encoders — find the highest-priority active input:

ComponentInput WidthOutput Width
Encoder4to242
Encoder8to383

Arithmetic Components

Adders

adder = RHDL::HDL::RippleCarryAdder.new(nil, width: 8)
adder.set_input(:a, 100)
adder.set_input(:b, 50)
adder.set_input(:cin, 0)
adder.propagate
adder.get_output(:sum)   # => 150
adder.get_output(:cout)  # => 0
ComponentDescriptionPorts
HalfAdder1-bit, no carry ina, bsum, cout
FullAdder1-bit with carrya, b, cinsum, cout
RippleCarryAdderN-bit addera, b, cinsum, cout, overflow
SubtractorN-bit subtractora, b, bindiff, bout, overflow
AddSubAdd or subtracta, b, subresult, cout

Comparators

cmp = RHDL::HDL::Comparator.new(nil, width: 8)
cmp.set_input(:a, 50)
cmp.set_input(:b, 30)
cmp.set_input(:signed, 0)
cmp.propagate
cmp.get_output(:gt)  # => 1
PortDescription
eqEqual
gtGreater than
ltLess than
gteGreater or equal
lteLess or equal

Multiplier and Divider

ComponentPorts
Multipliera, bproduct (2N bits)
Dividerdividend, divisorquotient, remainder, div_by_zero
IncDecIncrement or decrement

ALU

Full arithmetic logic unit with 16 operations:

alu = RHDL::HDL::ALU.new(nil, width: 8)
alu.set_input(:a, 10)
alu.set_input(:b, 5)
alu.set_input(:op, RHDL::HDL::ALU::OP_ADD)
alu.set_input(:cin, 0)
alu.propagate
alu.get_output(:result)  # => 15
OpCodeOperation
OP_ADD0Add
OP_SUB1Subtract
OP_AND2Bitwise AND
OP_OR3Bitwise OR
OP_XOR4Bitwise XOR
OP_NOT5Bitwise NOT (of A)
OP_SHL6Shift left
OP_SHR7Shift right logical
OP_SAR8Shift right arithmetic
OP_ROL9Rotate left
OP_ROR10Rotate right
OP_MUL11Multiply (low byte)
OP_DIV12Divide
OP_MOD13Modulo
OP_INC14Increment A
OP_DEC15Decrement A

ALU flags: cout (carry), zero, negative, overflow

Shifters and Bit Operations

Barrel Shifter

Fast multi-bit shifter supporting shift, arithmetic shift, and rotate:

PortDescription
aInput value
shiftShift amount (log2(N) bits)
dirDirection (0=left, 1=right)
arithArithmetic shift
rotateRotate instead of shift
yResult

Bit Utilities

ComponentFunction
SignExtendSign-extend to wider width
ZeroExtendZero-extend to wider width
ZeroDetectOutput 1 if input is all zeros
BitReverseReverse bit order
PopCountCount number of 1 bits
LZCountCount leading zeros

Next Steps