/[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 35 by dpavlin, Mon Jul 30 21:53:04 2007 UTC revision 92 by dpavlin, Thu Aug 2 12:49:19 2007 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';  our @EXPORT = qw'dump_R @mem $PC $A $P $X $Y $S $IPeriod $ICount $IRequest $IAutoReset $TrapBadOps $Trap $Trace $run_for $debug';
10    our $VERSION = '0.0.2';
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    
26  # program counter  # program counter
27  our $PC = 0xbeef;  our $PC = 0xbeef;
28  # CPU registars  # CPU registars
29  our ( $A, $P, $X, $Y, $S ) = (0x42) 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;  
   
 =head1 init  
30    
31  Called before C<Run6502>  our $IPeriod=1;         # Set IPeriod to number of CPU cycles between calls to Loop6502
32    our $ICount;
33    our $IRequest;          # Set to the INT_IRQ when pending IRQ
34    our $IAutoReset;        # Set to 1 to autom. reset IRequest
35    our $TrapBadOps=1;      # Set to 1 to warn of illegal opcodes
36    our $Trap;                      # Set Trap to address to trace from
37    our $Trace;                     # Set Trace=1 to start tracing
38    
39    # Exec6502 cycles
40    our $run_for = 0;
41    
42    =head2 init
43    
44    Setup read and write memory hooks (to implement memory mapped devices)
45    
46      $init->(
47            read => sub {
48                    return $mem[$_[0]];
49            },
50            write => sub {
51                    $mem[$_[0]] = $_[1];
52            },
53      );
54    
55  =cut  =cut
56    
57  sub init {  our $_rw_hooks = {
58          my $self = shift;          read => sub {
59          warn dump(@_);                  warn sprintf("# callback read(%04x) not implemented", @_) if $debug;
60          warn "inside init low-level M6502 from $self\n";                  return $mem[$_[0]];
61            },
62            write => sub {
63                    warn sprintf("# callback write(%04x,%02x) not implemented", @_) if $debug;
64                    $mem[$_[0]] = $_[1];
65            },
66  };  };
67    
68  =head2 read  sub init {
69            my $self = shift;
70  Read from memory          my $args = {@_};
71            warn "inside init low-level M6502 from ",ref($self),"\n";
   $byte = read( $address );  
72    
73  =cut          foreach my $p ( qw/read write/ ) {
74                    confess "need $p argument as coderef" unless ( $args->{$p} && ref($args->{$p}) eq 'CODE' );
75                    $_rw_hooks->{$p} = $args->{$p};
76            }
77    
78  sub read {  };
         my ($addr) = @_;  
         my $byte = $mem[$addr];  
         warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug;  
         return $byte;  
 }  
79    
80  =head2 write  =head2 poke_code
81    
82  Write into emory  Write series of bytes into memory passing through MMU (C<read> and C<write>)
83    functions. If you don't want to trigger MMU, use C<write_chunk>.
84    
85    write( $address, $byte );    $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );
86    
87  =cut  =cut
88    
89  sub write {  sub poke_code {
90          warn "# write(",dump(@_),")\n" if $debug;          my $self = shift;
91          my ($addr,$byte) = @_;          my $addr = shift;
92          $mem[$addr] = $byte;          warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;
93            #$mem[$addr++] = $_ foreach @_;
94            # call low-level write
95            $_rw_hooks->{write}->( $addr++, $_ ) foreach @_;
96  }  }
97    
98  =head2 poke_code  =head2 ram
99    
100  Write series of bytes into memory without passing through MMU  Read series of bytes into memory without MMU interaction
101    
102    $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );    my @code = $emu->ram( 0xc000, 0xc1000 );
103    
104  =cut  =cut
105    
106  sub poke_code {  sub ram {
107          my $self = shift;          my $self = shift;
108          my $addr = shift;          my ( $from, $to ) = @_;
109          warn sprintf("# poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;          warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug;
110          $mem[$addr++] = $_ foreach @_;          return @mem[ $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 95  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  =head2 prompt
128    
129  Read searies of bytes from memory without passing through MMU  Call this after C<< $run_for >> cycles have been run on processor
130    
131    $emu->ram( $from, $to );  =cut
132    
133    sub prompt {
134            warn "prompt -- you should override this\n";
135            return 1;
136    }
137    
138    =head1 XS Callbacks
139    
140    This functions are called from C<M6502.xs>
141    
142    =head2 _read
143    
144    Read from memory C callback
145    
146      $byte = M6502::_read( $address );
147    
148  =cut  =cut
149    
150  sub ram {  sub _read {
151            return $_rw_hooks->{read}->( @_ );
152    }
153    
154    =head2 _write
155    
156    Write into memory C callback
157    
158      M6502:_write( $address, $byte );
159    
160    =cut
161    
162    sub _write {
163            return $_rw_hooks->{write}->( @_ );
164    }
165    
166    =head2 _update_perl_R
167    
168    called by C<M6502.xs> to push changes in registars back to perl variables
169    
170    =cut
171    
172    sub _update_perl_R {
173            warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug;
174            ( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_;
175            dump_R();
176    }
177    
178    =head2 dump_R
179    
180    helper function which dumps registers in humanly readable form
181    
182      my $dump = dump_R;
183    
184    =cut
185    
186    sub dump_R {
187            my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x "
188                    . "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d"
189                    . "\n",
190                    $PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace,
191            );
192            warn "## M6502::dump_R $dump" if $debug;
193            return $dump;
194    }
195    
196    =head2 debug
197    
198    Turn perl and C-level debugging on/off
199    
200      $emu->debug( 0 );
201      $emu->debug( 1 );
202      print $emu->debug;
203    
204    =cut
205    
206    sub debug {
207          my $self = shift;          my $self = shift;
208          my ($from,$to) = @_;          my $value = shift;
209          warn "ram($from,$to)\n";          if (defined($value)) {
210          if ($from + $to) {                  $debug = M6502::set_debug($value);
211                  printf "ram %04x - %04x\n", $from, $to;          } else {
212                  return @mem[$from .. $to - 1];                  $debug = M6502::get_debug();
213          }          }
214          printf "ram %04x\n", $from;          return $debug;
         return $mem[$from] if defined($from);  
         confess "no from address";  
215  }  }
216    
217    =head1 SEE ALSO
218    
219    L<Orao> is sample implementation using this module
220    
221    =head1 AUTHOR
222    
223    Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
224    
225    =head1 COPYRIGHT & LICENSE
226    
227    Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
228    
229    This program is free software; you can redistribute it and/or modify it
230    under the same terms as Perl itself.
231    
232    =cut
233  1;  1;

Legend:
Removed from v.35  
changed lines
  Added in v.92

  ViewVC Help
Powered by ViewVC 1.1.26