예제 #1
0
        protected internal virtual int writeStringBytes(sbyte[] bytes, int addr, int maxSize, int trailingNulls)
        {
            int           bytesWritten = 0;
            IMemoryWriter memoryWriter = MemoryWriter.getMemoryWriter(addr, 1);

            if (bytes != null)
            {
                int Length = System.Math.Min(bytes.Length, maxSize - 1);
                for (int i = 0; i < Length; i++)
                {
                    memoryWriter.writeNext(bytes[i] & 0xFF);
                }
                bytesWritten += Length;
            }

            // write trailing '\0'
            for (int i = 0; i < trailingNulls; i++)
            {
                memoryWriter.writeNext(0);
            }

            memoryWriter.flush();

            return(bytesWritten);
        }
예제 #2
0
        /// <summary>
        /// Write a string in UTF16, including a trailing '\0\0' </summary>
        /// <param name="addr"> address where to write the string </param>
        /// <param name="s"> the string to write </param>
        /// <returns> the number of bytes written (not including the trailing '\0\0') </returns>
        protected internal virtual int writeStringUTF16Z(int addr, string s)
        {
            if (addr == 0 || string.ReferenceEquals(s, null))
            {
                return(0);
            }

            sbyte[] bytes = s.GetBytes(charset16);
            if (bytes == null)
            {
                return(0);
            }

            IMemoryWriter memoryWriter = MemoryWriter.getMemoryWriter(addr, bytes.Length + 2, 1);

            for (int i = 0; i < bytes.Length; i++)
            {
                memoryWriter.writeNext(bytes[i] & 0xFF);
            }

            // Write trailing '\0\0'
            memoryWriter.writeNext(0);
            memoryWriter.writeNext(0);
            memoryWriter.flush();

            return(bytes.Length);
        }
예제 #3
0
        private bool read(sbyte[] data)
        {
            if (data == null || data.Length == 0)
            {
                return(false);
            }

            inputLength = data.Length;
            IMemoryWriter memoryWriter = MemoryWriter.getMemoryWriter(inputAddr, inputLength, 1);

            for (int i = 0; i < data.Length; i++)
            {
                memoryWriter.writeNext(data[i] & 0xFF);
            }
            memoryWriter.flush();

            sceAtrac3plus.AtracFileInfo atracFileInfo = new sceAtrac3plus.AtracFileInfo();
            int codecType = sceAtrac3plus.analyzeRiffFile(mem, inputAddr, inputLength, atracFileInfo);

            if (codecType < 0)
            {
                return(false);
            }

            bool result = read(codecType, atracFileInfo);

            return(result);
        }
예제 #4
0
        public static void write(int addr, int Length, int[] buffer, int offset)
        {
            Length = System.Math.Min(Length, buffer.Length - offset);
            if (log.TraceEnabled)
            {
                log.trace(string.Format("write addr=0x{0:X8}, Length=0x{1:X}", addr, Length));
            }

            // Optimize the most common case
            if (RuntimeContext.hasMemoryInt())
            {
                int   length4    = Length >> 2;
                int   addrOffset = addr >> 2;
                int[] memoryInt  = RuntimeContext.MemoryInt;
                for (int i = 0, j = offset; i < length4; i++)
                {
                    int value = buffer[j++] & 0xFF;
                    value += (buffer[j++] & 0xFF) << 8;
                    value += (buffer[j++] & 0xFF) << 16;
                    value += buffer[j++] << 24;
                    memoryInt[addrOffset++] = value;
                }
            }
            else
            {
                IMemoryWriter memoryWriter = MemoryWriter.getMemoryWriter(addr, Length, 1);
                for (int i = 0, j = offset; i < Length; i++)
                {
                    memoryWriter.writeNext(buffer[j++] & 0xFF);
                }
                memoryWriter.flush();
            }
        }
