Continued hacking with a hex editor

Since I finally beat the game, I decided to look a little deeper at hex editing the saved games of Phantasy Star for the SEGA Master System. While I am generally not a big fan of cheat codes and game hacking, I just kept looking at this hex data and started seeing a pattern. And once I saw it, I couldn't "un-see" it. For editing the saved RAM file, you can use any hex editor, but in my case I used Ghex on Ubuntu Linux, which has a nice GUI. Previously, I had used hexedit from the command line, which worked rather well, but made things like copy/paste a little more tedious.

As I mentioned last time, because I had already read a guide on save state hacking[1] for this game, I sort of had a good idea of what to look for. However, that saved state hack was for the saved state of the game with the MekaW emulator, not the actual saved backup RAM from the game. E.g., it is the RAM in use for the game, with register addresses that map to the running game, and not to the saved RAM of the cartridge. I also read some other guides, but they were for the newer SEGA Genesis version and also didn't apply. However, like I mentioned last post, I found a tool written in Go [2] which did have this helpful line:

// A Phantasy Star 1 save file consists of five saved games.
// A header of size 0x200 exists at position 0x100 in the save file.
// Assuming saved game indexes ("GameIndex") numbered 0 through 4, each saved
// game can be found in the file at location `0x500 + 0x400 * gamenum`.
// In other words, saved games start at index 0x500 and are each of size 0x400.
// Each saved game consists of four packed 16-byte PlayerRecord structures,
// representing Alis, Myau, Odin, and Noah. At offset 0xC0 in each saved game,
// there is a 32-byte array of inventory items. At offset 0xE0 in the saved
// game, a two-byte value can be found representing the number of meseta
// followed by a one-byte value representing the number of inventory items.

As I mentioned last time: If you use a hex editor like hexedit or ghex (terminal and gui, respectively), you will find that the first game save starts at 0x500, goes for 400, then the second game save starts at 0x900, goes for 400, and so on. And, a saved game looks like this:

00000500: 01cc 1c1a 5518 cc1c 6ee0 0e16 2000 0502  ....U...n... ...
00000510: 01ca 31e9 4d18 ca31 6eb6 0a14 1d00 0403  ..1.M..1n.......
00000520: 01bb 00cc 4e19 bb00 5cbd 0d17 1f00 0000  ....N...\.......
00000530: 01a8 4b8e 4516 a84b 4ab9 0418 1e00 0505  ..K.E..KJ.......
00000540: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000550: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000560: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000570: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000580: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000590: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000005a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000005b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000005c0: 3334 352d 3832 2639 2122 3b0f 2330 313e  345-82&9!";.#01>
000005d0: 2525 2525 2525 2525 0000 0000 0000 0000  %%%%%%%%........
000005e0: 423f 1800 0000 0000 0000 0000 0000 0000  B?..............
000005f0: 0300 0000 0000 0000 0000 0000 0000 0000  ................
00000600: ff01 0102 0700 01ff 0101 ff00 0000 0000  ................
00000610: 00ff 0000 0000 0000 ff00 0000 0000 0000  ................
00000620: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000630: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000640: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000650: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000660: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000670: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000680: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000690: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000700: ffff 00ff ffff ff00 0000 0000 0000 0000  ................
00000710: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000720: 0000 0000 ff00 0000 0000 0000 0000 0000  ................
00000730: 00ff 0000 ff00 0000 0000 0000 0000 0000  ................
00000740: ff00 00ff 0000 0000 0000 00ff 0000 0000  ................
00000750: 0000 00ff 0000 ff00 0000 0000 0000 0000  ................
00000760: 0000 00ff 00ff ffff ff00 0000 00ff 00ff  ................
00000770: ff00 ff00 0000 ffff ffff ff00 0000 0000  ................
00000780: 00ff 0000 0000 0000 0000 00ff 0000 ff00  ................
00000790: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007c0: ffff 0000 00ff 0000 0000 00ff ff00 ff00  ................
000007d0: 0000 0000 0000 0000 ff00 0000 0000 0000  ................
000007e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000800: 00e0 0500 0050 0100 0404 0200 de00 0000  .....P..........
00000810: 0050 01e0 0500 0d03 0000 0000 0000 0000  .P..............
00000820: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000830: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000840: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000850: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000860: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000870: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000880: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000890: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

