Exemple #1
0
        public SPUCommand(byte[] cmd, int offset)
        {
            string key = ConversionUtil.byteToBinString(cmd, offset);

            key         += ConversionUtil.byteToBinString(cmd, offset + 1);
            key         += ConversionUtil.byteToBinString(cmd, offset + 2);
            key         += ConversionUtil.byteToBinString(cmd, offset + 3);
            node         = SPUOpcodeTable.Instance.Opcodes.getTreeNodeFirstLeafByKey(key);
            rt           = ra = rb = rc = idx = 0;
            functionName = "";

            try
            {
                fullCommand = node.data.getParameterString(key, offset);
                mnemonics   = node.data.mnemonic;
                type        = node.data.type;
                parseParameter(key);
            }
            catch (Exception)
            {
                mnemonics   = ".byte";
                fullCommand = "";
                type        = SPUOpcodeType.FAIL;
            }
        }
Exemple #2
0
 public static void float_to_reg(SPU spu, int r, float[] d)
 {
     for (int i = 0; i < 4; i++)
     {
         spu.Register[r, i] = ConversionUtil.byteToUInt(BitConverter.GetBytes(d[i]));
     }
 }
Exemple #3
0
 public static float[] reg_to_float(SPU spu, int r)
 {
     float[] d = new float[4];
     for (int i = 0; i < 4; i++)
     {
         d[i] = BitConverter.ToSingle(ConversionUtil.uintToByte(spu.Register[r, i]), 0);
     }
     return(d);
 }
Exemple #4
0
 public static void ls2reg(SPU spu, int r, uint addr)
 {
     addr &= SPUCommandHelper.LSLR & 0xfffffff0;
     byte[] ls = new byte[4];
     System.Array.Copy(spu.LocalStorage, addr, ls, 0, 4);
     spu.Register[r, 0] = ConversionUtil.byteToUInt(ls);
     System.Array.Copy(spu.LocalStorage, addr + 4, ls, 0, 4);
     spu.Register[r, 1] = ConversionUtil.byteToUInt(ls);
     System.Array.Copy(spu.LocalStorage, addr + 8, ls, 0, 4);
     spu.Register[r, 2] = ConversionUtil.byteToUInt(ls);
     System.Array.Copy(spu.LocalStorage, addr + 12, ls, 0, 4);
     spu.Register[r, 3] = ConversionUtil.byteToUInt(ls);
 }
Exemple #5
0
 public static void double_to_reg(SPU spu, int r, double[] d)
 {
     byte[] b;
     byte[] b2 = new byte[4];
     b     = BitConverter.GetBytes(d[0]);
     b2[0] = b[4]; b2[1] = b[5]; b2[2] = b[6]; b2[3] = b[7];
     spu.Register[r, 1] = ConversionUtil.byteToUInt(b);
     spu.Register[r, 0] = ConversionUtil.byteToUInt(b2);
     b     = BitConverter.GetBytes(d[1]);
     b2[0] = b[4]; b2[1] = b[5]; b2[2] = b[6]; b2[3] = b[7];
     spu.Register[r, 3] = ConversionUtil.byteToUInt(b);
     spu.Register[r, 2] = ConversionUtil.byteToUInt(b2);
 }
