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 $ICount $IRequest $IAutoReset $TrapBadOps $Trap $Trace $run_for $debug'; |
10 |
|
our $VERSION = '0.0.2'; |
11 |
|
require XSLoader; |
12 |
|
XSLoader::load('M6502', $VERSION); |
13 |
|
|
14 |
=head1 NAME |
=head1 NAME |
15 |
|
|
16 |
M6502 - perl bindings for 6502 emulator |
M6502 - perl bindings for M6502 CPU emulator |
17 |
|
|
18 |
=cut |
=cut |
19 |
|
|
20 |
my $debug = 1; |
my $debug = 0; |
|
|
|
|
our $VERSION = qw(0.0.1); |
|
21 |
|
|
22 |
our @mem = (0xff) x 0x10000; # 64M |
our @mem = (0xff) x 0x10000; # 64M |
23 |
|
|
24 |
# program counter |
# program counter |
25 |
our $PC = 0xdd11; |
our $PC = 0xbeef; |
26 |
# CPU registars |
# CPU registars |
27 |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
|
# Set IPeriod to number of CPU cycles between calls to Loop6502 |
|
|
our $IPeriod = 1; |
|
28 |
|
|
29 |
=head1 init |
our $IPeriod=1; # Set IPeriod to number of CPU cycles between calls to Loop6502 |
30 |
|
our $ICount; |
31 |
|
our $IRequest; # Set to the INT_IRQ when pending IRQ |
32 |
|
our $IAutoReset; # Set to 1 to autom. reset IRequest |
33 |
|
our $TrapBadOps=1; # Set to 1 to warn of illegal opcodes |
34 |
|
our $Trap; # Set Trap to address to trace from |
35 |
|
our $Trace; # Set Trace=1 to start tracing |
36 |
|
|
37 |
|
# Exec6502 cycles |
38 |
|
our $run_for = 0; |
39 |
|
|
40 |
|
=head1 FUNCTIONS |
41 |
|
|
42 |
|
=head2 init |
43 |
|
|
44 |
Called before C<Run6502> |
Called before C<Run6502> |
45 |
|
|
47 |
|
|
48 |
sub init { |
sub init { |
49 |
my $self = shift; |
my $self = shift; |
|
warn dump(@_); |
|
50 |
warn "inside init low-level M6502 from $self\n"; |
warn "inside init low-level M6502 from $self\n"; |
51 |
}; |
}; |
52 |
|
|
61 |
sub read { |
sub read { |
62 |
my ($addr) = @_; |
my ($addr) = @_; |
63 |
my $byte = $mem[$addr]; |
my $byte = $mem[$addr]; |
64 |
warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
warn "## M6502::read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
65 |
return $byte; |
return $byte; |
66 |
} |
} |
67 |
|
|
74 |
=cut |
=cut |
75 |
|
|
76 |
sub write { |
sub write { |
77 |
warn "# write(",dump(@_),")\n" if $debug; |
warn "## M6502::write(",dump(@_),")\n" if $debug; |
78 |
my ($addr,$byte) = @_; |
my ($addr,$byte) = @_; |
79 |
$mem[$addr] = $byte; |
$mem[$addr] = $byte; |
80 |
} |
} |
81 |
|
|
82 |
=head2 poke_code |
=head2 poke_code |
83 |
|
|
84 |
Write series of bytes into memory without passing through MMU |
Write series of bytes into memory passing through MMU (C<read> and C<write>) |
85 |
|
functions. If you don't want to trigger MMU, use C<write_chunk>. |
86 |
|
|
87 |
$emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa ); |
$emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa ); |
88 |
|
|
91 |
sub poke_code { |
sub poke_code { |
92 |
my $self = shift; |
my $self = shift; |
93 |
my $addr = shift; |
my $addr = shift; |
94 |
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; |
95 |
$mem[$addr++] = $_ foreach @_; |
#$mem[$addr++] = $_ foreach @_; |
96 |
|
# call low-level write |
97 |
|
Arch::write($addr++, $_) foreach @_; |
98 |
|
} |
99 |
|
|
100 |
|
=head2 ram |
101 |
|
|
102 |
|
Read series of bytes into memory without MMU interaction |
103 |
|
|
104 |
|
my @code = $emu->ram( 0xc000, 0xc1000 ); |
105 |
|
|
106 |
|
=cut |
107 |
|
|
108 |
|
sub ram { |
109 |
|
my $self = shift; |
110 |
|
my ( $from, $to ) = @_; |
111 |
|
warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug; |
112 |
|
return @mem[ $from .. $to ]; |
113 |
} |
} |
114 |
|
|
115 |
=head2 write_chunk |
=head2 write_chunk |
116 |
|
|
117 |
|
Low-level update of memory, overriding user specified MMU functions C<read> and C<write> |
118 |
|
|
119 |
$emu->write_chunk( $address, $chunk_of_data ); |
$emu->write_chunk( $address, $chunk_of_data ); |
120 |
|
|
121 |
=cut |
=cut |
126 |
splice @mem, $addr, $len, unpack('C*', $chunk); |
splice @mem, $addr, $len, unpack('C*', $chunk); |
127 |
} |
} |
128 |
|
|
129 |
=head2 ram |
=head2 prompt |
130 |
|
|
131 |
|
Call this after C<< $run_for >> cycles have been run on processor |
132 |
|
|
133 |
|
=cut |
134 |
|
|
135 |
Read searies of bytes from memory without passing through MMU |
sub prompt { |
136 |
|
warn "prompt -- you should override this\n"; |
137 |
|
return 1; |
138 |
|
} |
139 |
|
|
140 |
|
=head2 push_R |
141 |
|
|
142 |
$emu->ram( $from, $to ); |
called by C<perl.c> to push changes in registars back to perl variables |
143 |
|
|
144 |
=cut |
=cut |
145 |
|
|
146 |
sub ram { |
sub push_R { |
147 |
my $self = shift; |
warn "## M6502::push_R(",dump(@_),")\n" if $debug; |
148 |
my ($from,$to) = @_; |
( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_; |
149 |
if ($from + $to) { |
dump_R(); |
|
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"; |
|
150 |
} |
} |
151 |
|
|
152 |
|
=head2 dump_R |
153 |
|
|
154 |
|
helper function which dumps registers in humanly readable form |
155 |
|
|
156 |
|
my $dump = dump_R; |
157 |
|
|
158 |
|
=cut |
159 |
|
|
160 |
|
sub dump_R { |
161 |
|
my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x " |
162 |
|
. "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d" |
163 |
|
. "\n", |
164 |
|
$PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace, |
165 |
|
); |
166 |
|
warn "## M6502::dump_R $dump" if $debug; |
167 |
|
return $dump; |
168 |
|
} |
169 |
|
|
170 |
|
=head1 SEE ALSO |
171 |
|
|
172 |
|
L<Orao> is sample implementation using this module |
173 |
|
|
174 |
|
=head1 AUTHOR |
175 |
|
|
176 |
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
177 |
|
|
178 |
|
=head1 COPYRIGHT & LICENSE |
179 |
|
|
180 |
|
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
181 |
|
|
182 |
|
This program is free software; you can redistribute it and/or modify it |
183 |
|
under the same terms as Perl itself. |
184 |
|
|
185 |
|
=cut |
186 |
1; |
1; |