News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Inserting custom code into PSX rom  (Read 2319 times)

Grafeisenn

  • Newbie
  • *
  • Posts: 3
    • View Profile
Inserting custom code into PSX rom
« on: April 21, 2020, 06:25:29 pm »
Hello,
New to rom hacking but been trying to tackle the translation of "Little Princess: Marl Ōkoku no Ningyō Hime 2",  since the translated dialogues already exist. I'm aware someone from this forum was doing it some time ago but I guess the project was abandoned.

So, working on it I found where the dialogue is read from the file, it reads 2 bytes, and looks for them on the Kernel.pak file, and saves the offset to display the characters later, and then goes to the next 2 bytes. Using what I guess is shift-jis.
I decided to make it read 1 byte at a time, to get more space, and write a function to translate the ascii byte into what the Kernel.pak is using as reference(since it already has latin characters in there), it worked well for the uppercase, lowercase letters and numbers, since they have the same order as in ascii.
The problem is that punctuation marks are unordered, so I had to write what I think is a crude "if-else" using armips.
Here is the script I'm currently trying to use:
Code: [Select]

; Little princess of Marl Kingdom 2 - Dialogue fix 2bytes to 1 byte
.psx ; Set the architecture to PSX
.open "FIELD.OVL",0x001C0000 ; Open file for output

; 256e in file 6e25 in memory
 ; 1 means line feed
 ; 2 or 3 means continues on next window
 ; Apparently we can't use 0a
 ; we'll use carriage return 0xd
 
.org 0x001d48e0 ; Some new code
shift_jis_fix:
lbu r3,0x0(r16)
addiu r5,r0,0x61         ; lowercase limit
bge r3,r5, lower_case
addiu r5,r0,0xd          ; New line / new window
beq r3,r5, new_line
addiu r5,r0,0x21
;beq r3,r5, exclamation   ; exclamation mark
nop
sll r3,r3,0x8
addiu r3,r3,0x1f82
j return

.org 0x001d48d4
return:
addiu r16,r16,0x1
j 0x001c4ef0

.org 0x001d4910
lower_case:
sll r3,r3,0x8
addiu r3,r3,0x2082
j return

.org 0x001d4920
new_line:
sll r3,r3,0x8
addiu r3,r3,0x6125
j return

.org 0x001d4930
exclamation:
sll r3,r3,0x8
addiu r3,r3,0x2881
j return

.org 0x001c4ed8 ; here, a branch to the new code
addiu r18,r18,0xffff
j shift_jis_fix

.close

Notice that the ";beq r3,r5, exclamation" is commented, this is how it looks without that line:


But if I uncomment the line:


I know it isn't actually a problem with the jump to exclamation nor that code section since it works with the exclamation marks if I remove the branch to new_line:


I've been thinking it could be that something is overwriting certain part of the code, but checking that block of memory with pSX's debugger just shows that it is always 00.
Also, I've tried to debug the patched game but no debbuger is working.
No$psx, PCSX 1.5 with Debugger, PCSX Agemo, PCSXTrace, they all freeze at the start of the rom ("NIPPON ICHI SOFTWARE presents"), only pSX has worked but it only works with the unmodified rom (been using psxImager to rebuild the rom). The modified rom is only running with ePSXe.

Any tips on how to continue would be appreciated, and sorry for the lengthy post, I hope I explained the problem well.
Also, I'm sorry if I didn't post on the correct section.

mz

  • Sr. Member
  • ****
  • Posts: 447
  • Whore
    • View Profile
Re: Inserting custom code into PSX rom
« Reply #1 on: April 21, 2020, 10:02:09 pm »
What we usually do instead of adding lots of ifs and elses is to use table files and/or modify the font graphics.

I think Gemini was the one working on this game, so maybe he can give you what he has. He has given his older projects to other people before...
There has to be a better life.

Grafeisenn

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Inserting custom code into PSX rom
« Reply #2 on: April 21, 2020, 10:58:43 pm »
What we usually do instead of adding lots of ifs and elses is to use table files and/or modify the font graphics.

Well I'll be damned, I couldn't find the font using Tile Molester at first, so I tried just fixing it by code.
Now I open the Kernel file again and see the font right there, I guess this will make it much more easy since I can just reorder the characters.
I'll see how far I can get with it.

Risae

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Inserting custom code into PSX rom
« Reply #3 on: April 22, 2020, 04:06:22 am »
Hi!

not sure if this can help you, but this looked interesting so i took a look the game.
I never worked on PS1 games before so i used "jpsxdec_v1-03_rev3953" to dump the contents of the games .BIN file.
I looked at a video on youtube to see some japanese text and find the script files.
I searched for "な、なにこれ?" and found it in the file "FIELD.PAK":

Spoiler:
Spoiler:
Spoiler:

Viewing the file through a HEX viewer you can see that the game uses so called Control Codes to control the text in the text bubble:

256E: new line?
256E256E: new window?

Some things that i found out:

- SHIFT-JIS is used as encoding for the script files.
- I believe there is a pointer table above the script text:

Spoiler:

But i might be wrong about this because in between the different text lines there is also some kind of control code.

