2008-07-31 23:05:27 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 Cary R. (cygcary@yahoo.com)
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it 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 <assert.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <vpi_user.h>
|
|
|
|
|
#include "sys_priv.h"
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Check that the function is called with the correct argument.
|
|
|
|
|
*/
|
|
|
|
|
static PLI_INT32 sys_clog2_compiletf(PLI_BYTE8 *name)
|
|
|
|
|
{
|
|
|
|
|
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
|
|
|
|
|
assert(callh != 0);
|
|
|
|
|
vpiHandle argv = vpi_iterate(vpiArgument, callh);
|
|
|
|
|
vpiHandle arg;
|
|
|
|
|
(void) name; // Not used!
|
|
|
|
|
|
|
|
|
|
/* We must have an argument. */
|
|
|
|
|
if (argv == 0) {
|
|
|
|
|
vpi_printf("ERROR: %s line %d: ", vpi_get_str(vpiFile, callh),
|
|
|
|
|
(int)vpi_get(vpiLineNo, callh));
|
|
|
|
|
vpi_printf("$clog2 requires one numeric argument.\n");
|
|
|
|
|
vpi_control(vpiFinish, 1);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The argument must be numeric. */
|
|
|
|
|
arg = vpi_scan(argv);
|
|
|
|
|
if (! is_numeric_obj(arg)) {
|
|
|
|
|
vpi_printf("ERROR: %s line %d: ", vpi_get_str(vpiFile, callh),
|
|
|
|
|
(int)vpi_get(vpiLineNo, callh));
|
|
|
|
|
vpi_printf("The first argument to $clog2 must be numeric.\n");
|
|
|
|
|
vpi_control(vpiFinish, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We can have a maximum of one argument. */
|
|
|
|
|
if (vpi_scan(argv) != 0) {
|
|
|
|
|
char msg [64];
|
|
|
|
|
snprintf(msg, 64, "ERROR: %s line %d:",
|
|
|
|
|
vpi_get_str(vpiFile, callh),
|
|
|
|
|
(int)vpi_get(vpiLineNo, callh));
|
|
|
|
|
|
|
|
|
|
unsigned argc = 1;
|
|
|
|
|
while (vpi_scan(argv)) argc += 1;
|
|
|
|
|
|
|
|
|
|
vpi_printf("%s $clog2 takes at most one argument.\n", msg);
|
|
|
|
|
vpi_printf("%*s Found %u extra argument%s.\n",
|
2008-08-26 18:44:44 +02:00
|
|
|
(int) strlen(msg), " ", argc, argc == 1 ? "" : "s");
|
2008-07-31 23:05:27 +02:00
|
|
|
vpi_control(vpiFinish, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PLI_INT32 sys_clog2_calltf(PLI_BYTE8 *name)
|
|
|
|
|
{
|
|
|
|
|
vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
|
|
|
|
|
vpiHandle argv = vpi_iterate(vpiArgument, callh);
|
|
|
|
|
vpiHandle arg;
|
|
|
|
|
s_vpi_value val;
|
2008-08-16 04:28:11 +02:00
|
|
|
s_vpi_vecval vec;
|
2008-07-31 23:05:27 +02:00
|
|
|
(void) name; // Not used!/
|
|
|
|
|
|
|
|
|
|
/* Get the argument. */
|
|
|
|
|
arg = vpi_scan(argv);
|
|
|
|
|
vpi_free_object(argv);
|
|
|
|
|
|
2008-08-16 04:28:11 +02:00
|
|
|
vec = vpip_calc_clog2(arg);
|
2008-07-31 23:05:27 +02:00
|
|
|
|
2008-08-16 04:28:11 +02:00
|
|
|
val.format = vpiVectorVal;
|
|
|
|
|
val.value.vector = &vec;
|
2008-07-31 23:05:27 +02:00
|
|
|
vpi_put_value(callh, &val, 0, vpiNoDelay);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Register the function with Verilog.
|
|
|
|
|
*/
|
|
|
|
|
void sys_clog2_register(void)
|
|
|
|
|
{
|
|
|
|
|
s_vpi_systf_data tf_data;
|
|
|
|
|
|
|
|
|
|
tf_data.type = vpiSysFunc;
|
|
|
|
|
tf_data.sysfunctype = vpiIntFunc;
|
|
|
|
|
tf_data.calltf = sys_clog2_calltf;
|
|
|
|
|
tf_data.compiletf = sys_clog2_compiletf;
|
|
|
|
|
tf_data.sizetf = 0;
|
|
|
|
|
tf_data.tfname = "$clog2";
|
|
|
|
|
tf_data.user_data = 0;
|
|
|
|
|
vpi_register_systf(&tf_data);
|
|
|
|
|
}
|