2001-03-31 21:00:43 +02:00
|
|
|
/*
|
2010-05-31 22:12:06 +02:00
|
|
|
* Copyright (c) 2001-2010 Stephen Williams (steve@icarus.com)
|
2001-03-31 21:00:43 +02:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2003-03-13 21:31:40 +01:00
|
|
|
# include "config.h"
|
2001-03-31 21:00:43 +02:00
|
|
|
# include "vpi_priv.h"
|
|
|
|
|
# include "schedule.h"
|
2010-05-31 22:12:06 +02:00
|
|
|
# include <cstdio>
|
|
|
|
|
# include <cmath>
|
|
|
|
|
# include <cassert>
|
2001-03-31 21:00:43 +02:00
|
|
|
|
2002-12-21 01:55:57 +01:00
|
|
|
/*
|
|
|
|
|
* The $time system function is supported in VPI contexts (i.e. an
|
2003-02-01 06:50:04 +01:00
|
|
|
* argument to a system task/function) as a vpiSysFuncCall object. The
|
|
|
|
|
* $display function divines that this is a function call and uses a
|
|
|
|
|
* vpi_get_value to get the value.
|
2002-12-21 01:55:57 +01:00
|
|
|
*/
|
2001-07-01 01:03:16 +02:00
|
|
|
|
2002-12-21 01:55:57 +01:00
|
|
|
/*
|
|
|
|
|
* vpi_time_precision is the precision of the simulation clock. It is
|
|
|
|
|
* set by the :vpi_time_precision directive in the vvp source file.
|
|
|
|
|
*/
|
2001-07-01 01:03:16 +02:00
|
|
|
static int vpi_time_precision = 0;
|
|
|
|
|
|
2003-02-03 02:09:20 +01:00
|
|
|
static struct __vpiSystemTime global_simtime;
|
|
|
|
|
|
2002-04-20 06:33:23 +02:00
|
|
|
void vpip_time_to_timestruct(struct t_vpi_time*ts, vvp_time64_t ti)
|
|
|
|
|
{
|
|
|
|
|
ts->low = ti & 0xFFFFFFFF;
|
|
|
|
|
ts->high = (ti >> 32) & 0xFFFFFFFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vvp_time64_t vpip_timestruct_to_time(const struct t_vpi_time*ts)
|
|
|
|
|
{
|
|
|
|
|
vvp_time64_t ti = ts->high;
|
|
|
|
|
ti <<= 32;
|
|
|
|
|
ti += ts->low & 0xffffffff;
|
|
|
|
|
return ti;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-11 04:54:21 +01:00
|
|
|
double vpip_time_to_scaled_real(vvp_time64_t ti, struct __vpiScope*scope)
|
|
|
|
|
{
|
|
|
|
|
int units;
|
|
|
|
|
if (scope)
|
|
|
|
|
units = scope->time_units;
|
|
|
|
|
else
|
|
|
|
|
units = vpi_time_precision;
|
|
|
|
|
|
|
|
|
|
double val = pow(10.0L, vpi_time_precision - units);
|
|
|
|
|
val *= ti;
|
|
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vvp_time64_t vpip_scaled_real_to_time64(double val, struct __vpiScope*scope)
|
|
|
|
|
{
|
|
|
|
|
int units;
|
|
|
|
|
if (scope)
|
|
|
|
|
units = scope->time_units;
|
|
|
|
|
else
|
|
|
|
|
units = vpi_time_precision;
|
|
|
|
|
|
|
|
|
|
double scale = pow(10.0L, units - vpi_time_precision);
|
|
|
|
|
val *= scale;
|
|
|
|
|
|
2008-01-01 18:45:02 +01:00
|
|
|
return (vvp_time64_t) val;
|
2007-11-11 04:54:21 +01:00
|
|
|
}
|
2001-03-31 21:00:43 +02:00
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
static int timevar_time_get(int code, vpiHandle ref)
|
2002-01-15 04:06:29 +01:00
|
|
|
{
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiSize:
|
2003-02-01 06:50:04 +01:00
|
|
|
return 64;
|
2002-01-15 04:06:29 +01:00
|
|
|
|
|
|
|
|
case vpiSigned:
|
2003-02-01 06:50:04 +01:00
|
|
|
return 0;
|
2002-01-15 04:06:29 +01:00
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
case vpiFuncType:
|
|
|
|
|
return vpiTimeFunc;
|
|
|
|
|
|
2008-11-09 01:26:55 +01:00
|
|
|
case vpiAutomatic:
|
|
|
|
|
return 0;
|
|
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "Code: %d\n", code);
|
|
|
|
|
assert(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-09 04:10:15 +01:00
|
|
|
static int timevar_stime_get(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiSize:
|
|
|
|
|
return 32;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return timevar_time_get(code, ref);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
static char* timevar_time_get_str(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiName:
|
2007-12-19 00:11:50 +01:00
|
|
|
return simple_set_rbuf_str("$time");
|
2003-02-01 06:50:04 +01:00
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "Code: %d\n", code);
|
|
|
|
|
assert(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-09 04:10:15 +01:00
|
|
|
static char* timevar_stime_get_str(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiName:
|
|
|
|
|
return simple_set_rbuf_str("$stime");
|
|
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "Code: %d\n", code);
|
|
|
|
|
assert(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-14 00:56:02 +02:00
|
|
|
static char* timevar_simtime_get_str(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiName:
|
2007-12-19 00:11:50 +01:00
|
|
|
return simple_set_rbuf_str("$simtime");
|
2007-08-14 00:56:02 +02:00
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "Code: %d\n", code);
|
|
|
|
|
assert(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
static char* timevar_realtime_get_str(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiName:
|
2007-12-19 00:11:50 +01:00
|
|
|
return simple_set_rbuf_str("$realtime");
|
2003-02-01 06:50:04 +01:00
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "Code: %d\n", code);
|
|
|
|
|
assert(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int timevar_realtime_get(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiSize:
|
2009-01-09 04:10:15 +01:00
|
|
|
return 1;
|
2003-02-01 06:50:04 +01:00
|
|
|
|
|
|
|
|
case vpiSigned:
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
case vpiFuncType:
|
|
|
|
|
return vpiRealFunc;
|
|
|
|
|
|
2008-11-09 01:26:55 +01:00
|
|
|
case vpiAutomatic:
|
|
|
|
|
return 0;
|
|
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "Code: %d\n", code);
|
|
|
|
|
assert(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static vpiHandle timevar_handle(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiSystemTime*rfp
|
|
|
|
|
= reinterpret_cast<struct __vpiSystemTime*>(ref);
|
|
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiScope:
|
|
|
|
|
return &rfp->scope->base;
|
2002-01-15 04:06:29 +01:00
|
|
|
default:
|
2003-02-01 06:50:04 +01:00
|
|
|
return 0;
|
2002-01-15 04:06:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-09 04:10:15 +01:00
|
|
|
static void timevar_get_value(vpiHandle ref, s_vpi_value*vp, bool is_int_func,
|
|
|
|
|
bool is_stime)
|
2001-03-31 21:00:43 +02:00
|
|
|
{
|
2002-12-21 01:55:57 +01:00
|
|
|
/* Keep a persistent structure for passing time values back to
|
|
|
|
|
the caller. */
|
|
|
|
|
static struct t_vpi_time time_value;
|
|
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
struct __vpiSystemTime*rfp
|
|
|
|
|
= reinterpret_cast<struct __vpiSystemTime*>(ref);
|
2003-03-13 21:31:40 +01:00
|
|
|
unsigned long num_bits;
|
|
|
|
|
vvp_time64_t x, simtime = schedule_simtime();
|
2003-02-03 02:09:20 +01:00
|
|
|
int units = rfp->scope? rfp->scope->time_units : vpi_time_precision;
|
2003-02-02 03:14:14 +01:00
|
|
|
|
2003-03-13 05:59:21 +01:00
|
|
|
char*rbuf = need_result_buf(128, RBUF_VAL);
|
|
|
|
|
|
2003-02-02 03:14:14 +01:00
|
|
|
/* Calculate the divisor needed to scale the simulation time
|
|
|
|
|
(in time_precision units) to time units of the scope. */
|
|
|
|
|
vvp_time64_t divisor = 1;
|
2002-12-21 01:55:57 +01:00
|
|
|
while (units > vpi_time_precision) {
|
2003-02-02 03:14:14 +01:00
|
|
|
divisor *= 10;
|
2002-12-21 01:55:57 +01:00
|
|
|
units -= 1;
|
|
|
|
|
}
|
2001-03-31 21:00:43 +02:00
|
|
|
|
2003-02-02 03:14:14 +01:00
|
|
|
/* Scale the simtime, and use the modulus to round up if
|
|
|
|
|
appropriate. */
|
|
|
|
|
vvp_time64_t simtime_fraction = simtime % divisor;
|
|
|
|
|
simtime /= divisor;
|
|
|
|
|
|
|
|
|
|
if ((divisor >= 10) && (simtime_fraction >= (divisor/2)))
|
|
|
|
|
simtime += 1;
|
|
|
|
|
|
2009-01-09 04:10:15 +01:00
|
|
|
/* If this is a call to $stime only return the lower 32 bits. */
|
|
|
|
|
if (is_stime) simtime &= 0xffffffff;
|
|
|
|
|
|
2001-03-31 21:00:43 +02:00
|
|
|
switch (vp->format) {
|
|
|
|
|
case vpiObjTypeVal:
|
2003-03-14 19:01:00 +01:00
|
|
|
/* The default format is vpiTimeVal. */
|
|
|
|
|
vp->format = vpiTimeVal;
|
2001-03-31 21:00:43 +02:00
|
|
|
case vpiTimeVal:
|
2002-12-21 01:55:57 +01:00
|
|
|
vp->value.time = &time_value;
|
2001-03-31 21:00:43 +02:00
|
|
|
vp->value.time->type = vpiSimTime;
|
2002-12-21 01:55:57 +01:00
|
|
|
vpip_time_to_timestruct(vp->value.time, simtime);
|
2001-03-31 21:00:43 +02:00
|
|
|
break;
|
|
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
case vpiRealVal:
|
2008-08-02 02:19:40 +02:00
|
|
|
/* If this is an integer based call (anything but $realtime)
|
|
|
|
|
* just return the value as a double. */
|
|
|
|
|
if (is_int_func) vp->value.real = double (simtime);
|
|
|
|
|
/* This is a call to $realtime to return a real value so
|
|
|
|
|
* scale this using the scaled real rules. */
|
|
|
|
|
else vp->value.real = vpip_time_to_scaled_real(schedule_simtime(),
|
|
|
|
|
rfp->scope);
|
2003-02-01 06:50:04 +01:00
|
|
|
break;
|
|
|
|
|
|
2004-10-04 03:10:51 +02:00
|
|
|
case vpiBinStrVal:
|
2002-12-21 01:55:57 +01:00
|
|
|
x = simtime;
|
2003-03-13 21:31:40 +01:00
|
|
|
num_bits = 8 * sizeof(vvp_time64_t);
|
2001-08-16 05:29:31 +02:00
|
|
|
|
2003-03-13 05:59:21 +01:00
|
|
|
rbuf[num_bits] = 0;
|
2001-10-15 04:55:03 +02:00
|
|
|
for (unsigned i = 1; i <= num_bits; i++) {
|
2003-03-13 05:59:21 +01:00
|
|
|
rbuf[num_bits-i] = x & 1 ? '1' : '0';
|
2001-08-16 05:29:31 +02:00
|
|
|
x = x >> 1;
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-13 05:59:21 +01:00
|
|
|
vp->value.str = rbuf;
|
2001-08-16 05:29:31 +02:00
|
|
|
break;
|
|
|
|
|
|
2001-04-03 05:46:14 +02:00
|
|
|
case vpiDecStrVal:
|
2009-03-20 21:21:12 +01:00
|
|
|
sprintf(rbuf, "%" TIME_FMT_U, simtime);
|
2003-03-13 05:59:21 +01:00
|
|
|
vp->value.str = rbuf;
|
2001-04-03 05:46:14 +02:00
|
|
|
break;
|
|
|
|
|
|
2001-08-16 05:29:31 +02:00
|
|
|
case vpiOctStrVal:
|
2009-03-20 21:21:12 +01:00
|
|
|
sprintf(rbuf, "%" TIME_FMT_O, simtime);
|
2003-03-13 05:59:21 +01:00
|
|
|
vp->value.str = rbuf;
|
2001-08-16 05:29:31 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case vpiHexStrVal:
|
2009-03-20 21:21:12 +01:00
|
|
|
sprintf(rbuf, "%" TIME_FMT_X, simtime);
|
2003-03-13 05:59:21 +01:00
|
|
|
vp->value.str = rbuf;
|
2001-08-16 05:29:31 +02:00
|
|
|
break;
|
|
|
|
|
|
2001-04-03 05:46:14 +02:00
|
|
|
default:
|
2009-12-10 21:53:58 +01:00
|
|
|
fprintf(stderr, "vpi_time: unknown format: %d\n", (int)vp->format);
|
2001-04-03 05:46:14 +02:00
|
|
|
assert(0);
|
2001-03-31 21:00:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-02 02:19:40 +02:00
|
|
|
static void timevar_get_ivalue(vpiHandle ref, s_vpi_value*vp)
|
|
|
|
|
{
|
2009-01-09 04:10:15 +01:00
|
|
|
timevar_get_value(ref, vp, true, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void timevar_get_svalue(vpiHandle ref, s_vpi_value*vp)
|
|
|
|
|
{
|
|
|
|
|
timevar_get_value(ref, vp, true, true);
|
2008-08-02 02:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void timevar_get_rvalue(vpiHandle ref, s_vpi_value*vp)
|
|
|
|
|
{
|
2009-01-09 04:10:15 +01:00
|
|
|
timevar_get_value(ref, vp, false, false);
|
2008-08-02 02:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
2001-03-31 21:00:43 +02:00
|
|
|
static const struct __vpirt vpip_system_time_rt = {
|
2003-02-01 06:50:04 +01:00
|
|
|
vpiSysFuncCall,
|
|
|
|
|
timevar_time_get,
|
|
|
|
|
timevar_time_get_str,
|
2008-08-02 02:19:40 +02:00
|
|
|
timevar_get_ivalue,
|
2001-03-31 21:00:43 +02:00
|
|
|
0,
|
2003-02-01 06:50:04 +01:00
|
|
|
timevar_handle,
|
2001-03-31 21:00:43 +02:00
|
|
|
0
|
|
|
|
|
};
|
|
|
|
|
|
2009-01-09 04:10:15 +01:00
|
|
|
static const struct __vpirt vpip_system_stime_rt = {
|
|
|
|
|
vpiSysFuncCall,
|
|
|
|
|
timevar_stime_get,
|
|
|
|
|
timevar_stime_get_str,
|
|
|
|
|
timevar_get_svalue,
|
|
|
|
|
0,
|
|
|
|
|
timevar_handle,
|
|
|
|
|
0
|
|
|
|
|
};
|
|
|
|
|
|
2007-08-14 00:56:02 +02:00
|
|
|
static const struct __vpirt vpip_system_simtime_rt = {
|
|
|
|
|
vpiSysFuncCall,
|
|
|
|
|
timevar_time_get,
|
|
|
|
|
timevar_simtime_get_str,
|
2008-08-02 02:19:40 +02:00
|
|
|
timevar_get_ivalue,
|
2007-08-14 00:56:02 +02:00
|
|
|
0,
|
|
|
|
|
timevar_handle,
|
|
|
|
|
0
|
|
|
|
|
};
|
|
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
static const struct __vpirt vpip_system_realtime_rt = {
|
|
|
|
|
vpiSysFuncCall,
|
|
|
|
|
timevar_realtime_get,
|
|
|
|
|
timevar_realtime_get_str,
|
2008-08-02 02:19:40 +02:00
|
|
|
timevar_get_rvalue,
|
2003-02-01 06:50:04 +01:00
|
|
|
0,
|
|
|
|
|
timevar_handle,
|
|
|
|
|
0
|
|
|
|
|
};
|
2001-03-31 21:00:43 +02:00
|
|
|
|
2003-02-03 02:09:20 +01:00
|
|
|
/*
|
|
|
|
|
* Create a handle to represent a call to $time/$stime/$simtime. The
|
|
|
|
|
* $time and $stime system functions return a value scaled to a scope,
|
|
|
|
|
* and the $simtime returns the unscaled time.
|
|
|
|
|
*/
|
2009-01-09 04:10:15 +01:00
|
|
|
vpiHandle vpip_sim_time(struct __vpiScope*scope, bool is_stime)
|
2001-03-31 21:00:43 +02:00
|
|
|
{
|
2003-02-03 02:09:20 +01:00
|
|
|
if (scope) {
|
2009-01-09 04:10:15 +01:00
|
|
|
if (is_stime) {
|
|
|
|
|
scope->scoped_stime.base.vpi_type = &vpip_system_stime_rt;
|
|
|
|
|
scope->scoped_stime.scope = scope;
|
|
|
|
|
return &scope->scoped_stime.base;
|
|
|
|
|
} else {
|
|
|
|
|
scope->scoped_time.base.vpi_type = &vpip_system_time_rt;
|
|
|
|
|
scope->scoped_time.scope = scope;
|
|
|
|
|
return &scope->scoped_time.base;
|
|
|
|
|
}
|
2003-02-03 02:09:20 +01:00
|
|
|
} else {
|
2007-08-14 00:56:02 +02:00
|
|
|
global_simtime.base.vpi_type = &vpip_system_simtime_rt;
|
2003-02-03 02:09:20 +01:00
|
|
|
global_simtime.scope = 0;
|
|
|
|
|
return &global_simtime.base;
|
|
|
|
|
}
|
2001-03-31 21:00:43 +02:00
|
|
|
}
|
|
|
|
|
|
2003-02-01 06:50:04 +01:00
|
|
|
vpiHandle vpip_sim_realtime(struct __vpiScope*scope)
|
|
|
|
|
{
|
|
|
|
|
scope->scoped_realtime.base.vpi_type = &vpip_system_realtime_rt;
|
|
|
|
|
scope->scoped_realtime.scope = scope;
|
|
|
|
|
return &scope->scoped_realtime.base;
|
|
|
|
|
}
|
|
|
|
|
|
2001-07-01 01:03:16 +02:00
|
|
|
int vpip_get_time_precision(void)
|
|
|
|
|
{
|
|
|
|
|
return vpi_time_precision;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void vpip_set_time_precision(int pre)
|
|
|
|
|
{
|
|
|
|
|
vpi_time_precision = pre;
|
|
|
|
|
}
|