예제 #5
0
        public static void sortFloatArray8Reverse()
        {
            int addr = GprA0;
            int size = GprA1;

            if (size < 2)
            {
                return;
            }

            // Read the objects from memory
            Float8ObjectReverse[] objects      = new Float8ObjectReverse[size];
            IMemoryReader         memoryReader = MemoryReader.getMemoryReader(addr, size << 3, 4);

            for (int i = 0; i < size; i++)
            {
                objects[i] = new Float8ObjectReverse(memoryReader);
            }

            // Sort the objects
            Array.Sort(objects);

            // Write back the objects to memory
            IMemoryWriter memoryWriter = MemoryWriter.getMemoryWriter(addr, size << 3, 4);

            for (int i = 0; i < size; i++)
            {
                objects[i].write(memoryWriter);
            }
            memoryWriter.flush();
        }
        static public Boolean FromInt32(IMemoryWriter tMemory, UInt32 tAddress, Int32 tData)
        {
            if (null == tMemory)
            {
                return(false);
            }

            return(tMemory.Write(tAddress, BitConverter.GetBytes(tData)));
        }
예제 #7
0
        public virtual int sceUriUnescape(TPointer unescapedAddr, TPointer32 unescapedLengthAddr, int unescapedBufferLength, TPointer source)
        {
            IMemoryReader memoryReader = MemoryReader.getMemoryReader(source.Address, 1);
            IMemoryWriter memoryWriter = null;

            if (unescapedAddr.NotNull)
            {
                memoryWriter = MemoryWriter.getMemoryWriter(unescapedAddr.Address, unescapedBufferLength, 1);
            }
            int unescapedLength = 0;

            while (true)
            {
                int c = memoryReader.readNext();
                if (c == 0)
                {
                    if (unescapedAddr.NotNull)
                    {
                        if (unescapedLength < unescapedBufferLength)
                        {
                            memoryWriter.writeNext(c);
                        }
                    }
                    unescapedLength++;
                    break;
                }
                if (unescapedAddr.NotNull)
                {
                    if (unescapedLength + 1 > unescapedBufferLength)
                    {
                        break;
                    }
                    if (c == '%')
                    {
                        int hex1 = memoryReader.readNext();
                        int hex2 = memoryReader.readNext();
                        c = (getHexValue(hex1) << 4) + getHexValue(hex2);
                    }
                    // Remark: '+' sign is not unescaped to ' ' by this function
                    memoryWriter.writeNext(c);
                }
                unescapedLength++;
            }
            if (memoryWriter != null)
            {
                memoryWriter.flush();
            }
            unescapedLengthAddr.setValue(unescapedLength);

            return(0);
        }
예제 #8
0
        public override void copyToMemory(int address, ByteBuffer source, int Length)
        {
            // copy in 1 byte steps until address is "int"-aligned
            while (!isIntAligned(address) && Length > 0 && source.hasRemaining())
            {
                sbyte b = source.get();
                write8(address, b);
                address++;
                Length--;
            }

            // copy 1 int at each loop
            int           countInt     = System.Math.Min(Length, source.remaining()) >> 2;
            IMemoryWriter memoryWriter = MemoryWriter.getMemoryWriter(address, countInt << 2, 4);

            for (int i = 0; i < countInt; i++)
            {
                int data1 = source.get() & 0xFF;
                int data2 = source.get() & 0xFF;
                int data3 = source.get() & 0xFF;
                int data4 = source.get() & 0xFF;
                int data  = (data4 << 24) | (data3 << 16) | (data2 << 8) | data1;
                memoryWriter.writeNext(data);
            }
            memoryWriter.flush();
            int copyLength = countInt << 2;

            Length  -= copyLength;
            address += copyLength;

            // copy rest Length in 1 byte steps (rest Length <= 3)
            while (Length > 0 && source.hasRemaining())
            {
                sbyte b = source.get();
                write8(address, b);
                address++;
                Length--;
            }
        }
