Monday, 30 November 2020

Lunchtime Coder #4-3 - 8080 Emulator for Space Invaders Arcade - The 8080 Processor Part 1

Before I go diving into the processor, I am slightly concerned about whether I can achieve the speed required for full speed Space Invaders emulation, so the first job is a bit of fact finding and then a programming language capability test.

The 8080 processor is clocked at approx. 2 MHz on the Space Invaders arcade board. Each instruction that the processor executes takes a set number of clock cycles to execute. This leads to a lot of different claims about how fast the processor actually runs in terms of MIPS (Million Instructions Per Second).

The normal way to rate the speed of a processor is to take an average number of cycles per instruction and then divide the clock speed by this number, which is why so many sites claim that the 8080 clocked at 2 MHz runs at 0.4 MIPS or 400,000 instructions per second.

I found this somewhat difficult to believe. It seemed somewhat of an overestimate to me. It was based on an instruction taking 5 clock cycles to execute when a lot of them take 7, 11 or even 17 cycles.

After some searching, I found a sensible estimate by Intel who make an educated guess at approximately 0.08 MIPS or 80,000 instructions per second. This sounds sensible.

The Space Invaders display runs at a refresh rate of 60 Hz, and so with 80000 instructions per second, and a refresh rate of 60 Hz, this means that the processor emulator would need to execute around 1333 instructions between each display update.

There is also over 51000 pixels bits to translate from the memory buffer to the screen, and just using "If value=0 plot black dot / If value=1 plot white dot" would mean over 102,000 "If / Then" checks. That's time consuming from a processor point of view, so simplifying this will save a lot of processing time.

For a start, lets count how many white pixels there actually is on a typical fully populated Space Invaders display.

DEV 1 - White pixel counter. A program that counts the colour of each pixel on a correct resolution, fully loaded Space Invaders screen snapshot (courtesy of MAME).

It turns out that even with a full complement of invaders / lives / shields etc, around 90 % of the screen is black, and therefore by not drawing the black parts of the screen, a lot of time can be saved.

So by only drawing the white pixels onto a background that's already black, we have reduced the If/Then checks by 50% to 51000 ish, but we are reading bytes from the array and then converting the decimal byte into an 8 bit binary string, and then reading the bits.

If we check whether the byte value is 0 in the array before we split it into bits (and according to DEV 1, 90 % of them are) then we could cut 51000 If/Then checks down to about 5000 on a full screen, and even less as the invaders get wiped out.

Now we have enough information to run a speed test.

DEV 2 - A dummy emulator program / speed test. The program will run at 60 FPS and will execute 1350 simple instructions between each frame update. The program will also run a simulation of reading 5000 bits of the screen buffer to the back buffer ready to display 60 times a second. FPS must remain at 60 and at least 1333 instructions must execute between each frame.

I made a real rookie error, and a big discovery about the programming language that I've been using for years when I wrote this DEV program.

As a matter of habit, I always put a frame timer in the main program loop to ensure that it doesn't peak above 60 FPS. I didn't realise that this was limiting the amount of processor power I had available.

My initial test was abysmal. I managed less than 5000 instructions per second. After a few tweaks (I was flipping the back buffer too often) things improved a bit, but even then the speed wasn't fantastic (less than 10,000 instructions per second).

Then I removed the frame timer and suddenly I have over 100000 instructions per second.

So it looks like my frame timer is actually a processor power limiter, a limiter that I will never use in my code again!

This isn't DEV 2 but a later partially functional emulator snap, but it shows full speed emulation

With a high confidence level, I am now ready to dive in. Now for the real challenge! The processor emulator.

1 comment:


    ❌ Binary Option, Forex and their likes are a means of making money but it’s more like gambling. There are no sure means to guarantee that a person could make profit with them and that’s why it can also be reasoned to be scam. Let’s not forget that some individuals even give you 💯 % guarantee of making profits and end up running away with your money. The internet today is full of Binary Option Recovery Scam, you see so much testimonies been shared about how a firm or Company helped them recover what they lost to Binary Options. But believe it, it’s just a way to lure more people and end up scamming them.

    ❌ You might have also come across some individuals that say they will give you guarantee on successful trades but they only end up as SCAMMERS as well. You here them say stuffs like 200% guaranteed in just 2 weeks and when you go into trade with them, they start telling you to pay profits percentage before you can get your income. These are all liars please avoid them. But if you have been a victim of this guys, then you should contact PYTHONAX now‼️

    ✳️The big Question is “Can someone Recover their money lost to Binary Option and Scam⁉️
    I will say yes, and will tell you how.

    The only way to Recovery your money back is by hiring HACKERS to help you break into the Firms Database Security System using the information you provide them with, Extract your file and get back your money. It seems like a really impossible thing to do, I will tell you, it should be impossible, but with the use of specially designed softwares known to HACKERS and Authorities (such as The FBI, CIA e.t.c) it is possible and the only way to recover your money.

    ✅ PYTHONAX are a group of hackers who use their hacking skill to hunt down SCAMMERS and help individuals recovery their money from Internet SCAMMERS.
    We just need the contact details of the SCAMMERS and Paymnet Info and within 4-8 hours your money will be return to you.

    ✳️ You can contact us via the emails below-:

    We also provide Legit Hacking Services such as-:
    🔸Phone Hacking/Cloning
    🔸Email Hacking & Password Recovery
    🔸Social Media Hacking & Passowrd Recovery
    🔸Deleted Files Recovery 🔸Mobil Tracking
    🔸Virus detection & Elimination.


    2020 © All Right Reserved.