| |
predictor_matlab.svh
一旦我们编译并将matlab_dpi.so添加到Questa vsim命令行,我们就可以启动/停止MATLAB引擎并使用MATLAB引擎作为记分板预测器。扩展了原始预测器,我们将build(),write()和report_phase()函数替换目的为了控制MATLAB。要使用上面的MATLAB函数ml_adder,我们只需将命令字符串“X = ml_adder(in0,in1)”发送到MATLAB。MATLAB将调用ml_adder函数,结果放在MATLAB输出缓冲区中,我们读回来解析响应。预测器然后将预测的事务发送到记分板预测的analysis port。
//----------------------------------------------------------------------
// Mentor Graphics Corporation
//----------------------------------------------------------------------
// Project : my_project
// Unit : predictor_matlab
// File : predictor_matlab.svh
//----------------------------------------------------------------------
// Created by : cgales
// Creation Date : 2012/01/04
//----------------------------------------------------------------------
// Title:
//
// Summary:
//
// Description:
//
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// predictor_matlab
//----------------------------------------------------------------------
class predictor_matlab extends predictor;
// factory registration macro
`uvm_component_utils(predictor_matlab)
...
//--------------------------------------------------------------------
// build_phase
//--------------------------------------------------------------------
function void build_phase(uvm_phase phase);
// During the build phase, start MATLAB and add
// MATLAB dir to enable access to MATLAB functions
if (!start_matlab("matlab -nosplash")) begin
`uvm_fatal(get_name(), "Unable to start MATLAB");
end
void'(send_matlab_cmd("addpath ./MATLAB;"));
m_output_ap = new("m_output_ap", this);
endfunction : build_phase
//--------------------------------------------------------------------
// write
//--------------------------------------------------------------------
function void write(T t);
in_tran m_out_item;
string msg, cmd, cmd_rsp;
m_out_item = in_tran::type_id::create("m_out_item");
//t is the input sequence item (transaction). Process t and then
// write the new processed output to the m_output_ap.
m_out_item.do_copy(t);
$sformat(msg, "INPUT: %s",t.convert2string());
`uvm_info(get_name(),msg, UVM_HIGH);
$sformat(cmd, "X = ml_adder(%0d,%0d);", t.input0, t.input1);
`uvm_info(get_name(), cmd, UVM_HIGH);
// Call our MATLAB function with our transaction inputs
void'(send_matlab_cmd(cmd));
// Readback the MATLAB buffer with our output
cmd_rsp = get_matlab_buffer();
`uvm_info(get_name(), $sformatf("MATLAB Buffer is %s", cmd_rsp), UVM_HIGH);
if (!$sscanf(cmd_rsp, ">> %d", m_out_item.output0)) begin
`uvm_warning(get_name(), "Error parsing MATLAB response");
end
m_output_ap.write(m_out_item);
endfunction : write
function void report_phase(uvm_phase phase);
// All done - shut down MATLAB
stop_matlab();
endfunction
endclass : predictor_matlab
(在http://verificationacademy.com/uvm-ovm上在线下载源代码示例)。