/** * Notifies all the ProgramEventListeners on a change in the ROM's program by creating a * ProgramEvent (with the new event type and program's file name) and sending it using the * programChanged method to all the listeners. */ protected void notifyProgramListeners(byte eventType, String programFileName) { ProgramEvent event = new ProgramEvent(this, eventType, programFileName); for (int i = 0; i < listeners.size(); i++) { ((ProgramEventListener) listeners.elementAt(i)).programChanged(event); } }
/** * Calls a function according to the given function number stating that the given number of * arguments have been pushed onto the stack * * <p>If callerIsBuiltIn then the caller is a builtIn function that called this function through * callFunctionFromBuiltIn. If address is -1 then a native function should be looked up and * called. */ public void callFunction( short address, short numberOfArguments, String functionName, boolean callerIsBuiltIn) throws ProgramException { stackFrames.addElement(new Integer(workingStackSegment.getStartAddress())); workingStackSegment.setStartAddress(getSP() + 5); if (callerIsBuiltIn) { pushValue(MAIN_STACK, VMProgram.BUILTIN_FUNCTION_ADDRESS); } else { pushValue(MAIN_STACK, program.getPC()); } pushFromRAM(MAIN_STACK, Definitions.LOCAL_POINTER_ADDRESS); pushFromRAM(MAIN_STACK, Definitions.ARG_POINTER_ADDRESS); pushFromRAM(MAIN_STACK, Definitions.THIS_POINTER_ADDRESS); pushFromRAM(MAIN_STACK, Definitions.THAT_POINTER_ADDRESS); ram.setValueAt( Definitions.ARG_POINTER_ADDRESS, (short) (getSP() - numberOfArguments - 5), false); ram.setValueAt(Definitions.LOCAL_POINTER_ADDRESS, getSP(), false); // enable in the arg segment only the number of args that were sent to the called function. argSegment.setEnabledRange( argSegment.getStartAddress(), argSegment.getStartAddress() + numberOfArguments - 1, true); if (address == VMProgram.BUILTIN_FUNCTION_ADDRESS) { // Perform some actions normally done in the function() method localSegment.setEnabledRange( localSegment.getStartAddress(), localSegment.getStartAddress() - 1, true); // no local variables callStack.pushFunction(functionName + " (built-in)"); staticSegment.setEnabledRange(0, -1, true); // empty static segment // Read parameters from the stack short[] params = new short[numberOfArguments]; for (int i = 0; i < numberOfArguments; ++i) { params[i] = argSegment.getValueAt(i); } // Call the built-in implementation builtInFunctionsRunner.callBuiltInFunction(functionName, params); } else if (address >= 0 || address < program.getSize()) { program.setPC(address); program.setPC(address); // make sure previouspc isn't pc-1 // which might happen if the calling // function called this function in the // last line before the "return" and // was declared just before this function. // In this case encountering the "function" // command will issue an error about // "missing return"... } else { error("Illegal call address"); } }
/** Initializes the cpu. */ public void boot() { stackSegment.setStartAddress(Definitions.STACK_START_ADDRESS); workingStackSegment.setStartAddress(Definitions.STACK_START_ADDRESS); localSegment.setEnabledRange( Definitions.STACK_START_ADDRESS, Definitions.STACK_END_ADDRESS, true); argSegment.setEnabledRange( Definitions.STACK_START_ADDRESS, Definitions.STACK_END_ADDRESS, true); thisSegment.setEnabledRange(Definitions.HEAP_START_ADDRESS, Definitions.HEAP_END_ADDRESS, true); thatSegment.setEnabledRange( Definitions.HEAP_START_ADDRESS, Definitions.SCREEN_END_ADDRESS, true); staticSegment.setStartAddress(Definitions.VAR_START_ADDRESS); staticSegment.setEnabledRange( Definitions.VAR_START_ADDRESS, Definitions.VAR_END_ADDRESS - 1, true); setSP(Definitions.STACK_START_ADDRESS); stackFrames.clear(); if (builtInFunctionsRunner != null) { builtInFunctionsRunner.killAllRunningBuiltInFunctions(); } }
/** Un-registers the given ProgramEventListener from being a listener to this GUI. */ public void removeProgramListener(ProgramEventListener listener) { listeners.remove(listener); }
/** Registers the given ProgramEventListener as a listener to this GUI. */ public void addProgramListener(ProgramEventListener listener) { listeners.add(listener); }
/** Returns the value of the function to the top of the stack. */ public void returnFromFunction() throws ProgramException { // make sure that there's somewhere to return to (old local <> 0) if (stackSegment.getValueAt(Definitions.LOCAL_POINTER_ADDRESS) == 0) throw new ProgramException( "Nowhere to return to in " + getCallStack().getTopFunction() + "." + getCurrentInstruction().getIndexInFunction()); // done in order to clear the method stack's contents workingStackSegment.setStartAddress(getSP()); bus.send(ram, Definitions.LOCAL_POINTER_ADDRESS, ram, Definitions.R13_ADDRESS); // R13 = lcl bus.send( stackSegment, stackSegment.getValueAt(Definitions.LOCAL_POINTER_ADDRESS) - 5, ram, Definitions.R14_ADDRESS); // R14 = return address bus.send( stackSegment, getSP() - 1, stackSegment, ram.getValueAt(Definitions.ARG_POINTER_ADDRESS)); // *arg = return value setSP((short) (ram.getValueAt(Definitions.ARG_POINTER_ADDRESS) + 1)); // SP = arg + 1 bus.send( stackSegment, ram.getValueAt(Definitions.R13_ADDRESS) - 1, ram, Definitions.THAT_POINTER_ADDRESS); // that = *(R13 - 1) bus.send( stackSegment, ram.getValueAt(Definitions.R13_ADDRESS) - 2, ram, Definitions.THIS_POINTER_ADDRESS); // this = *(R13 - 2) bus.send( stackSegment, ram.getValueAt(Definitions.R13_ADDRESS) - 3, ram, Definitions.ARG_POINTER_ADDRESS); // arg = *(R13 - 3) bus.send( stackSegment, ram.getValueAt(Definitions.R13_ADDRESS) - 4, ram, Definitions.LOCAL_POINTER_ADDRESS); // lcl = *(R13 - 4) // removes the top function from the call stack callStack.popFunction(); // check whether there is a "calling frame" if (stackFrames.size() > 0) { // retrieve stack frame address of old function int frameAddress = ((Integer) stackFrames.lastElement()).intValue(); stackFrames.removeElementAt(stackFrames.size() - 1); workingStackSegment.setStartAddress(frameAddress); // disable non relevant range of the local segment - enable only the locals // of the function that we returned to. localSegment.setEnabledRange( Math.max(localSegment.getStartAddress(), Definitions.STACK_START_ADDRESS), frameAddress - 1, true); // enable in the arg segment only the number of args that were sent to the function // that we returned to. argSegment.setEnabledRange( argSegment.getStartAddress(), localSegment.getStartAddress() - 6, true); // enable this, that according to their retrieved pointers thisSegment.setEnabledRange( Math.max(thisSegment.getStartAddress(), Definitions.HEAP_START_ADDRESS), Definitions.HEAP_END_ADDRESS, true); thatSegment.setEnabledRange( Math.max(thatSegment.getStartAddress(), Definitions.HEAP_START_ADDRESS), Definitions.SCREEN_END_ADDRESS, true); } /* else { error("Nowhere to return to"); } */ // Allow return if we previously had "function" even with no call - // For the SimpleFunction test short returnAddress = ram.getValueAt(Definitions.R14_ADDRESS); if (returnAddress == VMProgram.BUILTIN_FUNCTION_ADDRESS) { staticSegment.setEnabledRange(0, -1, true); // empty static segment builtInFunctionsRunner.returnToBuiltInFunction(popValue(METHOD_STACK)); } else if (returnAddress >= 0 && returnAddress < program.getSize()) { // sets the static segment range if (stackFrames.size() > 0) { setStaticRange(callStack.getTopFunction()); } else { staticSegment.setStartAddress(Definitions.VAR_START_ADDRESS); staticSegment.setEnabledRange( Definitions.VAR_START_ADDRESS, Definitions.VAR_END_ADDRESS - 1, true); } program.setPC((short) (returnAddress - 1)); // set previousPC correctly program.setPC(returnAddress); // pc = *sp } else { error("Illegal return address"); } }
/** Hides all highlightes. */ public void hideHighlight() { highlightIndex.removeAllElements(); repaint(); }
/** Highlights the value at the given index. */ public void highlight(int index) { highlightIndex.addElement(new Integer(index)); repaint(); }
/** Un-registers the given ErrorEventListener from being a listener to this GUI. */ public void removeErrorListener(ErrorEventListener listener) { errorEventListeners.removeElement(listener); }
/** * Notifies all the ErrorEventListener on an error in this gui by creating an ErrorEvent (with the * error message) and sending it using the errorOccured method to all the listeners. */ public void notifyErrorListeners(String errorMessage) { ErrorEvent event = new ErrorEvent(this, errorMessage); for (int i = 0; i < errorEventListeners.size(); i++) ((ErrorEventListener) errorEventListeners.elementAt(i)).errorOccured(event); }
/** Registers the given ErrorEventListener as a listener to this GUI. */ public void addErrorListener(ErrorEventListener listener) { errorEventListeners.addElement(listener); }
public void notifyListeners() { ComputerPartEvent event = new ComputerPartEvent(this); for (int i = 0; i < listeners.size(); i++) { ((ComputerPartEventListener) listeners.elementAt(i)).guiGainedFocus(); } }
/** * Notifies all the ComputerPartEventListeners on a change in the memory by creating a MemoryEvent * (with the changed address and value) and sending it using the memoryChanged method to all the * listeners. */ public void notifyListeners(int address, short value) { ComputerPartEvent event = new ComputerPartEvent(this, address, value); for (int i = 0; i < listeners.size(); i++) { ((ComputerPartEventListener) listeners.elementAt(i)).valueChanged(event); } }
/** Un-registers the given ComputerPartEventListener from being a listener to this GUI. */ public void removeListener(ComputerPartEventListener listener) { listeners.removeElement(listener); }
/** Registers the given ComputerPartEventListener as a listener to this GUI. */ public void addListener(ComputerPartEventListener listener) { listeners.addElement(listener); }