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

📄 mipsgui.java

📁 MIPS 仿真器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;

class MIPSGUI extends JFrame implements ActionListener
{
	TextArea mipsarea,binarea,runarea;
	JButton Loadmips,Savemips,Loadbin,Savebin,Asm,UnAsm,ViewReg,ViewMem,RuntoEnd,RuntoPoint,Go,GiveUp,Notepad,Calc,Help;
	JTextField StopPoint;
//	JLabel sp=new JLabel("");
	JLabel str1,str2;
	PC pc;
	int currentins,nextins,lc,totalline;
	boolean stoprun;
	String mipsold;
	String binold;

	public MIPSGUI()
	{
		super("MIPS Assembler Simulator v0.6.24.1014 beta");
	    int width=1024;                           
		int height=768;
		setSize(width,height);
	    mipsarea=new TextArea(40,38);
		binarea=new TextArea(40,38);
		runarea=new TextArea(40,54);
		StopPoint=new JTextField(8);
		Loadmips=new JButton("            Load MIPS            ");
		Savemips=new JButton("            Save MIPS            ");
		Loadbin=new JButton("             Load bin              ");
		Savebin=new JButton("             Save bin             ");
		Asm=new JButton("             Asm             ");
		UnAsm=new JButton("             UnAsm            ");
		ViewReg=new JButton("                View Regs                   ");
		ViewMem=new JButton("                View Memory                 ");
		RuntoEnd=new JButton("Run the Whole MIPS Program");
		RuntoPoint=new JButton("Run MIPS Manually According to the Stop Points");
		str1=new JLabel("( Set Next Stop Point: Line Number = ");
		str2=new JLabel(" )");
		Go=new JButton("Go");
		GiveUp=new JButton("Give Up");
		Notepad=new JButton("                  Notepad                  ");
		Calc=new JButton("                Calculator                ");
		Help=new JButton("                 Help                 ");

		StopPoint.addActionListener(this);
		Loadmips.addActionListener(this);
		Savemips.addActionListener(this);
		Loadbin.addActionListener(this);
		Savebin.addActionListener(this);
		Asm.addActionListener(this);
		UnAsm.addActionListener(this);
		ViewReg.addActionListener(this);
		ViewMem.addActionListener(this);
		RuntoEnd.addActionListener(this);
		RuntoPoint.addActionListener(this);
		Go.addActionListener(this);
		GiveUp.addActionListener(this);
		Notepad.addActionListener(this);
		Calc.addActionListener(this);
		Help.addActionListener(this);
		JPanel  pane=new JPanel();
		pc=new PC();
		currentins=-1;
		stoprun=false;
		mipsold="";
		binold="";

		pane.add(mipsarea);
//		mipsarea.setBounds(0,0,245,435); 		
		pane.add(binarea);
//		binarea.setBounds(0,0,245,435);		
//		JPanel  pane2=new JPanel();
		pane.add(runarea);
//		runarea.setBounds(700,500,100,100);	
		pane.add(Loadmips);
		pane.add(Savemips);
		pane.add(Asm);
//		pane.add(sp);
		pane.add(UnAsm);
		pane.add(Loadbin);
		pane.add(Savebin);
//		pane.add(sp);
/*		pane.add(str);
		pane.add(StopPoint);
		pane.add(sp);*/
		pane.add(RuntoEnd);
//		pane.add(sp);
		pane.add(RuntoPoint);
		pane.add(str1);
		pane.add(StopPoint);
		pane.add(Go);
		pane.add(GiveUp);
		pane.add(str2);
//		pane.add(sp);
		pane.add(ViewReg);
//		pane.add(sp);
		pane.add(ViewMem);
		pane.add(Notepad);
		pane.add(Calc);
		pane.add(Help);
		add(pane);
		

	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource()==Notepad)
		{
			String path = "notepad"; 
			try{ 
				Runtime.getRuntime().exec(path); 
			}
			catch(Exception ex){}
		}
		if (e.getSource()==Calc)
		{
			String path = "calc"; 
			try{ 
				Runtime.getRuntime().exec(path); 
			}
			catch(Exception ex){}
		}
		if (e.getSource()==ViewReg)
		{
			RegViewer rv=new RegViewer(pc);
			rv.show();
		}
		if (e.getSource()==Help)
		{
			HelpViewer hv=new HelpViewer();
			hv.show();
		}
		if (e.getSource()==ViewMem)
		{
			MemViewer mv=new MemViewer(pc,lc);
			mv.show();
		}
		if (e.getSource()==Loadmips)
		{		
//			////System.out.println(new JFileChooser().getCurrentDirectory().getName());
			JFileChooser jfc=new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter("MIPS Assemble Files(.Asm;.Mips;.Txt)", "mips", "asm","txt");
		    jfc.setFileFilter(filter);

			if (jfc.showOpenDialog(Loadmips)== JFileChooser.APPROVE_OPTION)
			{
				File fi;
				fi=jfc.getSelectedFile();
				////System.out.println(jfc.getName(fi));
				LoadMIPS(fi);
			}
		}

