--- M6502/M6502.pm 2007/07/30 14:23:22 26 +++ M6502/M6502.pm 2007/07/31 08:41:06 40 @@ -3,18 +3,32 @@ use strict; use warnings; -# Dobrica Pavlinusic, 07/30/07 13:23:19 CEST -# -# Simple Orao emulation +use Data::Dump qw/dump/; +use Carp qw/confess/; +use Exporter 'import'; +our @EXPORT = qw'@mem $PC $A $P $X $Y $S $IPeriod $run_for'; -my $mem = (0) x 0x10000; # 64M +=head1 NAME + +M6502 - perl bindings for 6502 emulator + +=cut + +my $debug = 1; + +our $VERSION = qw(0.0.1); + +our @mem = (0xff) x 0x10000; # 64M # program counter our $PC = 0xbeef; # CPU registars our ( $A, $P, $X, $Y, $S ) = (0) x 5; # Set IPeriod to number of CPU cycles between calls to Loop6502 -our $IPeriod = 20000; +our $IPeriod = 1; +# Exec6502 cycles +our $run_for = 1; +$run_for = 0x42; =head1 init @@ -23,7 +37,110 @@ =cut sub init { - warn "inside init\n"; - print "stdout\n"; + my $self = shift; + warn dump(@_); + warn "inside init low-level M6502 from $self\n"; }; + +=head2 read + +Read from memory + + $byte = read( $address ); + +=cut + +sub read { + my ($addr) = @_; + my $byte = $mem[$addr]; + warn "# read(",dump(@_),") = ",dump( $byte ),"\n" if $debug; + return $byte; +} + +=head2 write + +Write into emory + + write( $address, $byte ); + +=cut + +sub write { + warn "# write(",dump(@_),")\n" if $debug; + my ($addr,$byte) = @_; + $mem[$addr] = $byte; +} + +=head2 poke_code + +Write series of bytes into memory without passing through MMU + + $emu->poke_code( 0xbeef, 0xff, 0x00, 0xff, 0x00, 0xaa ); + +=cut + +sub poke_code { + my $self = shift; + my $addr = shift; + warn sprintf("# poke_code(%04x,%s)\n", $addr, dump( @_ )) if $self->debug; + $mem[$addr++] = $_ foreach @_; +} + +=head2 write_chunk + + $emu->write_chunk( $address, $chunk_of_data ); + +=cut + +sub write_chunk { + my ($self, $addr, $chunk) = @_; + my $len = length($chunk); + splice @mem, $addr, $len, unpack('C*', $chunk); +} + +=head2 ram + +Read searies of bytes from memory without passing through MMU + + $emu->ram( $from, $to ); + +=cut + +sub ram { + my $self = shift; + my ($from,$to) = @_; + warn "ram($from,$to)\n"; + 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"; +} + +=head2 push_R + +called by C to push changes in registars back to perl variables + +=cut + +sub push_R { + warn "push_R(",dump(@_),")\n"; + my ( $a, $p, $x, $y, $s, $pc ) = @_; + $PC = $pc; + $S=$s; $X=$x; $Y=$y; $P=$p; $A=$a; + dump_R(); +} + +=head2 dump_R + +helper function which dumps registers + +=cut + +sub dump_R { + warn sprintf("PC: %04x A:%02x P:%02x X:%02x Y:%02x S:%02x\n", $PC, $A, $P, $X, $Y, $S); +} + 1;