Rearrange fileio functions, and add ungetc.

This commit is contained in:
steve 2003-10-30 03:43:19 +00:00
parent e88c3a20a2
commit 02ec36806c
4 changed files with 393 additions and 251 deletions

View File

@ -16,7 +16,7 @@
# 59 Temple Place - Suite 330 # 59 Temple Place - Suite 330
# Boston, MA 02111-1307, USA # Boston, MA 02111-1307, USA
# #
#ident "$Id: Makefile.in,v 1.48 2003/10/10 03:34:15 steve Exp $" #ident "$Id: Makefile.in,v 1.49 2003/10/30 03:43:19 steve Exp $"
# #
# #
SHELL = /bin/sh SHELL = /bin/sh
@ -64,9 +64,9 @@ dep:
$(CC) -Wall -I$(srcdir)/.. -I$(srcdir) -I.. $(CPPFLAGS) $(CFLAGS) -MD -c $< -o $*.o $(CC) -Wall -I$(srcdir)/.. -I$(srcdir) -I.. $(CPPFLAGS) $(CFLAGS) -MD -c $< -o $*.o
mv $*.d dep mv $*.d dep
O = sys_table.o sys_convert.o sys_deposit.o sys_display.o sys_finish.o \ O = sys_table.o sys_convert.o sys_deposit.o sys_display.o sys_fileio.o \
sys_plusargs.o sys_random.o sys_readmem.o sys_readmem_lex.o sys_time.o \ sys_finish.o sys_plusargs.o sys_random.o sys_readmem.o sys_readmem_lex.o \
sys_vcd.o sys_vcdoff.o vcd_priv.o mt19937int.o priv.o stringheap.o sys_time.o sys_vcd.o sys_vcdoff.o vcd_priv.o mt19937int.o priv.o stringheap.o
ifeq (@HAVE_LIBZ@,yes) ifeq (@HAVE_LIBZ@,yes)
O += sys_lxt.o lxt_write.o sys_lxt2.o lxt2_write.o O += sys_lxt.o lxt_write.o sys_lxt2.o lxt2_write.o

View File

