-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathv_module.cc
377 lines (362 loc) · 12.1 KB
/
v_module.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* -*- C++ -*-
* Copyright (C) 2001 Albert Davis
* 2022, 2023 Felix Salfelder
*
* This file is part of "Gnucap", the Gnu Circuit Analysis Package
*
* 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 3, 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.
*------------------------------------------------------------------
* module stuff
*/
#include <u_nodemap.h>
#include <e_node.h>
#include <globals.h>
#include <e_paramlist.h>
#include <e_subckt.h>
#include <io_trace.h>
/*--------------------------------------------------------------------------*/
namespace{
/*--------------------------------------------------------------------------*/
// components with one node are unlikely.
const int node_capacity_floor = 2;
/*--------------------------------------------------------------------------*/
static void grow_nodes(int Index, node_t*& n, int& capacity, int capacity_floor)
{
if(Index < capacity){
}else{
int new_capacity = std::max(capacity, capacity_floor);
while(new_capacity <= Index) {
assert(new_capacity < new_capacity * 2);
new_capacity *= 2;
}
node_t* new_nodes = new node_t[new_capacity];
for(int i=0; i<capacity; ++i){
new_nodes[i] = n[i];
}
delete[] n;
n = new_nodes;
capacity = new_capacity;
}
}
/*--------------------------------------------------------------------------*/
static COMMON_PARAMLIST Default_SUBCKT(CC_STATIC);
/*--------------------------------------------------------------------------*/
class DEV_MODULE : public BASE_SUBCKT {
node_t* _n{nullptr};
private:
const BASE_SUBCKT* _parent;
int _node_capacity{0};
private:
explicit DEV_MODULE(const DEV_MODULE&);
public:
explicit DEV_MODULE();
~DEV_MODULE() { delete[] _n; _node_capacity = 0; }
CARD* clone()const override;
private:
void set_port_by_index(int Index, std::string& Value) override;
// void set_port_by_name(std::string&, std::string&) override;
private: // override virtual
bool is_device()const override {return _parent;}
char id_letter()const override { untested();return 'X';}
bool print_type_in_spice()const override {return true;}
std::string value_name()const override {return "#";}
int max_nodes()const override;
int min_nodes()const override;
int matrix_nodes()const override {return 0;}
// int net_nodes()const override { untested();return _net_nodes;}
void precalc_first()override;
bool makes_own_scope()const override {return !_parent;}
bool is_valid()const override;
CARD_LIST* scope()override;
const CARD_LIST* scope()const override {return const_cast<DEV_MODULE*>(this)->scope();}
void expand() override;
private: // no ops for prototype
void map_nodes()override {if(is_device()){ BASE_SUBCKT::map_nodes();}else{} }
void tr_begin()override {if(is_device()){ BASE_SUBCKT::tr_begin();}else{} }
void ac_begin()override {if(is_device()){ BASE_SUBCKT::ac_begin();}else{} }
void tr_load()override {if(is_device()){ BASE_SUBCKT::tr_load();}else{} }
void tr_accept()override {if(is_device()){ BASE_SUBCKT::tr_accept();}else{} }
void tr_regress()override {if(is_device()){ BASE_SUBCKT::tr_regress();}else{} }
void dc_final()override {if(is_device()){ BASE_SUBCKT::dc_final();}else{}}
void tr_final()override { untested();if(is_device()){ BASE_SUBCKT::tr_final();}else{}}
void tr_advance()override {if(is_device()){ BASE_SUBCKT::tr_advance();}else{} }
TIME_PAIR tr_review() override { if(is_device()){return BASE_SUBCKT::tr_review();}else{
return TIME_PAIR(NEVER, NEVER);}}
void dc_advance()override {if(is_device()){ BASE_SUBCKT::dc_advance();}else{} }
void do_ac()override {if(is_device()){ BASE_SUBCKT::do_ac();}else{} }
void ac_load()override {if(is_device()){ BASE_SUBCKT::ac_load();}else{} }
void ac_final()override {if(is_device()){ BASE_SUBCKT::ac_final();}else{}}
void tr_queue_eval()override{
if(is_device()){
BASE_SUBCKT::tr_queue_eval();
}else{
}
}
bool do_tr() override {if(is_device()){untested(); return BASE_SUBCKT::do_tr();}else{untested(); return true;} }
bool tr_needs_eval()const override{ untested();
if(is_device()){untested();
return BASE_SUBCKT::tr_needs_eval();
}else{untested();
return false;
}
}
private:
void precalc_last()override;
double tr_probe_num(const std::string&)const override;
int param_count_dont_print()const override{return common()->COMMON_COMPONENT::param_count();}
node_t& n_(int i)const {
assert(_n); assert(i>=0); assert(i<_node_capacity); return _n[i];
}
std::string port_name(int i)const override;
int set_param_by_name(std::string Name, std::string Value)override;
} p1;
DISPATCHER<CARD>::INSTALL d1(&device_dispatcher, "module", &p1);
/*--------------------------------------------------------------------------*/
void DEV_MODULE::set_port_by_index(int Index, std::string& Value)
{
grow_nodes(Index, _n, _node_capacity, node_capacity_floor);
BASE_SUBCKT::set_port_by_index(Index, Value);
}
/*--------------------------------------------------------------------------*/
int DEV_MODULE::max_nodes() const
{
if(_parent){
return ((CARD const*)_parent)->net_nodes();
}else{
// allow one more, building a prototype.
return net_nodes()+1;
}
}
/*--------------------------------------------------------------------------*/
int DEV_MODULE::min_nodes() const
{
if(_parent){
return ((CARD const*)_parent)->net_nodes();
}else{
return 0;
}
}
/*--------------------------------------------------------------------------*/
CARD_LIST* DEV_MODULE::scope()
{
if(is_device()){
return COMPONENT::scope();
}else{
return subckt();
}
}
/*--------------------------------------------------------------------------*/
bool DEV_MODULE::is_valid() const
{
assert(subckt());
assert(_parent);
assert(_parent->subckt());
PARAM_LIST const* params = _parent->subckt()->params();
PARAM_INSTANCE v = params->deep_lookup("_..is_valid");
trace2("DEV_MODULE::is_valid I", long_label(), v.string());
Base const* x = v.e_val(nullptr, subckt());
Integer c;
Integer* res = c.assign(x);
if(!res) { untested();
return true;
}else{
assert(x);
trace1("DEV_MODULE::is_valid I", typeid(*x).name());
int a = res->value();
delete res;
return a;
}
}
/*--------------------------------------------------------------------------*/
CARD* DEV_MODULE::clone()const
{
DEV_MODULE* new_instance = new DEV_MODULE(*this);
assert(!new_instance->subckt());
if (this == &p1){
// cloning from static, empty model
// has no parent.
new_instance->new_subckt(); // from DEV_SUBCKT_PROTO::DEV_SUBCKT_PROTO
}else if(_parent){
new_instance->_parent = _parent;
assert(new_instance->is_device());
}else{
new_instance->_parent = this;
assert(new_instance->is_device());
}
return new_instance;
}
/*--------------------------------------------------------------------------*/
DEV_MODULE::DEV_MODULE()
:BASE_SUBCKT(),
_parent(NULL), _node_capacity(0)
{
attach_common(&Default_SUBCKT);
assert(_n == NULL);
}
/*--------------------------------------------------------------------------*/
DEV_MODULE::DEV_MODULE(const DEV_MODULE& p)
:BASE_SUBCKT(p),
_parent(p._parent)
{
trace2("DEV_MODULE::DEV_MODULE", long_label(), net_nodes());
_node_capacity = net_nodes();
if(_node_capacity){
_n = new node_t[_node_capacity];
}else{
assert(_n == NULL);
}
if(p.is_device()){
for (int ii = 0; ii < net_nodes(); ++ii) {
_n[ii] = p._n[ii];
}
}else{
for (int ii = 0; ii < net_nodes(); ++ii) {
assert(!_n[ii].n_());
}
}
assert(!subckt());
}
/*--------------------------------------------------------------------------*/
int DEV_MODULE::set_param_by_name(std::string Name, std::string Value)
{
assert(_parent);
assert(_parent->subckt());
PARAM_LIST::const_iterator p = _parent->subckt()->params()->find(Name);
if(p != _parent->subckt()->params()->end()){
return BASE_SUBCKT::set_param_by_name(Name,Value);
}else{
throw Exception_No_Match(Name);
}
}
/*--------------------------------------------------------------------------*/
std::string DEV_MODULE::port_name(int i)const
{
if (const DEV_MODULE* p=dynamic_cast<const DEV_MODULE*>(_parent)) {
if (i<p->net_nodes()){
return p->port_value(i);
}else{untested();
return "";
}
}else if(_parent) { untested(); untested();
// reachable?
return "";
}else if(i<net_nodes()) {
return port_value(i);
}else{ untested();
return "";
}
}
/*--------------------------------------------------------------------------*/
void DEV_MODULE::expand()
{
if(!_parent){
// not a device. probably a prototype
}else{
BASE_SUBCKT::expand();
COMMON_PARAMLIST* c = prechecked_cast<COMMON_PARAMLIST*>(mutable_common());
assert(c);
BASE_SUBCKT::expand();
assert(_parent);
assert(_parent->subckt());
assert(_parent->subckt()->params());
PARAM_LIST* pl = const_cast<PARAM_LIST*>(_parent->subckt()->params());
assert(pl);
c->_params.set_try_again(pl);
renew_subckt(_parent, &(c->_params));
subckt()->expand();
#if 1 // move to CARD_LIST::expand?
for(CARD_LIST::iterator i=subckt()->begin(); i!=subckt()->end(); ++i){
CARD* d = (*i)->deflate();
if(d == (*i)){ untested();
}else{
assert(d->owner() == this);
delete *i;
*i = d;
}
}
#endif
}
}
/*--------------------------------------------------------------------------*/
void DEV_MODULE::precalc_first()
{
BASE_SUBCKT::precalc_first();
if (subckt()) {
}else{
new_subckt();
}
COMMON_PARAMLIST* c = prechecked_cast<COMMON_PARAMLIST*>(mutable_common());
assert(c);
if(_parent){
PARAM_LIST* pl = const_cast<PARAM_LIST*>(_parent->subckt()->params());
assert(pl);
c->_params.set_try_again(pl);
subckt()->attach_params(&(c->_params), scope());
assert(!is_constant()); /* because I have more work to do */
}else{
}
}
/*--------------------------------------------------------------------------*/
void DEV_MODULE::precalc_last()
{
if(is_device()){
BASE_SUBCKT::precalc_last();
COMMON_PARAMLIST* c = prechecked_cast<COMMON_PARAMLIST*>(mutable_common());
assert(c);
subckt()->attach_params(&(c->_params), scope());
subckt()->precalc_last();
assert(!is_constant()); /* because I have more work to do */
}else{
// its a proto.
}
}
/*--------------------------------------------------------------------------*/
double DEV_MODULE::tr_probe_num(const std::string& x)const
{
if (Umatch(x, "p ")) {untested();
double power = 0.;
assert(subckt());
for (CARD_LIST::const_iterator
ci = subckt()->begin(); ci != subckt()->end(); ++ci) {untested();
power += CARD::probe(*ci,"P");
}
return power;
}else if (Umatch(x, "pd ")) {untested();
double power = 0.;
assert(subckt());
for (CARD_LIST::const_iterator
ci = subckt()->begin(); ci != subckt()->end(); ++ci) {untested();
power += CARD::probe(*ci,"PD");
}
return power;
}else if (Umatch(x, "ps ")) {untested();
double power = 0.;
assert(subckt());
for (CARD_LIST::const_iterator
ci = subckt()->begin(); ci != subckt()->end(); ++ci) {untested();
power += CARD::probe(*ci,"PS");
}
return power;
}else{
return COMPONENT::tr_probe_num(x);
}
/*NOTREACHED*/
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
} // namespace
/*--------------------------------------------------------------------------*/
// vim:ts=8:sw=2:noet: