Jump to content

Using DPI-C sin function


Recommended Posts

Hi,

 

 I'm trying to use DPI-C to import sin function, but it doesn't work, as a workround i had used a sin approximative function which finally give a static value of 2.5 (the offset value) i think that the instanciation of the sin in the code does not work, and i did not know why, any help would be appreciable:

 

 package math_pkg;

  //import dpi task      C Name = SV function name
 //import "DPI-C" pure function real sin (input real rTheta);
 `define PI 3.14159265
 
  function real sin;
 
     input x;
 
     real x;
 
     real x1,y,y2,y3,y5,y7,sum,sign;
 
     begin
 
         sign = 1.0;
 
         x1 = x;
 
         if (x1<0)
 
           begin
 
               x1 = -x1;
 
               sign = -1.0;
 
           end
 
         while (x1 > `PI/2.0)        
 
           begin
 
               x1 = x1 - `PI;
 
               sign = -1.0*sign;
 
           end  
 
         y = x1*2/`PI;
 
         y2 = y*y;
 
         y3 = y*y2;
 
         y5 = y3*y2;
 
         y7 = y5*y2;
 
         sum = 1.570794*y - 0.645962*y3 +
 
               0.079692*y5 - 0.004681712*y7;
 
         sin = sign*sum;
 
     end
 
  endfunction // sin
 
 
endpackage : math_pkg
 
module sv_ams_sin_voltage_gen(output real sine_out);
import math_pkg::*;
parameter sampling_time = 5;
const real pi = 3.1416;
real time_us, time_s ;
bit sampling_clock;
real freq = 20;
real offset = 2.5;
real ampl = 2.5;
always sampling_clock = #(sampling_time) ~sampling_clock;
always @(sampling_clock) begin
time_us = $time/1000;
time_s = time_us/1000000;
end
assign sine_out = offset + (ampl * sin(2*pi*freq));
endmodule
 
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...