Reformulate number-to-real on vvp code generator
[iverilog.git] / PDelays.cc
blob0d87d099f03d6f1311f2aa062cbd777c926be404
1 /*
2 * Copyright (c) 1999 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
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: PDelays.cc,v 1.15 2006/07/08 21:48:46 steve Exp $"
21 #endif
23 # include "config.h"
25 # include <iostream>
27 # include "PDelays.h"
28 # include "PExpr.h"
29 # include "verinum.h"
31 PDelays::PDelays()
33 delete_flag_ = true;
34 for (unsigned idx = 0 ; idx < 3 ; idx += 1)
35 delay_[idx] = 0;
38 PDelays::~PDelays()
40 if (delete_flag_) {
41 for (unsigned idx = 0 ; idx < 3 ; idx += 1)
42 delete delay_[idx];
46 void PDelays::set_delay(PExpr*del)
48 assert(del);
49 assert(delay_[0] == 0);
50 delay_[0] = del;
51 delete_flag_ = true;
55 void PDelays::set_delays(const svector<PExpr*>*del, bool df)
57 assert(del);
58 assert(del->count() <= 3);
59 for (unsigned idx = 0 ; idx < del->count() ; idx += 1)
60 delay_[idx] = (*del)[idx];
62 delete_flag_ = df;
65 static NetExpr*calculate_val(Design*des, NetScope*scope, const PExpr*expr)
68 NetExpr*dex = expr->elaborate_expr(des, scope, -1, false);
69 if (NetExpr*tmp = dex->eval_tree()) {
70 delete dex;
71 dex = tmp;
74 /* If the delay expression is a real constant or vector
75 constant, then evaluate it, scale it to the local time
76 units, and return an adjusted value. */
78 if (NetECReal*tmp = dynamic_cast<NetECReal*>(dex)) {
79 verireal fn = tmp->value();
81 int shift = scope->time_unit() - des->get_precision();
82 long delay = fn.as_long(shift);
83 if (delay < 0)
84 delay = 0;
86 delete tmp;
87 NetEConst*tmp2 = new NetEConst(verinum(delay));
88 tmp2->set_line(*expr);
89 return tmp2;
93 if (NetEConst*tmp = dynamic_cast<NetEConst*>(dex)) {
94 verinum fn = tmp->value();
96 unsigned long delay =
97 des->scale_to_precision(fn.as_ulong(), scope);
99 delete tmp;
100 NetEConst*tmp2 = new NetEConst(verinum(delay));
101 tmp2->set_line(*expr);
102 return tmp2;
105 /* Oops, cannot evaluate down to a constant. */
106 return dex;
109 static NetExpr* make_delay_nets(Design*des, NetExpr*expr)
111 if (dynamic_cast<NetESignal*> (expr))
112 return expr;
114 if (dynamic_cast<NetEConst*> (expr))
115 return expr;
117 NetNet*sig = expr->synthesize(des);
118 if (sig == 0) {
119 cerr << expr->get_line() << ": error: Expression " << *expr
120 << " is not suitable for delay expression." << endl;
121 return 0;
124 expr = new NetESignal(sig);
125 return expr;
128 void PDelays::eval_delays(Design*des, NetScope*scope,
129 NetExpr*&rise_time,
130 NetExpr*&fall_time,
131 NetExpr*&decay_time,
132 bool as_nets_flag) const
134 assert(scope);
137 if (delay_[0]) {
138 rise_time = calculate_val(des, scope, delay_[0]);
139 if (as_nets_flag)
140 rise_time = make_delay_nets(des, rise_time);
142 if (delay_[1]) {
143 fall_time = calculate_val(des, scope, delay_[1]);
144 if (as_nets_flag)
145 fall_time = make_delay_nets(des, fall_time);
147 if (delay_[2]) {
148 decay_time = calculate_val(des, scope, delay_[2]);
149 if (as_nets_flag)
150 decay_time = make_delay_nets(des, decay_time);
152 } else {
153 if (rise_time < fall_time)
154 decay_time = rise_time;
155 else
156 decay_time = fall_time;
158 } else {
159 assert(delay_[2] == 0);
160 fall_time = rise_time;
161 decay_time = rise_time;
163 } else {
164 rise_time = 0;
165 fall_time = 0;
166 decay_time = 0;
171 * $Log: PDelays.cc,v $
172 * Revision 1.15 2006/07/08 21:48:46 steve
173 * Handle real valued literals in net contexts.
175 * Revision 1.14 2006/06/02 04:48:49 steve
176 * Make elaborate_expr methods aware of the width that the context
177 * requires of it. In the process, fix sizing of the width of unary
178 * minus is context determined sizes.
180 * Revision 1.13 2006/01/03 05:22:14 steve
181 * Handle complex net node delays.
183 * Revision 1.12 2006/01/02 05:33:19 steve
184 * Node delays can be more general expressions in structural contexts.
186 * Revision 1.11 2003/06/21 01:21:42 steve
187 * Harmless fixup of warnings.
189 * Revision 1.10 2003/02/08 19:49:21 steve
190 * Calculate delay statement delays using elaborated
191 * expressions instead of pre-elaborated expression
192 * trees.
194 * Remove the eval_pexpr methods from PExpr.
196 * Revision 1.9 2002/08/12 01:34:58 steve
197 * conditional ident string using autoconfig.
199 * Revision 1.8 2001/12/29 20:19:31 steve
200 * Do not delete delay expressions of UDP instances.
202 * Revision 1.7 2001/11/22 06:20:59 steve
203 * Use NetScope instead of string for scope path.
205 * Revision 1.6 2001/11/07 04:01:59 steve
206 * eval_const uses scope instead of a string path.
208 * Revision 1.5 2001/07/25 03:10:48 steve
209 * Create a config.h.in file to hold all the config
210 * junk, and support gcc 3.0. (Stephan Boettcher)
212 * Revision 1.4 2001/01/20 02:15:50 steve
213 * apologize for not supporting non-constant delays.
215 * Revision 1.3 2001/01/14 23:04:55 steve
216 * Generalize the evaluation of floating point delays, and
217 * get it working with delay assignment statements.
219 * Allow parameters to be referenced by hierarchical name.
221 * Revision 1.2 2000/02/23 02:56:53 steve
222 * Macintosh compilers do not support ident.
224 * Revision 1.1 1999/09/04 19:11:46 steve
225 * Add support for delayed non-blocking assignments.