@gilesgoat, thanks for your interest in the project.
I hope I've not given the wrong impression by implying that I did some hardcore tinkering with the actual arcade hardware in order to get to this point! Much as I like the idea of reviewing technical manuals, burning replacement ROM chips and so on, the truth is that I'm a bit crap when it comes to hardware and the only source material I worked with was dumps of the arcade roms (i.e. the files you'd use to play a game in MAME). I too, had a look at the Pole Position schematics but they might as well have been in Greek.
Anyway, i digress. I started the project in April 2013 and I've been working on it for a couple of days on most days to the present. My impetus for starting the project was clear - my first attempt at a Pole Position remake ("Grid Leader") was a bit disappointing - I was pleased with the graphical style (which has a lot in common with this newer project) but the gameplay and game logic was derived through observation of the arcade game rather than an understanding of the internal game mechanics. In short, Grid Leader looked and sounded nice but wasn't that satisfying to play.
I started off by observing that the arcade hardware was running on one Z80 and two Z8002s (as you've observed), and imagined that the game logic was likely to be driven by one or more of these three chips. Having identified the ROM holding the Z80 code through examination of the MAME source code, I had a poke around using commonly available Z80 disassembly tools and came to the conclusion that there was little or no game logic in the Z80 code. In fact, to this day, I'm not sure what the Z80 in Pole Position does - I can only imagine it's there to help coordinate the activities of all the other hardware in the machine. While looking around this ROM, I tried inserting NOPs in various places to comment out subroutines, but couldn't seem to make any discernible changes to the game logic. In fact, any changes I made would either break the game completely, or make no difference at all.
I moved onto the Z8002 after a couple of days and stumbled upon a rather unfortunate reality - there are (to my knowledge) no publicly available tools to disassemble Z8000/Z8002 code. But at this point, I was reasonably convinced that the game logic lay in the Z8002 ROMs, so I had no choice but to work out some way to disassemble the two Z8002 ROM files in order to progress. Again, with the help of MAME, I was able to establish that the Z8002 ROMs were four 8K files, two of which belong to the first Z8002 ('sub') and the other two belonging to the second ('sub2'). So each of the Z8002s has its own 16K block of code to execute, with the code being stored in a slightly peculiar but thankfully uncomplicated interleaved format within the ROM files.
If you've done any disassembly work in the past, you'll know that one of the major challenges lies in working out which parts of a ROM or memory dump represent code, and which parts represent data. Fortunately, MAME's debugger features a 'trace' function, where you can dump the instruction pointer address along with the instruction at that address as the emulated CPU executes it. Which, once you've picked out all the unique lines (using good old Linux command line tools like 'sort' and 'uniq'), will give you a list of addresses within the ROM at which instructions lie, along with the actual instructions at those addresses. Having played through a game of Pole Position within a build of MAME with some subtle modifications, I had such a list at my disposal (for the 'sub' CPU) and quickly knocked together a PHP script to combine this list with the ROM file, writing out instructions at instruction addresses, and '.byte' directives at addresses where no instruction was found.
I was keen at this point to understand whether my chosen approach was anything approaching correct. In order to establish this, I needed a tool that would take the disassembly generated by my PHP script, and assemble it back into the original ROM file. Open source software came to the rescue once again - a couple of hours digging around the net and I had a functioning Z8002 cross-assembler running courtesy of the GNU binutils project. The first run of the assembler against my assembly source file, rather disapppointingly, resulted in thousands of errors. Thankfully, the cause of these errors was merely a couple of syntactical differences between the instruction format generated by MAME and that expected by the assembler. A bit of tactical hacking on the PHP script, and the instructions generated in MAME were being converted into the format expected by the cross assembler. I managed to get a successful run of the assembler, wrote some further tools to convert the generated binary file back into the interleaved ROM format, and replaced the original Z8002 ROM files in my MAME roms directory with the ones I'd just generated. I ran Pole Position in MAME, expecting something unpleasant to happen, but amazingly the game ran as normal! Over time, I played several games of Pole Position, trying to execute as much of the codebase as possible by performing a wide variety of actions (e.g. crashing into other cars, running out of time, finishing the race with time to spare, getting a high score, not getting a high score...). As I did so, the size of the instruction map grew along with the number of instructions in the generated assembly file.
This was quite a major milestone in the project - I was equipped with a (somewhat flaky) set of tools to disassemble the Z8002 ROMs to generate an assembly listing, and reassemble this assembly listing back into the ROM file. I made a series of changes to these tools (which would become known as 'ppda' over time) to improve the legibility of the generated assembly file, using a series of text files to (amongst other things) insert labels, replace addresses with variable names and replace lists of .byte directives with ASCII strings:
Code: Select all
QUALIFYING_NUMBER_IS_WHITE: ! LOC_1F70
ldb rh1,#0x01
DRAW_QUALIFYING_NUMBER: ! LOC_1F72
ld @r12,r1 ! write the current qualifying number to the screen
inc r12,#4 ! move 2 characters to the right
incb rl1,#1 ! move onto the next qualifying number
djnz r0,DETERMINE_QUALIFYING_NUMBER_COLOUR ! go and display another number if we're not done yet
jr DRAW_QUALIFYING_BONUS
DRAW_POLE_POSITION_MESSAGE: ! LOC_1F7C
ld r12,#0x9e12
call DISPLAY_MESSAGE_CONTROL_IN_RH0
.ascii "POLE POSITION"
.byte 0x21
.byte 0x40
.byte 0xff
DRAW_QUALIFYING_BONUS: ! LOC_1F94
ldb rh0,rl6
ld r12,#0x9e98
call DISPLAY_MESSAGE_CONTROL_IN_RH0
.ascii "BONUS"
It's a bit hard to describe how the process of unravelling and documenting the assembly listing happens in reality - it's a very organic and unstructured process. Often, it was a case of randomly scrolling through the latest version of the disassembly listing, stumbling upon something that looks interesting, and then adding comments/labels or replacing addresses with best guesses of variable names. It very rarely becomes stale, because most discoveries (however small) will have knock-on effects elsewhere in the source file that open up other avenues of investigation. Sometimes I did get stuck for a couple of days and lose the will to carry on, only to discover a new avenue of investigation next time I opened the disassembly. As time went on, I gained more of a high-level view of the code - I began to see execution patterns and regularly-used data structures amongst the sea of instructions.
One technique that came in useful (which I understand is quite commonly used in this type of task) was making modifications to the disassembled code, reassembling and then running within MAME to understand the impact of those changes. For example, if I wanted to know what the subroutine at address 0x1234 was doing without actually reading through it, I could place the first instruction in that subroutine with a 'ret' instruction (being careful to pad out with NOP instructions as appropriate) to disable the execution of that subroutine. Subsequently executing Pole Position in MAME using the modified code would hopefully result in some obvious change in behaviour that would help to identify the purpose of the modified subroutine. Unfortunately, my initial efforts at making use of this approach were hampered by the checksum code in the ROMs - the emulated Pole Position machine was detecting that it's own ROMs were malfunctioning! It took a couple of days to work out where the checksum code was and how to bypass it and I was once again on my way.
So over the course of 3-4 months, I established the role of each Z8002 and gained an understanding of the relationship between them. Broadly speaking, the roles of each CPU are as follows:
[*]The first Z80002 is responsible for maintaining the game state, updating scores, moving the player car in accordance with controller input and communicating witthe graphics hardware[/*]
[*]The second Z8002 is responsible for moving the AI cars around the track. It can also execute one of a series of numbered operations at the request of sub1, by means of a function queue held in shared memory. For example, function number 0x1 lays the cars out on the starting grid[/*]
I'd found the game logic code much more readable (and interesting) than the code to communicate with the graphics hardware. It was clear by this point in time that trying to unravel the graphics hardware management code was going to be a pain due to the code complexity resulting from limitations in the (admittedly impressive for the time) graphics hardware. For example, AI cars beyond a certain distance are drawn at low detail using two sprites, whereas cars close to the camera are drawn at a higher level of details using 6(?) sprites - the logic for spreading a single car across 2 or 6 sprites along with the code to choose between these two approaches is all embedded in the code in a not so readable fashion. I was questioning the benefit of making sense of this graphics hardware management code at this point in time, and this was part of the reason I chose to focus my efforts on building a brand-new 3D scene graph on top of the existing game logic.
I'll try to write some more on the next steps (conversion of the assembly code to C and the challenges of mapping the game logic to a 'true' 3D environment) at a later point, but this should be enough to go with for now! I'm aware that I've probably skimmed over some important matters so feel free to fire any questions over.