About The Contest
The first ever Olympiad of Misguided Geeks contest at Worse Than Failure (or OMGWTF for short) is a new kind of programming contest. Readers are invited to be creative with devising a calculator with the craziest code they can write. One lucky and potentially insane winner will get either a brand new MacBook Pro or comparable Sony VAIO laptop.
Entry #100336: VICE, Virtual Integrated Circuit Engine
by Randolpho St. John
I hate writing buggy code (one of the reasons I visit worsethanfailure.com is to help me avoid it) so rather than take the buggy route for this contest I decided to take the "clever" route.
The idea for VICE came from an oddball thought I had once, long ago: Virtual Machines would need Virtual Transistors to work. I recalled that thought for the contest and figured it might be a clever way of doing things, so I decided to give it a go.
I dusted off my old digital logic books and did a little studying, then tried my hand. A somewhat fantastical account of my approach can be found in the readme file with the project. Please, the story of the idea was meant as a joke. Really, I did *not* abuse NyQuil. Honest.
I built VICE one component at a time. The VirtualTransistor and VirtualConnection came quickly. From them are built the VirtualAndGate and the VirtualOrGate, which when coupled with the VirtualInverter form the VirtualNandGate and VirtualNorGate. From these logic gates are built the VirtualMultiplexer_4_1 and the VirtualBinaryAdder. The VirtualBinaryAdder is in turn the main component of the Virtual4BitAdder, Virtual4BitAdderSubtractor, and the Virtual32BitAdderSubtractor.
At this point I was forced to make a hard decision -- I *wanted* to make the VirtualFlipFlop from VirtualAndGates and VirtualOrGates, but because a FlipFlop is based on a feedback loop, I couldn't see how to avoid an infinite loop. Up until this point, all of my VirtualComponents had been wired faithfully according to standard electronic schematics. "Virtual current" would flow from one logic gate to the next in the same way it would in a real integrated circuit, albeit with certain abstractions. In the end, I decided to fudge it, and gave my VirtualFlipFlop internal software algorithmic logic, rather than follow any virtual schematic like other components. I'm not proud of that, but it was a necessary trade-off.
After that decision was made, things began to flow again; I built the Virtual4BitRegister (a universal parallel in/out left/right shift register) and its 32- and 64-bit sisters from the VirtualFlipFlop and logic gates, and then built the Virtual4BitMultiplier from Virtual4BitRegisters and Virtual4BitAdders.
At this point I was forced again to make some more hard decisions. I was rapidly approaching the deadline for the contest, and I hadn't even gotten to floating point operations! How was I supposed to complete my VirtualALU, VirtualCPU, and VirtualMemory_4MB? Those were my goals. Alas, I was forced to cut corners. I finished a Virtual4BitMultiplier and its 32-bit cousin, but didn't use a virtual state machine, and skipped division altogether. I wired everything up to the skeleton solution provided for the contest, ran a few tests (passed with flying colors, w00t) and submitted.
The VICE library is straight-forward, well documented, strongly object oriented, mostly-portable C++. I also believe it's bug free; you can run the included suite of unit tests yourself, if you like.
Hopefully, the judges will think my approach was novel enough for the win.