def _PUTS(): """output a word string""" i = reg_read(Registers.R0) ch = mem_read(i) while chr( ch) != '\0': # check if the char is not null then print this char sys.stdout.write(ch) i += 1 ch = mem_read(i) sys.stdout.flush() # equal to fflush() in c
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 _PUTS(): """output a word string""" for i in range(reg_read(Registers.R0), 2**16): ch = mem_read(i) if ch == 0: # check if the char is not null then print this char break sys.stdout.write(chr(ch)) sys.stdout.flush() # equal to fflush() in c
def _STI(instruction): # Source Register (the register containing the data). SR = (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 that the data will be stored at. address = reg_read(Registers.PC) + PCoffset # write the data (stored in the SR) into the memory (the address is explained in the previous line). mem_write(mem_read(address), reg_read(SR))
def _PUTSP(): """output a byte string""" for i in range(reg_read(Registers.R0), 2**16): c = mem_read(i) if c == 0: break sys.stdout.write(chr(c & 0xFF)) char = c >> 8 if char: sys.stdout.write(chr(char)) sys.stdout.flush()
def _PUTSP(): """output a byte string""" for i in range(Registers.R0, (2**16)): c = mem_read(ushort(i)) if chr(c) == '\0': break sys.stdout.write(chr(c & 0xFF)) char = c >> 8 if char: sys.stdout.write(chr(char)) sys.stdout.flush()
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 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 _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 _OUT(): """output a character""" sys.stdout.write(chr(mem_read(reg_read(Registers.R0)))) sys.stdout.flush()
def process_step(self, external_input, memory, read_vectors, previous_weights): """ NTM.process(external_input, read_vector, previous_weights) -> new_memory, new_read_vectors, new_weights, output the main method of this entire program. Processes the data with a NTM. @param external_input: a batchsize x input_size matrix that represents the data for this timestep @param memory: a batchsize x N x M memory 3-tensor from the previous timestep @param read_vector: a self.read_heads x batchsize x N 3-tensor that represents all of the read vectors produced by read heads @param previous_weights: a 1 + self.read_heads x batchsize x M 3-tensor that represents all of the weightings produced by all heads in the previous timestep """ # concatenated_read_vector -> batchsize x self.slot_size * read_vector concatenated_read_vector = T.concatenate( [read_vectors[head] for head in range(len(self.read_heads))], axis=1) # concatenated_input -> batchsize x self.slot_size * read_vector + external_input_length concatenated_input = T.concatenate( [concatenated_read_vector, external_input], axis=1) # controller_output -> batchsize x self.controller_size controller_output = T.nnet.relu( self.controller.forward(concatenated_input)) # Represents NTM's actual output for this timestep # ntm_output -> batchsize x self.output_size ntm_output = T.dot(controller_output, self.output_weight) # let's get reading out of the way first for head in range(len(self.read_heads)): key, shift, sharpen, strengthen, interpolation = self.read_heads[ head].produce(controller_output) # preprocess the values in preparation for mem_focus # key -> batchsize x 1 x N # strengthen -> batchsize x 1 (already good) key = key.dimshuffle([0, 'x', 1]) # Focus by content + strengthen # preliminary_weight -> batchsize x M preliminary_weight = mem_focus(memory, key, strengthen) # Focus by location interpolated_weight = interpolation * preliminary_weight + ( 1 - interpolation) * previous_weights[head] # Shift # Both arguments are batchsize x M, first being weighting, second being shift shifted_weight = focus_shift(interpolated_weight, shift, self.memory_slots) # Sharpen # We added broadcasted the second axis of sharpen, remember? :)))))))))))))))))))))))))) # sharpened_weight -> batchsize x M sharpened_weight = shifted_weight**sharpen # Normalize # T.sum(...) -> batchsize x 1 final_weight = sharpened_weight / (T.sum( sharpened_weight, axis=1, keepdims=True) + SMALL_CONSTANT) # read, read, read! # read_vec -> batchsize x N x 1, so we gotta flatten to batchsize x N read_vec = mem_read(memory, final_weight) read_vectors = T.set_subtensor(read_vectors[head], T.flatten(read_vec, outdim=2)) previous_weights = T.set_subtensor(previous_weights[head], final_weight) # let's write now! key, add, erase, shift, sharpen, strengthen, interpolation = self.write_head.produce( controller_output) # preprocess the values in preparation for mem_focus # key -> batchsize x 1 x N # strengthen -> batchsize x 1 (already_good) key = key.dimshuffle([0, 'x', 1]) # Focus by content + strengthen # preliminary_weight -> batchsize x M preliminary_weight = mem_focus(memory, key, strengthen) # Focus by location interpolated_weight = interpolation * preliminary_weight + ( 1 - interpolation) * previous_weights[-1] # Shift shifted_weight = focus_shift(interpolated_weight, shift, self.memory_slots) # Sharpen sharpened_weight = shifted_weight**sharpen # Normalize # T.sum(...) -> batchsize x 1 final_weight = sharpened_weight / ( T.sum(sharpened_weight, axis=1, keepdims=True) + SMALL_CONSTANT) previous_weights = T.set_subtensor(previous_weights[-1], final_weight) # preprocess the values in preparation for mem_write # weighting -> batchsize x 1 x M # erase_vector -> batchsize x N x 1 # add_vector -> batchsize x N x 1 final_weight = final_weight.dimshuffle([0, 'x', 1]) erase = erase.dimshuffle([0, 1, 'x']) add = add.dimshuffle([0, 1, 'x']) new_memory = mem_write(memory, final_weight, erase, add) # phew, almost done! return new_memory, read_vectors, previous_weights, ntm_output