/[VRac]/M6502/M6502.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /M6502/M6502.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 96 - (hide annotations)
Thu Aug 2 13:58:26 2007 UTC (16 years, 8 months ago) by dpavlin
File size: 4970 byte(s)
added SDL event loop around CPU emulation
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 96 our @EXPORT = qw'dump_R @mem $PC $A $P $X $Y $S $IPeriod $ICount $IRequest $IAutoReset $TrapBadOps $Trap $Trace $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 54 =head2 init
40    
41 dpavlin 89 Setup read and write memory hooks (to implement memory mapped devices)
42 dpavlin 26
43 dpavlin 89 $init->(
44     read => sub {
45     return $mem[$_[0]];
46     },
47     write => sub {
48     $mem[$_[0]] = $_[1];
49     },
50     );
51    
52 dpavlin 26 =cut
53    
54 dpavlin 89 our $_rw_hooks = {
55     read => sub {
56 dpavlin 94 warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug;
57 dpavlin 89 return $mem[$_[0]];
58     },
59     write => sub {
60 dpavlin 92 warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug;
61 dpavlin 89 $mem[$_[0]] = $_[1];
62     },
63     };
64    
65 dpavlin 26 sub init {
66 dpavlin 33 my $self = shift;
67 dpavlin 89 my $args = {@_};
68     warn "inside init low-level M6502 from ",ref($self),"\n";
69 dpavlin 27
70 dpavlin 89 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 dpavlin 27
75 dpavlin 89 };
76 dpavlin 27
77 dpavlin 29 =head2 poke_code
78    
79 dpavlin 42 Write series of bytes into memory passing through MMU (C<read> and C<write>)
80     functions. If you don't want to trigger MMU, use C<write_chunk>.
81 dpavlin 29
82     $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );
83    
84     =cut
85    
86     sub poke_code {
87     my $self = shift;
88     my $addr = shift;
89 dpavlin 50 warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;
90 dpavlin 42 #$mem[$addr++] = $_ foreach @_;
91 dpavlin 51 # call low-level write
92 dpavlin 90 $_rw_hooks->{write}->( $addr++, $_ ) foreach @_;
93 dpavlin 29 }
94    
95 dpavlin 51 =head2 ram
96    
97     Read series of bytes into memory without MMU interaction
98    
99     my @code = $emu->ram( 0xc000, 0xc1000 );
100    
101     =cut
102    
103     sub ram {
104     my $self = shift;
105     my ( $from, $to ) = @_;
106     warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug;
107     return @mem[ $from .. $to ];
108     }
109    
110 dpavlin 31 =head2 write_chunk
111    
112 dpavlin 42 Low-level update of memory, overriding user specified MMU functions C<read> and C<write>
113    
114 dpavlin 31 $emu->write_chunk( $address, $chunk_of_data );
115    
116     =cut
117    
118     sub write_chunk {
119     my ($self, $addr, $chunk) = @_;
120     my $len = length($chunk);
121     splice @mem, $addr, $len, unpack('C*', $chunk);
122     }
123    
124 dpavlin 89 =head1 XS Callbacks
125    
126     This functions are called from C<M6502.xs>
127    
128     =head2 _read
129    
130     Read from memory C callback
131    
132     $byte = M6502::_read( $address );
133    
134     =cut
135    
136     sub _read {
137     return $_rw_hooks->{read}->( @_ );
138     }
139    
140     =head2 _write
141    
142     Write into memory C callback
143    
144     M6502:_write( $address, $byte );
145    
146     =cut
147    
148     sub _write {
149     return $_rw_hooks->{write}->( @_ );
150     }
151    
152 dpavlin 87 =head2 _update_perl_R
153 dpavlin 38
154 dpavlin 87 called by C<M6502.xs> to push changes in registars back to perl variables
155 dpavlin 38
156     =cut
157    
158 dpavlin 87 sub _update_perl_R {
159     warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug;
160 dpavlin 74 ( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_;
161 dpavlin 38 dump_R();
162     }
163    
164 dpavlin 94 =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 dpavlin 38 =head2 dump_R
203    
204 dpavlin 68 helper function which dumps registers in humanly readable form
205 dpavlin 38
206 dpavlin 68 my $dump = dump_R;
207    
208 dpavlin 38 =cut
209    
210     sub dump_R {
211 dpavlin 74 my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x "
212     . "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d"
213     . "\n",
214     $PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace,
215     );
216 dpavlin 68 warn "## M6502::dump_R $dump" if $debug;
217     return $dump;
218 dpavlin 38 }
219    
220 dpavlin 87 =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 dpavlin 55 =head1 SEE ALSO
242 dpavlin 54
243 dpavlin 55 L<Orao> is sample implementation using this module
244    
245 dpavlin 54 =head1 AUTHOR
246    
247     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
248    
249     =head1 COPYRIGHT & LICENSE
250    
251     Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
252    
253     This program is free software; you can redistribute it and/or modify it
254     under the same terms as Perl itself.
255    
256     =cut
257 dpavlin 24 1;

  ViewVC Help
Powered by ViewVC 1.1.26