2000-11-01 04:19:36 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2000 Stephen Williams (steve@icarus.com)
|
|
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form under the terms of the GNU
|
|
|
|
|
* General Public License as published by the Free Software
|
|
|
|
|
* Foundation; either version 2 of the License, or (at your option)
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
2002-08-12 03:34:58 +02:00
|
|
|
#ifdef HAVE_CVS_IDENT
|
2007-03-14 05:05:51 +01:00
|
|
|
#ident "$Id: sys_time.c,v 1.12 2007/03/14 04:05:51 steve Exp $"
|
2000-11-01 04:19:36 +01:00
|
|
|
#endif
|
|
|
|
|
|
2004-01-21 02:22:51 +01:00
|
|
|
# include "vpi_config.h"
|
2001-07-25 05:10:48 +02:00
|
|
|
|
2000-11-01 04:19:36 +01:00
|
|
|
# include "vpi_user.h"
|
2002-12-21 01:55:57 +01:00
|
|
|
# include <string.h>
|
2003-01-28 05:41:55 +01:00
|
|
|
# include <math.h>
|
2000-11-01 04:19:36 +01:00
|
|
|
# include <assert.h>
|
|
|
|
|
|
2002-12-21 01:55:57 +01:00
|
|
|
static vpiHandle module_of_function(vpiHandle obj)
|
|
|
|
|
{
|
|
|
|
|
while (vpi_get(vpiType, obj) != vpiModule) {
|
|
|
|
|
obj = vpi_handle(vpiScope, obj);
|
|
|
|
|
assert(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-14 05:05:51 +01:00
|
|
|
static PLI_INT32 sys_time_sizetf(PLI_BYTE8*x)
|
2000-11-01 04:19:36 +01:00
|
|
|
{
|
|
|
|
|
return 64;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-14 05:05:51 +01:00
|
|
|
static PLI_INT32 sys_stime_sizetf(PLI_BYTE8*x)
|
2002-01-11 06:20:59 +01:00
|
|
|
{
|
|
|
|
|
return 32;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-14 05:05:51 +01:00
|
|
|
static PLI_INT32 sys_time_calltf(PLI_BYTE8*name)
|
2000-11-01 04:19:36 +01:00
|
|
|
{
|
|
|
|
|
s_vpi_value val;
|
|
|
|
|
s_vpi_time now;
|
|
|
|
|
vpiHandle call_handle;
|
2002-12-21 01:55:57 +01:00
|
|
|
vpiHandle mod;
|
|
|
|
|
int units, prec;
|
|
|
|
|
long scale;
|
2003-02-07 03:44:25 +01:00
|
|
|
long frac;
|
2000-11-01 04:19:36 +01:00
|
|
|
|
|
|
|
|
call_handle = vpi_handle(vpiSysTfCall, 0);
|
|
|
|
|
assert(call_handle);
|
|
|
|
|
|
2002-12-21 01:55:57 +01:00
|
|
|
mod = module_of_function(call_handle);
|
|
|
|
|
|
|
|
|
|
now.type = vpiSimTime;
|
2000-11-01 04:19:36 +01:00
|
|
|
vpi_get_time(0, &now);
|
|
|
|
|
|
2002-12-21 01:55:57 +01:00
|
|
|
/* All the variants but $simtime return the time in units of
|
|
|
|
|
the local scope. The $simtime function returns the
|
|
|
|
|
simulation time. */
|
|
|
|
|
if (strcmp(name, "$simtime") == 0)
|
|
|
|
|
units = vpi_get(vpiTimePrecision, 0);
|
|
|
|
|
else
|
|
|
|
|
units = vpi_get(vpiTimeUnit, mod);
|
|
|
|
|
|
|
|
|
|
prec = vpi_get(vpiTimePrecision, 0);
|
|
|
|
|
scale = 1;
|
|
|
|
|
while (units > prec) {
|
|
|
|
|
scale *= 10;
|
|
|
|
|
units -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-18 02:54:28 +02:00
|
|
|
assert(8*sizeof(long long) >= 64);
|
|
|
|
|
{ long long tmp_now = ((long long)now.high) << 32;
|
|
|
|
|
tmp_now += (long long)now.low;
|
|
|
|
|
frac = tmp_now % (long long)scale;
|
|
|
|
|
tmp_now /= (long long)scale;
|
|
|
|
|
|
|
|
|
|
/* Round to the nearest integer, which may be up. */
|
|
|
|
|
if ((scale > 1) && (frac >= scale/2))
|
|
|
|
|
tmp_now += 1;
|
2000-11-01 04:19:36 +01:00
|
|
|
|
2003-06-18 02:54:28 +02:00
|
|
|
now.low = tmp_now & 0xffffffff;
|
|
|
|
|
now.high = tmp_now >> 32LL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val.format = vpiTimeVal;
|
|
|
|
|
val.value.time = &now;
|
2002-12-21 01:55:57 +01:00
|
|
|
|
2000-11-01 04:19:36 +01:00
|
|
|
vpi_put_value(call_handle, &val, 0, vpiNoDelay);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-14 05:05:51 +01:00
|
|
|
static PLI_INT32 sys_realtime_calltf(PLI_BYTE8*name)
|
2003-01-27 01:14:37 +01:00
|
|
|
{
|
|
|
|
|
s_vpi_value val;
|
|
|
|
|
s_vpi_time now;
|
|
|
|
|
vpiHandle call_handle;
|
|
|
|
|
vpiHandle mod;
|
|
|
|
|
int units, prec;
|
|
|
|
|
|
|
|
|
|
call_handle = vpi_handle(vpiSysTfCall, 0);
|
|
|
|
|
assert(call_handle);
|
|
|
|
|
|
|
|
|
|
mod = module_of_function(call_handle);
|
|
|
|
|
|
|
|
|
|
now.type = vpiSimTime;
|
|
|
|
|
vpi_get_time(0, &now);
|
|
|
|
|
|
2003-01-28 05:41:55 +01:00
|
|
|
units = vpi_get(vpiTimeUnit, mod);
|
2003-01-27 01:14:37 +01:00
|
|
|
prec = vpi_get(vpiTimePrecision, 0);
|
|
|
|
|
|
|
|
|
|
val.format = vpiRealVal;
|
2003-01-28 05:41:55 +01:00
|
|
|
val.value.real = pow(10.0, prec-units) * now.low;
|
2003-01-27 01:14:37 +01:00
|
|
|
assert(now.high == 0);
|
|
|
|
|
|
|
|
|
|
vpi_put_value(call_handle, &val, 0, vpiNoDelay);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-01 04:19:36 +01:00
|
|
|
void sys_time_register()
|
|
|
|
|
{
|
|
|
|
|
s_vpi_systf_data tf_data;
|
|
|
|
|
|
|
|
|
|
tf_data.type = vpiSysFunc;
|
|
|
|
|
tf_data.tfname = "$time";
|
|
|
|
|
tf_data.calltf = sys_time_calltf;
|
|
|
|
|
tf_data.compiletf = 0;
|
|
|
|
|
tf_data.sizetf = sys_time_sizetf;
|
2002-12-21 01:55:57 +01:00
|
|
|
tf_data.user_data = "$time";
|
2000-11-01 04:19:36 +01:00
|
|
|
vpi_register_systf(&tf_data);
|
2002-01-11 06:20:59 +01:00
|
|
|
|
2003-01-27 01:14:37 +01:00
|
|
|
tf_data.type = vpiSysFunc;
|
|
|
|
|
tf_data.tfname = "$realtime";
|
|
|
|
|
tf_data.calltf = sys_realtime_calltf;
|
|
|
|
|
tf_data.compiletf = 0;
|
|
|
|
|
tf_data.sizetf = 0;
|
|
|
|
|
tf_data.user_data = "$realtime";
|
|
|
|
|
vpi_register_systf(&tf_data);
|
|
|
|
|
|
2002-01-11 06:20:59 +01:00
|
|
|
tf_data.type = vpiSysFunc;
|
|
|
|
|
tf_data.tfname = "$stime";
|
|
|
|
|
tf_data.calltf = sys_time_calltf;
|
|
|
|
|
tf_data.compiletf = 0;
|
|
|
|
|
tf_data.sizetf = sys_stime_sizetf;
|
2002-12-21 01:55:57 +01:00
|
|
|
tf_data.user_data = "$stime";
|
|
|
|
|
vpi_register_systf(&tf_data);
|
|
|
|
|
|
|
|
|
|
tf_data.type = vpiSysFunc;
|
|
|
|
|
tf_data.tfname = "$simtime";
|
|
|
|
|
tf_data.calltf = sys_time_calltf;
|
|
|
|
|
tf_data.compiletf = 0;
|
|
|
|
|
tf_data.sizetf = sys_time_sizetf;
|
|
|
|
|
tf_data.user_data = "$simtime";
|
2002-01-11 06:20:59 +01:00
|
|
|
vpi_register_systf(&tf_data);
|
2000-11-01 04:19:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* $Log: sys_time.c,v $
|
2007-03-14 05:05:51 +01:00
|
|
|
* Revision 1.12 2007/03/14 04:05:51 steve
|
|
|
|
|
* VPI tasks take PLI_BYTE* by the standard.
|
|
|
|
|
*
|
2006-10-30 23:45:36 +01:00
|
|
|
* Revision 1.11 2006/10/30 22:45:37 steve
|
|
|
|
|
* Updates for Cygwin portability (pr1585922)
|
|
|
|
|
*
|
2004-01-21 02:22:51 +01:00
|
|
|
* Revision 1.10 2004/01/21 01:22:53 steve
|
|
|
|
|
* Give the vip directory its own configure and vpi_config.h
|
|
|
|
|
*
|
2003-06-18 02:54:28 +02:00
|
|
|
* Revision 1.9 2003/06/18 00:54:28 steve
|
|
|
|
|
* Account for all 64 bits in results of $time.
|
|
|
|
|
*
|
2003-02-07 03:44:25 +01:00
|
|
|
* Revision 1.8 2003/02/07 02:44:25 steve
|
|
|
|
|
* Properly round inter time values from $time.
|
|
|
|
|
*
|
2003-01-28 05:41:55 +01:00
|
|
|
* Revision 1.7 2003/01/28 04:41:55 steve
|
|
|
|
|
* Use more precise pow function to scale time by units.
|
|
|
|
|
*
|
2003-01-27 01:14:37 +01:00
|
|
|
* Revision 1.6 2003/01/27 00:14:37 steve
|
|
|
|
|
* Support in various contexts the $realtime
|
|
|
|
|
* system task.
|
|
|
|
|
*
|
2002-12-21 01:55:57 +01:00
|
|
|
* Revision 1.5 2002/12/21 00:55:58 steve
|
|
|
|
|
* The $time system task returns the integer time
|
|
|
|
|
* scaled to the local units. Change the internal
|
|
|
|
|
* implementation of vpiSystemTime the $time functions
|
|
|
|
|
* to properly account for this. Also add $simtime
|
|
|
|
|
* to get the simulation time.
|
|
|
|
|
*
|
2002-08-12 03:34:58 +02:00
|
|
|
* Revision 1.4 2002/08/12 01:35:05 steve
|
|
|
|
|
* conditional ident string using autoconfig.
|
|
|
|
|
*
|
2002-01-11 06:20:59 +01:00
|
|
|
* Revision 1.3 2002/01/11 05:20:59 steve
|
|
|
|
|
* Add the stime system function.
|
|
|
|
|
*
|
2001-07-25 05:10:48 +02:00
|
|
|
* Revision 1.2 2001/07/25 03:10:50 steve
|
|
|
|
|
* Create a config.h.in file to hold all the config
|
|
|
|
|
* junk, and support gcc 3.0. (Stephan Boettcher)
|
|
|
|
|
*
|
2000-11-01 04:19:36 +01:00
|
|
|
* Revision 1.1 2000/11/01 03:19:36 steve
|
|
|
|
|
* Add the general $time system function.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|