/[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 80 by dpavlin, Wed Aug 1 15:34:43 2007 UTC revision 208 by dpavlin, Mon Apr 14 19:40:02 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'@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.4';
11  require XSLoader;  require XSLoader;
12  XSLoader::load('M6502', $VERSION);  XSLoader::load('M6502', $VERSION);
13    
# Line 15  XSLoader::load('M6502', $VERSION); Line 15  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 = 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 34  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;  
   
 =head1 FUNCTIONS  
   
41  =head2 init  =head2 init
42    
43  Called before C<Run6502>  Setup read and write memory hooks (to implement memory mapped devices)
   
 =cut  
   
 sub init {  
         my $self = shift;  
         warn "inside init low-level M6502 from $self\n";  
 };  
   
 =head2 read  
44    
45  Read from memory    $init->(
46            read => sub {
47    $byte = read( $address );                  return $mem[$_[0]];
48            },
49            write => sub {
50                    $mem[$_[0]] = $_[1];
51            },
52      );
53    
54  =cut  =cut
55    
56  sub read {  our $_rw_hooks = {
57          my ($addr) = @_;          read => sub {
58          my $byte = $mem[$addr];                  warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug;
59          warn "## M6502::read(",dump(@_),") = ",dump( $byte ),"\n" if $debug;                  return $mem[$_[0]];
60          return $byte;          },
61  }          write => sub {
62                    warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug;
63  =head2 write                  $mem[$_[0]] = $_[1];
64            },
65  Write into emory  };
66    
67    write( $address, $byte );  sub init {
68            my $self = shift;
69            my $args = {@_};
70            warn "inside init low-level M6502 from ",ref($self),"\n";
71    
72  =cut          foreach my $p ( qw/read write/ ) {
73                    confess "need $p argument as coderef" unless ( $args->{$p} && ref($args->{$p}) eq 'CODE' );
74                    $_rw_hooks->{$p} = $args->{$p};
75            }
76    
77  sub write {  };
         warn "## M6502::write(",dump(@_),")\n" if $debug;  
         my ($addr,$byte) = @_;  
         $mem[$addr] = $byte;  
 }  
78    
79  =head2 poke_code  =head2 poke_code
80    
# Line 92  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          Arch::write($addr++, $_) foreach @_;          #$_rw_hooks->{write}->( $addr++, $_ ) foreach @_;
94            $mem[$addr++] = $_ foreach @_;
95  }  }
96    
97  =head2 ram  =head2 ram
# Line 109  sub ram { Line 106  sub ram {
106          my $self = shift;          my $self = shift;
107          my ( $from, $to ) = @_;          my ( $from, $to ) = @_;
108          warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug;          warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug;
109          return @mem[ $from .. $to ];  #       return @mem[ $from .. $to ];
110            return unpack('C*', M6502::mem_peek_region( $from, $to ));
111  }  }
112    
113  =head2 write_chunk  =head2 write_chunk
# Line 126  sub write_chunk { Line 124  sub write_chunk {
124          splice @mem, $addr, $len, unpack('C*', $chunk);          splice @mem, $addr, $len, unpack('C*', $chunk);
125  }  }
126    
127  =head2 prompt  =head1 XS Callbacks
128    
129    This functions are called from C<M6502.xs>
130    
131  Call this after C<< $run_for >> cycles have been run on processor  =head2 _read
132    
133    Read from memory C callback
134    
135      $byte = M6502::_read( $address );
136    
137  =cut  =cut
138    
139  sub prompt {  sub _read {
140          warn "prompt -- you should override this\n";          return $_rw_hooks->{read}->( @_ );
         return 1;  
141  }  }
142    
143  =head2 push_R  =head2 _write
144    
145  called by C<perl.c> to push changes in registars back to perl variables  Write into memory C callback
146    
147      M6502:_write( $address, $byte );
148    
149  =cut  =cut
150    
151  sub push_R {  sub _write {
152          warn "## M6502::push_R(",dump(@_),")\n" if $debug;          return $_rw_hooks->{write}->( @_ );
153    }
154    
155    =head2 _update_perl_R
156    
157    called by C<M6502.xs> to push changes in registars back to perl variables
158    
159    =cut
160    
161    sub _update_perl_R {
162            warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug;
163          ( $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 ) = @_;
164          dump_R();          dump_R();
165  }  }
166    
167    =head1 XS
168    
169    Following functions are implemented in C<M6502.xs> and exported to perl.
170    
171    =head2 set_debug
172    
173      M6502::set_debug( 0 );
174    
175    =head2 get_debug
176    
177      my $debug = M6502::set_debug();
178    
179    =head2 reset
180    
181    Reset 6502 CPU, reading PC from C<0xfffc>
182    
183      M6502::reset();
184    
185    =head2 update_C_R
186    
187    Push perl notion of register values to CPU emulator
188    
189      M6502::update_C_R();
190    
191    =head2 update_perl_R
192    
193    Update perl notion of register values
194    
195      M6502::update_perl_R();
196    
197    =head2 exec
198    
199    Execute cpu for specified number of cycles
200    
201      my $cycles_left = M6502::exec( $execute_cpu_cycles );
202    
203    =head1 Helpers
204    
205  =head2 dump_R  =head2 dump_R
206    
207  helper function which dumps registers in humanly readable form  helper function which dumps registers in humanly readable form
# Line 167  sub dump_R { Line 220  sub dump_R {
220          return $dump;          return $dump;
221  }  }
222    
223    =head2 debug
224    
225    Turn perl and C-level debugging on/off
226    
227      $emu->debug( 0 );
228      $emu->debug( 1 );
229      print $emu->debug;
230    
231    =cut
232    
233    sub debug {
234            my $self = shift;
235            my $value = shift;
236            if (defined($value)) {
237                    $debug = M6502::set_debug($value);
238            } else {
239                    $debug = M6502::get_debug();
240            }
241            return $debug;
242    }
243    
244  =head1 SEE ALSO  =head1 SEE ALSO
245    
246  L<Orao> is sample implementation using this module  L<Orao> is sample implementation using this module
# Line 177  Dobrica Pavlinusic, C<< <dpavlin@rot13.o Line 251  Dobrica Pavlinusic, C<< <dpavlin@rot13.o
251    
252  =head1 COPYRIGHT & LICENSE  =head1 COPYRIGHT & LICENSE
253    
254  Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.  Copyright 2007-8 Dobrica Pavlinusic, All Rights Reserved.
255    
256  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
257  under the same terms as Perl itself.  under the same terms as Perl itself.
258    
259  =cut  =cut
260    
261    package M6502::TieMem;
262    
263    use strict;
264    use warnings;
265    use Tie::Array;
266    use base qw(Tie::Array);
267    
268    #
269    sub TIEARRAY {
270            my $class = shift;
271    #       my $opt   = shift;
272    #       my $self  = { %$opt };
273            my $self = {};
274            bless($self, __PACKAGE__);
275            return $self;
276    }
277    
278    sub DESTROY {}
279    
280    #
281    sub FETCH {
282            my $self = shift;
283            my $n    = shift;
284            my $val = M6502::mem_peek( $n );
285    #       warn sprintf("FETCH %04x = %02x\n", $n, $val);
286            return $val;
287    }
288    
289    #
290    sub FETCHSIZE {
291            return 0xffff;
292    }
293    
294    #
295    sub STORE {
296            my $self = shift;
297            my $n    = shift;
298            my $val  = shift;
299            if ( $n > 0xffff ) {
300                    warn "over 64k: $n\n";
301                    return;
302            }
303            M6502::mem_poke( $n, $val );
304    #       warn sprintf("STORE %04x <- %02x\n",$n, $val);
305            return $val;
306    }
307    
308    #
309    sub STORESIZE {
310            die('not allowed (yet)');
311    }
312    
313    sub PUSH {
314            die('not allowed (yet)');
315    }
316    
317    sub POP {
318            die('not allowed (yet)');
319    }
320    
321    sub SHIFT {
322            die('not allowed (yet)');
323    }
324    
325    sub UNSHIFT {
326            die('not allowed (yet)');
327    }
328    
329    sub DELETE {
330            my $self = shift;
331            my $n    = shift;
332            $self->STORE($n, 0);
333    }
334    
335    sub EXISTS {
336            my $self = shift;
337            my $n    = shift;
338            return 0 if $n > 0xffff;
339            return 1;
340    }
341    
342  1;  1;

Legend:
Removed from v.80  
changed lines
  Added in v.208

  ViewVC Help
Powered by ViewVC 1.1.26