@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/ */
#ifdef HAVE_CVS_IDENT #ifdef HAVE_CVS_IDENT
#ident "$Id: sys_display.c,v 1.65 2003/08/26 03:51:05 steve Exp $" #ident "$Id: sys_display.c,v 1.66 2003/10/30 03:43:19 steve Exp $"
#endif #endif
# include "config.h" # include "config.h"
@ -69,7 +69,7 @@ struct strobe_cb_info {
unsigned mcd; unsigned mcd;
}; };
static int is_constant(vpiHandle obj) int is_constant(vpiHandle obj)
{ {
if (vpi_get(vpiType, obj) == vpiConstant) if (vpi_get(vpiType, obj) == vpiConstant)
return vpiConstant; return vpiConstant;
@ -1204,77 +1204,6 @@ static int sys_monitoroff_calltf(char*name)
return 0; return 0;
} }
/*
* Implement the $fopen system function.
*/
static int sys_fopen_calltf(char *name)
{
s_vpi_value value;
unsigned char *mode_string = 0;
vpiHandle call_handle = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, call_handle);
vpiHandle item = argv ? vpi_scan(argv) : 0;
vpiHandle mode = item ? vpi_scan(argv) : 0;
if (item == 0) {
vpi_printf("%s: file name parameter missing.\n", name);
return 0;
}
if (mode == 0) {
argv = 0;
}
if (! is_constant(item)) {
vpi_printf("ERROR: %s parameter must be a constant\n", name);
vpi_free_object(argv);
return 0;
}
if (vpi_get(vpiConstType, item) != vpiStringConst) {
vpi_printf("ERROR: %s parameter must be a string.\n", name);
vpi_free_object(argv);
return 0;
}
if (mode) {
if (! is_constant(mode)) {
vpi_printf("ERROR: %s parameter must be a constant\n", name);
vpi_free_object(argv);
return 0;
}
if (vpi_get(vpiConstType, mode) != vpiStringConst) {
vpi_printf("ERROR: %s parameter must be a string.\n", name);
vpi_free_object(argv);
return 0;
}
value.format = vpiStringVal;
vpi_get_value(mode, &value);
mode_string = strdup(value.value.str);
}
value.format = vpiStringVal;
vpi_get_value(item, &value);
value.format = vpiIntVal;
if (mode) {
value.value.integer = vpi_fopen(value.value.str, mode_string);
free(mode_string);
} else
value.value.integer = vpi_mcd_open(value.value.str);
vpi_put_value(call_handle, &value, 0, vpiNoDelay);
return 0;
}
static int sys_fopen_sizetf(char*x)
{
return 32;
}
/* Implement $fdisplay and $fwrite. /* Implement $fdisplay and $fwrite.
* Perhaps this could be merged into sys_display_calltf. * Perhaps this could be merged into sys_display_calltf.
*/ */
@ -1342,143 +1271,6 @@ static int sys_fdisplay_calltf(char *name)
return 0; return 0;
} }
/*
* Implement $fclose system function
*/
static int sys_fclose_calltf(char *name)
{
unsigned int mcd;
int type;
s_vpi_value value;
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle item = vpi_scan(argv);
if (item == 0) {
vpi_printf("%s: mcd parameter missing.\n", name);
return 0;
}
type = vpi_get(vpiType, item);
switch (type) {
case vpiReg:
case vpiRealVal:
case vpiIntegerVar:
break;
default:
vpi_printf("ERROR: %s mcd parameter must be of integral type",
name);
vpi_printf(", got vpiType=%d\n", type);
vpi_free_object(argv);
return 0;
}
value.format = vpiIntVal;
vpi_get_value(item, &value);
mcd = value.value.integer;
vpi_mcd_close(mcd);
return 0;
}
static int sys_fputc_calltf(char *name)
{
unsigned int mcd;
int type;
unsigned char x;
s_vpi_value value, xvalue;
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle item = vpi_scan(argv);
FILE *fp;
if (item == 0) {
vpi_printf("%s: mcd parameter missing.\n", name);
return 0;
}
type = vpi_get(vpiType, item);
switch (type) {
case vpiReg:
case vpiRealVal:
case vpiIntegerVar:
break;
default:
vpi_printf("ERROR: %s mcd parameter must be of integral", name);
vpi_printf(", got vpiType=%d\n", type);
vpi_free_object(argv);
return 0;
}
value.format = vpiIntVal;
vpi_get_value(item, &value);
mcd = value.value.integer;
if (IS_MCD(mcd)) return EOF;
item = vpi_scan(argv);
xvalue.format = vpiIntVal;
vpi_get_value(item, &xvalue);
x = xvalue.value.integer;
fp = vpi_get_file(mcd);
if (!fp) return EOF;
return fputc(x, fp);
}
static int sys_fgetc_calltf(char *name)
{
unsigned int mcd;
int type;
s_vpi_value value, rval;
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle item = vpi_scan(argv);
FILE *fp;
if (item == 0) {
vpi_printf("%s: mcd parameter missing.\n", name);
return 0;
}
type = vpi_get(vpiType, item);
switch (type) {
case vpiReg:
case vpiRealVal:
case vpiIntegerVar:
break;
default:
vpi_printf("ERROR: %s mcd parameter must be of integral", name);
vpi_printf(", got vpiType=%d\n", type);
vpi_free_object(argv);
return 0;
}
value.format = vpiIntVal;
vpi_get_value(item, &value);
mcd = value.value.integer;
rval.format = vpiIntVal;
fp = vpi_get_file(mcd);
if (!fp || IS_MCD(mcd))
rval.value.integer = EOF;
else
rval.value.integer = fgetc(fp);
vpi_put_value(sys, &rval, 0, vpiNoDelay);
return 0;
}
static int sys_fgetc_sizetf(char*x)
{
return 32;
}
static int sys_timeformat_compiletf(char *xx) static int sys_timeformat_compiletf(char *xx)
{ {
vpiHandle sys = vpi_handle(vpiSysTfCall, 0); vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
@ -1729,24 +1521,6 @@ void sys_display_register()
tf_data.user_data = "$monitoroff"; tf_data.user_data = "$monitoroff";
vpi_register_systf(&tf_data); vpi_register_systf(&tf_data);
//============================== fopen
tf_data.type = vpiSysFunc;
tf_data.tfname = "$fopen";
tf_data.calltf = sys_fopen_calltf;
tf_data.compiletf = 0;
tf_data.sizetf = sys_fopen_sizetf;
tf_data.user_data = "$fopen";
vpi_register_systf(&tf_data);
//============================== fclose
tf_data.type = vpiSysTask;
tf_data.tfname = "$fclose";
tf_data.calltf = sys_fclose_calltf;
tf_data.compiletf = 0;
tf_data.sizetf = 0;
tf_data.user_data = "$fclose";
vpi_register_systf(&tf_data);
//============================== fdisplay //============================== fdisplay
tf_data.type = vpiSysTask; tf_data.type = vpiSysTask;
tf_data.tfname = "$fdisplay"; tf_data.tfname = "$fdisplay";
@ -1787,24 +1561,6 @@ void sys_display_register()
tf_data.compiletf = 0; tf_data.compiletf = 0;
tf_data.sizetf = 0; tf_data.sizetf = 0;
tf_data.user_data = "$fwrite"; tf_data.user_data = "$fwrite";
vpi_register_systf(&tf_data);
//============================== fputc
tf_data.type = vpiSysTask;
tf_data.tfname = "$fputc";
tf_data.calltf = sys_fputc_calltf;
tf_data.compiletf = 0;
tf_data.sizetf = 0;
tf_data.user_data = "$fputc";
vpi_register_systf(&tf_data);
//============================== fgetc
tf_data.type = vpiSysFunc;
tf_data.tfname = "$fgetc";
tf_data.calltf = sys_fgetc_calltf;
tf_data.compiletf = 0;
tf_data.sizetf = sys_fgetc_sizetf;
tf_data.user_data = "$fgetc";
vpi_register_systf(&tf_data); vpi_register_systf(&tf_data);
//============================ timeformat //============================ timeformat
@ -1824,6 +1580,9 @@ void sys_display_register()
/* /*
* $Log: sys_display.c,v $ * $Log: sys_display.c,v $
* Revision 1.66 2003/10/30 03:43:19 steve
* Rearrange fileio functions, and add ungetc.
*
* Revision 1.65 2003/08/26 03:51:05 steve * Revision 1.65 2003/08/26 03:51:05 steve
* Add support for fstrobe system tasks. * Add support for fstrobe system tasks.
* *

378
vpi/sys_fileio.c Normal file
View File

@ -0,0 +1,378 @@
/*
* 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
#ident "$Id: sys_fileio.c,v 1.1 2003/10/30 03:43:20 steve Exp $"
#endif
# include "vpi_user.h"
# include "sys_priv.h"
# include <assert.h>
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
#define IS_MCD(mcd) !((mcd)>>31&1)
/*
* Implement the $fopen system function.
*/
static int sys_fopen_calltf(char *name)
{
s_vpi_value value;
unsigned char *mode_string = 0;
vpiHandle call_handle = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, call_handle);
vpiHandle item = argv ? vpi_scan(argv) : 0;
vpiHandle mode = item ? vpi_scan(argv) : 0;
if (item == 0) {
vpi_printf("%s: file name parameter missing.\n", name);
return 0;
}
if (mode == 0) {
argv = 0;
}
if (! is_constant(item)) {
vpi_printf("ERROR: %s parameter must be a constant\n", name);
vpi_free_object(argv);
return 0;
}
if (vpi_get(vpiConstType, item) != vpiStringConst) {
vpi_printf("ERROR: %s parameter must be a string.\n", name);
vpi_free_object(argv);
return 0;
}
if (mode) {
if (! is_constant(mode)) {
vpi_printf("ERROR: %s parameter must be a constant\n", name);
vpi_free_object(argv);
return 0;
}
if (vpi_get(vpiConstType, mode) != vpiStringConst) {
vpi_printf("ERROR: %s parameter must be a string.\n", name);
vpi_free_object(argv);
return 0;
}
value.format = vpiStringVal;
vpi_get_value(mode, &value);
mode_string = strdup(value.value.str);
}
value.format = vpiStringVal;
vpi_get_value(item, &value);
value.format = vpiIntVal;
if (mode) {
value.value.integer = vpi_fopen(value.value.str, mode_string);
free(mode_string);
} else
value.value.integer = vpi_mcd_open(value.value.str);
vpi_put_value(call_handle, &value, 0, vpiNoDelay);
return 0;
}
static int sys_fopen_sizetf(char*x)
{
return 32;
}
/*
* Implement $fclose system function
*/
static int sys_fclose_calltf(char *name)
{
unsigned int mcd;
int type;
s_vpi_value value;
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle item = vpi_scan(argv);
if (item == 0) {
vpi_printf("%s: mcd parameter missing.\n", name);
return 0;
}
type = vpi_get(vpiType, item);
switch (type) {
case vpiReg:
case vpiRealVal:
case vpiIntegerVar:
break;
default:
vpi_printf("ERROR: %s mcd parameter must be of integral type",
name);
vpi_printf(", got vpiType=%d\n", type);
vpi_free_object(argv);
return 0;
}
value.format = vpiIntVal;
vpi_get_value(item, &value);
mcd = value.value.integer;
vpi_mcd_close(mcd);
return 0;
}
static int sys_fputc_calltf(char *name)
{
unsigned int mcd;
int type;
unsigned char x;
s_vpi_value value, xvalue;
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle item = vpi_scan(argv);
FILE *fp;
if (item == 0) {
vpi_printf("%s: mcd parameter missing.\n", name);
return 0;
}
type = vpi_get(vpiType, item);
switch (type) {
case vpiReg:
case vpiRealVal:
case vpiIntegerVar:
break;
default:
vpi_printf("ERROR: %s mcd parameter must be of integral", name);
vpi_printf(", got vpiType=%d\n", type);
vpi_free_object(argv);
return 0;
}
value.format = vpiIntVal;
vpi_get_value(item, &value);
mcd = value.value.integer;
if (IS_MCD(mcd)) return EOF;
item = vpi_scan(argv);
xvalue.format = vpiIntVal;
vpi_get_value(item, &xvalue);
x = xvalue.value.integer;
fp = vpi_get_file(mcd);
if (!fp) return EOF;
return fputc(x, fp);
}
static int sys_fgetc_calltf(char *name)
{
unsigned int mcd;
int type;
s_vpi_value value, rval;
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle item = vpi_scan(argv);
FILE *fp;
if (item == 0) {
vpi_printf("%s: mcd parameter missing.\n", name);
return 0;
}
type = vpi_get(vpiType, item);
switch (type) {
case vpiReg:
case vpiRealVal:
case vpiIntegerVar:
break;
default:
vpi_printf("ERROR: %s mcd parameter must be of integral", name);
vpi_printf(", got vpiType=%d\n", type);
vpi_free_object(argv);
return 0;
}
value.format = vpiIntVal;
vpi_get_value(item, &value);
mcd = value.value.integer;
rval.format = vpiIntVal;
fp = vpi_get_file(mcd);
if (!fp || IS_MCD(mcd))
rval.value.integer = EOF;
else
rval.value.integer = fgetc(fp);
vpi_put_value(sys, &rval, 0, vpiNoDelay);
return 0;
}
static int sys_fgetc_sizetf(char*x)
{
return 32;
}
static int sys_ungetc_compiletf(char*name)
{
int type;
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle item = vpi_scan(argv);
if (item == 0) {
vpi_printf("%s: mcd parameter missing.\n", name);
return 0;
}
type = vpi_get(vpiType, item);
switch (type) {
case vpiReg:
case vpiRealVal:
case vpiIntegerVar:
break;
default:
vpi_printf("ERROR: %s mcd parameter must be of integral", name);
vpi_printf(", got vpiType=%d\n", type);
vpi_free_object(argv);
return 0;
}
return 0;
}
static int sys_ungetc_calltf(char *name)
{
unsigned int mcd;
unsigned char x;
s_vpi_value value, xvalue, rval;
vpiHandle sys = vpi_handle(vpiSysTfCall, 0);
vpiHandle argv = vpi_iterate(vpiArgument, sys);
vpiHandle item = vpi_scan(argv);
FILE *fp;
rval.format = vpiIntVal;
assert(item);
value.format = vpiIntVal;
vpi_get_value(item, &value);
mcd = value.value.integer;
if (IS_MCD(mcd)) {
rval.value.integer = EOF;
vpi_put_value(sys, &rval, 0, vpiNoDelay);
return 0;
}
item = vpi_scan(argv);
xvalue.format = vpiIntVal;
vpi_get_value(item, &xvalue);
x = xvalue.value.integer;
fp = vpi_get_file(mcd);
if ( !fp ) {
rval.value.integer = EOF;
vpi_put_value(sys, &rval, 0, vpiNoDelay);
return 0;
}
ungetc(x, fp);
rval.value.integer = 0;
vpi_put_value(sys, &rval, 0, vpiNoDelay);
return 0;
}
static int sys_ungetc_sizetf(char*x)
{
return 32;
}
void sys_fileio_register()
{
s_vpi_systf_data tf_data;
//============================== fopen
tf_data.type = vpiSysFunc;
tf_data.tfname = "$fopen";
tf_data.calltf = sys_fopen_calltf;
tf_data.compiletf = 0;
tf_data.sizetf = sys_fopen_sizetf;
tf_data.user_data = "$fopen";
vpi_register_systf(&tf_data);
//============================== fclose
tf_data.type = vpiSysTask;
tf_data.tfname = "$fclose";
tf_data.calltf = sys_fclose_calltf;
tf_data.compiletf = 0;
tf_data.sizetf = 0;
tf_data.user_data = "$fclose";
vpi_register_systf(&tf_data);
//============================== fputc
tf_data.type = vpiSysTask;
tf_data.tfname = "$fputc";
tf_data.calltf = sys_fputc_calltf;
tf_data.compiletf = 0;
tf_data.sizetf = 0;
tf_data.user_data = "$fputc";
vpi_register_systf(&tf_data);
//============================== fgetc
tf_data.type = vpiSysFunc;
tf_data.tfname = "$fgetc";
tf_data.calltf = sys_fgetc_calltf;
tf_data.compiletf = 0;
tf_data.sizetf = sys_fgetc_sizetf;
tf_data.user_data = "$fgetc";
vpi_register_systf(&tf_data);
//============================== ungetc
tf_data.type = vpiSysFunc;
tf_data.tfname = "$ungetc";
tf_data.calltf = sys_ungetc_calltf;
tf_data.compiletf = sys_ungetc_compiletf;
tf_data.sizetf = sys_ungetc_sizetf;
tf_data.user_data = "$ungetc";
vpi_register_systf(&tf_data);
}
/*
* $Log: sys_fileio.c,v $
* Revision 1.1 2003/10/30 03:43:20 steve
* Rearrange fileio functions, and add ungetc.
*
*/

View File

@ -19,7 +19,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/ */
#ifdef HAVE_CVS_IDENT #ifdef HAVE_CVS_IDENT
#ident "$Id: sys_priv.h,v 1.3 2003/09/30 01:33:39 steve Exp $" #ident "$Id: sys_priv.h,v 1.4 2003/10/30 03:43:20 steve Exp $"
#endif #endif
# include "vpi_user.h" # include "vpi_user.h"
@ -42,10 +42,15 @@ extern void sgenrand(struct context_s *context, unsigned long seed);
extern unsigned long genrand(struct context_s *context); extern unsigned long genrand(struct context_s *context);
extern int is_constant(vpiHandle obj);
extern PLI_UINT64 timerec_to_time64(const struct t_vpi_time*time); extern PLI_UINT64 timerec_to_time64(const struct t_vpi_time*time);
/* /*
* $Log: sys_priv.h,v $ * $Log: sys_priv.h,v $
* Revision 1.4 2003/10/30 03:43:20 steve
* Rearrange fileio functions, and add ungetc.
*
* Revision 1.3 2003/09/30 01:33:39 steve * Revision 1.3 2003/09/30 01:33:39 steve
* dumpers must be aware of 64bit time. * dumpers must be aware of 64bit time.
* *