File: t_constraint_assoc_arr_others.v

package info (click to toggle)
verilator 5.038-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 162,552 kB
  • sloc: cpp: 139,204; python: 20,931; ansic: 10,222; yacc: 6,000; lex: 1,925; makefile: 1,260; sh: 494; perl: 282; fortran: 22
file content (100 lines) | stat: -rwxr-xr-x 2,424 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2025 by PlanV GmbH.
// SPDX-License-Identifier: CC0-1.0

// Enum-based associative array
typedef enum { RED, GREEN, YELLOW } color_t;

class AssocArrayEnum;
    rand bit [7:0] colors [color_t];
    constraint c1 { foreach (colors[i]) colors[i] == 4; }

    function new();
        colors[RED] = 8'd5;
        colors[GREEN] = 8'd10;
        colors[YELLOW] = 8'd15;
    endfunction

    function void self_check();
        foreach (colors[i]) begin
            if (colors[i] != 4) $stop;
        end
    endfunction
endclass

// Struct (packed) index associative array
typedef struct packed {
    bit [2:0] high;
    bit [1:0] low;
} PackedIndexType;

class AssocArrayPackedStruct;
    rand bit [31:0] data [PackedIndexType];
    constraint c2 { foreach (data[i]) data[i] == 100; }

    function new();
        PackedIndexType idx;
        idx.high = 3'd1;
        idx.low = 2'd1;
        data[idx] = 32'd50;
    endfunction

    function void self_check();
        foreach (data[i]) begin
            if (data[i] != 100) $stop;
        end
    endfunction
endclass

// Array (packed) index associative array
typedef logic [2:0][7:0] IndexArrayType;

class AssocArrayArrayIndex;
    rand bit [31:0] data [IndexArrayType];
    constraint c3 { foreach (data[i]) data[i] == 0; }

    function new();
        IndexArrayType idx;
        idx = 0;
        data[idx] = 32'd75;
    endfunction

    function void self_check();
        foreach (data[i]) begin
            if (data[i] != 0) $stop;
        end
    endfunction
endclass

module t_constraint_assoc_array_others;

    AssocArrayEnum enum_arr;
    AssocArrayPackedStruct packed_arr;
    AssocArrayArrayIndex array_arr;
    int success = 0;

    initial begin
        // Create instances of the classes
        enum_arr = new();
        packed_arr = new();
        array_arr = new();

        // Randomization and self-check
        success = enum_arr.randomize();
        if (success != 1) $stop;
        enum_arr.self_check();

        success = packed_arr.randomize();
        if (success != 1) $stop;
        packed_arr.self_check();

        success = array_arr.randomize();
        if (success != 1) $stop;
        array_arr.self_check();

        $write("*-* All Finished *-*");
        $finish;
    end
endmodule