1 |
package M6502; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Data::Dump qw/dump/; |
7 |
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 |
12 |
|
13 |
M6502 - perl bindings for 6502 emulator |
14 |
|
15 |
=cut |
16 |
|
17 |
my $debug = 0; |
18 |
|
19 |
our $VERSION = qw(0.0.1); |
20 |
|
21 |
our @mem = (0xff) x 0x10000; # 64M |
22 |
|
23 |
# program counter |
24 |
our $PC = 0xbeef; |
25 |
# CPU registars |
26 |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
27 |
# Set IPeriod to number of CPU cycles between calls to Loop6502 |
28 |
our $IPeriod = 1; |
29 |
# Exec6502 cycles |
30 |
our $run_for = 0; |
31 |
|
32 |
=head1 init |
33 |
|
34 |
Called before C<Run6502> |
35 |
|
36 |
=cut |
37 |
|
38 |
sub init { |
39 |
my $self = shift; |
40 |
warn "inside init low-level M6502 from $self\n"; |
41 |
}; |
42 |
|
43 |
=head2 read |
44 |
|
45 |
Read from memory |
46 |
|
47 |
$byte = read( $address ); |
48 |
|
49 |
=cut |
50 |
|
51 |
sub read { |
52 |
my ($addr) = @_; |
53 |
my $byte = $mem[$addr]; |
54 |
warn "## M6502::read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
55 |
return $byte; |
56 |
} |
57 |
|
58 |
=head2 write |
59 |
|
60 |
Write into emory |
61 |
|
62 |
write( $address, $byte ); |
63 |
|
64 |
=cut |
65 |
|
66 |
sub write { |
67 |
warn "## M6502::write(",dump(@_),")\n" if $debug; |
68 |
my ($addr,$byte) = @_; |
69 |
$mem[$addr] = $byte; |
70 |
} |
71 |
|
72 |
=head2 poke_code |
73 |
|
74 |
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 ); |
78 |
|
79 |
=cut |
80 |
|
81 |
sub poke_code { |
82 |
my $self = shift; |
83 |
my $addr = shift; |
84 |
warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; |
85 |
#$mem[$addr++] = $_ foreach @_; |
86 |
# call low-level write |
87 |
Arch::write($addr++, $_) foreach @_; |
88 |
} |
89 |
|
90 |
=head2 ram |
91 |
|
92 |
Read series of bytes into memory without MMU interaction |
93 |
|
94 |
my @code = $emu->ram( 0xc000, 0xc1000 ); |
95 |
|
96 |
=cut |
97 |
|
98 |
sub ram { |
99 |
my $self = shift; |
100 |
my ( $from, $to ) = @_; |
101 |
warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug; |
102 |
return @mem[ $from .. $to ]; |
103 |
} |
104 |
|
105 |
=head2 write_chunk |
106 |
|
107 |
Low-level update of memory, overriding user specified MMU functions C<read> and C<write> |
108 |
|
109 |
$emu->write_chunk( $address, $chunk_of_data ); |
110 |
|
111 |
=cut |
112 |
|
113 |
sub write_chunk { |
114 |
my ($self, $addr, $chunk) = @_; |
115 |
my $len = length($chunk); |
116 |
splice @mem, $addr, $len, unpack('C*', $chunk); |
117 |
} |
118 |
|
119 |
=head2 prompt |
120 |
|
121 |
Call this after C<< $run_for >> cycles have been run on processor |
122 |
|
123 |
=cut |
124 |
|
125 |
sub prompt { |
126 |
warn "prompt -- you should override this\n"; |
127 |
return 1; |
128 |
} |
129 |
|
130 |
=head2 push_R |
131 |
|
132 |
called by C<perl.c> to push changes in registars back to perl variables |
133 |
|
134 |
=cut |
135 |
|
136 |
sub push_R { |
137 |
warn "## M6502::push_R(",dump(@_),")\n" if $debug; |
138 |
my ( $a, $p, $x, $y, $s, $pc ) = @_; |
139 |
$PC = $pc; |
140 |
$S=$s; $X=$x; $Y=$y; $P=$p; $A=$a; |
141 |
dump_R(); |
142 |
} |
143 |
|
144 |
=head2 dump_R |
145 |
|
146 |
helper function which dumps registers |
147 |
|
148 |
=cut |
149 |
|
150 |
sub dump_R { |
151 |
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; |
152 |
} |
153 |
|
154 |
1; |