1 |
dpavlin |
24 |
package M6502; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
|
6 |
dpavlin |
27 |
use Data::Dump qw/dump/; |
7 |
dpavlin |
29 |
use Carp qw/confess/; |
8 |
dpavlin |
34 |
use Exporter 'import'; |
9 |
dpavlin |
86 |
our @EXPORT = qw'dump_R @mem $PC $A $P $X $Y $S $IPeriod $ICount $IRequest $IAutoReset $TrapBadOps $Trap $Trace $run_for $debug'; |
10 |
dpavlin |
80 |
our $VERSION = '0.0.2'; |
11 |
|
|
require XSLoader; |
12 |
|
|
XSLoader::load('M6502', $VERSION); |
13 |
dpavlin |
27 |
|
14 |
dpavlin |
29 |
=head1 NAME |
15 |
dpavlin |
24 |
|
16 |
dpavlin |
54 |
M6502 - perl bindings for M6502 CPU emulator |
17 |
dpavlin |
29 |
|
18 |
dpavlin |
89 |
=head1 FUNCTIONS |
19 |
|
|
|
20 |
dpavlin |
29 |
=cut |
21 |
|
|
|
22 |
dpavlin |
87 |
our $debug = 0; |
23 |
dpavlin |
24 |
|
24 |
dpavlin |
29 |
our @mem = (0xff) x 0x10000; # 64M |
25 |
|
|
|
26 |
dpavlin |
25 |
# program counter |
27 |
dpavlin |
34 |
our $PC = 0xbeef; |
28 |
dpavlin |
25 |
# CPU registars |
29 |
dpavlin |
36 |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
30 |
dpavlin |
74 |
|
31 |
|
|
our $IPeriod=1; # Set IPeriod to number of CPU cycles between calls to Loop6502 |
32 |
|
|
our $ICount; |
33 |
|
|
our $IRequest; # Set to the INT_IRQ when pending IRQ |
34 |
|
|
our $IAutoReset; # Set to 1 to autom. reset IRequest |
35 |
|
|
our $TrapBadOps=1; # Set to 1 to warn of illegal opcodes |
36 |
|
|
our $Trap; # Set Trap to address to trace from |
37 |
|
|
our $Trace; # Set Trace=1 to start tracing |
38 |
|
|
|
39 |
dpavlin |
40 |
# Exec6502 cycles |
40 |
dpavlin |
42 |
our $run_for = 0; |
41 |
dpavlin |
24 |
|
42 |
dpavlin |
54 |
=head2 init |
43 |
|
|
|
44 |
dpavlin |
89 |
Setup read and write memory hooks (to implement memory mapped devices) |
45 |
dpavlin |
26 |
|
46 |
dpavlin |
89 |
$init->( |
47 |
|
|
read => sub { |
48 |
|
|
return $mem[$_[0]]; |
49 |
|
|
}, |
50 |
|
|
write => sub { |
51 |
|
|
$mem[$_[0]] = $_[1]; |
52 |
|
|
}, |
53 |
|
|
); |
54 |
|
|
|
55 |
dpavlin |
26 |
=cut |
56 |
|
|
|
57 |
dpavlin |
89 |
our $_rw_hooks = { |
58 |
|
|
read => sub { |
59 |
dpavlin |
94 |
warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug; |
60 |
dpavlin |
89 |
return $mem[$_[0]]; |
61 |
|
|
}, |
62 |
|
|
write => sub { |
63 |
dpavlin |
92 |
warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug; |
64 |
dpavlin |
89 |
$mem[$_[0]] = $_[1]; |
65 |
|
|
}, |
66 |
|
|
}; |
67 |
|
|
|
68 |
dpavlin |
26 |
sub init { |
69 |
dpavlin |
33 |
my $self = shift; |
70 |
dpavlin |
89 |
my $args = {@_}; |
71 |
|
|
warn "inside init low-level M6502 from ",ref($self),"\n"; |
72 |
dpavlin |
27 |
|
73 |
dpavlin |
89 |
foreach my $p ( qw/read write/ ) { |
74 |
|
|
confess "need $p argument as coderef" unless ( $args->{$p} && ref($args->{$p}) eq 'CODE' ); |
75 |
|
|
$_rw_hooks->{$p} = $args->{$p}; |
76 |
|
|
} |
77 |
dpavlin |
27 |
|
78 |
dpavlin |
89 |
}; |
79 |
dpavlin |
27 |
|
80 |
dpavlin |
29 |
=head2 poke_code |
81 |
|
|
|
82 |
dpavlin |
42 |
Write series of bytes into memory passing through MMU (C<read> and C<write>) |
83 |
|
|
functions. If you don't want to trigger MMU, use C<write_chunk>. |
84 |
dpavlin |
29 |
|
85 |
|
|
$emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa ); |
86 |
|
|
|
87 |
|
|
=cut |
88 |
|
|
|
89 |
|
|
sub poke_code { |
90 |
|
|
my $self = shift; |
91 |
|
|
my $addr = shift; |
92 |
dpavlin |
50 |
warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; |
93 |
dpavlin |
42 |
#$mem[$addr++] = $_ foreach @_; |
94 |
dpavlin |
51 |
# call low-level write |
95 |
dpavlin |
90 |
$_rw_hooks->{write}->( $addr++, $_ ) foreach @_; |
96 |
dpavlin |
29 |
} |
97 |
|
|
|
98 |
dpavlin |
51 |
=head2 ram |
99 |
|
|
|
100 |
|
|
Read series of bytes into memory without MMU interaction |
101 |
|
|
|
102 |
|
|
my @code = $emu->ram( 0xc000, 0xc1000 ); |
103 |
|
|
|
104 |
|
|
=cut |
105 |
|
|
|
106 |
|
|
sub ram { |
107 |
|
|
my $self = shift; |
108 |
|
|
my ( $from, $to ) = @_; |
109 |
|
|
warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug; |
110 |
|
|
return @mem[ $from .. $to ]; |
111 |
|
|
} |
112 |
|
|
|
113 |
dpavlin |
31 |
=head2 write_chunk |
114 |
|
|
|
115 |
dpavlin |
42 |
Low-level update of memory, overriding user specified MMU functions C<read> and C<write> |
116 |
|
|
|
117 |
dpavlin |
31 |
$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 |
dpavlin |
42 |
=head2 prompt |
128 |
|
|
|
129 |
|
|
Call this after C<< $run_for >> cycles have been run on processor |
130 |
|
|
|
131 |
|
|
=cut |
132 |
|
|
|
133 |
|
|
sub prompt { |
134 |
|
|
warn "prompt -- you should override this\n"; |
135 |
|
|
return 1; |
136 |
|
|
} |
137 |
|
|
|
138 |
dpavlin |
89 |
=head1 XS Callbacks |
139 |
|
|
|
140 |
|
|
This functions are called from C<M6502.xs> |
141 |
|
|
|
142 |
|
|
=head2 _read |
143 |
|
|
|
144 |
|
|
Read from memory C callback |
145 |
|
|
|
146 |
|
|
$byte = M6502::_read( $address ); |
147 |
|
|
|
148 |
|
|
=cut |
149 |
|
|
|
150 |
|
|
sub _read { |
151 |
|
|
return $_rw_hooks->{read}->( @_ ); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
=head2 _write |
155 |
|
|
|
156 |
|
|
Write into memory C callback |
157 |
|
|
|
158 |
|
|
M6502:_write( $address, $byte ); |
159 |
|
|
|
160 |
|
|
=cut |
161 |
|
|
|
162 |
|
|
sub _write { |
163 |
|
|
return $_rw_hooks->{write}->( @_ ); |
164 |
|
|
} |
165 |
|
|
|
166 |
dpavlin |
87 |
=head2 _update_perl_R |
167 |
dpavlin |
38 |
|
168 |
dpavlin |
87 |
called by C<M6502.xs> to push changes in registars back to perl variables |
169 |
dpavlin |
38 |
|
170 |
|
|
=cut |
171 |
|
|
|
172 |
dpavlin |
87 |
sub _update_perl_R { |
173 |
|
|
warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug; |
174 |
dpavlin |
74 |
( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_; |
175 |
dpavlin |
38 |
dump_R(); |
176 |
|
|
} |
177 |
|
|
|
178 |
dpavlin |
94 |
=head1 XS |
179 |
|
|
|
180 |
|
|
Following functions are implemented in C<M6502.xs> and exported to perl. |
181 |
|
|
|
182 |
|
|
=head2 set_debug |
183 |
|
|
|
184 |
|
|
M6502::set_debug( 0 ); |
185 |
|
|
|
186 |
|
|
=head2 get_debug |
187 |
|
|
|
188 |
|
|
my $debug = M6502::set_debug(); |
189 |
|
|
|
190 |
|
|
=head2 reset |
191 |
|
|
|
192 |
|
|
Reset 6502 CPU, reading PC from C<0xfffc> |
193 |
|
|
|
194 |
|
|
M6502::reset(); |
195 |
|
|
|
196 |
|
|
=head2 update_C_R |
197 |
|
|
|
198 |
|
|
Push perl notion of register values to CPU emulator |
199 |
|
|
|
200 |
|
|
M6502::update_C_R(); |
201 |
|
|
|
202 |
|
|
=head2 update_perl_R |
203 |
|
|
|
204 |
|
|
Update perl notion of register values |
205 |
|
|
|
206 |
|
|
M6502::update_perl_R(); |
207 |
|
|
|
208 |
|
|
=head2 exec |
209 |
|
|
|
210 |
|
|
Execute cpu for specified number of cycles |
211 |
|
|
|
212 |
|
|
my $cycles_left = M6502::exec( $execute_cpu_cycles ); |
213 |
|
|
|
214 |
|
|
=head1 Helpers |
215 |
|
|
|
216 |
dpavlin |
38 |
=head2 dump_R |
217 |
|
|
|
218 |
dpavlin |
68 |
helper function which dumps registers in humanly readable form |
219 |
dpavlin |
38 |
|
220 |
dpavlin |
68 |
my $dump = dump_R; |
221 |
|
|
|
222 |
dpavlin |
38 |
=cut |
223 |
|
|
|
224 |
|
|
sub dump_R { |
225 |
dpavlin |
74 |
my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x " |
226 |
|
|
. "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d" |
227 |
|
|
. "\n", |
228 |
|
|
$PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace, |
229 |
|
|
); |
230 |
dpavlin |
68 |
warn "## M6502::dump_R $dump" if $debug; |
231 |
|
|
return $dump; |
232 |
dpavlin |
38 |
} |
233 |
|
|
|
234 |
dpavlin |
87 |
=head2 debug |
235 |
|
|
|
236 |
|
|
Turn perl and C-level debugging on/off |
237 |
|
|
|
238 |
|
|
$emu->debug( 0 ); |
239 |
|
|
$emu->debug( 1 ); |
240 |
|
|
print $emu->debug; |
241 |
|
|
|
242 |
|
|
=cut |
243 |
|
|
|
244 |
|
|
sub debug { |
245 |
|
|
my $self = shift; |
246 |
|
|
my $value = shift; |
247 |
|
|
if (defined($value)) { |
248 |
|
|
$debug = M6502::set_debug($value); |
249 |
|
|
} else { |
250 |
|
|
$debug = M6502::get_debug(); |
251 |
|
|
} |
252 |
|
|
return $debug; |
253 |
|
|
} |
254 |
|
|
|
255 |
dpavlin |
55 |
=head1 SEE ALSO |
256 |
dpavlin |
54 |
|
257 |
dpavlin |
55 |
L<Orao> is sample implementation using this module |
258 |
|
|
|
259 |
dpavlin |
54 |
=head1 AUTHOR |
260 |
|
|
|
261 |
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
262 |
|
|
|
263 |
|
|
=head1 COPYRIGHT & LICENSE |
264 |
|
|
|
265 |
|
|
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
266 |
|
|
|
267 |
|
|
This program is free software; you can redistribute it and/or modify it |
268 |
|
|
under the same terms as Perl itself. |
269 |
|
|
|
270 |
|
|
=cut |
271 |
dpavlin |
24 |
1; |