Skip to content

Memory

Memory Component Provides Immediate and Data Memory to SIM component

Memory

Source code in sim/memory.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Memory:
    def __init__(self):
        self.INSTRUCTION_MEMORY_SIZE = 512
        self.DATA_MEMORY_SIZE = 512
        self.reset()

    def reset(self):
        self.instruction_memory = [0] * (self.INSTRUCTION_MEMORY_SIZE // 4)
        self.data_memory = [0] * (self.DATA_MEMORY_SIZE // 4)

    def read_instruction_memory(self):
        return self.instruction_memory

    def read_data_memory(self):
        return self.data_memory

    def load_instruction(self, address, instruction):
        if 0 <= address < len(self.instruction_memory):
            self.instruction_memory[address] = instruction
        else:
            raise ValueError(f"Invalid instruction memory address: {address}")

    def get_instruction(self, pc):
        """Get instruction at word-aligned PC address"""
        index = pc // 4  # Convert PC to array index
        if 0 <= index < len(self.instruction_memory):
            return self.instruction_memory[index]
        return 0

    def write_word(self, address, value):
        """Write a word to data memory at word-aligned address"""
        if (address % 4) != 0:
            raise ValueError(f"Memory Access Error: Unaligned adress {address}")
        if 0 <= address < len(self.data_memory) * 4:  # Check byte address
            word_index = address // 4  # Convert byte address to word index
            self.data_memory[word_index] = value & 0xFFFFFFFF
        else:
            raise ValueError(f"Memory Access Error: Invalid adress {address}")

    def read_word(self, address):
        """Read a word from data memory at word-aligned address"""
        if (address % 4) != 0:
            raise ValueError(f"Memory Access Error: Unaligned adress {address}")
        if 0 <= address < len(self.data_memory) * 4:  # Check byte address
            word_index = address // 4  # Convert byte address to word index
            return self.data_memory[word_index]
        else:
            raise ValueError(f"Memory Access Error: Invalid adress {address}")

get_instruction(pc)

Get instruction at word-aligned PC address

Source code in sim/memory.py
28
29
30
31
32
33
def get_instruction(self, pc):
    """Get instruction at word-aligned PC address"""
    index = pc // 4  # Convert PC to array index
    if 0 <= index < len(self.instruction_memory):
        return self.instruction_memory[index]
    return 0

read_word(address)

Read a word from data memory at word-aligned address

Source code in sim/memory.py
45
46
47
48
49
50
51
52
53
def read_word(self, address):
    """Read a word from data memory at word-aligned address"""
    if (address % 4) != 0:
        raise ValueError(f"Memory Access Error: Unaligned adress {address}")
    if 0 <= address < len(self.data_memory) * 4:  # Check byte address
        word_index = address // 4  # Convert byte address to word index
        return self.data_memory[word_index]
    else:
        raise ValueError(f"Memory Access Error: Invalid adress {address}")

write_word(address, value)

Write a word to data memory at word-aligned address

Source code in sim/memory.py
35
36
37
38
39
40
41
42
43
def write_word(self, address, value):
    """Write a word to data memory at word-aligned address"""
    if (address % 4) != 0:
        raise ValueError(f"Memory Access Error: Unaligned adress {address}")
    if 0 <= address < len(self.data_memory) * 4:  # Check byte address
        word_index = address // 4  # Convert byte address to word index
        self.data_memory[word_index] = value & 0xFFFFFFFF
    else:
        raise ValueError(f"Memory Access Error: Invalid adress {address}")