13 |
use Carp qw/confess/; |
use Carp qw/confess/; |
14 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
15 |
|
|
16 |
|
use Exporter 'import'; |
17 |
|
our @EXPORT = qw'$white $black @flip'; |
18 |
|
|
19 |
use base qw(Class::Accessor Prefs); |
use base qw(Class::Accessor Prefs); |
20 |
__PACKAGE__->mk_accessors(qw(app event)); |
__PACKAGE__->mk_accessors(qw(app event screen_width screen_height)); |
21 |
|
|
22 |
=head1 NAME |
=head1 NAME |
23 |
|
|
24 |
Screen - simulated 256*256 pixels monochrome screen using SDL |
Screen - simulated monochrome screen using SDL |
25 |
|
|
26 |
|
=head1 Architecture dependent |
27 |
|
|
28 |
|
You may override following methods if you want to implement keyboard on each |
29 |
|
keypress event. Alternative is to use <read> hook and trap memory access. |
30 |
|
|
31 |
|
=head2 screen_width |
32 |
|
|
33 |
|
Width of emulated screen (256 by default) |
34 |
|
|
35 |
|
=head2 screen_height |
36 |
|
|
37 |
|
Height of emulated screen (256 by default) |
38 |
|
|
39 |
|
=head2 key_down |
40 |
|
|
41 |
|
$self->key_down( 'a' ); |
42 |
|
|
43 |
|
=cut |
44 |
|
|
45 |
|
sub key_down {} |
46 |
|
|
47 |
|
=head2 key_up |
48 |
|
|
49 |
|
$self->key_up( 'a' ); |
50 |
|
|
51 |
|
=cut |
52 |
|
|
53 |
|
sub key_up {} |
54 |
|
|
55 |
|
|
56 |
|
=head1 Architecture independent |
57 |
|
|
58 |
|
You don't need to override any of following function in your architecture, |
59 |
|
but you might want to call them. |
60 |
|
|
61 |
=head2 open_screen |
=head2 open_screen |
62 |
|
|
76 |
warn "using default unscaled display\n"; |
warn "using default unscaled display\n"; |
77 |
} |
} |
78 |
|
|
79 |
|
$self->screen_width( 256 ) unless defined $self->screen_width; |
80 |
|
$self->screen_height( 256 ) unless defined $self->screen_height; |
81 |
|
|
82 |
$app = SDL::App->new( |
$app = SDL::App->new( |
83 |
-width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), |
-width => $self->screen_width * $self->scale + ( $self->show_mem ? 256 : 0 ), |
84 |
-height => 256 * $self->scale, |
-height => $self->screen_height * $self->scale, |
85 |
-depth => 16, |
-depth => 16, |
86 |
|
-flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL, |
87 |
); |
); |
88 |
#$app->grab_input( SDL_GRAB_QUERY ); |
#$app->grab_input( SDL_GRAB_QUERY ); |
89 |
$app->grab_input( SDL_GRAB_OFF ); |
$app->grab_input( SDL_GRAB_OFF ); |
90 |
|
$app->title( ref($self) ); |
91 |
|
|
92 |
$self->app( $app ); |
$self->app( $app ); |
93 |
|
|
94 |
my $event = SDL::Event->new(); |
my $event = SDL::Event->new(); |
95 |
$self->event( $event ); |
$self->event( $event ); |
96 |
|
|
97 |
warn "# created SDL::App\n"; |
warn "# created SDL::App with screen ", $self->screen_width, "x", $self->screen_height, "\n"; |
98 |
} |
} |
99 |
|
|
100 |
my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
our $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
101 |
my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
our $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
102 |
|
|
103 |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
104 |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
105 |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
106 |
|
|
|
my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
|
|
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 ); |
|
|
} |
|
|
|
|
107 |
=head2 mem_xy |
=head2 mem_xy |
108 |
|
|
109 |
Helper to return x and y coordinates in memory map |
Helper to return x and y coordinates in memory map |
116 |
my $self = shift; |
my $self = shift; |
117 |
my $offset = shift; |
my $offset = shift; |
118 |
my $x = $offset & 0xff; |
my $x = $offset & 0xff; |
119 |
$x += 256 * $self->scale; |
$x += $self->screen_width * $self->scale; |
120 |
my $y = $offset >> 8; |
my $y = $offset >> 8; |
121 |
return ($x,$y); |
return ($x,$y); |
122 |
} |
} |
123 |
|
|
|
=head2 vram |
|
|
|
|
|
Push byte to video memory and draw it |
|
|
|
|
|
$screen->vram( $offset, $byte ); |
|
|
|
|
|
=cut |
|
|
|
|
|
my $_vram_counter; |
|
|
|
|
|
sub vram { |
|
|
my ( $self, $offset, $byte ) = @_; |
|
|
my $x = ( $offset % 32 ) << 3; |
|
|
my $y = $offset >> 5; |
|
|
my $mask = 1; |
|
|
my $scale = $self->scale; |
|
|
|
|
|
printf "## vram %04x %02x*%02x %02x\n", $offset, $x, $y, $byte if $self->trace; |
|
|
|
|
|
foreach ( 0 .. 7 ) { |
|
|
my $on = $byte & $mask; |
|
|
if ( $scale == 1 ) { |
|
|
$app->pixel( $x + $_, $y, $on ? $white : $black ); |
|
|
} else { |
|
|
$self->p($x + $_, $y, $on ); |
|
|
} |
|
|
$mask = $mask << 1; |
|
|
} |
|
|
|
|
|
$app->sync if ( $_vram_counter++ % 10 == 0 ); |
|
|
} |
|
|
|
|
124 |
=head2 mmap_pixel |
=head2 mmap_pixel |
125 |
|
|
126 |
Draw pixel in memory map |
Draw pixel in memory map |
159 |
$app->sync; |
$app->sync; |
160 |
} |
} |
161 |
|
|
162 |
=head2 render |
=head2 render_vram |
163 |
|
|
164 |
Render one frame of video ram |
Render one frame of video ram |
165 |
|
|
166 |
$self->render( @video_memory ); |
$self->render_vram; |
167 |
|
|
168 |
=cut |
=cut |
169 |
|
|
170 |
sub render { |
sub render_vram { |
171 |
my $self = shift; |
my $self = shift; |
172 |
|
|
173 |
die "this function isn't supported if scale isn't 1" unless $self->scale == 1; |
confess "please implement $self::render_vram"; |
174 |
|
} |
175 |
|
|
|
my $pixels = pack("C*", @_); |
|
176 |
|
|
177 |
my $vram = SDL::Surface->new( |
=head2 render_frame |
178 |
-width => 256, |
|
179 |
-height => 256, |
Render one frame of video ram |
180 |
-depth => 1, # 1 bit per pixel |
|
181 |
-pitch => 32, # bytes per line |
$self->render_frame( $vram_sdl_surface ); |
182 |
-from => $pixels, |
|
183 |
); |
=cut |
184 |
$vram->set_colors( 0, $black, $white, $red ); |
|
185 |
|
sub render_frame { |
186 |
|
my $self = shift; |
187 |
|
|
188 |
|
my $vram = shift; |
189 |
|
confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; |
190 |
|
|
191 |
$vram->display_format; |
$vram->display_format; |
192 |
|
|
193 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
my $scale = $self->scale || confess "no scale?"; |
194 |
$vram->blit( $rect, $app, $rect_screen ); |
|
195 |
|
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => $self->screen_width * $scale, -height => $self->screen_height * $scale |
196 |
|
); |
197 |
|
my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => $self->screen_width * $scale, -height => $self->screen_height * $scale ); |
198 |
|
|
199 |
|
if ( $scale > 1 ) { |
200 |
|
use SDL::Tool::Graphic; |
201 |
|
# last parametar is anti-alias |
202 |
|
my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 ); |
203 |
|
$zoomed->blit( $rect, $app, $rect_screen ); |
204 |
|
} else { |
205 |
|
$vram->blit( $rect, $app, $rect_screen ); |
206 |
|
} |
207 |
|
|
208 |
$app->sync; |
$app->sync; |
209 |
} |
} |
210 |
|
|
211 |
|
|
212 |
=head2 render_mem |
=head2 render_mem |
213 |
|
|
214 |
$self->render_mem( @ram ); |
$self->render_mem( @mem ); |
215 |
|
|
216 |
=cut |
=cut |
217 |
|
|
235 |
|
|
236 |
$vram->display_format; |
$vram->display_format; |
237 |
|
|
238 |
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->screen_height ); |
239 |
|
my $rect_mem = SDL::Rect->new( -x => $self->screen_width * $self->scale, -y => 0, -width => 256, -height => 256 ); |
240 |
|
|
241 |
$vram->blit( $rect, $app, $rect_mem ); |
$vram->blit( $rect, $app, $rect_mem ); |
242 |
|
|
243 |
$app->sync; |
$app->sync; |
257 |
=cut |
=cut |
258 |
|
|
259 |
my $pending_key; |
my $pending_key; |
260 |
|
my $key_active; |
261 |
my $run_for = 2000; |
my $run_for = 2000; |
262 |
|
|
263 |
sub key_pressed { |
sub key_pressed { |
268 |
|
|
269 |
my $event = $self->event || confess "no event?"; |
my $event = $self->event || confess "no event?"; |
270 |
|
|
271 |
$event->poll || return $pending_key; |
if ( ! $event->poll ) { |
272 |
|
return $pending_key unless $self->can('session_event'); |
273 |
|
if ( my $h = $self->session_event('key_pressed') ) { |
274 |
|
my ( $key, $state ) = %$h; |
275 |
|
if ( $state ) { |
276 |
|
$pending_key = $key; |
277 |
|
$self->key_down( $key ); |
278 |
|
$key_active->{$key} = 1; |
279 |
|
} else { |
280 |
|
undef $pending_key; |
281 |
|
$self->key_up( $key ); |
282 |
|
$key_active->{$key} = 0; |
283 |
|
} |
284 |
|
} |
285 |
|
return $pending_key; |
286 |
|
} |
287 |
|
|
288 |
my $type = $event->type(); |
my $type = $event->type(); |
289 |
|
|
300 |
} else { |
} else { |
301 |
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
302 |
$pending_key = $k; |
$pending_key = $k; |
303 |
|
$self->key_down( $k ); |
304 |
|
$key_active->{$k} = 1; |
305 |
|
$self->record_session('key_pressed', { $k => 1 }); |
306 |
} |
} |
307 |
} elsif ( $type == SDL_KEYUP ) { |
} elsif ( $type == SDL_KEYUP ) { |
308 |
my $up = $event->key_name(); |
my $up = $event->key_name(); |
309 |
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
310 |
|
$self->key_up( $up ); |
311 |
|
$key_active->{$up} = 0; |
312 |
|
$self->record_session('key_pressed', { $up => 0 }); |
313 |
undef $pending_key; |
undef $pending_key; |
314 |
} |
} |
315 |
|
|
316 |
warn "key_pressed = $pending_key\n" if $pending_key; |
warn "key_pressed = $pending_key\n" if ( $pending_key ); |
317 |
|
|
318 |
return $pending_key; |
return $pending_key; |
319 |
} |
} |
320 |
|
|
321 |
|
=head2 key_active |
322 |
|
|
323 |
|
Is key currently pressed on keyboard or in session? |
324 |
|
|
325 |
|
$self->key_active( 'left shift', 'right shift', 'a' ); |
326 |
|
|
327 |
|
=cut |
328 |
|
|
329 |
|
sub key_active { |
330 |
|
my $self = shift; |
331 |
|
my @keys = @_; |
332 |
|
confess "Regexp is no longer supported" if ref($_[0]) eq 'Regexp'; |
333 |
|
|
334 |
|
my $active = 0; |
335 |
|
foreach my $key ( @keys ) { |
336 |
|
$active++ if $key_active->{$key}; |
337 |
|
} |
338 |
|
|
339 |
|
warn "## key_active(",dump(@keys),") = $active\n" if $active; |
340 |
|
return $active; |
341 |
|
} |
342 |
|
|
343 |
=head2 loop |
=head2 loop |
344 |
|
|
345 |
Implement SDL event loop |
Implement CPU run for C<$run_run> cycles inside SDL event loop |
346 |
|
|
347 |
|
$self->loop( sub { |
348 |
|
my $run_for = shift; |
349 |
|
CPU::exec( $run_for ); |
350 |
|
$self->render_vram; |
351 |
|
} ); |
352 |
|
|
353 |
=cut |
=cut |
354 |
|
|
355 |
sub loop { |
sub loop { |
356 |
my $self = shift; |
my $self = shift; |
357 |
my $event = SDL::Event->new(); |
my $exec = shift; |
358 |
|
|
359 |
|
confess "need coderef as argument" unless ref($exec) eq 'CODE'; |
360 |
|
my $event = SDL::Event->new(); |
361 |
|
|
|
MAIN_LOOP: |
|
362 |
while ( 1 ) { |
while ( 1 ) { |
363 |
$self->key_pressed( 1 ); |
$self->key_pressed( 1 ); |
364 |
M6502::exec($run_for); |
$exec->($run_for); |
365 |
} |
} |
366 |
} |
} |
367 |
|
|
368 |
|
# helper array to flip bytes for display |
369 |
|
our @flip; |
370 |
|
|
371 |
|
foreach my $i ( 0 .. 255 ) { |
372 |
|
my $t = 0; |
373 |
|
$i & 0b00000001 and $t = $t | 0b10000000; |
374 |
|
$i & 0b00000010 and $t = $t | 0b01000000; |
375 |
|
$i & 0b00000100 and $t = $t | 0b00100000; |
376 |
|
$i & 0b00001000 and $t = $t | 0b00010000; |
377 |
|
$i & 0b00010000 and $t = $t | 0b00001000; |
378 |
|
$i & 0b00100000 and $t = $t | 0b00000100; |
379 |
|
$i & 0b01000000 and $t = $t | 0b00000010; |
380 |
|
$i & 0b10000000 and $t = $t | 0b00000001; |
381 |
|
#warn "$i = $t\n"; |
382 |
|
$flip[$i] = $t; |
383 |
|
} |
384 |
|
|
385 |
=head1 SEE ALSO |
=head1 SEE ALSO |
386 |
|
|
387 |
L<Orao> is sample implementation using this module |
L<Orao> is sample implementation using this module |
398 |
under the same terms as Perl itself. |
under the same terms as Perl itself. |
399 |
|
|
400 |
=cut |
=cut |
401 |
|
|
402 |
1; |
1; |