/[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 68 by dpavlin, Tue Jul 31 17:15:54 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'@mem $PC $A $P $X $Y $S $IPeriod $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.3';
11    require XSLoader;
12    XSLoader::load('M6502', $VERSION);
13    
14  =head1 NAME  =head1 NAME
15    
16  M6502 - perl bindings for M6502 CPU emulator  M6502 - perl bindings for M6502 CPU emulator
17    
18  =cut  =head1 FUNCTIONS
19    
20  my $debug = 0;  =cut
21    
22  our $VERSION = qw(0.0.1);  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;
30  # CPU registars  # CPU registars
31  our ( $A, $P, $X, $Y, $S ) = (0) x 5;  our ( $A, $P, $X, $Y, $S ) = (0) x 5;
 # Set IPeriod to number of CPU cycles between calls to Loop6502  
 our $IPeriod = 1;  
 # Exec6502 cycles  
 our $run_for = 0;  
32    
33  =head1 FUNCTIONS  our $IPeriod=1;         # Set IPeriod to number of CPU cycles between calls to Loop6502
34    our $ICount;
35    our $IRequest;          # Set to the INT_IRQ when pending IRQ
36    our $IAutoReset;        # Set to 1 to autom. reset IRequest
37    our $TrapBadOps=1;      # Set to 1 to warn of illegal opcodes
38    our $Trap;                      # Set Trap to address to trace from
39    our $Trace;                     # Set Trace=1 to start tracing
40    
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  
   
 Read from memory  
44    
45    $byte = read( $address );    $init->(
46            read => sub {
47                    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    };
66    
67  Write into emory  sub init {
68            my $self = shift;
69            my $args = {@_};
70            warn "inside init low-level M6502 from ",ref($self),"\n";
71    
72    write( $address, $byte );          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  =cut  };
   
 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 84  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 118  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    
126  =head2 prompt  =head1 XS Callbacks
127    
128    This functions are called from C<M6502.xs>
129    
130    =head2 _read
131    
132  Call this after C<< $run_for >> cycles have been run on processor  Read from memory C callback
133    
134      $byte = M6502::_read( $address );
135    
136  =cut  =cut
137    
138  sub prompt {  sub _read {
139          warn "prompt -- you should override this\n";          return $_rw_hooks->{read}->( @_ );
         return 1;  
140  }  }
141    
142  =head2 push_R  =head2 _write
143    
144    Write into memory C callback
145    
146  called by C<perl.c> to push changes in registars back to perl variables    M6502:_write( $address, $byte );
147    
148  =cut  =cut
149    
150  sub push_R {  sub _write {
151          warn "## M6502::push_R(",dump(@_),")\n" if $debug;          return $_rw_hooks->{write}->( @_ );
152          my ( $a, $p, $x, $y, $s, $pc ) = @_;  }
153          $PC = $pc;  
154          $S=$s; $X=$x; $Y=$y; $P=$p; $A=$a;  =head2 _update_perl_R
155    
156    called by C<M6502.xs> to push changes in registars back to perl variables
157    
158    =cut
159    
160    sub _update_perl_R {
161            warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug;
162            ( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_;
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 152  helper function which dumps registers in Line 210  helper function which dumps registers in
210  =cut  =cut
211    
212  sub dump_R {  sub dump_R {
213          my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S);          my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x "
214                    . "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d"
215                    . "\n",
216                    $PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace,
217            );
218          warn "## M6502::dump_R $dump" if $debug;          warn "## M6502::dump_R $dump" if $debug;
219          return $dump;          return $dump;
220  }  }
221    
222    =head2 debug
223    
224    Turn perl and C-level debugging on/off
225    
226      $emu->debug( 0 );
227      $emu->debug( 1 );
228      print $emu->debug;
229    
230    =cut
231    
232    sub debug {
233            my $self = shift;
234            my $value = shift;
235            if (defined($value)) {
236                    $debug = M6502::set_debug($value);
237            } else {
238                    $debug = M6502::get_debug();
239            }
240            return $debug;
241    }
242    
243  =head1 SEE ALSO  =head1 SEE ALSO
244    
245  L<Orao> is sample implementation using this module  L<Orao> is sample implementation using this module
# Line 167  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.68  
changed lines
  Added in v.206

  ViewVC Help
Powered by ViewVC 1.1.26