Spoiler:

- I think (not sure) that inside the .PAK files are stored more smaller files, because above the possible pointer table is a lot of empty space:

Spoiler:

Using the Programs called "Atlas" and "Cartographer" you could dump the text with the associated pointers, not worrying about going over the original text length.

I believe with the tool called "quickBMS" you could extract the extra files inside the .PAK files.
Maybe with quickBMS you could even write a script that dumps and re-inserts the .BIN file?
For example, in my translation project i am using quickBMS to modify the games .DAT files.
I can dump and re-insert the files inside the .DAT and then put it back onto the ISO.
« Last Edit: April 22, 2020, 04:12:16 am by Risae »

Grafeisenn

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Inserting custom code into PSX rom
« Reply #4 on: April 22, 2020, 12:43:36 pm »
- I believe there is a pointer table above the script text:
...
But i might be wrong about this because in between the different text lines there is also some kind of control code.


Nice, I was actually missing some keywords to explain certain things, I tried doing an analysis of the text messages structure before even getting into the code (just by trial and error), only could decipher some values, this is an example of the "second message" after starting the game:

Code: [Select]
# text message start?
70 00    <- total size of message structure + 1  0x1B346 to 0x1B3B5 (0x6F)
03 DC
00 03
33 00
01 03
04 00
03 22 <- Text message ID
00 05    <- ?
21 03    <- Character portrait ID
00 00 01 <- ? is only different on the first text message

50 00    <- Lenght of text (0x1B35B-0x1B3AA)
82 B3 82 C1 81 41 83 41 83 4E 81 5B 83 57 83 87
82 B3 82 DC 81 41 25 6E 83 8D 81 5B 83 5C 83 4E
82 CC 89 CE 82 F0 8F C1 82 B5 82 C4 81 42
25 6E 25 6E  <- control codes    (This message continues on next window)
82 DD 82 F1 82 C8 82 C5 8F FC 82 E8 95 74
82 AF 82 B5 82 BD 82 F1 82 BE 82 E6 81 42

( Now Akurjo-sama, blow out the candles!
---
Everyone helped decorate it.  )

03 23 00 03 22   <- Next message ID | 00 | Last message ID?
00 05
69 00 00 00

Quote
Using the Programs called "Atlas" and "Cartographer" you could dump the text with the associated pointers, not worrying about going over the original text length.

I'll see what I can do with those, I was actually able to show longer messages by editing the two message lenghts that I found, but it also made the background image go black.

Quote
- I think (not sure) that inside the .PAK files are stored more smaller files, because above the possible pointer table is a lot of empty space:

I believe with the tool called "quickBMS" you could extract the extra files inside the .PAK files.
Maybe with quickBMS you could even write a script that dumps and re-inserts the .BIN file?
For example, in my translation project i am using quickBMS to modify the games .DAT files.
I can dump and re-insert the files inside the .DAT and then put it back onto the ISO.

Thanks, I'll check this one too, I tried running PSicture over the folder and mostly found images on .BIN files, and inside NOTEBOOK.PAK, still not as much images as I would expect for a full game, just the main character tiles and one enemy.


OliVDB

  • Jr. Member
  • **
  • Posts: 7
    • View Profile
Re: Inserting custom code into PSX rom
« Reply #5 on: April 22, 2020, 01:22:19 pm »
I'll be following that thread closely, as I found this forum by looking for a translation of that game!
Plus, you're really explaining your processes thoroughly, that's priceless for us newbies.

Risae

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Inserting custom code into PSX rom
« Reply #6 on: April 23, 2020, 12:16:21 pm »
To give you a better understanding on what you can do with Atlas and Cartographer i can show you how i used it for my translation:


In Growlanser 5 and 6 for the PS2 the pointer table is located right above the text script.
The pointers always point to the first byte of the text start:

Spoiler:

Now, using the tool called "Cartographer" i am able to dump the text with the corresponding text pointers using the following commands:

Code: [Select]
#GAME NAME: Growlanser 6

#BLOCK NAME: Dialogue Block (POINTER_RELATIVE)
#TYPE: NORMAL
#METHOD: POINTER_RELATIVE
#POINTER ENDIAN: LITTLE
#POINTER TABLE START: $4E20
#POINTER TABLE STOP: $4EF8
#POINTER SIZE: $04
#POINTER SPACE: $00
#ATLAS PTRS: Yes
#BASE POINTER: $4F00
#TABLE: abcde.tbl
#COMMENTS: Yes
#END BLOCK

And of course with the control codes of the game, in a so called "Table" file:

