Saturday, 1 October 2022

Characters, Tiles and Sprites

Just a quick update.

Yesterday I made some inroads into decoding the object tables for each area. I know the identifier bytes now for most of the static ground-based objects, including the Sol Citadels/Towers.

What this means is that I can now, in theory, generate the definitive map with the locations of all the Sol Towers. This is something I've wanted to do for well over a decade now.

It's interesting to note that the locations are per-area, which means different areas can (and likely do) have different locations for Sol Towers even when comprising overlapping areas of the global map.

And as an aside; these objects are all hardware sprites, not tiles, which surprises me a little. It would explain why you sometimes see them jitter by 1 pixel against the background when scrolling in MAME. It will be interesting to deduce whether it's a 'feature' of MAME, or a feature of the real hardware.

And way back in 2011 I was working on an FPGA implementation of Xevious and, like for my other FPGA projects, had developed a tool in C to convert the graphics ROMs and palette/clut PROMs to a more convenient format, as well as display them. I was using gcc (MinGW) and the (old) Allegro graphics library on Windows at the time.

Tonight I resurrected that tool, using a more up-to-date build toolchain and the new Allegro library. That took quite a bit of stuffing around identifying, downloading, installing and configuring the right components. Then I had to port the tool to the latest Allegro API - it's quite different from the old one. Fortunately I had my more recent Scramble tool as a reference.


Foreground layer tiles

Background layer tiles

Sprite tiles

This means I can now attempt to generate the maps; both area and global.

At this point I'm undecided whether to continue with the RE, or continue with the maps...

Wednesday, 28 September 2022

The Devious Map

I've been plugging away at the RE since the last post, but haven't been able to devote any large amount of time to it just yet, but thought I should post an update.

I have made some progress on the SUB CPU which reportedly controls the graphics objects and hardware. What I've discovered thus far would tend to support that theory.

I've spent most of the time trying to understand how the map data is stored in ROM. I'm almost embarrassed to say that it still eludes me, even after multiple sessions and with reference to both the MAME driver and the MiSTer Xevious core RTL code. In fact I'm beginning to wonder if my faculties are beginning to fail me; it has been a while since I've done any really challenging development, let alone reverse-engineering. But I'll come back to the map in a bit...

In the process I've worked out the scrolling logic and with it, identified the mechanism that defines the objects (both ground-based and air) in each area. Without going into too much detail, the game keeps track of what I've termed a scroll counter, and for each area there is a table with entries for each object, sorted by their coordinate (scroll counter value) and a few parameters. As the map scrolls, the code checks to see if there's an object on that row and if so, looks up a function table to handle the object.

By patching a single conditional return, the map can be rendered entirely devoid of any objects.

At this stage I haven't identified which functions correspond to which object, but I may focus on that next. It may not be as straightforward as one might think, as the first few functions I looked at simply set variables in shared RAM and return - no doubt for subsequent use by the main CPU. At least one of them does check the dipswitch setting for difficulty, so that will be a clue.

So, the map that is doing my head in. A full map image is 2048x1024 pixels, which corresponds to 256x128 tiles. Each of the 16 areas comprises a 256x32 tile (28 visible) strip on the map, some areas overlapping, some identical. I've even identified the table of offsets into the map for each area (strip).

The complete Xevious World Map

Two issues have me confounded. Firstly, there appears to be 512 different background tiles as displayed in MAME. However, the tilemap code (and the RTL) only deal with an 8-bit tile code. There's no tile bank switch that I'm aware of, so I simply cannot fathom how more than 256 tiles are used.

Secondly, a map of 256x128 tiles equals 32,768 tiles to define. Add 8 bits of attribute data (palette, colour, flip etc) and I'm expecting 64kB of data! However, there are three (3) ROM devices containing map data, for a total of just 16kB. But it gets more confusing; a lot more confusing. The map ROM is not directly accessible in CPU memory space, but rather the address is latched in two 8-bit registers. And the decoding from these registers is far from straightforward; 2 of the 3 ROMs are actually used to further generate addresses and with extra logic for automatically flipping thrown in.

I should add that I've found the code that reads in a 32-tile row from the map as it scrolls. As yet it hasn't revealed any clues, though I've just started to experiment and I do have a few ideas.

