/[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 87 - (hide annotations)
Thu Aug 2 11:08:10 2007 UTC (16 years, 8 months ago) by dpavlin
File size: 4038 byte(s)
- transfer debug state into C, added accesor M6502->debug();
- update_*_R functions to keep perl vars in sync with C
- tests
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     =cut
19    
20 dpavlin 87 our $debug = 0;
21 dpavlin 24
22 dpavlin 29 our @mem = (0xff) x 0x10000; # 64M
23    
24 dpavlin 25 # program counter
25 dpavlin 34 our $PC = 0xbeef;
26 dpavlin 25 # CPU registars
27 dpavlin 36 our ( $A, $P, $X, $Y, $S ) = (0) x 5;
28 dpavlin 74
29     our $IPeriod=1; # Set IPeriod to number of CPU cycles between calls to Loop6502
30     our $ICount;
31     our $IRequest; # Set to the INT_IRQ when pending IRQ
32     our $IAutoReset; # Set to 1 to autom. reset IRequest
33     our $TrapBadOps=1; # Set to 1 to warn of illegal opcodes
34     our $Trap; # Set Trap to address to trace from
35     our $Trace; # Set Trace=1 to start tracing
36    
37 dpavlin 40 # Exec6502 cycles
38 dpavlin 42 our $run_for = 0;
39 dpavlin 24
40 dpavlin 54 =head1 FUNCTIONS
41 dpavlin 26
42 dpavlin 54 =head2 init
43    
44 dpavlin 26 Called before C<Run6502>
45    
46     =cut
47    
48     sub init {
49 dpavlin 33 my $self = shift;
50     warn "inside init low-level M6502 from $self\n";
51 dpavlin 26 };
52 dpavlin 27
53     =head2 read
54    
55     Read from memory
56    
57     $byte = read( $address );
58    
59     =cut
60    
61     sub read {
62     my ($addr) = @_;
63     my $byte = $mem[$addr];
64 dpavlin 50 warn "## M6502::read(",dump(@_),") = ",dump( $byte ),"\n" if $debug;
65 dpavlin 27 return $byte;
66     }
67    
68     =head2 write
69    
70     Write into emory
71    
72     write( $address, $byte );
73    
74     =cut
75    
76     sub write {
77 dpavlin 50 warn "## M6502::write(",dump(@_),")\n" if $debug;
78 dpavlin 27 my ($addr,$byte) = @_;
79     $mem[$addr] = $byte;
80     }
81 dpavlin 29
82     =head2 poke_code
83    
84 dpavlin 42 Write series of bytes into memory passing through MMU (C<read> and C<write>)
85     functions. If you don't want to trigger MMU, use C<write_chunk>.
86 dpavlin 29
87     $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );
88    
89     =cut
90    
91     sub poke_code {
92     my $self = shift;
93     my $addr = shift;
94 dpavlin 50 warn sprintf("## M6502::poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;
95 dpavlin 42 #$mem[$addr++] = $_ foreach @_;
96 dpavlin 51 # call low-level write
97     Arch::write($addr++, $_) foreach @_;
98 dpavlin 29 }
99    
100 dpavlin 51 =head2 ram
101    
102     Read series of bytes into memory without MMU interaction
103    
104     my @code = $emu->ram( 0xc000, 0xc1000 );
105    
106     =cut
107    
108     sub ram {
109     my $self = shift;
110     my ( $from, $to ) = @_;
111     warn sprintf("## M6502::ram(%04x,%04x)\n", $from, $to) if $self->debug;
112     return @mem[ $from .. $to ];
113     }
114    
115 dpavlin 31 =head2 write_chunk
116    
117 dpavlin 42 Low-level update of memory, overriding user specified MMU functions C<read> and C<write>
118    
119 dpavlin 31 $emu->write_chunk( $address, $chunk_of_data );
120    
121     =cut
122    
123     sub write_chunk {
124     my ($self, $addr, $chunk) = @_;
125     my $len = length($chunk);
126     splice @mem, $addr, $len, unpack('C*', $chunk);
127     }
128    
129 dpavlin 42 =head2 prompt
130    
131     Call this after C<< $run_for >> cycles have been run on processor
132    
133     =cut
134    
135     sub prompt {
136     warn "prompt -- you should override this\n";
137     return 1;
138     }
139    
140 dpavlin 87 =head2 _update_perl_R
141 dpavlin 38
142 dpavlin 87 called by C<M6502.xs> to push changes in registars back to perl variables
143 dpavlin 38
144     =cut
145    
146 dpavlin 87 sub _update_perl_R {
147     warn "## M6502::update_perl_R(",dump(@_),")\n" if $debug;
148 dpavlin 74 ( $A, $P, $X, $Y, $S, $PC, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace ) = @_;
149 dpavlin 38 dump_R();
150     }
151    
152     =head2 dump_R
153    
154 dpavlin 68 helper function which dumps registers in humanly readable form
155 dpavlin 38
156 dpavlin 68 my $dump = dump_R;
157    
158 dpavlin 38 =cut
159    
160     sub dump_R {
161 dpavlin 74 my $dump = sprintf(" PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x "
162     . "IPeriod:%d ICount:%d IRequest:%02x IAutoReset:%02x TrapBadOps:%d Trap:%d Trace:%d"
163     . "\n",
164     $PC, $A, $P, $X, $Y, $S, $IPeriod, $ICount, $IRequest, $IAutoReset, $TrapBadOps, $Trap, $Trace,
165     );
166 dpavlin 68 warn "## M6502::dump_R $dump" if $debug;
167     return $dump;
168 dpavlin 38 }
169    
170 dpavlin 87 =head2 debug
171    
172     Turn perl and C-level debugging on/off
173    
174     $emu->debug( 0 );
175     $emu->debug( 1 );
176     print $emu->debug;
177    
178     =cut
179    
180     sub debug {
181     my $self = shift;
182     my $value = shift;
183     if (defined($value)) {
184     $debug = M6502::set_debug($value);
185     } else {
186     $debug = M6502::get_debug();
187     }
188     return $debug;
189     }
190    
191 dpavlin 55 =head1 SEE ALSO
192 dpavlin 54
193 dpavlin 55 L<Orao> is sample implementation using this module
194    
195 dpavlin 54 =head1 AUTHOR
196    
197     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
198    
199     =head1 COPYRIGHT & LICENSE
200    
201     Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
202    
203     This program is free software; you can redistribute it and/or modify it
204     under the same terms as Perl itself.
205    
206     =cut
207 dpavlin 24 1;

  ViewVC Help
Powered by ViewVC 1.1.26