/[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 42 by dpavlin, Tue Jul 31 09:37:01 2007 UTC revision 74 by dpavlin, Tue Jul 31 23:48: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 $run_for';  our @EXPORT = qw'@mem $PC $A $P $X $Y $S $IPeriod $ICount $IRequest $IAutoReset $TrapBadOps $Trap $Trace $run_for $debug';
10    
11  =head1 NAME  =head1 NAME
12    
13  M6502 - perl bindings for 6502 emulator  M6502 - perl bindings for M6502 CPU emulator
14    
15  =cut  =cut
16    
17  my $debug = 1;  my $debug = 0;
18    
19  our $VERSION = qw(0.0.1);  our $VERSION = qw(0.0.1);
20    
# Line 24  our @mem = (0xff) x 0x10000;   # 64M Line 24  our @mem = (0xff) x 0x10000;   # 64M
24  our $PC = 0xbeef;  our $PC = 0xbeef;
25  # CPU registars  # CPU registars
26  our ( $A, $P, $X, $Y, $S ) = (0) x 5;  our ( $A, $P, $X, $Y, $S ) = (0) x 5;
27  # Set IPeriod to number of CPU cycles between calls to Loop6502  
28  our $IPeriod = 1;  our $IPeriod=1;         # Set IPeriod to number of CPU cycles between calls to Loop6502
29    our $ICount;
30    our $IRequest;          # Set to the INT_IRQ when pending IRQ
31    our $IAutoReset;        # Set to 1 to autom. reset IRequest
32    our $TrapBadOps=1;      # Set to 1 to warn of illegal opcodes
33    our $Trap;                      # Set Trap to address to trace from
34    our $Trace;                     # Set Trace=1 to start tracing
35    
36  # Exec6502 cycles  # Exec6502 cycles
37  our $run_for = 0;  our $run_for = 0;
38    
39  =head1 init  =head1 FUNCTIONS
40    
41    =head2 init
42    
43  Called before C<Run6502>  Called before C<Run6502>
44    
# Line 37  Called before C<Run6502> Line 46  Called before C<Run6502>
46    
47  sub init {  sub init {
48          my $self = shift;          my $self = shift;
         warn dump(@_);  
49          warn "inside init low-level M6502 from $self\n";          warn "inside init low-level M6502 from $self\n";
50  };  };
51    
# Line 52  Read from memory Line 60  Read from memory
60  sub read {  sub read {
61          my ($addr) = @_;          my ($addr) = @_;
62          my $byte = $mem[$addr];          my $byte = $mem[$addr];
63          warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug;          warn "## M6502::read(",dump(@_),") = ",dump( $byte ),"\n" if $debug;
64          return $byte;          return $byte;
65  }  }
66    
# Line 65  Write into emory Line 73  Write into emory
73  =cut  =cut
74    
75  sub write {  sub write {
76          warn "# write(",dump(@_),")\n" if $debug;          warn "## M6502::write(",dump(@_),")\n" if $debug;
77          my ($addr,$byte) = @_;          my ($addr,$byte) = @_;
78          $mem[$addr] = $byte;          $mem[$addr] = $byte;
79  }  }
# Line 82  functions. If you don't want to trigger Line 90  functions. If you don't want to trigger
90  sub poke_code {  sub poke_code {
91          my $self = shift;          my $self = shift;
92          my $addr = shift;          my $addr = shift;
93          warn sprintf("# poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;          warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;
94          #$mem[$addr++] = $_ foreach @_;          #$mem[$addr++] = $_ foreach @_;
95          $self->write($addr++, $_) foreach @_;          # call low-level write
96            Arch::write($addr++, $_) foreach @_;
97  }  }
98    
99  =head2 write_chunk  =head2 ram
100    
101  Low-level update of memory, overriding user specified MMU functions C<read> and C<write>  Read series of bytes into memory without MMU interaction
102    
103    $emu->write_chunk( $address, $chunk_of_data );    my @code = $emu->ram( 0xc000, 0xc1000 );
104    
105  =cut  =cut
106    
107  sub write_chunk {  sub ram {
108          my ($self, $addr, $chunk) = @_;          my $self = shift;
109          my $len = length($chunk);          my ( $from, $to ) = @_;
110          splice @mem, $addr, $len, unpack('C*', $chunk);          warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug;
111            return @mem[ $from .. $to ];
112  }  }
113    
114  =head2 ram  =head2 write_chunk
115    
116  Read searies of bytes from memory without passing through MMU  Low-level update of memory, overriding user specified MMU functions C<read> and C<write>
117    
118    $emu->ram( $from, $to );    $emu->write_chunk( $address, $chunk_of_data );
119    
120  =cut  =cut
121    
122  sub ram {  sub write_chunk {
123          my $self = shift;          my ($self, $addr, $chunk) = @_;
124          my ($from,$to) = @_;          my $len = length($chunk);
125          warn "ram($from,$to)\n";          splice @mem, $addr, $len, unpack('C*', $chunk);
         if ($from + $to) {  
                 printf "ram %04x - %04x\n", $from, $to;  
                 return @mem[$from .. $to - 1];  
         }  
         printf "ram %04x\n", $from;  
         return $mem[$from] if defined($from);  
         confess "no from address";  
126  }  }
127    
128  =head2 prompt  =head2 prompt
# Line 140  called by C<perl.c> to push changes in r Line 143  called by C<perl.c> to push changes in r
143  =cut  =cut
144    
145  sub push_R {  sub push_R {
146          warn "push_R(",dump(@_),")\n";          warn "## M6502::push_R(",dump(@_),")\n" if $debug;
147          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;  
148          dump_R();          dump_R();
149  }  }
150    
151  =head2 dump_R  =head2 dump_R
152    
153  helper function which dumps registers  helper function which dumps registers in humanly readable form
154    
155      my $dump = dump_R;
156    
157  =cut  =cut
158    
159  sub dump_R {  sub dump_R {
160          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 "
161                    . "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d"
162                    . "\n",
163                    $PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace,
164            );
165            warn "## M6502::dump_R $dump" if $debug;
166            return $dump;
167  }  }
168    
169    =head1 SEE ALSO
170    
171    L<Orao> is sample implementation using this module
172    
173    =head1 AUTHOR
174    
175    Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
176    
177    =head1 COPYRIGHT & LICENSE
178    
179    Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
180    
181    This program is free software; you can redistribute it and/or modify it
182    under the same terms as Perl itself.
183    
184    =cut
185  1;  1;

Legend:
Removed from v.42  
changed lines
  Added in v.74

  ViewVC Help
Powered by ViewVC 1.1.26