So unless I'm really losing it, and someone reading this points out the obvious (in which case I'll consider throwing in the towel and retiring) there's something more to the map than a simple array of tiles. I'll definitely write up a post about it if and when I manage to decrypt it all.

Saturday, 24 September 2022

Xevious

 After about 6 months of inactivity I've been motivated again to work on a retro project.

A few days ago I saw a post about a YouTube video called "The History Of Xevious"

Xevious gameplay

After a discussion about the video on a shmup group I was also made aware of another cool video on the secrets of Xevious. I discovered at least one sol tower I wasn't aware of, and also a picture of a map that showed all the sol towers in (the MSX version of) the game.

A map of the MSX version, which is very close to the arcade version

Xevious is my all-time favourite arcade game and therefore my "holy grail" of reverse-engineering and transcoding/porting projects. I started on this back in March (see the previous post on the Ghidra disaster) but after RE'ing all the "low-hanging fruit" it stalled as I got busy with Real Life again.

Xevious is probably the most ambitious RE project I've tackled to date. It has no less than three (3) Z80's running in parallel; one for the game, one for the inputs and graphics, and one for the sound. And after working on the code for a while, it's apparent that it's going to be a real challenge.

I picked it up a few days ago, and after getting familiar with the code again, I've made some progress. It's still slow going, and it's going to be a matter of just chipping away until things fall into place. It's not something that I expect to be able to finish in a matter of weeks or even a few months. Or maybe not ever.

And if I do ever finish, I'm not convinced it lends itself to the transcoding process either, especially on a target with one processor. This may end up being more of a port than a transcode, but time will tell.

From time to time, when I want a bit of a break, I may post a few entries on the structure of the code, or interesting aspects of the code etc.

Tuesday, 1 March 2022

Do not under any circumstances try to use Ghidra.

 What a f*cking waste of 3 days RE effort. I thought Ghidra was going well until tonight.

Then I started losing random sections of work; code and data sections would arbitrarily revert to 'unknown', losing all comments and memory references with them.

At first I thought it was my fault, but after going through and 'fixing' all the code/data sections again (and retyping a bunch of comments), I saved and quit and then re-loaded only to find that other random sections were gone. After a few passes of this fruitless exercise I gave up.

It is completely and utterly unusable.

This is in addition to the minor inconvenience - in comparison - that the Z80 core doesn't know about the undocumented instructions. And my project uses them. A lot.

Ghidra, you suck. Welcome back IDA Pro!

Thursday, 24 February 2022

Galaxian Part Nothing - The Holy Grail

The last correspondence I got from Miro was probably not long after my last post in September. It seemed to me he was approaching this project from a different angle and I couldn't really reconcile what I was doing with what he was doing. This is not a criticism in any way, shape or form, and I might also be completely wrong, but my original plan of working lock-step wasn't panning out.

With the distractions of everything else in life, I drifted away from the project and here we are some 5 months later and in that time I can honestly say I haven't even thought about Galaxian, let alone worked on it. I'm only posting now because I'm assisting someone else working on porting one of my earlier projects to another - proprietary - platform and I inadventently found myself back on this blog tonight.

I hope that Miro is progressing well and can finish his project, if he hasn't done so already!

So yet again I find myself with a slew of abandoned projects, some all-but-finished, and a blog to update.

Truthfully, I find Real Life is taking priority over my retro projects. Having young kids will do that to you. In recent years I picked up a few more hobbies and interests, mostly involving the kids or at least inspired by the kids, and retro gaming has become more about (occasionally) collecting and playing than reverse engineering and developing.

So what now?

Current projects aside, I've had a list of future projects and then there's that one "Holy Grail" project that is perhaps a little more ambitious than any of my previous projects. For years I've been using that as the carrot to motivate me to clear the backlog of projects (by completing them) before starting on it. Clearly it hasn't worked.

And then I start to wonder whether I'll ever get around to it. Or worse - someone else beats me to it. I've been there before a few times, and it really sucks the motivation out of you. I also see more and more technically impressive projects being released that I thought impossible back when I was more actively developing hardware and software. The project I have in mind is certainly nowhere near as technically challenging as what I see coming out now, so there's really no reason to put it off. I think I've learned enough about the process itself and starting to feel that it's "now or never" for me.

