Games-NES-Emulator

 view release on metacpan or  search on metacpan

lib/Games/NES/Emulator/Mapper.pm  view on Meta::CPAN

package Games::NES::Emulator::Mapper;

use strict;
use warnings;

use base qw( Class::Accessor::Fast );

use Scalar::Util ();

__PACKAGE__->mk_accessors( qw( context chr_map prg_map ) );

=head1 NAME

Games::NES::Emulator::Mapper - Base class for mappers

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head2 init( )

=cut

sub init {
    my $self = shift;
    my $emu = shift;
    Scalar::Util::weaken( $emu );

    $self->context( $emu );

    $self->_init_maps;
    $self->_init_memory;
}

sub _init_maps {
    my $self = shift;
    $self->prg_map( {
        8  => [ (-1) x 4 ],
        16 => [ (-1) x 2 ],
        32 => [ -1 ],
    } );
    $self->chr_map( {
        1 => [ (-1) x 8 ],
        2 => [ (-1) x 4 ],
        4 => [ (-1) x 2 ],
        8 => [ -1 ],
    } );
}

sub _init_memory {
    my $self = shift;
    my $rom  = $self->context->rom;

    my $prgs = $rom->PRG_count;
    if( $prgs > 0 ) {
        if( $prgs == 1 ) {
            $self->swap_prg_16k( 0x8000, 0 );
            $self->swap_prg_16k( 0xC000, 0 );
        }
        else {
            $self->swap_prg_32k( 0 );
        }
    }

    if( $rom->CHR_count > 0 ) {
        $self->swap_chr_8k( 0 );
    }
}

=head2 read( $address )

Reads $address from the CPU's memory.

=cut

sub read {
    my( $self, $addr ) = @_;
    return $self->context->cpu->memory->[ $addr ];
}

=head2 write( $address => $data )

The base mapper doesn't actually do any writes.

=cut

sub write {



( run in 1.658 second using v1.01-cache-2.11-cpan-437f7b0c052 )