The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

CPU::Emulator::DCPU16 - an emulator for Notch's DCPU-16 virtual CPU for the game 0x10c

SYNOPSIS

    open(my $fh, ">:raw", $file) || die "Couldn't read file $file: $!";
    my $program = do { local $/=undef; <$fh> };
    $program    = CPU::Emulator::DCPU16::Assembler->assemble($program) if $file =~ /\.dasm(16)?$/;  

    # Create a new CPU and load a file
    my $cpu = CPU::Emulator::DCPU16->new();
    $cpu->load($program);
    
    # Run it ...
    $cpu->run;
    # ... which is basically the same as
    do { $cpu->step } until $cpu->halt;

DESCRIPTION

DCPU-16 is a spec for a virtual CPU by Notch from Mojang (of Minecraft fame).

The spec is available here

http://0x10c.com/doc/dcpu-16.txt

METHODS

new

Create a new CPU.

load <program> [opt[s]]

Load a program. Forces as re-init of the CPU.

You can also do

    my $cpu = CPU::Emulator::DCPU16->load($program, %opts);
    

which is exactly the same as

    my $cpu = CPU::Emulator::DCPU16->new(%opts);
    $cpu->load($program);

bytes_to_array <bytes>

Turn a scalar of bytes into an array of words

map_device <class> <start address> <end address> [opt[s]]

Map a device of the given class to these addresses

run [opt[s]]

Run CPU until completion.

Options available are:

debug

Whether or not we should print debug information and at what level.

Default is 0 (no debug output).

limit

Maxinum number of instructions to execute.

Default is 0 (no limit).

cycle_penalty

The time penalty for each instruction cycle in milliseconds.

Default is 0 (no penalty)

full_memory

Allow the PC to continue past the last instruction of the program (i.e the program_top).

This would allow programs to rewrite themselves into a larger program.

Default is 0 (no access)

step [opt[s]]

Run a single clock cycle of the CPU.

Takes the same options as run.

METHODS TO GET THE STATE OF THE CPU

pc

The current program counter.

sp

The current stack pointer.

o

The current overflow.

halt [halt state]

Halt the CPU or check to see whether it's halted.

program_top

The address of the first memory location after the loaded program.

register <location>

Get or set the value of a register.

memory <location>

Get or set the value of a memory location.

SEE ALSO

CPU::Emulator::DCPU16::Assembler

CPU::Emulator::DCPU16::Disassembler

ACKNOWLEDGEMENTS

Implementation inspiration came from:

Matt Bell's Javascript implementation (https://github.com/mappum/DCPU-16)
Brian Swetland's C Implementation (https://github.com/swetland/dcpu16)
Jesse Luehrs's Perl Implementation (https://github.com/doy/games-emulation-dcpu16)

AUTHOR

Simon Wistow <simon@thegestalt.org>

COPYRIGHT

Copyright 2011 - Simon Wistow

Released under the same terms as Perl itself.

DEVELOPMENT

Latest development version available from

https://github.com/simonwistow/CPU-Emulator-DCPU16