예제 #9
0
        private int hleMpegBaseCscAvcRange(TPointer bufferRGB, int unknown, int bufferWidth, SceMp4AvcCscStruct mp4AvcCscStruct, int rangeX, int rangeY, int rangeWidth, int rangeHeight)
        {
            int width  = mp4AvcCscStruct.width << 4;
            int height = mp4AvcCscStruct.height << 4;

            // It seems that the pixel output format is always ABGR8888.
            int videoPixelMode = TPSM_PIXEL_STORAGE_MODE_32BIT_ABGR8888;
            int bytesPerPixel  = sceDisplay.getPixelFormatBytes(videoPixelMode);
            int destAddr       = bufferRGB.Address;

            int width2  = width >> 1;
            int height2 = height >> 1;
            int Length  = width * height;
            int length2 = width2 * height2;

            // Read the YCbCr image.
            // See the description of the format used by the PSP in sceVideocodecDecode().
            int[] luma      = getIntBuffer(Length);
            int[] cb        = getIntBuffer(length2);
            int[] cr        = getIntBuffer(length2);
            int   sizeY1    = ((width + 16) >> 5) * (height >> 1) * 16;
            int   sizeY2    = (width >> 5) * (height >> 1) * 16;
            int   sizeCrCb1 = sizeY1 >> 1;
            int   sizeCrCb2 = sizeY1 >> 1;

            int[] bufferY1    = getIntBuffer(sizeY1);
            int[] bufferY2    = getIntBuffer(sizeY2);
            int[] bufferCrCb1 = getIntBuffer(sizeCrCb1);
            int[] bufferCrCb2 = getIntBuffer(sizeCrCb2);

            read(mp4AvcCscStruct.buffer0, sizeY1, bufferY1, 0);
            read(mp4AvcCscStruct.buffer1, sizeY2, bufferY2, 0);
            read(mp4AvcCscStruct.buffer4, sizeCrCb1, bufferCrCb1, 0);
            read(mp4AvcCscStruct.buffer5, sizeCrCb2, bufferCrCb2, 0);
            for (int x = 0, j = 0; x < width; x += 32)
            {
                for (int y = 0, i = x; y < height; y += 2, i += 2 * width, j += 16)
                {
                    Array.Copy(bufferY1, j, luma, i, 16);
                }
            }
            for (int x = 16, j = 0; x < width; x += 32)
            {
                for (int y = 0, i = x; y < height; y += 2, i += 2 * width, j += 16)
                {
                    Array.Copy(bufferY2, j, luma, i, 16);
                }
            }
            for (int x = 0, j = 0; x < width2; x += 16)
            {
                for (int y = 0; y < height2; y += 2)
                {
                    for (int xx = 0, i = y * width2 + x; xx < 8; xx++, i++)
                    {
                        cb[i] = bufferCrCb1[j++];
                        cr[i] = bufferCrCb1[j++];
                    }
                }
            }
            for (int x = 0, j = 0; x < width2; x += 16)
            {
                for (int y = 1; y < height2; y += 2)
                {
                    for (int xx = 0, i = y * width2 + x; xx < 8; xx++, i++)
                    {
                        cb[i] = bufferCrCb2[j++];
                        cr[i] = bufferCrCb2[j++];
                    }
                }
            }

            read(mp4AvcCscStruct.buffer2, sizeY1, bufferY1, 0);
            read(mp4AvcCscStruct.buffer3, sizeY2, bufferY2, 0);
            read(mp4AvcCscStruct.buffer6, sizeCrCb1, bufferCrCb1, 0);
            read(mp4AvcCscStruct.buffer7, sizeCrCb2, bufferCrCb2, 0);
            for (int x = 0, j = 0; x < width; x += 32)
            {
                for (int y = 1, i = x + width; y < height; y += 2, i += 2 * width, j += 16)
                {
                    Array.Copy(bufferY1, j, luma, i, 16);
                }
            }
            for (int x = 16, j = 0; x < width; x += 32)
            {
                for (int y = 1, i = x + width; y < height; y += 2, i += 2 * width, j += 16)
                {
                    Array.Copy(bufferY2, j, luma, i, 16);
                }
            }
            for (int x = 8, j = 0; x < width2; x += 16)
            {
                for (int y = 0; y < height2; y += 2)
                {
                    for (int xx = 0, i = y * width2 + x; xx < 8; xx++, i++)
                    {
                        cb[i] = bufferCrCb1[j++];
                        cr[i] = bufferCrCb1[j++];
                    }
                }
            }
            for (int x = 8, j = 0; x < width2; x += 16)
            {
                for (int y = 1; y < height2; y += 2)
                {
                    for (int xx = 0, i = y * width2 + x; xx < 8; xx++, i++)
                    {
                        cb[i] = bufferCrCb2[j++];
                        cr[i] = bufferCrCb2[j++];
                    }
                }
            }

            releaseIntBuffer(bufferY1);
            releaseIntBuffer(bufferY2);
            releaseIntBuffer(bufferCrCb1);
            releaseIntBuffer(bufferCrCb2);

            // Convert YCbCr to ABGR
            int[] abgr = getIntBuffer(Length);
            H264Utils.YUV2ABGR(width, height, luma, cb, cr, abgr);

            releaseIntBuffer(luma);
            releaseIntBuffer(cb);
            releaseIntBuffer(cr);

            // Do not cache the video image as a texture in the VideoEngine to allow fluid rendering
            VideoEngine.Instance.addVideoTexture(destAddr, destAddr + (rangeY + rangeHeight) * bufferWidth * bytesPerPixel);

            // Write the ABGR image
            if (videoPixelMode == TPSM_PIXEL_STORAGE_MODE_32BIT_ABGR8888 && RuntimeContext.hasMemoryInt())
            {
                // Optimize the most common case
                int pixelIndex = rangeY * width + rangeX;
                int addr       = destAddr;
                for (int i = 0; i < rangeHeight; i++)
                {
                    Array.Copy(abgr, pixelIndex, RuntimeContext.MemoryInt, addr >> 2, rangeWidth);
                    pixelIndex += width;
                    addr       += bufferWidth * bytesPerPixel;
                }
            }
            else
            {
                int addr = destAddr;
                for (int i = 0; i < rangeHeight; i++)
                {
                    IMemoryWriter memoryWriter = MemoryWriter.getMemoryWriter(addr, rangeWidth * bytesPerPixel, bytesPerPixel);
                    int           pixelIndex   = (i + rangeY) * width + rangeX;
                    for (int j = 0; j < rangeWidth; j++, pixelIndex++)
                    {
                        int abgr8888   = abgr[pixelIndex];
                        int pixelColor = Debug.getPixelColor(abgr8888, videoPixelMode);
                        memoryWriter.writeNext(pixelColor);
                    }
                    memoryWriter.flush();
                    addr += bufferWidth * bytesPerPixel;
                }
            }
            releaseIntBuffer(abgr);

            return(0);
        }
