Saturday, April 28, 2012

6502 emulator in Minecraft runs Forth

Notch's new game 0x10c and its virtual 16-bit computer, the DCPU-16, have made news recently. Somewhat eclipsed by Notch's project is the 6502 emulator now available for Minecraft as part of Prerelease 5 of the mod RedPower 2 by Eloraam.

Eloraam calls it the 65EL02, because "it supports all the 6502, 65C02, and part of the 65C816 instruction set" as well as "a set of completely new instructions and two addressing modes."

The emulated CPU comes with 8K RAM, with up to seven additional 8K RAM Modules installable on Backplanes that must be placed on blocks behind the CPU.

Additional components are Monitors, floppy Disk Drives, and IO Expanders, all of which may be located at a distance from the CPU as long as they're connected by Ribbon Cable blocks.

A minimal installation consists of a CPU, Disk Drive, and Monitor. Up to 256 devices (just the Monitors, Disk Drives, and IO Expanders thus far) are supported by Redbus over the ribbon cables.

RedPower 2's 65EL02 computer system in Minecraft. Shown are a CPU with three 8K RAM Modules, a Monitor on the left, and a floppy Disk Drive adjacent on the right. Further right a Ribbon Cable connects to an IO Expander interfaced to a Bundled Cable that carries 16 redstone signals. (Click to enlarge.)

The portion of Eloraam's addon with the computer is known as RedPower Control due to its intended use for industrial process control. Among other things the computer can control in Minecraft are automated mining machines built with RedPower's newly-introduced Frames, which are movable scaffold blocks used to build moving gantries, booms, and cranes.

Eloraam has put an incredible amount of work into RedPower Control and the other parts of Prerelease 5, and it shows. Her textures, while remaining at Minecraft's low resolution, are very attractive and make RedPower's items pleasant to look at in-game.

A example is the front panel displayed when you right-click the 65EL02 CPU block:

The front panel of the 65EL02 CPU. Yes, the toggle switches really work! (Click to enlarge.)

The front panel recalls the look of Digital Equipment Corporation (DEC) hardware really well. The rocker switches, in particular, are great renditions of those on the front panel of PDP-11/35s and /40s. When I complimented her on the appearance of the front panel, Eloraam described the effort it took:

The rocker switches took hours to draw. They were so hard that I ended up resorting to tracing the rough contours and illumination from the one good front-on picture of a PDP-11/35 I could find on Google. Even then, it wasn't a copy-paste job. The exemplar gave me a good map of where the speculars and shadows fall, but it was also a grainy JPEG with a myriad of photographic imperfections, as well as having the wrong aspect ratio. So once I put down the basic outline and marked out how the speculars fall, I tossed the exemplar and drew everything myself.

I must admit I found working on such a large canvas daunting. I'm an engineer, this art stuff is all new to me. I've developed a knack for 16x16 through lots of practice (RedPower has something like 500 sprites now, and I drew them all), but this was something else entirely.

Since the 65EL02 is an 8-bit CPU, Eloraam didn't have as many options for programming environments as we have on today's 64-bit computers. While it's possible to program the 65EL02 in assembly language, for general use she chose to implement a Forth interpreter. Less well-known than BASIC, Forth is far more powerful while retaining a tiny footprint and being easy to understand.

The output of the Forth WORDS command. (Click to enlarge.)

Note the sophistication of the scheduler built into the 6502 emulation as described in this reply by Eloraam to a question about infinite loops:

Infinite loops are fine. RP Control is carefully designed so that you won't screw up your world even if you crash the virtual computer. It's actually not especially hard to crash the virtual computer, since the whole OS is loaded into its RAM and you can easily write to that RAM. Still, since the computer is fully virtualized, it won't hurt your world or even cause a slowdown.

The one thing to note is that you should always use at least one TICK or some TICKS in a loop if you're just waiting for things in the world to change. The computer will run a lot faster if you're not constantly wasting CPU cycles. The CPU only gives you a small number of cycles each tick, but it saves them up (to a certain limit), so if you don't waste them, your CPU can run a lot of code at once when you need it. TICK (and TICKS) calls a special instruction in the CPU which saves up any unused cycles, so it's your friend.

Further technical information about RedPower Control's 65EL02 is available on Eloraam's blog RP Control Internals, and on the RedPower wiki's page for RedPower Control.

For those that prefer video, YouTuber direwolf20 has a video introduction and basic tutorial for RedPower 2's new computer system:

It's interesting to note that Notch considered the 6502 before switching to a custom 16-bit CPU, even going as far as writing a working 6502 emulator. Just two days after getting his emulator running, Notch tweeted:

Starting to dislike the 6502 idea. Spending heaps of cpu cycles to emulate not being able to multiply. Reconsidering custom CPU again.

It appears that Notch intends his virtual CPU to be a major part of his new game and thus decided that the 6502 would be too limited due to being eight bits. Judging by the enthusiastic reaction to the DCPU-16 it appears he made the right choice.

Eloraam took another path by augmenting the 6502 with 65C02 and some 65C816 instructions and implementing MUL and DIV. The goal for the 65EL02 is much lower than for the DCPU-16. While capable of cool things, the 65EL02 seems intended to be a workhorse for carrying out the business of industrial process control in Minecraft.

Much of the excitement about the DCPU-16 has been anticipatory, although this will change soon when Notch releases the DCPU-16 emulator and then the first playable release of 0x10c. On the other hand RedPower's 65EL02 is available now and fully usable in its intended environment.

Let the 65EL02 hacking begin!

Update: This article was linked on Hack a Day, Slashdot, and engadget.

No comments:

Post a Comment