📄 cpumanager.java
字号:
/******************************************
*cpu运行
*2004-9-14 赖卫国
******************************************/
package Manager;
import myException.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import instruction.*;
import memory.*;
import Global.*;
public class CPUManager extends Thread implements ActionListener
{
private static Timer SystemTimer;//SysTime:系统时钟
private static int currentID = 0;
private static int currentIndex;
private MemoryManager memm;
private HardDiskManager VHD;
private static int timeBlocks;
private static int TIMEBLOCKS = Global.TIMEBLOCK;
private static boolean firstRun = true;
public static boolean follow = true;
public CPUManager()
{
timeBlocks = TIMEBLOCKS;
memm = new MemoryManager();
VHD = new HardDiskManager();
if(firstRun)
{
firstRun = false;
SystemTimer = new Timer(Global.SYSTEMTIMER,this);//SystemTimer.start();
}
}
/********************************************
*启动线程
********************************************/
public void run()
{
SystemTimer.start();
//while(true);
}
/********************************************
*Timer事件
********************************************/
public void actionPerformed(ActionEvent e)
{
String MEM_Used="虚拟内存容量:"+memm.UsedMem()+"/"+Global.BLOCKS;
String VHD_Used="虚拟硬盘容量:"+VHD.GetUsedSize()+"/"+Global.HD_SIZE;
desktop.Desktop.setVHDLabel(VHD_Used);
desktop.Desktop.setVMEMLabel(MEM_Used);
desktop.Desktop.setHDUsed();
desktop.Desktop.setMEMUsed(memm.UsedMem());
desktop.Desktop.waitlistTimerRun();
if(Global.PCBLIST.Size()!=0)//PCB链表是否为空
{
int i=0,b;
int pcb[] = new int[9];
String cache;
if(timeBlocks==TIMEBLOCKS)
{
timeBlocks = 0;
if(currentID!=0)
desktop.Desktop.setCellValueToProcessTable(currentID,"就绪");
Global.PCBLIST.Change();
Global.PCBLIST.sort();
pcb = Global.PCBLIST.getLast();//获取PCB链表末尾结点PCB信息
currentIndex = pcb[8];
currentID = pcb[0];
int memadd1 = memm.getMemAddress(currentID,currentIndex);
try
{
Global.CPU.setRegister("ax",pcb[4]);
Global.CPU.setRegister("bx",pcb[5]);
Global.CPU.setRegister("cx",pcb[6]);
Global.CPU.setRegister("dx",pcb[7]);
Global.CPU.setRegister("pc",memadd1);
}
catch(NoSuchRegistException e1){}
}
try
{
if(Global.CPU.getPCLast()==Global.PAGE_SIZE+1)//读完一页内存
{
//换页处理程序加在这里
int add;
add = memm.getMemAddress(currentID,currentIndex+1);
//desktop.Desktop.addTextToStateTextField("80作业地址为"+currentIndex+"\n");
Global.CPU.setRegister("pc",add);
}
//desktop.Desktop.addTextToStateTextField("83作业地址为"+currentIndex+"\n");
Global.CPU.setRegister("ar",Global.CPU.getInt("pc"));
b = Global.CPU.getInt("ar");
cache = Global.MEMORY.getValue(Global.CPU.getPCFirst(),Global.CPU.getPCLast()).toString();
Global.CPU.setRegister("dr",cache);
Global.CPU.setRegister("ir",Global.CPU.getString("dr"));
//这里加入指令分析程序
desktop.Desktop.addTextToStateTextField("正在执行:"+currentID+"--"+Global.CPU.getString("ir")+"\n");
this.Analysis(Global.CPU.getString("ir"));
int first = Global.CPU.getPCFirst();
int last = Global.CPU.getPCLast();
int row = first*Global.PAGE_SIZE+last;
//桌面显示信息
if(currentID!=0)
{
desktop.Desktop.setCellValueToProcessTable(currentID,"运行");
desktop.Desktop.setCellValueToProcessTable(currentID,currentIndex);
}
if(follow)
{
desktop.Desktop.PSSetSelectRow(currentID);
}
desktop.Desktop.setSelectRow(row-1);
Global.CPU.IntPC();
int ppp[] = Global.PCBLIST.Get(Global.PCBLIST.getIndex(currentID));
pcb[0] = currentID;
pcb[1] = 0;
pcb[2] = ppp[2];
pcb[3] = Global.CPU.getInt("pc");
pcb[4] = Global.CPU.getInt("ax");
pcb[5] = Global.CPU.getInt("bx");
pcb[6] = Global.CPU.getInt("cx");
pcb[7] = Global.CPU.getInt("dx");
pcb[8] = currentIndex+1;
//desktop.Desktop.addTextToStateTextField("111作业地址为"+currentIndex+"\n");
Global.PCBLIST.setLast(pcb);
timeBlocks++;
}
catch(NoSuchRegistException e1){}
}//end if//PCB链表是否为空
else {timeBlocks = TIMEBLOCKS;}
}
/***************************************************
*功能描述: 指令分析
*参数说明: instruction:输入的指令
***************************************************/
private void Analysis(String instruction)
{
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(instruction," ");
int count = tokenizer.countTokens();
String ss[] = new String[count];
//if(ss.length!=1)
//{
for(int i=0;i<count;i++)
{
ss[i] = tokenizer.nextToken();
}
try
{
if(ss[1].equals("end"))
{
//desktop.Desktop.addTextToStateTextField("正在运行作业"+currentID+"作业地址为"+currentIndex+"-"+Global.CPU.getPCFirst()+"-"+Global.CPU.getPCLast()+"\n");
//一个作业结束处理程序在此加入
desktop.Desktop.setCellValueToProcessTable(currentID,"完成");
desktop.Desktop.setCellValueToProcessTable(currentID,currentIndex+1);
desktop.Desktop.removeProcess(currentID);
Global.PCBLIST.Remove(currentID);
memm.Delete(currentID);
//System.out.println("作业"+currentID+"已经做完"+currentIndex);
//desktop.Desktop.addTextToStateTextField("正在运行作业"+currentID+"完成"+"\n");
if(Global.PCBLIST.Size()>0)
{
int last[] = new int[Global.PCBLIST.Size()];
last = Global.PCBLIST.getLast();
currentID = last[0];
currentIndex = last[8]-1;
}
else//链表中所有作业都执行完闭
{
desktop.Desktop.ReFlashTable();//刷新虚拟内存table
currentID = 0;
currentIndex = 0;
}
}
/*else if(ss[1].equals("set")||ss[1].equals("SET"))
{
try
{
if(ss[2].equals("ax")||ss[2].equals("AX"))
{
Global.CPU.setRegister("ax",);
}
if(ss[2].equals("bx")||ss[2].equals("BX"))
{
desktop.Desktop.addTextToScreenTextField("作业"+currentID+"输出:"+Global.CPU.getInt("BX"));
}
if(ss[2].equals("cx")||ss[2].equals("CX"))
{
desktop.Desktop.addTextToScreenTextField("作业"+currentID+"输出:"+Global.CPU.getInt("CX"));
}
if(ss[2].equals("dx")||ss[2].equals("DX"))
{
desktop.Desktop.addTextToScreenTextField("作业"+currentID+"输出:"+Global.CPU.getInt("DX"));
}
else
{
desktop.Desktop.addTextToScreenTextField("作业"+currentID+"输出:"+ss[2]);
}
}
catch(NoSuchRegistException e){desktop.Desktop.addTextToStateTextField(""+e+"\n");}
}
else if(ss[1].equals("add")||ss[1].equals("ADD"))
{
try
{
Global.CPU.ADD(ss[2],ss[3]);
}
catch(myException e1){desktop.Desktop.addTextToStateTextField(""+e1+"\n");}
}
else if(ss[1].equals("sub")||ss[1].equals("SUB"))
{
try
{
Global.CPU.SUB(ss[2],ss[3]);
}
catch(myException e1){desktop.Desktop.addTextToStateTextField(""+e1+"\n");}
}
else if(ss[1].equals("mul")||ss[1].equals("MUL"))
{
try
{
Global.CPU.MUL(ss[2],ss[3]);
}
catch(myException e1){desktop.Desktop.addTextToStateTextField(""+e1+"\n");}
}
else if(ss[1].equals("div")||ss[1].equals("DIV"))
{
try
{
Global.CPU.DIV();
}
catch(myException e1){desktop.Desktop.addTextToStateTextField(""+e1+"\n");}
}*/
else if(ss[1].equals("out"))
{
try
{
if(ss[2].trim().equals("ax")||ss[2].trim().equals("AX"))
{
desktop.Desktop.addTextToScreenTextField("作业"+currentID+"输出:"+Global.CPU.getInt("AX")+"\n");
}
else if(ss[2].trim().equals("bx")||ss[2].trim().equals("BX"))
{
desktop.Desktop.addTextToScreenTextField("作业"+currentID+"输出:"+Global.CPU.getInt("BX")+"\n");
}
else if(ss[2].trim().equals("cx")||ss[2].trim().equals("CX"))
{
desktop.Desktop.addTextToScreenTextField("作业"+currentID+"输出:"+Global.CPU.getInt("CX")+"\n");
}
else if(ss[2].trim().equals("dx")||ss[2].trim().equals("DX"))
{
desktop.Desktop.addTextToScreenTextField("作业"+currentID+"输出:"+Global.CPU.getInt("DX")+"\n");
}
else
{
desktop.Desktop.addTextToScreenTextField("作业"+currentID+"输出:"+ss[2]+"\n");
}
}
catch(NoSuchRegistException e){desktop.Desktop.addTextToStateTextField(""+e+"\n");}
}
else
{
currentIndex = Integer.parseInt(ss[0]);
if(ss[1].equals("div")||ss[1].equals("DIV"))
Global.CPU.Calculate(ss[1],ss[1],ss[1]);
else
Global.CPU.Calculate(ss[1],ss[2],ss[3]);
//System.out.println("正在运行作业"+currentID+"作业地址为"+ss[0]);
//desktop.Desktop.addTextToStateTextField("AX="+Global.CPU.getInt("AX")+"\n");
}
}
catch(myException e){desktop.Desktop.addTextToStateTextField(""+e+"\n");}
}
/**************************************************
*功能描述: 换作业
*参数说明: id:作业ID pc:
***************************************************/
public void ChangeJob(int id,int pc,int ax,int bx,int cx,int dx)
{
SystemTimer.stop();
currentID = id;
currentIndex = pc;
int add = memm.getMemAddress(currentID,currentIndex);
try
{
Global.CPU.setRegister("ax",ax);
Global.CPU.setRegister("bx",bx);
Global.CPU.setRegister("cx",cx);
Global.CPU.setRegister("dx",dx);
Global.CPU.setRegister("pc",add);
}
catch(NoSuchRegistException e){}
SystemTimer.start();
}
public void ChangeJob(int id,int pc,float ax,float bx,float cx,float dx)
{
SystemTimer.stop();
currentID = id;
currentIndex = pc;
int add = memm.getMemAddress(currentID,currentIndex);
try
{
Global.CPU.setRegister("ax",ax);
Global.CPU.setRegister("bx",bx);
Global.CPU.setRegister("cx",cx);
Global.CPU.setRegister("dx",dx);
Global.CPU.setRegister("pc",add);
}
catch(NoSuchRegistException e){}
SystemTimer.start();
}
/******************************************************
*功能描述: 挂起一个在就绪队列中的作业
*参数说明: id:作业ID
******************************************************/
public void runToHangUP(int id)
{
int index = Global.PCBLIST.getIndex(id);
int tem[] = new int[Global.PCBLIST.PCBSize()];
tem = Global.PCBLIST.Get(index);
if(index+1==Global.PCBLIST.Size())//挂起的是当前执行的作业
{
Global.PCBLIST.Remove(id);
//System.out.println(Global.PCBLIST.Size());
int value[] = new int[Global.PCBLIST.PCBSize()];
value = Global.PCBLIST.getLast();
currentID = value[0];
currentIndex = value[8];
Global.PCBINTERUPT.addFirst(tem);
memm.Clear(id);
}
else//挂起的不是当前执行的作业
{
Global.PCBLIST.Remove(id);
Global.PCBINTERUPT.addFirst(tem);
memm.Clear(id);
}
desktop.Desktop.ReFlashTable();//刷新虚拟内存table
}
/******************************************************
*功能描述: 重新运行一个在挂起队列中的作业
*参数说明: id:作业ID
******************************************************/
public void hangUPToRun(int id)
{
if(Global.MEMORY.count()>=Global.PAGES)
{
int add;
int index = Global.PCBINTERUPT.getIndex(id);
int tem[] = new int[Global.PCBLIST.PCBSize()];
tem = Global.PCBINTERUPT.Get(index);
Global.PCBINTERUPT.Remove(index);
add = memm.insertPage(id,tem[8]);
//tem[3] = add;
Global.PCBLIST.addFirst(tem);
}
else {desktop.Desktop.addTextToStateTextField("没有内存空间了\n");}
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -