15 Extern
extern can be used to move class methods and constraints out of the body of the class. makes code more neat
extern a class method (and constraint) qualifier is used. extern can be used to move the implementation (i.e. declaration/body) of a method outside of a class. This can help make code more readable. Imagine a class that has many lines devoted to class and constraint declarations. Moving them outside (external to) the class can allow a quick skim of the class to see its contents and not be bothered with the particulars.
In this lesson, I experiment with explicit extern constraints, but my simulator version (irun 12.10-s010) does not support them.
extern - to move method and constraint bodies out of classes, for neater code
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
// "extern" see IEEE-1800-2009-SystemVerilog-std.pdf pgs 144-5,444
// moving methods/constraints out of classes
module dice_game;
class dice;
rand int value;
int stats[1:6] = {0,0,0,0,0,0};
constraint fixed_odds; //implicit external constraint (can skip declaration)
extern constraint even; //explicit external constraint (cannot skip declaration)
extern function void post_randomize();
extern function void show_stats();
endclass
constraint dice::even { (value%2)==0; }
//constraint dice::fixed_odds{ value dist {6,5,4,3,2,1}; }
function void dice::post_randomize();
$display(" dice roll is now = %0d", value);
stats[value] = stats[value]+1;
endfunction
function void dice::show_stats();
foreach (stats[iii]) begin
$display("%3d : %3d of %3d : %2.2f%",
iii, stats[iii], stats.sum(), (real'(stats[iii])/real'(stats.sum()))*100);
end
endfunction
initial begin
dice my_dice;
my_dice = new();
repeat (100) begin
assert (my_dice.randomize()) else begin
$display(" ERROR: Mister SVS, randomization failed, miserably.");
$fatal(1);
end
end
my_dice.show_stats();
end
endmodule // dice_game
.
//run command
1
irun -sv dice_game.sv