protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
         EnableViewState = true;
         ViewState.Add("micro", micro);
         Processor_Type = DropDownList_Processor.SelectedValue;
     }
     micro = (processor)ViewState["micro"];
     try
     {
         micro.inport = System.Convert.ToInt16(Text1.Value, 16);
     }
     catch
     {
         micro.inport = 0;
     }
     Hidden_htmlbox1.Value = micro.inport.ToString();
     try
     {
         micro.adc = (int)(255 * System.Convert.ToDouble(Analog_Input.Text) / 5);
     }
     catch
     {
         TextBox_Info.Text = "!Warning Analogue input not a number"; return;
     }
     if (micro.adc > 255)
     {
         micro.adc = 255;
     }
     if (micro.adc < 0)
     {
         micro.adc = 0;
     }
 }
        protected void Button_AssemblePIC(object sender, EventArgs e)
        {
            if (micro.maxcodelines == 0)
            {
                TextBox_Info.Text = "No code!"; return;
            }
            micro           = (processor)ViewState["micro"];
            Processor_Type  = DropDownList_Processor.SelectedValue;
            PIC_output.Text = "";
            string line = "";
            string path = "";

            path = Server.MapPath(@"App_Data/setup_" + Processor_Type + ".txt");
            string s = @"..\App_Data\EmulatorHelp.txt";

            using (StreamReader sr = new StreamReader(path))
            {
                while ((s = sr.ReadLine()) != null)
                {
                    PIC_output.Text += s + "\n";
                }
            }

            s = ProgramText.Text;//get text
            string s1 = ""; int n = 0;
            int    i = 0;

            micro.maxcodelines = 0; micro.maxlabels = 0;
            while (s.Length > 0)
            {
                line = s; i = s.IndexOf((char)0x0d);
                if (i >= 0)
                {
                    //multi line.. so split it..
                    line = s.Substring(0, i); s = s.Substring(i + 2);
                }
                else
                {
                    s = "";
                }
                s1 = line; //preserve orig line
                if (!ProcessLine(ref line))
                {          //we have an error...
                    TextBox_Info.Text = s1 + "at line " + n.ToString() + "   :" + line; return;
                }
                //no error so add the code
                PIC_output.Text += line;
                n++;
            }
            PIC_output.Text             += "    END";
            PIC_output.Visible           = true;
            Cross_Assemble_Label.Visible = true;
            return;
        }
        protected void Button_Step_Click(object sender, EventArgs e)
        {
            //need to get the pc line of code........
            if (micro.maxcodelines == 0)
            {
                TextBox_Info.Text = "No code!"; return;
            }
            micro = (processor)ViewState["micro"];
            string line = micro.memory[micro.CurrentPC];
            string label = ""; int Rd = 0; int Rs = 0; string data = ""; string op = "";

            if (!DecodeLine(ref line, ref label, ref op, ref Rs, ref Rd, ref data))
            {
                TextBox_Info.Text = "No such line"; return;
            }
            int imm = 0;

            if ((data == "READTABLE") && (op == "RCALL"))
            {
                op = "READTABLE"; data = "";
            }
            if ((data == "READADC") && (op == "RCALL"))
            {
                op = "READADC"; data = "";
            }
            if ((data == "WAIT1MS") && (op == "RCALL"))
            {
                op = "WAIT1MS"; data = "";
            }
            if (data != "")
            {
                if ((op == "JP") || (op == "JZ") || (op == "JNZ"))
                {
                    imm = FindLabel(data);//is it a label....
                }
                else
                {
                    try
                    {
                        imm = System.Convert.ToInt32(data, 16);
                    }
                    catch
                    {
                        imm = FindLabel(data);
                    }
                }
            }

            if (imm == -1)
            {
                return;
            }
            int d1 = (int)imm;

            try
            {
                double x = System.Convert.ToDouble(Analog_Input.Text);
                micro.adc = (int)(255 * (x / 5));
            }
            catch
            {
                micro.adc = 0;
            }
            micro.inport = System.Convert.ToInt16(Text1.Value);
            if (!micro.execute(op, Rd, Rs, d1, ref line))
            {
                TextBox_Info.Text = "Error! " + line;
            }
            UpdateView();
            TextBox_Info.Text = micro.memory[micro.CurrentPC];
        }