示例#1
0
def tabulate(in_wires, out_wires):
    sim = logsim.Sim()
    for values in truth_table(in_wires):
        for wire, value in zip(in_wires, values):
            sim.initialize(wire, value)
        sim.ticktock()
        print ' '.join(map(str,
                           wire_values(in_wires) + wire_values(out_wires)))


def wire_values(wires):
    return tuple(wire.value for wire in wires)


def truth_table(wires):
    return product(*[(0, 1)] * len(wires))


if __name__ == '__main__':
    a = logsim.Wire()
    b = logsim.Wire()
    tabulate([a, b], [logsim.nand(a, b)])
    print ''
    tabulate([a, b], [a & b])
    print ''
    tabulate([a, b], [a | b])
    print ''
    tabulate([a, b], [a ^ b])
    print ''
示例#2
0
文件: gates.py 项目: fuath/logsim
def xor(a, b):
    return nand(nand(a, ~b),
                nand(~a, b))
示例#3
0
文件: gates.py 项目: fuath/logsim
def or_(a, b):
    return nand(~a, ~b)
示例#4
0
文件: gates.py 项目: fuath/logsim
def and_(a, b):
    return ~nand(a, b)
示例#5
0
文件: gates.py 项目: fuath/logsim
def not_(a):
    return nand(a, a)
示例#6
0
文件: tabulate.py 项目: darius/logsim
from itertools import product

import logsim

def tabulate(in_wires, out_wires):
    sim = logsim.Sim()
    for values in truth_table(in_wires):
        for wire, value in zip(in_wires, values):
            sim.initialize(wire, value)
        sim.ticktock()
        print ' '.join(map(str, wire_values(in_wires) + wire_values(out_wires)))

def wire_values(wires):
    return tuple(wire.value for wire in wires)

def truth_table(wires):
    return product(*[(0, 1)]*len(wires))


if __name__ == '__main__':
    a = logsim.Wire()
    b = logsim.Wire()
    tabulate([a, b], [logsim.nand(a, b)])
    print ''
    tabulate([a, b], [a & b])
    print ''
    tabulate([a, b], [a | b])
    print ''
    tabulate([a, b], [a ^ b])
    print ''