Пример #1
0
 def from_order(order):
     """
     >>> x = Value.from_order(('R', 16, 'S'))
     >>> x.as_pretty_bits_string()
     '00100 0 0000010000 0'
     >>> x = Value.from_order(('T', 11, 'L'))
     >>> x.as_pretty_bits_string()
     '00101 0 0000001011 1'
     """
     op, addr, sl = order
     assert isinstance(op, str) and len(op) == 1
     assert isinstance(addr, int)
     assert sl in ['S', 'L']
     if sl == 'S':
         sl = [0]
     else:
         sl = [1]
     if addr < 2**10:
         # 5 bits op
         op_bit = integer_to_bits(io.ascii_to_edsac(op), 5)
         # 1 bit unused
         unused_bit = [0]
         # 10 bits address
         addr_bit = integer_to_bits(addr, 10)
         # 1 bit S/L
         result = op_bit + unused_bit + addr_bit + sl
     else:
         # such as "P10000S"
         if op != "P":
             raise NotImplementedError(
                 "I don't know how to put %s in bits" % order)
         # 16 bits
         result = integer_to_bits(addr, 16) + sl
     return Value(result)
Пример #2
0
 def from_order(order):
     """
     >>> x = Value.from_order(('R', 16, 'S'))
     >>> x.as_pretty_bits_string()
     '00100 0 0000010000 0'
     >>> x = Value.from_order(('T', 11, 'L'))
     >>> x.as_pretty_bits_string()
     '00101 0 0000001011 1'
     """
     op, addr, sl = order
     assert isinstance(op, str) and len(op) == 1
     assert isinstance(addr, int)
     assert sl in ['S', 'L']
     if sl == 'S':
         sl = [0]
     else:
         sl = [1]
     if addr < 2 ** 10:
         # 5 bits op
         op_bit = integer_to_bits(io.ascii_to_edsac(op), 5)
         # 1 bit unused
         unused_bit = [0]
         # 10 bits address
         addr_bit = integer_to_bits(addr, 10)
         # 1 bit S/L
         result = op_bit + unused_bit + addr_bit + sl
     else:
         # such as "P10000S"
         if op != "P":
             raise NotImplementedError(
                 "I don't know how to put %s in bits" % order)
         # 16 bits
         result = integer_to_bits(addr, 16) + sl
     return Value(result)
Пример #3
0
    def step(self):
        assert MIN_MEMORY_ADDR <= self.sequence_control < MAX_MEMORY_ADDR
        instr = self.get_memory(self.sequence_control)
        # debug
        if SHOW_RUNNNING_INSTRUCTION:
            print self.sequence_control, instr.as_order()

        op, addr, sl = instr.as_order()
        wide = (sl == "L")

        if op == "T":
            # TnS: m[n]=A; ABC=0
            # TnL: w[n]=AB; ABC=0
            a = self.get_accumulator(wide)
            self.set_memory(addr, a, wide)
            self.clear_accumulator()
        elif op == "H":
            # HnS: R += m[n]
            # HnL: RS += w[n]
            m = self.get_memory(addr, wide)
            r = self.get_multiplier(wide)
            r = m
            self.set_multiplier(r, wide)

        elif op == "E":
            # if A >= 0 goto n
            a = self.get_accumulator()
            if not a.is_negative():  # A >= 0
                self.sequence_control = addr - 1
        elif op == "G":
            # if A < 0 goto n
            a = self.get_accumulator()
            if a.is_negative():  # A < 0
                self.sequence_control = addr - 1

        elif op == "I":
            #  Place the next paper tape character
            #  in the *least* significant 5 bits of m[n].
            c = self.cards[self.next_char]
            self.next_char += 1
            v = io.ascii_to_edsac(c)
            self.set_memory(addr, Value.new_from_number(v))
            if DEBUG_IO:
                print "read", c, v

        elif op == "A":
            # AnS: A += m[n]
            # AnL: AB += w[n]
            m = self.get_memory(addr, wide)
            r = self.get_accumulator(wide)
            r = r + m
            self.set_accumulator(r, wide)
        elif op == "S":
            m = self.get_memory(addr, wide)
            r = self.get_accumulator(wide)
            r = r - m
            self.set_accumulator(r, wide)

        elif op == "V":
            m = self.get_memory(addr, wide)
            r = self.get_multiplier(wide)
            v = m.multiply(r)
            if wide:
                a = self.accumulator
            else:
                a = self.get_accumulator(wide=True)
            a.set(a + v)

        elif op == "N":
            m = self.get_memory(addr, wide)
            r = self.get_multiplier(wide)
            v = m.as_integer() * r.as_integer()
            v = m.multiply(r)
            if wide:
                a = self.accumulator
            else:
                a = self.get_accumulator(wide=True)
            a.set(a - v)

        elif op == "R":
            # Shift right
            num_shift = _calc_num_shift(instr)
            v = self.accumulator.as_integer()
            v = v >> num_shift
            self.accumulator.set_from_number(v)

        elif op == "L":
            # Shift left
            num_shift = _calc_num_shift(instr)
            v = self.accumulator.as_unsigned()
            v = v << num_shift
            self.accumulator.set_from_number(v)

        elif op == "U":
            # UnS: m[n]=A
            # UnL: w[n]=AB
            self.set_memory(addr, self.get_accumulator(wide), wide)

        elif op == "C":
            raise NotImplementedError
        elif op == "Y":
            raise NotImplementedError

        elif op == "O":
            # output
            if DEBUG_IO:
                code = self.get_memory(addr).as_charcode()
                print "output %s %s %s" % (
                    io.edsac_to_letter(code), io.edsac_to_figure(code), code)
            else:
                sys.stdout.write(
                    self.output(
                    self.get_memory(addr).as_charcode()))

        elif op == "X":
            pass  # no operation
        elif op == "F":
            raise NotImplementedError("Verify the last character"
                                      "output. What?")
        elif op == "Z":
            # finish
            return True
        else:
            raise AssertionError("Malformed Instruction:", instr.as_order())

        self.sequence_control += 1
        return False  # not finished
