2001-05-09 01:59:33 +02:00
|
|
|
/*
|
2010-12-14 02:42:48 +01:00
|
|
|
* Copyright (c) 1999-2010 Picture Elements, Inc.
|
2001-05-09 01:59:33 +02:00
|
|
|
* Stephen Williams (steve@picturel.com)
|
|
|
|
|
* Copyright (c) 2001 Stephan Boettcher <stephan@nevis.columbia.edu>
|
|
|
|
|
*
|
|
|
|
|
* 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. In order to redistribute the software in
|
|
|
|
|
* binary form, you will need a Picture Elements Binary Software
|
|
|
|
|
* License.
|
|
|
|
|
*
|
|
|
|
|
* 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-02-10 00:33:26 +01:00
|
|
|
* You should also have received a copy of the Picture Elements
|
2001-05-09 01:59:33 +02:00
|
|
|
* Binary Software License offer along with the source. This offer
|
|
|
|
|
* allows you to obtain the right to redistribute the software in
|
|
|
|
|
* binary (compiled) form. If you have not received it, contact
|
|
|
|
|
* Picture Elements, Inc., 777 Panoramic Way, Berkeley, CA 94704.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
# include "vpi_priv.h"
|
|
|
|
|
# include "memory.h"
|
2002-07-05 19:14:15 +02:00
|
|
|
# include "statistics.h"
|
2001-05-09 01:59:33 +02:00
|
|
|
# include <stdlib.h>
|
2002-07-03 04:09:38 +02:00
|
|
|
# include <string.h>
|
2001-05-09 01:59:33 +02:00
|
|
|
# include <assert.h>
|
|
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
# include <stdio.h>
|
|
|
|
|
|
2002-02-06 05:48:34 +01:00
|
|
|
extern const char hex_digits[256];
|
2001-05-09 01:59:33 +02:00
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
static void memory_make_word_handles(struct __vpiMemory*rfp);
|
|
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
struct __vpiMemoryWord {
|
|
|
|
|
struct __vpiHandle base;
|
|
|
|
|
struct __vpiMemory*mem;
|
2002-05-17 06:12:19 +02:00
|
|
|
struct __vpiDecConst index;
|
2001-05-09 01:59:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct __vpiMemory {
|
|
|
|
|
struct __vpiHandle base;
|
2002-05-10 18:00:57 +02:00
|
|
|
struct __vpiScope* scope;
|
2002-05-17 06:12:19 +02:00
|
|
|
struct __vpiMemoryWord*words;
|
2001-05-09 01:59:33 +02:00
|
|
|
vvp_memory_t mem;
|
2002-05-17 06:12:19 +02:00
|
|
|
struct __vpiDecConst left_range;
|
|
|
|
|
struct __vpiDecConst right_range;
|
|
|
|
|
struct __vpiDecConst word_left_range;
|
|
|
|
|
struct __vpiDecConst word_right_range;
|
2002-07-03 04:09:38 +02:00
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
};
|
|
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
struct __vpiMemWordIterator {
|
|
|
|
|
struct __vpiHandle base;
|
|
|
|
|
struct __vpiMemory*mem;
|
|
|
|
|
unsigned next;
|
|
|
|
|
};
|
|
|
|
|
|
2002-01-31 05:28:17 +01:00
|
|
|
static vpiHandle memory_get_handle(int code, vpiHandle obj)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiMemory*rfp = (struct __vpiMemory*)obj;
|
|
|
|
|
|
|
|
|
|
assert(obj->vpi_type->type_code==vpiMemory);
|
|
|
|
|
|
|
|
|
|
switch(code){
|
2002-05-10 18:00:57 +02:00
|
|
|
case vpiLeftRange:
|
2002-05-17 06:12:19 +02:00
|
|
|
return &(rfp->left_range.base);
|
2002-05-10 18:00:57 +02:00
|
|
|
|
|
|
|
|
case vpiRightRange:
|
2002-05-17 06:12:19 +02:00
|
|
|
return &(rfp->right_range.base);
|
2002-05-10 18:00:57 +02:00
|
|
|
|
|
|
|
|
case vpiScope:
|
|
|
|
|
return &rfp->scope->base;
|
2002-01-31 05:28:17 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
static int vpi_memory_get(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiMemory*rfp = (struct __vpiMemory*)ref;
|
|
|
|
|
|
|
|
|
|
assert(ref->vpi_type->type_code==vpiMemory);
|
|
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiSize:
|
|
|
|
|
return (int)memory_size(rfp->mem);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char* memory_get_str(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
assert(ref->vpi_type->type_code==vpiMemory);
|
|
|
|
|
|
2002-07-03 04:09:38 +02:00
|
|
|
struct __vpiMemory*rfp = (struct __vpiMemory*)ref;
|
|
|
|
|
|
2002-09-11 18:06:57 +02:00
|
|
|
char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base));
|
2002-07-03 04:09:38 +02:00
|
|
|
char *nm = memory_name(rfp->mem);
|
|
|
|
|
|
2002-09-11 18:06:57 +02:00
|
|
|
char *rbuf = need_result_buf(strlen(bn) + strlen(nm) + 2, RBUF_STR);
|
2002-07-03 04:09:38 +02:00
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
switch (code) {
|
|
|
|
|
case vpiFullName:
|
2002-07-04 01:39:57 +02:00
|
|
|
sprintf(rbuf, "%s.%s", bn, nm);
|
2002-09-11 18:06:57 +02:00
|
|
|
free(bn);
|
2002-07-04 01:39:57 +02:00
|
|
|
return rbuf;
|
2002-07-03 04:09:38 +02:00
|
|
|
case vpiName:
|
2002-07-04 01:39:57 +02:00
|
|
|
strcpy(rbuf, nm);
|
2002-09-11 18:06:57 +02:00
|
|
|
free(bn);
|
2002-07-04 01:39:57 +02:00
|
|
|
return rbuf;
|
2001-05-09 01:59:33 +02:00
|
|
|
}
|
2002-07-03 04:09:38 +02:00
|
|
|
|
2002-09-11 18:06:57 +02:00
|
|
|
free(bn);
|
2001-05-09 01:59:33 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static vpiHandle memory_scan(vpiHandle ref, int)
|
|
|
|
|
{
|
2002-05-17 06:12:19 +02:00
|
|
|
struct __vpiMemWordIterator*obj = (struct __vpiMemWordIterator*)ref;
|
2001-05-09 01:59:33 +02:00
|
|
|
assert(ref->vpi_type->type_code == vpiIterator);
|
|
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
if (obj->next >= memory_size(obj->mem->mem)) {
|
2001-05-09 01:59:33 +02:00
|
|
|
vpi_free_object(ref);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
return &obj->mem->words[obj->next++].base;
|
2001-05-09 01:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
2002-05-03 17:44:11 +02:00
|
|
|
static int mem_iter_free_object(vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
free(ref);
|
2003-02-02 02:40:24 +01:00
|
|
|
return 1;
|
2002-05-03 17:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
static const struct __vpirt vpip_mem_iter_rt = {
|
|
|
|
|
vpiIterator,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
memory_scan,
|
2002-05-03 17:44:11 +02:00
|
|
|
&mem_iter_free_object
|
2001-05-09 01:59:33 +02:00
|
|
|
};
|
|
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
static vpiHandle memory_iterate(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiMemory*rfp = (struct __vpiMemory*)ref;
|
|
|
|
|
assert(ref->vpi_type->type_code==vpiMemory);
|
|
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiMemoryWord: {
|
2002-05-17 06:12:19 +02:00
|
|
|
memory_make_word_handles(rfp);
|
|
|
|
|
|
|
|
|
|
struct __vpiMemWordIterator*res =
|
|
|
|
|
(struct __vpiMemWordIterator*)
|
|
|
|
|
calloc(1, sizeof(struct __vpiMemWordIterator));
|
2001-05-09 01:59:33 +02:00
|
|
|
assert(res);
|
|
|
|
|
res->base.vpi_type = &vpip_mem_iter_rt;
|
2002-05-17 06:12:19 +02:00
|
|
|
res->mem = rfp;
|
|
|
|
|
res->next = 0;
|
2001-05-09 01:59:33 +02:00
|
|
|
return &(res->base);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static vpiHandle memory_index(vpiHandle ref, int index)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiMemory*rfp = (struct __vpiMemory*)ref;
|
|
|
|
|
assert(ref->vpi_type->type_code==vpiMemory);
|
|
|
|
|
|
|
|
|
|
index -= memory_root(rfp->mem);
|
|
|
|
|
if (index >= (int)memory_size(rfp->mem)) return 0;
|
|
|
|
|
if (index < 0) return 0;
|
2002-05-17 06:12:19 +02:00
|
|
|
|
|
|
|
|
memory_make_word_handles(rfp);
|
|
|
|
|
return &(rfp->words[index].base);
|
2001-05-09 01:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
2002-01-31 05:28:17 +01:00
|
|
|
//==============================
|
|
|
|
|
|
|
|
|
|
static vpiHandle memory_word_get_handle(int code, vpiHandle obj)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiMemoryWord*rfp = (struct __vpiMemoryWord*)obj;
|
|
|
|
|
assert(obj->vpi_type->type_code==vpiMemoryWord);
|
|
|
|
|
|
|
|
|
|
switch(code){
|
2002-05-17 06:12:19 +02:00
|
|
|
case vpiLeftRange:
|
|
|
|
|
return &(rfp->mem->word_left_range.base);
|
|
|
|
|
|
|
|
|
|
case vpiRightRange:
|
|
|
|
|
return &(rfp->mem->word_right_range.base);
|
2002-01-31 05:28:17 +01:00
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
case vpiIndex:
|
|
|
|
|
return &(rfp->index.base);
|
2002-01-31 05:28:17 +01:00
|
|
|
}
|
|
|
|
|
|
2002-07-03 04:09:38 +02:00
|
|
|
|
2002-01-31 05:28:17 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
static int memory_word_get(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiMemoryWord*rfp = (struct __vpiMemoryWord*)ref;
|
|
|
|
|
assert(ref->vpi_type->type_code==vpiMemoryWord);
|
|
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiSize:
|
|
|
|
|
return memory_data_width(rfp->mem->mem);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-19 05:26:24 +02:00
|
|
|
static vpiHandle memory_word_put(vpiHandle ref, p_vpi_value val)
|
2001-05-09 01:59:33 +02:00
|
|
|
{
|
|
|
|
|
struct __vpiMemoryWord*rfp = (struct __vpiMemoryWord*)ref;
|
|
|
|
|
assert(ref->vpi_type->type_code==vpiMemoryWord);
|
|
|
|
|
|
|
|
|
|
|
2001-12-08 00:23:05 +01:00
|
|
|
/* Get the width of the memory, and the byte index of the
|
|
|
|
|
first byte of the word. */
|
2001-05-09 01:59:33 +02:00
|
|
|
unsigned width = memory_data_width(rfp->mem->mem);
|
2002-05-17 06:12:19 +02:00
|
|
|
unsigned word_offset = memory_root(rfp->mem->mem);
|
|
|
|
|
unsigned bidx = (rfp->index.value - word_offset) * ((width+3)&~3);
|
2001-05-09 01:59:33 +02:00
|
|
|
|
2001-12-08 00:23:05 +01:00
|
|
|
switch (val->format) {
|
|
|
|
|
|
|
|
|
|
case vpiVectorVal:
|
|
|
|
|
for (unsigned widx = 0; widx < width; widx += 32) {
|
|
|
|
|
p_vpi_vecval cur = val->value.vector + (widx/32);
|
|
|
|
|
for (unsigned idx = widx
|
|
|
|
|
; idx < width && idx < widx+32
|
|
|
|
|
; idx += 1) {
|
|
|
|
|
int aval = (cur->aval >> (idx%32)) & 1;
|
|
|
|
|
int bval = (cur->bval >> (idx%32)) & 1;
|
|
|
|
|
unsigned char val = (bval<<1) | (aval^bval);
|
|
|
|
|
memory_set(rfp->mem->mem, bidx+idx, val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case vpiIntVal:
|
|
|
|
|
for (unsigned widx = 0; widx < width; widx += 32) {
|
|
|
|
|
int cur = val->value.integer;
|
|
|
|
|
for (unsigned idx = widx
|
|
|
|
|
; idx < width && idx < widx+32
|
|
|
|
|
; idx += 1) {
|
|
|
|
|
unsigned char val = (cur&1)? 1 : 0;
|
|
|
|
|
memory_set(rfp->mem->mem, bidx+idx, val);
|
|
|
|
|
cur >>= 1;
|
|
|
|
|
}
|
2001-05-09 01:59:33 +02:00
|
|
|
}
|
2001-12-08 00:23:05 +01:00
|
|
|
break;
|
|
|
|
|
|
2002-05-11 06:39:35 +02:00
|
|
|
/* If the caller tries to set a HexStrVal, convert it to
|
|
|
|
|
bits and write the bits into the word. */
|
|
|
|
|
case vpiHexStrVal: {
|
|
|
|
|
unsigned char*bits = new unsigned char[(width+3) / 4];
|
|
|
|
|
vpip_hex_str_to_bits(bits, width, val->value.str, false);
|
|
|
|
|
|
|
|
|
|
for (unsigned idx = 0 ; idx < width ; idx += 1) {
|
|
|
|
|
unsigned bb = idx / 4;
|
|
|
|
|
unsigned bs = (idx % 4) * 2;
|
|
|
|
|
unsigned val = (bits[bb] >> bs) & 0x03;
|
|
|
|
|
memory_set(rfp->mem->mem, bidx+idx, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete[]bits;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case vpiDecStrVal: {
|
|
|
|
|
unsigned char*bits = new unsigned char[width];
|
|
|
|
|
vpip_dec_str_to_bits(bits, width, val->value.str, false);
|
|
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
for (unsigned idx = 0 ; idx < width ; idx += 1) {
|
2002-05-11 06:39:35 +02:00
|
|
|
memory_set(rfp->mem->mem, bidx+idx, bits[idx]);
|
2002-05-17 06:12:19 +02:00
|
|
|
}
|
2002-05-11 06:39:35 +02:00
|
|
|
|
|
|
|
|
delete[]bits;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case vpiOctStrVal: {
|
|
|
|
|
unsigned char*bits = new unsigned char[(width+3) / 4];
|
|
|
|
|
vpip_oct_str_to_bits(bits, width, val->value.str, false);
|
|
|
|
|
|
|
|
|
|
for (unsigned idx = 0 ; idx < width ; idx += 1) {
|
|
|
|
|
unsigned bb = idx / 4;
|
|
|
|
|
unsigned bs = (idx % 4) * 2;
|
|
|
|
|
unsigned val = (bits[bb] >> bs) & 0x03;
|
|
|
|
|
memory_set(rfp->mem->mem, bidx+idx, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete[]bits;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case vpiBinStrVal: {
|
|
|
|
|
unsigned char*bits = new unsigned char[(width+3) / 4];
|
|
|
|
|
vpip_bin_str_to_bits(bits, width, val->value.str, false);
|
|
|
|
|
|
|
|
|
|
for (unsigned idx = 0 ; idx < width ; idx += 1) {
|
|
|
|
|
unsigned bb = idx / 4;
|
|
|
|
|
unsigned bs = (idx % 4) * 2;
|
|
|
|
|
unsigned val = (bits[bb] >> bs) & 0x03;
|
|
|
|
|
memory_set(rfp->mem->mem, bidx+idx, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete[]bits;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-08 00:23:05 +01:00
|
|
|
default:
|
|
|
|
|
assert(0);
|
2001-05-09 01:59:33 +02:00
|
|
|
}
|
2001-12-08 00:23:05 +01:00
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-07-03 04:09:38 +02:00
|
|
|
static char* memory_word_get_str(int code, vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
assert(ref->vpi_type->type_code==vpiMemoryWord);
|
|
|
|
|
|
|
|
|
|
struct __vpiMemoryWord*rfp = (struct __vpiMemoryWord*)ref;
|
|
|
|
|
|
2002-09-12 17:13:07 +02:00
|
|
|
char *bn = strdup(vpi_get_str(vpiFullName, &rfp->mem->scope->base));
|
2002-07-03 04:09:38 +02:00
|
|
|
char *nm = memory_name(rfp->mem->mem);
|
|
|
|
|
|
2002-09-12 17:13:07 +02:00
|
|
|
char *rbuf = need_result_buf(strlen(bn) + strlen(nm) + 10 + 4, RBUF_STR);
|
2002-07-03 04:09:38 +02:00
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiFullName:
|
2002-07-04 01:39:57 +02:00
|
|
|
sprintf(rbuf, "%s.%s[%d]", bn, nm, rfp->index.value);
|
2002-09-12 17:13:07 +02:00
|
|
|
free(bn);
|
2002-07-04 01:39:57 +02:00
|
|
|
return rbuf;
|
2002-07-03 04:09:38 +02:00
|
|
|
break;
|
|
|
|
|
case vpiName: {
|
2002-07-04 01:39:57 +02:00
|
|
|
sprintf(rbuf, "%s[%d]", nm, rfp->index.value);
|
2002-09-12 17:13:07 +02:00
|
|
|
free(bn);
|
2002-07-04 01:39:57 +02:00
|
|
|
return rbuf;
|
2002-07-03 04:09:38 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-12 17:13:07 +02:00
|
|
|
free(bn);
|
2002-07-03 04:09:38 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
static void memory_word_get_value(vpiHandle ref, s_vpi_value*vp)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiMemoryWord*rfp = (struct __vpiMemoryWord*)ref;
|
|
|
|
|
assert(rfp->base.vpi_type->type_code==vpiMemoryWord);
|
2002-07-03 04:09:38 +02:00
|
|
|
|
2001-11-09 04:39:07 +01:00
|
|
|
unsigned width = memory_data_width(rfp->mem->mem);
|
2002-05-17 06:12:19 +02:00
|
|
|
unsigned word_offset = memory_root(rfp->mem->mem);
|
|
|
|
|
unsigned bidx = (rfp->index.value - word_offset) * ((width+3)&~3);
|
2001-11-09 04:39:07 +01:00
|
|
|
|
2002-07-03 04:09:38 +02:00
|
|
|
char *rbuf = 0;
|
|
|
|
|
|
2001-11-09 04:39:07 +01:00
|
|
|
switch (vp->format) {
|
|
|
|
|
default:
|
2002-06-30 06:35:47 +02:00
|
|
|
assert("format not implemented");
|
2001-11-09 04:39:07 +01:00
|
|
|
|
2002-07-03 04:09:38 +02:00
|
|
|
case vpiBinStrVal:
|
2002-07-09 05:24:37 +02:00
|
|
|
rbuf = need_result_buf(width+1, RBUF_VAL);
|
2002-02-06 05:48:34 +01:00
|
|
|
for (unsigned idx = 0 ; idx < width ; idx += 1) {
|
|
|
|
|
unsigned bit = memory_get(rfp->mem->mem, bidx+idx);
|
2002-07-03 04:09:38 +02:00
|
|
|
|
|
|
|
|
rbuf[width-idx-1] = "01xz"[bit];
|
2002-02-06 05:48:34 +01:00
|
|
|
}
|
2002-07-03 04:09:38 +02:00
|
|
|
rbuf[width] = 0;
|
|
|
|
|
vp->value.str = rbuf;
|
2002-02-06 05:48:34 +01:00
|
|
|
break;
|
|
|
|
|
|
2002-05-11 06:39:35 +02:00
|
|
|
case vpiOctStrVal: {
|
2002-07-03 04:09:38 +02:00
|
|
|
unsigned hwid = (width+2) / 3;
|
|
|
|
|
unsigned char*bits = new unsigned char[width];
|
2002-05-11 06:39:35 +02:00
|
|
|
|
|
|
|
|
for (unsigned idx = 0 ; idx < width ; idx += 1) {
|
|
|
|
|
unsigned bb = idx / 4;
|
|
|
|
|
unsigned bs = (idx % 4) * 2;
|
|
|
|
|
unsigned val = memory_get(rfp->mem->mem, bidx+idx);
|
|
|
|
|
if (bs == 0)
|
|
|
|
|
bits[bb] = val;
|
|
|
|
|
else
|
|
|
|
|
bits[bb] |= val << bs;
|
|
|
|
|
}
|
|
|
|
|
|
2002-07-09 05:24:37 +02:00
|
|
|
rbuf = need_result_buf(hwid+1, RBUF_VAL);
|
2002-07-03 04:09:38 +02:00
|
|
|
vpip_bits_to_oct_str(bits, width, rbuf, hwid+1, false);
|
2002-05-11 06:39:35 +02:00
|
|
|
|
|
|
|
|
delete[]bits;
|
2002-07-03 04:09:38 +02:00
|
|
|
vp->value.str = rbuf;
|
2002-05-11 06:39:35 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2002-02-06 05:48:34 +01:00
|
|
|
case vpiHexStrVal: {
|
|
|
|
|
unsigned hval, hwid;
|
|
|
|
|
hwid = (width + 3) / 4;
|
|
|
|
|
|
2002-07-09 05:24:37 +02:00
|
|
|
rbuf = need_result_buf(hwid+1, RBUF_VAL);
|
2002-07-03 04:09:38 +02:00
|
|
|
rbuf[hwid] = 0;
|
2002-02-06 05:48:34 +01:00
|
|
|
|
|
|
|
|
hval = 0;
|
|
|
|
|
for (unsigned idx = 0 ; idx < width ; idx += 1) {
|
|
|
|
|
unsigned bit = memory_get(rfp->mem->mem, bidx+idx);
|
|
|
|
|
hval = hval | (bit << 2*(idx % 4));
|
|
|
|
|
|
|
|
|
|
if (idx%4 == 3) {
|
|
|
|
|
hwid -= 1;
|
2002-07-03 04:09:38 +02:00
|
|
|
rbuf[hwid] = hex_digits[hval];
|
2002-02-06 05:48:34 +01:00
|
|
|
hval = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hwid > 0) {
|
|
|
|
|
unsigned padd = 0;
|
|
|
|
|
|
|
|
|
|
hwid -= 1;
|
2002-07-03 04:09:38 +02:00
|
|
|
rbuf[hwid] = hex_digits[hval];
|
|
|
|
|
switch(rbuf[hwid]) {
|
2002-02-06 05:48:34 +01:00
|
|
|
case 'X': padd = 2; break;
|
|
|
|
|
case 'Z': padd = 3; break;
|
|
|
|
|
}
|
|
|
|
|
if (padd) {
|
|
|
|
|
for (unsigned idx = width % 4; idx < 4; idx += 1) {
|
|
|
|
|
hval = hval | padd << 2*idx;
|
|
|
|
|
}
|
2002-07-03 04:09:38 +02:00
|
|
|
rbuf[hwid] = hex_digits[hval];
|
2002-02-06 05:48:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
2002-07-03 04:09:38 +02:00
|
|
|
vp->value.str = rbuf;
|
2002-02-06 05:48:34 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-11 06:39:35 +02:00
|
|
|
case vpiDecStrVal: {
|
|
|
|
|
unsigned char*bits = new unsigned char[width];
|
|
|
|
|
|
|
|
|
|
for (unsigned idx = 0 ; idx < width ; idx += 1)
|
|
|
|
|
bits[idx] = memory_get(rfp->mem->mem, bidx+idx);
|
|
|
|
|
|
2002-07-09 05:24:37 +02:00
|
|
|
rbuf = need_result_buf(width+1, RBUF_VAL);
|
2002-07-03 04:09:38 +02:00
|
|
|
vpip_bits_to_dec_str(bits, width, rbuf, width+1, false);
|
2002-05-11 06:39:35 +02:00
|
|
|
|
|
|
|
|
delete[]bits;
|
2002-07-03 04:09:38 +02:00
|
|
|
vp->value.str = rbuf;
|
2002-05-11 06:39:35 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 19:52:29 +01:00
|
|
|
case vpiIntVal:
|
2002-07-03 04:09:38 +02:00
|
|
|
assert(width <= 8 * sizeof vp->value.integer);
|
|
|
|
|
|
2001-11-09 04:39:07 +01:00
|
|
|
vp->value.integer = 0;
|
|
|
|
|
for (unsigned idx = 0; idx < width; idx += 1) {
|
|
|
|
|
|
|
|
|
|
unsigned bit = memory_get(rfp->mem->mem, bidx+idx);
|
|
|
|
|
if (bit>1) {
|
|
|
|
|
vp->value.integer = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-07-03 04:09:38 +02:00
|
|
|
|
2001-11-09 04:39:07 +01:00
|
|
|
vp->value.integer |= bit << idx;
|
|
|
|
|
}
|
2002-06-30 06:35:47 +02:00
|
|
|
break;
|
|
|
|
|
|
2002-07-03 04:09:38 +02:00
|
|
|
case vpiVectorVal: {
|
|
|
|
|
unsigned hwid = (width - 1)/32 + 1;
|
|
|
|
|
|
2002-07-09 05:24:37 +02:00
|
|
|
rbuf = need_result_buf(hwid * sizeof(s_vpi_vecval), RBUF_VAL);
|
2002-07-03 04:09:38 +02:00
|
|
|
s_vpi_vecval *op = (p_vpi_vecval)rbuf;
|
2002-06-30 06:35:47 +02:00
|
|
|
vp->value.vector = op;
|
|
|
|
|
|
|
|
|
|
op->aval = op->bval = 0;
|
2002-07-03 04:09:38 +02:00
|
|
|
for (unsigned idx = 0 ; idx < width ; idx += 1) {
|
2002-07-04 01:16:27 +02:00
|
|
|
switch (memory_get(rfp->mem->mem, bidx+idx)) {
|
2002-06-30 06:35:47 +02:00
|
|
|
case 0:
|
|
|
|
|
op->aval &= ~(1 << idx % 32);
|
|
|
|
|
op->bval &= ~(1 << idx % 32);
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
op->aval |= (1 << idx % 32);
|
|
|
|
|
op->bval &= ~(1 << idx % 32);
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
op->aval |= (1 << idx % 32);
|
|
|
|
|
op->bval |= (1 << idx % 32);
|
|
|
|
|
break;
|
2002-07-04 01:16:27 +02:00
|
|
|
case 3:
|
|
|
|
|
op->aval &= ~(1 << idx % 32);
|
|
|
|
|
op->bval |= (1 << idx % 32);
|
|
|
|
|
break;
|
2002-06-30 06:35:47 +02:00
|
|
|
}
|
2002-07-01 17:36:12 +02:00
|
|
|
if (!((idx+1) % 32) && (idx+1 < width)) {
|
2002-06-30 06:35:47 +02:00
|
|
|
op++;
|
|
|
|
|
op->aval = op->bval = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2002-07-03 04:09:38 +02:00
|
|
|
}
|
2001-11-09 04:39:07 +01:00
|
|
|
}
|
2001-05-09 01:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct __vpirt vpip_memory_rt = {
|
|
|
|
|
vpiMemory,
|
|
|
|
|
vpi_memory_get,
|
|
|
|
|
memory_get_str,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
2002-01-31 05:28:17 +01:00
|
|
|
memory_get_handle,
|
2001-05-09 01:59:33 +02:00
|
|
|
memory_iterate,
|
|
|
|
|
memory_index,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct __vpirt vpip_memory_word_rt = {
|
|
|
|
|
vpiMemoryWord,
|
|
|
|
|
memory_word_get,
|
2002-07-03 04:09:38 +02:00
|
|
|
memory_word_get_str,
|
2001-05-09 01:59:33 +02:00
|
|
|
memory_word_get_value,
|
|
|
|
|
memory_word_put,
|
2002-01-31 05:28:17 +01:00
|
|
|
memory_word_get_handle,
|
2001-05-09 01:59:33 +02:00
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
};
|
|
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
static void memory_make_word_handles(struct __vpiMemory*rfp)
|
|
|
|
|
{
|
|
|
|
|
if (rfp->words != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
unsigned word_count = memory_size(rfp->mem);
|
|
|
|
|
unsigned word_offset = memory_root(rfp->mem);
|
|
|
|
|
|
|
|
|
|
rfp->words = (struct __vpiMemoryWord*)
|
|
|
|
|
calloc(word_count, sizeof (struct __vpiMemoryWord));
|
|
|
|
|
|
|
|
|
|
for (unsigned idx = 0 ; idx < word_count ; idx += 1) {
|
|
|
|
|
struct __vpiMemoryWord*cur = rfp->words + idx;
|
|
|
|
|
cur->base.vpi_type = &vpip_memory_word_rt;
|
|
|
|
|
cur->mem = rfp;
|
|
|
|
|
vpip_make_dec_const(&cur->index, idx + word_offset);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-09 01:59:33 +02:00
|
|
|
vpiHandle vpip_make_memory(vvp_memory_t mem)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiMemory*obj = (struct __vpiMemory*)
|
|
|
|
|
malloc(sizeof(struct __vpiMemory));
|
2002-07-05 19:14:15 +02:00
|
|
|
count_vpi_memories += 1;
|
2001-05-09 01:59:33 +02:00
|
|
|
|
|
|
|
|
obj->base.vpi_type = &vpip_memory_rt;
|
2002-05-10 18:00:57 +02:00
|
|
|
obj->scope = vpip_peek_current_scope();
|
2001-05-09 01:59:33 +02:00
|
|
|
obj->mem = mem;
|
2002-05-17 06:12:19 +02:00
|
|
|
vpip_make_dec_const(&obj->left_range, memory_left_range(mem));
|
|
|
|
|
vpip_make_dec_const(&obj->right_range, memory_right_range(mem));
|
|
|
|
|
vpip_make_dec_const(&obj->word_left_range, memory_word_left_range(mem));
|
|
|
|
|
vpip_make_dec_const(&obj->word_right_range,memory_word_right_range(mem));
|
2002-01-31 05:28:17 +01:00
|
|
|
|
2002-05-17 06:12:19 +02:00
|
|
|
obj->words = 0;
|
2001-05-09 01:59:33 +02:00
|
|
|
|
|
|
|
|
return &(obj->base);
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-19 17:57:31 +01:00
|
|
|
void vpip_memory_value_change(struct __vpiCallback*cbh,
|
|
|
|
|
vpiHandle ref)
|
|
|
|
|
{
|
|
|
|
|
struct __vpiMemory*obj = (struct __vpiMemory*)ref;
|
|
|
|
|
cbh->next = obj->mem->cb;
|
|
|
|
|
obj->mem->cb = cbh;
|
|
|
|
|
}
|