Now, I know that CPUs have been done quite a few times in Minecraft, so this probably won't strike you as mindblowing, but I still had a fun time making it and I thought I'd share it with you guys.
So over the last three days, I've been building an 8-bit CPU in Minecraft using the magnificent Redpower mod.
It has 4 8-bit registers, 20 bytes of program ROM, an adder/subtracter unit and 4 instructions. Have a few screenshots before I go on with my rambling:
Click the images for larger resolutions.
Top left: ALU/multiplexers
Top middle: Instruction decoder/control unit/incrementer (for the program counter)
Bottom Middle: Register file/BUS
Middle right: Register address decoder
Top right: ROM
Closer look at the ALU and multiplexers
Closer look at the ROM.
So of course, the CPU has a lot of limitations - with no RAM to speak of and only four registers available (or three, actually, since one of them is reserved for the program counter), it's a bit hard to do something useful for it.
But still, I was able to write a program on it calculating the Fibonacci numbers, so I guess that's something.
To give a small overview, the instructions it currently supports are:
Add dest, regA, regB: Set the contents of the register at 'dest' to the result of 'regA' + 'regB'
Sub dest, regA, regB: Set the contents of the register at 'dest' to the result of 'regA' - 'regB'
Mov dest, reg : Copy the contents of register 'reg' into the register at 'dest'
MovImmediate dest, Immediate: Copy the 4-bit immediate value (constant value coded into the instruction) into the register at 'dest'
'dest', 'regA' and 'regB' are two bit addresses of registers, so you are free to combine any register in any instruction. You could even add the contents of a register to itself and store the result in the same register, for example.
All registers (including the program counter) are both readable and writable. This is to bypass the limitation of not having jump instructions - just add/subtract/move a value into the program counter register and you're good.
The control unit will increment the program counter at the end of the cycle, but only if you didn't write to the program counter. This means basically: If you executed a regular instruction, the next instruction is fetched and executed. But if you wrote something to the program counter (i.e. you jumped), the contents of the program counter are left as is.
It runs at 1/8 Hz right now to ensure nothing breaks, but it could probably go faster than that. I just have to check where the critical code path is and calculate how many ticks it needs to execute to come up with a more accurate (and faster) clock.
I'll probably work on this thing over the next few weeks if I find the time. Extending it to 16-bit is definitely on the todo list, since pretty much everything is in place for 16-bit support - I just have to duplicate the registers and put down a few more wires. Adding more registers is also something I had in mind, as well as some sort of I/O interface, since right now I have to hover above the registers to see what value is stored inside them.
I'll upload the map once I cleaned up the whole thing. Until then, feel free to post your feedback/questions.
That's awesome! I would go insane trying to make something like that. You should also post this in the Redstone section so you can get more opinions.
Thanks! I only saw the redstone section after I posted this here, but since this thread initially didn't get that many replies, I've been upgrading it to full 16 bit support plus a hexadecimal display and now I'm planning to add some RAM and a 16x16 pixel display (possibly running pong) before I'm posting a thread there.
Have a few screenshots of the version I'm working on right now (click images for higher resolution):