Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to pwl_cos #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions mLingua/samples/misc/prbs21.v

This file was deleted.

5 changes: 3 additions & 2 deletions mLingua/samples/prim/bit2pwl.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ authorization from Stanford University. Contact [email protected] for details.

****************************************************************/

`include "mLingua_pwl.vh"

module bit2pwl #(
parameter real vh=1.0, // value corresponds to logic 'H'
Expand All @@ -45,7 +46,7 @@ real transition;
event wakeup;
event dummy_evt;

`protect
//`protect
//pragma protect
//pragma protect begin

Expand Down Expand Up @@ -79,6 +80,6 @@ function real get_value (logic _in);
endfunction

//pragma protect end
`endprotect
//`endprotect

endmodule
185 changes: 96 additions & 89 deletions mLingua/samples/prim/pwl_cos.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,98 +19,105 @@ authorization from Stanford University. Contact [email protected] for details.

****************************************************************/

`include "mLingua_pwl.vh"

module pwl_cos #(
parameter real etol = 0.0001, // error tolerance of PWL approximation
parameter real freq = 100e6, // frequency
parameter real amp = 0.01, // amplitude
parameter real offset = 0.01, // DC offset
parameter real ph = 0.0 // initial phase in degree
parameter real etol=0.0001, // error tolerance of PWL approximation
parameter real freq=100e6, // frequency
parameter real amp=0.01, // amplitude
parameter real offset=0.01, // DC offset
parameter real ph=0.0 // initial phase in degrees
) (
`output_pwl out // cosine output in pwl
`output_pwl out // cosine output in pwl
);

timeunit `DAVE_TIMEUNIT ;
timeprecision `DAVE_TIMEUNIT ;

`protect
//pragma protect
//pragma protect begin

`get_timeunit // get timeunit in sec and assign it to the variable 'TU'
PWLMethod pm=new;

// DPI-C function if needed (only takes an input and produces a real output)
import "DPI-C" pure function real cos(input real x);

// wires
event wakeup; // event signal
real phase;
real dPhase;
real dTr; // time interval of PWL waveform
real t_cur; // current time
real out_cur; // current output signal value
real out_nxt; // out at (t_cur+dT) for pwl output data
time dT;


real out_slope; // out slope

// you may need outme additional wire definition here
initial begin
phase = ph/180*`M_PI;
dPhase = 0.0;
->> wakeup;
end

always @(wakeup) begin
t_cur = `get_time;
phase = phase + dPhase;
if (phase >= 2*`M_PI) phase = phase - 2*`M_PI;
out_cur = fn_pwl_cos(phase);
dTr = calculate_Tintv_pwl_cos(etol, phase);
dPhase = 2*`M_PI*freq*dTr;
out_nxt = fn_pwl_cos(phase+dPhase);
out_slope = (out_nxt-out_cur)/dTr;
out = pm.write(out_cur, out_slope, t_cur);
dT = time'(dTr/TU);
->> #(dT) wakeup;
end

/*******************************************
Response function, its 1st/2nd derivatives
*******************************************/

function real fn_pwl_cos;
input real phase;
begin
return offset + amp*cos(phase);
end
endfunction

function real f2max_pwl_cos;
input real phase;
begin
return abs(amp*(2*`M_PI*freq)**2);
end
endfunction

/*************************************
Caluating Tintv
*************************************/

function real calculate_Tintv_pwl_cos;
input real etol, phase;
real abs_f2max;
real calcT;
begin
abs_f2max = f2max_pwl_cos(phase);
calcT = sqrt(8.0*etol/abs_f2max);
return min(`DT_MAX,max(TU,min(1.0,calcT)));
end
endfunction

//pragma protect end
`endprotect
// set timing options
timeunit `DAVE_TIMEUNIT;
timeprecision `DAVE_TIMEUNIT;

// instantiate object for PWL updates
PWLMethod pm=new;

// wires
event wakeup; // event signal
real phase;
real dPhase;
real t_cur; // current time
real out_cur; // current output signal value
real out_nxt; // out at (t_cur+dT) for pwl output data
real dT; // time interval of PWL waveform
real out_slope; // out slope

// you may need some additional wire definitions here
initial begin
phase = ph/180*(`M_PI);
dPhase = 0.0;
->> wakeup;
end

always @(wakeup) begin
// get current time
t_cur = ($realtime/1s);

// update phase, wrapping as necessary
// TODO: should modular arithmetic be used here instead?
// The potential issue is that the phase update is greater
// than 2*pi. That shouldn't normally happen but is possible
// in some corner cases.
phase = phase + dPhase;
if (phase >= 2*(`M_PI)) begin
phase = phase - (2*(`M_PI));
end

// calculate current output
out_cur = fn_pwl_cos(phase);

// calculate next wakeup time
dT = calculate_Tintv_pwl_cos(etol, phase);

// calculate phase at next wakeup time
dPhase = (2*(`M_PI))*freq*dT;

// calculate value at next wakeup time
out_nxt = fn_pwl_cos(phase+dPhase);

// calculate slope between current value and next wakeup time
out_slope = (out_nxt-out_cur)/dT;

// write current output value and calculated slope to the output
out = pm.write(out_cur, out_slope, t_cur);

// trigger wakeup at some time in the future
->> #(dT*1s) wakeup;
end

// Value of the cosine function

function real fn_pwl_cos(input real phase);
return offset + amp*cos(phase);
endfunction

// Maximum second derivative of the cosine

function real f2max_pwl_cos(input real phase);
return abs(amp*(2*(`M_PI)*freq)**2);
endfunction

// Caluating Tintv

function real calculate_Tintv_pwl_cos(input real etol, input real phase);
// internal variables
real abs_f2max;
real calcT;

// calculate maximum second derivative of the cosine
abs_f2max = f2max_pwl_cos(phase);

// convert this second derivative to a time based
// on the allowed error tolerance
calcT = sqrt(8.0*etol/abs_f2max);

// return timestep, clamped between the min/max limits
return min(`DT_MAX, max((`DAVE_TIMEUNIT)/1s, calcT));
endfunction

endmodule
5 changes: 3 additions & 2 deletions mLingua/samples/prim/pwl_filter_real_p1.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ authorization from Stanford University. Contact [email protected] for details.

****************************************************************/

`include "mLingua_pwl.vh"

module pwl_filter_real_p1 #(
parameter etol = 0.005, // error tolerance of PWL approximation
Expand All @@ -32,13 +33,13 @@ module pwl_filter_real_p1 #(
timeunit `DAVE_TIMEUNIT ;
timeprecision `DAVE_TIMEUNIT ;

`protect
//`protect
//pragma protect
//pragma protect begin

pwl_filter_real_w_reset #(.etol(etol), .filter(0), .en_filter(en_filter)) xinst (.in(in), .out(out), .fp1(fp));

//pragma protect end
`endprotect
//`endprotect

endmodule
5 changes: 3 additions & 2 deletions mLingua/samples/prim/real2pwl.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ authorization from Stanford University. Contact [email protected] for details.

****************************************************************/

`include "mLingua_pwl.vh"

module real2pwl #(
parameter real tr = 100e-12 // transition time
Expand All @@ -38,7 +39,7 @@ timeprecision `DAVE_TIMEUNIT ;

pullup(en);

`protect
//`protect
//pragma protect
//pragma protect begin

Expand Down Expand Up @@ -79,6 +80,6 @@ always @(in or wakeup or en) begin
end

//pragma protect end
`endprotect
//`endprotect

endmodule