+  RHDN Forum Archive
|-+  Romhacking
| |-+  ROM Hacking Discussion
| | |-+  [psx]Use DMA to bypass decompressing routines? Need help.
Pages: [1]
Author Topic: [psx]Use DMA to bypass decompressing routines? Need help.  (Read 1 times)
Magil
Guest
« on: January 13, 2011, 08:39:38 am »

I took a  look at Culdcept's decompressing function and found it was too troublesome for me to write a recompressing tool (even if I can, perhaps I need to try hard to keep the recompressed data  smaller than original data). So I had an idea in mind that maybe I could let the game read the uncompressed data from CD instead (so I can append the data behind the CD, for example).

But I don't know how to do that.


The decompressing function is like this:

Code:
decompress(input_address, output_address, output_length)

Input address is stored in a1, refers to a memory address where the unheadered compressed data begins.
Output address is stored in a2, refers to a memory address where the decompressed data will be stored.
Output length is stored in a3, should be the length of the uncompressed data.

And here's my idea:

Store the sector/LBA info at the beginning of the hacked compressed data and flag it with a special byte. Let the hacked function read one byte, if it is zero (for example), read ahead to fetch the sector info and read from CD.

I've read some document but haven't figured out how to do actual CD reading. All I know is the memory address is stored at 1f8010b0, hmm...
Gemini
Guest
« Reply #1 on: January 13, 2011, 09:12:43 am »

Why don't you just append the compressed & expanded data instead? Bypassing a compression is not only a bad habit, but it also has severe consequences on loadings (i.e. the game will take almost twice the time to load anything not compressed).
Magil
Guest
« Reply #2 on: January 13, 2011, 09:38:17 am »

Hmm, I don't understand. What do you mean by compressed & expanded data?

The compressed data is not that long at all (some text, for example).
So I mentioned a flag byte, so the game only re-read those with the special flag but run normal decompressing routines if the flag is not present.
 Tongue And it is nothing bad to just learn how to read data via DMA, just in case I might need it in the long run.
Gemini
Guest
« Reply #3 on: January 13, 2011, 07:24:41 pm »

You don't need to know DMA registers or how to use them anyway, since all the CD functions are stored in the exe as included libraries. Check the official SDK instead (it floats around the net, very easy to find nowadays).

Quote
Hmm, I don't understand. What do you mean by compressed & expanded data?
Compressed data that grew bigger in size and that wouldn't fit in the original location. Example: a new script or a non-optimized compressed chunk.
Magil
Guest
« Reply #4 on: January 13, 2011, 07:32:22 pm »

Quote
Check the official SDK instead (it floats around the net, very easy to find nowadays).
Hmm, I will try.
Thank you.



Yeah, finally found the cd reading function after 10+ hours of tracing and testing.
The function itself is neat. It has 4 parameters, a0: the sector number of the package file, a1: buffer address, a2: offset in the package file, a3: length

« Last Edit: January 14, 2011, 01:48:29 pm by Magil »
Gemini
Guest
« Reply #5 on: January 15, 2011, 10:00:31 pm »

Why tracing? Not only that's not the correct way to hack consoles such as the PlayStation, but there are also way better alternatives, like IDA's flirt signatures containing pretty much all the Psy-Q functions or this special version of PCSX with a debugger that can monitor CD access. Seriously, nobody should use tracing anymore for this kind of stuff.
Magil
Guest
« Reply #6 on: January 15, 2011, 10:33:05 pm »

Yeah, I'm kinda slow to get things done. Undecided\

I used IDA (though I was new to it). But the game also created its own CD reading function which used DsGetSector and its own sector seeking function. So though I found the function that used DsGetSector pretty soon, I had to find its callers and figure out the parameters.

Hehe, guess I will be more skillful next time.
Gemini
Guest
« Reply #7 on: January 16, 2011, 09:27:26 am »

You can still do that with all the values stored in ra and Program Counter breakpoints. :p Backtracing is usually very easy on the PlayStation.
Magil
Guest
« Reply #8 on: January 17, 2011, 07:18:48 pm »

Quote
You can still do that with all the values stored in ra and Program Counter breakpoints.
Yeah, I figured that out though it was too late.  Tongue

BTW, actually, direct CD-reading was faster than decompressing the data on an emulator. The data block was 85kb in length after processed and it took a lot of time to be decompressed. That is not really an excuse though, I'm still researching the decompression routines hoping I can eventually understand it.
Gemini
Guest
« Reply #9 on: January 18, 2011, 02:08:29 am »

If the decompression is really that slow:
1) The decompression algorithm is so horrid it acts slower than it should;
2) The compression codec doesn't really help the stream to get smaller enough to quicken the loading+decompression process.

Still, usually compression reduces CD reading by A LOT. I've implemented a simple LZSS on Soul Hackers and now it takes half the time to load, so there's something not sitting right in your case. :S
Magil
Guest
« Reply #10 on: January 18, 2011, 02:33:39 am »

Hmm, I converted the asm code into ugly c code, so now I can decompress things without copying them out of RAM dump.

The decompressing process is strange.

1. calculate total decompressed buffer length

2. read from the compressed buffer, set up large look-up table(s).

3. read from the compressed buffer, jump here and there in the look-up table(s) to fetch something, write the results to the decompressed  buffer.
  In step 3, it sometimes copies bytes already decoded, so I wonder whether it is some kind of lz compression.

4. check the length from step 1, if it is not done, then repeat step 2 and 3.
Magil
Guest
« Reply #11 on: January 28, 2011, 02:36:58 am »

Hmm, I took a look at those help text in game just to find they have to be decoded using mdec after being decompressed.
http://img232.imageshack.us/img232/6369/testmq.png

So is there any tool that can decode/encode individual such pictures?
Pages: [1]  


Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC