def test_operations(intA, intB): from util import bits2int, int2bits from register import Register from bus import Bus A = Bus("A", 32) B = Bus("B", 32) C = Bus("C", 32) mir = Register("MIR", 36, None, None, None) alu = ALU(A, B, C, mir) wordSize = max(intA.bit_length(), intB.bit_length()) A.val = int2bits(intA, wordSize) B.val = int2bits(intB, wordSize) print("A = binary({0}) -> {1}".format(intA, list(reversed(A.val)))) print("B = binary({0}) -> {1}".format(intB, list(reversed(B.val)))) print() for _, op in operations.items(): mir.val = [0 for _ in range(16) ] + op.bit_string + [0 for _ in range(16 + 6, 36)] alu.tick() sol = C.val print("f(A,B) = {0}".format(op.name)) print("binary({0}) -> {1}".format(bits2int(sol), list(reversed(sol)))) print()
def read(self): word_start = 4 * bits2int(self.mar.old_read_val, wordSize=32) word = self.array[word_start:word_start + 4] self.mdr.val = int2bits(word[0], wordSize=8) + \ int2bits(word[1], wordSize=8) + \ int2bits(word[2], wordSize=8) + \ int2bits(word[3], wordSize=8)
def updateMIR(self): adr = self.get_address() nextMIRparts = control_store[adr][:6] next_adr = int2bits(nextMIRparts[0], wordSize=9) jam = int2bits(nextMIRparts[1], wordSize=3) shift_alu = int2bits(nextMIRparts[2], wordSize=8) C_field = int2bits(nextMIRparts[3], wordSize=9) mem = int2bits(nextMIRparts[4], wordSize=3) B_field = int2bits(nextMIRparts[5], wordSize=4) self.mir.val = B_field + mem + C_field + shift_alu + jam + next_adr
BIPUSH 1 # return code to indicate where we branched """ machine_code = assemble_simple(sum_to_n) prog_start_index = 4 machine_code.append(0xFF) # will cause KeyError in dict lookup # this means the program is finished for index, number in enumerate(machine_code): ijvm.ram.array[prog_start_index + index] = number # set program counter and memory byte register to first instruction # this has to be present before the program starts ijvm.registers["PC"].val = int2bits(prog_start_index, wordSize=32) ijvm.registers["MBR"].val = int2bits(ijvm.ram.array[prog_start_index], wordSize=8) # set stack pointer to an empty location, (multiple of 4) ijvm.registers["SP"].val = int2bits(4*((prog_start_index + len(machine_code))//4 + 1), wordSize=32) verbose = False debug = False try: while True: ijvm.tick(verbose or debug) if debug: input() except KeyError as e: if verbose or debug:
def fetch(self): self.mbr.val = int2bits(self.array[bits2int(self.pc.old_val, wordSize=32)], wordSize=8)
if __name__ == "__main__": from register import Register mir = Register("MIR", 36, None, None, None) mar = Register("MAR", 32, None, None, mir) mdr = Register("MDR", 32, None, None, mir) pc = Register("PC", 32, None, None, mir) mbr = Register("MBR", 8, None, None, mir) ram = RAM(mar, mdr, pc, mbr, mir, size=16) # set write, set mar addr, set value mir.val[6] = 1 mar.val = int2bits(0, wordSize=32) mdr.val = int2bits(0xFF0014, wordSize=32) ram.tick() print(ram.array) print(list(reversed(mdr.val))) mir.val[6] = 0 # set read, set mar addr, reset mdr mir.val[5] = 1 mar.val = int2bits(0, wordSize=32) mdr.val = int2bits(0, wordSize=32) ram.tick() print(ram.array) print(list(reversed(mdr.val))) mir.val[5] = 0