Friday, 23 April 2021

Lunchtime Coder #6 - Space Invaders Part II /Deluxe Emulator in Blitz3D / Blitz Basic

So once you have a few games running on your emulator successfully, the process of adding more games gets easier right? Wrong!

Every game added comes with its own individual issues to resolve, but then that's the fun of writing emulators.

The Space Invaders Part 2 romset comes as five 2 kB roms and two 1 kB eproms.

The first four 2 kB roms go into the bottom 8 kB of the memory area 0x0000 to 0x1FFF (as per Space Invaders and Space Invaders CV). The fifth 2 kB rom goes in above the screen ram area at 0x4000 to 0x47ff.

The two 1 kB eproms are colour maps for "Everything but player two playing" and "Player two playing".

I took a quick peek at the colour map eproms expecting similar / the same as for Space Invaders CV.

This time, the values are offset by 8 so instead of the eight different colours ranging from 0 to 7, they now range from 8 to 15.

The colour maps themselves also differ from Space Invaders CV slightly.

The screen turns red on the fly when a base is hit, so again I used the Intel 8080 Output Port 3 Bit 0x04, the trigger for sound sample for base being hit to control the trigger and duration of screen going red. Basically the same method that I used in Space Invaders CV.

I also used the same method to detect when Player two is on screen as I did with Space Invaders CV. This is the cocktail screen flip bit in Intel 8080 Output Port 5, Bit 0x20. When set to 1, player 2 is playing.

All good so far. So with the roms in place, the eproms integrated into the drawing routine and a few tweaks to the dip switch settings, I ran up the game.

I should point out that my Intel 8080 emulator now has all 256 opcodes and all 5 conditional bits / flags implemented, so no additional opcodes to throw into the code this time.

Issue 1: There was an erroneous manufacturers logo on the opening screen. This shouldn't be there.

Issue 2: When that logo is meant to be there on completion of the opening screen drawing, it should flash between the manufacturers name and the word "HI-SCORE". This wasn't happening either. Just a fixed manufacturers name.

Issue 3: All sorts of bizarre effects when you get in game / attract mode game, from graphic artefacts to resetting back to the opening screen.

It seemed to me that the interrupts were occurring too quickly, as in before the code had reached the point that it was waiting for an interrupt.

My first attempted resolution was to speed up the processor clock by allowing more processor clock pulses between each interrupt. This actually resolved issue 3, and the game became playable without any artefacts. The speed of the processor was now at 3.125 MHz which by chance is the same speed that an Intel 8080b runs at.

However MAME insists that this game should run on a 2 MHz Intel 8080, and I still had the issues with the manufacturers logo on the opening screen. Something was definitely not right.

I decided to go back to basics, and activate the debug log, and this was what saved the day.

I noticed an IN 0 opcode being run which means that the processor is reading Input Port 0. The romset versions of Space Invaders / Space Invaders CV / Super Earth Invasion didn't read Input Port 0, so for emulation purposes, Input 0 can be set to 0, or just ignored.

But...If its being read by the code, then there must be something to read in Input 0.

By a process of elimination, I set each bit in turn on Input 0 to 1, and ran the game... and Bingo! 0x40 set to 1 cleared all 3 issues.

There was some paranoia about people typing in rude names when they achieved the high score, so Taito supplied a "Reset High Score" button which is tied to 0x40 of Input Port 0.

The reason that the emulator hadn't been working correctly was that I was running it with this button permanently pushed down. This meant that additional code was running between interrupts and the program wasn't ready for the interrupts to happen, and this is why running it with more time between interrupts solved some of the issues.

Setting this bit to 1 resolved all of the issues.

Taito also provided a "Preset Mode" which I found by accident by setting what I thought was the bonus score dip switch.

The Preset mode enables you to set a score before you start the game, so in effect, cheat! This was so that arcade operators could set high scores higher and put in their own custom names as the high scorers.

Interestingly, The manuals for Space Invaders part 2 and Space Invaders Deluxe do not mention the High Score reset button. The preset mode is mentioned in the Space Invaders Deluxe manual.

Another game successfully emulated.

Thursday, 25 March 2021

Lunchtime Coder #5 - Space Invaders Colour Emulator in Blitz3D / Blitz Basic



Having written a Space Invaders emulator based around an early and late version of the various romsets available, I decided to up the game and try some other similar romsets in my emulator to see how they ran.

The first different romset I tried was Super Earth Invasion, a UK bootleg/clone of Space Invaders with different graphics for the Invaders, houses instead of shields, and also some code changes that enabled motherships to drop super bombs on you, and for shot invaders to split and multiply. It was designed to run on the Space Invaders Deluxe hardware, and in some ways has more in common with this game than with the original Space Invaders.

There was some minor differences in the DIP switch settings and how they related to the processor input ports, but with a few tweaks and adjustments it ran perfectly.



I added some colour to simulate the coloured gel treated mirror
effect seen in Space Invaders upright cabinets.


On the back of this success, I went for the Space Invaders Colour (CV) romset fully expecting some heavy additional coding to be required, at least in the screen drawing routine.

I was surprised to find that the romset contained 4 x 2 kB roms (similar to the original Space Invaders romset), and when I ran them I got monochrome Space Invaders out of them.

Also included in this romset are 2 x 1 kB eprom images. A bit of on line investigation showed that these two eproms hold the colour maps for a Player 1 and Player 2 game screen. Essentially the 1-bit bitmap is generated in ram, and then the colour is added during the video processing stage of the hardware.

To be more accurate, the player 1 colour map is really everything but player 2 playing colour map. The player 2 colour map changes the colour of the bases and highlights the player two score in white. The rest of the colour map is the same as the 'Everything else' colour map.

I wrote a small program that read the bytes from these eprom images and arranged them in a 32 by 32 square that showed that each square had a value of between 0 and 7 representing one of 8 different colours (Black, Red, Green, Blue, Cyan, Magenta, Yellow and White).

Each byte corresponds to an 8 x 8 pixel square in the game remembering that the Space Invaders screen is rotated 90 degrees anticlockwise so the screen drawing starts from bottom left to top left and continues moving right across the screen.





A quick update of my emulator code to read the eprom map as it was drawing the bits gave me glorious colour-o-vision.

After playing Space Invaders Colour on MAME a few times, I noticed that when a base is hit by an invader bomb, all of the on screen graphics turn red for a short duration. This must mean that the colour video processing can turn the whole screen a single colour 'on the fly'. I also noticed that the player colour maps change pretty much spot on the player change.

Making the colour change to red when a base gets hit was pretty easy. I used Intel 8080 Output Port 3 bit 0x04. This is the trigger for the 'Base Hit' sound sample to play, but this bit is set for a specific duration, so it seems that when its set to 1, all of the on screen graphics are red.





To flip between player colour maps, I used the Intel 8080 Output Port 5, Bit 0x20 which is for tabletop cabinet screen flipping. By default it is set to 0, but when Player 2 is playing, it flips to 1. This means that it must be the trigger for both cocktail mode screen flipping, and for changing between player 1 and Player 2 colour maps.



Player 1 Colour Map
Player 2 Colour Map


Near perfect.. Well you cant have 100 % perfection without having the original arcade machine running in front of you...

Tuesday, 23 March 2021

Lunchtime Coder #4 - Space Invaders Emulator in Blitz3D / Blitz Basic






Have you ever wanted to take a trip down gaming nostalgia lane and relive the “good old days” of late 70s/early 80s arcade gaming?

No?

Me neither... but apparently there are some odd types around who miss the retroness of old skool arcade gaming in smoke filled germ ridden festering pits of arcadiness, and this led to the miracle of EMULATION !

But... Playing retro games using emulators is one thing... Writing an emulator? Now thats a tough old well done rump!

(BTW I kid... I bloody love emulators!)





I wrote this emulator as a two headed "Technical Demonstration":

1. I wanted to demonstrate that Blitz3D, despite being an older language, still has good capability and can still be used for some kuul coding projects.

2. I wanted to prove to myself that I could write an emulator...





I have provided a link above for the Blitz3D source code of my basic Space Invaders emulator.

The basic emulator includes support for the following:

1. Two romsets of the original Space Invaders monochrome game - an older and newer version with noticeable differences.

2. Adjustable dip switch settings and access to the diagnostic mode and the coin mechanism tilt switch function.

3. A cocktail flip screen mode.

4. A simulated colour overlay (the old skool red, green and white overlay).

5. Any other settings that existed in the original arcade Space Invaders.

There is also a lot of rambling comments / remarks to explain what each part of the emulator code does.

The emulator should run other 8080 based Taito Arcade games and bootlegs with a bit of adjustment on the memory array size, rom placing within the array and dipswitch / Input Port settings and assignments.

I have tried Lunar Rescue, Galaxy Wars and other Space Invaders variants / bootlegs and these seemed to run fine.





The full compiled release has additional support for Space Invaders Colour, Space Invaders Part II, Space Invaders Deluxe and Super Earth Invasion (Bootleg of Space Invaders).

I included Super Earth Invasion for one reason only. Its the version I remember playing as a kid back in the very early 80's.

It also includes a lot of "fluff" such as Bezels, Backgrounds and simulated colour cellophane overlays, and the whole thing runs from a select menu system making jumping between games a bit easier.





So what does the emulator contain?

1. Full Intel 8080 processor emulator including all opcodes and conditional flags.
2. Rom and ram area up to a maximum of 64 kB, the maximum that the 8080 with its 16-bit memory bus could address.
3. Emulated 16 bit Shift Register.
4. Video Display emulator including Interrupt generator.
5. Master and Secondary dip switch Input.
6. Inputs from Coin mechanism including Tilt Switch.
7. Inputs from Player Controls.
8. Sound Output simulator using sound samples.

I will follow this post up with some posts on the trickier aspects of the emulator, and how to overcome them.