@Override
	public void trap(int savedProgramCounter, int trapNumber) {
		CheckValid.trapNumber(trapNumber);
		if (!machine.cpu.runProg) {
			return;
		}

		switch (trapNumber) {
		case Trap.TIMER:
			if (running_process == -1) {
				runNextProcess();
				setNextProcess();
			} else if (running_process >= 0
					&& process_table[running_process].getStatus() == ProcessState.RUNNING) {
				process_table[running_process].setPc(savedProgramCounter);
				process_table[running_process].setAcc(machine.cpu.acc);
				process_table[running_process].setX(machine.cpu.x);
				process_table[running_process].setStatus(ProcessState.READY);
				runNextProcess();
				setNextProcess();
			}
			break;
		case Trap.END: {
			process_table[running_process].setStatus(ProcessState.TERMINATED);
			free_space.add(new FreeSpace(process_table[running_process]
					.getBase(), process_table[running_process].getLimit()));
			// merge
			free_space = mergeSpace();
			//diagnostic();
			// done merging
			runNextProcess();
			setNextProcess();
		}

			break;
		default:
			System.out.println(savedProgramCounter);
			System.out.println("UNHANDLED TRAP " + trapNumber);
			System.exit(1);
		}
	}