So maybe I'll just grit my teeth and start on my "Holy Grail" project. Maybe.

Tuesday, 7 September 2021

Galaxian Part 7 - Preliminary Impressions

Whilst I've been waiting for Miro to digest the previous posts and hopefully develop his framework to the point I have described, I've actually been doing a little bit of the transcode. I'm not going to go into any specifics here as I'd like to document it in some form of ordered fashion, but I will make a few observations, if for no reason other than hinting that I haven't yet abandoned the project.

If there was ever any shadow of a doubt that Scramble was based on the Galaxian codebase (and there really wasn't), it is beyond question in my mind now.

It of course helps that Scott Tunstall reverse-engineered both games - and hence the disassemblies use common terminologies and labels - but there are plenty of instances where those identically-named variables actually share the same address in both games.

Despite having transcoded only a handful of routines thus far, I've already encountered common code. And it's very clear that both games also share the overall architecture of the code. It's an absolute certainty that Scramble code was derived from the Galaxian codebase.

Now that bodes well for the project. Aside from the obvious advantages that a familarity with the architecture presents, I can also say with confidence that the assembler code lends itself very nicely to a higher level implementaion. And that's certainly not true of all assembly code.

And touching on the subject of process I discussed in Part 5, I'm happy so far with how things are turning out when preserving the original assembly code interleaved with the C code. During the course of this blog I'll be posting examples to illustrate various aspects of the transcode, and you can make your own mind up, but I think it adds more than it detracts from, for example, the 'cleanliness' of the higher level source file.

At this stage of the transcode, particularly given the architecture of Galaxian, there is a lot of tedious typing to be done; defining functions and their prototypes, adding variables to the working RAM structure, defining tables etc. I'll likely press on with this aspect of the transcode whilst waiting for Miro, so I can't at this point say when I'll post the next blog entry.

Thursday, 2 September 2021

Galaxian Part 6 - Verifying the framework

Before we start on the transcode proper, it would be prudent to test the display output portion of the framework, as this is what we will be using to verify the transcode itself. The purpose is to verify the conversion of the graphics palette, tile and sprite data, the ability to display tiles and sprites, and that the correct output is displayed from the tile memory and attribute areas. Correct output from the sprite registers would also be nice, but is also slightly more complicated and not critical at this point.

First step is to simply dump the tiles and sprites to the display, using the same mechanisms that the ported game will employ. For my Allegro prototype platform, that's copying a region of my sprite sheets for each of the 256 8x8 tiles or 64 16x16 sprites from a memory bitmap to the display. The output for Galaxian looks like this:

Galaxian Tiles and Sprites

Next step is to render a display directly from the Galaxian tile memory (character RAM or CRAM at 5000h) and tile attribute memory (object RAM or OBJRAM at 5800h). Note that the OBJRAM also contains the sprite registers, so these could also be rendered. This is essentially implementing the function that will update the Galaxian display as the game is running, and requires detailed knowledge of the CRAM and OBJRAM structures and mappings; an exercise I'll leave to the reader.

We of course need some (sensible) data in the CRAM and OJBRAM memory blocks, and the easiest way to do this is to run MAME with the debugger enabled, invoke the debugger at an aesthetically interesting point in the game, and SAVE the entire Z80 address space to a file. Then it's simply a matter of reading the binary dump at startup, copying the RAM and OBJRAM areas to suitable buffers if required, and calling the display routine. You should see something like this:

Eye candy before we've even started!

I think the only sprite visible (active) in this dump was the player's laser atop the ship - it's not visible here for reasons unknown at this point. But quite obviously this is pretty close to the actual output, although there will be further enhancements required to the display routine later in the process.

Now we can be reasonably confident that once we implement any code that writes to CRAM and/or OBJRAM, it will be faithfully rendered on the host platform display. Take a good look now, because it's likely going to be quite a few weeks before we see anything like this particular output again!

And that's it for the preliminary work. Next up is to start the actual transcode. No better place to start than the Z80 reset address, 0000h, which we'll implement in the galaxian () function called when the framework has been initialised and we're ready to execute the game.