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 ); |
58 |
warn "# created SDL::App\n"; |
warn "# created SDL::App\n"; |
59 |
} |
} |
60 |
|
|
61 |
my $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
our $white = SDL::Color->new( -r => 0xff, -g => 0xff, -b => 0xff ); |
62 |
my $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
our $black = SDL::Color->new( -r => 0x80, -g => 0x80, -b => 0x80 ); |
63 |
|
|
64 |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
my $red = SDL::Color->new( -r => 0xff, -g => 0x00, -b => 0x00 ); |
65 |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
my $green = SDL::Color->new( -r => 0x00, -g => 0xff, -b => 0x00 ); |
67 |
|
|
68 |
my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 ); |
my $rect_mem = SDL::Rect->new( -x => 256, -y => 0, -width => 256, -height => 256 ); |
69 |
|
|
|
=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 ); |
|
|
} |
|
|
|
|
70 |
=head2 mem_xy |
=head2 mem_xy |
71 |
|
|
72 |
Helper to return x and y coordinates in memory map |
Helper to return x and y coordinates in memory map |
126 |
|
|
127 |
Render one frame of video ram |
Render one frame of video ram |
128 |
|
|
129 |
$self->render_vram( @video_memory ); |
$self->render_vram; |
130 |
|
|
131 |
=cut |
=cut |
132 |
|
|
133 |
my @flip; |
sub render_vram { |
134 |
|
my $self = shift; |
135 |
|
|
136 |
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; |
|
137 |
} |
} |
138 |
|
|
139 |
|
|
140 |
sub render_vram { |
=head2 render_frame |
|
my $self = shift; |
|
141 |
|
|
142 |
return unless $self->booted; |
Render one frame of video ram |
143 |
|
|
144 |
confess "no data?" unless (@_); |
$self->render_frame( $vram_sdl_surface ); |
|
confess "screen size not 256*256/8 but ",($#_+1) unless (($#_+1) == (256*256/8)); |
|
145 |
|
|
146 |
my $pixels = pack("C*", map { $flip[$_] } @_); |
=cut |
147 |
|
|
148 |
|
sub render_frame { |
149 |
|
my $self = shift; |
150 |
|
|
151 |
|
my $vram = shift; |
152 |
|
confess "need SDL::Surface as argument" unless ref($vram) eq 'SDL::Surface'; |
153 |
|
|
|
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 ); |
|
154 |
$vram->display_format; |
$vram->display_format; |
155 |
|
|
156 |
my $scale = $self->scale; |
my $scale = $self->scale || confess "no scale?"; |
157 |
|
|
158 |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
my $rect = SDL::Rect->new( -x => 0, -y => 0, -width => 256 * $scale, -height => 256 * $scale ); |
159 |
my $rect_screen = 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 ); |
170 |
$app->sync; |
$app->sync; |
171 |
} |
} |
172 |
|
|
173 |
|
|
174 |
=head2 render_mem |
=head2 render_mem |
175 |
|
|
176 |
$self->render_mem( @ram ); |
$self->render_mem( @mem ); |
177 |
|
|
178 |
=cut |
=cut |
179 |
|
|
269 |
|
|
270 |
=head2 loop |
=head2 loop |
271 |
|
|
272 |
Implement SDL event loop |
Implement CPU run for C<$run_run> cycles inside SDL event loop |
273 |
|
|
274 |
|
$self->loop( sub { |
275 |
|
my $run_for = shift; |
276 |
|
CPU::exec( $run_for ); |
277 |
|
$self->render_vram; |
278 |
|
} ); |
279 |
|
|
280 |
=cut |
=cut |
281 |
|
|
282 |
sub loop { |
sub loop { |
283 |
my $self = shift; |
my $self = shift; |
284 |
my $event = SDL::Event->new(); |
my $exec = shift; |
285 |
|
|
286 |
|
confess "need coderef as argument" unless ref($exec) eq 'CODE'; |
287 |
|
my $event = SDL::Event->new(); |
288 |
|
|
|
MAIN_LOOP: |
|
289 |
while ( 1 ) { |
while ( 1 ) { |
290 |
$self->key_pressed( 1 ); |
$self->key_pressed( 1 ); |
291 |
M6502::exec($run_for); |
$exec->($run_for); |
|
$self->render_vram( @mem[ 0x6000 .. 0x7fff ] ); |
|
292 |
} |
} |
293 |
} |
} |
294 |
|
|