473 lines
11 KiB
C++
473 lines
11 KiB
C++
/*
|
|
* Copyright (c) 2012-2026 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
|
|
# include "compile.h"
|
|
# include "vpi_priv.h"
|
|
# include "vvp_net_sig.h"
|
|
# include "vvp_darray.h"
|
|
# include "vvp_cobject.h"
|
|
# include "array_common.h"
|
|
# include "schedule.h"
|
|
#ifdef CHECK_WITH_VALGRIND
|
|
# include "vvp_cleanup.h"
|
|
#endif
|
|
# include <cstdio>
|
|
# include <cstdlib>
|
|
# include <cstring>
|
|
# include <cassert>
|
|
# include "ivl_alloc.h"
|
|
|
|
using namespace std;
|
|
|
|
__vpiDarrayVar::__vpiDarrayVar(__vpiScope*sc, const char*na, vvp_net_t*ne)
|
|
: __vpiBaseVar(sc, na, ne)
|
|
{
|
|
}
|
|
|
|
unsigned __vpiDarrayVar::get_size() const
|
|
{
|
|
const vvp_fun_signal_object*fun = dynamic_cast<vvp_fun_signal_object*> (get_net()->fun);
|
|
if(!fun)
|
|
return 0;
|
|
|
|
vvp_object_t val = fun->get_object();
|
|
const vvp_darray*aval = val.peek<vvp_darray>();
|
|
|
|
if(!aval)
|
|
return 0;
|
|
|
|
return aval->get_size();
|
|
}
|
|
|
|
vpiHandle __vpiDarrayVar::get_left_range()
|
|
{
|
|
left_range_.set_value(0);
|
|
return &left_range_;
|
|
}
|
|
|
|
vpiHandle __vpiDarrayVar::get_right_range()
|
|
{
|
|
right_range_.set_value(get_size() - 1);
|
|
return &right_range_;
|
|
}
|
|
|
|
int __vpiDarrayVar::get_word_size() const
|
|
{
|
|
vvp_vector4_t new_vec;
|
|
vvp_darray*aobj = get_vvp_darray();
|
|
aobj->get_word(0, new_vec);
|
|
return new_vec.size();
|
|
}
|
|
|
|
char*__vpiDarrayVar::get_word_str(struct __vpiArrayWord*word, int code)
|
|
{
|
|
unsigned index = word->get_index();
|
|
|
|
if (code == vpiFile) { // Not implemented for now!
|
|
return simple_set_rbuf_str(file_names[0]);
|
|
}
|
|
|
|
char sidx [64];
|
|
snprintf(sidx, 63, "%d", (int)index);
|
|
return generic_get_str(code, scope_, name_, sidx);
|
|
}
|
|
|
|
void __vpiDarrayVar::get_word_value(struct __vpiArrayWord*word, p_vpi_value vp)
|
|
{
|
|
unsigned index = word->get_index();
|
|
vvp_darray*aobj = get_vvp_darray();
|
|
|
|
if(vp->format == vpiObjTypeVal) {
|
|
if(dynamic_cast<vvp_darray_real*>(aobj))
|
|
vp->format = vpiRealVal;
|
|
else if(dynamic_cast<vvp_darray_string*>(aobj))
|
|
vp->format = vpiStringVal;
|
|
else
|
|
vp->format = vpiVectorVal;
|
|
}
|
|
|
|
switch(vp->format) {
|
|
case vpiBinStrVal:
|
|
case vpiOctStrVal:
|
|
case vpiDecStrVal:
|
|
case vpiHexStrVal:
|
|
case vpiScalarVal:
|
|
case vpiIntVal:
|
|
case vpiVectorVal:
|
|
{
|
|
vvp_vector4_t v;
|
|
aobj->get_word(index, v);
|
|
vpip_vec4_get_value(v, v.size(), false, vp); // TODO sign?
|
|
}
|
|
break;
|
|
|
|
case vpiRealVal:
|
|
{
|
|
double d;
|
|
aobj->get_word(index, d);
|
|
vpip_real_get_value(d, vp);
|
|
}
|
|
break;
|
|
|
|
case vpiStringVal:
|
|
{
|
|
string s;
|
|
aobj->get_word(index, s);
|
|
vpip_string_get_value(s, vp);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
fprintf(stderr, "vpi sorry: format is not implemented\n");
|
|
assert(false);
|
|
}
|
|
}
|
|
|
|
void __vpiDarrayVar::put_word_value(struct __vpiArrayWord*word, p_vpi_value vp, int)
|
|
{
|
|
unsigned index = word->get_index();
|
|
vvp_darray*aobj = get_vvp_darray();
|
|
|
|
switch(vp->format) {
|
|
case vpiScalarVal:
|
|
{
|
|
vvp_vector4_t vec(1, vp->value.scalar);
|
|
aobj->set_word(index, vec);
|
|
}
|
|
break;
|
|
|
|
case vpiIntVal:
|
|
{
|
|
vvp_vector4_t vec;
|
|
unsigned long val = vp->value.integer;
|
|
vec.setarray(0, 8 * sizeof(vp->value.integer), &val);
|
|
aobj->set_word(index, vec);
|
|
}
|
|
break;
|
|
|
|
case vpiVectorVal: // 2 vs 4 state logic?
|
|
{
|
|
int size = get_word_size();
|
|
PLI_INT32 a = 0, b = 0;
|
|
vvp_vector4_t new_vec(size);
|
|
p_vpi_vecval vec = vp->value.vector;
|
|
vec--; // it will be increased in the first loop iteration
|
|
|
|
for(int i = 0; i < size; ++i) {
|
|
int new_bit;
|
|
if(i % (8 * sizeof(vec->aval)) == 0) {
|
|
++vec;
|
|
a = vec->aval;
|
|
b = vec->bval;
|
|
}
|
|
|
|
// convert to vvp_bit4_t
|
|
new_bit = ((b & 1) << 2) | (a & 1);
|
|
new_vec.set_bit(i, (vvp_bit4_t) new_bit);
|
|
|
|
a >>= 1;
|
|
b >>= 1;
|
|
}
|
|
aobj->set_word(index, new_vec);
|
|
}
|
|
break;
|
|
|
|
case vpiRealVal:
|
|
aobj->set_word(index, vp->value.real);
|
|
break;
|
|
|
|
case vpiStringVal:
|
|
aobj->set_word(index, std::string(vp->value.str));
|
|
break;
|
|
|
|
default:
|
|
fprintf(stderr, "vpi sorry: format is not implemented");
|
|
assert(false);
|
|
}
|
|
}
|
|
|
|
vpiHandle __vpiDarrayVar::get_iter_index(struct __vpiArrayIterator*, int idx)
|
|
{
|
|
if (vals_words == 0) make_vals_words();
|
|
|
|
return &(vals_words[idx].as_word);
|
|
}
|
|
|
|
int __vpiDarrayVar::vpi_get(int code)
|
|
{
|
|
switch (code) {
|
|
case vpiArrayType:
|
|
return vpiDynamicArray;
|
|
case vpiLeftRange:
|
|
return 0;
|
|
case vpiRightRange:
|
|
return get_size() - 1;
|
|
case vpiSize:
|
|
return get_size();
|
|
|
|
default:
|
|
fprintf(stderr, "vpi sorry: property is not implemented");
|
|
assert(false);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
char* __vpiDarrayVar::vpi_get_str(int code)
|
|
{
|
|
if (code == vpiFile) { // Not implemented for now!
|
|
return simple_set_rbuf_str(file_names[0]);
|
|
}
|
|
|
|
return generic_get_str(code, scope_, name_, NULL);
|
|
}
|
|
|
|
vpiHandle __vpiDarrayVar::vpi_handle(int code)
|
|
{
|
|
switch (code) {
|
|
case vpiLeftRange:
|
|
return get_left_range();
|
|
|
|
case vpiRightRange:
|
|
return get_right_range();
|
|
|
|
case vpiScope:
|
|
return scope_;
|
|
|
|
case vpiModule:
|
|
return vpip_module(scope_);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
vpiHandle __vpiDarrayVar::vpi_index(int index)
|
|
{
|
|
if (index >= (long) get_size())
|
|
return 0;
|
|
if (index < 0)
|
|
return 0;
|
|
|
|
if (vals_words == 0)
|
|
make_vals_words();
|
|
|
|
return &(vals_words[index].as_word);
|
|
}
|
|
|
|
void __vpiDarrayVar::vpi_get_value(p_vpi_value val)
|
|
{
|
|
val->format = vpiSuppressVal;
|
|
}
|
|
|
|
vvp_darray*__vpiDarrayVar::get_vvp_darray() const
|
|
{
|
|
vvp_fun_signal_object*fun = dynamic_cast<vvp_fun_signal_object*> (get_net()->fun);
|
|
assert(fun);
|
|
vvp_object_t obj = fun->get_object();
|
|
|
|
return obj.peek<vvp_darray>();
|
|
}
|
|
|
|
vpiHandle vpip_make_darray_var(const char*name, vvp_net_t*net)
|
|
{
|
|
__vpiScope*scope = vpip_peek_current_scope();
|
|
const char*use_name = name ? vpip_name_string(name) : NULL;
|
|
|
|
__vpiDarrayVar*obj = new __vpiDarrayVar(scope, use_name, net);
|
|
|
|
return obj;
|
|
}
|
|
|
|
__vpiQueueVar::__vpiQueueVar(__vpiScope*sc, const char*na, vvp_net_t*ne)
|
|
: __vpiDarrayVar(sc, na, ne)
|
|
{
|
|
}
|
|
|
|
int __vpiQueueVar::vpi_get(int code)
|
|
{
|
|
switch (code) {
|
|
case vpiArrayType:
|
|
return vpiQueueArray;
|
|
default:
|
|
return __vpiDarrayVar::vpi_get(code);
|
|
}
|
|
}
|
|
|
|
void __vpiQueueVar::vpi_get_value(p_vpi_value val)
|
|
{
|
|
__vpiDarrayVar::vpi_get_value(val);
|
|
}
|
|
|
|
|
|
vpiHandle vpip_make_queue_var(const char*name, vvp_net_t*net)
|
|
{
|
|
__vpiScope*scope = vpip_peek_current_scope();
|
|
const char*use_name = name ? vpip_name_string(name) : NULL;
|
|
|
|
__vpiQueueVar*obj = new __vpiQueueVar(scope, use_name, net);
|
|
|
|
return obj;
|
|
}
|
|
|
|
__vpiPropQueueRef::__vpiPropQueueRef(__vpiScope*scope, unsigned pidx,
|
|
bool is_queue)
|
|
: class_net_(0), prop_idx_(pidx), is_queue_(is_queue), scope_(scope)
|
|
{
|
|
}
|
|
|
|
int __vpiPropQueueRef::get_type_code(void) const
|
|
{ return vpiArrayVar; }
|
|
|
|
static vvp_darray* get_live_darray_from_prop(vvp_net_t* class_net, unsigned pid)
|
|
{
|
|
if (!class_net)
|
|
return 0;
|
|
vvp_fun_signal_object* fun
|
|
= dynamic_cast<vvp_fun_signal_object*>(class_net->fun);
|
|
if (!fun)
|
|
return 0;
|
|
vvp_object_t obj = fun->get_object();
|
|
vvp_cobject* cobj = obj.peek<vvp_cobject>();
|
|
if (!cobj)
|
|
return 0;
|
|
vvp_object_t qobj;
|
|
cobj->get_object(pid, qobj, 0);
|
|
return qobj.peek<vvp_darray>();
|
|
}
|
|
|
|
int __vpiPropQueueRef::vpi_get(int code)
|
|
{
|
|
vvp_darray* aobj = get_live_darray_from_prop(class_net_, prop_idx_);
|
|
switch (code) {
|
|
case vpiArrayType:
|
|
return is_queue_ ? vpiQueueArray : vpiDynamicArray;
|
|
case vpiLeftRange:
|
|
return 0;
|
|
case vpiRightRange:
|
|
return aobj ? (int) (aobj->get_size() - 1) : 0;
|
|
case vpiSize:
|
|
return aobj ? (int) aobj->get_size() : 0;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
char* __vpiPropQueueRef::vpi_get_str(int code)
|
|
{
|
|
if (code == vpiFile) {
|
|
return simple_set_rbuf_str(file_names[0]);
|
|
}
|
|
return generic_get_str(code, scope_, "prop_darray", NULL);
|
|
}
|
|
|
|
void __vpiPropQueueRef::vpi_get_value(p_vpi_value val)
|
|
{
|
|
val->format = vpiSuppressVal;
|
|
}
|
|
|
|
vpiHandle vpip_make_prop_queue_ref(char* class_label, unsigned prop_idx,
|
|
unsigned is_queue_flag)
|
|
{
|
|
__vpiPropQueueRef* obj = new __vpiPropQueueRef(vpip_peek_current_scope(),
|
|
prop_idx,
|
|
is_queue_flag != 0);
|
|
functor_ref_lookup(&obj->class_net_, class_label);
|
|
return obj;
|
|
}
|
|
|
|
static char* format_darray_pretty(vvp_darray* aobj)
|
|
{
|
|
if (!aobj || aobj->get_size() == 0)
|
|
return strdup("{}");
|
|
|
|
string out = "{";
|
|
|
|
for (size_t i = 0; i < aobj->get_size(); i += 1) {
|
|
if (i > 0)
|
|
out += ", ";
|
|
if (dynamic_cast<vvp_darray_real*>(aobj)) {
|
|
double d;
|
|
aobj->get_word((unsigned) i, d);
|
|
char buf[256];
|
|
snprintf(buf, sizeof buf, "%g", d);
|
|
out += buf;
|
|
} else if (dynamic_cast<vvp_darray_string*>(aobj)) {
|
|
string s;
|
|
aobj->get_word((unsigned) i, s);
|
|
out += "\"";
|
|
out += s;
|
|
out += "\"";
|
|
} else {
|
|
vvp_vector4_t v;
|
|
aobj->get_word((unsigned) i, v);
|
|
s_vpi_value val;
|
|
val.format = vpiDecStrVal;
|
|
vpip_vec4_get_value(v, v.size(), false, &val);
|
|
out += val.value.str;
|
|
}
|
|
}
|
|
|
|
out += "}";
|
|
return strdup(out.c_str());
|
|
}
|
|
|
|
extern "C" char* vpip_format_pretty(vpiHandle ref)
|
|
{
|
|
if (!ref)
|
|
return 0;
|
|
|
|
if (__vpiPropQueueRef* pr = dynamic_cast<__vpiPropQueueRef*>(ref)) {
|
|
vvp_darray* a = get_live_darray_from_prop(pr->class_net_,
|
|
pr->prop_idx_);
|
|
return format_darray_pretty(a);
|
|
}
|
|
|
|
if (__vpiDarrayVar* dv = dynamic_cast<__vpiDarrayVar*>(ref)) {
|
|
vvp_fun_signal_object* fun
|
|
= dynamic_cast<vvp_fun_signal_object*>(dv->get_net()->fun);
|
|
if (!fun)
|
|
return 0;
|
|
vvp_object_t obj = fun->get_object();
|
|
vvp_darray* aobj = obj.peek<vvp_darray>();
|
|
return format_darray_pretty(aobj);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
#ifdef CHECK_WITH_VALGRIND
|
|
void array_delete(vpiHandle item)
|
|
{
|
|
if (__vpiPropQueueRef* pq = dynamic_cast<__vpiPropQueueRef*>(item)) {
|
|
delete pq;
|
|
return;
|
|
}
|
|
|
|
__vpiDarrayVar*dobj = dynamic_cast<__vpiDarrayVar*>(item);
|
|
if (dobj) {
|
|
if (dobj->vals_words) delete [] (dobj->vals_words-1);
|
|
delete dobj;
|
|
return;
|
|
}
|
|
|
|
fprintf(stderr, "Need support for deleting array type: %d\n", item->vpi_get(vpiArrayType));
|
|
assert(0);
|
|
}
|
|
#endif
|