self.asm.instr("@R15") self.asm.instr("D=M") self.asm.instr("@ARG") self.asm.instr("M=D") # JMP to R14 (the callee) self.asm.instr("@R14") self.asm.instr("A=M") self.asm.instr("0;JMP") # Used to push the return address in call ops: self.asm.label("VM._push_a") self.asm.instr("D=A") self.asm.instr("@SP") self.asm.instr("M=M+1") self.asm.instr("A=M-1") self.asm.instr("M=D") self.asm.instr("RTN") THREADED_PLATFORM = BUNDLED_PLATFORM._replace(chip=ThreadedComputer, assemble=assemble, translator=Translator) if __name__ == "__main__": # Note: this import requires pygame; putting it here allows the tests to import the module import computer computer.main(THREADED_PLATFORM)
self._fix_stack() solved_07.Translator.goto(self, name) def function(self, class_name, function_name, num_vars): assert not self.top_in_d solved_07.Translator.function(self, class_name, function_name, num_vars) def return_op(self): # TODO: an alt. return handler? self._fix_stack() solved_07.Translator.return_op(self) def call(self, class_name, function_name, num_args): self._fix_stack() solved_07.Translator.call(self, class_name, function_name, num_args) def _fix_stack(self): if self.top_in_d: self._push_d() self.top_in_d = False LAZY_PLATFORM = BUNDLED_PLATFORM._replace(translator=Translator) if __name__ == "__main__": # Note: this import requires pygame; putting it here allows the tests to import the module import computer computer.main(LAZY_PLATFORM)
@chip def Splice(inputs, outputs): """Wiring only: assemble two 8-bit signals into a 16-bit signal.""" for i in range(8): outputs.out[i] = inputs.lo[i] outputs.out[i + 8] = inputs.hi[i] @chip def Split(inputs, outputs): """Wiring only: extract two 8-bit signals from a 16-bit signal.""" for i in range(8): outputs.lo[i] = inputs.in_[i] outputs.hi[i] = inputs.in_[i + 8] # Main: EIGHT_PLATFORM = BUNDLED_PLATFORM._replace(chip=EightComputer) if __name__ == "__main__": # Note: this import requires pygame; putting it here allows the tests to import the module import computer print("Hint: currently requires --simulator 'vector' (and patience)") # TODO: fix the simulator so this can run at full (half) speed computer.main(EIGHT_PLATFORM)
def rewrite_ops(self, ops): result = [] while ops: # TODO: the same for other powers of two if len(ops) >= 2 and ops[0] == ('push_constant', (16,)) and ops[1] == ('call', ("Math", "divide", 2)): result.append(("shiftr", ())) result.append(("shiftr", ())) result.append(("shiftr", ())) result.append(("shiftr", ())) ops = ops[2:] print("rewrote divide by 16") # TODO: similar for multiply, but use "pop temp 1"; "push temp 1" "push temp 1" "add"? else: result.append(ops[0]) ops = ops[1:] return result SHIFT_PLATFORM = BUNDLED_PLATFORM._replace( chip=ShiftComputer, assemble=assemble, translator=Translator) # TODO: library with replaced divide, etc. if __name__ == "__main__": # Note: this import requires pygame; putting it here allows the tests to import the module import computer computer.main(SHIFT_PLATFORM)
# LCL = SP # Note: setting LCL here (as opposed to in "function") feels wrong, but it makes the # state of the segment pointers consistent after each opcode, so it's easier to debug. self.asm.instr("@SP") self.asm.instr("D=M") self.asm.instr("@LCL") self.asm.instr("M=D") # JMP to R14 (the callee) self.asm.instr("@R14") self.asm.instr("A=M") self.asm.instr("0;JMP") return label # TODO: improve the common sequence for `return`. def finish(self): pass SP_PLATFORM = BUNDLED_PLATFORM._replace(chip=SPComputer, assemble=assemble, translator=Translator) if __name__ == "__main__": # Note: this import requires pygame; putting it here allows the tests to import the module import computer computer.main(SP_PLATFORM)