import pickle # use with logic gate simulator (https://www.kolls.net/gatesim/) # wpf rip linux with open("circuit.pkl", "rb") as f: g = pickle.load(f) of = open("circuit.gcg", "w") of.write("""<?xml version="1.0" encoding="utf-8"?> <CircuitGroup Version="1.2"> <Circuit> <Gates> """) gateXDist = 120 gateYDist = 60 lowerGateType = { "XOR": "Xor", "AND": "And", "NOT": "Not" } gateCount = len(g['gates']) netCount = len(g['netlist']) def getGateType(gateIdx): return g['gates'][gateIdx]['op'][0] def getXRefs(gateIdx): xRefs = [] for netIdx in range(netCount): net = g['netlist'][netIdx] if gateIdx in net: xRefs.append(netIdx) return xRefs def getMinDepth(gateIdx, curDepth): if gateIdx >= 1821: return curDepth gateType = getGateType(gateIdx) xRefs = getXRefs(gateIdx) if gateType == "AND" or gateType == "XOR": return max(getMinDepth(xRefs[0], curDepth + 1), getMinDepth(xRefs[1], curDepth + 1)) elif gateType == "NOT": return getMinDepth(xRefs[0], curDepth + 1) depthDict = {} for i in range(1821): print(i) depth = getMinDepth(i, 0) if depth not in depthDict: depthDict[depth] = [] depthDict[depth].append(i) xPos = 200 for k, v in sorted(depthDict.items()): yPos = 100 for gateIdx in v: gateType = lowerGateType[getGateType(gateIdx)] if gateType != "Not": extra = ' NumInputs="2"' else: extra = '' of.write(f' <Gate Type="{gateType}" Name="{gateType}" ID="{gateIdx+1}"{extra}>\n') of.write(f' <Point X="{xPos}" Y="{yPos}" Angle="0" />\n') of.write(f' </Gate>\n') yPos += gateYDist xPos += gateXDist for i in range(64): of.write(f' <Gate Type="UserInput" Name="UserInput" ID="{1821+1+i}">') of.write(f' <Point X="100" Y="{100+60*i}" Angle="0" />') of.write(f' </Gate>') of.write(""" </Gates> <Wires> """) for i in range(1821): print(i) gateType = getGateType(i) xRefs = getXRefs(i) for j in range(len(xRefs)): xRef = xRefs[j] of.write(f' <Wire>\n') of.write(f' <From ID="{xRef+1}" Port="0" />\n') of.write(f' <To ID="{i+1}" Port="{j}" />\n') of.write(f' </Wire>\n') of.write(""" </Wires> </Circuit> </CircuitGroup> """) of.close()