📄 cpu.java
字号:
/* * CPU.java * * Created on 2006年3月15日, 下午9:55 * * */package os.cpu;/** * * @author Vernkin */import javax.swing.*;import os.compiler.*;//import os.MyProcess.*;/** * CPU Module */public class CPU implements BaseInstruction,Runnable{ public final String name = "[CPU]"; /** * register array */ private int[] register = new int[registerName.length]; /** * Programe Counter */ private int pc = 0; /** * Instruction Register */ private int ir = 0; /** * Condition Code */ private char cc = '\0'; /** * Current Process Information */ private String info = null; /** * The Max count of instruction that a process * can run at a time */ private static int MAX_COUNT = 5; /** * count for the number of instruction that * current process run */ private int count; /** * current Process */ private os.process.MyProcess currentPro; /** * The only object of MainMemory */ private os.memory.MainMemory mm; /** * The Only Object of Interrupt */ private Interrupt itp; /** * Constructor */ JTextField[] jf; JTextArea commonOut; public CPU(JTextField[] jf,JTextArea commonOut){ if(jf.length != 12){ System.out.println("Init CPU ,Wrong display JTextField numbers"); System.exit(1); } this.jf = jf; this.commonOut = commonOut; commonOut.append(name+" Ready\n"); clearText(); itp = Interrupt.getInstance(); mm = os.memory.MainMemory.getInstance(); currentPro = null; // requestProcess(); itp.cpuSetMeaasge(null, Interrupt.HALT_INTERRUPT); } /** * Implementation of interface Runnable */ public void run(){ while(true){ if(currentPro == null){ requestProcess(); }else{ System.out.println(name+" run "+currentPro.getName() + " "+currentPro.getID()+" PC:"+pc); operate(); display(); } itp.rest(250); } } /** * If CPU is free,request new Process to run */ private void requestProcess(){ // commonOut.append(name+" is free,request for new process\n"); currentPro = itp.cpuGetProcess(); if(currentPro == null) return; currentPro.setStatus("Running"); refreshData(mm.getPSW(currentPro)); commonOut.append(name+" Get Process "+currentPro+"\n"); count = 0; } /** * operate an instruction */ private void operate(){ if(currentPro == null) return; if(count >= MAX_COUNT){ mm.setPSW(currentPro, collectData());//save current state commonOut.append(name+" Interrupt name "+itp.getInterruptName(Interrupt.TIMEOUT_INTERRUPT)+" ID : "+Interrupt.TIMEOUT_INTERRUPT+"\n"); itp.cpuSetMeaasge(currentPro,Interrupt.TIMEOUT_INTERRUPT); currentPro = null; return; } ir = mm.getValueAt(currentPro, pc); String s = return16BitString(ir); pc++;//pc increment System.out.println("CPU operate "+s); operateSingle(s); count++; } /** * more detail about run a single instruction * @param in 16-bit instruction */ public void operateSingle(String in){ String opCode = in.substring(0, 4); int temp = 0; /**ADD**/ if(opCode.equals("0001")){ if(in.charAt(10) == '0') register[getValueAt(in,4,3)] = register[getValueAt(in,7,3)] + register[getValueAt(in,13,3)]; else register[getValueAt(in,4,3)] = register[getValueAt(in,7,3)] + getValueAt(in,11,5,false); //change sondition code status setCC(register[getValueAt(in,4,3)]); } /**AND**/ else if(opCode.equals("0101")){ if(in.charAt(10) == '0') register[getValueAt(in,4,3)] = register[getValueAt(in,7,3)] & register[getValueAt(in,13,3)]; else register[getValueAt(in,4,3)] = register[getValueAt(in,7,3)] & getValueAt(in,11,5,false); setCC(register[getValueAt(in,4,3)]); } /**NOT**/ else if(opCode.equals("1101")){ register[getValueAt(in,4,3)] = ~register[getValueAt(in,7,3)]; setCC(register[getValueAt(in,4,3)]); } /**BR**/ else if(opCode.equals("0000")){ boolean satisfy = false; if((cc == 'N' || cc == 'n') && in.charAt(4) == '1') satisfy = true; else if((cc == 'Z' || cc == 'z') && in.charAt(5) == '1') satisfy = true; else if((cc == 'P' || cc == 'p') && in.charAt(6) == '1') satisfy = true; //If satisfy the Condition Code ,Brance if(satisfy) pc = combineAddress7to9(return16BitString(pc), in); } /**JSR**/ else if(opCode.equals("0100")){ register[7] = pc; pc = combineAddress7to9(return16BitString(pc), in); } /**JSRR**/ else if(opCode.equals("1100")){ register[7] = pc; pc = register[getValueAt(in,7,3)] + getValueAt(in,10,6); } /**RET**/ else if(opCode.equals("1101")){ pc = register[7]; } /**LD**/ else if(opCode.equals("0010")){ register[getValueAt(in,4,3)] = mm.getValueAt(currentPro,combineAddress7to9(return16BitString(pc), in)); setCC(register[getValueAt(in,4,3)]); } /**ST**/ else if(opCode.equals("0011")){ mm.setValueAt(currentPro,combineAddress7to9(return16BitString(pc), in), register[getValueAt(in,4,3)]); } /**LDI**/ else if(opCode.equals("1010")){ temp = mm.getValueAt(currentPro,combineAddress7to9(return16BitString(pc), in)); register[getValueAt(in,4,3)] = mm.getValueAt(currentPro, temp); setCC(register[getValueAt(in,4,3)]); } /**STI**/ else if(opCode.equals("1011")){ temp = mm.getValueAt(currentPro,combineAddress7to9(return16BitString(pc), in)); mm.setValueAt(currentPro, temp, register[getValueAt(in,4,3)]); } /**LDR**/ else if(opCode.equals("0110")){ register[getValueAt(in,4,3)] = mm.getValueAt(currentPro,register[getValueAt(in,7,3)]+getValueAt(in,10,6)); setCC(register[getValueAt(in,4,3)]); } /**STR**/ else if(opCode.equals("0111")){ mm.setValueAt(currentPro,register[getValueAt(in,7,3)]+getValueAt(in,10,6), register[getValueAt(in,4,3)]); } /**LEA**/ else if(opCode.equals("1110")){ register[getValueAt(in,4,3)] = combineAddress7to9(return16BitString(pc), in); setCC(register[getValueAt(in,4,3)]); } /**HALT**/ else if(opCode.equals("1000")){ commonOut.append(name+" Halt Process "+currentPro+"\n"); itp.cpuSetMeaasge(currentPro,Interrupt.HALT_INTERRUPT); currentPro = null; } /**TRAP**/ else if(opCode.equals("1111")){ int value = getValueAt(in,8,8); commonOut.append(name+" Interrupt name "+itp.getInterruptName(value)+" ID : "+value+"\n"); mm.setPSW(currentPro, collectData());//save current state mm.setContextValueAt(currentPro, 11, value); itp.cpuSetMeaasge(currentPro,value); currentPro = null; } } /** * set the Condition Code * @param in the operation result */ private void setCC(int in){ if(in < 0) cc = 'N'; else if(in == 0) cc = 'Z'; else cc = 'P'; } /** * Combine address(16-bit) * s1[15:9] + s2[8:0] * @param s1 decide higher address bits [15:9] * @param s2 decide lower address bits [8:0] * @return conbined address */ private int combineAddress7to9(String s1,String s2){ try{ return Integer.parseInt(s1.substring(0, 7)+s2.substring(7), 2); }catch(NumberFormatException e){ System.err.println(e.getStackTrace()); } return -1; } /** * Convert int to 16-bit instruction Format * @param in instruction stored in a integer * @return 16-bit instruction Format */ private String return16BitString(int in){ String s = Integer.toBinaryString(in); return os.compiler.MyCompiler.fill(16, s, '0'); } /** * get a substring and convert in into int * (always Positive) * @param s source String * @param begin begin index * @param len length * @return substring to int */ public static int getValueAt(String s,int begin,int len){ return getValueAt(s, begin, len,true); } /** * get a substring and convert in into int * (always Positive or not) * @param s source String * @param begin begin index * @param len length * @param alwaysPositive whether the result is non-negetive * @return substring to int */ public static int getValueAt(String s,int begin,int len,boolean alwaysPositive){ String temp = s.substring(begin, begin+len); try{ // System.out.println(temp+" "+Integer.parseInt(temp, 2)); if(alwaysPositive || temp.charAt(0) == '0') return Integer.parseInt("0"+temp, 2); else{ temp = temp.replace("0", "A"); temp = temp.replace("1","0"); temp = temp.replace("A", "1"); return 0-(Integer.parseInt(temp, 2)+1); } }catch(NumberFormatException e){ System.err.println(e.getStackTrace()); } return -1; } /** * Collect the process status * Including(by order): * register,PC,IR,CC,(If TRAP)interrupt number * @return process status stored in integer array */ private int[] collectData(){ int[] ret = new int[11]; for(int i=0;i<8;i++) ret[i] = register[i]; ret[8] = pc; ret[9] = ir; ret[10] = (int)cc; return ret; } /** * Refresh process status * @param in the context store in main memory */ private void refreshData(int[] in){ for(int i=0;i<8;i++) register[i] = in[i]; pc = in[8]; ir = in[9]; cc = (char)in[10]; } private void display(){ for(int i=0;i<8;i++) jf[i].setText(""+register[i]); jf[8].setText(""+pc); jf[9].setText(return16BitString(ir)); jf[10].setText(""+cc); if(currentPro != null) jf[11].setText(""+currentPro.toString()); else jf[11].setText("No Process"); } private void clearText(){ for(int i=0;i<12;i++) jf[1].setText(""); } /** * * @return */ public String[] getPSW(){ String[] ret = new String[11]; for(int i=0;i<8;i++) ret[i] = "" + register[i]; ret[8] = MyCompiler.convertBinary("x"+Integer.toHexString(pc),16,Integer.MIN_VALUE,Integer.MAX_VALUE); ret[9] = MyCompiler.convertBinary("x"+Integer.toHexString(ir),16,Integer.MIN_VALUE,Integer.MAX_VALUE); ret[10] = ""+cc; return ret; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -