pointless.net  • Blog • Photos • About


2014- 02-03

A JavaScript/canvas port of KVM.kss

February 3, 2014

A few years ago I was playing around with the KDE desktop and found the "Virtual Machine" screensaver.

At first I was puzzled by its screen full of apparently random coloured numbers and a letters, but after it was left to run for some time, something weird happened - the random numbers and letters started forming repeating patterns.

After reading the source code I realised that the letters where instructions for a very simple stack based computer, and the numbers were data. The screen represented the memory of the computer, the numbers data, and the letters instructions.

Over a long period of time some of the memory locations would end up containing a self-replicating program, and that program (or programs) would then have many copies of them made and they would slowly take over the screen.

The screensaver wasn't easy to play with so I forgot about it for a while. I've been wanting to refresh my JavaScript skills and learn the canvas API's, so porting kvm.kss to HTML5/JavaScript/canvas was a good way of satisfying both itches.

Instruction set:

W
Wait - do noting
P
Pop - discard the top item on the stack
E
Execute - take the top item from the stack add it to the current location, then carry on executing from the new location.
S
Stop - stop executing
C
Copy - take the top 3 items from the stack, the first is the relative address behind the current location to start copying from, the 2nd is the location ahead of the current location to start copying to, and the 3rd is the number of cells to copy.

If the stack has more than 15 items on it, stop running. If the stack is empty when trying to pop a value from it, stop running. Stop executing after 1000 instructions have been run.

When running stops, start running from a random location with an empty stack and the instruction count set to zero.

This machine is similar (identical?) to the 'Core Wars' virtual machine.

Clicky here to see the system running. Code is here

tags: hacks canvas javascript



Valid HTML 4.01! Valid CSS! ipv6 ready