2003-01-26 00:48:05 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2003 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
|
|
|
|
|
*/
|
|
|
|
|
#ifdef HAVE_CVS_IDENT
|
2003-03-06 05:32:00 +01:00
|
|
|
#ident "$Id: vpi_real.cc,v 1.7 2003/03/06 04:32:00 steve Exp $"
|
2003-01-26 00:48:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
# include "vpi_priv.h"
|
2003-02-10 06:20:10 +01:00
|
|
|
# include "schedule.h"
|
2003-01-26 00:48:05 +01:00
|
|
|
# include <stdio.h>
|
|
|
|
|
# include <stdlib.h>
|
|
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
|
# include <malloc.h>
|
|
|
|
|
#endif
|
|
|
|
|
# include <assert.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct __vpiRealVar {
|
|
|
|
|
struct __vpiHandle base;
|
|
|
|
|
const char*name;
|
|
|
|
|
double value;
|
2003-02-10 06:20:10 +01:00
|
|
|
|
|
|
|
|
struct __vpiCallback*cb;
|
2003-01-26 00:48:05 +01:00
|
|
|
};
|
|
|
|
|
|
2003-02-10 06:20:10 +01:00
|
|
|
static char* real_var_get_str(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
assert(ref->vpi_type->type_code == vpiRealVar);
|
|
|
|
|
|
|
|
|
|
struct __vpiRealVar*rfp = (struct __vpiRealVar*)ref;
|
|
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
|
|
|
|
|
|
case vpiName:
|
|
|
|
|
return const_cast<char*>(rfp->name);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-26 00:48:05 +01:00
|
|
|
static void real_var_get_value(vpiHandle ref, s_vpi_value*vp)
|
|
|
|
|
{
|
|
|
|
|
assert(ref->vpi_type->type_code == vpiRealVar);
|
|
|
|
|
|
2003-02-04 05:03:40 +01:00
|
|
|
static char buf[66];
|
2003-01-26 00:48:05 +01:00
|
|
|
struct __vpiRealVar*rfp = (struct __vpiRealVar*)ref;
|
|
|
|
|
|
|
|
|
|
switch (vp->format) {
|
|
|
|
|
case vpiObjTypeVal:
|
|
|
|
|
vp->format = vpiRealVal;
|
2003-02-28 22:20:34 +01:00
|
|
|
|
2003-01-26 00:48:05 +01:00
|
|
|
case vpiRealVal:
|
|
|
|
|
vp->value.real = rfp->value;
|
|
|
|
|
break;
|
2003-02-04 05:03:40 +01:00
|
|
|
|
2003-02-28 22:20:34 +01:00
|
|
|
case vpiIntVal:
|
|
|
|
|
vp->value.integer = (int)(rfp->value + 0.5);
|
|
|
|
|
break;
|
|
|
|
|
|
2003-01-27 01:14:37 +01:00
|
|
|
case vpiDecStrVal:
|
|
|
|
|
sprintf(buf, "%0.0f", rfp->value);
|
|
|
|
|
vp->value.str = buf;
|
|
|
|
|
break;
|
|
|
|
|
|
2003-02-04 05:03:40 +01:00
|
|
|
case vpiHexStrVal:
|
|
|
|
|
sprintf(buf, "%lx", (long)rfp->value);
|
|
|
|
|
vp->value.str = buf;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case vpiBinStrVal: {
|
|
|
|
|
unsigned long val = (unsigned long)rfp->value;
|
|
|
|
|
unsigned len = 0;
|
|
|
|
|
|
|
|
|
|
assert(8*sizeof(val) < sizeof buf);
|
|
|
|
|
|
|
|
|
|
while (val > 0) {
|
|
|
|
|
len += 1;
|
|
|
|
|
val /= 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val = (unsigned long)rfp->value;
|
|
|
|
|
for (unsigned idx = 0 ; idx < len ; idx += 1) {
|
|
|
|
|
buf[len-idx-1] = (val & 1)? '1' : '0';
|
|
|
|
|
val /= 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf[len] = 0;
|
|
|
|
|
if (len == 0) {
|
|
|
|
|
buf[0] = '0';
|
|
|
|
|
buf[1] = 0;
|
|
|
|
|
}
|
|
|
|
|
vp->value.str = buf;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-26 00:48:05 +01:00
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "ERROR: Unsupported format code: %d\n",
|
|
|
|
|
vp->format);
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static vpiHandle real_var_put_value(vpiHandle ref, p_vpi_value vp,
|
|
|
|
|
p_vpi_time, int)
|
|
|
|
|
{
|
|
|
|
|
assert(ref->vpi_type->type_code == vpiRealVar);
|
|
|
|
|
|
|
|
|
|
struct __vpiRealVar*rfp = (struct __vpiRealVar*)ref;
|
|
|
|
|
|
|
|
|
|
assert(vp->format == vpiRealVal);
|
|
|
|
|
rfp->value = vp->value.real;
|
2003-02-10 06:20:10 +01:00
|
|
|
|
|
|
|
|
for (struct __vpiCallback*cur = rfp->cb ; cur ; cur = cur->next) {
|
|
|
|
|
cur->cb_data.time->type = vpiSimTime;
|
|
|
|
|
vpip_time_to_timestruct(cur->cb_data.time, schedule_simtime());
|
|
|
|
|
|
|
|
|
|
assert(cur->cb_data.cb_rtn != 0);
|
|
|
|
|
|
|
|
|
|
(cur->cb_data.cb_rtn)(&cur->cb_data);
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-26 00:48:05 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct __vpirt vpip_real_var_rt = {
|
|
|
|
|
vpiRealVar,
|
|
|
|
|
|
|
|
|
|
0,
|
2003-02-10 06:20:10 +01:00
|
|
|
&real_var_get_str,
|
2003-01-26 00:48:05 +01:00
|
|
|
&real_var_get_value,
|
|
|
|
|
&real_var_put_value,
|
|
|
|
|
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
|
2003-02-02 02:40:24 +01:00
|
|
|
0
|
2003-01-26 00:48:05 +01:00
|
|
|
};
|
|
|
|
|
|
2003-02-10 06:20:10 +01:00
|
|
|
void vpip_real_value_change(struct __vpiCallback*cbh,
|
|
|
|
|
vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiRealVar*rfp = (struct __vpiRealVar*)ref;
|
|
|
|
|
cbh->next = rfp->cb;
|
|
|
|
|
rfp->cb = cbh;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-26 00:48:05 +01:00
|
|
|
vpiHandle vpip_make_real_var(const char*name)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiRealVar*obj = (struct __vpiRealVar*)
|
|
|
|
|
malloc(sizeof(struct __vpiRealVar));
|
|
|
|
|
|
|
|
|
|
obj->base.vpi_type = &vpip_real_var_rt;
|
2003-03-06 05:32:00 +01:00
|
|
|
obj->name = vpip_name_string(name);
|
2003-01-26 00:48:05 +01:00
|
|
|
obj->value = 0.0;
|
2003-02-10 06:20:10 +01:00
|
|
|
obj->cb = 0;
|
2003-01-26 00:48:05 +01:00
|
|
|
|
|
|
|
|
return &obj->base;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* $Log: vpi_real.cc,v $
|
2003-03-06 05:32:00 +01:00
|
|
|
* Revision 1.7 2003/03/06 04:32:00 steve
|
|
|
|
|
* Use hashed name strings for identifiers.
|
|
|
|
|
*
|
2003-02-28 22:20:34 +01:00
|
|
|
* Revision 1.6 2003/02/28 21:20:34 steve
|
|
|
|
|
* Allow read of realvar as int.
|
|
|
|
|
*
|
2003-02-10 06:20:10 +01:00
|
|
|
* Revision 1.5 2003/02/10 05:20:10 steve
|
|
|
|
|
* Add value change callbacks to real variables.
|
|
|
|
|
*
|
2003-02-04 05:03:40 +01:00
|
|
|
* Revision 1.4 2003/02/04 04:03:40 steve
|
|
|
|
|
* Add hex and binary formatting of real values.
|
|
|
|
|
*
|
2003-02-02 02:40:24 +01:00
|
|
|
* Revision 1.3 2003/02/02 01:40:24 steve
|
|
|
|
|
* Five vpi_free_object a default behavior.
|
|
|
|
|
*
|
2003-01-27 01:14:37 +01:00
|
|
|
* Revision 1.2 2003/01/27 00:14:37 steve
|
|
|
|
|
* Support in various contexts the $realtime
|
|
|
|
|
* system task.
|
|
|
|
|
*
|
2003-01-26 00:48:05 +01:00
|
|
|
* Revision 1.1 2003/01/25 23:48:06 steve
|
|
|
|
|
* Add thread word array, and add the instructions,
|
|
|
|
|
* %add/wr, %cmp/wr, %load/wr, %mul/wr and %set/wr.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|