<?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>Rivija Pesara</title>
<link>https://rivija21.github.io/</link>
<description>Rivija Pesara, an electronic and telecommunication engineering undergraduate at the University of Moratuwa. Notes on hardware, signals, compilers and machine learning.</description>
<language>en</language>
<lastBuildDate>Sat, 24 Oct 2026 12:00:00 +0000</lastBuildDate>
<atom:link href="https://rivija21.github.io/feed.xml" rel="self" type="application/rss+xml"/>
<item>
<title>Hello, world</title>
<link>https://rivija21.github.io/posts/hello-world/</link>
<guid>https://rivija21.github.io/posts/hello-world/</guid>
<pubDate>Sat, 24 Oct 2026 12:00:00 +0000</pubDate>
<description>A new home for my notes, projects and updates, styled like the terminal I spend most of my time in.</description><category>meta</category>
<content:encoded><![CDATA[<div class="hl" data-lang="sh"><pre><code>$<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"hello, world"</span>
hello,<span class="w"> </span>world
</code></pre></div>
<p>Welcome to my blog. I&rsquo;m Rivija, an electronic and telecommunication engineering student at the University of Moratuwa, and this is where I&rsquo;ll write about what I&rsquo;m building and learning.</p>
<p>Until today, this address showed a single page: a motivational poster about finding the perfect avocado at the supermarket. It was the first thing I ever put here, so it hasn&rsquo;t gone anywhere. <a href="https://rivija21.github.io/posts/that-special-moment/">It has its own post now</a>.</p>
<h2 id="what-youll-find-here">What you&rsquo;ll find here</h2>
<ul>
<li><strong>Posts</strong> about my projects: hardware, signals, compilers and machine learning. Mostly the parts that surprised me.</li>
<li><strong><a href="https://rivija21.github.io/updates/">Updates</a></strong>: one-line notes when something works (or breaks). The latest few are always in the sidebar.</li>
<li><strong><a href="https://rivija21.github.io/projects/">Projects</a></strong>: everything I&rsquo;ve built, for university and for fun, in one place.</li>
</ul>
<h2 id="how-this-site-works">How this site works</h2>
<p>Every post is a plain Markdown file with a few lines at the top:</p>
<div class="hl" data-lang="markdown" data-title="content/posts/2026-10-24-hello-world.md"><pre><code>---
title: "Hello, world"
date: 2026-10-24
<span class="gu">tags: [meta]</span>
<span class="gu">---</span>

Welcome to my blog...
</code></pre></div>
<p>A short Python script, <code>build.py</code>, turns those files into the pages you&rsquo;re reading. When I push to GitHub, GitHub Pages builds and publishes the site automatically. It even handles maths, which will come in handy:</p>
<div class="math-block"><span class="math display">$$e^{i\pi} + 1 = 0$$</span></div>
<p>Thanks for stopping by.</p>]]></content:encoded>
</item><item>
<title>Building my own computer, from the instruction set up</title>
<link>https://rivija21.github.io/posts/building-my-own-computer/</link>
<guid>https://rivija21.github.io/posts/building-my-own-computer/</guid>
<pubDate>Fri, 23 Oct 2026 12:00:00 +0000</pubDate>
<description>My own instruction set, two CPUs in Verilog, caches, a C compiler and a small Unix-like operating system, all aimed at a Tang Nano 20K FPGA board.</description><category>hardware</category><category>fpga</category>
<content:encoded><![CDATA[<p>Most of my courses explain one layer of a computer at a time: logic, then processors, then compilers, then operating systems. I wanted to see how the layers fit together, so I decided to design every one of them myself and run the result on real hardware. The project is called <strong>Bedrock</strong>.</p>
<p>The target board is a Sipeed <strong>Tang Nano 20K</strong>: a Gowin GW2AR-18 FPGA with 8 MB of SDRAM in the same package. It&rsquo;s small and cheap, but it&rsquo;s enough for a complete little computer.</p>
<h2 id="the-layers">The layers</h2>
<div class="table-wrap"><table>
<thead>
<tr>
<th>Layer</th>
<th>What I built</th>
</tr>
</thead>
<tbody>
<tr>
<td>Instruction set</td>
<td><strong>BR32</strong>, my own 32-bit RISC instruction set</td>
</tr>
<tr>
<td>Processors</td>
<td><strong>M1</strong>, a multi-cycle CPU, and <strong>P5</strong>, a 5-stage pipeline, both in Verilog</td>
</tr>
<tr>
<td>Memory</td>
<td>instruction and data caches, a write-back system cache and an SDRAM controller</td>
</tr>
<tr>
<td>Toolchain</td>
<td>an assembler, linker and emulator in Python, plus a C compiler (a port of chibicc)</td>
</tr>
<tr>
<td>Operating system</td>
<td><strong>Flint</strong>, a small Unix-like kernel inspired by xv6, with a shell and programs like <code>ls</code>, <code>cat</code>, <code>grep</code> and <code>ps</code></td>
</tr>
</tbody>
</table></div>
<p>I even built an ALU out of 2,991 NAND gates, just to prove to myself that it all really does come down to one kind of gate. It can be dropped into the CPU in place of the normal one.</p>
<h2 id="testing-everything-in-lockstep">Testing everything in lockstep</h2>
<p>A bug in a CPU can hide for millions of cycles. To catch them early, the Python emulator acts as a golden model. The Verilog design runs in Verilator <strong>in lockstep</strong> with it, and the moment the two disagree about a single register, the simulation stops and says where.</p>
<div class="hl" data-lang="sh"><pre><code>make<span class="w"> </span><span class="nb">test</span><span class="w">         </span><span class="c1"># instruction, compiler and memory tests on both CPUs</span>
make<span class="w"> </span>sim<span class="w"> </span><span class="nv">CPU</span><span class="o">=</span>p5<span class="w">   </span><span class="c1"># boot Flint on the pipelined CPU in Verilator</span>
make<span class="w"> </span>bitstream<span class="w">    </span><span class="c1"># build the FPGA image for the Tang Nano 20K</span>
</code></pre></div>
<p>Every test passes on the emulator, on both CPUs, and on a model of the board&rsquo;s real memory system.</p>
<h2 id="what-a-cache-is-worth">What a cache is worth</h2>
<p>The most satisfying change so far was making the system cache <strong>write-back</strong> instead of write-through. Here&rsquo;s the average number of clock cycles per instruction (CPI) on my benchmark:</p>
<div class="table-wrap"><table>
<thead>
<tr>
<th>CPU</th>
<th style="text-align: right;">Before</th>
<th style="text-align: right;">After</th>
</tr>
</thead>
<tbody>
<tr>
<td>M1 (multi-cycle)</td>
<td style="text-align: right;">7.79</td>
<td style="text-align: right;">6.81</td>
</tr>
<tr>
<td>P5 (pipelined)</td>
<td style="text-align: right;">2.64</td>
<td style="text-align: right;">1.34</td>
</tr>
</tbody>
</table></div>
<p>With the new cache, the pipelined CPU runs as fast as it does with perfect, zero-wait memory.</p>
<p>I also added a <code>czero</code> instruction that zeroes a whole 16-byte cache line at once, to speed up <code>memset</code> and copying. It helped on the pipeline, but it made copying slightly <em>slower</em> on the multi-cycle CPU, where the extra instruction cost as much as the memory traffic it saved. Measure, don&rsquo;t guess.</p>
<h2 id="next-the-real-board">Next: the real board</h2>
<p>So far all of this runs in simulation. Both designs fit on the FPGA (about 30% and 40% of the logic) and close timing at the board&rsquo;s 27 MHz clock. The next step is to load the bitstream onto the actual Tang Nano and watch Flint boot over the serial port. That will be its own post.</p>]]></content:encoded>
</item><item>
<title>A neural network from scratch, down to the last gradient</title>
<link>https://rivija21.github.io/posts/neural-network-from-scratch/</link>
<guid>https://rivija21.github.io/posts/neural-network-from-scratch/</guid>
<pubDate>Mon, 19 Oct 2026 12:00:00 +0000</pubDate>
<description>I wrote my own deep-learning engine, with automatic differentiation, convolutions and optimisers, and taught it to read handwritten digits.</description><category>ml</category>
<content:encoded><![CDATA[<p>Frameworks like PyTorch make training a network feel like magic: call <code>loss.backward()</code> and the gradients appear. I wanted to know exactly what happens inside that call, so I built <strong>Neural Lab</strong>, a small deep-learning engine written from scratch, and trained it on MNIST, the classic dataset of handwritten digits.</p>
<h2 id="the-whole-trick-the-chain-rule">The whole trick: the chain rule</h2>
<p>A network is a long chain of simple operations. Training needs the gradient of the loss <span class="math">$L$</span> with respect to every weight <span class="math">$w$</span>, and the chain rule gives it one step at a time:</p>
<div class="math-block"><span class="math display">$$\frac{\partial L}{\partial w} = \frac{\partial L}{\partial y}\,\frac{\partial y}{\partial w}$$</span></div>
<p>Once you have the gradient, you nudge each weight downhill, scaled by the learning rate <span class="math">$\eta$</span>:</p>
<div class="math-block"><span class="math display">$$w \leftarrow w - \eta\,\frac{\partial L}{\partial w}$$</span></div>
<p><strong>Reverse-mode automatic differentiation</strong> records every operation as it runs, then walks the record backwards, applying the chain rule at each step. Here&rsquo;s the core idea in a few lines of Python:</p>
<div class="hl" data-lang="python" data-title="autodiff.py"><pre><code><span class="k">class</span><span class="w"> </span><span class="nc">Value</span><span class="p">:</span>
    <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">parents</span><span class="o">=</span><span class="p">()):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">grad</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">parents</span> <span class="o">=</span> <span class="n">data</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">parents</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_backward</span> <span class="o">=</span> <span class="k">lambda</span><span class="p">:</span> <span class="kc">None</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="n">out</span> <span class="o">=</span> <span class="n">Value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">+</span> <span class="n">other</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">))</span>
        <span class="k">def</span><span class="w"> </span><span class="nf">_backward</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">grad</span> <span class="o">+=</span> <span class="n">out</span><span class="o">.</span><span class="n">grad</span>
            <span class="n">other</span><span class="o">.</span><span class="n">grad</span> <span class="o">+=</span> <span class="n">out</span><span class="o">.</span><span class="n">grad</span>
        <span class="n">out</span><span class="o">.</span><span class="n">_backward</span> <span class="o">=</span> <span class="n">_backward</span>
        <span class="k">return</span> <span class="n">out</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__mul__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="n">out</span> <span class="o">=</span> <span class="n">Value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">*</span> <span class="n">other</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">))</span>
        <span class="k">def</span><span class="w"> </span><span class="nf">_backward</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">grad</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">data</span> <span class="o">*</span> <span class="n">out</span><span class="o">.</span><span class="n">grad</span>
            <span class="n">other</span><span class="o">.</span><span class="n">grad</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">*</span> <span class="n">out</span><span class="o">.</span><span class="n">grad</span>
        <span class="n">out</span><span class="o">.</span><span class="n">_backward</span> <span class="o">=</span> <span class="n">_backward</span>
        <span class="k">return</span> <span class="n">out</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">backward</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">order</span><span class="p">,</span> <span class="n">seen</span> <span class="o">=</span> <span class="p">[],</span> <span class="nb">set</span><span class="p">()</span>
        <span class="k">def</span><span class="w"> </span><span class="nf">visit</span><span class="p">(</span><span class="n">v</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">v</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">seen</span><span class="p">:</span>
                <span class="n">seen</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">v</span><span class="o">.</span><span class="n">parents</span><span class="p">:</span>
                    <span class="n">visit</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
                <span class="n">order</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
        <span class="n">visit</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">grad</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">order</span><span class="p">):</span>
            <span class="n">v</span><span class="o">.</span><span class="n">_backward</span><span class="p">()</span>

