<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="feed/pretty-atom-feed.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>Ben&#39;s Web Space</title>
  <subtitle>A website where I as software developer and electronics enthusiast share experiences, insights, and passion for the ever-evolving world of technology.</subtitle>
  <link href="https://benjcal.space/feed.xml" rel="self" />
  <link href="https://benjcal.space/" />
  <updated>2024-11-18T00:00:00Z</updated>
  <id>https://benjcal.space/</id>
  <author>
    <name>Ben Calderon</name>
  </author>
  <entry>
    <title>Just Do Something Already</title>
    <link href="https://benjcal.space/blog/just-do-something-already/" />
    <updated>2024-11-18T00:00:00Z</updated>
    <id>https://benjcal.space/blog/just-do-something-already/</id>
    <content type="html">&lt;blockquote&gt;
&lt;p&gt;This is a repost of an article I originally published on Medium in November of 2020. I&#39;m moving it here to consolidate all my writing in one place.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://benjcal.space/blog/just-do-something-already/OmCDLfUEIE-512.avif 512w&quot;&gt;&lt;img loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://benjcal.space/blog/just-do-something-already/OmCDLfUEIE-512.webp&quot; alt=&quot;&quot; width=&quot;512&quot; height=&quot;347&quot;&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;Have you had an idea? A deep seated conviction that things could be better? That &lt;em&gt;you&lt;/em&gt; could bring about that change for the better?
But, on the other hand, has your idea been with you for years with nothing to show for it? Have you seen others bring about their ideas? Maybe you have even tried a few times to do the same but haven’t or couldn’t? Then I’m writing this for &lt;em&gt;you&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This is the story of an idea (&lt;a href=&quot;https://gradely.pages.dev/&quot;&gt;&lt;strong&gt;Gradely&lt;/strong&gt;&lt;/a&gt;, use &lt;code&gt;bali5more&lt;/code&gt; as a key to test). It is not a success story in which I tell you how you should do things just the way I did them and that success is “guaranteed.” It is not a failure story either in which I reflect on all the things I did wrong and what I learned from the experience. It is the story of the ongoing struggle of being stuck, and now finally doing something about the idea!&lt;/p&gt;
&lt;p&gt;You see, I believe that many of us have thought about doing something, but are paralyzed by a myriad of excuses and factors, thinking that we don’t have what it takes and just conforming to do nothing. And I want to convince you otherwise!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I have always thought that one man of &lt;strong&gt;tolerable abilities&lt;/strong&gt; may work great changes, and accomplish great affairs among mankind… -Benjamin Franklin&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;the-entrepreneurship-bug&quot;&gt;The entrepreneurship bug&lt;/h2&gt;
&lt;p&gt;I don’t know the exact circumstances that lead a person to believe that &lt;em&gt;they&lt;/em&gt; can bring about change, that &lt;em&gt;li’l ol’ me&lt;/em&gt; can do something of value, but I can share how it happened to me.&lt;/p&gt;
&lt;p&gt;In 2012, I started a masters degree in leadership/philosophy and got a work-study position for the department of distant education of my school. I had never worked before in tech but ever since I can remember I have been involved with it. From breaking my mom’s irons to see what was in there (…I didn’t know about screwdrivers back then!), to building a crystal radio and experiencing the magic of “the waves,” to going down the Linux train, to electronics and more. I got my hands on whatever books I could and tried all kinds of tech things even from my childhood.&lt;/p&gt;
&lt;p&gt;Thus, I had no &lt;em&gt;“official”&lt;/em&gt; qualifications for that job except what I had learned and the crazy notion that if I came across something I didn’t know, I could just learn it. Nowadays I just say that &lt;strong&gt;everything is &lt;em&gt;figureoutable&lt;/em&gt;.&lt;/strong&gt; But I digress.&lt;/p&gt;
&lt;p&gt;It was at that job that I heard how much the university was paying for a service. I was appalled! The software that they were paying for was open-source/free and that meant that they were pretty much just paying for servers! To my naive mind it sounded like I could provide those services, charge them half what they were paying and with 4–5 clients I would be set for life! And just like that I was bitten by the entrepreneurship bug and have been affected ever since.&lt;/p&gt;
&lt;p&gt;From there the idea narrowed down to a general &lt;em&gt;“I want to do something with technology for education”&lt;/em&gt; to &lt;em&gt;“I want to build a gradebook that doesn’t suck!”&lt;/em&gt; But it would be years before anything happened.&lt;/p&gt;
&lt;h2 id=&quot;and-then-what&quot;&gt;And then… what?&lt;/h2&gt;
&lt;p&gt;And then… I did nothing. Well, not &lt;em&gt;nothing&lt;/em&gt; nothing. I Googled about how to start a business… and was inundated by the overwhelming amount of information that I found. It wasn’t just the sheer volume but how all-over-the-place it was. There were stories of &lt;em&gt;successful&lt;/em&gt; people that had found success doing the complete opposite things! I heard that they way to go was with sponsors (and spent $500 on a course to learn more about it). I heard that the way to go was with an accelerator and VCs. I heard that they way to go was being bootstrapped. I heard that B2B was king, that B2C has more volume and market… I read and read and read, maybe 30+ books, listened to a bunch of podcasts, read blog posts, HN stories, etc. etc.&lt;/p&gt;
&lt;p&gt;But one thing was true during all of this: &lt;strong&gt;I never felt ready.&lt;/strong&gt; It seemed to me that the people doing these things, starting organizations and business, bringing about change, had it all together. I thought these people just knew what to do and did it.&lt;/p&gt;
&lt;p&gt;And here I was with an idea that I had tried to bring to fruition multiple times, that I had talked over with many educators and received good feedback, and yet… nothing. Here I was with &lt;em&gt;“tolerable abilities”&lt;/em&gt; to build said idea and still I had done nothing, still felt unprepared, still felt &lt;em&gt;unworthy&lt;/em&gt; if you may. How dare I think I could bring about change?! How dare I think people would listen to me?! And for 8 years I was stuck in that quicksand.&lt;/p&gt;
&lt;p&gt;And then it hit me. I could spend the next eight years in that quick sand, with the dreaded “what if” question festering in my mind, or I could &lt;em&gt;just do something already!&lt;/em&gt; And it was that question that revealed the culprit that had been holding me back. It was not a lack of technical skills, or the lack of some intrinsic personality trait, or some genetically bestowed advantage. No, the culprit was fear, plain raw simple old fear. Fear that whatever I do won’t matter. Fear that the people I wanted to serve would reject me. Fear that all I could do was spend another eight years doing what I had done - nothing - and in the end accomplish nothing. Fear fear fear…&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Oh, such an old enemy, but how well it still fools us! How well it masquerades itself and blames &lt;em&gt;us&lt;/em&gt; for its deficiencies!&lt;/p&gt;
&lt;p&gt;When I finally realized all the fear I had and started looking around and listening, I was surprised by what I heard. Everybody else was also afraid!! My boss was afraid, the CEO of the company was afraid, the entrepreneur that I so admired was also afraid!&lt;/p&gt;
&lt;p&gt;But you know what? It is those that tell their fears to shut up who are the ones that bring about change! The ones who take their ideas and make them a reality and make the world, even if only a little, a better world!&lt;/p&gt;
&lt;p&gt;And here you are reading my feeble attempt to tell my feats to shut up. My attempt at believing that &lt;em&gt;li’l ol’ me&lt;/em&gt; can bring about a change, can make a difference in the lives of those I want to serve. That &lt;em&gt;I&lt;/em&gt; can make the world, even if only a little, a better world.&lt;/p&gt;
&lt;p&gt;And it is my strong belief that &lt;em&gt;you&lt;/em&gt; can too! I don’t know your circumstances, your fears, your challenges, but I do believe that you can overcome them as those before you have done. I believe in you and I want you to believe in you, too!&lt;/p&gt;
&lt;p&gt;And then there’s this quote by Teddy Roosevelt…&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“It is not the critic who counts; not the man who points out how the strong man stumbles, or where the doer of deeds could have done them better. The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood; who strives valiantly; who errs, who comes short again and again, because there is no effort without error and shortcoming; but who does actually strive to do the deeds; who knows great enthusiasms, the great devotions; who spends himself in a worthy cause; who at the best knows in the end the triumph of high achievement, and who at the worst, if he fails, at least fails while daring greatly, so that his place shall never be with those cold and timid souls who neither know victory nor defeat.”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;out-there&quot;&gt;Out there&lt;/h2&gt;
&lt;p&gt;So this is the beginning of that journey, of &lt;em&gt;“just doing something about it!”,&lt;/em&gt; my attempt to put myself out there. What will come of it is still to be seen, but I can tell you this: &lt;em&gt;“out there”&lt;/em&gt; the sun is shining! &lt;em&gt;“Out there”&lt;/em&gt; the possibilities are endless! &lt;em&gt;“Out there”&lt;/em&gt; is still full of dangers and risks, but you can overcome them! &lt;em&gt;“Out there”&lt;/em&gt; the world is waiting for you!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Binary Ninja! 🔪🍉 - CSS Edition</title>
    <link href="https://benjcal.space/blog/binary-ninja-css-edition/" />
    <updated>2023-08-28T00:00:00Z</updated>
    <id>https://benjcal.space/blog/binary-ninja-css-edition/</id>
    <content type="html">&lt;h3 id=&quot;binary-ninja&quot;&gt;🥷 Binary Ninja!&lt;/h3&gt;
&lt;p&gt;When learning a new programming language there&#39;s usually a section that I look at, expressly to &lt;em&gt;not&lt;/em&gt; to use! You see, for whatever reason most languages would ship with a &lt;em&gt;real&lt;/em&gt; &amp;quot;AND&amp;quot;, and then with &lt;em&gt;another&lt;/em&gt; &amp;quot;AND&amp;quot;, just confuse me! It&#39;d be like this in JS, for example:&lt;/p&gt;
&lt;pre class=&quot;language-js&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// &quot;real&quot; AND&lt;/span&gt;
&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then there&#39;s this... this &lt;em&gt;thing&lt;/em&gt;!&lt;/p&gt;
&lt;pre class=&quot;language-js&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// what in the world!?&lt;/span&gt;
&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Same thing with OR &lt;code&gt;|| vs. |&lt;/code&gt; and with NOT &lt;code&gt;! vs. ~&lt;/code&gt; and a few others.&lt;/p&gt;
&lt;p&gt;Well, it seems like some wise people called those bit&lt;em&gt;wise&lt;/em&gt; operator, and for the longest time I&#39;ve intentionally avoided them, and got scared when I came across code that used them! Well, on the &lt;a href=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2&quot;&gt;CHIP8 project&lt;/a&gt; I had no choice but to face my fears and in this post I want to share with you how cool bitwise operator really are! My goal is not to be exhaustive and tell you everything about them, your programming language manual would be the place for that. Instead I want to share with you a use case and hopefully encourage you to find out more and add them to your programming toolbox!&lt;/p&gt;
&lt;p&gt;Ok, buckle up and let&#39;s ge to it!&lt;/p&gt;
&lt;p&gt;And it all starts with the name.&lt;/p&gt;
&lt;h3 id=&quot;bitwise-some-wise-person-once-said-about-a-bit-maybe-sorry&quot;&gt;Bitwise, some wise person once said about a bit... maybe. (sorry) 🤦‍♂️&lt;/h3&gt;
&lt;p&gt;Paying close attention to the name of these operators we get the biggest clue we need. Bit! The bitwise operators, curiously enough, operate on bits.&lt;/p&gt;
&lt;p&gt;Back in the day, before every developer everywhere worked solely inside an &lt;code&gt;App.jsx&lt;/code&gt; file, people used to think about how the data inside the computer looked like. You see, computers are not too clever. They only know how to say two things: &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt;. That is all. When you put a bunch of those together and assign meaning to them you can fool yourself into thinking that computers are clever. Nah, people are, but people got tired of being clever and thus made computers to pretend to be instead of them.&lt;/p&gt;
&lt;p&gt;Anyways, it is 2023, so let&#39;s get on with the times and do what everybody is doing nowadays, making websites pretty! 🎨&lt;/p&gt;
&lt;p&gt;Imagine that we have the color &lt;a href=&quot;https://www.w3schools.com/colors/color_tryit.asp?color=OliveDrab&quot;&gt;OliveDrab&lt;/a&gt; (not to be confused with a drab olive, which might be moldy and thus not recommended for human consumption). That color in Hex is &lt;code&gt;0x6B8E23&lt;/code&gt;, of you might see it as &lt;code&gt;#6B8E23&lt;/code&gt; in a CSS file, same thing. Well, from our frontend dev knowledge we know that that number is actually three numbers that represent red, green, and blue values. And from a quick glance we can see that red would be &lt;code&gt;0x6B&lt;/code&gt;, green &lt;code&gt;0x8E&lt;/code&gt;, and blue &lt;code&gt;0x23&lt;/code&gt;. Well, that&#39;s easy, but that&#39;s because you are not a computer! The computer only sees &lt;code&gt;0110 1011 1000 1110 0010 0011&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So anyways, all the computer sees is &lt;code&gt;0x6B8E23&lt;/code&gt; as one number, which in decimal is the number &lt;code&gt;7048739&lt;/code&gt; or the binary &lt;code&gt;0110 1011 1000 1110 0010 0011&lt;/code&gt; without any notion that there actually three pieces of data in that one number.&lt;/p&gt;
&lt;p&gt;Now, we have received the task to convert all the hex numbers in our CSS files to rgb values (I know is hard to imagine such a ridiculous and meaningless task, but have you ever worked in tech?). Well, that&#39;s where our bitwise operators come to help us!&lt;/p&gt;
&lt;p&gt;Let&#39;s start with the &lt;em&gt;wrong&lt;/em&gt; &amp;quot;AND&amp;quot; &lt;code&gt;&amp;amp;&lt;/code&gt;. This, obviously, can be used to perform the logic AND function on two number, but is can also be used to mask bits (The term &amp;quot;mask&amp;quot; confused the heck out of me for many years! Hang on and hopefully after this it might make sense). By making we mean that it can let some bits &amp;quot;pass&amp;quot; and some others don&#39;t. Another way to think about it is, it can &amp;quot;hide&amp;quot; bits we don&#39;t care about.&lt;/p&gt;
&lt;p&gt;Let&#39;s give it a try!&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token number&quot;&gt;0x6B8E23&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x0000FF&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0x23&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Would you look at that! We where able to get rid of all the pesky red and green and now we have the value of blue by itself. If you run this code you might see the value &lt;code&gt;35&lt;/code&gt;, which is the value of &lt;code&gt;0x23&lt;/code&gt; in decimal.&lt;/p&gt;
&lt;p&gt;Very nifty, but what is happening? Why &lt;code&gt;0xFF&lt;/code&gt;? Well, let&#39;s look at the... ready? Bits!&lt;/p&gt;
&lt;pre class=&quot;language-js&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;  &lt;span class=&quot;token number&quot;&gt;0110&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1011&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1110&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0010&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0011&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0x6B8E23&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1111&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1111&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0xFF&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0010&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0011&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Uff, that&#39;s a bunch of zeros and ones! But notice what is happening, when we &lt;code&gt;&amp;amp;&lt;/code&gt; the original number with our &amp;quot;mask&amp;quot;, all the places where &lt;em&gt;both&lt;/em&gt; the original &lt;code&gt;1&lt;/code&gt; and our mask are &lt;code&gt;1&lt;/code&gt; result in &lt;code&gt;1&lt;/code&gt;. In the places where our mask is &lt;code&gt;0&lt;/code&gt; the result is &lt;code&gt;0&lt;/code&gt; regardless.&lt;/p&gt;
&lt;p&gt;It&#39;s like multiplying! Which is exactly right! Bitwise AND can be &lt;em&gt;sort of&lt;/em&gt; thought of as multiplication in boolean algebra, while bitwise OR can be thought as addition.&lt;/p&gt;
&lt;p&gt;As you might have noticed, writing number in binary is tending towards the painful side of things. Let&#39;s start using the hexadecimal number system, or hex for short to both, save some typing and possible errors. A hex number let&#39;s us represent each group of four bits (a nibble), with one number/letter between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;F&lt;/code&gt;. So, from our color above you can see how each nibble (4 bits) represent one number/letter on the hex number (&lt;code&gt;3 == 0011&lt;/code&gt;, &lt;code&gt;2 == 0010&lt;/code&gt;, &lt;code&gt;E == 1110&lt;/code&gt;, and so on).&lt;/p&gt;
&lt;p&gt;Awesome, let&#39;s keep going, how about getting the green value?&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token number&quot;&gt;0x6B8E23&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xFF00&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0x8E00&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Well, that&#39;s progress, we are able to get &lt;code&gt;8E&lt;/code&gt; alone, but there are two extra &lt;code&gt;00&lt;/code&gt; there. How do we get rid of them? This is the Ninja part!&lt;/p&gt;
&lt;h3 id=&quot;shift-like-you-are-driving-manual&quot;&gt;Shift like you are driving manual! 🏎️&lt;/h3&gt;
&lt;p&gt;Among the bitwise operators there are these curious looking &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; operators. Initially what they do might seem obscure. I mean with a description like this from &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Left_shift&quot;&gt;mdn web docs&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The left shift (&amp;lt;&amp;lt;) operator returns a number or BigInt whose binary representation is the first operand shifted by the specified number of bits to the left.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It&#39;s accurate, terse, and initially left me like, &lt;em&gt;wat&lt;/em&gt;?&lt;/p&gt;
&lt;p&gt;But let&#39;s look what it does, maybe we can spot a pattern or something.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0000 0001&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0000 0010&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0000 0100&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0000 1000&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0001 0000&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0010 0000&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Did you see that? All that that operator is doing is shifting to the left the &lt;code&gt;1&lt;/code&gt; on the left by the number on the right. Not bad!&lt;/p&gt;
&lt;p&gt;As you might have guessed we can shift something else than &lt;code&gt;1&lt;/code&gt;, say, &lt;code&gt;6&lt;/code&gt; (0110) and it would shift that whole number too.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0000 0110&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0000 1100&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0001 1000&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0011 0000&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0110 0000&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 1100 0000&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And we can do it reverse too!&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token number&quot;&gt;0010&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0001 0000&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;0010&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0000 1000&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;0010&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0000&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0000 0100&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With this now we know how to get rid of the extra 0s in &lt;code&gt;0x8E00&lt;/code&gt;!&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token number&quot;&gt;0x8E00&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0x2380&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Wait, what? (I spent and embarrassingly amount of time trying to figure this one out...)&lt;/p&gt;
&lt;p&gt;Remember the nibbles up there? how four bits represent one digit in hex? That means that to get rid of one &lt;code&gt;0&lt;/code&gt; in hex we need to get rid of 4 bits. And thus, to get rid of a whole nibbles we&#39;d need to shift in multiples of 4.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token number&quot;&gt;0x8E00&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0x8E0 --- closer!&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;0x8E00&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0x8E  --- let&#39;s go!!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok, let&#39;s recap super quick where are we, we can use &lt;code&gt;&amp;amp;&lt;/code&gt; to mask out the stuff that we don&#39;t want from a number, and then we can shift away the number that we don&#39;t want. Thus in order to get the green value out of &lt;code&gt;0x6B8E23&lt;/code&gt; we would do:&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x6B8E23&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x00FF00&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0x8E&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x6B8E23&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xFF0000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// =&gt; 0x6B&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&#39;s neat! Now we can very easily get our values for our hex CSS ro rgb function&lt;/p&gt;
&lt;pre class=&quot;language-js&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; hexColor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x6b8e23&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x6b8e23&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0xff0000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x6b8e23&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x00ff00&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0x6b8e23&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x0000ff&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;the rbg value is rgb(&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;r&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;g&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;b&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// the rbg value is rgb(107, 142, 35)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et Voilà!&lt;/p&gt;
&lt;p&gt;There&#39;s definitively a lot more to binary an bitwise operators, we just learned a little bit (or a nibble... I&#39;m so sorry) of the chopping part, we can stick them together (imagine another ridiculous task to convert all the rgb values to hex 🤦‍♂️), we can... well, do more, but chopping and sticking stuff is enough for a &lt;a href=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2&quot;&gt;CHIP8 emulator&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;See you on the next time!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Because I can&#39;t ever remember SDL</title>
    <link href="https://benjcal.space/blog/because-I-cant-ever-remember-sdl/" />
    <updated>2023-08-09T00:00:00Z</updated>
    <id>https://benjcal.space/blog/because-I-cant-ever-remember-sdl/</id>
    <content type="html">&lt;p&gt;From time to time I want to make a quick &#39;n dirty SDL window to play with some graphic something. The current curiosity is learning 3D rendering &lt;a href=&quot;https://machinethink.net/blog/3d-rendering-without-shaders/&quot;&gt;&amp;quot;by hand&amp;quot;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Well, every single time I need to do this I find myself having to research SDL and relearn it all over again 🤦‍♂️. I know I need to init sdl and then... create a window, right? a renderer or a surface? how was it that I handled events? something double loop... I think? Or did I just waited for events?&lt;/p&gt;
&lt;p&gt;Another pet peeve that I have with SDL is that it can be a bit verbose. Nothing against that though, the flexibility and control that that verbosity provides makes it worth it for me, something that often makes C libraries superior IMHO. But with that said, I like how tidy &lt;a href=&quot;https://github.com/raysan5/raylib/blob/master/examples/core/core_basic_window.c&quot;&gt;raylib&lt;/a&gt; is even though it can be limiting is some cases.&lt;/p&gt;
&lt;p&gt;And thus, after so much pain and searching for the same things over and over again, I finally decided to write a single header wrapper around SDL called &lt;a href=&quot;https://gist.github.com/benjcal/60bbbaf297a4a3be5cb3464832365eea&quot;&gt;sdlsimple.h&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Here&#39;s how it works:&lt;/p&gt;
&lt;p&gt;First, include the header in the project&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;token macro-name&quot;&gt;SDLSIMPLE_IMPLEMENTATION&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// use this only once in the your project&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;sdlsimple.h&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next initialize it.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;sdlsimple_context_t&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;ctx &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;malloc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;sdlsimple_context_t&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sdlsimple_init&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; WIDTH&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; HEIGHT&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All the magic then happens between in your main loop between &lt;code&gt;sdlsimple_start(ctx)&lt;/code&gt; and &lt;code&gt;sdlsimple_end(ctx)&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sdlsimple_should_quit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctx&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;sdlsimple_start&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctx&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;sdlsimple_clear&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// draw red line at y = 100&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; WIDTH&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
			&lt;span class=&quot;token function&quot;&gt;sdlsimple_set_pixel&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctx&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;sdlsimple_end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ctx&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And that is that. On the background sdlsimple uses an &lt;code&gt;SDL_Renderer&lt;/code&gt;, instead of a surface, to draw and the events are not blocking, that is, we are not waiting for events but drawing as fast as our computer let&#39;s us.&lt;/p&gt;
&lt;p&gt;I guess I should say that this is not a production ready library. It uses the OS as garbage collector (100% free of &lt;code&gt;free()&lt;/code&gt;!), there&#39;s very little error checking and all the rest of the stuff needed to make software reliable.&lt;/p&gt;
&lt;p&gt;But at the end of the day, done is better than perfect and this is enough to move on and actually start drawing pixels to my window.&lt;/p&gt;
&lt;p&gt;Until next time!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>The Adventures of Writing a CHIP8 Emulator - Part 2</title>
    <link href="https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/" />
    <updated>2023-06-11T00:00:00Z</updated>
    <id>https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/</id>
    <content type="html">&lt;p&gt;In my last installment of &lt;em&gt;&amp;quot;How to continually stub your toe with C&amp;quot;&lt;/em&gt;... Uhhh, I mean, &lt;a href=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-1&quot;&gt;The Adventures of Writing a CHIP8 Emulator - Part 1&lt;/a&gt; I took you on my journey of writing a CHIP8 Emulator in C. BTW, here&#39;s &lt;a href=&quot;https://git.sr.ht/~benjcal/chip8/tree&quot;&gt;the code&lt;/a&gt; and a gif of how it looks :-)&lt;/p&gt;
&lt;img loading=&quot;lazy&quot; decoding=&quot;async&quot; alt=&quot;chip8 emu&quot; src=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/rHlCEI6vj5-960.webp&quot; width=&quot;960&quot; height=&quot;740&quot;&gt;
&lt;p&gt;Well, I&#39;m now afraid that I might have given you an impression of competency. If that&#39;s so, I need to profusely apologize and quickly correct your assumption by pulling the curtain back from that &amp;quot;polished&amp;quot; blog post. The goal with this is simple, if my pain can help you in any way avoid my errors, then my sufferings are not in vain!&lt;/p&gt;
&lt;h3 id=&quot;why-clever-code-bad-code&quot;&gt;Why clever code == bad code! 🤓🤦‍♂️&lt;/h3&gt;
&lt;p&gt;One of the first ROMs traditionally used to test a CHIP8 emulator is an old ROM that displays an IBM logo. Here is how that looks rendering at 10Hz:&lt;/p&gt;
&lt;img loading=&quot;lazy&quot; decoding=&quot;async&quot; alt=&quot;ibm logo&quot; src=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/uKuisHHF7q-960.webp&quot; width=&quot;960&quot; height=&quot;545&quot;&gt;
&lt;p&gt;Yep, CHIP8 is that old! Anyways, the goal was very clear, I only needed to implement a handful of instructions (opcodes) to run that ROM and so I did. I then loaded the ROM and... nothing happened! I looked at my code again, everything looked correct! still, nothing. The output of the CHIP8 is a 64x32 display, so I dumped that to the terminal to see if anything was displaying, nothing. I added &lt;code&gt;printf&lt;/code&gt; everywhere, added a &lt;code&gt;scanf()&lt;/code&gt; to step through every instruction, looked at the registers after every instruction, but still nothing. The registers were updating properly, the PC (program counter register) was incrementing correctly, it all was working properly but for some reason the emulator would not work.&lt;/p&gt;
&lt;p&gt;I decided to blame the ROM and to look for other beginner ROM to test on my emulator with. After a quick search I came across &lt;a href=&quot;https://github.com/Timendus/chip8-test-suite#chip-8-splash-screen&quot;&gt;this one&lt;/a&gt; which, when I loaded it on my emulator... worked! But you see, that&#39;s terrible news! It showed me that my emulator worked, but only sometimes!? Only for some ROMs!?
It&#39;s like if you order your favorite Chipotle burrito, asking for the exact same thing every single time, but you get what you asked for &lt;em&gt;only&lt;/em&gt; if you pay with $1 bills on Tuesdays!
Both ROMs were very similar in the instructions they required to operate, so why was it working only on one!?&lt;/p&gt;
&lt;p&gt;Well, I proceeded to try everything I could think of. I talked softly and lovingly to my computer... nothing. I talked loudly and threateningly... nothing. I tried to make bargains with my computer... nothing. I changed my compiler flags... nothing. I changed my wallpaper... nothing. Asked my rubber ducky for advice... nothing. Asked ChatGPT for advice... nothing. I bought Sublime Text 4... nothing (in retrospect, Idk how this could have helped, but I still tried it!)&lt;/p&gt;
&lt;p&gt;After about two days of no progress at all I came to the conclusion that I would have to take things into my own hands. I&#39;d need to write a debugger for my emulator **dun dun DUN**.&lt;/p&gt;
&lt;p&gt;So I did. It was a whole three days detour, which was absolutely fascinating and SO worth it! (But that&#39;s a topic for another post.) It was such a success that in the first two minutes of running the half-cooked debugger I was able to see the issue! Mind you, not the cause, but the problem.&lt;/p&gt;
&lt;p&gt;Take a look at this screenshot of my debugger showing the bytes currently loaded in memory:&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/gg5DhRPxKh-710.avif 710w&quot;&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/gg5DhRPxKh-710.webp 710w&quot;&gt;&lt;img loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/gg5DhRPxKh-710.png&quot; alt=&quot;memory dump&quot; width=&quot;710&quot; height=&quot;343&quot;&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;CHIP8 Emu Debugger Memory Window&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And now take a look at this hexdump (&lt;a href=&quot;https://cutter.re/&quot;&gt;Cutter&lt;/a&gt;) of the ROM I&#39;m trying to load to memory:&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/Q5q8sXTTha-702.avif 702w&quot;&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/Q5q8sXTTha-702.webp 702w&quot;&gt;&lt;img loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/Q5q8sXTTha-702.png&quot; alt=&quot;cutter hexdump&quot; width=&quot;702&quot; height=&quot;416&quot;&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Cutter hexdump of IBM.ch8&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Do you see the difference? Look, I might not pay attention to details, like the milk I picked up at the grocery store, and just pick up the &amp;quot;red box with cursive letters&amp;quot; only to realize that it&#39;s the milk I don&#39;t like (true story!)... But I&#39;ve spent A LOT of time staring at the back of breakfast cereal boxes, so, I&#39;ve gotten pretty good at &amp;quot;spot the difference&amp;quot; games, if I say so myself... and, my emulator is missing data! What gives!? And the annoying part is that it only happened with the &lt;a href=&quot;https://github.com/Timendus/chip8-test-suite#ibm-logo&quot;&gt;IBM ROM&lt;/a&gt;! 🤦‍♂️&lt;/p&gt;
&lt;p&gt;Well, at least now I know &lt;em&gt;how&lt;/em&gt; my emulator doesn&#39;t work... it&#39;s not reading the whole file! Let&#39;s take a look at the function that loads the ROM file to memory:&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;chip8_load_rom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Chip8 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;chip8&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;uint16_t&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    FILE &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;f &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fopen&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;path&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;r&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// open path as readonly&lt;/span&gt;

    &lt;span class=&quot;token class-name&quot;&gt;int8_t&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// temporary byte to store read data&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint16_t&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; offset&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// data is usually loaded with a 0x200 offset&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fgetc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;EOF&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        chip8&lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt;memory&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a very simple function. Read one byte from the file, set memory to byte. Repeat until End of File (EOF). See, there&#39;s nothing wrong with it! 🤦‍♂️ I mean, it worked for some files but it didn&#39;t for others? People complain about segmentation faults, but those are preferable to this!&lt;/p&gt;
&lt;p&gt;So I decided to take a closer look at the file that my emulator was having trouble with, and you&#39;ll &lt;a href=&quot;https://youtu.be/dQw4w9WgXcQ&quot;&gt;never believe what I found&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;Take a look above and notice what is the very next byte missing in memory, or where reading the file stopped. On the emulator memory the lasts bytes are &lt;code&gt;0x1228&lt;/code&gt; and nothing else is read after that. In the ROM the very next byte after that is &lt;code&gt;0xFF&lt;/code&gt;... Well, that is interesting... Why would my program stop reading when it finds &lt;code&gt;0xFF&lt;/code&gt;? To confirm this theory I changed that byte with &lt;code&gt;0xFE&lt;/code&gt; and yep, the emulator read &lt;code&gt;0xFE&lt;/code&gt; without a problem, all the way to the next &lt;code&gt;0xFF&lt;/code&gt;! Well well well, the plot thickens!&lt;/p&gt;
&lt;p&gt;I then decided to take a look at &lt;code&gt;getc()&lt;/code&gt; to see if there was something I was missing. And interestingly enough, take a look at what the signature for that function is:&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getc&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt; FILE &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;stream &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Did you notice it? it returns an integer, not a character! The function is called &lt;code&gt;getc&lt;/code&gt;, presumably for &amp;quot;get character&amp;quot; but it returns an integer... interesting 🤔. So, on a whim I decided to change my &lt;code&gt;b&lt;/code&gt; variable above to and &lt;code&gt;int&lt;/code&gt;, and BOOM!, it worked!&lt;/p&gt;
&lt;p&gt;Now, why would that be?&lt;/p&gt;
&lt;p&gt;Take a look at the problematic byte one more time in &lt;a href=&quot;https://imhex.werwolv.net/&quot;&gt;ImHex&lt;/a&gt;, which conveniently shows us the value of a byte in various data types:&lt;/p&gt;
&lt;p&gt;&lt;picture&gt;&lt;source type=&quot;image/avif&quot; srcset=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/YymjhPoL-M-779.avif 779w&quot;&gt;&lt;source type=&quot;image/webp&quot; srcset=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/YymjhPoL-M-779.webp 779w&quot;&gt;&lt;img loading=&quot;lazy&quot; decoding=&quot;async&quot; src=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-2/YymjhPoL-M-779.png&quot; alt=&quot;imhex&quot; width=&quot;779&quot; height=&quot;380&quot;&gt;&lt;/picture&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;ImHex&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Did you see it? At the right side, in the data inspector, notice what &lt;code&gt;0xFF&lt;/code&gt; is as an &lt;code&gt;int8_t&lt;/code&gt;... it is &lt;code&gt;-1&lt;/code&gt;! And why would that be relevant? Because &lt;code&gt;EOF == -1&lt;/code&gt;! 🤦‍♂️&lt;/p&gt;
&lt;p&gt;You see, I had some esoteric idea that &lt;code&gt;EOF&lt;/code&gt; was some magical signal or something that the OS sent my program when the filesystem detected that I had reached the end of a file. NOPE. It&#39;s just a plain &lt;code&gt;-1&lt;/code&gt; (it might be something else in different platforms, that&#39;s why &lt;code&gt;EOF&lt;/code&gt; is used instead of &lt;code&gt;-1&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;So, this is what was happening, my function would go on merrily, reading my file to memory, one byte at a time. It would come to a byte &lt;code&gt;0xFF&lt;/code&gt; and be like &lt;em&gt;&amp;quot;La la la la... oh, what have we here? A &lt;code&gt;0xFF&lt;/code&gt;? This is obviously &lt;code&gt;EOF&lt;/code&gt; and thus I&#39;m done here and can go back to picking up berries in the forest for Granny! La la la la...&amp;quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Well, while I don&#39;t mind C programs wandering off trails to gather questionable berries for their ancestors in the woods, that is most certainly NOT what I wanted my program to do. But by trying to be clever and setting my variable to be &lt;em&gt;exactly&lt;/em&gt; one byte &lt;code&gt;int8_t&lt;/code&gt;, &lt;strong&gt;I created that bug&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And that&#39;s how, a full 5 days of work and... let me check... ~600 LOC led to changing &lt;code&gt;int8_t&lt;/code&gt; to &lt;code&gt;int&lt;/code&gt;!! 😅&lt;/p&gt;
&lt;p&gt;So, what is the moral of the story? To not be clever and try to save some imaginary space? (My computer is 64 bit, so a 64 bit operation or a 8 bit operation take the exact same space and time...) To not optimize early, and instead get my program working and look for optimizations later? Are they even necessary? That being sidetracked and spending 5 days in a pet project actually pays off? Well... I don&#39;t know what the moral is, but I learned nothing and made the variable a &lt;code&gt;int16_t&lt;/code&gt;! 😂&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>The Adventures of Writing a CHIP8 Emulator - Part 1</title>
    <link href="https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-1/" />
    <updated>2023-06-04T00:00:00Z</updated>
    <id>https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-1/</id>
    <content type="html">&lt;p&gt;A few days ago I decided to write a CHIP8 emulator. Why? Because it&#39;s &lt;a href=&quot;https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/full-text/book/book-Z-H-3.html&quot;&gt;fun&lt;/a&gt;! As for a programming language, I decided to use C. Wait a minute, isn&#39;t that illegal? This might surprise you but, it is not! You are not forced to use Rust for all new projects henceforth! Seriously though, in the past couple of months I decided to actually learn C and it has quickly become my &lt;a href=&quot;https://news.ycombinator.com/item?id=35468376&quot;&gt;absolute favorite language&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Another big reason for writing a CHIP8 emulator is because I truly enjoy &lt;em&gt;really&lt;/em&gt; understanding something deeply. With today&#39;s crazy complexity in software (I&#39;d argue mostly unnecessary, but I digress), tons of levels of indirections, abstractions, solving most problems by adding &lt;em&gt;Yet Another NPM Package®&lt;/em&gt;, and so on, emulating a system like CHIP8 with a language like C feels like a breath of fresh air.&lt;/p&gt;
&lt;p&gt;Let&#39;s set our expectations, I&#39;m not going to tell you how to write a CHIP8 emulator, or what to put on your pizza (!pineapple). It&#39;d be like spoiling a really good book, or telling you the clue to solving an interesting puzzle, or telling you how to get the Master Sword in TOTK without letting you figure it out! If you ask me, I think you &lt;strong&gt;should&lt;/strong&gt; write a CHIP8 emulator too, and have tons of fun doing it! What I plan to do is more like take you along a leisurely walk through my journey, show you the parts that I thought were particularly interesting, while encouraging you to look at the landscape for yourself, dig in and come up with your own favorite parts!&lt;/p&gt;
&lt;p&gt;And thus, armed with &lt;a href=&quot;https://tobiasvl.github.io/blog/write-a-chip-8-emulator/&quot;&gt;some&lt;/a&gt; &lt;a href=&quot;http://devernay.free.fr/hacks/chip8/C8TECH10.HTM&quot;&gt;amazing&lt;/a&gt; &lt;a href=&quot;https://github.com/Timendus/chip8-test-suite&quot;&gt;resources&lt;/a&gt;, I fired up &lt;a href=&quot;https://youtu.be/X34ZmkeZDos&quot;&gt;Microsoft Word&lt;/a&gt;, made a &lt;code&gt;main.c&lt;/code&gt; file, and with &lt;code&gt;gcc main.c -o chip8&lt;/code&gt; I was good to go.&lt;/p&gt;
&lt;p&gt;When I started this project I had a foggy idea of how a CPU works from reading a little about &lt;a href=&quot;https://github.com/hackclub/some-assembly-required&quot;&gt;assembly&lt;/a&gt;, or for that matter, how a compiler/interpreter sorta kinda works from reading another &lt;a href=&quot;https://craftinginterpreters.com/&quot;&gt;interesting book&lt;/a&gt; but here&#39;s the TLDR;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CPU reads instruction, also called opcode, from memory (fetch step)&lt;/li&gt;
&lt;li&gt;CPU interprets the instruction (decode step)&lt;/li&gt;
&lt;li&gt;CPU does what the instruction says (execute step)&lt;/li&gt;
&lt;li&gt;Rinse and repeat (until it crashes, of course, and you contact IT and they tell you to turn it off and on again...)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Sure, it&#39;s a bit more complicated than that, but that&#39;s enough to get one started.&lt;/p&gt;
&lt;h3 id=&quot;fetch-or-the-thing-you-wish-your-cat-did&quot;&gt;Fetch, or the thing you wish your cat did!&lt;/h3&gt;
&lt;p&gt;Let&#39;s break down those steps a bit more. First the CPU&lt;sup&gt;&lt;a href=&quot;https://benjcal.space/blog/the-adventures-of-writing-a-chip8-emulator-part-1/#1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; fetches an instruction from memory. But, how does the CPU know which instruction to read? Good question! Most CPUs have a register in it called Program Counter (PC) that keeps track of the memory location that the CPU should read the next instruction from. In the case of CHIP8 the PC generally has an initial value of &lt;code&gt;0x200&lt;/code&gt;. That is, when you first turn on the CHIP8 it will read &lt;code&gt;0x200&lt;/code&gt; from memory and do what that instruction says.&lt;/p&gt;
&lt;p&gt;Whoa there, hold your horses! Register what!? (that was my question!) A register is simply a very small memory inside the CPU that the CPU uses to store data. For example if I&#39;m going to add two numbers, the CPU needs to have access to the two numbers and then it also needs some place to put the results of adding those numbers. That&#39;s what the registers are used for. Btw, if you have heard about 32 bit, 64 bit, or even 16 bit CPUs, that&#39;s one of the things that it means, how many bits fit in its registers. CHIP8, if the name hasn&#39;t given it away, is an 8 bit CPU, so the registers can store 8 bits (or one byte) of data. If you are wondering why use registers when computers has tons of memory already? Well, the registers are A LOT FASTER than accessing memory. Also, the CPU can perform operations directly on them, for example, increment or decrement a register directly, &lt;code&gt;AND&lt;/code&gt; or &lt;code&gt;OR&lt;/code&gt; one register with another, and so on. Getting data from memory takes extra steps, time, and generally can&#39;t (at least in CHIP8) be manipulated directly.&lt;/p&gt;
&lt;p&gt;Talking about registers and memory, let&#39;s talk about the groundbreaking features of the CHIP8.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a whopping 4kB of memory&lt;/li&gt;
&lt;li&gt;16 &lt;code&gt;V&lt;/code&gt; registers, named &lt;code&gt;V0&lt;/code&gt;, &lt;code&gt;V1&lt;/code&gt; ... &lt;code&gt;VF&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;a 16 bit &lt;code&gt;I&lt;/code&gt; register usually used to store a memory address&lt;/li&gt;
&lt;li&gt;the aforementioned &lt;code&gt;PC&lt;/code&gt; register, also 16 bit&lt;/li&gt;
&lt;li&gt;a stack&lt;/li&gt;
&lt;li&gt;a 64x32 monochrome display&lt;/li&gt;
&lt;li&gt;other things that I don&#39;t know about yet because I haven&#39;t implemented them (hey, this is just Part 1 &lt;code&gt;¯&#92;_(ツ)_/¯&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you are wondering how this looks in code, this is what I came up with.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint8_t&lt;/span&gt;  memory&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4096&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint8_t&lt;/span&gt;  v&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint16_t&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint16_t&lt;/span&gt; pc&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint8_t&lt;/span&gt;  stack&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint8_t&lt;/span&gt;  display&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;64&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; Chip8&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are other fields like &lt;code&gt;stack_top&lt;/code&gt;, or a mysterious &lt;code&gt;display_image&lt;/code&gt; that I left out and we might talk about later... but I said that I wasn&#39;t going to spoil this for you!&lt;/p&gt;
&lt;p&gt;And while talking about a stack, if you are like me and have mostly done web development, and the sound of &lt;em&gt;&amp;quot;data structures and algorithms&amp;quot;&lt;/em&gt; makes you shudder, don&#39;t despair. A stack is pretty muck like an array in JavaScript but you only have two operations, you can &lt;code&gt;.push(item)&lt;/code&gt; or &lt;code&gt;.pop()&lt;/code&gt; something from it. And the way you do that when you don&#39;t have the niceties of a high level language is simply to have a variable that tells you how many items you have added to your stack (&lt;code&gt;stack_top&lt;/code&gt;), which gets incremented when you &lt;code&gt;push&lt;/code&gt; something to it, and decremented when you &lt;code&gt;pop&lt;/code&gt; something from it. Some people call this behavior LIFO, but is much better to actually understand what it&#39;s going on than have a fancy language that impresses nobody.&lt;/p&gt;
&lt;p&gt;Here&#39;s an example:&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; stack_top &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;push_to_stack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;item1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// add item to index 0 (stack_top) and increment stack_top&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;push_to_stack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;item2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// add item to index 1 (stack_top) and increment stack_top&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// stack_top is 2&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;pop_stack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// decrement stack_top and get stack[stack_top], or item2&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;pop_stack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// decrement stack_top and get stack[stack_top], or item1&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// stack_top is 0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;decode-and-execute-or-how-to-impress-your-manager-10-out-of-ff-times&quot;&gt;Decode and execute, or how to impress your manager 10 out of FF times!&lt;/h3&gt;
&lt;p&gt;Alright, remember the steps that CPU takes, mentioned, like, forever ago? Let&#39;s talk about the last two, that is, decode and execute. But for that we need to talk a little about CHIP8 programs. All they are is simple binary files, with instructions two bytes long (there can be data in there, but we don&#39;t need to worry about that). Now, before you run for the hills at the mention of binary, I promise, it&#39;s not as complicated as it sounds! Let&#39;s start by looking at a hex dump of a CHIP8 program.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// 00e0 6101 6008 a250 d01f 6010 a25f d01f&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// 6018 a26e d01f 6020 a27d d01f 6028 a28c&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// d01f 6030 a29b d01f 6110 6008 a2aa d01f&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// 6010 a2b9 d01f 6018 a2c8 d01f 6020 a2d7&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// d01f 6028 a2e6 d01f 6030 a2f5 d01f 124e&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you have never seen a hex dump, it is simply a more readable way to see 1s and 0s, which is all that computers speak. Each character above represents 4 bits, that is &lt;code&gt;0&lt;/code&gt; in hex would be &lt;code&gt;0 0 0 0&lt;/code&gt; in binary and &lt;code&gt;0&lt;/code&gt; in decimal, while &lt;code&gt;F&lt;/code&gt; would be &lt;code&gt;1 1 1 1&lt;/code&gt; in binary and &lt;code&gt;16&lt;/code&gt; in decimal. You might have seen this in colors such as &lt;code&gt;#FFFFFF&lt;/code&gt;being the color white, or &lt;code&gt;#FF0000&lt;/code&gt; the color red. All those are hex numbers to represent the value of red, green, and blue in three bytes. You see, you know more binary than you think! Ok, back to the hex dump. One character (as in &lt;code&gt;e&lt;/code&gt; in &lt;code&gt;00e0&lt;/code&gt;) is called a &amp;quot;nibble&amp;quot; and two characters (or 8 bits) is called a byte. (I love how a nibble is... a small bite!)&lt;/p&gt;
&lt;p&gt;If you want to understand more binary/hex, there&#39;s nothing like trying stuff, so pick up a &lt;a href=&quot;https://github.com/alt-romes/programmer-calculator&quot;&gt;programmer calculator&lt;/a&gt; and play around!&lt;/p&gt;
&lt;p&gt;Another tip about binary is that sometimes you&#39;ll see some puzzling numbers like &lt;code&gt;0x80&lt;/code&gt;... what does that mean? Well, if you look at that number in binary it is &lt;code&gt;1000 0000&lt;/code&gt;, so all they really needed was the 8th bit to be a one, but writing &lt;code&gt;0x80&lt;/code&gt; is more compact (and obscure) than other alternatives.&lt;/p&gt;
&lt;p&gt;Anyways, back to our hex dump. Each CHIP8 instruction is 2 bytes long, or 16bit, or 4 nibbles or 4 characters on the hex dump above (isn&#39;t it great how you can say the same thing in so many ways?). That is, the first instruction would be &lt;code&gt;00e0&lt;/code&gt; and the next one would be &lt;code&gt;6101&lt;/code&gt;. Simple enough.&lt;/p&gt;
&lt;p&gt;Now, the CHIP8 uses the leftmost nibble to identify what the instruction does. For example, if the instruction starts with &lt;code&gt;a&lt;/code&gt; that means the CPU will set register I to the value of the following 3 nibbles. So our fourth instruction &lt;code&gt;a250&lt;/code&gt; simply tells the CPU &lt;em&gt;&amp;quot;set the &lt;code&gt;I&lt;/code&gt; register to &lt;code&gt;250&lt;/code&gt;&amp;quot;&lt;/em&gt;. That&#39;s it!&lt;/p&gt;
&lt;p&gt;Another example, the &lt;code&gt;6...&lt;/code&gt; family of instructions tells the CPU to set the register Vx to some value. Let&#39;s look at the second and third instruction to get an idea of what this means. &lt;code&gt;6101&lt;/code&gt; is simply telling the CPU &lt;em&gt;&amp;quot;set &lt;code&gt;V1&lt;/code&gt; (&lt;code&gt;1&lt;/code&gt; comes from the 3rd nibble) to the value of 0c&amp;quot;&lt;/em&gt; while &lt;code&gt;6008&lt;/code&gt; would mean &lt;em&gt;&amp;quot;set &lt;code&gt;V0&lt;/code&gt; to the value of 08&amp;quot;&lt;/em&gt;. You see, not too hard!&lt;/p&gt;
&lt;p&gt;You can look around in other areas of the dump and you&#39;ll see other instructions just like that. On the third line, third instruction, could you guess what &lt;code&gt;a29b&lt;/code&gt; means? Look at you reading binary like it&#39;s nothing! (It&#39;s an interesting conversation starter for talking with your manager about a raise... &amp;quot;So, the other day I was reading a hex dump, as one does...&amp;quot;)&lt;/p&gt;
&lt;p&gt;Anyways, I think that&#39;s enough for this post. On the next one I&#39;ll share some of my follies and blunders, how I made such an obscure bug that I ended up writing a debugger for my emulator, how to write a program that your OS will send a kill 9 signal (the way my wife tells it is, a kill signal is like asking somebody with an apple in their hand, &amp;quot;can you please drop the apple?&amp;quot;; a kill 9 signal is chopping off the hand!), and the joys (as in frustration, but the amazing satisfaction of cracking a hard nut!) of low-level graphics programming. :-)&lt;/p&gt;
&lt;p&gt;See you on the next one!&lt;/p&gt;
&lt;hr&gt;
&lt;ol&gt;
    &lt;li id=&quot;1&quot;&gt;technically CHIP8 is not a CPU, but that&#39;s a story for another day.&lt;/li&gt;
&lt;/ol&gt;
</content>
  </entry>
</feed>