Z80/ALU.pm

filter: Statementstmt 100% 93 / 93 Branchbran 100% 8 / 8 Conditioncond 84.0% 37 / 44 MC/DCmcdc 61.9% 13 / 21 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 91.9% 172 / 187 SCAR 28.9
CC
18
Cov
95%
ALU_add16
5.1
ALU_sub16
4.0
ALU_add8
4.0
CRAP 18.0
« Z80.pm ↑ Summary Z80/Register16.pm »
1 # $Id: ALU.pm,v 1.9 2008/02/24 23:52:05 drhyde Exp $
2  
3 package CPU::Emulator::Z80::ALU;
4  
5 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 { # find and export ALU_*
12 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶ while(my($k, $v) = each(%{__PACKAGE__.'::'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{"CPU::Emulator::Z80::ALU::";}) 216 18 234
14 216 ▶ push @EXPORT, $k if(defined(&{$v}) && $k =~ /^ALU_/);
Branch: 1 branch
BranchTrueFalseTotal
if defined &{$v;} and $k =~ /^ALU_/ 144 72 216
Condition66%2/3
defined &{$v;} and $k =~ /^ALU_/
!ll&&!rl&&r
720144
MC/DC50%1/2
defined &{$v;} and $k =~ /^ALU_/
defined &{$v;} $k =~ /^ALU_/
Truth table66%2/3
A and B
A= defined &{$v;}B= $k =~ /^ALU_/
ABresult
0X0
100
111
15 }
16 }
17  
18 =head1 NAME
19  
20 CPU::Emulator::Z80::ALU
21  
22 =head1 DESCRIPTION
23  
24 This mix-in provides functions for addition and subtraction on a
25 Z80, settings flags and doing twos-complement jibber-jabber.
26  
27 =head1 FUNCTIONS
28  
29 All functions are
30 exported. They all take a
31 reference to the Flags register as the first parameter in addition
32 to the parameters listed, unless otherwise stated:
33  
34 =head2 ALU_add8/ALU_add16
35  
36 Takes two 8/16-bit values and returns their 8/16-bit sum, and for
37 add16, a
38 third parameter indicating whether this was really called as ADC.
39  
40 add16 doesn't frob the S, Z or P flags unless that extra parameter
41 is true.
42  
43 =cut
44  
45 sub ALU_add8 {
46 46 ▶ my($flags, $op1, $op2) = @_;
Subroutine ALU_add8: called CC=4 SCAR=13.9
Pod documented
47 46 my $halfcarry = (($op1 & 0b00001111) + ($op2 & 0b00001111)) & 0x10;
48 46 my $carry6to7 = (($op1 & 0b01111111) + ($op2 & 0b01111111)) & 0x80;
49 46 my $result = $op1 + $op2;
50 46 $flags->setC($result & 0x100);
51 46 $result &= 0xFF;
52 46 $flags->resetN();
53 46 $flags->setZ($result == 0);
54 46 $flags->set3($result & 0b1000);
55 46 $flags->setH($halfcarry);
56 46 $flags->set5($result & 0b100000);
57 46 $flags->setS($result & 0b10000000);
58 46 ▶ $flags->setP(
Condition100%9/9
!$flags->getC && $carry6to7 || $flags->getC && !$carry6to7
l!l&&r!l&&!r
3241
not $flags->getC and $carry6to7
!ll&&!rl&&r
25183
$flags->getC and not $carry6to7
!ll&&!rl&&r
18232
MC/DC75%3/4
!$flags->getC && $carry6to7 || $flags->getC && !$carry6to7
not $flags->getC $carry6to7 $flags->getC not $carry6to7
Truth table57%4/7
not(A) and B || C and not(D)
A= not $flags->getCB= $carry6to7C= $flags->getCD= not $carry6to7
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
59 (!$flags->getC() && $carry6to7) ||
60 ($flags->getC() && !$carry6to7)
61 );
62 46 return $result;
63 }
64 sub ALU_add16 {
65 20 ▶ my($flags, $op1, $op2, $adc) = @_;
Subroutine ALU_add16: called CC=5 SCAR=16.3
Pod documented
66 20 my $halfcarry = (($op1 & 0x0FFF) + ($op2 & 0x0FFF)) & 0x1000;
67 20 my $result = $op1 + $op2;
68 20 $flags->setC($result & 0x10000);
69 20 $result &= 0xFFFF;
70 20 $flags->resetN();
71 20 $flags->set3($result & 0x800);
72 20 $flags->setH($halfcarry);
73 20 $flags->set5($result & 0x2000);
74  
75 20 ▶ if($adc) { # only update these if this is really ADC
Branch: 1 branch
BranchTrueFalseTotal
if ($adc) 4 16 20
76 4 my $carry14to15 =
77 (($op1 & 0b0111111111111111) + ($op2 & 0b0111111111111111)) &
78 0b1000000000000000;
79 4 ▶ $flags->setP(
Condition55%5/9
!$flags->getC && $carry14to15 || $flags->getC && !$carry14to15
l!l&&r!l&&!r
103
not $flags->getC and $carry14to15
!ll&&!rl&&r
031
$flags->getC and not $carry14to15
!ll&&!rl&&r
300
MC/DC25%1/4
!$flags->getC && $carry14to15 || $flags->getC && !$carry14to15
not $flags->getC $carry14to15 $flags->getC not $carry14to15
Truth table28%2/7
not(A) and B || C and not(D)
A= not $flags->getCB= $carry14to15C= $flags->getCD= not $carry14to15
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
80 (!$flags->getC() && $carry14to15) ||
81 ($flags->getC() && !$carry14to15)
82 );
83 4 $flags->setZ($result == 0);
84 4 $flags->setS($result & 0x8000);
85 }
86 20 return $result;
87 }
88  
89 =head2 ALU_sub8/ALU_sub16
90  
91 Takes two 8/16-bit values and subtracts the second from the first,
92 returning the result.
93  
94 =cut
95  
96 sub ALU_sub8 {
97 108 ▶ my($flags, $op1, $op2) = @_;
Subroutine ALU_sub8: called CC=4 SCAR=13.9
Pod documented
98 108 my $result = ($op1 - $op2) & 0xFF;
99 108 $flags->setN();
100 108 $flags->setZ($result == 0);
101 108 $flags->setC($op2 > $op1);
102 108 $flags->set3($result & 0b1000);
103 108 $flags->setH(($op2 & 0b1111) > ($op1 & 0b1111));
104 108 ▶ $flags->setP(
Condition100%9/9
!$flags->getC && ($op2 & 127) > ($op1 & 127) || $flags->getC && !(($op2 & 127) > ($op1 & 127))
l!l&&r!l&&!r
53100
not $flags->getC and ($op2 & 127) > ($op1 & 127)
!ll&&!rl&&r
21825
$flags->getC and not +($op2 & 127) > ($op1 & 127)
!ll&&!rl&&r
82183
MC/DC75%3/4
!$flags->getC && ($op2 & 127) > ($op1 & 127) || $flags->getC && !(($op2 & 127) > ($op1 & 127))
not $flags->getC ($op2 & 127) > ($op1 & 127) $flags->getC not +($op2 & 127) > ($op1 & 127)
Truth table57%4/7
not(A) and B || C and not(D)
A= not $flags->getCB= ($op2 & 127) > ($op1 & 127)C= $flags->getCD= not +($op2 & 127) > ($op1 & 127)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
105 (!$flags->getC() && (($op2 & 0b1111111) > ($op1 & 0b1111111))) ||
106 ($flags->getC() && !(($op2 & 0b1111111) > ($op1 & 0b1111111)))
107 );
108 108 $flags->set5($result & 0b100000);
109 108 $flags->setS($result & 0b10000000);
110 108 return $result;
111 }
112  
113 sub ALU_sub16 {
114 31 ▶ my($flags, $op1, $op2) = @_;
Subroutine ALU_sub16: called CC=4 SCAR=13.9
Pod documented
115 31 my $result = ($op1 - $op2) & 0xFFFF;
116 31 $flags->setN();
117 31 $flags->setZ($result == 0);
118 31 $flags->setC($op2 > $op1);
119 31 $flags->set3($result & 0b100000000000);
120 31 $flags->setH(($op2 & 0b111111111111) > ($op1 & 0b111111111111));
121 31 ▶ $flags->setP(
Condition77%7/9
!$flags->getC && ($op2 & 32767) > ($op1 & 32767) || $flags->getC && !(($op2 & 32767) > ($op1 & 32767))
l!l&&r!l&&!r
1030
not $flags->getC and ($op2 & 32767) > ($op1 & 32767)
!ll&&!rl&&r
1291
$flags->getC and not +($op2 & 32767) > ($op1 & 32767)
!ll&&!rl&&r
2910
MC/DC50%2/4
!$flags->getC && ($op2 & 32767) > ($op1 & 32767) || $flags->getC && !(($op2 & 32767) > ($op1 & 32767))
not $flags->getC ($op2 & 32767) > ($op1 & 32767) $flags->getC not +($op2 & 32767) > ($op1 & 32767)
Truth table42%3/7
not(A) and B || C and not(D)
A= not $flags->getCB= ($op2 & 32767) > ($op1 & 32767)C= $flags->getCD= not +($op2 & 32767) > ($op1 & 32767)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
122 (!$flags->getC() && (($op2 & 0b111111111111111) > ($op1 & 0b111111111111111))) ||
123 ($flags->getC() && !(($op2 & 0b111111111111111) > ($op1 & 0b111111111111111)))
124 );
125 31 $flags->set5($result & 0b10000000000000);
126 31 $flags->setS($result & 0b1000000000000000);
127 31 return $result;
128 }
129  
130 =head2 ALU_inc8/ALU_dec8
131  
132 Take a single 8-bit value and incremement/decrement it, returning
133 the result. They're just wrappers around add8/sub8 that preserve
134 the C flag.
135  
136 =cut
137  
138 sub ALU_dec8 {
139 40 ▶ my($flags, $op) = @_;
Subroutine ALU_dec8: called CC=2 SCAR=6.9
Pod documented
140 40 my $c = $flags->getC();
141 40 my $s = $op & 0x80;
142 40 my $result = ALU_sub8($flags, $op, 1);
143 40 $flags->setC($c);
144 40 ▶ $flags->setP($s && !($result & 0x80));
Condition100%3/3
$s && !($result & 128)
!ll&&!rl&&r
28111
MC/DC100%2/2
$s && !($result & 128)
$s !($result & 128)
Truth table100%3/3
A && not(B)
A= $sB= !($result & 128)
ABresult
0X0
100
111
145 40 return $result;
146 }
147 sub ALU_inc8 {
148 15 ▶ my($flags, $op) = @_;
Subroutine ALU_inc8: called CC=1 SCAR=0.0
Pod documented
149 15 my $c = $flags->getC();
150 15 my $s = $op & 0x80;
151 15 my $result = ALU_add8($flags, $op, 1);
152 15 $flags->setC($c);
153 #$flags->setP(!$s && ($result & 0x80));
154 15 return $result;
155 }
156  
157 =head2 ALU_parity
158  
159 Returns the parity of its operand. No flags register is passed,
160 as this does *not* update the register.
161  
162 =cut
163  
164 sub ALU_parity {
165 271 ▶ my($v, $p) = (@_, 1);
Subroutine ALU_parity: called CC=2 SCAR=6.9
Pod documented
166 271 $p = !$p foreach(grep { $v & 2**$_ } 0 .. 15);
167 271 $p;
168 }
169  
170 =head2 ALU_getsigned
171  
172 Takes two parameters, a value and a number of bits. Decodes
173 the value 2s-complement-ly for the appropriate number of bits,
174 returning a signed value. undef is turned into 0.
175  
176 No flags reigster needed
177  
178 =cut
179  
180 sub ALU_getsigned {
181 1949 ▶ my($value, $bits) = @_;
Subroutine ALU_getsigned: called CC=3 SCAR=11.0
Pod documented
182 1949 ▶ $value ||= 0; # turn undef into 0
Condition100%2/2
$value ||= 0
l!l
1228721
MC/DC100%1/1
$value ||= 0
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
183 # if MSB == 0, just return the value
184 1949 ▶ return $value unless($value & (2 ** ($bits - 1)));
Branch: 1 branch
BranchTrueFalseTotal
unless $value & 2 ** ($bits - 1) 1359 590 1949
185 # algorithm is:
186 # flip all bits
187 # add 1
188 # negate
189 590 return -1 * (($value ^ ((2 ** $bits) - 1)) + 1);
190 }
191  
192 =head1 BUGS/WARNINGS/LIMITATIONS
193  
194 None known
195  
196 =head1 FEEDBACK
197  
198 I welcome feedback about my code, including constructive criticism and bug reports. The best bug reports include files that I can add to the test suite, which fail with the current code in CVS and will pass once I've fixed the bug.
199  
200 Feature requests are far more likely to get implemented if you submit a patch yourself.
201  
202 =head1 CVS
203  
204 L<http://drhyde.cvs.sourceforge.net/drhyde/perlmodules/CPU-Emulator-Z80/>
205  
206 =head1 AUTHOR, COPYRIGHT and LICENCE
207  
208 Copyright 2008 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
209  
210 This software is free-as-in-speech software, and may be used,
211 distributed, and modified under the terms of either the GNU
212 General Public Licence version 2 or the Artistic Licence. It's
213 up to you which one you use. The full text of the licences can
214 be found in the files GPL2.txt and ARTISTIC.txt, respectively.
215  
216 =head1 CONSPIRACY
217  
218 This module is also free-as-in-mason software.
219  
220 =cut
221  
222 18 1;
« Z80.pm ↑ Summary Z80/Register16.pm »