- Verilog Basics
- Verilog Introduction
- Verilog Install
- Verilog Datatypes
- Verilog Conditions
- Verilog Loops
- Verilog Objects
- Verilog Libs
- Verilog Control Flow
- Verilog Modules
- Verilog Ports
- Verilog Operators
- Verilog Procedural Blocks
- Verilog Wires
- Verilog - Advanced
- Verilog Param-Modules
- Verilog Interfaces
- Verilog Classes
- Verilog Assertions
- Verilog Generating Blocks
- Verilog Clocking Blocks
- Verilog State Machines
- Verilog Coverage
- Verilog Race Conditions
- Verilog Timing
- Verilog Testing
Verilog Interfaces
Verilog Interfaces
In Verilog (specifically SystemVerilog), interfaces are used to bundle together groups of related signals into a single entity. This simplifies module connections, especially when modules need to communicate using a large number of signals. Instead of passing each signal individually between modules, you can use an interface that encapsulates all these signals.
Basic Interface Declaration
An interface in Verilog is defined using the interface keyword. Inside the interface, you can declare variables such as wire, reg, or logic and even include procedural code.
veriloginterface bus_if; logic clk; // Clock signal logic reset; // Reset signal logic [7:0] data; // 8-bit data bus logic read; // Read control signal logic write; // Write control signal endinterface
In this example, the bus_if interface groups the following signals together:
clk(clock)reset(reset)data(8-bit data bus)read(read control signal)write(write control signal)
Using Interfaces in Modules
Once an interface is defined, it can be used in a module by passing it as a single entity. The module can access all signals declared inside the interface.
verilogmodule master (bus_if bus); always @(posedge bus.clk or posedge bus.reset) begin if (bus.reset) begin bus.data <= 8'b0; bus.write <= 0; bus.read <= 0; end else begin bus.data <= 8'hAA; // Write some data bus.write <= 1; end end endmodule
In this example:
- The
mastermodule uses thebus_ifinterface. - The signals in the interface (
clk,reset,data,read,write) are accessed viabus.
Instantiating the Interface
To instantiate the interface in a top-level module, declare the interface and connect it to the modules that will use it.
verilogmodule top; bus_if my_bus(); // Instantiate the interface master m1 (.bus(my_bus)); // Connect the master to the interface slave s1 (.bus(my_bus)); // Connect the slave to the interface endmodule
Here:
my_busis an instance of thebus_ifinterface.- The
masterandslavemodules are both connected to this interface instance, allowing them to share the same set of signals.
Modports
Modports control how a module can interact with an interface. Using modports, you can specify which signals are inputs, outputs, or bidirectional for specific modules.
veriloginterface bus_if; logic [7:0] data; logic clk, reset; modport master (input clk, reset, output data); // Master can write to 'data' modport slave (input clk, reset, input data); // Slave can read from 'data' endinterface
With modports:
- The
mastermodport definesdataas an output (write access). - The
slavemodport definesdataas an input (read access).
Modules Using Modports
When using modports, modules must specify the modport they are accessing.
verilogmodule master (bus_if.master bus); always @(posedge bus.clk) begin bus.data <= 8'hFF; // Master writes to data end endmodule module slave (bus_if.slave bus); always @(posedge bus.clk) begin $display("Slave received data: %h", bus.data); // Slave reads from data end endmodule
Top-Level Module with Modports
verilogmodule top; bus_if my_bus(); // Instantiate the interface master m1 (.bus(my_bus.master)); // Connect master to modport slave s1 (.bus(my_bus.slave)); // Connect slave to modport endmodule
In this top-level module:
- The
masteris connected to themastermodport, giving it write access todata. - The
slaveis connected to theslavemodport, giving it read access todata.
Complex Interfaces with Tasks and Functions
An interface can also include tasks and functions to encapsulate behavior.
veriloginterface bus_if; logic [7:0] data; logic clk, reset; task reset_signals; data = 0; endtask endinterface
In this example, the bus_if interface includes a reset_signals task that can be used by any module connected to the interface.