2017-03-09

World of wires

Or Wirewold for short.

In good old days I spent an inordinate amount of time doing weird things, such as drawing and simulating spaceships and battles using humble MS Paint (that itself is worth making a post on its own - if for nothing else but to explain myself). What would I give to see my painstakingly hand-crafted animations to come to life all on their own! I wish I knew of Wireworld back then.

Wireworld is a cellular automaton designed by Brian Silverman in 1987 (yay, I was born the same year) to simulate the behaviour of digital circuits. Its Wiki page explains the rules briefly, but to the point: https://en.wikipedia.org/wiki/Wireworld.

Basically, you have a grid of 'pixels', each of which can be in four different states: empty, wire, head of the electron or tail of the electron.
Each turn the pixels change their state according to the following rules:
1. empty remains empty,
2. tail of the electron becomes the wire,
3. head becomes tail,
4. wire becomes a head if, and only if 1 or two adjacent cells (4-connectivity!) are heads.

These rules make it possible to construct all sorts of circuits (logic gates, clocks, flip-flops, frequency multipliers etc.). You can see several examples on the Wiki page.

Isn't it hypnotizing to watch the electrons zooming around on beautiful copper wires? I can watch them all day. Especially in this picture:

Fig.1. A computer calculating primes implemented in Wireworld (by http://www.quinapalus.com).
The circuit presented in Fig. 1 is a complete (and working) computer implemented in Wireworld (made by http://www.quinapalus.com). What's more, it is programmed to calculate and display primes! I remember running the simulation for many, many hours before I managed to witness it jump to next prime in line.
(Here you can watch the recording of the Wireworld computer: https://youtu.be/jnIs7n9-LKs - I still have to figure out how to encode the videos to be clearer though).

The inner workings of the simulated device are presented in great detail on its webpage. It describes the philosophy behind the clocks, logic gates, ALU and the memory of the machine (it's the long skewed bank of horizontal parallel lines on the right side of the image). It's interesting to read how it operates.

And they do have the assembly code of the CPU described there. You could program the computer to do something else altogether!

I made some implementations of Wireworld (long ago) to play with the concept. These were the days when my complete lack of knowledge about good programming practices didn't stop me to try and to write all sorts of things. You can only imagine how I feel looking back at the code. Nevertheless, here it is (you have been warned!): https://github.com/dagothar/wireworld

The programs were made to run on Ubuntu, but should work on Windows as well, if you manage to configure the environment. They use the allegro graphic libraries (http://allegro.cc), which you can install on Ubuntu easily:

$ sudo apt-get install liballegro4-dev

Then it's only a matter of makeing the projects. Sorry, there's not much support that I can give if things do not work out! I myself cannot make heads and tails out of the scramble.

Let me explain the programs briefly.

1.  wireworld program
 This one is more of a sandbox. The board is quite small, and the data is represented as a char array and stored in text files. You can paint on the board with the mouse; scroll wheel changes the type of the pixel that you paint (empty, wire, head, tail). Spacebar starts/stops the simulation. Escape closes the window. The default diagram loaded is that of a binary adder module and Gray to Binary decoder (see Fig. 2.). You can save your work (to save.wir file) by pressing the S key. L loads the file.
Fig. 2. Wireworld program. Gray2Bin decoder presented.


2.  wire program
This one is more versatile and powerful. The program accepts a .bmp file as input, e.g.:

$ ./wire circuit.bmp

 The colors of wires, heads and tails (in that order) are taken from the pixels in the upper left corner of the picture (top row). Spacebar pauses and starts the simulation. You can use S/L to store and load the diagram. The BMP file for the computer is attached (see Fig. 3.). If you wish to make your own design, simply draw one in any Paint-like program!
Fig.3. Wire program. Quinaplus computer loaded and running.

I should make a new implementation of the simulator one of these days!

No comments:

Post a Comment