Skip to content

Commit

Permalink
[Sim][#51] Have a class for node groups.
Browse files Browse the repository at this point in the history
This should simplify computing and accessing dependencies between
nodes.
  • Loading branch information
kosarev committed Jun 12, 2023
1 parent 00491f0 commit 34d8957
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions tests/z80sim/z80sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,14 @@ def is_pin(self):
return self.custom_id in _PINS


class NodeGroup(object):
def __init__(self, nodes):
self.__nodes = tuple(sorted(nodes))

def __iter__(self):
yield from self.__nodes


class Transistor(object):
def __init__(self, index, gate, c1, c2):
self.index, self.gate, self.c1, self.c2 = index, gate, c1, c2
Expand Down Expand Up @@ -1296,19 +1304,19 @@ def get_node_states(self, ids=None):
return {id: self.__nodes_by_name[id].state for id in ids}

def __identify_group_of(self, n):
group = []
nodes = []
worklist = [n]
while worklist:
n = worklist.pop()
if n.is_gnd_or_pwr or n in group:
if n.is_gnd_or_pwr or n in nodes:
continue

group.append(n)
nodes.append(n)

for t in n.conn_of:
worklist.append(t.get_other_conn(n))

return sorted(group)
return NodeGroup(nodes)

def __identify_groups(self):
for n in self.__nodes.values():
Expand Down Expand Up @@ -1443,7 +1451,7 @@ def __update_groups_of(self, nodes, *, shuffle=True):
round += 1
assert round < 100, 'Loop encountered!'

nodes = sum(groups, start=[])
nodes = sum((tuple(g) for g in groups), start=())
assert len(nodes) == len(set(nodes))

if shuffle:
Expand Down

0 comments on commit 34d8957

Please sign in to comment.