//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; }
//decode the instruction from <value1> and <comp> and return the appropriate type static public DataProcessingInstruction Decode(uint value1, ref Computer comp) { uint val = Memory.ExtractBits(value1, 21, 24); switch (val) { case 4: //add instruction AddSubInstruction addins = new AddSubInstruction(value1, comp); addins.regshift = -1; addins.type = 0; uint amreg = Memory.ExtractBits(value1, 12, 15); addins.dest = new Operand(amreg, 0, 0, 0, true); amreg = Memory.ExtractBits(value1, 16, 19); addins.source = new Operand(amreg, 0, 0, 0, true); bool aisImm = Memory.TestBit(25, value1); //immediate value if (aisImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); addins.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); addins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); addins.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); addins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(addins); case 2: //sub instruction AddSubInstruction subins = new AddSubInstruction(value1, comp); subins.regshift = -1; subins.type = 1; uint smreg = Memory.ExtractBits(value1, 12, 15); subins.dest = new Operand(smreg, 0, 0, 0, true); smreg = Memory.ExtractBits(value1, 16, 19); subins.source = new Operand(smreg, 0, 0, 0, true); bool sisImm = Memory.TestBit(25, value1); //immediate value if (sisImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); subins.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); subins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); subins.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); subins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(subins); case 0: CompInstruction ains = new CompInstruction(value1, comp); ains.regshift = -1; ains.type = 0; uint areg = Memory.ExtractBits(value1, 12, 15); ains.dest = new Operand(areg, 0, 0, 0, true); areg = Memory.ExtractBits(value1, 16, 19); ains.source = new Operand(areg, 0, 0, 0, true); bool aImm = Memory.TestBit(25, value1); if (aImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); ains.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); ains.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); ains.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); ains.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(ains); case 14: BitClearInstruction bins = new BitClearInstruction(value1, comp); bins.regshift = -1; uint breg = Memory.ExtractBits(value1, 12, 15); bins.dest = new Operand(breg, 0, 0, 0, true); breg = Memory.ExtractBits(value1, 16, 19); bins.source = new Operand(breg, 0, 0, 0, true); bool bImm = Memory.TestBit(25, value1); if (bImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); bins.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); bins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); bins.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); bins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(bins); //and instruction case 10: CompInstruction cmpins = new CompInstruction(value1, comp); cmpins.regshift = -1; cmpins.type = 3; uint cmpreg = Memory.ExtractBits(value1, 16, 19); cmpins.dest = new Operand(cmpreg, 0, 0, 0, true); bool cmpImm = Memory.TestBit(25, value1); if (cmpImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); cmpins.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); cmpins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); cmpins.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); cmpins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(cmpins); //or instruction case 12: CompInstruction cins = new CompInstruction(value1, comp); cins.regshift = -1; cins.type = 1; uint creg = Memory.ExtractBits(value1, 12, 15); cins.dest = new Operand(creg, 0, 0, 0, true); amreg = Memory.ExtractBits(value1, 16, 19); cins.source = new Operand(amreg, 0, 0, 0, true); bool cImm = Memory.TestBit(25, value1); if (cImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); cins.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); cins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); cins.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); cins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(cins); //rsb instruction case 3: AddSubInstruction rsubins = new AddSubInstruction(value1, comp); rsubins.regshift = -1; rsubins.type = 2; uint srmreg = Memory.ExtractBits(value1, 12, 15); rsubins.dest = new Operand(srmreg, 0, 0, 0, true); amreg = Memory.ExtractBits(value1, 16, 19); rsubins.source = new Operand(amreg, 0, 0, 0, true); bool risImm = Memory.TestBit(25, value1); //immediate value if (risImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); rsubins.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); rsubins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); rsubins.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); rsubins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(rsubins); case 15: //mvn MovInstruction mvninstruction = new MovInstruction(value1, comp); mvninstruction.regshift = -1; mvninstruction.mvn = true; uint mreg = Memory.ExtractBits(value1, 12, 15); mvninstruction.dest = new Operand(mreg, 0, 0, 0, true); bool misImm = Memory.TestBit(25, value1); //immediate value if (misImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); mvninstruction.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(val, 0, 3); mvninstruction.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); mvninstruction.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); mvninstruction.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(mvninstruction); //eor instruction case 1: CompInstruction eins = new CompInstruction(value1, comp); eins.regshift = -1; eins.type = 2; uint ereg = Memory.ExtractBits(value1, 12, 15); eins.dest = new Operand(ereg, 0, 0, 0, true); areg = Memory.ExtractBits(value1, 16, 19); eins.source = new Operand(areg, 0, 0, 0, true); bool eImm = Memory.TestBit(25, value1); if (eImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); eins.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); eins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); eins.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); eins.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(eins); //mov instruciton case 13: MovInstruction instruction = new MovInstruction(value1, comp); instruction.sinstruct = Memory.TestBit(20, value1); instruction.regshift = -1; uint reg = Memory.ExtractBits(value1, 12, 15); instruction.dest = new Operand(reg, 0, 0, 0, true); bool isImm = Memory.TestBit(25, value1); //immediate value if (isImm) { uint rotate = Memory.ExtractBits(value1, 8, 11); uint immediate = Memory.ExtractBits(value1, 0, 7); instruction.operand2 = new Operand(immediate, 0, 0, rotate, false); } //register/shifted register else { // register/immediate shifted register bool isReg = Memory.TestBit(4, value1); if (!isReg) { uint shiftAmt = Memory.ExtractBits(value1, 7, 11); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); instruction.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } //register shifted register else { uint shiftAmt = Memory.ExtractBits(value1, 8, 11); instruction.regshift = (int)shiftAmt; shiftAmt = comp.getReg((int)shiftAmt); uint shiftType = Memory.ExtractBits(value1, 5, 6); uint RM = Memory.ExtractBits(value1, 0, 3); instruction.operand2 = new Operand(RM, shiftAmt, shiftType, 0, true); } } return(instruction); default: break; } return(null); }
//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); } }