/[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 42 - (hide annotations)
Tue Jul 31 09:37:01 2007 UTC (16 years, 8 months ago) by dpavlin
File size: 2718 byte(s)
- use M6502::run_for to Exec6502 with run_for cycles
- added primitive command-line callback (cli)
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 40 our @EXPORT = qw'@mem $PC $A $P $X $Y $S $IPeriod $run_for';
10 dpavlin 27
11 dpavlin 29 =head1 NAME
12 dpavlin 24
13 dpavlin 29 M6502 - perl bindings for 6502 emulator
14    
15     =cut
16    
17 dpavlin 27 my $debug = 1;
18 dpavlin 24
19 dpavlin 29 our $VERSION = qw(0.0.1);
20 dpavlin 27
21 dpavlin 29 our @mem = (0xff) x 0x10000; # 64M
22    
23 dpavlin 25 # program counter
24 dpavlin 34 our $PC = 0xbeef;
25 dpavlin 25 # CPU registars
26 dpavlin 36 our ( $A, $P, $X, $Y, $S ) = (0) x 5;
27 dpavlin 25 # Set IPeriod to number of CPU cycles between calls to Loop6502
28 dpavlin 27 our $IPeriod = 1;
29 dpavlin 40 # Exec6502 cycles
30 dpavlin 42 our $run_for = 0;
31 dpavlin 24
32 dpavlin 26 =head1 init
33    
34     Called before C<Run6502>
35    
36     =cut
37    
38     sub init {
39 dpavlin 33 my $self = shift;
40     warn dump(@_);
41     warn "inside init low-level M6502 from $self\n";
42 dpavlin 26 };
43 dpavlin 27
44     =head2 read
45    
46     Read from memory
47    
48     $byte = read( $address );
49    
50     =cut
51    
52     sub read {
53     my ($addr) = @_;
54     my $byte = $mem[$addr];
55     warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug;
56     return $byte;
57     }
58    
59     =head2 write
60    
61     Write into emory
62    
63     write( $address, $byte );
64    
65     =cut
66    
67     sub write {
68     warn "# write(",dump(@_),")\n" if $debug;
69     my ($addr,$byte) = @_;
70     $mem[$addr] = $byte;
71     }
72 dpavlin 29
73     =head2 poke_code
74    
75 dpavlin 42 Write series of bytes into memory passing through MMU (C<read> and C<write>)
76     functions. If you don't want to trigger MMU, use C<write_chunk>.
77 dpavlin 29
78     $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa );
79    
80     =cut
81    
82     sub poke_code {
83     my $self = shift;
84     my $addr = shift;
85     warn sprintf("# poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug;
86 dpavlin 42 #$mem[$addr++] = $_ foreach @_;
87     $self->write($addr++, $_) foreach @_;
88 dpavlin 29 }
89    
90 dpavlin 31 =head2 write_chunk
91    
92 dpavlin 42 Low-level update of memory, overriding user specified MMU functions C<read> and C<write>
93    
94 dpavlin 31 $emu->write_chunk( $address, $chunk_of_data );
95    
96     =cut
97    
98     sub write_chunk {
99     my ($self, $addr, $chunk) = @_;
100     my $len = length($chunk);
101     splice @mem, $addr, $len, unpack('C*', $chunk);
102     }
103    
104 dpavlin 29 =head2 ram
105    
106     Read searies of bytes from memory without passing through MMU
107    
108     $emu->ram( $from, $to );
109    
110     =cut
111    
112     sub ram {
113     my $self = shift;
114     my ($from,$to) = @_;
115 dpavlin 35 warn "ram($from,$to)\n";
116 dpavlin 29 if ($from + $to) {
117 dpavlin 30 printf "ram %04x - %04x\n", $from, $to;
118 dpavlin 35 return @mem[$from .. $to - 1];
119 dpavlin 29 }
120 dpavlin 30 printf "ram %04x\n", $from;
121 dpavlin 29 return $mem[$from] if defined($from);
122     confess "no from address";
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 38 =head2 push_R
137    
138     called by C<perl.c> to push changes in registars back to perl variables
139    
140     =cut
141    
142     sub push_R {
143     warn "push_R(",dump(@_),")\n";
144     my ( $a, $p, $x, $y, $s, $pc ) = @_;
145     $PC = $pc;
146     $S=$s; $X=$x; $Y=$y; $P=$p; $A=$a;
147     dump_R();
148     }
149    
150     =head2 dump_R
151    
152     helper function which dumps registers
153    
154     =cut
155    
156     sub dump_R {
157     warn sprintf("PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S);
158     }
159    
160 dpavlin 24 1;

  ViewVC Help
Powered by ViewVC 1.1.26