⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpu.java

📁 一个简单的操作系统模拟程序 java+swing 可进行一般的文件操作 进程建立及删除等
💻 JAVA
字号:
/*
 * CPU.java ver 0.1.0
 *
 * Created on 2006年12月12日, 下午3:12
 *
 *The simlulate CPU for the system
 *
 *History:  2006年12月12日 ver 0.1.0   class create
 *          2006年12月14日 ver 0.1.0   first release        
 *
 */

package ossimulation;

import java.util.*;

/**
 *
 * @author Decell.Zhou
 */
public class CPU {
    
    /** Creates a new instance of CPU */
    public CPU(ArrayList devA,ArrayList devB,ArrayList devC) {
        PSW = new int[8];
        currentProcess = null;
        this.reset();
        devAWaitingList = devA;//the devA I/O address
        devBWaitingList = devB;//the devB I/O address
        devCWaitingList = devC;//the devC I/O address
    }
    
    /**simulate for the cpu action*/
    public void run(){
        String instruction;
        char instructionName;
        
        //get the instruction from the PCB
        instruction = currentProcess.getInstruction();
        instructionName = instruction.charAt(0);
        switch(instructionName){
            case 'A':
                this.handlingA(instruction);
                break;
            case 'B':
                this.handlingB(instruction);
                break;
            case 'C':
                this.handlingC(instruction);
                break;
            case 'D':
                this.handlingD(instruction);
                break;
            case 'E':
                this.handlingE();
                break;
            default:
                PSW[3] = 1;
        }
    }
    
    /**the handling code for the instruction A*/
    private void handlingA(String instruction){
        char[] variable = null;
        char variableName;
        int variableValue;
        
        variableName = instruction.charAt(1);//get the variable name
        variableValue = Integer.parseInt(instruction.substring(2,3));//get the vaiable value
        //search for the right variable
        for(Iterator variableListIterator = currentProcess.getVariableList().iterator();variableListIterator.hasNext();){
            variable = (char[])variableListIterator.next();
            if(variable[0] == variableName){
                break;
            }
        }
        //set the value to the variable;
        variable[1] = (char)variableValue;
        currentProcess.increasePC();//prepare for the next execution
        
    }
    
    /**the handling code for the instruction B*/
    private void handlingB(String instruction){
        char[] variable = null;
        char variableName;
        int variableValue;
        
        variableName = instruction.charAt(1);
        for(Iterator variableListIterator = currentProcess.getVariableList().iterator();variableListIterator.hasNext();){
            variable = (char[])variableListIterator.next();
            if(variable[0] == variableName){
                break;
            }
        }        
        
        variableValue = (int)variable[1];
        variableValue ++;
        if(variableValue == 10){
            variableValue = 0;
            PSW[1] = 1;//set the overflow flag
        }
        
        variable[1] = (char)variableValue;
        currentProcess.increasePC();//prepare for the next execution
        
    }
    
    /**the handling code for the instruction C*/
    private void handlingC(String instruction){
        char[] variable = null;
        char variableName;
        int variableValue;
        
        variableName = instruction.charAt(1);
        for(Iterator variableListIterator = currentProcess.getVariableList().iterator();variableListIterator.hasNext();){
            variable = (char[])variableListIterator.next();
            if(variable[0] == variableName){
                break;
            }
        }        
        
        variableValue = (int)variable[1];
        variableValue --;
        if(variableValue == -1){
            variableValue = 9;
            PSW[1] = 1;//set the overflow flag
        }
        
        variable[1] = (char)variableValue;
        currentProcess.increasePC();//prepare for the next execution
    }
    
    /**the handling code for the instruction D*/
    private void handlingD(String instruction){
        char devName;
        devName = instruction.charAt(1);
        //if the instruction request the wrong device
        if(devName != 'A' && devName != 'B' && devName != 'C'){
            PSW[3] = 1;//set the illegal instruction flag;
        } else {
            int requestTime = Integer.parseInt(instruction.substring(2,3));//get the request time
            DRCB deviceRequest = new DRCB(requestTime,currentProcess);//generate the device request control block
            
            switch(devName){
                case 'A':
                    devAWaitingList.add(deviceRequest);//request the device A,add the process to the device A waiting list
                    PSW[4] = 1;//set the interrupt bit
                    currentProcess.increasePC();//prepare for the next execution
                    break;
                case 'B':
                    devBWaitingList.add(deviceRequest);//request the device A,add the process to the device A waiting list
                    PSW[4] = 1;//set the interrupt bit
                    currentProcess.increasePC();//prepare for the next execution
                    break;
                case 'C':
                    devCWaitingList.add(deviceRequest);//request the device A,add the process to the device A waiting list
                    PSW[4] = 1;//set the interrupt bit
                    currentProcess.increasePC();//prepare for the next execution
                    break;
                default:
                    PSW[3] = 1;//set the illegal instruction bit
            }
        }
    }
    
