DCPU-16 implementation in Python
Find a file
2012-04-14 15:41:20 +02:00
.gitignore ignore pyc 2012-04-08 19:58:53 +02:00
dcpu_16.py guard against SET PC, [SP++], and SET PC, [--SP] 2012-04-14 15:41:20 +02:00
LICENSE 3-Clause BSD license 2012-04-06 11:50:29 +02:00
README.mdown title changes 2012-04-08 20:19:55 +02:00
test.py cosmetic 2012-04-08 19:58:35 +02:00

What is this?

A DCPU-16 implementation in Python. See the spec.

Goal

Many high-level implementations looked like C-in-other-language, so let's have a pythonic enough (whatever that means, but you should read this) implementation. The spirit of the thing is to be educative for everyone.

Usage

It's meant to be used interactively via the Python REPL as well as programmatically. A specific ASM REPL might be implemented at some point.

An example of a Python REPL session:

>>> from dcpu_16 import CPU, spec_demo
>>> c = CPU(debug=True)
>>> c.load_m(spec_demo)         # loads demo program
>>> c.step()                    # step by one instruction
 << SET
 << c.r[0x0]
 << c.m[0x0001]
 << A=0030 B=0000 C=0000 X=0000 Y=0000 Z=0000 I=0000 J=0000 PC=0002 SP=0000 O=0000
>>> c.pc = 0xA                  # jump to 'loopy thing'
>>> c.step()
 << SET
 << c.r[0x6]
 << 0x000A
 << A=0030 B=0000 C=0000 X=0000 Y=0000 Z=0000 I=000A J=0000 PC=000B SP=0000 O=0000
>>> c.reset()                   # reset CPU
>>> c.clear()                   # clear memory
>>> c.dump_r()                  # get CPU register state as string
'A=0000 B=0000 C=0000 X=0000 Y=0000 Z=0000 I=0000 J=0000 PC=0000 SP=0000 O=0000'

Status

As of v1.0 the CPU implementation ought to be complete according to DCPU-16 spec v1.1.

Features

Opcodes and valcodes are as declarative as possible using decorators, leaving the dispatcher as a quasi-one-liner and leveraging dict power instead of if/elif.

Using functions/methods mean there are doctrings everywhere, hence documentation is both very local and as exhaustive as possible. Try help(dcpu_16).

You can use cpu[] to dispatch valcodes and get/set directly without having to handle a pointer structure.

The CPU is a class, so you can instantiate a bunch of them. I might move memory outside the CPU so that it would be shared by CPU instances (SMP!)