File: t_constraint_mode.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 (99 lines) | stat: -rw-r--r-- 2,484 bytes parent folder | download | duplicates (2)
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
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2023 by Wilson Snyder.
// SPDX-License-Identifier: CC0-1.0

class Foo;
  rand int x;
endclass

class Bar extends Foo;
  rand int y;

  constraint cons_x {x > 0; x < 10;};
  constraint cons_y {y == 10;};
endclass

class Qux extends Bar;
  rand int z;
  rand Bar bar;
  constraint cons_z {z == x || z == y;};

  function new;
    bar = new;
  endfunction

  function void test;
    logic[1:0] ok = 0;
    cons_x.constraint_mode(1);
    if (cons_x.constraint_mode == 0) $stop;
    cons_y.constraint_mode(0);
    if (cons_y.constraint_mode == 1) $stop;
    bar.cons_x.constraint_mode(0);
    if (bar.cons_x.constraint_mode == 1) $stop;
    for (int i = 0; i < 20; ++i) begin
      x = 11;
      y = 12;
      z = 13;
      bar.x = 8;
      bar.y = 9;
      void'(randomize());
      if (x <= 0 || x >= 10) $stop;
      if (y != 10 && y != 12) ok[0] = 1;
      if (z != x && z != y) $stop;
      if (bar.x <= 0 || bar.x >= 10) ok[1] = 1;
      if (bar.y != 10) $stop;
    end
    if (ok != 2'b11) $stop;
    constraint_mode(1);
    if (cons_x.constraint_mode != 1) $stop;
    if (cons_y.constraint_mode != 1) $stop;
    if (cons_z.constraint_mode != 1) $stop;
    x = 14;
    y = 15;
    z = 16;
    void'(randomize());
    if (x <= 0 || x >= 10) $stop;
    if (y != 10) $stop;
    if (z != x && z != y) $stop;
  endfunction
endclass

module t;
  initial begin
    logic[1:0] ok = 0;
    int res;
    Qux qux = new;
    Bar bar = qux;

    qux.test;

    qux.bar.constraint_mode(1);
    bar.cons_y.constraint_mode(1);
    if (bar.cons_y.constraint_mode == 0) $stop;
    qux.cons_z.constraint_mode(0);
    if (qux.cons_z.constraint_mode == 1) $stop;
    qux.bar.cons_y.constraint_mode(0);
    if (qux.bar.cons_y.constraint_mode == 1) $stop;
    for (int i = 0; i < 20; ++i) begin
       qux.x = 17;
       qux.y = 18;
       qux.z = 19;
       qux.bar.x = 20;
       qux.bar.y = 10;
       void'(bar.randomize());
       if (qux.x <= 0 || qux.x >= 10) $stop;
       if (qux.y != 10 && qux.y != 12) $stop;
       if (qux.z != qux.x && qux.z != qux.y) ok[0] = 1;
       if (qux.bar.x <= 0 || qux.bar.x >= 10) $stop;
       if (qux.bar.y != 10) ok[1] = 1;
       res = qux.randomize() with {z == 100;};
       if (qux.z != 100) $stop;
    end
    if (ok != 2'b11) $stop;

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