<span class="n">x</span><span class="p">,</span> <span class="n">w</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">Value</span><span class="p">(</span><span class="mf">2.0</span><span class="p">),</span> <span class="n">Value</span><span class="p">(</span><span class="o">-</span><span class="mf">3.0</span><span class="p">),</span> <span class="n">Value</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">w</span> <span class="o">+</span> <span class="n">b</span>
<span class="n">y</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">w</span><span class="o">.</span><span class="n">grad</span><span class="p">)</span>   <span class="c1"># 2.0, because dy/dw = x</span>
</code></pre></div>
<p>Neural Lab does the same thing with whole tensors instead of single numbers, and adds what a real network needs: dense and convolutional layers, and the usual optimisers.</p>
<h2 id="trust-but-check-the-gradients">Trust, but check the gradients</h2>
<p>A wrong gradient doesn&rsquo;t crash anything. The network just learns badly, and you never find out why. So every operation is checked against a numerical estimate:</p>
<div class="math-block"><span class="math display">$$\frac{\partial f}{\partial x} \approx \frac{f(x+h) - f(x-h)}{2h}$$</span></div>
<p>All 49 gradient checks pass, and 24 results match an independent NumPy implementation. One check taught me something: for a deep network of sigmoids, the gradients were so tiny that I had to use a bigger step <span class="math">$h$</span>. That&rsquo;s the <strong>vanishing gradient</strong> problem, seen up close.</p>
<h2 id="the-result">The result</h2>
<p>A LeNet-style convolutional network reaches <strong>98.7% accuracy</strong> on the 10,000 test digits after 3 passes over the training data, in about 13 seconds on my M1 MacBook.</p>
<p>My favourite part is the Draw station: write a digit with the mouse and see what the network thinks it is.</p>]]></content:encoded>
</item><item>
<title>A spinning donut and a ripple tank, in plain text</title>
<link>https://rivija21.github.io/posts/ascii-donut-and-ripples/</link>
<guid>https://rivija21.github.io/posts/ascii-donut-and-ripples/</guid>
<pubDate>Sat, 17 Oct 2026 12:00:00 +0000</pubDate>
<description>My terminal animations now run on macOS, the ASCII donut flies through 3D space, and there&#x27;s a new ripple tank you can click to make waves.</description><category>c++</category><category>simulation</category>
<content:encoded><![CDATA[<p><a href="https://github.com/rivija21/ASCII-doughnut">ASCII-doughnut</a> is my collection of C++ animations that draw with nothing but text characters in a terminal. It started as a Windows-only donut. It now runs on macOS too, and has grown a few new tricks.</p>
<h2 id="the-donut-in-flight">The donut, in flight</h2>
<p>The donut is a torus. Every frame, each point on its surface is rotated, projected onto the screen and lit, and its brightness picks a character from a ramp like <code>.,-~:;=!*#$@</code>. A depth buffer makes sure nearer points hide farther ones. Now the donut also flies along a figure-eight path in 3D.</p>
<h2 id="a-ripple-tank-you-can-click">A ripple tank you can click</h2>
<p>The newest simulation is a <strong>ripple tank</strong>. Click anywhere in the terminal (on macOS) and a wave spreads out from that point. Underneath is the 2D wave equation:</p>
<div class="math-block"><span class="math display">$$\frac{\partial^2 u}{\partial t^2} = c^2\,\nabla^2 u$$</span></div>
<p>On a grid, with a time step and a spacing, it becomes a simple update. The next value at each point depends on its current value, its previous value and its four neighbours:</p>
<div class="math-block"><span class="math display">$$u^{t+1}_{i,j} = 2u^{t}_{i,j} - u^{t-1}_{i,j} + C^2\left(u^{t}_{i+1,j} + u^{t}_{i-1,j} + u^{t}_{i,j+1} + u^{t}_{i,j-1} - 4u^{t}_{i,j}\right)$$</span></div>
<p>In code, that&rsquo;s two loops:</p>
<div class="hl" data-lang="cpp"><pre><code><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">H</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">y</span><span class="p">)</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">W</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="kt">float</span><span class="w"> </span><span class="n">lap</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">u</span><span class="p">[</span><span class="n">y</span><span class="p">][</span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">u</span><span class="p">[</span><span class="n">y</span><span class="p">][</span><span class="n">x</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">u</span><span class="p">[</span><span class="n">y</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">][</span><span class="n">x</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">u</span><span class="p">[</span><span class="n">y</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">][</span><span class="n">x</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">u</span><span class="p">[</span><span class="n">y</span><span class="p">][</span><span class="n">x</span><span class="p">];</span>
<span class="w">        </span><span class="n">next</span><span class="p">[</span><span class="n">y</span><span class="p">][</span><span class="n">x</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">u</span><span class="p">[</span><span class="n">y</span><span class="p">][</span><span class="n">x</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">prev</span><span class="p">[</span><span class="n">y</span><span class="p">][</span><span class="n">x</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">C2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">lap</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
</code></pre></div>
<p>The constant <span class="math">$C^2$</span> (the square of the Courant number) controls how far a wave travels in one step. I use <span class="math">$C^2 = 0.25$</span>. In two dimensions the simulation becomes unstable and blows up if <span class="math">$C^2 &gt; 0.5$</span>.</p>
<p>The edges absorb waves like open water, so ripples fade out instead of bouncing back forever. You can also press <strong>E</strong> to add walls, <strong>B</strong> for the classic double slit (and see the interference pattern appear), <strong>W</strong> for a wave maker and <strong>C</strong> to calm the water.</p>
<h2 id="mouse-clicks-in-a-terminal">Mouse clicks in a terminal?</h2>
<p>Terminals can report mouse clicks if you ask nicely. Printing the escape codes <code>ESC[?1000h</code> and <code>ESC[?1006h</code> turns on mouse reporting. After that, every click arrives as a short text sequence with its row and column, which the program reads like a key press.</p>]]></content:encoded>
</item><item>
<title>From C to RISC-V: following one program down to the pipeline</title>
<link>https://rivija21.github.io/posts/from-c-to-risc-v/</link>
<guid>https://rivija21.github.io/posts/from-c-to-risc-v/</guid>
<pubDate>Fri, 16 Oct 2026 12:00:00 +0000</pubDate>
<description>A compiler, assembler and linker for RISC-V, plus a 5-stage pipeline and caches, built so I could watch a C program turn into hardware activity.</description><category>compilers</category><category>hardware</category>
<content:encoded><![CDATA[<p>We study computer architecture from Patterson &amp; Hennessy&rsquo;s RISC-V edition. The book explains each step from C code to a running processor, but I wanted to <em>watch</em> one program go through every step. So I built <strong>Silicon Path</strong>.</p>
<h2 id="the-stations">The stations</h2>
<p>Silicon Path takes a small C program through every step, and you can stop and look at each one:</p>
<ol>
<li><strong>Tokens</strong>: the lexer splits the source into words and symbols.</li>
<li><strong>Syntax tree</strong>: the parser works out the program&rsquo;s structure.</li>
<li><strong>Symbols and types</strong>: semantic analysis checks that the program makes sense.</li>
<li><strong>IR</strong>: the tree is lowered to simple three-address code, and seven optimisation passes clean it up.</li>
<li><strong>RISC-V assembly</strong>: register allocation and code generation for RV32IM.</li>
<li><strong>Machine code and linking</strong>: my own assembler and linker produce a binary.</li>
<li><strong>The pipeline</strong>: the classic 5-stage pipeline (IF, ID, EX, MEM, WB) with hazards, forwarding and stalls, plus L1 and L2 caches in front of memory.</li>
</ol>
<h2 id="one-loop-all-the-way-down">One loop, all the way down</h2>
<p>Take this function:</p>
<div class="hl" data-lang="c"><pre><code><span class="kt">int</span><span class="w"> </span><span class="nf">sum</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="w">        </span><span class="n">s</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">s</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>Its loop turns into RISC-V along these lines:</p>
<div class="hl" data-lang="asm"><pre><code><span class="nl">sum:</span><span class="w">    </span><span class="nf">li</span><span class="w">   </span><span class="no">t0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="w">          </span><span class="c1"># s = 0</span>
<span class="w">        </span><span class="nf">li</span><span class="w">   </span><span class="no">t1</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="w">          </span><span class="c1"># i = 0</span>
<span class="nl">loop:</span><span class="w">   </span><span class="nf">bge</span><span class="w">  </span><span class="no">t1</span><span class="p">,</span><span class="w"> </span><span class="no">a1</span><span class="p">,</span><span class="w"> </span><span class="no">done</span><span class="w">   </span><span class="c1"># stop when i &gt;= n</span>
<span class="w">        </span><span class="nf">slli</span><span class="w"> </span><span class="no">t2</span><span class="p">,</span><span class="w"> </span><span class="no">t1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="w">      </span><span class="c1"># byte offset = i * 4</span>
<span class="w">        </span><span class="nf">add</span><span class="w">  </span><span class="no">t2</span><span class="p">,</span><span class="w"> </span><span class="no">a0</span><span class="p">,</span><span class="w"> </span><span class="no">t2</span><span class="w">     </span><span class="c1"># address of a[i]</span>
<span class="w">        </span><span class="nf">lw</span><span class="w">   </span><span class="no">t3</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">(</span><span class="no">t2</span><span class="p">)</span><span class="w">      </span><span class="c1"># load a[i]</span>
<span class="w">        </span><span class="nf">add</span><span class="w">  </span><span class="no">t0</span><span class="p">,</span><span class="w"> </span><span class="no">t0</span><span class="p">,</span><span class="w"> </span><span class="no">t3</span><span class="w">     </span><span class="c1"># s += a[i]</span>
<span class="w">        </span><span class="nf">addi</span><span class="w"> </span><span class="no">t1</span><span class="p">,</span><span class="w"> </span><span class="no">t1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="w">      </span><span class="c1"># i++</span>
<span class="w">        </span><span class="nf">j</span><span class="w">    </span><span class="no">loop</span>
<span class="nl">done:</span><span class="w">   </span><span class="nf">mv</span><span class="w">   </span><span class="no">a0</span><span class="p">,</span><span class="w"> </span><span class="no">t0</span><span class="w">         </span><span class="c1"># return s</span>
<span class="w">        </span><span class="nf">ret</span>
</code></pre></div>
<p>Look at the <code>lw</code> followed straight away by an <code>add</code> that uses the loaded value. Even with forwarding, the pipeline has to <strong>stall for one cycle</strong>, because the data only arrives at the end of the MEM stage. In the pipeline view you can see the bubble move through the stages. That&rsquo;s a load-use hazard, the kind of thing the book draws as a diagram, happening live.</p>
<h2 id="how-do-you-know-a-compiler-is-right">How do you know a compiler is right?</h2>
<p>You compare it against something you trust. Every test program runs three ways: on a reference interpreter that walks the syntax tree, on an instruction-set simulator, and on the cycle-accurate pipeline. All three must print the same output. The hand-written tests are also checked against the output of clang.</p>
<p>On top of that, a random program generator writes over a thousand programs, and each one is compiled with 18 different build settings and run on 5 pipeline configurations. If any of them disagree, there&rsquo;s a bug.</p>
<p>The code is on GitHub: <a href="https://github.com/rivija21/silicon_RISC">rivija21/silicon_RISC</a>.</p>]]></content:encoded>
</item><item>
<title>Filters you can hear</title>
<link>https://rivija21.github.io/posts/filters-you-can-hear/</link>
<guid>https://rivija21.github.io/posts/filters-you-can-hear/</guid>
<pubDate>Thu, 08 Oct 2026 12:00:00 +0000</pubDate>
<description>Filter Lab lets you design a digital filter, drag its poles and zeros around, and hear the result live. Seeing a frequency response is one thing; hearing it is another.</description><category>dsp</category>
<content:encoded><![CDATA[<p>In signals courses, filters live on paper: transfer functions, pole–zero plots, Bode plots. I wanted to <em>hear</em> what a pole does. So I built <strong>Filter Lab</strong>: design a filter, see its poles, zeros and frequency response, and listen to it live on music, test signals or your microphone. Press Space to switch between the original and filtered sound.</p>
<h2 id="from-analog-prototype-to-digital-filter">From analog prototype to digital filter</h2>
<p>The classic IIR families (Butterworth, Chebyshev I and II, Elliptic and Bessel) are all designed in the analog domain first. The <strong>bilinear transform</strong> then maps them to digital filters:</p>
<div class="math-block"><span class="math display">$$s = \frac{2}{T}\,\frac{z - 1}{z + 1}$$</span></div>
<p>It maps the whole left half of the <span class="math">$s$</span>-plane into the unit circle, so a stable analog filter always becomes a stable digital one. The catch is that it squeezes the frequency axis:</p>
<div class="math-block"><span class="math display">$$\Omega = \frac{2}{T}\tan\frac{\omega}{2}$$</span></div>
<p>So the cutoff frequency has to be <strong>pre-warped</strong> before the analog design, or it lands in the wrong place. Filter Lab follows the same steps as SciPy&rsquo;s <code>iirfilter</code>: analog prototype, band transform, then the bilinear transform with pre-warping.</p>
<h2 id="checking-against-scipy">Checking against SciPy</h2>
<p>To be sure the maths was right, I compared Filter Lab with SciPy on 780 random designs. The poles and zeros agree to about <span class="math">$10^{-12}$</span>, and the magnitude responses to <span class="math">$10^{-9}$</span> dB. The reference side is only a couple of lines:</p>
<div class="hl" data-lang="python"><pre><code><span class="kn">from</span><span class="w"> </span><span class="nn">scipy</span><span class="w"> </span><span class="kn">import</span> <span class="n">signal</span>

<span class="n">sos</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">butter</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">btype</span><span class="o">=</span><span class="s2">"low"</span><span class="p">,</span> <span class="n">fs</span><span class="o">=</span><span class="mi">48000</span><span class="p">,</span> <span class="n">output</span><span class="o">=</span><span class="s2">"sos"</span><span class="p">)</span>
<span class="n">w</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">sosfreqz</span><span class="p">(</span><span class="n">sos</span><span class="p">,</span> <span class="n">fs</span><span class="o">=</span><span class="mi">48000</span><span class="p">)</span>
</code></pre></div>
<h2 id="things-to-try">Things to try</h2>
<ul>
<li><strong>Push a pole outside the unit circle</strong> and watch the impulse response blow up. That&rsquo;s instability, and you can hear it.</li>
<li>Load the <strong>50 Hz hum remover</strong> preset on music with mains hum added. (Sri Lanka&rsquo;s mains frequency is 50 Hz.) The notch is surprisingly narrow.</li>
<li>Compare a Butterworth and an Elliptic filter of the same order. The elliptic one is much steeper, but listen to the ripple in its passband.</li>
</ul>
<p>Filter Lab can also export the design as MATLAB, Python or C code, and save any plot as a PNG for a lab report. The code is on GitHub: <a href="https://github.com/rivija21/interactive-DSP">rivija21/interactive-DSP</a>.</p>]]></content:encoded>
</item><item>
<title>I stopped typing my Wi-Fi password</title>
<link>https://rivija21.github.io/posts/campus-wifi-auto-login/</link>
<guid>https://rivija21.github.io/posts/campus-wifi-auto-login/</guid>
<pubDate>Tue, 06 Oct 2026 12:00:00 +0000</pubDate>
<description>Every time my laptop woke up on campus, the Wi-Fi asked me to log in again. Now a small script does it for me, on Mac and Windows.</description><category>tools</category>
<content:encoded><![CDATA[<p>On the University of Moratuwa network, every time your laptop wakes up you get the Wi-Fi login page again: username, password, click. Several times a day, every day. So I automated it.</p>
<h2 id="what-it-does">What it does</h2>
<p>After you set it up once, the script notices when you join <code>UoM_Wireless</code> and logs you in before you&rsquo;ve even opened a browser.</p>
<ul>
<li><strong>Mac:</strong> a background LaunchAgent that runs when the network changes. Your password is kept in the macOS Keychain.</li>
<li><strong>Windows:</strong> a scheduled task, with the password stored encrypted for your Windows account. This version hasn&rsquo;t been tested on a real Windows PC yet.</li>
</ul>
<p>Setting it up is one double-click and a box for your username and password. Friends are using it too.</p>
<h2 id="the-tricky-part">The tricky part</h2>
<p>Until you log in, macOS lets only its own login pop-up (the Captive Network Assistant) look up names on that network. Other programs get no DNS at all, so a normal script can&rsquo;t even find the login server.</p>
<p>The fix was to tie the script&rsquo;s connection to the Wi-Fi interface itself and ask the campus DNS server directly. Now it logs in about three seconds after the laptop wakes up.</p>
<p>The scripts are on GitHub: <a href="https://github.com/rivija21/captive-portals">rivija21/captive-portals</a>.</p>]]></content:encoded>
</item><item>
<title>That special moment</title>
<link>https://rivija21.github.io/posts/that-special-moment/</link>
<guid>https://rivija21.github.io/posts/that-special-moment/</guid>
<pubDate>Tue, 24 Sep 2024 12:00:00 +0000</pubDate>
<description>The very first page I put on this site. It deserves a place in the archive.</description><category>life</category>
<content:encoded><![CDATA[<p><img loading="lazy" alt="A motivational poster: an avocado in a white frame on black, over the words &quot;That special moment: when you find the perfect avocado at the supermarket&quot;" src="https://rivija21.github.io/images/that-special-moment.svg" /></p>
<p>This poster was the first page I ever published at rivija21.github.io, back in September 2024. The site has grown up since then. The feeling hasn&rsquo;t.</p>]]></content:encoded>
</item>
</channel>
</rss>
