Memory/Banked.pm

filter: Statementstmt 91.5% 76 / 83 Branchbran 75.7% 50 / 66 Conditioncond 73.8% 31 / 42 MC/DCmcdc 54.1% 13 / 24 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 80.3% 184 / 229 CC 52 SCAR 30.7
CC
52
Cov
82%
bank
36.1
poke
17.8
peek
7.0
CRAP 21.5
↑ Summary Memory.pm »
1 package CPU::Emulator::Memory::Banked;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use base qw(CPU::Emulator::Memory);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 $VERSION = '1.1002';
12  
13 =head1 NAME
14  
15 CPU::Emulator::Memory::Banked - banked memory for a CPU emulator
16  
17 =head1 SYNOPSIS
18  
19 my $memory = CPU::Emulator::Memory::Banked->new();
20 $memory->poke(0xBEEF, ord('s'));
21
22 my $value = $memory->peek(0xBEEF); # 115 == ord('s')
23  
24 $memory->bank(
25 address => 0x8000,
26 size => 0x4000,
27 type => 'ROM',
28 file => '.../somerom.rom',
29 writethrough => 1
30 );
31  
32 my $value = $memory->peek(0xBEEF); # read from ROM instead
33 $memory->poke(0xBEEF, 0); # write to underlying RAM
34  
35 =head1 DESCRIPTION
36  
37 This class adds multiple memory banks to the flat memory space provided
38 by CPU::Emulator::Memory. These
39 temporarily replace chunks of memory with other chunk, to
40 simulate bank-switching. Those chunks can be of arbitrary size,
41 and can be either RAM, ROM, or 'dynamic', meaning that instead
42 of being just dumb storage, when you read or write them perl code
43 gets run.
44  
45 =head1 METHODS
46  
47 It inherits all the methods from CPU::Emulator::Memory, including the
48 constructor, and also implements ...
49  
50 =head2 bank
51  
52 This method performs a bank switch. This changes your view of
53 the memory, mapping another block of memory in place of part of the
54 main RAM. The main RAM's contents are preserved (although see
55 below for an exception). It takes several named parameters, three
56 of which are compulsory:
57  
58 =over
59  
60 =item address
61  
62 The base address at which to swap in the extra bank of memory.
63  
64 =item size
65  
66 The size of the bank to swap. This means that you'll be swapping
67 addresses $base_address to $base_address + $size - 1.
68 This defaults to the size of the given C<file>, if supplied.
69  
70 =item type
71  
72 Either 'ROM' (for read-only memory), or 'RAM' to swap in a block of
73 RAM. Support will be added in the future for type 'dynamic' which
74 will let you run arbitrary perl code for reads and writes to/from
75 the memory.
76  
77 =back
78  
79 When you change memory banks, any banks already loaded which would
80 overlap are unloaded.
81  
82 The following optional parameters are also supported:
83  
84 =over
85  
86 =item file
87  
88 A file which backs the memory. For ROM memory this is compulsory,
89 for RAM it is optional.
90  
91 Note, however, that for RAM it must be a read/writeable *file* which
92 will be created if necessary, whereas
93 for ROM it must be a readable file or a readable *file handle*. It is
94 envisioned that ROMs will often be initialised from data embedded in
95 your program. You can turn a string into a filehandle using IO::Scalar -
96 there's an example of this in the tests.
97  
98 =item writethrough
99  
100 This is only meaningful for ROM. If set, then any writes to the
101 addresses affected will be directed through to the underlying main
102 RAM. Otherwise writes will be ignored.
103  
104 =item function_read and function_write
105  
106 Coderefs which will be called when 'dynamic' memory is read/written.
107 Both are compulsory for 'dynamic' memory. They are called with a
108 reference to the memory object, the address being accessed, and
109 (for function_write) the byte to be written. function_read should
110 return a byte. function_write's return value is ignored.
111  
112 =back
113  
114 =cut
115  
116 sub bank {
117 15 ▶ my($self, %params) = @_;
Subroutine bank: called CC=26 SCAR=35.9
Pod documented
118
119 # init size from file
120 15 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $params{"size"} and exists $params{"file"} and not ref $params{"file"} and -s $params{"file"}) 6 9 15
Condition66%6/9
not exists $params{"size"} and exists $params{"file"} and not ref $params{"file"} and -s $params{"file"}
!ll&&!rl&&r
906
not exists $params{"size"} and exists $params{"file"} and not ref $params{"file"}
!ll&&!rl&&r
906
not exists $params{"size"} and exists $params{"file"}
!ll&&!rl&&r
906
MC/DC25%1/4
not exists $params{"size"} and exists $params{"file"} and not ref $params{"file"} and -s $params{"file"}
not exists $params{"size"} exists $params{"file"} not ref $params{"file"} -s $params{"file"}
Truth table40%2/5
not(A) and B and not(C) and D
A= not exists $params{"size"}B= exists $params{"file"}C= not ref $params{"file"}D= -s $params{"file"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
121 !exists($params{size}) && # no size given
122 exists($params{file}) && # but a file given
123 !ref($params{file}) && # file is not filehandle
124 -s $params{file} # file exists and has size > 0
125 ) {
126 6 $params{size} = -s $params{file};
127 }
128  
129 15 my($address, $size, $type) = @params{qw(address size type)};
130 15 foreach (qw(address size type)) {
131 die("bank: No $_ specified\n")
132 45 ▶ if(!exists($params{$_}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{$_} 0 45 45
133 }
134 die("bank: address and size is out of range\n")
135 15 ▶ if($address < 0 || $address + $size - 1 > $self->{size} - 1);
Branch: 1 branch
BranchTrueFalseTotal
if $address < 0 or $address + $size - 1 > $self->{"size"} - 1 1 14 15
Condition66%2/3
$address < 0 or $address + $size - 1 > $self->{"size"} - 1
l!l&&r!l&&!r
0114
MC/DC50%1/2
$address < 0 or $address + $size - 1 > $self->{"size"} - 1
$address < 0 $address + $size - 1 > $self->{"size"} - 1
Truth table66%2/3
A or B
A= $address < 0B= $address + $size - 1 > $self->{"size"} - 1
ABresult
1X1
011
000
136  
137 14 my $contents ='';
138 14 ▶ if($type eq 'ROM') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'ROM') { } 13 1 14
elsif ($type eq 'RAM') { } 0 1 1
elsif ($type eq 'dynamic') { } 1 0 1
139 die("For ROM banks you need to specify a file\n")
140 13 ▶ unless(exists($params{file}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"file"} 1 12 13
141 12 $contents = $self->_readROM($params{file}, $size);
142 } elsif($type eq 'RAM') {
143 $contents = (exists($params{file}))
144 0 ▶ ? $self->_readRAM($params{file}, $size)
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'file'} ? : 0 0 0
145 : chr(0) x $size;
146 } elsif($type eq 'dynamic') {
147 die("For dynamic banks you need to specify function_read and function_write\n")
148 1 ▶ unless(exists($params{function_read}) && exists($params{function_write}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"function_read"} and exists $params{"function_write"} 0 1 1
Condition33%1/3
exists $params{"function_read"} and exists $params{"function_write"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $params{"function_read"} and exists $params{"function_write"}
exists $params{"function_read"} exists $params{"function_write"}
Truth table33%1/3
A and B
A= exists $params{"function_read"}B= exists $params{"function_write"}
ABresult
0X0
100
111
149 }
150 12 foreach my $bank (@{$self->{overlays}}) {
151 8 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($bank->{"address"} >= $address and $bank->{"address"} < $address + $size or $address >= $bank->{"address"} and $address < $bank->{"address"} + $bank->{"size"}) 8 0 8
Condition55%5/9
$bank->{"address"} >= $address and $bank->{"address"} < $address + $size or $address >= $bank->{"address"} and $address < $bank->{"address"} + $bank->{"size"}
l!l&&r!l&&!r
710
$bank->{"address"} >= $address and $bank->{"address"} < $address + $size
!ll&&!rl&&r
107
$address >= $bank->{"address"} and $address < $bank->{"address"} + $bank->{"size"}
!ll&&!rl&&r
001
MC/DC0%0/4
$bank->{"address"} >= $address and $bank->{"address"} < $address + $size or $address >= $bank->{"address"} and $address < $bank->{"address"} + $bank->{"size"}
$bank->{"address"} >= $address $bank->{"address"} < $address + $size $address >= $bank->{"address"} $address < $bank->{"address"} + $bank->{"size"}
Truth table28%2/7
A and B or C and D
A= $bank->{"address"} >= $addressB= $bank->{"address"} < $address + $sizeC= $address >= $bank->{"address"}D= $address < $bank->{"address"} + $bank->{"size"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
152 ( # does an older bank start in the middle of this one?
153 $bank->{address} >= $address &&
154 $bank->{address} < $address + $size
155 ) || ( # does this start in the middle of an older bank?
156 $address >= $bank->{address} &&
157 $address < $bank->{address} + $bank->{size}
158 )
159 8 ) { $self->unbank(address => $bank->{address}) }
160 }
161 12 ▶ push @{$self->{overlays}}, {
Branch: 5 branches
BranchTrueFalseTotal
length $contents ? : 11 1 12
exists $params{'file'} ? : 11 1 12
exists $params{'writethrough'} ? : 1 11 12
exists $params{'function_read'} ? : 1 11 12
exists $params{'function_write'} ? : 1 11 12
162 address => $address,
163 size => $size,
164 type => $type,
165 (length($contents) ? (contents => $contents) : ()),
166 (exists($params{file}) ? (file => $params{file}) : ()),
167 (exists($params{writethrough}) ? (writethrough => $params{writethrough}) : ()),
168 (exists($params{function_read}) ? (function_read => $params{function_read}) : ()),
169 12 (exists($params{function_write}) ? (function_write => $params{function_write}) : ())
170 };
171 }
172  
173 =head2 unbank
174  
175 This method unloads a bank of memory, making the main RAM visible
176 again at the affected addresses. It takes a single named parameter
177 'address' to tell which bank to switch.
178  
179 =cut
180  
181 sub unbank {
182 10 ▶ my($self, %params) = @_;
Subroutine unbank: called CC=2 SCAR=7.0
Pod documented
183 10 ▶ die("unbank: No address specified\n") unless(exists($params{address}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"address"} 0 10 10
184 $self->{overlays} = [
185 10 grep { $_->{address} != $params{address} }
186 10 @{$self->{overlays}}
187 ];
188 }
189  
190 =head2 peek
191  
192 This is replaced by a version that is aware of memory banks but has the
193 same interface. peek8
194 and peek16 are wrappers around it and so are unchanged.
195  
196 =cut
197  
198 sub peek {
199 38 ▶ my($self, $addr) = @_;
Subroutine peek: called CC=7 SCAR=19.5
Pod documented
200 38 ▶ die("Address $addr out of range") if($addr< 0 || $addr > $self->{size} - 1);
Branch: 1 branch
BranchTrueFalseTotal
if $addr < 0 or $addr > $self->{"size"} - 1 3 35 38
Condition100%3/3
$addr < 0 or $addr > $self->{"size"} - 1
l!l&&r!l&&!r
1235
MC/DC100%2/2
$addr < 0 or $addr > $self->{"size"} - 1
$addr < 0 $addr > $self->{"size"} - 1
Truth table100%3/3
A or B
A= $addr < 0B= $addr > $self->{"size"} - 1
ABresult
1X1
011
000
201 35 foreach my $bank (@{$self->{overlays}}) {
202 28 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($bank->{"address"} <= $addr and $bank->{"address"} + $bank->{"size"} > $addr) 23 5 28
Condition100%3/3
$bank->{"address"} <= $addr and $bank->{"address"} + $bank->{"size"} > $addr
!ll&&!rl&&r
2323
MC/DC100%2/2
$bank->{"address"} <= $addr and $bank->{"address"} + $bank->{"size"} > $addr
$bank->{"address"} <= $addr $bank->{"address"} + $bank->{"size"} > $addr
Truth table100%3/3
A and B
A= $bank->{"address"} <= $addrB= $bank->{"address"} + $bank->{"size"} > $addr
ABresult
0X0
100
111
203 $bank->{address} <= $addr &&
204 $bank->{address} + $bank->{size} > $addr
205 ) {
206 23 ▶ if($bank->{type} eq 'dynamic') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bank->{'type'} eq 'dynamic') { } 2 21 23
207 2 return $bank->{function_read}->($self, $addr);
208 } else {
209 21 return ord(substr($bank->{contents}, $addr - $bank->{address}, 1));
210 }
211 }
212 }
213 12 return ord(substr($self->{contents}, $addr, 1));
214 }
215  
216 =head2 poke
217  
218 This method is replaced by a bank-aware version with the same interface.
219 poke8 and poke16 are wrappers around it and so are unchanged.
220  
221 =cut
222  
223 sub poke {
224 13 ▶ my($self, $addr, $value) = @_;
Subroutine poke: called CC=16 SCAR=28.8
Pod documented
225 13 ▶ die("Value $value out of range") if($value < 0 || $value > 255);
Branch: 1 branch
BranchTrueFalseTotal
if $value < 0 or $value > 255 2 11 13
Condition100%3/3
$value < 0 or $value > 255
l!l&&r!l&&!r
1111
MC/DC100%2/2
$value < 0 or $value > 255
$value < 0 $value > 255
Truth table100%3/3
A or B
A= $value < 0B= $value > 255
ABresult
1X1
011
000
226 11 ▶ die("Address $addr out of range") if($addr< 0 || $addr > $self->{size} - 1);
Branch: 1 branch
BranchTrueFalseTotal
if $addr < 0 or $addr > $self->{"size"} - 1 2 9 11
Condition100%3/3
$addr < 0 or $addr > $self->{"size"} - 1
l!l&&r!l&&!r
119
MC/DC100%2/2
$addr < 0 or $addr > $self->{"size"} - 1
$addr < 0 $addr > $self->{"size"} - 1
Truth table100%3/3
A or B
A= $addr < 0B= $addr > $self->{"size"} - 1
ABresult
1X1
011
000
227 9 $value = chr($value);
228 9 foreach my $bank (@{$self->{overlays}}) {
229 7 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($bank->{"address"} <= $addr and $bank->{"address"} + $bank->{"size"} > $addr) 5 2 7
Condition66%2/3
$bank->{"address"} <= $addr and $bank->{"address"} + $bank->{"size"} > $addr
!ll&&!rl&&r
025
MC/DC50%1/2
$bank->{"address"} <= $addr and $bank->{"address"} + $bank->{"size"} > $addr
$bank->{"address"} <= $addr $bank->{"address"} + $bank->{"size"} > $addr
Truth table66%2/3
A and B
A= $bank->{"address"} <= $addrB= $bank->{"address"} + $bank->{"size"} > $addr
ABresult
0X0
100
111
230 $bank->{address} <= $addr &&
231 $bank->{address} + $bank->{size} > $addr
232 ) {
233 5 ▶ if($bank->{type} eq 'RAM') {
Branch: 4 branches
BranchTrueFalseTotal
if ($bank->{'type'} eq 'RAM') { } 0 5 5
elsif ($bank->{'type'} eq 'ROM' and $bank->{'writethrough'}) { } 2 3 5
elsif ($bank->{'type'} eq 'ROM') { } 1 2 3
elsif ($bank->{'type'} eq 'dynamic') { } 2 0 2
234 0 substr($bank->{contents}, $addr - $bank->{address}, 1) = $value;
235 $self->_writeRAM($bank->{file}, $bank->{contents})
236 0 ▶ if(exists($bank->{file}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $bank->{"file"} 0 0 0
237 0 return 1;
238 ▶ } elsif($bank->{type} eq 'ROM' && $bank->{writethrough}) {
Condition100%3/3
$bank->{"type"} eq "ROM" and $bank->{"writethrough"}
!ll&&!rl&&r
212
MC/DC100%2/2
$bank->{"type"} eq "ROM" and $bank->{"writethrough"}
$bank->{"type"} eq "ROM" $bank->{"writethrough"}
Truth table100%3/3
A and B
A= $bank->{"type"} eq "ROM"B= $bank->{"writethrough"}
ABresult
0X0
100
111
239 2 substr($self->{contents}, $addr, 1) = $value;
240 $self->_writeRAM($self->{file}, $self->{contents})
241 2 ▶ if(exists($self->{file}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"file"} 2 0 2
242 2 return 1;
243 } elsif($bank->{type} eq 'ROM') {
244 1 return 0;
245 } elsif($bank->{type} eq 'dynamic') {
246 2 return $bank->{function_write}->($self, $addr, ord($value));
247 } else {
248 0 die("Type ".$bank->{type}." NYI");
249 }
250 }
251 }
252 4 substr($self->{contents}, $addr, 1) = $value;
253 $self->_writeRAM($self->{file}, $self->{contents})
254 4 ▶ if(exists($self->{file}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"file"} 1 3 4
255 4 return 1;
256 }
257  
258 sub _readROM {
259 12 ▶ my($self, $file, $size) = @_;
Subroutine _readROM: called CC=5 SCAR=16.7
260 12 ▶ if(!ref($file)) { return $self->_read_file($file, $size); }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 9 3 12
261  
262 3 ▶ if(reftype($file) eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($file) eq 'GLOB') { } 3 0 3
263 3 local $/ = undef;
264 # Win32 is stupid, see RT 62379
265 3 ▶ if (eval {$file->can('binmode')}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $file->can('binmode') } }) { } 3 0 3
266 3 $file->binmode; # IO::HANDLE
267 } else {
268 0 binmode $file; # file handle
269 }
270 3 my $contents = <$file>;
271 3 ▶ die("data in filehandle is wrong size (got ".length($contents).", expected $size)\n") unless(length($contents) == $size);
Branch: 1 branch
BranchTrueFalseTotal
unless length $contents == $size 0 3 3
272 3 return $contents;
273 } else {
274 0 die("file mustn't be a ".reftype($file)."-ref");
275 }
276 }
277  
278 =head1 SUBCLASSING
279  
280 The private method _readROM may be useful for subclasses. If passed
281 a filename, it is just a wrapper around the parent class's _read_file.
282 If passed a reference to a filehandle, it reads from that.
283  
284 =head1 BUGS/WARNINGS/LIMITATIONS
285  
286 All those inherited from the parent class.
287  
288 No others known.
289  
290 =head1 FEEDBACK
291  
292 I welcome feedback about my code, including constructive criticism
293 and bug reports. The best bug reports include files that I can add
294 to the test suite, which fail with the current code and will
295 pass once I've fixed the bug.
296  
297 Feature requests are far more likely to get implemented if you submit
298 a patch yourself.
299  
300 =head1 SOURCE CODE REPOSITORY
301  
302 L<git://github.com/DrHyde/perl-modules-CPU-Emulator-Memory.git>
303  
304 =head1 AUTHOR, LICENCE and COPYRIGHT
305  
306 Copyright 2008 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
307  
308 This module is free-as-in-speech software, and may be used,
309 distributed, and modified under the same terms as Perl itself.
310  
311 =head1 CONSPIRACY
312  
313 This module is also free-as-in-mason software.
314  
315 =cut
316  
317 4 1;
↑ Summary Memory.pm »