<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by friol on Medium]]></title>
        <description><![CDATA[Stories by friol on Medium]]></description>
        <link>https://medium.com/@frioltheoil?source=rss-4b33c178bdae------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*pWpqMb2XVUWNK5Z3DabolQ.png</url>
            <title>Stories by friol on Medium</title>
            <link>https://medium.com/@frioltheoil?source=rss-4b33c178bdae------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 21:04:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@frioltheoil/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 10: summing it all]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-10-summing-it-all-41d5423dec92?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/41d5423dec92</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[diary]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:27:13 GMT</pubDate>
            <atom:updated>2023-02-19T19:27:13.630Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 10: summing it all</h3><p>Ok, I can consider myself pleased with the result.</p><p>The emulator is far from perfect, but it runs Sonic and Outrun quite well, and this was our initial objective. Speed is also not bad on my old PC, and it could surely be improved.</p><p>On the bad side, many games still show a black screen, the graphic modes different from M4 were not implemented, it is missing “sampled” audio, etc.</p><p>Github says I have spent about <strong>20 days</strong> coding this emulator:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5bly88wv0BTsZHQDlKTNsg.png" /></figure><p>(it goes from the 30th of january to today, the 19th of febraury) (the stripe in january is for other projects).</p><p>The Z80 core is 7651 rows long, and far from complete, but 99% of the opcodes pass the jsmoo tests.</p><p>It was a short but nice trip, and reading this diary from the start shows how much you can progress in 20 days, not even coding all day, but just when you don’t have to work.</p><p>Now back playing Sonic!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41d5423dec92" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 9: sounds like maneskin]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-9-sounds-like-maneskin-184315018e9f?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/184315018e9f</guid>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[diary]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:25:20 GMT</pubDate>
            <atom:updated>2023-02-19T19:25:20.767Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 9: sounds like maneskin</h3><p>Sounding like maneskin may happen when you are at the early stages, and the emulator produces a buzzing sound completely distorted, piercing up your ears.</p><p>The SN76489 doesn’t seem too complex (how many times will I have to go back on this type of sentences?): 4 channels, just one single port to push data into the chip.</p><p>Ok, came back from the office (it was an hard day, and it’s also friday the 17th), with a high fever and no medications at home. So what? I started coding.</p><p>Some struggling and aspirine later, the first 3 voices are sounding at least at the correct pitch, but the channel volume doesn’t seem to work properly:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F800148348%3Fh%3D7067183bbb%26app_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F800148348&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1613748460-6a2d311dda8cdcb57811a57236f8d3893bcf1b3f43e7fb55025b3773e9c655af-d_640&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="888" height="708" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e41a85f48f42fc174eac8ed553abe01/href">https://medium.com/media/8e41a85f48f42fc174eac8ed553abe01/href</a></iframe><p>Ok, I was not considering that the volume works in reverse on the Sega Master System, so a channel with volume 0x0f is muted (how intuitive). This is the result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F800167752%3Fh%3D80d2da16fa%26app_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F800167752&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1613807578-c7defab0e65f0bf651a131f107dec50b49fd5f85db7a67553f47d5d34843001d-d_1280&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1788" height="1398" frameborder="0" scrolling="no"><a href="https://medium.com/media/50beb92dc4c3bc842060ac9beae2d7ba/href">https://medium.com/media/50beb92dc4c3bc842060ac9beae2d7ba/href</a></iframe><p>I also hacked up a 4th channel with noise, but it is not correct, for now. It just produces random noise with the correct volume.</p><p>I also started to add functions to the GUI, like a fast forward key (“\”), that helps with the dead times when testing the zexall rom. By the way, I’m at 652 opcodes, but some games still complain for unimplemented ones. Let me finally check how many damned opcodes this beast of a CPU has. I’ve read a book by Federico Faggin and the Z80 in the past (“Silicio”), and, if I’m not wrong, at a certain point he says that he designed the Z80 by hand, and that he had to redesign 1/3 of the chip because he was short of space! So I’m not in the position of complaining for one more opcode or two.</p><p>Ok, Internet says there are 677 opcodes, but for sure those are only the documented ones. I’ve made a rough count, and I see at least 1000–1200 opcodes there in total.</p><p>By the way, if there’s an unimplemented opcode, the emulator now crashes freezing the sound generation, so it really sounds like maneskin.</p><p>I’ve spent the last sessions of coding mainly playing the games and not coding. This is not good! But the emulator doesn’t crash so often anymore, so playing a game or two is pleasant.</p><p>Not so emulation related, but I was searching for the cost of a particular cartridge back in the 90s, and I stumbled into an Italian magazine that produced even a Sonic comic (!):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0nGK_6MUN4QcJy8OOeOMCg.png" /></figure><p>The game I was searching for, btw, was “Columns”, and the price back in 1990 was a whopping 24.99 pounds:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*JFi_xpMmgYkh-KvM6qKvbQ.png" /></figure><p>(what a theft).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=184315018e9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 8: doing it by scanline]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-8-doing-it-by-scanline-e79fbcc1ac3d?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/e79fbcc1ac3d</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[diary]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:23:36 GMT</pubDate>
            <atom:updated>2023-02-19T19:23:36.254Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 8: doing it by scanline</h3><p>Ok, the time has come. <br>We cannot push it further.<br>We have to write a scanline renderer!</p><p>Ok, implemented a first rough version of the scanline renderer for the background, and WOW! Sonic doesn’t seem to flicker anymore!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xu1nz4qEhTjQHRmOV8AGVg.png" /></figure><p>(btw, it’s hard to demonstrate the absence of flickering with a static image, but just believe me for now)</p><p>Ok, also done with the sprites:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*92Lf6WzHRPaiAO7zi1DvyA.png" /></figure><p>At this point, a fight with y finescroll started.</p><p>That y finescroll was sending me nuts, but after a while I have pixel-perfect y-scroll:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cx96DMYQpdtpcpYo2_WyLA.png" /></figure><p>And Bubble Bobble’s sprites don’t glitch anymore:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0reiOrTrTypD8Mvcx5uK5A.png" /></figure><p>And Hang-on bends properly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C_JVh2Z664lNHMdAEIdNsg.png" /></figure><p>smspal shows 64 gorgeous colours:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7e65E28teI0XUwoG8RNASQ.png" /></figure><p>And…hey! This is Outrun:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZDEPEOy2tTRBjSUy5X1mfQ.png" /></figure><p>Yeah, this linescroll upgrade was really worth it, and it only took me a couple of hours of work, at the end.</p><p>And, oh, that’s how it was meant to be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-2J17Qq2T-VDUL5I5aoGzg.png" /></figure><p>(that’s an italian flag, folks) (and that’s “Ciao”, our mascotte! pretty rad, isn’t it?).</p><p>It’s 01:57, and we are at 642 opcodes.</p><p>Oh gosh: I was using a wrong formula to set the colours (basically, the r/g/b components were in the 0–192 range instead of 0–255). Now the palette is much brighter:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d1_Kill9ifMYe4RGUKLkHg.png" /></figure><p>This means that all the previous screenshots are “wrong”. Damn.</p><p>Implemented (or thought I’d implemented) the tile priority to sprites, but now Sonic disappears behind a tree and a bunch of tiles. (update some days later: I changed slightly the priority algorithm, and now I set the priority bits only if the tile colour is not zero; this gives the correct result).</p><p>I think I will switch to sound for now.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e79fbcc1ac3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 7: taking off]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-7-taking-off-efca94f65bc1?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/efca94f65bc1</guid>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[diary]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:23:03 GMT</pubDate>
            <atom:updated>2023-02-19T19:23:03.588Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 7: taking off</h3><p>Ok, so I’ve downloaded a couple of cartridges to see if they run inside my emulator, and, differently from last days, they all at least show the title screen and maybe some of the inner game:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*lCcNMhqQC7OFkiE5W9W4IA.png" /></figure><p>We are at 539 opcodes, and this number didn’t grow so much in the last days. I’m still finding some unimplemented ones, don’t worry. But this means that to write a somewhat functional Sega Master System emulator, you need to implement at least 550–600 Z80 opcodes.</p><p>This makes this system a lot harder than the 6502-based systems, where you need to implement say 100 opcodes to see something on the screen (I think the C64 requires even less opcodes to show the boot screen).</p><p>I asked on the emulation Discord, and it seems that my roms that show a black screen don’t use mode M4, so they have a fixed palette and don’t use CRAM. Someone said also they could have a “<a href="https://www.smspower.org/Development/Mappers#KoreanMapper">Korean mapper</a>”. That explains many things (and that we should implement banking and the mappers, some day).</p><p>Ok, so I decided to implement banking now.<br>Starting with this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XYumWU2PeSoTi_2N_3JlUA.png" /></figure><p>Check the rom name. <br>Aaaand BOOM!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*6mCRCxp718xTkQfvhokysg.png" /></figure><p>Running Sonic was one of the milestones I was aiming for, and we made it. Of course it badly needs sound. Going further ingame, it badly needs x/y scrolling too:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*f24N1ChmCahtqi_mzaW0BA.png" /></figure><p>But look how cute he is!<br>(unfortunately, I am noticing what seems to be a screen blanking every 2 seconds or such in this game. I will investigate later)</p><p>The “new” banked games require a lot of more opcodes. And, at 570 implemented instructions, you start seeing this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*qvS4QVZIG7XjSqllSIPqmw.png" /></figure><p>Still sorry for the unimplemented tile flip.<br>Ok, implemented tile flipping. It was just like 4 lines of code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*GLxH6BBlyC3O9lRlSORcDA.png" /></figure><p>Spent the morning looking at the title screens of famous games in all their glory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*O5X8gTKCsuhH5KXemK-xCg.png" /></figure><p>But also:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*uffYd6IMmuWY6ugtw5ZTMQ.png" /></figure><p>But even:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*pf9DbRlw4KsQZdPIj-B36Q.png" /></figure><p>That’s the joy of emulation: staring at all the title screens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*WFQfUyWXV7wHBps2fIVoqQ.png" /></figure><p>It’s incredible how cute and finely crafted those title screens are. By the way, emulation is also a mean to discover graphics and sound that you would have missed if it wasn’t for emulation.</p><p>600 opcodes.<br>I feel I’ve come to a plateau. The motivation to continue with the development decreases, but I’ll try to stay strong. At the end, the final objective of this emulator is to emulate Sonic with a decent sound, and I’ll try to pursue it.</p><p>Implemented x/y background scrolling.<br>It somewhat works. Sonic scrolls nicely:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*A4GZ1uwTCO4qg8UQTQotOw.png" /></figure><p>I still have to understand how to deal with the leftmost column.</p><p>Oh, you noticed the truncated palms on the right. After fetching all the 28 screen map rows, here’s a better screenshot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*577xEAuSeZ_fXh-4Va-F-Q.png" /></figure><p>I urgently need a bigger visualization for my emulator. Now, since it started as a debugger, the screen looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2v_awFkHWQ-_FSbXuZzXsQ.png" /></figure><p>(yep, the emulated screen is that little thing on the right).</p><p>Ok, now we have a megascreen when the emulator is in running mode.</p><p>I think I have encountered a (another?) “showstopper” bug. The sprites in Final Bubble Bobble seems to flicker each 2 seconds or such. Oh gosh, this is daunting. Seems to be linked to the vcounter we update on each line, so, since I don’t have a line-based renderer yet, this could be linked to our renderer…?</p><p>Despite this, we have a good progress on some other roms, like for example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ATV1CSKLBpfXsJ1a6HW1oA.png" /></figure><p>Noticed that also have a new beautiful icon for our emulator?</p><p>I implemented the line interrupt register, and now this gorgeous beauty boots:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jTShZ9RZQYwGCSNnJ2LglA.png" /></figure><p>Ok, I’m fixing the tests for a bunch of opcodes.</p><p>Seems there is light at the end of the tunnel: one of the fellows on the Discord channel confirmed that Sonic switches the display on and off every frame, but between scanlines 194 and 220. So I have to implement the scanline renderer to see it working properly.</p><p>So, yes: next step will be the scanline renderer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efca94f65bc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 6: the hardest part]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-6-the-hardest-part-1e1d90bf70c5?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e1d90bf70c5</guid>
            <category><![CDATA[diary]]></category>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:20:58 GMT</pubDate>
            <atom:updated>2023-02-19T19:20:58.443Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 6: the hardest part</h3><p>At this point, the hardest part begins.<br>Writing each instruction of a Z80 core is not that hard; it’s mainly boring.</p><p>Understanding why a program does not run, or crashes, or gets stuck is the real challenge. And, at the current state, I have a lot of roms that don’t run, that crash or that get stuck. And we are at 335 wild opcodes.</p><p>One spare consideration: I noticed, while implementing the opcodes, that some pieces of the code of some roms often use a “family” of opcodes (0xfd, 0xcb, etc.). Just like the coders discovered that type of opcode and wanted to use them all.</p><p>Ok, back to work. <br>One of the most evident problems for the emulator (and maybe “easily” solvable) is that some roms show garbage onscreen for the background. For example “Spy vs Spy”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*_n6CAAlNlaZsPJYoI1-Reg.png" /></figure><p>Probably I can try to debug the “ColorCheck.sms” rom, that has a similar behaviour.</p><p>Ok, I was erroneously initializing the vram with a crescent pattern (not zeroes). Damn coders that don’t clean the vram.<br>Now it feels much better:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*Z9vYUqpJUc0oJ99_ihho_w.png" /></figure><p>Ok, 411 opcodes later, and still some roms stuck/crashing. Really, this is not exact. Most of the roms stopped complaining about unimplemented opcodes (well, 411 opcodes should be enough for any program), but instead stay stuck on a black screen.</p><p>Implemented the status flag interrupt bit, and some games that showed a black screen started working:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*DNno_IJussmiQyYDHUTnog.png" /></figure><p>(I obviously did not implement yet tile flipping, as you can see).</p><p>Finally, after 449 opcodes, one of the initial roms I debugged, the <a href="https://github.com/sverx/SMSTestSuite">SMS Test Suite</a>, boots:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*BxeAnDzVI5Nz_jjZUCKXRw.png" /></figure><p>Now it’s a good time to implement the remaining inputs (port AB).<br>After implementing just the “down arrow” (because I’m lazy), I can test several aspects of the emulator with this rom (video, audio in the future):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*BpNt8qAR98v6XzHwjB7TeQ.png" /></figure><p>(still you can notice some glitches/unflipped tiles).</p><p>It’s 00.00 and I’ve implemented 467 opcodes, but the Z80 wants more.</p><p>Some consideration on the state of the emulator at this point: first of all, even if the blit is at 60fps (59.922743), the emulation is slower than normal. My hypothesis on this problem is that, for my greater laziness, I did not emulate the variable cycle count for a bunch of instructions in the Z80 core. We’ll see if fixing this fixes the slowness.</p><p>Also, a consideration on the code: even if I planned the structure of Z80 core at the beginning, there is still a lot of duplicated code. Also, at this point I think that the DDCB and FFCB instructions should be treated with an unique function, because they seem completely specular.</p><p>Ok, I adjusted the missing clock cycles in some instructions, but that was not the reason of the slowness. I am just incrementing the H counter in the VDP like 10 times slower. I should see again the documentation on how many Z80 clocks each scanline uses. But setting this value to 311 (for now) seems a good improvement.</p><p>I implemented the remaining pad buttons, so I can start to play little games:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*1JclOy_-KAEnLAW7TzFasA.png" /></figure><p>I noticed only now that the sprites are halved. Will check on this later.</p><p>More info on the “black screen” roms: I noticed, switching to a fixed palette, that the roms are running, and executing the correct program, but the tiles are corrupt and the palette remains black if I don’t force it. Maybe there are errors in the opcodes, despite passing the tests? (answer: no. See later thoughts)</p><p>I’m also checking the number of cycles in the Z80 core, so I’m fixing a bunch of erroneous cycle counts. We are at 531 opcodes implemented.</p><p>Oh, and about the “Sub rescue” game before:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*HjA8692F7D9woWJ78RSJvg.png" /></figure><p>Now the sprites are correct. The sprites were 8x16, so the 2nd (bottom) half is the upper sprite tile index plus one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e1d90bf70c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 5: starting the real action]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-5-starting-the-real-action-3b0f232daab6?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b0f232daab6</guid>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[diary]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:19:59 GMT</pubDate>
            <atom:updated>2023-02-19T19:19:59.965Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 5: starting the real action</h3><p>So, at this point I have a more or less working debugger for the SMS. <br>It’s time to implement the “emulator” part (with batch execution of cycles, input handling and many other things) to create a real application.</p><p>At the moment, we are at 161 opcodes implemented and more or less tested.</p><p>(Zenga: the handcrafted emulator)</p><p>Btw, the fps counter in the “emulation” mode I just implemented shows a worrying 13fps. There will be some optimizations to do, but for now, I will stick with the infinite race with the opcode implementation (187 opcodes and counting).</p><p>213 opcodes and a nice title screen of “Teddy Boy” shows up:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QkbT7LAyhyQAijnjpY0YCw.png" /></figure><p>As you can see, there are still an infinite number of unimplemented opcodes, for example the quadruple byte opcode of the screenshot (ddcbxx6e). At this point, my mind starts thinking that the x86 opcodes were less in number, but that can’t really be, can it?</p><p>I think that emulator writing is like composing a giant puzzle: the picture is not complete unless you put down the latest piece. Even if some game can run without all the opcodes. So it’s not like a puzzle. It’s more like an incomplete program.</p><p>Ok, I understood why the fps are so low: I’m blitting the tiles using the canvas primitives for each pixel, so the performances are very poor. I will try to blit to a in-memory array and then blit all to the canvas it in a single operation.</p><p>At the end of the day (00:51) the Z80 CPU counts 263 opcodes implemented (maybe more), and unfortunately some roms started to crash (damn).</p><p>280 opcodes later, and the title screen of “Ghost House” is only waiting for me to press the start button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*KixVHLr9mZgH8EcFj6V3Fg.png" /></figure><p>At 303 opcodes, another game shows the title screen, and, surprisingly enough, invites me to press “space” or “return”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*VOJGCEq-5HHycpnEl-2Wug.png" /></figure><p>Probably, it refers to this accessory <a href="https://segaretro.org/Sega_Keyboard">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b0f232daab6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 4: stressing (me and) the CPU]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-4-stressing-me-and-the-cpu-8c3a016987b?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c3a016987b</guid>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[diary]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:19:12 GMT</pubDate>
            <atom:updated>2023-02-19T19:19:12.793Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 4: stressing (me and) the CPU</h3><p>Ok so we wrote a bunch of instructions in our Z80 core, but in the good propositions for the new year, we said that we would have unit tested the CPU with at least one CPU exerciser.</p><p>So I plugged in the Z80 cpu tests at <a href="https://github.com/raddad772/jsmoo/tree/main/misc/tests/GeneratedTests/z80">jsmoo</a>, and started testing. Even with so few opcodes (64 unprefixed opcodes implemented as of now), the CPU resulted quite bugged. And I fixed (almost) all the errors. I also opened a couple of <a href="https://github.com/raddad772/jsmoo/issues">issues</a> to jsmoo; I think that a couple of test cases are wrong (I was wrong, at the end).</p><p>So now, as I implement new opcodes, I can immediately test them with the press of a button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CU0BbkOHsm4KXPAxepVMqA.png" /></figure><p>(note the enthusiasm in my buttons).<br>But still, we’re at 83 opcodes implemented, and the SMSTestSuite rom is far from running. (…some opcodes later…) Now I think that the SMSTestSuite rom is waiting for a frame interrupt. We’ll have to implement this as well.</p><p>Around 120 opcodes implemented, and another demo works:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wFIqpx-_fBGKyMQCIjDk0g.png" /></figure><p>But with a wrong palette.<br>(noticed also there is a typo in the fps counter text).</p><p>More than 130 opcodes from the beginning, and the Hang-On title screen shows up:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*v0G0z5mbvjjN32oVKBMI7g.png" /></figure><p>(that lonely zero raises some suspects, but we’ll see when we go further in the code) (it was replaced by a correct double zero some more opcodes later).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c3a016987b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 3: the VDP (Video Display Problem)]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-3-the-vdp-video-display-problem-e8d715b3ec56?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8d715b3ec56</guid>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[diary]]></category>
            <category><![CDATA[emulation]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:18:20 GMT</pubDate>
            <atom:updated>2023-02-19T19:18:20.157Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 3: the VDP (Video Display Problem)</h3><p>After the deathly boredom of the Z80 instructions, in parallel I started building the VDP emulation code, just to have something onscreen as the construction of the CPU goes on.</p><p>The VDP is controlled by the ports 0xbf (control port) and 0xbe (data port). Then, the data written to the control port controls also the write/read address and the control code to change the meaning of writes to the data port.</p><p>It’s easier to see the code than to explain it.</p><p>(note: I’m just emulating “mode 4” of the VDP. There are 3 other modes used by some games that I’m not considering, at the moment)</p><p>Some more instructions, and a rough implementation of the background later, and the “<a href="https://www.smspower.org/Homebrew/OnlyWords-SMS">Only Words</a>” demo (which we can consider one of the simplest cartridges) is running:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oZ4XnlP_MSF_0R4Ayf624Q.png" /></figure><p>A nice progress for a few days of work.</p><p>The VDP has also a couple of registers (0x7e and 0x7f) that act as line and horizontal counter. We have to emulate the 1st one at this stage because some code gets stuck waiting for 0x7e to be equal to a certain scanline.</p><p>Anyway, the Z80 is infinite.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8d715b3ec56" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 2: tackling the CPU]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-2-tackling-the-cpu-3f6a14b6658?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f6a14b6658</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[emulation]]></category>
            <category><![CDATA[diary]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:17:09 GMT</pubDate>
            <atom:updated>2023-02-19T19:17:09.963Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 2: tackling the CPU</h3><p>The Z80 seems more complex than the 6502, but not excessively complex. Less complex than a 8086, anyway (note: at the end, the source code was longer than my 8086 core, at least if we compare javascript with Rust). <br>Anyway, it’s complex.</p><p>My previous emulators (except the 8086 one, that I rewrote a couple of times) had a big switch statement to execute the opcodes. With the Z80 this could be feasible, but this approach introduces a lot of duplicated code and it’s error-prone.</p><p>This time, I will use arrays of opcode functions, with the main traits of each opcode (cycles, if it’s undocumented, etc.) and a function that will implement the opcode if called. Those arrays will also contain the decoded instructions for the debugger.</p><p>In a couple of days, the debugger is up and running, and the Z80 core is starting to execute the instructions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RVaLyU4xld5BS_YMISOBnA.png" /></figure><p>As you can notice, the unimplemented instructions are shown as “UNK”. I’m using a test rom that is 32k in size, so for now we should not mess with banking and such. Ah, and the screenshot shows you the fantastic name I’ve come up for this piece of sofware: <strong>Zenga</strong>, the SMS emulator.<br>You easily understand to whom it’s related.</p><p>(Ok, explanation for the not Italian and not born in the 80s: Walter Zenga was the goalkeeper of the Italian football team, and he did a bunch of TV spots for the SMS and the Megadrive — see for example the one <a href="https://www.youtube.com/watch?v=1vupBMI4AZQ&amp;ab_channel=GamescollectionTV">here</a> — sorry, it’s in Italian)</p><p>Unfortunately, this part of the code is the most boring, since you have to implement all the opcodes (we are talking of at least hundreds of opcodes before seeing anything meaningful on the screen). Ok, it’s 00.24. Let’s implement the last opcode of the day (0x20, that is jr NZ,d).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f6a14b6658" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding a Sega Master System emulator — Chapter 1: planning it all]]></title>
            <link>https://medium.com/@frioltheoil/coding-a-sega-master-system-emulator-chapter-1-planning-it-all-9b49c75ff5cd?source=rss-4b33c178bdae------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b49c75ff5cd</guid>
            <category><![CDATA[diary]]></category>
            <category><![CDATA[sega]]></category>
            <category><![CDATA[emulation]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[friol]]></dc:creator>
            <pubDate>Sun, 19 Feb 2023 19:15:21 GMT</pubDate>
            <atom:updated>2023-02-19T19:15:21.759Z</atom:updated>
            <content:encoded><![CDATA[<h3>Coding a Sega Master System emulator — Chapter 1: planning it all</h3><p>So I decided to code an SMS (Sega Master System) emulator from scratch, in javascript (applause).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vcPIDm2LPayBmYdM4KqnEw.jpeg" /></figure><p>Why the SMS?</p><ol><li>Because it’s colorful (64 colours with linescroll tricks, graphics modes, a lot of tiles)</li><li>Because of the ’80s TV spots with <a href="https://en.wikipedia.org/wiki/Walter_Zenga">Walter Zenga</a></li><li>Because of Sonic and Outrun</li><li>Because… oh, come on, look at that design there. Isn’t that the cutest console in the universe?</li></ol><p>Javascript should be quite handy for this kind of application (I’ve done emulators in javascript before). Also, this time I’ll try to produce a Z80 core that is quite brief (note: I wrote this before looking at the multi-prefixed opcodes of Z80) (note2: with 560 implemented opcodes, the Z80 core source file is 7374 lines long and counting) (note3: the x86 CPU core I wrote in Rust was less than 5000 lines long — I should review my definition of ‘brief’), and try to test the single instructions as we go on (I’ve got a couple of test suites at hand). The code will be of course published on <a href="https://github.com/friol/zenga">GitHub </a>(as the other emulators).</p><p>The Z80 core will <strong>not </strong>be cycle-accurate. I think (I hope) 99% of the software can run without this degree of accuracy.</p><p>Wanted features: it should have a nice debugger, with a lot of functions, breakpoints, run/pause, a memory viewer. That is, more complex than my previous debuggers. Will see how far we get with this one.</p><p>So let’s go on with the main program structure.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b49c75ff5cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>