Search code examples
cverilogvpi

In Verilog Procedural Interface, is it possible to scan through iteration loop several times?


We can use vpi_scan in the following way:

vpiHandle iter = vpi_iterate(property, handle);
if (iter)
  while ( entry = vpi_scan(iter) )
    /*code*/;

iter will be freed when vpi_scan() returns NULL. But what if I need to scan through the loop several times? Is there a way to tell vpi_scan not to free the iterator, so that I could vpi_free_object when I'm done? I think I can solve the problem using an additional container (to collect and keep all entries), but is it really needed?

EDIT: 1. I would not like to call vpi_iterate more than once, since it can be expensive. 2. Suppose I go with an additional container solution. Is there a way to find out the number of entries in the vpi_scan loop without actual scanning through the loop? This could make implementation simpler.


Solution

    1. ut what if I need to scan through the loop several times? vpi_iterate returns an initialized pointer to the iterator. Every vpi_scan removes an element from the list and frees it. If vpi_scan did not run till the end, you'd better use vpi_free_object to clean the rest of the iterator list. If you need to rescan the same object again, you can call vpi_iterate again and it will return a new iterator object which you can re-scan.

    2. s there a way to tell vpi_scan not to free the iterator, so that I could vpi_free_object when I'm done? No, (1) is the only mechanism which can be used to access and free iterator elements. There is no other exist in the standard.

    3. I think I can solve the problem using an additional container (to collect and keep all entries), but is it really needed? -- this is a good idea if you want to re-scan your data structs. It could be much better from the performance point of view. Verilog simulator usually has a separately generated vpi database which it needs to consult, probably multiple times to provide you with vpi access information.

    4. Is there a way to find out the number of entries in the vpi_scan loop without actual scanning through the loop? not really. there are no defined way to query this information from the iterator. There might be a work-around with using vpi_handle(vpiUse, iterator) but it depends on the underlying data and the type of iteration. It is much easier to use linked lists created by the first scanning loop.

    you can get all additional information for LRM or a verilog pli handbook.