Example #1
0
    def write(self, address, byte):
        """Write a byte to cache."""

        written = self.cache.write(address, byte)

        if written:
            self.hits += 1
        else:
            self.misses += 1

        if self.write_policy == Cache.WRITE_THROUGH:
            # Write block to memory
            block = self.memory.get_block(address)

            block[self.cache.get_offset(address)] = byte

            self.memory.set_block(address, block)

            print()
            print("Byte 0x%s (%s) written to block %s @ %s in main memory\n" %
                  (util.hex_str(byte, 2), byte, self.memory.get_block(address),
                   util.bin_str(address, self.memory_size)))

        elif self.write_policy == Cache.WRITE_BACK:
            if not written:
                # Write block to cache
                block = self.memory.get_block(address)

                victim = self.cache.load(address, block)
                written = self.cache.write(address, byte)

            print()
            print("Byte 0x%s (%s) written @ %s in cache\n" % (util.hex_str(
                byte, 2), byte, util.bin_str(address, self.memory_size)))
Example #2
0
    def print_section(self, start, amount):
        """
        Print a section of the cache.

        :param int start: start address to print from.
        :param int amount: amount of lines to print.
        """

        line_len = len(str(self.size // self.block_size - 1))
        use_len = max([len(str(line.use)) for line in self.lines])
        tag_len = int(
            log((self.mapping_policy * self.memory_size) // self.size, 2))
        address_len = int(log(self.memory_size, 2))

        if start < 0 or (start + amount) > (self.size // self.block_size):
            raise IndexError

        print("\n" + " " * line_len + " " * use_len + "  U M V  T" +
              " " * tag_len + "<DATA @ ADDRESS>")
        for i in range(start, start + amount):
            print("%s : %s %s %s %s <%s @ %s>" %
                  (util.dec_str(
                      i, line_len), util.dec_str(self.lines[i].use, use_len),
                   util.bin_str(self.lines[i].modified,
                                1), util.bin_str(self.lines[i].valid, 1),
                   util.bin_str(self.lines[i].tag, tag_len), " ".join(
                       [util.hex_str(j, 2) for j in self.lines[i].data]),
                   util.bin_str(self.get_physical_address(i), address_len)))
        print()
Example #3
0
    def print_section(self, start, amount):
        """
        Print a section of main memory.

        :param int start: start address to print from.
        :param int amount: amount of blocks to print.
        """

        address_length = len(str(self.size - 1))
        start = start - (start % self.block_size)
        amount *= self.block_size

        if start < 0 or (start + amount) > self.size:
            raise IndexError

        print()
        for i in range(start, start + amount, self.block_size):
            print(
                '%s:%s' %
                (
                    util.dec_str(i, address_length),
                    " ".join(
                        [
                            util.hex_str(j, 2) for j in self.get_block(i)
                        ]
                    )
                )
            )
        print()
Example #4
0
    def print_section(self, start, amount):
        """Print a section of the cache.

        :param int start: start address to print from
        :param int amount: amount of lines to print
        """
        line_len = len(str(self._size // self._block_size - 1))
        use_len = max([len(str(i.use)) for i in self._lines])
        tag_len = int(log(self._mapping_pol * self._mem_size // self._size, 2))
        address_len = int(log(self._mem_size, 2))

        if start < 0 or (start + amount) > (self._size // self._block_size):
            raise IndexError

        print("\n" + " " * line_len + " " * use_len + " U M V T" +
              " " * tag_len + "<DATA @ ADDRESS>")

        for i in range(start, start + amount):
            print(
                util.dec_str(i, line_len) + ": " +
                util.dec_str(self._lines[i].use, use_len) + " " +
                util.bin_str(self._lines[i].modified, 1) + " " +
                util.bin_str(self._lines[i].valid, 1) + " " +
                util.bin_str(self._lines[i].tag, tag_len) + " <" +
                " ".join([util.hex_str(i, 2)
                          for i in self._lines[i].data]) + " @ " +
                util.bin_str(self.get_physical_address(i), address_len) + ">")
        print()
    def print_section(self, start, amount):
        """Print a section of the cache.

        :param int start: start address to print from
        :param int amount: amount of lines to print
        """
        line_len = len(str(self._size // self._block_size - 1))
        use_len = max([len(str(i.use)) for i in self._lines])
        tag_len = int(log(self._mapping_pol * self._mem_size // self._size, 2))
        address_len = int(log(self._mem_size, 2))

        if start < 0 or (start + amount) > (self._size // self._block_size):
            raise IndexError

        print("\n" + " " * line_len + " " * use_len + " U M V T" +
              " " * tag_len + "<DATA @ ADDRESS>")

        for i in range(start, start + amount):
            print(util.dec_str(i, line_len) + ": " +
                  util.dec_str(self._lines[i].use, use_len) + " " +
                  util.bin_str(self._lines[i].modified, 1) + " " +
                  util.bin_str(self._lines[i].valid, 1) + " " +
                  util.bin_str(self._lines[i].tag, tag_len) + " <" +
                  " ".join([util.hex_str(i, 2) for i in self._lines[i].data]) + " @ " +
                  util.bin_str(self.get_physical_address(i), address_len) + ">")
        print()
    def print_section(self, start, amount):
        """Print a section of main memory.

        :param int start: start address to print from
        :param int amount: amount of blocks to print
        """
        address_len = len(str(self._size - 1))
        start = start - (start % self._block_size)
        amount *= self._block_size

        if start < 0 or (start + amount) > self._size:
            raise IndexError

        print()
        for i in range(start, start + amount, self._block_size):
            print(util.dec_str(i, address_len) + ": " +
                  " ".join([util.hex_str(i, 2) for i in self.get_block(i)]))
        print()
Example #7
0
command = None

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

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

        if command == "read" and len(params) == 1:
            address = int(params[0], 0)
            byte = read(address, memory, cache)

            print("\nByte 0x" + util.hex_str(byte, 2) + " read from " +
                  util.bin_str(address, args.MEMORY) + "\n")

        elif command == "write" and len(params) == 2:
            address = int(params[0], 0)
            byte = int(params[1], 0)

            write(address, byte, memory, cache)

            print("\nByte 0x" + util.hex_str(byte, 2) + " written to " +
                  util.bin_str(address, args.MEMORY) + "\n")

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

            for i in range(amount):
command = None

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

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

        if command == "read" and len(params) == 1:
            address = int(params[0])
            byte = read(address, memory, cache)

            print("\nByte 0x" + util.hex_str(byte, 2) + " read from " +
                  util.bin_str(address, args.MEMORY) + "\n")

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

            write(address, byte, memory, cache)

            print("\nByte 0x" + util.hex_str(byte, 2) + " written to " +
                  util.bin_str(address, args.MEMORY) + "\n")

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

            for i in range(amount):
Example #9
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)