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

📄 cpuview.java

📁 这里是我在学校时所写的一些程序
💻 JAVA
字号:
/*
 * @(#)CpuView.java 1.0 03/12/20
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_1\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */

//CpuView.java
//Very Simple CPU View
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class CpuView extends JFrame{
	private RandomAccessFile input;
	private String mem[];
	private StringBuffer buf;
	private JTextArea output;
	private JTextField inPC,display;
	protected JButton doReset,doLoad,doStep;
	protected JPanel buttonPanel,textPanel;
	private String ar,dr,ir,ac;
	private int pc=0;
	private int f1=1,f2=0,f3=0,ad1=0,ad2=0,an1=0,an2=0,j1=0,i1=0,first=1;

	public CpuView()
	{

          super("Very Simple CPU Simulator");
 		  buf=new StringBuffer();

          mem=new String[64];
          for(int i=0;i<64;i++)
          	mem[i]="00000000";

          ar="000000";
          dr="00000000";
          ir="00";
          ac="00000000";
          pc=0;

          JMenuBar bar = new JMenuBar(); //create menubar
          setJMenuBar(bar); //set the menubar for the JFrame

          JMenu fileMenu = new JMenu("File");
          fileMenu.setMnemonic('F');
          JMenuItem openItem = new JMenuItem("Open...");
          openItem.setMnemonic('O');
		  openItem.addActionListener(
			 new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					openFile();
                }
             }
          );
          fileMenu.add(openItem);

          JMenuItem exitItem=new JMenuItem("Exit");
          exitItem.setMnemonic('x');
          exitItem.addActionListener(
          	new ActionListener(){
          		public void actionPerformed(ActionEvent e)
          		{
          			System.exit(0);
          		}
          	}
          );
          fileMenu.add(exitItem);

          bar.add(fileMenu);	//add File menu

          //create the Run menu
          JMenu simMenu=new JMenu("Simulate");
          simMenu.setMnemonic('S');
          JMenuItem resetItem=new JMenuItem("Reset");
          resetItem.setMnemonic('R');
          resetItem.addActionListener(
          	new ActionListener(){
          		public void actionPerformed(ActionEvent e)
          		{
          			for(int i=0;i<100;i++)
          				mem[i]="00000000";
          		}
          	}
          );
          simMenu.add(resetItem);

          JMenuItem loadItem=new JMenuItem("Load");
          loadItem.setMnemonic('L');
          loadItem.addActionListener(
			 new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					openFile();
                }
             }
          );
          simMenu.add(loadItem);

          JMenuItem stepItem=new JMenuItem("Step");
          stepItem.setMnemonic('S');
          stepItem.addActionListener(
			 new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					if(first==1){
						output.setText("");
						buf.append("AR\tDR\tPC\tAC\tIR\n");
						output.append(buf.toString());
						first=0;
					}

					if(f1==1)
					fetch1();
					else if(f2==1)
					fetch2();
					else if(f3==1)
					fetch3();
					else if(ir.equals("00")&&ad1==1)
					add1();
					else if(ad2==1)
					add2();
					else if(ir.equals("01")&&an1==1)
					and1();
					else if(an2==1)
					and2();
					else if(ir.equals("10")&&j1==1)
					jmp1();
					else if(ir.equals("11")&&i1==1)
					inc1();
					buf.append(ar+"\t"+dr+"\t"+pc+"\t"+ac+"\t"+ir+"\n");
					output.setText(buf.toString());

					//step run
					//openFile();
                }
             }
          );
          simMenu.add(stepItem);

          bar.add(simMenu);	//add Simulate menu

          JMenu helpMenu = new JMenu("Help");
          helpMenu.setMnemonic('H');
          JMenuItem aboutItem = new JMenuItem("About...");
          aboutItem.setMnemonic('A');
		  aboutItem.addActionListener(
			 new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					JOptionPane.showMessageDialog(CpuView.this,
						"Very Simple CPU Simulator\nHao Zhigang\t2003.12",
						"About",JOptionPane.PLAIN_MESSAGE);
                }
             }
          );
          helpMenu.add(aboutItem);

          bar.add(helpMenu);	//add Help menu

          doReset=new JButton("Reset");
          doLoad=new JButton("Load");
          doStep=new JButton("Step");
          inPC=new JTextField();
          buttonPanel=new JPanel();
          buttonPanel.setLayout(new GridLayout(3,2));
          buttonPanel.add(doReset);
          buttonPanel.add(doLoad);
          buttonPanel.add(doStep);
          buttonPanel.add(inPC);
          inPC.setText("Enter incept memory address here");
 		  display=new JTextField();
 		  display.setText("Show the operate");
          
          inPC.addActionListener(
          	new ActionListener(){
          		public void actionPerformed(ActionEvent e)
          		{
          			pc=BDchange(e.getActionCommand());
          			inPC.setText("incept memory address:"+e.getActionCommand());
          			first=1;
          		}
          	}
          );

          doReset.addActionListener(
          	new ActionListener(){
          		public void actionPerformed(ActionEvent e)
          		{
          			for(int i=0;i<100;i++)
          				mem[i]="00000000";
          		}
          	}
          );

          doLoad.addActionListener(
			 new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					openFile();
                }
             }
          );
         
          doStep.addActionListener(
			 new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					if(first==1){
						output.setText("");
						buf.append("AR\tDR\tPC\tAC\tIR\n");
						output.append(buf.toString());
						first=0;
					}

					if(f1==1)
					fetch1();
					else if(f2==1)
					fetch2();
					else if(f3==1)
					fetch3();
					else if(ir.equals("00")&&ad1==1)
					add1();
					else if(ad2==1)
					add2();
					else if(ir.equals("01")&&an1==1)
					and1();
					else if(an2==1)
					and2();
					else if(ir.equals("10")&&j1==1)
					jmp1();
					else if(ir.equals("11")&&i1==1)
					inc1();
					buf.append(ar+"\t"+dr+"\t"+pc+"\t"+ac+"\t"+ir+"\n");
					output.setText(buf.toString());
                }
             }
          );

          output=new JTextArea();
          Container c=getContentPane();
          ScrollPane p=new ScrollPane();
          p.add(output);
          c.add(buttonPanel,BorderLayout.NORTH);
          c.add(p,BorderLayout.CENTER);
          c.add(display,BorderLayout.SOUTH);


          addWindowListener(
          	new WindowAdapter(){
          		public void windowClosing(WindowEvent e)
          		{
          			if(input!=null)
          				closeFile();

          			System.exit(0);
          		}
          	}
          );

          getContentPane().setBackground(Color.lightGray);

          setSize(800,600);
          show();
    }

    private void fetch1()
    {
    	ar=DBchange(pc);
    	if(ar.length()==1)
    		ar="0"+ar;
    	f1=0;
    	f2=1;
    	display.setText("AR<--PC");
    }

    private void fetch2()
    {
    	dr=mem[BDchange(ar)];
    	pc++;
    	f2=0;
    	f3=1;
    	display.setText("DR<--M, PC<--PC+1");
    }

    private void fetch3()
    {
    	ir=dr.substring(0,2);
    	ar=dr.substring(2,8);
    	f3=0;
    	ad1=1;
    	an1=1;
    	j1=1;
    	i1=1;
    	display.setText("IR<--DR[7..6], AR<--DR[5..0]");
    }

    private void add1()
    {
    	dr=mem[BDchange(ar)];
    	ad1=0;
    	an1=0;
    	j1=0;
    	i1=0;
    	ad2=1;
    	display.setText("DR<--M");
    }

    private void add2()
    {
    	ac=DBchange(BDchange(ac)+BDchange(dr));
    	while(ac.length()<8)
    		ac="0"+ac;
    	ad2=0;
    	f1=1;
    	display.setText("AC<--AC+DR");
    }

    private void and1()
    {
    	dr=mem[BDchange(ar)];
    	an1=0;
    	ad1=0;
    	j1=0;
    	i1=0;
    	an2=1;
    	display.setText("DR<--M");
    }

    private void and2()
    {
    	ac=DBchange(BDchange(ac)&BDchange(dr));
    	while(ac.length()<8)
    		ac="0"+ac;
    	an2=0;
    	f1=1;
    	display.setText("AC<--AC^DR");
    }

    private void jmp1()
    {
    	pc=BDchange(dr.substring(2,8));
    	j1=0;
    	an1=0;
    	ad1=0;
    	i1=0;
    	f1=1;
    	display.setText("PC<--DR[5..0]");
    }

    private void inc1()
    {
    	ac=DBchange(BDchange(ac)+1);
    	while(ac.length()<8)
    		ac="0"+ac;
    	i1=0;
    	j1=0;
    	an1=0;
    	ad1=0;
    	f1=1;
    	display.setText("AC<--AC+1");
    }

    private void closeFile()
    {
    	try{
    		input.close();
    		System.exit(0);
    	}
    	catch(IOException e){
    		JOptionPane.showMessageDialog(this,
    			"Error closing file",
    			"Error",JOptionPane.ERROR_MESSAGE);
    		System.exit(1);
    	}
    }

    private void openFile()
    {
    	JFileChooser fileChooser=new JFileChooser();

    	fileChooser.setFileSelectionMode(
    		JFileChooser.FILES_ONLY);
    	int result=fileChooser.showOpenDialog(this);

    	//user clicked Cancel button on dialog
    	if(result==JFileChooser.CANCEL_OPTION)
    		return;

    	File fileName=fileChooser.getSelectedFile();

    	if(fileName==null || fileName.getName().equals(""))
    		JOptionPane.showMessageDialog(this,
    			"Invalid File Name",
    			"Invalid File Name",
    			JOptionPane.ERROR_MESSAGE);
    	else{
    		//open the file
    		try{
    			RandomAccessFile r=
    				new RandomAccessFile(fileName,"r");
    			int i=0;
    			while((mem[i]=r.readLine())!=null){
    				if(i<10)
    					buf.append("0"+i+"      "+mem[i]+"    "
    						+Integer.toHexString(BDchange(mem[i]))+"\t");
    				else
    					buf.append(i+"      "+mem[i]+"    "
    						+Integer.toHexString(BDchange(mem[i]))+"\t");
    				i++;
    				if(i%4==0)
    					buf.append("\n");
    				output.setText(buf.toString());
    			}
    		}
    		catch(IOException e){
    			JOptionPane.showMessageDialog(this,
    				"Error Opening File","Error",
    				JOptionPane.ERROR_MESSAGE);
    		}
    	}
    }

    private int BDchange(String s)
    {
    	return Integer.parseInt(s,2);
    }
    
    private String DBchange(int n)
    {
    	return Integer.toBinaryString(n);
    }

    public static void main(String args[])
    {
    	new CpuView();
    }
    
}



⌨️ 快捷键说明

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