--- Screen.pm 2007/08/04 14:13:28 124 +++ Screen.pm 2007/09/30 20:13:10 190 @@ -12,14 +12,51 @@ use Carp qw/confess/; use Data::Dump qw/dump/; -use M6502 qw'@mem'; + +use Exporter 'import'; +our @EXPORT = qw'$white $black @flip'; use base qw(Class::Accessor Prefs); -__PACKAGE__->mk_accessors(qw(app event)); +__PACKAGE__->mk_accessors(qw(app event screen_width screen_height window_width window_height)); =head1 NAME -Screen - simulated 256*256 pixels monochrome screen using SDL +Screen - simulated monochrome screen using SDL + +=head1 Architecture dependent + +You may override following methods if you want to implement keyboard on each +keypress event. Alternative is to use hook and trap memory access. + +=head2 screen_width + +Width of emulated screen (256 by default) + +=head2 screen_height + +Height of emulated screen (256 by default) + +=head2 key_down + + $self->key_down( 'a' ); + +=cut + +sub key_down {} + +=head2 key_up + + $self->key_up( 'a' ); + +=cut + +sub key_up {} + + +=head1 Architecture independent + +You don't need to override any of following function in your architecture, +but you might want to call them. =head2 open_screen @@ -39,56 +76,44 @@ warn "using default unscaled display\n"; } + $self->screen_width( 256 ) unless defined $self->screen_width; + $self->screen_height( 256 ) unless defined $self->screen_height; + + my $w = $self->screen_width * $self->scale + ( $self->show_mem ? 256 : 0 ); + $self->window_width( $w ); + + my $h = $self->screen_height; + # expand screen size to show whole 64k 256*256 memory map + $h = 256 if $self->show_mem && $h < 256; + $h *= $self->scale; + $self->window_height( $h ); + $app = SDL::App->new( - -width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), - -height => 256 * $self->scale, + -width => $w, + -height => $h, -depth => 16, + -flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL, ); #$app->grab_input( SDL_GRAB_QUERY ); $app->grab_input( SDL_GRAB_OFF ); + $app->title( ref($self) ); $self->app( $app ); my $event = SDL::Event->new(); $self->event( $event ); - warn "# created SDL::App\n"; + warn "# created SDL::App with screen ", $self->screen_width, "x", $self->screen_height, " in window ", + $self->window_width, "x", $self->window_height, "\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 ); -my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 ); - -=head2 p - - $screen->p( $x, $y, 1 ); - -=cut - -sub p { - my $self = shift; - - my ($x,$y,$w) = (@_); - - warn "p($x,$y,$w)\n" if $self->debug; - - my $scale = $self->scale; - my $rect = SDL::Rect->new( - -height => $scale, - -width => $scale, - -x => $x * $scale, - -y => $y * $scale, - ); - - $app->fill( $rect, $w ? $white : $black ); - $app->update( $rect ); -} - =head2 mem_xy Helper to return x and y coordinates in memory map @@ -101,7 +126,7 @@ my $self = shift; my $offset = shift; my $x = $offset & 0xff; - $x += 256 * $self->scale; + $x += $self->screen_width * $self->scale; my $y = $offset >> 8; return ($x,$y); } @@ -148,51 +173,37 @@ Render one frame of video ram - $self->render_vram( @video_memory ); + $self->render_vram; =cut -my @flip; +sub render_vram { + my $self = shift; -foreach my $i ( 0 .. 255 ) { - my $t = 0; - $i & 0b00000001 and $t = $t | 0b10000000; - $i & 0b00000010 and $t = $t | 0b01000000; - $i & 0b00000100 and $t = $t | 0b00100000; - $i & 0b00001000 and $t = $t | 0b00010000; - $i & 0b00010000 and $t = $t | 0b00001000; - $i & 0b00100000 and $t = $t | 0b00000100; - $i & 0b01000000 and $t = $t | 0b00000010; - $i & 0b10000000 and $t = $t | 0b00000001; - #warn "$i = $t\n"; - $flip[$i] = $t; + confess "please implement $self::render_vram"; } -sub render_vram { - my $self = shift; +=head2 render_frame - return unless $self->booted; +Render one frame of video ram - confess "no data?" unless (@_); - confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8)); + $self->render_frame( $vram_sdl_surface ); - my $pixels = pack("C*", map { $flip[$_] } @_); +=cut + +sub render_frame { + my $self = shift; + + my $vram = shift; + confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; - my $vram = SDL::Surface->new( - -width => 256, - -height => 256, - -depth => 1, # 1 bit per pixel - -pitch => 32, # bytes per line - -from => $pixels, - ); - $vram->set_colors( 0, $black, $white, $red ); $vram->display_format; - my $scale = $self->scale; + my $scale = $self->scale || confess "no scale?"; - 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 ); + my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => $self->screen_width * $scale, -height => $self->screen_height * $scale ); + my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => $self->screen_width * $scale, -height => $self->screen_height * $scale ); if ( $scale > 1 ) { use SDL::Tool::Graphic; @@ -206,9 +217,10 @@ $app->sync; } + =head2 render_mem - $self->render_mem( @ram ); + $self->render_mem( @mem ); =cut @@ -232,7 +244,9 @@ $vram->display_format; - my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); + my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => $self->screen_width, -height => $self->window_height ); + my $rect_mem = SDL::Rect->new( -x => $self->screen_width * $self->scale, -y => 0, -width => 256, -height => 256 ); + $vram->blit( $rect, $app, $rect_mem ); $app->sync; @@ -252,17 +266,9 @@ =cut my $pending_key; +my $key_active; 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; @@ -271,7 +277,22 @@ my $event = $self->event || confess "no event?"; - $event->poll || return $pending_key; + if ( ! $event->poll ) { + return $pending_key unless $self->can('session_event'); + if ( my $h = $self->session_event('key_pressed') ) { + my ( $key, $state ) = %$h; + if ( $state ) { + $pending_key = $key; + $self->key_down( $key ); + $key_active->{$key} = 1; + } else { + undef $pending_key; + $self->key_up( $key ); + $key_active->{$key} = 0; + } + } + return $pending_key; + } my $type = $event->type(); @@ -281,7 +302,6 @@ 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"; @@ -289,38 +309,95 @@ } else { warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; $pending_key = $k; + $self->key_down( $k ); + $key_active->{$k} = 1; + $self->record_session('key_pressed', { $k => 1 }); } } elsif ( $type == SDL_KEYUP ) { my $up = $event->key_name(); - $key_down->{$up} = 0; warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; + $self->key_up( $up ); + $key_active->{$up} = 0; + $self->record_session('key_pressed', { $up => 0 }); undef $pending_key; } - warn "key_pressed = $pending_key\n" if $pending_key; + warn "key_pressed = $pending_key\n" if ( $pending_key ); return $pending_key; } +=head2 key_active + +Is key currently pressed on keyboard or in session? + + $self->key_active( 'left shift', 'right shift', 'a' ); + +=cut + +sub key_active { + my $self = shift; + my @keys = @_; + confess "Regexp is no longer supported" if ref($_[0]) eq 'Regexp'; + + my $active = 0; + foreach my $key ( @keys ) { + $active++ if $key_active->{$key}; + } + + warn "## key_active(",dump(@keys),") = $active\n" if $active; + return $active; +} + =head2 loop -Implement SDL event 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 $event = SDL::Event->new(); + my $exec = shift; + confess "need coderef as argument" unless ref($exec) eq 'CODE'; + my $event = SDL::Event->new(); - MAIN_LOOP: while ( 1 ) { $self->key_pressed( 1 ); - M6502::exec($run_for); - $self->render_vram( @mem[ 0x6000 .. 0x7fff ] ); + $exec->($run_for); } } +=head2 @flip + +Exported helper array used to flip bytes (from character roms for example) + + my $flipped = $flip[ $byte ]; + +=cut + +our @flip; + +foreach my $i ( 0 .. 255 ) { + my $t = 0; + $i & 0b00000001 and $t = $t | 0b10000000; + $i & 0b00000010 and $t = $t | 0b01000000; + $i & 0b00000100 and $t = $t | 0b00100000; + $i & 0b00001000 and $t = $t | 0b00010000; + $i & 0b00010000 and $t = $t | 0b00001000; + $i & 0b00100000 and $t = $t | 0b00000100; + $i & 0b01000000 and $t = $t | 0b00000010; + $i & 0b10000000 and $t = $t | 0b00000001; + #warn "$i = $t\n"; + $flip[$i] = $t; +} + =head1 SEE ALSO L is sample implementation using this module @@ -337,4 +414,5 @@ under the same terms as Perl itself. =cut + 1;