1 |
package M6502; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Data::Dump qw/dump/; |
7 |
use Carp qw/confess/; |
8 |
|
9 |
=head1 NAME |
10 |
|
11 |
M6502 - perl bindings for 6502 emulator |
12 |
|
13 |
=cut |
14 |
|
15 |
my $debug = 1; |
16 |
|
17 |
our $VERSION = qw(0.0.1); |
18 |
|
19 |
our @mem = (0xff) x 0x10000; # 64M |
20 |
|
21 |
# program counter |
22 |
our $PC = 0xbeef; |
23 |
# CPU registars |
24 |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
25 |
# Set IPeriod to number of CPU cycles between calls to Loop6502 |
26 |
our $IPeriod = 1; |
27 |
|
28 |
=head1 init |
29 |
|
30 |
Called before C<Run6502> |
31 |
|
32 |
=cut |
33 |
|
34 |
sub init { |
35 |
warn "inside init\n"; |
36 |
print "stdout\n"; |
37 |
}; |
38 |
|
39 |
=head2 read |
40 |
|
41 |
Read from memory |
42 |
|
43 |
$byte = read( $address ); |
44 |
|
45 |
=cut |
46 |
|
47 |
sub read { |
48 |
my ($addr) = @_; |
49 |
my $byte = $mem[$addr]; |
50 |
warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
51 |
return $byte; |
52 |
} |
53 |
|
54 |
=head2 write |
55 |
|
56 |
Write into emory |
57 |
|
58 |
write( $address, $byte ); |
59 |
|
60 |
=cut |
61 |
|
62 |
sub write { |
63 |
warn "# write(",dump(@_),")\n" if $debug; |
64 |
my ($addr,$byte) = @_; |
65 |
$mem[$addr] = $byte; |
66 |
} |
67 |
|
68 |
=head2 poke_code |
69 |
|
70 |
Write series of bytes into memory without passing through MMU |
71 |
|
72 |
$emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa ); |
73 |
|
74 |
=cut |
75 |
|
76 |
sub poke_code { |
77 |
my $self = shift; |
78 |
my $addr = shift; |
79 |
warn sprintf("# poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; |
80 |
$mem[$addr++] = $_ foreach @_; |
81 |
} |
82 |
|
83 |
=head2 write_chunk |
84 |
|
85 |
$emu->write_chunk( $address, $chunk_of_data ); |
86 |
|
87 |
=cut |
88 |
|
89 |
sub write_chunk { |
90 |
my ($self, $addr, $chunk) = @_; |
91 |
my $len = length($chunk); |
92 |
splice @mem, $addr, $len, unpack('C*', $chunk); |
93 |
} |
94 |
|
95 |
=head2 ram |
96 |
|
97 |
Read searies of bytes from memory without passing through MMU |
98 |
|
99 |
$emu->ram( $from, $to ); |
100 |
|
101 |
=cut |
102 |
|
103 |
sub ram { |
104 |
my $self = shift; |
105 |
my ($from,$to) = @_; |
106 |
if ($from + $to) { |
107 |
printf "ram %04x - %04x\n", $from, $to; |
108 |
return $mem[$from .. $to - 1]; |
109 |
} |
110 |
printf "ram %04x\n", $from; |
111 |
return $mem[$from] if defined($from); |
112 |
confess "no from address"; |
113 |
} |
114 |
|
115 |
1; |