SystemVerilog compile error when declaring interface for a module (undeclared identifier [12.5(IEEE)]) - interface

I am learning how to use interfaces to wrap around a DUT (top-level module entity) in SystemVerilog. So, for this purpose, I came up with a basic example where the DUT is a simple synchronous RAM.
However, while I try to compile my code, I receive an error for every signal declared in the interface and used in the module ("undeclared identifier [12.5(IEEE)]"). I hope to get the precious help from this community to understand my error. I kept the code short and hopefully readable. Thank you in advance!
I tried removing the parameters and transform them into fixed numbers as well as using the define directive to make them global but did not help. The error, in fact, shows up also with signals that are not parametrized (such as oe signal).
// ********** Define the INTERFACE TO MODULE RAM: **********
interface clocked_bus
#(
// ---------- Parameters definition: ----------
parameter MEM_WIDTH=16, // word size of the memory
parameter ADDR_WIDTH=8 // => [2^ADDR_WIDTH locations]
)
(
// ---------- I/Os declaration: ----------
input clk
);
// ---------- Ports declaration: ----------
logic wr_rd_n_en, oe;
logic [MEM_WIDTH-1:0] data_out;
logic [2**ADDR_WIDTH-1:0] addr;
logic [MEM_WIDTH-1:0] data_in;
endinterface
// ********** Define the MODULE RAM: **********
module RAM(input clk, clocked_bus cb);
// ---------- CREATION OF MEM MATRIX: ----------
logic [MEM_WIDTH-1:0] mem [2**ADDR_WIDTH-1:0];
// ---------- BEHAVIORAL ARCHITECTURE DEFINITION: ----------
always_ff#(posedge clk)
begin
if (wr_rd_n_en == 0)
if (oe ==1)
data_out <= mem[addr];
else
mem[addr] <= data_in;
end
endmodule
// ********** Define the MODULE RAM: **********
module top;
// Define the clock as 'free running process':
logic clk = 0;
always #10 clk = !clk;
// Instantiate the Interface:
clocked_bus #(.MEM_WIDTH(16), .ADDR_WIDTH(8)) cb(clk);
// Instantiate the DUT:
RAM mem1(clk, cb);
endmodule
I expect to compile however I get the following error:
interface worklib.clocked_bus:sv
errors: 0, warnings: 0
logic [MEM_WIDTH-1:0] mem [2**ADDR_WIDTH-1:0];
|
xmvlog: *E,UNDIDN (lab.sv,31|43): 'ADDR_WIDTH': undeclared identifier [12.5(IEEE)].
logic [MEM_WIDTH-1:0] mem [2**ADDR_WIDTH-1:0];
|
xmvlog: *E,UNDIDN (lab.sv,31|19): 'MEM_WIDTH': undeclared identifier [12.5(IEEE)].
if (wr_rd_n_en == 0)
|
xmvlog: *E,UNDIDN (lab.sv,36|21): 'wr_rd_n_en': undeclared identifier [12.5(IEEE)].
if (oe ==1)
|
xmvlog: *E,UNDIDN (lab.sv,37|17): 'oe': undeclared identifier [12.5(IEEE)].
data_out <= mem[addr];
|
xmvlog: *E,UNDIDN (lab.sv,38|23): 'data_out': undeclared identifier [12.5(IEEE)].
data_out <= mem[addr];
|
xmvlog: *E,UNDIDN (lab.sv,38|35): 'addr': undeclared identifier [12.5(IEEE)].
mem[addr] <= data_in;
|
xmvlog: *E,UNDIDN (lab.sv,40|19): 'addr': undeclared identifier [12.5(IEEE)].
mem[addr] <= data_in;
|
xmvlog: *E,UNDIDN (lab.sv,40|31): 'data_in': undeclared identifier [12.5(IEEE)].
module worklib.RAM:sv
errors: 8, warnings: 0
module worklib.top:sv
errors: 0, warnings: 0

When you access variables and parameters inside an interface, you should use the interface name to denote them. An interface provides a namespace capability by encapsulating those. Your code for RAM should look like the following:
module RAM(input clk, clocked_bus cb);
// ---------- CREATION OF MEM MATRIX: ----------
logic [cb.MEM_WIDTH-1:0] mem [2**cb.ADDR_WIDTH-1:0];
// ---------- BEHAVIORAL ARCHITECTURE DEFINITION: ----------
always_ff#(posedge clk)
begin
if (cb.wr_rd_n_en == 0)
if (cb.oe ==1)
cb.data_out <= mem[cb.addr];
else
mem[cb.addr] <= cb.data_in;
end
endmodule

Related

Connections between sub modules wrong