Exemple #6
0
        public static void LoadElf(string FileName, SPU spu, bool setProgramCounter)
        {
            BinaryReader br = new BinaryReader(File.OpenRead(FileName));

            // GetElfHeader
            byte[] elfMagic = new byte[4];
            br.Read(elfMagic, 0, 4);
            if (elfMagic[0] != 0x7F ||
                elfMagic[0] != 0x7F ||
                elfMagic[0] != 0x7F ||
                elfMagic[0] != 0x7F)
            {
                MessageBox.Show("Elf Magic Wrong (" + FileName + ")");
                return;
            }
            br.BaseStream.Seek(0, SeekOrigin.Begin);
            byte[] eHDR = new byte[0x34];
            br.Read(eHDR, 0, eHDR.Length);
            uint   phdr_offset = ConversionUtil.byteToUInt(eHDR, 0x1C);
            ushort n_phdrs     = ConversionUtil.byteToUShort(eHDR, 0x2C);

            for (ushort i = 0; i < n_phdrs; i++)
            {
                int error = LoadElfPHDR(br, spu, phdr_offset, i);
                if (error == 1)
                {
                    MessageBox.Show("Didn't Load phdr " + i + " of File " + FileName);
                }
                else if (error == 2)
                {
                    MessageBox.Show("Local Storage Overflow!");
                }
            }

            if (setProgramCounter)
            {
                spu.IP = ConversionUtil.byteToUInt(eHDR, 0x18);
            }
            br.Close();
        }
Exemple #7
0
        public static void reg2ls(SPU spu, int r, uint addr)
        {
            addr &= SPUCommandHelper.LSLR & 0xfffffff0;
            byte[] r0 = ConversionUtil.uintToByte(spu.Register[r, 0]);
            byte[] r1 = ConversionUtil.uintToByte(spu.Register[r, 1]);
            byte[] r2 = ConversionUtil.uintToByte(spu.Register[r, 2]);
            byte[] r3 = ConversionUtil.uintToByte(spu.Register[r, 3]);


            for (int i = 0; i < 4; i++)
            {
                spu.LocalStorage[addr + i]      = r0[i];
                spu.LocalStorage[addr + 4 + i]  = r1[i];
                spu.LocalStorage[addr + 8 + i]  = r2[i];
                spu.LocalStorage[addr + 12 + i] = r3[i];
            }

            for (uint i = 0; i < 16; i += 4)
            {
                uint cmdI = (addr + i) >> 2;
                spu.LocalStorageCommands[cmdI] = new SPUCommand(spu.LocalStorage, (int)(cmdI << 2));
            }
        }
Exemple #8
0
        public static int LoadElfPHDR(BinaryReader br, SPU spu, uint phdr_offset, uint i)
        {
            byte[] phdr = new byte[0x20];
            uint   offset, paddr, size;

            br.BaseStream.Seek(phdr_offset + 0x20 * i, SeekOrigin.Begin);
            br.Read(phdr, 0, phdr.Length);
            if (ConversionUtil.byteToUInt(phdr) != 1)
            {
                return(1);
            }
            offset = ConversionUtil.byteToUInt(phdr, 0x04);
            paddr  = ConversionUtil.byteToUInt(phdr, 0x0C);
            size   = ConversionUtil.byteToUInt(phdr, 0x10);

            if ((offset + size) > spu.LocalStorage.Length)
            {
                return(2);
            }
            br.BaseStream.Seek(offset, SeekOrigin.Begin);
            br.Read(spu.LocalStorage, (int)paddr, (int)size);
            return(0);
        }
Exemple #9
0
 public static double[] reg_to_double(SPU spu, int r)
 {
     byte[]   b  = new byte[4];
     byte[]   b2 = new byte[4];
     double[] d  = new double[2];
     byte[]   db = new byte[8];
     b  = ConversionUtil.uintToByte(spu.Register[r, 1]);
     b2 = ConversionUtil.uintToByte(spu.Register[r, 0]);
     for (int i = 0; i < 4; i++)
     {
         db[i]     = b[i];
         db[i + 4] = b2[i];
     }
     d[0] = BitConverter.ToDouble(db, 0);
     b    = ConversionUtil.uintToByte(spu.Register[r, 3]);
     b2   = ConversionUtil.uintToByte(spu.Register[r, 2]);
     for (int i = 0; i < 4; i++)
     {
         db[i]     = b[i];
         db[i + 4] = b2[i];
     }
     d[1] = BitConverter.ToDouble(db, 0);
     return(d);
 }
        public string getParameterString(string key, int pc)
        {
            string result = "";
            int    idx    = 0;
            string ra     = "";
            string rt     = "";

            switch (type)
            {
            case SPUOpcodeType.RR:
                return(mnemonic + " " + getRegisterString(ConversionUtil.binStringToInt(key.Substring(25, 7))) + ", " +
                       getRegisterString(ConversionUtil.binStringToInt(key.Substring(18, 7))) + ", " +
                       getRegisterString(ConversionUtil.binStringToInt(key.Substring(11, 7))));

            case SPUOpcodeType.RRR:
                return(mnemonic + " " + getRegisterString(ConversionUtil.binStringToInt(key.Substring(4, 7))) + ", " +
                       getRegisterString(ConversionUtil.binStringToInt(key.Substring(18, 7))) + ", " +
                       getRegisterString(ConversionUtil.binStringToInt(key.Substring(11, 7))) + ", " +
                       getRegisterString(ConversionUtil.binStringToInt(key.Substring(25, 7))));

            case SPUOpcodeType.RI7:
                rt     = getRegisterString(ConversionUtil.binStringToInt(key.Substring(25, 7)));
                result = mnemonic + " " + rt + ", ";
                ra     = getRegisterString(ConversionUtil.binStringToInt(key.Substring(18, 7)));
                idx    = ConversionUtil.binStringToInt(key.Substring(11, 7));
                if (signed)
                {
                    idx <<= 32 - 7;
                    idx >>= 32 - 7;
                }
                idx <<= shift;
                if (mnemonic[0] == 'b' && mnemonic[1] != 'r' && mnemonic[2] != 'a')
                {
                    idx += pc;
                }
                else if (mnemonic[mnemonic.Length - 1] == 'd')
                {
                    return(result + ((idx <= 0) ? "" + idx : "0x" + idx.ToString("X")) + "(" + ra + ")");
                }
                if (mnemonic == "shlqbyi" && idx == 0)
                {
                    return("lr " + rt + ", " + ra);
                }
                return(result + ra + ", " + ((idx <= 0) ? "" + idx : "0x" + idx.ToString("X")));

            case SPUOpcodeType.RI8:
                rt     = getRegisterString(ConversionUtil.binStringToInt(key.Substring(25, 7)));
                result = mnemonic + " " + rt + ", ";
                ra     = getRegisterString(ConversionUtil.binStringToInt(key.Substring(18, 7)));
                idx    = ConversionUtil.binStringToInt(key.Substring(10, 8));
                if (signed)
                {
                    idx <<= 32 - 8;
                    idx >>= 32 - 8;
                }
                idx <<= shift;
                if (mnemonic[0] == 'b' && mnemonic[1] != 'r' && mnemonic[2] != 'a')
                {
                    idx += pc;
                }
                else if (mnemonic[mnemonic.Length - 1] == 'd')
                {
                    return(result + ((idx <= 0) ? "" + idx : "0x" + idx.ToString("X")) + "(" + ra + ")");
                }
                return(result + ra + ", " + ((idx <= 0) ? "" + idx : "0x" + idx.ToString("X")));

            case SPUOpcodeType.RI10:
                rt     = getRegisterString(ConversionUtil.binStringToInt(key.Substring(25, 7)));
                result = mnemonic + " " + rt + ", ";
                ra     = getRegisterString(ConversionUtil.binStringToInt(key.Substring(18, 7)));
                idx    = ConversionUtil.binStringToInt(key.Substring(8, 10));
                if (signed)
                {
                    idx <<= 32 - 10;
                    idx >>= 32 - 10;
                }
                idx <<= shift;
                if (mnemonic[0] == 'b' && mnemonic[1] != 'r' && mnemonic[2] != 'a')
                {
                    idx += pc;
                }
                else if (mnemonic[mnemonic.Length - 1] == 'd')
                {
                    return(result + ((idx <= 0) ? "" + idx : "0x" + idx.ToString("X")) + "(" + ra + ")");
                }
                else if (mnemonic == "ori" && idx == 0)
                {
                    return("lr " + rt + ", " + ra);
                }
                return(result + ra + ", " + ((idx <= 0) ? "" + idx : "0x" + idx.ToString("X")));

            case SPUOpcodeType.RI16:
                rt     = getRegisterString(ConversionUtil.binStringToInt(key.Substring(25, 7)));
                result = mnemonic + " " + rt + ", ";
                idx    = ConversionUtil.binStringToInt(key.Substring(9, 16));
                if (signed)
                {
                    idx <<= 32 - 16;
                    idx >>= 32 - 16;
                }
                idx <<= shift;
                if (mnemonic[0] == 'b' && mnemonic[1] != 'r' && mnemonic[2] != 'a')
                {
                    idx += pc;
                }
                return(result + ((idx <= 0) ? "" + idx : "0x" + idx.ToString("X")));

            case SPUOpcodeType.RI18:
                rt     = getRegisterString(ConversionUtil.binStringToInt(key.Substring(25, 7)));
                result = mnemonic + " " + rt + ", ";
                idx    = ConversionUtil.binStringToInt(key.Substring(7, 18));
                if (signed)
                {
                    idx <<= 32 - 16;
                    idx >>= 32 - 16;
                }
                idx <<= shift;
                if (mnemonic[0] == 'b' && mnemonic[1] != 'r' && mnemonic[2] != 'a')
                {
                    idx += pc;
                }
                return(result + ((idx <= 0) ? "" + idx : "0x" + idx.ToString("X")));

            case SPUOpcodeType.Special:
                return(mnemonic);
            }
            return("");
        }
