Merge branch 'master' into verilog-ams
[sverilog.git] / net_func.cc
blob5a4d4e5bb219dd81f26d2770858149f38e42810b
1 /*
2 * Copyright (c) 2002-2004 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 # include "config.h"
21 # include "netlist.h"
22 # include "compiler.h"
23 # include "PExpr.h"
24 # include <iostream>
27 * To make a NetUserFunc device, make as many pins as there are ports
28 * in the function. Get the port count from the function definition,
29 * which accounts for all the inputs, plus one for the phantom output
30 * that is the result.
32 NetUserFunc::NetUserFunc(NetScope*s, perm_string n, NetScope*d)
33 : NetNode(s, n, d->func_def()->port_count()+1),
34 def_(d)
36 pin(0).set_dir(Link::OUTPUT);
37 pin(0).set_name(def_->basename(), 0);
39 for (unsigned idx = 1 ; idx < pin_count() ; idx += 1) {
41 pin(idx).set_dir(Link::INPUT);
42 pin(idx).set_name(perm_string::literal("D"), idx-1);
43 pin(idx).drive0(Link::HIGHZ);
44 pin(idx).drive1(Link::HIGHZ);
48 NetUserFunc::~NetUserFunc()
52 ivl_variable_type_t NetUserFunc::data_type(unsigned port) const
54 NetFuncDef*def = def_->func_def();
56 /* Port 0 is the return port. */
57 if (port == 0) {
58 const NetNet*sig = def->return_sig();
59 assert(sig);
60 return sig->data_type();
63 port -= 1;
64 assert(port < def->port_count());
65 const NetNet*port_sig = def->port(port);
67 return port_sig->data_type();
70 unsigned NetUserFunc::port_width(unsigned port) const
72 NetFuncDef*def = def_->func_def();
74 /* Port 0 is the return port. */
75 if (port == 0) {
76 const NetNet*sig = def->return_sig();
77 assert(sig);
78 return sig->vector_width();
81 port -= 1;
82 assert(port < def->port_count());
83 const NetNet*port_sig = def->port(port);
85 return port_sig->vector_width();
88 const NetScope* NetUserFunc::def() const
90 return def_;
94 * This method of the PECallFunction class checks that the parameters
95 * of the PECallFunction match the function definition. This is used
96 * during elaboration to validate the parameters before using them.
98 bool PECallFunction::check_call_matches_definition_(Design*des, NetScope*dscope) const
100 assert(dscope);
102 /* How many parameters have I got? Normally the size of the
103 list is correct, but there is the special case of a list of
104 1 nil pointer. This is how the parser tells me of no
105 parameter. In other words, ``func()'' is 1 nil parameter. */
107 unsigned parms_count = parms_.count();
108 if ((parms_count == 1) && (parms_[0] == 0))
109 parms_count = 0;
111 if (dscope->type() != NetScope::FUNC) {
112 cerr << get_fileline() << ": error: Attempt to call scope "
113 << scope_path(dscope) << " as a function." << endl;
114 des->errors += 1;
115 return false;
118 if (parms_count != dscope->func_def()->port_count()) {
119 cerr << get_fileline() << ": error: Function " << scope_path(dscope)
120 << " expects " << (dscope->func_def()->port_count())
121 << " arguments, you passed " << parms_count << "."
122 << endl;
123 des->errors += 1;
124 return false;
127 return true;
131 NetSysFunc::NetSysFunc(NetScope*s, perm_string n,
132 const struct sfunc_return_type*def,
133 unsigned ports)
134 : NetNode(s, n, ports)
136 def_ = def;
138 pin(0).set_dir(Link::OUTPUT);
139 pin(0).set_name(perm_string::literal("Q"), 0);
141 for (unsigned idx = 1 ; idx < pin_count() ; idx += 1) {
143 pin(idx).set_dir(Link::INPUT);
144 pin(idx).set_name(perm_string::literal("D"), idx-1);
145 pin(idx).drive0(Link::HIGHZ);
146 pin(idx).drive1(Link::HIGHZ);
150 NetSysFunc::~NetSysFunc()
154 const char*NetSysFunc::func_name() const
156 return def_->name;
159 ivl_variable_type_t NetSysFunc::data_type() const
161 return def_->type;
164 unsigned NetSysFunc::vector_width() const
166 return def_->wid;