-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_momentum.pl
352 lines (232 loc) · 10.2 KB
/
08_momentum.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/local/bin/perl
# test _momentum
use strict;
use warnings;
use Test::More;
use Acme::6502;
use lib '.';
use symbols;
use Data::Dumper;
use Carp; $SIG{__DIE__} = sub { Carp::confess @_ };
use PadWalker;
my $debug = 0;
my $symbols = symbols::symbols('newbies.lst');
my $cpu = Acme::6502->new();
$cpu->load_rom( 'newbies.bin', 0xf000 );
sub run_cpu {
my @stop_symbols = @_;
my $cycles = 0;
$cpu->run(10000, sub {
my ($pc, $inst, $a, $x, $y, $s, $p) = @_;
my $name = name_that_location($pc);
diag sprintf "a = %s x = %s y = %x", $a, $x, $y if $debug;
diag $name . ':' if $name !~ m/unknown/ and $debug;
diag $symbols->source->[ $pc ] if $debug;
if( grep $pc == $_, @stop_symbols ) {
${ PadWalker::peek_my(1)->{'$ic'} } = 0;
}
});
return $cycles;
}
sub name_that_location {
my $loc = shift;
my %locations = reverse %$symbols;
return $locations{$loc} if $locations{$loc};
#return $locations{$loc-1} if $locations{$loc-1};
#return $locations{$loc-2} if $locations{$loc-2};
#return $locations{$loc-3} if $locations{$loc-3};
return 'unknown location';
}
#
# init
#
my $level0 = $symbols->level0 or die;
for my $sym (
# start Z, end Z, Y, color
1, 11, 0x1e, 0xe0, # 0 (0)
20, 25, 0x14, 0x60, # 1 (4)
30, 40, 0x18, 0x20, # 2 (8)
0, 0, 0, 0,
) {
$cpu->write_8( $level0++, $sym );
}
#
# test Z momentum not being enough to move the player forward one unit
#
$cpu->write_8( $symbols->playerz, 20 );
$cpu->write_8( $symbols->playery, 0x14+5 );
$cpu->write_8( $symbols->playerzlo, 0x70 );
$cpu->write_8( $symbols->playerzspeed, 0x05 );
$cpu->write_8( $symbols->playerylo, 0x00 );
$cpu->write_8( $symbols->playeryspeed, 0x00 );
$cpu->set_pc( $symbols->momentum0);
run_cpu( $symbols->momentum4);
is $cpu->read_8( $symbols->playerz), 20; # unchanged
is $cpu->read_8( $symbols->playery), 0x14+5; # unchanged
#
# test Z momentum moving the player forward one unit
#
$cpu->write_8( $symbols->playerz, 20 );
$cpu->write_8( $symbols->playery, 0x14+5 );
$cpu->write_8( $symbols->playerzlo, 0x70 );
$cpu->write_8( $symbols->playerzspeed, 0x70 ); # adding those together should carry and increase playerz from 20 to 21
$cpu->write_8( $symbols->playerylo, 0x00 );
$cpu->write_8( $symbols->playeryspeed, 0x00 );
$cpu->set_pc( $symbols->momentum0);
run_cpu( $symbols->momentum4);
is $cpu->read_8( $symbols->playerz), 21; # changed
is $cpu->read_8( $symbols->playery), 0x14+5; # unchanged
# diag sprintf "playerzlo: %x\n", $cpu->read_8( $symbols->playerzlo);
#
# test Z momentum moving the player backwards one unit
#
$cpu->write_8( $symbols->playerz, 20 );
$cpu->write_8( $symbols->playery, 0x14+5 );
$cpu->write_8( $symbols->playerzlo, 0x20 );
$cpu->write_8( $symbols->playerzspeed, 0xff - 0x20 );
$cpu->write_8( $symbols->playerylo, 0x00 );
$cpu->write_8( $symbols->playeryspeed, 0x00 );
$cpu->set_pc( $symbols->momentum0);
run_cpu( $symbols->momentum4);
is $cpu->read_8( $symbols->playerz), 19; # changed
is $cpu->read_8( $symbols->playery), 0x14+5; # unchanged
# diag sprintf "playerzlo: %x\n", $cpu->read_8( $symbols->playerzlo); # $ff
#
# test Y momentum moving the player forward one unit
#
$cpu->write_8( $symbols->playerz, 20 );
$cpu->write_8( $symbols->playery, 0x14+5 );
$cpu->write_8( $symbols->playerylo, 0x70 );
$cpu->write_8( $symbols->playeryspeed, 0x70 ); # adding those together should carry and increase playery from 25 to 26
$cpu->write_8( $symbols->playerzlo, 0x00 );
$cpu->write_8( $symbols->playerzspeed, 0x00 );
$cpu->set_pc( $symbols->momentum0);
run_cpu( $symbols->momentum4);
is $cpu->read_8( $symbols->playerz), 20; # unchanged
is $cpu->read_8( $symbols->playery), 0x14+6; # changed
#
# test Y momentum moving the player down one unit
#
# 20, 25, 0x14, 0x60, # 1 (4)
$cpu->write_8( $symbols->playerz, 20 );
$cpu->write_8( $symbols->playery, 0x14+5 ); # 5 units above this platform
$cpu->write_8( $symbols->playerylo, 0x30 );
$cpu->write_8( $symbols->playeryspeed, 0xff - 0x30 ); # eg -0x30
$cpu->write_8( $symbols->playerzlo, 0x00 );
$cpu->write_8( $symbols->playerzspeed, 0x00 );
$cpu->set_pc( $symbols->momentum0);
run_cpu( $symbols->momentum4);
is $cpu->read_8( $symbols->playerz), 20; # unchanged
is $cpu->read_8( $symbols->playery), 0x14+4; # changed
# diag sprintf "playerylo: %x\n", $cpu->read_8( $symbols->playerylo); # $ff
#
# test Y momentum failing to move us down one unit because we've collided with a platform
#
# start Z, end Z, Y, color
# 20, 25, 0x14, 0x60, # 1 (4)
$cpu->write_8( $symbols->SWCHA, 0xff ); # neither joystick is pushed any direction
$cpu->write_8( $symbols->playerz, 20 );
$cpu->write_8( $symbols->playery, 0x14+1 ); # one unit above the platform; this should keep us from being able to go down
$cpu->write_8( $symbols->playerzlo, 0 );
$cpu->write_8( $symbols->playerzspeed, 0 );
$cpu->write_8( $symbols->playerylo, 0x30 );
$cpu->write_8( $symbols->playeryspeed, 0xff - 0x31 ); # eg -0x31
$cpu->set_pc( $symbols->collisions );
# $debug = 1;
run_cpu( $symbols->collisions9a );
# $debug = 0;
is $cpu->read_8( $symbols->collision_platform ), 4, 'collision logic decided that we are standing on the second platform which has index 4';
diag "playeryspeed = ", $cpu->read_8( $symbols->playeryspeed );
# resume execution...
run_cpu( $symbols->momentum4 );
is $cpu->read_8( $symbols->playerz), 20; # unchanged
is $cpu->read_8( $symbols->playery), 0x14+1; # unchanged
is $cpu->read_8( $symbols->playeryspeed), 0; # downward momentum zero'd out
# diag sprintf "playerzlo: %x\n", $cpu->read_8( $symbols->playerzlo);
#
# test Y momentum failing to move up one unit because we're hitting our head on a platform
#
# start Z, end Z, Y, color
# 20, 25, 0x14, 0x60, # 1 (4)
$cpu->write_8( $symbols->SWCHA, 0xff ); # neither joystick is pushed any direction
$cpu->write_8( $symbols->playerz, 20 );
$cpu->write_8( $symbols->playery, 0x14-1 ); # one unit below the platform; this should keep us from being able to go up
$cpu->write_8( $symbols->playerzlo, 0 );
$cpu->write_8( $symbols->playerzspeed, 0 );
$cpu->write_8( $symbols->playerylo, 0x71 );
$cpu->write_8( $symbols->playeryspeed, 0x70 ); # adding these together should carry, but if/when 0x70 gets negated, 0x71 will be larger than it so we won't immediately go down
$cpu->set_x( 0 ); # collisions uses X to index which movable object it is currently working with
$cpu->set_pc( $symbols->collisions );
# $debug = 1;
run_cpu( $symbols->collisions9a );
# $debug = 0;
is $cpu->read_8( $symbols->collision_bits ), 0b00000001, 'collision logic decided that we are hitting our head and cannot go up';
# resume execution...
run_cpu( $symbols->momentum4 );
is $cpu->read_8( $symbols->playerz), 20; # unchanged
is $cpu->read_8( $symbols->playery), 0x14-1; # unchanged
is $cpu->read_8( $symbols->playeryspeed), 0xff - 0x70; # upwards momentum turned into downward momentum
#
# test Z momentum failing to move the player forward one unit when they run in to the front of a platform
#
# start Z, end Z, Y, color
# 20, 25, 0x14, 0x60, # 1 (4)
$cpu->write_8( $symbols->playerz, 20-1 );
$cpu->write_8( $symbols->playery, 0x14 );
$cpu->write_8( $symbols->playerzlo, 0x71 );
$cpu->write_8( $symbols->playerzspeed, 0x70 ); # adding those together should carry, but subtracting 0x70 from 0x71 (after we bounce off of the platform) should avoid rolling over
$cpu->write_8( $symbols->playerylo, 0x00 );
$cpu->write_8( $symbols->playeryspeed, 0x00 );
$cpu->set_x( 0 ); # collisions uses X to index which movable object it is currently working with
$cpu->set_pc( $symbols->collisions );
run_cpu( $symbols->collisions9a );
is $cpu->read_8( $symbols->collision_bits ), 0b00000010, 'collision logic decided that we are hitting our head and cannot go forward';
is $cpu->read_8( $symbols->collision_platform ), 0xff, 'not standing on anything';
$cpu->set_pc( $symbols->momentum0);
run_cpu( $symbols->momentum4);
is $cpu->read_8( $symbols->playerz), 20-1; # unchanged
is $cpu->read_8( $symbols->playery), 0x14; # unchanged
is $cpu->read_8( $symbols->playerzspeed), 0xff - 0x70; # forward momentum turned into backwards momentum
#
# test applying gravity when it doesn't make us hit terminal velocity
#
$cpu->write_8( $symbols->playeryspeed, 0 ); # not moving up or down, yet
$cpu->set_pc( $symbols->momentum4 );
$cpu->set_x( 0 ); # _momentum uses X to index which movable object it is currently working with
# $debug = 1;
run_cpu( $symbols->momentum9 );
# $debug = 0;
is $cpu->read_8( $symbols->playeryspeed ), 0xff;
# diag sprintf "after gravity, playeryspeed: %x\n", $cpu->read_8( $symbols->playeryspeed);
$cpu->set_pc( $symbols->momentum4 );
$cpu->set_x( 0 );
run_cpu( $symbols->momentum9 );
is $cpu->read_8( $symbols->playeryspeed ), 0xfe;
#
# test applying gravity when it does make us hit terminal velocity
#
my $terminal_velocity = $symbols->terminal_velocity; # constant
diag "terminal velocity = " . sprintf "%x", $terminal_velocity;
diag "terminal velocity = aka " . - ( 0xff - $terminal_velocity );
my $gravity = $symbols->gravity; # constant
diag "gravity = $gravity";
$cpu->write_8( $symbols->playeryspeed, $terminal_velocity + $gravity );
$cpu->set_pc( $symbols->momentum4 );
$cpu->set_x( 0 );
run_cpu( $symbols->momentum9 );
# diag sprintf "after gravity, playeryspeed: %x\n", $cpu->read_8( $symbols->playeryspeed);
is $cpu->read_8( $symbols->playeryspeed ), $terminal_velocity;
$cpu->set_pc( $symbols->momentum4 );
$cpu->set_x( 0 );
run_cpu( $symbols->momentum9 );
diag sprintf "after gravity, playeryspeed: %x\n", $cpu->read_8( $symbols->playeryspeed);
is $cpu->read_8( $symbols->playeryspeed ), $terminal_velocity - 1; # since the test is less-than, gravity gets applied one extra time to bring us to falling just faster than terminal velocity.
$cpu->set_pc( $symbols->momentum4 );
$cpu->set_x( 0 );
run_cpu( $symbols->momentum9 );
diag sprintf "after gravity, playeryspeed: %x\n", $cpu->read_8( $symbols->playeryspeed);
is $cpu->read_8( $symbols->playeryspeed ), $terminal_velocity - 1; # ... but then we don't accelerate any faster than one more than terminal velocity (or one less than, since it is a negative number)
#
#
#
done_testing();