Build a CPU from virtual transistors – Part 1 – Setup

NeverTooOldToCode builds a functioning CPU from virtual relays in Racket, following Charles Petzold’s Code: the Hidden Language of Computer Hardware and Software

So, let’s set this up.

All elements we’re going to use (relays, gates, etc) can be seen as boxes with input interfaces that you can feed with 0s and 1s, some process in the middle and output interfaces which spit out the newly computed 0s and 1s:

 So, we’ll define a base Class   basic-element%   (by convention Racket Class names and with a “%”) that contains fields for the input and output interfaces, methods for accessing and setting the values (0 or 1) of these interfaces, and a method defining the processing that turns the input into output. All subsequent building blocks will be subclasses of this   basic-element%  Class.

The interfaces will have names (for instance A-in, B-in) and associated values (0 or 1), which suggests a hash table as a convenient data structure. We’ll have two of those, one for input and one for output interfaces:

This is how the Code looks in Racket:

Let’s go through this line by line.

The Class definition, its parent class (the built-in  object%  Class from which all other Classes in Racket are derived), and its mandatory initialization (super-new)

The fields for the input and output interfaces, initialized to one empty hash table for each.

The get and set methods for the fields. The /public  means that they can be called from outside the object (which is what we want).

The method that will do the actual processing. Here it’s set to  void  (which means: do nothing). We will have to override it for the subclasses that will describe the actual building blocks.

And that’s it!

Next installment we will build our very first actual building block: a relay. We’ll also take a look at Unit Testing.

Leave a Reply

Your email address will not be published. Required fields are marked *