CPU::Emulator::Z80::Register8 - an 8 bit register for a Z80


CPU-Emulator-Z80 documentation Contained in the CPU-Emulator-Z80 distribution.

Index


Code Index:

NAME

Top

CPU::Emulator::Z80::Register8 - an 8 bit register for a Z80

DESCRIPTION

Top

This class implements an 8-bit register for a Z80.

METHODS

Top

new

Returns an object. Takes two or three named parameters:

cpu

mandatory, a reference to the CPU this register lives in, mostly so that operations on the register can get at the flags register.

and either of:

value

The value to initialise the register to;

or

get, set

Subroutines to call when getting/setting the register instead of the default get/set methods. The 'get' subroutine will be passed no parameters, the 'set' subroutine will be passed the new value. Consequently, they are expected to be closures if they are to be of any use.

get

Get the register's current value.

set

Set the register's value to whatever is passed in as a parameter.

inc

Increment the register and set flags.

dec

Decrement the register and set flags.

add

Add the specified value to the register.

sub

Subtract the specified value from the register.

BUGS/WARNINGS/LIMITATIONS

Top

None known.

AUTHOR, COPYRIGHT and LICENCE

Top

CONSPIRACY

Top

This module is also free-as-in-mason software.


CPU-Emulator-Z80 documentation Contained in the CPU-Emulator-Z80 distribution.
# $Id: Register8.pm,v 1.6 2008/02/23 20:07:01 drhyde Exp $

package CPU::Emulator::Z80::Register8;

use strict;
use warnings;

use vars qw($VERSION);

use base qw(CPU::Emulator::Z80::Register);
use CPU::Emulator::Z80::ALU;

$VERSION = '1.0';

sub new {
    my $class = shift;
    my $self = {@_, bits => 8};
    bless $self, $class;
}

sub get {
    my $self = shift;
    if(exists($self->{get})) {
        return $self->{get}->();
    } else { return $self->{value} & 0xFF }
}

sub set {
    my $self = shift;
    if(exists($self->{set})) {
        return $self->{set}->(shift);
    } else { $self->{value} = shift() & 0xFF }
}

sub inc {
    my $self = shift;
    my $flags = $self->cpu()->register('F');
    $self->set(ALU_inc8($flags, $self->get()));
}

sub dec {
    my $self = shift;
    my $flags = $self->cpu()->register('F');
    $self->set(ALU_dec8($flags, $self->get()));
}

sub add {
    my($self, $op) = @_;
    $self->set(ALU_add8($self->cpu()->register('F'), $self->get(), $op));
}

sub sub {
    my($self, $op) = @_;
    $self->set(ALU_sub8($self->cpu()->register('F'), $self->get(), $op));
}

1;