Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am trying to make a simulator for a micro controller in which whenever I call the runNextStep() method, it will run the next instruction.

I have made a single button which will just execute the next instruction by calling the runNextStep() method, but I want to make a run button which will have a while loop which will call the runNextStep() method infinitely until the stop button is pressed. I have put a stop button, but whenever I click the Run button, my Frame no longer interacts with mouseclicks. I think I have to make a thread, but is there any way around this? I don't know whether to make thread i.e my JFrame or the other class where the runNextStep() method is there. I also think it will make the execution of the instructions slower, which is not intended for my controller simulator.

I am total newbie so i am just working with basic things so my logic might not be generalized

package simulator;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Simulator extends JFrame {
    static JButton initializeButton,stepRunButton,runButton,stopButton;
    static JToggleButton toggleButton;
    boolean stopFlag=false;
    static JLabel regR0Label,regR1Label,regR2Label,regR3Label,regR4Label,regR5Label,regR6Label,regR7Label,regALabel;
    static JTextField regR0Field,regR1Field,regR2Field,regR3Field,regR4Field,regR5Field,regR6Field,regR7Field,regAField;
        public static void main(String[] args) {
        new Simulator();
    }
 public Simulator(){

     this.setSize(400,400);
     this.setLocationRelativeTo(null);
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     JPanel panel1 = new JPanel();
     JPanel regPanel =  new JPanel();
     initializeButton = new JButton("Initialize");
     ListenForButton lsForButton = new ListenForButton();     
     initializeButton.addActionListener(lsForButton);
     panel1.add(initializeButton);
     stepRunButton = new JButton("Run step");
     stepRunButton.addActionListener(lsForButton);     
     runButton = new JButton("Run");
     runButton.addActionListener(lsForButton);
     stopButton = new JButton("Stop");
     stopButton.addActionListener(lsForButton);

     regALabel = new JLabel("A");
     regAField = new JTextField("00",2);
     regPanel.add(regALabel);
     regPanel.add(regAField);
     regR0Label = new JLabel("R0");
     regR0Field = new JTextField("00",2);
     regPanel.add(regR0Label);
     regPanel.add(regR0Field);
     regR1Field = new JTextField("00",2);
     regR1Label = new JLabel("R1");
     regPanel.add(regR1Label);
     regPanel.add(regR1Field);
     regR2Field = new JTextField("00",2);
     regR2Label = new JLabel("R2");
     regPanel.add(regR2Label);
     regPanel.add(regR2Field);
     regR3Field = new JTextField("00",2);
     regR3Label = new JLabel("R3");
     regPanel.add(regR3Label);
     regPanel.add(regR3Field);
     regR4Field = new JTextField("00",2);
     regR4Label = new JLabel("R4");
     regPanel.add(regR4Label);
     regPanel.add(regR4Field);
     regR5Field = new JTextField("00",2);
     regR5Label = new JLabel("R5");
     regPanel.add(regR5Label);
     regPanel.add(regR5Field);
     regR6Field = new JTextField("00",2);
     regR6Label = new JLabel("R6");
     regPanel.add(regR6Label);
     regPanel.add(regR6Field);
     regR7Field = new JTextField("00",2);
     regR7Label = new JLabel("R7");
     regPanel.add(regR7Label);
     regPanel.add(regR7Field);
     regPanel.setLayout(new GridBagLayout());

     panel1.add(stepRunButton);
     panel1.add(runButton);
     panel1.add(stopButton);

     panel1.add(regPanel);
     this.add(panel1);

     this.setVisible(true);

} 




private class ListenForButton implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == initializeButton)
            {

              AT89C51.initializeROM();  
            }
            else if(e.getSource() == stepRunButton)
            {
              AT89C51.runNextStep();

            }
            else if(e.getSource() == runButton)
            {
               toggleButton.setEnabled(true);
               while(!stopFlag){
                   AT89C51.runNextStep();
               }
            }
            else if (e.getSource() == stopButton)
               {
                   stopFlag=true;
               }



        }

}

    public static void setRefreshField(int regValue[]){
        regR0Field.setText(Integer.toHexString(regValue[0]).toUpperCase());
        regR1Field.setText(Integer.toHexString(regValue[1]).toUpperCase());
        regR2Field.setText(Integer.toHexString(regValue[2]).toUpperCase());
        regR3Field.setText(Integer.toHexString(regValue[3]).toUpperCase());
        regR4Field.setText(Integer.toHexString(regValue[4]).toUpperCase());
        regR5Field.setText(Integer.toHexString(regValue[5]).toUpperCase());
        regR6Field.setText(Integer.toHexString(regValue[6]).toUpperCase());
        regR7Field.setText(Integer.toHexString(regValue[7]).toUpperCase());
}
   public static void setRefreshField(int regValue1){
        regAField.setText(Integer.toHexString(regValue1).toUpperCase());
   }
}

