2002-03-18 01:19:34 +01:00
|
|
|
/*
|
2010-05-31 22:12:06 +02:00
|
|
|
* Copyright (c) 2002-2010 Stephen Williams (steve@icarus.com)
|
2002-03-18 01:19: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
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
|
|
|
|
|
2008-10-28 18:52:39 +01:00
|
|
|
# include "vvp_net.h"
|
2002-03-18 01:19:34 +01:00
|
|
|
# include "compile.h"
|
|
|
|
|
# include "symbols.h"
|
|
|
|
|
# include "codes.h"
|
|
|
|
|
# include "ufunc.h"
|
2009-04-16 04:08:37 +02:00
|
|
|
# include "vvp_net_sig.h"
|
2002-03-18 01:19:34 +01:00
|
|
|
# include "vthread.h"
|
|
|
|
|
# include "schedule.h"
|
2009-10-14 03:03:39 +02:00
|
|
|
#ifdef CHECK_WITH_VALGRIND
|
|
|
|
|
# include "vvp_cleanup.h"
|
|
|
|
|
#endif
|
2010-05-31 22:12:06 +02:00
|
|
|
# include <cstdlib>
|
|
|
|
|
# include <cstring>
|
2003-05-07 05:39:12 +02:00
|
|
|
# include <iostream>
|
2010-05-31 22:12:06 +02:00
|
|
|
# include <cassert>
|
2009-10-16 23:18:00 +02:00
|
|
|
# include <map>
|
2002-03-18 01:19:34 +01:00
|
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
ufunc_core::ufunc_core(unsigned owid, vvp_net_t*ptr,
|
|
|
|
|
unsigned nports, vvp_net_t**ports,
|
2008-10-06 18:53:39 +02:00
|
|
|
vvp_code_t sa, struct __vpiScope*call_scope__,
|
2008-09-27 01:54:13 +02:00
|
|
|
char*result_label, char*scope_label)
|
2005-04-01 08:02:45 +02:00
|
|
|
: vvp_wide_fun_core(ptr, nports)
|
2002-03-18 01:19:34 +01:00
|
|
|
{
|
2005-03-18 03:56:03 +01:00
|
|
|
owid_ = owid;
|
|
|
|
|
ports_ = ports;
|
|
|
|
|
code_ = sa;
|
2002-03-18 01:19:34 +01:00
|
|
|
thread_ = 0;
|
2008-10-06 18:53:39 +02:00
|
|
|
call_scope_ = call_scope__;
|
2002-03-18 01:19:34 +01:00
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
functor_ref_lookup(&result_, result_label);
|
2008-09-27 01:54:13 +02:00
|
|
|
|
|
|
|
|
compile_vpi_lookup((vpiHandle*)(&func_scope_), scope_label);
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ufunc_core::~ufunc_core()
|
|
|
|
|
{
|
2009-10-14 03:03:39 +02:00
|
|
|
delete [] ports_;
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
/*
|
2008-09-27 01:54:13 +02:00
|
|
|
* This method is called by the %exec_ufunc function to prepare the
|
2005-03-18 03:56:03 +01:00
|
|
|
* input variables of the function for execution. The method copies
|
|
|
|
|
* the input values collected by the core to the variables.
|
|
|
|
|
*/
|
2008-10-28 18:52:39 +01:00
|
|
|
void ufunc_core::assign_bits_to_ports(vvp_context_t context)
|
2002-03-18 01:19:34 +01:00
|
|
|
{
|
2005-04-01 08:02:45 +02:00
|
|
|
for (unsigned idx = 0 ; idx < port_count() ; idx += 1) {
|
2005-03-18 03:56:03 +01:00
|
|
|
vvp_net_t*net = ports_[idx];
|
|
|
|
|
vvp_net_ptr_t pp (net, 0);
|
2009-11-29 18:59:51 +01:00
|
|
|
|
|
|
|
|
// If the port is a real variable, then simply copy the
|
|
|
|
|
// propagated input to the port variable.
|
2008-01-29 03:57:55 +01:00
|
|
|
if (vvp_fun_signal_real*tmp = dynamic_cast<vvp_fun_signal_real*>(net->fun))
|
2008-10-28 18:52:39 +01:00
|
|
|
tmp->recv_real(pp, value_r(idx), context);
|
2009-11-29 18:59:51 +01:00
|
|
|
|
|
|
|
|
// If the port is a bit4 vector, then copy the
|
|
|
|
|
// propagated input to the port variable. Detect the
|
|
|
|
|
// special case that the input vector is nil, and
|
|
|
|
|
// convert that to an 'bx vector that matches the width
|
|
|
|
|
// of the port variable. This is to handle the uncommon
|
|
|
|
|
// startup case where the input values have not
|
|
|
|
|
// propagated useful values yet.
|
|
|
|
|
if (vvp_fun_signal_vec*tmp = dynamic_cast<vvp_fun_signal_vec*>(net->fun)) {
|
|
|
|
|
const vvp_vector4_t&tmp_val = value(idx);
|
|
|
|
|
if (tmp_val.size() == 0) {
|
|
|
|
|
const vvp_vector4_t&ref = tmp->vec4_unfiltered_value();
|
|
|
|
|
vvp_vector4_t xxx (ref.size(), BIT4_X);
|
|
|
|
|
tmp->recv_vec4(pp, xxx, context);
|
|
|
|
|
} else {
|
|
|
|
|
tmp->recv_vec4(pp, tmp_val, context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
/*
|
2008-09-27 01:54:13 +02:00
|
|
|
* This method is called by the %exec_ufunc instruction to copy the
|
2005-03-18 03:56:03 +01:00
|
|
|
* result from the return code variable and deliver it to the output
|
|
|
|
|
* of the functor, back into the netlist.
|
|
|
|
|
*/
|
2002-03-18 01:19:34 +01:00
|
|
|
void ufunc_core::finish_thread(vthread_t thr)
|
|
|
|
|
{
|
|
|
|
|
thread_ = 0;
|
2008-01-29 03:57:55 +01:00
|
|
|
if (vvp_fun_signal_real*sig = dynamic_cast<vvp_fun_signal_real*>(result_->fun))
|
2009-09-12 21:25:18 +02:00
|
|
|
propagate_real(sig->real_unfiltered_value());
|
2008-01-29 03:57:55 +01:00
|
|
|
|
|
|
|
|
if (vvp_fun_signal_vec*sig = dynamic_cast<vvp_fun_signal_vec*>(result_->fun))
|
2009-08-29 07:08:40 +02:00
|
|
|
propagate_vec4(sig->vec4_unfiltered_value());
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-22 22:48:34 +01:00
|
|
|
/*
|
|
|
|
|
* This method is only called when a trigger event occurs. Just arrange for
|
|
|
|
|
* the function to be called.
|
|
|
|
|
*/
|
|
|
|
|
void ufunc_core::recv_vec4(vvp_net_ptr_t port, const vvp_vector4_t&bit,
|
|
|
|
|
vvp_context_t)
|
|
|
|
|
{
|
|
|
|
|
invoke_thread_();
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-18 01:19:34 +01:00
|
|
|
/*
|
2005-03-18 03:56:03 +01:00
|
|
|
* The recv_vec4 methods of the input functors call this to assign the
|
|
|
|
|
* input value to the port of the functor. I save the input value and
|
|
|
|
|
* arrange for the function to be called.
|
2002-03-18 01:19:34 +01:00
|
|
|
*/
|
2005-04-01 08:02:45 +02:00
|
|
|
void ufunc_core::recv_vec4_from_inputs(unsigned port)
|
2008-01-29 03:57:55 +01:00
|
|
|
{
|
|
|
|
|
invoke_thread_();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ufunc_core::recv_real_from_inputs(unsigned port)
|
|
|
|
|
{
|
|
|
|
|
invoke_thread_();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ufunc_core::invoke_thread_()
|
2002-03-18 01:19:34 +01:00
|
|
|
{
|
2005-03-18 03:56:03 +01:00
|
|
|
if (thread_ == 0) {
|
2008-09-27 01:54:13 +02:00
|
|
|
thread_ = vthread_new(code_, call_scope_);
|
2005-03-18 03:56:03 +01:00
|
|
|
schedule_vthread(thread_, 0);
|
|
|
|
|
}
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This function compiles the .ufunc statement that is discovered in
|
|
|
|
|
* the source file. Create all the functors and the thread, and
|
|
|
|
|
* connect them all up.
|
2005-03-18 03:56:03 +01:00
|
|
|
*
|
|
|
|
|
* The argv list is a list of the inputs to the function.
|
|
|
|
|
*
|
|
|
|
|
* The portv list is a list of variables that the function reads as
|
|
|
|
|
* inputs. The core assigns values to these nets as part of the startup.
|
2002-03-18 01:19:34 +01:00
|
|
|
*/
|
|
|
|
|
void compile_ufunc(char*label, char*code, unsigned wid,
|
|
|
|
|
unsigned argc, struct symb_s*argv,
|
|
|
|
|
unsigned portc, struct symb_s*portv,
|
2008-12-22 22:48:34 +01:00
|
|
|
struct symb_s retv, char*scope_label,
|
|
|
|
|
char*trigger_label)
|
2002-03-18 01:19:34 +01:00
|
|
|
{
|
2005-03-18 03:56:03 +01:00
|
|
|
/* The input argument list and port list must have the same
|
|
|
|
|
sizes, since internally we will be mapping the inputs list
|
|
|
|
|
to the ports list. */
|
2002-03-18 01:19:34 +01:00
|
|
|
assert(argc == portc);
|
|
|
|
|
|
2008-09-27 01:54:13 +02:00
|
|
|
struct __vpiScope*call_scope = vpip_peek_current_scope();
|
|
|
|
|
assert(call_scope);
|
2002-03-18 01:19:34 +01:00
|
|
|
|
|
|
|
|
/* Construct some phantom code that is the thread of the
|
|
|
|
|
function call. The first instruction, at the start_address
|
|
|
|
|
of the function, loads the points and calls the function.
|
2003-05-07 05:39:12 +02:00
|
|
|
The last instruction is the usual %end. So the thread looks
|
|
|
|
|
like this:
|
|
|
|
|
|
2008-09-27 01:54:13 +02:00
|
|
|
%exec_ufunc <core>;
|
2003-05-07 05:39:12 +02:00
|
|
|
%end;
|
|
|
|
|
|
2008-09-27 01:54:13 +02:00
|
|
|
The %exec_ufunc copies the input values into local regs,
|
|
|
|
|
runs the function code, then copies the output values to
|
|
|
|
|
the destination net functors. */
|
2003-05-07 05:39:12 +02:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
vvp_code_t start_code = codespace_allocate();
|
2008-09-27 01:54:13 +02:00
|
|
|
start_code->opcode = of_EXEC_UFUNC;
|
2002-03-18 01:19:34 +01:00
|
|
|
code_label_lookup(start_code, code);
|
|
|
|
|
|
2008-09-27 01:54:13 +02:00
|
|
|
vvp_code_t end_code = codespace_allocate();
|
|
|
|
|
end_code->opcode = &of_END;
|
2002-03-18 01:19:34 +01:00
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
/* Run through the function ports (which are related to but
|
|
|
|
|
not the same as the input ports) and arrange for their
|
|
|
|
|
binding. */
|
|
|
|
|
vvp_net_t**ports = new vvp_net_t*[portc];
|
|
|
|
|
for (unsigned idx = 0 ; idx < portc ; idx += 1) {
|
|
|
|
|
functor_ref_lookup(&ports[idx], portv[idx].text);
|
|
|
|
|
}
|
2002-03-18 01:19:34 +01:00
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
/* Create the output functor and attach it to the label. Tell
|
|
|
|
|
it about the start address of the code stub, and the scope
|
|
|
|
|
that will contain the execution. */
|
|
|
|
|
vvp_net_t*ptr = new vvp_net_t;
|
|
|
|
|
ufunc_core*fcore = new ufunc_core(wid, ptr, portc, ports,
|
2008-09-27 01:54:13 +02:00
|
|
|
start_code, call_scope,
|
2008-12-22 22:48:34 +01:00
|
|
|
retv.text, scope_label);
|
2005-03-18 03:56:03 +01:00
|
|
|
ptr->fun = fcore;
|
|
|
|
|
define_functor_symbol(label, ptr);
|
|
|
|
|
free(label);
|
|
|
|
|
|
|
|
|
|
start_code->ufunc_core_ptr = fcore;
|
|
|
|
|
|
2005-04-01 08:02:45 +02:00
|
|
|
wide_inputs_connect(fcore, argc, argv);
|
2002-03-18 01:19:34 +01:00
|
|
|
|
2008-12-22 22:48:34 +01:00
|
|
|
/* If this function has a trigger event, connect the functor to
|
|
|
|
|
that event. */
|
|
|
|
|
if (trigger_label)
|
|
|
|
|
input_connect(ptr, 0, trigger_label);
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
free(argv);
|
|
|
|
|
free(portv);
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
2009-10-14 03:03:39 +02:00
|
|
|
|
|
|
|
|
#ifdef CHECK_WITH_VALGRIND
|
2009-10-16 23:18:00 +02:00
|
|
|
static map<ufunc_core*, bool> ufunc_map;
|
|
|
|
|
|
2009-10-14 03:03:39 +02:00
|
|
|
void exec_ufunc_delete(vvp_code_t euf_code)
|
|
|
|
|
{
|
2009-10-16 23:18:00 +02:00
|
|
|
ufunc_map[euf_code->ufunc_core_ptr] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ufunc_pool_delete(void)
|
|
|
|
|
{
|
|
|
|
|
map<ufunc_core*, bool>::iterator iter;
|
|
|
|
|
for (iter = ufunc_map.begin(); iter != ufunc_map.end(); iter++) {
|
|
|
|
|
delete iter->first;
|
|
|
|
|
}
|
2009-10-14 03:03:39 +02:00
|
|
|
}
|
|
|
|
|
#endif
|