4 |
use warnings; |
use warnings; |
5 |
|
|
6 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
7 |
|
use Carp qw/confess/; |
8 |
|
use Exporter 'import'; |
9 |
|
our @EXPORT = qw'@mem $PC $A $P $X $Y $S $IPeriod $run_for $debug'; |
10 |
|
|
11 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 07/30/07 13:23:19 CEST |
=head1 NAME |
|
# |
|
|
# Simple Orao emulation |
|
12 |
|
|
13 |
my $debug = 1; |
M6502 - perl bindings for M6502 CPU emulator |
14 |
|
|
15 |
my @mem = (0x42) x 0x10000; # 64M |
=cut |
16 |
|
|
17 |
|
my $debug = 0; |
18 |
|
|
19 |
|
our $VERSION = qw(0.0.1); |
20 |
|
|
21 |
|
our @mem = (0xff) x 0x10000; # 64M |
22 |
|
|
23 |
# program counter |
# program counter |
24 |
our $PC = 0xbeef; |
our $PC = 0xbeef; |
26 |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
our ( $A, $P, $X, $Y, $S ) = (0) x 5; |
27 |
# Set IPeriod to number of CPU cycles between calls to Loop6502 |
# Set IPeriod to number of CPU cycles between calls to Loop6502 |
28 |
our $IPeriod = 1; |
our $IPeriod = 1; |
29 |
|
# Exec6502 cycles |
30 |
|
our $run_for = 0; |
31 |
|
|
32 |
=head1 init |
=head1 FUNCTIONS |
33 |
|
|
34 |
|
=head2 init |
35 |
|
|
36 |
Called before C<Run6502> |
Called before C<Run6502> |
37 |
|
|
38 |
=cut |
=cut |
39 |
|
|
40 |
sub init { |
sub init { |
41 |
warn "inside init\n"; |
my $self = shift; |
42 |
print "stdout\n"; |
warn "inside init low-level M6502 from $self\n"; |
43 |
}; |
}; |
44 |
|
|
45 |
=head2 read |
=head2 read |
53 |
sub read { |
sub read { |
54 |
my ($addr) = @_; |
my ($addr) = @_; |
55 |
my $byte = $mem[$addr]; |
my $byte = $mem[$addr]; |
56 |
warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
warn "## M6502::read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; |
57 |
return $byte; |
return $byte; |
58 |
} |
} |
59 |
|
|
66 |
=cut |
=cut |
67 |
|
|
68 |
sub write { |
sub write { |
69 |
warn "# write(",dump(@_),")\n" if $debug; |
warn "## M6502::write(",dump(@_),")\n" if $debug; |
70 |
my ($addr,$byte) = @_; |
my ($addr,$byte) = @_; |
71 |
$mem[$addr] = $byte; |
$mem[$addr] = $byte; |
72 |
} |
} |
73 |
|
|
74 |
|
=head2 poke_code |
75 |
|
|
76 |
|
Write series of bytes into memory passing through MMU (C<read> and C<write>) |
77 |
|
functions. If you don't want to trigger MMU, use C<write_chunk>. |
78 |
|
|
79 |
|
$emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa ); |
80 |
|
|
81 |
|
=cut |
82 |
|
|
83 |
|
sub poke_code { |
84 |
|
my $self = shift; |
85 |
|
my $addr = shift; |
86 |
|
warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; |
87 |
|
#$mem[$addr++] = $_ foreach @_; |
88 |
|
# call low-level write |
89 |
|
Arch::write($addr++, $_) foreach @_; |
90 |
|
} |
91 |
|
|
92 |
|
=head2 ram |
93 |
|
|
94 |
|
Read series of bytes into memory without MMU interaction |
95 |
|
|
96 |
|
my @code = $emu->ram( 0xc000, 0xc1000 ); |
97 |
|
|
98 |
|
=cut |
99 |
|
|
100 |
|
sub ram { |
101 |
|
my $self = shift; |
102 |
|
my ( $from, $to ) = @_; |
103 |
|
warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug; |
104 |
|
return @mem[ $from .. $to ]; |
105 |
|
} |
106 |
|
|
107 |
|
=head2 write_chunk |
108 |
|
|
109 |
|
Low-level update of memory, overriding user specified MMU functions C<read> and C<write> |
110 |
|
|
111 |
|
$emu->write_chunk( $address, $chunk_of_data ); |
112 |
|
|
113 |
|
=cut |
114 |
|
|
115 |
|
sub write_chunk { |
116 |
|
my ($self, $addr, $chunk) = @_; |
117 |
|
my $len = length($chunk); |
118 |
|
splice @mem, $addr, $len, unpack('C*', $chunk); |
119 |
|
} |
120 |
|
|
121 |
|
=head2 prompt |
122 |
|
|
123 |
|
Call this after C<< $run_for >> cycles have been run on processor |
124 |
|
|
125 |
|
=cut |
126 |
|
|
127 |
|
sub prompt { |
128 |
|
warn "prompt -- you should override this\n"; |
129 |
|
return 1; |
130 |
|
} |
131 |
|
|
132 |
|
=head2 push_R |
133 |
|
|
134 |
|
called by C<perl.c> to push changes in registars back to perl variables |
135 |
|
|
136 |
|
=cut |
137 |
|
|
138 |
|
sub push_R { |
139 |
|
warn "## M6502::push_R(",dump(@_),")\n" if $debug; |
140 |
|
my ( $a, $p, $x, $y, $s, $pc ) = @_; |
141 |
|
$PC = $pc; |
142 |
|
$S=$s; $X=$x; $Y=$y; $P=$p; $A=$a; |
143 |
|
dump_R(); |
144 |
|
} |
145 |
|
|
146 |
|
=head2 dump_R |
147 |
|
|
148 |
|
helper function which dumps registers in humanly readable form |
149 |
|
|
150 |
|
my $dump = dump_R; |
151 |
|
|
152 |
|
=cut |
153 |
|
|
154 |
|
sub dump_R { |
155 |
|
my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S); |
156 |
|
warn "## M6502::dump_R $dump" if $debug; |
157 |
|
return $dump; |
158 |
|
} |
159 |
|
|
160 |
|
=head1 SEE ALSO |
161 |
|
|
162 |
|
L<Orao> is sample implementation using this module |
163 |
|
|
164 |
|
=head1 AUTHOR |
165 |
|
|
166 |
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
167 |
|
|
168 |
|
=head1 COPYRIGHT & LICENSE |
169 |
|
|
170 |
|
Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. |
171 |
|
|
172 |
|
This program is free software; you can redistribute it and/or modify it |
173 |
|
under the same terms as Perl itself. |
174 |
|
|
175 |
|
=cut |
176 |
1; |
1; |