Phantasy Star Tunnel routine Disassembly

The Master System & GameGear are virtually the same hardware!
Post Reply
User avatar
akuyou
Posts: 360
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Phantasy Star Tunnel routine Disassembly

Post by akuyou » Thu Dec 12, 2019 3:19 am

At the request of a patreon backer I took a look at the caves of the "Phantasy star" game...

These are a 3d animated cave section, which has smooth great looking graphics...
PSScreen.png
PSScreen.png (4.74 KiB) Viewed 495 times
While I don't usually do it, I had a go at disassembling the section in a livestream... the stream is pretty long, and not so interesting, so it's up to you if you watch it!
https://youtu.be/egghnQY_Qpg

After the livestream I've done some more work, and will now post my findings below!

Basically it's a pre-rendered animation, and the Tilemaps and patterns are RLE compressed... Please see below!

If you're not familiar with RLE... think of this... Suppose I wanted to store the string
"AAABBBCDEEEEEE"
and make it smaller... well lets store a letter and a count - eg:
"A3B3C1D1E6"
We've saved a few bytes!

Some data won't compress of course... that "CD" part got bigger as we stored it as C1D1 - but we could write our code to split RLE compressable data and LINEAR uncompressed data separately to avoid this.
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

User avatar
akuyou
Posts: 360
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Phantasy Star Tunnel routine Disassembly

Post by akuyou » Thu Dec 12, 2019 3:24 am

Pattern data!... this is the definitions of the 'pictures' that make up the tiles...

A byte is read in... if it's a zero, then the data has ended.

if the top bit is 1, then it is treated as 'Linear data' (not compresed)... the low 7 bits are a linecount (B)...

3 bytes of data are combined together with an OR to fill the 4 bitplanes - this will limit the number of possible colors, but saves some ram...

In Linear mode, the 'B' lines are all unique, in RLE mode the 'B' lines are all the same... when B reaches zero, the loop repeats, from the start... until a zero byte is found.
PS2.png
PS2.png (5.84 KiB) Viewed 494 times
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

User avatar
akuyou
Posts: 360
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Phantasy Star Tunnel routine Disassembly

Post by akuyou » Thu Dec 12, 2019 3:27 am

At this point, the pattern data is defined, but we need to change the visible tiles (The tilemap)... this is not done directly to VRAM - it's saved to a buffer in ram.

Each tile is defined by two bytes... the first is various things like palette and H/V flip... the second is tile number... Because both bytes are so different in purpose, consecutive bytes would be unlikely to repeat...

To counter this the EVEN bytes are done first, then the ODD bytes (&6B6E is executed twice)

A byte is read in... if it's a zero, then the data has ended.

if the top bit is 1, then it is treated as 'Linear data' (not compresed)... the low 7 bits are a linecount (B)...

for tilemap data Byte data is copied 'As is' into the buffer.

In Linear mode, the 'B' lines are all unique, in RLE mode the 'B' lines are all the same... when B reaches zero, the loop repeats, from the start... until a zero byte is found.
PS1.png
PS1.png (4.28 KiB) Viewed 494 times
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

User avatar
akuyou
Posts: 360
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Phantasy Star Tunnel routine Disassembly

Post by akuyou » Thu Dec 12, 2019 3:34 am

OK, our Pattern data is defined, and our Tilemap is correct in the RAM buffer, but we need to move it to VRAM!

Well this is pretty straight-forward, we just need a bunch of OUT commands! - But we're doing this in our interrupt handler, and time is of the essence... so we use a huge unwrapped loop - and call that loop many times to fill all the data from RAM to VRAM

And that's basically it!... Now of course, to make such an animation, you'd need to build the tile patterns, the tilemaps, and compress them all - you'd probably want some special software to do some of that for you, but I can't answer that from the source.

Anyway, I will try to do some kind of 'Animated compressed tilemap' tutorial at some point based on what I learned here - but it may be a while, as I'm pretty busy.
PS3.png
PS3.png (4.85 KiB) Viewed 493 times
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

SamusDrake
Posts: 5
Joined: Tue Nov 26, 2019 8:34 pm

Re: Phantasy Star Tunnel routine Disassembly

Post by SamusDrake » Sun Dec 29, 2019 5:30 pm

This has been a big mystery for years. Well done for investigating it.

Post Reply

Return to “Master System & GameGear Assembly Programming”