• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Functional Verification
  3. Sharing memory between SV and DPI-C methods

Stats

  • Locked Locked
  • Replies 3
  • Subscribers 65
  • Views 22520
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Sharing memory between SV and DPI-C methods

StephenH
StephenH over 14 years ago

Someone just asked me how to malloc() memory in a DPI-C import task (his reference model) such that the memory could be shared with the calling SV code. Because SV has some extra gubbins to track the array's properties, you cannot just use malloc() in C; even if you pass the pointer as an open array handle to the SV caller, SV won't know that you changed the array size.
A simple solution to this is to create a DPI-C export task in SV, that does the malloc for you in the SV domain.
Your C code can then call this function and use the array just as though you'd use C's malloc(), with the benefit that SV is aware of the array.

Here's an example.
// dpi.c
#include "svdpi.h"
#include <stdio.h>
#include <stdlib.h>
#include "vpi_user.h"

extern void allocate_mem ( int size );

void ref_model ( svOpenArrayHandle p )
{
  int unsigned *pp;
  int i;
  vpi_printf("c: Ref model started\n");
  allocate_mem(10);
  pp = ( int unsigned * ) svGetArrayPtr(p);
  for(i=0;i<10;i++) {
    pp[i] = i+1;
    vpi_printf("c: pp[%0d]=%0d\n",i,i+1);
  }
  vpi_printf("c: Ref model finished\n");
}

// dpi.sv
module top;
  typedef int unsigned mem_t;
  mem_t mem[];

  import "DPI-C" context task ref_model ( inout mem_t m[] );
  export "DPI-C" task allocate_mem;

  task allocate_mem ( input int size );
    $display("SV: allocating new SV DA of size %0d", size);
    mem = new[size];
  endtask

  initial begin
    ref_model(mem);
    $display("SV: ref model finished, mem.size()=%0d", mem.size());
    foreach (mem[i]) $display("SV: mem[%0d]=%0d", i,mem[i]);
  end
endmodule

And here's the corresponding output from "irun dpi.c dpi.sv"

c: Ref model started
SV: allocating new SV DA of size 10
c: pp[0]=1
c: pp[1]=2
c: pp[2]=3
c: pp[3]=4
c: pp[4]=5
c: pp[5]=6
c: pp[6]=7
c: pp[7]=8
c: pp[8]=9
c: pp[9]=10
c: Ref model finished
SV: ref model finished, mem.size()=10
SV: mem[0]=1
SV: mem[1]=2
SV: mem[2]=3
SV: mem[3]=4
SV: mem[4]=5
SV: mem[5]=6
SV: mem[6]=7
SV: mem[7]=8
SV: mem[8]=9
SV: mem[9]=10


Enjoy!

  • Cancel
Parents
  • EML1
    EML1 over 9 years ago

    Does this work, and is it portable? After fixing the '[' typo it just crashes in VCS.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • EML1
    EML1 over 9 years ago

    Does this work, and is it portable? After fixing the '[' typo it just crashes in VCS.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
No Data

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information