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

📄 mipsgui.java

📁 MIPS 仿真器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				{
					
					if (pc.binstr[i].equals("fc000001"))
						flag=false;
					String ss="";
					try
					{
						ss = new NewHEX().toHEX(i*4,8)+":  ";	
					}
					catch (Exception ee)
					{
					}
					if (flag)
						ss+=ua.BtoS(pc.binstr[i],i,pc);
					else
					{
						if (pc.binstr[i].equals("fc000001"))
							ss+="END 00000004";
						else
						{
							int k=Integer.parseInt(pc.binstr[i],16);
							ss+=("DW "+k);
						}
					}
					pc.instr[i] = ss;  //store the instr
					mipsarea.append("<"+i+">\t"+ss+"\n"); 
				}
			}
		}


	}


	public int toInstr()
	{

			String s=mipsarea.getText();
			int index;
			index=s.indexOf("\n");
			String Instruction;
			int Linecount=0;
			Instruction=s.substring(0,index);
			int index2;
			index2=Instruction.indexOf(">");
			Instruction=Instruction.substring(index2+1).trim();
			s=s.substring(index+1);
			while(!Instruction.contains("END"))
			{
				  index=Instruction.trim().indexOf(":");
				  if (index!=-1)
				  {
					  pc.label.put(Instruction.trim().substring(0,index),Linecount);
					  Instruction=Instruction.trim().substring(index+1).trim();
				  }
				  pc.instr[Linecount]=Instruction;
				  Linecount++;
				  index=s.indexOf("\n");
				  Instruction=s.substring(0,index);
				  index2=Instruction.indexOf(">");
				  Instruction=Instruction.substring(index2+1).trim();
				  s=s.substring(index+1);
			}
 		    pc.instr[Linecount]="END 00000004";

			int j=Linecount+1;

	//////////////////Instruction END tag
			pc.Memory[Linecount*4]=(new Integer(-4)).byteValue();
			pc.Memory[Linecount*4+1]=(new Integer(0)).byteValue();
			pc.Memory[Linecount*4+2]=(new Integer(0)).byteValue();
			pc.Memory[Linecount*4+3]=(new Integer(1)).byteValue();
			
	//////////////////////////////////////
			String Datai;
		    index=s.indexOf("\n");
			if (index!=-1)
			{
				Datai=s.substring(0,index);
				s=s.substring(index+1);
				int datains=0;
				while(true)
				{
					index=Datai.trim().indexOf(":");
					if (index!=-1)
					{
						 pc.label.put(Datai.trim().substring(0,index),Linecount);
						 Datai=Datai.trim().substring(index+1).trim();
					}
					index=Datai.trim().indexOf(" ");
					Datai=Datai.trim().substring(index+1).trim();
					String value="";
					try{
						value=new NewHEX().toHEX(Integer.parseInt(Datai),8);
						////System.out.println(""+value+" "+(Linecount+datains+1)*4);
						pc.MemWrite(value,(Linecount+datains+1)*4);
						pc.instr[j++]="DW "+Datai;
						////System.out.println(""+(j-1)+" "+ pc.instr[j-1]);
					}
					catch (Exception ee){}
					datains++;
					index=s.indexOf("\n");
					if (index==-1) break;
					Datai=s.substring(0,index);
					s=s.substring(index+1);
				}
			}
			totalline=j;
			return Linecount;
	}

	public int run(int startli, int endli, int lc)
	{
	//execute
			int lin=0,oldlin=-1;
			for (lin=startli;oldlin!=endli && !(pc.instr[lin].contains("END"));lin++ )
			{
				oldlin=lin;
				try{
					lin=new MIPScompiler().compile(pc.instr[lin],lin,pc,this);
				}
				catch (Exception ee){}
				if (lin==10)
				{
					////System.out.println(lin+pc.instr[lin+1]);
				}
			}
			if (pc.instr[lin].toLowerCase().contains("end"))
			{
				stoprun=false;
				runarea.append("\nExecuting Complete!\n");
				return -1;
			}
			////System.out.println("CurrentIns="+lin);
			return lin;
	}

	public void LoadMIPS(File fi)
	{
			int nins=0;
			try{
				String Instruction;
				FileReader fr=new FileReader(fi);
				BufferedReader br=new BufferedReader(fr);
				mipsarea.setText(null);
				Instruction=br.readLine();
				////System.out.println(Instruction);
				while(Instruction!=null)
				{
					if (!Instruction.trim().equals(""))
					{
						mipsarea.append("<"+nins+">\t"+Instruction.trim()+"\n");
						nins++;
					}
					Instruction=br.readLine();
				}
			}
			catch (Exception ee){}
			mipsold=mipsarea.getText();
	}

	public void LoadBIN(File fi)
	{
			int nins=0;
			try{
				String Instruction_b;
				FileReader fr=new FileReader(fi);
				BufferedReader br=new BufferedReader(fr);
				binarea.setText(null);
				Instruction_b=br.readLine();
				////System.out.println(Instruction_b);
				while(Instruction_b!=null)
				{
					if (!Instruction_b.trim().equals(""))
					{
						binarea.append("<"+nins+">\t"+Instruction_b.trim()+"\n");
						nins++;
					}
					Instruction_b=br.readLine();
				}
			}
			catch (Exception ee){}
			binold=binarea.getText();
	}

	public void SaveMIPS(File fi)
	{
			int nins=0;
			try{
//				String Instruction;
				FileWriter fw=new FileWriter(fi);
				BufferedWriter bw=new BufferedWriter(fw);
                String text = mipsarea.getText();
				String text2="";
				int i=0;
				while (i<text.length())
				{
					if (text.charAt(i)=='<')
					{
						while(text.charAt(i)!='>')
							i++;
						i++;
					}
					if (text.charAt(i)=='\t' && text.charAt(i-1)=='>')
						i++;
					text2=text2+text.charAt(i);
					i++;
				}
				////System.out.println(text2);
                bw.write(text2, 0, text2.length());
                bw.flush();
                fw.close();
				mipsarea.setText(null);
				LoadMIPS(fi);
			}
			catch (Exception ee){}
	}

	public void SaveBIN(File fi)
	{
			int nins=0;
			try{
				FileWriter fw=new FileWriter(fi);
				BufferedWriter bw=new BufferedWriter(fw);
                String text = binarea.getText();
				String text2="";
				int i=0;
				while (i<text.length())
				{
					if (text.charAt(i)=='<')
					{
						while(text.charAt(i)!='>')
							i++;
						i++;
					}
					if (text.charAt(i)=='\t' && text.charAt(i-1)=='>')
						i++;
					text2=text2+text.charAt(i);
					i++;
				}
				////System.out.println(text2);
                bw.write(text2, 0, text2.length());
                bw.flush();
                fw.close();
				binarea.setText(null);
				LoadBIN(fi);
			}
			catch (Exception ee){}
	}
	public boolean ASM()
	{

		/*for (int i=0;i<4194305; i++)
			pc.binstr[i]="";*/
		if (!mipsarea.getText().equals(mipsold))
		{
			JOptionPane.showMessageDialog(null,"Please Save First!");
			return false;
		}
		else
		{
			pc.clear();
			binarea.setText(null);
			int lb=	toInstr();
			for (int i=0;i<totalline ;i++ )
			{
				////System.out.println(pc.instr[i]);
			}
			Asm aa = new Asm();
			////System.out.println(""+totalline);
			for (int i=0;i<totalline ;i++ )
			{
				String m = aa.StoB(pc.instr[i],i,pc);
				m = aa.BtoH(m);
				if (i<lb)
				{
					try{
						pc.MemWrite(m,i*4);
						////System.out.println(i*4+":"+new NewHEX().toHEX(pc.Memory[i*4],2)+new NewHEX().toHEX(pc.Memory[i*4+1],2)+new NewHEX().toHEX(pc.Memory[i*4+2],2)+new NewHEX().toHEX(pc.Memory[i*4+3],2));
					}
					catch (Exception ee){}
				}
				pc.binstr[i] = m;  //store the code
				binarea.append("<"+i+">\t"+m+"\n"); //display the code
			}
			return true;
		}

	}

	public static void main(String[] args) 
	{
		MIPSGUI mipsg=new MIPSGUI();
		mipsg.addWindowListener(
			new WindowAdapter() 
			{
				public void windowClosing(WindowEvent e)
				{
					System.exit(0);
				}
			}
		);
		mipsg.show();
/*		JPanel pane=new JPanel();
		pane.add(m);
		setContentPane(pane);
		pane.show();*/
	}
}

⌨️ 快捷键说明

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