blob: 619295dff151f628834a2c4b15885e0cce446cb3 (
plain)
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
|
/***************************************************************************
* Copyright (C) 2005 by David Saxton *
* david@bluehaze.org *
* *
* 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 "pin.h"
#include "wire.h"
#include <assert.h>
Wire::Wire( Pin * startPin, Pin * endPin )
{
assert(startPin);
assert(endPin);
m_pStartPin = startPin;
m_pEndPin = endPin;
m_current = 0.;
m_bCurrentIsKnown = false;
m_pStartPin->addOutputWire(this);
m_pEndPin->addInputWire(this);
}
Wire::~Wire()
{
}
bool Wire::calculateCurrent()
{
if ( m_pStartPin->currentIsKnown() && m_pStartPin->numWires() < 2 )
{
m_current = m_pStartPin->current();
m_bCurrentIsKnown = true;
return true;
}
if ( m_pEndPin->currentIsKnown() && m_pEndPin->numWires() < 2 )
{
m_current = -m_pEndPin->current();
m_bCurrentIsKnown = true;
return true;
}
if ( m_pStartPin->currentIsKnown() )
{
double i = m_pStartPin->current();
bool ok = true;
const WireList outlist = m_pStartPin->outputWireList();
WireList::const_iterator end = outlist.end();
for ( WireList::const_iterator it = outlist.begin(); it != end && ok; ++it )
{
if ( *it && (Wire*)*it != this )
{
if ( (*it)->currentIsKnown() )
i -= (*it)->current();
else
ok = false;
}
}
const WireList inlist = m_pStartPin->inputWireList();
end = inlist.end();
for ( WireList::const_iterator it = inlist.begin(); it != end && ok; ++it )
{
if ( *it && (Wire*)*it != this )
{
if ( (*it)->currentIsKnown() )
i += (*it)->current();
else
ok = false;
}
}
if (ok)
{
m_current = i;
m_bCurrentIsKnown = true;
return true;
}
}
if ( m_pEndPin->currentIsKnown() )
{
double i = -m_pEndPin->current();
bool ok = true;
const WireList outlist = m_pEndPin->outputWireList();
WireList::const_iterator end = outlist.end();
for ( WireList::const_iterator it = outlist.begin(); it != end && ok; ++it )
{
if ( *it && (Wire*)*it != this )
{
if ( (*it)->currentIsKnown() )
i += (*it)->current();
else
ok = false;
}
}
const WireList inlist = m_pEndPin->inputWireList();
end = inlist.end();
for ( WireList::const_iterator it = inlist.begin(); it != end && ok; ++it )
{
if ( *it && (Wire*)*it != this )
{
if ( (*it)->currentIsKnown() )
i -= (*it)->current();
else
ok = false;
}
}
if (ok)
{
m_current = i;
m_bCurrentIsKnown = true;
return true;
}
}
m_bCurrentIsKnown = false;
return false;
}
double Wire::voltage() const
{
return ( m_pStartPin->voltage() + m_pEndPin->voltage() )/2;
}
void Wire::setCurrentKnown( bool known )
{
m_bCurrentIsKnown = known;
if (!known)
m_current = 0.;
}
|