Code: [Select]
20=
21=!
22="
23=#
24=$
25=%
26=&
27='
28=(
29=)
2A=*
2B=+
2C=,
2D=-
2E=.
2F=/
30=0
31=1
32=2
33=3
34=4
35=5
36=6
37=7
38=8
39=9
3A=:
3B=;
#3C=<
3D==
#3E=>
3F=?
40=@
41=A
42=B
43=C
44=D
45=E
46=F
47=G
48=H
49=I
4A=J
...
FFEAFEFF=[CC.EAFEFF]
FFEAFFFF=[CC.EAFFFF]
FFCD=[CC.FFCD]
FFFB=[CC.FFFB]
FFDF00=[COL00]
FFDF01=[COL01]
FFDF02=[COL02]
FFDF03=[COL03]
FFDF04=[COL04]
FFDF05=[COL05]
FFDF06=[COL06]
FFDF07=[COL07]
FFDF08=[COL08]
FFDF09=[COL09]
FFDF0A=[COL0A]
FFDF0B=[COL0B]
FFDF0C=[COL0C]
FFDF0D=[COL0D]
FFDF0E=[COL0E]
FFDF0F=[COL0F]
FFDF10=[COL10]
3C=[LESS-THAN]
3E=[GREATER-THAN]
84BD84BE=[{==}]
81B881B981BA=[---]
81BE=[!?]
81BF=[2-TEAR-DROP]
81CC=[{=}]
81CD=[HEART]
81CE=[TEAR-DROP]
81F4=[MUSIC-NOTE]
8488=[Lv]
#848D=[MONEY]
8480=[SWORD]
8481=[SHIELD]
8482=[GEM]
8452=[GOODS]
8484=[ITEMS]
8485=[PLATE]
8740=[(1)]
8741=[(2)]
8742=[(3)]
8743=[(4)]
8744=[(5)]
8745=[(6)]
8746=[(7)]
8747=[(8)]
8748=[(9)]
8749=[(19)]
874A=[(11)]
874B=[(12)]
874C=[(13)]
874D=[(14)]
874E=[(15)]
874F=[(16)]
8750=[(17)]
8751=[(18)]
8752=[(19)]
8753=[(20)]
8754=[ONE]
8755=[TWO]
8756=[THREE]
8757=[FOUR]
8758=[FIVE]
8759=[SIX]
875A=[SEVEN]
875B=[EIGTH]
875C=[NINE]
875D=[TEN]
84A5=[CIRCLE]
84A6=[CROSS]
84A7=[SQUARE]
84A8=[TRIANGLE]
84A184A2=[L1]
84A384A4=[L2]
84AC84AD=[R1]
84AE84AF=[R2]
84B084B184B2=[START]
84B584B684B7=[SELECT]
FFFC=[NLINE]\n
FFFD=[NWIN]\n\n
/FFFF00=[END-FF]\n\n\n\n\n
/FFFE00=[END-FE]\n\n\n\n\n


And i get the following dumped text:

Spoiler:
//POINTER #0 @ $4E20 - STRING #0 @ $4F00
#W32($4E20)
//[END-FF]
[END-FF]





//POINTER #1 @ $4E24 - STRING #1 @ $4F03
#W32($4E24)
//・敵の全滅[END-FE]
・敵の全滅[END-FE]





//POINTER #2 @ $4E28 - STRING #2 @ $4F10
#W32($4E28)
//・同行人の死亡[NLINE]
//・パーティーの全滅[END-FE]
・同行人の死亡[NLINE]
・パーティーの全滅[END-FE]





//POINTER #3 @ $4E2C - STRING #3 @ $4F35
#W32($4E2C)
//・同行人の死亡[NLINE]
//・パーティーの全滅[END-FE]
・同行人の死亡[NLINE]
・パーティーの全滅[END-FE]


Now, Atlas can use this dumped script and re-insert it into the game file and update the pointer table for each pointer according to its new position automaticly. All you have to do it put some Atlas code on top of the script file and it will do its magic:

Spoiler:
#VAR(dialogue, TABLE)
#ADDTBL("abcde.tbl", dialogue)
#ACTIVETBL(dialogue)

#VAR(PTR, CUSTOMPOINTER)
#CREATEPTR(PTR, "LINEAR", $4F00, 32)

#VAR(PTRTBL, POINTERTABLE)
#PTRTBL(PTRTBL, $4E20, 4, PTR)

#JMP($4F00)
#HDR($4F00)





//POINTER #0 @ $4E20 - STRING #0 @ $4F00
#W32($4E20)
//[END-FF]
[END-FF]





//POINTER #1 @ $4E24 - STRING #1 @ $4F03
#W32($4E24)
//・敵の全滅[END-FE]
・敵の全滅[END-FE]


Hope this can get you better understanding on how you can use those tools.
Cartographer and Atlas are highly customizable.
It was developed to support a huge amount of different kinds of pointer tables.
If you understand how your script file works you can configure Cartographer and Atlas in a way that it can dump the text like in my example.

Also, I recently switched to "abcde". This is an updated version of Cartographer and Atlas:

https://www.romhacking.net/forum/index.php?topic=25968.0

"abw", the developer of the new tool has a lot of knowledge in regards to script dumping and inserting.
I'm sure he can assist if you get stuck anywhere, or you don't understand something.

OliVDB

  • Jr. Member
  • **
  • Posts: 7
    • View Profile
Re: Inserting custom code into PSX rom
« Reply #7 on: April 24, 2020, 10:23:29 am »
Great, that's awesome, thanks for explaining even further!

I could deduct a little by reading your previous messages, but I hadn't figured that much, and would have spent some serious time experimenting through trial and error methods if not for your last post.