/[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

Diff of /M6502/M6502.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 90 by dpavlin, Thu Aug 2 12:23:18 2007 UTC revision 206 by dpavlin, Mon Apr 14 17:44:48 2008 UTC
# Line 6  use warnings; Line 6  use warnings;
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.3';
11  require XSLoader;  require XSLoader;
12  XSLoader::load('M6502', $VERSION);  XSLoader::load('M6502', $VERSION);
13    
# Line 21  M6502 - perl bindings for M6502 CPU emul Line 21  M6502 - perl bindings for M6502 CPU emul
21    
22  our $debug = 0;  our $debug = 0;
23    
24  our @mem = (0xff) x 0x10000;    # 64M  our @mem;
25    #@mem = (0xff) x 0x10000;       # 64M
26    tie @mem, 'M6502::TieMem';
27    
28  # program counter  # program counter
29  our $PC = 0xbeef;  our $PC = 0xbeef;
# Line 36  our $TrapBadOps=1;     # Set to 1 to warn of Line 38  our $TrapBadOps=1;     # Set to 1 to warn of
38  our $Trap;                      # Set Trap to address to trace from  our $Trap;                      # Set Trap to address to trace from
39  our $Trace;                     # Set Trace=1 to start tracing  our $Trace;                     # Set Trace=1 to start tracing
40    
 # Exec6502 cycles  
 our $run_for = 0;  
   
41  =head2 init  =head2 init
42    
43  Setup read and write memory hooks (to implement memory mapped devices)  Setup read and write memory hooks (to implement memory mapped devices)
# Line 56  Setup read and write memory hooks (to im Line 55  Setup read and write memory hooks (to im
55    
56  our $_rw_hooks = {  our $_rw_hooks = {
57          read => sub {          read => sub {
58                    warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug;
59                  return $mem[$_[0]];                  return $mem[$_[0]];
60          },          },
61          write => sub {          write => sub {
62                    warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug;
63                  $mem[$_[0]] = $_[1];                  $mem[$_[0]] = $_[1];
64          },          },
65  };  };
# Line 88  sub poke_code { Line 89  sub poke_code {
89          my $self = shift;          my $self = shift;
90          my $addr = shift;          my $addr = shift;
91          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;
         #$mem[$addr++] = $_ foreach @_;  
92          # call low-level write          # call low-level write
93          $_rw_hooks->{write}->( $addr++, $_ ) foreach @_;          #$_rw_hooks->{write}->( $addr++, $_ ) foreach @_;
94            $mem[$addr++] = $_ foreach @_;
95  }  }
96    
97  =head2 ram  =head2 ram
# Line 122  sub write_chunk { Line 123  sub write_chunk {
123          splice @mem, $addr, $len, unpack('C*', $chunk);          splice @mem, $addr, $len, unpack('C*', $chunk);
124  }  }
125    
 =head2 prompt  
   
 Call this after C<< $run_for >> cycles have been run on processor  
   
 =cut  
   
 sub prompt {  
         warn "prompt -- you should override this\n";  
         return 1;  
 }  
   
126  =head1 XS Callbacks  =head1 XS Callbacks
127    
128  This functions are called from C<M6502.xs>  This functions are called from C<M6502.xs>
# Line 173  sub _update_perl_R { Line 163  sub _update_perl_R {
163          dump_R();          dump_R();
164  }  }
165    
166    =head1 XS
167    
168    Following functions are implemented in C<M6502.xs> and exported to perl.
169    
170    =head2 set_debug
171    
172      M6502::set_debug( 0 );
173    
174    =head2 get_debug
175    
176      my $debug = M6502::set_debug();
177    
178    =head2 reset
179    
180    Reset 6502 CPU, reading PC from C<0xfffc>
181    
182      M6502::reset();
183    
184    =head2 update_C_R
185    
186    Push perl notion of register values to CPU emulator
187    
188      M6502::update_C_R();
189    
190    =head2 update_perl_R
191    
192    Update perl notion of register values
193    
194      M6502::update_perl_R();
195    
196    =head2 exec
197    
198    Execute cpu for specified number of cycles
199    
200      my $cycles_left = M6502::exec( $execute_cpu_cycles );
201    
202    =head1 Helpers
203    
204  =head2 dump_R  =head2 dump_R
205    
206  helper function which dumps registers in humanly readable form  helper function which dumps registers in humanly readable form
# Line 222  Dobrica Pavlinusic, C<< <dpavlin@rot13.o Line 250  Dobrica Pavlinusic, C<< <dpavlin@rot13.o
250    
251  =head1 COPYRIGHT & LICENSE  =head1 COPYRIGHT & LICENSE
252    
253  Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.  Copyright 2007-8 Dobrica Pavlinusic, All Rights Reserved.
254    
255  This program is free software; you can redistribute it and/or modify it  This program is free software; you can redistribute it and/or modify it
256  under the same terms as Perl itself.  under the same terms as Perl itself.
257    
258  =cut  =cut
259    
260    package M6502::TieMem;
261    
262    use strict;
263    use warnings;
264    use Tie::Array;
265    use base qw(Tie::Array);
266    
267    #
268    sub TIEARRAY {
269            my $class = shift;
270    #       my $opt   = shift;
271    #       my $self  = { %$opt };
272            my $self = {};
273            bless($self, __PACKAGE__);
274            return $self;
275    }
276    
277    sub DESTROY {}
278    
279    #
280    sub FETCH {
281            my $self = shift;
282            my $n    = shift;
283            my $val = M6502::mem_peek( $n );
284    #       warn sprintf("FETCH %04x = %02x\n", $n, $val);
285            return $val;
286    }
287    
288    #
289    sub FETCHSIZE {
290            return 0xffff;
291    }
292    
293    #
294    sub STORE {
295            my $self = shift;
296            my $n    = shift;
297            my $val  = shift;
298            if ( $n > 0xffff ) {
299                    warn "over 64k: $n\n";
300                    return;
301            }
302            M6502::mem_poke( $n, $val );
303    #       warn sprintf("STORE %04x <- %02x\n",$n, $val);
304            return $val;
305    }
306    
307    #
308    sub STORESIZE {
309            die('not allowed (yet)');
310    }
311    
312    sub PUSH {
313            die('not allowed (yet)');
314    }
315    
316    sub POP {
317            die('not allowed (yet)');
318    }
319    
320    sub SHIFT {
321            die('not allowed (yet)');
322    }
323    
324    sub UNSHIFT {
325            die('not allowed (yet)');
326    }
327    
328    sub DELETE {
329            my $self = shift;
330            my $n    = shift;
331            $self->STORE($n, 0);
332    }
333    
334    sub EXISTS {
335            my $self = shift;
336            my $n    = shift;
337            return 0 if $n > 0xffff;
338            return 1;
339    }
340    
341  1;  1;

Legend:
Removed from v.90  
changed lines
  Added in v.206

  ViewVC Help
Powered by ViewVC 1.1.26