2001-03-16 02:44:34 +01:00
|
|
|
/*
|
2012-08-31 04:11:25 +02:00
|
|
|
* Copyright (c) 2000-2012 Stephen G. Tell <steve@telltronics.org>
|
2001-03-16 02:44:34 +01: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
|
2012-08-29 03:41:23 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2001-03-16 02:44:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
# include "vpi_priv.h"
|
2009-01-30 02:23:09 +01:00
|
|
|
# include "config.h"
|
|
|
|
|
#ifdef CHECK_WITH_VALGRIND
|
2009-01-24 01:04:44 +01:00
|
|
|
# include "vvp_cleanup.h"
|
2009-01-30 02:23:09 +01:00
|
|
|
#endif
|
2010-05-31 22:12:06 +02:00
|
|
|
# include <cassert>
|
|
|
|
|
# include <cstdarg>
|
|
|
|
|
# include <cstdio>
|
|
|
|
|
# include <cstdlib>
|
|
|
|
|
# include <cstring>
|
2010-10-24 00:52:56 +02:00
|
|
|
# include "ivl_alloc.h"
|
2001-03-16 02:44:34 +01:00
|
|
|
|
2003-07-21 03:20:59 +02:00
|
|
|
extern FILE* vpi_trace;
|
|
|
|
|
|
2003-05-15 18:51:08 +02:00
|
|
|
/*
|
|
|
|
|
* This table keeps track of the MCD files. Note that there may be
|
|
|
|
|
* only 31 such files, and mcd bit0 (32'h00_00_00_01) is the special
|
|
|
|
|
* standard output file, which may be replicated to a logfile
|
|
|
|
|
* depending on flags to the command line.
|
|
|
|
|
*/
|
2003-05-23 06:04:02 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* MCD/FD manipulation macros
|
|
|
|
|
*/
|
|
|
|
|
#define IS_MCD(mcd) !((mcd)>>31&1)
|
|
|
|
|
#define FD_IDX(fd) ((fd)&~(1U<<31))
|
2009-06-05 01:12:08 +02:00
|
|
|
#define FD_INCR 32
|
2003-05-23 06:04:02 +02:00
|
|
|
|
2009-06-05 01:12:08 +02:00
|
|
|
typedef struct mcd_entry {
|
2001-03-16 02:44:34 +01:00
|
|
|
FILE *fp;
|
|
|
|
|
char *filename;
|
2009-06-05 01:12:08 +02:00
|
|
|
} mcd_entry_s;
|
|
|
|
|
static mcd_entry_s mcd_table[31];
|
|
|
|
|
static mcd_entry_s *fd_table = NULL;
|
|
|
|
|
static unsigned fd_table_len = 0;
|
2001-03-16 02:44:34 +01:00
|
|
|
|
2003-05-15 18:51:08 +02:00
|
|
|
static FILE* logfile;
|
2001-03-16 02:44:34 +01:00
|
|
|
|
|
|
|
|
/* Initialize mcd portion of vpi. Must be called before
|
|
|
|
|
* any vpi_mcd routines can be used.
|
|
|
|
|
*/
|
2009-06-05 01:12:08 +02:00
|
|
|
void vpip_mcd_init(FILE *log)
|
2001-03-16 02:44:34 +01:00
|
|
|
{
|
2009-06-05 01:12:08 +02:00
|
|
|
fd_table_len = FD_INCR;
|
|
|
|
|
fd_table = (mcd_entry_s *) malloc(fd_table_len*sizeof(mcd_entry_s));
|
|
|
|
|
for (unsigned idx = 0; idx < fd_table_len; idx += 1) {
|
|
|
|
|
fd_table[idx].fp = NULL;
|
|
|
|
|
fd_table[idx].filename = NULL;
|
|
|
|
|
}
|
2010-10-02 20:02:27 +02:00
|
|
|
|
2009-06-05 01:12:08 +02:00
|
|
|
mcd_table[0].fp = stdout;
|
|
|
|
|
mcd_table[0].filename = strdup("stdout");
|
|
|
|
|
|
|
|
|
|
fd_table[0].fp = stdin;
|
|
|
|
|
fd_table[0].filename = strdup("stdin");
|
|
|
|
|
fd_table[1].fp = stdout;
|
|
|
|
|
fd_table[1].filename = strdup("stdout");
|
|
|
|
|
fd_table[2].fp = stderr;
|
|
|
|
|
fd_table[2].filename = strdup("stderr");
|
|
|
|
|
|
|
|
|
|
logfile = log;
|
2001-03-16 02:44:34 +01:00
|
|
|
}
|
|
|
|
|
|
2009-01-30 02:23:09 +01:00
|
|
|
#ifdef CHECK_WITH_VALGRIND
|
2009-01-24 01:04:44 +01:00
|
|
|
void vpi_mcd_delete(void)
|
|
|
|
|
{
|
|
|
|
|
free(mcd_table[0].filename);
|
2009-06-05 01:12:08 +02:00
|
|
|
mcd_table[0].filename = NULL;
|
|
|
|
|
mcd_table[0].fp = NULL;
|
2009-01-24 01:04:44 +01:00
|
|
|
|
|
|
|
|
free(fd_table[0].filename);
|
2009-06-05 01:12:08 +02:00
|
|
|
fd_table[0].filename = NULL;
|
|
|
|
|
fd_table[0].fp = NULL;
|
2009-01-24 01:04:44 +01:00
|
|
|
|
|
|
|
|
free(fd_table[1].filename);
|
2009-06-05 01:12:08 +02:00
|
|
|
fd_table[1].filename = NULL;
|
|
|
|
|
fd_table[1].fp = NULL;
|
2009-01-24 01:04:44 +01:00
|
|
|
|
|
|
|
|
free(fd_table[2].filename);
|
2009-06-05 01:12:08 +02:00
|
|
|
fd_table[2].filename = NULL;
|
|
|
|
|
fd_table[2].fp = NULL;
|
|
|
|
|
|
|
|
|
|
free(fd_table);
|
|
|
|
|
fd_table = NULL;
|
|
|
|
|
fd_table_len = 0;
|
2009-01-24 01:04:44 +01:00
|
|
|
}
|
2009-01-30 02:23:09 +01:00
|
|
|
#endif
|
2009-01-24 01:04:44 +01:00
|
|
|
|
2001-03-16 02:44:34 +01:00
|
|
|
/*
|
|
|
|
|
* close one or more channels. we silently refuse to close the preopened ones.
|
|
|
|
|
*/
|
2003-05-23 06:04:02 +02:00
|
|
|
extern "C" PLI_UINT32 vpi_mcd_close(PLI_UINT32 mcd)
|
2001-03-16 02:44:34 +01:00
|
|
|
{
|
2003-05-23 06:04:02 +02:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
|
|
if (IS_MCD(mcd)) {
|
|
|
|
|
for(int i = 1; i < 31; i++) {
|
|
|
|
|
if(((mcd>>i) & 1) && mcd_table[i].fp) {
|
2004-10-04 03:10:51 +02:00
|
|
|
if(fclose(mcd_table[i].fp)) rc |= 1<<i;
|
2003-05-23 06:04:02 +02:00
|
|
|
free(mcd_table[i].filename);
|
|
|
|
|
mcd_table[i].fp = NULL;
|
|
|
|
|
mcd_table[i].filename = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
rc |= 1<<i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
unsigned idx = FD_IDX(mcd);
|
2009-06-05 01:12:08 +02:00
|
|
|
if (idx > 2 && idx < fd_table_len && fd_table[idx].fp) {
|
2003-05-23 06:04:02 +02:00
|
|
|
rc = fclose(fd_table[idx].fp);
|
|
|
|
|
free(fd_table[idx].filename);
|
|
|
|
|
fd_table[idx].fp = NULL;
|
|
|
|
|
fd_table[idx].filename = NULL;
|
2001-03-16 02:44:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-23 06:04:02 +02:00
|
|
|
extern "C" char *vpi_mcd_name(PLI_UINT32 mcd)
|
2001-03-16 02:44:34 +01:00
|
|
|
{
|
2003-05-23 06:04:02 +02:00
|
|
|
if (IS_MCD(mcd)) {
|
|
|
|
|
for(int i = 0; i < 31; i++) {
|
|
|
|
|
if((mcd>>i) & 1)
|
|
|
|
|
return mcd_table[i].filename;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
unsigned idx = FD_IDX(mcd);
|
2009-06-05 01:12:08 +02:00
|
|
|
if (idx < fd_table_len)
|
2003-05-23 06:04:02 +02:00
|
|
|
return fd_table[idx].filename;
|
2001-03-16 02:44:34 +01:00
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-23 06:04:02 +02:00
|
|
|
extern "C" PLI_UINT32 vpi_mcd_open(char *name)
|
2001-03-16 02:44:34 +01:00
|
|
|
{
|
|
|
|
|
int i;
|
2003-05-23 06:04:02 +02:00
|
|
|
|
2001-03-16 02:44:34 +01:00
|
|
|
for(i = 0; i < 31; i++) {
|
|
|
|
|
if(mcd_table[i].filename == NULL)
|
|
|
|
|
goto got_entry;
|
|
|
|
|
}
|
|
|
|
|
return 0; /* too many open mcd's */
|
|
|
|
|
|
|
|
|
|
got_entry:
|
2015-07-10 18:50:40 +02:00
|
|
|
#if defined(__GNUC__)
|
2003-05-23 06:04:02 +02:00
|
|
|
mcd_table[i].fp = fopen(name, "w");
|
2015-07-10 18:50:40 +02:00
|
|
|
#else
|
|
|
|
|
if (strcmp(name, "/dev/null") != 0)
|
|
|
|
|
mcd_table[i].fp = fopen(name, "w");
|
|
|
|
|
else
|
|
|
|
|
mcd_table[i].fp = fopen("nul", "w");
|
|
|
|
|
#endif
|
2001-03-16 02:44:34 +01:00
|
|
|
if(mcd_table[i].fp == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
mcd_table[i].filename = strdup(name);
|
2003-07-21 03:20:59 +02:00
|
|
|
|
|
|
|
|
if (vpi_trace) {
|
|
|
|
|
fprintf(vpi_trace, "vpi_mcd_open(%s) --> 0x%08x\n",
|
|
|
|
|
name, 1 << i);
|
|
|
|
|
}
|
|
|
|
|
|
2001-03-16 02:44:34 +01:00
|
|
|
return 1<<i;
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-15 18:51:08 +02:00
|
|
|
extern "C" PLI_INT32
|
2003-05-23 06:04:02 +02:00
|
|
|
vpi_mcd_vprintf(PLI_UINT32 mcd, const char*fmt, va_list ap)
|
2001-03-16 02:44:34 +01:00
|
|
|
{
|
2003-07-21 03:20:59 +02:00
|
|
|
char buffer[4096];
|
2012-08-31 04:11:25 +02:00
|
|
|
char *buf_ptr = buffer;
|
2003-07-21 03:20:59 +02:00
|
|
|
int rc = 0;
|
2012-08-31 04:11:25 +02:00
|
|
|
bool need_free = false;
|
2012-08-31 18:20:31 +02:00
|
|
|
va_list saved_ap;
|
2001-03-16 02:44:34 +01:00
|
|
|
|
2003-07-21 03:20:59 +02:00
|
|
|
if (!IS_MCD(mcd)) return 0;
|
|
|
|
|
|
|
|
|
|
if (vpi_trace) {
|
|
|
|
|
fprintf(vpi_trace, "vpi_mcd_vprintf(0x%08x, %s, ...);\n",
|
2009-12-10 21:53:58 +01:00
|
|
|
(unsigned int)mcd, fmt);
|
2003-07-21 03:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
2012-08-31 19:56:47 +02:00
|
|
|
va_copy(saved_ap, ap);
|
2003-07-21 03:20:59 +02:00
|
|
|
rc = vsnprintf(buffer, sizeof buffer, fmt, ap);
|
2012-08-31 04:11:25 +02:00
|
|
|
assert(rc >= 0);
|
|
|
|
|
/*
|
|
|
|
|
* If rc is greater than sizeof buffer then the result was truncated
|
|
|
|
|
* so the print needs to be redone with a larger buffer (very rare).
|
|
|
|
|
*/
|
|
|
|
|
if ((unsigned) rc >= sizeof buffer) {
|
|
|
|
|
buf_ptr = (char *)malloc(rc + 1);
|
|
|
|
|
need_free = true;
|
2012-08-31 18:20:31 +02:00
|
|
|
rc = vsnprintf(buf_ptr, rc+1, fmt, saved_ap);
|
2012-08-31 04:11:25 +02:00
|
|
|
}
|
2012-08-31 18:20:31 +02:00
|
|
|
va_end(saved_ap);
|
2003-07-21 03:20:59 +02:00
|
|
|
|
|
|
|
|
for(int i = 0; i < 31; i++) {
|
|
|
|
|
if((mcd>>i) & 1) {
|
|
|
|
|
if(mcd_table[i].fp) {
|
|
|
|
|
// echo to logfile
|
|
|
|
|
if (i == 0 && logfile)
|
2012-08-31 04:11:25 +02:00
|
|
|
fputs(buf_ptr, logfile);
|
|
|
|
|
fputs(buf_ptr, mcd_table[i].fp);
|
2003-07-21 03:20:59 +02:00
|
|
|
} else {
|
|
|
|
|
rc = EOF;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-31 04:11:25 +02:00
|
|
|
if (need_free) free(buf_ptr);
|
2003-07-21 03:20:59 +02:00
|
|
|
|
|
|
|
|
return rc;
|
2001-03-16 02:44:34 +01:00
|
|
|
}
|
|
|
|
|
|
2003-05-23 06:04:02 +02:00
|
|
|
extern "C" PLI_INT32 vpi_mcd_printf(PLI_UINT32 mcd, const char *fmt, ...)
|
2002-01-04 03:26:36 +01:00
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
int r = vpi_mcd_vprintf(mcd,fmt,ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-03 19:00:35 +02:00
|
|
|
extern "C" void vpip_mcd_rawwrite(PLI_UINT32 mcd, const char*buf, size_t cnt)
|
|
|
|
|
{
|
|
|
|
|
if (!IS_MCD(mcd)) return;
|
|
|
|
|
|
|
|
|
|
for(int idx = 0; idx < 31; idx += 1) {
|
|
|
|
|
if (((mcd>>idx) & 1) == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (mcd_table[idx].fp == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
fwrite(buf, 1, cnt, mcd_table[idx].fp);
|
|
|
|
|
if (idx == 0 && logfile)
|
2015-06-03 23:32:04 +02:00
|
|
|
fwrite(buf, 1, cnt, logfile);
|
2015-06-03 19:00:35 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-23 06:04:02 +02:00
|
|
|
extern "C" PLI_INT32 vpi_mcd_flush(PLI_UINT32 mcd)
|
2003-05-15 18:51:08 +02:00
|
|
|
{
|
2003-05-23 06:04:02 +02:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
|
|
if (IS_MCD(mcd)) {
|
|
|
|
|
for(int i = 0; i < 31; i++) {
|
|
|
|
|
if((mcd>>i) & 1) {
|
|
|
|
|
if (i == 0 && logfile) fflush(logfile);
|
|
|
|
|
if (fflush(mcd_table[i].fp)) rc |= 1<<i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
unsigned idx = FD_IDX(mcd);
|
2009-06-05 01:12:08 +02:00
|
|
|
if (idx < fd_table_len) rc = fflush(fd_table[idx].fp);
|
2003-05-15 18:51:08 +02:00
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2003-05-23 06:04:02 +02:00
|
|
|
/*
|
|
|
|
|
* MCD/FD Extensions
|
|
|
|
|
*/
|
2006-08-03 07:05:31 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The vpi_fopen function opens a file with the given path, and
|
|
|
|
|
* returns a file descriptor that includes bit 31 set. This is to
|
2008-01-29 21:19:59 +01:00
|
|
|
* differentiate the fd from a mcd descriptor. Note that these
|
2006-08-03 07:05:31 +02:00
|
|
|
* descriptors are distinct from the mcd descriptors, so uses a
|
|
|
|
|
* different fd table.
|
|
|
|
|
*/
|
2003-05-23 06:04:02 +02:00
|
|
|
extern "C" PLI_INT32 vpi_fopen(const char*name, const char*mode)
|
2001-03-22 03:23:17 +01:00
|
|
|
{
|
2009-06-05 01:12:08 +02:00
|
|
|
unsigned i;
|
|
|
|
|
for (i = 0; i < fd_table_len; i += 1) {
|
|
|
|
|
if (fd_table[i].filename == NULL) goto got_entry;
|
|
|
|
|
}
|
|
|
|
|
/* We need to allocate more table entries, but to keep things */
|
|
|
|
|
/* sane we'll hard limit this to 1024 file descriptors total. */
|
2009-07-31 02:51:09 +02:00
|
|
|
if (fd_table_len >= 1024) {
|
|
|
|
|
vpi_printf("WARNING: Icarus only supports 1024 open files!\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2009-06-05 01:12:08 +02:00
|
|
|
fd_table_len += FD_INCR;
|
|
|
|
|
fd_table = (mcd_entry_s *) realloc(fd_table,
|
|
|
|
|
fd_table_len*sizeof(mcd_entry_s));
|
|
|
|
|
for (unsigned idx = i; idx < fd_table_len; idx += 1) {
|
|
|
|
|
fd_table[idx].fp = NULL;
|
|
|
|
|
fd_table[idx].filename = NULL;
|
|
|
|
|
}
|
2003-05-23 06:04:02 +02:00
|
|
|
|
|
|
|
|
got_entry:
|
2015-07-10 18:50:40 +02:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
|
fd_table[i].fp = fopen(name, mode);
|
|
|
|
|
#else // Changed for MSVC++ so vpi/pr723.v will pass.
|
|
|
|
|
if(strcmp(name, "/dev/null") != 0)
|
|
|
|
|
fd_table[i].fp = fopen(name, mode);
|
|
|
|
|
else
|
|
|
|
|
fd_table[i].fp = fopen("nul", mode);
|
|
|
|
|
#endif
|
2009-06-05 01:12:08 +02:00
|
|
|
if (fd_table[i].fp == NULL) return 0;
|
|
|
|
|
fd_table[i].filename = strdup(name);
|
|
|
|
|
return ((1U<<31)|i);
|
2001-03-22 03:23:17 +01:00
|
|
|
}
|
|
|
|
|
|
2003-05-23 06:04:02 +02:00
|
|
|
extern "C" FILE *vpi_get_file(PLI_INT32 fd)
|
2001-03-22 03:23:17 +01:00
|
|
|
{
|
2003-05-23 06:04:02 +02:00
|
|
|
// Only deal with FD's
|
2009-06-05 01:12:08 +02:00
|
|
|
if (IS_MCD(fd)) return NULL;
|
2001-03-22 03:23:17 +01:00
|
|
|
|
2009-06-05 01:12:08 +02:00
|
|
|
// Only know about fd_table_len indices
|
|
|
|
|
if (FD_IDX(fd) >= fd_table_len) return NULL;
|
2003-05-23 06:04:02 +02:00
|
|
|
|
2009-06-05 01:12:08 +02:00
|
|
|
return fd_table[FD_IDX(fd)].fp;
|
2001-03-22 03:23:17 +01:00
|
|
|
}
|