@Test public void testRotateZero() { byte result = _c.ROL((byte) 0); // after assertEquals(0, result); assertTrue(_c.getZeroFlag()); }
/** Memory Access - Stage 4 */ public static void stage4_memory() throws IOException { String binary_rep = pad_binary(Integer.toBinaryString(cpu.IR), 32); String op_code = (String) binary_rep.subSequence(binary_rep.length() - 6, binary_rep.length()); if (cpu.c_mux_Y_select == 0) // If mux_Y is 0, use value from ALU (RZ) { cpu.RY = cpu.RZ; } else if (cpu.c_mux_Y_select == 1) // If mux_Y is set to 1, then must use memory { if (op_code.equals(definitions.STW)) // If the instruction is a STW (store) { pmi.memory_address = cpu.RZ; // memory address = RZ pmi.memory_data = cpu.RM; // memory data = RM mem = pmi.write_word(mem); // write memory } else if (op_code.equals(definitions.LDW)) // If the instruction is a LDW (load) { pmi.memory_address = cpu.RZ; // Memory address = RZ pmi.read_memory(file); // Read memory cpu.RY = PMI.memory_data; // RY = memory data } } else if (cpu.c_mux_Y_select == 2) // PC-Temp (Not required for this assignment) { } }
private byte rotate(int rotateCount_) { for (int i = 0; i < rotateCount_; i++) { _c.LDA(_c.ROL(_c.getA())); } return _c.getA(); }
public void exec() { UnsignedNumber pc = cpu.getReg(Reg.PC); UnsignedNumber op = extended(); UnsignedNumber sp = cpu.getReg(Reg.SP); sp.sub(2); mem.write(sp, pc); pc.setVal((op).getVal()); }
@Before public void initialize() throws UnableToLoadRomException { Cartridge c = new Cartridge(ClassLoader.getSystemResourceAsStream("Pac-Man (U) [!].nes")); NES _nes = new NES(); _nes.setCart(c); _c = _nes.getCPU(); _mem = _c.getMemory(); _c.reset(); }
@Test public void testVetoScaling() { control.replay(); int maxScore = VetoType.INSUFFICIENT_RESOURCES.getScore(); assertEquals((int) (maxScore * 1.0 / CPU.getRange()), CPU.veto(1).getScore()); assertEquals(maxScore, CPU.veto(CPU.getRange() * 10).getScore()); assertEquals((int) (maxScore * 2.0 / RAM.getRange()), RAM.veto(2).getScore()); assertEquals((int) (maxScore * 200.0 / DISK.getRange()), DISK.veto(200).getScore()); }
@Test public void testInsufficientResources() { control.replay(); IHostAttributes hostA = hostAttributes(HOST_A, host(HOST_A), rack(RACK_A)); assertVetoes( makeTask(DEFAULT_CPUS + 1, DEFAULT_RAM + 1, DEFAULT_DISK + 1), hostA, CPU.veto(1), DISK.veto(1), RAM.veto(1)); assertVetoes(makeTask(DEFAULT_CPUS + 1, DEFAULT_RAM, DEFAULT_DISK), hostA, CPU.veto(1)); assertVetoes(makeTask(DEFAULT_CPUS, DEFAULT_RAM + 1, DEFAULT_DISK), hostA, RAM.veto(1)); assertVetoes(makeTask(DEFAULT_CPUS, DEFAULT_RAM, DEFAULT_DISK + 1), hostA, DISK.veto(1)); }
public void startup() { System.out.println("start the computer!"); cpu.startup(); memory.startup(); disk.startup(); System.out.println("start computer finished!"); }
public void shutdown() { System.out.println("begin to close computer!"); disk.shutdowm(); memory.shutdowm(); cpu.shutdown(); System.out.println("GG"); }
public static int loadURIResource(URI uri, CPU cpu, int addr) { int i; System.out.println("loadURL: " + uri.toString()); try { DataInputStream s = new DataInputStream(new BufferedInputStream(uri.toURL().openStream())); i = 0; try { while (true) { cpu.write8_a32(addr + i, s.readByte()); i++; } } catch (EOFException e) { // end } } catch (IOException e) { e.printStackTrace(System.err); throw new IllegalArgumentException(e); } System.out.printf("loadURL: '%s' done, %dbytes.\n", uri.toString(), i); return i; }
public CtlProcessador(AcaoDeSistema interfaceSistema) { this.interfaceSistema = interfaceSistema; cpu = new CPU(); cpu.start(); filaDePrioridades = new HashMap<Integer, Vector<Processo>>(); EnviaFila e = new EnviaFila(); e.start(); }
public static void main(String[] args) { int timeSlice = 10; // Value of the time quantum provided /* how declare the following two variables as public variables */ int numOfInstructions = 50; // this is the variable which is considered as "the execution time" in Kotiya's Project CPU omegaCPU = new CPU(); // New CPU object EXQUEUE<PROCESS> readyQueue = new EXQUEUE(); // Ready Queue EXQUEUE<PROCESS> blockQueue = new EXQUEUE(); // Block Queue DISPATCHER dispatcher = new DISPATCHER(readyQueue, blockQueue); // New dispatcher object omegaCPU.getDispatcher(dispatcher); // Dispacther reference in CPU dispatcher.startup(); // All processes enqueued to the Ready Queue omegaCPU.execution(); // Execution starts }
/** ALU - Stage 3 */ public static void stage3_execute() throws IOException { String binary_rep = pad_binary(Integer.toBinaryString(cpu.IR), 32); String op_code = (String) binary_rep.subSequence(binary_rep.length() - 6, binary_rep.length()); int immediate_value = 0; if (op_code.equals(definitions.STW)) // If the operation is a STW (store) { String imm_val = binary_rep.substring(10, 26); immediate_value = calc_imm_val(imm_val); // Get the immediate value cpu.RZ = ALU(cpu.c_ALU_op, cpu.RA, immediate_value); // RZ = ALU result from RA and immediate value cpu.RM = cpu.RB; // RM = RB } else // If the operation isn't a store { if (cpu.c_mux_B_select == 0) // If mux_B = 0 then use RB { cpu.RZ = ALU(cpu.c_ALU_op, cpu.RA, cpu.RB); // Set RZ = ALU result from RA and RB } else if (cpu.c_mux_B_select == 1) // If mux_B = 1 then use immediate value { String imm_val = binary_rep.substring(10, 26); immediate_value = calc_imm_val(imm_val); // Get the immediate value cpu.RZ = ALU( cpu.c_ALU_op, cpu.RA, immediate_value); // RZ = ALU result from RA and immediate value } if (cpu.c_condition_signal == 1) // If the condition signal is set then condition passed { String imm_val = ""; if (op_code.equals(definitions.BR)) // If the condition is BR (branch) { imm_val = binary_rep.substring(0, 26); // Get the immediate value (B-Type) } else { imm_val = binary_rep.substring(10, 26); // Get the immediate value (I-Type) } short imm_value = (short) Integer.parseInt(imm_val, 2); immediate_value = imm_value; cpu.R[15] = cpu.R[15] + immediate_value; // Update the program counter (PC) } } }
public static void main(String[] args) { final String PROCESSES_FILE = "processes.txt"; final int TIME_QUANTUM = 2; // Read processes in from the file and add them to the ready queue. Queue<Process> readyQueue = new LinkedList<Process>(); for (int i = 0; i < 20; i++) { readyQueue.add(new Process(i, i + 20, i + 13, 0.3)); } int ticker = 0; CPU cpu = new CPU(); Process process = readyQueue.poll(); while (readyQueue.size() > 0) { if (process.getState() == ProcessState.TERMINATED) { readyQueue.remove(process); process = readyQueue.poll(); } else if (process.getBurstTime() % TIME_QUANTUM == 0) { // Put the current process in ready state and add it back to // the queue process.setState(ProcessState.READY); readyQueue.add(process); process = readyQueue.poll(); } process.setState(ProcessState.RUNNING); try { cpu.setPC(process.generateInstruction()); cpu.setRegisters(process.generateRegisters()); process.incrementBurstTime(); System.out.print("CPU at " + ticker + ": " + cpu + " on pid: " + process.getPid() + "\n"); } catch (Exception e) { System.out.println(e.getMessage()); } ticker++; } }
@Test public void testRotateNineTimes() { // before _c.LDA((byte) 0b1101_0100); byte result = rotate(9); // after assertEquals((byte) 0b1101_0100, result); }
public void update() { if (type.compareTo("display") == 0) upDisp.update(this); else if (type.compareTo("LED") == 0) if (proc.mem[mem] != 0) setIcon( new javax.swing.ImageIcon( getClass().getResource("/_emulator/resources/pallet/ledLit.png"))); else setIcon( new javax.swing.ImageIcon( getClass().getResource("/_emulator/resources/pallet/ledUnlit.png"))); proc.updated = true; }
@Override public String toString() { return "Notebook: id = " + id + ", " + model + ", " + vendor.getName() + ", " + cpu.getModel() + ", " + memory.getModel() + ", " + date; }
@Test public void testRotateLeftOnceNoCarry() { // before _c.LDA((byte) 0b0101_0101); byte result = _c.ROL(_c.getA()); // after assertEquals((byte) 0b1010_1010, result); assertTrue(_c.getNegativeFlag()); assertTrue(!_c.getCarryFlag()); assertTrue(!_c.getZeroFlag()); }
@Test public void testRotateLeftOnceCarry() { // before _c.LDA((byte) 0b1101_0100); byte result = _c.ROL(_c.getA()); // 0xA9 = 1010 1001 System.out.println(HexUtils.toHex(result)); System.out.println(HexUtils.toHex((byte) 0b1010_0101)); // after assertEquals((byte) 0b1010_1000, result); assertTrue(_c.getNegativeFlag()); assertTrue(_c.getCarryFlag()); assertTrue(!_c.getZeroFlag()); }
/** * ALU method, note that I was unsure of the typical ALU operation codes so I used the following: * 0: And, 1: Or, 2: Add, 3: If less-than, 4: If greater-than, 5: If greater-than or equal 6: * Substract, 7: If less-than or equal, 8: If-equal, 9: Not equal */ public static int ALU(int ALU_op, int input_A, int input_B) { switch (ALU_op) { case 0: // And return input_A & input_B; case 1: // Or return input_A | input_B; case 2: // Add return input_A + input_B; case 3: // If less-than if (input_A < input_B) { cpu.c_condition_signal = 1; // Set condition signal } break; case 4: // If greater-than if (input_A > input_B) { cpu.c_condition_signal = 1; // Set condition signal } break; case 5: // If greater-than or equal if (input_A >= input_B) { cpu.c_condition_signal = 1; // Set condition signal } break; case 6: // Subtract return input_A - input_B; case 7: // If less-than or equal if (input_A <= input_B) { cpu.c_condition_signal = 1; // Set condition signal } break; case 8: // If equal if (input_A == input_B) { cpu.c_condition_signal = 1; // Set condition signal } break; case 9: // If not-equal if (input_A != input_B) { cpu.c_condition_signal = 1; // Set condition signal } } return 0; }
public void executeProgram(int address) { cpu.clearStacks(); cpu.setPC(address); cpu.run(); System.out.println("(end of program execution)"); }
public void shutdown() { System.out.println("Computer shutdown..."); cpu.shutdown(); memory.shutdown(); disk.shutdown(); }
public void interrupt() { finaliza = true; cpu.interrupt(); }
public void setAccum(int i) { cpu.accum = i; }
public void startup() { System.out.println("Computer startup..."); cpu.startup(); memory.startup(); disk.startup(); }
public void setPC(int pc) { cpu.pc = pc; }
public void clear() { memory.clear(); code.clear(); cpu.accum = 0; cpu.pc = 0; }
public MachineModel(boolean gui) { withGUI = gui; // INSTRUCTION_MAP entry for "NOP" INSTRUCTIONS[0] = (arg, flags) -> { flags = flags & 0x6; // remove parity bit that will have been verified if (flags != 0) { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } cpu.pc++; }; // INSTRUCTION entry for LOD (Load accumulator) INSTRUCTIONS[0x1] = (arg, flags) -> { flags = flags & 0x6; if (flags == 0) // Direct Addressing cpu.accum = memory.getData(arg); else if (flags == 2) // Immediate Addressing cpu.accum = arg; else if (flags == 4) // Indirect Addressing cpu.accum = memory.getData(memory.getData(arg)); else // Illegal flag { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } cpu.pc++; }; // INSTRUCTION FOR STO (Store accumulator in mem) INSTRUCTIONS[0x2] = (arg, flags) -> { flags = flags & 0x6; if (flags == 0) // Direct Addressing memory.setData(arg, cpu.accum); else if (flags == 4) // Indirect Addressing memory.setData(memory.getData(arg), cpu.accum); else // Illegal flag { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } cpu.pc++; }; // INSTRUCTION FOR JUMP (Jump pc to a new location in code) INSTRUCTIONS[0x3] = (arg, flags) -> { flags = flags & 0x6; if (flags == 0) // Direct Addressing cpu.pc += arg; else if (flags == 2) // Immediate Addressing cpu.pc = arg; else if (flags == 4) // Indirect Addressing cpu.pc += memory.getData(arg); else // flags will = 6 cpu.pc = memory.getData(arg); }; // INSTRUCTION FOR JMPZ (jump to a new location, conditionally) INSTRUCTIONS[0x4] = (arg, flags) -> { flags = flags & 0x6; if (cpu.accum == 0) INSTRUCTIONS[3].execute(arg, flags); else cpu.pc++; }; // INSTRUCTION entry for ADD (add) INSTRUCTIONS[0x5] = (arg, flags) -> { flags = flags & 0x6; // remove parity bit that will have been verified if (flags == 0) // direct addressing cpu.accum += memory.getData(arg); else if (flags == 2) // immediate addressing cpu.accum += arg; else if (flags == 4) // indirect addressing cpu.accum += memory.getData(memory.getData(arg)); else // here the illegal case is "11" { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } cpu.pc++; }; // INSTRUCTION FOR SUB (subtract from accum) INSTRUCTIONS[0x6] = (arg, flags) -> { flags = flags & 0x6; if (flags == 0) // direct addressing cpu.accum -= memory.getData(arg); else if (flags == 2) // immediate addressing cpu.accum -= arg; else if (flags == 4) // indirect addressing cpu.accum -= memory.getData(memory.getData(arg)); else // here the illegal case is "11" { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } cpu.pc++; }; // INSTRUCTION FOR MUL (multiply accum) INSTRUCTIONS[0x7] = (arg, flags) -> { flags = flags & 0x6; if (flags == 0) // direct addressing cpu.accum *= memory.getData(arg); else if (flags == 2) // immediate addressing cpu.accum *= arg; else if (flags == 4) // indirect addressing cpu.accum *= memory.getData(memory.getData(arg)); else // here the illegal case is "11" { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } cpu.pc++; }; // INSTRUCTION FOR DIV (division accum) INSTRUCTIONS[0x8] = (arg, flags) -> { flags = flags & 0x6; if (flags == 0) // direct addressing { if (memory.getData(arg) == 0) throw new DivideByZeroException(); cpu.accum /= memory.getData(arg); } else if (flags == 2) // immediate addressing { if (arg == 0) throw new DivideByZeroException(); cpu.accum /= arg; } else if (flags == 4) // indirect addressing { if (memory.getData(memory.getData(arg)) == 0) throw new DivideByZeroException(); cpu.accum /= memory.getData(memory.getData(arg)); } else // here the illegal case is "11" { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } cpu.pc++; }; // INSTRUCTION FOR AND (and two values together) INSTRUCTIONS[0x9] = (arg, flags) -> { flags = flags & 0x6; if (flags == 0) // Direct Addressing { if (cpu.accum != 0 && memory.getData(arg) != 0) cpu.accum = 1; else cpu.accum = 0; } else if (flags == 2) // Immediate Addressing { if (cpu.accum != 0 && arg != 0) cpu.accum = 1; else cpu.accum = 0; } else // Illegal Flags { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } cpu.pc++; }; // INSTRUCTION FOR NOT (not two values together) INSTRUCTIONS[0xA] = (arg, flags) -> { flags = flags & 0x6; if (flags != 0) { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } if (cpu.accum == 0) cpu.accum = 1; else cpu.accum = 0; cpu.pc++; }; // INSTRUCTION FOR CMPL (compare less than 0) INSTRUCTIONS[0xB] = (arg, flags) -> { flags = flags & 0x6; if (flags != 0) { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } if (memory.getData(arg) < 0) cpu.accum = 1; else cpu.accum = 0; cpu.pc++; }; // INSTRUCTION FOR CMPZ (compare to zero) INSTRUCTIONS[0xC] = (arg, flags) -> { flags = flags & 0x6; if (flags != 0) { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } if (memory.getData(arg) == 0) cpu.accum = 1; else cpu.accum = 0; cpu.pc++; }; // INSTRUCTION FOR FOR INSTRUCTIONS[0xD] = (arg, flags) -> { flags = flags & 0x6; int oldPC = cpu.pc, total_instruction, total_iterations; if (flags == 0) // Direct Addressing { total_instruction = memory.getData(arg) / 0x1000; total_iterations = memory.getData(arg) % 0x1000; } else if (flags == 2) // Immediate Addressing { total_instruction = arg / 0x1000; total_iterations = arg % 0x1000; } else // Illegal Flags { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } // The actual iterations for (int i = 0; i < total_iterations; i++) { cpu.pc = oldPC + 1; for (int j = 0; j < total_instruction; j++) this.step(); } cpu.pc = oldPC + total_instruction; }; // INSTRUCTION FOR HALT (halt execution) INSTRUCTIONS[0xF] = (arg, flags) -> { flags = flags & 0x6; if (flags != 0) { String fString = "(" + (flags % 8 > 3 ? "1" : "0") + (flags % 4 > 1 ? "1" : "0") + ")"; throw new IllegalInstructionException("Illegal flags for this instruction: " + fString); } halt(); }; }
// executes process' swap in the CPU based on the "ready processes list" and // the routing algorithm(preemptive or non-preemptive) public void SJF(int currentTime) { Process cpuProcess = cpu.peekCpuProcess(); Process HeadofQueue = getReadyList() .getReadyList() .peek(); // first process of the ready queue/the one with the lowest burst if (!isPreemptive) { if (cpuProcess != null) { if (cpuProcess.getCpuRemainingTime() == 0) { // zero remaining time means that the process is finished, therefore update number of // finished processes and total waiting time if (stats != null) { stats.updatetotalwait(cpuProcess, currentTime); // total waiting time update stats.updatemyfinished(); // update number of finished processes } cpu.removeProcessFromCpu(); if (stats != null) { stats.updateFinishedNumber(1); // edit stats.updateResponseTime(currentTime - cpuProcess.getArrivalTime()); // edit. } } } cpuProcess = cpu.peekCpuProcess(); if (cpuProcess == null) { Process forCPU = getReadyList().getProcessToRunInCPU(); if (forCPU != null) { cpu.addProcess(forCPU); cpu.setLastProcessStartTime(currentTime); } } } // //////////////////PREEMPTIVE//////////////////// else if (isPreemptive) { if (cpuProcess != null) { if (cpuProcess.getCpuRemainingTime() == 0) { // same as non-preemptive // zero remaining time means that the process is finished, therefore update number of // finished processes and total waiting time if (stats != null) { stats.updatetotalwait(cpuProcess, currentTime); // total waiting time update stats.updatemyfinished(); // update number of finished processes } cpu.removeProcessFromCpu(); if (stats != null) { stats.updateFinishedNumber(1); stats.updateResponseTime(currentTime - cpuProcess.getArrivalTime()); } } else if ((HeadofQueue != null) && (cpuProcess.getCpuRemainingTime() > HeadofQueue .getCpuRemainingTime())) { // null check + compare the remaining CPU times // between the current CPU process and the one on the // top of the Queue readyList.addProcess( cpu.removeProcessFromCpu()); // remove current Process and put it again at ready list Process forCPU = getReadyList().getProcessToRunInCPU(); // add the one from the top of the queue { if (forCPU.getCpuTotalTime() == forCPU.getCpuTotalTime()) { if (stats != null) stats.updateResponseTime(currentTime - forCPU.getArrivalTime()); } cpu.addProcess(forCPU); cpu.setLastProcessStartTime(currentTime); } } } cpuProcess = cpu.peekCpuProcess(); if (cpuProcess == null) { // if CPU isn't occupied Process forCPU = getReadyList().getProcessToRunInCPU(); // get process from the top of the Queue if (forCPU != null) { if (forCPU.getCpuTotalTime() == forCPU.getCpuTotalTime()) { if (stats != null) stats.updateResponseTime(currentTime - forCPU.getArrivalTime()); } cpu.addProcess(forCPU); // add to CPU cpu.setLastProcessStartTime(currentTime); } } } if (stats != null) { stats.updateTotalWaitingTime(getReadyList().lengthOfQueue()); stats.WriteStatistics2File(currentTime); } }
public void toggle() { if (this.status) proc.mem[mem] = (byte) ID; if (!this.status) proc.mem[mem] = (byte) (~ID); proc.updated = true; } // toggle