/[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

Annotation of /M6502/M6502.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 90 - (hide annotations)
Thu Aug 2 12:23:18 2007 UTC (16 years, 8 months ago) by dpavlin
File size: 4455 byte(s)
minor tweaks, everything tested now works
1 dpavlin 24 package M6502;
2    
3     use strict;
4     use warnings;
5    
6 dpavlin 27 use Data::Dump qw/dump/;
7 dpavlin 29 use Carp qw/confess/;
8 dpavlin 34 use Exporter 'import';
9 dpavlin 86 our @EXPORT = qw'dump_R @mem $PC $A $P $X $Y $S $IPeriod $ICount $IRequest $IAutoReset $TrapBadOps $Trap $Trace $run_for $debug';
10 dpavlin 80 our $VERSION = '0.0.2';
11     require XSLoader;
12     XSLoader::load('M6502', $VERSION);
13 dpavlin 27
14 dpavlin 29 =head1 NAME
15 dpavlin 24
16 dpavlin 54 M6502 - perl bindings for M6502 CPU emulator
17 dpavlin 29
18 dpavlin 89 =head1 FUNCTIONS
19    
20 dpavlin 29 =cut
21    
22 dpavlin 87 our $debug = 0;
23 dpavlin 24
24 dpavlin 29 our @mem = (0xff) x 0x10000; # 64M
25    
26 dpavlin 25 # program counter
27 dpavlin 34 our $PC = 0xbeef;
28 dpavlin 25 # CPU registars
29 dpavlin 36 our ( $A, $P, $X, $Y, $S ) = (0) x 5;
30 dpavlin 74
31     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 dpavlin 40 # Exec6502 cycles
40 dpavlin 42 our $run_for = 0;
41 dpavlin 24
42 dpavlin 54 =head2 init
43    
44 dpavlin 89 Setup read and write memory hooks (to implement memory mapped devices)
45 dpavlin 26
46 dpavlin 89 $init->(
47     read => sub {
48     return $mem[$_[0]];
49     },
50     write => sub {
51     $mem[$_[0]] = $_[1];
52     },
53     );
54    
55 dpavlin 26 =cut
56    
57 dpavlin 89 our $_rw_hooks = {
58     read => sub {
59     return $mem[$_[0]];
60     },
61     write => sub {
62     $mem[$_[0]] = $_[1];
63     },
64     };
65    
66 dpavlin 26 sub init {
67 dpavlin 33 my $self = shift;
68 dpavlin 89 my $args = {@_};
69     warn "inside init low-level M6502 from ",ref($self),"\n";
70 dpavlin 27
71 dpavlin 89 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 dpavlin 27
76 dpavlin 89 };
77 dpavlin 27
78 dpavlin 29 =head2 poke_code
79    
80 dpavlin 42 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 dpavlin 29
83     $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );
84    
85     =cut
86    
87     sub poke_code {
88     my $self = shift;
89     my $addr = shift;
90 dpavlin 50 warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;
91 dpavlin 42 #$mem[$addr++] = $_ foreach @_;
92 dpavlin 51 # call low-level write
93 dpavlin 90 $_rw_hooks->{write}->( $addr++, $_ ) foreach @_;
94 dpavlin 29 }
95    
96 dpavlin 51 =head2 ram
97    
98     Read series of bytes into memory without MMU interaction
99    
100     my @code = $emu->ram( 0xc000, 0xc1000 );
101    
102     =cut
103    
104     sub ram {
105     my $self = shift;
106     my ( $from, $to ) = @_;
107     warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug;
108     return @mem[ $from .. $to ];
109     }
110    
111 dpavlin 31 =head2 write_chunk
112    
113 dpavlin 42 Low-level update of memory, overriding user specified MMU functions C<read> and C<write>
114    
115 dpavlin 31 $emu->write_chunk( $address, $chunk_of_data );
116    
117     =cut
118    
119     sub write_chunk {
120     my ($self, $addr, $chunk) = @_;
121     my $len = length($chunk);
122     splice @mem, $addr, $len, unpack('C*', $chunk);
123     }
124    
125 dpavlin 42 =head2 prompt
126    
127     Call this after C<< $run_for >> cycles have been run on processor
128    
129     =cut
130    
131     sub prompt {
132     warn "prompt -- you should override this\n";
133     return 1;
134     }
135    
136 dpavlin 89 =head1 XS Callbacks
137    
138     This functions are called from C<M6502.xs>
139    
140     =head2 _read
141    
142     Read from memory C callback
143    
144     $byte = M6502::_read( $address );
145    
146     =cut
147    
148     sub _read {
149     return $_rw_hooks->{read}->( @_ );
150     }
151    
152     =head2 _write
153    
154     Write into memory C callback
155    
156     M6502:_write( $address, $byte );
157    
158     =cut
159    
160     sub _write {
161     return $_rw_hooks->{write}->( @_ );
162     }
163    
164 dpavlin 87 =head2 _update_perl_R
165 dpavlin 38
166 dpavlin 87 called by C<M6502.xs> to push changes in registars back to perl variables
167 dpavlin 38
168     =cut
169    
170 dpavlin 87 sub _update_perl_R {
171     warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug;
172 dpavlin 74 ( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_;
173 dpavlin 38 dump_R();
174     }
175    
176     =head2 dump_R
177    
178 dpavlin 68 helper function which dumps registers in humanly readable form
179 dpavlin 38
180 dpavlin 68 my $dump = dump_R;
181    
182 dpavlin 38 =cut
183    
184     sub dump_R {
185 dpavlin 74 my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x "
186     . "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d"
187     . "\n",
188     $PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace,
189     );
190 dpavlin 68 warn "## M6502::dump_R $dump" if $debug;
191     return $dump;
192 dpavlin 38 }
193    
194 dpavlin 87 =head2 debug
195    
196     Turn perl and C-level debugging on/off
197    
198     $emu->debug( 0 );
199     $emu->debug( 1 );
200     print $emu->debug;
201    
202     =cut
203    
204     sub debug {
205     my $self = shift;
206     my $value = shift;
207     if (defined($value)) {
208     $debug = M6502::set_debug($value);
209     } else {
210     $debug = M6502::get_debug();
211     }
212     return $debug;
213     }
214    
215 dpavlin 55 =head1 SEE ALSO
216 dpavlin 54
217 dpavlin 55 L<Orao> is sample implementation using this module
218    
219 dpavlin 54 =head1 AUTHOR
220    
221     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
222    
223     =head1 COPYRIGHT & LICENSE
224    
225     Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
226    
227     This program is free software; you can redistribute it and/or modify it
228     under the same terms as Perl itself.
229    
230     =cut
231 dpavlin 24 1;

  ViewVC Help
Powered by ViewVC 1.1.26