예제 #10
0
        public virtual int sceMpegBaseCscVme(TPointer bufferRGB, TPointer bufferRGB2, int bufferWidth, TPointer32 bufferYCrCb)
        {
            SceMpegYCrCbBuffer sceMpegYCrCbBuffer = new SceMpegYCrCbBuffer();

            sceMpegYCrCbBuffer.read(bufferYCrCb);

            int width  = sceMpegYCrCbBuffer.frameBufferWidth16 << 4;
            int height = sceMpegYCrCbBuffer.frameBufferHeight16 << 4;

            int videoPixelMode = TPSM_PIXEL_STORAGE_MODE_32BIT_ABGR8888;
            int bytesPerPixel  = sceDisplay.getPixelFormatBytes(videoPixelMode);
            int rangeX         = 0;
            int rangeY         = 0;
            int rangeWidth     = width;
            int rangeHeight    = height;
            int destAddr       = bufferRGB.Address;

            //if (log.DebugEnabled)
            {
                Console.WriteLine(string.Format("sceMpegBaseCscVme sceMpegYCrCbBuffer: {0}", sceMpegYCrCbBuffer));
            }

            int width2  = width >> 1;
            int height2 = height >> 1;
            int Length  = width * height;
            int length2 = width2 * height2;

            // Read the YCbCr image
            int[] luma = getIntBuffer(Length);
            int[] cb   = getIntBuffer(length2);
            int[] cr   = getIntBuffer(length2);
            read(sceMpegYCrCbBuffer.bufferY, Length, luma, 0);
            read(sceMpegYCrCbBuffer.bufferCb, length2, cb, 0);
            read(sceMpegYCrCbBuffer.bufferCr, length2, cr, 0);

            // Convert YCbCr to ABGR
            int[] abgr = getIntBuffer(Length);
            H264Utils.YUV2ABGR(width, height, luma, cb, cr, abgr);

            releaseIntBuffer(luma);
            releaseIntBuffer(cb);
            releaseIntBuffer(cr);

            // Do not cache the video image as a texture in the VideoEngine to allow fluid rendering
            VideoEngine.Instance.addVideoTexture(destAddr, destAddr + (rangeY + rangeHeight) * bufferWidth * bytesPerPixel);

            // Write the ABGR image
            if (videoPixelMode == TPSM_PIXEL_STORAGE_MODE_32BIT_ABGR8888 && RuntimeContext.hasMemoryInt())
            {
                // Optimize the most common case
                int pixelIndex = rangeY * width + rangeX;
                for (int i = 0; i < rangeHeight; i++)
                {
                    int addr = destAddr + (i * bufferWidth) * bytesPerPixel;
                    Array.Copy(abgr, pixelIndex, RuntimeContext.MemoryInt, addr >> 2, rangeWidth);
                    pixelIndex += width;
                }
            }
            else
            {
                int addr = destAddr;
                for (int i = 0; i < rangeHeight; i++)
                {
                    IMemoryWriter memoryWriter = MemoryWriter.getMemoryWriter(addr, rangeWidth * bytesPerPixel, bytesPerPixel);
                    int           pixelIndex   = (i + rangeY) * width + rangeX;
                    for (int j = 0; j < rangeWidth; j++, pixelIndex++)
                    {
                        int abgr8888   = abgr[pixelIndex];
                        int pixelColor = Debug.getPixelColor(abgr8888, videoPixelMode);
                        memoryWriter.writeNext(pixelColor);
                    }
                    memoryWriter.flush();
                    addr += bufferWidth * bytesPerPixel;
                }
            }
            releaseIntBuffer(abgr);

            return(0);
        }
