CPU-Emulator-Memory

 view release on metacpan or  search on metacpan

lib/CPU/Emulator/Memory.pm  view on Meta::CPAN

package CPU::Emulator::Memory;

use strict;
use warnings;

use vars qw($VERSION);

$VERSION = '1.1005';

=head1 NAME

CPU::Emulator::Memory - memory for a CPU emulator

=head1 SYNOPSIS

    my $memory = CPU::Emulator::Memory->new();
    $memory->poke(0xBEEF, ord('s'));
    
    my $value = $memory->peek(0xBEEF); # 115 == ord('s')

=head1 DESCRIPTION

This class provides a flat array of values which you can 'peek'
and 'poke'.

=head1 METHODS

=head2 new

The constructor returns an object representing a flat memory
space addressable by byte.  It takes four optional named parameters:

=over

=item file

if provided, will provide a disc-based backup of the
RAM represented.  This file will be read when the object is created
(if it exists) and written whenever anything is altered.  If no
file exists or no filename is provided, then memory is initialised
to all zeroes.  If the file exists it must be writeable and of the
correct size.

=item endianness

defaults to LITTLE, can be set to BIG.  This matters for the peek16
and poke16 methods.

=item size

the size of the memory to emulate.  This defaults to 64K (65536 bytes), 
or to the length of the string passed to C<bytes> (plus C<org> if
applicable).
.
Note that this does *not* have to be a power of two. 

=item bytes

A string of characters with which to initialise the memory.  Note that
the length must match the size parameter.

=item org

an integer, Used in conjunction with C<bytes>, load the data at the specified
offset in bytes

=back

=cut

sub new {
    my($class, %params) = @_;
    if(exists($params{bytes}) && exists($params{org})) {
        $params{bytes} = (chr(0) x $params{org}).$params{bytes};
    }

    if(!exists($params{size})) {
        if(exists($params{bytes})) {
            $params{size} = length($params{bytes});
        } else {
            $params{size} = 0x10000;
        }
    }
    if(!exists($params{bytes})) {
        $params{bytes} = chr(0) x $params{size};
    }
    die("bytes and size don't match\n")
        if(length($params{bytes}) != $params{size});

    if(exists($params{file})) {
        if(-e $params{file}) {
            $params{bytes} = $class->_readRAM($params{file}, $params{size});
        } else {
            $class->_writeRAM($params{file}, $params{bytes})
        }
    }
    return bless(



( run in 0.483 second using v1.01-cache-2.11-cpan-df04353d9ac )