/* * ignore the user data field */ int vpi_fopen(PLI_BYTE8 *pargs) { unsigned int mcd; vpiHandle href, iter, aref; struct t_vpi_value valrec; s_vpi_value tmpval; valrec.format = vpiIntVal; valrec.value.integer = (int) 0; href = vpi_handle(vpiSysTfCall, NULL); /* if error after here will return 0 */ vpi_put_value(href, &valrec, NULL, vpiNoDelay); if ((iter = vpi_iterate(vpiArgument, href)) == NULL) return(0); if (vpi_get(vpiSize, iter) != 1) return(0); if ((aref = vpi_scan(iter)) == NULL) return(0); tmpval.format = vpiStringVal; /* evaluate expression */ vpi_get_value(aref, &tmpval); mcd = vpi_mcd_open(tmpval.value.str); if (mcd != 0) valrec.value.integer = (int) mcd; vpi_put_value(href, &valrec, NULL, vpiNoDelay); return(0); }
int _mon_check_mcd() { PLI_INT32 status; PLI_UINT32 mcd; PLI_BYTE8* filename = (PLI_BYTE8*)"obj_dir/t_vpi_var/mcd_open.tmp"; mcd = vpi_mcd_open(filename); CHECK_RESULT_NZ(mcd); { // Check it got written FILE* fp = fopen(filename,"r"); CHECK_RESULT_NZ(fp); fclose(fp); } status = vpi_mcd_printf(mcd, (PLI_BYTE8*)"hello %s", "vpi_mcd_printf"); CHECK_RESULT(status, strlen("hello vpi_mcd_printf")); status = vpi_mcd_flush(mcd); CHECK_RESULT(status, 0); status = vpi_mcd_close(mcd); CHECK_RESULT(status, 0); status = vpi_flush(); CHECK_RESULT(status, 0); return 0; }
static PLI_INT32 sys_fopen_calltf(PLI_BYTE8*name) { vpiHandle callh = vpi_handle(vpiSysTfCall, 0); vpiHandle argv = vpi_iterate(vpiArgument, callh); s_vpi_value val; int fail = 0; char *mode_string = 0; unsigned idx; vpiHandle item = vpi_scan(argv); vpiHandle mode = vpi_scan(argv); unsigned len; /* Get the mode handle if it exists. */ if (mode) { val.format = vpiStringVal; vpi_get_value(mode, &val); /* Verify that we have a string and that it is not NULL. */ if (val.format != vpiStringVal || !*(val.value.str)) { vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh)); vpi_printf("%s's mode argument is not a valid string.\n", name); fail = 1; } /* Make sure the mode string is correct. */ if (strlen(val.value.str) > 3) { vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh)); vpi_printf("%s's mode argument (%s) is too long.\n", name, val.value.str); fail = 1; } else { unsigned bin = 0, plus = 0; switch (val.value.str[0]) { case 'r': case 'w': case 'a': for (idx = 1; idx < 3 ; idx++) { if (val.value.str[idx] == '\0') break; switch (val.value.str[idx]) { case 'b': if (bin) fail = 1; bin = 1; break; case '+': if (plus) fail = 1; plus = 1; break; default: fail = 1; break; } } if (! fail) break; default: vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh)); vpi_printf("%s's mode argument (%s) is invalid.\n", name, val.value.str); fail = 1; break; } } mode_string = strdup(val.value.str); vpi_free_object(argv); } /* Get the string form of the file name from the file name argument. */ val.format = vpiStringVal; vpi_get_value(item, &val); /* Verify that we have a string and that it is not NULL. */ if (val.format != vpiStringVal || !*(val.value.str)) { vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh)); vpi_printf("%s's file name argument is not a valid string.\n", name); fail = 1; if (mode) free(mode_string); } /* * Verify that the file name is composed of only printable * characters. */ len = strlen(val.value.str); for (idx = 0; idx < len; idx++) { if (! isprint(val.value.str[idx])) { char msg [64]; snprintf(msg, 64, "WARNING: %s:%d:", vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh)); vpi_printf("%s %s's file name argument contains non-" "printable characters.\n", msg, name); vpi_printf("%*s \"%s\"\n", (int) strlen(msg), " ", val.value.str); fail = 1; if (mode) free(mode_string); } } /* If either the mode or file name are not valid just return. */ if (fail) return 0; val.format = vpiIntVal; if (mode) { val.value.integer = vpi_fopen(val.value.str, mode_string); free(mode_string); } else val.value.integer = vpi_mcd_open(val.value.str); vpi_put_value(callh, &val, 0, vpiNoDelay); 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 PLI_INT32 sys_fopen_calltf(PLI_BYTE8*name) { vpiHandle callh = vpi_handle(vpiSysTfCall, 0); vpiHandle argv = vpi_iterate(vpiArgument, callh); s_vpi_value val; int fail = 0; char *mode_string = 0; vpiHandle fileh = vpi_scan(argv); char *fname; vpiHandle mode = vpi_scan(argv); errno = 0; /* Get the mode handle if it exists. */ if (mode) { char *esc_md; val.format = vpiStringVal; vpi_get_value(mode, &val); /* Verify that we have a string and that it is not NULL. */ if (val.format != vpiStringVal || !*(val.value.str)) { vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh)); vpi_printf("%s's mode argument is not a valid string.\n", name); fail = 1; } /* Make sure the mode string is correct. */ if (strlen(val.value.str) > 3) { vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh)); esc_md = as_escaped(val.value.str); vpi_printf("%s's mode argument (%s) is too long.\n", name, esc_md); free(esc_md); fail = 1; } else { unsigned bin = 0, plus = 0, idx; switch (val.value.str[0]) { case 'r': case 'w': case 'a': for (idx = 1; idx < 3 ; idx++) { if (val.value.str[idx] == '\0') break; switch (val.value.str[idx]) { case 'b': if (bin) fail = 1; bin = 1; break; case '+': if (plus) fail = 1; plus = 1; break; default: fail = 1; break; } } if (! fail) break; default: vpi_printf("WARNING: %s:%d: ", vpi_get_str(vpiFile, callh), (int)vpi_get(vpiLineNo, callh)); esc_md = as_escaped(val.value.str); vpi_printf("%s's mode argument (%s) is invalid.\n", name, esc_md); free(esc_md); fail = 1; break; } } mode_string = strdup(val.value.str); vpi_free_object(argv); } fname = get_filename(callh, name, fileh); /* If either the mode or file name are not valid just return. */ if (fail || fname == 0) { free(fname); if (mode) free(mode_string); return 0; } val.format = vpiIntVal; if (mode) { val.value.integer = vpi_fopen(fname, mode_string); free(mode_string); } else val.value.integer = vpi_mcd_open(fname); vpi_put_value(callh, &val, 0, vpiNoDelay); free(fname); return 0; }