		if (e.getSource()==Loadbin)
		{
			JFileChooser jfc=new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter("MIPS Binary Files(.Bin;.Dat;.Txt)", "bin", "dat","txt");
		    jfc.setFileFilter(filter);

			if (jfc.showOpenDialog(Loadbin)== JFileChooser.APPROVE_OPTION)
			{
				File fi;
				fi=jfc.getSelectedFile();
				////System.out.println(jfc.getName(fi));
				LoadBIN(fi);
			}
		}

		if (e.getSource()==RuntoEnd)
		{
			if (mipsarea.getText().equals("") && !binarea.getText().equals(""))
			{
				JOptionPane.showMessageDialog(null,"Please UnAsm First!");
			}
			else
			{
				runarea.setText(null);
				StopPoint.setText(null);
/*				if (!mipsarea.getText().equals(mipsold))
				{
					JOptionPane.showMessageDialog(null,"Please Save First!");
				}		
				else*/
				{
					if (ASM())
					{
						stoprun=false;
						lc=toInstr();
						currentins=-1;
						nextins=lc;
						int r=run(currentins+1,nextins-1,lc);
					}
				}
			}
		}
		if (e.getSource()==RuntoPoint)
		{
			if (mipsarea.getText().equals("") && !binarea.getText().equals(""))
			{
				JOptionPane.showMessageDialog(null,"Please UnAsm First!");
			}
			else
			{
				runarea.setText(null);
/*				if (!mipsarea.getText().equals(mipsold))
				{
					JOptionPane.showMessageDialog(null,"Please Save First!");
				}
				else*/
				if (ASM())
				{
					stoprun=true;
					lc=toInstr();
					currentins=-1;
					runarea.append("Please Set the Stop Point with a Line Number, and then\nPress 'Go' to Continue and 'Give Up' to End\n\n");
				}
			}
		}
		if (e.getSource()==GiveUp)
		{
			stoprun=false;
			////System.out.println(stoprun);
			StopPoint.setText(null);
		}
		if (stoprun==true && e.getSource()==Go)
		{
				////System.out.println(stoprun);
				String s=StopPoint.getText();
				int stp=Integer.parseInt(s);
				runarea.append("\nThe Next Stop Point is at LINE "+stp+", \nthe program will go on until reach the point or the end.\n");
				nextins=stp;
				currentins=run(currentins+1,nextins,lc)-1;
				////System.out.println(currentins);
				if (stoprun==true)
					runarea.append("\nThe Execution of the Program Stops, and you can set another point.\n");
//				currentins=nextins;
		}
		if(e.getSource()==Savemips)
		{
			JFileChooser jfc=new JFileChooser("");
			FileNameExtensionFilter filter = new FileNameExtensionFilter("MIPS Assemble Files(.Asm)", "asm");
		    jfc.setFileFilter(filter);

			if (jfc.showSaveDialog(Savemips)== JFileChooser.APPROVE_OPTION)
			{
				File fi;
				fi=jfc.getSelectedFile();
				String fn;
				if (fi.getPath().contains("."))
					fn=fi.getPath();
				else fn=fi.getPath()+".asm";
				File f2=new File(fn);
				////System.out.println(jfc.getName(fi));
				SaveMIPS(f2);
			}
		}
		if(e.getSource()==Savebin)
		{
			JFileChooser jfc=new JFileChooser("");
			FileNameExtensionFilter filter = new FileNameExtensionFilter("MIPS Binary Files(.Bin)", "bin");
		    jfc.setFileFilter(filter);
			if (jfc.showSaveDialog(Savebin)== JFileChooser.APPROVE_OPTION)
			{
				File fi;
				fi=jfc.getSelectedFile();
				String fn;
				if (fi.getPath().contains("."))
					fn=fi.getPath();
				else fn=fi.getPath()+".bin";

//				////System.out.println(fi.getPath()+".bin");
				File f2=new File(fn);
				////System.out.println(jfc.getName(fi));
				SaveBIN(f2);
			}
		}
		if(e.getSource()== Asm)
		{
			ASM();
		}
		if(e.getSource()== UnAsm)
		{
			for (int i=0;i<4194305; i++)
				pc.instr[i]="";
			if (!binarea.getText().equals(binold))
			{
				JOptionPane.showMessageDialog(null,"Please Save First!");
			}
			else
			{
				mipsarea.setText(null);
				Unasm ua = new Unasm();
				String s=binarea.getText(),s2;
				int index,index2,ti=0;
				index=s.indexOf("\n");
				while(index!=-1)
				{
					s2=s.substring(0,index);
					s=s.substring(index+1);
					index2=s2.indexOf(">");
					s2=s2.substring(index2+1).trim().toLowerCase();
					pc.binstr[ti++]=s2;
					////System.out.println(""+(ti-1)+" "+s2+"\""+s2.charAt(0)+"\"");
					index=s.indexOf("\n");
				}
				boolean flag=true;
				for (int i=0;i<ti;i++ )

⌨️ 快捷键说明

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