예제 #11
0
        protected internal override void memcpy(int destination, int source, int Length, bool checkOverlap)
        {
            if (Length <= 0)
            {
                return;
            }

            destination = normalizeAddress(destination);
            source      = normalizeAddress(source);

            Modules.sceDisplayModule.write(destination);

            if (isIntAligned(source) && isIntAligned(destination) && isIntAligned(Length))
            {
                // Source, destination and Length are "int"-aligned
                memcpyAligned4(destination, source, Length, checkOverlap);
            }
            else if ((source & 0x03) == (destination & 0x03) && (!checkOverlap || !areOverlapping(destination, source, Length)))
            {
                // Source and destination have the same alignment and are not overlapping
                while (!isIntAligned(source) && Length > 0)
                {
                    write8(destination, (sbyte)read8(source));
                    source++;
                    destination++;
                    Length--;
                }

                int length4 = Length & ~0x03;
                if (length4 > 0)
                {
                    memcpyAligned4(destination, source, length4, checkOverlap);
                    source      += length4;
                    destination += length4;
                    Length      -= length4;
                }

                while (Length > 0)
                {
                    write8(destination, (sbyte)read8(source));
                    destination++;
                    source++;
                    Length--;
                }
            }
            else
            {
                //
                // Buffers are not "int"-aligned, copy in 1 byte steps.
                // Overlapping address ranges must be correctly handled:
                //   If source >= destination:
                //                 [---source---]
                //       [---destination---]
                //      => Copy from the head
                //   If source < destination:
                //       [---source---]
                //                 [---destination---]
                //      => Copy from the tail
                //
                if (!checkOverlap || source >= destination || !areOverlapping(destination, source, Length))
                {
                    if (areOverlapping(destination, source, 4))
                    {
                        // Cannot use MemoryReader if source and destination are overlapping in less than 4 bytes
                        for (int i = 0; i < Length; i++)
                        {
                            write8(destination + i, (sbyte)read8(source + i));
                        }
                    }
                    else
                    {
                        IMemoryReader sourceReader      = MemoryReader.getMemoryReader(source, Length, 1);
                        IMemoryWriter destinationWriter = MemoryWriter.getMemoryWriter(destination, Length, 1);
                        for (int i = 0; i < Length; i++)
                        {
                            destinationWriter.writeNext(sourceReader.readNext());
                        }
                        destinationWriter.flush();
                    }
                }
                else
                {
                    for (int i = Length - 1; i >= 0; i--)
                    {
                        write8(destination + i, (sbyte)read8(source + i));
                    }
                }
            }
        }
