Reputation: 1187
Suppose I have a (not synthesizable) simulation-only Verilog module.
How can I detect/enforce if it is elaborated more than once anywhere in a simulation?
module Singleton();
// I only want one of these...
endmodule
module GOOD_Design();
Singleton singleton();
endmodule
module BAD_Design();
Singleton singleton1();
Singleton singleton2();
endmodule
package SingletonPkg();
static bit once = 1'b0;
endpackage
module Singleton();
initial begin: singleton
if (SingletonPkg::once == 1'b1)
$fatal(2);
SingletonPkg::once = 1'b1;
end
...
endmodule: Singleton
Seems a lot of work (and I still have to make the test/set into an atomic operation). I could use the same approach with a DPI function as well, and keep the bit in C instead of SV.
Nevertheless, is there a better/simpler way?
Upvotes: 1
Views: 200
Reputation: 12344
and without classes
int count = 0;
module Singleton();
initial begin
if (count != 0)
$error("Multiple instances of %m");
count ++;
end
endmodule
module BAD_Design();
Singleton singleton1();
Singleton singleton2();
endmodule
Upvotes: 2
Reputation: 42623
You can make it slightly simpler by using a class
package SingletonPkg;
class C;
static bit once = 1'b0;
function new;
if (once++ != 0) $fatal(2);
endfunction
endclass
endpackage
module Singleton();
SingletonPkg::C = new;
...
endmodule: Singleton
Upvotes: 1