12 |
|
|
13 |
use Carp qw/confess/; |
use Carp qw/confess/; |
14 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
15 |
use M6502 qw'@mem'; |
|
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 window_width window_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 |
|
my $w = $self->screen_width * $self->scale + ( $self->show_mem ? 256 : 0 ); |
83 |
|
$self->window_width( $w ); |
84 |
|
|
85 |
|
my $h = $self->screen_height; |
86 |
|
# expand screen size to show whole 64k 256*256 memory map |
87 |
|
$h = 256 if $self->show_mem && $h < 256; |
88 |
|
$h *= $self->scale; |
89 |
|
$self->window_height( $h ); |
90 |
|
|
91 |
$app = SDL::App->new( |
$app = SDL::App->new( |
92 |
-width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), |
-width => $w, |
93 |
-height => 256 * $self->scale, |
-height => $h, |
94 |
-depth => 16, |
-depth => 16, |
95 |
|
-flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL, |
96 |
); |
); |
97 |
#$app->grab_input( SDL_GRAB_QUERY ); |
#$app->grab_input( SDL_GRAB_QUERY ); |
98 |
$app->grab_input( SDL_GRAB_OFF ); |
$app->grab_input( SDL_GRAB_OFF ); |
99 |
|
$app->title( ref($self) ); |
100 |
|
|
101 |
$self->app( $app ); |
$self->app( $app ); |
102 |
|
|
103 |
my $event = SDL::Event->new(); |
my $event = SDL::Event->new(); |
104 |
$self->event( $event ); |
$self->event( $event ); |
105 |
|
|
106 |
warn "# created SDL::App\n"; |
warn "# created SDL::App with screen ", $self->screen_width, "x", $self->screen_height, " in window ", |
107 |
|
$self->window_width, "x", $self->window_height, "\n"; |
108 |
} |
} |
109 |
|
|
110 |
my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
our $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
111 |
my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
our $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
112 |
|
|
113 |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
114 |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
115 |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
116 |
|
|
|
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 ); |
|
|
} |
|
|
|
|
117 |
=head2 mem_xy |
=head2 mem_xy |
118 |
|
|
119 |
Helper to return x and y coordinates in memory map |
Helper to return x and y coordinates in memory map |
126 |
my $self = shift; |
my $self = shift; |
127 |
my $offset = shift; |
my $offset = shift; |
128 |
my $x = $offset & 0xff; |
my $x = $offset & 0xff; |
129 |
$x += 256 * $self->scale; |
$x += $self->screen_width * $self->scale; |
130 |
my $y = $offset >> 8; |
my $y = $offset >> 8; |
131 |
return ($x,$y); |
return ($x,$y); |
132 |
} |
} |
173 |
|
|
174 |
Render one frame of video ram |
Render one frame of video ram |
175 |
|
|
176 |
$self->render_vram( @video_memory ); |
$self->render_vram; |
177 |
|
|
178 |
=cut |
=cut |
179 |
|
|
180 |
my @flip; |
sub render_vram { |
181 |
|
my $self = shift; |
182 |
|
|
183 |
foreach my $i ( 0 .. 255 ) { |
confess "please implement $self::render_vram"; |
|
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; |
|
184 |
} |
} |
185 |
|
|
186 |
|
|
187 |
sub render_vram { |
=head2 render_frame |
|
my $self = shift; |
|
188 |
|
|
189 |
return unless $self->booted; |
Render one frame of video ram |
190 |
|
|
191 |
die "this function isn't supported if scale isn't 1" unless $self->scale == 1; |
$self->render_frame( $vram_sdl_surface ); |
192 |
|
|
193 |
confess "no data?" unless (@_); |
=cut |
|
confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8)); |
|
194 |
|
|
195 |
|
sub render_frame { |
196 |
|
my $self = shift; |
197 |
|
|
198 |
my $pixels = pack("C*", map { $flip[$_] } @_); |
my $vram = shift; |
199 |
|
confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; |
200 |
|
|
|
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 ); |
|
201 |
$vram->display_format; |
$vram->display_format; |
202 |
|
|
203 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
my $scale = $self->scale || confess "no scale?"; |
204 |
$vram->blit( $rect, $app, $rect_screen ); |
|
205 |
|
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => $self->screen_width * $scale, -height => $self->screen_height * $scale ); |
206 |
|
my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => $self->screen_width * $scale, -height => $self->screen_height * $scale ); |
207 |
|
|
208 |
|
if ( $scale > 1 ) { |
209 |
|
use SDL::Tool::Graphic; |
210 |
|
# last parametar is anti-alias |
211 |
|
my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 ); |
212 |
|
$zoomed->blit( $rect, $app, $rect_screen ); |
213 |
|
} else { |
214 |
|
$vram->blit( $rect, $app, $rect_screen ); |
215 |
|
} |
216 |
|
|
217 |
$app->sync; |
$app->sync; |
218 |
} |
} |
219 |
|
|
220 |
|
|
221 |
=head2 render_mem |
=head2 render_mem |
222 |
|
|
223 |
$self->render_mem( @ram ); |
$self->render_mem( @mem ); |
224 |
|
|
225 |
=cut |
=cut |
226 |
|
|
244 |
|
|
245 |
$vram->display_format; |
$vram->display_format; |
246 |
|
|
247 |
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 ); |
248 |
|
my $rect_mem = SDL::Rect->new( -x => $self->screen_width * $self->scale, -y => 0, -width => 256, -height => 256 ); |
249 |
|
|
250 |
$vram->blit( $rect, $app, $rect_mem ); |
$vram->blit( $rect, $app, $rect_mem ); |
251 |
|
|
252 |
$app->sync; |
$app->sync; |
266 |
=cut |
=cut |
267 |
|
|
268 |
my $pending_key; |
my $pending_key; |
269 |
|
my $key_active; |
270 |
my $run_for = 2000; |
my $run_for = 2000; |
271 |
|
|
|
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}; |
|
|
} |
|
|
|
|
272 |
sub key_pressed { |
sub key_pressed { |
273 |
my $self = shift; |
my $self = shift; |
274 |
|
|
277 |
|
|
278 |
my $event = $self->event || confess "no event?"; |
my $event = $self->event || confess "no event?"; |
279 |
|
|
280 |
$event->poll || return $pending_key; |
if ( ! $event->poll ) { |
281 |
|
return $pending_key unless $self->can('session_event'); |
282 |
|
if ( my $h = $self->session_event('key_pressed') ) { |
283 |
|
my ( $key, $state ) = %$h; |
284 |
|
if ( $state ) { |
285 |
|
$pending_key = $key; |
286 |
|
$self->key_down( $key ); |
287 |
|
$key_active->{$key} = 1; |
288 |
|
} else { |
289 |
|
undef $pending_key; |
290 |
|
$self->key_up( $key ); |
291 |
|
$key_active->{$key} = 0; |
292 |
|
} |
293 |
|
} |
294 |
|
return $pending_key; |
295 |
|
} |
296 |
|
|
297 |
my $type = $event->type(); |
my $type = $event->type(); |
298 |
|
|
302 |
|
|
303 |
if ($type == SDL_KEYDOWN) { |
if ($type == SDL_KEYDOWN) { |
304 |
$k = $event->key_name(); |
$k = $event->key_name(); |
|
$key_down->{$k}++; |
|
305 |
if ( $k eq 'escape' ) { |
if ( $k eq 'escape' ) { |
306 |
$run_for = $self->cli; |
$run_for = $self->cli; |
307 |
warn "will check event loop every $run_for cycles\n"; |
warn "will check event loop every $run_for cycles\n"; |
309 |
} else { |
} else { |
310 |
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
311 |
$pending_key = $k; |
$pending_key = $k; |
312 |
|
$self->key_down( $k ); |
313 |
|
$key_active->{$k} = 1; |
314 |
|
$self->record_session('key_pressed', { $k => 1 }); |
315 |
} |
} |
316 |
} elsif ( $type == SDL_KEYUP ) { |
} elsif ( $type == SDL_KEYUP ) { |
317 |
my $up = $event->key_name(); |
my $up = $event->key_name(); |
|
$key_down->{$up} = 0; |
|
318 |
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
319 |
|
$self->key_up( $up ); |
320 |
|
$key_active->{$up} = 0; |
321 |
|
$self->record_session('key_pressed', { $up => 0 }); |
322 |
undef $pending_key; |
undef $pending_key; |
323 |
} |
} |
324 |
|
|
325 |
warn "key_pressed = $pending_key\n" if $pending_key; |
warn "key_pressed = $pending_key\n" if ( $pending_key ); |
326 |
|
|
327 |
return $pending_key; |
return $pending_key; |
328 |
} |
} |
329 |
|
|
330 |
|
=head2 key_active |
331 |
|
|
332 |
|
Is key currently pressed on keyboard or in session? |
333 |
|
|
334 |
|
$self->key_active( 'left shift', 'right shift', 'a' ); |
335 |
|
|
336 |
|
=cut |
337 |
|
|
338 |
|
sub key_active { |
339 |
|
my $self = shift; |
340 |
|
my @keys = @_; |
341 |
|
confess "Regexp is no longer supported" if ref($_[0]) eq 'Regexp'; |
342 |
|
|
343 |
|
my $active = 0; |
344 |
|
foreach my $key ( @keys ) { |
345 |
|
$active++ if $key_active->{$key}; |
346 |
|
} |
347 |
|
|
348 |
|
warn "## key_active(",dump(@keys),") = $active\n" if $active; |
349 |
|
return $active; |
350 |
|
} |
351 |
|
|
352 |
=head2 loop |
=head2 loop |
353 |
|
|
354 |
Implement SDL event loop |
Implement CPU run for C<$run_run> cycles inside SDL event loop |
355 |
|
|
356 |
|
$self->loop( sub { |
357 |
|
my $run_for = shift; |
358 |
|
CPU::exec( $run_for ); |
359 |
|
$self->render_vram; |
360 |
|
} ); |
361 |
|
|
362 |
=cut |
=cut |
363 |
|
|
364 |
sub loop { |
sub loop { |
365 |
my $self = shift; |
my $self = shift; |
366 |
my $event = SDL::Event->new(); |
my $exec = shift; |
367 |
|
|
368 |
|
confess "need coderef as argument" unless ref($exec) eq 'CODE'; |
369 |
|
my $event = SDL::Event->new(); |
370 |
|
|
|
MAIN_LOOP: |
|
371 |
while ( 1 ) { |
while ( 1 ) { |
372 |
$self->key_pressed( 1 ); |
$self->key_pressed( 1 ); |
373 |
M6502::exec($run_for); |
$exec->($run_for); |
|
$self->render_vram( @mem[ 0x6000 .. 0x7fff ] ); |
|
374 |
} |
} |
375 |
} |
} |
376 |
|
|
377 |
|
# helper array to flip bytes for display |
378 |
|
our @flip; |
379 |
|
|
380 |
|
foreach my $i ( 0 .. 255 ) { |
381 |
|
my $t = 0; |
382 |
|
$i & 0b00000001 and $t = $t | 0b10000000; |
383 |
|
$i & 0b00000010 and $t = $t | 0b01000000; |
384 |
|
$i & 0b00000100 and $t = $t | 0b00100000; |
385 |
|
$i & 0b00001000 and $t = $t | 0b00010000; |
386 |
|
$i & 0b00010000 and $t = $t | 0b00001000; |
387 |
|
$i & 0b00100000 and $t = $t | 0b00000100; |
388 |
|
$i & 0b01000000 and $t = $t | 0b00000010; |
389 |
|
$i & 0b10000000 and $t = $t | 0b00000001; |
390 |
|
#warn "$i = $t\n"; |
391 |
|
$flip[$i] = $t; |
392 |
|
} |
393 |
|
|
394 |
=head1 SEE ALSO |
=head1 SEE ALSO |
395 |
|
|
396 |
L<Orao> is sample implementation using this module |
L<Orao> is sample implementation using this module |
407 |
under the same terms as Perl itself. |
under the same terms as Perl itself. |
408 |
|
|
409 |
=cut |
=cut |
410 |
|
|
411 |
1; |
1; |