예제 #12
0
        public virtual int sceNpAuthGetTicket(int id, TPointer buffer, int Length)
        {
            int result;

            if (useDummyTicket)
            {
                SceNpTicket ticket = new SceNpTicket();
                ticket.version = 0x00000121;
                ticket.size    = 0xF0;
                addTicketParam(ticket, "XXXXXXXXXXXXXXXXXXXX", 20);
                addTicketParam(ticket, 0);
                long now = DateTimeHelper.CurrentUnixTimeMillis();
                addTicketDateParam(ticket, now);
                addTicketDateParam(ticket, now + 10 * 60 * 1000);                 // now + 10 minutes
                addTicketLongParam(ticket, 0L);
                addTicketParam(ticket, SceNpTicket.TicketParam.PARAM_TYPE_STRING, "DummyOnlineID", 32);
                addTicketParam(ticket, "gb", 4);
                addTicketParam(ticket, SceNpTicket.TicketParam.PARAM_TYPE_STRING, "XX", 4);
                addTicketParam(ticket, serviceId, 24);
                int status = 0;
                if (Modules.sceNpModule.parentalControl == sceNp.PARENTAL_CONTROL_ENABLED)
                {
                    status |= STATUS_ACCOUNT_PARENTAL_CONTROL_ENABLED;
                }
                status |= (Modules.sceNpModule.UserAge & 0x7F) << 24;
                addTicketParam(ticket, status);
                addTicketParam(ticket);
                addTicketParam(ticket);
                ticket.unknownBytes = new sbyte[72];
                //if (log.DebugEnabled)
                {
                    Console.WriteLine(string.Format("sceNpAuthGetTicket returning dummy ticket: {0}", ticket));
                }
                ticket.write(buffer);
                result = ticket.@sizeof();
            }
            else if (ticketBytesLength > 0)
            {
                result = System.Math.Min(ticketBytesLength, Length);
                IMemoryWriter memoryWriter = MemoryWriter.getMemoryWriter(buffer.Address, result, 1);
                for (int i = 0; i < result; i++)
                {
                    memoryWriter.writeNext(ticketBytes[i] & 0xFF);
                }

                //if (log.DebugEnabled)
                {
                    Console.WriteLine(string.Format("sceNpAuthGetTicket returning real ticket: {0}", Utilities.getMemoryDump(buffer.Address, result)));
                }
            }
            else
            {
                buffer.clear(Length);

                result = Length;

                //if (log.DebugEnabled)
                {
                    Console.WriteLine(string.Format("sceNpAuthGetTicket returning empty ticket"));
                }
            }

            return(result);
        }
 public ManhattanDistance(IMemoryWriter memoryWriter)
 {
     _memoryWriter = memoryWriter;
 }
예제 #14
0
 public MemoryReaderWriterGeneric(int address, int step)
 {
     memoryReader = MemoryReader.getMemoryReader(address, step);
     memoryWriter = MemoryWriter.getMemoryWriter(address, step);
     currentValue = memoryReader.readNext();
 }
예제 #15
0
 public virtual void write(IMemoryWriter memoryWriter)
 {
     memoryWriter.writeNext(n1);
     memoryWriter.writeNext(n2);
 }
예제 #16
0
        public virtual int sceUriEscape(TPointer escapedAddr, TPointer32 escapedLengthAddr, int escapedBufferLength, TPointer source)
        {
            IMemoryReader memoryReader = MemoryReader.getMemoryReader(source.Address, 1);
            IMemoryWriter memoryWriter = null;

            if (escapedAddr.NotNull)
            {
                memoryWriter = MemoryWriter.getMemoryWriter(escapedAddr.Address, escapedBufferLength, 1);
            }
            int escapedLength = 0;

            while (true)
            {
                int c = memoryReader.readNext();
                if (c == 0)
                {
                    if (escapedAddr.NotNull)
                    {
                        if (escapedLength < escapedBufferLength)
                        {
                            memoryWriter.writeNext(c);
                        }
                    }
                    escapedLength++;
                    break;
                }
                if (escapeCharTable[c])
                {
                    if (escapedAddr.NotNull)
                    {
                        if (escapedLength + 3 > escapedBufferLength)
                        {
                            break;
                        }
                        memoryWriter.writeNext('%');
                        memoryWriter.writeNext(hexTable[c >> 4]);
                        memoryWriter.writeNext(hexTable[c & 0x0F]);
                    }
                    escapedLength += 3;
                }
                else
                {
                    if (escapedAddr.NotNull)
                    {
                        if (escapedLength + 1 > escapedBufferLength)
                        {
                            break;
                        }
                        memoryWriter.writeNext(c);
                    }
                    escapedLength++;
                }
            }
            if (memoryWriter != null)
            {
                memoryWriter.flush();
            }
            escapedLengthAddr.setValue(escapedLength);

            return(0);
        }