And what I noticed, based on the notes above, was these lines:

00000500: 01cc 1c1a 5518 cc1c 6ee0 0e16 2000 0502  ....U...n... ...
00000510: 01ca 31e9 4d18 ca31 6eb6 0a14 1d00 0403  ..1.M..1n.......
00000520: 01bb 00cc 4e19 bb00 5cbd 0d17 1f00 0000  ....N...\.......
00000530: 01a8 4b8e 4516 a84b 4ab9 0418 1e00 0505  ..K.E..KJ.......

These lines are the four main characters. Which I believe breaks down like this:

HADDRESS: AABB CCDD EEFF GGHH IIJJ KKLL MMNN OOPP
00000500: 01cc 1c1a 5518 cc1c 6ee0 0e16 2000 0502  ....U...n... ...
AA = Have character, e.g., you don't start the game with Odin, but this becomes 01 when you find him.
BB = MAX HP, maximum health points
CC = MAX MP, maximum magic points
DD = UNKNOWN, I originally thought level, but it doesn't quite line up.
EEFF = Experience points. The total accumulated experience points of the character.
GG = Current health points (HP)
HH = Current magic points (MP)
II = Attack power
JJ = Defense power
KK = Equiped Weapon
LL = Equiped Armor
MM = Equiped Shield
NN = Unknown
OOPP = Some sort of table reference for what magic spells are learned and available.

What I gleaned from other sites is that when a battle happens, at the beginning, or at the end, when the game adjusts the experience, then it calculates everything against what it should be. E.g., is your experience up, then you level up, and if you level up, your attack/defense/HP/MP should change, etc. What that means for you as an editor, it is easier to adjust the experience points, let the character get leveled up in a battle, and then it will automatically apply the rest, like what magic you have, etc.

Unless, of course, you are trying to have more than the original maximums that were available.

I'd have to do some trial and error for NN and DD. There are flags that are set if your character is "tied up" in a battle and needs to break free. This can carry over from battle to battle in a way that I don't fully understand as a game dynamic, but does seem to happen from time to time, because you can leave one battle tied up, get in another battle shortly after and still be tied up. I will be honest, I don't understand how "rope" works or if this even applies here. There is also a point in the game where Myau changes form from a cat to a pegasus-like creature, and perhaps this is used here?

As we discussed last time, the inventory of your 24 items is fairly simple:

000005c0: 3334 352d 3832 2639 2122 3b0f 2330 313e  345-82&9!";.#01>
000005d0: 2525 2525 2525 2525 0000 0000 0000 0000  %%%%%%%%........

Of which I just borrow from Czechnmymail[1]:

---Items---
00 = nothing
01 =  Wood cane
02 = Short sword
03 = Iron sword
04 = Wand            *Noah's best weapon*
05 = Iron Fang
06 = Iron axe
07 = Titanium sword
08 = Ceramic sword
09 = Needle gun
0A = Silver Fang     *Myau's best weapon*
0B = Heat gun
0C = Light Sabre
0D = Laser gun
0E = Laconia sword   *Alis' best weapon*
0F = Laconia Axe     *Odin's best weapon*
10 = Leather Armor
11 = White mantle
12 = Light Suit
13 = Iron Armor
14 = Thick Fur       *Myau's best armor*
15 = Zirconia Armor
16 = Diamond Armor   *Alis' best armor*
17 = Laconia Armor   *Odin's best Armor*
18 = Frade Mantle    *Noah's best armor*
19 = Leather Shield
1A = Bronze Shield
1B = Iron Shield
1C = Ceramic Shield
1D = Gloves          *Myau's best shield*
1E = Laser Shield    *Noah's best shield*
1F = Mirror Shield   *Odin's best shield*
20 = Laconia Shield  *Alis' best shield*
21 = Landrover
22 = Hovercraft
23 = Ice digger
24 = Cola
25 = Burger
26 = Flute
27 = Flash
28 = Escaper
29 = Transfer
2A = Magic Hat
2B = Alsulin
2C = Polymeteral
2D = Dungeon Key
2E = Sphere
2F = Eclipse Torch
30 = Aero Prism
31 = Nuts
32 = Hapsby the Robot
33 = Road Pass
34 = Passport
35 = Compass
36 = Cake
37 = Letter
38 = Laconia Pot
39 = Magic Lamp
3A = Amber Eye
3B = Gas Shield
3C = Crystal
3D = M System
3E = Miracle Key
3F - FF = Debug

