File Coverage

blib/lib/CPU/Emulator/DCPU16/Device.pm
Criterion Covered Total %
statement 24 30 80.0
branch n/a
condition n/a
subroutine 7 10 70.0
pod 6 6 100.0
total 37 46 80.4


line stmt bran cond sub pod time code
1             package CPU::Emulator::DCPU16::Device;
2 4     4   23 use strict;
  4         8  
  4         1570  
3              
4             =head1 NAME
5              
6             CPU::Emulator::DCPU16::Device - generic base memory mapped device for the DCPU16 emulator
7              
8             =head1 SYNOPSIS
9              
10             $cpu->map_device('CPU::Emulator::DCPU16::Device::Console', $start_addr, $end_addr);
11            
12             =head1 DESCRIPTION
13              
14             This base class should not be used directly - it should be subclassed and get methods should be provided.
15              
16             =head1 METHODS
17              
18             =cut
19              
20              
21             =head2 new
22              
23             Create a new device and map it to the memory.
24              
25             =cut
26             sub new {
27 1     1 1 9 my $class = shift;
28 1         1 my $mem = shift;
29 1         4 my $start = shift;
30 1         2 my $end = shift;
31 1         2 my %opts = @_;
32 1         3 $opts{_start} = $start;
33 1         3 $opts{_end} = $end;
34 1         4 my $self = bless \%opts, $class;
35 1         15 tie $mem->[$_], $class, $_, $self for ($start..$end);
36 1         5 $self;
37             }
38              
39             =head2 start
40              
41             Get the start address of this mapped device
42            
43             =cut
44 86     86 1 678 sub start { shift->{_start} }
45              
46             =head2 end
47              
48             Get the end address of this mapped device
49            
50             =cut
51 1     1 1 19 sub end { shift->{_end} }
52              
53             =head2 tick
54              
55             Called after each instruction is called
56              
57             =cut
58             sub tick {
59 0     0 1 0 my $self = shift;
60             # no-op
61             }
62              
63             =head2 set
64              
65             Set the address of the mapped device to value.
66              
67             =cut
68             sub set {
69 0     0 1 0 my $self = shift;
70 0         0 my $addr = shift;
71 0         0 my $val = shift;
72             # no-op
73             }
74              
75             =head2 get
76              
77             Get the value at address of the mapped device.
78              
79             =cut
80             sub get {
81 0     0 1 0 my $self = shift;
82 0         0 my $addr = shift;
83             # no-op
84             }
85              
86             sub TIESCALAR {
87 384     384   436 my $class = shift;
88 384         547 my $addr = shift;
89 384         404 my $dev = shift;
90 384         1998 return bless { address => $addr, device => $dev }, $class; # create a proxy
91             }
92              
93             sub STORE {
94 84     84   112 my $proxy = shift;
95 84         107 my $value = shift;
96 84         316 $proxy->{device}->set($proxy->{address}, $value);
97             }
98              
99             sub FETCH {
100 1     1   25 my $proxy = shift;
101 1         10 $proxy->{device}->get($proxy->{address});
102             }
103              
104              
105             1;