SystemVerilog语法中,在Class中引用层次化信号

在class中可以像在verilog中一样,直接在class中引用层次化信号。示例如下:

1.DUT模块,文件名为top.v。

module top
(
	input 				clk			,
	input 				rst_n		,
	//总线信号	
	input 				wr_n		,
	input 				rd_n		,
	input 				cs0_n		,
	input 				cs7_n		);

2.cpu类,文件名为cpu.sv,在cpu类中可以通过.运算符直接引用其他模块的信号,如tb.top_inst.syn_rst_n。这么做的缺点就是,降低了类的封装性,增加了与其他模块的耦合性。

`include "tb_interface.sv"

class cpu;

virtual top_if  cpu_if;//声明虚拟接口

function new(virtual top_if  watch_dog_interface);//在构造函数中将虚拟接口传递到类变量中
	cpu_if=watch_dog_interface;

endfunction  

task signal_synchronous(logic [2:0]  signal);
	   wait(    tb.top_inst.syn_rst_n);//在类中直接引用其他模块的信号、变量
	   
	   @(posedge tb.top_inst.sys_clk);
			#1 ;
			cpu_if.cs0_n<=signal[0];//通过接口对接口中的信号赋值
			cpu_if.rd_n<=signal[1];
			cpu_if.wr_n<=signal[2];

	   @(posedge tb.top_inst.sys_clk);
			#1 ;
			cpu_if.cs0_n<=signal[0];
			cpu_if.rd_n<=signal[1];
			cpu_if.wr_n<=signal[2];		
			
	   @(posedge tb.top_inst.sys_clk);
			#1 ;
			cpu_if.cs0_n<=signal[0];
			cpu_if.rd_n<=signal[1];
			cpu_if.wr_n<=signal[2];
		
		repeat(10) @(posedge tb.top_inst.sys_clk);
	
endtask
endclass

3.创建接口,文件名为tb_interface.sv。

interface  top_if(  input bit 				clk );

//	logic 				clk			;
	logic 				rst_n		;
	//总线信号	                    
	logic 				wr_n		;
	logic 				rd_n		;
	logic 				cs0_n		;
	logic 				cs7_n		;
	logic [15 : 0] 		bus_addr_in	;
endinterface

4.在testbench中将dut和tb连接,并在tb模块中实例化类对象。

`timescale 1ns/1ps

`include "tb_interface.sv"        //将接口模块包含进来
`include "watch_dog.sv"
`include "cpu.sv"                //将cpu类模块包含进来

module tb;

	 bit 				clk			;

	 top_if topif(clk);    //实例化top_if对象,将clk传递给interface
	//top_if topif ;
	 dszj_2k_6001797_top   top_inst( .clk(topif.clk),               //将topif接口对象与DUT绑定,这里直接按照位置绑定
	                                   .rst_n( topif.rst_n    ),
	                                   .wr_n(topif.wr_n),		
	                                   .rd_n( topif.rd_n		),
	                                   .cs0_n( topif.cs0_n		),
	                                   .cs7_n(topif.cs7_n		)
	                              
	                                   );	                                                                   
	                              
	initial
		begin
			clk=0; 
			topif.rst_n=0;
			#100 topif.rst_n=1;  //在tb模块中可以直接通过.运算符直接操作接口信号
			
		end
	
	always #12.5 clk=~clk;
	
	 watch_dog  watch_dog_inst=new(topif);//将topif接口传递给watch_dog类的对象
	 
	 cpu cpu_inst=new(topif);//将topif接口传递给cpu类的对象
	initial
	   begin	 
	      
	       repeat(1000) @(posedge clk);
	       watch_dog_inst.print();
	      // watch_dog_inst.
	      cpu_inst.signal_synchronous(3'b111);
	   end
	
	endmodule
相关推荐
hh1992031 个月前
systemverilog中的DPI-C用例介绍
c语言·systemverilog·dpi-c
逍遥xiaoy4 个月前
SystemVerilog测试框架示例
systemverilog·uvm
谷公子的藏经阁4 个月前
设计模式在芯片验证中的应用——迭代器
设计模式·systemverilog·uvm·芯片验证·design pattern
wjh776a685 个月前
基于PCIE4C的数据传输(三)——使用遗留中断与MSI中断
linux·fpga开发·systemverilog·xilinx·pcie
wjh776a687 个月前
【RS422】基于未来科技FT4232HL芯片的多波特率串口通信收发实现
fpga开发·verilog·systemverilog·xilinx·rs422
一只迷茫的小狗9 个月前
systemverilog/verilog文件操作
systemverilog
apple_ttt10 个月前
SystemVerilog学习(0)——目录与传送门
fpga开发·fpga·systemverilog·芯片验证
一只迷茫的小狗1 年前
systemverilog:interface中端口方向理解
systemverilog
apple_ttt1 年前
SystemVerilog学习(8)——包的使用
fpga开发·fpga·systemverilog·芯片验证
apple_ttt1 年前
SystemVerilog学习 (10)——线程控制
fpga开发·fpga·systemverilog·芯片验证