The money line was a bit odd:

000005e0: 423f 1800 0000 0000 0000 0000 0000 0000  B?..............

Because the hex to decimal conversion of 423f is 16959, but in the game I had 16194, which is 3f42. So I believe you need to do it out of order to get the correct result. Or just make it FFFF, and you have so much money it doesn't matter.

While I am not 100% sure, these lines seem to be your location. E.g., which planet, town, village, dungeon, and position within it.

00000600: ff01 0102 0700 01ff 0101 ff00 0000 0000  ................
00000610: 00ff 0000 0000 0000 ff00 0000 0000 0000  ................

And finally, these lines seem to be switches. They are high or low. Either on or off. FF being high/on, and 00 being low/off. I believe these are the switches for in game logic such as: did you talk to the guy in the dungeon who told you the soothsayer's name so that when you talk to the soothsayer, he asks you the right questions, or when you have freed the professor from his prison cell, so now when you talk to his apprentice he agrees to help you... etc.

00000700: ffff 00ff ffff ff00 0000 0000 0000 0000  ................
00000710: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000720: 0000 0000 ff00 0000 0000 0000 0000 0000  ................
00000730: 00ff 0000 ff00 0000 0000 0000 0000 0000  ................
00000740: ff00 00ff 0000 0000 0000 00ff 0000 0000  ................
00000750: 0000 00ff 0000 ff00 0000 0000 0000 0000  ................
00000760: 0000 00ff 00ff ffff ff00 0000 00ff 00ff  ................
00000770: ff00 ff00 0000 ffff ffff ff00 0000 0000  ................
00000780: 00ff 0000 0000 0000 0000 00ff 0000 ff00  ................
00000790: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007c0: ffff 0000 00ff 0000 0000 00ff ff00 ff00  ................
000007d0: 0000 0000 0000 0000 ff00 0000 0000 0000  ................
000007e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

So that is my best guess and somewhat verified with some trial and error. I am 99% positive on the characters stats and equipment, 100% positive on the inventory items, and while I am pretty positive about the rest, I don't know how to actually use them.

I don't know if this will help anybody, but, there it is.

Linux - keep it simple.

[1] https://gamefaqs.gamespot.com/sms/588116-phantasy-star/faqs/24768 [2] https://github.com/mpontillo/psedit/blob/master/savefile.go

Phones ubuntu touch gearsystem sega master system ghex

Hacking with a hex editor and why I needed to cheat

After doing all that work to make the "SaveRAM" function of Gearsystem work on Ubuntu Touch, I figure I better give it a good test by playing through a game that uses it. If you are unfamiliar, my previous post is all about how I fixed the SaveRAM function so you could save the game ram to disk and have persistent games in the Gearsystem emulator. E.g., the ability to save your in game saves. So, I played through Phantasy Star to test it out.

First, while this is not a Phantasy Star review, I have to say, for a game made in the late 1980's, it is a really impressive game. It featured a top down "overworld" view while you travel from town to town. However, it had an amazing (for the time) 3D dungeon/cave/tower view which was really ahead of its time and made for some great game play. That said, this was also the TOUGHEST RPG I have EVER played. Since the dungeons are 3D in 1st person view, you had to remember every turn and way to go in each and every dungeon, and some of them were incredibly difficult mazes. One was so complicated that I eventually looked up a map just to get through it, because it had many "pitfall" traps, where the floor would collapse under your characters and drop you on some other floor, to which you don't know if it was a place you had been before or a new part of the maze. For a 4 MB cartridge, I'd say they did really well!

But, there was another reason the game was so tough, and why specifically I needed to cheat. I suppose looking up that map might be considered cheating too, I kind of figure it was more of a "time saver", but I'd accept that looking at the map was cheating too. However, what makes the game REALLY HARD is not the fact that you can get killed by just about anything, or that the mazes are so convoluded, or that you can only carry 24 items, so health restoring burgers are limited (the game designers use cola and burgers to heal you, rather than something exotic like potions). What makes the game REALLY HARD is that the game is not presented linearly, so you can go anywhere at just about any time. However, you need to talk to certain non playable characters in a linear order to advance the story line.

