Example #1
0
        public void _LoadFile(string fileName)
        {
            //GC.Collect();
            SetVirtualFolder(Path.GetDirectoryName(fileName));

            var memoryStream = new PspMemoryStream(PspMemory);

            var arguments = new[]
            {
                "ms0:/PSP/GAME/virtual/EBOOT.PBP",
            };

            Stream loadStream = File.OpenRead(fileName);
            //using ()
            {
                var elfLoadStreamTry = new List <Stream>();
                //Stream ElfLoadStream = null;

                var    format = new FormatDetector().DetectSubType(loadStream);
                string title  = null;
                switch (format)
                {
                case FormatDetector.SubType.Pbp:
                {
                    var pbp = new Pbp().Load(loadStream);
                    elfLoadStreamTry.Add(pbp[Pbp.Types.PspData]);
                    Logger.TryCatch(() =>
                        {
                            var paramSfo = new Psf().Load(pbp[Pbp.Types.ParamSfo]);

                            if (paramSfo.EntryDictionary.ContainsKey("TITLE"))
                            {
                                title = (string)paramSfo.EntryDictionary["TITLE"];
                            }

                            if (paramSfo.EntryDictionary.ContainsKey("PSP_SYSTEM_VER"))
                            {
                                HleConfig.FirmwareVersion = paramSfo.EntryDictionary["PSP_SYSTEM_VER"].ToString();
                            }
                        });
                }
                break;

                case FormatDetector.SubType.Elf:
                    elfLoadStreamTry.Add(loadStream);
                    break;

                case FormatDetector.SubType.Dax:
                case FormatDetector.SubType.Cso:
                case FormatDetector.SubType.Iso:
                {
                    arguments[0] = "disc0:/PSP/GAME/SYSDIR/EBOOT.BIN";

                    var iso = SetIso(fileName);
                    Logger.TryCatch(() =>
                        {
                            var paramSfo = new Psf().Load(iso.Root.Locate("/PSP_GAME/PARAM.SFO").Open());
                            title        = (string)paramSfo.EntryDictionary["TITLE"];
                        });

                    var filesToTry = new[]
                    {
                        "/PSP_GAME/SYSDIR/BOOT.BIN",
                        "/PSP_GAME/SYSDIR/EBOOT.BIN",
                        "/PSP_GAME/SYSDIR/EBOOT.OLD",
                    };

                    foreach (var fileToTry in filesToTry)
                    {
                        try
                        {
                            elfLoadStreamTry.Add(iso.Root.Locate(fileToTry).Open());
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                        //if (ElfLoadStream.Length != 0) break;
                    }

                    /*
                     * if (ElfLoadStream.Length == 0)
                     * {
                     *  throw (new Exception(String.Format("{0} files are empty", String.Join(", ", FilesToTry))));
                     * }
                     */
                }
                break;

                default:
                    throw (new NotImplementedException("Can't load format '" + format + "'"));
                }

                Exception      loadException  = null;
                HleModuleGuest hleModuleGuest = null;

                foreach (var elfLoadStream in elfLoadStreamTry)
                {
                    try
                    {
                        loadException = null;

                        if (elfLoadStream.Length < 256)
                        {
                            throw(new InvalidProgramException("File too short"));
                        }

                        hleModuleGuest = Loader.LoadModule(
                            elfLoadStream,
                            memoryStream,
                            MemoryManager.GetPartition(MemoryPartitions.User),
                            ModuleManager,
                            title,
                            moduleName: fileName,
                            isMainModule: true
                            );

                        loadException = null;

                        break;
                    }
                    catch (InvalidProgramException e)
                    {
                        loadException = e;
                    }
                }

                if (loadException != null)
                {
                    throw loadException;
                }

                RegisterSyscalls();

                const uint startArgumentAddress = 0x08000100;
                var        endArgumentAddress   = startArgumentAddress;

                var argumentsChunk = arguments
                                     .Select(argument => Encoding.UTF8.GetBytes(argument + "\0"))
                                     .Aggregate(new byte[] { }, (accumulate, chunk) => accumulate.Concat(chunk))
                ;

                var reservedSyscallsPartition = MemoryManager.GetPartition(MemoryPartitions.Kernel0).Allocate(
                    0x100,
                    Name: "ReservedSyscallsPartition"
                    );
                var argumentsPartition = MemoryManager.GetPartition(MemoryPartitions.Kernel0).Allocate(
                    argumentsChunk.Length,
                    Name: "ArgumentsPartition"
                    );
                PspMemory.WriteBytes(argumentsPartition.Low, argumentsChunk);

                Debug.Assert(ThreadManForUser != null);

                // @TODO: Use Module Manager

                //var MainThread = ThreadManager.Create();
                //var CpuThreadState = MainThread.CpuThreadState;
                var currentCpuThreadState = new CpuThreadState(CpuProcessor);
                {
                    if (hleModuleGuest == null)
                    {
                        //throw new InvalidOperationException("hleModuleGuest == null");
                    }
                    //CpuThreadState.PC = Loader.InitInfo.PC;
                    currentCpuThreadState.Gp           = hleModuleGuest.InitInfo.Gp;
                    currentCpuThreadState.CallerModule = hleModuleGuest;

                    var threadId = (int)ThreadManForUser.sceKernelCreateThread(currentCpuThreadState, "<EntryPoint>",
                                                                               hleModuleGuest.InitInfo.Pc, 10, 0x1000, PspThreadAttributes.ClearStack, null);

                    //var Thread = HleThreadManager.GetThreadById(ThreadId);
                    ThreadManForUser._sceKernelStartThread(currentCpuThreadState, threadId, argumentsPartition.Size,
                                                           argumentsPartition.Low);
                    //Console.WriteLine("RA: 0x{0:X}", CurrentCpuThreadState.RA);
                }
                currentCpuThreadState.DumpRegisters();
                MemoryManager.GetPartition(MemoryPartitions.User).Dump();
                //ModuleManager.LoadedGuestModules.Add(HleModuleGuest);

                //MainThread.CurrentStatus = HleThread.Status.Ready;
            }
        }