/[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 203 by dpavlin, Sun Apr 13 22:04:44 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.3';
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; # = (0xff) x 0x10000; # 64M
25    tie @mem, 'M6502::TieMem';
26    
27  # program counter  # program counter
28  our $PC = 0xbeef;  our $PC = 0xbeef;
29  # CPU registars  # CPU registars
30  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;  
31    
32  =head1 init  our $IPeriod=1;         # Set IPeriod to number of CPU cycles between calls to Loop6502
33    our $ICount;
34  Called before C<Run6502>  our $IRequest;          # Set to the INT_IRQ when pending IRQ
35    our $IAutoReset;        # Set to 1 to autom. reset IRequest
36    our $TrapBadOps=1;      # Set to 1 to warn of illegal opcodes
37    our $Trap;                      # Set Trap to address to trace from
38    our $Trace;                     # Set Trace=1 to start tracing
39    
40    =head2 init
41    
42    Setup read and write memory hooks (to implement memory mapped devices)
43    
44      $init->(
45            read => sub {
46                    return $mem[$_[0]];
47            },
48            write => sub {
49                    $mem[$_[0]] = $_[1];
50            },
51      );
52    
53  =cut  =cut
54    
55  sub init {  our $_rw_hooks = {
56          warn "inside init\n";          read => sub {
57          print "stdout\n";                  warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug;
58                    return $mem[$_[0]];
59            },
60            write => sub {
61                    warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug;
62                    $mem[$_[0]] = $_[1];
63            },
64  };  };
65    
66  =head2 read  sub init {
67            my $self = shift;
68  Read from memory          my $args = {@_};
69            warn "inside init low-level M6502 from ",ref($self),"\n";
   $byte = read( $address );  
70    
71  =cut          foreach my $p ( qw/read write/ ) {
72                    confess "need $p argument as coderef" unless ( $args->{$p} && ref($args->{$p}) eq 'CODE' );
73                    $_rw_hooks->{$p} = $args->{$p};
74            }
75    
76  sub read {  };
         my ($addr) = @_;  
         my $byte = $mem[$addr];  
         warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug;  
         return $byte;  
 }  
77    
78  =head2 write  =head2 poke_code
79    
80  Write into emory  Write series of bytes into memory passing through MMU (C<read> and C<write>)
81    functions. If you don't want to trigger MMU, use C<write_chunk>.
82    
83    write( $address, $byte );    $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );
84    
85  =cut  =cut
86    
87  sub write {  sub poke_code {
88          warn "# write(",dump(@_),")\n" if $debug;          my $self = shift;
89          my ($addr,$byte) = @_;          my $addr = shift;
90          $mem[$addr] = $byte;          warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;
91            #$mem[$addr++] = $_ foreach @_;
92            # call low-level write
93            $_rw_hooks->{write}->( $addr++, $_ ) foreach @_;
94  }  }
95    
96  =head2 poke_code  =head2 ram
97    
98  Write series of bytes into memory without passing through MMU  Read series of bytes into memory without MMU interaction
99    
100    $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );    my @code = $emu->ram( 0xc000, 0xc1000 );
101    
102  =cut  =cut
103    
104  sub poke_code {  sub ram {
105          my $self = shift;          my $self = shift;
106          my $addr = shift;          my ( $from, $to ) = @_;
107          warn sprintf("# poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;          warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug;
108          $mem[$addr++] = $_ foreach @_;          return @mem[ $from .. $to ];
109  }  }
110    
111  =head2 write_chunk  =head2 write_chunk
112    
113    Low-level update of memory, overriding user specified MMU functions C<read> and C<write>
114    
115    $emu->write_chunk( $address, $chunk_of_data );    $emu->write_chunk( $address, $chunk_of_data );
116    
117  =cut  =cut
# Line 92  sub write_chunk { Line 122  sub write_chunk {
122          splice @mem, $addr, $len, unpack('C*', $chunk);          splice @mem, $addr, $len, unpack('C*', $chunk);
123  }  }
124    
125  =head2 ram  =head1 XS Callbacks
126    
127  Read searies of bytes from memory without passing through MMU  This functions are called from C<M6502.xs>
128    
129    $emu->ram( $from, $to );  =head2 _read
130    
131    Read from memory C callback
132    
133      $byte = M6502::_read( $address );
134    
135  =cut  =cut
136    
137  sub ram {  sub _read {
138            return $_rw_hooks->{read}->( @_ );
139    }
140    
141    =head2 _write
142    
143    Write into memory C callback
144    
145      M6502:_write( $address, $byte );
146    
147    =cut
148    
149    sub _write {
150            return $_rw_hooks->{write}->( @_ );
151    }
152    
153    =head2 _update_perl_R
154    
155    called by C<M6502.xs> to push changes in registars back to perl variables
156    
157    =cut
158    
159    sub _update_perl_R {
160            warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug;
161            ( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_;
162            dump_R();
163    }
164    
165    =head1 XS
166    
167    Following functions are implemented in C<M6502.xs> and exported to perl.
168    
169    =head2 set_debug
170    
171      M6502::set_debug( 0 );
172    
173    =head2 get_debug
174    
175      my $debug = M6502::set_debug();
176    
177    =head2 reset
178    
179    Reset 6502 CPU, reading PC from C<0xfffc>
180    
181      M6502::reset();
182    
183    =head2 update_C_R
184    
185    Push perl notion of register values to CPU emulator
186    
187      M6502::update_C_R();
188    
189    =head2 update_perl_R
190    
191    Update perl notion of register values
192    
193      M6502::update_perl_R();
194    
195    =head2 exec
196    
197    Execute cpu for specified number of cycles
198    
199      my $cycles_left = M6502::exec( $execute_cpu_cycles );
200    
201    =head1 Helpers
202    
203    =head2 dump_R
204    
205    helper function which dumps registers in humanly readable form
206    
207      my $dump = dump_R;
208    
209    =cut
210    
211    sub dump_R {
212            my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x "
213                    . "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d"
214                    . "\n",
215                    $PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace,
216            );
217            warn "## M6502::dump_R $dump" if $debug;
218            return $dump;
219    }
220    
221    =head2 debug
222    
223    Turn perl and C-level debugging on/off
224    
225      $emu->debug( 0 );
226      $emu->debug( 1 );
227      print $emu->debug;
228    
229    =cut
230    
231    sub debug {
232            my $self = shift;
233            my $value = shift;
234            if (defined($value)) {
235                    $debug = M6502::set_debug($value);
236            } else {
237                    $debug = M6502::get_debug();
238            }
239            return $debug;
240    }
241    
242    =head1 SEE ALSO
243    
244    L<Orao> is sample implementation using this module
245    
246    =head1 AUTHOR
247    
248    Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
249    
250    =head1 COPYRIGHT & LICENSE
251    
252    Copyright 2007-8 Dobrica Pavlinusic, All Rights Reserved.
253    
254    This program is free software; you can redistribute it and/or modify it
255    under the same terms as Perl itself.
256    
257    =cut
258    
259    package M6502::TieMem;
260    
261    use strict;
262    use warnings;
263    use Tie::Array;
264    use base qw(Tie::Array);
265    
266    #
267    sub TIEARRAY {
268            my $class = shift;
269    #       my $opt   = shift;
270    #       my $self  = { %$opt };
271            my $self = {};
272            bless($self, __PACKAGE__);
273            return $self;
274    }
275    
276    sub DESTROY {}
277    
278    #
279    sub FETCH {
280          my $self = shift;          my $self = shift;
281          my ($from,$to) = @_;          my $n    = shift;
282          if ($from + $to) {          my $val = M6502::mem_peek( $n );
283                  printf "ram %04x - %04x\n", $from, $to;          warn sprintf("FETCH %04x = %02x\n", $n, $val);
284                  return $mem[$from .. $to - 1];          return $val;
285    }
286    
287    #
288    sub FETCHSIZE {
289            return 0xffff;
290    }
291    
292    #
293    sub STORE {
294            my $self = shift;
295            my $n    = shift;
296            my $val  = shift;
297            if ( $n > 0xffff ) {
298                    warn "over 64k: $n\n";
299                    return;
300          }          }
301          printf "ram %04x\n", $from;          M6502::mem_poke( $n, $val );
302          return $mem[$from] if defined($from);          warn sprintf("STORE %04x <- %02x\n",$n, $val);
303          confess "no from address";          return $val;
304    }
305    
306    #
307    sub STORESIZE {
308            die('not allowed (yet)');
309    }
310    
311    sub PUSH {
312            die('not allowed (yet)');
313    }
314    
315    sub POP {
316            die('not allowed (yet)');
317    }
318    
319    sub SHIFT {
320            die('not allowed (yet)');
321    }
322    
323    sub UNSHIFT {
324            die('not allowed (yet)');
325    }
326    
327    sub DELETE {
328            my $self = shift;
329            my $n    = shift;
330            $self->STORE($n, 0);
331    }
332    
333    sub EXISTS {
334            my $self = shift;
335            my $n    = shift;
336            return 0 if $n > 0xffff;
337            return 1;
338  }  }
339    
340  1;  1;

Legend:
Removed from v.31  
changed lines
  Added in v.203

  ViewVC Help
Powered by ViewVC 1.1.26