Comming soon - C++ Implementation with bindings in Python - Expanded Architecture: Unified Data and Instruction Memory. L1, L2, L3 caching.…
Comming soon - C++ Implementation with bindings in Python - Expanded Architecture: Unified Data and Instruction Memory. L1, L2, L3 caching.…
This is a basic RISC-V emulator written in Python. It emulates a single cycle data path system.
ADD
ADD XA, XB, XCXB and XC and stores the sum in XASUB
SUB XA, XB, XCXC from XB and stores the difference in XALDUR
LDUR XA, [XB, IMM]XB + IMM into register XALDUR X1, [X2, #24] loads data from memory address X2 + 24 into register X1STUR
STUR XA, [XB, IMM]XA into memory address XB + IMMSTUR X1, [X2, #24] stores the data in X1 into memory address X2 + 24ADDI
ADDI XA, XB, IMMXB and IMM and stores the sum in XASUBI
SUBI XA, XB, IMMIMM from XB and stores the difference in XAB
B IMM4 * IMMB #28 sets the program counter to PC + 4*28CBZ
CBZ XA, IMM4 * IMM if the content of XA is 0, else moves to next instructionCBZ X1, #8 sets the program counter to PC + 4*8 if X1 is 0, else it moves the program counter to the next instructionCBNZ
CBNZ XA, IMM4 * IMM if the content of XA is not 0, else moves to next instructionCBNZ X1, #8 sets the program counter to PC + 4*8 if X1 is not 0, else it moves the program counter to the next instructiongit clone [URL]cd src./main.py. (Probably will need to do chmod u+x main.py first). This is a sample program.Basic Usage Example:
…Basic Grading Example:
from cpu import CPU
code1 = [
# ... RISC-V Code here
]
code2 = [
# ... RISC-V Code here
]
cpu1 = CPU('Solution', code1)
cpu2 = CPU('Student Response', code2)
cpu1.run()
cpu2.run()
# CPU state equality
print(cpu1 == cpu2)
__init__(self, id, code, reg_config='', mem_config='', randomize=True)Instantiates a CPU object. By default, initializes the CPU to random values. This can be overridden to set specific values in registers and data memory.
id: Identifier for the CPU object (use for debugging).code: Array of strings, each containing a line of ARM source code.reg_config: String containing configuration for registersmem_config: String containing configuration for data memoryrandomize: Boolean flag used to enable/disable randomizing non-configured CPU values.Example1:
code = [
"ADDI X2,X12,#16",
"STUR X2, [X1, #5]",
]
cpu1 = CPU("first cpu", code)Example2:
code = [
"ADDI X2,X12,#16",
"STUR X2, [X1, #5]",
]
# Dmem Values
dmem_config = '''
[16]=1
[24]=2
'''
# Register Values
reg_config = '''
X1=16
X2=5
'''
# __init__(self, code, reg_config = '', dmem_config = '', randomize = False)
cpu3 = CPU('cpu3', code3, reg_config=reg_config, mem_config=dmem_config, randomize=False)run(self)Executes the program in it's entirety.
Example:
cpu1 = CPU("first cpu", code)
cpu1.run()step(self)Steps through every instruction of the program, printing the state every time.
Example:
cpu1 = CPU("first cpu", code)
cpu1.step()__eq__(cpu1, cpu2)Returns True if two CPU objects are equal, else False. Two CPUs are equal if their registers and data memory both have the same values.
cpu1: CPU object instancecpu2: CPU object instanceExample:
cpu1 = CPU("first", code1)
cpu1.run()
cpu2 = CPU("second", code2)
cpu2.run()
print(cpu1 == cpu2) # Checking Equality HereRelated:
__ne__(cpu1, cpu2)reg_eq(cpu1, cpu2) - Returns True if both CPUs have the same Register values, else Falsemem_eq(cpu1, cpu2) - Returns True if both CPUs have the same Data Memory values, else False__print__(self)Prints the state of the CPU.
Example:
cpu1 = CPU("first", code1)
cpu1.run()
print(cpu1) # Printing HereRelated:
__str__(self) - Returns a string containing the state of the CPUset_print_mode(self, hex_mode: True) - Use hex_mode boolean flag to set printing to decimal/hexadecimalwrite_state(self, file_name)Writes the state of the CPU to a file.
file_name: Name of the file that the state of the CPU is written to. Creates a new file in file_name doesn't exist.Example:
cpu1 = CPU("first", code1)
cpu1.run()
cpu1.write_state("my_cpu_state.txt") # Writing Hereget_reg_value(self, reg)Returns the value in a register.
reg: String containing the name of the register whose value we wish to get.Example:
cpu1 = CPU("first", code1)
cpu1.run()
reg_val = cpu1.get_reg_value('X1') # Getting value in 'X1' hereset_reg_value(self, reg, value)Sets the value in a register.
reg: String containing the name of the register whose value we wish to get.value: The new value we wish to place into the registerExample:
cpu1 = CPU("first", code1)
cpu1.set_reg_value('X1', 100) # Setting 'X1' to 100 here
cpu1.set_reg_value('X2', 200) # Setting 'X2' to 200 here
cpu1.set_reg_value('X3', 300) # Setting 'X3' to 300 here
cpu1.run()get_double_word(self, mem_index)Gets the double-word value at a memory index. Returns a 64-bit value from mem[mem_index] to mem[mem_index + 7] inclusive, where mem[i] is 8-bits of data.
mem_index: Index from which we want to get the value fromExample:
cpu1 = CPU("first", code1)
cpu1.run()
# Getting double-word starting at index 8 here
mem_val = cpu1.get_double_word(8)set_double_word(self, mem_index, value)Sets the double word value starting at a memory index.
mem_index: Index from which we want to get the value fromvalue: The new value we want to place into the double wordExample:
cpu1 = CPU("first", code1)
cpu1.set_double_word(24, 150) # Setting mem[24] to 150 here
cpu1.set_double_word(32, 250) # Setting mem[32] to 250 here
cpu1.set_double_word(40, 350) # Setting mem[40] to 350 here
cpu1.run()Related:
set_mem_val(self, mem_index, value) - Sets 1 byte of memory data to a certain valuerandomize_cpu(self)Randomizes the values in the registers and data memory within a CPU.
Example:
cpu1 = CPU("first", code1)
cpu1.randomize_cpu() # Randomizing here
cpu1.run()Related:
randomize_register(self, reg_index) - Randomizes value within a particular registerrandomize_mem_byte(self, byte_index) - Randomizes the value within a particular byte of memoryrandomize_registers(self) - Randomizes values within the registersrandomize_data_memory(self) - Randomizes values within data memoryNo open issues yet, or sync has not completed.