What is the error in this code ? It keeps on giving me the following syntax error:
testbench.sv(7): near “constraint”: syntax error, unexpected constraint, expecting function or task
Here is the code:
`timescale 1ns/1ps
class generator;
randc bit [7:0] x, y, z;
extern constraint data; //calling the external constraint
extern function void display(); //calling the external display function
endclass
constraint generator::data { //defining constraint outside class
x inside {[0:50]};
y inside {[0:50]};
z inside {[0:50]};
};
function void generator::display(); //defining function outside class
$display ("Values of x:%0d, y:%0d and z:%0d",x,y,z);
endfunction
module tb;
generator g;
int i=0;
g=new();
initial begin
for(i=0;i<20;i++)begin
assert(g.randomize()) else begin
$display("Randomization failed");
$finish();
end
g.display();
#20;
end
end
endmodule
There were several syntax errors in your code which prevented clean compilation. Below is the fixed code. However, the code only works on 3 of the 4 simulators on EDA Playground, with the failing one reporting that ‘extern’ is an invalid qualifier for the constraint declaration.
Per the LRM, you can explicitly use ‘extern’, or it may also be implicit if there is no actual constraint provided. If you remove the ‘extern’ qualifier, it works in all 4 simulators.
For the tool issue, you will need to report this to the vendor support team.
`timescale 1ns/1ps
class generator;
randc bit [7:0] x, y, z;
extern constraint data; //calling the external constraint
extern function void display(); //calling the external display function
endclass
constraint generator::data { //defining constraint outside class
x inside {[0:50]};
y inside {[0:50]};
z inside {[0:50]};
}
function void generator::display(); //defining function outside class
$display ("Values of x:%0d, y:%0d and z:%0d",x,y,z);
endfunction
module tb;
generator g;
int i=0;
initial begin
g=new();
for (i=0;i<20;i++) begin
if (!g.randomize()) $display("Randomization failed");
else g.display();
end
end
endmodule