What that means is if you miss talking to some dude sometime and progress through the game, you can't find the thing you need or get the information you need from a different place/character. And in my case, SPOILER at the end of the game, I tried to fight what I thought was the last boss (turns out there is more game after that!) and he roasts me instantaneously to a crisp. I tried to level up more, I have the best armor available, the legendary weapons told about in lore, and I still could not beat him. After several hours (counting leveling up more) I finally gave up and read a walkthrough guide. Turns out I need a crystal from a soothsayer in a dungeon. I actually had talked to that guy, but all he did was warn me to leave now before I am destroyed.

Reading the guide, it turns out that he is supposed to ask me 5 questions. I need to know to answer these questions a certain way, and then he gives me the crystal which protects me from the big boss' death ray. But he doesn't ask me those questions! So I read back through the guide, and there is another guy in a dungeon cell locked up in a tower that I have to talk to first, who tells me the soothsayer's name. So I trekked all the way out of this dungeon, back through the lava, through the scion cave maze, through the prison back to the main town, to the star port, through the manhole cover to the village of Gothic, into my personal space ship, flew all the way to another planet, through a series of maze-like caves, back to another tower, down to it's dungeon, and talked to that guy. He told me the name of the soothsayer, and I trekked all the way back, fighting dragons, zombies, reapers, and whatnot, only to get back to the soothsayer and he still didn't ask me the right questions!

So, I read through the whole guide from the start to when I should get the crystal. Nothing mentioned that I didn't do, that I know of. I believe that the only way to solve this is to talk to every person on every planet until I find someone who mentions something about the crystal, or Lassic, or soothsayer, and then come back and he will ask me the right questions. Only the entire population of 3 planets to talk to, so that shouldn't take long, right? I tried it, gave up after an hour on the first planet.

So, I decided to cheat and just give myself the crystal instead.

I thought it would be easy because I read a guide on save state hacking[1] for this game. But it turns out, that saved state hack was for the saved state of the game with the MekaW emulator, not the actual saved backup RAM from the game. E.g., the file I get from the backup with Gearsystem is the actual saved backup RAM as if you had the original cartridge, not the save state file you get from the MekaW emulator, which is a snapshot of the running game. For instance, the guide said that at address 0547 and 0548, there would be the hex for how much money your party had, but in the savedRAM, those areas are not that, in fact those addresses are blank, but I now I have lots of money in my game, from leveling up to try to fight the last boss and nothing left to spend the money on.

I tried to read some other guides, but they were for the newer SEGA Genesis version and also didn't apply. However, I did find some DOS command line tools made specifically for this purpose, but I use Linux, so I couldn't use them without setting up Wine or something like that. However, I found a tool written in Go [2] which didn't tell me what the hex codes were, but did have this helpful line:

// A Phantasy Star 1 save file consists of five saved games.
// A header of size 0x200 exists at position 0x100 in the save file.
// Assuming saved game indexes ("GameIndex") numbered 0 through 4, each saved
// game can be found in the file at location `0x500 + 0x400 * gamenum`.
// In other words, saved games start at index 0x500 and are each of size 0x400.
// Each saved game consists of four packed 16-byte PlayerRecord structures,
// representing Alis, Myau, Odin, and Noah. At offset 0xC0 in each saved game,
// there is a 32-byte array of inventory items. At offset 0xE0 in the saved
// game, a two-byte value can be found representing the number of meseta
// followed by a one-byte value representing the number of inventory items.

Ah-ha! Now I could use the hex codes from the first[1] website with the note from the second[2] website and hack my game. If you use a hex editor like hexedit or ghex (terminal and gui, respectively), you will find that the first game save starts at 0x500, goes for 400, then the second game save starts at 0x900, goes for 400, and so on. And, a saved game looks like this:

