hi ..
i make this code .. but when i run it ..doesn't show whole output in window ..and its not show correct answer ..
i can't find error / but its not worked welll
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package simulator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Simulator extends JPanel implements ActionListener {
static final int READ = 10, WRITE = 11, LOAD = 20, STORE = 21,
ADD = 30, SUBTRACT = 31, MULTIPLY = 32, DIVIDE = 33, BRANCH = 40,
BRANCH_NEG = 41, BRANCH_ZERO = 42, HALT = 43;
JTextField input, prompt, accum, counter, operandField,
operCode, register;
JLabel accumLabel, counterLabel, operandLabel,
operCodeLabel, registerLabel;
JButton doneButton, executeButton;
boolean readFlag;
int memory[], accumulator, instructionCounter, index,
operand, operationCode, instructionRegister;
public void init()
{
initializeRegisters();
instructions();
// create GUI components
doneButton = new JButton( "Done" );
doneButton.addActionListener( this );
executeButton = new JButton( "Execute next instruction" );
executeButton.setEnabled( false );
executeButton.addActionListener( this );
input = new JTextField( 5 );
input.addActionListener( this );
prompt = new JTextField( 15 );
register = new JTextField( 4 );
counter = new JTextField( 2 );
operCode = new JTextField( 2 );
operandField = new JTextField( 2 );
accum = new JTextField( 4 );
prompt.setEditable( false );
prompt.setText( "0" + index + " ? " );
register.setEditable( false );
counter.setEditable( false );
operCode.setEditable( false );
operandField.setEditable( false );
accumLabel = new JLabel( "Accumulator:" );
counterLabel = new JLabel( "InstructionCounter:" );
registerLabel = new JLabel( "InstructionRegister:" );
operCodeLabel = new JLabel( "OperationCode:" );
operandLabel = new JLabel( "Operand:" );
// add components to applet
JPanel panel = new JPanel();
panel.setLayout( new FlowLayout() );
panel.add( accumLabel );
panel.add( accum );
panel.add( counterLabel );
panel.add( counter );
panel.add( registerLabel );
panel.add( register );
panel.add( operCodeLabel );
panel.add( operCode );
panel.add( operandLabel );
panel.add( operandField );
panel.add( prompt );
panel.add( input );
panel.add( doneButton );
panel.add( executeButton );
displayRegisters(); // place correct values in fields
}
// set values held by text fields
public void displayRegisters()
{
operandField.setText( "" + operand );
counter.setText( "" + instructionCounter );
accum.setText( "" + accumulator );
operCode.setText( "" + operationCode );
register.setText( "" + instructionRegister );
}
// set all registers to the correct start value
public void initializeRegisters()
{
memory = new int[ 100 ];
accumulator = 0;
instructionCounter = 0;
instructionRegister = 0;
operand = 0;
operationCode = 0;
index = 0;
readFlag = false;
for ( int k = 0; k < memory.length; k++ )
memory[ k ] = 0;
}
// ensure value is within range
public boolean validation( int value )
{
if ( value < 9999 && value > -9999 )
return false;
else
return true;
}
// ensure that accumulator has not overflowed
public boolean testOverflow()
{
if ( validation( accumulator ) ) {
System.out.println( "*** Fatal error." +
"Accumulator overflow. ***" );
executeButton.setEnabled( false );
return true;
}
return false;
}
// perform all simulator functions
public void execute()
{
prompt.setText( "" );
instructionRegister = memory[ instructionCounter ];
operationCode = instructionRegister / 100;
operand = instructionRegister % 100;
switch( operationCode ) {
case READ:
// actual value is assigned in actionPeformed
readFlag = true;
input.setText( "" );
input.setEditable( true );
prompt.setText( "Enter an integer:" );
instructionCounter++;
break;
case WRITE:
System.out.println( "Contents of " + operand +
" is " + memory[ operand ] );
instructionCounter++;
break;
case LOAD:
accumulator = memory[ operand ];
instructionCounter++;
break;
case STORE:
memory[ operand ] = accumulator;
instructionCounter++;
break;
case ADD:
accumulator += memory[ operand ];
if ( testOverflow() == true )
return;
instructionCounter++;
break;
case SUBTRACT:
accumulator -= memory[ operand ];
if ( testOverflow() == true )
return;
instructionCounter++;
break;
case MULTIPLY:
accumulator *= memory[ operand ];
if ( testOverflow() == true )
return;
instructionCounter++;
break;
case DIVIDE:
if ( memory[ operand ] == 0 ) {
System.out.println( "*** Fatal error." +
"Attempt to divide by zero. ***" );
executeButton.setEnabled( false );
return;
}
accumulator /= memory[ operand ];
instructionCounter++;
break;
case BRANCH:
instructionCounter = operand;
break;
case BRANCH_NEG:
if ( accumulator < 0 )
instructionCounter = operand;
else
instructionCounter++;
break;
case BRANCH_ZERO:
if ( accumulator == 0 )
instructionCounter = operand;
else
instructionCounter++;
break;
case HALT:
executeButton.setEnabled( false );
dump();
break;
default:
executeButton.setEnabled( false );
System.out.println( "*** Fatal error. " +
" Invalid operation code. ***" );
}
// update the register textfields
displayRegisters();
}
// read in user input, test it, perform operations
public void actionPerformed( ActionEvent e )
{
if ( e.getSource() == input && index < 100 ) {
int temp = Integer.parseInt( input.getText() );
// not a read instruction
if ( !readFlag ) {
// allow user to reenter values
if ( validation( temp ) == false ) {
memory[ index++ ] = temp;
// clear text field
input.setText( "" );
if ( index < 10 )
prompt.setText( "0" + index + " ? " );
else
prompt.setText( index + " ? " );
}
else
input.setText( "" );
}
// read instruction
else {
if ( validation( temp ) == false ) {
memory[ operand ] = temp;
input.setEditable( false );
readFlag = false;
executeButton.setEnabled( true );
}
else {
input.setText( "" );
executeButton.setEnabled( false );
}
}
}
else if ( e.getSource() == doneButton ) {
input.setEditable( false );
executeButton.setEnabled( true );
doneButton.setEnabled( false );
index = 0;
execute(); // execute first instruction
}
else if ( e.getSource() == executeButton ) {
index++;
execute();
}
}
// print out user instructions
public void instructions()
{
System.out.println( "*** Welcome to Simpletron! ***\n" +
"*** Please enter your program one instruction ***\n" +
"*** ( or data word ) at a time into the input ***\n" +
"*** text field. I will display the location ***\n" +
"*** number and a question mark (?). You then ***\n" +
"*** type the word for that location. Press the ***\n" +
"*** Done button to stop entering your program ***\n" );
}
// create a string version of the number to output
public String prepareNumber( int number )
{
int count = 0, factor = 1000;
String output;
// account for sign of number
if ( number < 0 ) {
number *= -1;
output = "-";
}
else
output = "+";
// build String representation of number
while ( factor >= 1 ) {
output += number / factor;
number %= factor;
factor /= 10;
}
return output;
}
// output information in registers
public void dump()
{
System.out.println( "REGISTERS:" );
System.out.print( "accumulator " );
System.out.println( prepareNumber( accumulator ) );
System.out.print( "instructionCounter " );
System.out.print( ( instructionCounter / 10 ) );
System.out.println( ( instructionCounter % 10 ) );
System.out.print( "instructionRegister " );
System.out.println( prepareNumber( instructionRegister ) );
System.out.print( "operationCode " );
System.out.print( ( operationCode / 10 ) );
System.out.println( ( operationCode % 10 ) );
System.out.print( "operand " );
System.out.print( ( operand / 10 ) );
System.out.println( ( operand % 10 ) );
System.out.println( "\nMEMORY:" );
System.out.println( " 0 1 2 3" +
" 4 5 6 7 8 9" );
for ( int k = 0; k < 10; k++ ) {
if ( k == 0 )
System.out.print( " " + k + " " );
else
System.out.print( ( k * 10 ) + " " );
for ( int i = 0; i < 10; i++ )
System.out.print( prepareNumber(
memory[ k * 10 + i ] ) + " " );
System.out.println();
}
}
void setDefaultCloseOperation(int EXIT_ON_CLOSE) {
throw new UnsupportedOperationException("Not yet implemented");
}
} // end class Simulat
`