/[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 40 by dpavlin, Tue Jul 31 08:41:06 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';  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;
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 = 1;  
 $run_for = 0x42;  
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          my $self = shift;          read => sub {
58          warn dump(@_);                  warn sprintf("# callback read(%04x) not implemented\n", @_) if $debug;
59          warn "inside init low-level M6502 from $self\n";                  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  }  }
111    
112  =head2 write_chunk  =head2 write_chunk
113    
114    Low-level update of memory, overriding user specified MMU functions C<read> and C<write>
115    
116    $emu->write_chunk( $address, $chunk_of_data );    $emu->write_chunk( $address, $chunk_of_data );
117    
118  =cut  =cut
# Line 98  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 ram  =head1 XS Callbacks
127    
128    This functions are called from C<M6502.xs>
129    
130  Read searies of bytes from memory without passing through MMU  =head2 _read
131    
132    $emu->ram( $from, $to );  Read from memory C callback
133    
134      $byte = M6502::_read( $address );
135    
136  =cut  =cut
137    
138  sub ram {  sub _read {
139          my $self = shift;          return $_rw_hooks->{read}->( @_ );
140          my ($from,$to) = @_;  }
141          warn "ram($from,$to)\n";  
142          if ($from + $to) {  =head2 _write
143                  printf "ram %04x - %04x\n", $from, $to;  
144                  return @mem[$from .. $to - 1];  Write into memory C callback
145          }  
146          printf "ram %04x\n", $from;    M6502:_write( $address, $byte );
147          return $mem[$from] if defined($from);  
148          confess "no from address";  =cut
149    
150    sub _write {
151            return $_rw_hooks->{write}->( @_ );
152  }  }
153    
154  =head2 push_R  =head2 _update_perl_R
155    
156  called by C<perl.c> to push changes in registars back to perl variables  called by C<M6502.xs> to push changes in registars back to perl variables
157    
158  =cut  =cut
159    
160  sub push_R {  sub _update_perl_R {
161          warn "push_R(",dump(@_),")\n";          warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug;
162          my ( $a, $p, $x, $y, $s, $pc ) = @_;          ( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_;
         $PC = $pc;  
         $S=$s; $X=$x; $Y=$y; $P=$p; $A=$a;  
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  helper function which dumps registers in humanly readable form
207    
208      my $dump = dump_R;
209    
210  =cut  =cut
211    
212  sub dump_R {  sub dump_R {
213          warn 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;
219            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
244    
245    L<Orao> is sample implementation using this module
246    
247    =head1 AUTHOR
248    
249    Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
250    
251    =head1 COPYRIGHT & LICENSE
252    
253    Copyright 2007-8 Dobrica Pavlinusic, All Rights Reserved.
254    
255    This program is free software; you can redistribute it and/or modify it
256    under the same terms as Perl itself.
257    
258    =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.40  
changed lines
  Added in v.206

  ViewVC Help
Powered by ViewVC 1.1.26