the code for the AT89c51 class is as below(note:it is the another main class in my simulator package)

package simulator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author Silver
 */
public class AT89C51 {
    static int PC=0,regA=0;
    static int regs[] = new int[8];
    static File vivek;
    static int ROM[] = new int[1000]; 
    static boolean stopFlag=false;
    public static void initializeROM()
   {        
        int j=0,romAddress =0;
        PC=0;
        vivek = new File("C:/vivek.hex");
            try 
            {              
                //code for adding all data to controller ROM
                BufferedReader getdata = new BufferedReader(new FileReader(vivek));
                String data = new String();
           while(true)
           {
                data = getdata.readLine();
                if(data != null)
                {
                System.out.println(data);
                int a = (int) Integer.parseInt(data.substring(1,3),16);
                System.out.println(a);

                for (int i=0;i<2*a;i=i+2)
                {
                    ROM[romAddress]= Integer.parseInt(data.substring(i+9,i+11),16);
                    romAddress++;
                }                
                j++;
                }
                else
                {
                    break;
                }
           }

           for(int temp1=0; temp1<romAddress; temp1++)
           {
               System.out.println(temp1+" "+Integer.toHexString(ROM[temp1]));
           }

            }
            catch (IOException e) {
               e.printStackTrace();
            }
    }

