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'dump_R @mem $PC $A $P $X $Y $S $IPeriod $ICount $IRequest $IAutoReset $TrapBadOps $Trap $Trace $debug'; |
10 |
our $VERSION = '0.0.4'; |
11 |
require XSLoader; |
12 |
XSLoader::load('M6502', $VERSION); |
13 |
|
14 |
=head1 NAME |
15 |
|
16 |
M6502 - perl bindings for M6502 CPU emulator |
17 |
|
18 |
=head1 FUNCTIONS |
19 |
|
20 |
=cut |
21 |
|
22 |
our $debug = 0; |
23 |
|
24 |
our @mem; |
25 |
#@mem = (0xff) x 0x10000; # 64M |
26 |
tie @mem, 'M6502::TieMem'; |
27 |
|
28 |
# program counter |
29 |
our $PC = 0xbeef; |
30 |
# CPU registars |
31 |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
32 |
|
33 |
our $IPeriod=1; # Set IPeriod to number of CPU cycles between calls to Loop6502 |
34 |
our $ICount; |
35 |
our $IRequest; # Set to the INT_IRQ when pending IRQ |
36 |
our $IAutoReset; # Set to 1 to autom. reset IRequest |
37 |
our $TrapBadOps=1; # Set to 1 to warn of illegal opcodes |
38 |
our $Trap; # Set Trap to address to trace from |
39 |
our $Trace; # Set Trace=1 to start tracing |
40 |
|
41 |
=head2 init |
42 |
|
43 |
Setup read and write memory hooks (to implement memory mapped devices) |
44 |
|
45 |
$init->( |
46 |
read => sub { |
47 |
return $mem[$_[0]]; |
48 |
}, |
49 |
write => sub { |
50 |
$mem[$_[0]] = $_[1]; |
51 |
}, |
52 |
); |
53 |
|
54 |
=cut |
55 |
|
56 |
our $_rw_hooks = { |
57 |
read => sub { |
58 |
warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug; |
59 |
return $mem[$_[0]]; |
60 |
}, |
61 |
write => sub { |
62 |
warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug; |
63 |
$mem[$_[0]] = $_[1]; |
64 |
}, |
65 |
}; |
66 |
|
67 |
sub init { |
68 |
my $self = shift; |
69 |
my $args = {@_}; |
70 |
warn "inside init low-level M6502 from ",ref($self),"\n"; |
71 |
|
72 |
foreach my $p ( qw/read write/ ) { |
73 |
confess "need $p argument as coderef" unless ( $args->{$p} && ref($args->{$p}) eq 'CODE' ); |
74 |
$_rw_hooks->{$p} = $args->{$p}; |
75 |
} |
76 |
|
77 |
}; |
78 |
|
79 |
=head2 poke_code |
80 |
|
81 |
Write series of bytes into memory passing through MMU (C<read> and C<write>) |
82 |
functions. If you don't want to trigger MMU, use C<write_chunk>. |
83 |
|
84 |
$emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa ); |
85 |
|
86 |
=cut |
87 |
|
88 |
sub poke_code { |
89 |
my $self = shift; |
90 |
my $addr = shift; |
91 |
warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; |
92 |
# call low-level write |
93 |
#$_rw_hooks->{write}->( $addr++, $_ ) foreach @_; |
94 |
$mem[$addr++] = $_ foreach @_; |
95 |
} |
96 |
|
97 |
=head2 ram |
98 |
|
99 |
Read series of bytes into memory without MMU interaction |
100 |
|
101 |
my @code = $emu->ram( 0xc000, 0xc1000 ); |
102 |
|
103 |
=cut |
104 |
|
105 |
sub ram { |
106 |
my $self = shift; |
107 |
my ( $from, $to ) = @_; |
108 |
warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug; |
109 |
# return @mem[ $from .. $to ]; |
110 |
return unpack('C*', M6502::mem_peek_region( $from, $to )); |
111 |
} |
112 |
|
113 |
=head2 write_chunk |
114 |
|
115 |
Low-level update of memory, overriding user specified MMU functions C<read> and C<write> |
116 |
|
117 |
$emu->write_chunk( $address, $chunk_of_data ); |
118 |
|
119 |
=cut |
120 |
|
121 |
sub write_chunk { |
122 |
my ($self, $addr, $chunk) = @_; |
123 |
my $len = length($chunk); |
124 |
splice @mem, $addr, $len, unpack('C*', $chunk); |
125 |
} |
126 |
|
127 |
=head1 XS Callbacks |
128 |
|
129 |
This functions are called from C<M6502.xs> |
130 |
|
131 |
=head2 _read |
132 |
|
133 |
Read from memory C callback |
134 |
|
135 |
$byte = M6502::_read( $address ); |
136 |
|
137 |
=cut |
138 |
|
139 |
sub _read { |
140 |
return $_rw_hooks->{read}->( @_ ); |
141 |
} |
142 |
|
143 |
=head2 _write |
144 |
|
145 |
Write into memory C callback |
146 |
|
147 |
M6502:_write( $address, $byte ); |
148 |
|
149 |
=cut |
150 |
|
151 |
sub _write { |
152 |
return $_rw_hooks->{write}->( @_ ); |
153 |
} |
154 |
|
155 |
=head2 _update_perl_R |
156 |
|
157 |
called by C<M6502.xs> to push changes in registars back to perl variables |
158 |
|
159 |
=cut |
160 |
|
161 |
sub _update_perl_R { |
162 |
warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug; |
163 |
( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_; |
164 |
dump_R(); |
165 |
} |
166 |
|
167 |
=head1 XS |
168 |
|
169 |
Following functions are implemented in C<M6502.xs> and exported to perl. |
170 |
|
171 |
=head2 set_debug |
172 |
|
173 |
M6502::set_debug( 0 ); |
174 |
|
175 |
=head2 get_debug |
176 |
|
177 |
my $debug = M6502::set_debug(); |
178 |
|
179 |
=head2 reset |
180 |
|
181 |
Reset 6502 CPU, reading PC from C<0xfffc> |
182 |
|
183 |
M6502::reset(); |
184 |
|
185 |
=head2 update_C_R |
186 |
|
187 |
Push perl notion of register values to CPU emulator |
188 |
|
189 |
M6502::update_C_R(); |
190 |
|
191 |
=head2 update_perl_R |
192 |
|
193 |
Update perl notion of register values |
194 |
|
195 |
M6502::update_perl_R(); |
196 |
|
197 |
=head2 exec |
198 |
|
199 |
Execute cpu for specified number of cycles |
200 |
|
201 |
my $cycles_left = M6502::exec( $execute_cpu_cycles ); |
202 |
|
203 |
=head1 Helpers |
204 |
|
205 |
=head2 dump_R |
206 |
|
207 |
helper function which dumps registers in humanly readable form |
208 |
|
209 |
my $dump = dump_R; |
210 |
|
211 |
=cut |
212 |
|
213 |
sub dump_R { |
214 |
my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x " |
215 |
. "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d" |
216 |
. "\n", |
217 |
$PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace, |
218 |
); |
219 |
warn "## M6502::dump_R $dump" if $debug; |
220 |
return $dump; |
221 |
} |
222 |
|
223 |
=head2 debug |
224 |
|
225 |
Turn perl and C-level debugging on/off |
226 |
|
227 |
$emu->debug( 0 ); |
228 |
$emu->debug( 1 ); |
229 |
print $emu->debug; |
230 |
|
231 |
=cut |
232 |
|
233 |
sub debug { |
234 |
my $self = shift; |
235 |
my $value = shift; |
236 |
if (defined($value)) { |
237 |
$debug = M6502::set_debug($value); |
238 |
} else { |
239 |
$debug = M6502::get_debug(); |
240 |
} |
241 |
return $debug; |
242 |
} |
243 |
|
244 |
=head1 SEE ALSO |
245 |
|
246 |
L<Orao> is sample implementation using this module |
247 |
|
248 |
=head1 AUTHOR |
249 |
|
250 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
251 |
|
252 |
=head1 COPYRIGHT & LICENSE |
253 |
|
254 |
Copyright 2007-8 Dobrica Pavlinusic, All Rights Reserved. |
255 |
|
256 |
This program is free software; you can redistribute it and/or modify it |
257 |
under the same terms as Perl itself. |
258 |
|
259 |
=cut |
260 |
|
261 |
package M6502::TieMem; |
262 |
|
263 |
use strict; |
264 |
use warnings; |
265 |
use Tie::Array; |
266 |
use base qw(Tie::Array); |
267 |
|
268 |
# |
269 |
sub TIEARRAY { |
270 |
my $class = shift; |
271 |
# my $opt = shift; |
272 |
# my $self = { %$opt }; |
273 |
my $self = {}; |
274 |
bless($self, __PACKAGE__); |
275 |
return $self; |
276 |
} |
277 |
|
278 |
sub DESTROY {} |
279 |
|
280 |
# |
281 |
sub FETCH { |
282 |
my $self = shift; |
283 |
my $n = shift; |
284 |
my $val = M6502::mem_peek( $n ); |
285 |
# warn sprintf("FETCH %04x = %02x\n", $n, $val); |
286 |
return $val; |
287 |
} |
288 |
|
289 |
# |
290 |
sub FETCHSIZE { |
291 |
return 0xffff; |
292 |
} |
293 |
|
294 |
# |
295 |
sub STORE { |
296 |
my $self = shift; |
297 |
my $n = shift; |
298 |
my $val = shift; |
299 |
if ( $n > 0xffff ) { |
300 |
warn "over 64k: $n\n"; |
301 |
return; |
302 |
} |
303 |
M6502::mem_poke( $n, $val ); |
304 |
# warn sprintf("STORE %04x <- %02x\n",$n, $val); |
305 |
return $val; |
306 |
} |
307 |
|
308 |
# |
309 |
sub STORESIZE { |
310 |
die('not allowed (yet)'); |
311 |
} |
312 |
|
313 |
sub PUSH { |
314 |
die('not allowed (yet)'); |
315 |
} |
316 |
|
317 |
sub POP { |
318 |
die('not allowed (yet)'); |
319 |
} |
320 |
|
321 |
sub SHIFT { |
322 |
die('not allowed (yet)'); |
323 |
} |
324 |
|
325 |
sub UNSHIFT { |
326 |
die('not allowed (yet)'); |
327 |
} |
328 |
|
329 |
sub DELETE { |
330 |
my $self = shift; |
331 |
my $n = shift; |
332 |
$self->STORE($n, 0); |
333 |
} |
334 |
|
335 |
sub EXISTS { |
336 |
my $self = shift; |
337 |
my $n = shift; |
338 |
return 0 if $n > 0xffff; |
339 |
return 1; |
340 |
} |
341 |
|
342 |
1; |