forked from gregjames/Sim2600
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuitSimulatorUsingSets.py
173 lines (148 loc) · 6.47 KB
/
circuitSimulatorUsingSets.py
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
# Copyright (c) 2014 Greg James, Visual6502.org
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from circuitSimulatorBase import CircuitSimulatorBase
from nmosFet import NmosFet
from wire import Wire
class CircuitSimulator(CircuitSimulatorBase):
def __init__(self):
CircuitSimulatorBase.__init__(self)
def doWireRecalc(self, wireIndex):
if wireIndex == self.gndWireIndex or wireIndex == self.vccWireIndex:
return
group = set()
self.addWireToGroup(wireIndex, group)
newValue = self.getWireValue(group)
newHigh = newValue == Wire.HIGH or newValue == Wire.PULLED_HIGH or \
newValue == Wire.FLOATING_HIGH
for groupWireIndex in group:
if groupWireIndex == self.gndWireIndex or \
groupWireIndex == self.vccWireIndex:
# TODO: remove gnd and vcc from group?
continue
simWire = self.wireList[groupWireIndex]
simWire.state = newValue
for transIndex in simWire.gateInds:
t = self.transistorList[transIndex]
if newHigh == True and t.gateState == NmosFet.GATE_LOW:
self.turnTransistorOn(t)
if newHigh == False and t.gateState == NmosFet.GATE_HIGH:
self.turnTransistorOff(t)
def turnTransistorOn(self, t):
t.gateState = NmosFet.GATE_HIGH
wireInd = t.side1WireIndex
if self.newRecalcArray[wireInd] == 0:
self.newRecalcArray[wireInd] = 1
self.newRecalcOrder[self.newLastRecalcOrder] = wireInd
self.newLastRecalcOrder += 1
wireInd = t.side2WireIndex
if self.newRecalcArray[wireInd] == 0:
self.newRecalcArray[wireInd] = 1
self.newRecalcOrder[self.newLastRecalcOrder] = wireInd
self.newLastRecalcOrder += 1
def turnTransistorOff(self, t):
t.gateState = NmosFet.GATE_LOW
c1Wire = t.side1WireIndex
c2Wire = t.side2WireIndex
self.floatWire(c1Wire)
self.floatWire(c2Wire)
wireInd = c1Wire
if self.newRecalcArray[wireInd] == 0:
self.newRecalcArray[wireInd] = 1
self.newRecalcOrder[self.newLastRecalcOrder] = wireInd
self.newLastRecalcOrder += 1
wireInd = c2Wire
if self.newRecalcArray[wireInd] == 0:
self.newRecalcArray[wireInd] = 1
self.newRecalcOrder[self.newLastRecalcOrder] = wireInd
self.newLastRecalcOrder += 1
def getWireValue(self, group):
# TODO PERF: why turn into a list?
l = list(group)
sawFl = False
sawFh = False
value = self.wireList[l[0]].state
for wireIndex in group:
if wireIndex == self.gndWireIndex:
return Wire.GROUNDED
if wireIndex == self.vccWireIndex:
if self.gndWireIndex in group:
return Wire.GROUNDED
else:
return Wire.HIGH
wire = self.wireList[wireIndex]
if wire.pulled == Wire.PULLED_HIGH:
value = Wire.PULLED_HIGH
elif wire.pulled == Wire.PULLED_LOW:
value = Wire.PULLED_LOW
if wire.state == Wire.FLOATING_LOW:
sawFl = True
elif wire.state == Wire.FLOATING_HIGH:
sawFh = True
if value == Wire.FLOATING_LOW or value == Wire.FLOATING_HIGH:
# If two floating regions are connected together,
# set their voltage based on whichever region has
# the most components. The resulting voltage should
# be determined by the capacitance of each region.
# Instead, we use the count of the number of components
# in each region as an estimate of how much charge
# each one holds, and set the result hi or low based
# on which region has the most components.
if sawFl and sawFh:
sizes = self.countWireSizes(group)
if sizes[1] < sizes[0]:
value = Wire.FLOATING_LOW
else:
value = Wire.FLOATING_HIGH
return value
def addWireToGroup(self, wireIndex, group):
self.numAddWireToGroup += 1
group.add(wireIndex)
wire = self.wireList[wireIndex]
if wireIndex == self.gndWireIndex or wireIndex == self.vccWireIndex:
return
for t in wire.ctInds:
self.addWireTransistor (wireIndex, t, group)
def addWireTransistor(self, wireIndex, t, group):
self.numAddWireTransistor += 1
other = -1
trans = self.transistorList[t]
if trans.gateState == NmosFet.GATE_LOW:
return
if trans.side1WireIndex == wireIndex:
other = trans.side2WireIndex
if trans.side2WireIndex == wireIndex:
other = trans.side1WireIndex
if other == self.vccWireIndex or other == self.gndWireIndex:
group.add(other)
return
if other in group:
return
self.addWireToGroup(other, group)
def countWireSizes(self, group):
countFl = 0
countFh = 0
for i in group:
wire = self.wireList[i]
num = len(wire.ctInds) + len(wire.gateInds)
if wire.state == Wire.FLOATING_LOW:
countFl += num
if wire.state == Wire.FLOATING_HIGH:
countFh += num
return [countFl, countFh]