Code to Models
This repository captures the contents of the book, Code to Models: A Microcontroller from the Bottom Up. This book is envisioned as three parts. Currently, Part I is substantially complete. Part II is about half completed. It contains the data management portions of the Reactive System Execution Environment (ReSEE). Execution sequencing and synchronization are not yet available. Part III is empty.
Downloads
A PDF of the current version of the book is available.
As of Version 3.0.0 and onward, all code in the book is in the Zig language and the code and documentation is built using the Zig build system. To make it easier to view the code without having to tangle it out of the asciidoc source, the tangle source files are checked into the repository. In this way they are easier to obtain by simply cloning the repository and opening it into a working directory. In the future, explicit dependencies required to build the document and the source files from the asciidoc source will be cataloged.
Why I chose Zig
As background, this book originally started with Part I coded in "C". I'm an experienced "C" coder and I have designed and built execution environments using "C" in the past. After Part I was completed, I took some time off both to finish some other projects and to decide how I was going to design Part II. Somewhere in that off time, I stumbled upon Zig while lost in some corner of the Internet. It was around the time of Zig version 0.9 or 0.10. I downloaded the compiler and tried to write some small amounts of code, but was not particularly successful. But neither was I trying hard. It was more like poking at a snake.
What had originally attracted me to Zig was the ability to define new data types by comptime
code
execution.
This was important to me because I wanted to design the data management facilities of ReSEE using relational
algebra. Again, this is an area where I had done some work previously. My previous attempts at taking relational
concepts to a microcontroller environment based on "C" were a struggle.
In one case, I had used a large quantity of "C" pre-processor macros and in another case an embedded command language.
Both could be made to work, but it was challenging, requiring deep knowledge of the underlying approach.
One characteristic of the problem that made the Zig approach viable was that no new relational variables are created at run time and all operations are clearly known at compile time. I could not postpone continuing onto Part II any longer and decided to try my hand with Zig in earnest and see if it could generate the necessary types for a relational algebra library. The result is contained in Chapter 13 of Models to Code.
The reason I chose Zig is that it was the right tool for the work I was trying to accomplish. The combination of compile time execution and built-in compiler functions for both type introspection and type creation create a procedural typing mechanism that is an integral part of the Zig language itself.