Skip to main content

Constants

Constants define fixed bit patterns that can be used in connections without external inputs.

Declaration

<name> = <value>;
<name>[<width>] = <value>; # With explicit bit width

Constants are declared in the component body, alongside instance declarations.

component MyComponent(In[8]) -> (Out) {
THRESHOLD = 100;
MASK = 0xFF;
DATA[8] = 100; # Explicit 8-bit width

connect { ... }
}

Number Formats

FormatPrefixExampleDecimal Value
Decimal(none)100100
Hexadecimal0x0x64100
Binary0b0b01100100100
DECIMAL_VAL = 255;
HEX_VAL = 0xFF;
BINARY_VAL = 0b11111111;
# All three represent the same value

Bit Width

Inferred Width (from value)

By default, the bit width is automatically determined from the value:

ValueBinaryInferred Width
111 bit
61103 bits
10011001007 bits
255111111118 bits
2561000000009 bits
One = 1;           # 1-bit constant
Six = 6; # 3-bit constant (0b110)
OneHundred = 100; # 7-bit constant
MaxByte = 255; # 8-bit constant

When a constant needs to match a specific port width, use explicit width annotation:

Hundred[8] = 100;  # 8-bit constant (bits 1-8 defined, bit 8 = 0)
Zero[16] = 0; # 16-bit constant of all zeros
Pattern[4] = 5; # 4-bit constant: 0b0101
Best Practice

When using constants with generators that iterate over a fixed bit range (like >i[8]), always specify the width explicitly to avoid "undefined bit" errors.

Width Example

# Good: explicit width matches the port width
component Add100(A[8]) -> (Sum[8], Cout) {
Hundred[8] = 100; # Explicit 8-bit width
connect {
>i[8]{
Hundred[{i}] -> fa{i}.B; # All 8 bits are defined
}
}
}

# Risky: inferred width (7 bits for value 100)
component Add100Bad(A[8]) -> (Sum[8], Cout) {
Hundred = 100; # Only 7 bits! Hundred[8] is undefined
connect {
>i[8]{
Hundred[{i}] -> fa{i}.B; # Error: bit 8 doesn't exist
}
}
}

Using Constants

Constants can be used in connections like any signal. Individual bits can be accessed with indexing.

component Compare100(A[8]) -> (Equal) {
>i[8]{
xor{i}: XOR;
}
or1: Or8Inputs;
not1: NOT;

OneHundred[8] = 100;

connect {
>i[8]{
A[{i}] -> xor{i}.A;
OneHundred[{i}] -> xor{i}.B; # Access each bit
xor{i}.O -> or1.A[{i}];
}
or1.Out -> not1.A;
not1.O -> Equal;
}
}

Bit Indexing on Constants

MyConst = 0b1010;    # 4-bit constant
# MyConst[1] = 0 (LSB)
# MyConst[2] = 1
# MyConst[3] = 0
# MyConst[4] = 1 (MSB)

Remember: Bit 1 is always the LSB (rightmost bit in binary representation).