<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>@dansalvato</title>
    <link>https://dansalva.to/</link>
    
    <image>
      <url>https://dansalva.to/images/rss-icon.png</url>
      <title>@dansalvato</title>
      <link>https://dansalva.to/</link>
    </image>
    
    <atom:link href="https://dansalva.to/rss2.xml" rel="self" type="application/rss+xml"/>
    
    <description></description>
    <pubDate>Mon, 02 Dec 2024 00:00:00 GMT</pubDate>
    <generator>http://hexo.io/</generator>
    
    <item>
      <title>Writing my own dialogue scripting language</title>
      <link>https://dansalva.to/writing-my-own-dialogue-scripting-language/</link>
      <guid>https://dansalva.to/writing-my-own-dialogue-scripting-language/</guid>
      <pubDate>Mon, 02 Dec 2024 00:00:00 GMT</pubDate>
      
      <description>&lt;video class=&quot;post-video&quot; playsinline controls muted&gt;&lt;source src=&quot;/images/20241202/1.mp4#t=0.01&quot; type=&quot;video/mp4&quot;&gt;Your browser does not support the video tag.&lt;/video&gt;  &lt;p&gt;&lt;em&gt;Magicore Anomala&lt;/em&gt; is an Amiga game I&amp;#39;m working on. Watch the newest gameplay demo in full: &lt;a href=&quot;https://www.youtube.com/watch?v=xIYrhKHEPEA&quot;&gt;Link&lt;/a&gt;&lt;/p&gt; &lt;p&gt;I made &lt;em&gt;Doki Doki Literature Club&lt;/em&gt; using Ren&amp;#39;Py, a Python-based visual novel engine that has some staggering capabilities. But thinking about it, there are tons of games with visual novel-like dialogue features—maybe even the majority of them at this point.&lt;/p&gt; &lt;p&gt;How is dialogue implemented in these games? Do most of them use existing dialogue scripting libraries, or are they more often spinning up their own solution? I have no idea, but given the extremely specific features and limitations of Magicore, I had no choice but to create my entire stack from scratch (like literally everything else in the game engine).&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<video class="post-video" playsinline controls muted><source src="/images/20241202/1.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p><em>Magicore Anomala</em> is an Amiga game I&#39;m working on. Watch the newest gameplay demo in full: <a href="https://www.youtube.com/watch?v=xIYrhKHEPEA">Link</a></p> <p>I made <em>Doki Doki Literature Club</em> using Ren&#39;Py, a Python-based visual novel engine that has some staggering capabilities. But thinking about it, there are tons of games with visual novel-like dialogue features—maybe even the majority of them at this point.</p> <p>How is dialogue implemented in these games? Do most of them use existing dialogue scripting libraries, or are they more often spinning up their own solution? I have no idea, but given the extremely specific features and limitations of Magicore, I had no choice but to create my entire stack from scratch (like literally everything else in the game engine).</p> <span id="more"></span>  <h2>The components</h2><p>The dialogue system in Magicore is made up of four distinct components:</p> <ol> <li>The <strong>script</strong>, the file containing the actual dialogue (and other commands)</li> <li>The <strong>parser</strong>, which takes the script and compiles it into bytecode for the game engine</li> <li>The <strong>bytecode</strong>, our compiled script that the game engine reads byte-by-byte to determine whether to draw text or run special functions</li> <li>The <strong>renderer</strong>, or all the stuff that handles drawing the text and portraits to the screen</li> </ol> <h2>The script</h2><p>The dialogue script format should be easy to read, and efficient to write and edit. Here&#39;s a snippet of dialogue from the latest demo:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">let </span><span style="color: #575279">f_hurthands</span><span style="color: #286983"> </span><span style="color: #575279">=</span><span style="color: #286983"> </span><span style="color: #D7827E">4</span></span><br><span class="line"></span><br><span class="line"><span style="color: #286983">section </span><span style="color: #D7827E">3</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #D7827E">0</span><span style="color: #286983"> </span><span style="color: #575279">l</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #D7827E">0</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;I&#39;ve made it out.&quot;</span></span><br><span class="line"><span style="color: #286983">    if </span><span style="color: #575279">[f_hurthands]</span></span><br><span class="line"><span style="color: #286983">        </span><span style="color: #D7827E">5</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;Although my hands hurt for some reason...&quot;</span></span><br><span class="line"><span style="color: #286983">    fi</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #D7827E">1</span><span style="color: #286983"> </span><span style="color: #575279">r</span></span><br><span class="line"><span style="color: #286983">    event </span><span style="color: #D7827E">66</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #D7827E">1</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;A--&quot;</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #EA9D34">&quot;A spirit?!&quot;</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #D7827E">0</span><span style="color: #286983"> </span><span style="color: #575279">l</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #D7827E">0</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;...&quot;</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #D7827E">1</span><span style="color: #286983"> </span><span style="color: #575279">r</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #D7827E">11</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;...&quot;</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #EA9D34">&quot;</span><span style="color: #286983">&#123;40&#125;</span><span style="color: #EA9D34">...Are you evil?&quot;</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #D7827E">0</span><span style="color: #286983"> </span><span style="color: #575279">l</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #D7827E">5</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;Uh...&quot;</span></span><br><span class="line"><span style="color: #286983">    choice</span></span><br><span class="line"><span style="color: #286983">        </span><span style="color: #575279">evil_maybe</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;Maybe?&quot;</span></span><br><span class="line"><span style="color: #286983">        </span><span style="color: #575279">evil_no</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;I don&#39;t think so...&quot;</span></span><br><span class="line"><span style="color: #286983">    endchoice</span></span><br><span class="line"></span><br><span class="line"><span style="color: #286983">label </span><span style="color: #575279">evil_maybe</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #D7827E">0</span><span style="color: #286983"> </span><span style="color: #575279">l</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #D7827E">11</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;Maybe?&quot;</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #D7827E">1</span><span style="color: #286983"> </span><span style="color: #575279">r</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #D7827E">6</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;Ah--&quot;</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #EA9D34">&quot;Well--I can&#39;t vanquish you if you&#39;re only *maybe* evil!&quot;</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #D7827E">0</span><span style="color: #286983"> </span><span style="color: #575279">l</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #D7827E">8</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;Sorry...&quot;</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #D7827E">1</span><span style="color: #286983"> </span><span style="color: #575279">r</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #D7827E">7</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;...&quot;</span></span><br><span class="line"><span style="color: #286983">    jump </span><span style="color: #575279">evil_after</span></span></code></pre>  <p>For the sake of the file extension and syntax highlighting, this filetype needs a name. Arbitrarily, I called it &quot;magiscript&quot; with a <code>.mgs</code> file extension. It&#39;s really not magic, but thankfully, it&#39;s script, so my name only 50% sucks.</p> <h3>The syntax</h3><p>The syntax is loosely inspired by Ren&#39;Py, but it&#39;s much simpler and more barebones, because I don&#39;t need nearly as many features.</p> <p>The syntax is simply made up of <strong>directives</strong> (special commands) and <strong>dialogue</strong> (raw text to display).</p> <p>You can see all kinds of unique directives in the above snippet:</p> <ul> <li>A <code>section</code> is a top-level mark that is built into a table of contents. To start dialogue, I provide the section number as a parameter.</li> <li>A <code>label</code> can be <code>jump</code>ed to. It&#39;s not stored in the final file format like <code>section</code>; it&#39;s just an alias for a file offset.</li> <li><code>char</code> controls which portrait set to display (i.e. which character), and on which side (left&#x2F;right). Some dialogue lines start with a number, which selects the portrait ID to display from the set (i.e. which facial expression).</li> <li><code>if</code>&#x2F;<code>fi</code> encloses a conditional script block. It takes level flags as an argument, which the game can set and clear as it chooses.</li> <li><code>choice</code>&#x2F;<code>endchoice</code> encloses a choice selection. The choices can even be conditional, like if you only want a choice to be shown when a certain flag is set.</li> <li><code>event</code> runs a level event, which is a code function that can do literally anything. Events 0-63 are level-specific events (they get loaded with the rest of the level), whereas 64-127 are global events. In the snippet, event 66 is a global event used to resume the cutscene timeline, usually to move and animate character sprites while dialogue is happening.</li> <li>We can use <code>let</code> to define an alias, usually for level flags.</li> <li><code>set</code> and <code>clear</code> (not shown above) let us set level flags. This gives us complex branching logic, where we can set flags right in the script, and then conditionally branch depending on which flags are set.</li> </ul> <p>There is some other syntax that isn&#39;t shown above, but that&#39;s the meat of it.</p> <h3>Using <code>let</code></h3><p>The <code>let</code> directive is pretty flexible and lets us substitute just about anything. Besides level flags, another nice use case would be to name characters, facial expressions, and events. Maybe something like this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">let </span><span style="color: #575279">f_hurthands</span><span style="color: #286983"> </span><span style="color: #575279">=</span><span style="color: #286983"> </span><span style="color: #D7827E">4</span></span><br><span class="line"><span style="color: #286983">let </span><span style="color: #575279">nova</span><span style="color: #286983"> </span><span style="color: #575279">=</span><span style="color: #286983"> </span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #286983">let </span><span style="color: #575279">nana</span><span style="color: #286983"> </span><span style="color: #575279">=</span><span style="color: #286983"> </span><span style="color: #D7827E">1</span></span><br><span class="line"><span style="color: #286983">let </span><span style="color: #575279">nova_neutral</span><span style="color: #286983"> </span><span style="color: #575279">=</span><span style="color: #286983"> </span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #286983">let </span><span style="color: #575279">nova_lookdown</span><span style="color: #286983"> </span><span style="color: #575279">=</span><span style="color: #286983"> </span><span style="color: #D7827E">5</span></span><br><span class="line"><span style="color: #286983">let </span><span style="color: #575279">nana_surprised</span><span style="color: #286983"> </span><span style="color: #575279">=</span><span style="color: #286983"> </span><span style="color: #D7827E">1</span></span><br><span class="line"><span style="color: #286983">let </span><span style="color: #575279">tl_play</span><span style="color: #286983"> </span><span style="color: #575279">=</span><span style="color: #286983"> </span><span style="color: #D7827E">66</span></span><br><span class="line"></span><br><span class="line"><span style="color: #286983">section </span><span style="color: #D7827E">3</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #575279">[nova]</span><span style="color: #286983"> </span><span style="color: #575279">l</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #575279">[nova_neutral]</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;I&#39;ve made it out.&quot;</span></span><br><span class="line"><span style="color: #286983">    if </span><span style="color: #575279">[f_hurthands]</span></span><br><span class="line"><span style="color: #286983">        </span><span style="color: #575279">[nova_lookdown]</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;Although my hands hurt for some reason...&quot;</span></span><br><span class="line"><span style="color: #286983">    fi</span></span><br><span class="line"><span style="color: #286983">    char </span><span style="color: #575279">[nana]</span><span style="color: #286983"> </span><span style="color: #575279">r</span></span><br><span class="line"><span style="color: #286983">    event </span><span style="color: #575279">[tl_play]</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #575279">[nana_surprised]</span><span style="color: #286983"> </span><span style="color: #EA9D34">&quot;A--&quot;</span></span><br><span class="line"><span style="color: #286983">    </span><span style="color: #EA9D34">&quot;A spirit?!&quot;</span></span></code></pre>  <p>It just depends on what I feel is more readable for myself (and faster to write). If I know what a simple digit represents, that can end up being more readable to me than a verbose alias.</p> <h3>Syntax highlighting</h3><p>By the way, it&#39;s extremely easy to add basic syntax highlighting in Vim for custom filetypes. Here is the entire syntax file for magiscript:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">if</span><span style="color: #575279"> </span><span style="color: #D7827E">exists</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;b:current_syntax&quot;</span><span style="color: #797593">)</span></span><br><span class="line"><span style="color: #575279">  </span><span style="color: #286983">finish</span></span><br><span class="line"><span style="color: #286983">endif</span></span><br><span class="line"></span><br><span class="line"><span style="color: #56949F">syn keyword</span><span style="color: #575279; font-style: italic"> directive </span><span style="color: #575279">char </span><span style="color: #286983">end </span><span style="color: #575279">jump label choice endchoice set clear </span><span style="color: #286983">if</span><span style="color: #575279"> fi let section event wait</span></span><br><span class="line"></span><br><span class="line"><span style="color: #56949F">syn match</span><span style="color: #575279; font-style: italic"> comment </span><span style="color: #EA9D34">&#39;#.*$&#39;</span></span><br><span class="line"><span style="color: #56949F">syn region</span><span style="color: #575279; font-style: italic"> string </span><span style="color: #286983">start=</span><span style="color: #EA9D34">&#39;&quot;&#39;</span><span style="color: #575279"> </span><span style="color: #286983">end=</span><span style="color: #EA9D34">&#39;&quot;&#39;</span></span><br><span class="line"><span style="color: #56949F">syn match</span><span style="color: #575279; font-style: italic"> number </span><span style="color: #EA9D34">&#39;\v(^|\s)\d+&#39;</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">let </span><span style="color: #575279; font-style: italic">b:current_syntax</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;magiscript&quot;</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">hi </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">link</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">comment</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">Comment</span></span><br><span class="line"><span style="color: #575279">hi </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">link</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">string</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">Constant</span></span><br><span class="line"><span style="color: #575279">hi </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">link</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">directive</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">Statement</span></span><br><span class="line"><span style="color: #575279">hi </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">link</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">number</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">Constant</span></span></code></pre>  <p>It does have some slight inaccuracies that I may fix at some point, but it&#39;s good enough, and it took like 30 minutes to research and put together.</p> <h2>The parser</h2><p>The parser&#39;s job is to take our script and convert it into bytecode. It&#39;s a very simple parser that evaluates the script line by line, converting the different directives to their corresponding opcodes and arguments.</p> <p>Here is a simplified version of the top-level function in the script parser:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">parse_script</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">script_path</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #56949F">str</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">-&gt;</span><span style="color: #575279"> tuple</span><span style="color: #797593">[</span><span style="color: #56949F">bytes</span><span style="color: #797593">,</span><span style="color: #575279"> list</span><span style="color: #797593">[</span><span style="color: #56949F">int</span><span style="color: #797593">]]:</span></span><br><span class="line"><span style="color: #575279">    state </span><span style="color: #286983">=</span><span style="color: #575279"> ScriptState</span><span style="color: #797593">()</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Read script lines from file</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">with</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">open</span><span style="color: #797593">(</span><span style="color: #575279">script_path</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;r&#39;</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #286983">as</span><span style="color: #575279"> f</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">        state</span><span style="color: #797593">.</span><span style="color: #575279">script </span><span style="color: #286983">=</span><span style="color: #575279"> f</span><span style="color: #797593">.</span><span style="color: #575279">readlines</span><span style="color: #797593">()</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Iterate through lines</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">for</span><span style="color: #575279"> line_number</span><span style="color: #797593">,</span><span style="color: #575279"> line </span><span style="color: #286983">in</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">enumerate</span><span style="color: #797593">(</span><span style="color: #575279">state</span><span style="color: #797593">.</span><span style="color: #575279">script</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">        state</span><span style="color: #797593">.</span><span style="color: #575279">line_number </span><span style="color: #286983">=</span><span style="color: #575279"> line_number</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Remove comments</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">if</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;#&#39;</span><span style="color: #575279"> </span><span style="color: #286983">in</span><span style="color: #575279"> line</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">            line </span><span style="color: #286983">=</span><span style="color: #575279"> line</span><span style="color: #797593">[:</span><span style="color: #575279">line</span><span style="color: #797593">.</span><span style="color: #575279">index</span><span style="color: #797593">(</span><span style="color: #EA9D34">&#39;#&#39;</span><span style="color: #797593">)]</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">if</span><span style="color: #575279"> </span><span style="color: #286983">not</span><span style="color: #575279"> line</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">continue</span></span><br><span class="line"><span style="color: #575279">        directive </span><span style="color: #286983">=</span><span style="color: #575279"> line</span><span style="color: #797593">.</span><span style="color: #575279">split</span><span style="color: #797593">()[</span><span style="color: #D7827E">0</span><span style="color: #797593">]</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Check for known directive</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">if</span><span style="color: #575279"> directive </span><span style="color: #286983">in</span><span style="color: #575279"> Directives</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">            state</span><span style="color: #797593">.</span><span style="color: #575279">output </span><span style="color: #286983">+=</span><span style="color: #575279"> Directives</span><span style="color: #797593">[</span><span style="color: #575279">directive</span><span style="color: #797593">](</span><span style="color: #575279">state</span><span style="color: #797593">,</span><span style="color: #575279"> line</span><span style="color: #797593">)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">continue</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Check for portrait ID change</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">if</span><span style="color: #575279"> directive</span><span style="color: #797593">.</span><span style="color: #575279">isdigit</span><span style="color: #797593">():</span></span><br><span class="line"><span style="color: #575279">            state</span><span style="color: #797593">.</span><span style="color: #575279">output </span><span style="color: #286983">+=</span><span style="color: #575279"> _directive_portrait</span><span style="color: #797593">(</span><span style="color: #575279">state</span><span style="color: #797593">,</span><span style="color: #575279"> line</span><span style="color: #797593">)</span></span><br><span class="line"><span style="color: #575279">            state</span><span style="color: #797593">.</span><span style="color: #575279">output </span><span style="color: #286983">+=</span><span style="color: #575279"> _directive_text</span><span style="color: #797593">(</span><span style="color: #575279">state</span><span style="color: #797593">,</span><span style="color: #575279"> line</span><span style="color: #797593">)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">continue</span></span><br><span class="line"><span style="color: #575279">        state</span><span style="color: #797593">.</span><span style="color: #575279">output </span><span style="color: #286983">+=</span><span style="color: #575279"> _directive_text</span><span style="color: #797593">(</span><span style="color: #575279">state</span><span style="color: #797593">,</span><span style="color: #575279"> line</span><span style="color: #797593">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Resolve jumps</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">for</span><span style="color: #575279"> offset</span><span style="color: #797593">,</span><span style="color: #575279"> to_label </span><span style="color: #286983">in</span><span style="color: #575279"> Jumps</span><span style="color: #797593">.</span><span style="color: #575279">items</span><span style="color: #797593">():</span></span><br><span class="line"><span style="color: #575279">        to_offset </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #575279">Labels</span><span style="color: #797593">[</span><span style="color: #575279">to_label</span><span style="color: #797593">]</span><span style="color: #575279"> </span><span style="color: #286983">-</span><span style="color: #575279"> offset</span><span style="color: #797593">).</span><span style="color: #575279">to_bytes</span><span style="color: #797593">(</span><span style="color: #D7827E">2</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">signed</span><span style="color: #286983">=</span><span style="color: #D7827E">True</span><span style="color: #797593">)</span></span><br><span class="line"><span style="color: #575279">        state</span><span style="color: #797593">.</span><span style="color: #575279">output</span><span style="color: #797593">[</span><span style="color: #575279">offset </span><span style="color: #286983">+</span><span style="color: #575279"> </span><span style="color: #D7827E">1</span><span style="color: #797593">]</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> to_offset</span><span style="color: #797593">[</span><span style="color: #D7827E">0</span><span style="color: #797593">]</span></span><br><span class="line"><span style="color: #575279">        state</span><span style="color: #797593">.</span><span style="color: #575279">output</span><span style="color: #797593">[</span><span style="color: #575279">offset </span><span style="color: #286983">+</span><span style="color: #575279"> </span><span style="color: #D7827E">2</span><span style="color: #797593">]</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> to_offset</span><span style="color: #797593">[</span><span style="color: #D7827E">1</span><span style="color: #797593">]</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">return</span><span style="color: #575279"> </span><span style="color: #56949F">bytes</span><span style="color: #797593">(</span><span style="color: #575279">state</span><span style="color: #797593">.</span><span style="color: #575279">output</span><span style="color: #797593">),</span><span style="color: #575279"> state</span><span style="color: #797593">.</span><span style="color: #575279">section_offsets</span></span></code></pre>  <h3>The main loop</h3><p>Basically, after reading the file, the parser does this line by line:</p> <ol> <li>Remove comments.</li> <li>Check for known directives (e.g. <code>if</code>, <code>jump</code>, <code>char</code>). If found, run the function to handle that directive.</li> <li>If the &quot;directive&quot; is a decimal digit, that&#39;s the directive to change portrait ID, followed by a line of text.</li> <li>If no directive is found, then the line must simply be dialogue text.</li> </ol> <h3>Directive functions</h3><p>Directives are parsed in this line of the loop:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">        </span><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Check for known directive</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">if</span><span style="color: #575279"> directive </span><span style="color: #286983">in</span><span style="color: #575279"> Directives</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">            state</span><span style="color: #797593">.</span><span style="color: #575279">output </span><span style="color: #286983">+=</span><span style="color: #575279"> Directives</span><span style="color: #797593">[</span><span style="color: #575279">directive</span><span style="color: #797593">](</span><span style="color: #575279">state</span><span style="color: #797593">,</span><span style="color: #575279"> line</span><span style="color: #797593">)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">continue</span></span></code></pre>  <p>Which is calling a function from this table:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">Directives</span><span style="color: #797593">:</span><span style="color: #575279"> dict</span><span style="color: #797593">[</span><span style="color: #56949F">str</span><span style="color: #797593">,</span><span style="color: #575279"> Callable</span><span style="color: #797593">[[</span><span style="color: #575279">ScriptState</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #56949F">str</span><span style="color: #797593">],</span><span style="color: #575279"> </span><span style="color: #56949F">bytes</span><span style="color: #797593">]]</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593">&#123;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;char&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_char</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;end&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_end</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;label&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_label</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;jump&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_jump</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;set&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_set</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;clear&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_clear</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;let&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_let</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;if&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_if</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;fi&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_endif</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;section&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_section</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;event&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_event</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&#39;wait&#39;</span><span style="color: #797593">:</span><span style="color: #575279"> _directive_wait</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #797593">&#125;</span></span></code></pre>  <p>Here is an example of one such directive function:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">_directive_char</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">state</span><span style="color: #797593">:</span><span style="color: #575279"> ScriptState</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">line</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #56949F">str</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">-&gt;</span><span style="color: #575279"> </span><span style="color: #56949F">bytes</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&quot;&quot;&quot;Sets the character ID for using a specific portrait set and textbox side.&quot;&quot;&quot;</span></span><br><span class="line"><span style="color: #575279">    args </span><span style="color: #286983">=</span><span style="color: #575279"> line</span><span style="color: #797593">.</span><span style="color: #575279">split</span><span style="color: #797593">()</span></span><br><span class="line"><span style="color: #575279">    opcode </span><span style="color: #286983">=</span><span style="color: #575279"> Opcodes</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;char&#39;</span><span style="color: #797593">]</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> opcode 0x01</span></span><br><span class="line"><span style="color: #575279">    char_id </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #56949F">int</span><span style="color: #797593">(</span><span style="color: #575279">args</span><span style="color: #797593">[</span><span style="color: #D7827E">1</span><span style="color: #797593">]).</span><span style="color: #575279">to_bytes</span><span style="color: #797593">()</span></span><br><span class="line"><span style="color: #575279">    side </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #286983">0x</span><span style="color: #D7827E">ff</span><span style="color: #575279"> </span><span style="color: #286983">if</span><span style="color: #575279"> args</span><span style="color: #797593">[</span><span style="color: #D7827E">2</span><span style="color: #797593">]</span><span style="color: #575279"> </span><span style="color: #286983">==</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;l&#39;</span><span style="color: #575279"> </span><span style="color: #286983">else</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #797593">).</span><span style="color: #575279">to_bytes</span><span style="color: #797593">()</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">return</span><span style="color: #575279"> opcode </span><span style="color: #286983">+</span><span style="color: #575279"> char_id </span><span style="color: #286983">+</span><span style="color: #575279"> side</span></span></code></pre>  <p>Pretty simple, right? Each directive function just takes the script line, checks the parameters, and returns some bytes.</p> <h3>Resolving jumps</h3><p>After the script is done being parsed, we resolve jumps. The <code>jump</code> opcode is really just &quot;Jump to file offset <code>xxxx</code>&quot;.</p> <p>Well, we don&#39;t know any file offsets until we actually have the file. So, the script is fully compiled first, but all <code>jump</code>s are given an offset of <code>0000</code>. Now, we can go back through and replace those offsets with the actual file offset of the corresponding labels.</p> <p>Of note, <code>if</code> statements are just conditional jumps. The writer thinks of it as &quot;if condition, run this block&quot;. However, the parser converts it to &quot;if NOT condition, jump to the end of this block (<code>fi</code>)&quot;. So, just like labels, <code>fi</code> isn&#39;t its own opcode. It&#39;s more like a temporary unnamed label used for that conditional jump.</p> <h3>That parser seems pretty dumb and unsafe</h3><p>Indeed. The script parser is an internal tool that only I am using. Since I know what rules to follow, I don&#39;t feel the need for much error handling—especially since it&#39;s prone to change at any time. It&#39;s one of those &quot;just get it to work&quot; type things.</p> <p>This could bite me later on. If I make a typo in the script that results in undefined behavior, it might be hard to locate that bug. I think once I&#39;m 100% confident that the syntax and directives are set in stone, I&#39;ll go through and harden the parser to prevent such cases.</p> <p>Plus, I will eventually make the whole Magicore build pipeline open-source, so at some point, these tools will need enough safety to be usable by others.</p> <h2>The bytecode</h2><p>When Magicore reads the compiled script, it doesn&#39;t perform any extra logic or preprocessing. It simply reads the script one byte at a time.</p> <p>Here&#39;s the in-game function that reads the next byte in the script and processes it. Ready for some Assembly?</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">_txt_next_char</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.b</span><span style="color: #575279">      #TEXT_SPEED,_txt_NextCharTimer</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.l</span><span style="color: #575279">      txt_pSection,</span><span style="color: #D7827E">a1</span></span><br><span class="line"><span style="color: #D7827E">.n</span><span style="color: #575279">:         </span><span style="color: #286983">move.w</span><span style="color: #575279">      _txt_ScriptOffset,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">addq</span><span style="color: #575279">        #</span><span style="color: #D7827E">1</span><span style="color: #575279">,_txt_ScriptOffset</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">moveq</span><span style="color: #575279">       #</span><span style="color: #D7827E">0</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; d0 = current char byte</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; If char is 0 (pad), skip to next char</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.b</span><span style="color: #575279">      (</span><span style="color: #D7827E">a1</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span><span style="color: #575279">.w),</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">beq</span><span style="color: #575279">         .n</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">cmp.b</span><span style="color: #575279">       #</span><span style="color: #D7827E">32</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bhs</span><span style="color: #575279">         .literal</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Char &lt; 32, so it&#39;s an opcode</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">add.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d0</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">lea</span><span style="color: #575279">         txt_jOpcodes,</span><span style="color: #D7827E">a0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span><span style="color: #575279">.w),</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jmp</span><span style="color: #575279">         (</span><span style="color: #D7827E">a0</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span><span style="color: #575279">.w)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Char is a literal</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; If space (32), look ahead to see if we need to wrap word</span></span><br><span class="line"><span style="color: #D7827E">.literal</span><span style="color: #575279">:   </span><span style="color: #286983">sub.b</span><span style="color: #575279">       #</span><span style="color: #D7827E">32</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">beq</span><span style="color: #575279">         .lookahead</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Add new char to buffer</span></span><br><span class="line"><span style="color: #D7827E">.addchar</span><span style="color: #575279">:   </span><span style="color: #286983">move.w</span><span style="color: #575279">      </span><span style="color: #D7827E">d0</span><span style="color: #575279">,-(</span><span style="color: #D7827E">sp</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         tchr_add</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">sp</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Update HPos with char width</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr</span><span style="color: #575279">         _txt_get_char_width</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">add.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d0</span><span style="color: #575279">,txt_HPos</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Play SFX</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr</span><span style="color: #575279">         _txt_play_sfx</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">rts</span></span></code></pre>  <p>Even if you&#39;re not great with Assembly, the comments should help you get the general idea.</p> <h3>Opcodes</h3><p>Byte values 0-31 are the opcodes. If you know your ASCII, you know that printable characters start at 32, so it ends up being quite efficient to repurpose values 0-31 in this way.</p> <p>I use a jump table (basically a switch statement) to run a different function for each of the unique opcode values. But if the value is 32 or higher, the engine instead buffers that character to be rendered.</p> <p>Here is an example of how the <code>char</code> directive is processed in-game:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">_txt_directive_char</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">addq.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">2</span><span style="color: #575279">,_txt_ScriptOffset</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Portrait ID</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.b</span><span style="color: #575279">      </span><span style="color: #D7827E">1</span><span style="color: #575279">(</span><span style="color: #D7827E">a1</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span><span style="color: #575279">.w),</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Side ($00 right, $ff left)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.b</span><span style="color: #575279">      </span><span style="color: #D7827E">2</span><span style="color: #575279">(</span><span style="color: #D7827E">a1</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span><span style="color: #575279">.w),</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">ext.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">ext.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         prt_change_set</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr</span><span style="color: #575279">         _txt_cr</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bra</span><span style="color: #575279">         _txt_next_char</span></span></code></pre>  <p>Since the script is read byte by byte, different opcodes can easily have different parameter lengths. The <code>char</code> opcode has 2 bytes of parameters: Portrait ID, and which side the portrait goes on. That&#39;s why we add 2 to the script offset—those 2 bytes are being used right now, so the engine should skip over them next time a byte is read.</p> <p>Meanwhile, an opcode like <code>ctc</code> needs no arguments. The parser inserts the <code>ctc</code> opcode at the end of every dialogue line, which tells the game engine to pause the script, and continue after the fire button is pressed.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">_txt_directive_ctc</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      #TXT_STATE_IDLE,txt_State</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bra</span><span style="color: #575279">         ctc_enable</span></span></code></pre>  <p>As of now, the <code>jumpif</code> opcodes have the most arguments, at 6 bytes: The offset to jump to, and the level flags to use as our conditional.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5; font-style: italic">; Jump if all of the flags are set/clear</span></span><br><span class="line"><span style="color: #9893A5; font-style: italic">; opcode (byte), jump offset (word), flags (word), mask (word)</span></span><br><span class="line"><span style="color: #9893A5; font-style: italic">; Mask: Which bits are relevant</span></span><br><span class="line"><span style="color: #9893A5; font-style: italic">; Flags: Flag state (on or off) to test against</span></span><br><span class="line"><span style="color: #D7827E">_txt_directive_jumpifall</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      lvl_Flags,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; d2 = flags, d3 = mask</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      </span><span style="color: #D7827E">3</span><span style="color: #575279">(</span><span style="color: #D7827E">a1</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span><span style="color: #575279">.w),</span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      </span><span style="color: #D7827E">5</span><span style="color: #575279">(</span><span style="color: #D7827E">a1</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span><span style="color: #575279">.w),</span><span style="color: #D7827E">d3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">eor.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d0</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">and.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d2</span><span style="color: #575279">,</span><span style="color: #D7827E">d3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Jump if all flags within the mask match</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">beq</span><span style="color: #575279">         _txt_directive_jump</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Otherwise, just move on</span></span><br><span class="line"><span style="color: #D7827E">.nojump</span><span style="color: #575279">:    </span><span style="color: #286983">addq.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">6</span><span style="color: #575279">,_txt_ScriptOffset</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bra</span><span style="color: #575279">         _txt_next_char</span></span></code></pre>  <h3>Raw text</h3><p>If a byte in the script is value 32 or higher, that means it&#39;s a literal ASCII character for us to draw, so we add it to the buffer.</p> <h3>Lookahead at runtime</h3><p>The only time the engine actually looks ahead at upcoming data is when inserting a space. The reason for this is word wrapping. Is the next word going to fit on the current line? If not, we insert a newline instead of a space.</p> <p>Theoretically, we can do word wrapping at compile time instead of at runtime. If the parser knows what font is being used, and the width of every character in the font, it could preemptively figure out the newlines and save us some runtime code.</p> <p>But I don&#39;t know if I like that. Text wrapping is also based on the padding of the textbox, and whether a portrait is being shown. Yes, those things are also known ahead of time, but it feels more future-proof to let the game engine handle text wrapping. I&#39;ll keep it that way unless it&#39;s taking a toll on performance in key scenes.</p> <h2>The renderer</h2><p>Finally, we have to render the text. It&#39;s fairly complicated, so I won&#39;t go into full detail.</p> <h3>The font</h3><p>The font used for Magicore dialogue is not final—I think it looks cool, but it has some readability issues.</p> <p>In any case, I wanted my build system to support easily changing fonts. A font in Magicore is basically just a bitmap image with all the characters, and a table containing character widths (for good kerning).</p> <p>There are plenty of external tools to generate bitmaps of fonts, but I got tired of manually generating font bitmaps while experimenting with different fonts.</p> <p>Thankfully, <a href="https://pillow.readthedocs.io/en/stable/">Pillow</a> supports rendering text to an image using <code>ImageFont</code>. That means as part of my build system, I can pass in any TTF font and render all the characters to a bitmap.</p> <p><img src="/images/20241202/2.png"></p> <h3>The video memory</h3><p>Amiga doesn&#39;t have a static region of video memory—you get to give it a pointer to anywhere in Chip RAM, and whatever is there will be displayed.</p> <p>That means I can have separate screen memory for the textbox and the main playfield. Using the Copper (Amiga&#39;s parallel coprocessor), I first provide a pointer to the textbox as the screen memory. Then, the Copper waits for line 56 (the bottom of the textbox) before switching the screen pointer back to the main playfield.</p> <p>Pretty cool, right? Another way to think about it is hardware-supported splitscreen.</p> <h3>The colors</h3><p>Earlier, I mentioned we have a buffer of characters to render. Why do we need a buffer, rather than just rendering each character immediately?</p> <p>The reason is that we have a visual effect where each character fades in over a few frames. It&#39;s subtle, but you can see it if you look closely at the text in the video. It looks quite a bit better than having the characters appear instantly at full white.</p> <p>The textbox uses 5 bitplanes to achieve 32 colors (to learn more about bitplanes, see my <a href="/shattering-the-magicore-8-color-limit">last post</a>).</p> <p>The textbox colors are distributed like this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">00      BG (black)</span></span><br><span class="line"><span style="color: #575279">01      Text frame 1 (dark gray)</span></span><br><span class="line"><span style="color: #575279">02      Text frame 3 (white)</span></span><br><span class="line"><span style="color: #575279">03      Text frame 2 (light gray)</span></span><br><span class="line"><span style="color: #575279">04      Text emphasis (red)</span></span><br><span class="line"><span style="color: #575279">05-31   Portrait image</span></span></code></pre>  <p>When a new character is added to the buffer, it gets drawn over 3 frames:</p> <ol> <li>Blit to bitplane 0 (color 1 &#x3D; dark gray)</li> <li>Blit to bitplane 1 (color 3 &#x3D; light gray)</li> <li>Erase from bitplane 0 (color 2 &#x3D; white)</li> </ol> <p>Any emphasis characters (drawn in red) don&#39;t have this fade-in effect. I wouldn&#39;t mind adding it, but it depends on how many colors we want to reserve for the portrait image. Right now, the portrait gets 27 colors, but the assets so far only use about 20 colors, so we might have a few colors to spare. It just depends on the color complexity of future portraits.</p> <h2>Wrapping up</h2><p>The dialogue system is a good example of everything that goes into both the build pipeline and runtime functionality. I&#39;ve been working on Magicore and its game engine as a side project for over 2 years now. Hopefully, this gives you a good understanding of what that time goes into (and why I don&#39;t have a release date yet).</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/writing-my-own-dialogue-scripting-language/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Shattering the Magicore 8-color limit</title>
      <link>https://dansalva.to/shattering-the-magicore-8-color-limit/</link>
      <guid>https://dansalva.to/shattering-the-magicore-8-color-limit/</guid>
      <pubDate>Mon, 18 Nov 2024 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;&lt;img src=&quot;/images/20241118/1.png&quot;&gt;&lt;/p&gt; &lt;p&gt;Watch the newest gameplay demo of &lt;em&gt;Magicore Anomala&lt;/em&gt;: &lt;a href=&quot;https://www.youtube.com/watch?v=xIYrhKHEPEA&quot;&gt;Link&lt;/a&gt;&lt;/p&gt; &lt;p&gt;The technology behind &lt;em&gt;Magicore Anomala&lt;/em&gt; requires the background to be 8 colors or less, although any Amiga fan will know that the system is capable of a rather incredible color range. As with many classic systems, Amiga&amp;#39;s hardware limitations are extremely fascinating and follow rules that can be cleverly bent.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p><img src="/images/20241118/1.png"></p> <p>Watch the newest gameplay demo of <em>Magicore Anomala</em>: <a href="https://www.youtube.com/watch?v=xIYrhKHEPEA">Link</a></p> <p>The technology behind <em>Magicore Anomala</em> requires the background to be 8 colors or less, although any Amiga fan will know that the system is capable of a rather incredible color range. As with many classic systems, Amiga&#39;s hardware limitations are extremely fascinating and follow rules that can be cleverly bent.</p> <span id="more"></span>  <h2>Why 8 colors?</h2><p>Amiga has a palette of 32 colors, but those colors aren&#39;t fixed. The programmer can take any RGB value and assign it to a color in the palette. Magicore reserves 16 colors for sprites (colors 16-31), so realistically, we have 16 colors to draw the screen with (colors 0-15).</p> <p>Amiga uses bitplanes, which means we don&#39;t have one single screen space—we define multiple screens, each 1 bit per pixel. That means each pixel can be either &quot;on&quot; or &quot;off&quot;, like classic Macintosh systems.</p> <p>The difference is that with multiple bitplanes, Amiga can take several 1-bit screens and stack them together to create a multicolored display. My background is 3 bitplanes, which means I&#39;m stacking three 1-bit screens to give myself 3 bits per pixel. 3 bits can represent <code>000-111</code> or 0-7, hence 8 colors.</p> <p>For example, here is a diagram of an 8-color background that has been split into three individual bitplanes. Any given pixel can be on or off in each bitplane, for a total of 8 combinations.</p> <p><img src="/images/20241118/2.png"></p> <p>The illustrator doesn&#39;t need to worry about this. They can simply produce an 8-color PNG image—it&#39;s converted to bitplanes during the build process.</p> <h2>Bitplanes make Magicore possible</h2><p>Bitplanes are the entire reason I conceptualized Magicore. I asked myself: Is Amiga fast enough to completely clear and redraw one bitplane every single frame?</p> <p>The answer is yes—very yes. In fact, I can clear and draw up to 200 unique bullets every frame, and maintain a full 60fps. With that kind of throughput, I was set on making a bullet hell game.</p> <video class="post-video" playsinline controls muted><source src="/images/20241118/4.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p>But the limitation at play is that the attacks layer is just one bitplane, which means each pixel only gets 1 bit (on or off).</p> <p>When stacking the attacks bitplane on top of the 3 background bitplanes, interesting things happen. The background alone, 3 bitplanes, means each pixel can be assigned a color in the range <code>000-111</code> (0-7). If we&#39;re drawing another bitplane on top of that—in this case, the attacks—the pixel is transformed to the color range <code>1000-1111</code> (we&#39;re adding an extra bit to the front). Now, any pixel where there&#39;s no bullet is color <code>0000-0111</code> (0-7), and a pixel containing a bullet is color <code>1000-1111</code> (8-15).</p> <p>So, despite the attacks layer being just 1 bit per pixel, the way it gets stacked on the background means it takes up 8 colors in our palette.</p> <p>That&#39;s the full distribution of the color palette: 0-7 for the background, 8-15 for the attacks, and 16-31 for the sprites.</p> <h2>8 palette slots !&#x3D; 8 colors</h2><p>What happens if we change the color palette in the middle of the frame?</p> <p>Like most classic systems, Amiga doesn&#39;t build a complete frame buffer—it draws the screen line by line, taking about 16.67ms (or 20ms for PAL) to draw the entire frame. That means we can edit hardware registers, like the color palette, while the frame is still being drawn.</p> <p>Amiga has a special chip, the Copper (coprocessor), to handle this. The Copper takes a unique instruction set whose entire purpose is to wait for the desired line on the screen, then adjust hardware registers with near-perfect timing.</p> <p>In fact, the Copper is so good at its job that if I really wanted, I could give it instructions to change all 8 background colors on every single line of the screen. And it does this in parallel with the CPU!</p> <h2>Logistics hurdles</h2><p>So, using the Copper, we can change the color palette while different regions of the background are being displayed, creating an even more colorful image. That&#39;s great, but how the heck do we produce backgrounds to comply with such a specific workaround?</p> <p>The first rule is that a background can&#39;t have more than 8 colors per horizontal line. Realistically, the Copper isn&#39;t precise enough for pixel-perfect color swaps, so we&#39;re only changing the palette in between lines.</p> <p>That means we&#39;re looking at a process like this:</p> <ol> <li>Illustrate a compliant background image</li> <li>Note which lines introduce new colors</li> <li>Figure out which palette indices to replace (i.e. which colors are no longer being used) with the new colors</li> <li>Carefully produce an 8-color variant of the background, where each pixel is represented by the decided palette index</li> <li>Write the Copper instructions to set the palette indices to the correct color values at the correct parts of the screen</li> </ol> <p>Without some kind of automation, this workflow quickly becomes unreasonable. The color regions need careful planning, and any edits to the source image could require also editing (or redoing) those accompanying assets.</p> <h2>Automatic color regions</h2><p>Magicore&#39;s asset data, like graphics and audio, is built using a Python framework I wrote called Build-A-File (see <a href="/rapidly-building-custom-file-formats-for-my-game">this post</a>). Thanks to this, I can leverage <a href="https://pillow.readthedocs.io/en/stable/">Pillow</a>, an extremely powerful image manipulation library.</p> <p>Using Pillow, here is the new pipeline that background images are put through as part of my data compiler:</p> <ol> <li>The background is split into 180 individual lines. Each line is quantized so that its palette contains only the colors used in that line.</li> <li>Traversing line by line, we figure out what the &quot;color regions&quot; are, based on which lines have new colors introduced.</li> <li>The lines are grouped into regions of 8-color palettes.</li> <li>From one region to the next, we rearrange the palettes to match as many colors as possible between regions. (Pillow can use <code>remap_palette()</code> to reorder a palette, and rewrite the image contents to match the new palette indices.)</li> <li>Now, we can go through each region&#39;s palette and determine which color indices need to be updated per region. This is written to a color region data table that&#39;s separate from the image.</li> <li>All the regions are recombined into a single image, using the palette of the topmost region as the starting palette.</li> </ol> <p>Below is an example of a background image that has been processed down to 8 colors, and then after its color regions are applied (with lines showing separation between regions).</p> <p><img src="/images/20241118/3.png"></p> <p>See how in the top image, the grassy hill is the same color as the sky? That&#39;s because when the image processor needed a new color region for the hill, it noticed that the sky color is no longer being used. So, the hill was assigned that same palette index to be recycled into a new color.</p> <p>Now, back to the header image:</p> <p><img src="/images/20241118/1.png"></p> <p>The background here is a free image asset I found online (illustrated by ansimuz) to use as a stress test. In the final game, an artist will be illustrating each background with color regions in mind, but I wanted to see how far I could push the color regions.</p> <p>It works even better than I expected—there&#39;s so much detail preserved in the moon, mountains, clouds, and trees. Including all the color regions, the background has a total of 23 colors!</p> <p>As a reminder, the background uses palette indices 0-7. The cyan boing ball is on the attacks layer, so it&#39;s using palette indices 8-15. By copying colors 0-7 into colors 8-15 and boosting the green and blue values, we get this cool additive blending effect.</p> <p>Finally, the character standing on top of the ball is a sprite, using colors 24-31.</p> <h2>Building the copperlist</h2><p>Once the game engine loads the background and its color region data, it needs to assemble a copperlist (set of Copper instructions) to change colors on the correct lines of the screen.</p> <p>The copperlist itself is pretty simple, looking something like this after it&#39;s built:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        </span><span style="color: #D7827E">$9c07</span><span style="color: #575279">,</span><span style="color: #D7827E">$fffe</span><span style="color: #575279">      </span><span style="color: #9893A5; font-style: italic">; hill</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        COLOR0</span><span style="color: #D7827E">7</span><span style="color: #575279">,</span><span style="color: #D7827E">$0ce8</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        </span><span style="color: #D7827E">$b607</span><span style="color: #575279">,</span><span style="color: #D7827E">$fffe</span><span style="color: #575279">      </span><span style="color: #9893A5; font-style: italic">; platforms</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        COLOR0</span><span style="color: #D7827E">6</span><span style="color: #575279">,</span><span style="color: #D7827E">$0db8</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        </span><span style="color: #D7827E">$be07</span><span style="color: #575279">,</span><span style="color: #D7827E">$fffe</span><span style="color: #575279">      </span><span style="color: #9893A5; font-style: italic">; hill base</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        COLOR0</span><span style="color: #D7827E">6</span><span style="color: #575279">,</span><span style="color: #D7827E">$0bd8</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        </span><span style="color: #D7827E">$d507</span><span style="color: #575279">,</span><span style="color: #D7827E">$fffe</span><span style="color: #575279">      </span><span style="color: #9893A5; font-style: italic">; ground</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        COLOR0</span><span style="color: #D7827E">7</span><span style="color: #575279">,</span><span style="color: #D7827E">$08b7</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        </span><span style="color: #D7827E">$01fe</span><span style="color: #575279">,</span><span style="color: #D7827E">$0000</span><span style="color: #575279">      </span><span style="color: #9893A5; font-style: italic">; ground 2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        COLOR0</span><span style="color: #D7827E">5</span><span style="color: #575279">,</span><span style="color: #D7827E">$0344</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        </span><span style="color: #D7827E">$01fe</span><span style="color: #575279">,</span><span style="color: #D7827E">$0000</span><span style="color: #575279">      </span><span style="color: #9893A5; font-style: italic">; ground 3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dc.w</span><span style="color: #575279">        COLOR0</span><span style="color: #D7827E">4</span><span style="color: #575279">,</span><span style="color: #D7827E">$0486</span></span></code></pre>  <p>For each region, the first line specifies which line to wait until (e.g. <code>$9c</code> or <code>156</code> for the hill). Then, the second line sets the appropriate color.</p> <p>The ground requires 3 colors to be changed on the same line, so the latter two &quot;wait&quot; instructions are no-ops. Each color definition needs to remain evenly-spaced, because the colors in this copperlist aren&#39;t static—they&#39;re modified by an external loop that writes to the copperlist like an array.</p> <p>Why do the colors need to be modified? Primarily, to handle screen fades.</p> <video class="post-video" playsinline controls muted><source src="/images/20241118/5.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p>It&#39;s easy to forget, but screen fades aren&#39;t very simple on old hardware—they typically require running calculations on every color in the palette.</p> <p>This means there has to be some middle layer that can transform the colors before applying them to the copperlist. Magicore&#39;s game engine already has a lot of good functions and data structures for this purpose (see <a href="/getting-clever-with-the-amiga-blitter">this post</a>), so it was straightforward to set them up for use on this copperlist as well.</p> <p>Did you also notice the screen-shake effect in the above video? That&#39;s interesting, and a little less straightforward. If the background is moving up and down, that means the lines that require color changes are shifting around all over the place.</p> <p>So, I have to account for that as well. Whenever there&#39;s a vertical scroll, the copperlist is updated to offset all of those line definitions by the scroll amount.</p> <p>Side note: The textbox that slides in at the end of that video? That also uses the copperlist to edit all 32 colors, as well as the <em>screen pointer itself</em>, to render the portrait and text region. That&#39;s right, the &quot;video RAM&quot; can be anywhere you want in memory, and even changed mid-frame with a simple pointer update. So, the textbox isn&#39;t drawn &quot;on top&quot; of the main screen—it has its own dedicated memory space which is composited to the top of the display using the Copper. Isn&#39;t that incredible for 1985 hardware?</p> <h2>The heart of Amiga</h2><p>I consider Magicore to be an honest game—that is, I&#39;m not using crazy tricks and demoscene effects to make it possible. The engine is built with consistency, flexibility, and good-practice principles.</p> <p>In fact, given the function of bitplanes and the Copper, the Amiga was practically designed with these kinds of effects in mind. The way bitplanes enable &quot;stackable&quot; graphics, and the way the Copper allows for adjusting the screen mid-frame—that&#39;s the vision under which Amiga was designed. It&#39;s the reason Magicore is an Amiga game not just in code, but at its heart.</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/shattering-the-magicore-8-color-limit/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Decoding Impossible Mission 2 for Amiga</title>
      <link>https://dansalva.to/decoding-impossible-mission-2-for-amiga/</link>
      <guid>https://dansalva.to/decoding-impossible-mission-2-for-amiga/</guid>
      <pubDate>Sat, 17 Aug 2024 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;&lt;img src=&quot;/images/20240817/1.png&quot;&gt;&lt;/p&gt; &lt;p&gt;&lt;em&gt;Impossible Mission&lt;/em&gt; (legally distinct from its extremely obvious permutation) was one of the more popular games on Commodore 64, and I think it still has its charm. I think the mechanics like basing floor detection on the position of your foot, and the unadjustable front-flip jump, make for an interesting game design study.&lt;/p&gt; &lt;p&gt;I missed out on the famous original and grew up with &lt;em&gt;Impossible Mission II&lt;/em&gt; on Amiga, released in 1988. The gameplay is quite similar, and I think it&amp;#39;s a bit of a hidden gem, especially given how few people in my demographic (millenials in North America) have heard of &lt;em&gt;Impossible Mission&lt;/em&gt; or have even played a game on C64 or Amiga.&lt;/p&gt; &lt;p&gt;So, as any reasonable enjoyer of a video game does, I started to reverse engineer it.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p><img src="/images/20240817/1.png"></p> <p><em>Impossible Mission</em> (legally distinct from its extremely obvious permutation) was one of the more popular games on Commodore 64, and I think it still has its charm. I think the mechanics like basing floor detection on the position of your foot, and the unadjustable front-flip jump, make for an interesting game design study.</p> <p>I missed out on the famous original and grew up with <em>Impossible Mission II</em> on Amiga, released in 1988. The gameplay is quite similar, and I think it&#39;s a bit of a hidden gem, especially given how few people in my demographic (millenials in North America) have heard of <em>Impossible Mission</em> or have even played a game on C64 or Amiga.</p> <p>So, as any reasonable enjoyer of a video game does, I started to reverse engineer it.</p> <span id="more"></span>  <h2>The disassembly</h2><p>I used <a href="https://howprice.itch.io/aira-force">Aira Force</a> to disassemble the binary and, surprisingly, was able to reassemble and boot the game without needing to fix too many things.</p> <p>My first goal was to map out some of the functions used for starting up the game, because if I&#39;m going to be doing a lot of debugging, I want to be able to boot into the game as quickly as possible.</p> <p>Thankfully, the boot process is extremely linear. It was easy to find and disable lots of functions that pertain to running the game&#39;s intro sequence. Soon, the game was booting straight into gameplay.</p> <p>Here&#39;s a (partially) mapped excerpt from the end of the boot process. <code>pl_update</code> is an infinite loop that returns only when the game ends, sending us back to <code>.newgame</code> for another go.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">.newgame</span><span style="color: #575279">:   </span><span style="color: #286983">jsr</span><span style="color: #575279">         unk_build_world(</span><span style="color: #D7827E">pc</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         clear_inputs</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         sub_</span><span style="color: #D7827E">19170</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         setup_clear_tape_data</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         setup_load_tower</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         setup_init_player_vars</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         sub_12fa6</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         do_room_trans_fade</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         sub_1a1ae</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         setup_spawn_player</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">$0001</span><span style="color: #575279">,TimerActive</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">jsr</span><span style="color: #575279">         pl_update</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bra.s</span><span style="color: #575279">       .newgame</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">unlk</span><span style="color: #575279">        </span><span style="color: #D7827E">a5</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">rts</span></span><br><span class="line"><span style="color: #D7827E">.file1</span><span style="color: #575279">:     </span><span style="color: #286983">dc.b</span><span style="color: #575279">        </span><span style="color: #EA9D34">&quot;hang/wh_d&quot;</span><span style="color: #575279">,</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #D7827E">.file2</span><span style="color: #575279">:     </span><span style="color: #286983">dc.b</span><span style="color: #575279">        </span><span style="color: #EA9D34">&quot;hang/cho3_d.k2&quot;</span><span style="color: #575279">,</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #D7827E">.file3</span><span style="color: #575279">:     </span><span style="color: #286983">dc.b</span><span style="color: #575279">        </span><span style="color: #EA9D34">&quot;game&quot;</span><span style="color: #575279">,</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #D7827E">.playsaved</span><span style="color: #575279">: </span><span style="color: #286983">dc.b</span><span style="color: #575279">        </span><span style="color: #EA9D34">&quot;play a saved game?&quot;</span><span style="color: #575279">,</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #D7827E">.spaces</span><span style="color: #575279">:    </span><span style="color: #286983">dc.b</span><span style="color: #575279">        </span><span style="color: #EA9D34">&quot;                  &quot;</span><span style="color: #575279">,</span><span style="color: #D7827E">0</span></span></code></pre>  <p>There are some strings at the bottom of the function. Whatever C compiler they used (most of IM2 was written in C), any string literals written as function parameters are placed at the bottom of the function like this, where it&#39;s easy to retrieve a PC-relative pointer to them. (PC-relative means referencing data relative to the <em>program counter</em>, or the current spot in execution, which is more efficient than using 32-bit absolute pointers.)</p> <h2>The file compression</h2><p>Of course, part of the reverse-engineering effort involves mapping and understanding the data files, not just the executable. I think it would be really cool to eventually add custom rooms&#x2F;levels to the game.</p> <p>The first thing I noticed was that even small files take a really long time to load. Each 10kb file would take 1-2 seconds to load; well, the disk activity was instantaneous, but it would be another second of silence before the file was &quot;done&quot; loading. Seemed likely that there was some kind of decompression going on.</p> <p>Here&#39;s what I found:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">file_unpack</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; a2 = pointer to source file</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">movea.l</span><span style="color: #575279">     FileSourcePtr,</span><span style="color: #D7827E">a2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">clr.l</span><span style="color: #575279">       </span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Get first 2 bytes from source, which is destination file size</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a2</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.l</span><span style="color: #575279">      </span><span style="color: #D7827E">d2</span><span style="color: #575279">,FileDestSize</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Set up d2 as a loop counter equal to the file size</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">subq.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">1</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; a2 = Source+2, a0 = Source+16</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">movea.l</span><span style="color: #575279">     </span><span style="color: #D7827E">a2</span><span style="color: #575279">,</span><span style="color: #D7827E">a0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">adda.l</span><span style="color: #575279">      #</span><span style="color: #D7827E">$0000000e</span><span style="color: #575279">,</span><span style="color: #D7827E">a0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; a1 = Destination pointer</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">movea.l</span><span style="color: #575279">     FileDestPtr,</span><span style="color: #D7827E">a1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Prep initial data</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.b</span><span style="color: #575279">      #</span><span style="color: #D7827E">$01</span><span style="color: #575279">,</span><span style="color: #D7827E">d3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.b</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d6</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Run decode_byte for every byte in the destination size</span></span><br><span class="line"><span style="color: #D7827E">.0</span><span style="color: #575279">:         </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       decode_byte</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">dbf</span><span style="color: #575279">         </span><span style="color: #D7827E">d2</span><span style="color: #575279">,.</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">rts</span></span></code></pre>  <p>I added the comments myself <em>after</em> coming to understand this whole algorithm, and hopefully they&#39;ll help me explain it as we go.</p> <p>We haven&#39;t gotten to the meat yet, but the above function sets up the source and destination pointers, and then runs <code>decode_byte</code> once for every byte in the final file. Basically, <code>decode_byte</code> will &quot;decode&quot; one byte of the destination file; this compression algorithm decodes one byte at a time.</p> <p>I&#39;m not going to nitpick code quality too hard, because it was written in the 80s. But goodness, it would have been an extremely simple optimization to just inline <code>decode_byte</code> rather than calling it as a function over and over again. That&#39;s Assembly 101 in C64 world. (These unpack functions were hand-written in Assembly, not compiled from C.)</p> <p>Calling (and returning from) a subroutine costs about 34 cycles in total. This is happening for <em>every byte</em> in a 15+kb file. If you do the math, that&#39;s over 500,000 cycles wasted. In the best case, that&#39;s about 75ms, but I wouldn&#39;t be surprised if it could cost up to 150ms depending on what else the system is doing. Not for <em>running</em> the subroutine, just for calling it.</p> <p>A 10-15% speed improvement is pretty dang good for such a small change to the code. But we&#39;d be here all day if our goal was to fully optimize this routine.</p> <p>Let&#39;s move on to <code>decode_byte</code>, shall we?</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">decode_byte</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Get next 2 bits in compressed file</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">clr.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d4</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; If not 00, enter dict lookup</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">tst.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d4</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bne.s</span><span style="color: #575279">       decode_byte_from_dict</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Otherwise, next 8 bits are a literal byte</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; (Literal mode) Write next byte</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.b</span><span style="color: #575279">      </span><span style="color: #D7827E">d4</span><span style="color: #575279">,(</span><span style="color: #D7827E">a1</span><span style="color: #575279">)+</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">rts</span></span><br><span class="line"></span><br><span class="line"><span style="color: #D7827E">next_bit</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; d6 = 1-byte buffer from source file</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">roxl.b</span><span style="color: #575279">      #</span><span style="color: #D7827E">1</span><span style="color: #575279">,</span><span style="color: #D7827E">d6</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; d4 = current bits being read/processed</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; (roxl shifts out of d6 into d4)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">roxl.b</span><span style="color: #575279">      #</span><span style="color: #D7827E">1</span><span style="color: #575279">,</span><span style="color: #D7827E">d4</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; d3 = counter, have we read 8 bits out of buffer?</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">rol.b</span><span style="color: #575279">       #</span><span style="color: #D7827E">1</span><span style="color: #575279">,</span><span style="color: #D7827E">d3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bcc.s</span><span style="color: #575279">       .e</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; If so, read next byte into buffer</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.b</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d6</span></span><br><span class="line"><span style="color: #D7827E">.e</span><span style="color: #575279">:         </span><span style="color: #286983">rts</span></span><br><span class="line"></span><br><span class="line"><span style="color: #D7827E">decode_byte_from_dict</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Dict lookup mode - get index based on 2-bit &quot;size ID&quot;</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; d1 = index size</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      </span><span style="color: #D7827E">d4</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">clr.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d4</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Index size 1, get next 1 bit for dict index 0-1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">subq.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">1</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">beq.s</span><span style="color: #575279">       .write</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Index size 2, get next 2 bits for dict index 2-5</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">subq.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">1</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">beq.s</span><span style="color: #575279">       .</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Index size 3, get next 3 bits for dict index 6-13</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bsr.s</span><span style="color: #575279">       next_bit</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">addq.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">4</span><span style="color: #575279">,</span><span style="color: #D7827E">d4</span></span><br><span class="line"><span style="color: #D7827E">.0</span><span style="color: #575279">:         </span><span style="color: #286983">addq.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">2</span><span style="color: #575279">,</span><span style="color: #D7827E">d4</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Write byte from dict to destination</span></span><br><span class="line"><span style="color: #D7827E">.write</span><span style="color: #575279">:     </span><span style="color: #286983">move.b</span><span style="color: #575279">      </span><span style="color: #D7827E">0</span><span style="color: #575279">(</span><span style="color: #D7827E">a2</span><span style="color: #575279">,</span><span style="color: #D7827E">d4</span><span style="color: #575279">.w),(</span><span style="color: #D7827E">a1</span><span style="color: #575279">)+</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">rts</span></span></code></pre>  <p>I&#39;ll summarize the way this function works:</p> <ol> <li>Get the next 2 bits in the source file, which denotes size of the dictionary index.</li> <li>If index size is 0, there&#39;s no dictionary match, so just read the next 8 bits from the source, which is our literal byte.</li> <li>Otherwise, get a byte from the dictionary based on index size:<ul> <li>Index size 1 (1-bit index): Entries 0-1</li> <li>Index size 2 (2-bit index): Entries 2-5</li> <li>Index size 3 (3-bit index): Entries 6-13</li> </ul> </li> </ol> <p>I don&#39;t know much about compression algorithms, so I can&#39;t immediately identify this as an implementation of a well-known algorithm, but I think there are some cool things about it.</p> <p>Basically, the dictionary contains what I assume are the most &quot;popular&quot; 14 bytes in the unpacked file. To compress the most popular byte, we&#39;d have <code>010</code> in binary: <code>01</code> for a 1-bit index, then <code>0</code> for index 0.</p> <p>So, the two most popular bytes get compressed down to 3 bits in every occurrence. Slightly less popular bytes will get compressed to 4 or 5 bits, depending on the index size. If we have <code>1011</code> in binary: <code>10</code> for a 2-bit index, then <code>11</code> for index 5. (It&#39;s 5 because the 2-bit index starts at 2; 0-1 are covered by the 1-bit index.)</p> <p>The downside is that if our byte isn&#39;t in the dictionary, then it takes ten bits to encode: <code>00</code> for &quot;literal mode&quot;, then the literal byte.</p> <p>Anyway, now we have a better idea of why these files take so long to unpack. Not only are we in subroutine hell, but the compressed file isn&#39;t even byte-aligned, so we have no choice but to process it by shifting 1 bit at a time. Sure, there are plenty of code optimizations we could make, but it&#39;s clear that this algorithm wasn&#39;t built for fast unpacking. In fact, it almost feels more suited to 8-bit machines, which are good at shifting bits one by one.</p> <h2>What about second compression?</h2><p>What if I told you...that once the file is unpacked, it gets unpacked <em>again</em>?</p> <p>I don&#39;t know how common this was, but these folks decided to double up on their compression, putting their files through two separate algorithms. Funny thing is, the second unpack routine is far more efficient—there are no subroutines, and the data is even 16-bit aligned.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">file_unpack_2</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Set up pointers and get source file size</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">movea.l</span><span style="color: #575279">     FileSourcePtr,</span><span style="color: #D7827E">a0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.l</span><span style="color: #575279">      FileSourceSize,</span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">movea.l</span><span style="color: #575279">     FileDestPtr,</span><span style="color: #D7827E">a1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">suba.l</span><span style="color: #575279">      </span><span style="color: #D7827E">a3</span><span style="color: #575279">,</span><span style="color: #D7827E">a3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Prep d4 and d5 with &quot;magic numbers&quot; in file header</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d4</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d5</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">subq.l</span><span style="color: #575279">      #</span><span style="color: #D7827E">4</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Start loop</span></span><br><span class="line"><span style="color: #D7827E">.loop</span><span style="color: #575279">:      </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">cmp.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d4</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">beq.s</span><span style="color: #575279">       .fillmode</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">cmp.w</span><span style="color: #575279">       </span><span style="color: #D7827E">d5</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bne.s</span><span style="color: #575279">       .literal</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Index mode (magic num in d5)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">clr.l</span><span style="color: #575279">       </span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">adda.w</span><span style="color: #575279">      </span><span style="color: #D7827E">d3</span><span style="color: #575279">,</span><span style="color: #D7827E">a3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">movea.l</span><span style="color: #575279">     </span><span style="color: #D7827E">a1</span><span style="color: #575279">,</span><span style="color: #D7827E">a2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">suba.l</span><span style="color: #575279">      </span><span style="color: #D7827E">d0</span><span style="color: #575279">,</span><span style="color: #D7827E">a2</span></span><br><span class="line"><span style="color: #D7827E">.iloop</span><span style="color: #575279">:     </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a2</span><span style="color: #575279">)+,(</span><span style="color: #D7827E">a1</span><span style="color: #575279">)+</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">subq.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">2</span><span style="color: #575279">,</span><span style="color: #D7827E">d3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bne.s</span><span style="color: #575279">       .iloop</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">subq.l</span><span style="color: #575279">      #</span><span style="color: #D7827E">6</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bra.s</span><span style="color: #575279">       .next</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Fill mode (magic num in d4)</span></span><br><span class="line"><span style="color: #D7827E">.fillmode</span><span style="color: #575279">:  </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a0</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">adda.w</span><span style="color: #575279">      </span><span style="color: #D7827E">d1</span><span style="color: #575279">,</span><span style="color: #D7827E">a3</span></span><br><span class="line"><span style="color: #D7827E">.floop</span><span style="color: #575279">:     </span><span style="color: #286983">move.w</span><span style="color: #575279">      </span><span style="color: #D7827E">d0</span><span style="color: #575279">,(</span><span style="color: #D7827E">a1</span><span style="color: #575279">)+</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">subq.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">2</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bne.s</span><span style="color: #575279">       .floop</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">subq.l</span><span style="color: #575279">      #</span><span style="color: #D7827E">6</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bra.s</span><span style="color: #575279">       .next</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Literal mode no magic num match)</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Just write the word we took from source file</span></span><br><span class="line"><span style="color: #D7827E">.literal</span><span style="color: #575279">:   </span><span style="color: #286983">move.w</span><span style="color: #575279">      </span><span style="color: #D7827E">d1</span><span style="color: #575279">,(</span><span style="color: #D7827E">a1</span><span style="color: #575279">)+</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">subq.l</span><span style="color: #575279">      #</span><span style="color: #D7827E">2</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">addq.l</span><span style="color: #575279">      #</span><span style="color: #D7827E">2</span><span style="color: #575279">,</span><span style="color: #D7827E">a3</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; Loop if there&#39;s more to do</span></span><br><span class="line"><span style="color: #D7827E">.next</span><span style="color: #575279">:      </span><span style="color: #286983">tst.l</span><span style="color: #575279">       </span><span style="color: #D7827E">d2</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">bne.s</span><span style="color: #575279">       .loop</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5; font-style: italic">; We&#39;re done, return destination size in d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">move.l</span><span style="color: #575279">      </span><span style="color: #D7827E">a3</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #286983">rts</span></span></code></pre>  <p>This one is an LZ77 compression algorithm! LZ77 is the basis for many modern compression formats that we use today. This is a word-aligned version of the algorithm that seems written specifically for being efficient on 16-bit hardware.</p> <p>The staple feature of LZ77 is <em>sliding-window compression</em>. As the data is getting unpacked, it can look back at previously-unpacked data and copy it.</p> <p>Here is a summary:</p> <ol> <li>Get the next 2 bytes from the source file.</li> <li>Check if it&#39;s a magic number:<ul> <li>Magic number 1: Fill mode, get next 4 bytes as parameters</li> <li>Magic number 2: Index mode, get next 4 bytes as parameters</li> <li>No magic number: Literal, just write bytes to destination</li> </ul> </li> <li>Perform a copy based on the mode:<ul> <li>Fill mode: Fill Y bytes of destination with 16-bit value X</li> <li>Index mode: Look back in destination X bytes, and copy Y bytes to current spot</li> </ul> </li> <li>Loop if we haven&#39;t reached the end of the source file yet.</li> </ol> <p>In the previous compression algorithm, every individual byte in the unpacked file had a representation in the packed file, each 3-10 bits in size. But here, a single operation can represent many bytes if that same pattern has appeared earlier in the file.</p> <p>&quot;Fill mode&quot; is a standout feature that isn&#39;t part of LZ77. The file can provide a 16-bit value to fill some arbitrary amount of space with. This takes 6 bytes (2 for magic number, 2 for fill value, 2 for size).</p> <p>Without &quot;fill mode&quot;, we can do the exact same thing in 8 bytes using a literal and &quot;index mode&quot;. (Write the literal, then index back 2 bytes and copy however much you want to fill.) Still, the inclusion of &quot;fill mode&quot; is slightly more efficient, and it&#39;s probably an easier job for the compression software to figure out when packing the file.</p> <p>Another interesting observation: We don&#39;t even know the unpacked file size until we&#39;re done unpacking. But the game is only unpacking data structures of a predetermined size, so this isn&#39;t an issue for the game. Otherwise, I guess it would be included in the header, like in the previous compression format.</p> <h2>Results of the compression</h2><p>Taking one of the game files, <code>PECHARG.SQ</code>, let&#39;s test the compression efficiency of the packers that were used for IM2. I&#39;ll also compare to some modern compression algorithms.</p> <ul> <li>Uncompressed: 29,112 bytes</li> <li>LZ77 (Original IM2 packer): 18,082 bytes</li> <li>LZ77 + Dictionary (Original IM2 packer): 13,602 bytes</li> <li>LZ4: 12,612 bytes</li> <li>ZX0: 9,596 bytes</li> <li>LZMA2: 8,556 bytes</li> </ul> <p>Not bad! The 1988 packers are within reach of LZ4, but the clear winner is LZMA2 (7-Zip). Not that LZMA2 decompression is likely feasible to implement on such old hardware with any decent performance.</p> <p>Given that, the most notable one here is ZX0. ZX0 is a modern compression algorithm based on LZ77, and it&#39;s optimized for 8-bit computers and other retro hardware. It&#39;s extremely fast to decompress, and it can do in-place decompression. That means you can unpack the file right on top of the packed file, without having to allocate memory for both separately (you just need to allocate enough for the unpacked file).</p> <p>I don&#39;t have an exact benchmark, but some quick calculations tell me that ZX0 would decompress <code>PECHARG.SQ</code> in 150ms or less. That&#39;s at least a 6-10x speed improvement over the original unpacker(s), and at ~70% the file size.</p> <p>Part of ZX0 is that while the decompression is extremely fast, the compression is not. Thankfully, we have modern CPUs to do the compressing, and I can compress <code>PECHARG.SQ</code> in about 500ms.</p> <p>Even if they had ZX0 in 1988, it would be impossible to use. Fellow community member alpine9000 took the ZX0 compressor <a href="https://github.com/emmanuel-marty/salvador">salvador</a> and compiled it for Amiga, just to see how it would run. On a 14MHz 68020, it took about an hour to compress that same file. That&#39;s <em>maybe</em> okay, until I noticed that it took 88MB of RAM to do the job. Back then, the lucky ones had a 20MB hard disk, and not likely more than 1MB of RAM (maybe 4-8MB if they were really maxed out).</p> <p>This is just one example of how we can use modern tools and knowledge to breathe new life into old hardware. With a format like ZX0, it becomes feasible to decompress assets practically in realtime, enabling new gameplay experiences that would not have been possible in 1988. In my own Amiga game, I&#39;m doing lots of realtime asset decompression mid-gameplay, probably something that was hardly ever considered back then. For example, check out <a href="/coding-the-anime-woosh-screen-on-amiga/">this effect</a> I made, which takes advantage of ZX0&#39;s fast decompression.</p> <p>If you have an idea of what the source software might be for the compression formats I found in IM2, I&#39;d love to hear about it!</p> ]]></content:encoded>
      
      
      
      
      <comments>https://dansalva.to/decoding-impossible-mission-2-for-amiga/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Hot loading code on Amiga, the dumb way</title>
      <link>https://dansalva.to/hot-loading-code-on-amiga-the-dumb-way/</link>
      <guid>https://dansalva.to/hot-loading-code-on-amiga-the-dumb-way/</guid>
      <pubDate>Tue, 13 Aug 2024 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;In my opinion, an understated programming skill—one that only comes with experience—is knowing when to be &amp;quot;good practice&amp;quot;, when to be clever, and when to be dumb. Today, we&amp;#39;re being dumb.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>In my opinion, an understated programming skill—one that only comes with experience—is knowing when to be &quot;good practice&quot;, when to be clever, and when to be dumb. Today, we&#39;re being dumb.</p> <span id="more"></span>  <p>The codebase of <em>Magicore Anomala</em> has steadily increased in size over the years—it&#39;s currently sitting at about 70kb. And it&#39;s only going to get much larger, perhaps eventually even pushing against the classic Amiga&#39;s limit of 512kb general-purpose RAM. The more code, the less room for game.</p> <p>So to keep things sustainable, I need to be able to load code from disk only when it&#39;s needed (like level-specific code). These code &quot;modules&quot; need full, unrestricted access to the game&#39;s codebase and memory.</p> <p>The situation is this: Let&#39;s say a code module wants to call a game function, or adjust a variable such as the player&#39;s position. These functions and variables can exist anywhere in memory—it changes every time the game is loaded. So, how do the code modules determine where the game&#39;s functions and variables are located, if they&#39;re standalone units that exist separately from the main program?</p> <h2>Variables in Assembly</h2><p>A brief explanation on how &quot;variables&quot; work in Assembly. Consider this example:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">    #</span><span style="color: #D7827E">10</span><span style="color: #575279">,pl_PosX</span></span></code></pre>  <p>You might infer that this simple instruction adds 10 to the player&#39;s X position, and you&#39;d be correct. However, <code>pl_PosX</code> is meaningless to the computer; it needs to be translated into a memory address, specifically the address that holds the player&#39;s X position.</p> <p>In simple terms, programs have a &quot;data&quot; section where these variables are defined. The assembler knows that <code>pl_PosX</code> is, say, 0x324 bytes into the data section. When creating the final executable, a table is added to the bottom of the program (the <em>relocation table</em>) that tracks references like this. It tells the OS that this instruction is trying to access <code>DATA+0x324</code>.</p> <p>When the OS loads the executable, it finds some spot in memory to load the data section—let&#39;s say the data section gets loaded into address 0x20000. Now that we finally have a real memory address for our data, the OS can go through the relocation table and replace our instruction with this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">    #</span><span style="color: #D7827E">10</span><span style="color: #575279">,</span><span style="color: #D7827E">$20324</span></span></code></pre>  <p>FYI, these variables are usually called &quot;symbols&quot; or &quot;labels&quot;, because they&#39;re doing little more than labeling a spot in memory.</p> <h2>Problem solved?</h2><p>Okay, so there&#39;s already a good mechanism for performing &quot;relocation&quot; to convert these variables to absolute memory addresses when the code is loaded. However, I chose not to go down that route, for a couple reasons.</p> <p>The biggest reason is that my code almost never uses this absolute addressing method when accessing memory. I use <em>base-relative addressing</em>, which is both faster and smaller.</p> <h2>All your base are belong to us</h2><p>Base-relative addressing is a technique where we access data by using a pointer to the data section, instead of hard-coding all those absolute memory addresses. The pointer is loaded into a register when the program starts, and it&#39;s expected to persist all throughout the program.</p> <p>Let&#39;s say I have a pointer to my data section in register <code>a4</code>. Now, in my code, I can do this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">    #</span><span style="color: #D7827E">10</span><span style="color: #575279">,pl_PosX(</span><span style="color: #D7827E">a4</span><span style="color: #575279">)</span></span></code></pre>  <p>As long as I tell the assembler that <code>a4</code> is my base register, it&#39;s smart enough to see <code>pl_PosX</code> and replace it with the correct offset into the data section:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">    #</span><span style="color: #D7827E">10</span><span style="color: #575279">,</span><span style="color: #D7827E">$324</span><span style="color: #575279">(</span><span style="color: #D7827E">a4</span><span style="color: #575279">)</span></span></code></pre>  <p>This instruction adds 10 to the memory address <code>(a4)+0x324</code>, which, in our example, is where <code>pl_PosX</code> is stored. This translation is done by the assembler, without the OS having to take care of relocation or anything like that.</p> <p>As opposed to using the absolute address, this instruction is 4 cycles faster and 2 bytes smaller. Throughout the entire program, that&#39;s a lot of savings.</p> <p>Furthermore, since we can access our entire data section through <code>a4</code>, it enables us to build <em>position-independent code</em>—code where no relocation is required. That means I can build code as standalone, raw binary files that contain nothing but the exact code and data I write; not an object file, not an executable, just the exact binary output of my source file. That&#39;s the key ingredient for my &quot;dumb&quot; code modules.</p> <h2>Fighting dumb with dumb</h2><p>There&#39;s another problem, and it&#39;s during the build process. The main program knows that <code>pl_PosX</code> is at position 0x324 of the data section, because the data section is included in the main build. But my standalone code modules are effectively each their own independent program, so they have no idea what <code>pl_PosX</code> or any other symbol is referring to. How can I get that information over to the modules?</p> <p>There is probably a decent, clever way to do this using advanced assembler and&#x2F;or linker features. But we&#39;re being dumb today, which means I&#39;m sidestepping that added complexity and just writing a simple script.</p> <p>When I build the main program, I use a build flag to generate a symbol map. The map file looks something like this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">Symbols of DATA:</span></span><br><span class="line"><span style="color: #575279">  0x00000000 _DataStart: local reloc, size 0</span></span><br><span class="line"><span style="color: #575279">  0x00000000 atk_Params: local reloc, size 0</span></span><br><span class="line"><span style="color: #575279">  0x00000080 os_GraphicsName: local reloc, size 0</span></span><br><span class="line"><span style="color: #575279">  0x00000092 os_DOSName: local reloc, size 0</span></span><br><span class="line"><span style="color: #575279">  0x0000009e scr_YPosTable: local reloc, size 0</span></span><br><span class="line"><span style="color: #575279">  0x0000029e scr_ShakeTable: local reloc, size 0</span></span><br><span class="line"><span style="color: #575279">  0x000002cc lvl_PathList: local reloc, size 0</span></span><br><span class="line"><span style="color: #575279">  0x000002e6 lvl_Paths: local reloc, size 0</span></span></code></pre>  <p>This is just an excerpt, but you get the idea. All I do is take this map file and put it through a script that reformats it into an include file, <code>symbols.i</code>, that my code modules can use.</p> <p>The output looks like this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279; font-style: italic">atk_Params</span><span style="color: #575279">              = </span><span style="color: #D7827E">$0000</span></span><br><span class="line"><span style="color: #575279; font-style: italic">os_GraphicsName</span><span style="color: #575279">         = </span><span style="color: #D7827E">$0080</span></span><br><span class="line"><span style="color: #575279; font-style: italic">os_DOSName</span><span style="color: #575279">              = </span><span style="color: #D7827E">$0092</span></span><br><span class="line"><span style="color: #575279; font-style: italic">scr_YPosTable</span><span style="color: #575279">           = </span><span style="color: #D7827E">$009e</span></span><br><span class="line"><span style="color: #575279; font-style: italic">scr_ShakeTable</span><span style="color: #575279">          = </span><span style="color: #D7827E">$029e</span></span><br><span class="line"><span style="color: #575279; font-style: italic">lvl_PathList</span><span style="color: #575279">            = </span><span style="color: #D7827E">$02cc</span></span><br><span class="line"><span style="color: #575279; font-style: italic">lvl_Paths</span><span style="color: #575279">               = </span><span style="color: #D7827E">$02e6</span></span></code></pre>  <p>Now, in my code module, I just include <code>symbols.i</code>, and I get access to every symbol in my program as a position-independent offset.</p> <p>This isn&#39;t a one-time thing; it needs to be done on every single build. The reason is simple: If I insert some new data into the program, it will change all the offsets of the data that comes after it.</p> <p>That&#39;s not an issue, because it takes virtually no time at all to &quot;build&quot; <code>symbols.i</code>. I build the main program, then <code>symbols.i</code>, then the code modules.</p> <p>By the way, I use another base register for the code section, which gives my modules full access to all the functions in my main program, not just the data.</p> <h2>Modularity and modding</h2><p>Rather than come up with some library or API, my code modules have full, unrestricted access to the entire program&#39;s memory space and functions, as though they were a part of the main program itself.</p> <p>I&#39;m doing this in part to make my life easier, but I&#39;m also doing it for the possibility of modding. Magicore&#39;s level files are 100% self-contained, meaning they contain all of their own assets, code, and data needed in a single package. That basically means new levels can be added to the game simply by dropping a new level file into the game directory. The level is all-powerful, because it can run its own code, so the sky is the limit for modders. They have complete access to everything.</p> <p>And before you lecture me about the security implications of that, I&#39;d like to remind you that this game is for the goddamn Amiga. What are they gonna do, play techno music at me?</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/hot-loading-code-on-amiga-the-dumb-way/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Coding the anime &quot;woosh&quot; screen on Amiga</title>
      <link>https://dansalva.to/coding-the-anime-woosh-screen-on-amiga/</link>
      <guid>https://dansalva.to/coding-the-anime-woosh-screen-on-amiga/</guid>
      <pubDate>Fri, 23 Feb 2024 00:00:00 GMT</pubDate>
      
      <description>&lt;video class=&quot;post-video&quot; playsinline controls muted&gt;&lt;source src=&quot;/images/20240223/1.mp4#t=0.01&quot; type=&quot;video/mp4&quot;&gt;Your browser does not support the video tag.&lt;/video&gt;  &lt;p&gt;The Amiga was a spectacle of graphics and sound when it debuted in 1985. While it can trivially display colorful images like in the above example, doing so in the context of a game engine presents a lot of unique challenges.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<video class="post-video" playsinline controls muted><source src="/images/20240223/1.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p>The Amiga was a spectacle of graphics and sound when it debuted in 1985. While it can trivially display colorful images like in the above example, doing so in the context of a game engine presents a lot of unique challenges.</p> <span id="more"></span>  <p>If you haven&#39;t seen the gameplay proof-of-concept video for Magicore Anomala, you can check it out <a href="https://www.youtube.com/watch?v=4SB20aFHc08">here</a>.</p> <h2>Challenge 1: The RAM requirements</h2><p>A run-of-the-mill Amiga 500 has 512kb of &quot;Chip RAM&quot; and 512kb of expansion RAM (sometimes called &quot;Slow RAM&quot;), which is the platform target for Magicore. Only Chip RAM can be used by the Amiga chipset to present graphics and sound, which makes it much more valuable—all other kinds of expansion RAM can only be accessed by the CPU.</p> <p>The fullscreen character graphic (CG) is a 320x240 bitmap with 32 colors, which takes up 48kb of RAM uncompressed. That is a <em>lot</em>. Between all the common assets, level data, and screen memory allocations, we don&#39;t want to afford that kind of overhead on the RAM.</p> <p>Thankfully, I recently added support for asset compression, using the ZX0 compression format. Compressed, the CG is about 8kb, which is acceptable.</p> <p>When the level assets are loaded, the compressed CG is loaded into the expansion RAM. Then, right before it gets displayed, I unpack it into Chip RAM.</p> <p>The trick is that instead of finding 48kb of free Chip RAM to use, I reuse other parts of screen memory:</p> <ul> <li>The room background image (the grassy field, in this example)</li> <li>The screen layer used to render hazardous objects (not shown in the video)</li> <li>The textbox screen area (as seen in the gameplay proof-of-concept video)</li> </ul> <p>All three of these memory regions are contiguous in RAM, and they come out to 48,000 bytes, which is the <em>exact</em> size of the CG!</p> <p>It&#39;s also okay to overwrite the room background image, because we can restore it after the CG is done being displayed.</p> <p>In the video below, I force the CG to always be shown on the screen, so we can watch how the data gets decompressed and overwritten.</p> <video class="post-video" playsinline controls muted><source src="/images/20240223/2.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p>Pretty cool, right? As you can see, the CG takes maybe 500ms to fully decompress. But that &quot;loading time&quot; is hidden into the flow of the cutscene.</p> <h2>Challenge 2: The &quot;screen split&quot; effect</h2><p>I was initially thinking about doing a vertical wipe for the screen transition. But for that to look nice, the wipe would have to be a gradient, adjusting the color palette every scanline. That&#39;s pretty possible, but the coprocessor (copper) alone struggles to set all 32 colors in a single horizontal blank, and—I&#39;ll be honest—I didn&#39;t want to deal with &quot;racing the beam&quot;.</p> <p>The screen split effect is easier to pull off, and I think it looks cooler to the common viewer. In fact, the copper was practically purpose-designed for this effect! Check out <a href="https://www.youtube.com/watch?v=YlAhRJjOhDg">this</a> video, which demonstrates a similar effect built right into Amiga Workbench.</p> <p>This demonstrates two special features of Amiga working in tandem:</p> <ol> <li>The copper runs in parallel to the CPU with its own instruction list. Those instructions can tell it to change certain hardware registers at specific lines on the screen.</li> <li>The screen memory can be changed to <em>anywhere</em> in Chip RAM by setting screen pointers in the hardware registers. That means you can have multiple bitmap screens and switch between them whenever you want—every frame (e.g. for double buffering), or even multiple times in one frame.</li> </ol> <p>Let&#39;s say my &quot;main&quot; screen memory begins at <code>0x20000</code>. Normally, I instruct the copper to arm the bitplane DMA registers with this address. Once I enable the bitplanes, the DMA happily marches through this region of memory, drawing its data to the screen while incrementing the address pointer as it goes.</p> <p>There is an interesting trick. Let&#39;s say each horizontal line takes up <code>0x100</code> bytes of screen memory. What if I set the screen pointer to <code>0x20800</code> instead?</p> <p>The screen will appear to &quot;scroll up&quot; by 8 lines, because the screen officially starts 8 lines down into memory.</p> <p>I have the top half of the split scroll up in this fashion. Then, at the split point, the copper is instructed to shut off bitplane DMA (and change the BG color to red).</p> <p>Now, all the bitplane-related hardware registers are effectively frozen in time. Once we reach the bottom of the split, the copper resets the BG color and resumes bitplane DMA. The display picks up right where it left off, just lower down on the screen!</p> <p>If you&#39;re wondering what this looks like, here is the copperlist used for the effect:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">vs_TCop</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">                 </span><span style="color: #9893A5; font-style: italic">; Wait for top split to end</span></span><br><span class="line"><span style="color: #D7827E">vs_TCopTop</span><span style="color: #575279">:      </span><span style="color: #286983">dc.w</span><span style="color: #575279">   </span><span style="color: #D7827E">$9007</span><span style="color: #575279">,</span><span style="color: #D7827E">$fffe</span></span><br><span class="line"><span style="color: #575279">                 </span><span style="color: #9893A5; font-style: italic">; Set BG color</span></span><br><span class="line"><span style="color: #D7827E">vs_TCopColorTop</span><span style="color: #575279">: </span><span style="color: #286983">dc.w</span><span style="color: #575279">   COLOR0</span><span style="color: #D7827E">0</span><span style="color: #575279">,</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">                 </span><span style="color: #9893A5; font-style: italic">; Disable sprite and bitplane DMA</span></span><br><span class="line"><span style="color: #575279">                 </span><span style="color: #286983">dc.w</span><span style="color: #575279">   DMACON,</span><span style="color: #D7827E">$0120</span></span><br><span class="line"><span style="color: #575279">                 </span><span style="color: #9893A5; font-style: italic">; Wait for bottom split to start</span></span><br><span class="line"><span style="color: #D7827E">vs_TCopBottom</span><span style="color: #575279">:   </span><span style="color: #286983">dc.w</span><span style="color: #575279">   </span><span style="color: #D7827E">$90e1</span><span style="color: #575279">,</span><span style="color: #D7827E">$fffe</span></span><br><span class="line"><span style="color: #575279">                 </span><span style="color: #9893A5; font-style: italic">; Restore BG color</span></span><br><span class="line"><span style="color: #D7827E">vs_TCopColorOld</span><span style="color: #575279">: </span><span style="color: #286983">dc.w</span><span style="color: #575279">   COLOR0</span><span style="color: #D7827E">0</span><span style="color: #575279">,</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">                 </span><span style="color: #9893A5; font-style: italic">; Enable sprite and bitplane DMA</span></span><br><span class="line"><span style="color: #575279">                 </span><span style="color: #286983">dc.w</span><span style="color: #575279">   DMACON,</span><span style="color: #D7827E">$8120</span></span></code></pre>  <p>That&#39;s it! Every frame, I use the CPU to adjust <code>vs_TCopTop</code> and <code>vs_TCopBottom</code> based on the current width of the split. (Not shown: Adjusting the screen pointer for the top split, as described above.)</p> <h2>Challenge 3: The &quot;motion lines&quot;</h2><p>You can&#39;t reach full anime without having the lines that go &quot;woosh&quot; in the background.</p> <p>I use sprites to draw the lines, which is a good choice because they can be drawn and moved fully independently from screen memory. The issue is that Amiga sprites are both very limited and very complicated.</p> <h3>Sprite colors</h3><p>Sprites share a color palette with bitplanes, meaning I want to use up as few colors as possible. The sprite is only 3 colors, leaving 28 for the CG (and 1 for the background).</p> <p>The problem is that different sprites use different colors in the palette. The first two sprites use colors 16-19, the second two sprites use colors 20-23, and so on.</p> <p>This changes if you combine sprites. By &quot;attaching&quot; two sprites together, they become one sprite with a 16-color palette (colors 16-31). That means for the motion lines, I can use 4 attached sprites, and use only colors 29-31 in the graphic. It&#39;s a silly workaround for a silly limitation.</p> <h3>Reusing a sprite graphic</h3><p>The first 4 bytes of a sprite graphic are actually &quot;control bits&quot; that tell the Amiga the position and height of the sprite. That is actually a pain—what if we want to draw the same graphic in multiple locations?</p> <p>My first thought was to manually set the hardware registers for sprite control bits, but I simply could not get the sprite to display on screen when doing this. Amiga sprite DMA works similarly to bitplane DMA; it has a pointer to the sprite data that it walks through in order to display it to the screen. But when manually setting the control bits, I just couldn&#39;t get it to do that. I&#39;m sure it can be done, but I decided to find another way.</p> <p>I instead created 8 fake sprites that are only 4 bytes large—<em>just</em> the control bits. I set all the sprite pointers to those fake sprites.</p> <p>Around line 19, the sprite DMA looks at all the sprite pointers and arms itself with the control bits, preparing to draw the data to the screen at the specified position.</p> <p>Once it does so, I pull a switcheroo: I change all the sprite pointers to the &quot;motion line&quot; graphic. Now, the DMA is armed to draw all the sprites at different positions, but using the same graphic.</p> <p>Again, this can be trivially done in the copperlist:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">             </span><span style="color: #9893A5; font-style: italic">; Set dummy sprite pointers (to arm control bits)</span></span><br><span class="line"><span style="color: #D7827E">vs_CopSprP</span><span style="color: #575279">:  COPPTR     SPR0PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR1PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR2PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR3PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR4PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR5PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR6PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR7PT</span></span><br><span class="line"><span style="color: #575279">             </span><span style="color: #9893A5; font-style: italic">; Wait for line 19</span></span><br><span class="line"><span style="color: #575279">             </span><span style="color: #286983">dc.w</span><span style="color: #575279">       </span><span style="color: #D7827E">$14df</span><span style="color: #575279">,</span><span style="color: #D7827E">$fffe</span></span><br><span class="line"><span style="color: #575279">             </span><span style="color: #9893A5; font-style: italic">; Now that control bits are armed, set data pointers</span></span><br><span class="line"><span style="color: #D7827E">vs_CopSprP2</span><span style="color: #575279">: COPPTR     SPR0PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR1PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR2PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR3PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR4PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR5PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR6PT</span></span><br><span class="line"><span style="color: #575279">             COPPTR     SPR7PT</span></span></code></pre>  <h3>Sprites don&#39;t get drawn when bitplanes are off</h3><p>Before the CG reaches the top of the screen, there is a bunch of empty space between the top of the screen and the start of the CG. If bitplanes are enabled during this time, they will draw junk data to the screen. Here is an example of that:</p> <video class="post-video" playsinline controls muted><source src="/images/20240223/3.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p>Dang, that&#39;s actually kind of cool-looking. Missed opportunity?</p> <p>In that screen region, we want to disable bitplanes so that the DMA doesn&#39;t run away with junk data like that.</p> <p>One problem: If you disable bitplanes, then sprites also don&#39;t get drawn! I don&#39;t know why it works like this, but it does.</p> <video class="post-video" playsinline controls muted><source src="/images/20240223/4.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p>See how the lines only get drawn within the bounds of the CG?</p> <p>My solution was to keep just 1 bitplane enabled, and set the screen pointer to empty data. That way, it <em>is</em> drawing to the screen, but it&#39;s just drawing nothing.</p> <p>But where do I find a screen full of empty data? Thankfully, I don&#39;t have to. There are &quot;bitplane modulo&quot; registers (<code>BPLMOD1</code> and <code>BPLMOD2</code>) that let you increment the screen pointer by a certain amount after each line. This is useful for interleaved bitplanes, which I won&#39;t get into here.</p> <p>At 1 bit per pixel, a 320-pixel line is 320 bits, or 40 bytes. If I set <code>BPLMOD1</code> to -40, then it will go backwards 40 bytes after each line, causing it to draw the same 40 bytes over and over, on each new line.</p> <p>That means I only need to find 40 bytes of empty data, which is easy to find; my screen has &quot;safety margins&quot; which hold nothing but junk data from objects that are drawn beyond the screen borders. I can just clear out the first 40 bytes of the safety margin, and I&#39;m good to go.</p> <h2>Conclusion</h2><p>I originally wasn&#39;t sure if I would include CGs like this in the game, because I was worried about the RAM requirements. But now that I have data compression implemented, I proved that the overhead is extremely reasonable, and I can add this extra bit of flair to Magicore.</p> <p>There were a lot of other small challenges I didn&#39;t go over here, like getting the bottom of the 100px motion line to not abruptly disappear after the sprite leaves the top of the screen. But the ones I covered were the most interesting to me, especially how they involve unique quirks of Amiga hardware.</p> <p>Also, if you like Amiga, you might have noticed that this effect doesn&#39;t use the blitter at all! If you want to read a blitter-related post, try <a href="/getting-clever-with-the-amiga-blitter">this</a> one.</p> <p>Amiga is so great at displaying colorful graphics that I hope I can impress people with its capabilities today, just as they were in the latter half of the 80s.</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/coding-the-anime-woosh-screen-on-amiga/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Apple Vision Pro and ADHD</title>
      <link>https://dansalva.to/apple-vision-pro-and-adhd/</link>
      <guid>https://dansalva.to/apple-vision-pro-and-adhd/</guid>
      <pubDate>Sat, 03 Feb 2024 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;Apple Vision Pro reminds me that computers have become too efficient.&lt;/p&gt; &lt;p&gt;I&amp;#39;m currently typing this blog post in a floating window in the middle of snow-covered Yosemite. I am a floating body manifested into the transcendental Tim Cook matrix. I look into the sky and follow the soft clouds as they pass overhead. They slowly part, revealing Steve Jobs&amp;#39; smiling face watching over me from the heavens above. I am merely one more thing.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>Apple Vision Pro reminds me that computers have become too efficient.</p> <p>I&#39;m currently typing this blog post in a floating window in the middle of snow-covered Yosemite. I am a floating body manifested into the transcendental Tim Cook matrix. I look into the sky and follow the soft clouds as they pass overhead. They slowly part, revealing Steve Jobs&#39; smiling face watching over me from the heavens above. I am merely one more thing.</p> <span id="more"></span>  <p>When I walked into the Apple Store yesterday, the first question I had for the assistant was the return policy on Apple Vision Pro. I have about 13 days left to decide if this thing is worth $3,500. My early conclusion is that if you&#39;re an enthusiast for entertainment and you have a lot of money to spend on it, the Apple Vision Pro might be for you. Even the folks at the Apple Store seemed convinced that &quot;entertainment&quot; is the current selling point of this device.</p> <p>I don&#39;t fall into that category, so I&#39;m currently trying to figure out what the hell to use it for.</p> <p>I tried watching a YouTube video placed over my countertop while making coffee. The display quality is stunning, but the video pass-through is starkly disappointing—granted, it&#39;s probably the best there is, but the world around me appears grainy and poorly-lit. On top of that, I&#39;m definitely a bit clumsier with my hands.</p> <p>iPad apps also feel clumsy. In visionOS, buttons highlight as your eyes pass over them, indicating they&#39;re selected. This does not always happen in iPad apps. In Octal (a Hacker News reader), I have to guess whether I&#39;m looking hard enough at the correct element to bring up the story or the comments. In Obsidian, pressing the corner buttons to expand the sidebar is nearly impossible.</p> <p>If I&#39;m staring at the center of a window, the window edges tend to shimmer as I move my head around. This tells me visionOS uses higher-quality anti-aliasing wherever your eyes are focused. Cool performance trick, but human peripheral vision is highly sensitive to motion, which makes the shimmering somewhat distracting.</p> <p>I&#39;m sure I sound pretty underwhelmed by my experience. Perhaps, but the most exciting thing to me about Apple Vision Pro is that it has flaws like this. Flaws bring charm to gadgets. They make you use them more selectively and deliberately. Computers, phones and tablets have gotten boring because there is no longer such deliberate use—they are always there, able to do anything and instantly switch tasks as fast as your fingers can move.</p> <h2>That&#39;s where ADHD comes in</h2><p>It&#39;s not strictly an ADHD problem that technology has gotten unbelievably distracting. But as someone with ADHD, I feel acutely aware of my relationship to technology in this way. It is incredibly hard to focus on a single task and clear my mind of the &quot;itch to screw around&quot; when all the technology around me is so goddamn fast and easy to use.</p> <p>That&#39;s what makes Apple Vision Pro interesting for me. Navigating with your eyes and fingers is (currently) not as fast or easy as using a mouse or a touchscreen. Positioning windows in 3D space also takes time.</p> <p>This has an interesting effect. I feel less inclined to mindlessly open and use apps, because it has become a fundamentally mindful gesture to do so. I actually have to go out of my way to bring up the Home Screen, launch an app, position it where I want it, and interact with it by looking directly at it. I can&#39;t do these things on autopilot.</p> <p>Putting myself in an &quot;environment&quot; also seems to have the effect of eliminating distractions from the outside world. I&#39;m not glancing around at the stuff in my office every time I pause to think.</p> <p>There is an amusing irony here, the way that strapping a $3,500 computer to my face is making my interaction with the digital world more deliberate and mindful.</p> <p>So, am I going to keep this thing after all?</p> <p>It&#39;s still a toss-up. I have a decent idea for a productivity app I want to build for Apple Vision Pro. If I make solid progress on it this week, I might keep the headset under justification for business.</p> <p>However, 14 days is unfortunately not enough time for me to decide whether Apple Vision Pro is a true quality-of-life improvement for an ADHD lifestyle. Maybe I&#39;m still swept up in the novelty of it all. If I do end up keeping it, I&#39;ll definitely write a follow-up in the near future. But for this price tag, I can&#39;t justify the purchase on a &quot;maybe&quot;.</p> ]]></content:encoded>
      
      
      
      
      <comments>https://dansalva.to/apple-vision-pro-and-adhd/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Resurrecting a prototype Chromebook with Arch Linux</title>
      <link>https://dansalva.to/resurrecting-a-prototype-chromebook-with-arch-linux/</link>
      <guid>https://dansalva.to/resurrecting-a-prototype-chromebook-with-arch-linux/</guid>
      <pubDate>Sun, 07 Jan 2024 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;In 2011, I was selected to be a part of Google&amp;#39;s &lt;a href=&quot;https://techcrunch.com/2010/12/07/google-announces-chrome-os-pilot-program-12-1-inch-notebook-cr-48/&quot;&gt;Chrome OS Test Pilot Program&lt;/a&gt;. I received a free Google Cr-48 laptop, and it was an absolutely charming machine.&lt;/p&gt; &lt;p&gt;The rubbery, matte-black chassis had absolutely no branding anywhere on it. Upon opening the lid, the system would boot into Chrome OS, which, at the time, was literally nothing more than a fullscreen Chrome browser—no desktop, no apps, nothing.&lt;/p&gt; &lt;p&gt;I used the Cr-48 throughout college, often as a thin client that would connect to my desktop PC via Chrome Remote Desktop.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>In 2011, I was selected to be a part of Google&#39;s <a href="https://techcrunch.com/2010/12/07/google-announces-chrome-os-pilot-program-12-1-inch-notebook-cr-48/">Chrome OS Test Pilot Program</a>. I received a free Google Cr-48 laptop, and it was an absolutely charming machine.</p> <p>The rubbery, matte-black chassis had absolutely no branding anywhere on it. Upon opening the lid, the system would boot into Chrome OS, which, at the time, was literally nothing more than a fullscreen Chrome browser—no desktop, no apps, nothing.</p> <p>I used the Cr-48 throughout college, often as a thin client that would connect to my desktop PC via Chrome Remote Desktop.</p> <span id="more"></span>  <p>Unfortunately, there were several factors that would prevent my beloved free laptop from lasting much longer:</p> <ol> <li>Once Chrome OS got its UI overhaul and included a &quot;real&quot; desktop interface, performance was noticeably much more sluggish.</li> <li>The Intel Atom N455 CPU included in the Cr-48 did not have hardware decoding for H.264 video. As H.264 took over the Web (including remote desktop software!), the poor Cr-48 couldn&#39;t keep up.</li> <li>The thing was simply not built to last. The rubber coating started to deteriorate, the charging port would come loose, and the hinge would break. This was the fate of almost every Cr-48 that saw regular use.</li> </ol> <p>Despite this, the Cr-48 never stopped being an absolutely wonderful tinkerer&#39;s machine. Inside the laptop, there is a switch that disables BIOS write protection, allowing you to flash a custom BIOS and install whatever OS you want.</p> <p><img src="/images/20240106/1.jpg"></p> <div class="image-description">My Cr-48 running a snazzy Arch Linux setup</div>  <h2>The specs</h2><p>The Cr-48 was considered a slow device even when it was new. Here is what we&#39;re working with:</p> <ul> <li>Intel Atom N455, single-core 1.6GHz CPU</li> <li>2GB DDR3 RAM</li> <li>16GB mSATA SSD</li> <li>802.11n Wi-Fi</li> <li>1280x800 LCD display</li> </ul> <p>Could be worse, but not much worse. The single-core CPU is seriously rough, and the RAM limitation puts most mainstream OS choices out of the question.</p> <h2>Before we begin</h2><p>For brevity, I don&#39;t list every single change I make to my config files.</p> <p>Please see <a href="https://github.com/dansalvato/cr48-dotfiles">here</a> for the collection of config files that are referenced in this post.</p> <p>If you&#39;re setting up your own installation, feel free to check out these config files as I reference them in this post, and adopt what you want from them.</p> <h2>Installing Arch Linux</h2><p>I&#39;ve never used Arch Linux, but I thought it would be a fantastic choice for such a low-powered device. It&#39;s a famously barebones distro that expects the user to meticulously configure their hardware and desktop environment from the ground up.</p> <p>First, we need to disable BIOS write protection, which I won&#39;t cover in this post, but you can find a nice guide <a href="https://medium.com/@capuccino/revive-your-cr-48-install-any-os-on-the-fabled-chromebook-b17a763654d9">here</a>.</p> <p>Next, we&#39;ll create an Arch Linux bootable USB drive, stick it in the Cr-48&#39;s <em>only</em> USB port, and boot that sucker up.</p> <p><img src="/images/20240106/1.png"></p> <p>(I&#39;m capturing the screen using the Cr-48&#39;s VGA monitor port, which outputs in 1024x768 and will cut off the left side of the screen until we get a proper desktop environment going.)</p> <p>First things first, we want to connect to Wi-Fi using <code>iwctl</code>. You can do this in a single command—just add in your SSID and password where indicated:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">iwctl</span><span style="color: #575279"> </span><span style="color: #286983">--passphrase</span><span style="color: #575279"> [passphrase] station wlan0 connect </span><span style="color: #797593">[</span><span style="color: #575279">SSID</span><span style="color: #797593">]</span></span></code></pre>  <p>Alrighty, test your connection with a <code>ping</code> and we should be online!</p> <p><img src="/images/20240106/2.png"></p> <p>Next up, run <code>archinstall</code> and configure all the settings. <code>archinstall</code> is an installation script that streamlines most of the process, but the Arch wiki also has an <a href="https://wiki.archlinux.org/title/Installation_guide">installation guide</a> for full manual installation.</p> <p>Here are my <code>archinstall</code> settings (some fields blanked for privacy):</p> <p><img src="/images/20240106/3.png"></p> <p>For now, I&#39;m disabling swap for performance reasons; we can add it later if RAM usage is an issue. I&#39;m also choosing the Minimal installation profile, just to have a full understanding of which packages I&#39;m installing.</p> <p>For additional packages, I&#39;m installing <code>iwd</code> and <code>iw</code> just in case the network needs to be configured again. But the installation option to copy ISO network configuration to the installation should cover this.</p> <p>15 minutes later, and we&#39;re all done. We&#39;re given the option to chroot into the new installation for additional configuration, but since we already set up networking, we can just boot right into the OS and start installing packages from there.</p> <p>Reboot, and we&#39;re in! Welcome to Arch Linux!</p> <p><img src="/images/20240106/4.png"></p> <h2>Installing yay</h2><p>Arch Linux comes with the <code>pacman</code> package manager, but we&#39;ll be using <code>yay</code> instead. Most importantly, it lets us easily install packages from the Arch User Repository (AUR), which are unofficial packages maintained by other users.</p> <p>We need to install <code>git</code>, then grab the <code>yay</code> repository and build it.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">sudo</span><span style="color: #575279"> </span><span style="color: #EA9D34">pacman</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">git</span></span><br><span class="line"><span style="color: #D7827E">git</span><span style="color: #575279"> </span><span style="color: #EA9D34">clone</span><span style="color: #575279"> </span><span style="color: #EA9D34">https://aur.archlinux.org/yay.git</span></span><br><span class="line"><span style="color: #B4637A; font-style: italic">cd</span><span style="color: #575279"> </span><span style="color: #EA9D34">yay</span></span><br><span class="line"><span style="color: #D7827E">makepkg</span><span style="color: #575279"> </span><span style="color: #286983">-si</span></span></code></pre>  <h2>Setting up the desktop manager</h2><p>Next up, let&#39;s get some graphics going. We&#39;re going to be using the X window system with i3. I would love to run Wayland with sway, but unfortunately it fails to properly detect and use hardware-accelerated graphics drivers, causing it to fall back to a software renderer.</p> <p>I researched this, and Intel 915 graphics <em>should</em> still be supported, but I didn&#39;t get anywhere with basic troubleshooting, so I decided to ditch it and just go with X.</p> <p>Anyway, here are the initial packages we need to install:</p> <ul> <li>Intel graphics drivers</li> <li>Xorg server</li> <li>xterm (terminal emulator)</li> <li><code>gnu-free-fonts</code> for xterm&#39;s <code>ttf-font</code> requirement</li> </ul> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">xf86-video-intel</span><span style="color: #575279"> </span><span style="color: #EA9D34">xorg-server</span><span style="color: #575279"> </span><span style="color: #EA9D34">xorg-init</span><span style="color: #575279"> </span><span style="color: #EA9D34">xterm</span><span style="color: #575279"> </span><span style="color: #EA9D34">gnu-free-fonts</span></span></code></pre>  <p>Now, we type <code>startx</code> and we get our first desktop environment! It&#39;s beautiful!</p> <p><img src="/images/20240106/5.png"></p> <p>(Type <code>exit</code> in all the terminal windows to quit X and get back to the TTY.)</p> <h2>Setting up i3</h2><p>We&#39;re going to use i3, a popular tiling window manager. To start, we&#39;ll install the whole <code>i3</code> group, which includes a handful of packages needed to get i3 up and running.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">i3</span></span></code></pre>  <p>We&#39;ll also install <code>xbanish</code> from the AUR, which is a tiny application that makes the mouse cursor disappear whenever we start typing.</p> <p>Simply typing <code>yay [query]</code> searches all package sources for your query, and then you can select the ones you want to install. It&#39;s a convenient way to search for packages if you don&#39;t know their exact name, or if they&#39;re in the AUR.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #EA9D34">xbanish</span></span></code></pre>  <p>Finally, we&#39;ll set up a <code>.xinitrc</code> file which is a script that runs when the X window system starts up. X comes with a default one that we&#39;ll copy to our user folder, and then we&#39;ll edit it.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #B4637A; font-style: italic">cd</span><span style="color: #575279"> </span><span style="color: #EA9D34">~</span></span><br><span class="line"><span style="color: #D7827E">cp</span><span style="color: #575279"> </span><span style="color: #EA9D34">/etc/X11/xinit/xinitrc</span><span style="color: #575279"> </span><span style="color: #EA9D34">.xinitrc</span></span></code></pre>  <p>Oh yeah, we need a text editor. I use Neovim, but if you&#39;re not comfortable with Vim then you can install <code>nano</code>, a popular minimal text editor found on many Linux distributions.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">nano</span></span></code></pre>  <p>Then, open <code>xinitrc</code> with your text editor. At the bottom, we add the commands we want to execute on startup. Feel free to delete the stuff beneath the <code># start some nice programs</code> comment, and add something like this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> start some nice programs</span></span><br><span class="line"><span style="color: #D7827E">xbanish</span><span style="color: #575279"> </span><span style="color: #797593">&amp;</span></span><br><span class="line"><span style="color: #D7827E">i3</span></span></code></pre>  <p>(Make sure to add the <code>&amp;</code> after each command, except the last one.)</p> <p><img src="/images/20240106/6.png"></p> <p>Save that, and then try <code>startx</code> again. This time, we&#39;re greeted by i3!</p> <p><img src="/images/20240106/7.png"></p> <p>Follow the instructions in that config window and have it use the &quot;Win&quot; key as the modifier key. On the Cr-48, that&#39;s the &quot;search&quot; key, where you&#39;d normally find Caps Lock.</p> <p>I&#39;ll refer to it as the Mod key from now on; for example, Mod + Enter will open up a terminal window.</p> <p>If you&#39;re not familiar with using i3 or another tiling window manager, I recommend looking up a tutorial or guide that explains how it works.</p> <h2>Installing a better terminal emulator</h2><p>The default terminal emulator, xterm, is fine, but we could benefit from having one with better color and font support, and more customization options.</p> <p>Hardware-accelerated terminals are all the rage nowadays. But the Cr-48 doesn&#39;t have support for modern graphics APIs, so most of the hot new terminals will either have awful performance, or they won&#39;t work at all.</p> <p>I went with <code>urxvt</code> and have had a solid experience with it. Specifically, I use <code>rxvt-unicode-truecolor-wide-glyphs</code> which seems to be reliably kept up-to-date, and the support for true color and enhanced glyphs are likely good for Neovim compatibility.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #EA9D34">rxvt-unicode-truecolor-wide-glyphs</span></span></code></pre>  <h2>Installing an app launcher</h2><p>We don&#39;t want to have to launch every app from the terminal, so we&#39;ll get rofi, which is a minimal window switcher and application launcher.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">rofi</span></span></code></pre>  <p>In <code>~/.config/i3/config</code>, I set up Mod + D to open the rofi application launcher.</p> <h2>Theming i3</h2><p>We&#39;re going to theme our desktop environment based on the Catppuccin color palette and the Cozette font.</p> <p>At 1280x800 resolution, anti-aliased fonts look pretty awful in the status bar and the terminal. Cozette is a small, cute-looking bitmap font that I think will fit our theme very nicely.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #EA9D34">cozette-otb</span></span></code></pre>  <p>Now, we do some configuration in <code>~/.Xresources</code> and <code>~/.config/i3/config</code>. And we&#39;re starting to look pretty good!</p> <p><img src="/images/20240106/8.png"></p> <p>Note that the Cr-48 VGA output can only do 1024x768, so that&#39;s what I&#39;m showing in these screenshots. The built-in display has a resolution of 1280x800.</p> <h2>Setting wallpaper</h2><p>We&#39;ll use Nitrogen for setting our desktop wallpaper.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">nitrogen</span></span></code></pre>  <p>Nitrogen comes with a GUI, so simply launch <code>nitrogen</code> and you can use the GUI to set up your wallpaper.</p> <p>My wallpaper is from the collection of Catppuccin wallpapers found <a href="https://github.com/Gingeh/wallpapers">here</a>.</p> <p>I have <code>nitrogen --restore</code> at the bottom of my i3 config, which will run nitrogen to load our wallpaper once i3 starts up.</p> <p><img src="/images/20240106/9.png"></p> <h2>Installing a window compositor</h2><p>Optionally, we might want a window compositor. I don&#39;t think we really need it, but it can provide transparency and other nice visual effects to our desktop environment. Some applications might also expect it.</p> <p>I use picom:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">picom</span></span></code></pre>  <p>Then, we can add it to the bottom of our <code>.xinitrc</code> above <code>i3</code>. Now, things are starting to look really wonderful.</p> <p><img src="/images/20240106/10.png"></p> <h2>Installing a web browser</h2><p>I tried out a whole host of &quot;lightweight&quot; Web browsers, but it&#39;s a mixed bag when it comes to compatibility and performance. I found qutebrowser to be performant and to have a UI that matches the whole i3 experience quite well.</p> <p>The fact is that browsing the modern Web is going to be a bit painful on this machine; this thing was already slow even when it first released. However, I find it perfectly usable for searches, wikis, forums, and other lightweight use cases. It&#39;s just good to have.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">qutebrowser</span></span></code></pre>  <p><img src="/images/20240106/11.png"></p> <p><img src="/images/20240106/12.png"></p> <h2>Miscellaneous stuff</h2><p>Many applications expect the XDG user directories to be defined and in place, so we should install <code>xdg-user-dirs</code> for that compatibility. <code>yay -S xdg-user-dirs</code></p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">xdg-user-dirs</span></span></code></pre>  <p>Then, run <code>xdg-user-dirs-update</code> and the XDG directories will be automatically created in the home directory.</p> <p>It may also be a good idea to install GTK3, for applications that require it.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">yay</span><span style="color: #575279"> </span><span style="color: #286983">-S</span><span style="color: #575279"> </span><span style="color: #EA9D34">gtk3</span></span></code></pre>  <h2>Things to do</h2><p>With all the above, I&#39;ve created a usable and beautiful desktop environment for myself, and I&#39;m delighted with it.</p> <p>There are some additional minor things that would be nice to take care of, such as mapping the volume and brightness keys to their expected functions (they are just Function keys, but labeled on the keyboard differently).</p> <p>I don&#39;t have a lock screen set up, because I don&#39;t plan on taking this computer out of the house, but it&#39;s trivial to set one up if need be.</p> <p>And as I use the machine, I&#39;m sure I&#39;ll run into various other quirks and things that aren&#39;t set up properly. There is nothing set up for applets, notifications, etc., but I don&#39;t think I&#39;ll be using any apps that require those things.</p> <p>In any case, I hope you enjoyed this little journey! The Cr-48 is an absolute delight of a machine and a cute piece of history. If you ever come across one or a similarly low-powered device, I&#39;d highly recommend trying this out.</p> ]]></content:encoded>
      
      
      
      
      <comments>https://dansalva.to/resurrecting-a-prototype-chromebook-with-arch-linux/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>My simple, life-changing keybinds</title>
      <link>https://dansalva.to/my-simple-life-changing-keybinds/</link>
      <guid>https://dansalva.to/my-simple-life-changing-keybinds/</guid>
      <pubDate>Fri, 05 Jan 2024 00:00:00 GMT</pubDate>
      
      <description>&lt;video class=&quot;post-video&quot; playsinline controls muted&gt;&lt;source src=&quot;/images/20240105/1.mp4#t=0.01&quot; type=&quot;video/mp4&quot;&gt;Your browser does not support the video tag.&lt;/video&gt;  &lt;p&gt;One day, I got fed up with constantly reaching for the arrow keys or the mouse while typing. So, I set up a few simple keybinds that let me jump the cursor around and select text without having to move my hands off the keyboard.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<video class="post-video" playsinline controls muted><source src="/images/20240105/1.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p>One day, I got fed up with constantly reaching for the arrow keys or the mouse while typing. So, I set up a few simple keybinds that let me jump the cursor around and select text without having to move my hands off the keyboard.</p> <span id="more"></span>  <p>I don&#39;t think &quot;life-changing&quot; is an exaggeration; I use these keybinds constantly, in Discord, emails, Web forms, social media, etc. Whenever I&#39;m at a computer without them, I feel much clumsier.</p> <p>Plus, it only took me a couple days to get used to when I first set it up!</p> <p>To start, I remap the <strong>Caps Lock</strong> key to use as a function key, which is fairly common practice for programmers.</p> <p>Then, while holding <strong>Caps Lock</strong>:</p> <ul> <li>The <strong>I J K L</strong> keys work as arrow keys</li> <li>The <strong>H</strong> and <strong>;</strong> keys jump forward and backward one word</li> <li>The <strong>U</strong> and <strong>O</strong> keys jump to the beginning and end of the line (like <strong>Home</strong> and <strong>End</strong>)</li> <li>Holding the <strong>F</strong> key is like holding <strong>Shift</strong>, which lets me select text</li> </ul> <p>The above video is a quick demonstration of this functionality.</p> <h2>How to set it up yourself</h2><p>On macOS, I use <a href="https://karabiner-elements.pqrs.org/">Karabiner Elements</a> to configure my keymaps. After installing, you can get my config file <a href="https://gist.github.com/dansalvato/43dc802acaa33e47bee04f42fbb17009">here</a> and put it in <code>$HOME/.config/karabiner/karabiner.json</code>.</p> <p>(Note that my config maps Caps Lock to &quot;Hyper&quot;, which is Cmd+Control+Option+Shift. Karabiner Elements is powerful and has ways of avoiding this, but it&#39;s what I got to work for me.)</p> <p>Windows and Linux have other options for key remapping software, which I haven&#39;t explored much. A lot of gaming keyboards allow you to remap keys in their configuration software (usually Windows exclusive). For instance, I had a SteelSeries Apex Pro which let me map the Caps Lock key to the &quot;meta&quot; key, and then all the other keybinds on the keyboard&#39;s &quot;meta layer&quot;.</p> <p>I hope this offers some inspiration for you to look into it yourself!</p> ]]></content:encoded>
      
      
      
      
      <comments>https://dansalva.to/my-simple-life-changing-keybinds/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Rapidly building custom file formats for my game</title>
      <link>https://dansalva.to/rapidly-building-custom-file-formats-for-my-game/</link>
      <guid>https://dansalva.to/rapidly-building-custom-file-formats-for-my-game/</guid>
      <pubDate>Thu, 26 Oct 2023 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;Earlier this year, I had to figure out how I was going to load level data into &lt;a href=&quot;https://www.youtube.com/watch?v=4SB20aFHc08&quot;&gt;Magicore Anomala&lt;/a&gt;. A level contains backgrounds, sprites, animations, dialogue scripts, and much more. Ideally, I want to pack all of these things into a single file, so that I can deal with the filesystem and file names as little as possible.&lt;/p&gt; &lt;p&gt;I did some cursory searching on tools or frameworks that make it easy to define and build custom file formats, but the stuff I found was a little too complex for my use case, and not flexible enough. So I created my own tool, and I open-sourced it in case others find it useful too.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>Earlier this year, I had to figure out how I was going to load level data into <a href="https://www.youtube.com/watch?v=4SB20aFHc08">Magicore Anomala</a>. A level contains backgrounds, sprites, animations, dialogue scripts, and much more. Ideally, I want to pack all of these things into a single file, so that I can deal with the filesystem and file names as little as possible.</p> <p>I did some cursory searching on tools or frameworks that make it easy to define and build custom file formats, but the stuff I found was a little too complex for my use case, and not flexible enough. So I created my own tool, and I open-sourced it in case others find it useful too.</p> <span id="more"></span>  <h2>Build-A-File</h2><p><a href="https://github.com/dansalvato/build-a-file">Build-A-File</a> is a tool I made that lets me define data structures in clean, readable Python and then build them by sourcing my data from a simple .toml file.</p> <p>For example, here is a data structure that I use for sound effects:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">class</span><span style="color: #575279"> </span><span style="color: #56949F">SFXDef</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">Block</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">    data_pointer</span><span style="color: #797593">:</span><span style="color: #575279"> U32</span></span><br><span class="line"><span style="color: #575279">    length</span><span style="color: #797593">:</span><span style="color: #575279"> U16</span></span><br><span class="line"><span style="color: #575279">    period</span><span style="color: #797593">:</span><span style="color: #575279"> U16</span></span><br><span class="line"><span style="color: #575279">    volume</span><span style="color: #797593">:</span><span style="color: #575279"> U16</span></span><br><span class="line"><span style="color: #575279">    channel</span><span style="color: #797593">:</span><span style="color: #575279"> U8</span></span><br><span class="line"><span style="color: #575279">    priority</span><span style="color: #797593">:</span><span style="color: #575279"> U8</span></span><br><span class="line"><span style="color: #575279">    sfx_data</span><span style="color: #797593">:</span><span style="color: #575279"> Bytes</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">set_data_pointer</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">self</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">_</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">return</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">set_period</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">self</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">data</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #56949F">dict</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">return</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">round</span><span style="color: #797593">(</span><span style="color: #D7827E">3579545</span><span style="color: #575279"> </span><span style="color: #286983">/</span><span style="color: #575279"> data</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;samplerate&#39;</span><span style="color: #797593">])</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">set_channel</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">self</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">_</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">return</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">set_length</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">self</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">_</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">return</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">self</span><span style="color: #797593">.</span><span style="color: #575279">sfx_data</span><span style="color: #797593">.</span><span style="color: #575279">size</span><span style="color: #797593">()</span><span style="color: #575279"> </span><span style="color: #286983">//</span><span style="color: #575279"> </span><span style="color: #D7827E">2</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">set_sfx_data</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">self</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">data</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #56949F">dict</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">return</span><span style="color: #575279"> sfx</span><span style="color: #797593">.</span><span style="color: #575279">get_sfx</span><span style="color: #797593">(</span><span style="color: #575279; font-style: italic">self</span><span style="color: #797593">.</span><span style="color: #575279">root_path</span><span style="color: #286983">/</span><span style="color: #575279">data</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;data&#39;</span><span style="color: #797593">],</span><span style="color: #575279"> data</span><span style="color: #797593">[</span><span style="color: #EA9D34">&#39;samplerate&#39;</span><span style="color: #797593">])</span></span></code></pre>  <p>And here is how the SFX are defined in my .toml file:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593">[[</span><span style="color: #56949F">sfx</span><span style="color: #797593">]]</span></span><br><span class="line"><span style="color: #575279; font-style: italic">data</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;sfx/shoot.wav&quot;</span></span><br><span class="line"><span style="color: #575279; font-style: italic">samplerate</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">22050</span></span><br><span class="line"><span style="color: #575279; font-style: italic">volume</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">64</span></span><br><span class="line"><span style="color: #575279; font-style: italic">priority</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">64</span></span><br><span class="line"></span><br><span class="line"><span style="color: #797593">[[</span><span style="color: #56949F">sfx</span><span style="color: #797593">]]</span></span><br><span class="line"><span style="color: #575279; font-style: italic">data</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;sfx/jump.wav&quot;</span></span><br><span class="line"><span style="color: #575279; font-style: italic">samplerate</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">27000</span></span><br><span class="line"><span style="color: #575279; font-style: italic">volume</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">48</span></span><br><span class="line"><span style="color: #575279; font-style: italic">priority</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">60</span></span></code></pre>  <p>Then, as part of my build script, I just give this .toml file to Build-A-File, and it outputs a data file with the exact layout you see in the Python type annotations. Some values (like volume and priority) are taken right from the .toml file, but others require logic or computation, handled in the corresponding Python setter function.</p> <p>Python was a great choice because it&#39;s extremely flexible and you can be hacky with it. For instance, <code>sfx_data</code> takes the path of the .wav file and converts it to 8-bit RAW using an external process called <code>sox</code>. I was able to do this in just a few lines of code, thanks to Python&#39;s <code>subprocess</code> module.</p> <p>I am also using some very aggressive reflection in Build-A-File to enable the definition format you see above, where the data structure is defined using Python type annotations. It was very important to me that my data structures were highly self-documenting like that, but still extremely flexible by living in the code. That also lets me create more primitive types as needed, like this 8-bit <code>Bool</code> type and a simple <code>List</code> type:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">class</span><span style="color: #575279"> </span><span style="color: #56949F">Bool</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">U8</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">__init__</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">self</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">parent</span><span style="color: #797593">:</span><span style="color: #575279"> Optional</span><span style="color: #797593">[</span><span style="color: #575279">Block</span><span style="color: #797593">],</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">data</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #56949F">bool</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">-&gt;</span><span style="color: #575279"> </span><span style="color: #D7827E">None</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #56949F">super</span><span style="color: #797593">().</span><span style="color: #B4637A; font-style: italic">__init__</span><span style="color: #797593">(</span><span style="color: #575279">parent</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">0x</span><span style="color: #D7827E">ff</span><span style="color: #575279"> </span><span style="color: #286983">if</span><span style="color: #575279"> data </span><span style="color: #286983">else</span><span style="color: #575279"> </span><span style="color: #286983">0x</span><span style="color: #D7827E">00</span><span style="color: #797593">)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span style="color: #286983">class</span><span style="color: #575279"> </span><span style="color: #56949F">List</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">Block</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #EA9D34">&quot;&quot;&quot;Contains item count, item offsets, and a byte array of all the items.&quot;&quot;&quot;</span></span><br><span class="line"><span style="color: #575279">    count</span><span style="color: #797593">:</span><span style="color: #575279"> U16</span></span><br><span class="line"><span style="color: #575279">    offsets</span><span style="color: #797593">:</span><span style="color: #575279"> Array</span><span style="color: #797593">[</span><span style="color: #575279">U32</span><span style="color: #797593">]</span></span><br><span class="line"><span style="color: #575279">    items</span><span style="color: #797593">:</span><span style="color: #575279"> Array</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">set_count</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">self</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">_</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">return</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">len</span><span style="color: #797593">(</span><span style="color: #575279; font-style: italic">self</span><span style="color: #797593">.</span><span style="color: #575279">items</span><span style="color: #797593">)</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">set_offsets</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">self</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">_</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">        offsets </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593">[]</span></span><br><span class="line"><span style="color: #575279">        current_offset </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">self</span><span style="color: #797593">.</span><span style="color: #575279">offsets</span><span style="color: #797593">.</span><span style="color: #575279">offset</span><span style="color: #797593">()</span><span style="color: #575279"> </span><span style="color: #286983">+</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #D7827E">4</span><span style="color: #575279"> </span><span style="color: #286983">*</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">self</span><span style="color: #797593">.</span><span style="color: #575279">count</span><span style="color: #797593">)</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">for</span><span style="color: #575279"> item </span><span style="color: #286983">in</span><span style="color: #575279"> </span><span style="color: #575279; font-style: italic">self</span><span style="color: #797593">.</span><span style="color: #575279">items</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">            offsets</span><span style="color: #797593">.</span><span style="color: #575279">append</span><span style="color: #797593">(</span><span style="color: #575279">current_offset</span><span style="color: #797593">)</span></span><br><span class="line"><span style="color: #575279">            current_offset </span><span style="color: #286983">+=</span><span style="color: #575279"> item</span><span style="color: #797593">.</span><span style="color: #575279">size</span><span style="color: #797593">()</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">return</span><span style="color: #575279"> offsets</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">def</span><span style="color: #575279"> </span><span style="color: #D7827E">set_items</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">self</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">data</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">return</span><span style="color: #575279"> data</span></span></code></pre>  <p>Going back to the SFX example, I&#39;m using that <code>List</code> type to store all my SFX as a list:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">class</span><span style="color: #575279"> </span><span style="color: #56949F">SFXs</span><span style="color: #797593">(</span><span style="color: #907AA9; font-style: italic">List</span><span style="color: #797593">):</span></span><br><span class="line"><span style="color: #575279">    count</span><span style="color: #797593">:</span><span style="color: #575279"> U16</span></span><br><span class="line"><span style="color: #575279">    offsets</span><span style="color: #797593">:</span><span style="color: #575279"> Array</span><span style="color: #797593">[</span><span style="color: #575279">U32</span><span style="color: #797593">]</span></span><br><span class="line"><span style="color: #575279">    items</span><span style="color: #797593">:</span><span style="color: #575279"> Array</span><span style="color: #797593">[</span><span style="color: #575279">SFXDef</span><span style="color: #797593">]</span></span></code></pre>  <p>So in my .toml file, I just have the list of SFX definitions, and then the <code>count</code> and <code>offsets</code> fields are computed at build time by the setter functions!</p> <h2>Rapid iteration</h2><p>I think one of the biggest advantages of modern development tools is the extremely rapid iteration process. We can develop applications several times faster if we can see our changes as soon as we make them.</p> <p>So in developing my own game engine, a lot of this luxury is lost, and menial things like getting data into my game can be a slog. Build-A-File solved some of those problems on its own, but its extensibility means that I was able to hack some of my own workflow on top of it.</p> <p>For instance, the rooms in my levels have a grid of 16x16 collision tiles, and a &quot;hazards layer&quot; which is just a bitmap of spikes and things that kill you. This data is all read from a PNG file, like this one:</p> <p><img src="/images/20231026/1.png"></p> <p>Then, my room definition in the .toml file looks like this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593">[[</span><span style="color: #56949F">rooms</span><span style="color: #797593">]]</span></span><br><span class="line"><span style="color: #575279; font-style: italic">has_hazards</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span></span><br><span class="line"><span style="color: #575279; font-style: italic">has_tiles</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span></span><br><span class="line"><span style="color: #575279; font-style: italic">event_id</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">128</span></span><br><span class="line"><span style="color: #575279; font-style: italic">event_params</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">1</span></span><br><span class="line"><span style="color: #575279; font-style: italic">starting_pos</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #797593">[</span><span style="color: #D7827E">8</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">192</span><span style="color: #797593">]</span></span><br><span class="line"><span style="color: #575279; font-style: italic">exits</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #797593">[</span><span style="color: #D7827E">-1</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">2</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">-1</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">-1</span><span style="color: #797593">]</span></span><br><span class="line"><span style="color: #575279; font-style: italic">background_id</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279; font-style: italic">hazards_image</span><span style="color: #575279"> </span><span style="color: #797593">=</span><span style="color: #575279"> </span><span style="color: #EA9D34">&quot;rooms/1-hazards.png&quot;</span></span></code></pre>  <p>Of note here is <code>hazards_image</code>. The PNG path gets sent over to my script, which extracts the white pixels into a bitmap for the hazards layer (the image above is obviously placeholder graphics). More than that, my script looks at the purple tiles to build a tilemap for the room. A full purple block means that the tile has collision on all sides, and a thin rectangle means it&#39;s a platform that you can pass through from underneath.</p> <p>As I add more tiles with unique behaviors, I can just tell my script to look for a new color in the PNG and put the corresponding tile ID in the tilemap.</p> <p>So, instead of having to maintain an entirely separate level editor application, I can simply design my levels entirely in Photoshop, just as quickly! Now, it helps that these rooms don&#39;t have tons of custom assets required per room, but this is a case where I gave a simple solution to a simple problem.</p> <h2>Try it yourself</h2><p>These are some examples of how I used Build-A-File for Magicore, but perhaps you have a way to make use of it in your own game. You can find it <a href="https://github.com/dansalvato/build-a-file">here</a> on GitHub. I tried to provide lots of examples in the readme. If you do end up using it, I&#39;d love to hear about it!</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/rapidly-building-custom-file-formats-for-my-game/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>There is no &quot;if&quot; in Assembly</title>
      <link>https://dansalva.to/there-is-no-if-in-assembly/</link>
      <guid>https://dansalva.to/there-is-no-if-in-assembly/</guid>
      <pubDate>Fri, 28 Jul 2023 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;If there was, it would be spelled &amp;quot;Assemblify&amp;quot;, which is now the name of your next startup.&lt;/p&gt; &lt;p&gt;So, the cool thing about Assembly is that whenever you perform an instruction, the CPU runs a bunch of comparisons automatically! They are stored in &amp;quot;condition flags&amp;quot;, which you can use to branch (goto) or do other stuff if the right conditions are met.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>If there was, it would be spelled &quot;Assemblify&quot;, which is now the name of your next startup.</p> <p>So, the cool thing about Assembly is that whenever you perform an instruction, the CPU runs a bunch of comparisons automatically! They are stored in &quot;condition flags&quot;, which you can use to branch (goto) or do other stuff if the right conditions are met.</p> <span id="more"></span>  <p>Consider this code:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">   </span><span style="color: #D7827E">d1</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span><span style="color: #575279">   </span><span style="color: #9893A5; font-style: italic">; Add register d1 to d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.w</span><span style="color: #575279">   #</span><span style="color: #D7827E">0</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span><span style="color: #575279">   </span><span style="color: #9893A5; font-style: italic">; Is d0 equal to 0?</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">beq</span><span style="color: #575279">     .zero   </span><span style="color: #9893A5; font-style: italic">; If so, branch to .zero</span></span></code></pre>  <p>The above should be pretty readable, even if you&#39;re not used to looking at Assembly. We&#39;re adding register <code>d1</code> to <code>d0</code>, then checking if <code>d0</code> is equal to zero. If it is, we branch to the label <code>.zero</code>.</p> <p>However, we are not yet leveraging the magic of condition flags. We can accomplish the same thing like this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">   </span><span style="color: #D7827E">d1</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span><span style="color: #575279">   </span><span style="color: #9893A5; font-style: italic">; Add register d1 to d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">beq</span><span style="color: #575279">     .zero   </span><span style="color: #9893A5; font-style: italic">; If zero, branch to .zero</span></span></code></pre>  <p>We saved a whole CPU instruction for free! Congrats, you&#39;re now entitled to add &quot;blazing fast&quot; to your GitHub project description.</p> <p>The reason this works is that whenever you do things like move data or perform arithmetic, the CPU automatically sets condition flags based on the result. Some examples of condition flags are &quot;Zero&quot;, &quot;Negative&quot;, &quot;Carry&quot;.</p> <p>When we do our <code>add</code> in the above code, the CPU will set the &quot;Zero&quot; condition flag if the result is zero—otherwise, it will clear the flag.</p> <p><code>beq</code> stands for &quot;Branch if equal&quot;, but it actually checks for the Zero flag, not for equality to some other value. That&#39;s why we don&#39;t have to explicitly compare to 0 first.</p> <p>That gets interesting in an example like this:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">   </span><span style="color: #D7827E">d1</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span><span style="color: #575279">   </span><span style="color: #9893A5; font-style: italic">; Add register d1 to d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.w</span><span style="color: #575279">   #</span><span style="color: #D7827E">10</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span><span style="color: #575279">  </span><span style="color: #9893A5; font-style: italic">; Is d0 equal to 10?</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">beq</span><span style="color: #575279">     .ten    </span><span style="color: #9893A5; font-style: italic">; If so, branch to .ten</span></span></code></pre>  <p>This time, instead of comparing to zero, we&#39;re comparing to 10, and we&#39;re branching if equal.</p> <p>But hang on—if <code>beq</code> is checking for the Zero flag, how does this make sense? Aren&#39;t we comparing to 10?</p> <h2>The Zero flag</h2><p>As it turns out, the <code>cmp</code> instruction performs a subtraction under the hood. Unlike <code>sub</code>, the <em>real</em> subtraction operand, <code>cmp</code> throws away the result. However, it still sets all the condition flags.</p> <p>So, when we run <code>cmp.w #10,d0</code>, we are subtracting 10 from <code>d0</code>. Well, if <code>d0</code> is also 10, then we get 10 - 10 &#x3D; 0, and the CPU sets the Zero flag. That&#39;s why <code>beq</code> is called &quot;branch if equal&quot; but checks for zero!</p> <h2>Checking other conditions</h2><p>All the human-readable mnemonics like <code>bge</code> (greater or equal), <code>blt</code> (less than), etc. are actually checking for a set of condition flags that do what we expect based on the mnemonic.</p> <p>For example, <code>bge</code> respects signed values, so it has to check some complicated conditions:</p> <ol> <li>Negative flag is clear AND Overflow flag is clear, OR</li> <li>Negative flag is set AND Overflow flag is set</li> </ol> <p>The Overflow flag gets set if you move from &quot;positive&quot; (<code>0x0000</code>-<code>0x7fff</code>) to &quot;negative&quot; (<code>0x8000</code>-<code>0xffff</code>) or vice-versa. (Only if you cross the <code>0x8000</code> barrier; going from <code>0xffff</code> to <code>0x0000</code> does not set Overflow, because that&#39;s like a signed integer going from -1 to 0, which is not an overflow.)</p> <p>So, 3 ≥ 2 because <code>0x0003</code> - <code>0x0002</code> &#x3D; <code>0x0001</code>, which is not negative, and didn&#39;t overflow.</p> <p>Likewise, 3 ≥ -2 because <code>0x0003</code> - <code>0xfffe</code> &#x3D; <code>0x0005</code>, which is not negative, and didn&#39;t overflow.</p> <p>But also, 127 ≥ -16, because <code>0x7fff</code> - <code>0xfff0</code> &#x3D; <code>0x8010</code>, which IS negative, and it DID overflow.</p> <p>We also have <code>bhi</code> and <code>blo</code> (branch if higher&#x2F;lower), which don&#39;t use the Overflow flag. That means you can use these to compare unsigned values. (Assembly has no concept of signed&#x2F;unsigned values—all that matters is which condition flags you check for in your comparisons!)</p> <p>Also popular are <code>bcs</code> and <code>bcc</code> (branch if Carry set&#x2F;clear). The Carry flag gets set when an unsigned overflow happens (like incrementing from <code>0xff</code> to <code>0x00</code>), or when you&#39;re bit shifting and a <code>1</code> bit leaves the edge.</p> <p>Here&#39;s a straightforward example where <code>bcc</code> is used to clamp a value to a maximum of 255:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a2</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d2</span><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Get color value to d2</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.b</span><span style="color: #575279">       #</span><span style="color: #D7827E">$80</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span><span style="color: #575279">     </span><span style="color: #9893A5; font-style: italic">; Add 128</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">bcc</span><span style="color: #575279">         .</span><span style="color: #D7827E">1</span><span style="color: #575279">          </span><span style="color: #9893A5; font-style: italic">; If no carry, move on</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">moveq</span><span style="color: #575279">       #</span><span style="color: #D7827E">-1</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span><span style="color: #575279">      </span><span style="color: #9893A5; font-style: italic">; Otherwise, set d2 to -1 (255)</span></span><br><span class="line"><span style="color: #D7827E">.1</span><span style="color: #575279">: </span><span style="color: #286983">jsr</span><span style="color: #575279">         cfx_hsv2rgb </span><span style="color: #9893A5; font-style: italic">; Apply color effect</span></span></code></pre>  <h2>Other uses of the condition flags</h2><p>Usually, CPUs have various other instructions that use the condition flags, not just branch instructions. m68k Assembly doesn&#39;t have many, but one example is <code>sxx</code>, eg. &quot;set on a certain condition&quot;.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">   </span><span style="color: #D7827E">d1</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span><span style="color: #575279">   </span><span style="color: #9893A5; font-style: italic">; Add register d1 to d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.w</span><span style="color: #575279">   #</span><span style="color: #D7827E">10</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span><span style="color: #575279">  </span><span style="color: #9893A5; font-style: italic">; Is d0 equal to 10?</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">seq</span><span style="color: #575279">     </span><span style="color: #D7827E">d1</span><span style="color: #575279">      </span><span style="color: #9893A5; font-style: italic">; If so, set d1 to $ff</span></span></code></pre>  <p>In the above, <code>d1</code> gets &quot;set&quot; (<code>0xff</code>) if <code>d0</code> is 10. Otherwise, it gets cleared (<code>0x00</code>).</p> <p>You can sometimes use this to avoid branching completely. Here is an alternative to my previous example that used <code>bcc</code> to clamp to 255, but instead using <code>scs</code> to do the same thing without branching.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.w</span><span style="color: #575279">      (</span><span style="color: #D7827E">a2</span><span style="color: #575279">)+,</span><span style="color: #D7827E">d2</span><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Get color value to d2</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.b</span><span style="color: #575279">       #</span><span style="color: #D7827E">$80</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span><span style="color: #575279">     </span><span style="color: #9893A5; font-style: italic">; Add 128</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">scs</span><span style="color: #575279">         </span><span style="color: #D7827E">d3</span><span style="color: #575279">          </span><span style="color: #9893A5; font-style: italic">; If carry, set d3</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">or.b</span><span style="color: #575279">        </span><span style="color: #D7827E">d3</span><span style="color: #575279">,</span><span style="color: #D7827E">d2</span><span style="color: #575279">       </span><span style="color: #9893A5; font-style: italic">; Combine result with d2</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">jsr</span><span style="color: #575279">         cfx_hsv2rgb </span><span style="color: #9893A5; font-style: italic">; Apply color effect</span></span></code></pre>  <p>If there was a carry, <code>d3</code> gets set to <code>0xff</code> and <code>or</code>&#39;d with d2 (which just makes <code>d2</code> <code>0xff</code>). If there was NOT a carry, <code>d3</code> gets cleared to <code>0x00</code>, and the <code>or</code> does nothing.</p> <p>The old example using <code>bcc</code> took 12-14 cycles, while this one only takes 8-10 cycles. We&#39;re blazing fast again!</p> <p>Imagine not having condition flags and being forced to nest your code in a bunch of &quot;if&quot; statements (this post was written by the m68k Assembly gang)</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/there-is-no-if-in-assembly/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Assembly tricks for fast bounds checking</title>
      <link>https://dansalva.to/assembly-tricks-for-fast-bounds-checking/</link>
      <guid>https://dansalva.to/assembly-tricks-for-fast-bounds-checking/</guid>
      <pubDate>Sun, 09 Jul 2023 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;I came across a cute little trick while looking to optimize my physics routine in Magicore Anomala.&lt;/p&gt; &lt;p&gt;In the routine, I need to check if each object is inbounds, and deactivate it if it goes out of bounds. Pretty typical situation, and we&amp;#39;d probably see it written as something like this:&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>I came across a cute little trick while looking to optimize my physics routine in Magicore Anomala.</p> <p>In the routine, I need to check if each object is inbounds, and deactivate it if it goes out of bounds. Pretty typical situation, and we&#39;d probably see it written as something like this:</p> <span id="more"></span>  <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">int</span><span style="color: #575279"> </span><span style="color: #D7827E">isInbounds</span><span style="color: #797593">(</span><span style="color: #286983">int</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">position</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">int</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">upperBound</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">&#123;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">return</span><span style="color: #575279"> position </span><span style="color: #286983">&gt;=</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #575279"> </span><span style="color: #286983">&amp;&amp;</span><span style="color: #575279"> position </span><span style="color: #286983">&lt;</span><span style="color: #575279"> upperBound</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #797593">&#125;</span></span></code></pre>  <p>(You&#39;d probably inline this, but we&#39;re making it a function for the readability of this example.)</p> <p>The cute part is that if the position is negative, then the <em>unsigned</em> version of that value is some extremely large number. So, you can get away with doing this in one comparison instead of two:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">int</span><span style="color: #575279"> </span><span style="color: #D7827E">isInboundsFast</span><span style="color: #797593">(</span><span style="color: #286983">int</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">position</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #286983">int</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">upperBound</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">&#123;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">return</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #286983">unsigned</span><span style="color: #575279"> </span><span style="color: #286983">int</span><span style="color: #797593">)</span><span style="color: #575279">position </span><span style="color: #286983">&lt;</span><span style="color: #575279"> upperBound</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #797593">&#125;</span></span></code></pre>  <p>You just want to make sure that <code>upperBound</code> is not close to the maximum int size, but in most realistic cases, it won&#39;t be.</p> <p>If we throw these into <a href="https://godbolt.org/">Compiler Explorer</a>, we get to see the output:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">isInbounds</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">test</span><span style="color: #575279">    </span><span style="color: #D7827E">edi</span><span style="color: #575279">, </span><span style="color: #D7827E">edi</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">setns</span><span style="color: #575279">   </span><span style="color: #D7827E">al</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">cmp</span><span style="color: #575279">     </span><span style="color: #D7827E">edi</span><span style="color: #575279">, </span><span style="color: #D7827E">esi</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">setl</span><span style="color: #575279">    </span><span style="color: #D7827E">cl</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">and</span><span style="color: #575279">     </span><span style="color: #D7827E">cl</span><span style="color: #575279">, </span><span style="color: #D7827E">al</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">movzx</span><span style="color: #575279">   </span><span style="color: #D7827E">eax</span><span style="color: #575279">, </span><span style="color: #D7827E">cl</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">ret</span></span><br><span class="line"><span style="color: #D7827E">isInboundsFast</span><span style="color: #797593">:</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">xor</span><span style="color: #575279">     </span><span style="color: #D7827E">eax</span><span style="color: #575279">, </span><span style="color: #D7827E">eax</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">cmp</span><span style="color: #575279">     </span><span style="color: #D7827E">edi</span><span style="color: #575279">, </span><span style="color: #D7827E">esi</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">setb</span><span style="color: #575279">    </span><span style="color: #D7827E">al</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #286983">ret</span></span></code></pre>  <p>Very cool! Clearly, <code>isInboundsFast</code> is more efficient. The compiler doesn&#39;t &quot;optimize&quot; the original any further, because again, our two checks aren&#39;t identical—we&#39;re taking a shortcut that will <em>probably</em> not have any realistic consequences, but it&#39;s not logically guaranteed.</p> <p>Anyway, x86 Assembly is all but gibberish to me. In this household, we only stan CPUs with a sense of sophistication and class, like the Motorola 68000.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5; font-style: italic">; d0 = position</span></span><br><span class="line"><span style="color: #9893A5; font-style: italic">; d1 = upper bound</span></span><br><span class="line"><span style="color: #D7827E">is_inbounds</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.w</span><span style="color: #575279">   </span><span style="color: #D7827E">d1</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">bhs</span><span style="color: #575279">     .destroy</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">rts</span></span><br><span class="line"></span><br><span class="line"><span style="color: #D7827E">.destroy</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #9893A5; font-style: italic">; Destroy the object</span></span></code></pre>  <p>The instruction <code>bhs</code> stands for &quot;branch if higher or same&quot;. Unlike <code>bge</code> (branch if greater or equal), <code>bhs</code> ignores signed&#x2F;negative flags and branches if our pure, unsigned value is higher than the upper bound.</p> <h2>Cramming two 16-bit values into a single 32-bit register</h2><p>I have to do bounds checking for both X and Y position, and I&#39;m taking a few shortcuts there as well. My positions are 16-bit values, but the 68000 has 32-bit registers, meaning I can hold both of them in a single register. That ends up saving me a few cycles.</p> <p>Consider this traditional method:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5; font-style: italic">; a0 = pointer to object</span></span><br><span class="line"><span style="color: #9893A5; font-style: italic">; d2 = X upper bound</span></span><br><span class="line"><span style="color: #9893A5; font-style: italic">; d3 = Y upper bound</span></span><br><span class="line"><span style="color: #D7827E">update_physics</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Load positions into d0 and d1</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.w</span><span style="color: #575279">  BOB_xpos(</span><span style="color: #D7827E">a0</span><span style="color: #575279">),</span><span style="color: #D7827E">d0</span><span style="color: #575279"> </span><span style="color: #9893A5; font-style: italic">; get X position in d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.w</span><span style="color: #575279">  BOB_ypos(</span><span style="color: #D7827E">a0</span><span style="color: #575279">),</span><span style="color: #D7827E">d1</span><span style="color: #575279"> </span><span style="color: #9893A5; font-style: italic">; get Y position in d1</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Add velocity to position</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">   BOB_xvel(</span><span style="color: #D7827E">a0</span><span style="color: #575279">),</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.w</span><span style="color: #575279">   BOB_yvel(</span><span style="color: #D7827E">a0</span><span style="color: #575279">),</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Check if new positions are inbounds</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.w</span><span style="color: #575279">   </span><span style="color: #D7827E">d2</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">bhs</span><span style="color: #575279">     .destroy</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.w</span><span style="color: #575279">   </span><span style="color: #D7827E">d3</span><span style="color: #575279">,</span><span style="color: #D7827E">d1</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">bhs</span><span style="color: #575279">     .destroy</span></span></code></pre>  <p>The above takes about 76 cycles. Here is my optimized version:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5; font-style: italic">; a0 = pointer to object</span></span><br><span class="line"><span style="color: #9893A5; font-style: italic">; d2 = X upper bound, shifted left 16 bits to the upper word</span></span><br><span class="line"><span style="color: #9893A5; font-style: italic">; d3 = Y upper bound (not shifted)</span></span><br><span class="line"><span style="color: #D7827E">update_physics</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Load X/Y position into d0 (X upper, Y lower)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.l</span><span style="color: #575279">  BOB_xpos(</span><span style="color: #D7827E">a0</span><span style="color: #575279">),</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Add velocity to position</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.l</span><span style="color: #575279">   BOB_xvel(</span><span style="color: #D7827E">a0</span><span style="color: #575279">),</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Check if new positions are inbounds</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.l</span><span style="color: #575279">   </span><span style="color: #D7827E">d2</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">bhs</span><span style="color: #575279">     .destroy</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.w</span><span style="color: #575279">   </span><span style="color: #D7827E">d3</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">bhs</span><span style="color: #575279">     .destroy</span></span></code></pre>  <p>This version is only 56 cycles. We&#39;re saving ~20 cycles per loop, which adds up to thousands of cycles over the course of 200 objects.</p> <p>A breakdown of how this works:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">update_physics</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Load X/Y position into d0 (X upper, Y lower)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.l</span><span style="color: #575279">  BOB_xpos(</span><span style="color: #D7827E">a0</span><span style="color: #575279">),</span><span style="color: #D7827E">d0</span></span></code></pre>  <p>In memory, X position and Y position are adjacent 16-bit values. By using <code>move.l</code>, we&#39;re loading 32 bits of data into <code>d0</code>. So this, makes <code>d0</code> look like <code>XXXX YYYY</code>.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Add velocity to position</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">add.l</span><span style="color: #575279">   BOB_xvel(</span><span style="color: #D7827E">a0</span><span style="color: #575279">),</span><span style="color: #D7827E">d0</span></span></code></pre>  <p>Similarly, velocity is stored as two adjacent 16-bit values. We&#39;re adding velocity <code>XXXX YYYY</code> to our position <code>XXXX YYYY</code>, letting us update both positions in a single instruction.</p> <p>The downside: If Y is negative, then the addition will cause a carry into the lowest subpixel of X. For instance, if our X&#x2F;Y position is <code>0200 0500</code>, and our velocity is <code>0000 ffff</code> (very slight negative Y velocity), then our resulting position will be <code>0201 04ff</code>.</p> <p>As you can see, the Y calculation spilled over into X. So, I only use this for attack patterns where the spill into the X subpixel is inconsequential.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Check if new positions are inbounds</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.l</span><span style="color: #575279">   </span><span style="color: #D7827E">d2</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">bhs</span><span style="color: #575279">     .destroy</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">cmp.w</span><span style="color: #575279">   </span><span style="color: #D7827E">d3</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">bhs</span><span style="color: #575279">     .destroy</span></span></code></pre>  <p>Here, <code>d2</code> is our X upper bound and <code>d3</code> is our Y upper bound. The trick is that since <code>d0</code> looks like <code>XXXX YYYY</code>, we have <code>d2</code> shifted to the high word of the register, like <code>XXXX 0000</code>.</p> <p>That means when we use <code>cmp.l</code> to compare <code>d0</code> to <code>d2</code>, it&#39;s comparing all 32 bits. Importantly, that includes X. It turns out the lower 16 bits (Y) end up not affecting this comparison at all.</p> <p>Finally, our Y position is in the lower 16 bits, so we can just use a <code>cmp.w</code> to compare the lower word like normal.</p> <p>That&#39;s it for now—thanks for letting me share some of these meticulous little optimizations that make Assembly coding on old hardware so much fun.</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/assembly-tricks-for-fast-bounds-checking/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Getting clever with the Amiga blitter</title>
      <link>https://dansalva.to/getting-clever-with-the-amiga-blitter/</link>
      <guid>https://dansalva.to/getting-clever-with-the-amiga-blitter/</guid>
      <pubDate>Mon, 03 Jul 2023 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;Magicore Anomala is powered largely by the Amiga&amp;#39;s blitter, allowing me to quickly clear the screen and draw hundreds of objects every frame at a full 60fps. It runs in parallel with the CPU and excels at copying or manipulating large blocks of data.&lt;/p&gt; &lt;p&gt;But the blitter goes above and beyond the functionality of simply hauling bits around. You can shift, mask, and logically combine up to three independent sources anywhere in shared memory.&lt;/p&gt; &lt;p&gt;Today I&amp;#39;ll show you how Magicore uses the copper and blitter to convert and copy a 24-bit RGB color palette into the Amiga&amp;#39;s 12-bit color registers, every frame, using zero CPU cycles.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>Magicore Anomala is powered largely by the Amiga&#39;s blitter, allowing me to quickly clear the screen and draw hundreds of objects every frame at a full 60fps. It runs in parallel with the CPU and excels at copying or manipulating large blocks of data.</p> <p>But the blitter goes above and beyond the functionality of simply hauling bits around. You can shift, mask, and logically combine up to three independent sources anywhere in shared memory.</p> <p>Today I&#39;ll show you how Magicore uses the copper and blitter to convert and copy a 24-bit RGB color palette into the Amiga&#39;s 12-bit color registers, every frame, using zero CPU cycles.</p> <span id="more"></span>  <video class="post-video" playsinline controls muted><source src="/images/20230703/1.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video> <div class="image-description">An example of using HSV to RGB conversion to produce a rainbow effect</div>  <h2>Quick blitter intro</h2><p>&quot;Blit&quot; stands for &quot;block transfer&quot;—in other words, transferring a block of memory from a source to a destination.</p> <p>Jay Miner, the &quot;father of the Amiga&quot;, insisted on calling the Amiga&#39;s blitter a &quot;bimmer&quot; (which evidently did not catch on). He wanted to distinguish it as a &quot;bitmap image manipulator&quot; because it could shift and combine up to three independent sources in many unique ways.</p> <p>As you&#39;ll soon see, we can use the blitter (bimmer?) to do some clever bit shifting, masking, and ORing on a big chunk of memory—something the CPU would strain to do.</p> <h2>Our RGB data</h2><p>I have a palette of 32 colors, and each color is stored with 8-bit RGB values. To make this work, I technically store it as GRB (swapped green and red). In memory, each color looks like <code>00GgRrBb</code>, and we want to convert them to <code>0RGB</code> (discarding the low 4 bits of each color).</p> <p>I&#39;m storing the palette as 8-bit colors because I&#39;m doing some color effects like additive blending and converting HSV to RGB. These operations are much easier on byte-aligned values, especially for our 7MHz CPU.</p> <h2>The blit operation</h2><p>Here is the blit operation to convert our <code>00GgRrBb</code> to <code>0RGB</code>:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; 00GgRrBb -&gt; 0RGB</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; A points to RrBb, B points to 00Gg</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; C has no source, BLTCDAT loaded with $00f0 as a constant</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; 1. Mask A (R0B0), shift A 4 bits (0R0B)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; 2. D = A + BC, i.e. 0R0B | (00Gg &amp; $00f0)</span></span><br><span class="line"></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    </span><span style="color: #D7827E">$0001</span><span style="color: #575279">,</span><span style="color: #D7827E">$0000</span><span style="color: #575279">         </span><span style="color: #9893A5; font-style: italic">;wait for blitter</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTCON0,</span><span style="color: #D7827E">$4df8</span><span style="color: #575279">       </span><span style="color: #9893A5; font-style: italic">;4: shift A, d: use ABD, f8: D = A + BC</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTCON1,</span><span style="color: #D7827E">$0000</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTCDAT,</span><span style="color: #D7827E">$00f0</span><span style="color: #575279">       </span><span style="color: #9893A5; font-style: italic">;C is always $00f0 to mask B</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTAPTH,</span><span style="color: #D7827E">0</span><span style="color: #575279">           </span><span style="color: #9893A5; font-style: italic">;GRB8 color source +2 (RrBb)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTAPTL,</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTBPTH,</span><span style="color: #D7827E">0</span><span style="color: #575279">           </span><span style="color: #9893A5; font-style: italic">;GRB8 color source (00Gg)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTBPTL,</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTDPTH,</span><span style="color: #D7827E">0</span><span style="color: #575279">           </span><span style="color: #9893A5; font-style: italic">;Copperlist for color registers</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTDPTL,</span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTAFWM,</span><span style="color: #D7827E">$f0f0</span><span style="color: #575279">       </span><span style="color: #9893A5; font-style: italic">;mask out color low bits</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTALWM,</span><span style="color: #D7827E">$f0f0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTAMOD,</span><span style="color: #D7827E">2</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTBMOD,</span><span style="color: #D7827E">2</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTDMOD,</span><span style="color: #D7827E">2</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.w</span><span style="color: #575279">    BLTSIZE,</span><span style="color: #D7827E">32</span><span style="color: #575279">&lt;&lt;</span><span style="color: #D7827E">6+1</span><span style="color: #575279">     </span><span style="color: #9893A5; font-style: italic">;blit 32 lines of 1 word each</span></span></code></pre>  <p>The above is done using copper (coprocessor) instructions. Here is an equivalent using the CPU:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">_scr_blit_colors</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; Blit color registers</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.l</span><span style="color: #575279">      #</span><span style="color: #D7827E">$4df80000</span><span style="color: #575279">,BLTCON0(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">$00f0</span><span style="color: #575279">,BLTCDAT(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.l</span><span style="color: #575279">      #CopColor+</span><span style="color: #D7827E">2</span><span style="color: #575279">,BLTDPTH(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">lea</span><span style="color: #575279">         cfx_WorkingColors,</span><span style="color: #D7827E">a0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.l</span><span style="color: #575279">      </span><span style="color: #D7827E">a0</span><span style="color: #575279">,BLTBPTH(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">addq</span><span style="color: #575279">        #</span><span style="color: #D7827E">2</span><span style="color: #575279">,</span><span style="color: #D7827E">a0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.l</span><span style="color: #575279">      </span><span style="color: #D7827E">a0</span><span style="color: #575279">,BLTAPTH(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.l</span><span style="color: #575279">      #</span><span style="color: #D7827E">$f0f0f0f0</span><span style="color: #575279">,BLTAFWM(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">moveq</span><span style="color: #575279">       #</span><span style="color: #D7827E">2</span><span style="color: #575279">,</span><span style="color: #D7827E">d0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.w</span><span style="color: #575279">      </span><span style="color: #D7827E">d0</span><span style="color: #575279">,BLTAMOD(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.w</span><span style="color: #575279">      </span><span style="color: #D7827E">d0</span><span style="color: #575279">,BLTBMOD(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.w</span><span style="color: #575279">      </span><span style="color: #D7827E">d0</span><span style="color: #575279">,BLTDMOD(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">move.w</span><span style="color: #575279">      #</span><span style="color: #D7827E">32</span><span style="color: #575279">&lt;&lt;</span><span style="color: #D7827E">6+1</span><span style="color: #575279">,BLTSIZE(</span><span style="color: #D7827E">a6</span><span style="color: #575279">)</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">rts</span></span></code></pre>  <h2>A more readable breakdown</h2><p>Let&#39;s walk through what happens step by step:</p> <ol> <li>A gets loaded with <code>RrBb</code> (from source memory)</li> <li>B gets loaded with <code>00Gg</code> (from source memory)</li> <li>C gets loaded with <code>00f0</code> (as a constant)</li> <li>A gets masked with <code>f0f0</code>. A is now <code>R0B0</code></li> <li>A gets shifted 4 bits. A is now <code>0R0B</code></li> <li>The minterms kick in to combine A, B, and C:<ol> <li>B intersects C, combining <code>00Gg</code> with <code>00f0</code> to give us <code>00G0</code></li> <li>A unions BC, combining <code>0R0B</code> with <code>00G0</code> to give us <code>0RGB</code></li> </ol> </li> <li>The result <code>0RGB</code> gets written to destination D</li> <li>A, B, and D move 2 bytes forward but have reached the end of the line, so they move another 2 bytes forward (because of our modulo 2), which brings them to the next color entry</li> </ol> <p>This all happens in 8 cycles. That&#39;s the power of the Amiga blitter!</p> <h2>Copper vs. CPU</h2><p>Above, I gave examples of performing this blit using either the copper or the CPU. The CPU version takes about 64 cycles, which is perfectly reasonable for a 68000.</p> <p>The copper version takes 0 cycles, because it&#39;s the exact same set of instructions every frame—no logic needs to be performed to set up the copper instructions, except for the very first time. Only a small optimization over the CPU version, but it feels cool!</p> <h2>A minor downside</h2><p>Only one component at a time can access shared memory. If the blitter is running, that means the CPU has to wait its turn to read from shared memory—whether that&#39;s reading&#x2F;writing data, or simply fetching CPU instructions.</p> <p>By default, the blitter gives every 4th DMA cycle to the CPU. Thankfully, this blit is quite small (only 32 words), so we&#39;re not tied up for too long. For larger blits, you can try to structure your program so that your most expensive CPU instructions happen during the blit. A single multiply or divide can take 70 or more CPU cycles, so it&#39;s a great time for the CPU to be doing that, rather than repeatedly waiting around a lot to fetch its next instruction.</p> <p>If your Amiga has &quot;Fast RAM&quot; then this is no issue, because Fast RAM is CPU-only and doesn&#39;t have to be shared among all the other chips.</p> <h2>Blitter fun</h2><p>One of the most fun parts of working on Magicore is leveraging Amiga-specific features to make the game do cool stuff. The blitter is so powerful that it just makes me hope I come across all kinds of unique use cases for it. Maybe it gets your imagination going, too.</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/getting-clever-with-the-amiga-blitter/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Performant data structures for bullet hell</title>
      <link>https://dansalva.to/performant-data-structures-for-bullet-hell/</link>
      <guid>https://dansalva.to/performant-data-structures-for-bullet-hell/</guid>
      <pubDate>Thu, 29 Jun 2023 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;One interesting challenge when coding the game engine for Magicore Anomala is figuring out the ideal data structures for different scene objects.&lt;/p&gt; &lt;p&gt;Since we&amp;#39;re on a 7MHz CPU and need to process hundreds of objects per frame, every CPU cycle counts. Here are the requirements for the bullet objects:&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>One interesting challenge when coding the game engine for Magicore Anomala is figuring out the ideal data structures for different scene objects.</p> <p>Since we&#39;re on a 7MHz CPU and need to process hundreds of objects per frame, every CPU cycle counts. Here are the requirements for the bullet objects:</p> <span id="more"></span>  <ol> <li>We have a list of 200 bullets. Every frame, each active bullet in the list needs to be updated.</li> <li>During the update, if a bullet leaves the play area, then it needs to despawn and&#x2F;or become inactive. This means any random bullet in the list can despawn at any time, leaving free memory in that spot.</li> <li>At arbitrary times (sometimes multiple in a frame), we need to spawn a new bullet to the list.</li> </ol> <p>Number 3 is the hardest here: To spawn a new bullet, we have to find a free entry in our list of bullets. It&#39;s tough to do that fast.</p> <p>I don&#39;t have much experience with manual memory management, so these kinds of problems are brand new to me.</p> <h2>My current solution</h2><p>A barebones array is out of the question—to spawn a bullet, we&#39;d have to traverse the array to find the first free spot, and that is way too costly.</p> <p>To go with the array of bullet objects, I have a second array of pointers, and I pre-populate it with a pointer to every element of the bullets array.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">FreeBulletStackPtr</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    FreeBulletStack</span></span><br><span class="line"></span><br><span class="line"><span style="color: #D7827E">FreeBulletStack</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">0</span><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; &lt;-- We are here</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">1</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">2</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">3</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">4</span></span><br><span class="line"><span style="color: #575279">    ...</span></span><br><span class="line"></span><br><span class="line"><span style="color: #D7827E">Bullets</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dcb.b</span><span style="color: #575279">   BULLETSIZE*</span><span style="color: #D7827E">200</span></span></code></pre>  <p>Every time we want to spawn a bullet, we look at the current entry in our stack, and we spawn it to that location:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">FreeBulletStackPtr</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    FreeBulletStack+</span><span style="color: #D7827E">4</span><span style="color: #575279">       </span><span style="color: #9893A5; font-style: italic">; Now points to next element in stack</span></span><br><span class="line"></span><br><span class="line"><span style="color: #D7827E">FreeBulletStack</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    </span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">1</span><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; &lt;-- We are here</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">2</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">3</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">4</span></span><br><span class="line"><span style="color: #575279">    ...</span></span></code></pre>  <p>I zeroed out the first item in the stack to make things easier to visualize. After that pointer gets used to spawn a bullet, we move down the stack, and that entry is now junk.</p> <p>Let&#39;s spawn in a couple more bullets.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">FreeBulletStackPtr</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    FreeBulletStack+</span><span style="color: #D7827E">12</span></span><br><span class="line"></span><br><span class="line"><span style="color: #D7827E">FreeBulletStack</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    </span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    </span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    </span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">3</span><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; &lt;-- We are here</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">4</span></span><br><span class="line"><span style="color: #575279">    ...</span></span></code></pre>  <p>But wait—the first bullet has gone offscreen, so that bullet needs to despawn. First, we set that object to &quot;inactive&quot; in the bullets array, so the update loop knows to skip over it (not pictured). Then, we write its pointer to <code>FreeBulletStack</code>:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">FreeBulletStackPtr</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    FreeBulletStack+</span><span style="color: #D7827E">8</span></span><br><span class="line"></span><br><span class="line"><span style="color: #D7827E">FreeBulletStack</span><span style="color: #575279">:</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    </span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    </span><span style="color: #D7827E">0</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">0</span><span style="color: #575279">    </span><span style="color: #9893A5; font-style: italic">; &lt;-- We are here</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">3</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #286983">dc.l</span><span style="color: #575279">    Bullets+BULLETSIZE*</span><span style="color: #D7827E">4</span></span><br><span class="line"><span style="color: #575279">    ...</span></span></code></pre>  <p>As you can see, the next bullet will spawn into the newly-freed memory of the one that just despawned. This means the bullets can despawn in any order, and we can always instantly figure out where in memory to spawn in the next bullet.</p> <h2>Improvements?</h2><p>This solves the problem of spawning in new bullets, but the update routine still has to loop through the entire list and check if the bullet is active before running physics on it.</p> <p>I&#39;m actually fine with this. In the worst-case scenario where all 200 bullets are active, I need to loop through the entire list anyway. If I make sure I&#39;m hitting 60fps in that scenario, then I&#39;m guaranteed to hit 60fps if there are fewer bullets—even if I&#39;m wasting time by having to find and skip over inactive objects. The overhead of reading and following pointers (e.g. in a linked list) would be less efficient in our worst-case scenario than a simple little check to see if each bullet is inactive.</p> <p>To help with this further, each attack has a &quot;max bullets&quot; value specified. When I design an attack, I approximate the maximum number of bullets that can possibly be onscreen, and I instantiate a list of that size. That ensures the worst-case scenario for that attack is not still looping through 100 inactive objects, and I can dedicate that extra CPU to the design of the attack in some way.</p> <p>I think further optimizing this system would involve really scrutinizing the performance cost of specific CPU instructions or memory reads. I&#39;m already doing that where I can, so it&#39;s diminishing returns at this point.</p> <p>As someone with little experience managing memory and data structures, it was a pleasure to come up with a system that worked well for this very specific need.</p> <p>But oftentimes, the experience I gain (and change in requirements) is causing me to rip out old systems and replace them with something better, so I wouldn&#39;t be surprised if that eventually happens here, too.</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/performant-data-structures-for-bullet-hell/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>CSS-only scrollable navigation bar with dynamic &quot;more&quot; arrow</title>
      <link>https://dansalva.to/css-only-scrollable-navigation-bar-with-dynamic-more-arrow/</link>
      <guid>https://dansalva.to/css-only-scrollable-navigation-bar-with-dynamic-more-arrow/</guid>
      <pubDate>Thu, 15 Jun 2023 00:00:00 GMT</pubDate>
      
      <description>&lt;p&gt;I recently redesigned the &lt;a href=&quot;https://teamsalvato.com/&quot;&gt;Team Salvato website&lt;/a&gt;, moving it away from WordPress (thank goodness!) and into a static site generator, which I feel is appropriate for the content it serves.&lt;/p&gt; &lt;p&gt;(I reused a lot of it for this website, because I&amp;#39;m too lazy to come up with something different!)&lt;/p&gt; &lt;p&gt;When thinking about a mobile layout, I decided that I didn&amp;#39;t want the typical &amp;quot;hamburger menu&amp;quot; icon, because I was never a fan of those myself—I&amp;#39;m not sure why, but I always feel a bit bothered for site navigation to be hidden from me until I expand the menu (which inevitably takes up nearly the entire viewport).&lt;/p&gt; &lt;p&gt;So, I decided it would be nice to have the same navigation bar as on desktop, but with the ability to scroll it left and right.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>I recently redesigned the <a href="https://teamsalvato.com/">Team Salvato website</a>, moving it away from WordPress (thank goodness!) and into a static site generator, which I feel is appropriate for the content it serves.</p> <p>(I reused a lot of it for this website, because I&#39;m too lazy to come up with something different!)</p> <p>When thinking about a mobile layout, I decided that I didn&#39;t want the typical &quot;hamburger menu&quot; icon, because I was never a fan of those myself—I&#39;m not sure why, but I always feel a bit bothered for site navigation to be hidden from me until I expand the menu (which inevitably takes up nearly the entire viewport).</p> <p>So, I decided it would be nice to have the same navigation bar as on desktop, but with the ability to scroll it left and right.</p> <span id="more"></span>  <p>The challenge is that it&#39;s not obviously scrollable, unless maybe the user&#39;s screen size causes a menu item to be cut off at the edge, which would help them figure it out. Not very graceful.</p> <p>My solution was to add a &quot;more&quot; arrow that would appear at the right side of the menu, hinting that it&#39;s scrollable. But I want the arrow to disappear once the user has scrolled all the way to the right, because that&#39;s how UIs typically behave.</p> <p>Can we do this without any JavaScript? Sort of—it&#39;s a <em>little</em> bit janky, and I don&#39;t know if it&#39;s extremely future-proof. But as of this writing, it works in all major browsers.</p> <h2>The code</h2><p>First, let&#39;s define the basic HTML and CSS we&#39;ll be using for the menu bar. (This is slightly simplified for brevity.)</p> <p>HTML:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5">&lt;</span><span style="color: #56949F">nav</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;navbar&quot;</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;nav-list&quot;</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">ul</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Games</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">News</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Extras</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Team</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Contact</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">IP Guidelines</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">            </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Merch</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">li</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">ul</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span><br><span class="line"><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">nav</span><span style="color: #9893A5">&gt;</span></span></code></pre>  <p>CSS:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #907AA9; font-style: italic">navbar</span><span style="color: #575279"> </span><span style="color: #797593">&#123;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">margin</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">padding</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">1.5</span><span style="color: #286983">rem</span><span style="color: #EA9D34"> </span><span style="color: #D7827E">0</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">position</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">sticky</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">width</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">100</span><span style="color: #286983">%</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">height</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">2.5</span><span style="color: #286983">rem</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">top</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">background</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #797593">#</span><span style="color: #286983">fff</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">z-index</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">2</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">max-width</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">100</span><span style="color: #286983">vw</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #797593">&#125;</span></span><br><span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #907AA9; font-style: italic">nav-list</span><span style="color: #575279"> </span><span style="color: #797593">&#123;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">height</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">100</span><span style="color: #286983">%</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">overflow-x</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">scroll</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">scrollbar-width</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">none</span><span style="color: #797593">;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">/*</span><span style="color: #9893A5; font-style: italic"> Firefox </span><span style="color: #797593; font-style: italic">*/</span></span><br><span class="line"><span style="color: #797593">&#125;</span></span><br><span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #907AA9; font-style: italic">navbar</span><span style="color: #575279"> </span><span style="color: #56949F">ul</span><span style="color: #575279"> </span><span style="color: #797593">&#123;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">padding-inline-start</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">padding</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #EA9D34"> </span><span style="color: #D7827E">2.5</span><span style="color: #286983">rem</span><span style="color: #EA9D34"> </span><span style="color: #D7827E">0</span><span style="color: #EA9D34"> </span><span style="color: #D7827E">1</span><span style="color: #286983">rem</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">height</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">100</span><span style="color: #286983">%</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">width</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">max-content</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">display</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">flex</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">gap</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0.75</span><span style="color: #286983">rem</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">white-space</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">nowrap</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">margin-block-start</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">margin-block-end</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #797593">&#125;</span></span></code></pre>  <video class="post-video" playsinline controls muted><source src="/images/20230615/1.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p>Looking okay so far!</p> <p>Next, let&#39;s add a &quot;more&quot; arrow to the right side of the bar, to help indicate that it&#39;s scrollable. I&#39;ll use <code>:after</code> to draw a Unicode arrow and give it absolute positioning on the right side of the element.</p> <p>Since it&#39;s overlapping the navbar text, it also needs a background. I&#39;m giving it a subtle gradient, which I think helps indicate that the content is &quot;hidden&quot; behind the overflow.</p> <p>CSS:</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #907AA9; font-style: italic">navbar</span><span style="color: #575279"> </span><span style="color: #56949F">ul</span><span style="color: #797593; font-style: italic">:</span><span style="color: #907AA9; font-style: italic">after</span><span style="color: #575279"> </span><span style="color: #797593">&#123;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">position</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">absolute</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">right</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">top</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">height</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">95</span><span style="color: #286983">%</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">width</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">2</span><span style="color: #286983">rem</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">padding-left</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">0.75</span><span style="color: #286983">rem</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">display</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">flex</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">align-items</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">center</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">justify-content</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">center</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">font-size</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #D7827E">1.75</span><span style="color: #286983">rem</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">content</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #EA9D34">&#39;</span><span style="color: #286983">\0203A</span><span style="color: #EA9D34">&#39;</span><span style="color: #797593">;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">background</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">linear-gradient</span><span style="color: #797593">(</span></span><br><span class="line"><span style="color: #EA9D34">        </span><span style="color: #286983">to</span><span style="color: #EA9D34"> left</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #EA9D34">        </span><span style="color: #797593">#</span><span style="color: #286983">fff</span><span style="color: #EA9D34"> </span><span style="color: #D7827E">1.75</span><span style="color: #286983">rem</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #EA9D34">        transparent</span></span><br><span class="line"><span style="color: #EA9D34">    </span><span style="color: #797593">);</span></span><br><span class="line"><span style="color: #797593">&#125;</span></span></code></pre>  <video class="post-video" playsinline controls muted><source src="/images/20230615/2.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <p>Now, the tricky part—getting the arrow to disappear when we&#39;ve finished scrolling.</p> <p>What we can do is mask out the right side of <code>#navbar ul</code>, so that elements behind the mask don&#39;t get drawn. The cut-out part of the mask scrolls onscreen as we reach the end of the menu, covering up the &quot;more&quot; arrow and making it invisible.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #907AA9; font-style: italic">navbar</span><span style="color: #575279"> </span><span style="color: #56949F">ul</span><span style="color: #575279"> </span><span style="color: #797593">&#123;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">-webkit-mask-image</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">linear-gradient</span><span style="color: #797593">(</span></span><br><span class="line"><span style="color: #EA9D34">        </span><span style="color: #286983">to</span><span style="color: #EA9D34"> left</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #EA9D34">        transparent </span><span style="color: #D7827E">1.5</span><span style="color: #286983">rem</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #EA9D34">        </span><span style="color: #797593">#</span><span style="color: #286983">000</span><span style="color: #EA9D34"> </span><span style="color: #D7827E">1.5</span><span style="color: #286983">rem</span></span><br><span class="line"><span style="color: #EA9D34">    </span><span style="color: #797593">);</span></span><br><span class="line"><span style="color: #797593">&#125;</span></span></code></pre>  <p>We&#39;ll also add that gradient effect to the left side of the menu bar. I think there are a few ways to do this, but this is the simplest I found, without having to add more HTML elements. It&#39;s just another mask gradient, which starts transparent and graduates to a solid color.</p> <pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #907AA9; font-style: italic">nav-list</span><span style="color: #575279"> </span><span style="color: #797593">&#123;</span></span><br><span class="line"><span style="color: #575279">    </span><span style="color: #56949F">-webkit-mask-image</span><span style="color: #797593">:</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">linear-gradient</span><span style="color: #797593">(</span></span><br><span class="line"><span style="color: #EA9D34">        </span><span style="color: #286983">to</span><span style="color: #EA9D34"> right</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #EA9D34">        transparent</span><span style="color: #797593">,</span></span><br><span class="line"><span style="color: #EA9D34">        </span><span style="color: #797593">#</span><span style="color: #286983">000</span><span style="color: #EA9D34"> </span><span style="color: #D7827E">1</span><span style="color: #286983">rem</span></span><br><span class="line"><span style="color: #EA9D34">    </span><span style="color: #797593">);</span></span><br><span class="line"><span style="color: #797593">&#125;</span></span></code></pre>  <p>The final result:</p> <video class="post-video" playsinline controls muted><source src="/images/20230615/3.mp4#t=0.01" type="video/mp4">Your browser does not support the video tag.</video>  <h2>Downsides</h2><p>I can think of a couple downsides to this approach.</p> <p>The first is that scrollable menu bars are not nearly as common as hamburger menus, so the user may not have an immediate intuition that they can scroll.</p> <p>Secondly, the &quot;more&quot; arrow could possibly be mistaken for a button. I don&#39;t think it&#39;s too likely, but it does resemble the button seen at the clickable ends of classic scrollbars, so I&#39;d call it a nonzero chance.</p> <p>Still, the way this solution fuses jank and elegance feels pretty on-brand for me, so naturally it&#39;s a good fit for the website representing my brand. In any case, I&#39;ve seen much worse!</p> ]]></content:encoded>
      
      
      
      
      <comments>https://dansalva.to/css-only-scrollable-navigation-bar-with-dynamic-more-arrow/#disqus_thread</comments>
      
    </item>
    
    <item>
      <title>Magicore Anomala: A bullet hell game for Amiga</title>
      <link>https://dansalva.to/magicore-anomala-a-bullet-hell-game-for-amiga/</link>
      <guid>https://dansalva.to/magicore-anomala-a-bullet-hell-game-for-amiga/</guid>
      <pubDate>Fri, 26 May 2023 15:15:05 GMT</pubDate>
      
      <description>&lt;p&gt;These past two years, I&amp;#39;ve been working off and on to create a game for Amiga, a home computer first released in 1985.&lt;/p&gt; &lt;p&gt;This post is a general overview of the project. In future posts, I will dive into the details of the design, code, game engine, and more.&lt;/p&gt;</description>
      
      
      
      <content:encoded><![CDATA[<p>These past two years, I&#39;ve been working off and on to create a game for Amiga, a home computer first released in 1985.</p> <p>This post is a general overview of the project. In future posts, I will dive into the details of the design, code, game engine, and more.</p> <span id="more"></span>  <div class="video-container"><iframe src="https://www.youtube.com/embed/4SB20aFHc08" frameborder="0" loading="lazy" allowfullscreen></iframe></div>  <p>The game is titled <em>Magicore Anomala</em>. It&#39;s a bullet hell game that is somewhat reminiscent of <em>Touhou</em>, <em>Mega Man</em>, and <em>IWBTG</em> fangames.</p> <p>Thanks to the Amiga&#39;s incredible design, it&#39;s possible to draw hundreds of bullets while maintaining a full 60 frames per second—despite running on 1985 hardware.</p> <h2>But why...?</h2><p>It&#39;s been a lifelong dream of mine to make an Amiga game. My family&#39;s first computer was an Amiga, and I&#39;ve been in love with retro hardware for as long as I can remember.</p> <p>I want to share my passion by making a game for this system that looks, feels, and plays like a modern indie game—something that brings lasting enjoyment and is more than just a curiosity for most.</p> <p><em>Magicore</em> was conceptualized specifically to leverage Amiga&#39;s unique hardware capabilities, so the platform and the game&#39;s design go hand in hand. I love that sort of thing, allowing limitations to breed creativity.</p> <h2>Hardware overview</h2><p>Amiga is powered by a 7MHz Motorola 68000. It&#39;s the same CPU found in Macintosh, Sega Genesis, and many other systems of the time. It has 512kb onboard RAM, and usually at least 512kb of expansion RAM.</p> <p>The normalcy of Amiga ends there, because everything else about the hardware is pretty ludicrous for the time period. It has full bitmap graphics (no tile or text modes at all, in fact) with a maximum color range of 4,096 colors. As for audio, you get 4-channel PCM—it has full digital audio playback.</p> <p>If that wasn&#39;t enough, Amiga famously has a set of coprocessors that effectively run <em>in parallel</em> with the CPU. Most notable is the blitter, which can be thought of as a GPU that excels at copying, shifting, and masking blocks of memory. (Yes, we&#39;re using the blitter to draw all those bullets!)</p> <h2>Bitplanes</h2><p>Early Macintosh computers famously have a 1-bit display—each pixel can either be on or off. Amiga is similar, except you can stack up to 5 of these displays on top of each other. This means each pixel is a &quot;stack&quot; of 5 bits, and together, those bits determine which color to draw. For example, a stack of bits <code>10110</code> corresponds to color slot 22. (We get to pick what color goes into each slot.)</p> <p>These 5 &quot;screens&quot; (bitplanes) are completely independent of each other and can each be anywhere in memory. We can update any of the screen locations any time we wish. This is very cool, and it lets you do some wild things.</p> <p><em>Magicore</em> is possible because the bullets layer is a single bitplane, meaning each pixel is only 1 bit. Using the blitter, we can clear and redraw this layer every frame, without having to touch the background.</p> <p>At a resolution of 320x200, one bitplane is 8kb in size. For the blitter, that&#39;s child&#39;s play.</p> <p>I mentioned that <em>Magicore</em> was inspired by the Amiga&#39;s capabilities—that may make more sense now. If I was going to make an Amiga game, I wanted it to do something special with Amiga&#39;s graphics hardware, to take advantage of its unique design in some way. Rather than try to mimic full-color &quot;chunky&quot; pixels, how can I take advantage of this bitplanes system? Bullet hell was the answer!</p> <h2>Dev environment</h2><p>It is an absolute blessing that we can use modern tools to develop software for retro systems.</p> <p>There is an <a href="https://github.com/prb28/vscode-amiga-assembly">Amiga Assembly extension</a> for VS Code, which makes it very easy to build and even debug Amiga software right in the editor. I do most of my coding in <a href="https://neovim.io/">Neovim</a> with the <a href="https://github.com/grahambates/m68k-lsp">m68k LSP</a>, but I hop over to VS Code when I need to debug.</p> <p>(Update: I&#39;ve since managed to get a debugger set up in Neovim thanks to the <a href="https://github.com/grahambates/uae-dap">UAE DAP</a>, so VS Code isn&#39;t being used much anymore.)</p> <p>The entire game is written in Assembly language. Amazingly, C is a first-class citizen on Amiga (even the OS was written in C), but Assembly is definitely the way to go for a game that demands high performance at almost all times.</p> <p>Meanwhile, the tooling for building the game data and level files is written in Python. It&#39;s built around Build-A-File, a framework that lets you easily define and assemble custom binary file formats. It was originally an internal tool, but I put some extra work into it so that I could open-source it. Find it <a href="https://github.com/dansalvato/build-a-file">here</a>.</p> <p>Again, thanks to these modern tools, it takes less than 1 second to go from hitting &quot;build&quot; to having the emulator start up. This enables some very rapid design and testing.</p> <h2>Future content</h2><p>I expect <em>Magicore</em> to be in development for another 2 years or so. During this time, I hope to write some more posts that go into greater detail on specific aspects of the game engine, or problems I&#39;ve had to solve.</p> <p>Thanks for your interest in this passion project of mine!</p> ]]></content:encoded>
      
      
      <category domain="https://dansalva.to/blog/magicore/">Magicore</category>
      
      
      
      <comments>https://dansalva.to/magicore-anomala-a-bullet-hell-game-for-amiga/#disqus_thread</comments>
      
    </item>
    
  </channel>
</rss>
