Subject: | New feature: size can be optional |
In CPU::Emulator::Memory::new, size can be determined from the length of
bytes, if bytes is supplied. This allows replacing
* CPU::Emulator::Memory->new(bytes => 'str', size => length('str'))
by
* CPU::Emulator::Memory->new(bytes => 'str')
In CPU::Emulator::Memory::Banked::new, size can be determined from the
size of the file, if supplied. This allows replacing
* $mem->bank(address => 0, size => xxx, type => 'ROM', file => 'f.rom)
by
* $mem->bank(address => 0, type => 'ROM', file => 'f.rom)
The attached patch includes this feature, together with test cases to
check its working.
Thank you,
Paulo Custodio
Subject: | CPU-Emulator-Memory-1.1001-patch03-RT.txt |
diff -cr CPU-Emulator-Memory-1.1001/lib/CPU/Emulator/Memory/Banked.pm CPU-Emulator-Memory-1.1001_03/lib/CPU/Emulator/Memory/Banked.pm
*** CPU-Emulator-Memory-1.1001/lib/CPU/Emulator/Memory/Banked.pm 2008-02-28 22:15:52.000000000 +0000
--- CPU-Emulator-Memory-1.1001_03/lib/CPU/Emulator/Memory/Banked.pm 2010-10-22 21:17:13.518229900 +0100
***************
*** 66,72 ****
=item size
The size of the bank to swap. This means that you'll be swapping
! addresses $base_address to $base_address + $size - 1.
=item type
--- 66,73 ----
=item size
The size of the bank to swap. This means that you'll be swapping
! addresses $base_address to $base_address + $size - 1.
! This defaults to the size of the given C<file>, if supplied.
=item type
***************
*** 116,121 ****
--- 117,131 ----
sub bank {
my($self, %params) = @_;
+
+ # init size from file
+ if (!exists($params{size}) && # no size given
+ exists($params{file}) && # but a file given
+ !ref($params{file}) && # file is not filehandle
+ -s $params{file}) { # file exists and has size > 0
+ $params{size} = -s $params{file};
+ }
+
my($address, $size, $type) = @params{qw(address size type)};
foreach (qw(address size type)) {
die("bank: No $_ specified\n")
diff -cr CPU-Emulator-Memory-1.1001/lib/CPU/Emulator/Memory.pm CPU-Emulator-Memory-1.1001_03/lib/CPU/Emulator/Memory.pm
*** CPU-Emulator-Memory-1.1001/lib/CPU/Emulator/Memory.pm 2008-02-28 19:41:29.000000000 +0000
--- CPU-Emulator-Memory-1.1001_03/lib/CPU/Emulator/Memory.pm 2010-10-22 21:16:32.829902600 +0100
***************
*** 50,57 ****
=item size
! the size of the memory to emulate. This defaults to 64K (65536 bytes).
! Note that this does *not* have to be a power of two.
=item bytes
--- 50,58 ----
=item size
! the size of the memory to emulate. This defaults to 64K (65536 bytes),
! or to the length of the string passed to C<bytes>.
! Note that this does *not* have to be a power of two.
=item bytes
***************
*** 64,70 ****
sub new {
my($class, %params) = @_;
! $params{size} ||= 0x10000;
if(!exists($params{bytes})) {
$params{bytes} = chr(0) x $params{size};
}
--- 65,72 ----
sub new {
my($class, %params) = @_;
! $params{size} ||= exists($params{bytes}) ?
! length($params{bytes}) : 0x10000;
if(!exists($params{bytes})) {
$params{bytes} = chr(0) x $params{size};
}
diff -cr CPU-Emulator-Memory-1.1001/t/03-banked-memory-ROM.t CPU-Emulator-Memory-1.1001_03/t/03-banked-memory-ROM.t
*** CPU-Emulator-Memory-1.1001/t/03-banked-memory-ROM.t 2008-02-28 19:41:29.000000000 +0000
--- CPU-Emulator-Memory-1.1001_03/t/03-banked-memory-ROM.t 2010-10-22 20:56:24.124768700 +0100
***************
*** 1,7 ****
use strict;
$^W = 1;
! use Test::More tests => 19;
undef $/;
--- 1,7 ----
use strict;
$^W = 1;
! use Test::More tests => 23;
undef $/;
***************
*** 18,23 ****
--- 18,24 ----
print $fh 'This is a ROM';
close($fh);
+ # no file
eval { $memory->bank(
address => 0,
size => length('This is a ROM'),
***************
*** 25,30 ****
--- 26,50 ----
) };
ok($@, "Can't map ROM without a filename");
+ # size mismatch
+ eval { $memory->bank(
+ address => 0,
+ size => 1,
+ type => 'ROM',
+ file => 'romfile.rom'
+ ) };
+ like($@, qr/is wrong size/, "size mismatch");
+
+ # default sise
+ $memory->bank(
+ address => 0,
+ type => 'ROM',
+ file => 'romfile.rom'
+ );
+ ok($memory->peek(0) == ord('T'), "peek returns data from the ROM");
+ ok($memory->peek(12) == ord('M'), "peek returns data from the ROM");
+
+ # defined size
$memory->bank(
address => 0,
size => length('This is a ROM'),
***************
*** 32,37 ****
--- 52,58 ----
file => 'romfile.rom'
);
ok($memory->peek(0) == ord('T'), "peek returns data from the ROM");
+ ok($memory->peek(12) == ord('M'), "peek returns data from the ROM");
ok($memory->poke(0, 1) == 0, "poke returns 0 when we try to write ...");
ok($memory->peek(0) == ord('T'), "... and really didn't change anything");
ok($memory->peek8(0) == ord('T'), "peek8 reads from ROM too");
***************
*** 42,48 ****
$memory->bank(
address => 1,
- size => length('This is a ROM'),
type => 'ROM',
file => 'romfile.rom',
writethrough => 1
--- 63,68 ----
***************
*** 62,74 ****
$memory->bank(
address => 0,
- size => length('This is a ROM'),
type => 'ROM',
file => 'romfile.rom'
);
$memory->bank(
address => 6,
- size => length('This is a ROM'),
type => 'ROM',
file => 'romfile.rom'
);
--- 82,92 ----
***************
*** 77,89 ****
$memory->bank(
address => 6,
- size => length('This is a ROM'),
type => 'ROM',
file => 'romfile.rom'
);
$memory->bank(
address => 0,
- size => length('This is a ROM'),
type => 'ROM',
file => 'romfile.rom'
);
--- 95,105 ----
diff -cr CPU-Emulator-Memory-1.1001/t/06-initialise-from-string.t CPU-Emulator-Memory-1.1001_03/t/06-initialise-from-string.t
*** CPU-Emulator-Memory-1.1001/t/06-initialise-from-string.t 2008-02-14 16:14:35.000000000 +0000
--- CPU-Emulator-Memory-1.1001_03/t/06-initialise-from-string.t 2010-10-22 20:28:27.213854800 +0100
***************
*** 1,13 ****
use strict;
$^W = 1;
! use Test::More tests => 1;
use CPU::Emulator::Memory;
my $memory = CPU::Emulator::Memory->new(
bytes => 'This is all the memory',
size => length('This is all the memory')
);
ok($memory->peek(length('This is all the memory') - 1) == ord('y'), "Memory is initialised correctly from a string");
--- 1,24 ----
use strict;
$^W = 1;
! use Test::More tests => 3;
use CPU::Emulator::Memory;
my $memory = CPU::Emulator::Memory->new(
bytes => 'This is all the memory',
+ );
+
+ ok($memory->peek(length('This is all the memory') - 1) == ord('y'), "Memory is initialised correctly from a string");
+
+
+ $memory = CPU::Emulator::Memory->new(
+ bytes => 'This is all the memory',
size => length('This is all the memory')
);
ok($memory->peek(length('This is all the memory') - 1) == ord('y'), "Memory is initialised correctly from a string");
+
+
+ eval { CPU::Emulator::Memory->new(bytes => 'This is all the memory', size => 2) };
+ like($@, qr/bytes and size don't match/, "size mismatch");