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

📄 cpu.cs

📁 os 课程设计不含文档 123456789
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace os
{
    public class cpu:jincheng
    {
        public int yxj;
        private int psw;//程序状态字寄存器
        private int eax;//数值寄存器
        private int ip;//程序计数器
        private int[] pt;//pagetable页表首地址
        char[] code = new char[4];//指令寄存器
        private ram ram_;//内存
        private process procontrol;//进程
        #region 寄存器
        public int PSW
        {
            get
            {
                return psw;
            }
            set
            {
                psw = value;
            }
        }

        public int EAX
        {
            get
            {
                return eax;
            }
            set
            {
                eax = value;
            }
        }
        public int IP
        {
            get
            {
                return ip;
            }
            set
            {
                ip = value;
            }
        }
        public int[] PT
        {
            get
            {
                return pt;
            }
            set
            {
                pt = value;
            }
        }
        public char[] CODE
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }
        #endregion
        public cpu(ram Ram, process Procontrol)//cpu构造函数
        {
            ram_ = Ram;
            procontrol = Procontrol;
            pt = new int[32];
            psw = 0;
            eax = 0;
            ip = 0;
            pt[0] = 0;
        }
        public void loadcode()//装载指令
        {
            for (int i = 0; i < 4; i++)
            {
                ram_.readdata(pt,ip++,ref code[i]);
            }
        }
        public string dqzhl()
        {
            string zhl = Convert.ToString(code[0]) + Convert.ToString(code[1]) + Convert.ToString(code[2]) + Convert.ToString(code[3]);
            return zhl;
        }
        public string zhjjg()
        {
            string jg = Convert.ToString(this.eax);
            return jg;
        }
        public bool executecode()//执行指令函数
        {
            char code0 = code[0];
            char code1 = code[1];
            char code2 = code[2];
            char code3 = code[3];
            if (code0 == 'e')
            {
                psw = 1;     //1号中断为进程结束		  
                return true;
            }
            if (code3 != ';')
            {
                return false;
            }
            if (code0 == 'x')
            {
                if (code1 == '=') //遇到赋值语句
                {
                    if (code2 > '9' || code2 < '0')  //遇到非数字字符
                        return false;
                    this.eax = int.Parse(code2.ToString());
                    return true;
                }
                if (code1 == '+' && code2 == '+')  //遇到加1语句
                {
                    this.eax++;
                    return true;
                }
                if (code1 == '-' && code2 == '-')  //遇到减1语句
                {
                    this.eax--;
                    return true;
                }
                return false;
            }
            else if (code0 == '!')
            {
                if (code1 >= 'a' && code1 <= 'c')
                    if (code2 >= '0' && code2 <= '9')   //是申请设备的指令
                        psw = 3;                        //3号中断为申请设备
                return true;
            }
            else
                return false;
        }
        public bool CPU()//cpu运行函数
        {
            //if (procontrol.ProCount > 0)
            //{
                loadcode();
                if (executecode() == false)
                {
                    psw = 2;
                    return false;
                }

                if (stop() == true)           //处理中断函数
                    return false;
                procontrol.GOperation();
            //}
            return true;
        }
        public bool stop()//中断函数
        {
            int error = PSW;
            switch (error)
            {
                case 0:      //无中断	 
                    return false;
                case 1:      //进程结束
                    procontrol.DelProcess(procontrol.currentpro);
                    procontrol.currentpro = NULL;
                    break;
                case 2:      //错误代码
                    procontrol.DelProcess(procontrol.currentpro);
                    procontrol.currentpro = NULL;
                    break;
                case 3:     //申请设备
                    procontrol.RequestDeive(code[1], code[2]);
                    break;
                case 4:    //优先级不够被抢占
                    procontrol.ChangeProcess();
                    break;
            }
            psw = 0;
            procontrol.GOperation();
            return true;
        }
    }
}

⌨️ 快捷键说明

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