File Coverage

blib/lib/CPU/Emulator/Z80/Register.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 4 4 100.0
total 24 24 100.0


line stmt bran cond sub pod time code
1             # $Id: Register.pm,v 1.6 2008/02/26 21:54:55 drhyde Exp $
2              
3             package CPU::Emulator::Z80::Register;
4              
5 17     17   118 use vars qw($VERSION);
  17         31  
  17         886  
6              
7             $VERSION = '1.0';
8              
9 17     17   23445 use CPU::Emulator::Z80::ALU;
  17         52  
  17         5801  
10              
11             =head1 NAME
12              
13             CPU::Emulator::Z80::Register - a register for a Z80
14              
15             =head1 DESCRIPTION
16              
17             This is a base class that defines some useful routines for
18             registers of any size.
19              
20             =head1 METHODS
21              
22             The following methods exist in the base class:
23              
24             =head2 getsigned
25              
26             Decodes the register 2s-complement-ly and return a signed value.
27              
28             =cut
29              
30             sub getsigned {
31 8     8 1 18 my $self = shift;
32 8         41 my $value = $self->get();
33 8         55 return ALU_getsigned($value, $self->{bits});
34             }
35              
36             =head2 cpu
37              
38             Return a reference to the CPU this object lives in.
39              
40             =cut
41              
42 203     203 1 1398 sub cpu { shift()->{cpu}; }
43              
44             =head2 inc
45              
46             Increment the register. But note that if incrementing means you
47             need to do anything else, such as set flags, you will need to
48             override this.
49              
50             =cut
51              
52             sub inc {
53 1682     1682 1 20170 my $self = shift();
54 1682         31260 $self->set($self->get() + 1);
55             }
56              
57             =head2 dec
58              
59             Decrement the register, again without bothering with flags and
60             stuff so override if necessary.
61              
62             =cut
63              
64             sub dec {
65 54     54 1 1062 my $self = shift;
66 54         157 $self->set($self->get() - 1);
67             }
68              
69             =pod
70              
71             and the following methods need to be defined in all sub-classes:
72              
73             =head2 get, set
74              
75             Must be over-ridden in
76             sub-classes such
77             that setting stores a value, truncated to the right length, and
78             getting retrieves a value, truncated to the right length.
79              
80             The set() method must accept -ve values and store them in
81             2s-complement. Its behaviour is undefined if the user is foolish
82             enough to store too large a -ve value.
83              
84             The get() method must return the value assuming it to be unsigned.
85              
86             =head1 FIELDS
87              
88             All subclasses must have the following fields:
89              
90             =head2 bits
91              
92             The number of bits in the register
93              
94             =head2 cpu
95              
96             A reference to the CPU this register resides in - this is so that
97             mathemagical operators can get at the flags register.
98              
99             =head1 AUTHOR, COPYRIGHT and LICENCE
100              
101             Copyright 2008 David Cantrell EFE
102              
103             This software is free-as-in-speech software, and may be used,
104             distributed, and modified under the terms of either the GNU
105             General Public Licence version 2 or the Artistic Licence. It's
106             up to you which one you use. The full text of the licences can
107             be found in the files GPL2.txt and ARTISTIC.txt, respectively.
108              
109             =head1 CONSPIRACY
110              
111             This module is also free-as-in-mason software.
112              
113             =cut
114              
115             1;