def _LEA(instruction): """load effective address""" dr = (instruction >> 9) & 0x7 pc_offset = sign_extend(instruction & 0x1ff, 9) address = pc_offset + reg_read(Registers.PC) reg_write(dr, address) update_flags(dr)
def _NOT(instruction): # Destination Register. DR = (instruction >> 9) & 0X7 # Source Register (register cntaining the data). SR = (instruction >> 6) & 0X7 # every bit in the DR will equal to the flipped one with the same index in SR. reg_write(DR, ~reg_read(SR)) # store the sign of the last excuted instruction data (which is in the DR). update_flags(DR)
def _LDI(instruction): # Destenation Register. DR = (instruction >> 9) & 0x7 # the value of what called an offset (embedded within the instruction code). PCoffset = sign_extend(instruction & 0X1ff, 9) # the address of the address of the desired data. address = reg_read(Registers.PC) + PCoffset # write the data (its address is explained in the previous line) in the register (DR). reg_write(DR, mem_read(mem_read(address))) # store the sign of the last excuted instruction data (which is in the DR). update_flags(DR)
def main(): reg_write(Registers.PC, PC_START) rom = read_Rom_file(input('Enter filename: ')) load_image(rom) try: while True: pc = reg_read(Registers.PC) instruction = mem_read(pc) execute(instruction) except Halt: # TODO print('Halted.')
def _LDR(instruction): """load register""" # compute the index of distination register dis_reg = (instruction >> 9) & 0x0007 # compute the index of BaseR register BaseR = (instruction >> 6) & 0x0007 # compute and extention offset6 value offset6 = sign_extend(instruction & 0x003F, 6) # compute memory addresse mem_addresse = reg_read(BaseR) + offset6 # read the memory storage value value = mem_read(mem_addresse) # write value to distination register reg_write(dis_reg, value) # update flags update_flags(dis_reg)
def _ADD(instruction): """add""" # destination register DR DR = (instruction >> 9) & 0x7 # first operand SR1 SR1 = (instruction >> 6) & 0x7 # for immediate mode imm_flag = (instruction >> 5) & 0x1 if imm_flag: imm5 = sign_extend(instruction & 0x1F, 5) reg_write(DR, reg_read(SR1) + imm5) else: SR2 = instruction & 0x7 reg_write(DR, reg_read(SR1) + reg_read(SR2)) update_flags(DR)
def main(): if len(sys.argv) < 2: print('Usage: python3 lc3.py [obj-file]') exit(2) reg_write(Registers.PC, PC_START) read_Rom_file(sys.argv[1]) try: while True: pc = reg_read(Registers.PC) instruction = mem_read(pc) # increment PC by #1. reg_write(Registers.PC, reg_read(Registers.PC) + 1) execute(instruction) except Halt: # TODO print('Halted.')
def _AND(instruction): """bitwise and""" # compute the index of distination register dis_reg = (instruction >> 9) & 0x0007 # compute the index of source1 register source1 = (instruction >> 6) & 0x0007 if (instruction & 0x0020) >= 1: # check bit 6 # compute and extend the immediate value imm5 = sign_extend(instruction & 0x001F, 5) # compute output value in case bit6 == 1 value = reg_read(source1) & imm5 # update dist ination register value reg_write(dis_reg, value) else: # compute the index of source2 register source2 = instruction & 0x0007 # compute output value in case bit6 == 0 value = reg_read(source1) & reg_read(source2) # update dist ination register value reg_write(dis_reg, value)
def update_flags(reg_index): reg_value = reg_read(reg_index) if reg_value == 0: reg_write(Registers.COND, Flags.ZRO) elif reg_value >> 15: # a 1 in the left-most bit indicates negative. reg_write(Registers.COND, Flags.NEG) else: reg_write(Registers.COND, Flags.POS)
def _JSR(instruction): """jump register""" pc_reg = reg_read(Registers.PC) # read current pc value # R7=current pc is the linkage back to the calling routine reg_write(Registers.R7, pc_reg) # check bit 11 if 1 or 0 if ((instruction >> 11) & 0x0001) == 1: # compute and extend the immediate offset PCoffset = sign_extend(instruction & 0x07FF, 11) # update pc value by adding offset reg_write(Registers.PC, PCoffset + pc_reg) else: baseREG = (instruction >> 6) & 0x0007 reg_value = reg_read(baseREG) # read value in base register reg_write(Registers.PC, reg_value) # update pc value by new value
def _LD(instruction): """load""" DR = (instruction >> 9) & 0x7 pc_offset = sign_extend(instruction & 0x1ff, 9) reg_write(DR, mem_read(reg_read(Registers.PC) + pc_offset)) update_flags(DR)
def _BR(instruction): """branch""" pc_offset = sign_extend(instruction & 0x1ff, 9) cond_flag = (instruction >> 9) & 0x7 if cond_flag & reg_read(Registers.COND): reg_write(Registers.PC, (reg_read(Registers.PC) + pc_offset))
def _JMP(instruction): """jump""" br = (instruction >> 6) & 0x7 # base register. reg_write(Registers.PC, reg_read(br))
def _IN(): """input a single character, echoed onto the console""" sys.stdout.write("Enter a character: ") sys.stdout.flush() reg_write(Registers.R0, ord(sys.stdin.read(1)))
def _GETC(): """get character from keyboard, character is not echoed onto the console. """ ch = getch() reg_write(Registers.R0, ord(ch))
def _GETC(): """get character from keyboard""" ch = getch() reg_write(Registers.R0, ord(ch))