Ejemplo n.º 1
0
        //load the file specified by <name> and ready the Disassembly etc. panels
        protected void OnFileSelect(EventArgs e, string name)
        {
            Loader.Log("Form1: Loading File", false, false);
            terminalBox.Clear();
            int fl    = flagView.Rows.Add();
            var flrow = flagView.Rows[fl];

            flagView.ForeColor   = Color.Black;
            flrow.Cells[0].Value = "0";
            flrow.Cells[1].Value = "0";
            flrow.Cells[2].Value = "0";
            flrow.Cells[3].Value = "0";
            flrow.Cells[4].Value = "0";
            comp.setFileName(name);

            disassemblyView.Rows.Clear();

            stackView.Rows.Clear();
            memoryView.Rows.Clear();
            name = name.Substring(name.LastIndexOf("\\") + 1);
            try
            {
                int sum = comp.resetComputer();
                comp.cpu.clearBreak();
                fileNamePanel.Text = "Filename: " + name;
                checkSumPanel.Text = "Checksum: " + sum.ToString();
                int    index  = 0;
                byte[] memory = comp.getRam();
                while (index < 16)
                {
                    uint entry1 = comp.getReg(index);
                    if (index == 15)
                    {
                        entry1 -= 4;
                    }
                    setRegister(index, entry1);
                    index++;
                }
                uint addr = comp.RAM.begin;

                int          byteindex = 0;
                bool         instruct  = false;
                StreamReader rd        = new StreamReader("sample.txt");
                bool         endins    = false;
                Loader.Log("Form1: Setting Memory", false, false);
                while (addr < comp.RAM.end)
                {
                    int             row = memoryView.Rows.Add();
                    DataGridViewRow r1  = memoryView.Rows[row];
                    r1.Height = (memoryView.ClientRectangle.Height - memoryView.ColumnHeadersHeight) / memoryView.Rows.Count;
                    r1.Cells["Address"].Value = "0x" + addr.ToString("X8");
                    DataGridViewCell c1 = memoryView.Rows[row].Cells["Address"];
                    int readindex       = 0;
                    while (readindex < 4)
                    {
                        string bytestr = "";
                        while (byteindex < 4)
                        {
                            if (addr >= comp.RAM.end)
                            {
                                break;
                            }
                            byte b = memory[addr + byteindex];
                            bytestr += b.ToString("X2") + " ";
                            byteindex++;
                        }
                        memoryView.Rows[row].Cells[readindex + 1].Value = bytestr;
                        byteindex = 0;
                        addr     += 4;
                        readindex++;
                    }
                    //memoryView.Rows.Insert()
                }
                DataGridViewRow lrow   = memoryView.Rows[memoryView.Rows.Count - 1];
                int             cindex = 1;
                while (cindex < 5)
                {
                    if ((string)lrow.Cells[cindex].Value == "")
                    {
                        lrow.Cells[cindex].Value = "00 00 00 00";
                    }
                    cindex++;
                }
                addr = 0;
                comp.setCounter(comp.Registers.getPC());
                Loader.Log("Form1: Loading Disassembly", false, false);

                addr = 0;
                var re = comp.Registers.getPC();
                if (comp.RAM.ReadWord(0) == 0)
                {
                    addr     = comp.RAM.begin;
                    instruct = true;
                }
                while (addr < comp.RAM.end)
                {
                    string          x = addr.ToString("X");
                    DataGridViewRow r2;
                    int             row2;
                    comp.Registers.setRegister(15, addr + 4);
                    if (addr == 0 || instruct)
                    {
                        if (comp.RAM.ReadWord(addr) != 0)
                        {
                            if (!endins)
                            {
                                row2 = disassemblyView.Rows.Add();
                                r2   = disassemblyView.Rows[row2];

                                r2.Cells["AddressColumn"].Value     = "0x" + x;
                                r2.Cells["MachineLangColumn"].Value = "0x" + addPlace(comp.RAM.ReadWord(addr));

                                try
                                {
                                    Instruction x1 = Instruction.Factory(comp.RAM.ReadWord(addr), ref comp);
                                    if (x1.GetType() == typeof(BranchInstruction))
                                    {
                                        BranchInstruction bran = (BranchInstruction)x1;

                                        r2.Cells["AssemblyColumn"].Value = (bran.ToAssembly());
                                    }
                                    else
                                    {
                                        r2.Cells["AssemblyColumn"].Value = x1.ToAssembly();
                                    }
                                }
                                catch
                                {
                                    r2.Cells["AssemblyColumn"].Value = "";
                                }
                                instruct = true;
                            }
                        }
                    }
                    addr += 4;
                }
                comp.Registers.setRegister(15, re);
                uint entry = comp.Registers.getPC();
                foreach (DataGridViewRow y in disassemblyView.Rows)
                {
                    if (y.Cells[0].Value == null)
                    {
                        continue;
                    }
                    string iii     = (string)y.Cells[0].Value;
                    var    nupoint = UInt32.Parse(iii.Substring(2), System.Globalization.NumberStyles.HexNumber);
                    if (nupoint == re)
                    {
                        if (y.Index > 2)
                        {
                            disassemblyView.FirstDisplayedScrollingRowIndex = y.Index - 2;
                        }
                        else
                        {
                            disassemblyView.FirstDisplayedScrollingRowIndex = y.Index;
                        }
                        y.Selected = true;

                        firstinstructAddress = y.Index;
                        disassemblyView.Update();
                        break;
                    }

                    disassemblyView.Rows[0].Selected = true;
                    disassemblyView.Enabled          = false;
                    rd.Close();
                }
                addStack();
                if (comp.Registers.modetrack == 0)
                {
                    modeBox.Text = "SYS";
                }
                else
                {
                    modeBox.Text = "SVC";
                }
            }
            catch
            {
                fileNamePanel.Text = "None";
                checkSumPanel.Text = "Load Error";
                disassemblyView.Rows.Clear();
                memoryView.Rows.Clear();
                flagView.Rows.Clear();
                stackView.Rows.Clear();
                MessageBox.Show("Error: File could not load.");
            }

            disassemblyView.AutoResizeColumns();
            memoryView.AutoResizeColumns();
            flagView.AutoResizeColumns();
            disassemblyView.CellBorderStyle = DataGridViewCellBorderStyle.None;
            memoryView.CellBorderStyle      = DataGridViewCellBorderStyle.None;
        }