00000500: 01cc 1c1a 5518 cc1c 6ee0 0e16 2000 0502  ....U...n... ...
00000510: 01ca 31e9 4d18 ca31 6eb6 0a14 1d00 0403  ..1.M..1n.......
00000520: 01bb 00cc 4e19 bb00 5cbd 0d17 1f00 0000  ....N...\.......
00000530: 01a8 4b8e 4516 a84b 4ab9 0418 1e00 0505  ..K.E..KJ.......
00000540: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000550: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000560: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000570: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000580: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000590: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000005a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000005b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000005c0: 3334 352d 3832 2639 2122 3b0f 2330 313e  345-82&9!";.#01>
000005d0: 2525 2525 2525 2525 0000 0000 0000 0000  %%%%%%%%........
000005e0: 423f 1800 0000 0000 0000 0000 0000 0000  B?..............
000005f0: 0300 0000 0000 0000 0000 0000 0000 0000  ................
00000600: ff01 0102 0700 01ff 0101 ff00 0000 0000  ................
00000610: 00ff 0000 0000 0000 ff00 0000 0000 0000  ................
00000620: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000630: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000640: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000650: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000660: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000670: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000680: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000690: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000006f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000700: ffff 00ff ffff ff00 0000 0000 0000 0000  ................
00000710: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000720: 0000 0000 ff00 0000 0000 0000 0000 0000  ................
00000730: 00ff 0000 ff00 0000 0000 0000 0000 0000  ................
00000740: ff00 00ff 0000 0000 0000 00ff 0000 0000  ................
00000750: 0000 00ff 0000 ff00 0000 0000 0000 0000  ................
00000760: 0000 00ff 00ff ffff ff00 0000 00ff 00ff  ................
00000770: ff00 ff00 0000 ffff ffff ff00 0000 0000  ................
00000780: 00ff 0000 0000 0000 0000 00ff 0000 ff00  ................
00000790: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007c0: ffff 0000 00ff 0000 0000 00ff ff00 ff00  ................
000007d0: 0000 0000 0000 0000 ff00 0000 0000 0000  ................
000007e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000007f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000800: 00e0 0500 0050 0100 0404 0200 de00 0000  .....P..........
00000810: 0050 01e0 0500 0d03 0000 0000 0000 0000  .P..............
00000820: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000830: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000840: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000850: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000860: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000870: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000880: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000890: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000008f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

Which doesn't line up with the first[1] website, but this was still really useful. I knew that I had eight burgers. And the hex code for a burger, per the second[2] website was 37. Well, searching for 37 didn't reveal anything. But per the first website was 25. Look at this line:

000005c0: 3334 352d 3832 2639 2122 3b0f 2330 313e  345-82&9!";.#01>
000005d0: 2525 2525 2525 2525 0000 0000 0000 0000  %%%%%%%%........

Wait a minute! That second line is 2525 2525 2525 2525, which is 8 burgers! Further, if each quad is two items, and you can only carry 24 items total in the game, then those two lines equal 24 items! Further study revealed that 33, 34, and 35 are the roadpass, passport, and compass, which are my first three items in my inventory!

So, how do I solve my original problem? Remember, I needed to get the crystal from the soothsayer. So, I just gave myself the crystal by changing one of the burgers (25) to be the crystal (3C). Then I just copied the saved file back to the phone, put it back in the proper folder for the emulator SaveRAM files, and when I loaded Phantasy Star, it worked! Saved game slot 1 had all my usual stuff, but the last burger was now the crystal.

By the way, that boss wasn't the final boss, and even with the crystal he is still pretty tough!

Linux - keep it simple.

[1] https://gamefaqs.gamespot.com/sms/588116-phantasy-star/faqs/24768 [2] https://github.com/mpontillo/psedit/blob/master/savefile.go

Phones ubuntu touch gearsystem sega master system ghex

Gearsystem Start Button Fix

A while back, I had the pleasure of updating the Gearsystem app from Xenial to Focal and then pushed it up to Noble for Ubuntu Touch phones. If you are unfamiliar, Gearsystem is a Sega Game Gear / Master System emulator, which allows you to play old games from those consoles on your phone.

When I did the original updates, and then moved it up through to Noble, I actually had not used the emulator that much. It was fun, but the older games used there were not my favorite titles. Don't get me wrong, I love old games, but I wasn't particularly interested in the old Master System titles. I guess I was just a Nintendo fanboy at heart. However, lately I started playing it more, due to some other life circumstances, and I realized that the emulator had a big problem!

