Module 4: Introduction to Verilog HDL · 4.2 Verilog Language Basics

Number Literals

In Verilog, the format of a numerical literal determines its bit width. The specified bit width affects concatenation, comparison operations, and synthesized circuit sizing. Unsized literals introduce silent truncation errors. You must write every constant with an explicit bit width and radix.

Syntax Format

A sized literal uses this standard format:

<size>'<base><value>
  • <size> specifies the total bit width in decimal.
  • The apostrophe (') separates width from radix.
  • <base> specifies the radix: b (binary), o (octal), d (decimal), or h (hexadecimal).
  • <value> provides the numerical digits. You can insert underscores (_) to improve readability.
4'b1010          // 4 bits, binary, equals decimal 10
8'hFF            // 8 bits, hexadecimal, equals decimal 255
8'hff            // Case insensitive; identical to 8'hFF
16'hDEAD         // 16 bits, hexadecimal
8'd255           // 8 bits, decimal
8'o377           // 8 bits, octal
1'b0             // Single-bit binary 0
1'b1             // Single-bit binary 1
32'h0000_0001    // 32 bits with grouping underscores
12'hABC          // 12 bits, hexadecimal

The Critical Importance of Bit Width

Every net and register in a design possesses an explicit bit width. When you assign a constant of a different width, the compiler truncates or zero-extends the value:

wire [3:0] nibble;

assign nibble = 255;    // 255 is an unsized 32-bit constant; upper 28 bits are discarded
assign nibble = 4'hF;   // Exact 4-bit constant

Unsized literals cause errors in vector concatenations because unsized numbers default to 32 bits:

wire [3:0] nib_a = 4'hA;
wire [7:0] byte_val;

// Literal 0 is 32 bits; creates 36-bit concatenation truncated to 8 bits (results in 8'h00)
assign byte_val = {nib_a, 0};

// Sized constant produces intended 8-bit result: 8'hA0
assign byte_val = {nib_a, 4'h0};

In relational comparisons such as if (state == 3), tools automatically zero-extend the narrow operand. However, you must specify explicit bit widths to prevent truncation errors in concatenations and arithmetic.

Four-State Logic Values

Verilog models physical digital circuits using four discrete logic values:

Value Physical Hardware State
0 Logic low level (ground reference)
1 Logic high level (supply voltage)
x Unknown state: uninitialized register or driver contention
z High impedance: floating or disconnected node

A reg variable defaults to x at simulation time zero because physical flip-flops power up in an unknown state. Two opposing drivers on the same wire create an x condition. Omitted branches in combinational statements also resolve to x.

`timescale 1ns / 1ps
`default_nettype none

module x_z_demo;

    reg  [3:0] uninit_reg;     // Resolves to 4'bxxxx at time 0
    wire [3:0] high_z_wire;    // Undriven wire: resolves to 4'bzzzz

    initial begin
        $display("uninit_reg at t=0: %b", uninit_reg);   // Displays xxxx
        uninit_reg = 4'b1010;
        $display("after assignment: %b", uninit_reg);     // Displays 1010

        uninit_reg = 4'bxxxx;   // Unknown bits
        uninit_reg = 4'b10xz;   // Mixed logic states
        uninit_reg = 4'hx;      // 4-bit unknown shorthand
        $finish;
    end

endmodule

Unknown states propagate through downstream logic. An AND gate with one x input and one 1 input produces an x output. Waveform displays of x states allow designers to trace uninitialized registers.

The z value indicates an undriven, floating net. In physical hardware, tri-state drivers generate z states to share bidirectional communication buses:

`timescale 1ns / 1ps
`default_nettype none

module tristate_driver #(
    parameter WIDTH = 8
)(
    input  wire [WIDTH-1:0] data_in,
    input  wire             oe_n,     // Active-low output enable
    output wire [WIDTH-1:0] bus
);

    assign bus = oe_n ? {WIDTH{1'bz}} : data_in;

endmodule

Inside modern FPGAs and ASICs, multiplexers replace internal tri-state buses. Restrict tri-state z states to physical external I/O pins.

Negative Number Representation

Verilog numerical literals are unsigned by default. A preceding minus sign represents a unary negation operator:

-8'd5    // Produces 8'b11111011 (decimal 251 unsigned, or -5 in two's complement)

The bit pattern remains identical. The operational interpretation depends on the signed attribute of the connected signals.

Selecting Appropriate Radix Bases

Select numeral bases to convey circuit intent:

  • Hexadecimal: Use for addresses and bus data to clarify byte boundaries:
parameter RESET_VECTOR = 32'h0000_0100;
parameter STATUS_MASK  = 8'h3F;
  • Binary: Use for control bitmasks and one-hot state encodings to show bit locations:
parameter INTERRUPT_MASK = 8'b0000_1111;
localparam S_IDLE = 4'b0001;
localparam S_RUN  = 4'b0010;
localparam S_DONE = 4'b0100;
  • Decimal: Use for timer intervals and counter thresholds:
parameter BAUD_DIVISOR   = 16'd868;    // 50 MHz clock / 57600 baud
parameter TIMEOUT_CYCLES = 10'd1000;

Insert underscores every four digits in long vectors (32'hDEAD_BEEF). In comparison operations, match the literal width to the target signal width: count == 8'd255.

Synthesis Interpretation of Unknown Values

In logic synthesis, assigning x to an output tells the optimizer that the condition cannot occur:

case (sel)
    2'b00: out = a;
    2'b01: out = b;
    2'b10: out = c;
    2'b11: out = 2'bxx;   // Synthesis tool treats this as "don't care"
endcase

The synthesis tool minimizes logic gates by treating x as a “don’t care” condition. Only assign x when you can guarantee that the specified condition will never occur in physical operation.

Practice Exercise

Identify the syntax issues in the following code and rewrite the module using explicit sized literals:

module bad_literals (
    input  wire [7:0] data,
    output wire [7:0] masked,
    output wire [3:0] upper
);
    assign masked = data & 15;
    assign upper  = data >> 4;
endmodule
Solution
`timescale 1ns / 1ps
`default_nettype none

module good_literals (
    input  wire [7:0] data,
    output wire [7:0] masked,
    output wire [3:0] upper
);
    assign masked = data & 8'b0000_1111;   // Sized mask shows active bit positions
    assign upper  = data[7:4];             // Part-select states slice intent directly
endmodule

The constant 15 was an unsized 32-bit integer. The shift operation produced an 8-bit intermediate vector that was silently truncated to 4 bits. The revised code uses an explicit sized mask and a direct part-select.

The next article covers compiler directives, including the directive that converts undeclared signal typos into compile-time syntax errors.

← Parameters Compiler Directives →