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'dump_R @mem $PC $A $P $X $Y $S $IPeriod $ICount $IRequest $IAutoReset $TrapBadOps $Trap $Trace $run_for $debug'; |
our @EXPORT = qw'dump_R @mem $PC $A $P $X $Y $S $IPeriod $ICount $IRequest $IAutoReset $TrapBadOps $Trap $Trace $debug'; |
10 |
our $VERSION = '0.0.2'; |
our $VERSION = '0.0.3'; |
11 |
require XSLoader; |
require XSLoader; |
12 |
XSLoader::load('M6502', $VERSION); |
XSLoader::load('M6502', $VERSION); |
13 |
|
|
21 |
|
|
22 |
our $debug = 0; |
our $debug = 0; |
23 |
|
|
24 |
our @mem = (0xff) x 0x10000; # 64M |
our @mem; |
25 |
|
#@mem = (0xff) x 0x10000; # 64M |
26 |
|
tie @mem, 'M6502::TieMem'; |
27 |
|
|
28 |
# program counter |
# program counter |
29 |
our $PC = 0xbeef; |
our $PC = 0xbeef; |
38 |
our $Trap; # Set Trap to address to trace from |
our $Trap; # Set Trap to address to trace from |
39 |
our $Trace; # Set Trace=1 to start tracing |
our $Trace; # Set Trace=1 to start tracing |
40 |
|
|
|
# Exec6502 cycles |
|
|
our $run_for = 0; |
|
|
|
|
41 |
=head2 init |
=head2 init |
42 |
|
|
43 |
Setup read and write memory hooks (to implement memory mapped devices) |
Setup read and write memory hooks (to implement memory mapped devices) |
55 |
|
|
56 |
our $_rw_hooks = { |
our $_rw_hooks = { |
57 |
read => sub { |
read => sub { |
58 |
|
warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug; |
59 |
return $mem[$_[0]]; |
return $mem[$_[0]]; |
60 |
}, |
}, |
61 |
write => sub { |
write => sub { |
62 |
|
warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug; |
63 |
$mem[$_[0]] = $_[1]; |
$mem[$_[0]] = $_[1]; |
64 |
}, |
}, |
65 |
}; |
}; |
89 |
my $self = shift; |
my $self = shift; |
90 |
my $addr = shift; |
my $addr = shift; |
91 |
warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; |
warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; |
|
#$mem[$addr++] = $_ foreach @_; |
|
92 |
# call low-level write |
# call low-level write |
93 |
$_rw_hooks->{write}->( $addr++, $_ ) foreach @_; |
#$_rw_hooks->{write}->( $addr++, $_ ) foreach @_; |
94 |
|
$mem[$addr++] = $_ foreach @_; |
95 |
} |
} |
96 |
|
|
97 |
=head2 ram |
=head2 ram |
123 |
splice @mem, $addr, $len, unpack('C*', $chunk); |
splice @mem, $addr, $len, unpack('C*', $chunk); |
124 |
} |
} |
125 |
|
|
|
=head2 prompt |
|
|
|
|
|
Call this after C<< $run_for >> cycles have been run on processor |
|
|
|
|
|
=cut |
|
|
|
|
|
sub prompt { |
|
|
warn "prompt -- you should override this\n"; |
|
|
return 1; |
|
|
} |
|
|
|
|
126 |
=head1 XS Callbacks |
=head1 XS Callbacks |
127 |
|
|
128 |
This functions are called from C<M6502.xs> |
This functions are called from C<M6502.xs> |
163 |
dump_R(); |
dump_R(); |
164 |
} |
} |
165 |
|
|
166 |
|
=head1 XS |
167 |
|
|
168 |
|
Following functions are implemented in C<M6502.xs> and exported to perl. |
169 |
|
|
170 |
|
=head2 set_debug |
171 |
|
|
172 |
|
M6502::set_debug( 0 ); |
173 |
|
|
174 |
|
=head2 get_debug |
175 |
|
|
176 |
|
my $debug = M6502::set_debug(); |
177 |
|
|
178 |
|
=head2 reset |
179 |
|
|
180 |
|
Reset 6502 CPU, reading PC from C<0xfffc> |
181 |
|
|
182 |
|
M6502::reset(); |
183 |
|
|
184 |
|
=head2 update_C_R |
185 |
|
|
186 |
|
Push perl notion of register values to CPU emulator |
187 |
|
|
188 |
|
M6502::update_C_R(); |
189 |
|
|
190 |
|
=head2 update_perl_R |
191 |
|
|
192 |
|
Update perl notion of register values |
193 |
|
|
194 |
|
M6502::update_perl_R(); |
195 |
|
|
196 |
|
=head2 exec |
197 |
|
|
198 |
|
Execute cpu for specified number of cycles |
199 |
|
|
200 |
|
my $cycles_left = M6502::exec( $execute_cpu_cycles ); |
201 |
|
|
202 |
|
=head1 Helpers |
203 |
|
|
204 |
=head2 dump_R |
=head2 dump_R |
205 |
|
|
206 |
helper function which dumps registers in humanly readable form |
helper function which dumps registers in humanly readable form |
250 |
|
|
251 |
=head1 COPYRIGHT & LICENSE |
=head1 COPYRIGHT & LICENSE |
252 |
|
|
253 |
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
Copyright 2007-8 Dobrica Pavlinusic, All Rights Reserved. |
254 |
|
|
255 |
This program is free software; you can redistribute it and/or modify it |
This program is free software; you can redistribute it and/or modify it |
256 |
under the same terms as Perl itself. |
under the same terms as Perl itself. |
257 |
|
|
258 |
=cut |
=cut |
259 |
|
|
260 |
|
package M6502::TieMem; |
261 |
|
|
262 |
|
use strict; |
263 |
|
use warnings; |
264 |
|
use Tie::Array; |
265 |
|
use base qw(Tie::Array); |
266 |
|
|
267 |
|
# |
268 |
|
sub TIEARRAY { |
269 |
|
my $class = shift; |
270 |
|
# my $opt = shift; |
271 |
|
# my $self = { %$opt }; |
272 |
|
my $self = {}; |
273 |
|
bless($self, __PACKAGE__); |
274 |
|
return $self; |
275 |
|
} |
276 |
|
|
277 |
|
sub DESTROY {} |
278 |
|
|
279 |
|
# |
280 |
|
sub FETCH { |
281 |
|
my $self = shift; |
282 |
|
my $n = shift; |
283 |
|
my $val = M6502::mem_peek( $n ); |
284 |
|
# warn sprintf("FETCH %04x = %02x\n", $n, $val); |
285 |
|
return $val; |
286 |
|
} |
287 |
|
|
288 |
|
# |
289 |
|
sub FETCHSIZE { |
290 |
|
return 0xffff; |
291 |
|
} |
292 |
|
|
293 |
|
# |
294 |
|
sub STORE { |
295 |
|
my $self = shift; |
296 |
|
my $n = shift; |
297 |
|
my $val = shift; |
298 |
|
if ( $n > 0xffff ) { |
299 |
|
warn "over 64k: $n\n"; |
300 |
|
return; |
301 |
|
} |
302 |
|
M6502::mem_poke( $n, $val ); |
303 |
|
# warn sprintf("STORE %04x <- %02x\n",$n, $val); |
304 |
|
return $val; |
305 |
|
} |
306 |
|
|
307 |
|
# |
308 |
|
sub STORESIZE { |
309 |
|
die('not allowed (yet)'); |
310 |
|
} |
311 |
|
|
312 |
|
sub PUSH { |
313 |
|
die('not allowed (yet)'); |
314 |
|
} |
315 |
|
|
316 |
|
sub POP { |
317 |
|
die('not allowed (yet)'); |
318 |
|
} |
319 |
|
|
320 |
|
sub SHIFT { |
321 |
|
die('not allowed (yet)'); |
322 |
|
} |
323 |
|
|
324 |
|
sub UNSHIFT { |
325 |
|
die('not allowed (yet)'); |
326 |
|
} |
327 |
|
|
328 |
|
sub DELETE { |
329 |
|
my $self = shift; |
330 |
|
my $n = shift; |
331 |
|
$self->STORE($n, 0); |
332 |
|
} |
333 |
|
|
334 |
|
sub EXISTS { |
335 |
|
my $self = shift; |
336 |
|
my $n = shift; |
337 |
|
return 0 if $n > 0xffff; |
338 |
|
return 1; |
339 |
|
} |
340 |
|
|
341 |
1; |
1; |