예제 #1
0
        /*End Chase's Code*/


        /*/////NEXT BUTTON/////////*/

        /*Begin Chase's Code*/

        //Move to the next instruction
        private void Next_Button_Click(object sender, EventArgs e)
        {
            txtAccumulator.Text = f.GetAccumulator().ToString();
            //declare variables
            DataGridViewRow row;
            string          user_input  = "";
            string          instruction = f.GetNextInstruction();
            int             opcode      = int.Parse(instruction.Substring(0, 2));
            int             location    = 0;

            if (opcode != 43)
            {
                string str_location = Start_DataGridView[0, (f.GetProgramCtr() + 1)].Value.ToString();
                location = int.Parse(str_location);
            }
            else
            {
                location = 0;
            }



            //initialize row
            row = (DataGridViewRow)Start_DataGridView.Rows[0];

            //interpret opcode
            switch (opcode)
            {
            //READ OPCODE
            case 10:
            {
                //ask user for input
                if (Start_DataGridView[2, f.GetProgramCtr()].Value.ToString() == "")
                {
                    Start_DataGridView[2, f.GetProgramCtr()].Value = "ERROR";
                    break;
                }
                else
                {
                    user_input = Start_DataGridView[2, f.GetProgramCtr()].Value.ToString();
                    //Put user input into the array
                    f.Read(user_input, location);

                    //highlight next instruction
                    UnhighlightRow(row, f.GetProgramCtr(), false);
                    f.IncrementProgramCtr();         //skip memory address
                    f.IncrementProgramCtr();
                    break;
                }
            }

            //WRITE OPCODE
            case 11:
            {
                string number_in_address = "";
                number_in_address = f.Write(location);
                Start_DataGridView[2, f.GetProgramCtr()].Value = number_in_address;

                //Write to Output box
                txtOutput.Text += number_in_address + "\n";

                //highlight next instruction
                UnhighlightRow(row, f.GetProgramCtr(), false);
                f.IncrementProgramCtr();
                f.IncrementProgramCtr();
                break;
            }

            //LOAD OPCODE
            case 20:
            {
                f.Load(location);
                Start_DataGridView[2, f.GetProgramCtr()].Value = f.GetInstructionAt(location) + " -> accumulator";

                //highlight next instruction
                UnhighlightRow(row, f.GetProgramCtr(), false);
                f.IncrementProgramCtr();
                f.IncrementProgramCtr();
                break;
            }

            //STORE OPCODE
            case 21:
            {
                f.Store(location);
                Start_DataGridView[2, f.GetProgramCtr()].Value = "Stored " + f.GetInstructionAt(location) + " from accumulator -> " + location;


                //highlight next instruction
                UnhighlightRow(row, f.GetProgramCtr(), false);
                f.IncrementProgramCtr();
                f.IncrementProgramCtr();
                break;
            }

            //ADD OPCODE
            case 30:
            {
                int lho = f.GetAccumulator();
                int rho = int.Parse(f.GetInstructionAt(location));
                int sum = f.ADD(lho, rho);
                f.SetAccumulator(sum);
                Start_DataGridView[2, f.GetProgramCtr()].Value = f.GetInstructionAt(location) + " added -> accumulator";


                //highlight next instruction
                UnhighlightRow(row, f.GetProgramCtr(), false);
                f.IncrementProgramCtr();
                f.IncrementProgramCtr();
                break;
            }

            //SUBTRACT OPCODE
            case 31:
            {
                int lho        = f.GetAccumulator();
                int rho        = int.Parse(f.GetInstructionAt(location));
                int difference = f.SUBTRACT(lho, rho);
                f.SetAccumulator(difference);
                Start_DataGridView[2, f.GetProgramCtr()].Value = f.GetInstructionAt(location) + " subtracted -> accumulator";

                //highlight next instruction
                UnhighlightRow(row, f.GetProgramCtr(), false);
                f.IncrementProgramCtr();
                f.IncrementProgramCtr();
                break;
            }

            //DIVIDE OPCODE
            case 32:
            {
                int lho = f.GetAccumulator();
                int rho = int.Parse(f.GetInstructionAt(location));
                if (rho == 0)
                {
                    MessageBox.Show("Cannot divide by zero... exiting...");
                    Application.Exit();
                }

                int remainder = 0;
                int quotient  = f.DIVIDE(lho, rho, ref remainder);
                f.SetAccumulator(quotient);
                f.SetOverflow(remainder);
                Start_DataGridView[2, f.GetProgramCtr()].Value = f.GetInstructionAt(location) + " divided -> accumulator";

                //highlight next instruction
                UnhighlightRow(row, f.GetProgramCtr(), false);
                f.IncrementProgramCtr();
                f.IncrementProgramCtr();
                break;
            }

            //MULTIPLY
            case 33:
            {
                int lho     = f.GetAccumulator();
                int rho     = int.Parse(f.GetInstructionAt(location));
                int product = f.MULTIPLY(lho, rho);
                f.SetAccumulator(product);
                Start_DataGridView[2, f.GetProgramCtr()].Value = f.GetInstructionAt(location) + " multiply -> accumulator";

                UnhighlightRow(row, f.GetProgramCtr(), false);
                f.IncrementProgramCtr();
                f.IncrementProgramCtr();
                break;
            }

            /*Control Opcodes*/


            //branch positive
            case 40:
            {
                if (f.GetAccumulator() > 0)
                {
                    UnhighlightRow(row, location, true);
                    f.Branch_Positive(location);
                    break;
                }
                else
                {
                    f.IncrementProgramCtr();
                    UnhighlightRow(row, f.GetProgramCtr(), false);
                    break;
                }
            }

            //branch negative
            case 41:
            {
                if (f.GetAccumulator() < 0)
                {
                    UnhighlightRow(row, location, true);
                    f.Branch_Positive(location);
                    break;
                }
                else
                {
                    f.IncrementProgramCtr();
                    UnhighlightRow(row, f.GetProgramCtr(), false);
                    break;
                }
            }

            //branch zero
            case 42:
            {
                if (f.GetAccumulator() == 0)
                {
                    UnhighlightRow(row, location, true);
                    f.Branch_Positive(location);
                    break;
                }
                else
                {
                    f.IncrementProgramCtr();
                    UnhighlightRow(row, f.GetProgramCtr(), false);
                    break;
                }
            }

            /*Begin Chase's Code*/

            //halt command
            //Start of Benny code
            case 43:
            {
                const int MAX_INSTR = 1000;
                const int MAX_COL   = 100;

                print = "";
                //New method for expanded memory
                //Gives the top header row for the first 20
                for (int i = 0; i < MAX_COL; i++)
                {
                    print += "\t0" + i;
                }
                print += "\r\n";

                for (int i = 0; i < MAX_INSTR; i++)
                {
                    if (0 == i % 100)
                    {
                        print += i + "\t";
                    }
                    if (99 == i % 100)
                    {
                        print += f.GetInstructionForMemDump(i) + "\r\n";
                    }
                    else
                    {
                        print += f.GetInstructionForMemDump(i) + "\t";
                    }
                }


                //Still need this to show new form
                dump            = new WindowsFormsApp.MemDumpFrm();
                dump.dumpHere   = print;
                dump.accumValue = f.GetAccumulator();
                dump.Show();
                break;
            }

            default:
                break;
            }
        }