Saturday 30 November 2013

A title fit for a Kong

Not a lot more work required to produce the title screen (sans sprites)...

Donkey Kong (left) vs. Neo Kong (right) sans sprites

I've patched the main sequencer to skip the game-play demo and go to the title screen, which flashes for a few seconds like the real deal. Obviously Mr Kong is made from sprites - 10 to be exact. Now it's time to undo the patch and at least get the game-play demo screen rendered. Here the code gets a little more involved and again there are some sections I'm yet to reverse-engineer. However, I don't think it's going to be a huge amount of work to see the main background for level 1 (a.k.a. the barrel level).

I've also got a 'define' in the source for which level the game-play demo uses. Obviously Mario's attract-mode movements aren't going to be suited to the other levels, but at least at a later date I can have each of the levels rendered without delving into the game-play proper.

Friday 29 November 2013

Insert Coin

Neo Kong is coming along nicely, and now it displays the 1st attract mode screen - the 'INSERT COIN' screen - in its entirety. It doesn't seem like much, but there's a decent amount of infrastructure code behind the scenes; as well as the main program flow I described last post, there's also the routines for displaying general messages, credits and scores, for example.

Donkey Kong (left) vs Neo Kong (right)

One of the more interesting areas I reversed-engineered last night was the mechanism for queuing functions to be executed in the foreground loop. Up to 32 functions can be scheduled by storing the function's jump-table offset, together with a 1-byte parameter, in a queue. Typical functions that may be queued include messages and bonus & score updates. Perhaps not surprisingly, the code for this mechanism has deviated the most from the original Z80 code.

With a reasonable amount of the infrastructure now in place, I'm hoping that there's more eye-candy to show (i.e. attract mode screens) for relatively little more effort.

And here I may have found a 'bug' in MAME. "L=00" isn't supposed to be updated in attract mode; but the code to initialise the attract-mode flag after power-on is executed from the VBLANK interrupt. In MAME the foreground loop gets executed at least once before the 1st VBLANK interrupt, which results in the level being displayed. Not so on the Neo Geo (I've fudged it for comparison). I'm wondering what the real hardware does!?!

EDIT: Seems I was a little too quick to blame MAME. Turns out that the update of "L=00" is a side-effect of the routine being called before the variables are initialised; the score is compared against the bonus life setting ('00,000') and an extra life awarded, causing the level to be displayed. I hadn't finished coding the routine that is responsible for the erroneous update.

I'll skip the game-play demo - which appears after the 'INSERT COIN' screen - and start on the title screen next (sans sprites).

Thursday 28 November 2013

Not so horrible after all!

After a few sessions of getting into the business of porting Z80 code, I must say it's not nearly as onerous as I first feared. Having said that, there's a big difference between blindly porting code instruction-for-instruction, and re-writing commented Z80 code in 68K.

The former would be unbearably tedious, not to mention horribly error-prone, and in hindsight I don't think I'd ever attempt such an exercise. The latter is almost fun!

Whilst technically I'm porting the code line-by-line, I'm striking a balance between keeping the code as congruous as possible, and taking advantage of the 68K instruction set. For the most part I'm using a one-to-one mapping between Z80 and 68K registers, but not strictly adhering to that when it's more efficient not to. I'm also taking care to keep the routines (and data) in the same order, with the intention that once it's all complete, I should be able to merge the Z80 and 68K listings to produce a side-by-side code listing.

Thus far, both the foreground (main) loop and background (NMI/IRQ1) loop structure is there and running. The hardware initialization (video h/w and dipswitches) is done as is reading the controller inputs (which happen to map nicely to the Neo Geo controller). Right now the 1st attract screen is shown (incomplete) and it then transitions to the 2nd (game-play demo, barely started) after a time delay. Here is where the code starts to get more interesting - drawing the in-game screens!

I'm also using macros for all VRAM accesses which means, in theory, I can alter the mapping at a later date by editing a few simple macros. This should allow me to, for example, rotate the screen or map the video accesses directly to non-contiguous / sparse memories such as the Neo Geo sprite tilemap memory.

I'm starting to encounter areas of the code that I haven't commented yet, which means I'll be switching back-and-forth between commenting the Z80 code and porting it to 68K. Actually it's not a bad way of doing things at all, and in some ways makes it a little easier from the reverse-engineering perspective - not to mention a little more interesting!

In the back of my mind I'm trying to think of other classic video arcade candidates for a Neo Geo port. I chose Donkey Kong only because I had access to a (partly) commented disassembly. I'd like to try my hand at a horizontal game next; something more suited to the Neo Geo screen. Unfortunately, most of my favourites are vertical shmups. In particular my all-time favourite, Xevious, which is another Z80 game. The interesting thing about Xevious is that there is already a 68K port - as part of Namco Classics Vol 1. What's perhaps even more interesting is that I wrote the MAME driver for it, and so I'm familiar with the graphics processor that is used!

I wonder if there's a market for an MVS cartridge with a compilation of classic arcade games???

Tuesday 26 November 2013

Less hacky

Last night I managed to clean up the linker script and it now builds with multiple ASM files linked together, as opposed to a monolithic source file with .includes scattered throughout.

And today at lunchtime I put together a minimal header include file and linker script for the NGCD.

Have to say, the NeoCD emulator does a better job of rendering the scaled sprites than MAME does. Whether it looks that much better on real hardware - I have no idea. Unfortunately, developing for the NGCD is a more tedious process, as each build requires un-mounting the virtual CD image, burning another with ImageBurn (which I've managed to reduce to a 2-click process) and then re-mounting and launching the emulator. There's also the lack of a built-in debugger, which I've already used in MAME extensively whilst trying to solve my numerous NOOB mistakes, and I haven't even scratched the surface of the port yet!

EDIT: After proof-reading this I realised I could probably leave the image mounted and copy the PRG file directly to the virtual CD drive - DOH!

As a result I will primarily be working on the cart version, which can be up-and-running in MAME well within 2 seconds from the last edit change. Every now-and-then I'll check that I haven't broken the NGCD build.

So now it's down to the business of porting the Z80 code. My first milestone is to have the attract mode screens (minus the game-play demo) ported and cycling through. That's at least a few week's work given my time constraints, so probably not a lot to blog about (except for hardware news) until then.

Monday 25 November 2013

Same Same But Faster

After lunchtimes and nights stumbling my way around GNU assembler and linker, Neo Geo hardware and 68K ASM, I've finally managed to replicate my Donkey Kong rendering demo in 100% assembler.

This time 'round, I've got it rendering in both horizontal and vertical orientations, the plan being to develop both variations if practical - even if it means assembling both variations in their entirety and having them side-by-side in the same PROM image (after all, it's only 16KB of Z80 code!).

100% ASM rendering - horizontal and vertical orientations
It's not perfect yet - there are obviously a few nuances of Neo Geo sprite hardware I need to tame - but it's enough to get me started on porting the code. The demo comprises a single render function for the entire screen (which handles either orientation, selected with the controller A & B buttons) and is being called from the demo VBLANK handler (after executing the BIOS handler).

I'm still yet to decide the best approach for rendering in-game graphics, so for now I'll take the 'easy' way out, and maintain a copy of Donkey Kong's 1KB VRAM in Neo Geo RAM, and render from that every VBLANK (as the demo is doing now) until the best solution becomes clearer. I figure if I use macros to address the VRAM it'll be easier to adapt them to direct Neo Geo (and other platform) hardware accesses at a later date, as well as facilitating rotation of the display.

Before I get much further though, I need to step back and configure the linker properly, and I should also set-up the CD version as well rather than wait until the end. Regardless, there's a decent amount of code to port until I get anything at all to show for it, so probably not much to blog about in the next few weeks... although I am attempting to get the NGPACE hardware moving along...

Friday 22 November 2013

(Not Sega) Genesis

Today I spent my lunchtime setting up a new 68K ASM project that will become NeoKong (aka Neo Geo port of the arcade Donkey Kong). I've decided to stick with the GNU assembler for compatibility with C projects and the devkit, and I also figured (rightly or wrongly) that it was the de-facto standard assembler for Neo Geo development.

As it stands, my code instructs the BIOS to run the eye-catcher animation, sets the stack pointer, acknowledges interrupts and then loops kicking the watchdog. Not too exciting I admit, but it took a bit of time to convert the header files from the (ASW-based) sprite sample source from the Neo Geo Development Wiki page for GNU AS.

Now the fun begins.

No brute force for Kong?

I've been pressing ahead with the disassembly during my lunchtimes and evenings, where possible. Right now I've completed the bulk of the non-game-play portions of the code, which includes the initialisation, title and coin-screens, and the high-level program flow. I'd estimate about 25% code coverage so far, which perhaps sounds less advanced than it is, considering a lot of the data areas have been identified. These data areas appear all throughout the non-commented portions of the ROM code, giving valuable time-saving clues to their function. There's also a fair amount of drawing and animating code that has yet to be commented, which should prove more tedious and time-consuming than technically challenging.

I have to say that the code is rather well structured, and obviously well thought-out. After initialisation, the main foreground loop is responsible for mundane tasks like checking bonuses and updating scores, whilst the rest of the main logic is scheduled from the 60Hz (VBLANK) NMI. Here the inputs are read, sprites and sounds updated, and the rest of the logic dispatched via a hierarchy of function vector tables, not unlike Pacman.

The hardware is also funky; two (2) banks of up to 128 sprites (a total of 16 per scan-line) whose registers are DMA'd from CPU memory space every NMI. Not unexpectedly, Donkey Kong only uses a handful of those sprites.

As I become more familiar with the code, I'm now considering re-thinking my approach to a 68K port. A brute-force, line-by-line translation would not only be mind-numbingly tedious and error-prone, but I'm starting to think also unnecessary! Previously I was worried about implementing portions of code that weren't understood, and whose side-effects were unknown, but now I'm thinking that those incidences will be kept to an absolute minimum.

It also tends to negate the need to maintain a side-by-side Z80-68K ASM listing. I would retain the overall architecture whilst organising the code and data in the same sequence as the original Z80 code, but in a separate file. Perhaps a little more cumbersome during development, but ultimately more flexible in the long-run. For instance, it would be easier to start implementation before 100% completion of the disassembly - for example right now I could code the attract screens without the game-play portion (and I may do just that) - and easier to patch functions and/or modify program flow for debugging/development.

The end result would be cleaner, more native 68K code, and easier to maintain. I would also strive to separate the Neo Geo hardware aspects of the code, facilitating easier porting to other 68K-based platforms, such as the Amiga.

All still pie-in-the-sky atm, but I firmly believe completely achievable!

Tuesday 19 November 2013

Back to Donkey Kong...

After playing around with the Phoenix emulator, and unsuccessfully trying to get it running on the MVS system, I've decided to leave that to the original author and get back on track with my Neo Geo BIOS and Donkey Kong projects.

To that end, I've now finished transcribing Sockmaster's comments into IDAPro which would mean that, technically, there should be enough information to start work on the 68K port. However, I'm loathe to leave the source half-commented (actually, considerably less than half) and have decided to press on with the reverse-engineering and comment as much of the code - aiming for 100% - as possible.

As it stands I've got a reasonable idea of the structure of the code; the program flow and the data structures involved. The code should also be almost completely re-locatable (at assembly time) now; later on in the piece I'll try moving code and data around and see if it still runs on Donkey Kong hardware! It might also be possible to port it to other, suitable, Z80-based hardware as well - any 8x8 tilemap system with 16x16 sprites should suffice. There is, for example, a port on Galaxian hardware.

Anyway, once I start the port my aim to is get the first level running, and release that as a playable demo. I'm also intending on coding both horizontal and vertical versions (selectable via soft dipswitch) - I guess it'll be the Neo Geo's 2nd ever vertical game!?!

Thinking more about the 68K port - it will perhaps be a little more work than the 6809 because of the 68K's 16-bit data bus. There will also be the temptation to simply re-write routines in 68K ASM with little regard to Z80 registers; an approach frought with danger when other routines expect certain values to be retained in certain registers. Must... resist... temptation...

So it's not going to end up as nice 68K code by any standard, unfortunately. But once it's running on the Neo Geo, other possibilities are interesting. An Amiga port almost goes without saying, but then there's also the Sega Genesis/Megadrive! Hmmm...

Tuesday 12 November 2013

Phoenix Emulator (not mine)

I've been in touch with the NGCD Phoenix emulator author, and unfortunately (for me) the Z80 core he's using - Marcel de Kogel's portable Z80 emulator - is written in C. Right now the emulator itself is in its infancy, and running very slowly, although obviously there hasn't been any optimisation done to it yet.

I also volunteered to help out with the rendering; first ripping the tiles and palette from MAME and enhancing my Donkey Kong demo to work with Phoenix as well.

Neo Geo Phoenix demo
Then I started work on building the emulator, which was designed for the NGCD, for the MVS (cartridge) system. Ultimately I discovered that there isn't enough RAM on the MVS to run the emulator as-is, so I then turned to building it for the NGCD in an environment I was familiar with.

After coming to grips with the nuances of the NGCD vs MVS, such as the sprite format, it was quite straightforward to add my demo rendering to the emulator. I've still got some tweaks and clean-up before I send it back to the original author, but at least it will give him a head-start on the display - something I've had a lot of experience with whilst working with FPGA emulations!

It might also be worth my while getting Donkey Kong running with the C Z80 core, just for fun...

Monday 11 November 2013

Eye catcher, and possible emulation?

I noticed during development that the so-called eye-catcher graphics (the spinning NEO-GEO graphics and SNK logo) had completely random tiles on my Donkey Kong demo. This piqued my curiosity so I decided to look into how it is actually rendered.

The standard eye-catcher graphic


Turns out that the animated graphics can be either rendered by the BIOS, or the game itself (or not at all), depending on a flag set in the cartridge header. Either way, the actual tile data must be supplied by the cartridge - there is no graphic data in the BIOS itself!

The official documentation mentions as much, but doesn't give a lot of detail on exactly what is required. So I did some more digging in the BIOS disassembly, together with some experimentation with my demo. I discovered that the spinning logo is - not surprisingly - rendered as a series of sprites, and it uses a bank of (somewhat less than 256) tiles whose MSB is read from the cartridge header. As for the remaining text and copyright symbol; these are rendered from the FIX layer ROM on the cartridge and comprises various tiles interspersed with the more commonly-used ASCII characters.

So in order to clean-up my demo, I injected 256 sprite tiles from Baseball Starts Professional (surprisingly, NGH-001 - aka, NAM-1975 - renders its own eye-catcher) and set my cartridge header appropriately. Then I simply replaced the entire FIX ROM with that from the same and viola - a nice clean eye-catcher animation! Nothing exciting, but at least it's one more mystery solved for me.

In the mean-time I've been slowly plodding through the Donkey Kong disassembly, since the version I received - whilst extremely helpful - isn't quite as complete as I'd like. And since I'm using IDAPro to produce a Z80 source file as a starting point (I'd like to re-assemble it at a different address and see if it still runs in MAME), I'd like to ensure that it's as complete as possible since once I start adding 68K code to it, there's no going back.

It is a daunting task but I can see how, in some cases, a brute force approach - for some areas of the code - is sufficient. And that's no doubt what has happened with the 6809 conversion. But perfectly valid, obviously, as it runs beautifully on the Coco!

However, I'm starting to wonder whether an emulation might actually be possible on the Neo Geo. No doubt it would have to be hand-crafted in 68K assembler, as compiled C-code is just not going to cut the mustard on a 12MHz 68K. But maybe, just maybe, an assembler core with hand-patched graphics routines will do the job!?! I'm not sure, but I'm hoping to find out, as I'm now aware of possibly two (2) Z80 cores written in 68K assembler; one in a TRS-80 emulator on the Amiga, and another (yet to be confirmed) on a WIP Phoenix emulator on the Neo Geo.

A Z80 core would make life a little easier for sure, and open up the possibility for more arcade ports on the Neo Geo. Or maybe a TRS-80 emulator for it??? ;)


Wednesday 6 November 2013

Neo Kong - rendered

I've managed to put together a C program to render the Donkey Kong tilemap on the Neo Geo display, with the correct palette!

I started with the Neo Thunder source code, and after a hiccup with displaying sprites, I've got some rather hastily hacked-together source code that does the job. The stumbling blocks were defining palettes and tilemaps in the source code, as opposed to having the graphics tools create them for you automatically. But the upshot is, once everything is setup properly, the entire screen can be rendered as a single sprite.

The demo has two (2) modes of display; one shows 1:1 pixels and can be scrolled up/down to see the entire screen, the other scales the screen to the Neo Geo display. It looks a little less than awesome, but it's still playable. It should be possible to tweak the sprite graphics to have it looking as good as Sockmaster's Donkey Kong on the Coco, as in theory the scaling algorithm is deterministic and well documented.

Scaled display on the Neo Geo under MAME
Note that there will have to be some algorithm for scaling Donkey Kong sprite positions on the display. Or I could cheat and define 16 versions of each sprite (there's plenty of ROM space), and build maps that are scaled in the same way that the background is. Either way, I think that this is good enough to prove at least that Donkey Kong is viable on the Neo Geo.

Next step is to actually start on the port which will, of course, be 100% assembler.

Tuesday 5 November 2013

Neo Runner?

After looking at the sprite scaling specifications yesterday, I've realised that the 10-pixel tile height of Lode Runner wouldn't be an issue at all on the Neo Geo! So a port may still be on the cards...

In the meantime, my first attempt at generating Neo Geo sprites for Donkey Kong were - recognisable - if not perfect. Hopefully at lunchtime today I'll get time to clean them up and load them into MAME...

EDIT: I've fixed the graphics and filled JoyJoy (aka Puzzled) with the tilemap from Donkey Kong. Unfortunately, the MAME graphics display <F4> does not work with Neo Geo emulation, so here's a snapshot of the game running:

Next step is to write some Neo Geo code to render a sample screen, and play around with sprite control...

Monday 4 November 2013

Neo Kong

I managed to start on the program to convert Donkey Kong graphics to Neo Geo sprite format.

I've got the ripped arcade graphics - tiles and sprites - displaying on the screen. I'm also using the the real palette (CLUT 0) to display the tiles:



Next step is to convert them to Neo Geo MVS Sprite ROM format. I'll upscale the 8x8 tiles to 16x16. I'll also produce two versions of the arcade sprites; 16x16 and upscaled to 32x32. The latter will enable me to apply the same scaling to the tile characters as the sprite characters, if required.

Then I'll load the new sprite ROM's into one of the Neo Geo games and see if I can make out the sprites on the screen.

Any port in a storm?

To complement my BIOS disassembly, I had an idea to port an existing arcade/computer game to the NeoGeo. Ordinarily I would be tempted to emulate, but I don't think a 12MHz 68K is going to be powerful enough to emulate even a lowly 2MHz Z80.

What I had in mind rather was taking a simple arcade game, for which I had a fairly comprehensive disassembly, and then more-or-less translate it line-for-line into 68K assembler for the Neo Geo. This is exactly what Sockmaster has done for the arcade Donkey Kong on the TRS-80 Color Computer, and it's a very, very impressive piece of work too!

The disassemblies I have include (arcade) Space Invaders (Z80), (arcade) Tutankham (6809), (arcade) Donkey Kong (Z80) and (MSX) Lode Runner (Z80). Unfortunately both Space Invaders and Tutankham run on bit-mapped hardware, which the Neo Geo is incapable of supporting. That leaves the tilemap/sprite-based hardware of Donkey Kong and Lode Runner.

The FIX layer on the Neo Geo is, unfortunately, the top-layer graphic and as a result can't be used for background tilemaps. This leaves the system sprites to be used for all tilemap/sprite graphics. The Neo Geo has sprites in bucket-loads, but unfortunately they're all 16x16 pixels. Donkey Kong is 8x8 and Lode Runner is 8x10 - making it all-but-impossible to use Neo Geo sprites and still get a full-screen display.

One option is to scale 8x8 tiles to 16x16 and either use a scrolling half-screen display, or scale the sprites by 50%, as the Neo Geo has hardware sprite scaling. That's OK for Donkey Kong but not for Lode Runner with its 8x10 tiles.

So right now I need to find a candidate horizontal arcade game that I'm actually interested in with either 8x8 or 16x16 tiles and/or sprites and that can be disassembled relatively quickly/easily. But nothing is coming to mind atm...

EDIT: I've just had a brainwave!

The idea is to render the Donkey Kong tilemap in 16x16 tiles (by pixel-doubling each tile) and then have the hardware scale to the visible area of the screen. It should be possible to render the entire tilemap as a single sprite, using column sprites and chaining.



Anyway, I'd like to get a proof-of-concept up and running, by rendering a sample playfield from Donkey Kong on the Neo Geo screen. This is itself will be an interesting exercise...


Sunday 3 November 2013

From the ashes.

Real Life time demands are still preventing me from working on the hardware atm.

However I have been somewhat motivated since I recently discovered the existence of the Neopen BIOS project! With the UNIBIOS project and now this, there's evidently a few people floating around that are quite familiar with the Neo Geo BIOS; something that I think will be useful - if not required - when I get into the thick of HDL emulation of the system. It would also be cool to be able to contribute to the Neopen BIOS project!

To this end, I've started my own disassembly of the Neo Geo BIOS. Or rather the US 6-slot version - SP-E.SP1 - to be exact. Obviously I'm covering ground well-trodden by others, but the idea is to really learn about the system internals, and there's no better way than to reverse-engineer it yourself!

I'm no stranger to disassembling to reverse-engineer hardware platforms either, having all-but-completely commented Space Invaders (Z80), Tutankham (6809) and MSX Lode Runner (Z80). As for 68K, I've done a bit of work on Namco Classics Vol.1 whilst writing the MAME driver. These aside, I've also had to dig into dozens of arcade games and micro computer BIOS'es whilst attempting to emulate them in an FPGA.

The other good thing about it is that it's something I can pick up and work on whenever I've got a spare 20 minutes or so, whether it be at work or at home. So hopefully I can keep trucking along and also keep motivated about getting the hardware off the ground!