示例#1
0
        // Called on run button click
        // 1. Assembles program (exits if error)
        // 2. Reads "constant" variables from textboxes
        // 3. Allocates new memory
        // 4. Creates cpu object with memory reference and constants
        // 5. Runs program on new thread
        private void RunProgram_Click(object sender, EventArgs e)
        {
            // Do not allow run to be clicked if program running
            if (sim != null)
            {
                return;
            }

            ProgramOutput.Clear();

            // Maximum number of bytes in memory
            MAX_BYTES = UInt32.Parse(MaxBytesTB.Text);

            // Assembles program
            // Displays error message and returns if unsuccessful
            if (!Assembler.Assemble(ProgramData.Lines, MAX_BYTES))
            {
                ProgramOutput.Text  = Assembler.GetAsmInfo();
                ProgramOutput.Text += Environment.NewLine + "Could not assemble program.";
                return;
            }

            // Load program from assembler
            pgm = Assembler.GetProgram();

            ProgramOutput.Text = "Program assembled successfully. " + (MAX_BYTES - Assembler.GetBytesUsed()) + " bytes free." + Environment.NewLine;

            // How many instructions to execute before termination
            int insnCount = Int32.Parse(MaxInsnsTB.Text);

            // Delay in milliseconds between each instruction
            int delay = Int32.Parse(InsnDelayTB.Text);

            // Create new memory for the CPU
            // Initialize it with the created program
            // and max ram address space
            mem = new Memory(pgm, MAX_BYTES);

            // Create a new cpu simulation
            // Initialize it with the created memory
            cpu = new EaterIsa(ref mem, ref OutputRegTB, insnCount, delay);

            // Create new thread for program
            // This allows user to still use buttons
            // and text areas
            sim = new Thread(start);

            // Execute the program on a new thread
            sim.Start();

            sim = null;
        }