2002-03-18 01:19:34 +01:00
|
|
|
/*
|
2005-03-18 03:56:03 +01:00
|
|
|
* Copyright (c) 2002-2005 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
|
|
|
|
|
*/
|
2002-08-12 03:34:58 +02:00
|
|
|
#ifdef HAVE_CVS_IDENT
|
2005-03-18 03:56:03 +01:00
|
|
|
#ident "$Id: ufunc.cc,v 1.6 2005/03/18 02:56:04 steve Exp $"
|
2002-03-18 01:19:34 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
# include "compile.h"
|
|
|
|
|
# include "symbols.h"
|
|
|
|
|
# include "codes.h"
|
|
|
|
|
# include "functor.h"
|
|
|
|
|
# include "ufunc.h"
|
|
|
|
|
# include "vthread.h"
|
|
|
|
|
# include "schedule.h"
|
|
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
|
# include <malloc.h>
|
|
|
|
|
#endif
|
|
|
|
|
# include <stdlib.h>
|
|
|
|
|
# include <string.h>
|
2003-05-07 05:39:12 +02:00
|
|
|
# include <iostream>
|
2002-03-18 01:19:34 +01:00
|
|
|
# include <assert.h>
|
|
|
|
|
|
|
|
|
|
#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,
|
|
|
|
|
vvp_code_t sa, struct __vpiScope*run_scope,
|
|
|
|
|
char*result_label)
|
2002-03-18 01:19:34 +01:00
|
|
|
{
|
2005-03-18 03:56:03 +01:00
|
|
|
owid_ = owid;
|
|
|
|
|
onet_ = ptr;
|
|
|
|
|
nports_ = nports;
|
|
|
|
|
ports_ = ports;
|
|
|
|
|
port_values_ = new vvp_vector4_t[nports];
|
|
|
|
|
code_ = sa;
|
2002-03-18 01:19:34 +01:00
|
|
|
thread_ = 0;
|
|
|
|
|
scope_ = run_scope;
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
functor_ref_lookup(&result_, result_label);
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ufunc_core::~ufunc_core()
|
|
|
|
|
{
|
2005-03-18 03:56:03 +01:00
|
|
|
delete[]port_values_;
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
/*
|
|
|
|
|
* This method is called by the %fork_ufunc function to prepare the
|
|
|
|
|
* input variables of the function for execution. The method copies
|
|
|
|
|
* the input values collected by the core to the variables.
|
|
|
|
|
*/
|
2002-03-18 01:19:34 +01:00
|
|
|
void ufunc_core::assign_bits_to_ports(void)
|
|
|
|
|
{
|
2005-03-18 03:56:03 +01:00
|
|
|
for (unsigned idx = 0 ; idx < nports_ ; idx += 1) {
|
|
|
|
|
vvp_net_t*net = ports_[idx];
|
|
|
|
|
vvp_net_ptr_t pp (net, 0);
|
|
|
|
|
net->fun->recv_vec4(pp, port_values_[idx]);
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
/*
|
|
|
|
|
* This method is called by the %join_ufunc instruction to copy the
|
|
|
|
|
* 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;
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
vvp_fun_signal*sig = reinterpret_cast<vvp_fun_signal*>(result_->fun);
|
|
|
|
|
|
|
|
|
|
vvp_send_vec4(onet_->out, sig->vec4_value());
|
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-03-18 03:56:03 +01:00
|
|
|
void ufunc_core::recv_vec4_from_inputs(unsigned port, vvp_vector4_t bit)
|
2002-03-18 01:19:34 +01:00
|
|
|
{
|
2005-03-18 03:56:03 +01:00
|
|
|
assert(port < nports_);
|
|
|
|
|
port_values_[port] = bit;
|
|
|
|
|
|
|
|
|
|
if (thread_ == 0) {
|
|
|
|
|
thread_ = vthread_new(code_, scope_);
|
|
|
|
|
schedule_vthread(thread_, 0);
|
|
|
|
|
}
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
ufunc_input_functor::ufunc_input_functor(ufunc_core*c, unsigned base)
|
|
|
|
|
: core_(c), port_base_(base)
|
|
|
|
|
{
|
|
|
|
|
}
|
2002-03-18 01:19:34 +01:00
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
ufunc_input_functor::~ufunc_input_functor()
|
|
|
|
|
{
|
|
|
|
|
}
|
2002-03-18 01:19:34 +01:00
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
void ufunc_input_functor::recv_vec4(vvp_net_ptr_t port, vvp_vector4_t bit)
|
2002-03-18 01:19:34 +01:00
|
|
|
{
|
2005-03-18 03:56:03 +01:00
|
|
|
unsigned pidx = port_base_ + port.port();
|
|
|
|
|
core_->recv_vec4_from_inputs(pidx, bit);
|
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,
|
2005-03-18 03:56:03 +01:00
|
|
|
struct symb_s retv)
|
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);
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
struct __vpiScope*run_scope = vpip_peek_current_scope();
|
|
|
|
|
assert(run_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:
|
|
|
|
|
|
|
|
|
|
%fork_ufunc <core>;
|
|
|
|
|
%join;
|
|
|
|
|
%join_ufunc;
|
|
|
|
|
%end;
|
|
|
|
|
|
|
|
|
|
The %fork_ufunc starts the user defined function by copying
|
|
|
|
|
the input values into local regs, forking a thread and
|
|
|
|
|
pushing that thread. The %join waits on that thread. The
|
|
|
|
|
$join_ufunc then copies the output values to the
|
|
|
|
|
destination net functors. */
|
|
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
vvp_code_t start_code = codespace_allocate();
|
2003-05-07 05:39:12 +02:00
|
|
|
start_code->opcode = of_FORK_UFUNC;
|
2002-03-18 01:19:34 +01:00
|
|
|
code_label_lookup(start_code, code);
|
|
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
{ vvp_code_t codep = codespace_allocate();
|
2003-05-07 05:39:12 +02:00
|
|
|
codep->opcode = &of_JOIN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vvp_code_t ujoin_code;
|
2003-07-03 22:03:36 +02:00
|
|
|
ujoin_code = codespace_allocate();
|
|
|
|
|
ujoin_code->opcode = &of_JOIN_UFUNC;
|
2003-05-07 05:39:12 +02:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
{ vvp_code_t codep = codespace_allocate();
|
2002-03-18 01:19:34 +01:00
|
|
|
codep->opcode = &of_END;
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
start_code, run_scope,
|
|
|
|
|
retv.text);
|
|
|
|
|
ptr->fun = fcore;
|
|
|
|
|
define_functor_symbol(label, ptr);
|
|
|
|
|
free(label);
|
|
|
|
|
|
|
|
|
|
start_code->ufunc_core_ptr = fcore;
|
|
|
|
|
ujoin_code->ufunc_core_ptr = fcore;
|
|
|
|
|
|
|
|
|
|
/* Create input functors to receive values from the
|
|
|
|
|
network. These functors pass the data to the core. */
|
|
|
|
|
unsigned input_functors = (argc+3) / 4;
|
|
|
|
|
for (unsigned idx = 0 ; idx < input_functors ; idx += 1) {
|
|
|
|
|
unsigned base = idx*4;
|
|
|
|
|
unsigned trans = 4;
|
|
|
|
|
if (base+trans > argc)
|
|
|
|
|
trans = argc - base;
|
|
|
|
|
|
|
|
|
|
ufunc_input_functor*cur = new ufunc_input_functor(fcore, base);
|
|
|
|
|
ptr = new vvp_net_t;
|
|
|
|
|
ptr->fun = cur;
|
|
|
|
|
|
|
|
|
|
inputs_connect(ptr, trans, argv+base);
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
free(argv);
|
|
|
|
|
free(portv);
|
2002-03-18 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
2005-03-18 03:56:03 +01:00
|
|
|
|
2002-03-18 01:19:34 +01:00
|
|
|
/*
|
|
|
|
|
* $Log: ufunc.cc,v $
|
2005-03-18 03:56:03 +01:00
|
|
|
* Revision 1.6 2005/03/18 02:56:04 steve
|
|
|
|
|
* Add support for LPM_UFUNC user defined functions.
|
|
|
|
|
*
|
2004-12-11 03:31:25 +01:00
|
|
|
* Revision 1.5 2004/12/11 02:31:30 steve
|
|
|
|
|
* Rework of internals to carry vectors through nexus instead
|
|
|
|
|
* of single bits. Make the ivl, tgt-vvp and vvp initial changes
|
|
|
|
|
* down this path.
|
|
|
|
|
*
|
2003-07-03 22:03:36 +02:00
|
|
|
* Revision 1.4 2003/07/03 20:03:36 steve
|
|
|
|
|
* Remove the vvp_cpoint_t indirect code pointer.
|
|
|
|
|
*
|
2003-05-07 05:39:12 +02:00
|
|
|
* Revision 1.3 2003/05/07 03:39:12 steve
|
|
|
|
|
* ufunc calls to functions can have scheduling complexities.
|
|
|
|
|
*
|
2002-08-12 03:34:58 +02:00
|
|
|
* Revision 1.2 2002/08/12 01:35:08 steve
|
|
|
|
|
* conditional ident string using autoconfig.
|
|
|
|
|
*
|
2002-03-18 01:19:34 +01:00
|
|
|
* Revision 1.1 2002/03/18 00:19:34 steve
|
|
|
|
|
* Add the .ufunc statement.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|