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
| import re from kiutils.board import Board
def get_logic(): board = Board.from_file('smart-brick-v2.kicad_pcb')
components = {} for fp in board.footprints: ref = fp.properties.get('Reference', '') val = fp.properties.get('Value', '')
pins = {} for pad in fp.pads: if pad.net: pins[pad.number] = pad.net.name
components[ref] = { 'value': val, 'pins': pins }
gate_defs = { '74LS00': [(('1', '2'), '3'), (('4', '5'), '6'), (('9', '10'), '8'), (('12', '13'), '11')], '74LS02': [(('2', '3'), '1'), (('5', '6'), '4'), (('8', '9'), '10'), (('11', '12'), '13')], '74LS04': [(('1',), '2'), (('3',), '4'), (('5',), '6'), (('9',), '8'), (('11',), '10'), (('13',), '12')], '74LS08': [(('1', '2'), '3'), (('4', '5'), '6'), (('9', '10'), '8'), (('12', '13'), '11')], '74LS20': [(('1', '2', '4', '5'), '6'), (('9', '10', '12', '13'), '8')], '74LS21': [(('1', '2', '4', '5'), '6'), (('9', '10', '12', '13'), '8')], '74LS27': [(('1', '2', '13'), '12'), (('3', '4', '5'), '6'), (('9', '10', '11'), '8')], '74LS32': [(('1', '2'), '3'), (('4', '5'), '6'), (('9', '10'), '8'), (('12', '13'), '11')], '74LS86': [(('1', '2'), '3'), (('4', '5'), '6'), (('9', '10'), '8'), (('12', '13'), '11')], }
net_logic = {}
for ref, comp in components.items(): val = comp['value'] if val in gate_defs: for inputs, output in gate_defs[val]: if output in comp['pins']: out_net = comp['pins'][output] in_nets = [comp['pins'][i] for i in inputs if i in comp['pins']] if len(in_nets) == len(inputs): op = val[4:] net_logic[out_net] = (op, in_nets)
led_nets = [] for i in range(1, 20): ref = f'Q{i}' if ref in components: if '1' in components[ref]['pins']: led_nets.append(components[ref]['pins']['1'])
def simulate(inputs_bits): vals = {f'/IN{i}': inputs_bits[i] for i in range(7)} vals['GND'] = False vals['+5V'] = True
memo = {} def get_val(net): if net in vals: return vals[net] if net in memo: return memo[net] if net not in net_logic: return False
op, ins = net_logic[net] in_vals = [get_val(i) for i in ins]
if op == '00': return not (in_vals[0] and in_vals[1]) elif op == '02': return not (in_vals[0] or in_vals[1]) elif op == '04': return not in_vals[0] elif op == '08': return in_vals[0] and in_vals[1] elif op == '20': return not all(in_vals) elif op == '21': return all(in_vals) elif op == '27': return not any(in_vals) elif op == '32': return any(in_vals) elif op == '86': return in_vals[0] ^ in_vals[1] else: return False
memo[net] = res return res
return [get_val(ln) for ln in led_nets]
for i in range(19): print(f"LED {i+1}: ", end='') for code in range(128): bits = [(code >> j) & 1 == 1 for j in range(7)] outputs = simulate(bits) if outputs[i]: print(f"'{chr(code)}' (0x{code:02x})", end=' ') print()
get_logic()
|