public string[] Disassemble(int LineCount = 1, bool PrintLines = false) { int ret = 2; // word List <string> tmpResult = new List <string>(); while (tmpResult.Count < LineCount) { string result = $"{ this.Index.ToString("X8")} "; // TODO: FIX THIS MESS WTF? int grabSize = 6; if ((this.Index + grabSize) - this.Buffer.Length > 0) { grabSize = (this.Index + grabSize) - this.Buffer.Length; } byte[] buf = new byte[grabSize]; // FIX End of array error? // fill temp buff for (int b = 0; b < buf.Length; b++) { buf[b] = this.Buffer[this.Index + b]; } U8Cmd cmd = new U8Cmd(); ret = U8Decoder.DecodeOpcode(buf, ref cmd); if (ret == -1) { result += "err"; tmpResult.Add(result); if (PrintLines) { PrintDisasm(result); } break; } this.Index += ret; string bytestr = ""; for (int j = 0; j < ret; j += 2) { bytestr += BitConverter.ToUInt16(buf, j).ToString("X4") + " "; } result += bytestr.PadRight(15) + cmd.Instruction.PadRight(10) + cmd.Operands; tmpResult.Add(result); _CacheDisassembly(this.Index, result); if (PrintLines) { PrintDisasm(result); } } return(tmpResult.ToArray()); }
private void AddHandler(U8Cmd cmd) { // This instruction adds the sign-extended signed8 to the contetnts of the stack // poionter and stores the result in the stack pointer. // bit 7 in signed8 is interpreted as the sign bit, so signed8 is an integer // quantity between -128 and +127. if(U8Decoder.isNegative7Bit((byte)cmd.Op2) == 1) StackPtr -= U8Decoder.ABS7Bit((byte)cmd.Op2); else StackPtr += U8Decoder.ABS7Bit((byte)cmd.Op2); }
private void AddSpO(U8Cmd cmd) { // This inst adds the sign-extended signed8 to the contents of the stack pointer // and stores the result in the stack pointer if (U8Decoder.isNegative7Bit((byte)cmd.Op1) == 1) { this.Registers.SP -= U8Decoder.ABS7Bit((byte)cmd.Op1); // TODO: verify, may need Op2!!! } else { this.Registers.SP += U8Decoder.ABS7Bit((byte)cmd.Op1); } this.Registers.PC += 2; }
private void AddERegO(U8Cmd cmd) { // This inst adds the sign-extended immediate vlaue to the contents of the specified // word-sized register and stores the result in the register. var sum = 0; if (U8Decoder.isNegative7Bit((byte)cmd.Op2) == 1) { sum = this.Registers.GetERegisterByIndex((byte)cmd.Op1) - U8Decoder.ABS7Bit((byte)cmd.Op2); } else { sum = this.Registers.GetERegisterByIndex((byte)cmd.Op1) + U8Decoder.ABS7Bit((byte)cmd.Op2); } this.Registers.SetERegisterByIndex((byte)cmd.Op1, (ushort)sum); this.Registers.PC += 2; // TODO: this.Registers.PSW.C this.Registers.PSW.Z = sum == 0; // TODO: this.Registers.PSW.S = TRACK TOP BIT OF RESULT this.Registers.PSW.OV = sum > ushort.MaxValue; // TODO: this.Registers.PSW.HC = idk ;/ }
// disassembles with block propertys private int GetBlock(int Address, ref U8Cmd Cmd, ref U8CodeBlock newBlock, ref bool isEndOfBlock) { // ALL Conditional relative branch instructions: int ret = -1; byte[] buf = new byte[6]; if (Address + 6 > this.Memory.Length) { isEndOfBlock = true; // wont come back again return(-1); // Cia Adios } // fill temp buff for (int b = 0; b < buf.Length; b++) { buf[b] = this.Memory[Address + b]; } // get opcode //u8_cmd opcode = new u8_cmd(); ret = U8Decoder.DecodeOpcode(buf, ref Cmd); Cmd.Address = Address;// dafuq??? //Cmds.Add(opcode); // check branches switch ((U8_OP)Cmd.Type) { case U8_OP.BGE_RAD: // conditional branch case U8_OP.BLT_RAD: case U8_OP.BGT_RAD: case U8_OP.BLE_RAD: case U8_OP.BGES_RAD: case U8_OP.BLTS_RAD: case U8_OP.BGTS_RAD: case U8_OP.BLES_RAD: case U8_OP.BNE_RAD: case U8_OP.BEQ_RAD: case U8_OP.BNV_RAD: case U8_OP.BOV_RAD: case U8_OP.BPS_RAD: case U8_OP.BNS_RAD: case U8_OP.BAL_RAD: // if cond ? radr : PC+=2 // if op1 < 0 then negative else positive; // conditions? dont care actually ;D newBlock.JumpsToBlock = Cmd.Address + Cmd.Op1; // takes jump newBlock.NextBlock = Cmd.Address + 2; // continue isEndOfBlock = true; break; case U8_OP.B_AD: // branch // PC = cadr[15:0] (op1) + second word newBlock.NextBlock = (Cmd.Op1 * 0x10000) + Cmd.sWord; // takes jump newBlock.JumpsToBlock = -1; // newBlock.JumpsToBlock; // dont set so we know its forced? isEndOfBlock = true; break; //// NOTE: Just ignore them? //case U8Decoder.U8_BL_AD: // caller address // newBlock.NextBlock = Cmd.Address + 4; // len 2 // newBlock.JumpsToBlock = -1; // isEndOfBlock = true; // break; //case U8Decoder.U8_BL_ER: // caller register ER // newBlock.NextBlock = Cmd.Address + 2; ; // len 1 // newBlock.JumpsToBlock = -1; // isEndOfBlock = true; // break; case U8_OP.B_ER: // this jumps to ER? whats in ER? newBlock.NextBlock = Cmd.Address += 2; // continue isEndOfBlock = true; break; case U8_OP.RT: // return from subroutine? isEndOfBlock = true; // there is no next ;D break; default: break; } return(ret); }