6 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
7 |
use Carp qw/confess/; |
use Carp qw/confess/; |
8 |
use Exporter 'import'; |
use Exporter 'import'; |
9 |
our @EXPORT = qw'@mem $PC $A $P $X $Y $S $IPeriod $run_for'; |
our @EXPORT = qw'@mem $PC $A $P $X $Y $S $IPeriod $run_for $debug'; |
10 |
|
|
11 |
=head1 NAME |
=head1 NAME |
12 |
|
|
13 |
M6502 - perl bindings for 6502 emulator |
M6502 - perl bindings for M6502 CPU emulator |
14 |
|
|
15 |
=cut |
=cut |
16 |
|
|
17 |
my $debug = 1; |
my $debug = 0; |
18 |
|
|
19 |
our $VERSION = qw(0.0.1); |
our $VERSION = qw(0.0.1); |
20 |
|
|
29 |
# Exec6502 cycles |
# Exec6502 cycles |
30 |
our $run_for = 0; |
our $run_for = 0; |
31 |
|
|
32 |
=head1 init |
=head1 FUNCTIONS |
33 |
|
|
34 |
|
=head2 init |
35 |
|
|
36 |
Called before C<Run6502> |
Called before C<Run6502> |
37 |
|
|
39 |
|
|
40 |
sub init { |
sub init { |
41 |
my $self = shift; |
my $self = shift; |
|
warn dump(@_); |
|
42 |
warn "inside init low-level M6502 from $self\n"; |
warn "inside init low-level M6502 from $self\n"; |
43 |
}; |
}; |
44 |
|
|
53 |
sub read { |
sub read { |
54 |
my ($addr) = @_; |
my ($addr) = @_; |
55 |
my $byte = $mem[$addr]; |
my $byte = $mem[$addr]; |
56 |
warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
warn "## M6502::read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
57 |
return $byte; |
return $byte; |
58 |
} |
} |
59 |
|
|
66 |
=cut |
=cut |
67 |
|
|
68 |
sub write { |
sub write { |
69 |
warn "# write(",dump(@_),")\n" if $debug; |
warn "## M6502::write(",dump(@_),")\n" if $debug; |
70 |
my ($addr,$byte) = @_; |
my ($addr,$byte) = @_; |
71 |
$mem[$addr] = $byte; |
$mem[$addr] = $byte; |
72 |
} |
} |
83 |
sub poke_code { |
sub poke_code { |
84 |
my $self = shift; |
my $self = shift; |
85 |
my $addr = shift; |
my $addr = shift; |
86 |
warn sprintf("# poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; |
warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; |
87 |
#$mem[$addr++] = $_ foreach @_; |
#$mem[$addr++] = $_ foreach @_; |
88 |
$self->write($addr++, $_) foreach @_; |
# call low-level write |
89 |
|
Arch::write($addr++, $_) foreach @_; |
90 |
} |
} |
91 |
|
|
92 |
=head2 write_chunk |
=head2 ram |
93 |
|
|
94 |
Low-level update of memory, overriding user specified MMU functions C<read> and C<write> |
Read series of bytes into memory without MMU interaction |
95 |
|
|
96 |
$emu->write_chunk( $address, $chunk_of_data ); |
my @code = $emu->ram( 0xc000, 0xc1000 ); |
97 |
|
|
98 |
=cut |
=cut |
99 |
|
|
100 |
sub write_chunk { |
sub ram { |
101 |
my ($self, $addr, $chunk) = @_; |
my $self = shift; |
102 |
my $len = length($chunk); |
my ( $from, $to ) = @_; |
103 |
splice @mem, $addr, $len, unpack('C*', $chunk); |
warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug; |
104 |
|
return @mem[ $from .. $to ]; |
105 |
} |
} |
106 |
|
|
107 |
=head2 ram |
=head2 write_chunk |
108 |
|
|
109 |
Read searies of bytes from memory without passing through MMU |
Low-level update of memory, overriding user specified MMU functions C<read> and C<write> |
110 |
|
|
111 |
$emu->ram( $from, $to ); |
$emu->write_chunk( $address, $chunk_of_data ); |
112 |
|
|
113 |
=cut |
=cut |
114 |
|
|
115 |
sub ram { |
sub write_chunk { |
116 |
my $self = shift; |
my ($self, $addr, $chunk) = @_; |
117 |
my ($from,$to) = @_; |
my $len = length($chunk); |
118 |
warn "ram($from,$to)\n"; |
splice @mem, $addr, $len, unpack('C*', $chunk); |
|
if ($from + $to) { |
|
|
printf "ram %04x - %04x\n", $from, $to; |
|
|
return @mem[$from .. $to - 1]; |
|
|
} |
|
|
printf "ram %04x\n", $from; |
|
|
return $mem[$from] if defined($from); |
|
|
confess "no from address"; |
|
119 |
} |
} |
120 |
|
|
121 |
=head2 prompt |
=head2 prompt |
136 |
=cut |
=cut |
137 |
|
|
138 |
sub push_R { |
sub push_R { |
139 |
warn "push_R(",dump(@_),")\n"; |
warn "## M6502::push_R(",dump(@_),")\n" if $debug; |
140 |
my ( $a, $p, $x, $y, $s, $pc ) = @_; |
my ( $a, $p, $x, $y, $s, $pc ) = @_; |
141 |
$PC = $pc; |
$PC = $pc; |
142 |
$S=$s; $X=$x; $Y=$y; $P=$p; $A=$a; |
$S=$s; $X=$x; $Y=$y; $P=$p; $A=$a; |
150 |
=cut |
=cut |
151 |
|
|
152 |
sub dump_R { |
sub dump_R { |
153 |
warn sprintf("PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S); |
warn sprintf("## M6502::dump_R PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S) if $debug; |
154 |
} |
} |
155 |
|
|
156 |
|
|
157 |
|
=head1 AUTHOR |
158 |
|
|
159 |
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
160 |
|
|
161 |
|
=head1 COPYRIGHT & LICENSE |
162 |
|
|
163 |
|
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
164 |
|
|
165 |
|
This program is free software; you can redistribute it and/or modify it |
166 |
|
under the same terms as Perl itself. |
167 |
|
|
168 |
|
=cut |
169 |
1; |
1; |