Back to the '74 with a 8080 emulator - part 2: rendering
Tags: dotnet, csharp, programming, emulators
Published: 2019-10-10 19:21:38

<!-- wp:paragraph -->
<p>And finally I got something working! Took me a bit of searching and head scratching, but at the end I managed to get some pixels on the screen :)</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://raw.githubusercontent.com/mizrael/8080-emulator-net/master/screenshots/space_invaders1.png" alt=""/><figcaption>finally!</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>If you remember <a rel="noreferrer noopener" aria-label="my last post (opens in a new tab)" href="https://www.davideguida.com/back-to-the-74-with-a-8080-emulator-part-1/" target="_blank">my last post</a>, we discussed a bit about emulators and why I started this project. The Intel 8080 is a quite easy platform to emulate and honestly, also a very interesting programming exercise.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>I managed to quickly implement the first 50 opcodes, which cover more or less 45k instructions of the original <a rel="noreferrer noopener" aria-label="Space Invaders (opens in a new tab)" href="http://computerarcheology.com/Arcade/SpaceInvaders/Code.html" target="_blank">Space Invaders</a>.</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>But just looking at registers dump in the console is not very appealing, at least <a href="https://matrix.fandom.com/wiki/Link" target="_blank" rel="noreferrer noopener" aria-label="not for me (opens in a new tab)">not for me</a>. So once I made sure the implementation was running fine, I started working on the rendering part.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now, Space Invaders has an interesting mechanism for displaying pixels on the screen. The video hardware generates 2 interrupts at a fixed time, each at 60Hz. This means that each interrupt occurs 60 times per second, which is approximately 16ms.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When an interrupt occurs, normally the current Program Counter is PUSHed into the stack and then is replaced with a low memory location. This location most of the times contains a CALL (or RST) to some routine. After that the Program Counter is reset to its initial value stored in the stack.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As I previously wrote, there are 2 video interrupts, each one handling half of the screen. This was done at the time to achieve smoother animations: when one of these interrupt occurs, half of the framebuffer gets updated, while the other one can be safely rendered. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>As you can imagine, updating the entire framebuffer and rendering it at the same time would have caused lots of performance problems plus other issues I guess.</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>For this project I'm using <a rel="noreferrer noopener" aria-label="MonoGame (opens in a new tab)" href="http://www.monogame.net/" target="_blank">MonoGame</a> to handle the grunt work for window creation and 2d rendering. I used XNA a lot during its glory days so the setup for me was a no-brainer.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a rel="noreferrer noopener" aria-label="Code for now (opens in a new tab)" href="https://github.com/mizrael/8080-emulator-net/blob/c22813ebed94da7a73706976cc69cb516a243992/emu8080.Game/Game1.cs" target="_blank">Code for now</a> is very easy and doesn't fully handle all the cases. If the interrupts are enabled (via the 0xfb EI opcode), it is triggering the two interrupts every 16ms in the Update() method. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once the CPU has completed its job, the framebuffer is copied to a texture and rendered on the screen. I'm not handling the half-screen update discussed before, and I'm not sure if I'm going to ever implement it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There's still a lot to do of course, but the results are encouraging :)</p>
<!-- /wp:paragraph -->

<small><i>this article is available also on my blog, <a href="https://www.davideguida.com/back-to-the-74-with-a-8080-emulator-part-2-rendering/">check it out!</a></i></small>