"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetOpcodeName = exports.EncodeInstruction = exports.DecodeJumpTarget = exports.EncodeJumpTarget = exports.DecodeImmediate = exports.EncodeImmediate = exports.DecodeRegisterSource = exports.EncodeRegisterSource = exports.DecodeRegisterTarget = exports.EncodeRegisterTarget = exports.DecodeRegisterDestination = exports.EncodeRegisterDestination = exports.DecodeShift = exports.EncodeShift = exports.DecodeOffset = exports.EncodeOffset = exports.DecodeOpcode = exports.Instruction = exports.REGISTER = exports.OPCODEINDEXTYPE = exports.OPCODETYPE = exports.OPCODE_COND = exports.OPCODE_SPECIAL1 = exports.OPCODE_SPECIAL0 = exports.OPCODE_DEFAULT = exports.LUIORI_DECODE = exports.LUIORI_ENCODE = exports.JAL_DECODE = exports.J_ENCODE = exports.JAL_ENCODE = void 0;
// encodes JAL instruction to jump to the address in data
function JAL_ENCODE(data) {
    return 0x0C000000 | ((data >> 2) & 0x3FFFFFF);
}
exports.JAL_ENCODE = JAL_ENCODE;
function J_ENCODE(data) {
    return 0x08000000 | ((data >> 2) & 0x3FFFFFF);
}
exports.J_ENCODE = J_ENCODE;
// returns the address that the instruction in data would JAL to
function JAL_DECODE(data) {
    return ((data.readUInt32BE(0) & 0x3FFFFFF) << 2);
}
exports.JAL_DECODE = JAL_DECODE;
// encodes LUI/ORI instruction data to use immediate
function LUIORI_ENCODE(data, immediate) {
    let instruction_base = data.readUInt16BE(0);
    data.writeUInt16BE(immediate, 2);
    return data.readUInt32BE(0);
}
exports.LUIORI_ENCODE = LUIORI_ENCODE;
// returns the immediate of a LUI/ORI in data
function LUIORI_DECODE(data) {
    return data.readUInt16BE(2);
}
exports.LUIORI_DECODE = LUIORI_DECODE;
var OPCODE_DEFAULT;
(function (OPCODE_DEFAULT) {
    OPCODE_DEFAULT[OPCODE_DEFAULT["SPEC"] = 0] = "SPEC";
    OPCODE_DEFAULT[OPCODE_DEFAULT["BCOND"] = 1] = "BCOND";
    OPCODE_DEFAULT[OPCODE_DEFAULT["J"] = 2] = "J";
    OPCODE_DEFAULT[OPCODE_DEFAULT["JAL"] = 3] = "JAL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["BEQ"] = 4] = "BEQ";
    OPCODE_DEFAULT[OPCODE_DEFAULT["BNE"] = 5] = "BNE";
    OPCODE_DEFAULT[OPCODE_DEFAULT["BLEZ"] = 6] = "BLEZ";
    OPCODE_DEFAULT[OPCODE_DEFAULT["BGTZ"] = 7] = "BGTZ";
    OPCODE_DEFAULT[OPCODE_DEFAULT["ADDI"] = 8] = "ADDI";
    OPCODE_DEFAULT[OPCODE_DEFAULT["ADDIU"] = 9] = "ADDIU";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SLTI"] = 10] = "SLTI";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SLTIU"] = 11] = "SLTIU";
    OPCODE_DEFAULT[OPCODE_DEFAULT["ANDI"] = 12] = "ANDI";
    OPCODE_DEFAULT[OPCODE_DEFAULT["ORI"] = 13] = "ORI";
    OPCODE_DEFAULT[OPCODE_DEFAULT["XORI"] = 14] = "XORI";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LUI"] = 15] = "LUI";
    OPCODE_DEFAULT[OPCODE_DEFAULT["COP0"] = 16] = "COP0";
    OPCODE_DEFAULT[OPCODE_DEFAULT["COP1"] = 17] = "COP1";
    OPCODE_DEFAULT[OPCODE_DEFAULT["COP2"] = 18] = "COP2";
    OPCODE_DEFAULT[OPCODE_DEFAULT["COP3"] = 19] = "COP3";
    OPCODE_DEFAULT[OPCODE_DEFAULT["BEQL"] = 20] = "BEQL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["BNEL"] = 21] = "BNEL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["BLEZL"] = 22] = "BLEZL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["BGTZL"] = 23] = "BGTZL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["DADDI"] = 24] = "DADDI";
    OPCODE_DEFAULT[OPCODE_DEFAULT["DADDIU"] = 25] = "DADDIU";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LDL"] = 26] = "LDL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LDR"] = 27] = "LDR";
    OPCODE_DEFAULT[OPCODE_DEFAULT["OP34"] = 28] = "OP34";
    OPCODE_DEFAULT[OPCODE_DEFAULT["OP35"] = 29] = "OP35";
    OPCODE_DEFAULT[OPCODE_DEFAULT["OP36"] = 30] = "OP36";
    OPCODE_DEFAULT[OPCODE_DEFAULT["OP37"] = 31] = "OP37";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LB"] = 32] = "LB";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LH"] = 33] = "LH";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LWL"] = 34] = "LWL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LW"] = 35] = "LW";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LBU"] = 36] = "LBU";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LHU"] = 37] = "LHU";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LWR"] = 38] = "LWR";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LWU"] = 39] = "LWU";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SB"] = 40] = "SB";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SH"] = 41] = "SH";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SWL"] = 42] = "SWL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SW"] = 43] = "SW";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SDL"] = 44] = "SDL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SDR"] = 45] = "SDR";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SWR"] = 46] = "SWR";
    OPCODE_DEFAULT[OPCODE_DEFAULT["CACHE"] = 47] = "CACHE";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LL"] = 48] = "LL";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LWC1"] = 49] = "LWC1";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LWC2"] = 50] = "LWC2";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LWC3"] = 51] = "LWC3";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LLD"] = 52] = "LLD";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LDC1"] = 53] = "LDC1";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LDC2"] = 54] = "LDC2";
    OPCODE_DEFAULT[OPCODE_DEFAULT["LD"] = 55] = "LD";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SC"] = 56] = "SC";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SWC1"] = 57] = "SWC1";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SWC2"] = 58] = "SWC2";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SWC3"] = 59] = "SWC3";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SCD"] = 60] = "SCD";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SDC1"] = 61] = "SDC1";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SDC2"] = 62] = "SDC2";
    OPCODE_DEFAULT[OPCODE_DEFAULT["SD"] = 63] = "SD";
    OPCODE_DEFAULT[OPCODE_DEFAULT["COUNT"] = 64] = "COUNT";
})(OPCODE_DEFAULT = exports.OPCODE_DEFAULT || (exports.OPCODE_DEFAULT = {}));
;
var OPCODE_SPECIAL0;
(function (OPCODE_SPECIAL0) {
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SLL"] = 0] = "SLL";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC01"] = 1] = "SPEC01";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SRL"] = 2] = "SRL";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SRA"] = 3] = "SRA";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SLLV"] = 4] = "SLLV";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC05"] = 5] = "SPEC05";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SRLV"] = 6] = "SRLV";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SRAV"] = 7] = "SRAV";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["JR"] = 8] = "JR";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["JALR"] = 9] = "JALR";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC12"] = 10] = "SPEC12";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC13"] = 11] = "SPEC13";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SYSCALL"] = 12] = "SYSCALL";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["BREAK"] = 13] = "BREAK";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC16"] = 14] = "SPEC16";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SYNC"] = 15] = "SYNC";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["MFHI"] = 16] = "MFHI";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["MTHI"] = 17] = "MTHI";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["MFLO"] = 18] = "MFLO";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["MTLO"] = 19] = "MTLO";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSLLV"] = 20] = "DSLLV";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC25"] = 21] = "SPEC25";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSRLV"] = 22] = "DSRLV";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSRAV"] = 23] = "DSRAV";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["MULT"] = 24] = "MULT";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["MULTU"] = 25] = "MULTU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DIV"] = 26] = "DIV";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DIVU"] = 27] = "DIVU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DMULT"] = 28] = "DMULT";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DMULTU"] = 29] = "DMULTU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DDIV"] = 30] = "DDIV";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DDIVU"] = 31] = "DDIVU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["ADD"] = 32] = "ADD";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["ADDU"] = 33] = "ADDU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SUB"] = 34] = "SUB";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SUBU"] = 35] = "SUBU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["AND"] = 36] = "AND";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["OR"] = 37] = "OR";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["XOR"] = 38] = "XOR";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["NOR"] = 39] = "NOR";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC50"] = 40] = "SPEC50";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC51"] = 41] = "SPEC51";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SLT"] = 42] = "SLT";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SLTU"] = 43] = "SLTU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DADD"] = 44] = "DADD";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DADDU"] = 45] = "DADDU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSUB"] = 46] = "DSUB";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSUBU"] = 47] = "DSUBU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["TGE"] = 48] = "TGE";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["TGEU"] = 49] = "TGEU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["TLT"] = 50] = "TLT";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["TLTU"] = 51] = "TLTU";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["TEQ"] = 52] = "TEQ";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC65"] = 53] = "SPEC65";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["TNE"] = 54] = "TNE";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC67"] = 55] = "SPEC67";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSLL"] = 56] = "DSLL";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC71"] = 57] = "SPEC71";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSRL"] = 58] = "DSRL";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSRA"] = 59] = "DSRA";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSLL32"] = 60] = "DSLL32";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["SPEC75"] = 61] = "SPEC75";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSRL32"] = 62] = "DSRL32";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["DSRA32"] = 63] = "DSRA32";
    OPCODE_SPECIAL0[OPCODE_SPECIAL0["COUNT"] = 64] = "COUNT";
})(OPCODE_SPECIAL0 = exports.OPCODE_SPECIAL0 || (exports.OPCODE_SPECIAL0 = {}));
;
var OPCODE_SPECIAL1;
(function (OPCODE_SPECIAL1) {
    OPCODE_SPECIAL1[OPCODE_SPECIAL1["MAD"] = 0] = "MAD";
    OPCODE_SPECIAL1[OPCODE_SPECIAL1["MADU"] = 1] = "MADU";
    OPCODE_SPECIAL1[OPCODE_SPECIAL1["MUL"] = 2] = "MUL";
    OPCODE_SPECIAL1[OPCODE_SPECIAL1["SPEC3"] = 3] = "SPEC3";
    OPCODE_SPECIAL1[OPCODE_SPECIAL1["COUNT"] = 4] = "COUNT";
})(OPCODE_SPECIAL1 = exports.OPCODE_SPECIAL1 || (exports.OPCODE_SPECIAL1 = {}));
var OPCODE_COND;
(function (OPCODE_COND) {
    OPCODE_COND[OPCODE_COND["BLTZ"] = 0] = "BLTZ";
    OPCODE_COND[OPCODE_COND["BGEZ"] = 1] = "BGEZ";
    OPCODE_COND[OPCODE_COND["BLTZL"] = 2] = "BLTZL";
    OPCODE_COND[OPCODE_COND["BGEZL"] = 3] = "BGEZL";
    OPCODE_COND[OPCODE_COND["UNKNOWN0"] = 4] = "UNKNOWN0";
    OPCODE_COND[OPCODE_COND["UNKNOWN1"] = 5] = "UNKNOWN1";
    OPCODE_COND[OPCODE_COND["UNKNOWN2"] = 6] = "UNKNOWN2";
    OPCODE_COND[OPCODE_COND["UNKNOWN3"] = 7] = "UNKNOWN3";
    OPCODE_COND[OPCODE_COND["TGEI"] = 8] = "TGEI";
    OPCODE_COND[OPCODE_COND["TGEIU"] = 9] = "TGEIU";
    OPCODE_COND[OPCODE_COND["TLTI"] = 10] = "TLTI";
    OPCODE_COND[OPCODE_COND["TLTIU"] = 11] = "TLTIU";
    OPCODE_COND[OPCODE_COND["TEQI"] = 12] = "TEQI";
    OPCODE_COND[OPCODE_COND["UNKNOWN4"] = 13] = "UNKNOWN4";
    OPCODE_COND[OPCODE_COND["TNEI"] = 14] = "TNEI";
    OPCODE_COND[OPCODE_COND["UNKNOWN5"] = 15] = "UNKNOWN5";
    OPCODE_COND[OPCODE_COND["BLTZAL"] = 16] = "BLTZAL";
    OPCODE_COND[OPCODE_COND["BGEZAL"] = 17] = "BGEZAL";
    OPCODE_COND[OPCODE_COND["BLTZALL"] = 18] = "BLTZALL";
    OPCODE_COND[OPCODE_COND["BGEZALL"] = 19] = "BGEZALL";
    OPCODE_COND[OPCODE_COND["UNKNOWN6"] = 20] = "UNKNOWN6";
    OPCODE_COND[OPCODE_COND["UNKNOWN7"] = 21] = "UNKNOWN7";
    OPCODE_COND[OPCODE_COND["UNKNOWN8"] = 22] = "UNKNOWN8";
    OPCODE_COND[OPCODE_COND["UNKNOWN9"] = 23] = "UNKNOWN9";
    OPCODE_COND[OPCODE_COND["UNKNOWN10"] = 24] = "UNKNOWN10";
    OPCODE_COND[OPCODE_COND["UNKNOWN11"] = 25] = "UNKNOWN11";
    OPCODE_COND[OPCODE_COND["UNKNOWN12"] = 26] = "UNKNOWN12";
    OPCODE_COND[OPCODE_COND["UNKNOWN13"] = 27] = "UNKNOWN13";
    OPCODE_COND[OPCODE_COND["UNKNOWN14"] = 28] = "UNKNOWN14";
    OPCODE_COND[OPCODE_COND["UNKNOWN15"] = 29] = "UNKNOWN15";
    OPCODE_COND[OPCODE_COND["UNKNOWN16"] = 30] = "UNKNOWN16";
    OPCODE_COND[OPCODE_COND["UNKNOWN17"] = 31] = "UNKNOWN17";
    OPCODE_COND[OPCODE_COND["COUNT"] = 32] = "COUNT";
})(OPCODE_COND = exports.OPCODE_COND || (exports.OPCODE_COND = {}));
;
var OPCODETYPE;
(function (OPCODETYPE) {
    OPCODETYPE[OPCODETYPE["NONE"] = 0] = "NONE";
    OPCODETYPE[OPCODETYPE["IMMEDIATE"] = 1] = "IMMEDIATE";
    OPCODETYPE[OPCODETYPE["JUMP"] = 2] = "JUMP";
    OPCODETYPE[OPCODETYPE["REGISTER"] = 3] = "REGISTER";
    OPCODETYPE[OPCODETYPE["FR"] = 4] = "FR";
    OPCODETYPE[OPCODETYPE["COUNT"] = 5] = "COUNT";
})(OPCODETYPE = exports.OPCODETYPE || (exports.OPCODETYPE = {}));
var OPCODEINDEXTYPE;
(function (OPCODEINDEXTYPE) {
    OPCODEINDEXTYPE[OPCODEINDEXTYPE["NONE"] = 0] = "NONE";
    OPCODEINDEXTYPE[OPCODEINDEXTYPE["DEFAULT"] = 1] = "DEFAULT";
    OPCODEINDEXTYPE[OPCODEINDEXTYPE["SPECIAL0"] = 2] = "SPECIAL0";
    OPCODEINDEXTYPE[OPCODEINDEXTYPE["SPECIAL1"] = 3] = "SPECIAL1";
    OPCODEINDEXTYPE[OPCODEINDEXTYPE["COND"] = 4] = "COND";
    OPCODEINDEXTYPE[OPCODEINDEXTYPE["COUNT"] = 5] = "COUNT";
})(OPCODEINDEXTYPE = exports.OPCODEINDEXTYPE || (exports.OPCODEINDEXTYPE = {}));
var REGISTER;
(function (REGISTER) {
    REGISTER[REGISTER["R0"] = 0] = "R0";
    REGISTER[REGISTER["AT"] = 1] = "AT";
    REGISTER[REGISTER["V0"] = 2] = "V0";
    REGISTER[REGISTER["V1"] = 3] = "V1";
    REGISTER[REGISTER["A0"] = 4] = "A0";
    REGISTER[REGISTER["A1"] = 5] = "A1";
    REGISTER[REGISTER["A2"] = 6] = "A2";
    REGISTER[REGISTER["A3"] = 7] = "A3";
    REGISTER[REGISTER["T0"] = 8] = "T0";
    REGISTER[REGISTER["T1"] = 9] = "T1";
    REGISTER[REGISTER["T2"] = 10] = "T2";
    REGISTER[REGISTER["T3"] = 11] = "T3";
    REGISTER[REGISTER["T4"] = 12] = "T4";
    REGISTER[REGISTER["T5"] = 13] = "T5";
    REGISTER[REGISTER["T6"] = 14] = "T6";
    REGISTER[REGISTER["T7"] = 15] = "T7";
    REGISTER[REGISTER["S0"] = 16] = "S0";
    REGISTER[REGISTER["S1"] = 17] = "S1";
    REGISTER[REGISTER["S2"] = 18] = "S2";
    REGISTER[REGISTER["S3"] = 19] = "S3";
    REGISTER[REGISTER["S4"] = 20] = "S4";
    REGISTER[REGISTER["S5"] = 21] = "S5";
    REGISTER[REGISTER["S6"] = 22] = "S6";
    REGISTER[REGISTER["S7"] = 23] = "S7";
    REGISTER[REGISTER["T8"] = 24] = "T8";
    REGISTER[REGISTER["T9"] = 25] = "T9";
    REGISTER[REGISTER["K0"] = 26] = "K0";
    REGISTER[REGISTER["K1"] = 27] = "K1";
    REGISTER[REGISTER["GP"] = 28] = "GP";
    REGISTER[REGISTER["SP"] = 29] = "SP";
    REGISTER[REGISTER["S8"] = 30] = "S8";
    REGISTER[REGISTER["RA"] = 31] = "RA";
    REGISTER[REGISTER["COUNT"] = 32] = "COUNT";
})(REGISTER = exports.REGISTER || (exports.REGISTER = {}));
class Instruction {
    type = OPCODEINDEXTYPE.NONE;
    indx = 0;
    data = Buffer.alloc(4);
    uint = 0;
    code = "NOP";
}
exports.Instruction = Instruction;
function DecodeOpcode(inst) {
    let instruction_index = 0;
    let instruction = new Instruction();
    instruction.data = inst;
    instruction.uint = inst.readUInt32BE(0);
    instruction.type = OPCODEINDEXTYPE.DEFAULT;
    if (instruction.uint === 0) {
        instruction.code = "NOP";
        return instruction;
    }
    instruction_index = instruction.uint >> 26;
    if (instruction_index < OPCODE_DEFAULT.COUNT)
        instruction.code = OPCODE_DEFAULT[instruction_index];
    else
        instruction.code = "UNKNOWN";
    if (instruction_index === OPCODE_DEFAULT.SPEC) {
        instruction_index = instruction.uint & 0b111111;
        instruction.type = OPCODEINDEXTYPE.SPECIAL0;
        instruction.code = OPCODE_SPECIAL0[instruction_index];
    }
    else if (instruction_index === OPCODE_DEFAULT.COUNT) { //SPECIAL2
        instruction_index = instruction.uint & 0b111111;
        instruction.type = OPCODEINDEXTYPE.SPECIAL1;
        instruction.code = OPCODE_SPECIAL1[instruction_index];
    }
    else if (instruction_index === OPCODE_DEFAULT.BCOND) {
        instruction_index = instruction.uint & 0b111111;
        instruction.type = OPCODEINDEXTYPE.COND;
        instruction.code = OPCODE_COND[instruction_index];
    }
    instruction.indx = instruction_index;
    return instruction;
}
exports.DecodeOpcode = DecodeOpcode;
function EncodeOffset(inst, offset) {
    // TODO
    return DecodeOpcode(inst.data);
}
exports.EncodeOffset = EncodeOffset;
function DecodeOffset(inst) {
    return (inst.uint & 0x0000FFFF) << 2;
}
exports.DecodeOffset = DecodeOffset;
function EncodeShift(inst, shift) {
    // TODO
    return DecodeOpcode(inst.data);
}
exports.EncodeShift = EncodeShift;
function DecodeShift(inst) {
    return (inst.uint >> 6) & 0x1F;
}
exports.DecodeShift = DecodeShift;
function EncodeRegisterDestination(inst, register) {
    // TODO
    return DecodeOpcode(inst.data);
}
exports.EncodeRegisterDestination = EncodeRegisterDestination;
function DecodeRegisterDestination(inst) {
    return ((inst.uint >> 11) & 0x1F);
}
exports.DecodeRegisterDestination = DecodeRegisterDestination;
function EncodeRegisterTarget(inst, target) {
    // TODO
    return DecodeOpcode(inst.data);
}
exports.EncodeRegisterTarget = EncodeRegisterTarget;
function DecodeRegisterTarget(inst) {
    return (inst.uint >> 16) & 0x1F;
}
exports.DecodeRegisterTarget = DecodeRegisterTarget;
function EncodeRegisterSource(inst, source) {
    // TODO
    return DecodeOpcode(inst.data);
}
exports.EncodeRegisterSource = EncodeRegisterSource;
function DecodeRegisterSource(inst) {
    return (inst.uint >> 21) & 0x1F;
}
exports.DecodeRegisterSource = DecodeRegisterSource;
function EncodeImmediate(inst, immediate) {
    inst.data.writeUInt16BE(immediate & 0x0000FFFF, 2);
    return DecodeOpcode(inst.data);
}
exports.EncodeImmediate = EncodeImmediate;
function DecodeImmediate(inst) {
    return inst.data.readUInt16BE(2);
}
exports.DecodeImmediate = DecodeImmediate;
function EncodeJumpTarget(inst, target) {
    inst.data.writeUInt32BE(0x0C000000 | ((target >> 2) & 0x3FFFFFF), 0);
    return DecodeOpcode(inst.data);
}
exports.EncodeJumpTarget = EncodeJumpTarget;
function DecodeJumpTarget(inst) {
    return ((inst.uint & 0x3FFFFFF) << 2);
}
exports.DecodeJumpTarget = DecodeJumpTarget;
function EncodeInstruction(data) {
    let encoded = 0;
    let index = 0;
    let buf = Buffer.alloc(4);
    for (index = 0; index < data.length; index++) {
        encoded |= data[index];
    }
    buf.writeUInt32BE(encoded, 0);
    return DecodeOpcode(buf);
}
exports.EncodeInstruction = EncodeInstruction;
function GetOpcodeName(inst) {
    if (inst.type === OPCODEINDEXTYPE.DEFAULT)
        return OPCODE_DEFAULT[inst.indx];
    else if (inst.type === OPCODEINDEXTYPE.SPECIAL0)
        return OPCODE_SPECIAL0[inst.indx];
    else if (inst.type === OPCODEINDEXTYPE.SPECIAL1)
        return OPCODE_SPECIAL1[inst.indx];
    else
        return OPCODE_COND[inst.indx];
}
exports.GetOpcodeName = GetOpcodeName;
//# sourceMappingURL=OpcodeBullshit.js.map