Ejemplo n.º 2
0
        //returns an Instruction based on the opcode contained in nval.
        static public Instruction Factory(uint nval, ref Computer comp)
        {
            //determine type of instruction by comparing bit values


            //return swi
            if (Memory.ExtractBits(nval, 24, 27) == 0xF)
            {
                SwiInstruction swi = new SwiInstruction(nval, ref comp);
                swi.swivalue = Memory.ExtractBits(nval, 0, 23);
                return(swi);
            }
            uint Mul1 = Memory.ExtractBits(nval, 21, 27);
            uint Mul2 = Memory.ExtractBits(nval, 4, 7);

            //return mulinstruction
            if (Mul1 == 0 && Mul2 == 9)
            {
                MulInstruction ins = new MulInstruction(nval, ref comp);
                ins.Decode();
                ins.setCode(nval);
                return(ins);
            }
            if (nval == 0xE12fFf1e)
            {
                BranchInstruction b = new BranchInstruction(nval, ref comp);
                b.setCode(nval);
                b.bx = true;
                return(b);
            }
            if (Memory.ExtractBits(nval, 23, 27) == 2 && Memory.ExtractBits(nval, 20, 21) == 0)
            {
                MRSInstruction mrs = new MRSInstruction(nval, ref comp);
                mrs.Decode();
                return(mrs);
            }
            if (Memory.ExtractBits(nval, 23, 27) == 6 && Memory.ExtractBits(nval, 20, 21) == 2)
            {
                MSRInstruction msr = new MSRInstruction(nval, ref comp);
                msr.Decode();
                return(msr);
            }
            if (Memory.ExtractBits(nval, 23, 27) == 2 && Memory.ExtractBits(nval, 20, 21) == 2)
            {
                MSRInstruction msr = new MSRInstruction(nval, ref comp);
                msr.Decode();
                return(msr);
            }
            uint branch = Memory.ExtractBits(nval, 25, 27);

            if (branch == 5)
            {
                BranchInstruction bran = new BranchInstruction(nval, ref comp);
                bran.setCode(nval);
                if (Memory.TestBit(24, nval))
                {
                    bran.link = true;
                }
                else
                {
                    bran.link = false;
                }

                bran.offset = Memory.ExtractBits(nval, 0, 23);
                bran.strset = bran.offset;
                //bran.offset = (bran.offset & 0x2f000000);

                bran.offset = signExtension((uint)bran.offset);
                //bran.offset = bran.offset >> 6;
                bran.offset = bran.offset << 2;
                bran.offset = comp.getReg(15) + bran.offset;

                return(bran);
            }


            uint LSM = Memory.ExtractBits(nval, 23, 27);

            //return ldm/stm instruction
            if (LSM == 18 || LSM == 17)
            {
                LoadStoreMultipleInstruction ins = new LoadStoreMultipleInstruction(nval, ref comp);
                ins.Decode();
                ins.setCode(nval);
                return(ins);
            }
            //return dataprocessing instruction
            uint type = Memory.ExtractBits(nval, 26, 27);

            if (type == 0)
            {
                DataProcessingInstruction ins = DataProcessingInstruction.Decode(nval, ref comp);
                ins.setCode(nval);
                return(ins);
            }
            else
            {//return loadstore instruction
                LoadStoreInstruction load = new LoadStoreInstruction(nval, ref comp);
                load.Decode();
                load.setCode(nval);
                return(load);
            }
        }