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

📄 box.java

📁 熟悉非常简单CPU模拟器 1、将所给模拟器的源程序编译成执行程序。 2、运行并观察非常简单CPU模拟器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//import javarchia.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//import java.io.*;
import java.net.*;

public
class Box
   extends java.applet.Applet
   //extends JApplet
   implements ActionListener,MouseMotionListener 
{
   // Declare variables
	String AR = new String("000000");    //declare register values
	String PC = new String("000000");
	String DR = new String("00000000");
	//String TR = new String("00000000");
	String IR = new String("00");
	//String R  = new String("00000000");
	String AC = new String("00000000");
	//String Z  = new String("0");

	long ARint=0;
	long PCint=0;
	long DRint=0;
//	long TRint=0;
	long IRint=0;
	//long Rint=0;
	long ACint=0;
	//long Zint=0;
        
        public Object lastEventSource=new Object();

   public void init()
   {
           
      //initialize gui components
      FlowLayout flo = new FlowLayout( FlowLayout.CENTER, 10, 30 );
      //  FlowLayout flo2 = new FlowLayout( FlowLayout.CENTER);
      //add components to screen
      programBox = new TextArea( 18, 36 );

      setLayout( new FlowLayout(FlowLayout.CENTER,10,25) );
      setBackground(Color.white);
      memoryLocation.setText("0");
     //Cont.setLayout(flo2);
      //Cont.add(Msg);
      //Cont.add( label );
     // Cont.add( memoryLocation );
     // Cont.add( space2 );
     // Cont.add( cuType );
      
     // Cont.validate();
      
      add( programBox);
      add( label);
      add( memoryLocation);
      add( space2);
      add( cuType);
      //add(Cont);
      
      add( hardWiredCU);
      add( microCodedCU);

      add( space3 );
      add( space );
      add( assembleButton );
      add( viewResultsButton );
      add( viewTraceButton );
      add( viewCPUButton );
      add( viewMemoryButton );
//      add( viewALUButton );
      add( viewControlUnitButton );
      add( clearMemoryButton );
      add( viewBreakpointsButton );
      add( helpButton );
      add(aboutButton );
      add(sampleButton);
      add(Msg);
      Msg.setEditable(false);
      Msg.setBackground(Color.white);

      assembleButton.addActionListener( this );

      viewResultsButton.setEnabled( false );
      viewResultsButton.addActionListener( this );

      viewCPUButton.addActionListener( this );
      viewTraceButton.addActionListener( this );

      viewMemoryButton.addActionListener( this );

//      viewALUButton.addActionListener( this );

      viewControlUnitButton.addActionListener( this );

      clearMemoryButton.addActionListener( this );

      viewBreakpointsButton.addActionListener( this );

      helpButton.addActionListener( this );
      aboutButton.addActionListener( this );
      sampleButton.addActionListener( this );
      
      
      programBox.addMouseMotionListener(this);
      
      label.addMouseMotionListener(this);
      memoryLocation.addMouseMotionListener(this);
      
      cuType.addMouseMotionListener(this);
      hardWiredCU.addMouseMotionListener(this);
      microCodedCU.addMouseMotionListener(this);
      
      assembleButton.addMouseMotionListener(this);
      viewResultsButton.addMouseMotionListener(this);
      viewCPUButton.addMouseMotionListener(this);
      viewMemoryButton.addMouseMotionListener(this);
      viewControlUnitButton.addMouseMotionListener(this);
      clearMemoryButton.addMouseMotionListener(this);
      viewBreakpointsButton.addMouseMotionListener(this);
      helpButton.addMouseMotionListener(this);
      aboutButton.addMouseMotionListener(this);
      sampleButton.addMouseMotionListener( this );
      this.addMouseMotionListener(this);
        
        
      // Define object and pass this
      CPUBox = new CPU( this, "Register Section" );

      hardWiredCU.addItemListener(

	    new ItemListener()
	    {

	       public void itemStateChanged( ItemEvent e )
	       {

		  if ( ( ! CPUBox.isRunning() ) && ( hardWiredCU.
		     getState() ) )
		  {
                     //System.out.println( "DEBUG ==> hw cu selected" );

                     if ( CPUBox.mcu.isShowing() )
		     {
			CPUBox.controlUnit.setVisible( true );
                        CPUBox.mcu.setVisible( false );
		     }

                     CPUBox.controlUnit.setActive( true );
                     CPUBox.mcu.setActive( false );

		     CPUBox.setControlUnitType( CPU.CU_HARDWIRED );
		  }

	       }

	    }

	 );
		  
      microCodedCU.addItemListener(

	    new ItemListener()
	    {

	       public void itemStateChanged( ItemEvent e )
	       {

		  if ( ( ! CPUBox.isRunning() ) && ( microCodedCU.
		     getState() ) )
		  {
                     //System.out.println( "DEBUG ==> mc cu selected" );

		     if ( CPUBox.controlUnit.isShowing() )
		     {
                        CPUBox.controlUnit.setVisible( false );
                        CPUBox.mcu.setVisible( true );
		     }

                     CPUBox.controlUnit.setActive( false );
                     CPUBox.mcu.setActive( true );

		     CPUBox.setControlUnitType( CPU.CU_MICROCODED );
		  }

	       }

	    }

	 );

   }

   public void stop()
   {
//      CPUBox.hideAll();

//      if ( resultsDialog != null )
//      {
//         resultsDialog.dispose();
//      }

   }

   public void destroy()
   {
      //CPUBox.dispose();

      if ( resultsDialog != null )
      {
         resultsDialog.dispose();
      }

   }

   public void paint( Graphics g )
   {
      Font f = new Font( "SansSerif", Font.BOLD, 12 );

      g.setFont( f );
      g.drawString( "Please Enter Code Below", 90, 20 );
   }

   public void mouseMoved(MouseEvent e){
           
           Object eventSource = e.getSource();
           if(!eventSource.equals(lastEventSource)){
                if(eventSource == programBox){
                        Msg.setText("This text field is used to input the program for the Very Simple CPU.");
                }else if(eventSource == label || eventSource == memoryLocation){
                        Msg.setText("Please enter the memory location(0-63), starting from where you wish to have\n the program put into memory.");
                }else if(eventSource == cuType){
                        Msg.setText("Please choose which type of Control Unit you wish to use.");
                }else if(eventSource == hardWiredCU){
                        Msg.setText("Option to select Hard Wired Control Unit.");
                }else if(eventSource == microCodedCU){
                        Msg.setText("Option to select Microcoded Control Unit.");
                        
                }else if(eventSource == assembleButton){
                        Msg.setText("Press this button to assemble the program, this puts assembled\n program into memory.");
                }else if(eventSource == viewResultsButton){
                        Msg.setText("Press this button to see any resultant messages or error messages from the\n assembly of the program.");
                }else if(eventSource == viewCPUButton){
                        Msg.setText("Press this button to open a new window with the view of the Register Section; this is where you can simulate the execution of your program.");
                }else if(eventSource == viewMemoryButton){
                        Msg.setText("Press this button to open a new window with the view of the memory; you\n can also edit memory contents there.");
                }else if(eventSource == viewControlUnitButton){
                        Msg.setText("Press this button to open a new window with the view of the Control Unit.");
                }else if(eventSource == clearMemoryButton){
                        Msg.setText("Pressing this button clears the memory.");
                }else if(eventSource == viewBreakpointsButton){
                        Msg.setText("Allows for viewing and editing of Break Points;\n the program has to be assembled first.");
                }else if(eventSource == helpButton){
                        Msg.setText("Pressing this button will open a new window, which contains\nhelp information about the simulator.");
                }else if(eventSource == aboutButton){
                        Msg.setText("Pressing this button will show information about the sponsors and the developers of this simulator.");
                }else if(eventSource == sampleButton){
                        Msg.setText("Pressing this button will open a new window, which contains\na sample program for the simulator.\nYou can cut and paste this sample program to see the simulator in action.");
                }else if(eventSource == this){
                        Msg.setText("Welcome to the Very Simple CPU Simulator.  To start using the simulator type\n in a program followed by the memory location and press assemble.\nAfterwards click on View Register Section and choose run from the menu to start the simulation.");
                }
                lastEventSource=eventSource;
           }
   }
   
   public void mouseDragged(MouseEvent e){
           
   }
   
   public void actionPerformed( ActionEvent event )
   {
      Object eventSource = event.getSource();

      if ( eventSource == assembleButton )
      {

	 if ( ! CPUBox.isRunning() )
	 {
	    int assemblyStartLocation = AssemblyInstruction.toWordInteger(
	       memoryLocation.getText() );

	    if ( assemblyStartLocation != -1 )
	    {

               if ( ( resultsDialog != null ) && ( resultsDialog.
		  isShowing() ) )
	       {
                  resultsDialog.dispose();
	       }

               results = Assembler.assemble( programBox.
                  getText(), assemblyStartLocation, this, CPUBox, CPUBox.
                  getMemory() );

//               AssemblyError[] errors = results.getErrors();

//               for ( int index = 0; index < errors.length; index++ )
//               {
//                  System.out.println( "DEBUG ==> Line " + errors[ index ].
//                     getLineNumber() + ": " + errors[ index ].
//                     getString() );
//               }

                AssemblyError[] errors = results.getErrors();
                if(errors.length>0){
                programBox.select(FindNthRowStart(errors[ 0 ].getLineNumber(),programBox.getText()),FindNthRowEnd(errors[ 0 ].getLineNumber(),programBox.getText())+1);
               }

               resultsDialog = new AssemblyResultsDialog( getFrame(),
                  "Assembly Results", false, results );
                  
               traceDialog = new TraceResults( getFrame(), "Trace Results");

	       Dimension screenSize = Toolkit.getDefaultToolkit().
		  getScreenSize();
               Dimension resultsDialogSize = resultsDialog.getSize();
               Point resultsDialogLocation = new Point( ( screenSize.width
                  - resultsDialogSize.width ) / 2, ( screenSize.height -
                  resultsDialogSize.height ) / 2 );

               resultsDialog.setLocation( resultsDialogLocation );
               resultsDialog.setVisible( true );
	       CPUBox.getBreakpoints().refreshBreakpointList();
	       viewResultsButton.setEnabled( true );
	    }
	    else
	    {
	       showStatus( "Invalid memory location!" );
	    }

	 }
	 else
	 {
	    showStatus( "CPU is running!" );
	 }

      }
      else if ( eventSource == viewResultsButton )
      {

         if ( resultsDialog != null )
	 {
	    // Opens assembly results window
            resultsDialog.setVisible( true );
	 }
	 else
	 {
	    showStatus( "No results to report!" );
	 }

      }

⌨️ 快捷键说明

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