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.2'; |
11 |
require XSLoader; |
require XSLoader; |
12 |
XSLoader::load('M6502', $VERSION); |
XSLoader::load('M6502', $VERSION); |
15 |
|
|
16 |
M6502 - perl bindings for M6502 CPU emulator |
M6502 - perl bindings for M6502 CPU emulator |
17 |
|
|
18 |
|
=head1 FUNCTIONS |
19 |
|
|
20 |
=cut |
=cut |
21 |
|
|
22 |
my $debug = 1; |
our $debug = 0; |
23 |
|
|
24 |
our @mem = (0xff) x 0x10000; # 64M |
our @mem = (0xff) x 0x10000; # 64M |
25 |
|
|
36 |
our $Trap; # Set Trap to address to trace from |
our $Trap; # Set Trap to address to trace from |
37 |
our $Trace; # Set Trace=1 to start tracing |
our $Trace; # Set Trace=1 to start tracing |
38 |
|
|
|
# Exec6502 cycles |
|
|
our $run_for = 0; |
|
|
|
|
|
=head1 FUNCTIONS |
|
|
|
|
39 |
=head2 init |
=head2 init |
40 |
|
|
41 |
Called before C<Run6502> |
Setup read and write memory hooks (to implement memory mapped devices) |
42 |
|
|
43 |
=cut |
$init->( |
44 |
|
read => sub { |
45 |
sub init { |
return $mem[$_[0]]; |
46 |
my $self = shift; |
}, |
47 |
warn "inside init low-level M6502 from $self\n"; |
write => sub { |
48 |
}; |
$mem[$_[0]] = $_[1]; |
49 |
|
}, |
50 |
=head2 read |
); |
|
|
|
|
Read from memory |
|
|
|
|
|
$byte = read( $address ); |
|
51 |
|
|
52 |
=cut |
=cut |
53 |
|
|
54 |
sub read { |
our $_rw_hooks = { |
55 |
my ($addr) = @_; |
read => sub { |
56 |
my $byte = $mem[$addr]; |
warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug; |
57 |
warn "## M6502::read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
return $mem[$_[0]]; |
58 |
return $byte; |
}, |
59 |
} |
write => sub { |
60 |
|
warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug; |
61 |
=head2 write |
$mem[$_[0]] = $_[1]; |
62 |
|
}, |
63 |
Write into emory |
}; |
64 |
|
|
65 |
write( $address, $byte ); |
sub init { |
66 |
|
my $self = shift; |
67 |
|
my $args = {@_}; |
68 |
|
warn "inside init low-level M6502 from ",ref($self),"\n"; |
69 |
|
|
70 |
=cut |
foreach my $p ( qw/read write/ ) { |
71 |
|
confess "need $p argument as coderef" unless ( $args->{$p} && ref($args->{$p}) eq 'CODE' ); |
72 |
|
$_rw_hooks->{$p} = $args->{$p}; |
73 |
|
} |
74 |
|
|
75 |
sub write { |
}; |
|
warn "## M6502::write(",dump(@_),")\n" if $debug; |
|
|
my ($addr,$byte) = @_; |
|
|
$mem[$addr] = $byte; |
|
|
} |
|
76 |
|
|
77 |
=head2 poke_code |
=head2 poke_code |
78 |
|
|
89 |
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; |
90 |
#$mem[$addr++] = $_ foreach @_; |
#$mem[$addr++] = $_ foreach @_; |
91 |
# call low-level write |
# call low-level write |
92 |
Arch::write($addr++, $_) foreach @_; |
$_rw_hooks->{write}->( $addr++, $_ ) foreach @_; |
93 |
} |
} |
94 |
|
|
95 |
=head2 ram |
=head2 ram |
121 |
splice @mem, $addr, $len, unpack('C*', $chunk); |
splice @mem, $addr, $len, unpack('C*', $chunk); |
122 |
} |
} |
123 |
|
|
124 |
=head2 prompt |
=head1 XS Callbacks |
125 |
|
|
126 |
|
This functions are called from C<M6502.xs> |
127 |
|
|
128 |
Call this after C<< $run_for >> cycles have been run on processor |
=head2 _read |
129 |
|
|
130 |
|
Read from memory C callback |
131 |
|
|
132 |
|
$byte = M6502::_read( $address ); |
133 |
|
|
134 |
=cut |
=cut |
135 |
|
|
136 |
sub prompt { |
sub _read { |
137 |
warn "prompt -- you should override this\n"; |
return $_rw_hooks->{read}->( @_ ); |
|
return 1; |
|
138 |
} |
} |
139 |
|
|
140 |
=head2 push_R |
=head2 _write |
141 |
|
|
142 |
called by C<perl.c> to push changes in registars back to perl variables |
Write into memory C callback |
143 |
|
|
144 |
|
M6502:_write( $address, $byte ); |
145 |
|
|
146 |
=cut |
=cut |
147 |
|
|
148 |
sub push_R { |
sub _write { |
149 |
warn "## M6502::push_R(",dump(@_),")\n" if $debug; |
return $_rw_hooks->{write}->( @_ ); |
150 |
|
} |
151 |
|
|
152 |
|
=head2 _update_perl_R |
153 |
|
|
154 |
|
called by C<M6502.xs> to push changes in registars back to perl variables |
155 |
|
|
156 |
|
=cut |
157 |
|
|
158 |
|
sub _update_perl_R { |
159 |
|
warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug; |
160 |
( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_; |
( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_; |
161 |
dump_R(); |
dump_R(); |
162 |
} |
} |
163 |
|
|
164 |
|
=head1 XS |
165 |
|
|
166 |
|
Following functions are implemented in C<M6502.xs> and exported to perl. |
167 |
|
|
168 |
|
=head2 set_debug |
169 |
|
|
170 |
|
M6502::set_debug( 0 ); |
171 |
|
|
172 |
|
=head2 get_debug |
173 |
|
|
174 |
|
my $debug = M6502::set_debug(); |
175 |
|
|
176 |
|
=head2 reset |
177 |
|
|
178 |
|
Reset 6502 CPU, reading PC from C<0xfffc> |
179 |
|
|
180 |
|
M6502::reset(); |
181 |
|
|
182 |
|
=head2 update_C_R |
183 |
|
|
184 |
|
Push perl notion of register values to CPU emulator |
185 |
|
|
186 |
|
M6502::update_C_R(); |
187 |
|
|
188 |
|
=head2 update_perl_R |
189 |
|
|
190 |
|
Update perl notion of register values |
191 |
|
|
192 |
|
M6502::update_perl_R(); |
193 |
|
|
194 |
|
=head2 exec |
195 |
|
|
196 |
|
Execute cpu for specified number of cycles |
197 |
|
|
198 |
|
my $cycles_left = M6502::exec( $execute_cpu_cycles ); |
199 |
|
|
200 |
|
=head1 Helpers |
201 |
|
|
202 |
=head2 dump_R |
=head2 dump_R |
203 |
|
|
204 |
helper function which dumps registers in humanly readable form |
helper function which dumps registers in humanly readable form |
217 |
return $dump; |
return $dump; |
218 |
} |
} |
219 |
|
|
220 |
|
=head2 debug |
221 |
|
|
222 |
|
Turn perl and C-level debugging on/off |
223 |
|
|
224 |
|
$emu->debug( 0 ); |
225 |
|
$emu->debug( 1 ); |
226 |
|
print $emu->debug; |
227 |
|
|
228 |
|
=cut |
229 |
|
|
230 |
|
sub debug { |
231 |
|
my $self = shift; |
232 |
|
my $value = shift; |
233 |
|
if (defined($value)) { |
234 |
|
$debug = M6502::set_debug($value); |
235 |
|
} else { |
236 |
|
$debug = M6502::get_debug(); |
237 |
|
} |
238 |
|
return $debug; |
239 |
|
} |
240 |
|
|
241 |
=head1 SEE ALSO |
=head1 SEE ALSO |
242 |
|
|
243 |
L<Orao> is sample implementation using this module |
L<Orao> is sample implementation using this module |