File Coverage

blib/lib/CPU/Emulator/Z80/Register8F.pm
Criterion Covered Total %
statement 28 28 100.0
branch 2 2 100.0
condition n/a
subroutine 9 10 90.0
pod n/a
total 39 40 97.5


line stmt bran cond sub pod time code
1             package CPU::Emulator::Z80::Register8F;
2              
3 16     16   59465 use strict;
  16         24  
  16         431  
4 16     16   68 use warnings;
  16         24  
  16         426  
5              
6 16     16   69 use vars qw($VERSION $AUTOLOAD);
  16         28  
  16         738  
7              
8 16     16   85 use base qw(CPU::Emulator::Z80::Register8);
  16         42  
  16         5019  
9              
10             $VERSION = '1.01';
11              
12             =head1 NAME
13              
14             CPU::Emulator::Z80::Register8F - the flags register for a Z80
15              
16             =head1 DESCRIPTION
17              
18             This class is a ...::Register8 with additional methods for
19             getting at the individual flag bits
20              
21             =head1 METHODS
22              
23             It has the same methods as its parent, with the following additions:
24              
25             =head2 getX/setX/resetX
26              
27             where X can be any of S Z H P N C 5 or 3, where 5 and 3 are the
28             un-named bits 5 and 3 of the register (0 being the least-significant
29             bit).
30              
31             getX takes no parameters and returns 1 or 0 depending
32             on the flag's status.
33              
34             setX if called with no parameters sets the flag true. If called with
35             a parameter it sets the flag true or false depending on the param's
36             value.
37              
38             resetX takes no parameters and sets the flag false.
39              
40             =cut
41              
42             my %masks = (
43             S => 0b10000000, # sign (copy of MSB)
44             Z => 0b01000000, # zero
45             5 => 0b00100000,
46             H => 0b00010000, # half-carry (from bit 3 to 4)
47             3 => 0b00001000,
48             P => 0b00000100, # parity (set if even number of bits set)
49             # overflow (2-comp result doesn't fit in reg)
50             N => 0b00000010, # subtract (was the last op a subtraction?)
51             C => 0b00000001 # carry (result doesn't fit in register)
52             );
53              
54             sub _get {
55 2663     2663   4199 my($self, $flag) = @_;
56 2663         4619 return !!($self->get() & $flag)
57             }
58             sub _set {
59 11592     11592   17163 my($self, $flag, $value) = @_;
60 11592 100       18706 $value = 1 if(!defined($value));
61 11592         19632 $self->set(
62             ($self->get() & (0xFF - $flag)) +
63             $flag * (0 + !!$value)
64             );
65             }
66             sub _reset {
67 1368     1368   2124 my($self, $flag) = @_;
68 1368         2558 $self->set($self->get() & (0xFF - $flag));
69             }
70              
71       0     sub DESTROY{}
72             # only calculate this once
73             my $autoload_prefix_length = length(__PACKAGE__) + 2;
74             sub AUTOLOAD {
75 15623     15623   33692 my $sub = substr($AUTOLOAD, $autoload_prefix_length);
76 15623         19042 my $flag = substr($sub, -1);
77 15623         19377 substr($sub, -1, 1) = '';
78              
79 15623         18739 my $self = shift();
80 16     16   107 no strict 'refs';
  16         44  
  16         1295  
81 15623         21575 return *{"_$sub"}->($self, $masks{$flag}, @_);
  15623         30385  
82             }
83              
84             =head1 BUGS/WARNINGS/LIMITATIONS
85              
86             None known.
87              
88             =head1 AUTHOR, COPYRIGHT and LICENCE
89              
90             Copyright 2008 David Cantrell EFE
91              
92             This software is free-as-in-speech software, and may be used,
93             distributed, and modified under the terms of either the GNU
94             General Public Licence version 2 or the Artistic Licence. It's
95             up to you which one you use. The full text of the licences can
96             be found in the files GPL2.txt and ARTISTIC.txt, respectively.
97              
98             =head1 CONSPIRACY
99              
100             This module is also free-as-in-mason software.
101              
102             =cut
103              
104             1;