Back to the '74 with a 8080 emulator - part 1
Tags: programming, dotnet, csharp, emulators
Published: 2019-10-10 19:19:01

<!-- wp:paragraph -->
<p>I decided to take a walk down memory lane, precisely back to '74. I named my horse 8080, like <a rel="noreferrer noopener" aria-label="the planet (opens in a new tab)" href="https://en.wikipedia.org/wiki/List_of_minor_planets:_8001%E2%80%939000#080" target="_blank">the planet</a> .</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Well, actually it's not a horse but an emulator, my first emulator ever :)</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It all started when I saw <a rel="noreferrer noopener" aria-label="this article (opens in a new tab)" href="https://www.hanselman.com/blog/EmulatingAPlayStation1PSXEntirelyWithCAndNET.aspx" target="_blank">this article</a> on Scott Hanselmann's blog, about this very smart guy who's trying to emulate a PSX with C# and .NET.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I never wrote an emulator before and honestly, I had no clue how these things work. A PSX might be a little out of my league right now so I started digging a bit, looking for something very simple.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Apparently, the <strong><a href="https://en.wikipedia.org/wiki/Intel_8080" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Intel 8080</a></strong> is the perfect learning platform.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"width":640,"height":332,"linkDestination":"custom"} -->
<figure class="wp-block-image is-resized"><a href="https://en.wikipedia.org/wiki/Intel_8080" target="_blank" rel="noreferrer noopener"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/KL_Intel_i8080_Black_Background.jpg/800px-KL_Intel_i8080_Black_Background.jpg" alt="the Intel 8080 processor, image courtesy of Wikipedia" width="640" height="332"/></a><figcaption>the Intel 8080 processor, image courtesy of Wikipedia</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The 8080 architecture is relatively simple, 8-bit for basically everything, running at 2MHz.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>It had a widespread use since its early days, but in 1978 it became the pivot of one of the first arcade, <a rel="noreferrer noopener" aria-label="Space Invaders (opens in a new tab)" href="https://en.wikipedia.org/wiki/Space_Invaders" target="_blank">Space Invaders</a>!</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I guess everyone knows <strong>Space Invaders</strong>, so probably no introduction is needed.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>On GitHub there's a lot of <strong>8080 </strong>emulators, mainly in C, but there's also something in C# or <a rel="noreferrer noopener" aria-label="Rust  (opens in a new tab)" href="https://github.com/XAMPPRocky/i8080" target="_blank">Rust </a>as well.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I'm coding mainly in C# these days, and honestly for my first tentative, I didn't wanted to move to another programming language. My goal was to get a decent understanding of how emulators are coded. Also, bringing back from the ol' college days binary/hex math and bitwise operations is always a good idea.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Obviously I didn't wanted to just clone an existing emulator. So I started looking online for references about the <strong>8080 </strong>and <strong>Space Invaders</strong> in general and eventually I found <a rel="noreferrer noopener" aria-label="this website (opens in a new tab)" href="http://www.emulator101.com/" target="_blank">this website</a>.It's full of resources and complete tutorials about the 8080 emulation. It helped <strong>a lot</strong> . </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So after a week of tinkering and debugging, I managed to write an initial prototype. As of now is not rendering anything, just processing the first <strong>45k instructions</strong> and displaying the CPU state.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>How am I testing it? Well, internet is full of crazy people. Apparently someone wrote <a rel="noreferrer noopener" aria-label="another 8080 in javascript (opens in a new tab)" href="https://bluishcoder.co.nz/js8080/" target="_blank">another 8080 in javascript</a>, but this is more like a debugging tool. I used this one to assert that I was implementing the OPs correctly and the CPU was always in the right state.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>All the code is <a rel="noreferrer noopener" aria-label="available as usual (opens in a new tab)" href="https://github.com/mizrael/8080-emulator-net" target="_blank">available as usual</a> on Github. I'm not focusing on clean code right now. And (dear God forgive me) I'm not doing TDD. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I'll make sure to refactor it once I get to see some <strong>real</strong> results on the screen, for now I just want to make sure all the <a href="http://www.emulator101.com/finishing-the-cpu-emulator.html" target="_blank" rel="noreferrer noopener" aria-label="50 Space Invaders ops (opens in a new tab)">50 Space Invaders ops</a> are implemented correctly.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Next step is to render something :)</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-1/" target="_blank">check it out!</a></i></small>