def _load_program(self, startaddr, tapename, procname=None): '''Load a program into memory from a stored tape (a file) starting at address startaddr. Create a PCB for the program and add to the ready q. Use the first part of the tapename as the procname, if not provided. ''' if procname is None: # Lop off .* from the end. procname = tapename[:tapename.find(".")] pcb = None try: with open(tapename, "r") as f: pcb = calos.PCB(procname) if self._debug: print("Created PCB for process {}".format(procname)) addr = startaddr for line in f: line = line.strip() if line == '': continue # skip empty lines if line.startswith('#'): continue # skip comment lines if line.isdigit(): # data self._ram[addr] = int(line) else: # instructions or label # Detect entry point label if line == "__main:": if self._debug: print("Main found at location", addr) pcb.set_entry_point(addr) continue self._ram[addr] = line addr += 1 print("Tape loaded from {} to {}".format(startaddr, addr)) pcb.set_memory_limits(startaddr, addr) if self._debug: print(pcb) except FileNotFoundError: print("File not found") if pcb is not None: self._os.add_to_ready_q(pcb)
def _load_program(self, startaddr, tapename, procname=None): '''Load a program into memory from a stored tape (a file) starting at address startaddr. Create a PCB for the program and add to the ready q. Use the first part of the tapename as the procname, if not provided. ''' if procname is None: # Lop off .* from the end. procname = tapename[:tapename.find(".")] pcb = None try: with open(tapename, "r") as f: pcb = calos.PCB(procname) if self._debug: print("Created PCB for process {}".format(procname)) addr = startaddr pcb.set_low_mem(addr) for line in f: line = line.strip() if line == '': continue # skip empty lines if line.startswith('#'): continue # skip comment lines if line.isdigit(): # data self._ram[addr] = int(line) addr += 1 elif line.startswith("__main:"): self._handle_main_label(addr, line, pcb) elif line.startswith("__data:"): self._handle_data_label(addr, line, pcb) else: # the line is regular code: store it in ram self._ram[addr] = line addr += 1 print("Tape loaded from {} to {}".format(startaddr, addr)) if self._debug: print(pcb) except FileNotFoundError: print("File not found") if pcb is not None: self._os.add_to_ready_q(pcb)