    public static void runNextStep() {
        int t1 = getnextopcode();
        switch(t1)
        {
                case 0x00: //NOP
                     break;
                case 0x01: //AJMP addr
                     int temp1=PC+1;
                     temp1 =temp1 & 0xF800 |(0x01>>1) | ROM[PC];
                     System.out.println("now new PC "+temp1);
                     PC = temp1;
                     break;
                case 0x02: //LJMP addr
                     int temp2=ROM[PC];
                     temp2 = temp2<<8 | ROM[PC+1];
                     PC = temp2;
                     break;
                case 0x03: //RR A
                     int temp3=regA & 0x01;
                     regA = (regA >> 1)| (temp3 << 7);
                     regA = regA & 0x0FF;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x04: //INC A
                     regA = executeADDInst(regA,1);
                     Simulator.setRefreshField(regA);
                     break;
                case 0x08: //INC R0
                     regs[0] = executeADDInst(regs[0],1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x09: //INC R1
                     regs[1] = executeADDInst(regs[1],1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x0A: //INC R2
                     regs[2] = executeADDInst(regs[2],1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x0B: //INC R3
                     regs[3] = executeADDInst(regs[3],1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x0C: //INC R4
                     regs[4] = executeADDInst(regs[4],1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x0D: //INC R5
                     regs[5] = executeADDInst(regs[5],1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x0E: //INC R6
                     regs[6] = executeADDInst(regs[6],1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x0F: //INC R7
                     regs[7] = executeADDInst(regs[7],1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x18: //DEC R0
                     regs[0] = executeADDInst(regs[0],-1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x19: //DEC R1
                     regs[1] = executeADDInst(regs[1],-1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x1A: //DEC R2
                     regs[2] = executeADDInst(regs[2],-1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x1B: //DEC R3
                     regs[3] = executeADDInst(regs[3],-1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x1C: //DEC R4
                     regs[4] = executeADDInst(regs[4],-1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x1D: //DEC R5
                     regs[5] = executeADDInst(regs[5],-1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x1E: //DEC R6
                     regs[6] = executeADDInst(regs[6],-1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x1F: //DEC R7
                     regs[7] = executeADDInst(regs[7],-1);
                     Simulator.setRefreshField(regs);
                     break;
                case 0x21: //AJMP addr
                     int temp21=PC+1;
                     temp21 =temp21 & 0xF800 |(0x01>>1) | ROM[PC];
                     System.out.println("now new PC "+temp21);
                     PC = temp21;
                     break;
                case 0x23: //RL A
                     int temp23=regA & 0x80;
                     regA = (regA << 1)| (temp23 >> 7);
                     regA = regA & 0x0FF;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x24: //ADD A,#data
                     regA = executeADDInst(regA,ROM[PC]);
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x28: //ADD A,R0
                     regA = executeADDInst(regA,regs[0]);
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x29: //ADD A,R1
                     regA = executeADDInst(regA,regs[1]);
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x2A: //ADD A,R2
                     regA = executeADDInst(regA,regs[2]);
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x2B: //ADD A,R3
                     regA = executeADDInst(regA,regs[3]);
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x2C: //ADD A,R4
                     regA = executeADDInst(regA,regs[4]);
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x2D: //ADD A,R5
                     regA = executeADDInst(regA,regs[5]);
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x2E: //ADD A,R6
                     regA = executeADDInst(regA,regs[6]);
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x2F: //ADD A,R7
                     regA = executeADDInst(regA,regs[7]);
                     PC++;
                     Simulator.setRefreshField(regA);
                     break; 
                case 0x41: //AJMP addr
                     int temp41=PC+1;
                     temp41 =temp41 & 0xF800 |(0x01>>1) | ROM[PC];
                     System.out.println("now new PC "+temp41);
                     PC = temp41;
                     break;
                case 0x44: //ORL A,#data
                     regA = regA | ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x48: //ORL A,R0
                     regA = regA | regs[0];
                     Simulator.setRefreshField(regA);
                     break;
                case 0x49: //ORL A,R1
                     regA = regA | regs[1];
                     Simulator.setRefreshField(regA);
                     break;
                case 0x4A: //ORL A,R2
                     regA = regA | regs[2];
                     Simulator.setRefreshField(regA);
                     break;
                case 0x4B: //ORL A,R3
                     regA = regA | regs[3];
                     Simulator.setRefreshField(regA);
                     break;
                case 0x4C: //ORL A,R4
                     regA = regA | regs[4];
                     Simulator.setRefreshField(regA);
                     break;
                case 0x4D: //ORL A,R5
                     regA = regA | regs[5];
                     Simulator.setRefreshField(regA);
                     break;
                case 0x4E: //ORL A,R6
                     regA = regA | regs[6];
                     Simulator.setRefreshField(regA);
                     break;
                case 0x4F: //ORL A,R7
                     regA = regA | regs[7];
                     Simulator.setRefreshField(regA);
                     break;    
                case 0x74:
                     regA = ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regA);
                     break;
                case 0x78: //MOV R0,#data
                     regs[0] = ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regs);
                     break;
                case 0x79: //MOV R1,#data
                     regs[1] = ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regs);
                     break;
                case 0x7A: //MOV R2,#data
                     regs[2] = ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regs);
                     break;
                case 0x7B: //MOV R3,#data
                     regs[3] = ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regs);
                     break;
                case 0x7C: //MOV R4,#data
                     regs[4] = ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regs);
                     break;
                case 0x7D: //MOV R5,#data
                     regs[5] = ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regs);
                     break;
                case 0x7E: //MOV R6,#data
                     regs[6] = ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regs);
                     break;
                case 0x7F: //MOV R7,#data
                     regs[7] = ROM[PC];
                     PC++;
                     Simulator.setRefreshField(regs);
                     break;
                case 0x80: //SJMP address
                     int temp80 = ROM[PC];
                     temp80 = temp80+PC+1;
                     if(temp80>=0x100){
                         temp80 = temp80-0x100;
                     }
                     PC=temp80;
                     System.out.println(PC);
                     System.out.println("hhhh"+temp80);
                     break; 
        }

    }

    private static int getnextopcode() {
//        if(ROM[PC] == null)
//        {
//          System.out.println("program end occured");  
//          return 0;
//        }        
        int opcode = ROM[PC];
        System.out.println(ROM[PC]);
        PC++; 
        return opcode;
        }
    private static int executeADDInst(int a ,int b){
        a=a+b;
        a = a & 0x0FF;
        return a;
    }

    static void runCode(){
        while(!stopFlag)
        {
            runNextStep();
        }
    }

    static void stopCodeRunning() {
        stopFlag = true;
    }
    }

while the file used in AT89C51 class vivek.hex contains hex code generated for the AT89C51 microcontroller. the content is given below.........

:060000007800240180FAE3 :00000001FF

above content must be in two separate lines i.e first line :060000007800240180FAE3 and second line :00000001FF it is basically a infinite loop whose ASM code is

ORG 0000H
HERE:
MOV R0,#0x00
ADD A,#0x01
SJMP HERE
END

my code works like this first of all initialize button is clicked which will initialize the ROM then when run step button is clicked next instruction is executed.......but in run button how to do i am not able to understand

share|improve this question

closed as off-topic by syb0rg, ChrisWue, rolfl, Mat's Mug, asteri Jan 15 '14 at 14:43

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code." – syb0rg, ChrisWue, rolfl, Mat's Mug, asteri
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code –  Malachi Jan 15 '14 at 14:45
    
i have uploaded my full code now....sorry for just uploading half code before..... –  user34974 Jan 16 '14 at 16:31
2  
@user34974 you have taken some effort to get your code uploaded here, but I believe you are missing the point. CodeReview is not here to help you add functionality to your program... adding the logic behind the 'Play' button is a great idea, and once it is working, we can review it, but we are not here to help you write the 'play' code. –  rolfl Jan 16 '14 at 16:52

Browse other questions tagged or ask your own question.