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'; |
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)); |
45 |
-width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), |
-width => 256 * $self->scale + ( $self->show_mem ? 256 : 0 ), |
46 |
-height => 256 * $self->scale, |
-height => 256 * $self->scale, |
47 |
-depth => 16, |
-depth => 16, |
48 |
|
-flags=>SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL, |
49 |
); |
); |
50 |
#$app->grab_input( SDL_GRAB_QUERY ); |
#$app->grab_input( SDL_GRAB_QUERY ); |
51 |
$app->grab_input( SDL_GRAB_OFF ); |
$app->grab_input( SDL_GRAB_OFF ); |
52 |
|
$app->title( ref($self) . ' ' . $self::VERSION ); |
53 |
|
|
54 |
$self->app( $app ); |
$self->app( $app ); |
55 |
|
|
59 |
warn "# created SDL::App\n"; |
warn "# created SDL::App\n"; |
60 |
} |
} |
61 |
|
|
62 |
my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
our $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
63 |
my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
our $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
64 |
|
|
65 |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
66 |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
67 |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
my $blue = SDL::Color->new( -r => 0x00, -g => 0x00, -b => 0xff ); |
68 |
|
|
|
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 ); |
|
|
} |
|
|
|
|
69 |
=head2 mem_xy |
=head2 mem_xy |
70 |
|
|
71 |
Helper to return x and y coordinates in memory map |
Helper to return x and y coordinates in memory map |
125 |
|
|
126 |
Render one frame of video ram |
Render one frame of video ram |
127 |
|
|
128 |
$self->render_vram( @video_memory ); |
$self->render_vram; |
129 |
|
|
130 |
=cut |
=cut |
131 |
|
|
132 |
my @flip; |
sub render_vram { |
133 |
|
my $self = shift; |
134 |
|
|
135 |
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; |
|
136 |
} |
} |
137 |
|
|
138 |
|
|
139 |
sub render_vram { |
=head2 render_frame |
|
my $self = shift; |
|
140 |
|
|
141 |
return unless $self->booted; |
Render one frame of video ram |
142 |
|
|
143 |
die "this function isn't supported if scale isn't 1" unless $self->scale == 1; |
$self->render_frame( $vram_sdl_surface ); |
144 |
|
|
145 |
confess "no data?" unless (@_); |
=cut |
|
confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8)); |
|
146 |
|
|
147 |
|
sub render_frame { |
148 |
|
my $self = shift; |
149 |
|
|
150 |
my $pixels = pack("C*", map { $flip[$_] } @_); |
my $vram = shift; |
151 |
|
confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; |
152 |
|
|
|
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 ); |
|
153 |
$vram->display_format; |
$vram->display_format; |
154 |
|
|
155 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
my $scale = $self->scale || confess "no scale?"; |
156 |
$vram->blit( $rect, $app, $rect_screen ); |
|
157 |
|
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
158 |
|
my $rect_screen = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
159 |
|
|
160 |
|
if ( $scale > 1 ) { |
161 |
|
use SDL::Tool::Graphic; |
162 |
|
# last parametar is anti-alias |
163 |
|
my $zoomed = SDL::Tool::Graphic->zoom( $vram, $self->scale, $self->scale, 1 ); |
164 |
|
$zoomed->blit( $rect, $app, $rect_screen ); |
165 |
|
} else { |
166 |
|
$vram->blit( $rect, $app, $rect_screen ); |
167 |
|
} |
168 |
|
|
169 |
$app->sync; |
$app->sync; |
170 |
} |
} |
171 |
|
|
172 |
|
|
173 |
=head2 render_mem |
=head2 render_mem |
174 |
|
|
175 |
$self->render_mem( @ram ); |
$self->render_mem( @mem ); |
176 |
|
|
177 |
=cut |
=cut |
178 |
|
|
196 |
|
|
197 |
$vram->display_format; |
$vram->display_format; |
198 |
|
|
199 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256, -height => 256 ); |
200 |
|
my $rect_mem = SDL::Rect->new( -x => 256 * $self->scale, -y => 0, -width => 256, -height => 256 ); |
201 |
|
|
202 |
$vram->blit( $rect, $app, $rect_mem ); |
$vram->blit( $rect, $app, $rect_mem ); |
203 |
|
|
204 |
$app->sync; |
$app->sync; |
237 |
|
|
238 |
my $event = $self->event || confess "no event?"; |
my $event = $self->event || confess "no event?"; |
239 |
|
|
240 |
$event->poll || return $pending_key; |
if ( ! $event->poll ) { |
241 |
|
if ( my $h = $self->session_event('key_pressed') ) { |
242 |
|
my ( $key, $state ) = %$h; |
243 |
|
if ( $state ) { |
244 |
|
$pending_key = $key; |
245 |
|
$key_down->{$key}++; |
246 |
|
} else { |
247 |
|
undef $pending_key; |
248 |
|
$key_down->{$key} = 0; |
249 |
|
} |
250 |
|
} |
251 |
|
return $pending_key; |
252 |
|
} |
253 |
|
|
254 |
my $type = $event->type(); |
my $type = $event->type(); |
255 |
|
|
267 |
} else { |
} else { |
268 |
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
warn "SDL_KEYDOWN ($type) = '$k'", $just_checking ? ' fake' : '', "\n"; |
269 |
$pending_key = $k; |
$pending_key = $k; |
270 |
|
$self->record_session('key_pressed', { $k => 1 }); |
271 |
} |
} |
272 |
} elsif ( $type == SDL_KEYUP ) { |
} elsif ( $type == SDL_KEYUP ) { |
273 |
my $up = $event->key_name(); |
my $up = $event->key_name(); |
|
$key_down->{$up} = 0; |
|
274 |
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
warn "SDL_KEYUP ($type) = '$up'", $just_checking ? ' fake' : '', "\n"; |
275 |
|
$self->record_session('key_pressed', { $up => 0 }); |
276 |
|
$key_down->{$up} = 0; |
277 |
undef $pending_key; |
undef $pending_key; |
278 |
} |
} |
279 |
|
|
280 |
warn "key_pressed = $pending_key\n" if $pending_key; |
warn "key_pressed = $pending_key\n" if ( $pending_key ); |
281 |
|
|
282 |
return $pending_key; |
return $pending_key; |
283 |
} |
} |
284 |
|
|
285 |
=head2 loop |
=head2 loop |
286 |
|
|
287 |
Implement SDL event loop |
Implement CPU run for C<$run_run> cycles inside SDL event loop |
288 |
|
|
289 |
|
$self->loop( sub { |
290 |
|
my $run_for = shift; |
291 |
|
CPU::exec( $run_for ); |
292 |
|
$self->render_vram; |
293 |
|
} ); |
294 |
|
|
295 |
=cut |
=cut |
296 |
|
|
297 |
sub loop { |
sub loop { |
298 |
my $self = shift; |
my $self = shift; |
299 |
my $event = SDL::Event->new(); |
my $exec = shift; |
300 |
|
|
301 |
|
confess "need coderef as argument" unless ref($exec) eq 'CODE'; |
302 |
|
my $event = SDL::Event->new(); |
303 |
|
|
|
MAIN_LOOP: |
|
304 |
while ( 1 ) { |
while ( 1 ) { |
305 |
$self->key_pressed( 1 ); |
$self->key_pressed( 1 ); |
306 |
M6502::exec($run_for); |
$exec->($run_for); |
|
$self->render_vram( @mem[ 0x6000 .. 0x7fff ] ); |
|
307 |
} |
} |
308 |
} |
} |
309 |
|
|
323 |
under the same terms as Perl itself. |
under the same terms as Perl itself. |
324 |
|
|
325 |
=cut |
=cut |
326 |
|
|
327 |
1; |
1; |