Thursday, October 25, 2007

Interfaces in SystemVerilog

Connectivity across different design modules in Verilog is accomplished through port connections for each interface signal in instantiations. For large designs, this is not very productive as it involves redundant instances and signal declarations and is prone to error.

To resolve this issue, SystemVerilog adds a powerful interface construct to encapsulate the communication between blocks i.e. multiple signals are grouped together to form a single port. The signals declared in the interface definition is in a single location so that any changes in the interface can be captured accurately modifying only once instead of multiple modules as in Verilog. All the modules using this interface need only declare a single interface type port rather than multiple signals. An example of SV interface is below

SV:
  1. // AHB Interface Definition
  2. interface ahb_bus (input logic ahb_hclk, ahb_hresetn); // Interface can have ports.
  3. // These external signals (hclk, hresetn can be implicitly connected between modules
  4. wire [31:0] ahb_hdata;
  5. wire [31:0] ahb_addr;
  6. logic ahb_hwrite;
  7. logic [1:0] ahb_htrans;
  8. logic [2:0] ahb_hsize;
  9. logic [2:0] ahb_hburst;
  10. logic ahb_hready;
  11. logic [1:0] ahb_hresp;
  12. endinterface
  13. // Top level Module
  14. module ahb_top (input logic ahb_hclk, ahb_hresetn);
  15. ahb_bus bus (
  16. .ahb_hclk(ahb_hclk),
  17. .ahb_hresetn(ahb_hresetn) ); // instance of interface
  18. arm_core arm_inst (
  19. // ahb_bus ports
  20. .bus (bus),
  21. // other ports
  22. .port1 (port1) // not part of the interface
  23. ... );
  24. ahb_slave ahb_slave0 (
  25. .bus (bus) );
  26. dma_master dma_master_inst (
  27. .bus (bus),
  28. // Other ports
  29. ---
  30. );
  31. on_chip_mem on_chip_mem_inst (
  32. .bus (bus),
  33. // Other ports
  34. --
  35. );
  36. endmodule
  37. // Module Definitions
  38. module arm_core (
  39. ahb_bus bus, // interface port
  40. // other ports
  41. input logic port1
  42. );
  43. // functionality
  44. endmodule
  45. module ahb_slave (
  46. ahb_slave bus
  47. );
  48. logic [1:0] ahb_hsel;
  49. logic [31:0] slave_address;
  50. // functionality
  51. // Referring signals inside interface requires using interface port name
  52. assign bus.ahb_addr = (ahb_hsel == 2'b0) ? slave_address; 'z;
  53. endmodule
  54. module dma_master (
  55. ahb_bus bus;
  56. // Other ports
  57. ---
  58. );
  59. // functionality
  60. endmodule

An interface can also have tasks, functions, parameters, procedural blocks and type declarations to aid in defining the communication protocol. SV interface cannot have a design hierarchy i.e. no modules can be instantiated in the interface. A modport constructs is provided to define the direction for interface port for different modules. Selecting the appropriate modport for each module can be accomplished during either module definition or during module instance as shown in example below.

SV:
  1. interface ahb_bus (input logic ahb_hclk, ahb_hresetn); // Interface can have ports.
  2. wire [31:0] ahb_hdata;
  3. wire [31:0] ahb_addr;
  4. logic ahb_hwrite;
  5. logic [1:0] ahb_htrans;
  6. logic [2:0] ahb_hsize;
  7. logic [2:0] ahb_hburst;
  8. logic ahb_hready;
  9. logic [1:0] ahb_hresp;
  10. modport master ( inout ahb_hdata,
  11. output ahb_haddr,
  12. output ahb_hsize,
  13. input ahb_hready,
  14. ---
  15. );
  16. modport slave ( inout ahb_hdata,
  17. input ahb_haddr,
  18. input ahb_hsize,
  19. output ahb_hready,
  20. ---
  21. );
  22. endinterface
  23. module ahb_top (.. );
  24. ahb_bus bus (...);
  25. dma_master dma_master_inst (bus.master); // Use the master modport view
  26. ahb_slave ahb_slave0 (bus.slave); // Use the slave modport view
  27. endmodule
  28. // Alternatively, modport construct can be used in module definition
  29. module ahb_slave (ahb_bus.master bus);
  30. endmodule

If no modport is associated with a module, by default all net types are inout and all variable types are ref.

A generic interface port defines the port type using the keyword interface instead of using a name of a specific interface type - the advantage is being able to connect the module to different interfaces. Unlike Verilog, no interface port can be left unconnected and SV .name and .* connection rules can be also be used for interface instances.

SV:
  1. module ahb_slave (interface bus); // generic interface port
  2. ahb_slave ahb_slave0 (bus.slave);

SystemVerilog allows tasks and functions to be declared within interface definitions known as interface methods. The task or function has the same syntax as when declared in a module. Using these interface methods, the communication protocol details can be embedded within the interface definition itself.

SV:
  1. interface ahb_bus (input logic ahb_hclk, ahb_hresetn); // Interface can have ports.
  2. wire [31:0] ahb_hdata;
  3. wire [31:0] ahb_addr;
  4. logic ahb_hwrite;
  5. logic [1:0] ahb_htrans;
  6. logic [2:0] ahb_hsize;
  7. logic [2:0] ahb_hburst;
  8. logic ahb_hready;
  9. logic [1:0] ahb_hresp;
  10. // Task defined internal to interface
  11. task slave_read ( input logic [31:0] raddr);
  12. // ...
  13. endtask
  14. endinterface

Interface methods can also be imported if they are not defined within the interface definition itself. If an interface is connected using modport construct, then the import keyword is used to specify the method. Alternative way is to add the task keyword next to the import and also include function arguments - this is required if the task is exported from an external module.

SV:
  1. interface ahb_bus (input logic ahb_hclk, ahb_hresetn); // Interface can have ports.
  2. wire [31:0] ahb_hdata;
  3. wire [31:0] ahb_addr;
  4. logic ahb_hwrite;
  5. logic [1:0] ahb_htrans;
  6. logic [2:0] ahb_hsize;
  7. logic [2:0] ahb_hburst;
  8. logic ahb_hready;
  9. logic [1:0] ahb_hresp;
  10. modport slave ( import slave_read, // simplest way of importing tasks
  11. inout ahb_hdata,
  12. input ahb_haddr,
  13. input ahb_hsize,
  14. output ahb_hready,
  15. ---
  16. );
  17. // Alternative explicit way
  18. modport slave ( import task slave_read (input [31:0] addr), // explicit
  19. inout ahb_hdata,
  20. input ahb_haddr,
  21. input ahb_hsize,
  22. output ahb_hready,
  23. ---
  24. );

Importing a task or a function through a modport gives the module access to that task by prepending the interface port name to the task name as with other variables. The imported function/task must be declared as automatic in order to be synthesizable.

SV also includes a way to export a task defined in one module to be available to other modules through an interface. For example, if a function is defined in module A and is exported in the modport construct within interface definition using the export keyword, then the task is available to module B that uses that modport. However, this is not synthesizable and we cannot export the same function from multiple instances of a module.

It is also possible to define a task or function using extern keyword without associating it with the modport construct.

SV:
  1. interface shb_bus (....)
  2. ---
  3. extern check_parity (input logic [31:0] data); // Not associated with modport
  4. modport ahb_slave (...);
  5. endinterface
  6. module A (...);
  7. task check_parity (input logic [31:0] data);
  8. // --
  9. endtask
  10. endmodule

Interfaces can also contain procedural blocks like always, always_ff, parameters and generate statements similar to modules.


No comments: