<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title>Such Programming</title>
	<link href="https://suchprogramming.com/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="https://suchprogramming.com"/>
	<generator uri="https://www.getzola.org/">Zola</generator>
	<updated>2023-07-28T00:00:00+00:00</updated>
	<id>https://suchprogramming.com/atom.xml</id>
	<entry xml:lang="en">
		<title>Mad Computer Science – Part 6 – An Open Hardware Workbench</title>
		<published>2023-07-28T00:00:00+00:00</published>
		<updated>2023-07-28T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/mad-computer-science-part-6/" type="text/html"/>
		<id>https://suchprogramming.com/mad-computer-science-part-6/</id>
		<content type="html">&lt;p&gt;Trying to teach myself hardware design was a monumental pain in the ass compared to learning software engineering, but today I want to tell you about 4 projects that are paving the road for anyone to go beyond telling computers what to do and to start telling them what to be!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;hardware-built-for-anyone-and-everyone&quot;&gt;Hardware Built For Anyone and Everyone&lt;&#x2F;h1&gt;
&lt;p&gt;Let&#x27;s start off with the idea that we don&#x27;t want to pay a couple hundred dollars to buy an FPGA board that we have no clue how to use. The board I used to recommend was the Arty A7-35T, which has great publicly available documentation and a pretty nice set of on-board peripherals for about $150 USD. That model has been discontinued though and only the higher trim model is still available at almost double that price. I still like my Arty quite a lot but deprecated already describes way too much of my experience with logic design for FPGAs.&lt;&#x2F;p&gt;
&lt;p&gt;A couple months ago I saw the &lt;a href=&quot;https:&#x2F;&#x2F;pico-ice.tinyvision.ai&#x2F;&quot;&gt;pico-ice by tinyVision.ai&lt;&#x2F;a&gt; and... well it&#x27;s an open source board with an RP2040 and an iCE40 FPGA for $35, that sounds awesome! I was thinking that the RP2040 sounded like an awesome peripheral for the FPGA... or was the FPGA the peripheral? I bet I get to choose and either way I am all for it&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;6&#x2F;pico_ice_front.jpg&quot; alt=&quot;The Pico-Ice&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It didn&#x27;t take very long to arrive, and I was excited to get started. I love the Raspbery Pi Pico but this is my first time working with a Lattice FPGA. I&#x27;m mostly familiar with Vivado so I wanted to try out the Lattice tool chain out to check it out. I downloaded and installed a few gigs of software but...&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;6&#x2F;licenses.png&quot; alt=&quot;Bah!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I dunno, I&#x27;m still waiting to find out&lt;&#x2F;p&gt;
&lt;h1 id=&quot;open-source-you-re-my-only-hope&quot;&gt;Open Source, You&#x27;re my Only Hope&lt;&#x2F;h1&gt;
&lt;p&gt;That was a bit disappointing, but fortunately I have other options.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;YosysHQ&#x2F;oss-cad-suite-build&#x2F;&quot;&gt;&lt;code&gt;oss-cad-suite&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is a nice binary bundle of hardware designs tools. It has HDL synthesizers, simulators and many, many other tools that are useful for logic design. It was incredibly convenient to just download and extract a directory, include it in my &lt;code&gt;PATH&lt;&#x2F;code&gt; and I&#x27;m just about ready to go.&lt;&#x2F;p&gt;
&lt;p&gt;I worked through some documentation for my pico-ice, got a little help from the tinyVision Discord and setup my project. When I finally tried to build the design I was shocked that it took literally 1 second, I assumed something must have gone wrong.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ time fusesoc run --target pico_ice fusesoc:utils:blinky:1.1.1
&lt;&#x2F;span&gt;&lt;span&gt;INFO: Preparing fusesoc:utils:blinky:1.1.1
&lt;&#x2F;span&gt;&lt;span&gt;INFO: Setting up project
&lt;&#x2F;span&gt;&lt;span&gt;INFO: Building
&lt;&#x2F;span&gt;&lt;span&gt;INFO: Running
&lt;&#x2F;span&gt;&lt;span&gt;INFO: Running post_run script iceprog
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Build was completed
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;To program the board run:
&lt;&#x2F;span&gt;&lt;span&gt;iceprog &#x2F;tmp&#x2F;blinky&#x2F;build&#x2F;fusesoc_utils_blinky_1.1.1&#x2F;pico_ice-icestorm&#x2F;fusesoc_utils_blinky_*.bin
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;real	0m1.030s
&lt;&#x2F;span&gt;&lt;span&gt;user	0m0.927s
&lt;&#x2F;span&gt;&lt;span&gt;sys	0m0.104s
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nothing went wrong though! I&#x27;m used to it taking minutes to synthesize even a trivial design. That&#x27;s definitely something I could get used to and I&#x27;m eager to see what other goodies oss-cad-suite has in store, but I&#x27;ve got a couple other great projects to share.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;what-i-was-missing&quot;&gt;What I was missing&lt;&#x2F;h1&gt;
&lt;p&gt;My engineering nightmare is a world without package managers, and that of course leads me to &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;olofk&#x2F;fusesoc&quot;&gt;FuseSoC&lt;&#x2F;a&gt;, a tool for making your logic design experience so much better.&lt;&#x2F;p&gt;
&lt;p&gt;When I first came across FuseSoC I thought it was a high level generator focused on MCU or SoC development and I overlooked its broader capability. It&#x27;s the critical tool that I was missing to enjoy logic design much more. I dabbled a bit with packaging some of my own cores and I&#x27;ll be looking into making many more and trying to contribute to the project as I can.&lt;&#x2F;p&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;7eWRAOK9mns&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; allowfullscreen&gt;&lt;&#x2F;iframe&gt;
&lt;p&gt;I think FuseSoC is awesome and deserves a lot more attention and interest. The code reuse and knowledge share that can be built around this type of package manager can really help people jump into logic design when there are a lot of good building blocks to work with. We need folks of all backgrounds to mess around and share ideas. I can&#x27;t wait to see what the critical mass on this looks like, it&#x27;ll be absolutely amazing.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;oh-and-you-can-make-asics-too&quot;&gt;Oh, and you can make ASICs too&lt;&#x2F;h1&gt;
&lt;p&gt;This to me is the most mind-explodey open source hardware project that has me excited. Through &lt;a href=&quot;https:&#x2F;&#x2F;tinytapeout.com&#x2F;&quot;&gt;Tiny Tapeout&lt;&#x2F;a&gt; you can make your own open source ASIC for like $100. Yes... YOU! If you&#x27;re willing to try&lt;&#x2F;p&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;f4w1QOpHzOo&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; allowfullscreen&gt;&lt;&#x2F;iframe&gt;
&lt;p&gt;Until this project I had pretty much assumed that for me to make an actual chip design was impossible. I&#x27;d never be able to do that... Well now I feel like I have to!&lt;&#x2F;p&gt;
&lt;p&gt;Without even installing anything you could build a small custom chip using open, browser-based schematic design tools or via HDL. I highly recommend checking it out.&lt;&#x2F;p&gt;
&lt;p&gt;My pico-ice is going to be quite busy with some prototyping!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Mad Computer Science – Part 5 – Eating Serial for Breakfast</title>
		<published>2023-06-10T00:00:00+00:00</published>
		<updated>2023-06-10T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/mad-computer-science-part-5/" type="text/html"/>
		<id>https://suchprogramming.com/mad-computer-science-part-5/</id>
		<content type="html">&lt;p&gt;A huge element of software and hardware design is figuring out how various components are going to fit together and talk to each other. This could be interfaces within a codebase like function names and signatures, networked components communicating via REST or gRPC, or the PCI-Express lanes between your processor and GPU. In today&#x27;s episode, I&#x27;ll be experimenting with designing a SystemVerilog module that can work with the classic TTL serial protocol.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;talking-to-my-fpga&quot;&gt;Talking to my FPGA&lt;&#x2F;h1&gt;
&lt;p&gt;In some of my planned projects, I&#x27;m going to want a way to communicate between a digital design I&#x27;ve loaded onto my FPGA and my laptop. Here&#x27;s the design that I&#x27;m working towards here:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;uart-elaborated.png&quot; alt=&quot;UART Design&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;For my target board, the Arty A-7, the easiest available option for me is serial since this board has a USB controller with &lt;code&gt;rx&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;tx&lt;&#x2F;code&gt; pins routed to the FPGA.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;arty-uart.png&quot; alt=&quot;Arty UART&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;
&lt;p&gt;To kick things off, I&#x27;m going to start a &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;KennethWilke&#x2F;wiz&quot;&gt;Wiz&lt;&#x2F;a&gt; project and map some IO pins. For this design I&#x27;ll have the pins: &lt;code&gt;rx&lt;&#x2F;code&gt; and &lt;code&gt;tx&lt;&#x2F;code&gt; for serial, &lt;code&gt;clk&lt;&#x2F;code&gt; as my input clock using the 100mhz clock from the Arty board and &lt;code&gt;reset&lt;&#x2F;code&gt; to help make sure things initialize into a good state.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;package_name &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;uart&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;builder &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;vivado&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;programmer &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;vivado&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target_part &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;xc7a35ticsg324-1L&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target_device &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;xc7a35t_0&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;source_files &lt;&#x2F;span&gt;&lt;span&gt;= [
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;top.sv&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;,
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;bitstream_path &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;uart.bit&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[pins]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;tx &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;D10&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rx &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A9&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;clk &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;E3&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;reset &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;C2&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I find it really easy to swap &lt;code&gt;rx&lt;&#x2F;code&gt; and &lt;code&gt;tx&lt;&#x2F;code&gt; by accident, especially in cases like this where the diagram above says &lt;code&gt;TXD&lt;&#x2F;code&gt; goes to &lt;code&gt;A9&lt;&#x2F;code&gt;. From the perspective of my module that is actually the &lt;code&gt;rx&lt;&#x2F;code&gt; line since that is the line I&#x27;ll be receiving from, so I&#x27;ll map those as such to help me keep their usage straight in my design.&lt;&#x2F;p&gt;
&lt;p&gt;I want to verify that my pins are working as expected, so before I start trying to build some kind of UART I&#x27;m going to just wire &lt;code&gt;rx&lt;&#x2F;code&gt; to &lt;code&gt;tx&lt;&#x2F;code&gt; to create a stupid-simple echo implementation for the serial communication.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;top (
&lt;&#x2F;span&gt;&lt;span&gt;  input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; clk,
&lt;&#x2F;span&gt;&lt;span&gt;  input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; reset,
&lt;&#x2F;span&gt;&lt;span&gt;  input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; rx,
&lt;&#x2F;span&gt;&lt;span&gt;  output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; tx
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;assign&lt;&#x2F;span&gt;&lt;span&gt; tx = rx;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;endmodule
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I&#x27;ll give this a &lt;code&gt;wiz build&lt;&#x2F;code&gt; to build it and a &lt;code&gt;wiz program&lt;&#x2F;code&gt; to slap it on my Arty-A7. I&#x27;ll write a short Python script to use the serial port.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;serial
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;ttl = serial.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Serial&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;dev&#x2F;ttyUSB1&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;115200&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;ttl.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;test!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(ttl.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;decode&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;utf-8&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lastly, I&#x27;ll run that to make sure it works.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ python test.py
&lt;&#x2F;span&gt;&lt;span&gt;test!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Perfect! I now have a working &amp;quot;echo&amp;quot; serial device, though so far I&#x27;d probably just call it a complicated way of jumping two pins together.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;building-a-uart&quot;&gt;Building a UART&lt;&#x2F;h1&gt;
&lt;p&gt;I&#x27;m not familiar with UART design techniques, but through trying to get this project to work I settled on a simple design that&#x27;s based on some stateful shift registers. I&#x27;ll have one register working on the RX side receiving data and one on the TX side sending data.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;uart-design.png&quot; alt=&quot;UART design&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;One of the big considerations for how I want this UART to work is going to be its baud rate. On this board I have a 100mhz input clock to work with, so I can very reliably time a 115200 baud communication. The approach is going to be very similar to a software bitbanging approach, but kind of simpler and very accurate by comparison.&lt;&#x2F;p&gt;
&lt;p&gt;For this post I won&#x27;t detail the TTL Serial protocol in full, as I think &lt;a href=&quot;https:&#x2F;&#x2F;learn.sparkfun.com&#x2F;tutorials&#x2F;serial-communication&#x2F;all&quot;&gt;this Sparkfun tutorial&lt;&#x2F;a&gt; covers that very nicely and it continues to be my go-to resource for UART protocol details. The most important part is the general transmission format, which in my case here will be with 3.3V TTL serial.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;uart-bitsies.png&quot; alt=&quot;UART bits&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll build my design as a state machine. It will start at an initial state where we&#x27;re waiting for the RX line to drop low to indicate the &lt;code&gt;IDLE&lt;&#x2F;code&gt; bit. At that detection, I&#x27;ll reset and start a counter of &lt;code&gt;clk&lt;&#x2F;code&gt; transitions and at the right number of clock cycles I&#x27;ll sample the RX line for data, easy enough!&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll start this part of the design by build the core guts of my RX register:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;UartRxRegister (
&lt;&#x2F;span&gt;&lt;span&gt;    input clk,
&lt;&#x2F;span&gt;&lt;span&gt;    input reset,
&lt;&#x2F;span&gt;&lt;span&gt;    input rx,
&lt;&#x2F;span&gt;&lt;span&gt;    output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; available,
&lt;&#x2F;span&gt;&lt;span&gt;    output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] data
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef enum &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IDLE&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;STOP
&lt;&#x2F;span&gt;&lt;span&gt;  } RxState;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;RxState &lt;&#x2F;span&gt;&lt;span&gt;state;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;  logic&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] counter;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;always &lt;&#x2F;span&gt;&lt;span&gt;@ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;posedge&lt;&#x2F;span&gt;&lt;span&gt; clk or &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;negedge&lt;&#x2F;span&gt;&lt;span&gt; reset) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (!reset) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IDLE&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      available &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;endmodule
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So far this module has it&#x27;s inputs and outputs, an &lt;code&gt;enum&lt;&#x2F;code&gt; defining the &lt;code&gt;RxState&lt;&#x2F;code&gt; type along with the 10 states for this system (&lt;code&gt;IDLE&lt;&#x2F;code&gt;, &lt;code&gt;BIT0&lt;&#x2F;code&gt; through &lt;code&gt;BIT7&lt;&#x2F;code&gt; and &lt;code&gt;STOP&lt;&#x2F;code&gt;). I&#x27;ve also started added the reset logic to default &lt;code&gt;state&lt;&#x2F;code&gt; to &lt;code&gt;IDLE&lt;&#x2F;code&gt; and to set the &lt;code&gt;available&lt;&#x2F;code&gt; output flag low.&lt;&#x2F;p&gt;
&lt;p&gt;Next I&#x27;ll start to shape out the state machine itself.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; (state)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IDLE&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (!rx) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      available &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      counter &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT0&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT1&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT2&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT2&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT3&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT3&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT4&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT4&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT5&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT5&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT6&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT6&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT7&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT7&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;STOP&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;STOP&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IDLE&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    available &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;endcase
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this state machine, the system should wait in &lt;code&gt;IDLE&lt;&#x2F;code&gt; until the &lt;code&gt;rx&lt;&#x2F;code&gt; serial line is brought low for the starting bit. Once the starting bit arrives, the system should begin transitioning through the other states in order until the &lt;code&gt;STOP&lt;&#x2F;code&gt; bit prepares the system to begin again.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;testing-it-live&quot;&gt;Testing it Live&lt;&#x2F;h1&gt;
&lt;p&gt;At this point I want to see how my state machine behaves when presented with some serial traffic, so I&#x27;m going to generate an ILA (integrated logic analyzer) core to debug my design with.&lt;&#x2F;p&gt;
&lt;p&gt;After giving my project a fresh &lt;code&gt;wiz build&lt;&#x2F;code&gt;, I&#x27;ll open up the generated Vivado project and instantiate a new ILA core with 2 inputs: a 1-bit input I&#x27;ll use for &lt;code&gt;rx&lt;&#x2F;code&gt; and a 4-bit value to monitor how the &lt;code&gt;STATE&lt;&#x2F;code&gt; register reacts to it and a 14-bit register for the &lt;code&gt;counter&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;uart-ila.png&quot; alt=&quot;UART ILA&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll have vivado kick off the generator it uses to build the ILA, and add an instantiation of this new module in my rx module:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ila_uart &lt;&#x2F;span&gt;&lt;span&gt;debugger (
&lt;&#x2F;span&gt;&lt;span&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clk&lt;&#x2F;span&gt;&lt;span&gt;(clk),
&lt;&#x2F;span&gt;&lt;span&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe0&lt;&#x2F;span&gt;&lt;span&gt;(rx),
&lt;&#x2F;span&gt;&lt;span&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe1&lt;&#x2F;span&gt;&lt;span&gt;(state),
&lt;&#x2F;span&gt;&lt;span&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe2&lt;&#x2F;span&gt;&lt;span&gt;(counter)
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then I&#x27;ll build the bitstream in Vivado and run it along with the debugging stuff it generated. When I deploy the design via the Hardware Manager in Vivado it&#x27;ll bring up the ILA&#x27;s waveform viewer, where I&#x27;ll see the design is stably in the &lt;code&gt;IDLE&lt;&#x2F;code&gt; state until &lt;code&gt;rx&lt;&#x2F;code&gt; goes low to indicate the start of a transmission.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;uart-ila2.png&quot; alt=&quot;UART ILA&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;reading-time&quot;&gt;Reading Time&lt;&#x2F;h1&gt;
&lt;p&gt;With the state transitions working, I&#x27;ll now work on triggering those transitions as close to the middle point of each bit as I can with my given input clock of 100Mhz. I&#x27;ll refer again to the diagram from SparkFun.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;uart-bitsies.png&quot; alt=&quot;UART bits&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If I want to read the value of the least significant bit, the ideal time to poll at &amp;quot;1.5 baud&amp;quot;. Each next bit is 1 baud away until the stop bit, which we&#x27;re not going to be using so the final transition from STOP to IDLE should happen at 0.5 baud after we read the most significant bit.&lt;&#x2F;p&gt;
&lt;p&gt;To determine how many of the 100Mhz clock cycles we should be waiting for at each transition, I whipped up a python script that spits out the numbers I&#x27;m looking for.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#!&#x2F;usr&#x2F;bin&#x2F;env python3
&lt;&#x2F;span&gt;&lt;span&gt;input_clock = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100000000.0
&lt;&#x2F;span&gt;&lt;span&gt;baud = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;115200.0
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;bauds_per_clock = input_clock &#x2F; baud
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    next_step = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1.5 &lt;&#x2F;span&gt;&lt;span&gt;+ i) * bauds_per_clock)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;BIT&lt;&#x2F;span&gt;&lt;span&gt;{i}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;{next_step}&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;last_step = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9 &lt;&#x2F;span&gt;&lt;span&gt;* bauds_per_clock)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;STOP: &lt;&#x2F;span&gt;&lt;span&gt;{last_step}&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When ran this gives me:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;BIT0: 1302
&lt;&#x2F;span&gt;&lt;span&gt;BIT1: 2170
&lt;&#x2F;span&gt;&lt;span&gt;BIT2: 3038
&lt;&#x2F;span&gt;&lt;span&gt;BIT3: 3906
&lt;&#x2F;span&gt;&lt;span&gt;BIT4: 4774
&lt;&#x2F;span&gt;&lt;span&gt;BIT5: 5642
&lt;&#x2F;span&gt;&lt;span&gt;BIT6: 6510
&lt;&#x2F;span&gt;&lt;span&gt;BIT7: 7378
&lt;&#x2F;span&gt;&lt;span&gt;STOP: 7812
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I&#x27;ll now add checks to each state so that when the &lt;code&gt;counter&lt;&#x2F;code&gt; hits that number it transitions to the next state, I&#x27;ll also go ahead and read &lt;code&gt;rx&lt;&#x2F;code&gt; to set the appropriate bit in my output &lt;code&gt;data&lt;&#x2F;code&gt; register.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; (state)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT0&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1302&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;= rx;
&lt;&#x2F;span&gt;&lt;span&gt;      available &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT1&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2170&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;= rx;
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT2&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT2&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3038&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;= rx;
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT3&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT3&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3906&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;= rx;
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT4&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT4&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4774&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;= rx;
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT5&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT5&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5642&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;= rx;
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT6&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT6&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6510&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;= rx;
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT7&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT7&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7378&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;= rx;
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;STOP&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      available &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;STOP&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7812&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IDLE&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;endcase
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;BIT7&lt;&#x2F;code&gt; gets a little special treatment, as that&#x27;s our last bit so at this point our transferred byte is available and I&#x27;ll indicate that with the &lt;code&gt;available&lt;&#x2F;code&gt; output.&lt;&#x2F;p&gt;
&lt;p&gt;The reading side should be good to go now I think... I&#x27;ll recustomize the ILA core to add probes for &lt;code&gt;data&lt;&#x2F;code&gt; and &lt;code&gt;available&lt;&#x2F;code&gt; so that I can see how they are changing as well.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;uart-ila-reading.png&quot; alt=&quot;ILA reading&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Perfect, the transitions are happening at the right times, and &lt;code&gt;data&lt;&#x2F;code&gt; holds the appropriate values when &lt;code&gt;available&lt;&#x2F;code&gt; is raised!&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;writing-back&quot;&gt;Writing Back&lt;&#x2F;h1&gt;
&lt;p&gt;To wrap up this echoing UART design I&#x27;ll implement the TX register in roughly the same way as the RX register, but basically in reverse. It&#x27;ll take in &lt;code&gt;clk&lt;&#x2F;code&gt; and &lt;code&gt;reset&lt;&#x2F;code&gt; just like RX register, but the TX register will also have the inputs &lt;code&gt;send&lt;&#x2F;code&gt; and &lt;code&gt;data&lt;&#x2F;code&gt; for writing a byte out to its one output: &lt;code&gt;tx&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;UartTxRegister (
&lt;&#x2F;span&gt;&lt;span&gt;    input clk,
&lt;&#x2F;span&gt;&lt;span&gt;    input reset,
&lt;&#x2F;span&gt;&lt;span&gt;    input send,
&lt;&#x2F;span&gt;&lt;span&gt;    input [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] data,
&lt;&#x2F;span&gt;&lt;span&gt;    output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; tx
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The state enum is a little different as well. We&#x27;ll still start in &lt;code&gt;IDLE&lt;&#x2F;code&gt;, but we&#x27;ll need to bring &lt;code&gt;tx&lt;&#x2F;code&gt; low for 1 baud as the &lt;code&gt;START&lt;&#x2F;code&gt; bit. After that, we&#x27;ll send out the data bits.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef enum &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IDLE&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;START&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;} TxState;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Within the module I have a &lt;code&gt;state&lt;&#x2F;code&gt; and &lt;code&gt;counter&lt;&#x2F;code&gt; just like before, but this time I&#x27;ll also add a &lt;code&gt;send_data&lt;&#x2F;code&gt; register that will store the input &lt;code&gt;data&lt;&#x2F;code&gt; when &lt;code&gt;send&lt;&#x2F;code&gt; goes high.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TxState &lt;&#x2F;span&gt;&lt;span&gt;state;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;13&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] counter;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] send_data;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now to implement the logic for sending. I&#x27;ll add some additional outputs to my Python script to give me the clock times I&#x27;ll need.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Writing times:&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;first_step = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;(bauds_per_clock)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;START: &lt;&#x2F;span&gt;&lt;span&gt;{first_step}&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    next_step = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;+ i) * bauds_per_clock)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;BIT&lt;&#x2F;span&gt;&lt;span&gt;{i}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;{next_step}&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Similar to before, this prints out my timing for when each state should transition to the next.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;START: 868
&lt;&#x2F;span&gt;&lt;span&gt;BIT0: 1736
&lt;&#x2F;span&gt;&lt;span&gt;BIT1: 2604
&lt;&#x2F;span&gt;&lt;span&gt;BIT2: 3472
&lt;&#x2F;span&gt;&lt;span&gt;BIT3: 4340
&lt;&#x2F;span&gt;&lt;span&gt;BIT4: 5208
&lt;&#x2F;span&gt;&lt;span&gt;BIT5: 6076
&lt;&#x2F;span&gt;&lt;span&gt;BIT6: 6944
&lt;&#x2F;span&gt;&lt;span&gt;BIT7: 7812
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now to implement the &lt;code&gt;always&lt;&#x2F;code&gt; block. It&#x27;ll start off with the reset logic followed by the state machine. It&#x27;ll hold the &lt;code&gt;counter&lt;&#x2F;code&gt; to &lt;code&gt;0&lt;&#x2F;code&gt; and &lt;code&gt;tx&lt;&#x2F;code&gt; to &lt;code&gt;1&lt;&#x2F;code&gt; while reset or idle. When &lt;code&gt;send&lt;&#x2F;code&gt; is raised the value will be stored in &lt;code&gt;send_data&lt;&#x2F;code&gt;, the &lt;code&gt;state&lt;&#x2F;code&gt; transitions to &lt;code&gt;START&lt;&#x2F;code&gt; and the counter starts counting.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;always &lt;&#x2F;span&gt;&lt;span&gt;@ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;posedge&lt;&#x2F;span&gt;&lt;span&gt; clk) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (!reset) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IDLE&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    tx &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    counter &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end else begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (state == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IDLE&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;      counter &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      tx &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (send) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;        state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;START&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;        send_data &amp;lt;= data;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end else begin
&lt;&#x2F;span&gt;&lt;span&gt;      counter &amp;lt;= counter + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; (state)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;START&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;          tx &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;868&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT0&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;          tx &amp;lt;= send_data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1736&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT1&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;          tx &amp;lt;= send_data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2604&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT2&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT2&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;          tx &amp;lt;= send_data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3472&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT3&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT3&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;          tx &amp;lt;= send_data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4340&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT4&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT4&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;          tx &amp;lt;= send_data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5208&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT5&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT5&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;          tx &amp;lt;= send_data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6076&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT6&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT6&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;          tx &amp;lt;= send_data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6944&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT7&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BIT7&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;          tx &amp;lt;= send_data[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (counter == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7812&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IDLE&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;endcase
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That should take care of the TX register. The last bit left to do is to wire it up on the &lt;code&gt;top&lt;&#x2F;code&gt; module.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;top (
&lt;&#x2F;span&gt;&lt;span&gt;  input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; clk,
&lt;&#x2F;span&gt;&lt;span&gt;  input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; reset,
&lt;&#x2F;span&gt;&lt;span&gt;  input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; rx,
&lt;&#x2F;span&gt;&lt;span&gt;  output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; tx
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;  logic&lt;&#x2F;span&gt;&lt;span&gt; available;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;  logic&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] data;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;UartRxRegister &lt;&#x2F;span&gt;&lt;span&gt;RxRegister(clk, reset, rx, available, data);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;UartTxRegister &lt;&#x2F;span&gt;&lt;span&gt;TxRegister(clk, reset, available, data, tx);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;endmodule
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now to hope it all works! I&#x27;ll instantiate another ILA instance and watch all the good stuff in the &lt;code&gt;top&lt;&#x2F;code&gt; module.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;uart-ila-working.png&quot; alt=&quot;Working echo&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There it is, the UART successfully reads and echos back! This was a pretty large post, but if you made it to the end here I appreciate it and hope you enjoyed the post.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>6502 Reasons to Learn Assembly</title>
		<published>2023-01-29T00:00:00+00:00</published>
		<updated>2023-01-29T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/preview/6502-reasons-to-learn-assembly/" type="text/html"/>
		<id>https://suchprogramming.com/preview/6502-reasons-to-learn-assembly/</id>
		<content type="html">&lt;p&gt;Learning the language of a processor can be a daunting task, but &lt;a href=&quot;https:&#x2F;&#x2F;nostarch.com&#x2F;writegreatcode1_2e&quot;&gt;understanding the machine&lt;&#x2F;a&gt; you&#x27;re working with makes it easier to write great code for it. Modern systems are so complex that directly writing assembly is laborious. However, modern open source tooling allows us to travel back in time 40 years and explore foundational software and hardware concepts in ways unimaginable during the lifetime of those systems.&lt;&#x2F;p&gt;
&lt;p&gt;One of the systems I plan to explore quite a bit in this blog series is the NES, my all time favorite retrocomputer. In this post I want to warm you up to some of the shenanigans to come and show you that tinkering with assembly on the NES may be &lt;em&gt;a lot&lt;&#x2F;em&gt; easier than you think!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;nintendopowerglove.jpg&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;simpler-times&quot;&gt;Simpler Times&lt;&#x2F;h1&gt;
&lt;p&gt;Compared to writing assembly for a desktop computer, the NES has no operating sytem, no &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Memory_management_unit&quot;&gt;MMU&lt;&#x2F;a&gt; and no &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Memory_protection&quot;&gt;memory protection&lt;&#x2F;a&gt;. These complex systems are valuable to understand but are also more difficult to appreciate without a context. The lack of these modern features means the code you write for a 6502-based machine runs in a very raw and simple environment that is very close to hardware.&lt;&#x2F;p&gt;
&lt;p&gt;A lot of what I&#x27;d like to share about why and how to get started with 6502 assembly development is already well covered in &lt;a href=&quot;https:&#x2F;&#x2F;skilldrick.github.io&#x2F;easy6502&#x2F;&quot;&gt;Easy 6502&lt;&#x2F;a&gt;, which offers a nice getting started tutorial and provides an in-browser assembler and simple debugger. The main aspect I want to focus on for this post are the &amp;quot;APIs&amp;quot; of the processor.&lt;&#x2F;p&gt;
&lt;p&gt;There &lt;a href=&quot;http:&#x2F;&#x2F;www.6502.org&#x2F;tutorials&#x2F;6502opcodes.html&quot;&gt;are only 56 operations&lt;&#x2F;a&gt; the 6502 can perform, most of which are trivial to understand. These are the building blocks used to make great stuff and are the internal API for your software.&lt;&#x2F;p&gt;
&lt;p&gt;Via the pins connecting the 6502 to whatever system it&#x27;s placed in, the CPU has a 16-bit address bus, an 8-bit data bus and a control bus that lets the processor interact with the outside world. This is the external interface that the CPU will use to interact with RAMs, ROMs and devices.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fpga-meets-6502&#x2F;W65C02S-40-pin-dip-color.gif&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;pointers-for-the-hardware&quot;&gt;Pointers for the Hardware&lt;&#x2F;h1&gt;
&lt;p&gt;Before exploring the internal API more, let&#x27;s look at the external side of things. Since the 6502 has a 16-bit address bus and an 8-bit data bus, it can read&#x2F;write 8 bits of data from&#x2F;to &lt;code&gt;2^16&lt;&#x2F;code&gt; different addresses. The CPU controls what address is being referred to on this address bus and uses the control bus to notify other connected devices if the data bus should be used for a read or a write operation.&lt;&#x2F;p&gt;
&lt;p&gt;To compare this to something like JavaScript, imagine an array of 65536 &lt;code&gt;[Object]&lt;&#x2F;code&gt;s, some which may support a &lt;code&gt;.read()&lt;&#x2F;code&gt; and&#x2F;or &lt;code&gt;.write()&lt;&#x2F;code&gt; operation. What type of device exists at each address varies greatly by system, though for 6502 you can comfortably rely on the lowest addresses being system RAM. For the NES, here&#x27;s how &lt;a href=&quot;https:&#x2F;&#x2F;www.nesdev.org&#x2F;wiki&#x2F;CPU_memory_map&quot;&gt;nesdev.org&lt;&#x2F;a&gt; describes the CPU memory map.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;nesdev-memmap.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Would you like to read the first byte of RAM? that&#x27;s effectively someting like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;first_byte &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;memory_map&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What about something that sounds more difficult, like reading the status of the &lt;a href=&quot;https:&#x2F;&#x2F;www.nesdev.org&#x2F;wiki&#x2F;PPU&quot;&gt;Picture Processing Unit&lt;&#x2F;a&gt;? Well that&#x27;s pretty much the same thing but instead of reading from &lt;code&gt;0&lt;&#x2F;code&gt; we&#x27;d want to read from &lt;a href=&quot;https:&#x2F;&#x2F;www.nesdev.org&#x2F;wiki&#x2F;PPU_registers#Status_($2002)_%3C_read&quot;&gt;&lt;code&gt;$2002&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ppu_status &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;memory_map&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x2002&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We could even use our old friend, the pointer!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PPU_STATUS &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0x2002&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pointer &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PPU_STATUS&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ppu_status &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;memory_map&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;pointer&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is really the main concept to understand for the hardware-facing side of the interface. There are interesting details in the implementation and the devices you would communicate to but hopefully this give a good functional base for the exploration of the internal API, coming up after the break.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;operating-the-6502&quot;&gt;Operating the 6502&lt;&#x2F;h1&gt;
&lt;p&gt;This section summarizes the controls available within the code you run on a 6502. Don&#x27;t worry if some or many of these terms sound entirely unfamiliar, they are concepts learned best by tinkering with in their own experimentation.&lt;&#x2F;p&gt;
&lt;p&gt;As I mentioned before there are only 56 Opcodes for the 6502. Many of these have similarities and can be grouped together. The other big element to be aware of is that the 6502 has 3 primary registers: the accumulator (&lt;code&gt;A&lt;&#x2F;code&gt;) and 2 index registers (&lt;code&gt;X&lt;&#x2F;code&gt; and &lt;code&gt;Y&lt;&#x2F;code&gt;). Internally, there is also a Program Counter &lt;code&gt;PC&lt;&#x2F;code&gt; that points to the memory address of the next location that should be fetched from and executed as code, as well as a Stack Pointer (&lt;code&gt;SP&lt;&#x2F;code&gt;) that is used for stack based operations. There are also a few processor status flags: &lt;code&gt;Carry&lt;&#x2F;code&gt;, &lt;code&gt;Zero&lt;&#x2F;code&gt;, &lt;code&gt;Interrupt&lt;&#x2F;code&gt;, &lt;code&gt;Decimal&lt;&#x2F;code&gt;, &lt;code&gt;Overflow&lt;&#x2F;code&gt; and &lt;code&gt;Negative&lt;&#x2F;code&gt; encoded into a Status register.&lt;&#x2F;p&gt;
&lt;p&gt;I would broadly categorize these instructions into 4 groups:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Load&#x2F;Store&#x2F;Transfer Instructions&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;LDA&lt;&#x2F;code&gt; - Load A Register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;LDX&lt;&#x2F;code&gt; - Load X Register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;LDY&lt;&#x2F;code&gt; - Load Y Register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;PHA&lt;&#x2F;code&gt; - Push Accumulator&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;PHP&lt;&#x2F;code&gt; - Push processor status&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;PLA&lt;&#x2F;code&gt; - Pull Accumulator&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;PLP&lt;&#x2F;code&gt; - Pull processor status&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;STA&lt;&#x2F;code&gt; - Store A Register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;STX&lt;&#x2F;code&gt; - Store X Register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;STY&lt;&#x2F;code&gt; - Store Y Register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TAX&lt;&#x2F;code&gt; - Transfer A to X&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TAY&lt;&#x2F;code&gt; - Transfer A to Y&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TSX&lt;&#x2F;code&gt; - Transfer stack pointer to X&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TXA&lt;&#x2F;code&gt; - Transfer X to A&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TXS&lt;&#x2F;code&gt; - Transfer X to stack pointer&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TYA&lt;&#x2F;code&gt; - Transfer Y to A&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Control Flow&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;BCC&lt;&#x2F;code&gt; - Branch &amp;quot;Carry&amp;quot; clear&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;BCS&lt;&#x2F;code&gt; - Branch &amp;quot;Carry&amp;quot; set&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;BEQ&lt;&#x2F;code&gt; - Branch equal&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;BMI&lt;&#x2F;code&gt; - Branch &amp;quot;minus&amp;quot;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;BNE&lt;&#x2F;code&gt; - Branch not equal&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;BPL&lt;&#x2F;code&gt; - Branch &amp;quot;plus&amp;quot;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;BRK&lt;&#x2F;code&gt; - Break&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;BVC&lt;&#x2F;code&gt; - Branch &amp;quot;Overflow&amp;quot; clear&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;BVS&lt;&#x2F;code&gt; - Branch &amp;quot;Overflow&amp;quot; set&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;CMP&lt;&#x2F;code&gt; - Compare A Register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;CPX&lt;&#x2F;code&gt; - Compare X Register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;CPY&lt;&#x2F;code&gt; - Compary Y Register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;JMP&lt;&#x2F;code&gt; - Jump&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;JSR&lt;&#x2F;code&gt; - Jump to subroutine&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;NOP&lt;&#x2F;code&gt; - No-operation&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;RTI&lt;&#x2F;code&gt; - Return from interrupt&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;RTS&lt;&#x2F;code&gt; - Return from subroutine&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Math&#x2F;Logic&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ADC&lt;&#x2F;code&gt; - Add with Carry&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;AND&lt;&#x2F;code&gt; - Bitwise AND&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;ASL&lt;&#x2F;code&gt; - Arithmetic shift left&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;DEC&lt;&#x2F;code&gt; - Decrement A register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;DEX&lt;&#x2F;code&gt; - Decrement X register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;DEY&lt;&#x2F;code&gt; - Decrement Y register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;EOR&lt;&#x2F;code&gt; - Exclusive OR&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;INC&lt;&#x2F;code&gt; - Increment A register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;INX&lt;&#x2F;code&gt; - Increment X register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;INY&lt;&#x2F;code&gt; - Increment Y register&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;LSR&lt;&#x2F;code&gt; - Logical shift right&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;ORA&lt;&#x2F;code&gt; - Bitwise OR&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;ROL&lt;&#x2F;code&gt; - Rotate Left&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;ROR&lt;&#x2F;code&gt; - Rotate Right&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;SBC&lt;&#x2F;code&gt; - Subtract with Carry&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Flags&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;BIT&lt;&#x2F;code&gt; - Test BITs&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;CLC&lt;&#x2F;code&gt; - Clear &amp;quot;Carry&amp;quot; Flag&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;CLD&lt;&#x2F;code&gt; - Clear &amp;quot;Decimal&amp;quot; Flag&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;CLI&lt;&#x2F;code&gt; - Clear &amp;quot;Interrupt&amp;quot; Flag&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;CLV&lt;&#x2F;code&gt; - Clear &amp;quot;Overflow&amp;quot; Flag&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;SEC&lt;&#x2F;code&gt; - Set &amp;quot;Carry&amp;quot; Flag&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;SED&lt;&#x2F;code&gt; - Set &amp;quot;Decimal&amp;quot; Flag&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;SEI&lt;&#x2F;code&gt; - Set &amp;quot;Instruction&amp;quot; Flag&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This is not a exactly a small amount of instructions, but there are 987 in x86_64 so by comparison it is quite tiny!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;the-learning-journey&quot;&gt;The Learning Journey&lt;&#x2F;h1&gt;
&lt;p&gt;Generally speaking I think it&#x27;s fair to say that understanding 6502 assembly is easy. What is much less easy is seeing and understanding how you can put these tools together to do something useful, like letting me jump on the head of a Koopa.&lt;&#x2F;p&gt;
&lt;p&gt;Generally speaking this is a situation that get&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Mad Computer Science – Part 4 – Schematics to Verilog and Back(ish)</title>
		<published>2023-01-16T00:00:00+00:00</published>
		<updated>2023-01-16T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/mad-computer-science-part-4/" type="text/html"/>
		<id>https://suchprogramming.com/mad-computer-science-part-4/</id>
		<content type="html">&lt;p&gt;I like tinkering with schematic based logic design, but I can easily find myself distracted away from the technical problem at hand by trying to make my schematic nice and neat. Using code instead of a schematic will let me abstract away this problem while also providing means of making my designs more scalable and easier to understand.&lt;&#x2F;p&gt;
&lt;p&gt;In this post I&#x27;ll export a schematic based design from Digital to run on a real FPGA and I&#x27;ll also look at how custom blocks in Verilog can be added into a schematic design in Digital.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;about-damn-time.jpg&quot; alt=&quot;About damn time&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;digital-goes-irl&quot;&gt;Digital Goes IRL&lt;&#x2F;h1&gt;
&lt;p&gt;So far in this series all of the circuit tinkery has been virtual, but the HDL exporting features in &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hneemann&#x2F;Digital&quot;&gt;Digital&lt;&#x2F;a&gt; will let us bring these designs to life.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll first come up with a rough plan. The &lt;a href=&quot;https:&#x2F;&#x2F;digilent.com&#x2F;reference&#x2F;programmable-logic&#x2F;arty-a7&#x2F;reference-manual&quot;&gt;Arty-A7 board&lt;&#x2F;a&gt; I will use has enough lights, buttons and switches for me to play around with, so that is not a big concern.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;arty-a7.png&quot; alt=&quot;Arty A7&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The schematic has one big issue though: the telnet block is &lt;em&gt;actually&lt;&#x2F;em&gt; powered my magic (or simulator software) so it can&#x27;t be exported to HDL. That&#x27;s not a dealbreaker for today&#x27;s goal though, I&#x27;ll just... simulate the emulated hardware with actual hardware, obviously!&lt;&#x2F;p&gt;
&lt;p&gt;Since the telnet block is going away, so does the communication protocol from before and so I&#x27;ll simplify the schematic a bit before getting deeper into this.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;irl-cleanup.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;That cleans up a good bit of clutter. The register now has floating inputs so I&#x27;ll need to route some new signals to it. This is also a good time to replace the switches and LEDs in the design as those physical devices are also not part of the HDL model of the digital world. Everything will get simplified down to inputs an outputs at the top level.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;irl-inputify.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m not sure if &lt;code&gt;Clk&lt;&#x2F;code&gt; needs to be changed, I guess we&#x27;ll find out when I try to export it!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;irl-export.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So &lt;code&gt;Clk&lt;&#x2F;code&gt; wasn&#x27;t a problem, but all of my other pins being unlabeled was. After slapping some names on them, Digital is ready to export it to Verilog for me.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s see what it generated...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;systemverilog&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-systemverilog &quot;&gt;&lt;code class=&quot;language-systemverilog&quot; data-lang=&quot;systemverilog&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; * Generated by Digital. Don&amp;#39;t modify this file!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; * Any changes will be lost if this file is regenerated.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt; *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;DIG_Register_BUS #(
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;    parameter&lt;&#x2F;span&gt;&lt;span&gt; Bits = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    input en,
&lt;&#x2F;span&gt;&lt;span&gt;    input [(Bits - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;D&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    output [(Bits - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Q
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;    reg&lt;&#x2F;span&gt;&lt;span&gt; [(Bits - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] state = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;h0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;assign &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Q &lt;&#x2F;span&gt;&lt;span&gt;= state;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;always &lt;&#x2F;span&gt;&lt;span&gt;@ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;posedge &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (en)
&lt;&#x2F;span&gt;&lt;span&gt;            state &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;D&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;endmodule
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;top (
&lt;&#x2F;span&gt;&lt;span&gt;  input Clk,
&lt;&#x2F;span&gt;&lt;span&gt;  input r1,
&lt;&#x2F;span&gt;&lt;span&gt;  input r2,
&lt;&#x2F;span&gt;&lt;span&gt;  input t,
&lt;&#x2F;span&gt;&lt;span&gt;  input s1,
&lt;&#x2F;span&gt;&lt;span&gt;  input s2,
&lt;&#x2F;span&gt;&lt;span&gt;  output l1,
&lt;&#x2F;span&gt;&lt;span&gt;  output l2
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] s0;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;wire&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] s3;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;assign&lt;&#x2F;span&gt;&lt;span&gt; s0[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] = r1;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;assign&lt;&#x2F;span&gt;&lt;span&gt; s0[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;] = r2;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;DIG_Register_BUS &lt;&#x2F;span&gt;&lt;span&gt;#(
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;Bits&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  )
&lt;&#x2F;span&gt;&lt;span&gt;  DIG_Register_BUS_i0 (
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;D&lt;&#x2F;span&gt;&lt;span&gt;( s0 ),
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;C&lt;&#x2F;span&gt;&lt;span&gt;( Clk ),
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;en&lt;&#x2F;span&gt;&lt;span&gt;( t ),
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;Q&lt;&#x2F;span&gt;&lt;span&gt;( s3 )
&lt;&#x2F;span&gt;&lt;span&gt;  );
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;assign&lt;&#x2F;span&gt;&lt;span&gt; l1 = (s1 ^ s3[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;assign&lt;&#x2F;span&gt;&lt;span&gt; l2 = (s2 ^ s3[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;endmodule
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It&#x27;s interesting to see what kind of HDL it generated for me. There&#x27;s a module digital made to represent it&#x27;s version of a register and the &lt;code&gt;top&lt;&#x2F;code&gt; module which is my design. I named the file &lt;code&gt;top.v&lt;&#x2F;code&gt; so that the module would be named &lt;code&gt;top&lt;&#x2F;code&gt; and that is conventionally the name for the top level module in an FPGA project.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;some-fpga-wizardry&quot;&gt;Some FPGA Wizardry&lt;&#x2F;h1&gt;
&lt;p&gt;One of my side projects, which will hopefully be receiving some more love in the near future, is a tool to aide in this kind of digital tinkery called &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;KennethWilke&#x2F;wiz&quot;&gt;Wiz&lt;&#x2F;a&gt;. My aim for this tool is to make playing with FPGAs a lot easier and more fun, especially for curious software developers.&lt;&#x2F;p&gt;
&lt;p&gt;As of this writing the tool is very young and in rough shape, but it&#x27;s made for this exact type of exploratory usage so here we go!&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll start by creating a new folder, and within that folder I&#x27;ll make a &lt;code&gt;wiz.toml&lt;&#x2F;code&gt; file, this is a manifest file used by Wiz that tells it how I want to build and program my logic design project.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;package_name &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;irl&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;builder &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;vivado&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;programmer &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;vivado&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target_part &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;xc7a35ticsg324-1L&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target_device &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;xc7a35t_0&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;source_files &lt;&#x2F;span&gt;&lt;span&gt;= [&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;top.v&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;bitstream_path &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;irl.bit&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[pins]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This file format is very likely to change, but the important parts are that &lt;code&gt;vivado&lt;&#x2F;code&gt; is what I&#x27;ll be using for building and programming the device and it also specifies some details about my target Artix-A35T FPGA part&#x2F;device.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll also need to add more detail to the &lt;code&gt;[pins]&lt;&#x2F;code&gt; section to map out the FPGA pins to the inputs and outputs of my &lt;code&gt;top&lt;&#x2F;code&gt; module. This is mostly going to be a matter of referring to the &lt;a href=&quot;https:&#x2F;&#x2F;digilent.com&#x2F;reference&#x2F;programmable-logic&#x2F;arty-a7&#x2F;reference-manual&quot;&gt;board reference material&lt;&#x2F;a&gt; and selecting from that. &lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;digilent.com&#x2F;reference&#x2F;_media&#x2F;arty&#x2F;arty_gpio.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In this case, I&#x27;ll use the Arty&#x27;s &lt;code&gt;LD7&lt;&#x2F;code&gt; and &lt;code&gt;LD6&lt;&#x2F;code&gt; LEDs as LED1 (&lt;code&gt;l1&lt;&#x2F;code&gt;) and LED2 (&lt;code&gt;l2&lt;&#x2F;code&gt;), which are mapped to pins &lt;code&gt;T10&lt;&#x2F;code&gt; and &lt;code&gt;T9&lt;&#x2F;code&gt; respectively. For the physical switch inputs (&lt;code&gt;s1&lt;&#x2F;code&gt; and &lt;code&gt;s2&lt;&#x2F;code&gt;), I&#x27;ll use the left two dip switches (&lt;code&gt;SW3&lt;&#x2F;code&gt; and &lt;code&gt;SW2&lt;&#x2F;code&gt;) and for the register inputs (&lt;code&gt;r1&lt;&#x2F;code&gt; and &lt;code&gt;r2&lt;&#x2F;code&gt;) I&#x27;ll use the right two (&lt;code&gt;SW1&lt;&#x2F;code&gt; and &lt;code&gt;SW0&lt;&#x2F;code&gt;). For &lt;code&gt;t&lt;&#x2F;code&gt;, my trigger input to save the new register state, I&#x27;ll use the &lt;code&gt;BTN0&lt;&#x2F;code&gt; button. That just leaves &lt;code&gt;Clk&lt;&#x2F;code&gt;, which I&#x27;ll hook up to the 100mhz clock on FPGA pin &lt;code&gt;E3&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span&gt;[pins]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Clk &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;E3&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r1 &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;C11&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;r2 &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A8&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;D9&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s1 &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;A10&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;s2 &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;C10&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l1 &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;T10&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;l2 &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;T9&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Picking and mapping the pins can be a little tedious, but professional FPGA tools often make it much more work than this!&lt;&#x2F;p&gt;
&lt;p&gt;Next I&#x27;ll build the project with &lt;code&gt;wiz build&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;irl-build.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It takes a good bit to get through all its steps, but at the end I have my bitfile ready to flash. I&#x27;ll plug in my Arty board and load it up with &lt;code&gt;wiz program&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;irl-program.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Huzzah, my design is now implemented and working in real hardware!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;irl-arty.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The design itself isn&#x27;t very interesting, but I&#x27;m pretty happy with the flow my Wiz tool is starting to enable.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;returning-to-schematics&quot;&gt;Returning to Schematics&lt;&#x2F;h1&gt;
&lt;p&gt;Now what I&#x27;ll do is a little bit of the opposite direction, just to prove that these tools can be mixed and matched. I&#x27;ll make a new schematic and include a new Verilog block that includes the design. This is the exported Verilog from earlier, but this time imported into Digital as a module.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;irl-back-to-digital.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Everything needs to match up, including the one &lt;code&gt;Clk&lt;&#x2F;code&gt; signal I capitalized for no reason and the &lt;code&gt;top&lt;&#x2F;code&gt; module name for the block. I now have a pretty decent grip on a flow for how to mix and match HDL and schematics for logic design. That&#x27;ll be all for this post!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Mad Computer Science – Part 3 – A Virtual Light Controller</title>
		<published>2023-01-03T00:00:00+00:00</published>
		<updated>2023-01-03T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/mad-computer-science-part-3/" type="text/html"/>
		<id>https://suchprogramming.com/mad-computer-science-part-3/</id>
		<content type="html">&lt;p&gt;The little Telnet block is humorous, but to make it much more interesting I want to wrap more useful functionality around it over time. So far I&#x27;ve only used it for sending data to the simulated design, but now I want to support sending data back as well.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;a-virtual-device-project&quot;&gt;A Virtual Device Project&lt;&#x2F;h1&gt;
&lt;p&gt;I want to make a little simulated device to prove out the basic concepts of interacting between simulated hardware and running software. The device will be a little LED controller, with 2 lights and 2 switches; toggling the switches will toggle if the associated LED is on or off.&lt;&#x2F;p&gt;
&lt;p&gt;Via the socket interface, software can read the status of the lights and switches and can also toggle the lights. The software can&#x27;t toggle the switches, since those are &amp;quot;physical&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll start off my modeling the basic hardware bits in &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hneemann&#x2F;Digital&quot;&gt;Digital&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;basic-togglies.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Before going too much further, I want to consider the communcation model and where this device is placed when integrated into a complete design. This device will be driven by a remote connection, so it&#x27;ll act kind of a like server in a client-server model as far as the communication protocol is concerned.&lt;&#x2F;p&gt;
&lt;p&gt;The Telnet block in Digital reads and writes a byte at a time, but I&#x27;ll only need a few bits for this protocol. For read requests I&#x27;ll just send a &lt;code&gt;0&lt;&#x2F;code&gt;, and encode the switch and light states into 4 bits of the reply. For writes, I&#x27;ll have 1 bit to indicate the write operation and the 2 other bits will be settings for the LEDs.&lt;&#x2F;p&gt;
&lt;p&gt;Now to the building!&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;initial-bits-and-bobs&quot;&gt;Initial Bits and Bobs&lt;&#x2F;h1&gt;
&lt;p&gt;The first change I&#x27;ll make is to add some &lt;code&gt;XOR&lt;&#x2F;code&gt; gates for the LEDs, and a second set of switches to use for some early testing.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-xorify.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With that in place, the bottom two switches can effecively override the top two switches. What I &lt;em&gt;really&lt;&#x2F;em&gt; want here is a 2-bit register to store these values, so I&#x27;ll wire that up instead.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-regify.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;One more easy thing to add will be a status bus for reading the states of the switches and LEDs. I&#x27;ll just merge the relevant wires onto a bus for that. I ultimately know this will be going to an 8-bit input to the Telnet block so I&#x27;ll size it as such from the start.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-readbus.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Lastly, I&#x27;ll slap my Telnet block into the design and loosely wire it. That&#x27;ll be the next area of focus.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-addtelnet.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;That takes care of the easy bits.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;now-the-tricky-part&quot;&gt;Now the Tricky Part&lt;&#x2F;h1&gt;
&lt;p&gt;How I would wire up the rest of this design was not obvious to me. I thought I&#x27;d first try an approach that felt like it would simple if it worked... though I had strong doubts that it would work at all. I learn the most by breaking stuff so we&#x27;ll call this an experiment!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-notquiteright.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It starts off looking well enough, until the hardware version of undefined behavior (high impedance) says otherwise. The problem is that the &lt;code&gt;D&lt;&#x2F;code&gt; and &lt;code&gt;en&lt;&#x2F;code&gt; inputs to the register are not being driven high or low, though on each clock cycle the register still does its best to use those garbage inputs.&lt;&#x2F;p&gt;
&lt;p&gt;The first little change I&#x27;ll make to try to fix this is to use an &lt;code&gt;AND&lt;&#x2F;code&gt; gate for the register&#x27;s &lt;code&gt;en&lt;&#x2F;code&gt; input. This will trigger the storage of the two register bits when &lt;code&gt;out[0]&lt;&#x2F;code&gt; and &lt;code&gt;av&lt;&#x2F;code&gt; from the telnet block are &lt;code&gt;1&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-andify-en.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I believe this circuit is going to have some timing issues because the register and telnet blocks are both triggering at the same rising edge of &lt;code&gt;Clk&lt;&#x2F;code&gt;. I&#x27;ll test this out to see what happens with a small Python script.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#!&#x2F;usr&#x2F;bin&#x2F;env python3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;socket &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;create_connection
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;time
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS &lt;&#x2F;span&gt;&lt;span&gt;= (&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;127.0.0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1337&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;socket = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;create_connection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x01&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;time.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x03&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;time.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x05&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;time.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x07&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will send 4 operations that save to the 2 bit register. The binary form of these operations is something like &lt;code&gt;?????ABW&lt;&#x2F;code&gt; where &lt;code&gt;W&lt;&#x2F;code&gt; is the bit that indicates a set operation, &lt;code&gt;A&lt;&#x2F;code&gt; and &lt;code&gt;B&lt;&#x2F;code&gt; are the values to save and the &lt;code&gt;?&lt;&#x2F;code&gt; bits are ignored. This little script cycles through all 4 states.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-mightwork.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I was a little surprised this looked stable. My thought was that on the rising edge of &lt;code&gt;Clk&lt;&#x2F;code&gt; the telnet block would check its internal status and then set &lt;code&gt;av&lt;&#x2F;code&gt; if incoming data was available. If that was the case, what would the &lt;code&gt;rd&lt;&#x2F;code&gt; input be when &lt;code&gt;Clk&lt;&#x2F;code&gt; rises? Not knowing why something doesn&#x27;t work can be a pain, but not knowing why it &lt;em&gt;does&lt;&#x2F;em&gt; work is a little more frustrating!&lt;&#x2F;p&gt;
&lt;p&gt;Then I noticed the &lt;code&gt;av&lt;&#x2F;code&gt; output may actually be asynchronous, which would explain the stability. I thought I&#x27;d slow that down and inspect it a bit more.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-async-av.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Ah! That confirms it. For the sake of science though... what if I made it synchronous? I&#x27;ll add an &lt;code&gt;AND&lt;&#x2F;code&gt; gate to test and see!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-very-broken.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Oh my, that is entirely broken. Good to know! &lt;&#x2F;p&gt;
&lt;p&gt;On the rising edge of &lt;code&gt;Clk&lt;&#x2F;code&gt;, the telnet block and the &lt;code&gt;AND&lt;&#x2F;code&gt; gate below it recieve this transition at approximately the same time. The &lt;code&gt;AND&lt;&#x2F;code&gt; gate gets it&#x27;s true condition and shifts its output to a &lt;code&gt;1&lt;&#x2F;code&gt;, but the telnet block also reads &lt;code&gt;rd&lt;&#x2F;code&gt; as the clock transition happened which is probably before the &lt;code&gt;AND&lt;&#x2F;code&gt; gate had time to update its output. The tiny delay is just enough guarantee the telnet block is never read from and breaks everything.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll remove that &lt;code&gt;AND&lt;&#x2F;code&gt; gate, it was definitely better before that.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;works-on-my-box&quot;&gt;Works On My Box&lt;&#x2F;h1&gt;
&lt;p&gt;This is probably something worth fixing, or at least investigating more, for a real project. In this case I am not feeling too worried about it. If &lt;code&gt;av&lt;&#x2F;code&gt; goes high before or after &lt;code&gt;Clk&lt;&#x2F;code&gt; there will be no issues. When I manually caused a timing issue this prevented a read, but in a design where &lt;code&gt;av&lt;&#x2F;code&gt; is allowed to stay synchronous that would simply add a minor delay.&lt;&#x2F;p&gt;
&lt;p&gt;At the end of the day, I&#x27;m using the telnet block so this design can only really ever run in Digital. As a single threaded software application, I&#x27;d bet the behavior is certainly deterministic enough for my silly needs.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m on to finishing up some final bits of this project to handle read commands as well. I&#x27;ll just need to trigger the &lt;code&gt;wr&lt;&#x2F;code&gt; input when &lt;code&gt;av&lt;&#x2F;code&gt; is set and &lt;code&gt;out[0]&lt;&#x2F;code&gt; is not.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-readify.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I think that may do it, but I&#x27;ll need a bit more Python for testing.&lt;&#x2F;p&gt;
&lt;p&gt;In this case, I&#x27;m going to make a little script that I&#x27;ll include into my terminal to use as a cheap terminal interface and use via &lt;code&gt;ipython&lt;&#x2F;code&gt;. It&#x27;ll just define a few lazily written functions to perform the two types of interactions.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#!&#x2F;usr&#x2F;bin&#x2F;env python
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;socket &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;create_connection
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;time
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS &lt;&#x2F;span&gt;&lt;span&gt;= (&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;127.0.0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1337&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;socket = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;create_connection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;set_lights&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;light1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;light2&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;light1:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;light2:
&lt;&#x2F;span&gt;&lt;span&gt;            socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x07&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x03&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;light2:
&lt;&#x2F;span&gt;&lt;span&gt;            socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x05&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x01&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;read_status&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x00&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    x = socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;recv&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{:04b}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;format&lt;&#x2F;span&gt;&lt;span&gt;(x))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now for the fun part, does it work!?&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;lc-worky-work.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Eh... almost! There were some crossed wires.&lt;&#x2F;p&gt;
&lt;p&gt;These were quick enough to fix, I apparently tied the same LED to two bits of my read bus and the bit I thought was my second switch was actually the output of my first register bit. Once I spotted those though, everything was pretty simple to fix and worked like a charm!&lt;&#x2F;p&gt;
&lt;p&gt;There are certainly a variety of ways to improve this design, but I&#x27;m pretty content with where it&#x27;s at for this post.&lt;&#x2F;p&gt;
&lt;p&gt;See you in the next one!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Mad Computer Science – Part 2 – 7-Segment Driver</title>
		<published>2022-10-28T00:00:00+00:00</published>
		<updated>2022-10-28T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/mad-computer-science-part-2/" type="text/html"/>
		<id>https://suchprogramming.com/mad-computer-science-part-2/</id>
		<content type="html">&lt;p&gt;In the previous post, I implemented a single decimal digit display that used &amp;quot;hardware&amp;quot; pretty minimally. The digital logic side of the design simply split each bit of a byte to allow a software controlled means of controlling each segment of the display.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;telnet-seggy.png&quot; alt=&quot;Telnet Seggy&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In this post I&#x27;ll expand on this design and explore a few more core concepts in hardware tinkery.&lt;&#x2F;p&gt;
&lt;p&gt;The primary goal for this post is to use the 7-segment displays to view the hexademical values for the binary data we&#x27;re delivering through the Telnet block. When I send an &lt;a href=&quot;https:&#x2F;&#x2F;upload.wikimedia.org&#x2F;wikipedia&#x2F;commons&#x2F;1&#x2F;1b&#x2F;ASCII-Table-wide.svg&quot;&gt;ASCII&lt;&#x2F;a&gt; &lt;code&gt;K&lt;&#x2F;code&gt; to the design, I want to see the &lt;code&gt;4B&lt;&#x2F;code&gt; displayed in the simulated displays.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h2 id=&quot;what-the-hex&quot;&gt;What the Hex&lt;&#x2F;h2&gt;
&lt;p&gt;In the last post I had a table for which segments should be lit for &lt;code&gt;0-9&lt;&#x2F;code&gt;. I&#x27;ll need &lt;code&gt;A-F&lt;&#x2F;code&gt; as well, so I&#x27;ll add them to that table.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;center&quot;&gt;Value &lt;&#x2F;th&gt;&lt;th align=&quot;center&quot;&gt; Segments &lt;&#x2F;th&gt;&lt;th align=&quot;center&quot;&gt; Hexadecimal&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;--543210&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;3F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-----21-&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;06&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;2&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6-432-0&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;5D&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;3&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6--3210&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;4F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;4&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-65--21-&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;66&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;5&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-65-3-10&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;6B&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;6&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6543-10&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;7B&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;7&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;----321-&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0E&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;8&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6543210&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;7F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;9&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-65-3210&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;6F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;A&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-654321-&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;7E&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;B&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-654--10&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;73&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;C&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;--543--0&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;39&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;D&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6-4-210&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;57&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;E&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6543--0&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;79&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6543---&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;78&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;My plan is to make a couple small components to organize my implementation for this. I&#x27;ll start with a single 7-segment driver that takes a 4-bit (nibble) input and drives 1 segment. Once I have that, I&#x27;ll combine two of them into 1 block that can drive 2 segments.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll start by planning out my driver design, first dropping a display in the schematic and making an input bus.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;segment-addr-bus.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So now I need to light up the right segments based on the values of 4 inputs. I thought of a few different ways for how this could be done.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;logic-gates-logically&quot;&gt;Logic Gates, Logically&lt;&#x2F;h2&gt;
&lt;p&gt;I decided to see what it&#x27;d take to light up segment 6 for the right inputs. I started by looking at which numbers should and should not be lit for this segment.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;bit-numbery.png&quot; alt=&quot;Segmented bits&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Should be lit:&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;center&quot;&gt;Hexadecimal&lt;&#x2F;th&gt;&lt;th align=&quot;center&quot;&gt;  Binary&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;2&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0010&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;3&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0011&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;4&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0100&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;5&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0101&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;6&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0110&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;8&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1000&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;9&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1001&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;A&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1010&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;B&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1011&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;D&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1101&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;E&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1110&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1111&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Should not be lit&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;center&quot;&gt;Hexadecimal&lt;&#x2F;th&gt;&lt;th align=&quot;center&quot;&gt;  Binary&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0000&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0001&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;7&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0111&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;C&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1100&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;I don&#x27;t see any obvious simple pattern for implementing this with logic gates. Since the display is lit 3&#x2F;4 of the time, my quick implemention idea is to use 4-input &lt;code&gt;AND&lt;&#x2F;code&gt; gates with some inverted inputs to match each pattern that should not be lit, and input all of those into a 4-input &lt;code&gt;NOR&lt;&#x2F;code&gt; gate.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll roughly layout the logic gates and route &lt;code&gt;0&lt;&#x2F;code&gt; to the remaining segments for now.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-and-wiring.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Each gate will match a condition where the segment shouldn&#x27;t be lit. For when a bit value I&#x27;m looking for is &lt;code&gt;0&lt;&#x2F;code&gt;, I&#x27;ll flip that invert on its way into the gate. I&#x27;ll struggle a little bit to wire things up properly then give it a little bit of manual testing.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;logic-6.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;proper-engineering&quot;&gt;&amp;quot;Proper&amp;quot; engineering&lt;&#x2F;h2&gt;
&lt;p&gt;While this solution only has 5 gates in my schematic, 4-input gates are basically each made of 3 gates. So this design would take at least 15 gates, before including the inverted bits... which is pretty inefficient. There are better, boolean-mathical ways of addressing this, and the simulator has a bit of tooling to assist in avoiding the maths.&lt;&#x2F;p&gt;
&lt;p&gt;If I add and name an output, and name the inputs, I can ask the analysis tool to do it&#x27;s thing. It&#x27;ll generate a truth table for me. At the bottom of that table I can find a simplified function that&#x27;ll generate the same output. I can also bring up the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Karnaugh_map&quot;&gt;Karnaugh map&lt;&#x2F;a&gt;, though I don&#x27;t find it particularly helpful here.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-analyze.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s still... not exactly small is it? &lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;seggy-6-optimized.png&quot; alt=&quot;Seggy Optimized&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m not particularly interesting in doing that 6 more times, so I&#x27;m going to change my approach.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h2 id=&quot;cheat-codes&quot;&gt;Cheat Codes&lt;&#x2F;h2&gt;
&lt;p&gt;Instead of going through all that, I&#x27;m going to use a LUT (Look-Up Table) to design this a bit more like the original software-oriented route. I&#x27;ll toss all of this implementation out the door and wire a new LUT block into the design.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-redo.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Next, I&#x27;ll load up a file that contains the hex values of the digits.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;v2.0 raw
&lt;&#x2F;span&gt;&lt;span&gt;3f
&lt;&#x2F;span&gt;&lt;span&gt;06
&lt;&#x2F;span&gt;&lt;span&gt;5d
&lt;&#x2F;span&gt;&lt;span&gt;4f
&lt;&#x2F;span&gt;&lt;span&gt;66
&lt;&#x2F;span&gt;&lt;span&gt;6b
&lt;&#x2F;span&gt;&lt;span&gt;7b
&lt;&#x2F;span&gt;&lt;span&gt;0e
&lt;&#x2F;span&gt;&lt;span&gt;7f
&lt;&#x2F;span&gt;&lt;span&gt;6f
&lt;&#x2F;span&gt;&lt;span&gt;7e
&lt;&#x2F;span&gt;&lt;span&gt;73
&lt;&#x2F;span&gt;&lt;span&gt;39
&lt;&#x2F;span&gt;&lt;span&gt;57
&lt;&#x2F;span&gt;&lt;span&gt;79
&lt;&#x2F;span&gt;&lt;span&gt;78
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-loadtable.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Well! That was a heck of a lot quicker and easier. I&#x27;ll just tweak the inputs and outputs so that I can save this design and use it elsewhere.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-display.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Then I&#x27;ll quickly make a dual-display variant, that simply takes in an 8-bit bus and outputs two segment busses. If I open straight to a new file, the simulator doesn&#x27;t pick up my other files in the &lt;code&gt;Components&lt;&#x2F;code&gt; menu. If I choose a &lt;code&gt;New embedded Circuit&lt;&#x2F;code&gt;, it will find those files.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-dual.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Finally, I&#x27;ll go back into my Telnet schematic and wire up everything to use my new block. To keep things looking relatively tidy, I&#x27;ll pre-wire the segments to space things out then collapse those individual signals into 4-bit busses. I&#x27;ll then split the 8-bit segments out into top and bottom busses. I used the horizontal offset of the &lt;code&gt;8:4*2&lt;&#x2F;code&gt; splitters to help me visually remember that S1 is on the left and S0 on the right.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-buildemup.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;that-darn-impedence&quot;&gt;That Darn Impedence&lt;&#x2F;h2&gt;
&lt;p&gt;With everything wired up, things do work, but there&#x27;s a little bit of undesired behavior.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-ub.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Before and after I send my sequence through the Telnet block, the displays will show a random value due to the inputs that aren&#x27;t being driven to a valid state for reading.&lt;&#x2F;p&gt;
&lt;p&gt;I need somewhere to latch onto just the valid inputs at just the right times, which is exactly what flip-flops are here for!&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Latches and flip-flops are circuits for storing bits of information. &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;playlist?list=PLTd6ceoshpreKyY55hA4vpzAUv9hSut1H&quot;&gt;This playlist on YouTube&lt;&#x2F;a&gt; covers the standard variants&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;I generally prefer the D-type flip-flops, as they are simple and good for registers. I just feed my data into &lt;code&gt;D&lt;&#x2F;code&gt;, and when &lt;code&gt;C&lt;&#x2F;code&gt; transitions from &lt;code&gt;0&lt;&#x2F;code&gt; to &lt;code&gt;1&lt;&#x2F;code&gt; the value will be stored and output on &lt;code&gt;Q&lt;&#x2F;code&gt; until the next rising clock (the opposite values will be output on the inverted &lt;code&gt;Q&lt;&#x2F;code&gt;, unused here)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-flippy-flop.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I also added the &lt;code&gt;AND&lt;&#x2F;code&gt; gate here so that the flip-flop will only latch to the value when the &lt;code&gt;av&lt;&#x2F;code&gt; and clock signals are high. This still leaves me an unexpected result at the end of my sequence.&lt;&#x2F;p&gt;
&lt;p&gt;The problem here is that both the Telnet block and the flip-flop latch when the clock transitions to &lt;code&gt;1&lt;&#x2F;code&gt;. Does the Telnet block update fast enough to allow this? Probably not.&lt;&#x2F;p&gt;
&lt;p&gt;Though, if I invert the clock the latching will happen one half-cycle after the Telnet block has updated its value. Giving the Telnet block a little time to change its output before storing it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;seggy-clockinvert.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There we have it, a decently working hexadecimal display! With the goal complete I&#x27;ll call it a wrap for this post.&lt;&#x2F;p&gt;
&lt;p&gt;I hope you enjoyed this post, stay tuned for the next one!&lt;&#x2F;p&gt;
&lt;h6 id=&quot;bonus-material&quot;&gt;Bonus material&lt;&#x2F;h6&gt;
&lt;p&gt;While you wait for the next post, here are a couple good videos to check out:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=vxSExMj9lV8&quot;&gt;Combination Circuit Vs Sequential Circuit&lt;&#x2F;a&gt; by &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;c&#x2F;ChinmayaASV&quot;&gt;Chinmaya A.S.V&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=7zffjsXqATg&quot;&gt;Designing a 7-segment hex decoder&lt;&#x2F;a&gt; by &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;c&#x2F;BenEater&quot;&gt;Ben Eater&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=VkVe_wNU6RI&quot;&gt;5.2.4 D Register Timing&lt;&#x2F;a&gt; by &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;c&#x2F;mitocw&quot;&gt;MIT OpenCourseWare&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Mad Computer Science – Part 1 – Softening up Hardware</title>
		<published>2022-08-16T00:00:00+00:00</published>
		<updated>2022-08-16T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/mad-computer-science-part-1/" type="text/html"/>
		<id>https://suchprogramming.com/mad-computer-science-part-1/</id>
		<content type="html">&lt;p&gt;Programmable logic is my favorite concept in computing, integrated circuits that let you define hardware behavior. This is an excessively powerful concept that is just waiting for the open source community to do amazing things with.&lt;&#x2F;p&gt;
