SystemVerilog OOP Mastery · All levels
Parameterized Classes with Type and Value Parameters: Code Examples
Code Examples for Parameterized Classes with Type and Value Parameters.
Code examples
Type and depth parameter in one class
systemverilog
class window #(type T = int, int N = 4);
T data[N];
function void set(int idx, T item);
if (idx < 0 || idx >= N) begin
$fatal(1, "idx=%0d out of range N=%0d", idx, N);
end
data[idx] = item;
endfunction
endclass
window#(bit [15:0], 8) w16 = new();
window#(string, 2) ws = new();The method signature is type-safe for each specialization, and N is a compile/elaboration-time constant that sizes the array.
Specialization aliases for readability
systemverilog
class fifo_model #(type T = int, int DEPTH = 8);
T q[$];
function void push(T item);
if (q.size() == DEPTH) $fatal(1, "fifo full");
q.push_back(item);
endfunction
endclass
typedef fifo_model#(bit [31:0], 32) word_fifo_t;
typedef fifo_model#(byte, 16) byte_fifo_t;typedef captures an approved specialization name so call sites do not repeat long #(type, value) argument lists.