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
|
/***************************************************************************
* Copyright (C) 2003-2005 by David Saxton *
* [email protected] *
* *
* 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 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include <vector>
#include "circuit.h"
#include "elementset.h"
#include "logic.h"
#include "matrix.h"
#include "simulator.h"
#include "src/core/ktlconfig.h"
LogicIn::LogicIn( LogicConfig config )
: Element::Element()
{
m_config = config;
m_pCallbackFunction = 0l;
m_numCNodes = 1;
m_bLastState = false;
m_pNextLogic = 0l;
setLogic(getConfig());
}
LogicIn::~LogicIn()
{
Simulator::self()->removeLogicInReferences(this);
}
void LogicIn::setCallback( CallbackClass * object, CallbackPtr func )
{
m_pCallbackFunction = func;
m_pCallbackObject = object;
}
void LogicIn::check()
{
if (!b_status)
return;
bool newState;
if (m_bLastState)
{
// Was high, will still be high unless voltage is less than falling trigger
newState = p_cnode[0]->v > m_config.fallingTrigger;
}
else
{
// Was low, will still be low unless voltage is more than rising trigger
newState = p_cnode[0]->v > m_config.risingTrigger;
}
if ( m_pCallbackFunction && (newState != m_bLastState) )
{
m_bLastState = newState;
(m_pCallbackObject->*m_pCallbackFunction)(newState);
}
m_bLastState = newState;
}
void LogicIn::setLogic( LogicConfig config )
{
m_config = config;
check();
}
void LogicIn::setElementSet( ElementSet *c )
{
if (c)
m_pNextLogic = 0l;
else
m_cnodeI[0] = 0.;
Element::setElementSet(c);
}
void LogicIn::add_initial_dc()
{
}
void LogicIn::updateCurrents()
{
}
LogicConfig LogicIn::getConfig()
{
LogicConfig c;
c.risingTrigger = KTLConfig::logicRisingTrigger();
c.fallingTrigger = KTLConfig::logicFallingTrigger();
c.output = KTLConfig::logicOutputHigh();
c.highImpedance = KTLConfig::logicOutputHighImpedance();
c.lowImpedance = KTLConfig::logicOutputLowImpedance();
return c;
}
LogicOut::LogicOut( LogicConfig config, bool _high )
: LogicIn(config)
{
m_bCanAddChanged = true;
m_bOutputHighConductanceConst = false;
m_bOutputLowConductanceConst = false;
m_bOutputHighVoltageConst = false;
m_pNextChanged[0] = m_pNextChanged[1] = 0l;
m_pSimulator = 0l;
m_bUseLogicChain = false;
b_state = false;
m_numCNodes = 1;
m_vHigh = m_gHigh = m_gLow = 0.0;
m_old_g_out = m_g_out = 0.0;
m_old_v_out = m_v_out = 0.0;
setHigh(_high);
// Although we already call this function in LogicIn's constructor, our
// virtual function will not have got called, so we have to call it again.
setLogic(getConfig());
}
LogicOut::~LogicOut()
{
if (!m_pSimulator)
m_pSimulator = Simulator::self();
// Note that although this function will get called in the destructor of
// LogicIn, we must call it here as well as it needs to be called before
// removeLogicOutReferences(this) is called.
m_pSimulator->removeLogicInReferences(this);
m_pSimulator->removeLogicOutReferences(this);
}
void LogicOut::setUseLogicChain( bool use )
{
if (!m_pSimulator)
m_pSimulator = Simulator::self();
m_bUseLogicChain = use;
if (use)
setElementSet(0l);
}
void LogicOut::setElementSet( ElementSet *c )
{
if (!m_pSimulator)
m_pSimulator = Simulator::self();
if (c)
{
m_bUseLogicChain = false;
m_pNextChanged[0] = m_pNextChanged[1] = 0l;
}
// NOTE Make sure that the next two lines are the same as those in setHigh and setLogic
m_g_out = b_state ? m_gHigh : m_gLow;
m_v_out = b_state ? m_vHigh : 0.0;
LogicIn::setElementSet(c);
}
void LogicOut::setOutputHighConductance( double g )
{
m_bOutputHighConductanceConst = true;
if ( g == m_gHigh )
return;
m_gHigh = g;
configChanged();
}
void LogicOut::setOutputLowConductance( double g )
{
m_bOutputLowConductanceConst = true;
if ( g == m_gLow )
return;
m_gLow = g;
configChanged();
}
void LogicOut::setOutputHighVoltage( double v )
{
m_bOutputHighVoltageConst = true;
if ( v == m_vHigh )
return;
m_vHigh = v;
configChanged();
}
void LogicOut::setLogic( LogicConfig config )
{
m_config = config;
if (!m_bOutputHighConductanceConst)
m_gHigh = 1.0/config.highImpedance;
if (!m_bOutputLowConductanceConst)
m_gLow = (config.lowImpedance == 0.0) ? 0.0 : 1.0/config.lowImpedance;
if (!m_bOutputHighVoltageConst)
m_vHigh = config.output;
configChanged();
}
void LogicOut::configChanged()
{
if (m_bUseLogicChain)
return;
if (p_eSet)
p_eSet->setCacheInvalidated();
// Re-add the DC stuff using the new values
m_old_g_out = m_g_out;
m_old_v_out = m_v_out;
// NOTE Make sure that the next two lines are the same as those in setElementSet and setHigh
m_g_out = b_state ? m_gHigh : m_gLow;
m_v_out = b_state ? m_vHigh : 0.0;
add_initial_dc();
m_old_g_out = 0.;
m_old_v_out = 0.;
check();
}
void LogicOut::add_map()
{
if (!b_status) return;
p_A->setUse( p_cnode[0]->n(), p_cnode[0]->n(), Map::et_variable, false );
}
void LogicOut::add_initial_dc()
{
if (!b_status)
return;
A_g( 0, 0 ) += m_g_out-m_old_g_out;
b_i( 0 ) += m_g_out*m_v_out-m_old_g_out*m_old_v_out;
}
void LogicOut::updateCurrents()
{
if (m_bUseLogicChain)
{
m_cnodeI[0] = 0.;
return;
}
if (!b_status) {
return;
}
m_cnodeI[0] = (p_cnode[0]->v-m_v_out)*m_g_out;
}
void LogicOut::setHigh( bool high )
{
if ( high == b_state )
return;
if (m_bUseLogicChain)
{
b_state = high;
for ( LogicIn * logic = this; logic; logic = logic->nextLogic() )
logic->setLastState(high);
if (m_bCanAddChanged)
{
m_pSimulator->addChangedLogic(this);
m_bCanAddChanged = false;
}
return;
}
m_old_g_out = m_g_out;
m_old_v_out = m_v_out;
// NOTE Make sure that the next two lines are the same as those in setElementSet and setLogic
m_g_out = high ? m_gHigh : m_gLow;
m_v_out = high ? m_vHigh : 0.0;
add_initial_dc();
m_old_g_out = 0.;
m_old_v_out = 0.;
b_state = high;
if ( p_eSet && p_eSet->circuit()->canAddChanged() )
{
m_pSimulator->addChangedCircuit( p_eSet->circuit() );
p_eSet->circuit()->setCanAddChanged(false);
}
}
|