My codes for Alu and Mux:
module alu(input logic [31:0]srca, srcb,
input logic [2:0] alucontrol,
output logic zero,
output logic [31:0]aluout);
logic [31:0] addr, subr, sltr, Andr, Orr;
assign addr = srca + srcb;
assign subr = srca - srcb;
assign sltr = (srca < srcb) ? 1 : 0;
assign Andr = srca & srcb;
assign Orr = srca | srcb;
always_comb
begin
case(alucontrol)
3'b010: aluout = addr;
3'b110: aluout = subr;
3'b111: aluout = sltr;
3'b000: aluout = Andr;
3'b001: aluout = Orr;
default: aluout = 32'bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
endcase
zero = (subr == 0) ? 1 : 0;
end
endmodule
module mux2#(parameter WIDTH = 8)
(input logic [WIDTH-1:0] d0, d1,
input logic s,
output logic [WIDTH-1:0] y);
always_comb
begin
y = s?d1 : d0;
end
endmodule
and their instantiation under the same top module:
alu alu(srca, srcb, alucontrol, aluout, zero);
mux2 #(32) resmux(aluout, readdata, memtoreg, result);
When I try to connect my 2-1Mux resmux with my alu, the aluout doesn't get connected to resmux
aluout gets suspended
I could solve this by exchanging the order of aluout and zero, but could anybody explain why this happens and how to avoid? Thanks a lot!
There are a number of mechanisms for connecting ports in SystemVerilog. The mechanism you are using is positional. That means each signal connection has to go in the prescribed order that they appear in the module declaration of alu. The way your code is written, the signal zero in module top is connected to last port aluout declared in module alu.
SystemVerilog also has a by_name syntax .portname(signal). You can list all your port connections in any order that way.
alu alu(.srca(srca), .srcb(srcb), .alucontrol(alucontrol), .alu(aluout), .zero(zero));
When the signal name you are connecting matches the declared port name, you can just use .portname.
alu alu(.alucontrol, .aluout, .zero, .srca, .srcb);
And finally, if all the port names match the signal names, you can use a wildcard, .* as well as list exceptions explicitly
alu alu(.*, .zero(my_zero));

SystemVerilog Interface multiplexer

I´m new to systemverilog and trying to build a systemverilog testbench.
I have a DUT that should be connected to one of two external modules via a multiplexer. I want to switch the connection during simulation and I want to use systemverilog interfaces for the connection between the DUT and the multiplexer as well as the connection between multiplexer and the two external modules. The signals in the interface are bidirectional.
I am facing trouble writing the multiplexer. I´m getting an error for the current implementation that the LHS of the expression can´t be a wire. If I change the type in the interface to logic, I get an error that this is not possible with bidirectional signals.
I tried to google, but I didn´t find any tutorials on connecting interface to interface. Is this not possible? Or is there a better way of doing what I´m trying to do?
So far I have the following:
interface flash_connect_interface;
wire interface_f_cle;
wire interface_f_ale;
endinterface: flash_connect_interface
module flash_connect_testbench_top;
[...]
// Interfaces
flash_connect_interface flash_connect_interface_i0();
flash_connect_interface flash_connect_interface_i1();
flash_connect_interface flash_connect_interface_i2();
// Connecting DUT to interface
flash_connect flash_connect_i0(
.flash_connect_interface_i(flash_connect_interface_i0),
);
// Multiplexer
flash_connect_mux mux1(
.flash_connect_interface_i_0(flash_connect_interface_i0),
.flash_connect_interface_i_1(flash_connect_interface_i1),
.flash_connect_interface_i_2(flash_connect_interface_i2),
.select(sel)
);
nand_model nand_model0 (
.Cle (flash_connect_interface_i1.interface_f_cle),
.Ale (flash_connect_interface_i1.interface_f_ale),
);
nand_model nand_model1 (
.Cle (flash_connect_interface_i2.interface_f_cle),
.Ale (flash_connect_interface_i2.interface_f_ale),
);
[...]
endmodule // end testbench_top
module flash_connect_mux(
flash_connect_interface flash_connect_interface_i_0,
flash_connect_interface flash_connect_interface_i_1,
flash_connect_interface flash_connect_interface_i_2,
input select
);
always_comb begin
// *** Here is the problem ***
if (select == 1'b0) flash_connect_interface_i_1 = flash_connect_interface_i_0;
else flash_connect_interface_i_2 = flash_connect_interface_i_0;
end
endmodule
The interfaces in this case is just bundle of wires . There seems nothing apparently wrong with your code. But if you are trying to assign interface directly to each other based on the select signal it will not work. You will need to assign all the wires individually based on the select signal. There is nothing special to mux interfaces.
The code below does the muxing.
interface flash_connect_interface;
wire interface_f_cle;
wire interface_f_ale;
endinterface: flash_connect_interface
module nand_model ( inout Cle , inout Ale ) ; // Sample nand model
reg r = 1;
assign Cle = r?1:1'bz;
assign Ale = r?1:1'bz;
endmodule
module flash_connect_mux ( flash_connect_interface flash_connect_interface_i_0 , flash_connect_interface flash_connect_interface_i_1 , flash_connect_interface
flash_connect_interface_i_2 ,input [3:0] select ) ;
// Interconnect interface assignment
assign flash_connect_interface_i_0.interface_f_cle = (select== 0) ? flash_connect_interface_i_1.interface_f_cle : flash_connect_interface_i_2.interface_f_cle;
assign flash_connect_interface_i_0.interface_f_ale = (select== 0) ? flash_connect_interface_i_1.interface_f_ale : flash_connect_interface_i_2.interface_f_ale;
endmodule
module flash_connect ( flash_connect_interface flash_connect_interface_i ) ;
//check flash_connect_interface_i.interface_f_cle ;
//check flash_connect_interface_i.interface_f_ale ;
endmodule
module flash_connect_testbench_top;
reg [3:0] select ;
// Interfaces
flash_connect_interface flash_connect_interface_i0();
flash_connect_interface flash_connect_interface_i1();
flash_connect_interface flash_connect_interface_i2();
// Connecting DUT to interface
flash_connect flash_connect_i0(
.flash_connect_interface_i(flash_connect_interface_i0)
);
// Multiplexer
flash_connect_mux mux1(
.flash_connect_interface_i_0(flash_connect_interface_i0),
.flash_connect_interface_i_1(flash_connect_interface_i1),
.flash_connect_interface_i_2(flash_connect_interface_i2),
.select(select)
);
nand_model nand_model0 (
.Cle (flash_connect_interface_i1.interface_f_cle),
.Ale (flash_connect_interface_i1.interface_f_ale)
);
nand_model nand_model1 (
.Cle (flash_connect_interface_i2.interface_f_cle),
.Ale (flash_connect_interface_i2.interface_f_ale)
);
endmodule // end testbench_top
Link to interface tutorial -
https://www.doulos.com/knowhow/sysverilog/tutorial/interfaces/

can packed arrays be passed by reference to the task in systemverilog

Can s_clk be passed as argument to xyz task in below code?
module test(input logic m_clk, output [1:0] logic s_clk);
...
xyz (m_clk,s_clk);//assuming m_clks and s_clks are generated from top
...
task automatic xyz (ref logic clk1, ref [1:0] logic clk2);
...
endtask
endmodule
I have read your problem, first of all you have typo mistake
module test(input logic m_clk, output [1:0] logic s_clk);
task automatic xyz (ref logic clk1, ref [1:0] logic clk2);
instead of this you have to write
module test(input logic m_clk, output logic [1:0] s_clk);
task automatic xyz (ref logic clk1, ref logic [1:0] clk2);
For better understanding I have also share one demo code for packed arrays can be passed by reference to the task in systemverilog.
Here is code :
program main();
bit [31:0] a = 25;
initial
begin
#10 a = 7;
#10 a = 20;
#10 a = 3;
#10 $finish;
end
task pass_by_val(int i);
$monitor("===============================================%d",i);
forever
#a $display("pass_by_val: I is %0d",i);
endtask
task pass_by_ref(ref bit [31:0]i);
forever
begin
#a $display("pass_by_ref: I is %0d",i[0]);
$display("This is pass_by value a ====== %d \n a[0] ====== %0d ",a,a[0]);
end
endtask
initial
begin
pass_by_val(a);
end
initial
pass_by_ref(a);
endprogram
By running this example you can observe that packed arrays can be passed by reference to the task in systemverilog and its value is also reflected to it.
pass_by_val task will register the value of the variables
only once at the time when task is called. Subsequently when the variable changes its value, pass_by_val task cannot see the newer values. On the other hand, 'ref' variables in a task are registered whenever its value changes. As a result, when the variable 'a' value changes, the pass_by_ref task can register and display the value correctly.
I simulated Ashutosh Rawal's code and the output display is given below:
=============================================== 25
pass_by_val: I is 25
pass_by_ref: I is 1
This is pass_by value a ====== 7
a[0] ====== 1
pass_by_val: I is 25
pass_by_ref: I is 0
This is pass_by value a ====== 20
a[0] ====== 0
pass_by_val: I is 25
pass_by_ref: I is 1
This is pass_by value a ====== 3
a[0] ====== 1
$finish called from file "testbench.sv", line 13.
$finish at simulation time 40
V C S S i m u l a t i o n R e p o r t

Automatic SystemVerilog variable size using interface width and $size?

I am trying to make a module (a DSP in my case) with a standardized memory interface in SystemVerilog, and would like the variables in the module to size automatically based on the bus widths in the attached interface. My rationale: this makes the code more portable by allowing it to auto-size to any connected interface, instead of requiring an HDL coder to pass in parameters that tell the module the widths of all the interface busses that will connect to it (not that this would be terrible, it just seems cleaner without the parameters).
I can't seem to get this to work, however. Here's an example that illustrates the problem; the following synthesizes in Quartus II 12.1:
// Top level module with some 15-bit ports
module top ( input [14:0] data_in,
output [14:0] data_out1, data_out2,
data_out3, data_out4 );
test_interface my_interface(); // Initialize the interface
test_module my_module(.*); // Initialize & connect module
endmodule
// Define a simple interface:
interface test_interface ();
logic [8:0] my_port;
endinterface
// Define the module:
module test_module ( input [14:0] data_in,
test_interface my_interface,
output [14:0] data_out1, data_out2,
data_out3, data_out4 );
localparam width1 = $size(data_in); // should be 15
localparam width2 = $size(my_interface.my_port); // should be 9
logic [width1-1:0] auto_sized1; // gets correct size (14:0)
logic [width2-1:0] auto_sized2; // **PROBLEM**: gets size of 0:0!
always_comb begin
auto_sized1 = 5; // ok
auto_sized2 = 5; // problem; value now truncated to 1
data_out1 = data_in + width1; // Yields data_in + 15 (ok)
data_out2 = data_in + width2; // Yields data_in + 9 (ok...!)
data_out3 = data_in + auto_sized1; // Yields data_in + 5 (ok)
data_out4 = data_in + auto_sized2; // Yields data_in + 1 (problem)
end
endmodule
Note that width2 does eventually get the correct value (9) - just too late for it to correctly set the width of auto_sized2. I initially thought that $size was simply evaluated after all variables had been assigned their widths, but this doesn't seem to be the case either since $size(data_in) works just fine for setting the width of auto_sized1.
Any thoughts? Again, it's not critical to the project's success, I'm mostly curious at this point!
Thanks -
Looks like a compiler bug. I'd probably use a parameter in the interface definition.
module top ( input [14:0] data_in,
output [14:0] data_out1, data_out2,
data_out3, data_out4 );
test_interface #(.port_size(8)) my_interface(); // Initialize the interface
test_module my_module(.*); // Initialize & connect module
endmodule
interface test_interface ();
parameter port_size = 1;
logic [port_size-1:0] my_port;
endinterface
module test_module ( input [14:0] data_in,
test_interface my_interface,
output [14:0] data_out1, data_out2,
data_out3, data_out4 );
localparam width1 = $size(data_in);
localparam width2 = my_interface.port_size;
endmodule

How to use const in verilog

Instead of using
module ... ( .. ) ;
#15
endmodule
I want use
module ... ( ... ) ;
// GateDelay is a const, like in c language const int GateDelay = 15 ;
# GateDelay
endmodule
Or same thing
module ... ( ... ) ;
// assume Wordsize is defined at " define Wordsize 15 "
reg [ Wordsize -1 : 0 ] mem ;
endmodule
Can I do that wish in verilog ?
You've a few options :
Macros with `defines
parameters
localparams
Here's a small example with them all.
`define CONSTANT_MACRO 1 /* important: no ';' here */
module mymodule
#( parameter WIDTH = 5 )
(
input wire [WIDTH-1:0] in_a,
output wire [WIDTH-1:0] out_a
);
localparam CONSTANT_LOCAL = 2;
assign out_a = in_a + `CONSTANT_MACRO - CONSTANT_LOCAL;
endmodule
For the cases you listed, I would recommend parameters.
Like the C compiler directive, `define is global for the compilation. If your code is ever going to be used with code you don't control you will need to be careful here.
Parameters are always local to the module scope so identically named parameters in different design elements will not conflict with each other. They also have the advantage that they can be overridden on a per-instance basis.
module #(parameter DATA_WIDTH = 1) busSlave(
input [DATA_WIDTH-1:0] bus_data,
input bus_wr,
...
);
endmodule
module top;
//DATA_WIDTH is 32 in this instance
busSlave #(.DATA_WIDTH(32)) slave32(
.bus_data(data_0),
.bus_wr(wr_0),
...
);
//DATA_WIDTH is 64 in this instance
busSlave #(.DATA_WIDTH(64)) slave64(
.bus_data(data_1),
.bus_wr(wr_1),
...
);
endmodule