When I started playing it originally, I tested all the buttons, but being unfamiliar with the console, I did not realize that there was a problem with the start button and "1" button. As it turns out, they were tied to the same function, so pressing either button had the same affect as pressing the "1" or "A" button.

Note: I don't know why for sure, but I think Sega called their buttons "1" and "2" instead of A and B so as not to infringe upon the Nintendo controls.

The reason I didn't realize there was a problem is because when testing a game, I would press the Start button at the title screen, which would start the game, then I would play the game with the 1 and 2 buttons. The game I used for testing was Street Fighter, which was from Brazil, which was technically a hack/backport where they took the Street Fighter II arcade game and redid it for the Master System. Since the 1 and 2 buttons were the kick and the punch, and pressing start started the game, I didn't realize that pressing the 1 button would also start the game, and pressing the start button would also punch.

However, while playing several other titles lately, I quickly came to understand that the start button was not functioning as it should. Like in Sonic the Hedgehog, you can't pause the game. So, I started digging around. At first, I thought it might not be an emulator problem, but rather a QML problem with the QML displayed buttons not being mapped to the correct thing. I added some debugging logging statements and watched the console as I tested the emulator and found that all the buttons were mapped correctly, e.g., if you pushed the 1 button, it said "A button pressed" and if you pushed the start button it would say "Start button pressed", so that ruled out QML button mapping issues.

The next place I went to take a look was in the input.cpp and input.h files. Unfortunately, I spent way too much time there, only to realize in the end that it was not an input problem. However, that did lead me to a searchable term, which I was able to find with rgrep:

$ rgrep -i key_start
platforms/qt-shared/MainWindow.cpp:                m_pEmulator->KeyPressed(joypad, Key_Start);
platforms/qt-shared/MainWindow.cpp:                m_pEmulator->KeyReleased(joypad, Key_Start);
src/GSEmulator.cpp:        keyPressed(Joypad_1, Key_Start);
src/GSEmulator.cpp:        //keyPressed(Joypad_1, Key_Start);
src/GSEmulator.cpp:        keyReleased(Joypad_1, Key_Start);
src/GSEmulator.cpp:        //keyReleased(Joypad_1, Key_Start);
src/Input.cpp:        if (!m_bGameGear && (key == Key_Start) && IsSetBit(m_Joypad1, Key_Start))
src/Input.cpp:    m_IOPort00 = (IsSetBit(m_Joypad1, Key_Start) ? 0x80 : 0) & 0x80;
src/definitions.h:    Key_Start = 6,

This listed all of the files with the phrase Key_Start in them, which you can see I originally got from the Input.cpp file. As it turned out, in the GSEmulator.cpp file, there was this odd block of code:

void GSEmulator::startPressed()
{
    if (not m_emu->isGameGear()) {
        aPressed();
    } else {
        keyPressed(Joypad_1, Key_Start);
    }
}

So if the emulated ROM (game) is not a Game Gear game, then if you press the start button, it calls the function for pressing the "a" button. But if the ROM is a Game Gear game, then it calls the function for pressing the Start button. Odd. I was playing a bunch of Master System games, and the start button would not work for them, so I changed the code like this:

void GSEmulator::startPressed()
{
    if (not m_emu->isGameGear()) {
        //aPressed();
        keyPressed(Joypad_1, Key_Start);
    } else {
        aPressed();
        //keyPressed(Joypad_1, Key_Start);
    }
}

I changed it this way because I thought it must be backwards. And this worked great for my test of Master System games. Unfortunately, it caused Game Gear games to not have a working start button! To be honest, I don't know why there is a difference between Game Gear and Master System in the code. The start button should just be the start button! So, I edited it again:

void GSEmulator::startPressed()
{
    if (not m_emu->isGameGear()) {
        //aPressed();
        keyPressed(Joypad_1, Key_Start);
    } else {
        //aPressed();
        keyPressed(Joypad_1, Key_Start);
    }
}

I probably should remove the whole check if/else statement. But I left it in, in case there is some reason that I later will realize needs the check and to differentiate the two uses of the button. Either way, now it works for both Sega Game Gear games and for Sega Master System games.

Linux - keep it simple.

Phones ubuntu touch gearsystem sega master system