&lt;p&gt;For many hobby electronics projects, you can use a software-focused approach and use an Arduino or Raspberry Pi to accomplish a wide variety of tasks. I&#x27;ve enjoyed that approach but, I want to go a bit more off road than mere software allows. Programmable logic lets me, someone who should not [yet] be paid to design custom hardware, do it anyway!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;we-dont-need-roads.gif&quot; alt=&quot;Where we&#x27;re goin&#x27;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;d like to share a path that I think makes it a lot easier and more fun to get into. It&#x27;ll start with tinkering in a virtualized hardware environment that lets you explore a bit more in the comfort of your own programming language.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h2 id=&quot;virtual-tinkering&quot;&gt;Virtual Tinkering&lt;&#x2F;h2&gt;
&lt;p&gt;I want to bring more software programmers into the world of tinkering with programmable logic, but jumping head first into HDL is kinda hard and it helps to have a lot more context. I found it was really helpful to play with some of schematic based logic design tools first, but what I could accomplish with them felt pretty quickly constrained.&lt;&#x2F;p&gt;
&lt;p&gt;I recently came across a digital logic designer and simulator tool called &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hneemann&#x2F;Digital&quot;&gt;Digital&lt;&#x2F;a&gt;, and it is pretty awesome. It&#x27;s an open source, Java-based program that runs pretty simply on Linux, MacOS and Windows.&lt;&#x2F;p&gt;
&lt;p&gt;This tool has all the features I want in a simulator, plus a few extras that surprised and excited me. It has the core stuff like logic gates, buttons and LEDs, and a few nice higher level components like flip-flops, multiplexers and 7-segment displays and even an LED matrix! A few components I&#x27;d never seen in a free simulator before, like blocks for keyboard input, VGA, MIDI and blocks for VHDL and Verilog.&lt;&#x2F;p&gt;
&lt;p&gt;A couple blocks feel particularly ready for some mad computer science...&lt;&#x2F;p&gt;
&lt;h2 id=&quot;setting-the-stage&quot;&gt;Setting the Stage&lt;&#x2F;h2&gt;
&lt;p&gt;Digital is super easy to obtain and run, &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;hneemann&#x2F;Digital#download-and-installation&quot;&gt;you can check here to see just how easy&lt;&#x2F;a&gt;. When you first launch the program it&#x27;ll offer a short tutorial that helps quickly acclimate to placing components in the schematic, wiring them together and simulating the design.&lt;&#x2F;p&gt;
&lt;p&gt;Start your new schematic and go to Components -&amp;gt; IO -&amp;gt; Peripherals and add a Telnet component.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;telnet-block.png&quot; alt=&quot;Did I say Telnet?&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I chuckled when I saw the name Telnet, but I am definitely going to use this component because it sounds awesome. How does it work? I&#x27;ll just hit play&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;error-in-input.png&quot; alt=&quot;Error number 1&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Alright, it&#x27;s mad at me for not feeding it. It needs some inputs so I&#x27;ll wire a button into all the inputs.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;button-up.png&quot; alt=&quot;Button up&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This will definitely work&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;error-in-input2.png&quot; alt=&quot;Error number 2&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Okay fine, maybe some docs would be useful. If I right click on this component I get the following prompt.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;telnet-menu1.png&quot; alt=&quot;Telnet menu defaults&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m going to change a few settings while I&#x27;m here, like turning &lt;code&gt;Telnet Mode&lt;&#x2F;code&gt; off so that it&#x27;s a simple TCP server and I&#x27;ll pick &lt;code&gt;1337&lt;&#x2F;code&gt; since that&#x27;s a not a &lt;a href=&quot;https:&#x2F;&#x2F;www.w3.org&#x2F;Daemon&#x2F;User&#x2F;Installation&#x2F;PrivilegedPorts.html&quot;&gt;privileged port&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;telnet-menu2.png&quot; alt=&quot;My settings&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;And look a handy help button!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;telnet-help.png&quot; alt=&quot;Telnet help&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So based on what I&#x27;m seeing here, I&#x27;ll wire up my inputs a bit more sanely. I don&#x27;t plan on writing data out yet, so I&#x27;ll set &lt;code&gt;in&lt;&#x2F;code&gt; and &lt;code&gt;wr&lt;&#x2F;code&gt; to 0, feed the output of a clock to &lt;code&gt;C&lt;&#x2F;code&gt;, and a basic input for &lt;code&gt;rd&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;wire-up-telnet.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Next, I&#x27;ll wire up a Terminal block as the output. I&#x27;ll add a new button to &lt;code&gt;en&lt;&#x2F;code&gt;able the terminal, route the &lt;code&gt;out&lt;&#x2F;code&gt; from the Telnet block to the &lt;code&gt;D&lt;&#x2F;code&gt;ata input, and plumb the same clock signal into &lt;code&gt;C&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;wire-out-telnet.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I think we&#x27;re looking about ready to go!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;hello-digital-world&quot;&gt;Hello Digital World&lt;&#x2F;h2&gt;
&lt;p&gt;Now for the fun part, when I simulate this design I&#x27;ll be able to connect to the Telnet block on &lt;code&gt;127.0.0.1:1337&lt;&#x2F;code&gt; and data I pass into that socket connection should emit from the block. I like that this lets me hook into my design, relatively simply, with any language of my choice.&lt;&#x2F;p&gt;
&lt;p&gt;My go-to language for quick hackyness is Python, so I&#x27;ll write a little script to send some data:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#!&#x2F;usr&#x2F;bin&#x2F;env python
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;socket &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;create_connection
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS &lt;&#x2F;span&gt;&lt;span&gt;= (&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;127.0.0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1337&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;socket = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;create_connection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, Digital World!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There&#x27;s a slight problem before running this though. Before I even send input, activating the terminal will spit out some junk data. I&#x27;ll enable my clock and show you want I mean:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;terminal-garbage.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If you notice, before I let the terminal body-block the schematic, the data in the line is &lt;code&gt;Z&lt;&#x2F;code&gt;. This state is called &amp;quot;high impedance&amp;quot;, which basically means nothing is driving this signal and is also referred to as &amp;quot;floating&amp;quot;. Effectively this means we&#x27;re going to be getting some undefined behavior!&lt;&#x2F;p&gt;
&lt;p&gt;To address this, and make things a little bit more automatic, I&#x27;m going to route the &lt;code&gt;av&lt;&#x2F;code&gt;ailable signal to both the &lt;code&gt;en&lt;&#x2F;code&gt;able input for the Terminal block and the &lt;code&gt;rd&lt;&#x2F;code&gt; (read) input of the Telnet block.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;improved-telnet.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This should work a bit nicer. I&#x27;ll now run my python script while the simulation is also going. The 1hz clock signal I sent before was painfully slow, so I&#x27;m going to crank that up to a whopping 10hz!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;hello-digital-world.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It works! I know it&#x27;s a bit silly, but now I have this interface that I can use to interact with little simulated hardware designs. I think that&#x27;s actually pretty nifty.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h2 id=&quot;more-softwarification&quot;&gt;More Softwarification&lt;&#x2F;h2&gt;
&lt;p&gt;Before wrapping this post, I want to use this pattern to do something a bit more hardware oriented. Certainly &lt;code&gt;cat&lt;&#x2F;code&gt; or &lt;code&gt;nc&lt;&#x2F;code&gt; can offer pretty similar functionality to this without all the fuss!&lt;&#x2F;p&gt;
&lt;p&gt;Considering the output is 8-bit, the first thing that comes to mind for me is the 7-segment display, which is more of an 8-segment display really! I can use each bit of a byte sent to the Telnet block to map directly to a display segment.&lt;&#x2F;p&gt;
&lt;p&gt;In the video below, I remove the Terminal bits and replace it with the 7-segment display. To open up the 8-bit &lt;code&gt;out&lt;&#x2F;code&gt; bus I&#x27;ll use the &lt;code&gt;Splitter&#x2F;Merger&lt;&#x2F;code&gt; component and take the 8-bit input and split it into 8 1-bit outputs, the &lt;code&gt;1*8&lt;&#x2F;code&gt; expression helps me specify this. Then I route the wires to the display&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;telnet-to-segment.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I definitely didn&#x27;t practice this like a half dozen times to create the facade that I know what I&#x27;m doing...&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll modify the output of my python script to light up 1 bit at a time. I&#x27;ve played enough with &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=thrx3SBEpL8&quot;&gt;binary numbers&lt;&#x2F;a&gt; to have picked up a few tricks so I&#x27;ll just use some hexadecimal encoded characters for this.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#!&#x2F;usr&#x2F;bin&#x2F;env python
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;socket &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;create_connection
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS &lt;&#x2F;span&gt;&lt;span&gt;= (&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;127.0.0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1337&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;socket = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;create_connection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x00\x01\x02\x04\x08\x10\x20\x40\x80&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Running this while simulating, I can visually identify which bits goes to which segment.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;what-bits.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It was a little handy to have a little video clip of this running too, so I could take a screenshot and label my semi-arbitrary segment numbering!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mad-computer-science&#x2F;bit-numbery.png&quot; alt=&quot;Segmented bits&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With this, I&#x27;ll make one more reference for what segments I want lit for each number.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th align=&quot;center&quot;&gt;Number &lt;&#x2F;th&gt;&lt;th align=&quot;center&quot;&gt; Segments &lt;&#x2F;th&gt;&lt;th align=&quot;center&quot;&gt; Hexadecimal&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;--543210&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;3F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;1&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-----21-&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;06&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;2&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6-432-0&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;5D&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;3&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6--3210&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;4F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;4&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-65--21-&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;66&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;5&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-65-3-10&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;6B&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;6&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6543-10&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;7B&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;7&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;----321-&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;0E&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;8&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-6543210&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;7F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;9&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;-65-3210&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;6F&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;For my final trick, I&#x27;ll make a countdown. I&#x27;ll tick down from 9 to 0, then flash the whole display a couple times with &lt;code&gt;0x00&lt;&#x2F;code&gt; and &lt;code&gt;0xFF&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#!&#x2F;usr&#x2F;bin&#x2F;env python
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;socket &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;create_connection
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS &lt;&#x2F;span&gt;&lt;span&gt;= (&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;127.0.0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1337&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;socket = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;create_connection&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ADDRESS&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x00\x6f\x7f\x0e\x7b\x6b\x66\x4f\x5d\x06\x3f&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;socket.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sendall&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\x00\xff\x00\xff\x00\xff&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I&#x27;m hoping that&#x27;ll work!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;mad-computer-science&#x2F;countdown.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It does!&lt;&#x2F;p&gt;
&lt;p&gt;With that, we have our little software foot in the door for some hardware tinkering. The Telnet block certainly won&#x27;t easily translate to a real world design, but this pattern will be very useful for the tinkering to come. If nothing else, it allows for some experimentation through a mix of software and hardware!&lt;&#x2F;p&gt;
&lt;p&gt;I hope you enjoyed this post, stay tuned for the next one!&lt;&#x2F;p&gt;
&lt;h6 id=&quot;bonus-material&quot;&gt;Bonus material&lt;&#x2F;h6&gt;
&lt;p&gt;While you wait for the next post, here are a couple good videos to check out:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=gI-qXk7XojA&quot;&gt;Boolean Logic &amp;amp; Logic Gates: Crash Course Computer Science #3&lt;&#x2F;a&gt; by &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;c&#x2F;crashcourse&quot;&gt;Crash Course&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;playlist?list=PLowKtXNTBypGqImE405J2565dvjafglHU&quot;&gt;Building an 8-bit breadboard computer&lt;&#x2F;a&gt; by &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;c&#x2F;BenEater&quot;&gt;Ben Eater&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;playlist?list=PLilenfQGj6CEG6iZ4TQJ10PI7pCWsy1AO&quot;&gt;Building A CPU From Scratch&lt;&#x2F;a&gt; by &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;c&#x2F;rj45Creates&quot;&gt;rj45 Creates&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Mad Computer Science – Part 0 – Intro</title>
		<published>2022-08-07T00:00:00+00:00</published>
		<updated>2022-08-07T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/mad-computer-science-part-0/" type="text/html"/>
		<id>https://suchprogramming.com/mad-computer-science-part-0/</id>
		<content type="html">&lt;p&gt;I strongly believe we&#x27;re approaching a revolution in computer technology, and I&#x27;d like you to join me in making that happen!&lt;&#x2F;p&gt;
&lt;p&gt;The performance growth of computers is stalling out. The smart folks that build our modern CPUs, GPUs and other hardware peripherals still have some tricks up their sleeve, but we&#x27;re continuing to approach some very difficult limitations. CPU &lt;a href=&quot;https:&#x2F;&#x2F;www.maketecheasier.com&#x2F;why-cpu-clock-speed-isnt-increasing&#x2F;&quot;&gt;clock speeds have reached a plateau&lt;&#x2F;a&gt;, and we’re starting to build &lt;a href=&quot;https:&#x2F;&#x2F;newsroom.ibm.com&#x2F;2021-05-06-IBM-Unveils-Worlds-First-2-Nanometer-Chip-Technology,-Opening-a-New-Frontier-for-Semiconductors&quot;&gt;transistors that are just 4 silicon atoms wide&lt;&#x2F;a&gt;. We’re headed to a technology era where the mostly centralized model of computing is reaching its peak.&lt;&#x2F;p&gt;
&lt;p&gt;The computer engineering field is heavily software focused, with about &lt;a href=&quot;https:&#x2F;&#x2F;www.bls.gov&#x2F;ooh&#x2F;computer-and-information-technology&#x2F;software-developers.htm&quot;&gt;29 software engineers&lt;&#x2F;a&gt; for every &lt;a href=&quot;https:&#x2F;&#x2F;www.bls.gov&#x2F;ooh&#x2F;architecture-and-engineering&#x2F;computer-hardware-engineers.htm#tab-1&quot;&gt;1 hardware engineer&lt;&#x2F;a&gt; in the US. This ratio is increasing too, with software jobs expected to grow by 22% over the next ten years compared to 2% for hardware. The Open Source Software movement made dramatic improvements in the accessibility of software design, and while there have been significant strides in Open Source Hardware, we still haven&#x27;t democratized hardware design to nearly the same extent.&lt;&#x2F;p&gt;
&lt;p&gt;The hardware side of computing needs more software engineers to dive deeper into the hardware world. It didn&#x27;t used to be reasonably possible to tinker with hardware design as an amateur due to prohibitively expensive and complex design tools. This dam is finally starting to break, with amazing efforts such as the &lt;a href=&quot;https:&#x2F;&#x2F;f4pga.org&#x2F;&quot;&gt;F4PGA&lt;&#x2F;a&gt; project opening up FPGA based logic design and the &lt;a href=&quot;https:&#x2F;&#x2F;efabless.com&#x2F;open_shuttle_program&quot;&gt;Efabless Open MPW Program&lt;&#x2F;a&gt; offering anyone the chance to make a small ASIC with Open Source design tools. I believe these types of projects will be invaluable over the long term.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s intimidating to get into hardware and logic design, but it is getting easier, more open and more approachable every year. I think the main element we&#x27;re missing for a &lt;a href=&quot;https:&#x2F;&#x2F;iscaconf.org&#x2F;isca2018&#x2F;turing_lecture.html&quot;&gt;paradigm shift in computing&lt;&#x2F;a&gt; is more communities of hackers and tinkerers experimenting with hardware designs in willy-nilly fashion, bringing fresh eyes and novel ideas to this area of design that&#x27;s been traditionally infeasible for most of us.&lt;&#x2F;p&gt;
&lt;p&gt;In the aspiration of contributing to this change, I&#x27;m hoping to share the fun of experimenting with mad computer science. I&#x27;m not a professionally trained hardware engineer, and you don&#x27;t have to be either! In my journey, I&#x27;ve found that many aspects of hardware or logic design aren&#x27;t nearly as difficult as they seem to be to tinker with as a noob, but there&#x27;s definitely a lot to learn with fewer entry level resources available.&lt;&#x2F;p&gt;
&lt;p&gt;In this post series I&#x27;ll dig into various computer engineering topics and demonstrate simple and inexpensive ways to tinker with these concepts. I hope curious folks will check it out and begin their own experimentation that may lead to incredible outcomes in the future!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Rusty Macros - Declarative Macros in Rust</title>
		<published>2022-05-21T00:00:00+00:00</published>
		<updated>2022-05-21T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/rusty-macros/" type="text/html"/>
		<id>https://suchprogramming.com/rusty-macros/</id>
		<content type="html">&lt;p&gt;I&#x27;ve been learning and loving Rust for something like 9 months now, but I haven&#x27;t yet gotten a good grip on its various macro systems. So in this post I&#x27;ll be exploring and sharing what I learn about writing macros in Rust. I&#x27;ll start with a brief exploration of the different types of macros Rust supports, a few random anecdotes and then get into some declarative macro tinkering!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;what-are-we-even-looking-at&quot;&gt;What are we even looking at?&lt;&#x2F;h1&gt;
&lt;p&gt;Macros in Rust can provide a lot of utility and they can help simplify the process of writing code. Built-in macros like &lt;code&gt;print!&lt;&#x2F;code&gt;, &lt;code&gt;write!&lt;&#x2F;code&gt;, &lt;code&gt;vec!&lt;&#x2F;code&gt; and others are sprinkled throughout my code. Many of the dependencies I often use provide handy macros as well like &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;tokio&#x2F;latest&#x2F;tokio&#x2F;attr.main.html&quot;&gt;&lt;code&gt;#[tokio::main]&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;docs.serde.rs&#x2F;serde_json&#x2F;macro.json.html&quot;&gt;&lt;code&gt;json!&lt;&#x2F;code&gt; from serde&lt;&#x2F;a&gt; or the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;thiserror&#x2F;latest&#x2F;thiserror&#x2F;index.html&quot;&gt;derive and attribute macros provided by &lt;code&gt;thiserror&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. I&#x27;ve looked with curiosity at how some of the macros I&#x27;ve used are implemented, but for the most part my experience with that has given me the same feelings I get when I look at my old Perl code... &amp;quot;wtf I am I even looking at?&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ve dabbled with enough complex systems to know that I learn better with a bottom-up approach, so I&#x27;ll start with the very basic building blocks and work my way up to increasingly more useful and complex designs. I&#x27;ve read that there are different types of macros, so enumerating those types feels like the best place to start.&lt;&#x2F;p&gt;
&lt;p&gt;Looking at &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;book&#x2F;ch19-06-macros.html&quot;&gt;The Book&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;blog.logrocket.com&#x2F;macros-in-rust-a-tutorial-with-examples&#x2F;#typesofmacrosinrust&quot;&gt;this post by LogRocket&lt;&#x2F;a&gt;. These are the types of macros in Rust:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Declarative macros via &lt;code&gt;macro_rules!&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Procedural macros
&lt;ul&gt;
&lt;li&gt;Derive macros&lt;&#x2F;li&gt;
&lt;li&gt;Attribute-like macros&lt;&#x2F;li&gt;
&lt;li&gt;Function-like procedural macros&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Generally, macros are tools for code generation. Between declarative and procedural macros, the former seems to be more common and the latter more advanced, so I&#x27;ll explore the declarative macro systems first.&lt;&#x2F;p&gt;
&lt;p&gt;Just before jumping into declarative macros, I also want to call out &lt;a href=&quot;https:&#x2F;&#x2F;veykril.github.io&#x2F;tlborm&#x2F;introduction.html&quot;&gt;The Little Book of Rust Macros&lt;&#x2F;a&gt; as another great resource to have on hand. Often when I see or hear mentions of this book, it&#x27;s followed by a warning that the information is outdated. &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;veykril&#x2F;tlborm&quot;&gt;This version is a fork&lt;&#x2F;a&gt; that&#x27;s actively maintained though, so it is very much worth consideration.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;i-declare&quot;&gt;I declare!&lt;&#x2F;h1&gt;
&lt;p&gt;When I look at the example macros in the docs, references and posts, my first reaction has usually been a strong aversion to the syntax. Right from the start, declarative macros begin with &lt;code&gt;macro_rules!&lt;&#x2F;code&gt;, which certainly comes off as declarative expression to me but... is this really the syntax&#x2F;keyword for this? I was glad to find &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;issues&#x2F;8224&quot;&gt;I&#x27;m not alone&lt;&#x2F;a&gt; in thinking this should be something more like &lt;code&gt;macro!&lt;&#x2F;code&gt;, and &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;issues&#x2F;39412&quot;&gt;I&#x27;m hoping someday that&#x27;ll be what makes this post outdated&lt;&#x2F;a&gt;. (See also: &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rfcs&#x2F;issues&#x2F;440&quot;&gt;Rust RFC #440&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rfcs&#x2F;pull&#x2F;1584&quot;&gt;Rust RFC #1584&lt;&#x2F;a&gt;)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;rusty-macros&#x2F;odoyle-rules.gif&quot; alt=&quot;Macro Rules!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s partially comforting to learn that this is a result of &lt;a href=&quot;https:&#x2F;&#x2F;en.wikibooks.org&#x2F;wiki&#x2F;Scheme_Programming&#x2F;Macros&quot;&gt;Scheme&#x27;s influence on Rust&lt;&#x2F;a&gt; and not just an odd choice (though it certainly foreshadows the &lt;a href=&quot;https:&#x2F;&#x2F;i.redd.it&#x2F;9gwghrz9rcs11.jpg&quot;&gt;Lispy business&lt;&#x2F;a&gt; to come). I know there are people that have a strong adoration for Lisp and they definitely have good reasons to feel that way, but my personal opinion is that Lisp dialects are usually more academic than they are practical. I really like when non-programmers can look at my code and follow along and that just doesn&#x27;t seem feasible to me with a Lisp dialect. When I see the 7 closing parenthesis in a row in a tiny example I feel like I&#x27;m looking at a syntax made for machines rather than human beings.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;scm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-scm &quot;&gt;&lt;code class=&quot;language-scm&quot; data-lang=&quot;scm&quot;&gt;&lt;span&gt;(define-syntax &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while
&lt;&#x2F;span&gt;&lt;span&gt;  (syntax-rules ()
&lt;&#x2F;span&gt;&lt;span&gt;    ((_ condition . body)
&lt;&#x2F;span&gt;&lt;span&gt;     (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let loop &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;       (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;cond &lt;&#x2F;span&gt;&lt;span&gt;(condition
&lt;&#x2F;span&gt;&lt;span&gt;        (begin . body)
&lt;&#x2F;span&gt;&lt;span&gt;        (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;loop&lt;&#x2F;span&gt;&lt;span&gt;)))))))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;15% parenthesis by volume! (or 20% if you remove non-essential whitespace)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;rusty-macros&#x2F;yo-lisp.jpg&quot; alt=&quot;Yo Lisp!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With that context in place, and the cathartic vent, I already feel more prepared to enter &lt;a href=&quot;https:&#x2F;&#x2F;esolangs.org&#x2F;wiki&#x2F;Parenthesis_Hell&quot;&gt;Parenthesis Hell&lt;&#x2F;a&gt;. Don&#x27;t fret though! I&#x27;m still talking about Rust, so at least we&#x27;re going to &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Elysium&quot;&gt;the Elysian plain&lt;&#x2F;a&gt; and not &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Tartarus&quot;&gt;Tartarus&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;basic-syntax-and-structure&quot;&gt;Basic syntax and structure&lt;&#x2F;h1&gt;
&lt;p&gt;I want to start with a very simple macro that uses minimum syntax and build up from there. For this, I&#x27;m mostly referencing &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;reference&#x2F;macros-by-example.html&quot;&gt;The Rust Reference - Macros By Example&lt;&#x2F;a&gt;. I also ended up on &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;rust-by-example&#x2F;macros.html&quot;&gt;Rust By Example - macro_rules!&lt;&#x2F;a&gt; by accident at least twice thinking &amp;quot;I swear this page looked different 5 minutes ago&amp;quot;, both are good to keep handy!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span&gt;my_simplest_macro_yet {
&lt;&#x2F;span&gt;&lt;span&gt;    () =&amp;gt; {}
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    my_simplest_macro_yet!();
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Alright, first goal achieved! It&#x27;s &lt;em&gt;technically&lt;&#x2F;em&gt; a macro, and I&#x27;d say it certainly achieves the goal of doing nothing quite well.&lt;&#x2F;p&gt;
&lt;p&gt;While looking at the syntax tree for &lt;code&gt;macro_rules!&lt;&#x2F;code&gt; I noticed that &lt;a href=&quot;https:&#x2F;&#x2F;users.rust-lang.org&#x2F;t&#x2F;braces-square-brackets-and-parentheses-in-macro-call&#x2F;9984&#x2F;7&quot;&gt;you can also use parenthesis or square brackets&lt;&#x2F;a&gt; instead of curly brackets as long as you also add a semicolon at the end:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span&gt;my_simplest_macro_yet [
&lt;&#x2F;span&gt;&lt;span&gt;    () =&amp;gt; {}
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span&gt;my_simplest_macro_yet (
&lt;&#x2F;span&gt;&lt;span&gt;    () =&amp;gt; {}
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As far as I can tell there is no real difference between these three. I don&#x27;t think I&#x27;ve actually seen these last two formats in the wild... but hey, if you want to be the oddball here&#x27;s one way to do it!&lt;&#x2F;p&gt;
&lt;p&gt;As described in &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;book&#x2F;ch19-06-macros.html&quot;&gt;The Book&lt;&#x2F;a&gt;, you&#x27;ll write declarative macros in Rust using a pattern matching syntax very similar to what is employed for &lt;code&gt;match&lt;&#x2F;code&gt; expressions. If the macro inputs match the thingies (matcher) to the left of the &lt;code&gt;=&amp;gt;&lt;&#x2F;code&gt;, apply the block of codestuffs (transcriber) from the right side.&lt;&#x2F;p&gt;
&lt;p&gt;The inputs you provide to macros like &lt;code&gt;println!&lt;&#x2F;code&gt; are not &lt;em&gt;normal&lt;&#x2F;em&gt; inputs. In the world of Rust macros, these inputs are lexical tokens of the language, so your actual code is the input to it. This is also the case on the output side as well. &lt;code&gt;println!&lt;&#x2F;code&gt; doesn&#x27;t &lt;em&gt;technically&lt;&#x2F;em&gt; perform writing to standard out, but it generates code that will.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;ll be handy to see the actual results of the macros, so let&#x27;s explore how to see their expanded outputs before dabbling with their inputs.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;show-me-the-macro&quot;&gt;Show me the macro!&lt;&#x2F;h1&gt;
&lt;p&gt;Since the macros aren&#x27;t &lt;em&gt;normal&lt;&#x2F;em&gt; code, debugging them isn&#x27;t quite normal either. I want some way to experiment and inspect the results, and &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dtolnay&#x2F;cargo-expand&quot;&gt;cargo-expand&lt;&#x2F;a&gt; currently looks like the simplest way to do that.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;cargo install cargo-expand
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To take a look at what&#x27;s going on, I&#x27;ll run &lt;code&gt;cargo expand&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ cargo expand
&lt;&#x2F;span&gt;&lt;span&gt;    Checking rusty-macros v0.1.0
&lt;&#x2F;span&gt;&lt;span&gt;error: the option `Z` is only accepted on the nightly compiler
&lt;&#x2F;span&gt;&lt;span&gt;error: could not compile `rusty-macros`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or not, I suppose! I&#x27;ll run &lt;code&gt;rustup default nightly&lt;&#x2F;code&gt; to appease the deities of macro expansion and then try again.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ cargo expand
&lt;&#x2F;span&gt;&lt;span&gt;    Checking rusty-macros v0.1.0
&lt;&#x2F;span&gt;&lt;span&gt;    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;#![&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;feature&lt;&#x2F;span&gt;&lt;span&gt;(prelude_import)]
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;prelude_import&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;std::prelude::rust_2021::*;
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;macro_use&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span&gt; std;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hmm... well I guess I&#x27;m not sure what I actually expected here, but this does seem like a pretty reasonable implementation of my &lt;em&gt;does nothing at all&lt;&#x2F;em&gt; macro! Maybe it&#x27;s a little more useful to consider what the default Rust &amp;quot;Hello World&amp;quot; expands to.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;cargo expand&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;#![&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;feature&lt;&#x2F;span&gt;&lt;span&gt;(prelude_import)]
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;prelude_import&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;std::prelude::rust_2021::*;
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;macro_use&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;extern crate&lt;&#x2F;span&gt;&lt;span&gt; std;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        ::std::io::_print(::core::fmt::Arguments::new_v1(&amp;amp;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &amp;amp;[]));
&lt;&#x2F;span&gt;&lt;span&gt;    };
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is a bit more educational, as I can see the &lt;code&gt;println!&lt;&#x2F;code&gt; macro has indeed been replaced with code that will do the needful. I also get the same expansion if I modify by basic macro to wrap around the same functionality like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span&gt;say_hello_world {
&lt;&#x2F;span&gt;&lt;span&gt;    () =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    say_hello_world!()
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What if I add a touch of conditional logic to the matching block for this macro? I&#x27;m interested to see what happens (this is my kind of computer science right here: less math and more experimentation!)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span&gt;say_hello_world {
&lt;&#x2F;span&gt;&lt;span&gt;    () =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;            println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    say_hello_world!()
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What say you, &lt;code&gt;cargo expand&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        {
&lt;&#x2F;span&gt;&lt;span&gt;            ::std::io::_print(::core::fmt::Arguments::new_v1(&amp;amp;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &amp;amp;[]));
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are two things about this result I find interesting.&lt;&#x2F;p&gt;
&lt;p&gt;The first observation is that my always-true &lt;code&gt;if&lt;&#x2F;code&gt; condition survived expansion. I added this useless condition to test if&#x2F;where it would be optimized away. Indeed, I don&#x27;t see this condition &lt;em&gt;actually&lt;&#x2F;em&gt; implemented in the compiled program, which indicates to me that the optimization pass(es) occur sometime later in the compilation process. This makes total sense though, as optimization during macro expansion would likely be a bit premature. It feels like something useful to be aware of.&lt;&#x2F;p&gt;
&lt;p&gt;The second observation is not quite as useful: &lt;code&gt;println!&lt;&#x2F;code&gt; seems to be implemented in a way that that nests the output in its own block. This results in the expansion coming out as &lt;code&gt;if 1 &amp;gt; 0 {{ print }}&lt;&#x2F;code&gt;... &lt;code&gt;(maybe (equal ((it) (&amp;quot;just that Lisp influence again&amp;quot;))))&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m also a little tickled that &lt;code&gt;print!&lt;&#x2F;code&gt; doesn&#x27;t have these extra curlies:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span&gt;say_hello_world {
&lt;&#x2F;span&gt;&lt;span&gt;    () =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;            print!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    say_hello_world!()
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;cargo expand&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        ::std::io::_print(::core::fmt::Arguments::new_v1(&amp;amp;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &amp;amp;[]))
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Seeing the expanded macros should be pretty helpful for debugging, and with that it&#x27;s time for some tinkering with macro inputs!&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;charon-s-obol&quot;&gt;Charon&#x27;s obol&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Charon%27s_obol&quot;&gt;Charon&#x27;s obol?!&lt;&#x2F;a&gt; Pardon me, I&#x27;m probably enjoying myself a little too much with the Parenthesis Hell and Hades references. I hope you don&#x27;t mind though because it&#x27;s time to visit the ferryman (he just wants your tokens, dammit!)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;rusty-macros&#x2F;facepalm.jpg&quot; alt=&quot;:facepalm:&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;To start off as simply as possible, I&#x27;ll first look at matching some simple literals in my macro.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span&gt;say_what {
&lt;&#x2F;span&gt;&lt;span&gt;    (true) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        print!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, world!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    };
&lt;&#x2F;span&gt;&lt;span&gt;    (false) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        print!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Goodbye world?&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    };
&lt;&#x2F;span&gt;&lt;span&gt;    (2) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        print!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, worlds!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    say_what!(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It does exactly what I&#x27;m expecting it to!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Hello, worlds!&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Looking at how this expands, I can verify that this &lt;code&gt;say_what!&lt;&#x2F;code&gt; macro indeed acts a lot like a &lt;code&gt;match&lt;&#x2F;code&gt; expression (so far).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    ::std::io::_print(::core::fmt::Arguments::new_v1(&amp;amp;[&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, worlds!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;], &amp;amp;[]))
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Post-expansion there are no indications left over of the original macro, the inputs to it, nor the un-matched branches. This macro isn&#x27;t quite useful though, so it&#x27;ll be much more interesting to implement something that&#x27;s closer to a more realistic goal.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;if-at-first-you-don-t-succeed-that-makes-me-feel-better-because-neither-did-i&quot;&gt;If at first you don&#x27;t succeed... that makes me feel better because neither did I&lt;&#x2F;h1&gt;
&lt;p&gt;The example I have in mind is a &lt;code&gt;retry!&lt;&#x2F;code&gt; macro. The scenario I&#x27;m giving myself is that I have some kind of network function that happens to use &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;IP_over_Avian_Carriers&quot;&gt;IPoAC&lt;&#x2F;a&gt;. Since this protocol has pretty questionable reliability I&#x27;d like to retry the request a couple times before giving up.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;rusty-macros&#x2F;pigeon.gif&quot; alt=&quot;DJ pidgey&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s where I&#x27;m starting from:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;rand::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;anyhow::{Result,anyhow};
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pigeon_post&lt;&#x2F;span&gt;&lt;span&gt;() -&amp;gt; Result&amp;lt;()&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;random&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        Ok(())
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        Err(anyhow!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The pigeon got distracted in a grocery store parking lot&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;pigeon_post&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        Ok(_) =&amp;gt; println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Success: The pigeon arrived!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;),
&lt;&#x2F;span&gt;&lt;span&gt;        Err(error) =&amp;gt; println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Error: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, error)
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I want my &lt;code&gt;retry!&lt;&#x2F;code&gt; macro to take 2 arguments: the identifier of the unreliable function to call and a numeric literal for the maximum amount of attempts to make.&lt;&#x2F;p&gt;
&lt;p&gt;The first couple ways I tried to implement this macro weren&#x27;t going very well, even when I could see the problems pretty clearly using &lt;code&gt;cargo expand&lt;&#x2F;code&gt;. What ended up working for me was to go backwards: writing the result I want from the macro, moving that into the new macro, setting up the inputs then refactoring those inputs into the transcribed code.&lt;&#x2F;p&gt;
&lt;p&gt;So starting off in &lt;code&gt;main()&lt;&#x2F;code&gt;, I wrote what I wanted out of the macro:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;_ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;pigeon_post&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;            Ok(_) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Success: The pigeon arrived!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;            Err(error) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Error: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, error)
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;We should probably hire a better pigeoneer&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To setup the inputs, I&#x27;ll need to use &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;reference&#x2F;macros-by-example.html#metavariables&quot;&gt;metavariables&lt;&#x2F;a&gt;. These are written in the format &lt;code&gt;$&amp;lt;NAME&amp;gt;:&amp;lt;FRAGMENT-SPECIFIER&amp;gt;&lt;&#x2F;code&gt;. The fragment specifiers declare what kind of Rust syntax we&#x27;re looking to match on and capture. Picking the right fragment specifier seems like one of those things you generally get better at with time and practice. In my case, I&#x27;m using &lt;code&gt;ident&lt;&#x2F;code&gt; to pass in the identifier of the function to call and &lt;code&gt;literal&lt;&#x2F;code&gt; for the number of attempts to make.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span&gt;retry {
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$iffy_call&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ident&lt;&#x2F;span&gt;&lt;span&gt;, $max_attempts:literal) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        ...
&lt;&#x2F;span&gt;&lt;span&gt;    };
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now all I need is a bit of copy-pasta and to replace some syntax with my metavariables.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;rand::prelude::*;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;anyhow::{Result,anyhow};
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;macro_rules! &lt;&#x2F;span&gt;&lt;span&gt;retry {
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$iffy_call&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ident&lt;&#x2F;span&gt;&lt;span&gt;, $max_attempts:literal) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;_ in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$max_attempts &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$iffy_call&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;                Ok(_) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                    println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Success: The pigeon arrived!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return
&lt;&#x2F;span&gt;&lt;span&gt;                }
&lt;&#x2F;span&gt;&lt;span&gt;                Err(error) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;                    println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Error: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, error)
&lt;&#x2F;span&gt;&lt;span&gt;                }
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;        println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;We should probably hire a better pigeoneer&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    };
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pigeon_post&lt;&#x2F;span&gt;&lt;span&gt;() -&amp;gt; Result&amp;lt;()&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;random&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        Ok(())
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        Err(anyhow!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The pigeon got distracted in a grocery store parking lot&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    retry!(pigeon_post, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Error: The pigeon got distracted in a grocery store parking lot
&lt;&#x2F;span&gt;&lt;span&gt;Error: The pigeon got distracted in a grocery store parking lot
&lt;&#x2F;span&gt;&lt;span&gt;Success: The pigeon arrived!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There it is, a working declarative retry macro! That&#x27;s where I&#x27;ll call it a wrap on this post. I hope you&#x27;ve found this entertaining and&#x2F;or helpful in your own Rust adventures.&lt;&#x2F;p&gt;
&lt;p&gt;Originally I was planning to make this a little series on Rust macros, but as I researched my topic ideas I kept finding other good resources that did a great job of covering those topics. I&#x27;m not averse to reinventing the wheel, but only when I think I have a good idea! So instead, here&#x27;s some other resources I thought could be pretty helpful:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;A few macro guides that re-implement &lt;code&gt;vec!&lt;&#x2F;code&gt;:
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;book&#x2F;ch19-06-macros.html&quot;&gt;The Rust Book&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=q6paRBbLgNw&quot;&gt;The Crust of Rust&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Similarly, some resources that work to implement a &lt;code&gt;hashmap!&lt;&#x2F;code&gt; macro:
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;27582739&#x2F;how-do-i-create-a-hashmap-literal&quot;&gt;This stack overflow answer by Shepmaster&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;medium.com&#x2F;@phoomparin&#x2F;a-beginners-guide-to-rust-macros-5c75594498f1&quot;&gt;A Beginner&#x27;s Guide to Rust Macros&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rfcs&#x2F;issues&#x2F;542&quot;&gt;Eventually we&#x27;ll get this in &lt;code&gt;std&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Also pretty good: &lt;a href=&quot;https:&#x2F;&#x2F;danielkeep.github.io&#x2F;practical-intro-to-macros.html&quot;&gt;A Practical Intro to Macros in Rust 1.0&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>FPGA meets 6502</title>
		<published>2018-12-11T00:00:00+00:00</published>
		<updated>2018-12-11T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/fpga-meets-6502/" type="text/html"/>
		<id>https://suchprogramming.com/fpga-meets-6502/</id>
		<content type="html">&lt;p&gt;So I&#x27;m working on designing my own homebrew 6502-based microcomputer. I have a lot of ideas for features I&#x27;d like, but before I make too many crazy decisions I&#x27;d like to solidify my understanding of the processor I&#x27;m building the whole system around. To this end, I am grabbing my &lt;a href=&quot;https:&#x2F;&#x2F;store.digilentinc.com&#x2F;arty-a7-artix-7-fpga-development-board-for-makers-and-hobbyists&#x2F;&quot;&gt;Arty-A7&lt;&#x2F;a&gt;, a &lt;a href=&quot;http:&#x2F;&#x2F;www.westerndesigncenter.com&#x2F;wdc&#x2F;w65c02s-chip.cfm&quot;&gt;W65C02S&lt;&#x2F;a&gt; mounted on a breadboard and a bag of jumper cables to wire them together.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;planning-phase&quot;&gt;Planning Phase&lt;&#x2F;h1&gt;
&lt;p&gt;I chose to use my Arty-A7 FPGA board because it has a ton of I&#x2F;O pins, it can easily meet my performance requirements and because I just plain love tinkering with FPGAs.&lt;&#x2F;p&gt;
&lt;p&gt;This will take a smidge of organization to get started. I began by taking an inventory of the various pin functions on the W65C02S package.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fpga-meets-6502&#x2F;W65C02S-40-pin-dip-color.gif&quot; alt=&quot;Pin diagram for W65C02S from Western Design Center Product Documentation&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Generally speaking, there are 16 pins for the address bus, 8 pins for the data bus, 13 control pins, 2 power pins and 1 no-connect. This means I’ll need to wire 37 IO pins + 2 power pins to my FPGA board. With that in mind I’ve decided to use a mix of the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Pmod_Interface&quot;&gt;PMOD&lt;&#x2F;a&gt; interfaces on my Arty board as well as the Arduino-shield pins next to them. Each PMOD has 8 IO pins so I’ll use 1 PMOD for the data bus and 2 for the address bus, with the control pins on adjacent shield header. I’ll pull power from one of the PMODs as well.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fpga-meets-6502&#x2F;plan.png&quot; alt=&quot;Rough Pin Planning&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With my rough plan in place, I took some notes on how my 6502 pins will map to the specific pins. I planned for which PMOD pin each bit of the address and data buses will go, along with associating each control pin to a numbered shield pin. Now for the fun part!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;wired-up&quot;&gt;Wired Up&lt;&#x2F;h1&gt;
&lt;p&gt;Now that the rough planning is complete, I carefully use the wire jumpers to connect everything as planned. I wired one pin at a time roughly starting from the top of the 6502 and working down, it’s a bit of a mess but it does the job.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fpga-meets-6502&#x2F;franken6502.jpg&quot; alt=&quot;The resulting mess of wires&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now that the plumbing is in place, the real fun begins. I start by opening up Vivado and begin getting these pins mapped into a design. I’ll create a new &lt;code&gt;top.sv&lt;&#x2F;code&gt; file to kick off the design.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sv&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sv &quot;&gt;&lt;code class=&quot;language-sv&quot; data-lang=&quot;sv&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;`timescale 1ns &lt;&#x2F;span&gt;&lt;span&gt;&#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1ps
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;top (
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Arty stuff
&lt;&#x2F;span&gt;&lt;span&gt;  input  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; clk_100mhz,
&lt;&#x2F;span&gt;&lt;span&gt;  input  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; reset,
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; 6502 stuff 
&lt;&#x2F;span&gt;&lt;span&gt;  input  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] address,
&lt;&#x2F;span&gt;&lt;span&gt;  inout  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] data,
&lt;&#x2F;span&gt;&lt;span&gt;  input  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; vpb,
&lt;&#x2F;span&gt;&lt;span&gt;  input  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; phi1o,
&lt;&#x2F;span&gt;&lt;span&gt;  input  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; phi2o,
&lt;&#x2F;span&gt;&lt;span&gt;  input  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; rwb,
&lt;&#x2F;span&gt;&lt;span&gt;  input  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; mlb,
&lt;&#x2F;span&gt;&lt;span&gt;  input  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; sync,
&lt;&#x2F;span&gt;&lt;span&gt;  output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; resb,
&lt;&#x2F;span&gt;&lt;span&gt;  output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; sob,
&lt;&#x2F;span&gt;&lt;span&gt;  output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; phi2,
&lt;&#x2F;span&gt;&lt;span&gt;  output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; be,
&lt;&#x2F;span&gt;&lt;span&gt;  output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; irqb,
&lt;&#x2F;span&gt;&lt;span&gt;  output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; nmib,
&lt;&#x2F;span&gt;&lt;span&gt;  output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; rdy
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;endmodule
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The next step is to tediously map the various pins I planned out to the pins on the pins I’ve defined in my &lt;code&gt;top&lt;&#x2F;code&gt; module. The PMOD pins are easily found in the &lt;a href=&quot;https:&#x2F;&#x2F;reference.digilentinc.com&#x2F;reference&#x2F;programmable-logic&#x2F;arty-a7&#x2F;reference-manual&quot;&gt;reference wiki for the board&lt;&#x2F;a&gt;, but I didn’t see the IO shield pins on the primary documentation page, so I referred to the &lt;a href=&quot;https:&#x2F;&#x2F;reference.digilentinc.com&#x2F;reference&#x2F;programmable-logic&#x2F;arty-a7&#x2F;schematic&quot;&gt;provided schematic files&lt;&#x2F;a&gt; for that information. With all that pin mapping information, I added the pins properties into a constraints file.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fpga-meets-6502&#x2F;6502pins.png&quot; alt=&quot;A portion of the XDC Constraint File&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;At this point, everything looks to be ready for tinkering. As an initial experiment to validate that it is working, I’ll use &lt;img src=&quot;https:&#x2F;&#x2F;www.xilinx.com&#x2F;products&#x2F;intellectual-property&#x2F;vio.html&quot; alt=&quot;Vivado’s VIO (Virtual Input&#x2F;Output)&quot; &#x2F;&gt; IP block to manually probe and interact with these pins. Though, just before I generate that, I’ll add a few more lines to my &lt;code&gt;top.sv&lt;&#x2F;code&gt; to support &lt;a href=&quot;https:&#x2F;&#x2F;suchprogramming.com&#x2F;beginning-logic-design-part-8&#x2F;&quot;&gt;bi-directional&lt;&#x2F;a&gt; use of the data bus. For the 6502, when the &lt;code&gt;rwb&lt;&#x2F;code&gt; pin is high, the CPU is reading from the data bus, so I’ll use that as the trigger to put my &lt;code&gt;write_data&lt;&#x2F;code&gt; on that data bus during those times.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;systemverilog&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-systemverilog &quot;&gt;&lt;code class=&quot;language-systemverilog&quot; data-lang=&quot;systemverilog&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;logic&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;] write_data;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;assign&lt;&#x2F;span&gt;&lt;span&gt; data = (rwb) ? write_data : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;&amp;#39;bZ&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    sob = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    be = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    irqb = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    nmib = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    rdy = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once I am running on real hardware, I’ll be stepping through the reset logic for the 6502. So for this project &lt;code&gt;irqb&lt;&#x2F;code&gt;, &lt;code&gt;be&lt;&#x2F;code&gt;, &lt;code&gt;nmib&lt;&#x2F;code&gt;, &lt;code&gt;rdy&lt;&#x2F;code&gt; and &lt;code&gt;sob&lt;&#x2F;code&gt; can all stay at &lt;code&gt;1&lt;&#x2F;code&gt; for now, as I won’t be using them.&lt;&#x2F;p&gt;
&lt;p&gt;Now seems like the time to start building the VIO block. I’ll add ports for the various inputs and outputs, including the register I just added for bi-directional communication on the data bus.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fpga-meets-6502&#x2F;6502vio2.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I’ll let Vivado synthesize the core, and I’ll add it to the &lt;code&gt;top&lt;&#x2F;code&gt; module.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;systemverilog&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-systemverilog &quot;&gt;&lt;code class=&quot;language-systemverilog&quot; data-lang=&quot;systemverilog&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;vio_0 &lt;&#x2F;span&gt;&lt;span&gt;debug_core (
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clk&lt;&#x2F;span&gt;&lt;span&gt;(clk_100mhz),         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; input wire clk
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_in0&lt;&#x2F;span&gt;&lt;span&gt;(address),      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; input wire [15 : 0] probe_in0
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_in1&lt;&#x2F;span&gt;&lt;span&gt;(data),         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; input wire [7 : 0] probe_in1
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_in2&lt;&#x2F;span&gt;&lt;span&gt;(vpb),          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; input wire [0 : 0] probe_in2
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_in3&lt;&#x2F;span&gt;&lt;span&gt;(phi1o),        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; input wire [0 : 0] probe_in3
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_in4&lt;&#x2F;span&gt;&lt;span&gt;(phi2o),        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; input wire [0 : 0] probe_in4
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_in5&lt;&#x2F;span&gt;&lt;span&gt;(rwb),          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; input wire [0 : 0] probe_in5
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_in6&lt;&#x2F;span&gt;&lt;span&gt;(mlb),          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; input wire [0 : 0] probe_in6
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_in7&lt;&#x2F;span&gt;&lt;span&gt;(sync),         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; input wire [0 : 0] probe_in7
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_out0&lt;&#x2F;span&gt;&lt;span&gt;(write_data),  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; output wire [7 : 0] probe_out0
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_out1&lt;&#x2F;span&gt;&lt;span&gt;(resb),        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; output wire [0 : 0] probe_out1
&lt;&#x2F;span&gt;&lt;span&gt;    .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;probe_out2&lt;&#x2F;span&gt;&lt;span&gt;(phi2)         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; output wire [0 : 0] probe_out2
&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now it is time for the moment of truth!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;playing-with-a-running-cpu&quot;&gt;Playing with a Running CPU&lt;&#x2F;h1&gt;
&lt;p&gt;With all that in place, it’s time to build everything and start playing around. After running synthesis, implementation and generating a bitfile, I can flash my new design and debug core to the Arty board and pull up the VIO interface that shows me the current state of things.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fpga-meets-6502&#x2F;vio.png&quot; alt=&quot;VIO showing probe states&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;At this point a bit more knowledge on the how the 6502 operates is necessary.  The &lt;code&gt;phi2&lt;&#x2F;code&gt; signal is the input clock for the CPU. The core of this 6502 is fully static, so this clock can be stopped at anytime and the state within the CPU preserved. This allows me to interact and step the clock manually through VIO, even though my manual input will be extremely slow for interacting with hardware. &lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;fpga-meets-6502&#x2F;vio-clocks.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Since the clock looks good, I’m going to perform a reset of the 6502. Near the end of the reset cycle I’m expecting the address bus to read for the reset vector, a pointer to where the CPU should begin execution after reset. For the timing requirements on getting a reset to work properly, the &lt;a href=&quot;http:&#x2F;&#x2F;www.westerndesigncenter.com&#x2F;wdc&#x2F;documentation&#x2F;w65c02s.pdf&quot;&gt;documentation from WDC&lt;&#x2F;a&gt; states specifically what to do:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;3.11
&lt;&#x2F;span&gt;&lt;span&gt;Reset (RESB)
&lt;&#x2F;span&gt;&lt;span&gt;The Reset (RESB) input is used to initialize the microprocessor and start program execution. The
&lt;&#x2F;span&gt;&lt;span&gt;RESB signal must be held low for at least two clock cycles after VDD reaches operating voltage.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;…
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;When a positive edge is detected, there will be a reset sequence lasting seven clock cycles. The
&lt;&#x2F;span&gt;&lt;span&gt;program counter is loaded with the reset vector from locations FFFC (low byte) and FFFD (high
&lt;&#x2F;span&gt;&lt;span&gt;byte). This is the start location for program control. RESB should be held high after reset for
&lt;&#x2F;span&gt;&lt;span&gt;normal operation. 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So I’ll hold reset low for at least 2 cycles, then set it back to high. I’ll continue to manually cycle the clock until I see CPU begin to fetch the &lt;code&gt;RESET&lt;&#x2F;code&gt; vector. As that vector is read, I will manually give it the address &lt;code&gt;0xDEAD&lt;&#x2F;code&gt; to begin execution at. Then, to keep it running I’ll give it the opcode &lt;code&gt;0xEA&lt;&#x2F;code&gt; (NOP) so it continues to read empty instructions over the bus. It’s worth noting that the 6502 does most things in alignment to the falling edge of the &lt;code&gt;phi2&lt;&#x2F;code&gt; clock.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;video controls=&quot;&quot; src=&quot;&#x2F;videos&#x2F;fpga-meets-6502&#x2F;doing-it.mp4&quot;&gt;&lt;&#x2F;video&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The 6502 has been reset and is running the instructions it’s reading from the FPGA! In the future I’ll extend this design to better simulate an imaginary system. That’ll be my stopping place for today, hope you enjoyed!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Spawning New Linux Processes in C</title>
		<published>2018-05-31T00:00:00+00:00</published>
		<updated>2018-05-31T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/new-linux-process-c/" type="text/html"/>
		<id>https://suchprogramming.com/new-linux-process-c/</id>
		<content type="html">&lt;p&gt;There are many good reasons to spawn other programs in Linux to do your bidding, but most programming languages don’t give you nearly as much control of the process as in C. In this post I will cover some of the most common ways to create new processes and manage them in C on a Linux system.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;grab-a-fork&quot;&gt;Grab a fork&lt;&#x2F;h1&gt;
&lt;p&gt;The classic fork() function is the most popular way to create a new process. It works by duplicating the process that calls it. It may sound complicated but it’s a fairly simple system.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;#include &amp;lt;unistd.h&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;pid_t fork(void);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;    pid_t fork_pid = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fork&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(fork_pid == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello from the child!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello from the parent!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;$ .&#x2F;forktest 
&lt;&#x2F;span&gt;&lt;span&gt;Hello from the parent!
&lt;&#x2F;span&gt;&lt;span&gt;Hello from the child!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When a process calls &lt;code&gt;fork()&lt;&#x2F;code&gt;, Linux will duplicate that current process. The value returned by &lt;code&gt;fork()&lt;&#x2F;code&gt; will be &lt;code&gt;0&lt;&#x2F;code&gt; in the child process. In the parent process &lt;code&gt;fork()&lt;&#x2F;code&gt; will return the PID (Process ID) of the new child process or &lt;code&gt;-1&lt;&#x2F;code&gt; if some error occurs.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;replacing-a-running-process&quot;&gt;Replacing a Running Process&lt;&#x2F;h1&gt;
&lt;p&gt;fter creating a new process, it’s common to replace that child process with an entirely different program. The &lt;code&gt;exec()&lt;&#x2F;code&gt; family of functions can handle this for us. The &lt;code&gt;execl()&lt;&#x2F;code&gt; is the simplest method.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;#include &amp;lt;unistd.h&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;int execl(const char *filename, const char *arg, ...);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All you need to provide is the location of the file to load, and the arguments you’d like to provide to it. Just like for any normal process, the first process is the process name.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;execl&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;yes&#x2F;its&#x2F;bash&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-c&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;echo $0 &amp;amp;&amp;amp; uptime&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;      
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;execltest
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;yes&#x2F;its&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;10:11:12 up 1:07, 2 users, load average: 0.07, 0.10, 0.18
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;a href=&quot;http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man3&#x2F;exec.3.html&quot;&gt;other functions&lt;&#x2F;a&gt; in the &lt;code&gt;exec()&lt;&#x2F;code&gt; family have various options to control arguments and environment variables for the new process that takes over.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;playing-with-the-kids&quot;&gt;Playing with the Kids&lt;&#x2F;h1&gt;
&lt;p&gt;After you have wee ‘lil child processes, you’ll probably want to make sure they are doing as they are told. After you’ve sent the child process off to do its chores, you can use the &lt;code&gt;wait()&lt;&#x2F;code&gt; function to see what it returns with after it’s done.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;#include &amp;lt;sys&#x2F;wait.h&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;pid_t wait(int *status);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When you call &lt;code&gt;wait()&lt;&#x2F;code&gt;, it will block the parent process until any of it’s children processes change state. The &lt;code&gt;status&lt;&#x2F;code&gt; pointer can be used if you’re interested in knowing what kind of state change has occurred, to determine if the program exited normally, or if it was terminated by a control signal. The return value of &lt;code&gt;wait()&lt;&#x2F;code&gt; is the child PID that has changed states.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sys&#x2F;wait.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  pid_t child = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fork&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(child) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;wait&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;child process terminated&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;execl&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;bin&#x2F;bash&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;THECHILD&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;waitkids
&lt;&#x2F;span&gt;&lt;span&gt;$ echo $0
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;THECHILD
&lt;&#x2F;span&gt;&lt;span&gt;$ exit
&lt;&#x2F;span&gt;&lt;span&gt;exit
&lt;&#x2F;span&gt;&lt;span&gt;child process terminated
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Check &lt;a href=&quot;http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man2&#x2F;waitpid.2.html&quot;&gt;the man page for wait&lt;&#x2F;a&gt; for more details on the various options available when waiting.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;attack-of-the-clones&quot;&gt;Attack of the Clones&lt;&#x2F;h1&gt;
&lt;p&gt;The &lt;code&gt;fork()&lt;&#x2F;code&gt;, &lt;code&gt;exec()&lt;&#x2F;code&gt; and &lt;code&gt;wait()&lt;&#x2F;code&gt; families of functions are portable across POSIX compliant systems. If more fine grained control over the process creation is desired, we’ll need to use the Linux specific clone() function.&lt;&#x2F;p&gt;
&lt;p&gt;I won’t cover the &lt;code&gt;clone()&lt;&#x2F;code&gt; in this post, as that’s probably more suited for a dedicated post. Regardless, I suggest perusing &lt;a href=&quot;http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man2&#x2F;clone.2.html&quot;&gt;the man page for it&lt;&#x2F;a&gt; to get an idea of what capabilities it offers.&lt;&#x2F;p&gt;
&lt;p&gt;That covers the basics of process creation using C on Linux!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Debugging C Programs with GDB – Part 3</title>
		<published>2018-03-14T00:00:00+00:00</published>
		<updated>2018-03-14T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/debugging-with-gdb-part-3/" type="text/html"/>
		<id>https://suchprogramming.com/debugging-with-gdb-part-3/</id>
		<content type="html">&lt;p&gt;In my previous GDB post, I went over many common debugger commands while exploring the stack build up and initialization of stack variables for the &lt;code&gt;main&lt;&#x2F;code&gt; function in a simple C program. In this post I’ll use more GDB commands to further inspect the body of this small program.&lt;&#x2F;p&gt;
&lt;p&gt;Quick references:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;darkdust.net&#x2F;files&#x2F;GDB%20Cheat%20Sheet.pdf&quot;&gt;GDB Cheat Sheet&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Example program source at: &lt;a href=&quot;https:&#x2F;&#x2F;www.suchprogramming.com&#x2F;debugging-with-gdb-part-1&#x2F;&quot;&gt;Debugging C Programs with GDB – Part 1&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h1 id=&quot;for-loop-assembly&quot;&gt;For Loop Assembly&lt;&#x2F;h1&gt;
&lt;p&gt;To dig deeper into the exact operation of my &lt;code&gt;main&lt;&#x2F;code&gt; function, I’ll first look at the block of instructions the compiler built when interpreting and optimizing my C code.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;0x0000000000400575 &amp;lt;+15&amp;gt;:	mov    DWORD PTR [rbp-0x4],0x0
&lt;&#x2F;span&gt;&lt;span&gt;0x000000000040057c &amp;lt;+22&amp;gt;:	jmp    0x400596 &amp;lt;main+48&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;0x000000000040057e &amp;lt;+24&amp;gt;:	mov    eax,DWORD PTR [rbp-0x4]
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000400581 &amp;lt;+27&amp;gt;:	mov    esi,eax
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000400583 &amp;lt;+29&amp;gt;:	mov    edi,0x40064a
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000400588 &amp;lt;+34&amp;gt;:	mov    eax,0x0
&lt;&#x2F;span&gt;&lt;span&gt;0x000000000040058d &amp;lt;+39&amp;gt;:	call   0x400440 &amp;lt;printf@plt&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000400592 &amp;lt;+44&amp;gt;:	add    DWORD PTR [rbp-0x4],0x1
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000400596 &amp;lt;+48&amp;gt;:	cmp    DWORD PTR [rbp-0x4],0x9
&lt;&#x2F;span&gt;&lt;span&gt;0x000000000040059a &amp;lt;+52&amp;gt;:	jle    0x40057e &amp;lt;main+24&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;0x000000000040059c &amp;lt;+54&amp;gt;:	mov    rax,QWORD PTR [rip+0x200a9d]
&lt;&#x2F;span&gt;&lt;span&gt;0x00000000004005a3 &amp;lt;+61&amp;gt;:	mov    rdi,rax
&lt;&#x2F;span&gt;&lt;span&gt;0x00000000004005a6 &amp;lt;+64&amp;gt;:	call   0x400430 &amp;lt;puts@plt&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As seen at the end of the previous post, the instruction at &lt;code&gt;&amp;lt;+15&amp;gt;&lt;&#x2F;code&gt; (15 bytes into &lt;code&gt;main&lt;&#x2F;code&gt;) is setting &lt;code&gt;PTR [rbp-4]&lt;&#x2F;code&gt; to &lt;code&gt;0&lt;&#x2F;code&gt;, which for this program is the &lt;code&gt;i&lt;&#x2F;code&gt; iterator variable used in the for loop of &lt;code&gt;main&lt;&#x2F;code&gt;. This is the implementation of the initializer &lt;code&gt;i = 0;&lt;&#x2F;code&gt; in my for loop.&lt;&#x2F;p&gt;
&lt;p&gt;The program then unconditionally jumps to &lt;code&gt;&amp;lt;+48&amp;gt;&lt;&#x2F;code&gt;, which compares (&lt;code&gt;cmp&lt;&#x2F;code&gt;) &lt;code&gt;i&lt;&#x2F;code&gt; to &lt;code&gt;9&lt;&#x2F;code&gt;. This compare instruction will change some CPU flags that are use with conditional jumps such as the &lt;code&gt;jle&lt;&#x2F;code&gt; (jump if less than or equal) instruction that follows. This &lt;code&gt;cmp&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;jle&lt;&#x2F;code&gt; combo is the implementation of the condition of the for loop: &lt;code&gt;i &amp;lt; 10&lt;&#x2F;code&gt;. This shows us that the compiler decided this was better implemented as &lt;code&gt;i &amp;lt;= 9&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now I’ll watch the loop a bit more closely. I’ll start off by adding the breakpoint at the start of main with &lt;code&gt;break main&lt;&#x2F;code&gt;, start the program with &lt;code&gt;run&lt;&#x2F;code&gt;, and &lt;code&gt;disas main&lt;&#x2F;code&gt; to verify where the breakpoint was set.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-3&#x2F;break-main.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now I’ll use&lt;code&gt; watch i&lt;&#x2F;code&gt; to ask GDB to notify me of changes to &lt;code&gt;i&lt;&#x2F;code&gt;. When I continue the program, it’ll run until the value of &lt;code&gt;i&lt;&#x2F;code&gt; is modified, which happens during the &lt;code&gt;add&lt;&#x2F;code&gt; instruction at &lt;code&gt;&amp;lt;main+44&amp;gt;&lt;&#x2F;code&gt;. This instruction, &lt;code&gt;add DWORD PTR [rbp-0x4],0x1]&lt;&#x2F;code&gt;, is the &lt;code&gt;i++&lt;&#x2F;code&gt; step of my for loop.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-3&#x2F;watch-i.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With &lt;code&gt;i&lt;&#x2F;code&gt; incremented, the program checks to see if another iteration of the loop should be ran by comparing &lt;code&gt;i&lt;&#x2F;code&gt; to &lt;code&gt;9&lt;&#x2F;code&gt;. When this comparison happens various &lt;a href=&quot;https:&#x2F;&#x2F;en.wikibooks.org&#x2F;wiki&#x2F;X86_Assembly&#x2F;X86_Architecture#EFLAGS_Register&quot;&gt;CPU flags&lt;&#x2F;a&gt; are modified in the process. If we check the flags before and after the &lt;code&gt;cmp&lt;&#x2F;code&gt;, we can see what’s getting changed.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-3&#x2F;flags.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;cmp&lt;&#x2F;code&gt; operation will substitute &lt;code&gt;i&lt;&#x2F;code&gt; by &lt;code&gt;9&lt;&#x2F;code&gt; to see what the difference between them is. In this case the operation was &lt;code&gt;1 - 9&lt;&#x2F;code&gt;, so &lt;code&gt;-8&lt;&#x2F;code&gt;. The flags that &lt;code&gt;jle&lt;&#x2F;code&gt; will look for is the zero flag &lt;code&gt;ZF&lt;&#x2F;code&gt; that indicates the result is &lt;code&gt;0&lt;&#x2F;code&gt; (and the numbers compared are equal) or the sign flag &lt;code&gt;SF&lt;&#x2F;code&gt; that indicates the result of the operations resulted in a negative number. With how this program is written, &lt;code&gt;jle&lt;&#x2F;code&gt; will always jump the program to &lt;code&gt;&amp;lt;main+24&amp;gt;&lt;&#x2F;code&gt; until &lt;code&gt;i&lt;&#x2F;code&gt; is &lt;code&gt;10&lt;&#x2F;code&gt;, as &lt;code&gt;cmp&lt;&#x2F;code&gt; will have cleared both the zero and sign flags at that point so no jump will be taken.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;calling-function&quot;&gt;Calling Function&lt;&#x2F;h1&gt;
&lt;p&gt;Inside of this loop, not too much is going on. The body of the loop is setting up some parameters to be passed to the &lt;code&gt;printf()&lt;&#x2F;code&gt; function, then calling it.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;0x000000000040057e &amp;lt;+24&amp;gt;: mov eax,DWORD PTR [rbp-0x4] 
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000400581 &amp;lt;+27&amp;gt;: mov esi,eax 
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000400583 &amp;lt;+29&amp;gt;: mov edi,0x40064a 
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000400588 &amp;lt;+34&amp;gt;: mov eax,0x0 
&lt;&#x2F;span&gt;&lt;span&gt;0x000000000040058d &amp;lt;+39&amp;gt;: call 0x400440 &amp;lt;printf@plt&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So I’ll set a new breakpoint at &lt;code&gt;*0x40057e&lt;&#x2F;code&gt; to watch more closely what’s going on at that point. I’ll use &lt;code&gt;nexti&lt;&#x2F;code&gt; and &lt;code&gt;i r &amp;lt;registers&amp;gt;&lt;&#x2F;code&gt; to monitor the register changes during these operations.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-3&#x2F;moar-mov.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So generally it looks like &lt;code&gt;eax&lt;&#x2F;code&gt; was used to grab the value of &lt;code&gt;i&lt;&#x2F;code&gt;, then set &lt;code&gt;esi&lt;&#x2F;code&gt; to it. I’m not sure why this was done, but the compiler usually knows what it’s doing so I trust it’s for a good reason. The last &lt;code&gt;mov&lt;&#x2F;code&gt; sets &lt;code&gt;eax&lt;&#x2F;code&gt; to &lt;code&gt;0&lt;&#x2F;code&gt; so the value of &lt;code&gt;i&lt;&#x2F;code&gt; is only set in &lt;code&gt;eax&lt;&#x2F;code&gt; for usage in the next instruction. There’s also a &lt;code&gt;mov&lt;&#x2F;code&gt; here to set &lt;code&gt;edi&lt;&#x2F;code&gt; to &lt;code&gt;0x400440&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I referenced the &lt;a href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;Calling_Conventions#Cheat_Sheets&quot;&gt;calling convention&lt;&#x2F;a&gt; for System V x86_64 and see that &lt;code&gt;rdi&lt;&#x2F;code&gt; and &lt;code&gt;rsi&lt;&#x2F;code&gt; are the first and second registers used as parameters in a function call. Matching my C call to &lt;code&gt;printf&lt;&#x2F;code&gt;, &lt;code&gt;edi&lt;&#x2F;code&gt; is my string and &lt;code&gt;esi&lt;&#x2F;code&gt; is the &lt;code&gt;i&lt;&#x2F;code&gt; variables value. I can take a look at this string with &lt;code&gt;x&lt;&#x2F;code&gt; similar to how I inspected numbers before, but I’ll use &lt;code&gt;s&lt;&#x2F;code&gt; as the display type indicator.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-3&#x2F;strangs.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The last instruction of the loop body is simply the &lt;code&gt;call&lt;&#x2F;code&gt; to the address of the &lt;code&gt;printf&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;after-the-loop&quot;&gt;After the Loop&lt;&#x2F;h1&gt;
&lt;p&gt;After our for loop finally completes, there are just 6 more instructions to run before &lt;code&gt;main&lt;&#x2F;code&gt; is complete.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;0x000000000040059c &amp;lt;+54&amp;gt;:	mov    rax,QWORD PTR [rip+0x200a9d] # 0x601040
&lt;&#x2F;span&gt;&lt;span&gt;0x00000000004005a3 &amp;lt;+61&amp;gt;:	mov    rdi,rax
&lt;&#x2F;span&gt;&lt;span&gt;0x00000000004005a6 &amp;lt;+64&amp;gt;:	call   0x400430 &amp;lt;puts@plt&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;0x00000000004005ab &amp;lt;+69&amp;gt;:	mov    eax,0x0
&lt;&#x2F;span&gt;&lt;span&gt;0x00000000004005b0 &amp;lt;+74&amp;gt;:	leave  
&lt;&#x2F;span&gt;&lt;span&gt;0x00000000004005b1 &amp;lt;+75&amp;gt;:	ret
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I hope you’re used to what &lt;code&gt;mov&lt;&#x2F;code&gt; does by now, though in this case it’s doing another offset with an important register I haven’t yet touched on, &lt;code&gt;rip&lt;&#x2F;code&gt;. This is the Instruction Pointer, this register keeps holds the value of the next instruction the CPU should execute.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-3&#x2F;rip.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Here I restarted my debugger and set a breakpoint at the &lt;code&gt;&amp;lt;main+54&amp;gt;&lt;&#x2F;code&gt; instruction. I can confirm that’s the next instruction to run because &lt;code&gt;rip&lt;&#x2F;code&gt; is set to that same address. It’s interesting that the compiler decided to use an offset from a register that’s always going to be the same value, GDB even noticed this and tells me the final address will be &lt;code&gt;0x602040&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-3&#x2F;puts.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Just as before, this is pulling up a string (character pointer) that’ll be used in the &lt;code&gt;puts&lt;&#x2F;code&gt; function call. This pointer has a much different address because of how I defined it in my program. This one was the global variable &lt;code&gt;done&lt;&#x2F;code&gt;, as opposed to my &lt;code&gt;Iteration %s\n&lt;&#x2F;code&gt; string which was a variable in the function scope of &lt;code&gt;main&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The last &lt;code&gt;mov&lt;&#x2F;code&gt; of the program is setting &lt;code&gt;eax&lt;&#x2F;code&gt; to &lt;code&gt;0&lt;&#x2F;code&gt;, this register is used as the return value of a function call and I wrote &lt;code&gt;main&lt;&#x2F;code&gt; to return &lt;code&gt;0&lt;&#x2F;code&gt;. The &lt;code&gt;leave&lt;&#x2F;code&gt; begins restoring the stack to as it was before &lt;code&gt;main&lt;&#x2F;code&gt; was called and &lt;code&gt;ret&lt;&#x2F;code&gt; completes the exit of my &lt;code&gt;main&lt;&#x2F;code&gt; function. Internally libc will hold onto the value &lt;code&gt;main&lt;&#x2F;code&gt; returned, do some cleanup then make a system call to &lt;code&gt;exit()&lt;&#x2F;code&gt; with that return value to notify the OS that the program has completed.&lt;&#x2F;p&gt;
&lt;p&gt;With all that I hope this provides a helpful introduction to GDB and some x86 assembly. GDB has a ton of features and this only scratches the surface but I wanted to share these basics as I’ll be using them in future posts when I dive more into advanced C topics. &lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Debugging C Programs with GDB – Part 2</title>
		<published>2018-02-28T00:00:00+00:00</published>
		<updated>2018-02-28T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/debugging-with-gdb-part-2/" type="text/html"/>
		<id>https://suchprogramming.com/debugging-with-gdb-part-2/</id>
		<content type="html">&lt;p&gt;In my &lt;a href=&quot;https:&#x2F;&#x2F;suchprogramming.com&#x2F;debugging-with-gdb-part-1&#x2F;&quot;&gt;previous post&lt;&#x2F;a&gt; I covered a few basics around building a C program for debugging, looking at the code listing and assembly of the program, setting breakpoints and peeking into the registers.&lt;&#x2F;p&gt;
&lt;p&gt;For Part 2, I’ll be using more gdb commands to explore the assembly code that’s involved in building up and initializing the stack frame at the start of a function.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;stacking-up&quot;&gt;Stacking Up&lt;&#x2F;h1&gt;
&lt;p&gt;I want to dig down into every bit of what this program is doing, I’ll begin by pulling up the disassembly version of the program once more.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;disambly-1.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If I use &lt;code&gt;break main&lt;&#x2F;code&gt; to set a breakpoint at the start of my &lt;code&gt;main&lt;&#x2F;code&gt; function, GDB will be practical and set this breakpoint at the address &lt;code&gt;0x400575&lt;&#x2F;code&gt;, 15 bytes into the assembled version of main. It’ll do this because the first 6 instructions of the function are setting up the stack for the function and normally you can trust that the compiler has done a good job handling that for you.&lt;&#x2F;p&gt;
&lt;p&gt;I want to really start at the beginning, so I’m going to instead set my breakpoint with a pointer to the first byte of the function with the command &lt;code&gt;break&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;break-first.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The first assembly instruction seen here is &lt;code&gt;push rbp&lt;&#x2F;code&gt;. This is pushing the value stored in the &lt;code&gt;rbp&lt;&#x2F;code&gt; (base pointer) register onto the stack, but where is the stack? The &lt;code&gt;rsp&lt;&#x2F;code&gt; (stack pointer) register tell us where the top of the stack currently is.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;stack-registers.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;These first 3 instructions are building a new &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Call_stack&quot;&gt;call stack frame&lt;&#x2F;a&gt;. Since this program uses libc the program starts by running code to initialize a few things then calls the &lt;code&gt;main&lt;&#x2F;code&gt; function that was created in the source code. The compiler adds these instructions to build out the stack frame for the given function.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;stack-diagram.png&quot; alt=&quot;The stack when printf is being called from main&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As mentioned before, the &lt;code&gt;rsp&lt;&#x2F;code&gt; register points to the points to the top of the stack. By running &lt;code&gt;nexti&lt;&#x2F;code&gt; (next instruction), I can execute one machine operation at a time. After letting &lt;code&gt;push rbp&lt;&#x2F;code&gt; run, the current value of &lt;code&gt;rbp&lt;&#x2F;code&gt; is added to the top of the stack. While this happens, the stack grows and &lt;code&gt;rsp&lt;&#x2F;code&gt; will be updated to the new top of the stack.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;top-of-stack.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The value of &lt;code&gt;rbp&lt;&#x2F;code&gt; hasn’t changed, but the value of &lt;code&gt;rsp&lt;&#x2F;code&gt; is 8 bytes smaller. In most CPU architectures the stack grows “down” like this, where the lowest call frame represents the current frame.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;peeking-into-memory&quot;&gt;Peeking Into Memory&lt;&#x2F;h1&gt;
&lt;p&gt;We should now be able to see the &lt;code&gt;rbp&lt;&#x2F;code&gt; value at the top of the frame. By using the &lt;code&gt;x&lt;&#x2F;code&gt; command to print data in memory, we can take a look at the frame. &lt;code&gt;x&lt;&#x2F;code&gt; has a few useful options, while you’re learning GDB they can be tricky to remember so I recommend keeping a &lt;a href=&quot;http:&#x2F;&#x2F;darkdust.net&#x2F;files&#x2F;GDB%20Cheat%20Sheet.pdf&quot;&gt;cheat sheet&lt;&#x2F;a&gt; handy until you’ve got it down.&lt;&#x2F;p&gt;
&lt;p&gt;To look at the value at the top of the stack, I’ll use &lt;code&gt;x&#x2F;1xg 0x7fffffffda10&lt;&#x2F;code&gt;. This is asking to see 1 unit of data, in he&lt;code&gt;x&lt;&#x2F;code&gt;adecimal format, considering 64-bit “&lt;code&gt;g&lt;&#x2F;code&gt;iant” words. You can choose instead to dereference the &lt;code&gt;rsp&lt;&#x2F;code&gt; register directly in the command by using &lt;code&gt;$rsp&lt;&#x2F;code&gt; for the address.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;dereference-rsp.png&quot; alt=&quot;The value of rbp is indeed at the top of the stack&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With the commands covered so far we can more easily inspect what each instruction is doing. The next instruction is &lt;code&gt;mov rbp, rsp&lt;&#x2F;code&gt;, this moves the value of &lt;code&gt;rsp&lt;&#x2F;code&gt; into &lt;code&gt;rbp&lt;&#x2F;code&gt;. This is to set the base pointer to the top of the previous stack frame which is used at the end of a function to restore the previous stack state.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;set-base.png&quot; alt=&quot;Now rbp and rsp are set to the same value&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The next instruction, &lt;code&gt;sub rsp,0x20&lt;&#x2F;code&gt;, lowers the value of &lt;code&gt;rsp&lt;&#x2F;code&gt; by 32 (4 64-bit words). This is the size of the frame being built for main. Using &lt;code&gt;x&lt;&#x2F;code&gt; again, I’ll look at the 4 words in the stack, plus the next word after (the &lt;code&gt;rbp&lt;&#x2F;code&gt; value that was pushed to the stack).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;main-frame.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There’s already data in this stack! This is data left over from previous execution and is considered uninitialized, as this function doesn’t know what data was been left behind here. When gcc warns that &lt;code&gt;&#x27;i&#x27; is used uninitialized in this function [-Wuninitialized]&lt;&#x2F;code&gt;, it is because the program is using a variable that’s not in a known state.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;pointers-irl&quot;&gt;Pointers IRL&lt;&#x2F;h1&gt;
&lt;p&gt;The next 3 instructions are going to be initializing the new stack frame region using pointers. If you’ve been confused about pointers in C, pointers in assembly might help demystify that concept for you.  Let’s consider the next 3 instructions:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;=&amp;gt; 0x000000000040056e &amp;lt;+8&amp;gt;:     mov    DWORD PTR [rbp-0x14],edi
&lt;&#x2F;span&gt;&lt;span&gt;   0x0000000000400571 &amp;lt;+11&amp;gt;:    mov    QWORD PTR [rbp-0x20],rsi
&lt;&#x2F;span&gt;&lt;span&gt;   0x0000000000400575 &amp;lt;+15&amp;gt;:    mov    DWORD PTR [rbp-0x4],0x0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These are assignment operators that refer to addresses relative to a memory address that is stored in a register. In this case they are modifying the data in the new stack frame (addresses below &lt;code&gt;rbp&lt;&#x2F;code&gt;). Sometime before &lt;code&gt;main&lt;&#x2F;code&gt; was called, the &lt;code&gt;edi&lt;&#x2F;code&gt; and &lt;code&gt;rsi&lt;&#x2F;code&gt; registers were set to some values that the compiler wants kept around.&lt;&#x2F;p&gt;
&lt;p&gt;The first instruction here is setting a 32-bit (Double Word) at the memory address 20 bytes below &lt;code&gt;rbp&lt;&#x2F;code&gt;. I’ll need to give &lt;code&gt;x&lt;&#x2F;code&gt; a&lt;code&gt; w&lt;&#x2F;code&gt; parameter so that it’ll know I’m now looking for a 32-bit word.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;20-below.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Basically the same thing going on for the next instruction, but setting the memory address 32 bytes below the base pointer to the 64-bit value currently in &lt;code&gt;rsi&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;32-below.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;These registers (&lt;code&gt;*di&lt;&#x2F;code&gt;, &lt;code&gt;*si&lt;&#x2F;code&gt;) are &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;1856320&#x2F;purpose-of-esi-edi-registers&quot;&gt;index registers&lt;&#x2F;a&gt; that can be used for various string operations. They are also part of the &lt;a href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;Calling_Conventions#Cheat_Sheets&quot;&gt;x86_64 calling convention&lt;&#x2F;a&gt; to be used when calling functions. In this case, these registers contain the parameters for &lt;code&gt;main&lt;&#x2F;code&gt;, my &lt;code&gt;argc&lt;&#x2F;code&gt; and &lt;code&gt;argv&lt;&#x2F;code&gt; variables.&lt;&#x2F;p&gt;
&lt;p&gt;We can use the &lt;code&gt;print&lt;&#x2F;code&gt; command to look at the variables as they were named in the source code. The &lt;code&gt;&amp;amp;&lt;&#x2F;code&gt; operator can be used similar to how it used in C to verify that these variables are stored at those locations in the stack.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-2&#x2F;all-the-pointers.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The next instruction, &lt;code&gt;mov DWORD PTR [rbp-0x4],0x0&lt;&#x2F;code&gt;, is the &lt;code&gt;i&lt;&#x2F;code&gt; variable used within the &lt;code&gt;for&lt;&#x2F;code&gt; loop inside of main, here being initialized to the value we specified, &lt;code&gt;0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;At this point the stack for main is initialized and &lt;code&gt;main&lt;&#x2F;code&gt; is ready to do its thing. In the next post I’ll continue the GDB exploration to investigate the rest of what this function is up to!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Debugging C Programs with GDB – Part 1</title>
		<published>2018-02-21T00:00:00+00:00</published>
		<updated>2018-02-21T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/debugging-with-gdb-part-1/" type="text/html"/>
		<id>https://suchprogramming.com/debugging-with-gdb-part-1/</id>
		<content type="html">&lt;p&gt;When you write C code, you’re playing with power! You’re bound to let this power go to your head and shoot yourself in the foot here and there. At some point(s) your program is going to do something that just doesn’t quite make sense.&lt;&#x2F;p&gt;
&lt;p&gt;The bad news is that your program doesn’t make any sense because you’ve written flaws into it. That’s fine, you’ve either written janky C programs, or not written any C. The good news is that GDB is here to help us learn from our mistakes!&lt;&#x2F;p&gt;
&lt;p&gt;Through the next few posts I’ll share some tips on basic GDB usage, explore a bit of history and dig more into how the C programs on my machine are actually working.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;building-for-debugging&quot;&gt;Building for Debugging&lt;&#x2F;h1&gt;
&lt;p&gt;To kick things off, I’m going to just slap together a quick C program and a Makefile to assist in building it and running my debugger.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; test.c
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*done = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Done!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;; i++) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Iteration &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, i);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, done);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This program has a simple for loop and a few print statements and I’ll use GDB to inspect what it’s doing a bit more. To provide more information to the debugger about this program I’ll use the &lt;code&gt;-g&lt;&#x2F;code&gt; flag when building it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Makefile
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CC&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;gcc -g -o &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; -Wall &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;test
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;test.c
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;CC&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;debug&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;test
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gdb -q&lt;&#x2F;span&gt;&lt;span&gt; .&#x2F;test
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For maximum laziness, I added a &lt;code&gt;debug&lt;&#x2F;code&gt; target to my &lt;code&gt;Makefile&lt;&#x2F;code&gt; here so that I can use &lt;code&gt;make debug&lt;&#x2F;code&gt; to jump right it. I gave &lt;code&gt;gdb&lt;&#x2F;code&gt; the &lt;code&gt;-q&lt;&#x2F;code&gt; option to quiet down since it normally has a lot to say on startup.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;make-debug.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;That’s about all I need to get my program ready for debugging!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;basic-commands&quot;&gt;Basic Commands&lt;&#x2F;h1&gt;
&lt;p&gt;Now we get to the hard part. GDB has a bajillion features so getting started can be daunting. Probably one of the best commands to learn first is the &lt;code&gt;run&lt;&#x2F;code&gt; command, as so far the program has been looked at a little bit, but isn’t actually running at the moment.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;gdb-run.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;You can also provide arguments to the program by providing arguments to run. This program doesn’t care about arguments, but don’t let that stop you from giving it some anyway!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;gdb-runargs.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The excitement of just running a program in GDB is very short lived, I want to be able to stop the program somewhere and poke around a bit. The &lt;code&gt;list&lt;&#x2F;code&gt; command can spit out a listing of the program.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;gdb-list.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Initially &lt;code&gt;gdb&lt;&#x2F;code&gt; will show the first 10 lines of the source. You could run &lt;code&gt;list&lt;&#x2F;code&gt; again to see the next 10 lines but GDB has a friendly feature where hitting &lt;code&gt;enter&lt;&#x2F;code&gt; will automatically rerun your last command, so I used that to continue reading the full source.&lt;&#x2F;p&gt;
&lt;p&gt;Looking at this listing, I think a good place to pause and look around would be at the &lt;code&gt;printf()&lt;&#x2F;code&gt; call within my &lt;code&gt;for&lt;&#x2F;code&gt; loop. To have GDB stop here I’ll use the &lt;code&gt;break&lt;&#x2F;code&gt; command and I’ll give it the argument &lt;code&gt;10&lt;&#x2F;code&gt; to indicate I’d like to set a breakpoint at line 10.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;break-10.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now when I give it a run, it’ll stop the program when it hits that line.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;run-break.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;To resume the program, until the next breakpoint is hit, you can use the &lt;code&gt;continue&lt;&#x2F;code&gt; command. Another little time-saver trick with gdb is that many commands have shortcuts, such as &lt;code&gt;c&lt;&#x2F;code&gt; for &lt;code&gt;continue&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;gdb-continue.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;peeking-into-the-code&quot;&gt;Peeking Into The Code&lt;&#x2F;h1&gt;
&lt;p&gt;The ability to set breakpoints and resume execution is a good start, but even better is getting a look around at this point in time to glean more about what the program is doing. It’s time to start looking beyond the C code and see what the program is actually doing in assembly, the state of the CPU in the context of our program and what’s going on in memory.&lt;&#x2F;p&gt;
&lt;p&gt;First let’s look at the assembly version of the &lt;code&gt;main&lt;&#x2F;code&gt; function. I’ll use the &lt;code&gt;disassemble&lt;&#x2F;code&gt; command for that, and I’ll tell it that &lt;code&gt;main&lt;&#x2F;code&gt; is what I’m interested in disassembling.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;disassemble.png&quot; alt=&quot;Oh noes! Assmebly!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Assembly code get’s a bad rep, but it’s not as bad as people think it is. You might not want to write a large application in assembly, and that’s reasonable, but if you want to be a strong C programmer you need to know enough assembly to figure out what your program is up to.&lt;&#x2F;p&gt;
&lt;p&gt;x86_64 assembly has two different syntaxes to choose from, AT&amp;amp;T syntax and Intel syntax. They both work just fine but GDB defaults to AT&amp;amp;T syntax and I prefer the Intel syntax so I’ll use the command &lt;code&gt;set disassembly-flavor intel&lt;&#x2F;code&gt; to get it to my liking.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;intel-asm.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;That looks better! Now let’s briefly look at a few things. Looks like my &lt;code&gt;main&lt;&#x2F;code&gt; function is 21 instructions long, alright… a smidge more than half of the operations are &lt;code&gt;mov&lt;&#x2F;code&gt; (move) instructions and I see a few branching operations, &lt;code&gt;jmp&lt;&#x2F;code&gt; (jump), &lt;code&gt;call&lt;&#x2F;code&gt; (call a subroutine), &lt;code&gt;jle&lt;&#x2F;code&gt; (jump if less than or equal to) and &lt;code&gt;ret&lt;&#x2F;code&gt; (return from subroutine).&lt;&#x2F;p&gt;
&lt;p&gt;One thing I find interesting is the instruction at offset &lt;code&gt;&amp;lt;+64&amp;gt;&lt;&#x2F;code&gt;, &lt;code&gt;call 0x400430 &amp;lt;puts@plt&amp;gt;&lt;&#x2F;code&gt;. I did not use the &lt;code&gt;puts()&lt;&#x2F;code&gt; function in my code! The compiler caught on that my last &lt;code&gt;printf()&lt;&#x2F;code&gt; statement doesn’t need to be a format string and optimized the result a little bit.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s get back to inspecting what this program is up to, I’m currently still in the middle of my paused program, and I’m at the very start of one of my loop iterations. In this disassembly output I can see I’m at offset &lt;code&gt;&amp;lt;+24&amp;gt;&lt;&#x2F;code&gt;, as indicated by the little &lt;code&gt;=&amp;gt;&lt;&#x2F;code&gt; arrow, this is the next instruction the program will run.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;mov&lt;&#x2F;code&gt; instruction moves a value from one place to another, similar to the assignment operator &lt;code&gt;=&lt;&#x2F;code&gt; in most programming languages. In this case the full instruction is &lt;code&gt;mov eax,DWORD PTR [rbp-0x4]&lt;&#x2F;code&gt; which is basically &lt;code&gt;eax = DWORD PTR [rbp - 0x4]&lt;&#x2F;code&gt;. Ignoring the right side of that for now, we’re assigning a value to something called &lt;code&gt;eax&lt;&#x2F;code&gt;. This &lt;code&gt;eax&lt;&#x2F;code&gt; thing is a CPU register, which is basically a variable in the hardware of the CPU. We can look at all the registers with the &lt;code&gt;info&lt;&#x2F;code&gt; command by saying &lt;code&gt;info registers&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;registers.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Okay so there are a bunch of registers, and &lt;code&gt;eax&lt;&#x2F;code&gt; is not one of them… GREAT! This is because the x86 architecture has been through a lot, way back in the day (early 70s) Intel released their &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Intel_8008&quot;&gt;8008 CPU&lt;&#x2F;a&gt; that had some 8-bit registers with names like &lt;code&gt;A&lt;&#x2F;code&gt; (for Accumulator).&lt;&#x2F;p&gt;
&lt;p&gt;When Intel got to the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Intel_8086&quot;&gt;8086&lt;&#x2F;a&gt; in the late 70s they made the &lt;code&gt;A&lt;&#x2F;code&gt; register twice the size (16-bits) and started calling it the &lt;code&gt;AX&lt;&#x2F;code&gt; register. To help with software compatibility with older system the &lt;code&gt;AX&lt;&#x2F;code&gt; register could be used as an 8-bit register with &lt;code&gt;AH&lt;&#x2F;code&gt; representing the higher 8 bits and &lt;code&gt;AL&lt;&#x2F;code&gt; the lower 8 bits.&lt;&#x2F;p&gt;
&lt;p&gt;Then the mid-80s showed up and Intel was like MOAR BITS and released their &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Intel_80386&quot;&gt;80386&lt;&#x2F;a&gt; that had 32-bit registers, now they refer to the &lt;code&gt;A&lt;&#x2F;code&gt; register as &lt;code&gt;EAX&lt;&#x2F;code&gt; (there’s our guy!), again preserving backward compatibility by allowing the 16 and 8 bit registers to remain the same. Now-a-days our 64-bit processors are king, so we have the 64-bit register &lt;code&gt;RAX&lt;&#x2F;code&gt;, but can still use &lt;code&gt;EAX&lt;&#x2F;code&gt;, &lt;code&gt;AX&lt;&#x2F;code&gt;, &lt;code&gt;AH&lt;&#x2F;code&gt;, and &lt;code&gt;AL&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;All that history lesson to give full context on why &lt;code&gt;mov eax, &amp;lt;stuff&amp;gt;&lt;&#x2F;code&gt; is going to modify our &lt;code&gt;rax&lt;&#x2F;code&gt; register!&lt;&#x2F;p&gt;
&lt;p&gt;Now, to run just that one instruction, I’ll use the &lt;code&gt;nexti&lt;&#x2F;code&gt; command. I’ll then check the registers again with the shorthand version of &lt;code&gt;info registers&lt;&#x2F;code&gt; and just look at the &lt;code&gt;eax&lt;&#x2F;code&gt; register: &lt;code&gt;i r eax&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;i-r-eax.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If I &lt;code&gt;continue&lt;&#x2F;code&gt; my program, I’ll notice that this number correlates with something in my program.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;debugging-with-gdb-part-1&#x2F;iteration-4.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;eax&lt;&#x2F;code&gt; register is getting set to the &lt;code&gt;i&lt;&#x2F;code&gt; value I’m setting during my for loop!&lt;&#x2F;p&gt;
&lt;p&gt;In the next post I’ll continue digging into this program and discover more about the disassembled version of my C program and show off some more GDB commands along the way!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Exploring ELF</title>
		<published>2018-02-14T00:00:00+00:00</published>
		<updated>2018-02-14T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/exploring-elf/" type="text/html"/>
		<id>https://suchprogramming.com/exploring-elf/</id>
		<content type="html">&lt;p&gt;Hello there! This post is going to be about the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Executable_and_Linkable_Format&quot;&gt;Executable and Linkable Format (ELF)&lt;&#x2F;a&gt;.  This is one of the most important binary formats out there and is designed to store &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Object_code&quot;&gt;object code&lt;&#x2F;a&gt; in a way that can be used on a wide variety of processor types and operating systems. It’s the most popular format used to contain compiled programs and libraries.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s take a file apart and look at what it’s made of!&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;elf-header-of-a-c-program&quot;&gt;ELF Header of a C Program&lt;&#x2F;h1&gt;
&lt;p&gt;To kick things off I will build a very small C program, &lt;code&gt;test.c&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I’ll give it a quick and lazy build with &lt;code&gt;make test&lt;&#x2F;code&gt; and look at the first 64 bytes of it with &lt;code&gt;hexdump&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;exploring-elf&#x2F;elf-hexdump.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We can see right away this is an ELF file, the first 4 bytes are &lt;code&gt;&amp;quot;\x7fELF&amp;quot;&lt;&#x2F;code&gt;.  The ELF format supports 32-bit and 64-bit machines. This header will be 52 bytes long on a 32-bit machine and 64 bytes long on a 64-bit machine to support the longer memory addresses. For this post I’ll only be looking at 64-bit files.&lt;&#x2F;p&gt;
&lt;p&gt;Here’s the structure of the 64-bit header from the &lt;a href=&quot;https:&#x2F;&#x2F;www.uclibc.org&#x2F;docs&#x2F;elf-64-gen.pdf&quot;&gt;ELF-64 documentation&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef struct &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned char&lt;&#x2F;span&gt;&lt;span&gt; e_ident[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;16&lt;&#x2F;span&gt;&lt;span&gt;]; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  ELF  identification  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Half e_type;         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Object  file  type  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Half e_machine;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Machine  type  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Word e_version;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Object  file  version  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Addr e_entry;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Entry  point  address  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Off e_phoff;         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Program  header  offset  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Off e_shoff;         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Section  header  offset  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Word e_flags;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Processor-specific  flags  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Half e_ehsize;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  ELF  header  size  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Half e_phentsize;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Size  of  program  header  entry  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Half e_phnum;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Number  of  program  header  entries  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Half e_shentsize;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Size  of  section  header  entry  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Half e_shnum;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Number  of  section  header  entries  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf64_Half e_shstrndx;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;*  Section  name  string  table  index  *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;} Elf64_Ehdr
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A visual representation of this structure:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;exploring-elf&#x2F;elf-header.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;e_ident&lt;&#x2F;code&gt; field contains all the information needed to understand what format and version of an ELF file this is. As I mentioned before, the first 4 bytes of this should be &lt;code&gt;\x7f&lt;&#x2F;code&gt;, &lt;code&gt;E&lt;&#x2F;code&gt;, &lt;code&gt;L&lt;&#x2F;code&gt;, &lt;code&gt;F&lt;&#x2F;code&gt;, the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Magic_number_(programming)&quot;&gt;magic number&lt;&#x2F;a&gt; for all ELF files. The remaining bytes of the &lt;code&gt;e_ident&lt;&#x2F;code&gt; field will indicate if the file is 32 or 64-bit, if the file is in little &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Endianness&quot;&gt;endian&lt;&#x2F;a&gt; or big endian format and a few other details about what the file was built for.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;e_type&lt;&#x2F;code&gt; field indicates what type of data is represented in the file, if it’s a object (&lt;code&gt;1&lt;&#x2F;code&gt;), executable (&lt;code&gt;2&lt;&#x2F;code&gt;), shared object (&lt;code&gt;3&lt;&#x2F;code&gt;) or core file (&lt;code&gt;4&lt;&#x2F;code&gt;). The &lt;code&gt;e_machine&lt;&#x2F;code&gt; field represents what &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Instruction_set_architecture&quot;&gt;instruction set&lt;&#x2F;a&gt; the file is in. &lt;code&gt;e_version&lt;&#x2F;code&gt; is always &lt;code&gt;1&lt;&#x2F;code&gt; for now.&lt;&#x2F;p&gt;
&lt;p&gt;Up this this point the header is the same for 32-bit and 64-bit machines. The next three fields &lt;code&gt;e_entry&lt;&#x2F;code&gt; (program entry point), &lt;code&gt;e_phoff&lt;&#x2F;code&gt; (program header offset) and &lt;code&gt;e_shoff&lt;&#x2F;code&gt; (section header offset) are used to refer to memory addresses so are twice as long on a 64-bit machine. The &lt;code&gt;e_entry&lt;&#x2F;code&gt; field is a pointer to the start of program in virtual memory. If you’re unfamiliar with what virtual memory is I suggest &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=2quKyPnUShQ&quot;&gt;this video&lt;&#x2F;a&gt; from &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;channel&#x2F;UCgyqtNWZmIxTx3b6OxTSALw&quot;&gt;Android Authority’s YouTube channel&lt;&#x2F;a&gt;. The &lt;code&gt;e_phoff&lt;&#x2F;code&gt; and &lt;code&gt;e_shoff&lt;&#x2F;code&gt; indicate the offsets within the ELF file to the program and section headers tables.&lt;&#x2F;p&gt;
&lt;p&gt;The rest of the header contains some flags, counts and sizes relating to the program and section headers in the file, I suggest checking out the elf header &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Executable_and_Linkable_Format#File_header&quot;&gt;documentation on Wikipedia&lt;&#x2F;a&gt; for more detail on the header.&lt;&#x2F;p&gt;
&lt;p&gt;We can easily see all of this header detail in a human readable way with the &lt;code&gt;readelf&lt;&#x2F;code&gt; program.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;exploring-elf&#x2F;readelf.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;a-simpler-elf-file&quot;&gt;A Simpler ELF File&lt;&#x2F;h1&gt;
&lt;p&gt;I want to dive a bit deeper into the file, but even an empty C program like this contains a ton of data compared to a small &lt;code&gt;Hello, world!&lt;&#x2F;code&gt; program written in assembly. Here’s my hello world assembly source for a x86_64 Linux machine:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;section &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.text
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;global &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_start
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;_start:
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  ; write(1, msg, len)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdi&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rsi&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;msg
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdx&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;len
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;syscall
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  ; exit(0)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rax&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;60
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mov &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rdi&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;syscall
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;section &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;.data
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;msg &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;db &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;quot;Hello, world!&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0xa
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;len &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;equ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;$ &lt;&#x2F;span&gt;&lt;span&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;msg
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I won’t dive into the syntax of this assembly, but it makes one call to Linux to write &lt;code&gt;Hello, world!\n&lt;&#x2F;code&gt; to stdout (&lt;code&gt;1&lt;&#x2F;code&gt;) and  a second call to exit the program with &lt;code&gt;0&lt;&#x2F;code&gt;. I’ll assemble it into an elf64 object file with &lt;code&gt;nasm -f elf64 hello.asm&lt;&#x2F;code&gt; and link it into an executable with &lt;code&gt;ld -s -o hello hello.o&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;exploring-elf&#x2F;asm-build.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This file is a lot smaller than the C program, 512 bytes vs 8552. There is also a lot less data in the ELF program and sections headers as indicated by the  header.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;exploring-elf&#x2F;asm-readelf.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There are 2 program headers instead of 9 and 4 sections headers instead of 31, so much less to look at!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;elf-sections&quot;&gt;ELF Sections&lt;&#x2F;h1&gt;
&lt;p&gt;Let’s look at more of what &lt;code&gt;readelf&lt;&#x2F;code&gt; can tell us about this ELF file, starting with closer inspection of the sections headers.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ readelf -a hello
&lt;&#x2F;span&gt;&lt;span&gt;ELF Header:
&lt;&#x2F;span&gt;&lt;span&gt;  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
&lt;&#x2F;span&gt;&lt;span&gt;  Class:                             ELF64
&lt;&#x2F;span&gt;&lt;span&gt;  Data:                              2&amp;#39;s complement, little endian
&lt;&#x2F;span&gt;&lt;span&gt;  Version:                           1 (current)
&lt;&#x2F;span&gt;&lt;span&gt;  OS&#x2F;ABI:                            UNIX - System V
&lt;&#x2F;span&gt;&lt;span&gt;  ABI Version:                       0
&lt;&#x2F;span&gt;&lt;span&gt;  Type:                              EXEC (Executable file)
&lt;&#x2F;span&gt;&lt;span&gt;  Machine:                           Advanced Micro Devices X86-64
&lt;&#x2F;span&gt;&lt;span&gt;  Version:                           0x1
&lt;&#x2F;span&gt;&lt;span&gt;  Entry point address:               0x4000b0
&lt;&#x2F;span&gt;&lt;span&gt;  Start of program headers:          64 (bytes into file)
&lt;&#x2F;span&gt;&lt;span&gt;  Start of section headers:          256 (bytes into file)
&lt;&#x2F;span&gt;&lt;span&gt;  Flags:                             0x0
&lt;&#x2F;span&gt;&lt;span&gt;  Size of this header:               64 (bytes)
&lt;&#x2F;span&gt;&lt;span&gt;  Size of program headers:           56 (bytes)
&lt;&#x2F;span&gt;&lt;span&gt;  Number of program headers:         2
&lt;&#x2F;span&gt;&lt;span&gt;  Size of section headers:           64 (bytes)
&lt;&#x2F;span&gt;&lt;span&gt;  Number of section headers:         4
&lt;&#x2F;span&gt;&lt;span&gt;  Section header string table index: 3
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Section Headers:
&lt;&#x2F;span&gt;&lt;span&gt;  [Nr] Name              Type             Address           Offset
&lt;&#x2F;span&gt;&lt;span&gt;       Size              EntSize          Flags  Link  Info  Align
&lt;&#x2F;span&gt;&lt;span&gt;  [ 0]                   NULL             0000000000000000  00000000
&lt;&#x2F;span&gt;&lt;span&gt;       0000000000000000  0000000000000000           0     0     0
&lt;&#x2F;span&gt;&lt;span&gt;  [ 1] .text             PROGBITS         00000000004000b0  000000b0
&lt;&#x2F;span&gt;&lt;span&gt;       0000000000000027  0000000000000000  AX       0     0     16
&lt;&#x2F;span&gt;&lt;span&gt;  [ 2] .data             PROGBITS         00000000006000d8  000000d8
&lt;&#x2F;span&gt;&lt;span&gt;       000000000000000e  0000000000000000  WA       0     0     4
&lt;&#x2F;span&gt;&lt;span&gt;  [ 3] .shstrtab         STRTAB           0000000000000000  000000e6
&lt;&#x2F;span&gt;&lt;span&gt;       0000000000000017  0000000000000000           0     0     1
&lt;&#x2F;span&gt;&lt;span&gt;Key to Flags:
&lt;&#x2F;span&gt;&lt;span&gt;  W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
&lt;&#x2F;span&gt;&lt;span&gt;  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
&lt;&#x2F;span&gt;&lt;span&gt;  O (extra OS processing required) o (OS specific), p (processor specific)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;There are no section groups in this file.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Program Headers:
&lt;&#x2F;span&gt;&lt;span&gt;  Type           Offset             VirtAddr           PhysAddr
&lt;&#x2F;span&gt;&lt;span&gt;                 FileSiz            MemSiz              Flags  Align
&lt;&#x2F;span&gt;&lt;span&gt;  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
&lt;&#x2F;span&gt;&lt;span&gt;                 0x00000000000000d7 0x00000000000000d7  R E    200000
&lt;&#x2F;span&gt;&lt;span&gt;  LOAD           0x00000000000000d8 0x00000000006000d8 0x00000000006000d8
&lt;&#x2F;span&gt;&lt;span&gt;                 0x000000000000000e 0x000000000000000e  RW     200000
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt; Section to Segment mapping:
&lt;&#x2F;span&gt;&lt;span&gt;  Segment Sections...
&lt;&#x2F;span&gt;&lt;span&gt;   00     .text 
&lt;&#x2F;span&gt;&lt;span&gt;   01     .data 
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;There is no dynamic section in this file.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;There are no relocations in this file.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;No version information found in this file.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Looking first at the section table, there are 4 section entries. The sections headers have the following structure:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef struct &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  uint32_t   sh_name;
&lt;&#x2F;span&gt;&lt;span&gt;  uint32_t   sh_type;
&lt;&#x2F;span&gt;&lt;span&gt;  uint32_t   sh_flags;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf32_Addr sh_addr;
&lt;&#x2F;span&gt;&lt;span&gt;  Elf32_Off  sh_offset;
&lt;&#x2F;span&gt;&lt;span&gt;  uint32_t   sh_size;
&lt;&#x2F;span&gt;&lt;span&gt;  uint32_t   sh_link;
&lt;&#x2F;span&gt;&lt;span&gt;  uint32_t   sh_info;
&lt;&#x2F;span&gt;&lt;span&gt;  uint32_t   sh_addralign;
&lt;&#x2F;span&gt;&lt;span&gt;  uint32_t   sh_entsize;
&lt;&#x2F;span&gt;&lt;span&gt;} Elf32_Shdr;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Precise detail on this structure, and what the fields are for can be found pretty easily in the &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;5&#x2F;elf&quot;&gt;Linux man page for elf&lt;&#x2F;a&gt;. Going on the output of &lt;code&gt;readelf&lt;&#x2F;code&gt;, the first section is is a &lt;code&gt;NULL&lt;&#x2F;code&gt; section. I don’t quite understand &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;26812142&#x2F;what-is-the-use-of-the-sht-null-section-in-elf&quot;&gt;why this first section is empty&lt;&#x2F;a&gt;, but it is part of the ELF standard.&lt;&#x2F;p&gt;
&lt;p&gt;The next two sections are &lt;code&gt;.text&lt;&#x2F;code&gt; and &lt;code&gt;.data&lt;&#x2F;code&gt;, which were defined in the assembly code. These are both of type &lt;code&gt;PROGBITS&lt;&#x2F;code&gt; which means all the data in the section is defined and formatted for the programs own usage, it is a chunk of arbitrary bytes as far as ELF is concerned.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;address&lt;&#x2F;code&gt; listed is where the section should be loaded in the program’s virtual memory space when the program is loaded by the operating system, if that section should be loaded into memory space of the process. The &lt;code&gt;offset&lt;&#x2F;code&gt; indicates how many bytes into the ELF file is the start of this section and &lt;code&gt;size&lt;&#x2F;code&gt; is the length of that section. So we can see here that &lt;code&gt;.text&lt;&#x2F;code&gt; will be loaded at &lt;code&gt;0x4000b0&lt;&#x2F;code&gt; for the process, which is &lt;code&gt;0xb0&lt;&#x2F;code&gt; bytes into the ELF binary file on the filesystem and that chunk of data is &lt;code&gt;0x27&lt;&#x2F;code&gt; bytes long.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;flags&lt;&#x2F;code&gt; tells us a few other bits about the section, both &lt;code&gt;.text&lt;&#x2F;code&gt; and &lt;code&gt;.data&lt;&#x2F;code&gt; have the allocate flag, so it should reside in the programs memory space when the program is executed. The &lt;code&gt;.text&lt;&#x2F;code&gt; section is the only section marked as executable, so the system should only allow code execution to occur within that section. The &lt;code&gt;.data&lt;&#x2F;code&gt; section is the only section with the writable flag, so the memory in that range can be modified by the running program.&lt;&#x2F;p&gt;
&lt;p&gt;The program headers are pretty similar to the section headers, they describe information the system needs to run the program. They describe program segments and generally point to the same regions of memory and the binary file as the sections do, but with some detail that’s specific to executable and shared objects. I’ll defer again to &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;5&#x2F;elf&quot;&gt;the manual&lt;&#x2F;a&gt; for all the raw details.&lt;&#x2F;p&gt;
&lt;p&gt;That covers my introduction to the ELF format. Hopefully this provides good enough context to explore a bit more on your own. I suggest trying to see if you can build an ELF file from scratch or modify one that is already built for some binary hacking shenanigans!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Barebones Linux ISO</title>
		<published>2018-02-07T00:00:00+00:00</published>
		<updated>2018-02-07T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/barebones-linux-iso/" type="text/html"/>
		<id>https://suchprogramming.com/barebones-linux-iso/</id>
		<content type="html">&lt;p&gt;My post on &lt;a href=&quot;https:&#x2F;&#x2F;suchprogramming.com&#x2F;barebones-linux-system&#x2F;&quot;&gt;Building a Barebones Linux System&lt;&#x2F;a&gt; has been the most popular post on my blog so far. Back when my blog had comments, a reader left a comment asking how the system described in that post could be loaded onto an ISO file along with GRUB to make it into a bootable image. I think that’s a pretty good follow-up so today I will share the process on how to do just that!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;continuing-the-barebones-journey&quot;&gt;Continuing the Barebones Journey&lt;&#x2F;h1&gt;
&lt;p&gt;It’s been a bit since I posted the original Barebones Linux post and in &lt;a href=&quot;https:&#x2F;&#x2F;suchprogramming.com&#x2F;getting-busybox-with-it&#x2F;&quot;&gt;Getting Busybox With It&lt;&#x2F;a&gt; I added BusyBox to the system and structured my build into a &lt;code&gt;Makefile&lt;&#x2F;code&gt; to preserve that effort (&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;KennethWilke&#x2F;my-barebones-linux&quot;&gt;available on my Github&lt;&#x2F;a&gt;). That is what I’ll use as the starting point for this post.&lt;&#x2F;p&gt;
&lt;p&gt;Back then I used the Linux Kernel version &lt;code&gt;4.13.3&lt;&#x2F;code&gt; and BusyBox version &lt;code&gt;1.27.2&lt;&#x2F;code&gt;, while I’m pulling this up again I’ll change a few of my &lt;code&gt;Makefile&lt;&#x2F;code&gt; variables to use the latest versions for today, Linux &lt;code&gt;4.15&lt;&#x2F;code&gt; and BusyBox &lt;code&gt;1.28.0&lt;&#x2F;code&gt;. After updating my versions I let the &lt;code&gt;Makefile&lt;&#x2F;code&gt; run and realized I needed to update my BusyBox config. I opted for the changes that the BusyBox build system suggested and gave it another test in &lt;a href=&quot;https:&#x2F;&#x2F;www.qemu.org&#x2F;&quot;&gt;QEMU&lt;&#x2F;a&gt; to make sure it was all good to go.&lt;&#x2F;p&gt;
&lt;p&gt;Now to build an ISO for it! I looked to see what resources I could find on ow to get that done. I saw what &lt;a href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;GRUB&quot;&gt;osdev.org&lt;&#x2F;a&gt; had to offer and found a lot of good info on &lt;a href=&quot;http:&#x2F;&#x2F;lukeluo.blogspot.com&#x2F;2013&#x2F;06&#x2F;grub-how-to-2-make-boot-able-iso-with.html&quot;&gt;how grub-mkresue works here&lt;&#x2F;a&gt;. I took a peek at the &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;grub&#x2F;manual&#x2F;grub&#x2F;grub.html#Making-a-GRUB-bootable-CD_002dROM&quot;&gt;grub2 manual&lt;&#x2F;a&gt; and it looks easier than I expected it would be.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;creating-a-grub-iso&quot;&gt;Creating a GRUB ISO&lt;&#x2F;h1&gt;
&lt;p&gt;As an initial test I wanted to verify that a normal grub2 rescue image would book in QEMU. I used &lt;code&gt;grub-mkrescue -o test.iso&lt;&#x2F;code&gt; to build this image.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-iso&#x2F;mkrescue.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Then to test it I ran &lt;code&gt;qemu-system-x86_64 -m 2048 -cdrom test.iso -boot d&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-iso&#x2F;qemu-grub.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Easy enough. There is no &lt;code&gt;grub.cfg&lt;&#x2F;code&gt; so it gives you this basic rescue shell, but I’m glad it works. The grub documentation and examples I found show that you can give &lt;code&gt;grub-mkrescue&lt;&#x2F;code&gt; a directory that it will include in the build ISO and I decided to give that a shot.&lt;&#x2F;p&gt;
&lt;p&gt;I returned to my barebones repo directory and created a directory &lt;code&gt;iso&lt;&#x2F;code&gt; to store my files that I’d like to go in the ISO, and the &lt;code&gt;boot&#x2F;grub&lt;&#x2F;code&gt; directory within that to store my grub config.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-iso&#x2F;mkdir-grub.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I copied my built &lt;code&gt;vmlinuz&lt;&#x2F;code&gt; and &lt;code&gt;initramfs&lt;&#x2F;code&gt; files to &lt;code&gt;iso&#x2F;boot&#x2F;&lt;&#x2F;code&gt; and wrote the smallest &lt;code&gt;grub.cfg&lt;&#x2F;code&gt; to test it.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;menuentry &amp;#39;barebones&amp;#39; {
&lt;&#x2F;span&gt;&lt;span&gt;  linux	&#x2F;boot&#x2F;vmlinuz
&lt;&#x2F;span&gt;&lt;span&gt;  initrd &#x2F;boot&#x2F;initramfs
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With all the files where they should be I run &lt;code&gt;grub-mkrescue&lt;&#x2F;code&gt; again to make the build.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-iso&#x2F;mkiso-barebones.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Then I’ll give my &lt;code&gt;barebones.iso&lt;&#x2F;code&gt; a test in QEMU and there’s my menu entry:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-iso&#x2F;grub-barebones.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Selected my barebones OS and let it do it’s thing:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-iso&#x2F;hello-iso.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Huzzah! IT’S ALIVE!&lt;&#x2F;p&gt;
&lt;p&gt;I thought there was going to be a bit more to this but I was pleasantly surprised!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>C Strings and Standard Input</title>
		<published>2018-01-31T00:00:00+00:00</published>
		<updated>2018-01-31T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/c-strings-and-standard-input/" type="text/html"/>
		<id>https://suchprogramming.com/c-strings-and-standard-input/</id>
		<content type="html">&lt;p&gt;Many C tutorials out there will show you some bad ways to do things. In this case I mean objectively bad, as security issues could arrise from naive approaches. Though a hacker wouldn&#x27;t exactly mind this so maybe that is subjective. I’ll pick on &lt;a href=&quot;https:&#x2F;&#x2F;www.tutorialspoint.com&#x2F;cprogramming&#x2F;c_input_output.htm&quot;&gt;this input and output tutorial&lt;&#x2F;a&gt; as an example.&lt;&#x2F;p&gt;
&lt;p&gt;It has what may appear as a pretty reasonable way to read input with the deprecated &lt;code&gt;gets()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;( ) {
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; str[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;( &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Enter a value :&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;gets&lt;&#x2F;span&gt;&lt;span&gt;( str );
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;( &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;You entered: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt;( str );
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or via &lt;code&gt;scanf()&lt;&#x2F;code&gt; like this&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;( ) {
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; str[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;( &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Enter a value :&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;scanf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s %d&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, str, &amp;amp;i);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;( &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;You entered: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s %d &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, str, i);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In either case, the program wants you to enter a string. The string can only fit 100 ascii characters, though should really only have 99 so that your string can end with a &lt;code&gt;0&lt;&#x2F;code&gt; byte to be properly &lt;code&gt;NULL&lt;&#x2F;code&gt;-terminated. When I give it 120 &lt;code&gt;a&lt;&#x2F;code&gt;‘s, my system is reasonably displeased as I clobber over other parts of my stack.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;badhabits 
&lt;&#x2F;span&gt;&lt;span&gt;Enter a value :aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;*** stack smashing detected ***: .&#x2F;badhabits terminated
&lt;&#x2F;span&gt;&lt;span&gt;You entered: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1 Aborted (core dumped)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One option would be to use a better format string. Referring to something like the &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;manual&#x2F;html_node&#x2F;String-Input-Conversions.html#String-Input-Conversions&quot;&gt;GNU C Library Manual&lt;&#x2F;a&gt;, we can see that the &lt;code&gt;scanf()&lt;&#x2F;code&gt; function has a few other tricks up its sleeve that can help us.&lt;&#x2F;p&gt;
&lt;p&gt;If we really wanted a 99 character limit on this string, we could change the format string to &lt;code&gt;&amp;quot;%99s %d&amp;quot;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;badhabits 
&lt;&#x2F;span&gt;&lt;span&gt;Enter a value :aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;You entered: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this case &lt;code&gt;scanf()&lt;&#x2F;code&gt; will truncate the string so that it fits into the size of the buffer. If the library is &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;POSIX&quot;&gt;POSIX&lt;&#x2F;a&gt; compliant, the m modifier can also be used to ask &lt;code&gt;scanf()&lt;&#x2F;code&gt; to dynamically allocate your string with &lt;code&gt;malloc()&lt;&#x2F;code&gt; and give you a pointer to that newly allocated memory space that now holds the input string nicely.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*name;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Enter your name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;scanf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%ms&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;amp;name);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, name);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;betterscanf 
&lt;&#x2F;span&gt;&lt;span&gt;Enter your name: test
&lt;&#x2F;span&gt;&lt;span&gt;Hello test!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;beyond-scanf&quot;&gt;Beyond Scanf&lt;&#x2F;h1&gt;
&lt;p&gt;Personally, I’m not a big fan of &lt;code&gt;scanf()&lt;&#x2F;code&gt; in general. When hunting for other options, I first will peruse the &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;manual&#x2F;html_node&#x2F;index.html&quot;&gt;GNU C Library’s manual&lt;&#x2F;a&gt;. In &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;manual&#x2F;html_node&#x2F;Line-Input.html&quot;&gt;section 12.9&lt;&#x2F;a&gt; I find an approach that fits a common need I have, reading one line at a time with &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;3&#x2F;getline&quot;&gt;getline()&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;getline()&lt;&#x2F;code&gt; function offers a few things that I like. It expects to work with dynamically allocated buffers and will allocate or reallocate them to size for you.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;ssize_t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getline&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;lineptr&lt;&#x2F;span&gt;&lt;span&gt;, size_t *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;n&lt;&#x2F;span&gt;&lt;span&gt;, FILE *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using it is pretty simple, it takes a pointer to a &lt;code&gt;char&lt;&#x2F;code&gt; pointer (&lt;code&gt;lineptr&lt;&#x2F;code&gt;) along with a pointer to a &lt;code&gt;size_t&lt;&#x2F;code&gt; type number (&lt;code&gt;n&lt;&#x2F;code&gt;). It’ll read a line from the &lt;code&gt;stream&lt;&#x2F;code&gt; file descriptor and return a &lt;code&gt;ssize_t&lt;&#x2F;code&gt; (signed size) value of the number of bytes read or &lt;code&gt;-1&lt;&#x2F;code&gt; on failure.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;_GNU_SOURCE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*string = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  size_t buffer_size = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  ssize_t read_size;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Enter some stuff!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  read_size = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getline&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;string, &amp;amp;buffer_size, stdin);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Read &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%zd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; bytes, buffer is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%zd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, read_size, buffer_size);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Line read:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, string);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You need to make sure to set the string to &lt;code&gt;NULL&lt;&#x2F;code&gt; if it’s not already dynamically allocated or you’ll be passing whatever just happened to be laying around in the stack.&lt;&#x2F;p&gt;
&lt;p&gt;Also worth noting, for the environment I’m building this within I needed to place the processor directive &lt;code&gt;#define _GNU_SOURCE&lt;&#x2F;code&gt; prior to including &lt;code&gt;stdio.h&lt;&#x2F;code&gt; to properly pull in the &lt;code&gt;getline()&lt;&#x2F;code&gt; functionality without angering the compiler.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;getline 
&lt;&#x2F;span&gt;&lt;span&gt;Enter some stuff!
&lt;&#x2F;span&gt;&lt;span&gt;weeeeeeeeeeee
&lt;&#x2F;span&gt;&lt;span&gt;Read 14 bytes, buffer is 120 bytes
&lt;&#x2F;span&gt;&lt;span&gt;Line read:
&lt;&#x2F;span&gt;&lt;span&gt;weeeeeeeeeeee
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In my run here, the &lt;code&gt;buffer_size&lt;&#x2F;code&gt; is getting set to a larger size than the string, whose length I got back from the &lt;code&gt;getline()&lt;&#x2F;code&gt; call. There is some consideration here on the part of the library that it is more efficient to give a longer buffer since it is likely to be added to later on and resizing buffers can be slow.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;fancier-getline&quot;&gt;Fancier Getline&lt;&#x2F;h1&gt;
&lt;p&gt;I like the idea of making a function that abstracts this a bit so it’s a bit friendlier to use. I can reuse portions of the &lt;code&gt;FancyString&lt;&#x2F;code&gt; type I built in a &lt;a href=&quot;https:&#x2F;&#x2F;suchprogramming.com&#x2F;c-structs-and-org&#x2F;&quot;&gt;previous post&lt;&#x2F;a&gt; to build some functions that will let me dynamically read a line in a single step.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;_GNU_SOURCE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef struct &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  ssize_t length;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*string;
&lt;&#x2F;span&gt;&lt;span&gt;  size_t buffer_size;
&lt;&#x2F;span&gt;&lt;span&gt;} FancyString;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;FancyString_free&lt;&#x2F;span&gt;&lt;span&gt;(FancyString *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;string) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;string);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(target);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;FancyString* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fancy_getline&lt;&#x2F;span&gt;&lt;span&gt;(FILE *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *new = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;(sizeof(*new));
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;string = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;buffer_size = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;length = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getline&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;(new-&amp;gt;string), &amp;amp;(new-&amp;gt;buffer_size), stream);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(new-&amp;gt;length == -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(new);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; new;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *line = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fancy_getline&lt;&#x2F;span&gt;&lt;span&gt;(stdin);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Read &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%zd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; bytes, buffer is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%zd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;         line-&amp;gt;length,
&lt;&#x2F;span&gt;&lt;span&gt;         line-&amp;gt;buffer_size);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Line read:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, line-&amp;gt;string);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FancyString_free&lt;&#x2F;span&gt;&lt;span&gt;(line);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;fancy_getline 
&lt;&#x2F;span&gt;&lt;span&gt;this is a test of the fanciness
&lt;&#x2F;span&gt;&lt;span&gt;Read 32 bytes, buffer is 120 bytes
&lt;&#x2F;span&gt;&lt;span&gt;Line read:
&lt;&#x2F;span&gt;&lt;span&gt;this is a test of the fanciness
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I find this a bit more convenient to manage the lines of input, this could even be extended to include a function that would operate like &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;io.html#io.IOBase.readlines&quot;&gt;readlines() in Python&lt;&#x2F;a&gt;. I’ll modify my &lt;code&gt;FancyString&lt;&#x2F;code&gt; to support usage as a linked list, I’ll share more about linked lists and other data structure patterns in a future post.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;_GNU_SOURCE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef struct&lt;&#x2F;span&gt;&lt;span&gt; _fancystring {
&lt;&#x2F;span&gt;&lt;span&gt;  ssize_t length;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*string;
&lt;&#x2F;span&gt;&lt;span&gt;  size_t buffer_size;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; _fancystring *next;
&lt;&#x2F;span&gt;&lt;span&gt;} FancyString;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;FancyString_free&lt;&#x2F;span&gt;&lt;span&gt;(FancyString *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;string) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;string);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(target);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;FancyString* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fancy_getline&lt;&#x2F;span&gt;&lt;span&gt;(FILE *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *new = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;(sizeof(*new));
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;string = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;buffer_size = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;next = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;length = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getline&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;(new-&amp;gt;string), &amp;amp;(new-&amp;gt;buffer_size), stream);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(new-&amp;gt;length == -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(new);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; new;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;FancyString* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fancy_readlines&lt;&#x2F;span&gt;&lt;span&gt;(FILE *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stream&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *first = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *last = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;((i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fancy_getline&lt;&#x2F;span&gt;&lt;span&gt;(stream)) != &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(first == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      first = i;
&lt;&#x2F;span&gt;&lt;span&gt;      last = i;
&lt;&#x2F;span&gt;&lt;span&gt;    } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      last-&amp;gt;next = i;
&lt;&#x2F;span&gt;&lt;span&gt;      last = i;
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; first;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Enter many lines, end with CTRL+D&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *line = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fancy_readlines&lt;&#x2F;span&gt;&lt;span&gt;(stdin);
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *previous_line;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(line != &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Line &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, i, line-&amp;gt;string);
&lt;&#x2F;span&gt;&lt;span&gt;    i++;
&lt;&#x2F;span&gt;&lt;span&gt;    previous_line = line;
&lt;&#x2F;span&gt;&lt;span&gt;    line = line-&amp;gt;next;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FancyString_free&lt;&#x2F;span&gt;&lt;span&gt;(previous_line);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;readlines
&lt;&#x2F;span&gt;&lt;span&gt;Enter many lines, end with CTRL+D
&lt;&#x2F;span&gt;&lt;span&gt;this is a line
&lt;&#x2F;span&gt;&lt;span&gt;and this!
&lt;&#x2F;span&gt;&lt;span&gt;and moar
&lt;&#x2F;span&gt;&lt;span&gt;and moooooaoOOAOOAOARRRRR
&lt;&#x2F;span&gt;&lt;span&gt;Line 1: this is a line
&lt;&#x2F;span&gt;&lt;span&gt;Line 2: and this!
&lt;&#x2F;span&gt;&lt;span&gt;Line 3: and moar
&lt;&#x2F;span&gt;&lt;span&gt;Line 4: and moooooaoOOAOOAOARRRRR
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;ll be it for this post, I hope you learned something or find some of this useful!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Command Line Arguments in C</title>
		<published>2018-01-24T00:00:00+00:00</published>
		<updated>2018-01-24T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/command-line-c/" type="text/html"/>
		<id>https://suchprogramming.com/command-line-c/</id>
		<content type="html">&lt;p&gt;Today I’m going to share some tips and tricks to using command line arguments with C programs. First I’ll explore the plain ‘ol &lt;code&gt;argc&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;argv&lt;&#x2F;code&gt; style followed by a &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;manual&#x2F;html_node&#x2F;Getopt.html&quot;&gt;getopt&lt;&#x2F;a&gt; approach.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s jump right to it!&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;classic-approach&quot;&gt;Classic Approach&lt;&#x2F;h1&gt;
&lt;p&gt;Most C programmers will be quite familiar with this approach, so I’ll keep it brief. The most common function signature for &lt;code&gt;main&lt;&#x2F;code&gt; is &lt;code&gt;int main(int argc, char *argv[])&lt;&#x2F;code&gt;. In this setup the &lt;code&gt;argc&lt;&#x2F;code&gt; will tell you the number of arguments passed to the program at launch, with &lt;code&gt;argc&lt;&#x2F;code&gt; getting a list of string pointers to those arguments.&lt;&#x2F;p&gt;
&lt;p&gt;With a small test program, we can inspect these variables pretty easily. I’ll even look at where the &lt;code&gt;argv&lt;&#x2F;code&gt; pointer is and where the string pointers within it are pointing to, because why not!&lt;&#x2F;p&gt;
&lt;p&gt;My &lt;code&gt;classic.c&lt;&#x2F;code&gt; source:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Argument Count: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, argc);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;argv is at &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, argv);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i &amp;lt;= argc; i++) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; at &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, i, argv[i], argv[i]);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Testing it:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;classic well hello there
&lt;&#x2F;span&gt;&lt;span&gt;Argument Count: 4
&lt;&#x2F;span&gt;&lt;span&gt;argv is at 0x7ffc923396b8
&lt;&#x2F;span&gt;&lt;span&gt;0 at 0x7ffc92339f4d: .&#x2F;classic
&lt;&#x2F;span&gt;&lt;span&gt;1 at 0x7ffc92339f57: well
&lt;&#x2F;span&gt;&lt;span&gt;2 at 0x7ffc92339f5c: hello
&lt;&#x2F;span&gt;&lt;span&gt;3 at 0x7ffc92339f62: there
&lt;&#x2F;span&gt;&lt;span&gt;4 at (nil): (null)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first argument is the name of the program as it was executed, the remaining &lt;code&gt;argc - 1&lt;&#x2F;code&gt; are the command line arguments to that program. They are most often accessed using an index operator &lt;code&gt;[i]&lt;&#x2F;code&gt; as was done here, though usually &lt;code&gt;i &amp;lt; argc;&lt;&#x2F;code&gt; is used in the &lt;code&gt;for&lt;&#x2F;code&gt; loop condition to skip the &lt;code&gt;null&lt;&#x2F;code&gt; element that terminates the array.&lt;&#x2F;p&gt;
&lt;p&gt;Since this is a &lt;code&gt;null&lt;&#x2F;code&gt; terminated array, you could also use &lt;a href=&quot;https:&#x2F;&#x2F;suchprogramming.com&#x2F;fun-with-pointers&#x2F;&quot;&gt;pointer arithmetic&lt;&#x2F;a&gt; to process through the list and not use &lt;code&gt;argc&lt;&#x2F;code&gt; at all.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;**arg = argv;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(*arg) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, *arg);
&lt;&#x2F;span&gt;&lt;span&gt;    arg++;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;$ .&#x2F;classic do it live
.&#x2F;classic
do
it
live&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;How you interpret the command line arguments from there is entirely up to you, which is why you&amp;#39;ll see a lot of variance in command line applications. As with other programming languages, some patterns are very common and there are libraries to assist in implementing them.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Getopt Basics
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;The [getopt()](http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man3&#x2F;getopt.3.html) function is part of any Standard C Library implementation that follows a [POSIX](https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;POSIX) standard. Many libraries will also follow the GNU standard for [getopt_long()](https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;manual&#x2F;html_node&#x2F;Getopt-Long-Options.html#Getopt-Long-Options).
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;The general idea is that options use a format starting with `-` followed by a single letter to indicate something about what the user wants the program to do. As an example, many programs on Linux will have a `-v` option you can use to see more verbose console output, or a `-h` option to get help on using the program.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;The `getopt` implementation has a few variables that represent the current internal state of its parsing system.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;```c
&lt;&#x2F;span&gt;&lt;span&gt;extern int optind, opterr, optopt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;optind&lt;&#x2F;code&gt; variable is the index value of the next argument that should be handled by the &lt;code&gt;getopt()&lt;&#x2F;code&gt; function. &lt;code&gt;opterr&lt;&#x2F;code&gt; will let you control if the &lt;code&gt;getopt()&lt;&#x2F;code&gt; function should print errors to the console. If the &lt;code&gt;getopt()&lt;&#x2F;code&gt; call returns&lt;code&gt; ?&lt;&#x2F;code&gt; because it did not recognize the option being given, &lt;code&gt;optopt&lt;&#x2F;code&gt; will be set to the character it did not recognize.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) { 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optind: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;opterr: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optopt: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ,
&lt;&#x2F;span&gt;&lt;span&gt;    optind, opterr, optopt, optopt
&lt;&#x2F;span&gt;&lt;span&gt;  );
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;getopt 
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: ? (63)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To use the &lt;code&gt;getopt()&lt;&#x2F;code&gt; function you provide the &lt;code&gt;argc&lt;&#x2F;code&gt; and &lt;code&gt;argv&lt;&#x2F;code&gt; variables along with an &lt;code&gt;optstring&lt;&#x2F;code&gt; variable that contains the list of options it should look for.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optind: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;opterr: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optopt: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ,
&lt;&#x2F;span&gt;&lt;span&gt;    optind, opterr, optopt, optopt
&lt;&#x2F;span&gt;&lt;span&gt;  );
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; character;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*options = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  character = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getopt&lt;&#x2F;span&gt;&lt;span&gt;(argc, argv, options);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;getopt returned: &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, character, character);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;getopt 
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: ? (63)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;�&amp;#39; (-1)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$ .&#x2F;getopt -v
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: ? (63)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;v&amp;#39; (118)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 2 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$ .&#x2F;getopt -h
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: ? (63)
&lt;&#x2F;span&gt;&lt;span&gt;.&#x2F;getopt: invalid option -- &amp;#39;h&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;?&amp;#39; (63)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 2 opterr: 1 optopt: h (104)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;On each run of &lt;code&gt;getopt()&lt;&#x2F;code&gt;, until it reaches the end of the argument list and returns &lt;code&gt;-1&lt;&#x2F;code&gt;, it will check the next argument and return the option found or &lt;code&gt;?&lt;&#x2F;code&gt; if an unrecognized option was given.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optind: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;opterr: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optopt: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ,
&lt;&#x2F;span&gt;&lt;span&gt;    optind, opterr, optopt, optopt
&lt;&#x2F;span&gt;&lt;span&gt;  );
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; character;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*options = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abcd&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  character = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getopt&lt;&#x2F;span&gt;&lt;span&gt;(argc, argv, options);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while&lt;&#x2F;span&gt;&lt;span&gt;(character != -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;getopt returned: &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, character, character);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  
&lt;&#x2F;span&gt;&lt;span&gt;    character = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getopt&lt;&#x2F;span&gt;&lt;span&gt;(argc, argv, options);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;getopt returned: &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, character, character);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;getopt -d -a -b
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: ? (63)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;d&amp;#39; (100)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 2 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;a&amp;#39; (97)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 3 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;b&amp;#39; (98)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 4 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;�&amp;#39; (-1)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 4 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can also include multiple options in a single argument by just not separating them. Multiple instances of the same option will be iterated on multiple times.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;getopt -baddd
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: ? (63)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;b&amp;#39; (98)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;a&amp;#39; (97)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;d&amp;#39; (100)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;d&amp;#39; (100)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;d&amp;#39; (100)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 2 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;�&amp;#39; (-1)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 2 opterr: 1 optopt: (0)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;optional-and-positional-arguments&quot;&gt;Optional and Positional Arguments&lt;&#x2F;h1&gt;
&lt;p&gt;A colon after an option in the &lt;code&gt;optstring&lt;&#x2F;code&gt; can be used to indicate that option requires an argument, while two colons can indicate that it supports an argument but is not required. In either case, if an argument is given to an option that supports it, &lt;code&gt;getopt()&lt;&#x2F;code&gt; will set the &lt;code&gt;optarg&lt;&#x2F;code&gt; pointer it provides to the argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optind: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;opterr: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optopt: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optarg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ,
&lt;&#x2F;span&gt;&lt;span&gt;    optind, opterr, optopt, optopt, optarg
&lt;&#x2F;span&gt;&lt;span&gt;  );
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; character;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*options = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abc:d::&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  character = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getopt&lt;&#x2F;span&gt;&lt;span&gt;(argc, argv, options);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while&lt;&#x2F;span&gt;&lt;span&gt;(character != -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;getopt returned: &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, character, character);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  
&lt;&#x2F;span&gt;&lt;span&gt;    character = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getopt&lt;&#x2F;span&gt;&lt;span&gt;(argc, argv, options);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;getopt returned: &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, character, character);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;getopt -dwith -dwithout -c
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: ? (63) optarg: (null)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;d&amp;#39; (100)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 2 opterr: 1 optopt: (0) optarg: with
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;d&amp;#39; (100)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 3 opterr: 1 optopt: (0) optarg: without
&lt;&#x2F;span&gt;&lt;span&gt;.&#x2F;getopt: option requires an argument -- &amp;#39;c&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;?&amp;#39; (63)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 4 opterr: 1 optopt: c (99) optarg: (null)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;�&amp;#39; (-1)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 4 opterr: 1 optopt: c (99) optarg: (null)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If &lt;code&gt;optstring&lt;&#x2F;code&gt; begins with &lt;code&gt;-&lt;&#x2F;code&gt;, non-option positional arguments can also be handled. In these cases &lt;code&gt;getopt()&lt;&#x2F;code&gt; will return the value &lt;code&gt;1&lt;&#x2F;code&gt; to indicate it has found a positional argument and set the &lt;code&gt;optarg&lt;&#x2F;code&gt; pointer to it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optind: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;opterr: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optopt: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optarg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ,
&lt;&#x2F;span&gt;&lt;span&gt;    optind, opterr, optopt, optopt, optarg
&lt;&#x2F;span&gt;&lt;span&gt;  );
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; character;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*options = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-abc:d::&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  character = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getopt&lt;&#x2F;span&gt;&lt;span&gt;(argc, argv, options);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while&lt;&#x2F;span&gt;&lt;span&gt;(character != -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;getopt returned: &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, character, character);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  
&lt;&#x2F;span&gt;&lt;span&gt;    character = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getopt&lt;&#x2F;span&gt;&lt;span&gt;(argc, argv, options);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;getopt returned: &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, character, character);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;print_getopt_state&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;getopt well -a -b now
&lt;&#x2F;span&gt;&lt;span&gt;optind: 1 opterr: 1 optopt: ? (63) optarg: (null)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;&amp;#39; (1)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 2 opterr: 1 optopt: (0) optarg: well
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;a&amp;#39; (97)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 3 opterr: 1 optopt: (0) optarg: (null)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;b&amp;#39; (98)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 4 opterr: 1 optopt: (0) optarg: (null)
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;&amp;#39; (1)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 5 opterr: 1 optopt: (0) optarg: now
&lt;&#x2F;span&gt;&lt;span&gt;getopt returned: &amp;#39;�&amp;#39; (-1)
&lt;&#x2F;span&gt;&lt;span&gt;optind: 5 opterr: 1 optopt: (0) optarg: (null)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If &lt;code&gt;optstring&lt;&#x2F;code&gt; begins with a &lt;code&gt;+&lt;&#x2F;code&gt;, it will stop option parsing and return the value&lt;code&gt; -1&lt;&#x2F;code&gt; at the first non-option argument. The &lt;code&gt;-&lt;&#x2F;code&gt; and &lt;code&gt;+&lt;&#x2F;code&gt; prefixes are only provided by C libraries that follow the GNU extension’s&lt;&#x2F;p&gt;
&lt;h1 id=&quot;long-options&quot;&gt;Long Options&lt;&#x2F;h1&gt;
&lt;p&gt;The &lt;code&gt;GNU&lt;&#x2F;code&gt; extensions provide a fancier version of &lt;code&gt;getopt&lt;&#x2F;code&gt; that supports longer, more verbose options that begin with &lt;code&gt;--&lt;&#x2F;code&gt;. It has the same first 3 parameters as &lt;code&gt;getopt()&lt;&#x2F;code&gt;. The 4th parameter is an array, &lt;code&gt;longopts&lt;&#x2F;code&gt;, of &lt;code&gt;struct option&lt;&#x2F;code&gt; structures that describe the longer options. The last parameter is an integer pointer, &lt;code&gt;longindex&lt;&#x2F;code&gt;, that on match will be the index of the matched option from the &lt;code&gt;longopts&lt;&#x2F;code&gt; array. &lt;code&gt;longindex&lt;&#x2F;code&gt; may be set to &lt;code&gt;NULL&lt;&#x2F;code&gt; if you don’t plan to use it.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;struct option&lt;&#x2F;code&gt; has this format:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;option {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const char &lt;&#x2F;span&gt;&lt;span&gt;*name;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;         has_arg;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int        &lt;&#x2F;span&gt;&lt;span&gt;*flag;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;         val;
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;name&lt;&#x2F;code&gt; is the option name that is supported. The &lt;code&gt;has_arg&lt;&#x2F;code&gt; field can be set to &lt;code&gt;no_argument&lt;&#x2F;code&gt;, &lt;code&gt;required_argument&lt;&#x2F;code&gt; or &lt;code&gt;optional_argument&lt;&#x2F;code&gt;, which correlate to the values &lt;code&gt;0&lt;&#x2F;code&gt;, &lt;code&gt;1&lt;&#x2F;code&gt;, and &lt;code&gt;2&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;flag&lt;&#x2F;code&gt; pointer, will be set to &lt;code&gt;val&lt;&#x2F;code&gt; on a match and &lt;code&gt;getopt_long()&lt;&#x2F;code&gt; will return &lt;code&gt;0&lt;&#x2F;code&gt;. If &lt;code&gt;flag&lt;&#x2F;code&gt; is &lt;code&gt;NULL&lt;&#x2F;code&gt;, &lt;code&gt;val&lt;&#x2F;code&gt; will be what &lt;code&gt;getopt_long()&lt;&#x2F;code&gt;returns when the long option is matched. Often a program will have a long option that returns the short option. This is common for an option like &lt;code&gt;--help&lt;&#x2F;code&gt;, to set &lt;code&gt;val&lt;&#x2F;code&gt; to &lt;code&gt;h&lt;&#x2F;code&gt; so that the code that handles &lt;code&gt;-h&lt;&#x2F;code&gt; can handle either.&lt;&#x2F;p&gt;
&lt;p&gt;Here’s an example that uses a few different ways of handling the long options.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;getopt.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; character;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*options = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;h&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; longindex;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; moartest_flag = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; option longopts[] = {
&lt;&#x2F;span&gt;&lt;span&gt;    {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;help&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, no_argument, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;h&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;},
&lt;&#x2F;span&gt;&lt;span&gt;    {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, required_argument, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;    {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;longtest&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, optional_argument, &amp;amp;moartest_flag, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while&lt;&#x2F;span&gt;&lt;span&gt;((character = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getopt_long&lt;&#x2F;span&gt;&lt;span&gt;(argc, argv, options, longopts, &amp;amp;longindex)) != -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;getopt_long returned: &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, character, character);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;switch &lt;&#x2F;span&gt;&lt;span&gt;(character) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;h&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;help!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;break&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;longindex: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, longindex);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;longopts[longindex].name = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, longopts[longindex].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;optarg: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, optarg);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;moartest_flag: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, moartest_flag);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;getopt --help --longtest --echo wee
&lt;&#x2F;span&gt;&lt;span&gt;getopt_long returned: &amp;#39;h&amp;#39; (104)
&lt;&#x2F;span&gt;&lt;span&gt;help!
&lt;&#x2F;span&gt;&lt;span&gt;getopt_long returned: &amp;#39;&amp;#39; (0)
&lt;&#x2F;span&gt;&lt;span&gt;longindex: 2
&lt;&#x2F;span&gt;&lt;span&gt;longopts[longindex].name = longtest
&lt;&#x2F;span&gt;&lt;span&gt;optarg: (null)
&lt;&#x2F;span&gt;&lt;span&gt;moartest_flag: 12
&lt;&#x2F;span&gt;&lt;span&gt;getopt_long returned: &amp;#39;&amp;#39; (0)
&lt;&#x2F;span&gt;&lt;span&gt;longindex: 1
&lt;&#x2F;span&gt;&lt;span&gt;longopts[longindex].name = echo
&lt;&#x2F;span&gt;&lt;span&gt;optarg: wee
&lt;&#x2F;span&gt;&lt;span&gt;moartest_flag: 12
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this code, &lt;code&gt;-h&lt;&#x2F;code&gt; and &lt;code&gt;--help&lt;&#x2F;code&gt; will match the &lt;code&gt;h&lt;&#x2F;code&gt; case and print my help message. Both &lt;code&gt;--longtest&lt;&#x2F;code&gt; and -&lt;code&gt;-echo&lt;&#x2F;code&gt; are handled by the &lt;code&gt;0&lt;&#x2F;code&gt; case, I can tell them apart from the value that &lt;code&gt;longindex&lt;&#x2F;code&gt; gets set to. When &lt;code&gt;--longtest&lt;&#x2F;code&gt; is given, the &lt;code&gt;moartest_flag&lt;&#x2F;code&gt; integer will get set to 12 since I provided the pointer to that integer in my &lt;code&gt;flags&lt;&#x2F;code&gt; field for that option.&lt;&#x2F;p&gt;
&lt;p&gt;That covers what I wanted to share on parsing command line arguments! There are other strategies out there for handling it, but these are the most common and I hope this guide proves helpful. &lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Structures and Code Organization in C</title>
		<published>2018-01-17T00:00:00+00:00</published>
		<updated>2018-01-17T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/c-structs-and-org/" type="text/html"/>
		<id>https://suchprogramming.com/c-structs-and-org/</id>
		<content type="html">&lt;p&gt;Many C tutorials very briefly gloss over structures and most don’t offer any guidance on how to organize your code and build it in a more modular fashion.&lt;&#x2F;p&gt;
&lt;p&gt;In this post I will share the general system that I follow and prefer when it comes to organizing my C code. There are many other perfectly valid ways to organize your code; this is what works well for me and hopefully for you too!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;code-organization&quot;&gt;Code Organization&lt;&#x2F;h1&gt;
&lt;p&gt;For this post, I’m going to define a new type, &lt;code&gt;FancyString&lt;&#x2F;code&gt;, along with a few functions that can provide some useful operations with it. I’ll have a &lt;code&gt;main.c&lt;&#x2F;code&gt; file to hold the application code I’m using to play with it, and &lt;code&gt;main.h&lt;&#x2F;code&gt; to include my headers for it. All of the declarations and data needed to use &lt;code&gt;FancyString&lt;&#x2F;code&gt; will be in &lt;code&gt;fancystring.h&lt;&#x2F;code&gt;, with the implementations of the functions declared in &lt;code&gt;fancystring.c&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;As I normally do, I’ll start by building a &lt;a href=&quot;&#x2F;making-with-make&#x2F;&quot;&gt;Makefile&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;test
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILE_PROGRAM &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;gcc -Wall -o &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$@ $&amp;lt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILE_OBJECT &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;gcc -Wall -c -o &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$@ $&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;main.c main.h fancystring.o
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILE_PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fancystring.o
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fancystring.o&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fancystring.c fancystring.h
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILE_OBJECT&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;clean&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rm -vf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt; fancystring.o
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This time around, I’m using the &lt;code&gt;-c&lt;&#x2F;code&gt; flag for &lt;code&gt;gcc&lt;&#x2F;code&gt; to have it compile some the code to an object file &lt;code&gt;fancystring.o&lt;&#x2F;code&gt;, and not link it into a program or library. When I compile the actual program I’ll include this object file as a source, and it’ll end up in my &lt;code&gt;test&lt;&#x2F;code&gt; program. This will let me skip recompiling my &lt;code&gt;fancystring&lt;&#x2F;code&gt; code if I don’t make any changes to it.&lt;&#x2F;p&gt;
&lt;p&gt;For &lt;code&gt;main.c&lt;&#x2F;code&gt;, I’ll start with a basic shell of a program.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;main.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For the accompanying header, &lt;code&gt;main.h&lt;&#x2F;code&gt;, it’ll look pretty bare. I’ll just include my &lt;code&gt;fancystring.h&lt;&#x2F;code&gt; header.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fancystring.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now for &lt;code&gt;fancystring.c&lt;&#x2F;code&gt;, I don’t have anything quite yet. I’ll get to that file when I start implementing some code that uses my new structure. For &lt;code&gt;fancystring.h&lt;&#x2F;code&gt;, I’ll have a few standard library includes but eventually my structure will be defined here along with declarations of functions that will work with it.&lt;&#x2F;p&gt;
&lt;p&gt;In larger projects you might have multiple files that reference this header, and if it’s included twice the compiler will lose it’s cool because you defined some things multiple times. To prevent this, I’ll wrap the whole header in a preprocessor &lt;code&gt;ifndef&lt;&#x2F;code&gt; (if not defined) &#x2F; &lt;code&gt;endif&lt;&#x2F;code&gt; combination. When the first preprocessor line sees that my macro &lt;code&gt;__FANCYSTRING_H__&lt;&#x2F;code&gt; is not defined the next line of the will define that macro so later it can be checked against to make sure the contents of the header will only be handled one time, no matter how many bits of code are including it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#ifndef&lt;&#x2F;span&gt;&lt;span&gt; __FANCYSTRING_H__
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;__FANCYSTRING_H__
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#endif
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That’ll give the general framework to build the rest of the code within. Anything I don’t want duplicated during compile will go between my &lt;code&gt;ifndef&lt;&#x2F;code&gt; and its &lt;code&gt;endif&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;building-the-fancystring-type&quot;&gt;Building the FancyString Type&lt;&#x2F;h1&gt;
&lt;p&gt;I’ll start off by declaring my structure for the &lt;code&gt;FancyString&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;FancyString {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned int&lt;&#x2F;span&gt;&lt;span&gt; length;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*string;
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This structure has 2 fields: &lt;code&gt;length&lt;&#x2F;code&gt; contains the length of the string that the &lt;code&gt;string&lt;&#x2F;code&gt; pointer points to.&lt;&#x2F;p&gt;
&lt;p&gt;When you define a structure like this, all your variables of this structure type would have to be defined as &lt;code&gt;struct FancyString variableName;&lt;&#x2F;code&gt;. To make it a bit shorter we can declare this structure as a data type instead with &lt;code&gt;typedef&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The general syntax of &lt;code&gt;typedef&lt;&#x2F;code&gt; is&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;typedef &amp;lt;declaration or existing type&amp;gt; &amp;lt;new type name&amp;gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You could use this to create a type named &lt;code&gt;byte&lt;&#x2F;code&gt; that is a shortcut to an &lt;code&gt;unsigned char&lt;&#x2F;code&gt; with &lt;code&gt;typedef unsigned char byte;&lt;&#x2F;code&gt;. In this case I’ll declare my structure and type all in one go within my &lt;code&gt;fancystring.h&lt;&#x2F;code&gt; header.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#ifndef&lt;&#x2F;span&gt;&lt;span&gt; __FANCYSTRING_H__
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;__FANCYSTRING_H__
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef struct &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned int&lt;&#x2F;span&gt;&lt;span&gt; length;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*string;
&lt;&#x2F;span&gt;&lt;span&gt;} FancyString;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#endif
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With my structure in place, I’ll whip up a few functions that will help me dynamically allocate an empty &lt;code&gt;FancyString&lt;&#x2F;code&gt; object and free it from memory. I’ll add the declarations of these functions to my &lt;code&gt;fancystring.h&lt;&#x2F;code&gt; header so that other files can be aware of it’s existence.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#ifndef&lt;&#x2F;span&gt;&lt;span&gt; __FANCYSTRING_H__
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;__FANCYSTRING_H__
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;typedef struct &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned int&lt;&#x2F;span&gt;&lt;span&gt; length;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*string;
&lt;&#x2F;span&gt;&lt;span&gt;} FancyString;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;FancyString* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;FancyString_new&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;FancyString_free&lt;&#x2F;span&gt;&lt;span&gt;(FancyString *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#endif
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The actual implementation of these will be in &lt;code&gt;fancystring.c&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fancystring.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;FancyString* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;FancyString_new&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *new = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;(sizeof(*new));
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;length = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;string = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; new;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;FancyString_free&lt;&#x2F;span&gt;&lt;span&gt;(FancyString *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;string) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;string);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(target);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first function here dynamically allocates memory for the structure with &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;3&#x2F;malloc&quot;&gt;malloc()&lt;&#x2F;a&gt;, and sets the other variables to useful defaults.&lt;&#x2F;p&gt;
&lt;p&gt;Structure fields can be accessed in two ways. If you’re dealing with a direct instance of a structure, rather than a pointer to the structure, you use the &lt;code&gt;.&lt;&#x2F;code&gt; operator to access the field. For this structure I’m always going to use it as a pointer, so I use the &lt;code&gt;-&amp;gt;&lt;&#x2F;code&gt; operator instead.&lt;&#x2F;p&gt;
&lt;p&gt;I could still use &lt;code&gt;.&lt;&#x2F;code&gt; operator with a pointer if i really wanted to by first dereferencing the pointer with something like &lt;code&gt;(*new).length = 0;&lt;&#x2F;code&gt; but that is pretty wonky so I won’t be doing that.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;building-around-the-structure&quot;&gt;Building Around the Structure&lt;&#x2F;h1&gt;
&lt;p&gt;This string is not yet fancy at all, but there’s a few things I’ve decided for it, firstly that it’s only used for dynamically allocated strings. Though it’ll still be pretty nice to define it with normal static strings in C. I’ll add another function that’ll let me create a new &lt;code&gt;FancyString&lt;&#x2F;code&gt; from an ordinary C string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span&gt;FancyString* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;FancyString_new_from_string&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *new = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FancyString_new&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;length = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strlen&lt;&#x2F;span&gt;&lt;span&gt;(string);
&lt;&#x2F;span&gt;&lt;span&gt;  new-&amp;gt;string = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;(new-&amp;gt;length + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strcpy&lt;&#x2F;span&gt;&lt;span&gt;(new-&amp;gt;string, string);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; new;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now I’ll give it a test in my &lt;code&gt;main&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;main.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *test = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FancyString_new_from_string&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Well hello!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, test-&amp;gt;string);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FancyString_free&lt;&#x2F;span&gt;&lt;span&gt;(test);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It works like a charm! Though it’s still not particularly useful. As a final exercise, and to give it some purpose in life, I’ll create another function that let’s me concatenate other C strings to the end of it. Unlike normal &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;3&#x2F;strcat&quot;&gt;strcat()&lt;&#x2F;a&gt;, the FancyString concatenation will automatically resize my string so that it won’t risk a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Buffer_overflow&quot;&gt;buffer overflow&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;FancyString_concat&lt;&#x2F;span&gt;&lt;span&gt;(FancyString *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;(!target-&amp;gt;string) {
&lt;&#x2F;span&gt;&lt;span&gt;    target-&amp;gt;length = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strlen&lt;&#x2F;span&gt;&lt;span&gt;(string);
&lt;&#x2F;span&gt;&lt;span&gt;    target-&amp;gt;string = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;length + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strcpy&lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;string, string);
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    target-&amp;gt;length += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strlen&lt;&#x2F;span&gt;&lt;span&gt;(string);
&lt;&#x2F;span&gt;&lt;span&gt;    target-&amp;gt;string = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;realloc&lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;string, target-&amp;gt;length + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strcat&lt;&#x2F;span&gt;&lt;span&gt;(target-&amp;gt;string, string);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This new function will do the initial allocation if the &lt;code&gt;FancyString&lt;&#x2F;code&gt; was initially empty, otherwise it’ll use &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;3&#x2F;realloc&quot;&gt;realloc()&lt;&#x2F;a&gt; to expand the dynamically allocated buffer and then concatenate it all together.&lt;&#x2F;p&gt;
&lt;p&gt;I’ll give this all one more test in &lt;code&gt;main&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;main.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  FancyString *test = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FancyString_new&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;new: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, test-&amp;gt;string);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FancyString_concat&lt;&#x2F;span&gt;&lt;span&gt;(test, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;first concat: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, test-&amp;gt;string);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FancyString_concat&lt;&#x2F;span&gt;&lt;span&gt;(test, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;, world&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;second concat: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, test-&amp;gt;string);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;FancyString_free&lt;&#x2F;span&gt;&lt;span&gt;(test);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ make
&lt;&#x2F;span&gt;&lt;span&gt;gcc -Wall -o test main.c fancystring.o
&lt;&#x2F;span&gt;&lt;span&gt;$ .&#x2F;test 
&lt;&#x2F;span&gt;&lt;span&gt;new: (null)
&lt;&#x2F;span&gt;&lt;span&gt;first concat: hello
&lt;&#x2F;span&gt;&lt;span&gt;second concat: hello, world
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Works as expected! That’ll wrap things up for this post, I hope this might help with your journey with C!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Fun With Pointers</title>
		<published>2018-01-10T00:00:00+00:00</published>
		<updated>2018-01-10T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/fun-with-pointers/" type="text/html"/>
		<id>https://suchprogramming.com/fun-with-pointers/</id>
		<content type="html">&lt;p&gt;Pointers are one of the most misunderstood concepts in C but also one the most powerful tools it provides. In general they are just numbers in memory like everything else, but their value is interpreted as an address to other data. In this document I’ll attempt to demystify the arcane pointer and show some practical examples of their power.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;everything-lives-in-memory&quot;&gt;Everything Lives In Memory&lt;&#x2F;h1&gt;
&lt;p&gt;Before I get into pointers, I want to clarify some aspects of how a program runs. This will all be based on my local Linux environment so your mileage may vary, but conceptually it’s all the same.&lt;&#x2F;p&gt;
&lt;p&gt;The C compiler and linker you use takes your code, parses it, and converts it to a binary file that works for the targeted processor and operating system. The compiler will validate your C code and generate machine code that your processor can work with, the linker will do some organizational tasks to allow your program to use libraries and be loaded by the operating system.&lt;&#x2F;p&gt;
&lt;p&gt;Most, if not all, UNIX systems use the Executable and Linkable Format (&lt;a href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Executable_and_Linkable_Format&quot;&gt;ELF&lt;&#x2F;a&gt;) for compiled programs and libraries. It has a header that describes what type of binary the file is, and has a lot of other data that tells the operating system where in &lt;a href=&quot;http:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20160716011430&#x2F;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Virtual_memory&quot;&gt;virtual memory&lt;&#x2F;a&gt; the program’s assets should be loaded.&lt;&#x2F;p&gt;
&lt;p&gt;Here’s a simple program that we’ll inspect a little bit. It spits out the addresses of a few things, the locations in the running virtual memory space of a program. The &lt;code&gt;&amp;amp;&lt;&#x2F;code&gt; operator before a variable will give you the address of that variable.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; my_global = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;123&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; my_local = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;456&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;main has an address, it is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, main);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;my_global has an address, it is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;amp;my_global);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;my_local has an address, it is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;amp;my_local);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;printf has an address, it is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, printf);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Tons of crap ends up in the binary for a C program, like all the stuff that provides you the goodness of the standard C library and the stuff the C library itself needs. One of the more interesting things we can look at is the symbol table. Symbols help give us names to find important things in the program. I’ll use &lt;code&gt;readelf&lt;&#x2F;code&gt; to dump the symbol table from the binary, and filter out globals.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fun-with-pointers&#x2F;symbol-table.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;From this list there are a few things that were defined in my source file. Entries 4 and 53 both refer to the &lt;code&gt;printf&lt;&#x2F;code&gt; call I’m using from glibc which for this program is located at the memory address &lt;code&gt;0x400470&lt;&#x2F;code&gt;, entry 59 is the &lt;code&gt;my_global&lt;&#x2F;code&gt; variable from my program at the address &lt;code&gt;0x601040&lt;&#x2F;code&gt; and entry 64 is my &lt;code&gt;main&lt;&#x2F;code&gt; function at &lt;code&gt;0x400596&lt;&#x2F;code&gt;. The only thing missing is the &lt;code&gt;my_local&lt;&#x2F;code&gt; variable that’s inside of my &lt;code&gt;main&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;p&gt;If I run the program a few times I’ll see where the program says these things are.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fun-with-pointers&#x2F;where-the-pointers-at-1.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As seen here, &lt;code&gt;main&lt;&#x2F;code&gt;, &lt;code&gt;my_global&lt;&#x2F;code&gt; and &lt;code&gt;printf&lt;&#x2F;code&gt; all have consistent locations in memory and it matches what was listed in the symbol table, while &lt;code&gt;my_local&lt;&#x2F;code&gt; changes between calls of the program. This is because the local variable is part of the function which is part of the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Call_stack&quot;&gt;call stack&lt;&#x2F;a&gt; and Linux uses &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Address_space_layout_randomization&quot;&gt;a security feature that randomizes where the stack begins&lt;&#x2F;a&gt;. As functions are called in your C program the stack is used for function arguments, function local variables and a pointer to code that should be ran after the function is complete. Each function call gets a new section of the stack, referred to as a Stack Frame.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fun-with-pointers&#x2F;memory-address.jpg&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Just like a string and everything else on the computer, the executable code resides somewhere in memory. I can use &lt;code&gt;objdump&lt;&#x2F;code&gt; to get the machine code version of main, and it’ll show us each instruction that main is composed of and the addresses of each processor operation.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;0000000000400596 &amp;lt;main&amp;gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  400596:       55                      push   rbp
&lt;&#x2F;span&gt;&lt;span&gt;  400597:       48 89 e5                mov    rbp,rsp
&lt;&#x2F;span&gt;&lt;span&gt;  40059a:       48 83 ec 10             sub    rsp,0x10
&lt;&#x2F;span&gt;&lt;span&gt;  40059e:       64 48 8b 04 25 28 00    mov    rax,QWORD PTR fs:0x28
&lt;&#x2F;span&gt;&lt;span&gt;  4005a5:       00 00 
&lt;&#x2F;span&gt;&lt;span&gt;  4005a7:       48 89 45 f8             mov    QWORD PTR [rbp-0x8],rax
&lt;&#x2F;span&gt;&lt;span&gt;  4005ab:       31 c0                   xor    eax,eax
&lt;&#x2F;span&gt;&lt;span&gt;  4005ad:       c7 45 f4 c8 01 00 00    mov    DWORD PTR [rbp-0xc],0x1c8
&lt;&#x2F;span&gt;&lt;span&gt;  4005b4:       be 96 05 40 00          mov    esi,0x400596
&lt;&#x2F;span&gt;&lt;span&gt;  4005b9:       bf b8 06 40 00          mov    edi,0x4006b8
&lt;&#x2F;span&gt;&lt;span&gt;  4005be:       b8 00 00 00 00          mov    eax,0x0
&lt;&#x2F;span&gt;&lt;span&gt;  4005c3:       e8 a8 fe ff ff          call   400470 &amp;lt;printf@plt&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  4005c8:       be 40 10 60 00          mov    esi,0x601040
&lt;&#x2F;span&gt;&lt;span&gt;  4005cd:       bf d8 06 40 00          mov    edi,0x4006d8
&lt;&#x2F;span&gt;&lt;span&gt;  4005d2:       b8 00 00 00 00          mov    eax,0x0
&lt;&#x2F;span&gt;&lt;span&gt;  4005d7:       e8 94 fe ff ff          call   400470 &amp;lt;printf@plt&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  4005dc:       48 8d 45 f4             lea    rax,[rbp-0xc]
&lt;&#x2F;span&gt;&lt;span&gt;  4005e0:       48 89 c6                mov    rsi,rax
&lt;&#x2F;span&gt;&lt;span&gt;  4005e3:       bf 00 07 40 00          mov    edi,0x400700
&lt;&#x2F;span&gt;&lt;span&gt;  4005e8:       b8 00 00 00 00          mov    eax,0x0
&lt;&#x2F;span&gt;&lt;span&gt;  4005ed:       e8 7e fe ff ff          call   400470 &amp;lt;printf@plt&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  4005f2:       be 70 04 40 00          mov    esi,0x400470
&lt;&#x2F;span&gt;&lt;span&gt;  4005f7:       bf 28 07 40 00          mov    edi,0x400728
&lt;&#x2F;span&gt;&lt;span&gt;  4005fc:       b8 00 00 00 00          mov    eax,0x0
&lt;&#x2F;span&gt;&lt;span&gt;  400601:       e8 6a fe ff ff          call   400470 &amp;lt;printf@plt&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  400606:       b8 00 00 00 00          mov    eax,0x0
&lt;&#x2F;span&gt;&lt;span&gt;  40060b:       48 8b 55 f8             mov    rdx,QWORD PTR [rbp-0x8]
&lt;&#x2F;span&gt;&lt;span&gt;  40060f:       64 48 33 14 25 28 00    xor    rdx,QWORD PTR fs:0x28
&lt;&#x2F;span&gt;&lt;span&gt;  400616:       00 00 
&lt;&#x2F;span&gt;&lt;span&gt;  400618:       74 05                   je     40061f &amp;lt;main+0x89&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  40061a:       e8 41 fe ff ff          call   400460 &amp;lt;__stack_chk_fail@plt&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  40061f:       c9                      leave  
&lt;&#x2F;span&gt;&lt;span&gt;  400620:       c3                      ret    
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Don’t be too daunted with the assembly here, we’re not here to talk about that. Just grasp that when this program is running &lt;code&gt;main&lt;&#x2F;code&gt; will be residing here in the memory space of the program.&lt;&#x2F;p&gt;
&lt;p&gt;To the memory, there is no difference between program code and bytes that may be strings, integers or pictures of cats.  To prove this, I’ll tell the compiler that I want to use &lt;code&gt;main&lt;&#x2F;code&gt; as a string, and I’ll look at the first character of it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*string = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;*) main;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;character: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, string[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hex: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, string[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When I build and run this program, this is what I get:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;test 
&lt;&#x2F;span&gt;&lt;span&gt;character: U
&lt;&#x2F;span&gt;&lt;span&gt;hex: 55
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Just like my previous program, &lt;code&gt;main&lt;&#x2F;code&gt; starts with the assembly operation &lt;code&gt;push rbp&lt;&#x2F;code&gt;, which if you look at as an ascii character would be a &lt;code&gt;U&lt;&#x2F;code&gt;, or &lt;code&gt;0x55&lt;&#x2F;code&gt; as a hexadecimal number.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-you-confuse-me-so&quot;&gt;Why You Confuse Me So?!&lt;&#x2F;h3&gt;
&lt;p&gt;The reason I’m pushing this so hard is to really drive home the fact that everything lives in memory and that the only difference between data types is how you treat the data in memory. You can interpret any given chunk of memory as any type, in the case of pointers we treat a chunk of memory as an address that points to another chunk of memory.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s look at some less esoteric examples of how this can be useful.&lt;&#x2F;p&gt;
&lt;p&gt;The Great Unknown&lt;&#x2F;p&gt;
&lt;p&gt;For someone writing a C application, it would be fantastic if you knew how much memory a program would use before running it. Sadly this is almost never the case, so we need ways to get more memory when we need it. The call stack can suffice to create space for the variables we use inside of a function, but when the function ends we lose scope of those variables. To dynamically allocate space for keeps we use &lt;a href=&quot;https:&#x2F;&#x2F;www.geeksforgeeks.org&#x2F;memory-layout-of-c-program&#x2F;&quot;&gt;the heap&lt;&#x2F;a&gt;, most commonly via &lt;a href=&quot;https:&#x2F;&#x2F;www.tutorialspoint.com&#x2F;c_standard_library&#x2F;c_function_malloc.htm&quot;&gt;malloc()&lt;&#x2F;a&gt; (memory allocate).&lt;&#x2F;p&gt;
&lt;p&gt;The malloc() function is provided by the C standard library, it asks the operating system to give your program a chunk of memory to work on. This is useful for making space as needed to store data in your program.&lt;&#x2F;p&gt;
&lt;p&gt;Here’s an example, this program will take a number of bytes to allocate. It’ll request a chunk of memory of that size, print out it’s address, and then releases that chunk of memory back to the system (to avoid &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Memory_leak&quot;&gt;memory leaks&lt;&#x2F;a&gt;!.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[])
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span&gt;*dynamic_space;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; size;
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;(argc != &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	{
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Usage: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; &amp;lt;number of bytes to allocate&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	size = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;atoi&lt;&#x2F;span&gt;&lt;span&gt;(argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;(size &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	{
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Number of bytes must be larger than 0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	dynamic_space = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;malloc&lt;&#x2F;span&gt;&lt;span&gt;(size);
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;We allocated &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; bytes, they are located at &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, size, dynamic_space);
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;free&lt;&#x2F;span&gt;&lt;span&gt;(dynamic_space);
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I’ll give this program a few runs asking for different amounts of memory&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fun-with-pointers&#x2F;much-allocation.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see, the memory gets allocated in a variety of places even if you ask for the same amount of memory on another run of the program. On my machine, when I asked for a huge allocation it provided me a much higher memory address to use. I’m not sure why this happened but if the system thinks that’s where it should be who am I to argue.&lt;&#x2F;p&gt;
&lt;p&gt;It’s easy enough to ask for space dynamically, the new problem introduced by this is how to be organize it all.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;pointer-usage&quot;&gt;Pointer Usage&lt;&#x2F;h1&gt;
&lt;p&gt;There is no one perfect way to organize your pointers, but there are a ton of tried and true methods that are useful for a variety of cases.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;null-terminated-arrays&quot;&gt;Null terminated arrays&lt;&#x2F;h3&gt;
&lt;p&gt;One of most common structures is a null terminated array, in the case of the &lt;code&gt;char&lt;&#x2F;code&gt; type we normally just call this a string. One of the handy things about these types of structures is they allow for &lt;a href=&quot;https:&#x2F;&#x2F;www.tutorialspoint.com&#x2F;cprogramming&#x2F;c_pointer_arithmetic.htm&quot;&gt;pointer arithmetic&lt;&#x2F;a&gt;. A null terminated array is a contiguous block of memory that contains no information about it’s size, but you can find the end of it by looking for the &lt;code&gt;NULL&lt;&#x2F;code&gt; or &lt;code&gt;0&lt;&#x2F;code&gt; (same thing in C) value at the end of it.&lt;&#x2F;p&gt;
&lt;p&gt;In C the &lt;code&gt;++&lt;&#x2F;code&gt; and &lt;code&gt;--&lt;&#x2F;code&gt; operators work to increment and decrement values, but in the case of pointers they increment by the size of the data type the pointer works with, so it’s identical to incrementing a number with character pointers since characters use a single byte, but for a 4 byte integer it’s more like&lt;code&gt; +=4&lt;&#x2F;code&gt;. Using this functionality you can loop through a null terminated list until the value the pointer points to is zero pretty easily.&lt;&#x2F;p&gt;
&lt;p&gt;Here’s an example of that:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; my_string[] = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;well hello there&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; many_numbers[] = {&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*a_character; 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;*an_integer;
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Start where my_string points, printing each character until a_character
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; points to a zero value
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt;(a_character = my_string; *a_character; a_character++) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;at &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; we have &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, a_character, *a_character);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(an_integer = many_numbers; *an_integer; an_integer++) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;at &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%p&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; we have &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, an_integer, *an_integer);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And here’s that program in action&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fun-with-pointers&#x2F;nullterms.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As you can see the address is incremented by 1 for character pointers and 4 for integer pointers. We can keep reading as long as the dereferenced value (the value in the address the pointer points to) is not zero. When it is zero we’re at the end of the array.&lt;&#x2F;p&gt;
&lt;p&gt;To drive this home further, here’s a program that’ll determine the length of a string given as the first argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; size = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*ptr;
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(argc != &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Usage: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; &amp;lt;string&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	ptr = argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(*ptr) {
&lt;&#x2F;span&gt;&lt;span&gt;		size++;
&lt;&#x2F;span&gt;&lt;span&gt;		ptr++;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;first argument is &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; bytes long&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, size);
&lt;&#x2F;span&gt;&lt;span&gt; 
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And let’s see how that fares&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fun-with-pointers&#x2F;strlen-the-program.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;passing-by-reference&quot;&gt;Passing By Reference&lt;&#x2F;h3&gt;
&lt;p&gt;In many languages, we hear about how things are passed by reference, as opposed to things that are passed by value. In many interpreted languages you have little to no control over how that works, but C lets (makes?) you control that completely.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s say I want to make a function that randomizes two numbers for me. In C you can’t return more than one value from a function, so setting two variables with one function call isn’t possible without pointers. I could return an array of two integers that have random values, then set my variables to those values, but that means my function doesn’t do everything I want it to on it’s own.&lt;&#x2F;p&gt;
&lt;p&gt;The easiest way to do this is to pass the addresses (pointers) to the variables I want randomized, then my function can dereference them as if they were ordinary variables with &lt;code&gt;*&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;randomize_them&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  *a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;random&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  *b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;random&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; my_a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; my_b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;before&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, my_a, my_b);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;randomize_them&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;my_a, &amp;amp;my_b);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;after&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, my_a, my_b);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fun-with-pointers&#x2F;ran-damn.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now that wasn’t too bad, was it?&lt;&#x2F;p&gt;
&lt;p&gt;That’ll wrap things up for this post, this covers most of the basics about pointers. I will be covering a bit more pointer-fu in the next post about defining and using structures in C, but I think this provides a good start for now.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>epoll() Tutorial – epoll() In 3 Easy Steps!</title>
		<published>2018-01-05T00:00:00+00:00</published>
		<updated>2018-01-05T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/epoll-in-3-easy-steps/" type="text/html"/>
		<id>https://suchprogramming.com/epoll-in-3-easy-steps/</id>
		<content type="html">&lt;p&gt;It wasn&#x27;t very long ago that it was a feat of greatness to get a single webserver setup to &lt;a href=&quot;http:&#x2F;&#x2F;www.kegel.com&#x2F;c10k.html&quot;&gt;support 10,000 concurrent connections&lt;&#x2F;a&gt;. There were many factors that made it possible to develop webservers, such as &lt;a href=&quot;https:&#x2F;&#x2F;www.nginx.com&#x2F;&quot;&gt;nginx&lt;&#x2F;a&gt;, that could handle more connections with greater efficiency than their predecessors. One of the biggest factors was the advent of constant-time polling ( &lt;a href=&quot;https:&#x2F;&#x2F;rob-bell.net&#x2F;2009&#x2F;06&#x2F;a-beginners-guide-to-big-o-notation&#x2F;&quot;&gt;O(1)&lt;&#x2F;a&gt; ) mechanisms for monitoring file descriptors introduced into most operating systems.&lt;&#x2F;p&gt;
&lt;p&gt;In the &lt;a href=&quot;https:&#x2F;&#x2F;www.nostarch.com&#x2F;&quot;&gt;No Starch Press&lt;&#x2F;a&gt; book, &lt;a href=&quot;https:&#x2F;&#x2F;nostarch.com&#x2F;tlpi&quot;&gt;The Linux Programming Interface&lt;&#x2F;a&gt;, section 63.4.5 provides a table of observations that describes the time it takes to check different quantities of file descriptors via some of the most common polling methods.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;epoll-in-3-easy-steps&#x2F;poll-times.png&quot; alt=&quot;Chart of polling times&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As this shows, the performance benefits of epoll are decent enough to have an impact on even as few as 10 descriptors. As the number of descriptors increases, using regular &lt;a href=&quot;http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man2&#x2F;poll.2.html&quot;&gt;poll()&lt;&#x2F;a&gt; or &lt;a href=&quot;http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man2&#x2F;select.2.html&quot;&gt;select()&lt;&#x2F;a&gt; becomes a very unattractive option compared to epoll().&lt;&#x2F;p&gt;
&lt;p&gt;This tutorial will run through some of the basics of using epoll() on Linux 2.6.27+.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h2 id=&quot;prerequisite-knowledge&quot;&gt;Prerequisite knowledge&lt;&#x2F;h2&gt;
&lt;p&gt;This tutorial assumes you’re familiar and comfortable with Linux, the syntax of C and the usage of file descriptors in UNIX-like systems.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;getting-started&quot;&gt;Getting started&lt;&#x2F;h1&gt;
&lt;p&gt;Make a new directory to work out of for this tutorial, here’s the Makefile we’re using.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;epoll_example
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;epoll_example&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;epoll_example.c
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gcc -Wall -Werror -o $@&lt;&#x2F;span&gt;&lt;span&gt; epoll_example.c
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;clean&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rm -v&lt;&#x2F;span&gt;&lt;span&gt; epoll_example
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Throughout this post I’ll be using functionality described by the following headers:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for fprintf()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for close(), read()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sys&#x2F;epoll.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for epoll_create1(), epoll_ctl(), struct epoll_event
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for strncmp
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;step-1-create-epoll-file-descriptor&quot;&gt;Step 1: Create epoll file descriptor&lt;&#x2F;h1&gt;
&lt;p&gt;First I’ll go through the process of just creating and closing an epoll instance.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for fprintf()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for close()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sys&#x2F;epoll.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for epoll_create1()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; epoll_fd = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;epoll_create1&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;	
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd == -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to create epoll file descriptor&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;close&lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd)) {
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to close epoll file descriptor&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Running this should work and display no output, if you do get errors you’re either probably running a very old Linux kernel or your system needs real help.&lt;&#x2F;p&gt;
&lt;p&gt;This first example uses &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;2&#x2F;epoll_create1&quot;&gt;epoll_create1()&lt;&#x2F;a&gt; to create a file descriptor to a new epoll instance given to us by the mighty kernel. While it doesn’t do anything with it quite yet we should still make sure to clean it up before the program terminates. Since it’s like any other Linux file descriptor we can just use &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;2&#x2F;close&quot;&gt;close()&lt;&#x2F;a&gt; for this.&lt;&#x2F;p&gt;
&lt;h5 id=&quot;level-triggered-and-edge-triggered-event-notifications&quot;&gt;Level triggered and edge triggered event notifications&lt;&#x2F;h5&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.quora.com&#x2F;What-are-the-key-differences-between-edge-triggered-and-level-triggered-interrupts&quot;&gt;Level-triggered and edge-triggered&lt;&#x2F;a&gt; are terms borrowed from electrical engineering. When we’re using epoll the difference is important. In edge triggered mode we will only receive events when the state of the watched file descriptors change; whereas in level triggered mode we will continue to receive events until the underlying file descriptor is no longer in a ready state. Generally speaking level triggered is the default and is easier to use and is what I’ll use for this tutorial, though it’s good to know edge triggered mode is available.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;step-2-add-file-descriptors-for-epoll-to-watch&quot;&gt;Step 2: Add file descriptors for epoll to watch&lt;&#x2F;h1&gt;
&lt;p&gt;The next thing to do is tell epoll what file descriptors to watch and what kinds of events to watch for. In this example I’ll use one of my favorite file descriptors in Linux, good ol&#x27; file descriptor &lt;code&gt;0&lt;&#x2F;code&gt; (also known as Standard Input).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for fprintf()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for close()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sys&#x2F;epoll.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for epoll_create1(), epoll_ctl(), struct epoll_event
&lt;&#x2F;span&gt;&lt;span&gt;	
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; epoll_event event;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; epoll_fd = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;epoll_create1&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;	
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd == -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to create epoll file descriptor&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt;	
&lt;&#x2F;span&gt;&lt;span&gt;	event.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;events &lt;&#x2F;span&gt;&lt;span&gt;= EPOLLIN;
&lt;&#x2F;span&gt;&lt;span&gt;	event.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fd &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;epoll_ctl&lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd, EPOLL_CTL_ADD, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &amp;amp;event)) {
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to add file descriptor to epoll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;close&lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt;	
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;close&lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd)) {
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to close epoll file descriptor&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here I’ve added an instance of an epoll_event structure and used &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;2&#x2F;epoll_ctl&quot;&gt;epoll_ctl()&lt;&#x2F;a&gt; to add the file descriptor &lt;code&gt;0&lt;&#x2F;code&gt; to our epoll instance &lt;code&gt;epoll_fd&lt;&#x2F;code&gt;. The event structure we pass in for the last argument lets epoll know we’re looking to watch only input events, &lt;code&gt;EPOLLIN&lt;&#x2F;code&gt;, and lets us provide some user-defined data that will be returned for events.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;step-3-profit&quot;&gt;Step 3: Profit&lt;&#x2F;h1&gt;
&lt;p&gt;That’s right! We’re almost there. Now let epoll do it’s magic.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;MAX_EVENTS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;READ_SIZE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for fprintf()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unistd.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for close(), read()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sys&#x2F;epoll.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for epoll_create1(), epoll_ctl(), struct epoll_event
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; for strncmp
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; running = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, event_count, i;
&lt;&#x2F;span&gt;&lt;span&gt;	size_t bytes_read;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; read_buffer[READ_SIZE + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; epoll_event event, events[MAX_EVENTS];
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; epoll_fd = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;epoll_create1&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd == -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to create epoll file descriptor&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;	event.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;events &lt;&#x2F;span&gt;&lt;span&gt;= EPOLLIN;
&lt;&#x2F;span&gt;&lt;span&gt;	event.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fd &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;epoll_ctl&lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd, EPOLL_CTL_ADD, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &amp;amp;event))
&lt;&#x2F;span&gt;&lt;span&gt;	{
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to add file descriptor to epoll&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;close&lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(running) {
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Polling for input...&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		event_count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;epoll_wait&lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd, events, MAX_EVENTS, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;30000&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; ready events&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, event_count);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; i &amp;lt; event_count; i++) {
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Reading file descriptor &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39; -- &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, events[i].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fd&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;			bytes_read = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;read&lt;&#x2F;span&gt;&lt;span&gt;(events[i].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fd&lt;&#x2F;span&gt;&lt;span&gt;, read_buffer, READ_SIZE);
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%zd&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; bytes read.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, bytes_read);
&lt;&#x2F;span&gt;&lt;span&gt;			read_buffer[bytes_read] = &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\0&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;;
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Read &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, read_buffer);
&lt;&#x2F;span&gt;&lt;span&gt;		
&lt;&#x2F;span&gt;&lt;span&gt;			&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;(!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;strncmp&lt;&#x2F;span&gt;&lt;span&gt;(read_buffer, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stop&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;			running = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;		}
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;close&lt;&#x2F;span&gt;&lt;span&gt;(epoll_fd)) {
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fprintf&lt;&#x2F;span&gt;&lt;span&gt;(stderr, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Failed to close epoll file descriptor&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;		&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;	}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally we’re getting down to business!&lt;&#x2F;p&gt;
&lt;p&gt;I added a few new variables here to support and expose what I’m doing. I also added a while loop that’ll keep reading from the file descriptors being watched until one of them says ‘stop’. I used &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;2&#x2F;epoll_wait&quot;&gt;epoll_wait()&lt;&#x2F;a&gt; to wait for events to occur from the epoll instance, the results will be stored in the events array up to &lt;code&gt;MAX_EVENTS&lt;&#x2F;code&gt; with a timeout of 30 second. The return value of epoll_wait() indicates how many members of the events array were filled with event data. Beyond that it’s just printing out what it got and doing some basic logic to close things out!&lt;&#x2F;p&gt;
&lt;p&gt;Here’s the example in action:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;epoll_example 
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Polling for input..
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;hello!&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;1 ready events
&lt;&#x2F;span&gt;&lt;span&gt;Reading file descriptor &amp;#39;0&amp;#39; -- 7 bytes read.
&lt;&#x2F;span&gt;&lt;span&gt;Read &amp;#39;hello!
&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Polling for input...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;this is too long for the buffer we made&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;1 ready events
&lt;&#x2F;span&gt;&lt;span&gt;Reading file descriptor &amp;#39;0&amp;#39; -- 10 bytes read.
&lt;&#x2F;span&gt;&lt;span&gt;Read &amp;#39;this is to&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Polling for input...
&lt;&#x2F;span&gt;&lt;span&gt;1 ready events
&lt;&#x2F;span&gt;&lt;span&gt;Reading file descriptor &amp;#39;0&amp;#39; -- 10 bytes read.
&lt;&#x2F;span&gt;&lt;span&gt;Read &amp;#39;o long for&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Polling for input...
&lt;&#x2F;span&gt;&lt;span&gt;1 ready events
&lt;&#x2F;span&gt;&lt;span&gt;Reading file descriptor &amp;#39;0&amp;#39; -- 10 bytes read.
&lt;&#x2F;span&gt;&lt;span&gt;Read &amp;#39; the buffe&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Polling for input...
&lt;&#x2F;span&gt;&lt;span&gt;1 ready events
&lt;&#x2F;span&gt;&lt;span&gt;Reading file descriptor &amp;#39;0&amp;#39; -- 10 bytes read.
&lt;&#x2F;span&gt;&lt;span&gt;Read &amp;#39;r we made
&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Polling for input...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;stop&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;1 ready events
&lt;&#x2F;span&gt;&lt;span&gt;Reading file descriptor &amp;#39;0&amp;#39; -- 5 bytes read.
&lt;&#x2F;span&gt;&lt;span&gt;Read &amp;#39;stop
&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;First I gave it a small string that fits in the buffer and it works fine and continues iterating over the loop. The second input was too long for the read buffer, and is where level triggering helped us out; events continued to populate until it read all of what was left in the buffer, in edge triggering mode we would have only received 1 notification and the application as-is would not progress until more was written to the file descriptor being watching.&lt;&#x2F;p&gt;
&lt;p&gt;I hope this helped you get some bearings on how to use epoll()!&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 14</title>
		<published>2017-12-20T00:00:00+00:00</published>
		<updated>2017-12-20T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-14/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-14/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Cross Compiling C Code for ARM</title>
		<published>2017-12-15T00:00:00+00:00</published>
		<updated>2017-12-15T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/cross-compiling-c-code-for-arm/" type="text/html"/>
		<id>https://suchprogramming.com/cross-compiling-c-code-for-arm/</id>
		<content type="html">&lt;p&gt;Hello! In this brief post I will share how you can cross compile some simple C code for running on an ARM based device. I have a desire to write a few simple things in C for tinkering with &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;openbmc&#x2F;openbmc&quot;&gt;OpenBMC&lt;&#x2F;a&gt; on &lt;a href=&quot;https:&#x2F;&#x2F;blog.rackspace.com&#x2F;zaius-barreleye-g2-server-development-update-2&quot;&gt;Barreleye G2&lt;&#x2F;a&gt; and figured I would share my process.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;the-build-box&quot;&gt;The Build Box&lt;&#x2F;h1&gt;
&lt;p&gt;To build this program I’m going to use a freshly built Ubuntu 16.04.3 VM, that way I know for sure what dependencies are needed. My host system is also running Ubuntu 16.04.3 and I’m using &lt;a href=&quot;https:&#x2F;&#x2F;virt-manager.org&#x2F;&quot;&gt;Virt Manager&lt;&#x2F;a&gt; as an interface to &lt;a href=&quot;https:&#x2F;&#x2F;libvirt.org&#x2F;&quot;&gt;libvirt&lt;&#x2F;a&gt; that is serving my VMs via &lt;a href=&quot;https:&#x2F;&#x2F;www.qemu.org&#x2F;&quot;&gt;QEMU&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;www.linux-kvm.org&#x2F;page&#x2F;Main_Page&quot;&gt;KVM&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;cross-compiling-c-code-for-arm&#x2F;ubuntu-kickstart.gif&quot; alt=&quot;Starting the install&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;After a few minutes I am ready to go!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;the-codes&quot;&gt;The Codes&lt;&#x2F;h1&gt;
&lt;p&gt;The code for this is pretty simple, I’ll just build the ol’ hello world!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, ARM!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I’ll SSH into my VM so that my commands are easier to follow. I start by creating the source file with vim.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;cross-compiling-c-code-for-arm&#x2F;vimit.gif&quot; alt=&quot;Writing hello.c in VIM&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;building-and-testing&quot;&gt;Building and Testing&lt;&#x2F;h1&gt;
&lt;p&gt;Ubuntu offers many pre-built cross compilers. All you need to do is run a &lt;code&gt;sudo apt update&lt;&#x2F;code&gt; to ensure your package listing is up to date and a &lt;code&gt;sudo apt install -y build-essential gcc-arm-linux-gnueabi&lt;&#x2F;code&gt; will get you what you need.&lt;&#x2F;p&gt;
&lt;p&gt;To build, I’ll use the &lt;code&gt;arm-linux-gnueabi-gcc&lt;&#x2F;code&gt; compiler instead of just &lt;code&gt;gcc&lt;&#x2F;code&gt;. I’ll verify it was indeed for an ARM machine with &lt;code&gt;readelf&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;cross-compiling-c-code-for-arm&#x2F;buildit.gif&quot; alt=&quot;Building and copying the binary&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Next, to test it, I’ll scp it over to my OpenBMC system, ssh into it and give it a go!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;cross-compiling-c-code-for-arm&#x2F;testit.gif&quot; alt=&quot;Building and copying the binary&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There it is! I’ve successfully cross compiled a simple program to run on an ARM machine.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 13</title>
		<published>2017-12-13T00:00:00+00:00</published>
		<updated>2017-12-13T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-13/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-13/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 12</title>
		<published>2017-12-06T00:00:00+00:00</published>
		<updated>2017-12-06T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-12/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-12/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Making with Make</title>
		<published>2017-12-05T00:00:00+00:00</published>
		<updated>2017-12-05T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/making-with-make/" type="text/html"/>
		<id>https://suchprogramming.com/making-with-make/</id>
		<content type="html">&lt;p&gt;I enjoy writing in a variety of low level and compiled languages. One of the tools I use almost every time regardless of the language, or sometimes mix of languages, is &lt;code&gt;make&lt;&#x2F;code&gt;. In this article, I want to share some of the ways that &lt;code&gt;make&lt;&#x2F;code&gt; can be used and some of the tips and tricks I employ the most when using &lt;code&gt;make&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I’ll be writing and running these examples on my Ubuntu 16.04.3 laptop with GNU Make 4.1.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;make-without-makefiles&quot;&gt;Make Without Makefiles&lt;&#x2F;h1&gt;
&lt;p&gt;I almost always use &lt;code&gt;make&lt;&#x2F;code&gt; with a &lt;code&gt;Makefile&lt;&#x2F;code&gt;, as it’s significantly more customizable that way, but I think it’s important to know that &lt;code&gt;make&lt;&#x2F;code&gt; does have some usage you can employ even without a &lt;code&gt;Makefile&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Without a &lt;code&gt;Makefile&lt;&#x2F;code&gt;, &lt;code&gt;make&lt;&#x2F;code&gt; can still follow some of it’s &lt;a href=&quot;https:&#x2F;&#x2F;ftp.gnu.org&#x2F;old-gnu&#x2F;Manuals&#x2F;make-3.79.1&#x2F;html_chapter&#x2F;make_10.html#SEC93&quot;&gt;implicit rules&lt;&#x2F;a&gt; for building some files. Let’s say we have the following &lt;code&gt;test.cpp&lt;&#x2F;code&gt; file ready to build:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello from test.cpp!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You could build the &lt;code&gt;test&lt;&#x2F;code&gt; program with a simple call to &lt;code&gt;make test&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;make-binary.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Or if you’d like to build a &lt;code&gt;test.o&lt;&#x2F;code&gt; object file, you can do &lt;code&gt;make test.o&lt;&#x2F;code&gt; and if that’s present when you do &lt;code&gt;make test&lt;&#x2F;code&gt; it’ll build the binary using the already built object file.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;make-object.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There is a pretty good smattering of things you can build this way, check &lt;a href=&quot;https:&#x2F;&#x2F;ftp.gnu.org&#x2F;old-gnu&#x2F;Manuals&#x2F;make-3.79.1&#x2F;html_chapter&#x2F;make_10.html#SEC93&quot;&gt;the documentation&lt;&#x2F;a&gt; if you’re interested in more detail on the rules and supported targets.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;looking-at-makefiles&quot;&gt;Looking at Makefiles&lt;&#x2F;h1&gt;
&lt;p&gt;Most projects will have a depth beyond what &lt;code&gt;make&lt;&#x2F;code&gt; is able to determine with all it’s smarty-pantsness, and in those cases the beloved &lt;code&gt;Makefile&lt;&#x2F;code&gt; is there to make life easy.&lt;&#x2F;p&gt;
&lt;p&gt;If you’ve pulled down and built a common open source project and looked at the &lt;code&gt;Makefile&lt;&#x2F;code&gt;, or generated one with a tool like &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;automake&#x2F;manual&#x2F;html_node&#x2F;Autotools-Introduction.html&quot;&gt;autotools&lt;&#x2F;a&gt; or &lt;a href=&quot;https:&#x2F;&#x2F;cmake.org&#x2F;&quot;&gt;cmake&lt;&#x2F;a&gt;, you may have looked at it with much confusion.&lt;&#x2F;p&gt;
&lt;p&gt;As an example I’ll look at &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;libuv&#x2F;libuv&quot;&gt;libuv&lt;&#x2F;a&gt;, my favorite cross platform asynchronous I&#x2F;O library. After cloning the repo down, running &lt;code&gt;.&#x2F;autogen.sh&lt;&#x2F;code&gt; to generate the build configuration script, then running the &lt;code&gt;.&#x2F;configure&lt;&#x2F;code&gt; script I get a nearly 5000 line &lt;code&gt;Makefile&lt;&#x2F;code&gt;. To me it looks like mostly gibberish and some tests. In all fairness there is a lot of good things happening in there but it’s not good for learning how to write a &lt;code&gt;Makefile&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;A &lt;code&gt;Makefile&lt;&#x2F;code&gt; doesn’t always need to be that cray-cray. For my own projects I try to keep it pretty simple, though over time it generally becomes more complex. An example of mine, from my post on &lt;a href=&quot;&#x2F;barebones-linux-system&#x2F;&quot;&gt;building a barebones Linux system&lt;&#x2F;a&gt;, is on my github &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;KennethWilke&#x2F;my-barebones-linux&#x2F;blob&#x2F;master&#x2F;Makefile&quot;&gt;here&lt;&#x2F;a&gt;. I’m not the only person crazy enough to stick with a handwritten Makefile; the &lt;a href=&quot;https:&#x2F;&#x2F;redis.io&#x2F;&quot;&gt;Redis&lt;&#x2F;a&gt; database also uses a &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;redis&#x2F;redis&#x2F;blob&#x2F;79866a6361829ed0602dedff9cb378c66977227a&#x2F;src&#x2F;Makefile&quot;&gt;handwritten Makefile&lt;&#x2F;a&gt;, and Redis is production quality and awesome AF.&lt;&#x2F;p&gt;
&lt;p&gt;Let’s start looking at the basics of making your own Makefile!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;makefile-basics&quot;&gt;Makefile Basics&lt;&#x2F;h1&gt;
&lt;p&gt;Most of the time when you use &lt;code&gt;make&lt;&#x2F;code&gt;, it will be looking for a file named &lt;code&gt;Makefile&lt;&#x2F;code&gt; to find your targets. If you run &lt;code&gt;make&lt;&#x2F;code&gt; without a &lt;code&gt;Makefile&lt;&#x2F;code&gt;, you’ll be greeted with this lovely message:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;make: *** No targets specified and no makefile found. Stop.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you had say, an empty &lt;code&gt;Makefile&lt;&#x2F;code&gt;, you’ll see something along the lines of:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;make: *** No targets. Stop.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first thing you should be aware of regarding &lt;code&gt;Makefile&lt;&#x2F;code&gt; syntax is that &lt;strong&gt;tabs are part of the syntax!&lt;&#x2F;strong&gt; I’ve seen a few developers start building a Makefile and be like “WTF!” when nothing works because their text editor is configured to insert spaces when they hit tab.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;tabsvspaces.gif&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The general format of a Makefile is a a list of targets with optional dependencies and commands&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;&amp;lt;target&amp;gt;: [dependency] [dependency]
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;tab&amp;gt;[command]
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;tab&amp;gt;[command]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As an example, I’ll define a target &lt;code&gt;test&lt;&#x2F;code&gt; that will not have dependencies and that target will run some echo commands.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; test!
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; IT WERKS!!!!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this in my &lt;code&gt;Makefile&lt;&#x2F;code&gt;, if I run &lt;code&gt;make&lt;&#x2F;code&gt; with no arguments it’ll run my first target &lt;code&gt;test&lt;&#x2F;code&gt;. The common convention is for a &lt;code&gt;Makefiles&lt;&#x2F;code&gt; to start with the target &lt;code&gt;all&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;makeytest.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If I add a second target &lt;code&gt;moartest&lt;&#x2F;code&gt; and I want to run that one, I’ll need to specify it during my command as &lt;code&gt;make moartest&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; test!
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; IT WERKS!!!!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;moartest&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; woah now, so fancy
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;moarmaketest.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;makefile-dependencies&quot;&gt;Makefile Dependencies&lt;&#x2F;h1&gt;
&lt;p&gt;One of my favorite things about &lt;code&gt;make&lt;&#x2F;code&gt; is the way it handles dependencies. If you’re using it for building a project you can organize the steps however you’d like and structure a hierarchy where one step runs before another.&lt;&#x2F;p&gt;
&lt;p&gt;I’ll extend my previous example to add my &lt;code&gt;moartest&lt;&#x2F;code&gt; target as a dependency of the &lt;code&gt;test&lt;&#x2F;code&gt; target.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;moartest
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; test!
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; IT WERKS!!!!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;moartest&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; woah now, so fancy
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now when I run &lt;code&gt;make&lt;&#x2F;code&gt;, &lt;code&gt;test&lt;&#x2F;code&gt; will be inspected since it is the first target and since &lt;code&gt;test&lt;&#x2F;code&gt; has &lt;code&gt;moartest&lt;&#x2F;code&gt; in its list of dependencies that make will first look for that target and if it’s commands execute successfully the commands for &lt;code&gt;test&lt;&#x2F;code&gt; will also be ran.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;evenmoarmake-1.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If for some reason the dependency commands should fail, &lt;code&gt;make&lt;&#x2F;code&gt; will error out at that point. To simulate this I will add an &lt;code&gt;exit 1&lt;&#x2F;code&gt; command to my &lt;code&gt;moartest&lt;&#x2F;code&gt; target.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;moartest
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; test!
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; IT WERKS!!!!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;moartest&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; woah now, so fancy
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt; 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;make-errors.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If the target name is a file, and that file already exists, the target will be skipped. Here’s an example where my randomcrap target generates a file that’s a dependency of my test target.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;randomcrap
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; we have random!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;randomcrap&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dd&lt;&#x2F;span&gt;&lt;span&gt; if=&#x2F;dev&#x2F;urandom of=randomcrap bs=1024 count=1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;makerandom.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;A dependency doesn’t need to be a another target, in many cases it’s useful if a dependency is some source file. &lt;code&gt;make&lt;&#x2F;code&gt; will look to see if that source file has been updated and will re-run the target only when it seems necessary.&lt;&#x2F;p&gt;
&lt;p&gt;Consider this example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;copiedfile
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; we have the latest copy!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;copiedfile&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;originalfile
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cp&lt;&#x2F;span&gt;&lt;span&gt; originalfile copiedfile
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And observe how &lt;code&gt;make&lt;&#x2F;code&gt; responds to the absence of the source file, how it skips the file when it’s already the same as the original, and how updates to the original will be noticed during the subsequent run.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;dependency-update.gif&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;variables-in-makefiles&quot;&gt;Variables in Makefiles&lt;&#x2F;h1&gt;
&lt;p&gt;It is often useful to have some variables in your &lt;code&gt;Makefile&lt;&#x2F;code&gt;. Variables can be set with the &lt;code&gt;NAME=VALUE&lt;&#x2F;code&gt; syntax. In my first example here I’ll compile the following &lt;code&gt;hello.c&lt;&#x2F;code&gt; program:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argc&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;argv&lt;&#x2F;span&gt;&lt;span&gt;[]) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Well hello you proverbial world you.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To make my compilation of the program a bit more flexible I’ll make a &lt;code&gt;COMPILER&lt;&#x2F;code&gt; variable to setup what compiler I’d like it to use.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILER &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;gcc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hello-world
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;hello-world&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hello.c
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILER&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;-o&lt;&#x2F;span&gt;&lt;span&gt; hello-world hello.c
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;gcc-make.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now if I wanted to switch my various build targets to use &lt;code&gt;clang&lt;&#x2F;code&gt; instead, I can just modify my &lt;code&gt;COMPILER&lt;&#x2F;code&gt; variable.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;clang-make.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I could even move my program name and source file to their own variables, and reference that variable as my target and its dependencies.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILER &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;clang
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hello-world
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SOURCE &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hello.c
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SOURCE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILER&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;-o &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;) $(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;SOURCE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Outside of the variables you define yourself, there are also some &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;make&#x2F;manual&#x2F;html_node&#x2F;Automatic-Variables.html&quot;&gt;automatic variables&lt;&#x2F;a&gt; that can be pretty handy.&lt;&#x2F;p&gt;
&lt;p&gt;The three I use the most are &lt;code&gt;$@&lt;&#x2F;code&gt; which if used in a command will be the name of the target, &lt;code&gt;$&amp;lt;&lt;&#x2F;code&gt; which will be the first dependency for that target and &lt;code&gt;$^&lt;&#x2F;code&gt; which will be all of the dependencies for the target.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;automagic
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;automagic&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;automation magic
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;target: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$@&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;first dependency: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;all dependencies: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$^&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;automagics.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;These variables can be combined in interesting and useful ways. The automatic variables can even be embedded in your normal variables. Let’s say we have some C program that has a header and a code file, you’d want to rebuild the program if the header was changed but not include the header as an argument to the compiler. You could make your own compiler rule that includes most of the settings you want and define a pattern where the first dependency is included in the commands for the target.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;myprogram
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILE_PROGRAM &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;gcc -Wall -o &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;$@ $&amp;lt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;main.c main.h
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;COMPILE_PROGRAM&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;fancy-variables.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;additional-command-fu&quot;&gt;Additional Command-fu&lt;&#x2F;h1&gt;
&lt;p&gt;There are two other things I think that are useful to know when writing the commands for the targets.&lt;&#x2F;p&gt;
&lt;p&gt;So far we’ve seen all our output repeated, which is normally quite handy for debugging. If you feel like making your output a little prettier you can start your command with &lt;code&gt;@&lt;&#x2F;code&gt; to squelch the output.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;one moment&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt; 5
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;okay i&amp;#39;m back&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt; 2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;sleep.gif&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Another good thing to be aware of is that each command is ran from your current working directory. If you want to do something like make a directory, jump into it and do more work inside of it, you’ll have to run multiple commands in a single go.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;mkdir&lt;&#x2F;span&gt;&lt;span&gt; subdirectory
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; subdirectory
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;pwd
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; subdirectory; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;pwd
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;make-pwd.png&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If you find yourself with really long lines in your Makefile you can always add a backslash (&lt;code&gt;\&lt;&#x2F;code&gt;) before your new line to ask the &lt;code&gt;make&lt;&#x2F;code&gt; parser to ignore that as you’re just trying to make things pretty.&lt;&#x2F;p&gt;
&lt;p&gt;And that will wrap up my post on Makefiles! I hope you find this useful!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;making-with-make&#x2F;&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 11</title>
		<published>2017-11-29T00:00:00+00:00</published>
		<updated>2017-11-29T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-11/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-11/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 10</title>
		<published>2017-11-22T00:00:00+00:00</published>
		<updated>2017-11-22T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-10/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-10/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 9</title>
		<published>2017-11-15T00:00:00+00:00</published>
		<updated>2017-11-15T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-9/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-9/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 8</title>
		<published>2017-11-08T00:00:00+00:00</published>
		<updated>2017-11-08T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-8/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-8/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 7</title>
		<published>2017-11-01T00:00:00+00:00</published>
		<updated>2017-11-01T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-7/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-7/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 6</title>
		<published>2017-10-25T00:00:00+00:00</published>
		<updated>2017-10-25T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-6/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-6/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 5</title>
		<published>2017-10-18T00:00:00+00:00</published>
		<updated>2017-10-18T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-5/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-5/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 4</title>
		<published>2017-10-11T00:00:00+00:00</published>
		<updated>2017-10-11T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-4/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-4/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 3</title>
		<published>2017-10-04T00:00:00+00:00</published>
		<updated>2017-10-04T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-3/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-3/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 2</title>
		<published>2017-09-27T00:00:00+00:00</published>
		<updated>2017-09-27T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-2/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-2/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Beginning Logic Design – Part 1</title>
		<published>2017-09-22T00:00:00+00:00</published>
		<updated>2017-09-22T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/beginning-logic-design-part-1/" type="text/html"/>
		<id>https://suchprogramming.com/beginning-logic-design-part-1/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Getting BusyBox with It</title>
		<published>2017-09-20T00:00:00+00:00</published>
		<updated>2017-09-20T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/getting-busybox-with-it/" type="text/html"/>
		<id>https://suchprogramming.com/getting-busybox-with-it/</id>
		<content type="html">&lt;p&gt;Following up on the &lt;a href=&quot;https:&#x2F;&#x2F;suchprogramming.com&#x2F;barebones-linux-system&#x2F;&quot;&gt;Building a Barebones Linux System&lt;&#x2F;a&gt; post, today I will be adding &lt;a href=&quot;https:&#x2F;&#x2F;busybox.net&#x2F;&quot;&gt;BusyBox&lt;&#x2F;a&gt; to make the system a smidge more usable.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;what-is-this-busybox&quot;&gt;What is this BusyBox?&lt;&#x2F;h1&gt;
&lt;p&gt;BusyBox is a refers to itself as &lt;em&gt;The Swiss Army Knife of Embedded Linux&lt;&#x2F;em&gt;. It is a collection of common Unix tools living within a single binary that uses the magic of &lt;code&gt;argv[0]&lt;&#x2F;code&gt; to facilitate a lightweight implementation of common programs like &lt;code&gt;sh&lt;&#x2F;code&gt;, &lt;code&gt;ls&lt;&#x2F;code&gt;, &lt;code&gt;cat&lt;&#x2F;code&gt; and many more. It is extremely common in the embedded systems community because it is a well maintained open source project and can be built very small.&lt;&#x2F;p&gt;
&lt;p&gt;For my purposes, I will use BusyBox instead of going into a full &lt;a href=&quot;http:&#x2F;&#x2F;www.linuxfromscratch.org&#x2F;&quot;&gt;Linux From Scratch&lt;&#x2F;a&gt; route. This will give me a shell and a few other basic userspace tools needed to allow me to make some use of this bare bones system.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;automating-the-builds&quot;&gt;Automating the builds&lt;&#x2F;h1&gt;
&lt;p&gt;At this point I have been dabbling with this bare bones setup long enough that I want some additional tooling to help manage rebuilding as necessary. There are many good tools out there for managing custom Linux builds, but for now I’d like to continue using custom tools to learn more of what it takes to build a Linux distro from square one.&lt;&#x2F;p&gt;
&lt;p&gt;I’ve made a &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;KennethWilke&#x2F;my-barebones-linux&#x2F;tree&#x2F;26dfc9441899541bf05aab9b07f22bf5ed9fb689&quot;&gt;repo on Github&lt;&#x2F;a&gt; and a Makefile that will pull in the kernel, build it, and build my initramfs. It also has a &lt;code&gt;runvm&lt;&#x2F;code&gt; command that will help me test my builds quickly.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Makefile&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Makefile &quot;&gt;&lt;code class=&quot;language-Makefile&quot; data-lang=&quot;Makefile&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_VERSION&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;4.13.3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_DIRECTORY&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;linux-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_VERSION&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_ARCHIVE&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_DIRECTORY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.tar.xz
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;KERNEL_URL=https&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;&#x2F;cdn.kernel.org&#x2F;pub&#x2F;linux&#x2F;kernel&#x2F;v4.x&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_ARCHIVE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;vmlinuz initramfs
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Kernel build targets
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;vmlinuz&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_DIRECTORY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;cd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_DIRECTORY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;) &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;make&lt;&#x2F;span&gt;&lt;span&gt; defconfig &amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;make -j&lt;&#x2F;span&gt;&lt;span&gt;`&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nproc&lt;&#x2F;span&gt;&lt;span&gt;`
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_DIRECTORY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;arch&#x2F;x86_64&#x2F;boot&#x2F;bzImage vmlinuz
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_DIRECTORY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;wget &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_URL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;tar&lt;&#x2F;span&gt;&lt;span&gt; xf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_ARCHIVE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Initramfs build targets
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;initramfs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;initfs initfs&#x2F;init
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; initfs&#x2F; &amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;find&lt;&#x2F;span&gt;&lt;span&gt; . | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;cpio -o --format&lt;&#x2F;span&gt;&lt;span&gt;=newc &amp;gt; ..&#x2F;initramfs
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;initfs&#x2F;init&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;initfs init.c
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;gcc -o&lt;&#x2F;span&gt;&lt;span&gt; initfs&#x2F;init&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -static&lt;&#x2F;span&gt;&lt;span&gt; init.c
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;initfs&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;mkdir -p&lt;&#x2F;span&gt;&lt;span&gt; initfs&#x2F;bin
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Utility targets
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;runvm&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;vmlinuz initramfs
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;qemu-system-x86_64 -m&lt;&#x2F;span&gt;&lt;span&gt; 2048&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -kernel&lt;&#x2F;span&gt;&lt;span&gt; vmlinuz&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -initrd&lt;&#x2F;span&gt;&lt;span&gt; initramfs
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;clean&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;	&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rm -rf&lt;&#x2F;span&gt;&lt;span&gt; vmlinuz &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_DIRECTORY&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;) $(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;KERNEL_ARCHIVE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h1 id=&quot;configuring-and-building-busybox&quot;&gt;Configuring and Building BusyBox&lt;&#x2F;h1&gt;
&lt;p&gt;BusyBox uses buildroot so its configuration and build process are very similar to the kernel itself. After downloading and extracting the source I’ll use &lt;code&gt;make menuconfig&lt;&#x2F;code&gt; to use the ncurses based config editor.&lt;&#x2F;p&gt;
&lt;p&gt;I’ll be leaving most settings at their default, but will configure the build to generate a static binary since I don’t yet have a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;C_standard_library&quot;&gt;libc&lt;&#x2F;a&gt; implementation to link against.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;getting-busybox-with-it&#x2F;busyboxing.gif&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;With BusyBox built, I’ll add it to my &lt;code&gt;initfs&lt;&#x2F;code&gt; directory so that my Makefile includes it in my initramfs.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;getting-busybox-with-it&#x2F;busyinitfs.gif&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;busybox&lt;&#x2F;code&gt; binary will act differently based on what it is called as, if we renamed it to &lt;code&gt;sh&lt;&#x2F;code&gt; it would act as a shell, or if named &lt;code&gt;ls&lt;&#x2F;code&gt; it will list our files and such. To allow it to serve all of these functions I’ll check what commands my build implements and create &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Symbolic_link&quot;&gt;symlinks&lt;&#x2F;a&gt; for each program it provides.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;getting-busybox-with-it&#x2F;busylinking.gif&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;switching-the-build-to-busybox&quot;&gt;Switching the Build to BusyBox&lt;&#x2F;h1&gt;
&lt;p&gt;With my process for building BusyBox roughly figured out, I’ll save a copy of my BusyBox config, so that future builds will also be static linked. I’ll also include a small shell script that builds my symlinks.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; initfs&#x2F;bin
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;`&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;busybox --list&lt;&#x2F;span&gt;&lt;span&gt;`
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ln -s&lt;&#x2F;span&gt;&lt;span&gt; busybox $&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;done
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that I have BusyBox in my initramfs, I’ll switch my init process from my custom binary to a shell script that will forever spawn a shell to use.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;#!&#x2F;bin&#x2F;sh
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sh
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt; 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;done
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this script in place I’ll update my Makefile and repo to include the changes I’ve made here.&lt;&#x2F;p&gt;
&lt;p&gt;And now we test!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;getting-busybox-with-it&#x2F;busytesting.gif&quot; alt=&quot;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There are a few deficiencies in here still, e.g. &lt;code&gt;ps&lt;&#x2F;code&gt; won’t work because I have no &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Procfs&quot;&gt;procfs&lt;&#x2F;a&gt; mounted and &lt;code&gt;sh&lt;&#x2F;code&gt; will complain about being unable to access tty.&lt;&#x2F;p&gt;
&lt;p&gt;To get a few more things to run sanely, I’ll add &lt;code&gt;&#x2F;dev&lt;&#x2F;code&gt;, &lt;code&gt;&#x2F;proc&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;sys&lt;&#x2F;code&gt; to my initramfs and modify my init script to mount the procfs and &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Sysfs&quot;&gt;sysfs&lt;&#x2F;a&gt;. I’ll also run &lt;code&gt;mdev -s&lt;&#x2F;code&gt; which is provided by BusyBox to populate &lt;code&gt;&#x2F;dev&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;sys&lt;&#x2F;code&gt;. Lastly, I’ll launch my shell with &lt;code&gt;setsid cttyhack sh&lt;&#x2F;code&gt; so that &lt;a href=&quot;http:&#x2F;&#x2F;tldp.org&#x2F;LDP&#x2F;abs&#x2F;html&#x2F;x9644.html&quot;&gt;job control&lt;&#x2F;a&gt; is enabled.&lt;&#x2F;p&gt;
&lt;p&gt;With all that setup, the system is substantially more usable! I’ll &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;KennethWilke&#x2F;my-barebones-linux&#x2F;commit&#x2F;424fc5491a12cea2ec35c4dfa1ca02c7a24eaafc&quot;&gt;push these changes to my repo&lt;&#x2F;a&gt; and call it a wrap.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Barebones Linux on Barreleye G1</title>
		<published>2017-09-11T00:00:00+00:00</published>
		<updated>2017-09-11T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/barebones-linux-barreleye/" type="text/html"/>
		<id>https://suchprogramming.com/barebones-linux-barreleye/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Building a Barebones Linux System</title>
		<published>2017-09-08T00:00:00+00:00</published>
		<updated>2017-09-08T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/barebones-linux-system/" type="text/html"/>
		<id>https://suchprogramming.com/barebones-linux-system/</id>
		<content type="html">&lt;p&gt;In this post, I will describe the process I’ve used to create a very minimal &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Linux&quot;&gt;Linux&lt;&#x2F;a&gt; build. It is minimal for the sake of learning, to get a clearer picture of how barebones a operating system could be put together. The build will consist of an &lt;a href=&quot;https:&#x2F;&#x2F;suchprogramming.com&#x2F;barebones-linux-system&#x2F;%22https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Kernel_(operating_system)%22&quot;&gt;operating system kernel&lt;&#x2F;a&gt; and an &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Initial_ramdisk&quot;&gt;initial ram disk&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I will be using Ubuntu 16.04 on my laptop as my build box, and use QEMU VMs as my test systems. If you’d like more information on building a linux kernel &lt;a href=&quot;https:&#x2F;&#x2F;kernelnewbies.org&#x2F;KernelBuild&quot;&gt;there is a decent tutorial on kernelnewbies&lt;&#x2F;a&gt; as well as &lt;a href=&quot;https:&#x2F;&#x2F;wiki.gentoo.org&#x2F;wiki&#x2F;Complete_Handbook&#x2F;Building_the_Linux_kernel&quot;&gt;the kernel building section of the Gentoo handbook&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;fieldset class=&quot;ad&quot;&gt;
        &lt;legend&gt;ad&lt;&#x2F;legend&gt;
        &lt;script async src=&quot;https:&#x2F;&#x2F;pagead2.googlesyndication.com&#x2F;pagead&#x2F;js&#x2F;adsbygoogle.js?client=ca-pub-5305013514298750&quot;
                crossorigin=&quot;anonymous&quot;&gt;&lt;&#x2F;script&gt;
        &lt;ins class=&quot;adsbygoogle&quot;
             style=&quot;display:block; text-align:center;&quot;
             data-ad-layout=&quot;in-article&quot;
             data-ad-format=&quot;fluid&quot;
             data-ad-client=&quot;ca-pub-5305013514298750&quot;
             data-ad-slot=&quot;6574356918&quot;&gt;&lt;&#x2F;ins&gt;
        &lt;script&gt;(adsbygoogle = window.adsbygoogle || []).push({});&lt;&#x2F;script&gt;
&lt;&#x2F;fieldset&gt;&lt;h1 id=&quot;the-linux-kernel&quot;&gt;The Linux Kernel&lt;&#x2F;h1&gt;
&lt;p&gt;It can be difficult to see the separation of what is Linux kernel and what is the Linux distribution. The Linux Kernel itself is there primarily to facilitate access to hardware and provide a safe haven for processes to live and breathe. The kernel’s user interface is its API. It provides a foundation for all other software to allow storing files, using networks, render graphics, handle keyboard input and so on. On top of the kernel your terminal, your login process, your window manager, your browser, etc. are usually provided by your &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Linux_distribution&quot;&gt;distribution&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Obtaining and building the kernel is very simple, though configuring the kernel can be a little trickier depending on your needs. For the sake of this excersize the default configurations for an x86_64 system will do just fine.&lt;&#x2F;p&gt;
&lt;p&gt;First off, I’ll create a new directory to work out of and get started. As of today, the latest linux kernel at the top of &lt;a href=&quot;https:&#x2F;&#x2F;www.kernel.org&#x2F;&quot;&gt;kernel.org&lt;&#x2F;a&gt; is &lt;code&gt;4.13&lt;&#x2F;code&gt;, I will download this latest tar file, extract it and jump into the extracted directory.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-system&#x2F;download-kernel.gif&quot; alt=&quot;Downloading the kernel&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now that I have the kernel source files, I’ll use &lt;code&gt;make defconfig&lt;&#x2F;code&gt; to configure the defaults for a standard x86_64 machine and will do &lt;code&gt;make -j4&lt;&#x2F;code&gt; to compile the kernel using all 4 threads my laptop has to offer.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-system&#x2F;kernel-startbuild.gif&quot; alt=&quot;Starting the kernel build&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Even with the extra threads, it takes about 8 minutes to compile on my machine. It’s not the simplest kernel as there are many default features we could disable to trim down the kernel size and compile time but I’m not worried about that for this build. Once it’s complete I copy the &lt;code&gt;bzImage&lt;&#x2F;code&gt; file that was generated into my barebones parent directory.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-system&#x2F;kernel-done.gif&quot; alt=&quot;Kernel build finishing&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;init-process-and-initramfs&quot;&gt;Init process and initramfs&lt;&#x2F;h1&gt;
&lt;p&gt;Typically when a computer boots, the host firmware will look for a bootloader on a bootable device. The bootloader will load the kernel and pass control of the system to it. The kernel gets control, does it’s own startup shenanigans and looks for a program to run as the primary (init) userspace process (PID 1).&lt;&#x2F;p&gt;
&lt;p&gt;In most Linux distributions, this init process can be a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Init&quot;&gt;SysV style init process&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;www.freedesktop.org&#x2F;wiki&#x2F;Software&#x2F;systemd&#x2F;&quot;&gt;SystemD&lt;&#x2F;a&gt;, &lt;a href=&quot;http:&#x2F;&#x2F;upstart.ubuntu.com&#x2F;&quot;&gt;Upstart&lt;&#x2F;a&gt; or &lt;a href=&quot;https:&#x2F;&#x2F;wiki.gentoo.org&#x2F;wiki&#x2F;OpenRC&quot;&gt;OpenRC&lt;&#x2F;a&gt;. In this case, it’s going to be a looping hello world program. I compile it with &lt;code&gt;-static&lt;&#x2F;code&gt; so that libraries are &lt;a href=&quot;https:&#x2F;&#x2F;suchprogramming.com&#x2F;barebones-linux-system&#x2F;%22https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Linker_(computing)#Static_linking%22&quot;&gt;staticly linked&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-system&#x2F;init-program.gif&quot; alt=&quot;Compiling a simple hello world looping program&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The next step is to build the initramfs, the initial file system that will go in ram for the kernel to interact with. To do this, I will &lt;a href=&quot;https:&#x2F;&#x2F;ryanstutorials.net&#x2F;linuxtutorial&#x2F;piping.php&quot;&gt;pipe&lt;&#x2F;a&gt; a newline separated list of files to pack to &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Cpio&quot;&gt;cpio&lt;&#x2F;a&gt;. I only have one file so I’ll just echo that to it. &lt;code&gt;cpio&lt;&#x2F;code&gt; will output the archive and I’ll store that to a file.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-system&#x2F;initramfs.gif&quot; alt=&quot;Building an uncompressed initramfs with a single file&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;testing-the-new-build-in-qemu&quot;&gt;Testing the new build in QEMU&lt;&#x2F;h1&gt;
&lt;p&gt;I’ll be using qemu to test the new build. The simplest way I could boot the VM would be to use &lt;code&gt;qemu-system-x86_64 -kernel vmlinuz -initrd initramfs&lt;&#x2F;code&gt;, but to run this within my terminal I’ll also add &lt;code&gt;-append console=ttyS0 -nographic&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;barebones-linux-system&#x2F;qemu-launch.gif&quot; alt=&quot;Testing the kernel in QEMU&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It boots fast and my hello world endlessly spits out its message! My barebones OS works! I may eventually use this to build a few utility images to network boot but for now this will do as a base for more tinkering.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Using CAPI within a Docker container</title>
		<published>2017-02-14T00:00:00+00:00</published>
		<updated>2017-02-14T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/using-capi-within-a-docker-container/" type="text/html"/>
		<id>https://suchprogramming.com/using-capi-within-a-docker-container/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Simulating CAPI Designs with PSLSE and Vivado</title>
		<published>2016-09-26T00:00:00+00:00</published>
		<updated>2016-09-26T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/simulating-capi-designs-with-pslse-and-vivado/" type="text/html"/>
		<id>https://suchprogramming.com/simulating-capi-designs-with-pslse-and-vivado/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Peeking into the Alpha-Data KU3</title>
		<published>2016-06-28T00:00:00+00:00</published>
		<updated>2016-06-28T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/peeking-into-the-alpha-data-ku3/" type="text/html"/>
		<id>https://suchprogramming.com/peeking-into-the-alpha-data-ku3/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Hello AFU on Alpha-Data KU</title>
		<published>2016-06-14T00:00:00+00:00</published>
		<updated>2016-06-14T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/hello-afu-on-alpha-data-ku3/" type="text/html"/>
		<id>https://suchprogramming.com/hello-afu-on-alpha-data-ku3/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Hello AFU – Part 6</title>
		<published>2016-03-08T00:00:00+00:00</published>
		<updated>2016-03-08T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/hello-afu-part-6/" type="text/html"/>
		<id>https://suchprogramming.com/hello-afu-part-6/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Hello AFU – Part 5</title>
		<published>2016-03-07T00:00:00+00:00</published>
		<updated>2016-03-07T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/hello-afu-part-5/" type="text/html"/>
		<id>https://suchprogramming.com/hello-afu-part-5/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Hello AFU – Part 4</title>
		<published>2016-03-02T00:00:00+00:00</published>
		<updated>2016-03-02T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/hello-afu-part-4/" type="text/html"/>
		<id>https://suchprogramming.com/hello-afu-part-4/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Hello AFU – Part 3</title>
		<published>2016-02-19T00:00:00+00:00</published>
		<updated>2016-02-19T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/hello-afu-part-3/" type="text/html"/>
		<id>https://suchprogramming.com/hello-afu-part-3/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Hello AFU – Part 2</title>
		<published>2016-02-17T00:00:00+00:00</published>
		<updated>2016-02-17T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/hello-afu-part-2/" type="text/html"/>
		<id>https://suchprogramming.com/hello-afu-part-2/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Hello AFU – Part 1</title>
		<published>2016-02-15T00:00:00+00:00</published>
		<updated>2016-02-15T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/hello-afu-part-1/" type="text/html"/>
		<id>https://suchprogramming.com/hello-afu-part-1/</id>
		<content type="html"></content>
	</entry>
	<entry xml:lang="en">
		<title>Tinkering with CAPI</title>
		<published>2016-01-18T00:00:00+00:00</published>
		<updated>2016-01-18T00:00:00+00:00</updated>
		<link href="https://suchprogramming.com/tinkering-with-capi/" type="text/html"/>
		<id>https://suchprogramming.com/tinkering-with-capi/</id>
		<content type="html"></content>
	</entry>
</feed>
