arrays_sv_ex
arrays_sv_ex
module Adder_4bit (
input logic [3:0] A, // 4-bit input A
input logic [3:0] B, // 4-bit input B
input logic Cin, // Carry-in
output logic [3:0] Sum, // 4-bit Sum
output logic Cout // Carry-out
);
always_comb begin
{Cout, Sum} = A + B + Cin;
end
endmodule
2. Testbench for 4-bit Adder
A simple testbench that instantiates the adder and applies test vectors.
module tb_Adder_4bit;
logic [3:0] A, B;
logic Cin, Cout;
logic [3:0] Sum;
initial begin
// Apply test vectors
$display("Test Start");
A = 4'b0001; B = 4'b0010; Cin = 0; #10;
$display("A = %b, B = %b, Cin = %b, Sum = %b, Cout = %b", A, B, Cin, Sum,
Cout);
$stop;
end
endmodule
3. D Flip-Flop with Enable
A simple D flip-flop with an enable signal.
module DFF_En (
input logic D, // Data input
input logic clk, // Clock input
input logic en, // Enable input
output logic Q // Output
);
always_ff @(posedge clk) begin
if (en)
Q <= D;
end
endmodule
4. FSM (Finite State Machine)
A simple 2-state FSM design that toggles its state based on an input signal.
module fsm (
input logic clk, // Clock signal
input logic rst, // Reset signal
input logic in, // Input signal
output logic out // Output signal
);
typedef enum logic [1:0] {
S0 = 2'b00,
S1 = 2'b01
} state_t;
always_comb begin
case (state)
S0: out = 0;
S1: out = 1;
default: out = 0;
endcase
end
endmodule
5. Testbench for FSM
A simple testbench for the FSM module.
module tb_fsm;
logic clk, rst, in;
logic out;
// Clock generation
always #5 clk = ~clk;
initial begin
// Initialize signals
clk = 0;
rst = 1;
in = 0;
module counter #(
parameter WIDTH = 4 // Default to 4-bit counter
)(
input logic clk,
input logic rst,
output logic [WIDTH-1:0] count
);
always_ff @(posedge clk or posedge rst) begin
if (rst)
count <= 0;
else
count <= count + 1;
end
endmodule
7. Assertions Example
Assertions are a key feature in SystemVerilog used for verifying design
correctness. Here’s an example of using an assert statement for checking valid
transitions in a 2-bit counter.
module counter_with_assert (
input logic clk,
input logic rst,
output logic [1:0] count
);
always_ff @(posedge clk or posedge rst) begin
if (rst)
count <= 0;
else
count <= count + 1;
end
class coverage_example;
covergroup counter_cover @(posedge clk);
coverpoint count;
endgroup
counter_cover cvr;
function new();
cvr = new;
endfunction
endclass
module counter_with_coverage (
input logic clk,
input logic rst,
output logic [1:0] count
);
coverage_example cov;
initial begin
cov = new();
end
endmodule
9. Random Number Generation
A simple example of generating random numbers using SystemVerilog's randomization
features.
module random_gen (
output logic [7:0] random_val
);
initial begin
// Randomize the value
randomize(random_val);
$display("Random value: %0d", random_val);
end
endmodule
10. Memory Array
A simple memory block design using a 2D array for RAM.
module memory (
input logic [3:0] addr, // Address
input logic [7:0] data_in, // Data input
input logic we, // Write enable
output logic [7:0] data_out // Data output
);
logic [7:0] mem [0:15]; // 16 words of 8 bits
1. 1D Array Example:
A simple example of a fixed-size 1D array.
module array_example;
logic [7:0] arr [0:3]; // Declare a 1D array with 4 elements (each 8 bits
wide)
initial begin
// Initialize the array elements
arr[0] = 8'hFF;
arr[1] = 8'hA5;
arr[2] = 8'h1F;
arr[3] = 8'h00;
module array_2d_example;
logic [7:0] matrix [0:3][0:2]; // Declare a 2D array (4 rows, 3 columns)
initial begin
// Initialize the 2D array elements
matrix[0][0] = 8'h1;
matrix[0][1] = 8'h2;
matrix[0][2] = 8'h3;
matrix[1][0] = 8'h4;
matrix[1][1] = 8'h5;
matrix[1][2] = 8'h6;
matrix[2][0] = 8'h7;
matrix[2][1] = 8'h8;
matrix[2][2] = 8'h9;
matrix[3][0] = 8'hA;
matrix[3][1] = 8'hB;
matrix[3][2] = 8'hC;
module dynamic_array_example;
logic [7:0] dynamic_array[]; // Declare a dynamic array (size unknown at
compile time)
integer i;
initial begin
// Allocate memory for the dynamic array
dynamic_array = new[5]; // Create an array of size 5
module associative_array_example;
// Declare an associative array with string keys and 8-bit values
typedef logic [7:0] my_data_t;
my_data_t assoc_array[string]; // Key is a string, value is an 8-bit logic
initial begin
// Assign values using string keys
assoc_array["apple"] = 8'hFF;
assoc_array["banana"] = 8'hA5;
assoc_array["cherry"] = 8'h1F;
module queue_example;
// Declare a queue of 8-bit logic values
logic [7:0] queue[$]; // '$' denotes a dynamic size for the queue
initial begin
// Enqueue some values
queue.push_back(8'h01);
queue.push_back(8'h02);
queue.push_back(8'h03);
Here are some more practical SystemVerilog examples that utilize arrays and other
features of the language. These examples showcase more advanced use cases, such as
managing memory, working with different types of arrays, and building testbenches.
module memory_model (
input logic [7:0] addr, // 8-bit address input
input logic [7:0] data_in, // Data input for writing
input logic write_enable, // Write enable signal
output logic [7:0] data_out // Data output
);
// Declare a 2D array to simulate a simple RAM (256 bytes)
logic [7:0] ram [0:255]; // 256 8-bit wide memory
endmodule
2. Multi-Dimensional Array with Testbench
This example uses a 3D array to represent a simple 3D memory model, like a volume
of data. It also includes a testbench to test it.
module memory_3d_example;
logic [7:0] memory [0:3][0:3][0:3]; // 3x3x3 memory (3D)
initial begin
// Initialize the 3D array with some values
memory[0][0][0] = 8'h1;
memory[0][0][1] = 8'h2;
memory[0][0][2] = 8'h3;
memory[0][1][0] = 8'h4;
memory[0][1][1] = 8'h5;
memory[0][1][2] = 8'h6;
memory[0][2][0] = 8'h7;
memory[0][2][1] = 8'h8;
memory[0][2][2] = 8'h9;
initial begin
// Initialize the array
for (int i = 0; i < DEPTH; i++) begin
arr[i] = i * 2;
end
systemverilog
Copy code
module dynamic_push_pop;
logic [7:0] dynamic_array[]; // Declare a dynamic array of 8-bit values
initial begin
// Allocate space for 4 elements
dynamic_array = new[4];
module associative_array_example;
// Declare an associative array with integer keys and 8-bit values
logic [7:0] my_array [int]; // Array indexed by integers
initial begin
// Store values in the associative array
my_array[100] = 8'hAA;
my_array[200] = 8'hBB;
my_array[300] = 8'hCC;
module queue_with_size_limit;
logic [7:0] fifo_queue[$]; // Declare a queue of 8-bit wide elements
integer max_size = 5;
initial begin
// Enqueue elements
if (fifo_queue.size() < max_size) fifo_queue.push_back(8'h01);
if (fifo_queue.size() < max_size) fifo_queue.push_back(8'h02);
if (fifo_queue.size() < max_size) fifo_queue.push_back(8'h03);
module functional_coverage_array;
logic [7:0] test_array [0:7]; // Array of 8 elements (8-bit wide)
covergroup cg @(posedge clk);
coverpoint test_array[0]; // Coverage for first element
coverpoint test_array[1]; // Coverage for second element
coverpoint test_array[2]; // Coverage for third element
coverpoint test_array[3]; // Coverage for fourth element
coverpoint test_array[4]; // Coverage for fifth element
coverpoint test_array[5]; // Coverage for sixth element
coverpoint test_array[6]; // Coverage for seventh element
coverpoint test_array[7]; // Coverage for eighth element
endgroup
initial begin
// Initialize the array
for (int i = 0; i < 8; i++) begin
test_array[i] = i;
end
systemverilog
Copy code
module bit_select_example;
logic [15:0] data [0:3]; // Declare a 1D array of 16-bit data (4 elements)
initial begin
// Initialize the array
data[0] = 16'hABCD;
data[1] = 16'h1234;
data[2] = 16'h5678;
data[3] = 16'h9ABC;
systemverilog
Copy code
module array_slicing;
logic [7:0] arr [0:7]; // Declare an 8-element array of 8-bit values
initial begin
// Initialize the array
arr[0] = 8'h01;
arr[1] = 8'h02;
arr[2] = 8'h03;
arr[3] = 8'h04;
arr[4] = 8'h05;
arr[5] = 8'h06;
arr[6] = 8'h07;
arr[7] = 8'h08;
systemverilog
Copy code
module array_of_structs;
// Define a struct for storing 8-bit data and a flag
typedef struct {
logic [7:0] data; // 8-bit data
logic valid; // Validity flag
} data_t;
systemverilog
Copy code
module string_array_example;
string names [0:3]; // Declare a string array with 4 elements
initial begin
// Initialize the string array
names[0] = "Alice";
names[1] = "Bob";
names[2] = "Charlie";
names[3] = "David";
systemverilog
Copy code
module dynamic_array_reallocation;
logic [7:0] dyn_arr[]; // Declare a dynamic array of 8-bit values
initial begin
// Initial allocation of 5 elements
dyn_arr = new[5];
systemverilog
Copy code
module multidimensional_array;
logic [7:0] matrix [0:2][0:2]; // Declare a 3x3 matrix
initial begin
// Initialize the matrix with values
for (int i = 0; i < 3; i++) begin
for (int j = 0; j < 3; j++) begin
matrix[i][j] = (i + 1) * (j + 1);
end
end
module array_of_queues;
// Declare an array of queues, each holding 8-bit values
logic [7:0] queue_array [0:2] [$]; // 3 queues, dynamic size
initial begin
// Initialize and enqueue values to each queue
queue_array[0].push_back(8'h11);
queue_array[0].push_back(8'h22);
queue_array[1].push_back(8'h33);
queue_array[1].push_back(8'h44);
queue_array[2].push_back(8'h55);
queue_array[2].push_back(8'h66);
// Dequeue and print from each queue
$display("Queue 0: %h, %h", queue_array[0].pop_front(),
queue_array[0].pop_front());
$display("Queue 1: %h, %h", queue_array[1].pop_front(),
queue_array[1].pop_front());
$display("Queue 2: %h, %h", queue_array[2].pop_front(),
queue_array[2].pop_front());
end
endmodule
8. Array-Based Testbench with Randomized Data
This example shows how to use randomization with arrays to create test data for
verification purposes.
module array_randomization;
logic [7:0] test_data [0:9]; // Declare an array of 10 elements for random
data
initial begin
// Randomize the array elements
foreach (test_data[i]) begin
test_data[i] = $random;
end
module memory_read_write;
logic [7:0] memory [0:15]; // Declare 16-byte memory
// Write function
task write(input logic [3:0] addr, input logic [7:0] data_in);
memory[addr] = data_in;
endtask
// Read function
function logic [7:0] read(input logic [3:0] addr);
return memory[addr];
endfunction
initial begin
// Writing to memory
write(4'h0, 8'hAA);
write(4'h1, 8'hBB);
write(4'h2, 8'hCC);
SystemVerilog offers even more powerful features related to arrays and other types
of collections that I didn't explicitly highlight in the previous examples. Here
are a few additional array-related concepts and operations that may be of interest
and weren't fully covered:
1. Packed Arrays
Packed arrays are arrays where the individual elements are packed together into a
contiguous bitfield. This is useful when you need a compact, contiguous
representation of data (e.g., for hardware registers or signals).
module packed_array_example;
logic [7:0] packed_arr [0:3]; // A packed array of 8-bit wide values
initial begin
packed_arr[0] = 8'hFF; // Assign 8-bit values
packed_arr[1] = 8'h0F;
packed_arr[2] = 8'hA1;
packed_arr[3] = 8'h5A;
module associative_array_enum_example;
typedef enum logic [1:0] {
IDLE = 2'b00,
RUNNING = 2'b01,
STOPPED = 2'b10
} state_t;
initial begin
// Initialize values for each enum state
state_data[IDLE] = 8'h01;
state_data[RUNNING] = 8'hFF;
state_data[STOPPED] = 8'h00;
module dynamic_array_no_new;
logic [7:0] arr[]; // Declare a dynamically sized array without explicit
allocation
initial begin
// Initially, the array is empty
$display("Array size initially: %0d", arr.size());
// Print elements
foreach (arr[i]) begin
$display("arr[%0d] = %h", i, arr[i]);
end
end
endmodule
This example uses direct array assignment, where the array is resized dynamically
based on the size of the assigned values.
4. Array-Based Functions
You can pass arrays to functions, which is useful when encapsulating array-related
logic in reusable functions.
module array_function_example;
logic [7:0] arr [0:4]; // Declare a 5-element array
initial begin
// Initialize the array
arr[0] = 8'h01;
arr[1] = 8'h02;
arr[2] = 8'h03;
arr[3] = 8'h04;
arr[4] = 8'h05;
module dynamic_array_remove;
logic [7:0] dyn_arr[]; // Declare a dynamic array
initial begin
// Initialize the array with some values
dyn_arr = new[5];
dyn_arr[0] = 8'hA1;
dyn_arr[1] = 8'hB2;
dyn_arr[2] = 8'hC3;
dyn_arr[3] = 8'hD4;
dyn_arr[4] = 8'hE5;
module nested_array_example;
logic [7:0] table [0:2][0:3]; // Declare a 2D array (3 rows, 4 columns)
initial begin
// Initialize the 2D array with values
table[0] = '{8'h01, 8'h02, 8'h03, 8'h04};
table[1] = '{8'h05, 8'h06, 8'h07, 8'h08};
table[2] = '{8'h09, 8'h0A, 8'h0B, 8'h0C};
module sort_array_example;
logic [7:0] arr [0:5]; // Declare an array
initial begin
// Initialize the array
arr[0] = 8'hA1;
arr[1] = 8'h5B;
arr[2] = 8'h30;
arr[3] = 8'hFF;
arr[4] = 8'h12;
arr[5] = 8'h7A;
initial begin
str_arr[0] = "Hello";
str_arr[1] = "SystemVerilog";
str_arr[2] = "Array Operations";
str_arr[3] = "Example";