Пример #4
0
    def step(self):
        assert MIN_MEMORY_ADDR <= self.sequence_control < MAX_MEMORY_ADDR
        instr = self.get_memory(self.sequence_control)
        # debug
        if SHOW_RUNNNING_INSTRUCTION:
            print self.sequence_control, instr.as_order()

        op, addr, sl = instr.as_order()
        wide = (sl == "L")

        if op == "T":
            # TnS: m[n]=A; ABC=0
            # TnL: w[n]=AB; ABC=0
            a = self.get_accumulator(wide)
            self.set_memory(addr, a, wide)
            self.clear_accumulator()
        elif op == "H":
            # HnS: R += m[n]
            # HnL: RS += w[n]
            m = self.get_memory(addr, wide)
            r = self.get_multiplier(wide)
            r = m
            self.set_multiplier(r, wide)

        elif op == "E":
            # if A >= 0 goto n
            a = self.get_accumulator()
            if not a.is_negative():  # A >= 0
                self.sequence_control = addr - 1
        elif op == "G":
            # if A < 0 goto n
            a = self.get_accumulator()
            if a.is_negative():  # A < 0
                self.sequence_control = addr - 1

        elif op == "I":
            #  Place the next paper tape character
            #  in the *least* significant 5 bits of m[n].
            c = self.cards[self.next_char]
            self.next_char += 1
            v = io.ascii_to_edsac(c)
            self.set_memory(addr, Value.new_from_number(v))
            if DEBUG_IO:
                print "read", c, v

        elif op == "A":
            # AnS: A += m[n]
            # AnL: AB += w[n]
            m = self.get_memory(addr, wide)
            r = self.get_accumulator(wide)
            r = r + m
            self.set_accumulator(r, wide)
        elif op == "S":
            m = self.get_memory(addr, wide)
            r = self.get_accumulator(wide)
            r = r - m
            self.set_accumulator(r, wide)

        elif op == "V":
            m = self.get_memory(addr, wide)
            r = self.get_multiplier(wide)
            v = m.multiply(r)
            if wide:
                a = self.accumulator
            else:
                a = self.get_accumulator(wide=True)
            a.set(a + v)

        elif op == "N":
            m = self.get_memory(addr, wide)
            r = self.get_multiplier(wide)
            v = m.as_integer() * r.as_integer()
            v = m.multiply(r)
            if wide:
                a = self.accumulator
            else:
                a = self.get_accumulator(wide=True)
            a.set(a - v)

        elif op == "R":
            # Shift right
            num_shift = _calc_num_shift(instr)
            v = self.accumulator.as_integer()
            v = v >> num_shift
            self.accumulator.set_from_number(v)

        elif op == "L":
            # Shift left
            num_shift = _calc_num_shift(instr)
            v = self.accumulator.as_unsigned()
            v = v << num_shift
            self.accumulator.set_from_number(v)

        elif op == "U":
            # UnS: m[n]=A
            # UnL: w[n]=AB
            self.set_memory(addr, self.get_accumulator(wide), wide)

        elif op == "C":
            raise NotImplementedError
        elif op == "Y":
            raise NotImplementedError

        elif op == "O":
            # output
            if DEBUG_IO:
                code = self.get_memory(addr).as_charcode()
                print "output %s %s %s" % (io.edsac_to_letter(code),
                                           io.edsac_to_figure(code), code)
            else:
                sys.stdout.write(
                    self.output(self.get_memory(addr).as_charcode()))

        elif op == "X":
            pass  # no operation
        elif op == "F":
            raise NotImplementedError("Verify the last character"
                                      "output. What?")
        elif op == "Z":
            # finish
            return True
        else:
            raise AssertionError("Malformed Instruction:", instr.as_order())

        self.sequence_control += 1
        return False  # not finished