    /**the handling code for the instruction E*/
    private void handlingE(){
        PSW[2] = 1;//treminate the program
    }
    
    /**load a process into to the cpu*/
    public void load(PCB targetPCB){
        this.reset();
        currentProcess = targetPCB;
    }
    
    /**unload a process from the cpu*/
    public void unload(){
        currentProcess = null;
        this.reset();
    }
    
    /**reset the cpu*/
    public void reset(){
        //clear the PSW
        PSW[0] = 0;//the zero flag set to 0;
        PSW[1] = 0;//the over flag set to 0;
        PSW[2] = 0;//the program terminate flag set to 0;
        PSW[3] = 0;//the illegal instruction flag set to 0;
        PSW[4] = 0;//the I/O instruction flag set set to 0;
        PSW[5] = 0;//reserve bit
        PSW[6] = 0;//reserve bit
        PSW[7] = 0;//reserve bit
    }
    
    /**get PSW from the cpu*/
    public int[] getPSW(){
        return PSW;
    }
    
    /**set the PSW of the cpu*/
    public void setPSW(int[] argPSW){
        PSW = argPSW;
    }
    
    /**get the current process from the cpu*/
    public PCB getProcess(){
        return currentProcess;
    }
    
    /**the main method,for class testing*/
    public static void main(String[] args){
        //test the construction method
        ArrayList devA = new ArrayList();
        ArrayList devB = new ArrayList();
        ArrayList devC = new ArrayList();
        CPU testCPU = new CPU(devA,devB,devC);
        
        //test the load method;
        String instructionName = "test";
        
        ArrayList instructions = new ArrayList();
        instructions.add("Ax1");
        instructions.add("AX1");
        instructions.add("By");
        instructions.add("BY");
        instructions.add("Cz");
        instructions.add("CZ");
        instructions.add("DA1");
        instructions.add("E");
        int lengthOfInstruction = instructions.size();
        
        int ramOffset = 0;
        int ramLength = 8;
        PCB testPCB = new PCB(0);
        char[] variable = new char[2];
        testPCB.initialize(instructionName,instructions,lengthOfInstruction,ramOffset,ramLength,null);
        
        testCPU.load(testPCB);
        testCPU.run();
        for(Iterator varListIterator = testPCB.getVariableList().iterator();varListIterator.hasNext();){
             variable = (char[])varListIterator.next();
            System.out.println(variable[0] + " " + (int)variable[1]);
        }
        System.out.println("-------------------------------------");
        testCPU.run();
        for(Iterator varListIterator = testPCB.getVariableList().iterator();varListIterator.hasNext();){
             variable = (char[])varListIterator.next();
            System.out.println(variable[0] + " " + (int)variable[1]);
        }
        System.out.println("-------------------------------------");
        testCPU.run();
        for(Iterator varListIterator = testPCB.getVariableList().iterator();varListIterator.hasNext();){
             variable = (char[])varListIterator.next();
            System.out.println(variable[0] + " " + (int)variable[1]);
        }
        System.out.println("-------------------------------------");
        testCPU.run();
        for(Iterator varListIterator = testPCB.getVariableList().iterator();varListIterator.hasNext();){
             variable = (char[])varListIterator.next();
            System.out.println(variable[0] + " " + (int)variable[1]);
        }
        System.out.println("-------------------------------------");
        testCPU.run();
        for(Iterator varListIterator = testPCB.getVariableList().iterator();varListIterator.hasNext();){
             variable = (char[])varListIterator.next();
            System.out.println(variable[0] + " " + (int)variable[1]);
        }
        System.out.println("-------------------------------------");
        testCPU.run();
        for(Iterator varListIterator = testPCB.getVariableList().iterator();varListIterator.hasNext();){
             variable = (char[])varListIterator.next();
            System.out.println(variable[0] + " " + (int)variable[1]);
        }
        System.out.println("-------------------------------------");
        testCPU.run();
        for(Iterator varListIterator = testPCB.getVariableList().iterator();varListIterator.hasNext();){
             variable = (char[])varListIterator.next();
            System.out.println(variable[0] + " " + (int)variable[1]);
        }
        System.out.println("end");
        
    }
    
    private int[] PSW;
    private PCB currentProcess;
    private ArrayList devAWaitingList;
    private ArrayList devBWaitingList;
    private ArrayList devCWaitingList;
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -