What's new

Space Invaders

serge2k

New member
well I started space invaders.

Got to the point where it was supposed to be showing stuff on the screen but it wasn't working properly and the design was incredibly painful to debug.

So I restarted and planned it out a bit better.

spaceinvadersfristscree.png


It is quite slow right now, I have a bunch of code that I'm just using to debug it that I think might be part fo the problem. Still have quite a few opcodes to implement as well.
 

redneon

New member
I'm trying to write a Space Invaders emulator to teach myself about writing emulators. I'm a professional programmer in the games industry and I've always been interested in emulators and fancied giving it a go.

I picked the Space Invaders cabinet because it seems extremely simple. I can theoretically just take existing 8080 or Z80 emulator code and just wire in the required functions to handle memory and whatnot.

Obviously, in future I'd like to do everything from scratch but as a start I thought I could knock up something like this in a couple of hours easily enough.

I'm using Marat Fayzullin's Z80 emulator code from http://fms.komkon.org. I've got a 64k memory buffer of which only the first 32k should be being used (16k for the cab and then the mirrored 16k). I'm loading the roms (invaders.h, invaders.g, invaders.f and invaders.e in that order) into the first 8k of my buffer.

I'm not sure how to do the mirror exactly but at the minute I'm literally just mirroring the first 16k into the second 16k (i.e. 0xab, 0x12, 0xd4 ... 0xd4, 0x12, 0xab).

The problem I'm having immediately is that only the read memory function from the Z80 emulator code is being accessed. The write memory, read port and write port functions don't get called at all from the Z80 code.

I'm presuming that the Z80 code starts picking up instructions at address 0x00 (the first element in my memory buffer and the first byte read in from the roms) and just runs from there. The first address it tries to read from is 0x00, followed by 0x01, 0x02, 0x03, 0x04 and 0x05. This would result in the following byte values being accessed 0x00, 0x00, 0x00, 0xc3, 0xd4, 0x18. Now I'm not sure how the Z80 works which is why I'm posting on here but the seventh address it tries to read is 0xd418 which seems strange to me for two reasons. 1) because it's greater than the 32k of memory that the cabinet is supposed to have (16k and 16k mirrored) but the main reason I find it strange is because 0xd418 is the combined value of the last two returned values (from accessing addresses 0x04 and 0x05).

I'm not sure whether it's a bug in the Z80 code or, more likely, something I'm doing wrong or not understanding.

My foray into the world of emulation is only a couple of hours long so far but I'm already enjoying it and am hoping someone can point me in the right direction.
 
Last edited:

swatgod

New member
space invaders uses a i8080a cpu, which would explain the trouble you are having. z80 wont be able to run the space invaders code at all to my knowledge as they are 2 completely different cpu cores.
 

nameuser

New member
Actually, the Z80 is backwards-compatible with the 8080/5, though there may be a few very differences when it comes to some flags. (for example, the P/V flag seems to be calculated differently)

For something as simple as booting Space Invaders - this shouldn't be a problem. (though fully running may prove problematic. Or may not.

Anyway, the program should jump to the valid address 0x18d4, so it seems that your emulator doesn't handle 16-bit words correctly? Maybe your machine's endianess is different from the machine endianess that the CPU emulator expects?


EDIT:
Oh and you misunderstood mirroring.
if, for example, a document says that "0x4000 - 0xffff" is a mirror of "0x2000 - 0x3fff", that means that:
mem[0x4000] = mem[0x2000]
mem[0x4001] = mem[0x2001]
...
mem[0x5fff] = mem[0x3fff]
mem[0x6000] = mem[0x2000]
...
mem[0xffff] = mem[0x3fff]
 
Last edited:

nameuser

New member
Sorry for double posting, but I was wondering what the differences between the Z80 and the 8080 are? (aside from things like timing and the addition of many instructions and features in the Z80)

Going by the 8080 doc I read, it seems that the 8080 sets the P flag to the result's parity even in arithmetic instructions, while the Z80 sets the P flag to the overflow. Is this true?
Additionally, there seems to be no trace of the "N" flag in the 8080 doc I've looked through, suggesting that the 8080's DAA instruction always operates as if after addition. Is this true?
Anything else?

Are there any "the undocumented documented"-style docs for the 8080/5?
Thanks.
 
Last edited:

redneon

New member
Oh and you misunderstood mirroring.

So it literally just duplicates the memory byte for byte from a specified address?

I know it's possible to emulate Space Invaders with a Z80 as a friend from work did it a few years ago. It was he who pointed me to Marat Fayzullin's website. I believe he used Marat Fayzullin's Z80 emulator too but it was a long time ago. 12 years, apparently :)

I see what you mean with the endianness but I believe the 8080 and Z80 are both little endian and the platform I'm developing on (x86, surprise surprise) is also little endian, so this shouldn't be an issue.
 

redneon

New member
Just thought I'd reply and add that I've figured out what the problem was. There's a define in the Z80 code (LSB_FIRST) which enables little endianness (I wrongly assumed this would be done by default).

I'm on the right track now, at any rate.
 

redneon

New member
Quick question. I've got it all working now but I don't understand why I need the mirrored memory? The game works exactly the same without it and with a 16k memory buffer.
 

Facon

I know nothing...
I've problems coding interruptions in 8080, how it should work?, I try to check hap's code, but it's unclear to me, I also check the documentation of the post but it says nothing (apparently) about it.

Can anybody explains it?
 

serge2k

New member
I was just wondering, is it actually necessary to emulate the 8088 auxilary carry flag?

edit: Okay, based on what I'm seeing it's not needed, but scoring and credits will be messed up if you don't.
 
Last edited:

n00b1

New member
I'm also making space invaders emulator and still have some problems which prevents playing the game.

At the moment everything looks like it should until to the part where invaders should come to the screen and then the screen wipes black and resets the program. Same thing happens if I insert coins and press start.

I think the shift register works like it should but I don't know why does this happen.

Other thing is that I got bored playing with the space invaders and tried out the balloon bomber and lunar rescue roms also as they should work with the same hardware I think. But I don't get it how can they work if the rom size is bigger than space invaders 8KB < 12KB. Then they try to set the stack pointer to 0x2400 like space invaders. Isn't that rom area? Anyway as result I get messed graphics in the title screen because it is writing over the program area :)
 

ASH-II

New member
Hello everybody (New here hope you can help :) )

I am trying to write Space Invaders on a new OS called SpecBas (it uses the ZX Spectrum Basic++ and outputs to Windows,Linux) it's in Alpha stage and I'm helping test it. (It's a bit like AMOS for the AMIGA but for PC's)

Anyway....

so I have managed to enter all the op codes and I am now up to the part where it draws the aliens in attract mode and from what I can understand (and I use that term in the loosest sense) I now need to emulate IN and OUT for the 'bit shifting' ???

and this is where I am stuck.

so in simple (idiot if you like) terms, what is 'bit shifting'?


how does the IN and OUT affect it (so which ones should I be emulating)

and where are the results stored? are the in the rom/ram or are they stored elsewhere (so I need to set up a new variable?)


Thanks for any help you can give me.

P.S. OR AM I TOTALLY WRONG HERE AND WHAT SHOULD I DO TO GET THE ALIENS DRAWING ? :(
 
Last edited:

pradipna

New member
"what is 'bit shifting'?"

I don't know the 'In' and 'Out' you're talking about (hadn't attempted to make a space invaders emulator :p) but bit shifting is really simple. Read the Bit Shift section of this: http://en.wikipedia.org/wiki/Bitwise_operation
In C++ you can use << and >> operators to shift bits.

And really nice screen shots, good luck!
 

ASH-II

New member
Thanks for the link.

yes I understand that bit it's similar to rrc,rlc, etc I am confused which ports I need to emulate (this is the IN and OUT) just enough to get it running. :)

As you can see it won't show the Aliens , so is there an interrupt that I'm missing like the screen beam at $20c0
 
Last edited:

ASH-II

New member
Update (if anyone is reading this) :)

so I got the aliens working and fixed loads of bugs (stupid , stupid opcode mistakes....by me)

the aliens not showing was the SBI opcode

and I now have sound

Just one more bug to fix.....when I shoot the saucer the score shows as 'CLE' instead of '150' also the saucer score is not added to the total score....sigh.
 

leonardo2204

New member
Hey guys, I've been looking at my opcodes for a week now but so far I couldn't accomplish anything on my game, it is always freezing on the ReadByte function telling me it has overflown. I'm developing my emulator in C#. I've seen a lot of sources, like the It's a trap, the one from emulator101.com and js8080, I've compared them with my opcodes but no lucky. Could you take a look and point out the error I'm making ? I'm not asking anyone to DO the emulator for me, but simply help and point the errors out !

My CPU class: pastebin.com/FzSXW2Cg

Thank you very much !
 

TRS

Member
Looking at the code provided I have to do some assumptions. I image manager is an array representing the memory which can be max 64K. Using an int as address pointer can cause overflows as an int in C# is 32 bits whereas the addressbus of the 8080 is only 16 bits. Try to change the PC.Value to ushort (that's a 16 bit unsigned int).

Also when returning a value from an array it is good practise to check if the value provided fits in the bounds of the array before returning the value from the array.

Good luck programming!
 

leonardo2204

New member
TRS, thanks for the quick reply ! I'm aware of all this assumptions you made, but I don't think that my problem is related to architecture problems, but with the opcode table. For what I've seen, SI uses only 50 opcodes, but I've implemented almost all of the 8080 op's, 'cause I want to develop a GB emulator in the future (I know its Z80 :)). Anyway, thanks for pointing that out but do you have any ideas ?

Thanks dude !
 

TRS

Member
Try running in debug mode. When the overflow occurs, check all relevant variables values and see if they are still within bounds. Then try to find out why the variable went out of bounds.
 

leonardo2204

New member
Hello everyone !
I managed to (hopefully) make my core works as expected. But now I'm having problem with the screen. I couldn't understand how to draw things on it. I've seen some sources but all people use Windows API to do so, and since I'm developing my emulator in C# I can override the "OnPaint" event to make it works, but I can't understand how video works (and how to code it) in c#; Simplifying, I know SI starts drawing at memory address 0x2400 and has a length of 1c00; The "img" must be rotated 90º anti-clock; I think my problem is related to the interrupt (which I couldn't fully understand as well).
Thank you !
 

Top