5 |
|
|
6 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
7 |
use Carp qw/confess/; |
use Carp qw/confess/; |
8 |
|
use Exporter 'import'; |
9 |
|
our @EXPORT = qw'@mem $PC $A $P $X $Y $S $IPeriod $run_for $debug'; |
10 |
|
|
11 |
=head1 NAME |
=head1 NAME |
12 |
|
|
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 |
|
|
26 |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
27 |
# Set IPeriod to number of CPU cycles between calls to Loop6502 |
# Set IPeriod to number of CPU cycles between calls to Loop6502 |
28 |
our $IPeriod = 1; |
our $IPeriod = 1; |
29 |
|
# Exec6502 cycles |
30 |
|
our $run_for = 0; |
31 |
|
|
32 |
=head1 init |
=head1 init |
33 |
|
|
36 |
=cut |
=cut |
37 |
|
|
38 |
sub init { |
sub init { |
39 |
warn "inside init\n"; |
my $self = shift; |
40 |
print "stdout\n"; |
warn "inside init low-level M6502 from $self\n"; |
41 |
}; |
}; |
42 |
|
|
43 |
=head2 read |
=head2 read |
51 |
sub read { |
sub read { |
52 |
my ($addr) = @_; |
my ($addr) = @_; |
53 |
my $byte = $mem[$addr]; |
my $byte = $mem[$addr]; |
54 |
warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
warn "## M6502::read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
55 |
return $byte; |
return $byte; |
56 |
} |
} |
57 |
|
|
64 |
=cut |
=cut |
65 |
|
|
66 |
sub write { |
sub write { |
67 |
warn "# write(",dump(@_),")\n" if $debug; |
warn "## M6502::write(",dump(@_),")\n" if $debug; |
68 |
my ($addr,$byte) = @_; |
my ($addr,$byte) = @_; |
69 |
$mem[$addr] = $byte; |
$mem[$addr] = $byte; |
70 |
} |
} |
71 |
|
|
72 |
=head2 poke_code |
=head2 poke_code |
73 |
|
|
74 |
Write series of bytes into memory without passing through MMU |
Write series of bytes into memory passing through MMU (C<read> and C<write>) |
75 |
|
functions. If you don't want to trigger MMU, use C<write_chunk>. |
76 |
|
|
77 |
$emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa ); |
$emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa ); |
78 |
|
|
81 |
sub poke_code { |
sub poke_code { |
82 |
my $self = shift; |
my $self = shift; |
83 |
my $addr = shift; |
my $addr = shift; |
84 |
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; |
85 |
$mem[$addr++] = $_ foreach @_; |
#$mem[$addr++] = $_ foreach @_; |
86 |
|
$self->write($addr++, $_) foreach @_; |
87 |
} |
} |
88 |
|
|
89 |
=head2 ram |
=head2 write_chunk |
90 |
|
|
91 |
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> |
92 |
|
|
93 |
$emu->ram( $from, $to ); |
$emu->write_chunk( $address, $chunk_of_data ); |
94 |
|
|
95 |
=cut |
=cut |
96 |
|
|
97 |
sub ram { |
sub write_chunk { |
98 |
my $self = shift; |
my ($self, $addr, $chunk) = @_; |
99 |
my ($from,$to) = @_; |
my $len = length($chunk); |
100 |
if ($from + $to) { |
splice @mem, $addr, $len, unpack('C*', $chunk); |
101 |
printf "ram %04x - %04x\n", $from, $to; |
} |
102 |
return $mem[$from .. $to - 1]; |
|
103 |
} |
=head2 prompt |
104 |
printf "ram %04x\n", $from; |
|
105 |
return $mem[$from] if defined($from); |
Call this after C<< $run_for >> cycles have been run on processor |
106 |
confess "no from address"; |
|
107 |
|
=cut |
108 |
|
|
109 |
|
sub prompt { |
110 |
|
warn "prompt -- you should override this\n"; |
111 |
|
return 1; |
112 |
|
} |
113 |
|
|
114 |
|
=head2 push_R |
115 |
|
|
116 |
|
called by C<perl.c> to push changes in registars back to perl variables |
117 |
|
|
118 |
|
=cut |
119 |
|
|
120 |
|
sub push_R { |
121 |
|
warn "## M6502::push_R(",dump(@_),")\n" if $debug; |
122 |
|
my ( $a, $p, $x, $y, $s, $pc ) = @_; |
123 |
|
$PC = $pc; |
124 |
|
$S=$s; $X=$x; $Y=$y; $P=$p; $A=$a; |
125 |
|
dump_R(); |
126 |
|
} |
127 |
|
|
128 |
|
=head2 dump_R |
129 |
|
|
130 |
|
helper function which dumps registers |
131 |
|
|
132 |
|
=cut |
133 |
|
|
134 |
|
sub dump_R { |
135 |
|
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; |
136 |
} |
} |
137 |
|
|
138 |
1; |
1; |