--- M6502/Screen.pm 2007/07/30 18:37:37 32 +++ Screen.pm 2007/08/05 01:34:40 143 @@ -8,9 +8,20 @@ use SDL::App; use SDL::Rect; use SDL::Color; +use SDL::Constants; -use base qw(Class::Accessor); -__PACKAGE__->mk_accessors(qw(debug scale show_mem run_for mem_dump trace)); +use Carp qw/confess/; +use Data::Dump qw/dump/; + +use Exporter 'import'; +our @EXPORT = qw'$white $black'; + +use base qw(Class::Accessor Prefs); +__PACKAGE__->mk_accessors(qw(app event)); + +=head1 NAME + +Screen - simulated 256*256 pixels monochrome screen using SDL =head2 open_screen @@ -23,6 +34,8 @@ sub open_screen { my $self = shift; + $self->prefs; + if ( ! $self->scale ) { $self->scale( 1 ); warn "using default unscaled display\n"; @@ -32,107 +45,267 @@ -width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), -height => 256 * $self->scale, -depth => 16, + -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL, ); - #$app->grab_input( 0 ); + #$app->grab_input( SDL_GRAB_QUERY ); + $app->grab_input( SDL_GRAB_OFF ); + + $self->app( $app ); + + my $event = SDL::Event->new(); + $self->event( $event ); warn "# created SDL::App\n"; } -my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); -my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); +our $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); +our $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); -=head2 p +=head2 mem_xy + +Helper to return x and y coordinates in memory map - $screen->p( $x, $y, 1 ); + my ( $x,$y ) = $screen->mem_xy( $address ); =cut -sub p { +sub mem_xy { my $self = shift; + my $offset = shift; + my $x = $offset & 0xff; + $x += 256 * $self->scale; + my $y = $offset >> 8; + return ($x,$y); +} - my ($x,$y,$w) = (@_); +=head2 mmap_pixel - warn "p($x,$y,$w)\n" if $self->debug; +Draw pixel in memory map - my $scale = $self->scale; - my $rect = SDL::Rect->new( - -height => $scale, - -width => $scale, - -x => $x * $scale, - -y => $y * $scale, - ); + $self->mmap_pixel( $addr, $r, $g, $b ); + +=cut + +# keep accesses to memory +my $_mem_stat; + +sub mmap_pixel { + my ( $self, $addr, $r, $g, $b ) = @_; + return unless $self->show_mem && $self->app; + + my ( $x, $y ) = $self->mem_xy( $addr ); + warn sprintf "## mem %04x %02x %02x %02d*%02d\n", $addr, $r, $g, $x, $y if $self->debug; + + my $col = SDL::Color->new( -r => $r, -g => $g, -b => $b ); + $self->app->pixel( $x, $y, $col ); - $app->fill( $rect, $w ? $white : $black ); - $app->update( $rect ); + $_mem_stat++; + if ( $_mem_stat % 1000 == 0 ) { + $self->app->sync; + } } -=head2 mem_xy -Helper to return x and y coordinates in memory map +=head2 sync - my ( $x,$y ) = $screen->mem_xy( $address ); + $self->sync; =cut -sub mem_xy { +sub sync { + $app->sync; +} + +=head2 render_vram + +Render one frame of video ram + + $self->render_vram; + +=cut + +sub render_vram { my $self = shift; - my $offset = shift; - my $x = $offset & 0xff; - $x += 256 * $self->scale; - my $y = $offset >> 8; - return ($x,$y); + + confess "please implement $self::render_vram"; } -=head2 vram -Push byte to video memory and draw it +=head2 render_frame + +Render one frame of video ram - $screen->vram( $offset, $byte ); + $self->render_frame( $vram_sdl_surface ); =cut -sub vram { - my ( $self, $offset, $byte ) = @_; - my $x = ( $offset % 32 ) << 3; - my $y = $offset >> 5; - my $mask = 1; +sub render_frame { + my $self = shift; + + my $vram = shift; + confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; + + $vram->display_format; - printf "## vram %04x %02x*%02x %02x\n", $offset, $x, $y, $byte if $self->trace; + my $scale = $self->scale || confess "no scale?"; - foreach ( 0 .. 7 ) { - p($x + $_, $y, $byte & $mask ); - $mask = $mask << 1; + my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); + my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); + + if ( $scale > 1 ) { + use SDL::Tool::Graphic; + # last parametar is anti-alias + my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 ); + $zoomed->blit( $rect, $app, $rect_screen ); + } else { + $vram->blit( $rect, $app, $rect_screen ); } + + $app->sync; } -=head2 mmap_pixel -Draw pixel in memory map +=head2 render_mem - $self->mmap_pixel( $addr, $r, $g, $b ); + $self->render_mem( @mem ); =cut -# keep accesses to memory -my $_mem_stat; +sub render_mem { + my $self = shift; -sub mmap_pixel { - my ( $self, $addr, $r, $g, $b ) = @_; + return unless $self->show_mem; - my ( $x, $y ) = mem_xy( $addr ); - warn sprintf "## mem %04x %02x %02x %02d*%02d\n", $addr, $r, $g, $x, $y if $self->trace; + my $pixels = pack("C*", @_); - my $col = sdl::color->new( -r => $r, -g => $g, -b => $b ); - $app->pixel( $x, $y, $col ); + my $vram = SDL::Surface->new( + -width => 256, + -height => 256, + -depth => 8, # 1 bit per pixel + -pitch => 256, # bytes per line + -from => $pixels, + -Rmask => 0xffff00ff, + -Gmask => 0xffff00ff, + -Bmask => 0xffff00ff, + ); - $_mem_stat++; - if ( $_mem_stat % 1000 == 0 ) { - $app->sync; + $vram->display_format; + + my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); + my $rect_mem = SDL::Rect->new( -x => 256 * $self->scale, -y => 0, -width => 256, -height => 256 ); + + $vram->blit( $rect, $app, $rect_mem ); + + $app->sync; +} + +=head2 key_pressed + +Check SDL event loop if there are any pending keys + + my $key = $self->key_pressed; + + if ( $self->key_pressed( 1 ) ) { + # just to check other events, don't process + # key + } + +=cut + +my $pending_key; +my $run_for = 2000; + +my $key_down; + +sub key_down { + my $self = shift; + my $key = shift; + warn "key_down($key) = ",$key_down->{$key}, "\n" if $self->debug; + return $key_down->{$key}; +} + +sub key_pressed { + my $self = shift; + + # don't take key, just pull event + my $just_checking = shift || 0; + + my $event = $self->event || confess "no event?"; + + $event->poll || return $pending_key; + + my $type = $event->type(); + + exit if ($type == SDL_QUIT); + + my $k = $pending_key; + + if ($type == SDL_KEYDOWN) { + $k = $event->key_name(); + $key_down->{$k}++; + if ( $k eq 'escape' ) { + $run_for = $self->cli; + warn "will check event loop every $run_for cycles\n"; + $pending_key = '~'; + } else { + warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; + $pending_key = $k; + } + } elsif ( $type == SDL_KEYUP ) { + my $up = $event->key_name(); + $key_down->{$up} = 0; + warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; + undef $pending_key; } + + warn "key_pressed = $pending_key\n" if $pending_key; + + return $pending_key; } +=head2 loop + +Implement CPU run for C<$run_run> cycles inside SDL event loop + + $self->loop( sub { + my $run_for = shift; + CPU::exec( $run_for ); + $self->render_vram; + } ); + +=cut + +sub loop { + my $self = shift; + my $exec = shift; + + confess "need coderef as argument" unless ref($exec) eq 'CODE'; + my $event = SDL::Event->new(); + + while ( 1 ) { + $self->key_pressed( 1 ); + $exec->($run_for); + } +} + +=head1 SEE ALSO + +L is sample implementation using this module + +=head1 AUTHOR + +Dobrica Pavlinusic, C<< >> + +=head1 COPYRIGHT & LICENSE + +Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=cut 1;