Exemplo n.º 1
0
    def __init__(self, size, block_size):
        """Initialize main memory with a set number of bytes and block size."""

        self.size = size
        self.block_size = block_size

        self.data = [util.rand_byte() for _ in range(size)]
Exemplo n.º 2
0
        elif command == "randread" and len(params) == 1:
            amount = int(params[0], 0)

            for i in range(amount):
                address = random.randint(0, mem_size - 1)
                read(address, memory, cache)

            print("\n" + str(amount) + " bytes read from memory\n")

        elif command == "randwrite" and len(params) == 1:
            amount = int(params[0], 0)

            for i in range(amount):
                address = random.randint(0, mem_size - 1)
                byte = util.rand_byte()
                write(address, byte, memory, cache)

            print("\n" + str(amount) + " bytes written to memory\n")

        elif command == "printcache" and len(params) == 2:
            start = int(params[0], 0)
            amount = int(params[1], 0)

            cache.print_section(start, amount)

        elif command == "printmem" and len(params) == 2:
            start = int(params[0], 0)
            amount = int(params[1], 0)

            memory.print_section(start, amount)
Exemplo n.º 3
0
        elif command == "randread" and len(params) == 1:
            amount = int(params[0])

            for i in range(amount):
                address = random.randint(0, mem_size - 1)
                read(address, memory, cache)

            print("\n" + str(amount) + " bytes read from memory\n")

        elif command == "randwrite" and len(params) == 1:
            amount = int(params[0])

            for i in range(amount):
                address = random.randint(0, mem_size - 1)
                byte = util.rand_byte()
                write(address, byte, memory, cache)

            print("\n" + str(amount) + " bytes written to memory\n")

        elif command == "printcache" and len(params) == 2:
            start = int(params[0])
            amount = int(params[1])

            cache.print_section(start, amount)

        elif command == "printmem" and len(params) == 2:
            start = int(params[0])
            amount = int(params[1])

            memory.print_section(start, amount)
Exemplo n.º 4
0
 def __init__(self, size, block_size):
     """Initialize main memory with a set number of bytes and block size."""
     self._size = size  # Memory size
     self._block_size = block_size  # Block size
     self._data = [util.rand_byte() for i in range(size)]
Exemplo n.º 5
0
    def run(self):
        command = None

        self.print_details()

        while command != 'quit':
            operation = input("Enter a command > ")
            operation = operation.split()

            try:
                command = operation[0]
                params = operation[1:]

                if command == 'write' and len(params) == 2:
                    address = int(params[0])
                    byte = params[1]

                    # Make sure byte is a digit
                    if byte.isdigit():
                        byte = int(params[1])

                    self.write(address, byte)

                elif command == 'read' and len(params) == 1:
                    address = int(params[0])
                    byte = self.read(address)

                    print("\nByte 0x%s (%s) read from %s in cache\n" %
                          (util.hex_str(byte, 2), byte,
                           util.bin_str(address, self.memory_size)))

                elif command == "randread" and len(params) == 1:
                    amount = int(params[0])

                    for i in range(amount):
                        address = random.randint(0, self.memory.get_size() - 1)
                        self.read(address)

                    print("\n%s bytes read from memory\n" % amount)

                elif command == "randwrite" and len(params) == 1:
                    amount = int(params[0])

                    for i in range(amount):
                        address = random.randint(0, self.memory.get_size() - 1)
                        byte = util.rand_byte()
                        self.write(address, byte)

                elif command == "printcache" and len(params) == 2:
                    start = int(params[0])
                    amount = int(params[1])

                    self.cache.print_section(start, amount)

                elif command == "printmem" and len(params) == 2:
                    start = int(params[0])
                    amount = int(params[1])

                    self.memory.print_section(start, amount)

                elif command == "stats" and len(params) == 0:
                    ratio = (self.hits / (
                        (self.hits + self.misses) if self.misses else 1)) * 100

                    print("\nHits: {0} | Misses: {1}".format(
                        self.hits, self.misses))
                    print("Hit/Miss Ratio: {0:.2f}%".format(ratio) + "\n")

                elif command == 'help':
                    self.print_details()

                elif command != 'quit':
                    print(INVALID_RESPONSE)
            except IndexError:
                print(OUT_OF_BOUNDS_ERROR)
            except:
                print(INCORRECT_SYNTAX_ERROR)