Jump to content

Problem with casting a VIF container object obtained from the config db.


Recommended Posts

My code for the driver's connect phase is shown below. I'm creating a simple VIF container object and setting it as a DB object. When I try to retrive it from the driver, I do get the object, but it seems to be of a different type, and my simulation fails at the casting step in the driver. Can someone help me with this. This is my first attempt with UVM. So, it might be a very simple mistake. Any help in this regard is appreciated.

I have an initial block in my TB that does something like

initial begin

typedef myvif_container #(virtual myInterface) myvif_container_t;

myvif_container_t myvif_container_h;

myvif_container_h = myvif_container_t::type_id::create("myvif_container_h");

set_config_object("*.*driver", "myvif_container", myvif_container_h);

....

end

function void connect();

uvm_object tmp;

typedef myvif_container #(virtual myInterface) myvif_container_t;

myvif_container_t myvif_container_h;

if (!(get_config_object("myvif_container", tmp))) begin

`uvm_fatal(get_type_name(), "VIF container not found")

end

if (tmp == null) begin

`uvm_fatal(get_type_name(), "VIF container is null");

end

if (!($cast(myvif_container_h, tmp))) begin

`uvm_info(get_type_name(), $psprintf ("container type is %s", tmp.get_type_name()), UVM_NONE);

`uvm_fatal(get_type_name(), "Interface container different type")

end

endfunction : connect

class myvif_container #(type VIF=int) extends uvm_object;

`uvm_object_param_utils (myvif_container #(VIF))

VIF m_vif_h;

function new (string name="vif_container");

super.new(name);

endfunction : new

endclass : myvif_container

Link to comment
Share on other sites

HI ,

Casting can be done from a child to parent class.

Forexample,

class A;

endclass

class B extends A;

endclass

module top;

A a;

B b;

initial

begin

b=new();

void'($cast(a,B));

endmodule

But in your case you are doing reverse from parent to child ,that's an error.

end

Link to comment
Share on other sites

To further safeguard you with typos in the get(), you can also create a typedef for uvm_config_db parametrized with a specific interface type.

e.g.

In uvm_pkg:

typedef uvm_config_db #(virtual myinterface) myinterface_config_db;

In module testbench:

myinterface_config_db::set(null, "*.tb.uvc.agent.*", "vif", my_if_inst);

And in the driver:

myinterface_config_db::get(this, "", "vif", myifinstance);

-Jigar

Link to comment
Share on other sites

HI ,

Casting can be done from a child to parent class.

hi,

you dont need to cast from child to parent at all since a derived is always a base. you can directly assign a derived class handle to a base class handle. the reverse is not always true therefore you need to check at runtime.

HI ,

But in your case you are doing reverse from parent to child ,that's an error.

not necessarily, have a look at the attached code

module test193;
virtual class car;
endclass
class mercedes extends car;
endclass
class renault extends car;
endclass

car c;
mercedes m;
renault r;

initial
begin
m=new();

// this is not needed since a "mercedes" is always a "car"
void'($cast(c,m));

// do the previous the simple way
c=m;

// the reverse needs a cast since a "car" is not necessarily a "mercedes"
r = new();
c=r;
assert($cast(m,c)); //this should fail
c=m;
assert($cast(m,c)); // this should pass
end
endmodule

Link to comment
Share on other sites

hi,

you dont need to cast from child to parent at all since a derived is always a base. you can directly assign a derived class handle to a base class handle. the reverse is not always true therefore you need to check at runtime.

not necessarily, have a look at the attached code

module test193;
virtual class car;
endclass
class mercedes extends car;
endclass
class renault extends car;
endclass

car c;
mercedes m;
renault r;

initial
begin
m=new();

// this is not needed since a "mercedes" is always a "car"
void'($cast(c,m));

// do the previous the simple way
c=m;

// the reverse needs a cast since a "car" is not necessarily a "mercedes"
r = new();
c=r;
assert($cast(m,c)); //this should fail
c=m;
assert($cast(m,c)); // this should pass
end
endmodule

Even I doubted whether amitrana's comments were correct, as I saw uvm_object type objects being casted to VIF container objects everywhere in the UVM manuals/books. But, I wasn't quite sure if I was in a place to comment. Anyway, thanks for the detailed explanation. I didn't know that a derived guy can be assigned to base class handle directly. Thanks again.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...