Marmmodore-1K: Fantasy and Tiny 8 bit computer
Writing a tiny fantasy 8-bit computer in 1 KB of JavaScript
Marmmodore-1K or Marmmodore-1000 is a fantasy 8 bit computer developed for the JS1K contest of 2016. It’s inspired by the MOS 6502 chip and the Commodore 64. It comes with a very primitive assembler and it has very limited set of instructions. 17 to be exact.
You can try out the code here DEMO.
SPECS
1) 1000 Bytes of RAM
2) It uses the last 512 bytes of RAM as frame buffer and stack.
3) The frame buffer starts at address $02E8 (744) and ends at $03E7 (999). The stack goes from $01E8 (488) to $02E7 (743).
4) Keyboard input is stored at address $01E7 (487).
5) 17 CPU instructions.
INSTRUCTION SET
#AD
ADd
Pops the 2 last elements
on the stack and adds them. Then
stores the vale on the accumulator.
#SB
SuBtract
Pops the 2 last elements on the
stack and subracts them. Then
stores the vale on the accumulator.
#PH
PusH
Pushes the accumulator into the stack
#PP
PoP
Pops the last element of the stack
in the accumulator.
#MA $FFFF
Memory to Accumulator
Loads value from memory into the
accumulator using 16 bit address
#SA $FFFF
Store Accumulator
Stores accumulator value into memory
using 16 bit address
#LA $FF
Load Accumulator
Loads 8 bit value into accumulator
#IN
INcrement
Increments the accumulator
#DE
DEcrement
Decrements the accumulator
#JP $FFFF
JumP
Equivalent to GOTO or JUMP. It'll set
program counter to the 16 bit address
argument.
#SO $FFFF
Store accumulator with stack Offset
This is an indexed store. It's like
"ma" instruction but it uses the las
element on the stack as an offset.
It's something like
mov a, $FFFF + stack.pop()
#CP $FF
ComPare
Compares the accumulator with 8 bit
value and pushes the result into
the stack
#JE $FF
Jump if Equal
Branches if last element on the
stack is 0
#JN $FF
Jump if Not equal
Branches if last element on the
stack is not 0
#JL $FFFF
Jump if Lower than
Branches if last element on the
stack is lower than 0
#JG $FFFF
Jump if Greater than
Branches if last element on the
stack is greater than 0
#BR
BReak
Breaks the execution of the program.
EXAMPLE
1) This will print 256 colors in the screen.
2) This will get the keyboard input and store it on the frame buffer. It’ll do it for each pixel.
The JS1K code
Here you can see the extreme compression of the code. I used a mix of Google’s Closure Compiler and RegPack to do this “crushing of the code”.
This code is exactly 1024 Bytes or 1KB which is the main limit of the JS1K contest.
The uncompressed code looks like this. It’s much more clear with all the comments.