add missing tools/pngdump to the repo
[gbajam22] / src / gamescr.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "gbaregs.h"
4 #include "game.h"
5 #include "dma.h"
6 #include "util.h"
7 #include "intr.h"
8 #include "input.h"
9 #include "gba.h"
10 #include "sprite.h"
11 #include "timer.h"
12 #include "debug.h"
13 #include "voxscape.h"
14 #include "data.h"
15 #include "scoredb.h"
16
17 #define POS_MASK        ((VOX_SZ << 16) - 1)
18
19 #define FOV             30
20 #define NEAR    2
21 #define FAR             85
22
23 #define P_RATE  250
24 #define E_RATE  3000
25 #define SHOT_TIME       50
26
27 #define ENEMY_VIS_RANGE (2 * FAR / 3)
28
29 static int gamescr_start(void);
30 static void gamescr_stop(void);
31 static void gamescr_frame(void);
32 static void gamescr_vblank(void);
33
34 static int update(void);
35 static void draw(void);
36 static void victory(void);
37
38 static struct screen gamescr = {
39         "game",
40         gamescr_start,
41         gamescr_stop,
42         gamescr_frame,
43         gamescr_vblank
44 };
45
46 #define ENEMY_ENERGY    4
47 struct enemy {
48         struct vox_object vobj;
49         short hp;
50         unsigned char anm;
51         signed char shot_frame;
52         int last_shot;
53 };
54 #define ENEMY_VALID(e)  ((e)->anm != 0)
55 #define NUM_SHOT_FRAMES 16
56
57 #define SFRM_LVL1       5
58 #define SFRM_LVL2       12
59
60 static uint16_t *framebuf;
61
62 static int nframes, backbuf;
63 static uint16_t *vram[] = { gba_vram_lfb0, gba_vram_lfb1 };
64
65 static int32_t pos[2], angle, horizon = 80;
66 static long last_shot, hitfrm;
67 static int hit_px, hit_py;
68 static int pheight;
69
70 #define COLOR_HORIZON   192
71 #define COLOR_ZENITH    255
72
73 #define MAX_SPR         48
74 static uint16_t oam[4 * MAX_SPR];
75 static int dynspr_base, dynspr_count;
76
77
78 #define MAX_ENEMIES             (255 - CMAP_SPAWN0)
79 static struct enemy enemies[MAX_ENEMIES];
80 static int num_kills, total_enemies;
81 static int energy;
82 #define MAX_ENERGY      5
83
84 static int score, gameover;
85 static unsigned long total_time, start_time;
86 static int running;
87
88 #define XFORM_PIXEL_X(x, y)     (xform_ca * (x) - xform_sa * (y) + (120 << 8))
89 #define XFORM_PIXEL_Y(x, y)     (xform_sa * (x) + xform_ca * (y) + (80 << 8))
90 static int32_t xform_sa, xform_ca;      /* for viewport bank/zoom */
91 static int xform_s;
92
93 static short vblcount;
94 static void *prev_iwram_top;
95
96 static int hit_frame;
97 static uint16_t color0;
98
99 static inline void xform_pixel(int *xp, int *yp);
100
101
102 struct screen *init_game_screen(void)
103 {
104         return &gamescr;
105 }
106
107 static void setup_palette(void)
108 {
109         int i;
110         unsigned char *cmap = gba_colors ? color_gba_cmap : color_cmap;
111
112         for(i=0; i<256; i++) {
113                 int r = cmap[i * 3];
114                 int g = cmap[i * 3 + 1];
115                 int b = cmap[i * 3 + 2];
116                 gba_bgpal[i] = RGB555(r, g, b);
117         }
118
119         color0 = gba_bgpal[0];
120 }
121
122 static int gamescr_start(void)
123 {
124         int i, j, sidx;
125         uint8_t *cptr;
126         struct enemy *enemy;
127
128         prev_iwram_top = iwram_sbrk(0);
129
130         gba_setmode(4, DISPCNT_BG2 | DISPCNT_OBJ | DISPCNT_FB1);
131         fillblock_16byte(gba_vram_lfb1, 0, 240 * 160 / 16);
132
133         vblperf_setcolor(0);
134
135         pos[0] = pos[1] = VOX_SZ << 15;
136         angle = 0x8000;
137         last_shot = -P_RATE - 1;
138
139         vox_init(VOX_SZ, VOX_SZ, height_pixels, color_pixels);
140         vox_proj(FOV, NEAR, FAR);
141         pheight = vox_view(pos[0], pos[1], -40, angle);
142
143         /* setup color image palette */
144         setup_palette();
145
146         spr_setup(16, 16, spr_game_pixels, gba_colors ? spr_game_gba_cmap : spr_game_cmap);
147         wait_vblank();
148         spr_clear();
149
150         for(i=0; i<MAX_SPR; i++) {
151                 spr_oam_clear(oam, i);
152         }
153
154         sidx = 0;
155         spr_oam(oam, sidx++, SPRID_CROSS, 120-8, 80-8, SPR_SZ16 | SPR_256COL);
156         spr_oam(oam, sidx++, SPRID_UIMID, 0, 144, SPR_VRECT | SPR_256COL);
157         spr_oam(oam, sidx++, SPRID_UIRIGHT, 48, 144, SPR_SZ16 | SPR_256COL);
158         spr_oam(oam, sidx++, SPRID_UILEFT, 168, 144, SPR_SZ16 | SPR_256COL);
159         spr_oam(oam, sidx++, SPRID_UITGT, 184, 144, SPR_SZ16 | SPR_256COL);
160         spr_oam(oam, sidx++, SPRID_UISLASH, 216, 144, SPR_VRECT | SPR_256COL);
161         dynspr_base = sidx;
162
163         num_kills = total_enemies = 0;
164         energy = 5;
165
166         memset(enemies, 0, sizeof enemies);
167         cptr = color_pixels;
168         for(i=0; i<VOX_SZ; i++) {
169                 for(j=0; j<VOX_SZ; j++) {
170                         if(*cptr == 255) {
171                                 /* player spawn point */
172                                 pos[0] = j << 16;
173                                 pos[1] = i << 16;
174
175                         } else if(*cptr >= CMAP_SPAWN0 && *cptr != 255) {
176                                 /* enemy spawn point */
177                                 int idx = *cptr - CMAP_SPAWN0;
178                                 enemy = enemies + idx;
179                                 if(enemy->anm) {
180                                         panic(get_pc(), "double spawn %d at %d,%d (prev: %d,%d)", idx,
181                                                         j, i, enemy->vobj.x, enemy->vobj.y);
182                                 }
183                                 enemy->vobj.x = j;
184                                 enemy->vobj.y = i;
185                                 enemy->vobj.px = -1;
186                                 enemy->anm = 0xff;
187                                 enemy->hp = ENEMY_ENERGY;
188                                 enemy->last_shot = -1;
189                                 enemy->shot_frame = -1;
190                                 if(++total_enemies >= MAX_ENEMIES) {
191                                         goto endspawn;
192                                 }
193                         }
194                         cptr++;
195                 }
196         }
197 endspawn:
198         /* check continuity */
199         for(i=0; i<total_enemies; i++) {
200                 if(enemies[i].anm <= 0) {
201                         panic(get_pc(), "discontinuous enemy list");
202                 }
203                 enemies[i].anm = rand() & 7;
204         }
205
206         vox_objects((struct vox_object*)enemies, total_enemies, sizeof *enemies);
207
208         energy = MAX_ENERGY;
209         xform_sa = 0;
210         xform_ca = 0x10000;
211         xform_s = 0x100;
212
213         gameover = 0;
214         score = -1;
215         total_time = 0;
216         start_time = timer_msec;
217
218         hitfrm = 0;
219
220         vblcount = 0;
221         nframes = 0;
222
223         running = 1;
224         return 0;
225 }
226
227 static void gamescr_stop(void)
228 {
229         running = 0;
230
231         iwram_brk(prev_iwram_top);
232
233         wait_vblank();
234         /* clear sprites */
235         spr_clear();
236         /* reset background rot/scale state */
237         REG_BG2X = 0;
238         REG_BG2Y = 0;
239         REG_BG2PA = 0x100;
240         REG_BG2PB = 0;
241         REG_BG2PC = 0;
242         REG_BG2PD = 0x100;
243
244 }
245
246 static void gamescr_frame(void)
247 {
248         backbuf = ++nframes & 1;
249         framebuf = vram[backbuf];
250
251         vox_framebuf(240, 160, framebuf, horizon);
252
253         if(update() == -1) {
254                 return;
255         }
256         draw();
257
258         vblperf_end();
259         wait_vblank();
260         present(backbuf);
261
262         /*
263         if(!(nframes & 15)) {
264                 emuprint("vbl: %d", vblperf_count);
265         }*/
266 #ifdef VBLBAR
267         vblperf_begin();
268 #else
269         vblperf_count = 0;
270 #endif
271 }
272
273 #define NS(x)   (SPRID_UINUM + ((x) << 1))
274 static int numspr[][2] = {
275         {NS(0),NS(0)}, {NS(0),NS(1)}, {NS(0),NS(2)}, {NS(0),NS(3)}, {NS(0),NS(4)},
276         {NS(0),NS(5)}, {NS(0),NS(6)}, {NS(0),NS(7)}, {NS(0),NS(8)}, {NS(0),NS(9)},
277         {NS(1),NS(0)}, {NS(1),NS(1)}, {NS(1),NS(2)}, {NS(1),NS(3)}, {NS(1),NS(4)},
278         {NS(1),NS(5)}, {NS(1),NS(6)}, {NS(1),NS(7)}, {NS(1),NS(8)}, {NS(1),NS(9)},
279         {NS(2),NS(0)}, {NS(2),NS(1)}, {NS(2),NS(2)}, {NS(2),NS(3)}, {NS(2),NS(4)},
280         {NS(2),NS(5)}, {NS(2),NS(6)}, {NS(2),NS(7)}, {NS(2),NS(8)}, {NS(2),NS(9)},
281         {NS(3),NS(0)}, {NS(3),NS(1)}, {NS(3),NS(2)}, {NS(3),NS(3)}, {NS(3),NS(4)},
282         {NS(3),NS(5)}, {NS(3),NS(6)}, {NS(3),NS(7)}, {NS(3),NS(8)}, {NS(3),NS(9)}
283 };
284
285 #define WALK_SPEED      0x40000
286 #define TURN_SPEED      0x200
287 #define ELEV_SPEED      8
288
289 #define MAX(a, b)       ((a > (b) ? (a) : (b)))
290
291 static int update(void)
292 {
293         int32_t fwd[2], right[2];
294         int i, snum, ledspr;
295         struct enemy *enemy;
296         int did_strafe = 0;
297
298         hit_frame = 0;
299
300         update_keyb();
301
302         if(KEYPRESS(BN_START)) {
303                 /* TODO pause menu */
304                 change_screen(find_screen("menu"));
305                 return -1;
306         }
307
308         if(gameover) {
309                 goto skip_game_logic;
310         }
311
312         if(keystate) {
313                 if(keystate & BN_LEFT) {
314                         angle += TURN_SPEED;
315                 }
316                 if(keystate & BN_RIGHT) {
317                         angle -= TURN_SPEED;
318                 }
319
320                 fwd[0] = -SIN(angle);
321                 fwd[1] = COS(angle);
322                 right[0] = fwd[1];
323                 right[1] = -fwd[0];
324
325                 if(keystate & BN_A) {
326                         pos[0] = (pos[0] + fwd[0]) & POS_MASK;
327                         pos[1] = (pos[1] + fwd[1]) & POS_MASK;
328                         if(pos[0] < 0) pos[0] += VOX_SZ << 16;
329                         if(pos[1] < 0) pos[1] += VOX_SZ << 16;
330                 }
331
332                 if((keystate & BN_B) && (timer_msec - last_shot >= P_RATE)) {
333                         last_shot = timer_msec;
334                         for(i=0; i<total_enemies; i++) {
335                                 if(enemies[i].hp && enemies[i].vobj.px >= 0) {
336                                         int dx = enemies[i].vobj.px - 120;
337                                         int dy = enemies[i].vobj.py - 80;
338                                         int rad = enemies[i].vobj.scale >> 5;
339
340                                         if(rad < 1) rad = 1;
341
342                                         if(abs(dx) < rad && abs(dy) < (rad << 1)) {
343                                                 enemies[i].shot_frame = -1;
344                                                 if(--enemies[i].hp <= 0) {
345                                                         if(++num_kills >= total_enemies) {
346                                                                 victory();
347                                                         }
348                                                 }
349                                                 hit_px = enemies[i].vobj.px;
350                                                 hit_py = enemies[i].vobj.py;
351                                                 hitfrm = nframes;
352                                                 break;
353                                         }
354                                 }
355                         }
356                 }
357                 if(keystate & BN_UP) {
358                         if(horizon > 40) horizon -= ELEV_SPEED;
359                 }
360                 if(keystate & BN_DOWN) {
361                         if(horizon < 200 - ELEV_SPEED) horizon += ELEV_SPEED;
362                 }
363                 if(keystate & BN_RT) {
364                         pos[0] = (pos[0] + right[0]) & POS_MASK;
365                         pos[1] = (pos[1] + right[1]) & POS_MASK;
366                         if(pos[0] < 0) pos[0] += VOX_SZ << 16;
367                         if(pos[1] < 0) pos[1] += VOX_SZ << 16;
368                         did_strafe = 1;
369                 }
370                 if(keystate & BN_LT) {
371                         pos[0] = (pos[0] - right[0]) & POS_MASK;
372                         pos[1] = (pos[1] - right[1]) & POS_MASK;
373                         if(pos[0] < 0) pos[0] += VOX_SZ << 16;
374                         if(pos[1] < 0) pos[1] += VOX_SZ << 16;
375                         did_strafe = 1;
376                 }
377
378                 pheight = vox_view(pos[0], pos[1], -40, angle);
379         }
380
381         /* enemy logic */
382         enemy = enemies;
383         for(i=0; i<total_enemies; i++) {
384                 /* only consider enemies which are not dead */
385                 if(enemy->hp <= 0) {
386                         enemy++;
387                         continue;
388                 }
389
390                 if(enemy->shot_frame >= 0) {
391                         /* in the process of charging a shot */
392                         if(++enemy->shot_frame >= NUM_SHOT_FRAMES - 1) {
393                                 /* only get hit if we can see the enemy and we didn't strafe */
394                                 if(!did_strafe && enemy->vobj.px >= 0) {
395                                         hit_frame = 1;
396                                         if(--energy <= 0) {
397                                                 gameover = 1;
398                                         }
399                                 }
400                                 enemy->shot_frame = -1;
401                         }
402                 } else if(enemy->vobj.px >= 0) {
403                         /* check rate of fire and start a shot if necessary */
404                         if(enemy->last_shot == -1) {
405                                 enemy->last_shot = timer_msec;
406                         } else if(timer_msec - enemy->last_shot >= E_RATE) {
407                                 enemy->last_shot = timer_msec;
408                                 enemy->shot_frame = 0;
409                         }
410                 }
411                 enemy++;
412         }
413
414 skip_game_logic:
415
416         snum = 0;
417         /* turrets number */
418         spr_oam(oam, dynspr_base + snum++, numspr[num_kills][0], 200, 144, SPR_VRECT | SPR_256COL);
419         spr_oam(oam, dynspr_base + snum++, numspr[num_kills][1], 208, 144, SPR_VRECT | SPR_256COL);
420         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][0], 224, 144, SPR_VRECT | SPR_256COL);
421         spr_oam(oam, dynspr_base + snum++, numspr[total_enemies][1], 232, 144, SPR_VRECT | SPR_256COL);
422         /* energy bar */
423         if(energy == MAX_ENERGY) {
424                 ledspr = SPRID_LEDBLU;
425         } else {
426                 ledspr = energy > 2 ? SPRID_LEDGRN : SPRID_LEDRED;
427         }
428         for(i=0; i<5; i++) {
429                 spr_oam(oam, dynspr_base + snum++, i >= energy ? SPRID_LEDOFF : ledspr,
430                                 8 + (i << 3), 144, SPR_VRECT | SPR_256COL);
431         }
432         /* blaster sprites */
433         if(timer_msec - last_shot <= SHOT_TIME) {
434                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, -8, 118, SPR_SZ32 | SPR_256COL);
435                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 22, 103, SPR_SZ32 | SPR_256COL);
436                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 54, 88, SPR_SZ32 | SPR_256COL);
437                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 86, 72, SPR_SZ32 | SPR_256COL);
438
439                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS0, 240 + 8 - 32, 118, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
440                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS1, 240 - 22 - 32, 103, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
441                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS2, 240 - 54 - 32, 88, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
442                 spr_oam(oam, dynspr_base + snum++, SPRID_LAS3, 240 - 86 - 32, 72, SPR_SZ32 | SPR_256COL | SPR_HFLIP);
443         }
444         /* hit sparks */
445         if(nframes - hitfrm < 5) {
446                 int id = SPRID_SPARK0 + (nframes - hitfrm);
447                 spr_oam(oam, dynspr_base + snum++, id, hit_px - 16, hit_py - 16,
448                                 SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
449         }
450         /* enemy sprites */
451         /*spr_oam(oam, dynspr_base + snum++, SPRID_ENEMY, 50, 50, SPR_VRECT | SPR_SZ64 | SPR_256COL);*/
452         enemy = enemies;
453         for(i=0; i<total_enemies; i++) {
454                 int sid, anm, px, py, yoffs;
455                 unsigned int flags;
456                 int16_t mat[4];
457                 int32_t sa, ca, scale;
458
459                 if(enemy->vobj.px >= 0) {
460                         flags = SPR_DBLSZ | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0);
461                         if(enemy->hp > 0) {
462                                 anm = (enemy->anm + (vblcount >> 3)) & 0xf;
463                                 sid = SPRID_ENEMY0 + ((anm & 7) << 2);
464                                 flags |= SPR_SZ32 | SPR_VRECT;
465                                 yoffs = 32;
466                         } else {
467                                 anm = 0;
468                                 sid = SPRID_HUSK;
469                                 flags |= SPR_SZ16;
470                                 yoffs = 16;
471                         }
472
473                         px = enemy->vobj.px - 120;
474                         py = enemy->vobj.py - 80;
475                         xform_pixel(&px, &py);
476
477
478                         if(enemy->shot_frame >= 0) {
479                                 if(enemy->shot_frame < SFRM_LVL1) {
480                                         spr_oam(oam, dynspr_base + snum++, SPRID_SHOT0, px - 16, py - 16,
481                                                         SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
482                                 } else if(enemy->shot_frame < SFRM_LVL2) {
483                                         spr_oam(oam, dynspr_base + snum++, SPRID_SHOT1, px - 16, py - 16,
484                                                         SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
485                                 } else {
486                                         spr_oam(oam, dynspr_base + snum++, SPRID_SHOT2, px - 16, py - 16,
487                                                         SPR_DBLSZ | SPR_SZ16 | SPR_256COL | SPR_ROTSCL | SPR_ROTSCL_SEL(0));
488                                 }
489                         }
490
491
492                         spr_oam(oam, dynspr_base + snum++, sid, px - 16, py - yoffs, flags);
493
494                         scale = enemy->vobj.scale;
495                         if(scale > 0x10000) scale = 0x10000;
496                         sa = xform_sa / scale;
497                         ca = xform_ca / scale;
498                         mat[0] = anm >= 8 ? -ca : ca;
499                         mat[1] = sa;
500                         mat[2] = -sa;
501                         mat[3] = ca;
502
503                         spr_transform(oam, 0, mat);
504                         enemy->vobj.px = -1;
505                 }
506                 enemy++;
507         }
508         for(i=snum; i<dynspr_count; i++) {
509                 spr_oam_clear(oam, dynspr_base + i);
510         }
511
512         mask(INTR_VBLANK);
513         dynspr_count = snum;
514         unmask(INTR_VBLANK);
515
516         return 0;
517 }
518
519 static void draw(void)
520 {
521         //dma_fill16(3, framebuf, 0, 240 * 160 / 2);
522         fillblock_16byte(framebuf, 0, 240 * 160 / 16);
523
524         if(hit_frame) {
525                 gba_bgpal[0] = 0x7fff;
526         } else {
527                 gba_bgpal[0] = color0;
528                 vox_render();
529         }
530         //vox_sky_grad(COLOR_HORIZON, COLOR_ZENITH);
531         //vox_sky_solid(COLOR_ZENITH);
532
533         if(score >= 0 || energy <= 0) {
534                 int sec = total_time / 1000;
535
536                 fillblock_16byte(framebuf + 8 * 240 / 2, 199 | (199 << 8) | (199 << 16) | (199 << 24), 40 * 240 / 16);
537
538                 glyphfb = framebuf;
539                 glyphbg = 199;
540                 glyphcolor = 197;
541                 if(energy > 0) {
542                         dbg_drawstr(80, 10, "Victory!");
543                         glyphcolor = 200;
544                         dbg_drawstr(30, 20, "       Score: %d", score);
545                         dbg_drawstr(30, 28, "Completed in: %lum.%lus", sec / 60, sec % 60);
546                 } else {
547                         dbg_drawstr(80, 10, "Game Over!");
548                 }
549                 glyphcolor = 198;
550                 dbg_drawstr(85, 40, "Press start to exit");
551         }
552 }
553
554 static void victory(void)
555 {
556         int sec, time_bonus = 0;
557
558         total_time = timer_msec - start_time;
559         sec = total_time / 1000;
560
561         if(sec < 60) {
562                 time_bonus = 1000 + (60 - sec) * 100;
563         } else if(sec < 120) {
564                 time_bonus = 250 + (60 - (sec - 60)) * 10;
565         } else if(sec < 300) {
566                 time_bonus = 100;
567         }
568
569         score = energy * 250 + time_bonus;
570
571         /* TODO enter name */
572         save_score("???", score, total_time, 0);
573         save_scores();
574 }
575
576 static inline void xform_pixel(int *xp, int *yp)
577 {
578         int32_t sa = xform_sa >> 8;
579         int32_t ca = xform_ca >> 8;
580         int x = *xp;
581         int y = *yp;
582
583         *xp = (ca * x - sa * y + (120 << 8)) >> 8;
584         *yp = (sa * x + ca * y + (80 << 8)) >> 8;
585 }
586
587 #define MAXBANK         0x100
588
589 ARM_IWRAM
590 static void gamescr_vblank(void)
591 {
592         static int bank, bankdir, theta;
593         int32_t sa, ca;
594
595         if(!running) return;
596
597         vblcount++;
598
599         /* TODO: pre-arrange sprite tiles in gba-native format, so that I can just
600          * DMA them from cartridge easily
601          */
602
603         /*dma_copy32(3, (void*)(OAM_ADDR + dynspr_base * 8), oam + dynspr_base * 4, MAX_SPR * 2, 0);*/
604         dma_copy32(3, (void*)OAM_ADDR, oam, MAX_SPR * 2, 0);
605
606         if(gameover) return;
607
608         theta = -(bank << 3);
609         xform_sa = SIN(theta);
610         xform_ca = COS(theta);
611 #if 0
612         xform_s = 0x100000 / (MAXBANK + (abs(bank) >> 3));
613         sa = (((xform_sa) >> 8) * xform_s) >> 12;
614         ca = (((xform_ca) >> 8) * xform_s) >> 12;
615 #else
616         xform_s = (MAXBANK + (abs(bank) >> 3));
617         sa = xform_sa / xform_s;
618         ca = xform_ca / xform_s;
619 #endif
620
621         REG_BG2X = -ca * 120 - sa * 80 + (120 << 8);
622         REG_BG2Y = sa * 120 - ca * 80 + (80 << 8);
623
624         REG_BG2PA = ca;
625         REG_BG2PB = sa;
626         REG_BG2PC = -sa;
627         REG_BG2PD = ca;
628
629         if((keystate & (BN_LEFT | BN_RIGHT)) == 0) {
630                 if(bank) {
631                         bank -= bankdir << 4;
632                 }
633         } else if(keystate & BN_LEFT) {
634                 bankdir = -1;
635                 if(bank > -MAXBANK) bank -= 16;
636         } else if(keystate & BN_RIGHT) {
637                 bankdir = 1;
638                 if(bank < MAXBANK) bank += 16;
639         }
640 }