Beispiel #1
0
    def get_by_identifier(self, identifier, is_word=False):
        """
            Gets the value of a register, Flash, or RAM by its name, or address.

            For example, get_by_identifier('A') returns the value in the accumulator,
            and get_by_identifier(0x01) returns the value in ram[1].
        """

        #assume we're working with words for word-size registers
        if identifier in ('SP', 'PC'):
            is_word = True

        #if we've been instructed to retrieve the operator HX, then do so
        if identifier == 'HX':
            return self.get_HX()

        #if the identifier is an integer, use it as a memory address
        elif isinstance(identifier, int):

            #if the address is in RAM, return the appropriate value
            if identifier in self.ram:

                #if we've been asked to retrieve a word, do so
                if is_word:
                    return merge_word(self.ram[identifier],
                                      self.ram[identifier + 1])

                #otherwise, return a byte
                else:
                    return self.ram[identifier]

            #do the same for an address in flash
            elif identifier in self.flash:

                #if we've been asked to retrieve a word, do so
                if is_word:
                    return merge_word(self.flash[identifier],
                                      self.flash[identifier + 1])

                #otherwise, return a byte
                else:
                    return self.flash[identifier]

            #if the address was neither in flash nor RAM, throw an exception
            else:
                raise InvalidMemoryException(
                    'One of your instructions tried to read from address ' +
                    repr(identifier) +
                    ', which isn\'t a valid memory address.')

        #if the identifier is a register name, return the register's contents
        elif identifier in self.REGISTERS or identifier in self.FLAGS or identifier in self.EXPOSED_PROPERTIES:
            return self.__dict__[identifier]

        else:
            raise ExecutionException(
                'The CPU tried to read from a non-existant register ' +
                repr(identifier) +
                '. This is likely an issue with the simulator; please bring this error to an instructor\'s attention.'
            )
Beispiel #2
0
    def get_by_identifier(self, identifier, is_word=False):
        """
            Gets the value of a register, Flash, or RAM by its name, or address.

            For example, get_by_identifier('A') returns the value in the accumulator,
            and get_by_identifier(0x01) returns the value in ram[1].
        """

        #assume we're working with words for word-size registers
        if identifier in ('SP', 'PC'):
            is_word = True

        #if we've been instructed to retrieve the operator HX, then do so
        if identifier == 'HX':
            return self.get_HX()

        #if the identifier is an integer, use it as a memory address
        elif isinstance(identifier, int):

            #if the address is in RAM, return the appropriate value
            if identifier in self.ram:

                #if we've been asked to retrieve a word, do so
                if is_word:
                    return merge_word(self.ram[identifier], self.ram[identifier + 1])

                #otherwise, return a byte
                else:
                    return self.ram[identifier]

            #do the same for an address in flash
            elif identifier in self.flash:

                #if we've been asked to retrieve a word, do so
                if is_word:
                    return merge_word(self.flash[identifier], self.flash[identifier + 1])

                #otherwise, return a byte
                else:
                    return self.flash[identifier]

            #if the address was neither in flash nor RAM, throw an exception
            else:
                raise InvalidMemoryException('One of your instructions tried to read from address ' + repr(identifier) + ', which isn\'t a valid memory address.')

        #if the identifier is a register name, return the register's contents
        elif identifier in self.REGISTERS or identifier in self.FLAGS or identifier in self.EXPOSED_PROPERTIES:
            return self.__dict__[identifier]

        else:
            raise ExecutionException('The CPU tried to read from a non-existant register ' + repr(identifier) + '. This is likely an issue with the simulator; please bring this error to an instructor\'s attention.')
Beispiel #3
0
    def fetch_word(self):
        """
            Fetches the word at the address contained in the system's program counter, then adds two to the program counter, so it points past the end of the read word.
       """

        #fetch two bytes, then merge them into a word
        return merge_word(self.fetch_byte(), self.fetch_byte())
Beispiel #4
0
    def get_HX(self):
        """
            Returns the value of HX (the system indexing register) as a 16-bit word.
        """

        #merge H and X to form HX, the 16-bit indexing register
        return merge_word(self.H, self.X)
Beispiel #5
0
    def fetch_word(self):
       """
            Fetches the word at the address contained in the system's program counter, then adds two to the program counter, so it points past the end of the read word.
       """

       #fetch two bytes, then merge them into a word
       return merge_word(self.fetch_byte(), self.fetch_byte())
Beispiel #6
0
    def get_HX(self):
        """
            Returns the value of HX (the system indexing register) as a 16-bit word.
        """

        #merge H and X to form HX, the 16-bit indexing register
        return merge_word(self.H, self.X)
Beispiel #7
0
    def pull_word(self):
        """
            Pulls a word from the stack, and returns it.
        """

        #pull two bytes from the stack
        msb = self.pull_byte()
        lsb = self.pull_byte()

        #and merge them into a single word
        return merge_word(msb, lsb)
Beispiel #8
0
    def pull_word(self):
        """
            Pulls a word from the stack, and returns it.
        """

        #pull two bytes from the stack
        msb = self.pull_byte()
        lsb = self.pull_byte()

        #and merge them into a single word
        return merge_word(msb, lsb)