/[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 31 by dpavlin, Mon Jul 30 18:07:29 2007 UTC revision 208 by dpavlin, Mon Apr 14 19:40:02 2008 UTC
# Line 5  use warnings; Line 5  use warnings;
5    
6  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
7  use Carp qw/confess/;  use Carp qw/confess/;
8    use Exporter 'import';
9    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.4';
11    require XSLoader;
12    XSLoader::load('M6502', $VERSION);
13    
14  =head1 NAME  =head1 NAME
15    
16  M6502 - perl bindings for 6502 emulator  M6502 - perl bindings for M6502 CPU emulator
17    
18  =cut  =head1 FUNCTIONS
19    
20  my $debug = 1;  =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;  
32    
33  =head1 init  our $IPeriod=1;         # Set IPeriod to number of CPU cycles between calls to Loop6502
34    our $ICount;
35  Called before C<Run6502>  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
42    
43    Setup read and write memory hooks (to implement memory mapped devices)
44    
45      $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 init {  our $_rw_hooks = {
57          warn "inside init\n";          read => sub {
58          print "stdout\n";                  warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug;
59                    return $mem[$_[0]];
60            },
61            write => sub {
62                    warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug;
63                    $mem[$_[0]] = $_[1];
64            },
65  };  };
66    
67  =head2 read  sub init {
68            my $self = shift;
69  Read from memory          my $args = {@_};
70            warn "inside init low-level M6502 from ",ref($self),"\n";
   $byte = read( $address );  
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 read {  };
         my ($addr) = @_;  
         my $byte = $mem[$addr];  
         warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug;  
         return $byte;  
 }  
78    
79  =head2 write  =head2 poke_code
80    
81  Write into emory  Write series of bytes into memory passing through MMU (C<read> and C<write>)
82    functions. If you don't want to trigger MMU, use C<write_chunk>.
83    
84    write( $address, $byte );    $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );
85    
86  =cut  =cut
87    
88  sub write {  sub poke_code {
89          warn "# write(",dump(@_),")\n" if $debug;          my $self = shift;
90          my ($addr,$byte) = @_;          my $addr = shift;
91          $mem[$addr] = $byte;          warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;
92            # call low-level write
93            #$_rw_hooks->{write}->( $addr++, $_ ) foreach @_;
94            $mem[$addr++] = $_ foreach @_;
95  }  }
96    
97  =head2 poke_code  =head2 ram
98    
99  Write series of bytes into memory without passing through MMU  Read series of bytes into memory without MMU interaction
100    
101    $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );    my @code = $emu->ram( 0xc000, 0xc1000 );
102    
103  =cut  =cut
104    
105  sub poke_code {  sub ram {
106          my $self = shift;          my $self = shift;
107          my $addr = shift;          my ( $from, $to ) = @_;
108          warn sprintf("# poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;          warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug;
109          $mem[$addr++] = $_ foreach @_;  #       return @mem[ $from .. $to ];
110            return unpack('C*', M6502::mem_peek_region( $from, $to ));
111  }  }
112    
113  =head2 write_chunk  =head2 write_chunk
114    
115    Low-level update of memory, overriding user specified MMU functions C<read> and C<write>
116    
117    $emu->write_chunk( $address, $chunk_of_data );    $emu->write_chunk( $address, $chunk_of_data );
118    
119  =cut  =cut
# Line 92  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 ram  =head1 XS Callbacks
128    
129    This functions are called from C<M6502.xs>
130    
131    =head2 _read
132    
133  Read searies of bytes from memory without passing through MMU  Read from memory C callback
134    
135    $emu->ram( $from, $to );    $byte = M6502::_read( $address );
136    
137  =cut  =cut
138    
139  sub ram {  sub _read {
140            return $_rw_hooks->{read}->( @_ );
141    }
142    
143    =head2 _write
144    
145    Write into memory C callback
146    
147      M6502:_write( $address, $byte );
148    
149    =cut
150    
151    sub _write {
152            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 ) = @_;
164            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
206    
207    helper function which dumps registers in humanly readable form
208    
209      my $dump = dump_R;
210    
211    =cut
212    
213    sub dump_R {
214            my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x "
215                    . "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d"
216                    . "\n",
217                    $PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace,
218            );
219            warn "## M6502::dump_R $dump" if $debug;
220            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;          my $self = shift;
235          my ($from,$to) = @_;          my $value = shift;
236          if ($from + $to) {          if (defined($value)) {
237                  printf "ram %04x - %04x\n", $from, $to;                  $debug = M6502::set_debug($value);
238                  return $mem[$from .. $to - 1];          } else {
239                    $debug = M6502::get_debug();
240          }          }
241          printf "ram %04x\n", $from;          return $debug;
242          return $mem[$from] if defined($from);  }
243          confess "no from address";  
244    =head1 SEE ALSO
245    
246    L<Orao> is sample implementation using this module
247    
248    =head1 AUTHOR
249    
250    Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
251    
252    =head1 COPYRIGHT & LICENSE
253    
254    Copyright 2007-8 Dobrica Pavlinusic, All Rights Reserved.
255    
256    This program is free software; you can redistribute it and/or modify it
257    under the same terms as Perl itself.
258    
259    =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.31  
changed lines
  Added in v.208

  ViewVC Help
Powered by ViewVC 1.1.26