Exemple #11
0
        public static void LoadScriptFile(string FileName, SPU spu)
        {
            StreamReader fs = new StreamReader(File.OpenRead(FileName));
            string       input;

            char[] tokens = { ',' };
            while ((input = fs.ReadLine()) != null)
            {
                string unModdedInput = input;
                input = input.Trim();
                input = input.Replace("\t", " ");
                input = input.Replace(" ", "");
                string inputLower = input.ToLower();
                if (input != "")
                {
                    SPUDumperCmd dcmd;
                    string[]     token = input.Split(tokens);
                    switch (token[0])
                    {
                    case "r":     // r,0,1,0xAFCE
                        int  registerSelect     = int.Parse(token[1]);
                        int  registerByteSelect = int.Parse(token[2]);
                        uint valueSelect        = ParseUInt(token[3]);
                        spu.Register[registerSelect, registerByteSelect] = valueSelect;
                        break;

                    case "elf":     // elf,blub.elf,true
                        FileLoader.LoadElf(token[1], spu, (token.Length > 2 && token[2] == "true"));
                        break;

                    case "bin":
                        int lsStart = (int)ParseUInt(token[1]);
                        FileLoader.LoadBin(lsStart, token[2], spu);
                        break;

                    case "mfc":
                        byte[] data = FileLoader.LoadBin(token[3]);
                        uint   EAH  = ParseUInt(token[1]);
                        uint   EAL  = ParseUInt(token[2]);
                        spu.MFC.WriteMemory(EAH, EAL, data);
                        break;

                    case "ip":     // ip,0x400
                        spu.IP = ParseUInt(token[1]);
                        break;

                    case "mBox":     // mBox, 0x0000
                        spu.mBox.Push(ParseUInt(token[1]));
                        break;

                    case "print":     // print, 0x12345678, [string]
                        string[] printToken = unModdedInput.Split(tokens, 3);
                        dcmd = new SPUDumperCmd(token[0], new string[] { printToken[2] });
                        SPUDumper.Instance.Add(ParseUInt(token[1]), dcmd);
                        break;

                    case "print_r":     // print_r, 0x12345678, [register], [register_part]
                    case "print_ls":    // print_ls, 0x12345678, [spezial_addr], [spezial_size]
                        dcmd = new SPUDumperCmd(token[0], new string[] { token[2], token[3] });
                        SPUDumper.Instance.Add(ParseUInt(token[1]), dcmd);
                        break;

                    case "print_dma":     // print_ls, 0x12345678, [spezial_eah], [spezial_eal], [spezial_size]
                        dcmd = new SPUDumperCmd(token[0], new string[] { token[2], token[3], token[4] });
                        SPUDumper.Instance.Add(ParseUInt(token[1]), dcmd);
                        break;

                    case "setls":
                        byte b = (byte)ParseUInt(token[2]);
                        spu.LocalStorage[ParseUInt(token[1])] = b;
                        break;

                    case "setls32":
                        uint   val  = ParseUInt(token[2]);
                        uint   addr = ParseUInt(token[1]);
                        byte[] bb   = ConversionUtil.uintToByte(val);
                        for (int i = 0; i < 4; i++)
                        {
                            spu.LocalStorage[addr + i] = bb[i];
                        }
                        break;
                    }
                }
            }
            fs.Close();
        }
