Friday, March 22, 2013

Transistors over HTTP

Sometimes you should probably just ignore a waking dream, and that's probably what I should have done here but what the heck. I like doing somewhat crazy things just for the heck of it. There's a lot of reducto ad absurdum kinds of things on the net like javascript engines written in javascript and IP over avian carriers, so I figured why not go for the whole enchilada: transistors which are the basis of everything on computers and fabricate them out of php and http rather than silicon and wire.

So here's what I did. I created three modules, fab.php, scope.php and transistor.php. Fab creates/modifies transistors, and scope tests the input and outputs of the pins of our http transistor (oscilloscope, duh). Each site has a sqlite database of the transistors it serves, their configuration, and state.

A typical transistor has three pins: a collector, a base and the emitter. The collector and base are inputs, and the emitter is the output depending on the state of the two inputs. For mine, I just wired this as a logical AND which I'm pretty sure is not really what NPN transistors (for example) perform, but what the heck I'm not a real EE so I can make my base transistor perform the logic I want it to.

The way we change the pin inputs on our transistor is to inform the transistor of the pin's state using transitor.php:

http://mtcc.com/ee/transistor.php?id=1&pin=base&state=1

This tells the base of transistor 1 on mtcc.com that it's state is now a 1. If we want to change it:

http://mtcc.com/ee/transistor.php?id=1&pin=base&state=0

and now it's state is 0. Simple right? The trick here is to wire up a network of transistors like you would with a real circuit. That's where our wire -- http -- comes in. Inside our transistor is an output field which tells our transistor to contact its neighbor (if any, and in reality it should be one to many). If it has a neighbor a change in output state of the emitter causes it to curl:

http://mtcc.com/ee/transistor.php?id=2&pin=base&state=0

Note id=2 which is its neighbor, and the state=0 because our transistor is an AND function and the base is now zero. So when we change our emitter state on id=1 back to 1:

http://mtcc.com/ee/transistor.php?id=1&pin=base&state=1

assuming the collector is already 1, it will then contact id=2's base with state=1 causing id=2's emitter to now read 1!

Here's a diagram of a network I actually implemented:


In principle I could make lots of different types of transistors -- and probably other components too like diodes -- but this was a fit of dementia and two hours of work, so sew me. The other cool thing about this is that it can span any number of servers implementing the transistor. The one thing I haven't actually tested is feedback like flip-flops. I do have dampening logic to only send changes when the emitter's output changes, but until proven I'm a bit afeared.