diff --git a/mLingua/samples/misc/prbs21.v b/mLingua/samples/misc/prbs21.v deleted file mode 100644 index 013b581..0000000 --- a/mLingua/samples/misc/prbs21.v +++ /dev/null @@ -1,43 +0,0 @@ -/**************************************************************** - -Copyright (c) 2018- Stanford University. All rights reserved. - -The information and source code contained herein is the -property of Stanford University, and may not be disclosed or -reproduced in whole or in part without explicit written -authorization from Stanford University. Contact bclim@stanford.edu for details. - -* Filename : prbs21.v -* Author : Byongchan Lim (bclim@stanford.edu) -* Description: - - Pseudo random bit sequence (run length of 21) generator. - -* Note : - -* Revision : - - 7/26/2016: First release - -****************************************************************/ - -module prbs21 ( - input clk, // clock - input rst, // reset (act. high) - output out // output stream -); - -timeunit `DAVE_TIMEUNIT ; -timeprecision `DAVE_TIMEUNIT ; - -reg [20:0] sr='1; - -always @(posedge clk or posedge rst) begin - if(rst) sr = '1; - else begin - sr[20:1] <= sr[19:0]; - sr[0] <= sr[20] ^ sr[1]; - end -end - -assign out = sr[6]; - -endmodule diff --git a/mLingua/samples/prim/bit2pwl.v b/mLingua/samples/prim/bit2pwl.v index e782e7b..20cdde0 100644 --- a/mLingua/samples/prim/bit2pwl.v +++ b/mLingua/samples/prim/bit2pwl.v @@ -22,6 +22,7 @@ authorization from Stanford University. Contact bclim@stanford.edu for details. ****************************************************************/ +`include "mLingua_pwl.vh" module bit2pwl #( parameter real vh=1.0, // value corresponds to logic 'H' @@ -45,7 +46,7 @@ real transition; event wakeup; event dummy_evt; -`protect +//`protect //pragma protect //pragma protect begin @@ -79,6 +80,6 @@ function real get_value (logic _in); endfunction //pragma protect end -`endprotect +//`endprotect endmodule diff --git a/mLingua/samples/prim/pwl_cos.v b/mLingua/samples/prim/pwl_cos.v index a90dc1c..eb65f43 100644 --- a/mLingua/samples/prim/pwl_cos.v +++ b/mLingua/samples/prim/pwl_cos.v @@ -19,98 +19,105 @@ authorization from Stanford University. Contact bclim@stanford.edu 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 diff --git a/mLingua/samples/prim/pwl_filter_real_p1.v b/mLingua/samples/prim/pwl_filter_real_p1.v index 8c1c664..3690b28 100644 --- a/mLingua/samples/prim/pwl_filter_real_p1.v +++ b/mLingua/samples/prim/pwl_filter_real_p1.v @@ -19,6 +19,7 @@ authorization from Stanford University. Contact bclim@stanford.edu for details. ****************************************************************/ +`include "mLingua_pwl.vh" module pwl_filter_real_p1 #( parameter etol = 0.005, // error tolerance of PWL approximation @@ -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 diff --git a/mLingua/samples/prim/real2pwl.v b/mLingua/samples/prim/real2pwl.v index 15021b9..755f3b2 100644 --- a/mLingua/samples/prim/real2pwl.v +++ b/mLingua/samples/prim/real2pwl.v @@ -22,6 +22,7 @@ authorization from Stanford University. Contact bclim@stanford.edu for details. ****************************************************************/ +`include "mLingua_pwl.vh" module real2pwl #( parameter real tr = 100e-12 // transition time @@ -38,7 +39,7 @@ timeprecision `DAVE_TIMEUNIT ; pullup(en); -`protect +//`protect //pragma protect //pragma protect begin @@ -79,6 +80,6 @@ always @(in or wakeup or en) begin end //pragma protect end -`endprotect +//`endprotect endmodule