Exemple #12
0
        public void parseParameter(string key)
        {
            switch (type)
            {
            case SPUOpcodeType.RR:
                rt = ConversionUtil.binStringToInt(key.Substring(25, 7));
                ra = ConversionUtil.binStringToInt(key.Substring(18, 7));
                rb = ConversionUtil.binStringToInt(key.Substring(11, 7));
                break;

            case SPUOpcodeType.RRR:
                rt = ConversionUtil.binStringToInt(key.Substring(4, 7));
                ra = ConversionUtil.binStringToInt(key.Substring(18, 7));
                rb = ConversionUtil.binStringToInt(key.Substring(11, 7));
                rc = ConversionUtil.binStringToInt(key.Substring(25, 7));
                break;

            case SPUOpcodeType.RI7:
                rt  = ConversionUtil.binStringToInt(key.Substring(25, 7));
                ra  = ConversionUtil.binStringToInt(key.Substring(18, 7));
                idx = ConversionUtil.binStringToInt(key.Substring(11, 7));
                if (node.data.signed)
                {
                    idx <<= 32 - 7;
                    idx >>= 32 - 7;
                }
                idx <<= node.data.shift;
                break;

            case SPUOpcodeType.RI8:
                rt  = ConversionUtil.binStringToInt(key.Substring(25, 7));
                ra  = ConversionUtil.binStringToInt(key.Substring(18, 7));
                idx = ConversionUtil.binStringToInt(key.Substring(10, 8));
                if (node.data.signed)
                {
                    idx <<= 32 - 8;
                    idx >>= 32 - 8;
                }
                idx <<= node.data.shift;
                break;

            case SPUOpcodeType.RI10:
                rt  = ConversionUtil.binStringToInt(key.Substring(25, 7));
                ra  = ConversionUtil.binStringToInt(key.Substring(18, 7));
                idx = ConversionUtil.binStringToInt(key.Substring(8, 10));
                if (node.data.signed)
                {
                    idx <<= 32 - 10;
                    idx >>= 32 - 10;
                }
                idx <<= node.data.shift;
                break;

            case SPUOpcodeType.RI16:
                rt  = ConversionUtil.binStringToInt(key.Substring(25, 7));
                idx = ConversionUtil.binStringToInt(key.Substring(9, 16));
                if (node.data.signed)
                {
                    idx <<= 32 - 16;
                    idx >>= 32 - 16;
                }
                idx <<= node.data.shift;
                break;

            case SPUOpcodeType.RI18:
                rt  = ConversionUtil.binStringToInt(key.Substring(25, 7));
                idx = ConversionUtil.binStringToInt(key.Substring(7, 18));
                if (node.data.signed)
                {
                    idx <<= 32 - 16;
                    idx >>= 32 - 16;
                }
                idx <<= node.data.shift;
                break;

            case SPUOpcodeType.Special:
                break;
            }
        }