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

Vectors and Arrays

A single-bit net represents one physical wire. Digital systems transfer data across multi-bit buses, such as 32-bit addresses and 64-bit data words. In Verilog, multi-bit buses are vectors. Slicing, concatenating, and indexing vectors are standard tasks in register-transfer level (RTL) design.

Vector Declarations

A vector is a wire or reg declared with a bit range. Standard conventions place the most significant bit (MSB) index first:

`timescale 1ns / 1ps
`default_nettype none

module vector_declarations (
    input  wire [31:0] data_in,
    output reg  [15:0] data_out
);

    wire [7:0]  byte_a;
    wire [15:0] halfword;
    wire [63:0] doubleword;
    reg  [3:0]  nibble;

    assign byte_a     = data_in[7:0];
    assign halfword   = data_in[15:0];
    assign doubleword = {32'h0, data_in};   // Zero-extend to 64 bits

    always @(*) begin
        data_out = data_in[31:16];
    end

endmodule

The total bit width equals the MSB index minus the LSB index plus one. The range [7:0] contains 8 bits, while [8:0] contains 9 bits. Always declare vector ranges in descending order ([N-1:0]). Ascending ranges ([0:7]) place the MSB at index 0 and can create indexing errors during vector assignments.

Bit Selection and Part Selection

You can select a single bit by using its index:

wire [31:0] word;
assign msb  = word[31];
assign lsb  = word[0];
assign bit7 = word[7];

The index can be a variable signal. Synthesis tools implement variable bit selection as a multiplexer.

Constant part-select operations extract a fixed bit slice:

assign byte3       = word[31:24];
assign byte0       = word[7:0];
assign nibble_high = word[7:4];
assign upper_half  = word[31:16];

Both slice bounds must be constants in standard part-select syntax.

Indexed Part-Selects

When selecting a multi-bit slice at a dynamic offset, use indexed part-select syntax (IEEE 1364-2001):

wire [31:0] word;
wire [1:0]  byte_sel;
wire [7:0]  selected_byte;

assign selected_byte = word[byte_sel*8 +: 8];   // 8 bits starting at bit byte_sel*8

The operator +: selects a fixed width of bits upward starting from a base index: [base +: width] spans [base+width-1 : base]. The operator -: selects bits downward: [base -: width] spans [base : base-width+1]. The width must be an elaboration-time constant. The base index can be a variable signal.

The following module extracts an 8-bit byte from a 32-bit word using an indexed part-select:

`timescale 1ns / 1ps
`default_nettype none

module byte_extractor (
    input  wire [31:0] word,
    input  wire [1:0]  byte_sel,   // 0 selects least significant byte
    output wire [7:0]  byte_out
);

    assign byte_out = word[byte_sel*8 +: 8];

endmodule

Vector Concatenation

The concatenation operator {} combines multiple signals and literals into a single wider vector:

wire [3:0] high_nibble, low_nibble;
wire [7:0] full_byte;

assign full_byte = {high_nibble, low_nibble};   // Maps to [7:4] and [3:0]

The resulting bit width equals the sum of the input widths. You can concatenate zero bits to extend word width and retain arithmetic carry bits:

wire [7:0] a;
wire [8:0] sum_no_overflow;

assign sum_no_overflow = {1'b0, a} + {1'b0, 8'd200};   // 9-bit addition

Concatenation also simplifies shift register implementation:

always @(posedge clk) begin
    shift_reg <= {shift_reg[6:0], data_in};   // Shift left; append new LSB
end

Vector Replication

The replication operator {N{expr}} repeats an expression N times inside an outer pair of braces:

wire [7:0]  byte_val;
wire [31:0] sign_extended;

// Replicate the sign bit (bit 7) 24 times
assign sign_extended = {{24{byte_val[7]}}, byte_val};

wire [15:0] pattern;
assign pattern = {4{4'hA}};   // Resolves to 16'hAAAA

Replication makes sign-extension logic explicit in RTL source files.

Memory Arrays

Declare an array by adding an unpacked dimension after the identifier name:

reg [7:0]  memory   [0:255];   // 256 words, 8 bits per word
reg [31:0] reg_file [0:31];    // 32 registers, 32 bits per register

The range before the identifier specifies the packed bit width per element. The range after the identifier specifies the unpacked array depth.

You must select an individual array element before accessing its bits:

`timescale 1ns / 1ps
`default_nettype none

module memory_model (
    input  wire       clk,
    input  wire       we,
    input  wire [7:0] addr,
    input  wire [7:0] data_in,
    output reg  [7:0] data_out
);

    reg [7:0] mem [0:255];

    always @(posedge clk) begin
        if (we)
            mem[addr] <= data_in;
        data_out <= mem[addr];    // Synchronous memory read
    end

endmodule

You cannot assign or read an entire array in a single expression:

reg [7:0] arr [0:3];
assign foo = arr;         // Illegal: direct whole-array assignment
assign foo = arr[2];      // Legal: selects single 8-bit element
assign bar = arr[1][3];   // Legal: selects bit 3 of element 1

Initializing ROM from External Files

The $readmemh system task loads array contents from a hexadecimal text file during simulation and FPGA synthesis:

`timescale 1ns / 1ps
`default_nettype none

module rom_256x8 (
    input  wire [7:0] addr,
    output reg  [7:0] data
);

    reg [7:0] rom_mem [0:255];

    initial begin
        $readmemh("rom_contents.hex", rom_mem);
    end

    always @(*) begin
        data = rom_mem[addr];
    end

endmodule

The system task $readmemb loads binary data. FPGA synthesis tools recognize arrays initialized with $readmemh inside initial blocks and infer block RAM primitives.

Practice Exercise

Given a 32-bit bus axi_data and a 2-bit selector sel, write assignments to perform these tasks:

  1. Extract byte 2 using a static part-select.
  2. Extract the byte selected by sel.
  3. Swap the upper and lower 16-bit halfwords.
  4. Sign-extend byte 0 to 32 bits.
Solution
`timescale 1ns / 1ps
`default_nettype none

module axi_manipulations (
    input  wire [31:0] axi_data,
    input  wire [1:0]  sel,
    output wire [7:0]  byte2_static,
    output wire [7:0]  byte_dynamic,
    output wire [31:0] swapped,
    output wire [31:0] sign_ext
);

    assign byte2_static = axi_data[23:16];
    assign byte_dynamic = axi_data[sel*8 +: 8];
    assign swapped      = {axi_data[15:0], axi_data[31:16]};
    assign sign_ext     = {{24{axi_data[7]}}, axi_data[7:0]};

endmodule

Vectors allow explicit declaration of data widths. The next article covers parameters and local parameters, which enable reusable and scalable module architectures.

← wire and reg Parameters →