diff --git a/projects/chopper/c64project.json b/projects/chopper/c64project.json new file mode 100644 index 0000000..4298eda --- /dev/null +++ b/projects/chopper/c64project.json @@ -0,0 +1,12 @@ +{ + "name": "chopper", + "machine": "c64", + "language": "c", + "sources": ["src/*.c", "src/*.s"], + "flags": ["-Oirs"], + "disk": true, + "test": { + "expect": ["SPEED", "ALT", "WATER", "LIVES", "DEMO"], + "timeoutSeconds": 90 + } +} diff --git a/projects/chopper/src/dec.s b/projects/chopper/src/dec.s new file mode 100644 index 0000000..0463de8 --- /dev/null +++ b/projects/chopper/src/dec.s @@ -0,0 +1,41 @@ +; dec.s - 16-bit unsigned to five decimal screen-code digits, by subtraction. +; About 300 cycles; the C version with % and / cost over 4000. +; +; void __fastcall__ to_dec(unsigned v); result in decbuf[0..4], '0'-'9' + + .export _to_dec, _decbuf + .importzp ptr1, tmp1 + + .segment "BSS" +_decbuf: .res 5 + + .segment "CODE" +_to_dec: + sta ptr1 + stx ptr1+1 + ldy #0 +@digit: ldx #'0' +@sub: lda ptr1 + sec + sbc lo_tab,y + sta tmp1 + lda ptr1+1 + sbc hi_tab,y + bcc @done + sta ptr1+1 + lda tmp1 + sta ptr1 + inx + bne @sub +@done: txa + sta _decbuf,y + iny + cpy #4 + bne @digit + lda ptr1 + ora #'0' + sta _decbuf+4 + rts + +lo_tab: .byte <10000, <1000, <100, <10 +hi_tab: .byte >10000, >1000, >100, >10 diff --git a/projects/chopper/src/gfx.h b/projects/chopper/src/gfx.h new file mode 100644 index 0000000..66a92c8 --- /dev/null +++ b/projects/chopper/src/gfx.h @@ -0,0 +1,202 @@ +/* Generated by tools/gen_gfx.py - do not edit by hand. */ +#ifndef GFX_H +#define GFX_H + +#define FIRST_TILE 64 +#define NUM_TILES 33 +#define T_CL_TL 64 +#define T_CL_TM1 65 +#define T_CL_TM2 66 +#define T_CL_TR 67 +#define T_CL_BL 68 +#define T_CL_BM 69 +#define T_CL_BR 70 +#define T_M_UP 71 +#define T_M_DN 72 +#define T_M_PEAK 73 +#define T_M_UP_S 74 +#define T_M_DN_S 75 +#define T_M_PK_S 76 +#define T_M_FL_S 77 +#define T_M_SOLID 78 +#define T_M_SNOW 79 +#define T_M_ROCK 80 +#define T_H_UP 81 +#define T_H_DN 82 +#define T_H_PEAK 83 +#define T_H_FLAT 84 +#define T_H_SOLID 85 +#define T_TREE_TOP 86 +#define T_TREE_TRUNK 87 +#define T_PINE_TOP 88 +#define T_PINE_MID 89 +#define T_PINE_TRUNK 90 +#define T_GRASS1 91 +#define T_GRASS2 92 +#define T_DIRT1 93 +#define T_DIRT2 94 +#define T_BURNT_TREE 95 +#define T_BURNT_PINE 96 + +static const unsigned char tile_data[NUM_TILES * 8] = { + 0x00,0x00,0x00,0x01,0x05,0x15,0x55,0x55, /* T_CL_TL */ + 0x00,0x14,0x55,0x55,0x55,0x55,0x55,0x55, /* T_CL_TM1 */ + 0x00,0x00,0x05,0x15,0x55,0x55,0x55,0x55, /* T_CL_TM2 */ + 0x00,0x00,0x40,0x50,0x54,0x54,0x55,0x55, /* T_CL_TR */ + 0x55,0x55,0x15,0x1A,0x0A,0x00,0x00,0x00, /* T_CL_BL */ + 0x55,0x55,0x55,0x99,0xAA,0x00,0x00,0x00, /* T_CL_BM */ + 0x55,0x55,0x54,0xA4,0xA0,0x00,0x00,0x00, /* T_CL_BR */ + 0x02,0x02,0x0B,0x0B,0x2F,0x2F,0xBF,0xBF, /* T_M_UP */ + 0xC0,0xC0,0xF0,0xF0,0xFC,0xFC,0xFF,0xFF, /* T_M_DN */ + 0x2C,0x2C,0x2F,0xBF,0xBF,0xBF,0xBF,0xFF, /* T_M_PEAK */ + 0x01,0x01,0x05,0x07,0x17,0x1F,0xBF,0xBF, /* T_M_UP_S */ + 0x40,0x40,0x50,0xD0,0xD4,0xF4,0xFC,0xFF, /* T_M_DN_S */ + 0x14,0x14,0x15,0x55,0x5D,0x7F,0xBF,0xBF, /* T_M_PK_S */ + 0x55,0x75,0xFF,0xF7,0xFF,0xFF,0xFF,0xFF, /* T_M_FL_S */ + 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, /* T_M_SOLID */ + 0xDF,0xFF,0xF7,0xFF,0x7F,0xFF,0xFF,0xFF, /* T_M_SNOW */ + 0xFF,0xFB,0xEF,0xFF,0xFF,0xFF,0xFF,0xFF, /* T_M_ROCK */ + 0x01,0x05,0x07,0x1F,0x1F,0x7F,0x7F,0xFF, /* T_H_UP */ + 0x40,0x50,0xD0,0xF4,0xF4,0xFD,0xFD,0xFF, /* T_H_DN */ + 0x00,0x14,0x55,0x7D,0xFF,0xDF,0xFF,0xFF, /* T_H_PEAK */ + 0x55,0xFF,0xDF,0xFF,0xFF,0xF7,0xFF,0xFF, /* T_H_FLAT */ + 0xFF,0xDF,0xFF,0xFF,0xFD,0xFF,0xFF,0xFF, /* T_H_SOLID */ + 0x14,0x77,0x5F,0x7F,0xFF,0xFF,0x3C,0x28, /* T_TREE_TOP */ + 0x28,0x28,0x28,0x28,0x28,0x28,0x28,0xAA, /* T_TREE_TRUNK */ + 0x00,0x1C,0x1C,0x5F,0x7F,0x1C,0x5F,0x7F, /* T_PINE_TOP */ + 0x1C,0x5F,0x7F,0xFF,0x1C,0x5F,0x7F,0xFF, /* T_PINE_MID */ + 0x28,0x28,0x28,0x28,0x28,0x28,0x28,0xAA, /* T_PINE_TRUNK */ + 0x44,0x55,0xDD,0xFF,0xF7,0xFF,0xDF,0xFF, /* T_GRASS1 */ + 0x11,0x55,0x77,0xFF,0xDF,0xFF,0xFD,0xFF, /* T_GRASS2 */ + 0xAA,0xBA,0xAA,0xAB,0xBA,0xAA,0xFF,0xFF, /* T_DIRT1 */ + 0xAA,0xAA,0xAE,0xAA,0xEA,0xAE,0xFF,0xFF, /* T_DIRT2 */ + 0x82,0x22,0x88,0x28,0x88,0x28,0x28,0x28, /* T_BURNT_TREE */ + 0x08,0x28,0x88,0x28,0x2A,0x88,0x28,0x28, /* T_BURNT_PINE */ +}; + +#define NUM_SPRITES 49 +/* blocks: [pose*2] outline, [pose*2+1] fill; poses 0-11 = facing*6+tilt*2+rotor, + 12-13 front view, then shadow big (28) and shadow small (29). */ +#define SP_SHADOW_BIG 28 +#define SP_SHADOW_SMALL 29 +#define SP_BIRD 30 +#define SP_BIRD2 31 +#define SP_BALLOON 32 +#define SP_JET_L 33 +#define SP_JET_L2 34 +#define SP_JET_R 35 +#define SP_JET_R2 36 +#define SP_BOOM 37 +#define SP_BOOM2 38 +#define SP_BOOM3 39 +#define SP_BLANK 40 +#define SP_FIRE 41 +#define SP_FIRE2 42 +#define SP_FIRE_S 43 +#define SP_FIRE_S2 44 +#define SP_SPRAY 45 +#define SP_SPRAY2 46 +#define SP_STEAM 47 +#define SP_STEAM2 48 +static const unsigned char sprite_data[NUM_SPRITES * 64] = { + /* 0: f0 t0 r0 outline */ + 0,0,0,0,0,0,0,0,0,255,255,255,64,12,0,64,63,0,64,64,192,160,128,48,161,0,8,158,0,4,128,0,2,191,0,1,65,0,1,64,128,2,0,127,254,0,32,32,1,255,252,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 1: f0 t0 r0 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,42,0,128,170,128,128,170,144,170,170,148,170,255,148,170,255,165,0,170,170,0,170,168,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 2: f0 t0 r1 outline */ + 0,0,0,0,0,0,0,0,0,3,255,192,0,12,0,160,63,0,64,64,192,160,128,48,161,0,8,158,0,4,128,0,2,191,0,1,65,0,1,160,128,2,0,127,254,0,32,32,1,255,252,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 3: f0 t0 r1 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,42,0,128,170,128,128,170,144,170,170,148,170,255,148,170,255,165,0,170,170,0,170,168,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 4: f0 t1 r0 outline */ + 0,0,0,240,0,0,79,240,0,64,15,240,64,60,15,160,79,0,160,128,192,145,0,48,142,0,0,176,0,8,79,0,4,65,0,2,0,128,1,0,112,1,0,47,242,1,240,46,0,15,240,0,0,12,0,0,0,0,0,0,0,0,0,0, + /* 5: f0 t1 r0 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,32,0,128,170,0,160,170,128,170,170,144,170,250,144,0,255,148,0,175,164,0,170,165,0,10,170,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 6: f0 t1 r1 outline */ + 0,0,0,0,0,0,3,240,0,160,15,192,64,60,0,160,79,0,160,128,192,145,0,48,142,0,0,176,0,8,79,0,4,161,0,2,0,128,1,0,112,1,0,47,242,1,240,46,0,15,240,0,0,12,0,0,0,0,0,0,0,0,0,0, + /* 7: f0 t1 r1 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,32,0,128,170,0,160,170,128,170,170,144,170,250,144,0,255,148,0,175,164,0,170,165,0,10,170,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 8: f0 t2 r0 outline */ + 0,0,0,0,0,15,0,15,240,15,252,0,240,15,0,64,48,192,64,64,56,64,128,4,161,0,2,174,0,1,144,0,1,143,0,2,177,0,14,64,143,240,64,112,44,0,47,240,1,240,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 9: f0 t2 r0 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,0,0,42,128,0,170,148,128,170,148,138,175,149,170,255,170,170,250,168,160,170,160,0,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 10: f0 t2 r1 outline */ + 0,0,0,0,0,0,0,15,192,3,252,0,0,15,0,0,48,192,160,64,56,64,128,4,161,0,2,174,0,1,144,0,1,143,0,2,177,0,14,64,143,240,160,112,44,0,47,240,1,240,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 11: f0 t2 r1 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,0,0,42,128,0,170,148,128,170,148,138,175,149,170,255,170,170,250,168,160,170,160,0,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 12: f1 t0 r0 outline */ + 0,0,0,0,0,0,0,0,0,255,255,255,0,48,2,0,252,2,3,2,2,12,1,5,16,0,133,32,0,121,64,0,1,128,0,253,128,0,130,64,1,2,127,254,0,4,4,0,63,255,128,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 13: f1 t0 r0 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,168,0,2,170,2,6,170,2,21,170,170,22,255,170,86,255,170,170,170,0,42,170,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 14: f1 t0 r1 outline */ + 0,0,0,0,0,0,0,0,0,3,255,192,0,48,0,0,252,5,3,2,2,12,1,5,16,0,133,32,0,121,64,0,1,128,0,253,128,0,130,64,1,5,127,254,0,4,4,0,63,255,128,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 15: f1 t0 r1 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,168,0,2,170,2,6,170,2,21,170,170,22,255,170,86,255,170,170,170,0,42,170,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 16: f1 t1 r0 outline */ + 0,0,0,0,0,15,0,15,242,15,240,2,240,60,2,0,242,5,3,1,5,12,0,137,0,0,113,16,0,13,32,0,242,64,0,130,128,1,0,128,14,0,79,244,0,116,15,128,15,240,0,48,0,0,0,0,0,0,0,0,0,0,0,0, + /* 17: f1 t1 r0 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,2,0,170,2,2,170,10,6,170,170,5,175,170,22,255,0,22,250,0,90,170,0,170,160,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 18: f1 t1 r1 outline */ + 0,0,0,0,0,0,0,15,192,3,240,5,0,60,2,0,242,5,3,1,5,12,0,137,0,0,113,16,0,13,32,0,242,64,0,133,128,1,0,128,14,0,79,244,0,116,15,128,15,240,0,48,0,0,0,0,0,0,0,0,0,0,0,0, + /* 19: f1 t1 r1 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,2,0,170,2,2,170,10,6,170,170,5,175,170,22,255,0,22,250,0,90,170,0,170,160,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 20: f1 t2 r0 outline */ + 0,0,0,240,0,0,15,240,0,0,63,240,0,240,15,3,12,2,28,2,2,32,1,2,64,0,133,128,0,117,128,0,9,64,0,241,112,0,141,15,241,2,52,14,2,15,244,0,0,15,128,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 21: f1 t2 r0 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,160,0,2,168,0,22,170,0,21,170,2,86,250,162,166,255,170,42,175,170,10,170,10,0,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 22: f1 t2 r1 outline */ + 0,0,0,0,0,0,3,240,0,0,63,192,0,240,0,3,12,0,28,2,5,32,1,2,64,0,133,128,0,117,128,0,9,64,0,241,112,0,141,15,241,2,52,14,5,15,244,0,0,15,128,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 23: f1 t2 r1 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,160,0,2,168,0,22,170,0,21,170,2,86,250,162,166,255,170,42,175,170,10,170,10,0,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 24: front r0 outline */ + 0,0,0,0,0,0,0,0,0,63,255,252,0,24,0,0,255,0,1,0,128,2,0,64,4,0,32,4,0,32,4,0,32,4,0,32,2,0,64,1,255,128,0,129,0,1,129,128,3,129,192,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 25: front r0 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,170,0,1,85,64,1,85,64,1,85,64,2,85,64,3,255,192,2,170,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 26: front r1 outline */ + 0,0,0,0,0,0,0,0,0,0,255,0,0,24,0,0,255,0,1,0,128,2,0,64,4,0,32,4,0,32,4,0,32,4,0,32,2,0,64,1,255,128,0,129,0,1,129,128,3,129,192,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 27: front r1 fill */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,170,0,1,85,64,1,85,64,1,85,64,2,85,64,3,255,192,2,170,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 28: SP_SHADOW_BIG */ + 10,170,160,21,85,84,10,170,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 29: SP_SHADOW_SMALL */ + 0,170,0,1,85,64,0,170,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 30: SP_BIRD */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,0,96,14,1,192,3,135,0,0,252,0,0,120,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 31: SP_BIRD2 */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,252,0,7,135,128,28,0,224,48,0,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 32: SP_BALLOON */ + 3,235,192,14,170,176,57,186,236,38,186,232,230,186,235,234,186,235,234,186,235,42,186,232,58,186,236,14,186,224,3,170,192,0,195,0,0,195,0,0,195,0,0,255,0,0,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 33: SP_JET_L */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,160,5,170,168,170,170,171,43,250,168,0,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 34: SP_JET_L2 */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,160,5,170,168,170,170,175,43,250,168,0,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 35: SP_JET_R */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,10,0,0,42,170,80,234,170,170,42,175,232,0,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 36: SP_JET_R2 */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,10,0,0,42,170,80,250,170,170,42,175,232,0,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 37: SP_BOOM */ + 0,0,0,0,0,0,0,0,0,0,12,0,0,12,0,0,63,0,0,60,0,0,63,192,0,235,192,0,234,64,3,235,192,3,234,192,0,235,0,0,255,192,0,63,0,0,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 38: SP_BOOM2 */ + 0,0,0,0,7,0,0,48,0,0,51,0,15,243,0,12,251,192,15,171,192,15,235,240,31,235,240,15,171,208,19,234,192,14,170,188,15,234,240,3,238,240,7,171,240,4,47,192,0,127,0,4,28,0,0,1,0,0,0,0,0,192,0,0, + /* 39: SP_BOOM3 */ + 0,76,0,3,115,0,0,255,0,0,255,240,7,239,240,15,235,240,15,235,195,62,170,240,254,170,188,254,170,252,30,170,244,254,170,252,14,170,240,15,170,204,63,251,252,15,171,252,15,255,192,15,255,240,4,63,208,3,192,0,12,77,0,0, + /* 40: SP_BLANK */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 41: SP_FIRE */ + 0,0,0,0,0,0,0,0,0,0,60,0,0,60,0,0,63,0,0,63,0,3,63,0,3,235,48,51,234,240,51,234,252,62,170,236,62,170,172,58,170,172,58,170,172,58,170,172,58,170,172,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 42: SP_FIRE2 */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,255,0,48,255,240,63,250,252,63,170,252,58,170,172,58,170,172,58,170,172,58,170,172,58,170,172,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 43: SP_FIRE_S */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,12,0,3,12,0,3,60,0,14,251,12,14,235,204,14,170,204,58,170,188,58,170,188,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 44: SP_FIRE_S2 */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,3,3,0,3,207,0,51,207,0,62,186,240,62,186,240,58,170,172,58,170,172,0,0,0,0,0,0,0,0,0,0,0,0,0, + /* 45: SP_SPRAY */ + 0,64,64,1,1,0,0,4,0,0,16,0,0,64,128,1,2,0,0,8,0,0,32,0,0,128,64,2,1,0,0,4,0,0,16,0,4,64,96,1,1,0,0,4,0,0,16,0,8,64,144,1,2,0,0,8,0,0,32,0,4,128,80,0, + /* 46: SP_SPRAY2 */ + 0,16,0,0,64,128,1,2,0,0,8,0,0,32,0,0,128,64,2,1,0,0,4,0,0,16,0,0,64,64,1,1,0,4,4,16,0,16,0,0,64,128,1,2,0,4,8,32,0,32,0,0,128,64,2,1,0,8,4,16,0,16,0,0, + /* 47: SP_STEAM */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,97,128,2,160,64,10,32,96,5,42,160,6,84,96,9,66,80,0,101,160,9,138,32,6,17,144,2,2,128,2,130,128,0,16,0,0,0,0,0,0,0,0, + /* 48: SP_STEAM2 */ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,149,128,2,133,128,0,166,128,2,74,128,10,162,32,2,154,16,1,74,96,2,166,32,10,96,160,1,41,64,1,34,0,0,8,0,0,0,0,0,0,0,0, +}; + +#endif diff --git a/projects/chopper/src/gfx.inc b/projects/chopper/src/gfx.inc new file mode 100644 index 0000000..07dc703 --- /dev/null +++ b/projects/chopper/src/gfx.inc @@ -0,0 +1,23 @@ +; Generated by tools/gen_gfx.py - do not edit by hand. Sprite block numbers. +SPR_BASE = 16 ; block of sprite 0: ($8400 - $8000) / 64 +SP_SHADOW_BIG = 28 +SP_SHADOW_SMALL = 29 +SP_BIRD = 30 +SP_BIRD2 = 31 +SP_BALLOON = 32 +SP_JET_L = 33 +SP_JET_L2 = 34 +SP_JET_R = 35 +SP_JET_R2 = 36 +SP_BOOM = 37 +SP_BOOM2 = 38 +SP_BOOM3 = 39 +SP_BLANK = 40 +SP_FIRE = 41 +SP_FIRE2 = 42 +SP_FIRE_S = 43 +SP_FIRE_S2 = 44 +SP_SPRAY = 45 +SP_SPRAY2 = 46 +SP_STEAM = 47 +SP_STEAM2 = 48 diff --git a/projects/chopper/src/hazards.inc b/projects/chopper/src/hazards.inc new file mode 100644 index 0000000..87a6fdf --- /dev/null +++ b/projects/chopper/src/hazards.inc @@ -0,0 +1,17 @@ +; hazards.inc - shared by hazards.s (main.c mirrors these as #defines) + .include "gfx.inc" + +MAXH = 8 ; hazard slots (mux.s MAXV = MAXH + 1 for the spray) +HZ_NONE = 0 +HZ_BIRD = 1 +HZ_BALLOON = 2 +HZ_JET = 3 +HZ_FIRE = 4 +HZ_STEAM = 5 +HZ_LOST = 6 ; a fire that left the screen burning (main.c clears it) + +FIRE_HP = 48 ; frames of water a fire needs +SPRAY_HALF_W = 12 ; water hits fires within this many px sideways +SPRAY_REACH = 40 ; ... and with their top this far below the chopper's +STEAM_TIME = 48 +COLOR_STEAM = 15 ; light grey diff --git a/projects/chopper/src/hazards.s b/projects/chopper/src/hazards.s new file mode 100644 index 0000000..c5dcabd --- /dev/null +++ b/projects/chopper/src/hazards.s @@ -0,0 +1,329 @@ +; hazards.s - per-frame update of the things the chopper has to avoid. +; +; Each hazard has a screen X with 1/256 px precision (xf, xlo, xhi). Every frame +; it moves by its own speed minus the ground speed, so it travels with the +; landscape, and it is dropped once it leaves the screen. Its Y follows a wave +; for birds and balloons. A box test against the chopper sets hz_hit, and the +; hazard is handed to the multiplexer as a virtual sprite (v_*, mux.s). +; Spawning and what a hit does are game rules: they live in main.c. +; +; Fires are hazards too: own speed 0, so they ride exactly with the ground +; layer (main.c starts them in step with scroll.s's sub-pixel position). Water +; from the chopper (spray_*) wears a fire's hp down; at 0 it turns into a puff +; of steam (counted in hz_saved). A fire that leaves the screen still burning +; becomes HZ_LOST for main.c to mark its tree as burnt. + + .export _haz_update, _world_d, _hit_on, _hit_xlo, _hit_xhi, _hit_y, _hz_hit + .export _hz_type, _hz_xf, _hz_xlo, _hz_xhi, _hz_vx, _hz_y, _hz_base, _hz_t, _hz_col + .export _hz_hp, _hz_saved, _hz_lostany, _spray_on, _spray_xlo, _spray_xhi, _spray_y + .export _fire_near + .import _v_on, _v_y, _v_x, _v_flags, _v_ptr, _v_col + .importzp tmp1, tmp2, tmp3, tmp4, ptr1 + + .include "hazards.inc" + + .segment "BSS" +_hz_type: .res MAXH ; HZ_NONE, HZ_BIRD, HZ_BALLOON, HZ_JET +_hz_xf: .res MAXH +_hz_xlo: .res MAXH +_hz_xhi: .res MAXH +_hz_vx: .res MAXH ; own speed, signed 1/16 px per frame +_hz_y: .res MAXH +_hz_base: .res MAXH +_hz_t: .res MAXH +_hz_col: .res MAXH +_world_d: .res 1 ; ground speed this frame, signed 1/16 px +_hit_on: .res 1 ; non-zero: test collisions +_hit_xlo: .res 1 ; chopper sprite position +_hit_xhi: .res 1 +_hit_y: .res 1 +_hz_hit: .res 1 ; 0, or 1 + index of a hazard touching the chopper +_hz_hp: .res MAXH ; fire: water still needed (frames of spray) +_hz_saved: .res 1 ; fires put out this frame +_hz_lostany: .res 1 ; non-zero: some slot became HZ_LOST this frame +_spray_on: .res 1 ; non-zero: water is falling from the chopper +_spray_xlo: .res 1 ; chopper sprite position +_spray_xhi: .res 1 +_spray_y: .res 1 +_fire_near: .res 1 ; a fire is in reach of the water (for the demo pilot) + + .segment "RODATA" +; per type (index 0 unused): collision half-widths/heights (chopper + hazard), +; multicolor flag for v_flags +; none bird balloon jet fire steam lost +half_w: .byte 0, 15, 16, 18, 0, 0, 0 ; 0 = never hits the chopper +half_h: .byte 0, 7, 12, 6, 0, 0, 0 +mc_flag: .byte 0, 0, 2, 2, 2, 2, 0 +; 32-step waves +wave_bird: .byte 0,1,2,3,4,5,5,6,6,6,5,5,4,3,2,1 + .byte 0,<-1,<-2,<-3,<-4,<-5,<-5,<-6,<-6,<-6,<-5,<-5,<-4,<-3,<-2,<-1 +wave_bal: .byte 0,2,4,5,7,8,9,10,10,10,9,8,7,5,4,2 + .byte 0,<-2,<-4,<-5,<-7,<-8,<-9,<-10,<-10,<-10,<-9,<-8,<-7,<-5,<-4,<-2 + + .segment "CODE" + +; void haz_update(void); +_haz_update: + lda #0 + sta _hz_hit + sta _hz_saved + sta _hz_lostany + sta _fire_near + ldx #0 +@loop: lda #0 + sta _v_on,x ; shown only if it reaches @emit + lda _hz_type,x + beq :+ + cmp #HZ_LOST ; waiting for main.c + bne :++ +: jmp @next + +: ; ---- move: d = own speed - ground speed, as 1/16 px; add d*16/256 px + lda _hz_vx,x + sec + sbc _world_d + sta tmp1 + asl a + asl a + asl a + asl a + sta tmp2 ; low byte of d*16 + lda tmp1 + cmp #$80 + ror a + cmp #$80 + ror a + cmp #$80 + ror a + cmp #$80 + ror a + sta tmp3 ; high byte + ldy #0 + cmp #$80 + bcc :+ + dey +: sty tmp4 + clc + lda _hz_xf,x + adc tmp2 + sta _hz_xf,x + lda _hz_xlo,x + adc tmp3 + sta _hz_xlo,x + lda _hz_xhi,x + adc tmp4 + sta _hz_xhi,x + + ; ---- keep while 0 <= x < 376 (hazards spawn just off either edge) + beq @inside + cmp #1 + bne @kill + lda _hz_xlo,x + cmp #120 + bcc @inside +@kill: lda _hz_type,x + cmp #HZ_FIRE + bne :+ + lda #HZ_LOST ; a fire got away: main.c burns its tree + sta _hz_type,x + sta _hz_lostany + jmp @next +: lda #HZ_NONE + sta _hz_type,x + jmp @next + +@inside: + inc _hz_t,x + lda _hz_type,x + cmp #HZ_BIRD + bne @notbird + lda _hz_t,x ; bird: quick wave, flap every 4 frames + and #31 + tay + lda wave_bird,y + clc + adc _hz_base,x + sta _hz_y,x + lda _hz_t,x + lsr a + lsr a + and #1 + clc + adc #SPR_BASE + SP_BIRD + sta ptr1 + jmp @hit +@notbird: + cmp #HZ_FIRE + bne :+ + jmp @fire +: cmp #HZ_STEAM + bne :+ + jmp @steam +: cmp #HZ_BALLOON + bne @jet + lda _hz_t,x ; balloon: slow bob + lsr a + lsr a + and #31 + tay + lda wave_bal,y + clc + adc _hz_base,x + sta _hz_y,x + lda #SPR_BASE + SP_BALLOON + sta ptr1 + jmp @hit +@jet: lda _hz_base,x ; jet: straight line, flickering exhaust + sta _hz_y,x + lda _hz_t,x + lsr a + and #1 + ldy _hz_vx,x + bmi :+ + clc + adc #SP_JET_R - SP_JET_L +: clc + adc #SPR_BASE + SP_JET_L + sta ptr1 + + jmp @hit + +@fire: lda _hz_base,x ; fire: fixed on its tree, flickering + sta _hz_y,x + lda _hz_t,x + lsr a + lsr a + and #1 + ldy _hz_hp,x + cpy #FIRE_HP / 2 + bcs :+ + clc + adc #SP_FIRE_S - SP_FIRE ; half put out: smaller flames +: clc + adc #SPR_BASE + SP_FIRE + sta ptr1 + lda _hz_xlo,x ; in reach: chopper right above, close enough + sec + sbc _spray_xlo + sta tmp2 + lda _hz_xhi,x + sbc _spray_xhi + beq :+ + cmp #$FF + bne @emit_j + lda tmp2 + beq @emit_j + eor #$FF + clc + adc #1 + sta tmp2 +: lda tmp2 + cmp #SPRAY_HALF_W + bcs @emit_j + lda _hz_y,x ; fire top must be 0..SPRAY_REACH below the chopper top + sec + sbc _spray_y + bcc @emit_j + cmp #SPRAY_REACH + bcs @emit_j + inc _fire_near + lda _spray_on + beq @emit_j + dec _hz_hp,x + bne @emit_j + lda #HZ_STEAM ; out! a puff of steam rises from the tree + sta _hz_type,x + lda #0 + sta _hz_t,x + lda #COLOR_STEAM + sta _hz_col,x + inc _hz_saved +@emit_j: + jmp @emit + +@steam: lda _hz_t,x ; steam: rises and fades out + cmp #STEAM_TIME + bcc :+ + lda #HZ_NONE + sta _hz_type,x + jmp @next +: lsr a + sta tmp1 + lda _hz_base,x + sec + sbc tmp1 + sta _hz_y,x + lda _hz_t,x + lsr a + lsr a + lsr a + and #1 + clc + adc #SPR_BASE + SP_STEAM + sta ptr1 + jmp @emit + + ; ---- collision box test against the chopper +@hit: lda _hit_on + beq @emit + lda _hz_xlo,x + sec + sbc _hit_xlo + sta tmp2 + lda _hz_xhi,x + sbc _hit_xhi + beq @dxok ; 0..255 to the right + cmp #$FF + bne @emit ; far away + lda tmp2 + beq @emit ; exactly -256 + eor #$FF + clc + adc #1 + sta tmp2 ; |dx| +@dxok: ldy _hz_type,x + lda tmp2 + cmp half_w,y + bcs @emit + lda _hz_y,x + sec + sbc _hit_y + bcs :+ + eor #$FF + adc #1 ; carry is clear: two's complement +: cmp half_h,y + bcs @emit + lda _hz_hit + bne @emit + inx + stx _hz_hit ; 1 + index + dex + + ; ---- hand it to the multiplexer +@emit: txa ; multiplexer slot = hazard slot + tay + lda #1 + sta _v_on,y + lda _hz_y,x + sta _v_y,y + lda _hz_xlo,x + sta _v_x,y + lda _hz_type,x + sta tmp1 + lda _hz_xhi,x + and #1 + stx tmp2 + ldx tmp1 + ora mc_flag,x + ldx tmp2 + sta _v_flags,y + lda ptr1 + sta _v_ptr,y + lda _hz_col,x + sta _v_col,y + +@next: inx + cpx #MAXH + bcs :+ + jmp @loop +: rts diff --git a/projects/chopper/src/main.c b/projects/chopper/src/main.c new file mode 100644 index 0000000..f60973a --- /dev/null +++ b/projects/chopper/src/main.c @@ -0,0 +1,1108 @@ +/* + * chopper - fly a helicopter over a parallax-scrolling landscape (C64, PAL). + * + * c64 build projects/chopper + * c64 run projects/chopper joystick in port 2 + * c64 test projects/chopper headless: self-test + 1000 frames of demo flight + * python projects/chopper/tools/gen_gfx.py regenerates src/gfx.h from the art + * + * Screen layout (VIC bank 2: screen $8000, sprites $8400, charset $A000): + * rows 0-4 clouds scroll at 1/4 speed + wind + * rows 5-14 mountains scroll at 1/2 speed + * rows 15-22 hills, trees, grass, dirt at full speed + * rows 23-24 status bar (fixed) + * Each band has its own $D016 value, set by the raster IRQ chain in irq.s. + * + * Frame budget (PAL, 19656 cycles): the main loop wakes at raster line 251. + * It first applies the scroll computed last frame (band shifts in shift.s, + * top band first, each band's fine scroll set right after its shift), then runs + * the game logic for the next frame. Terrain comes from height maps built at + * startup, and the next column in each direction is built ahead of time in a + * frame with time to spare, so a frame that scrolls only copies it in. + * Other background jobs (fires starting, the status bar) also run only when + * the raster shows the frame has room. + * The self-test fails if any frame runs late or a band misses its raster split. + * + * Helicopter = sprite 0 (hi-res black outline) over sprite 1 (multicolor fill), + * sprite 2 = ground shadow. Poses: facing left/right x level/nose-down/nose-up + * x two rotor frames, plus a front view while turning around. + * + * Hazards (bird flocks, hot-air balloons, jets) share hardware sprites 3-7 + * through the multiplexer in mux.s, so up to 10 can be on screen. hazards.s + * moves them and tests collisions; this file spawns them and handles hits. + * Three lives; a hit costs one and gives a few seconds of blinking safety. + * + * Fires: trees catch fire ahead of the chopper. Hold fire to spray water + * (the tank refills on the ground); hover low over a burning tree until it + * goes out. A fire left behind burns its tree for good (FIRE_T remembers). + */ +#include +#include +#include "c64test.h" +#include "gfx.h" + +#define SCREEN ((unsigned char *)0x8000) /* VIC bank 2: $8000-$BFFF */ +#define COLRAM ((unsigned char *)0xD800) +#define CHARSET ((unsigned char *)0xA000) /* RAM under BASIC ROM; the VIC sees RAM */ +#define SPRMEM ((unsigned char *)0x8400) +#define SPRPTR ((unsigned char *)0x83F8) +#define SPR_BASE 16 /* ($8400 - $8000) / 64 */ +#define JOY2 (*(volatile unsigned char *)0xDC00) +#define BLNSW (*(unsigned char *)0x00CC) /* non-zero: Kernal cursor blink off */ + +/* World tables, built at startup ($5000-$7FFF; the program must end below). */ +#define WORLD_COLS 4096 +#define WORLD_MASK 0x0FFF /* the world repeats every 4096 columns */ +#define MT_H ((unsigned char *)0x5000) /* mountain height per column, 2..9 */ +#define HL_H ((unsigned char *)0x6000) /* hill height per column, 0..3 */ +#define RND_T ((unsigned char *)0x7000) /* random byte per column */ +#define DATA_LOW 0x5000 +#define FIRE_T ((unsigned char *)0x9800) /* burnt-tree bits, 1 per column (512 bytes) */ + +#define CL_ROW 0 +#define CL_ROWS 5 +#define MT_ROW 5 +#define MT_ROWS 10 +#define HL_ROW 15 +#define HL_ROWS 8 +#define HUD_ROW 23 +#define HUD1 (SCREEN + HUD_ROW * 40) +#define HUD2 (SCREEN + (HUD_ROW + 1) * 40) + +#define MC_ON 0x10 +#define BLANK 32 + +/* Raster deadlines (see irq.s): a band's shift and $D016 value must be in + place before the IRQ that switches to it. */ +#define LINE_TOP 251 +#define LINE_MTNS 89 +#define LINE_HILLS 169 +#define BG_LIMIT 200 /* start background jobs only before this many + lines after LINE_TOP */ + +/* Helicopter limits, in sprite coordinates. Skids are on sprite row 16. */ +#define Y_MIN 48 +#define Y_MAX 202 /* skids rest on the grass row (raster 219) */ +#define VX_MAX 32 /* 1/16 px per frame: 2 px/frame at full speed */ +#define VY_MAX 24 +#define SHADOW_Y 220 + +#define JOY_UP 1 +#define JOY_DOWN 2 +#define JOY_LEFT 4 +#define JOY_RIGHT 8 +#define JOY_FIRE 16 + +/* mux.s: raster IRQ scheduler (band splits + sprite multiplexer) */ +extern unsigned char irq_frame, xs_clouds, xs_mtns, xs_hills; +void irq_init(void); +void mux_build(void); +extern unsigned char mux_shown, mux_dropped; +extern unsigned char v_on[], v_y[], v_x[], v_flags[], v_ptr[], v_col[]; +#define SPRAY_SLOT MAXH /* multiplexer slot of the water spray */ +extern unsigned char hs_x, hs_msb, hs_y, hs_p0, hs_p1, hs_p2, hs_en; + +/* hazards.s */ +#define MAXH 8 +#define HZ_NONE 0 +#define HZ_BIRD 1 +#define HZ_BALLOON 2 +#define HZ_JET 3 +#define HZ_FIRE 4 +#define HZ_STEAM 5 +#define HZ_LOST 6 +#define FIRE_HP 48 /* = hazards.inc */ +#define MAX_FIRES 3 +#define WATER_MAX 99 +void haz_update(void); +extern unsigned char hz_type[], hz_xf[], hz_xlo[], hz_xhi[], hz_y[], hz_base[], hz_t[], hz_col[]; +extern signed char hz_vx[], world_d; +extern unsigned char hit_on, hit_xlo, hit_xhi, hit_y, hz_hit; +extern unsigned char hz_hp[], hz_saved, hz_lostany, fire_near; +extern unsigned char spray_on, spray_xlo, spray_xhi, spray_y; +/* scroll.s: layer positions and the vertical-blank band shifter */ +#define L_CLOUDS 0 +#define L_MTNS 1 +#define L_HILLS 2 +unsigned char __fastcall__ layer_step(unsigned char layer); +void apply_scroll(void); +extern signed char move_d; +extern unsigned char lay_px_lo[3], lay_px_hi[3], lay_frac[3]; +extern unsigned char lay_col_lo[3], lay_col_hi[3], lay_pend[3], lay_xs[3]; +extern unsigned char nb_clouds[5], nb_mtns[10], nb_hills[8]; +#define LAY_PX(i) (lay_px_lo[i] | ((unsigned)lay_px_hi[i] << 8)) +#define LAY_COL(i) (lay_col_lo[i] | ((unsigned)lay_col_hi[i] << 8)) +void __fastcall__ to_dec(unsigned v); +extern unsigned char decbuf[5]; +extern unsigned char _BSS_RUN__[], _BSS_SIZE__[]; + +/* ------------------------------------------------------------------ world */ + +static unsigned char rnd[256]; + +static void rnd_init(void) +{ + unsigned s = 0xACE1; + unsigned char i = 0; + do { + s = (s >> 1) ^ ((0u - (s & 1u)) & 0xB400u); + rnd[i] = (unsigned char)(s >> 8) ^ (unsigned char)s; + } while (++i); +} + +static const unsigned char mtn_peaks[16] = { 3,4,5,6,7,8,9,5,6,7,8,4,6,9,7,5 }; +static const unsigned char hill_peaks[16] = { 0,0,1,2,3,1,2,0,3,2,1,0,2,3,1,0 }; + +/* Ridge of slope-1 triangles, one peak every 8 columns: rasterise each peak + into the height map, keeping the highest value per column. */ +static void build_ridge(unsigned char *map, unsigned char base, + unsigned char salt, const unsigned char *peaks) +{ + unsigned b, center; + unsigned char r, pk, d, h; + + memset(map, base, WORLD_COLS); + for (b = 0; b < WORLD_COLS / 8; ++b) { + r = rnd[(unsigned char)b ^ rnd[(unsigned char)(b >> 8) ^ salt]]; + center = (b << 3) + 2 + (r & 3); + pk = peaks[r >> 4]; + for (d = 0; d < pk; ++d) { + h = pk - d; + if (map[(center + d) & WORLD_MASK] < h) map[(center + d) & WORLD_MASK] = h; + if (map[(center - d) & WORLD_MASK] < h) map[(center - d) & WORLD_MASK] = h; + } + } +} + +static void build_world(void) +{ + unsigned char hi = 0, lo, x, *p = RND_T; + + rnd_init(); + do { /* RND_T[c] = rnd[lo ^ rnd[hi]] */ + x = rnd[hi * 16 + 7]; + lo = 0; + do { *p++ = rnd[lo ^ x]; } while (++lo); + } while (++hi < WORLD_COLS / 256); + build_ridge(MT_H, 2, 0x3C, mtn_peaks); + build_ridge(HL_H, 0, 0x77, hill_peaks); +} + +/* 0 flat/valley, 1 rising "/", 2 falling "\", 3 peak */ +static unsigned char top_shape(unsigned char hl, unsigned char h, unsigned char hr) +{ + unsigned char s = 0; + if (hl < h) s |= 1; + if (hr < h) s |= 2; + return s; +} + +static unsigned char colbuf[10]; + +static const unsigned char mtn_top[4] = { T_M_SOLID, T_M_UP, T_M_DN, T_M_PEAK }; +static const unsigned char snow_top[4] = { T_M_FL_S, T_M_UP_S, T_M_DN_S, T_M_PK_S }; +static const unsigned char hill_top[4] = { T_H_FLAT, T_H_UP, T_H_DN, T_H_PEAK }; + +static void gen_clouds(unsigned c) +{ + unsigned char r = RND_T[(c >> 3) ^ 0x0A5], o = (unsigned char)c & 7, w, s, row, k; + + memset(colbuf, BLANK, CL_ROWS); + if ((r & 3) == 0) return; /* 1 in 4 blocks is open sky */ + w = 3 + ((r >> 2) & 3); /* 3..6 columns wide */ + s = (r >> 4) & 1; + row = (r >> 6) == 3 ? 1 : (r >> 6); /* rows 0-2 (+1 for the base) */ + if (o < s || o >= s + w) return; + k = o - s; + if (k == 0) { colbuf[row] = T_CL_TL; colbuf[row + 1] = T_CL_BL; } + else if (k == w - 1) { colbuf[row] = T_CL_TR; colbuf[row + 1] = T_CL_BR; } + else { + colbuf[row] = (c & 1) ? T_CL_TM1 : T_CL_TM2; + colbuf[row + 1] = T_CL_BM; + } +} + +static void gen_mtns(unsigned c) +{ + unsigned char h = MT_H[c], top = MT_ROWS - h, shape, r; + + shape = top_shape(MT_H[(c - 1) & WORLD_MASK], h, MT_H[(c + 1) & WORLD_MASK]); + memset(colbuf, BLANK, top); + memset(colbuf + top, T_M_SOLID, h); + if (h >= 7) { + colbuf[top] = snow_top[shape]; + if (h >= 8) colbuf[top + 1] = T_M_SNOW; + } else { + colbuf[top] = mtn_top[shape]; + } + r = top + 2 + (RND_T[c] & 7); /* one rock speck, maybe; */ + if (r < MT_ROWS - 1 && colbuf[r] == T_M_SOLID) /* the bottom row stays solid */ + colbuf[r] = T_M_ROCK; +} + +static const unsigned char bit8[8] = { 1, 2, 4, 8, 16, 32, 64, 128 }; +#define IS_BURNT(c) (FIRE_T[(c) >> 3] & bit8[(unsigned char)(c) & 7]) +#define SET_BURNT(c) (FIRE_T[(c) >> 3] |= bit8[(unsigned char)(c) & 7]) + +/* Tree type at a column: 0 round, 1 tall pine, 2 small pine, else none + (and none on a hill). */ +static unsigned char tree_at(unsigned c) +{ + return HL_H[c] ? 7 : (RND_T[c ^ 0x5A5] >> 3) & 7; +} + +static void gen_hills(unsigned c) +{ + unsigned char h = HL_H[c], r = RND_T[c ^ 0x5A5]; + + memset(colbuf, BLANK, HL_ROWS); + colbuf[6] = (r & 1) ? T_GRASS1 : T_GRASS2; + colbuf[7] = (r & 2) ? T_DIRT1 : T_DIRT2; + if (h) { + colbuf[6 - h] = hill_top[top_shape(HL_H[(c - 1) & WORLD_MASK], h, HL_H[(c + 1) & WORLD_MASK])]; + memset(colbuf + 7 - h, T_H_SOLID, h - 1); + } else { + r = (r >> 3) & 7; + if (r < 3 && IS_BURNT(c)) { /* lost to a fire */ + colbuf[5] = r ? T_PINE_TRUNK : T_TREE_TRUNK; + colbuf[4] = r ? T_BURNT_PINE : T_BURNT_TREE; + } + else if (r == 0) { colbuf[5] = T_TREE_TRUNK; colbuf[4] = T_TREE_TOP; } + else if (r == 1) { colbuf[5] = T_PINE_TRUNK; colbuf[4] = T_PINE_MID; colbuf[3] = T_PINE_TOP; } + else if (r == 2) { colbuf[5] = T_PINE_TRUNK; colbuf[4] = T_PINE_TOP; } + } +} + +/* --------------------------------------------------------------- scrolling */ + +static void (*const gen_fn[3])(unsigned) = { gen_clouds, gen_mtns, gen_hills }; +static unsigned char *const nb_buf[3] = { nb_clouds, nb_mtns, nb_hills }; +static const unsigned char band_row0[3] = { CL_ROW, MT_ROW, HL_ROW }; +static const unsigned char band_rows[3] = { CL_ROWS, MT_ROWS, HL_ROWS }; + +/* Self-check: every step must move a layer by exactly d/16 px. It catches an + IRQ corrupting scroll.s's temporaries, which shows up as jerky scrolling. */ +static unsigned char track_bad; +static unsigned char track_layer = 255; /* layer checked this frame; 255 = off */ + +static void layer_set(unsigned char i, unsigned px) +{ + lay_px_lo[i] = (unsigned char)px; + lay_px_hi[i] = px >> 8; + lay_frac[i] = 0; + lay_col_lo[i] = (unsigned char)(px >> 3); + lay_col_hi[i] = (px >> 11) & 0x0F; + lay_pend[i] = 0; + lay_xs[i] = MC_ON | (7 - ((unsigned char)px & 7)); +} + +/* Columns built ahead: the next one to scroll in from the right (column + col + 40) and from the left (col - 1), with the world column each holds. */ +#define NO_COL 0xFFFF +static unsigned char pre_r[3][10], pre_l[3][10]; +static unsigned pre_rc[3] = { NO_COL, NO_COL, NO_COL }; +static unsigned pre_lc[3] = { NO_COL, NO_COL, NO_COL }; +static unsigned pre_miss; /* steps that had to build on the spot */ + +/* Background job: build one missing ahead-column. Returns 1 if it did work. */ +static unsigned char prebuild(void) +{ + static unsigned char i; + unsigned char n; + unsigned want; + + for (n = 0; n < 3; ++n) { + if (++i > 2) i = 0; + want = (LAY_COL(i) + 40) & WORLD_MASK; + if (pre_rc[i] != want) { + gen_fn[i](want); + memcpy(pre_r[i], colbuf, band_rows[i]); + pre_rc[i] = want; + return 1; + } + want = (LAY_COL(i) - 1) & WORLD_MASK; + if (pre_lc[i] != want) { + gen_fn[i](want); + memcpy(pre_l[i], colbuf, band_rows[i]); + pre_lc[i] = want; + return 1; + } + } + return 0; +} + +/* Draw a whole band (at startup). */ +static void layer_fill(unsigned char i) +{ + unsigned char x, r, rows = band_rows[i]; + unsigned c = LAY_COL(i); + unsigned char *p; + + for (x = 0; x < 40; ++x) { + gen_fn[i]((c + x) & WORLD_MASK); + p = SCREEN + band_row0[i] * 40 + x; + for (r = 0; r < rows; ++r) { *p = colbuf[r]; p += 40; } + } +} + +/* Game-logic phase: move a layer by d/16 px and, if it crosses a character, + hand apply_scroll() the column for next frame (normally built already). */ +static void layer_advance(unsigned char i, signed char d) +{ + unsigned c, before; + + if (i == track_layer) before = (lay_px_lo[i] << 8) | lay_frac[i]; + move_d = d; + if (layer_step(i)) { + c = LAY_COL(i); + if (lay_pend[i] == 1) { + c = (c + 39) & WORLD_MASK; + if (pre_rc[i] == c) { memcpy(nb_buf[i], pre_r[i], band_rows[i]); goto done; } + } else { + if (pre_lc[i] == c) { memcpy(nb_buf[i], pre_l[i], band_rows[i]); goto done; } + } + gen_fn[i](c); + memcpy(nb_buf[i], colbuf, band_rows[i]); + ++pre_miss; + } +done: + if (i == track_layer && + (unsigned)((lay_px_lo[i] << 8) | lay_frac[i]) - before != (unsigned)(d << 4)) + ++track_bad; +} + +/* -------------------------------------------------------------- helicopter */ + +static int hx = 160; /* sprite X */ +static int hy16 = Y_MAX * 16; /* sprite Y in 1/16 px */ +static signed char vx, vy; +static unsigned char facing; /* 0 right, 1 left */ +static unsigned char tilt; /* 0 level, 1 nose down, 2 nose up */ +static unsigned char rotor, rotor_t, turn_t, landed = 1; +static unsigned char demo = 1; +static unsigned demo_t; +static unsigned char spr_pose, spr_shadow, spr_y; +static unsigned char lives = 3; +static unsigned char boom_t; /* explosion running */ +static unsigned char safe_t; /* blinking, can't be hit */ +static unsigned char game_over; +static unsigned start_px; /* hills position where this game started */ +static unsigned char water = WATER_MAX; +static unsigned char spraying; +static unsigned char fires_active, fires_out, trees_lost; +static unsigned char ignite_t = 120; +static unsigned fire_col[MAXH]; /* tree column of each fire slot */ +static unsigned fires_started; /* statistics for the test */ + +/* Scripted stick for attract mode (and the headless test). */ +static unsigned char demo_stick(void) +{ + unsigned t = demo_t & 1023; + if (t < 50) return JOY_UP; /* take off */ + if (t < 260) return JOY_RIGHT; /* cruise right */ + if (t < 330) return JOY_LEFT; /* brake, turn */ + if (t < 520) return JOY_LEFT | ((t & 64) ? JOY_DOWN : JOY_UP); + if (t < 560) return JOY_DOWN; + if (t < 640) return JOY_RIGHT | JOY_UP; + if (t < 700) return 0; /* hover */ + if (t < 900) return JOY_LEFT; + return JOY_DOWN; /* land */ +} + +static signed char toward_zero(signed char v, unsigned char step) +{ + if (v > (signed char)step) return v - step; + if (v < -(signed char)step) return v + step; + return 0; +} + +static void fly(unsigned char stick, unsigned char frame) +{ + signed char want = (stick & JOY_RIGHT) ? 1 : (stick & JOY_LEFT) ? -1 : 0; + int target; + unsigned char y; + + tilt = 0; + if (boom_t || game_over) { + vx = toward_zero(vx, 1); /* the wreck drifts to a stop */ + vy = 0; + } else if (landed) { + vx = 0; + if (stick & JOY_UP) { landed = 0; vy = -10; } + } else { + /* horizontal: push forward = nose down, push back = brake (nose up) then turn */ + if (turn_t) { + if (--turn_t == 5) facing ^= 1; + vx = toward_zero(vx, 1); + } else if (want) { + if ((want > 0) == (facing == 0)) { + tilt = 1; + vx += want; + if (vx > VX_MAX) vx = VX_MAX; + if (vx < -VX_MAX) vx = -VX_MAX; + } else if ((facing == 0 && vx > 10) || (facing == 1 && vx < -10)) { + tilt = 2; + vx = toward_zero(vx, 2); + } else { + turn_t = 11; + } + } else if (frame & 1) { + vx = toward_zero(vx, 1); + } + + /* vertical */ + if (stick & JOY_UP) { if ((vy -= 2) < -VY_MAX) vy = -VY_MAX; } + else if (stick & JOY_DOWN) { if ((vy += 2) > VY_MAX) vy = VY_MAX; } + else vy = toward_zero(vy, 1); + + hy16 += vy; + if (hy16 < Y_MIN * 16) { hy16 = Y_MIN * 16; vy = 0; } + if (hy16 >= Y_MAX * 16) { + hy16 = Y_MAX * 16; + vy = 0; + if (!(stick & JOY_UP)) { landed = 1; vx = 0; turn_t = 0; } + } + } + + /* camera: let the chopper drift back from the direction of travel */ + target = 160 - vx * 2; + if (hx < target) ++hx; else if (hx > target) --hx; + + /* rotor: fast when climbing, slow idle on the ground */ + rotor_t += landed ? 1 : (vy < 0 ? 4 : 2); + if (rotor_t >= 4) { rotor_t = 0; rotor ^= 1; } + + spr_pose = turn_t ? 12 + rotor : facing * 6 + tilt * 2 + rotor; + y = (unsigned char)(hy16 >> 4); + spr_y = y; + if (!landed && vx == 0 && vy == 0 && (frame & 16)) ++spr_y; /* hover bob */ + spr_shadow = (Y_MAX - y) > 60 ? SP_SHADOW_SMALL : SP_SHADOW_BIG; + + /* the world moves under the chopper: ground 1, mountains 1/2, clouds 1/4 + wind */ + layer_advance(L_HILLS, vx); + layer_advance(L_MTNS, vx >> 1); + layer_advance(L_CLOUDS, (vx >> 2) + 2); +} + +/* Fill the helicopter's shadow registers; mux.s applies them at line 251. */ +static void set_heli_sprites(unsigned char frame) +{ + unsigned char ptr = SPR_BASE + spr_pose * 2; + + hs_x = (unsigned char)hx; + hs_msb = (hx > 255) ? 0x07 : 0x00; + hs_y = spr_y; + hs_p0 = ptr; + hs_p1 = ptr + 1; + hs_p2 = SPR_BASE + spr_shadow; + hs_en = 0x07; + if (boom_t) { /* explosion in the fill sprite */ + hs_p0 = SPR_BASE + SP_BLANK; + hs_p1 = SPR_BASE + (boom_t > 32 ? SP_BOOM : boom_t > 16 ? SP_BOOM2 : SP_BOOM3); + hs_en = 0x03; + } else if (game_over) { + hs_en = 0; + } else if (safe_t && (frame & 4)) { + hs_en = 0x04; /* blink: shadow only */ + } +} + +/* --------------------------------------------------------------- hazards */ + +static unsigned char ri, rj; +static unsigned char spawn_t = 90; +static unsigned spawned; /* statistics for the test */ +static unsigned char peak_v; /* most hazards on screen at once */ +static unsigned mux_frames; /* frames that needed more than 5 sprites */ + +static unsigned char rand8(void) +{ + rj += 37; + return rnd[++ri] ^ rnd[rj]; +} + +static unsigned char free_slot(void) +{ + unsigned char i; + for (i = 0; i < MAXH; ++i) + if (hz_type[i] == HZ_NONE) return i; + return 255; +} + +static void place(unsigned char i, unsigned char type, unsigned x, unsigned char y, + signed char v, unsigned char t, unsigned char col) +{ + hz_xf[i] = 0; + hz_xlo[i] = (unsigned char)x; + hz_xhi[i] = x >> 8; + hz_y[i] = hz_base[i] = y; + hz_vx[i] = v; + hz_t[i] = t; + hz_col[i] = col; + hz_type[i] = type; + ++spawned; +} + +static const unsigned char balloon_cols[8] = { + COLOR_YELLOW, COLOR_ORANGE, COLOR_LIGHTGREEN, COLOR_LIGHTRED, + COLOR_PURPLE, COLOR_WHITE, COLOR_GREEN, COLOR_YELLOW +}; + +/* New hazards come in from the side the chopper is flying toward. */ +static void spawn_hazards(void) +{ + unsigned char r, side, i, n, y, t; + + if (--spawn_t) return; + r = rand8(); + spawn_t = 30 + (r & 63); + side = vx > 6 ? 1 : vx < -6 ? 0 : (r >> 7); /* 1 = right edge */ + y = 64 + (rand8() & 127); + if (y > 180) y -= 60; + t = rand8(); + + switch (r & 7) { + case 0: case 1: case 2: /* a flock of three birds */ + for (n = 0; n < 3; ++n) { + if ((i = free_slot()) == 255) return; + place(i, HZ_BIRD, + side ? 344 + n * 12 : 24 - n * 12, + n == 0 ? y : n == 1 ? y - 11 : y + 11, + side ? -12 : 12, t + n * 5, COLOR_BLACK); + } + break; + case 3: case 4: /* a balloon on the wind */ + if ((i = free_slot()) == 255) return; + if (y > 150) y = 150; + place(i, HZ_BALLOON, side ? 344 : 0, y, 2, t, balloon_cols[t & 7]); + break; + default: /* a jet at your altitude */ + if ((i = free_slot()) == 255) return; + y = spr_y + 2; + if (y < 56) y = 56; + if (y > 190) y = 190; + place(i, HZ_JET, side ? 344 : 0, y, side ? -40 : 40, t, COLOR_GRAY3); + break; + } +} + +static void clear_hazards(void) +{ + memset(hz_type, HZ_NONE, MAXH); + fires_active = 0; +} + +static void sound_boom(void) +{ + SID.amp = 0x0F; + SID.v1.freq = 0x0C00; + SID.v1.ad = 0x0A; /* fast attack, long decay */ + SID.v1.sr = 0x00; + SID.v1.ctrl = 0x80; + SID.v1.ctrl = 0x81; /* noise, gate on */ +} + +static unsigned char burning(unsigned c) +{ + unsigned char i; + for (i = 0; i < MAXH; ++i) + if (hz_type[i] == HZ_FIRE && fire_col[i] == c) return 1; + return 0; +} + +/* Set a tree on fire, near the edge the chopper is flying toward. */ +static void ignite_fire(void) +{ + unsigned col = LAY_COL(L_HILLS), c; + unsigned char r = rand8(), k, n, i, t; + + if (fires_active >= MAX_FIRES) return; + k = vx > 6 ? 24 + (r & 7) : vx < -6 ? 3 + (r & 7) : 6 + (r & 15); + for (n = 0; n < 12 && k < 38; ++n, ++k) { + c = (col + k) & WORLD_MASK; + t = tree_at(c); + if (t > 2 || IS_BURNT(c) || burning(c)) continue; + if ((i = free_slot()) == 255) return; + /* The fire sprite must round to the same pixel as its tree on every + frame: column k's left edge is at sprite X 31 + 8k - (px & 7), and + starting the fraction at 255 - frac keeps floor() in step with the + scroll's integer position (both then move by the same amount). */ + place(i, HZ_FIRE, 23 + k * 8 - (lay_px_lo[L_HILLS] & 7), + t == 1 ? 186 : 191, 0, r, COLOR_YELLOW); + hz_xf[i] = 255 - lay_frac[L_HILLS]; + hz_hp[i] = FIRE_HP; + fire_col[i] = c; + ++fires_active; + ++fires_started; + return; + } +} + +static void sound_pssh(void) +{ + SID.amp = 0x0F; + SID.v1.freq = 0x6000; /* high hiss */ + SID.v1.ad = 0x08; + SID.v1.sr = 0x00; + SID.v1.ctrl = 0x80; + SID.v1.ctrl = 0x81; +} + +static void spray_sound(unsigned char on) +{ + static unsigned char was; + if (on == was) return; + was = on; + SID.amp = 0x0F; + SID.v2.freq = 0x2800; + SID.v2.ad = 0x00; + SID.v2.sr = 0x60; /* steady hiss while the button is held */ + SID.v2.ctrl = on ? 0x81 : 0x80; +} + +static void reset_game(void) +{ + water = WATER_MAX; + fires_active = fires_out = trees_lost = 0; + ignite_t = 120; + lives = 3; + boom_t = 0; + safe_t = 60; + game_over = 0; + clear_hazards(); + spawn_t = 90; + hy16 = Y_MAX * 16; + landed = 1; + vx = vy = 0; + turn_t = 0; + start_px = LAY_PX(L_HILLS); +} + +/* Move the hazards, react to a hit, spawn more, and hand them to the multiplexer. */ +static void run_hazards(unsigned char stick, unsigned char frame) +{ + unsigned char i; + + /* water: fire button while flying; the tank refills on the ground */ + spraying = (stick & JOY_FIRE) && !landed && water && !boom_t && !game_over; + if (spraying && (frame & 1)) --water; + if (landed && water < WATER_MAX && (frame & 1)) ++water; + spray_sound(spraying); + spray_on = spraying; + spray_xlo = (unsigned char)hx; + spray_xhi = hx >> 8; + spray_y = spr_y; + + world_d = vx; + hit_on = !boom_t && !safe_t && !game_over; + hit_xlo = (unsigned char)hx; + hit_xhi = hx >> 8; + hit_y = spr_y; + haz_update(); + + if (hz_saved) { /* put out: steam is already rising */ + fires_out += hz_saved; + fires_active -= hz_saved; + sound_pssh(); + } + if (hz_lostany) { /* a fire got away: that tree is gone */ + for (i = 0; i < MAXH; ++i) + if (hz_type[i] == HZ_LOST) { + SET_BURNT(fire_col[i]); + pre_rc[L_HILLS] = pre_lc[L_HILLS] = NO_COL; /* may hold that tree */ + hz_type[i] = HZ_NONE; + ++trees_lost; + --fires_active; + } + } + v_on[SPRAY_SLOT] = spraying && spr_y <= 196; /* the water itself */ + if (v_on[SPRAY_SLOT]) { + i = SPRAY_SLOT; + v_y[i] = spr_y + 15; + v_x[i] = (unsigned char)hx; + v_flags[i] = (hx > 255 ? 1 : 0) | 2; + v_ptr[i] = SPR_BASE + SP_SPRAY + ((frame >> 1) & 1); + v_col[i] = COLOR_WHITE; + } + + if (hz_hit) { + hz_type[hz_hit - 1] = HZ_NONE; + boom_t = 48; + if (!demo) --lives; + sound_boom(); + } + if (boom_t) { + if (boom_t == 40) SID.v1.ctrl = 0x80; /* gate off */ + if (--boom_t == 0) { + if (lives == 0) game_over = 1; + else safe_t = 150; + } + } else if (safe_t) { + --safe_t; + } + + if (!game_over) { + spawn_hazards(); + if (ignite_t > 1) --ignite_t; /* at 1: background job (main loop) */ + } + + if (mux_shown + mux_dropped > peak_v) peak_v = mux_shown + mux_dropped; + if (mux_shown > 5) ++mux_frames; + mux_build(); +} + +/* ---------------------------------------------------------------- display */ + +static void put_text(unsigned char *p, const char *s) +{ + unsigned char ch; + while ((ch = *s++) != 0) { + if (ch >= 0x40 && ch < 0x60) ch -= 0x40; /* PETSCII letters -> screen codes */ + *p++ = ch; + } +} + +static void put_num(unsigned char *p, unsigned v, unsigned char digits) +{ + to_dec(v); + memcpy(p, decbuf + 5 - digits, digits); +} + +static void draw_hud_frame(void) +{ + memset(HUD1, BLANK, 80); /* both status rows */ + memset(COLRAM + HUD_ROW * 40, COLOR_WHITE, 80); + put_text(HUD1 + 1, "speed 00 alt 000 water 00 lives 3"); + memset(COLRAM + HUD_ROW * 40 + 20, COLOR_CYAN, 8); + memset(COLRAM + HUD_ROW * 40 + 30, COLOR_YELLOW, 7); +} + +/* Status messages, compiled straight to screen codes so showing one is a copy. */ +#pragma charmap (0x61, 0x01) +#pragma charmap (0x62, 0x02) +#pragma charmap (0x63, 0x03) +#pragma charmap (0x64, 0x04) +#pragma charmap (0x65, 0x05) +#pragma charmap (0x66, 0x06) +#pragma charmap (0x67, 0x07) +#pragma charmap (0x68, 0x08) +#pragma charmap (0x69, 0x09) +#pragma charmap (0x6A, 0x0A) +#pragma charmap (0x6B, 0x0B) +#pragma charmap (0x6C, 0x0C) +#pragma charmap (0x6D, 0x0D) +#pragma charmap (0x6E, 0x0E) +#pragma charmap (0x6F, 0x0F) +#pragma charmap (0x70, 0x10) +#pragma charmap (0x71, 0x11) +#pragma charmap (0x72, 0x12) +#pragma charmap (0x73, 0x13) +#pragma charmap (0x74, 0x14) +#pragma charmap (0x75, 0x15) +#pragma charmap (0x76, 0x16) +#pragma charmap (0x77, 0x17) +#pragma charmap (0x78, 0x18) +#pragma charmap (0x79, 0x19) +#pragma charmap (0x7A, 0x1A) +#define MSG_LEN 36 +static const char msg[4][MSG_LEN + 1] = { + "demo - press fire or move the stick ", + "landed - tank refills - up: take off", + "fires out 00 trees lost 00 ", + "game over - press fire to fly again ", +}; +/* back to cc65's normal PETSCII mapping for the rest of the file */ +#pragma charmap (0x61, 0x41) +#pragma charmap (0x62, 0x42) +#pragma charmap (0x63, 0x43) +#pragma charmap (0x64, 0x44) +#pragma charmap (0x65, 0x45) +#pragma charmap (0x66, 0x46) +#pragma charmap (0x67, 0x47) +#pragma charmap (0x68, 0x48) +#pragma charmap (0x69, 0x49) +#pragma charmap (0x6A, 0x4A) +#pragma charmap (0x6B, 0x4B) +#pragma charmap (0x6C, 0x4C) +#pragma charmap (0x6D, 0x4D) +#pragma charmap (0x6E, 0x4E) +#pragma charmap (0x6F, 0x4F) +#pragma charmap (0x70, 0x50) +#pragma charmap (0x71, 0x51) +#pragma charmap (0x72, 0x52) +#pragma charmap (0x73, 0x53) +#pragma charmap (0x74, 0x54) +#pragma charmap (0x75, 0x55) +#pragma charmap (0x76, 0x56) +#pragma charmap (0x77, 0x57) +#pragma charmap (0x78, 0x58) +#pragma charmap (0x79, 0x59) +#pragma charmap (0x7A, 0x5A) + +static void update_hud(unsigned char part) +{ + static unsigned char shown = 255; + static unsigned char shown_out = 255, shown_lost = 255; + unsigned char mode; + + switch (part & 3) { /* one field per frame keeps frames even */ + case 0: put_num(HUD1 + 7, (vx < 0 ? -vx : vx) * 3, 2); break; /* 0..96 */ + case 1: put_num(HUD1 + 15, (Y_MAX - (hy16 >> 4)) * 2, 3); break; + case 2: put_num(HUD1 + 26, water, 2); break; + case 3: + HUD1[36] = '0' + lives; + mode = demo ? 0 : game_over ? 3 : landed ? 1 : 2; + if (mode != shown) { + shown = mode; + shown_out = shown_lost = 255; + memcpy(HUD2 + 2, msg[mode], MSG_LEN); + } + if (mode == 2) { + if (fires_out != shown_out) put_num(HUD2 + 12, shown_out = fires_out, 2); + else if (trees_lost != shown_lost) put_num(HUD2 + 28, shown_lost = trees_lost, 2); + } + } +} + +static void setup_screen(void) +{ + unsigned char r, c; + + /* charset: copy the ROM set (keeps letters and digits), then add the tiles */ + __asm__("sei"); + *(unsigned char *)1 = 0x33; /* character ROM at $D000 */ + memcpy(CHARSET, (void *)0xD000, 0x800); + *(unsigned char *)1 = 0x37; + __asm__("cli"); + memcpy(CHARSET + FIRST_TILE * 8, tile_data, NUM_TILES * 8); + memcpy(SPRMEM, sprite_data, NUM_SPRITES * 64); + + VIC.bordercolor = COLOR_BLACK; + VIC.bgcolor0 = COLOR_LIGHTBLUE; + VIC.bgcolor1 = COLOR_WHITE; + VIC.bgcolor2 = COLOR_GRAY3; + CIA2.pra = (CIA2.pra & 0xFC) | 0x01; /* VIC bank 2 ($8000) */ + VIC.addr = 0x08; /* screen +$0000, chars +$2000 */ + VIC.ctrl2 = MC_ON | 7; + + /* colour RAM is fixed per row; only characters scroll (bit 3 = multicolor) */ + for (r = 0; r < HUD_ROW; ++r) { + c = r < MT_ROW ? 8 | COLOR_CYAN + : r < HL_ROW ? 8 | COLOR_BLUE + : r < 22 ? 8 | COLOR_GREEN + : 8 | COLOR_BLACK; + memset(COLRAM + r * 40, c, 40); + } + + memset(FIRE_T, 0, 512); /* no burnt trees yet */ + layer_set(L_CLOUDS, 200 * 8); + layer_set(L_MTNS, 600 * 8); + layer_set(L_HILLS, 1000 * 8); + start_px = 1000 * 8; + layer_fill(L_CLOUDS); + layer_fill(L_MTNS); + layer_fill(L_HILLS); + draw_hud_frame(); + + VIC.spr0_color = COLOR_BLACK; /* outline */ + VIC.spr1_color = COLOR_YELLOW; /* body */ + VIC.spr_mcolor0 = COLOR_CYAN; /* window */ + VIC.spr_mcolor1 = COLOR_RED; /* stripe */ + VIC.spr2_color = COLOR_BLACK; /* shadow */ + VIC.spr_exp_x = 0; + VIC.spr_exp_y = 0; + VIC.spr_bg_prio = 0; /* sprites in front of the scenery */ + spr_y = Y_MAX; + spr_shadow = SP_SHADOW_BIG; + set_heli_sprites(0); /* the IRQ applies these at line 251 */ +} + +/* --------------------------------------------------------------- self-test */ + +static unsigned char ok_step(unsigned char a, unsigned char b) +{ + return a + 1 >= b && a <= b + 1; +} + +static void self_test(void) +{ + unsigned c; + + /* the world tables at $5000-$7FFF must sit above the program's BSS */ + c64test_check((unsigned)_BSS_RUN__ + (unsigned)_BSS_SIZE__ <= DATA_LOW, 1); + + /* sub-pixel stepping and coarse-scroll detection (layer 0 as a scratch layer) */ + layer_set(0, 807); lay_frac[0] = 0xF0; /* 807 15/16 px, column 100 */ + move_d = 1; + c64test_check(layer_step(0) == 1 && LAY_PX(0) == 808 && lay_frac[0] == 0 && LAY_COL(0) == 101, 2); + move_d = -1; + c64test_check(layer_step(0) == 0xFF && LAY_PX(0) == 807 && lay_frac[0] == 0xF0, 3); + move_d = 0; + c64test_check(layer_step(0) == 0 && LAY_COL(0) == 100 && lay_xs[0] == (MC_ON | 0), 4); + move_d = 32; + c64test_check(layer_step(0) == 1 && LAY_PX(0) == 809, 5); + layer_set(0, 0); move_d = -16; /* wraps 0 -> column 4095 */ + c64test_check(layer_step(0) == 0xFF && LAY_COL(0) == WORLD_MASK, 11); + + /* rows that scroll.s does not shift must never change */ + for (c = 0; c < WORLD_COLS; c += 13) { + gen_clouds(c); + c64test_check(colbuf[4] == BLANK, 12); + gen_mtns(c); + c64test_check(colbuf[0] == BLANK && colbuf[9] == T_M_SOLID, 13); + gen_hills(c); + c64test_check(colbuf[0] == BLANK && colbuf[1] == BLANK && colbuf[2] == BLANK, 14); + } + + /* terrain: in range and continuous everywhere, including the wrap 4095 -> 0 */ + for (c = 0; c < WORLD_COLS; ++c) { + c64test_check(MT_H[c] >= 2 && MT_H[c] <= 9, 6); + c64test_check(HL_H[c] <= 3, 7); + c64test_check(ok_step(MT_H[c], MT_H[(c + 1) & WORLD_MASK]), 8); + c64test_check(ok_step(HL_H[c], HL_H[(c + 1) & WORLD_MASK]), 9); + } + + /* multiplexer: five sprites in one band use every hardware sprite; two more + lower down re-use them; one more crowding the first band must be dropped */ + { + static const unsigned char ys[8] = { 100, 60, 62, 64, 66, 68, 110, 70 }; + unsigned char i; + for (i = 0; i < 8; ++i) { + v_on[i] = 1; v_y[i] = ys[i]; v_x[i] = 100; v_flags[i] = 0; v_ptr[i] = 0; v_col[i] = 0; + } + mux_build(); + c64test_check(mux_shown == 7 && mux_dropped == 1, 15); + v_y[0] = 50; v_y[6] = 58; /* re-ordered next frame */ + mux_build(); + c64test_check(mux_shown == 5 && mux_dropped == 3, 29); /* 50..64 fill all five */ + memset(v_on, 0, MAXH + 1); + mux_build(); + c64test_check(mux_shown == 0 && mux_dropped == 0, 16); + } + + /* fires: water puts one out after FIRE_HP frames, and only within reach */ + clear_hazards(); + world_d = 0; hit_on = 0; + spray_xlo = 100; spray_xhi = 0; spray_y = 191 - 30; spray_on = 1; + place(0, HZ_FIRE, 100, 191, 0, 0, COLOR_YELLOW); + hz_hp[0] = FIRE_HP; + for (c = 1; c < FIRE_HP; ++c) haz_update(); + c64test_check(hz_type[0] == HZ_FIRE && fire_near && !hz_saved, 17); + haz_update(); + c64test_check(hz_type[0] == HZ_STEAM && hz_saved == 1, 18); + place(1, HZ_FIRE, 100, 191, 0, 0, COLOR_YELLOW); + hz_hp[1] = FIRE_HP; + spray_y = 191 - 60; /* too high: water can't reach */ + haz_update(); + c64test_check(hz_hp[1] == FIRE_HP && !fire_near, 19); + /* a fire carried off the left edge is reported lost */ + clear_hazards(); + place(0, HZ_FIRE, 3, 191, 0, 0, COLOR_YELLOW); + hz_hp[0] = FIRE_HP; + world_d = 32; /* flying right at full speed */ + haz_update(); haz_update(); + c64test_check(hz_type[0] == HZ_LOST && hz_lostany, 28); + clear_hazards(); + memset(v_on, 0, MAXH + 1); + spray_on = 0; + spawned = 0; + + /* number formatting */ + to_dec(8190); + c64test_check(memcmp(decbuf, "\x30\x38\x31\x39\x30", 5) == 0, 10); +} + +/* --------------------------------------------------------------------- main */ + +static unsigned char late; /* frames whose work ran past line 251 */ +static unsigned char missed; /* band shifts that missed their raster split */ +static unsigned busiest; /* most raster lines one frame's work took */ + +/* Current raster line as lines elapsed since LINE_TOP (0..311). */ +static unsigned since_top(void) +{ + unsigned ln = VIC.rasterline | ((VIC.ctrl1 & 0x80) << 1); + return ln >= LINE_TOP ? ln - LINE_TOP : ln + (312 - LINE_TOP); +} + +int main(void) +{ + unsigned char last, frame = 0, stick, raw, fire_was = 0, hud_part = 0; + + build_world(); + self_test(); + BLNSW = 1; + setup_screen(); + irq_init(); + last = irq_frame; + + for (;;) { + while (irq_frame == last) { } + last = irq_frame; + ++frame; + + /* --- vertical blank: apply what the last frame computed (scroll.s) */ + apply_scroll(); + if (since_top() >= 312 - LINE_TOP + LINE_MTNS - 4) ++missed; + + /* --- game logic for the next frame */ + raw = ~JOY2 & 0x1F; + if (demo && raw) { demo = 0; reset_game(); } + if (game_over && (raw & JOY_FIRE) && !(fire_was & JOY_FIRE)) reset_game(); + fire_was = raw; + if (demo) { + stick = demo_stick(); + ++demo_t; + } else { + stick = raw; + } + /* demo (and the headless test): sample one layer's steps per frame */ + track_layer = demo ? (track_layer >= 2 ? 0 : track_layer + 1) : 255; + if (demo && fire_near) stick |= JOY_FIRE; /* the demo pilot uses water too */ + fly(stick, frame); + run_hazards(stick, frame); + set_heli_sprites(frame); + + /* background jobs, only while this frame has time to spare (line 251 + comes at 312 - 251 + 251 = 312 lines; each job is under ~60 lines) */ + if (since_top() < BG_LIMIT) { + if (ignite_t == 1 && !game_over) { + ignite_t = 100 + (rand8() & 127); + ignite_fire(); + } else if (!prebuild()) { + update_hud(hud_part++); + } + } + if (irq_frame != last) ++late; + else if (since_top() > busiest) busiest = since_top(); + + if (demo) { + /* headless checks; the $D7FF stores do nothing on a real C64 */ + if (demo_t == 200) /* heading right, scenery moving */ + c64test_check(!landed && facing == 0 && + LAY_COL(L_HILLS) != 1000 && LAY_COL(L_MTNS) != 600, 20); + if (demo_t == 400) /* turned around, flying left */ + c64test_check(facing == 1, 21); + if (demo_t == 1000) { /* the whole demo route, every frame on time */ + *(unsigned char *)0x02FE = late; + *(unsigned char *)0x02FF = missed; + *(unsigned *)0x02FC = busiest; + *(unsigned *)0x02FA = spawned; + *(unsigned char *)0x02F9 = peak_v; + *(unsigned *)0x02F7 = mux_frames; + *(unsigned char *)0x02F6 = track_bad; + c64test_check(track_bad == 0, 26); /* scroll positions never jump */ + c64test_check(late == 0, 22); + c64test_check(missed == 0, 23); + c64test_check(spawned >= 15, 24); /* hazards keep coming */ + c64test_check(peak_v > 5 && mux_frames > 0, 25); /* multiplexing used */ + *(unsigned *)0x02F4 = fires_started; + *(unsigned char *)0x02F3 = fires_out; + *(unsigned *)0x02F0 = pre_miss; + c64test_check(fires_started >= 3, 27); /* trees keep catching fire */ + update_hud(0); update_hud(1); update_hud(2); + c64test_pass(); + } + } + } +} diff --git a/projects/chopper/src/mux.s b/projects/chopper/src/mux.s new file mode 100644 index 0000000..311a207 --- /dev/null +++ b/projects/chopper/src/mux.s @@ -0,0 +1,480 @@ +; mux.s - one raster IRQ scheduler for the parallax band splits AND a sprite +; multiplexer (PAL). +; +; Hardware sprites 0-2 are the helicopter (outline, fill, shadow) and are set +; once per frame at line 251. Sprites 3-7 are shared by up to MAXV "virtual" +; hazard sprites: after a hardware sprite has finished drawing one hazard, the +; IRQ re-points it at another hazard lower down the screen. +; +; Virtual sprites live in fixed slots (v_on[slot] = shown this frame): slots +; 0-9 are the hazards, slot 10 the water spray. Each frame mux_build sorts the +; active slots by Y. The order is kept from frame to frame, and things move +; only a little between frames, so the insertion sort is nearly free. In Y +; order, the hardware sprite that frees up first is always the one used five +; sprites earlier, so sprites are handed out round-robin. Each re-use becomes +; an event, merged with the band splits into one list sorted by raster line, +; which the IRQ walks. Lists are double buffered: the IRQ swaps to the new one +; at line 251, so a half-built list is never shown. +; +; Band splits have windows of rows that never change (see scroll.s), so a split +; delayed a line or two by a sprite event leaves no visible seam: +; mountains: line 90 (rows 4-5 are always sky, lines 83-98) +; hills: line 164 (row 14 is always solid rock, lines 163-170) +; status: line 233 (last two lines of row 22 are solid black) +; Hazards never go below line ~212, so no sprite event lands near the status bar. + + .export _irq_init, _irq_frame, _xs_clouds, _xs_mtns, _xs_hills + .export _mux_build, _mux_shown, _mux_dropped + .export _v_on, _v_y, _v_x, _v_flags, _v_ptr, _v_col + .export _hs_x, _hs_msb, _hs_y, _hs_p0, _hs_p1, _hs_p2, _hs_en + .importzp tmp1, tmp2, tmp3, tmp4 + +MAXV = 9 ; virtual sprites: 8 hazards + the water spray +EVB = 16 ; events per list buffer: MAXV sprites + 3 bands + end +HWN = 5 ; hardware sprites 3-7 +SPRPTR = $83F8 ; screen at $8000 +SHADOW_Y = 220 + +K_SPR = 0 +K_MTNS = 1 +K_HILLS = 2 +K_HUD = 3 +K_TOP = 4 + +LINE_TOP = 251 +LINE_MTNS = 90 +LINE_HILLS = 164 +LINE_HUD = 233 + +SKY = 14 +MC_ON = $10 + + .segment "DATA" +_irq_frame: .byte 0 +_xs_clouds: .byte MC_ON | 7 +_xs_mtns: .byte MC_ON | 7 +_xs_hills: .byte MC_ON | 7 +front: .byte EVB ; list the IRQ plays +back: .byte 0 ; list mux_build writes + + .segment "BSS" +; virtual sprites, one slot each, filled by the game every frame +_v_on: .res MAXV ; non-zero: show this slot +_v_y: .res MAXV +_v_x: .res MAXV ; X low byte +_v_flags: .res MAXV ; bit 0 = X bit 8, bit 1 = multicolor +_v_ptr: .res MAXV +_v_col: .res MAXV +; helicopter shadow registers, applied at line 251 +_hs_x: .res 1 +_hs_msb: .res 1 ; $D010 bits 0-2 +_hs_y: .res 1 +_hs_p0: .res 1 +_hs_p1: .res 1 +_hs_p2: .res 1 +_hs_en: .res 1 ; $D015 bits 0-2 +; statistics of the last build +_mux_shown: .res 1 +_mux_dropped: .res 1 + +ready: .res 1 ; back list is complete +irq_tmp: .res 1 ; the IRQ's own scratch byte +cur: .res 1 ; index of the next event +order: .res MAXV ; active slots in Y order, kept between frames +ordn: .res 1 +inlist: .res MAXV ; slot is in order[] +ring: .res 1 ; next hardware sprite to hand out +hwfree: .res HWN ; raster line each hardware sprite is free (0 = unused) +hwen: .res 1 +bandi: .res 1 +evw: .res 1 +; event lists (two buffers of EVB) +ev_line: .res 2*EVB +ev_kind: .res 2*EVB +ev_hw: .res 2*EVB +ev_y: .res 2*EVB +ev_x: .res 2*EVB +ev_flags: .res 2*EVB +ev_ptr: .res 2*EVB +ev_col: .res 2*EVB +ev_en: .res 2*EVB ; only [buffer base] used: sprites 3-7 enable bits + + .segment "RODATA" +band_line: .byte LINE_MTNS, LINE_HILLS, LINE_HUD +band_kind: .byte K_MTNS, K_HILLS, K_HUD +hw_bit: .byte $08, $10, $20, $40, $80 +hw_clr: .byte $F7, $EF, $DF, $BF, $7F + + .segment "CODE" + +; --------------------------------------------------------------------------- +; void irq_init(void); +_irq_init: + sei + lda #$7F + sta $DC0D ; no CIA timer IRQs + lda $DC0D + jsr _mux_build ; v_on all 0: just the band splits + jsr swap + ldx front ; start at this list's end-of-frame (line 251) event +: lda ev_kind,x + cmp #K_TOP + beq :+ + inx + bne :- +: stx cur + lda #irq ; and skipping the Kernal's IRQ entry and exit + sta $FFFF ; saves ~30 cycles on each of ~14 IRQs a frame + lda #nmi + sta $FFFB + lda #$35 ; RAM at $A000-$BFFF and $E000-$FFFF, I/O stays + sta $01 + lda $D011 + and #$7F + sta $D011 + lda #LINE_TOP + sta $D012 + lda #1 + sta $D01A + sta $D019 + cli + rts + +swap: lda ready + beq :+ + lda front + ldy back + sty front + sta back + lda #0 + sta ready +: rts + +; --------------------------------------------------------------------------- +; void mux_build(void); - build the next frame's event list from the v_* slots. +_mux_build: + lda #0 + sta ready ; the IRQ must not swap to a half-built list + sta _mux_shown + sta _mux_dropped + sta hwen + sta bandi + sta ring + ldx #HWN-1 +: sta hwfree,x + dex + bpl :- + + ; 1. drop slots that are no longer shown, keeping the old order + ldx #0 + ldy #0 +@keep: cpx ordn + bcs @kept + lda order,x + stx tmp2 + tax + lda _v_on,x + beq @gone + txa + sta order,y + iny + bne @knext ; always +@gone: lda #0 + sta inlist,x +@knext: ldx tmp2 + inx + bne @keep +@kept: sty ordn + + ; 2. append slots that just appeared + ldx #0 +@add: lda _v_on,x + beq @anext + lda inlist,x + bne @anext + lda #1 + sta inlist,x + txa + ldy ordn + sta order,y + inc ordn +@anext: inx + cpx #MAXV + bne @add + + ; 3. insertion sort by Y (nearly sorted already) + ldx #1 +@outer: cpx ordn + bcs @sorted + lda order,x + sta tmp1 ; key slot + tay + lda _v_y,y + sta tmp2 ; key y + txa + tay ; Y = j +@inner: lda order-1,y + sta tmp3 + stx tmp4 + tax + lda _v_y,x + ldx tmp4 + cmp tmp2 + bcc @place + beq @place + lda tmp3 + sta order,y + dey + bne @inner +@place: lda tmp1 + sta order,y + inx + bne @outer + +@sorted: + lda back + sta evw + ldx #0 ; k +@vloop: cpx ordn + bcc :+ + jmp @bands +: stx tmp3 + lda order,x + sta tmp4 ; slot + + ldy ring ; the hardware sprite that frees up first + sty tmp2 + lda hwfree,y + sta tmp1 + beq @fits ; not used yet this frame: set at the top + ldx tmp4 ; re-use: set up at least 3 lines before it starts + clc + adc #3 + cmp _v_y,x + bcc @fits + beq @fits + inc _mux_dropped + jmp @vnext + +@fits: ; first emit band splits that come before this event + lda tmp1 + beq @emit +@bpend: ldy bandi + cpy #3 + bcs @emit + lda band_line,y + cmp tmp1 + bcs @emit ; band line > event line: later + jsr emit_band + jmp @bpend + +@emit: ldy evw + lda tmp1 + sta ev_line,y + lda #K_SPR + sta ev_kind,y + lda tmp2 + sta ev_hw,y + ldx tmp4 + lda _v_y,x + sta ev_y,y + clc + adc #22 ; free again after 21 lines + ldx tmp2 + sta hwfree,x + lda hw_bit,x + ora hwen + sta hwen + inx ; next in the ring + cpx #HWN + bne :+ + ldx #0 +: stx ring + ldx tmp4 + lda _v_x,x + sta ev_x,y + lda _v_flags,x + sta ev_flags,y + lda _v_ptr,x + sta ev_ptr,y + lda _v_col,x + sta ev_col,y + inc evw + inc _mux_shown + +@vnext: ldx tmp3 + inx + jmp @vloop + +@bands: ldy bandi ; remaining band splits, then end of frame + cpy #3 + bcs @end + jsr emit_band + jmp @bands +@end: ldy evw + lda #LINE_TOP + sta ev_line,y + lda #K_TOP + sta ev_kind,y + ldy back + lda hwen + sta ev_en,y + lda #1 + sta ready + rts + +emit_band: ; Y = bandi + lda band_line,y + ldx band_kind,y + ldy evw + sta ev_line,y + txa + sta ev_kind,y + inc evw + inc bandi + rts + +; --------------------------------------------------------------------------- +; The IRQ: run every event that is due, then arm the raster compare for the next. +nmi: rti ; RESTORE key: ignored + +irq: pha + txa + pha + tya + pha + lda #1 + sta $D019 +@next: ldx cur + lda ev_kind,x + bne @notspr + jsr spr_setup + jmp @adv +@notspr: + cmp #K_MTNS + bne :+ + lda _xs_mtns + sta $D016 + lda #4 ; purple: lit mountain slopes + sta $D023 + jmp @adv +: cmp #K_HILLS + bne :+ + lda _xs_hills + sta $D016 + lda #6 ; blue plain behind the hills + sta $D021 + lda #13 ; light green + sta $D022 + lda #9 ; brown + sta $D023 + jmp @adv +: cmp #K_HUD + bne @top + lda #MC_ON + sta $D016 + lda #0 + sta $D021 + jmp @adv + +@top: jsr swap ; line 251: start the next frame + lda _hs_x + sta $D000 + sta $D002 + sta $D004 + lda _hs_y + sta $D001 + sta $D003 + lda #SHADOW_Y + sta $D005 + lda _hs_p0 + sta SPRPTR + lda _hs_p1 + sta SPRPTR+1 + lda _hs_p2 + sta SPRPTR+2 + lda _hs_msb + sta $D010 + lda #$02 ; helicopter fill is multicolor + sta $D01C + ldx front + lda ev_en,x + ora _hs_en + sta $D015 + lda _xs_clouds + sta $D016 + lda #SKY + sta $D021 + lda #1 ; white + sta $D022 + lda #15 ; light grey + sta $D023 + inc _irq_frame + ldx front ; first event of the new list + stx cur + lda ev_line,x + sta $D012 + jmp irq_exit + +@adv: inx + stx cur + lda ev_line,x + bit $D011 ; raster >= 256: nothing in this list is due yet + bmi @arm + ldy $D012 + cpy #LINE_TOP + bcs @arm + sta irq_tmp ; never cc65's tmp1-4: the IRQ can land in the + tya ; middle of code that is using them + clc + adc #2 + cmp irq_tmp ; raster + 2 >= event line: do it now + bcc :+ + jmp @next +: lda irq_tmp +@arm: sta $D012 + jmp irq_exit + +irq_exit: + pla + tay + pla + tax + pla + rti + +; X = event index (preserved) +spr_setup: + ldy ev_hw,x + lda ev_ptr,x + sta SPRPTR+3,y + lda ev_col,x + sta $D027+3,y + lda ev_flags,x + lsr a ; C = X bit 8 + lda $D010 + and hw_clr,y + bcc :+ + ora hw_bit,y +: sta $D010 + lda ev_flags,x + and #2 + beq :+ + lda $D01C + ora hw_bit,y + bne :++ +: lda $D01C + and hw_clr,y +: sta $D01C + tya + asl a + tay + lda ev_x,x + sta $D006,y + lda ev_y,x + sta $D007,y + rts diff --git a/projects/chopper/src/scroll.s b/projects/chopper/src/scroll.s new file mode 100644 index 0000000..96d995b --- /dev/null +++ b/projects/chopper/src/scroll.s @@ -0,0 +1,205 @@ +; scroll.s - the time-critical part of the parallax engine. +; +; Three layers (0 clouds, 1 mountains, 2 hills). Each has a 24-bit position: +; px (16-bit pixels) + frac (1/256 px). The C code moves a layer with +; layer_step(); when the layer crosses a character boundary it builds the new +; column into nb_, and apply_scroll() (called right after line 251) +; shifts the band one character and drops the column in. +; +; Only rows that can change are shifted. The generators guarantee: +; clouds rows 0-3 vary, row 4 always sky +; mountains rows 6-13 vary, row 5 always sky, row 14 always solid rock +; hills rows 18-22 vary, rows 15-17 always sky (hills and trees are low) +; main.c's self-test checks these invariants. + + .export _layer_step, _apply_scroll, _move_d + .export _lay_px_lo, _lay_px_hi, _lay_frac, _lay_col_lo, _lay_col_hi + .export _lay_pend, _lay_xs + .export _nb_clouds, _nb_mtns, _nb_hills + .import _xs_clouds, _xs_mtns, _xs_hills + .importzp tmp1, tmp2, tmp3, tmp4 + +SCREEN = $8000 +VIC_D016 = $D016 + + .segment "BSS" +_move_d: .res 1 ; signed step in 1/16 px, input to layer_step +_lay_px_lo: .res 3 +_lay_px_hi: .res 3 +_lay_frac: .res 3 +_lay_col_lo:.res 3 ; world column at screen column 0 (12 bits) +_lay_col_hi:.res 3 +_lay_pend: .res 3 ; coarse step for apply_scroll: 0, 1 (left), $FF (right) +_lay_xs: .res 3 ; $D016 value for the band +_nb_clouds: .res 5 ; incoming column, indexed by band row +_nb_mtns: .res 10 +_nb_hills: .res 8 + + .segment "CODE" + +; unsigned char __fastcall__ layer_step(unsigned char layer); +; Adds move_d/16 px to the layer. Returns (and stores in lay_pend) the coarse +; step: 0 none, 1 = contents move left, $FF = contents move right. +_layer_step: + tax + lda _move_d + sta tmp1 + asl a + asl a + asl a + asl a + sta tmp2 ; low byte of move_d * 16 + lda tmp1 + cmp #$80 + ror a + cmp #$80 + ror a + cmp #$80 + ror a + cmp #$80 + ror a + sta tmp3 ; high byte (arithmetic move_d >> 4) + ldy #0 + cmp #$80 + bcc :+ + dey +: sty tmp4 ; sign extension + clc + lda _lay_frac,x + adc tmp2 + sta _lay_frac,x + lda _lay_px_lo,x + adc tmp3 + sta _lay_px_lo,x + lda _lay_px_hi,x + adc tmp4 + sta _lay_px_hi,x + + lda _lay_px_lo,x ; $D016 = multicolor | (7 - (px & 7)) + and #7 + eor #$17 + sta _lay_xs,x + + lda _lay_px_hi,x ; new column = (px >> 3) & $0FFF + sta tmp2 + lda _lay_px_lo,x + lsr tmp2 + ror a + lsr tmp2 + ror a + lsr tmp2 + ror a + sta tmp1 + lda tmp2 + and #$0F + sta tmp2 + + lda tmp1 ; diff = new - old (mod 4096) + sec + sbc _lay_col_lo,x + sta tmp3 + lda tmp2 + sbc _lay_col_hi,x + and #$0F + sta tmp4 + ora tmp3 + beq @none + lda tmp4 + bne @back + lda tmp3 + cmp #1 + bne @back + lda #1 + bne @store +@back: lda #$FF + bne @store +@none: lda #0 +@store: sta _lay_pend,x + lda tmp1 + sta _lay_col_lo,x + lda tmp2 + sta _lay_col_hi,x + lda _lay_pend,x + ldx #0 + rts + +; Shift rows [row0, row0+nrows) one character; store the new column from +; buf[boff..] at column 39 (left) or column 0 (right). +.macro SHIFT_L row0, nrows, buf, boff + ldx #0 +: + .repeat nrows, I + lda SCREEN + (row0 + I) * 40 + 1, x + sta SCREEN + (row0 + I) * 40, x + .endrepeat + inx + cpx #39 + bne :- + .repeat nrows, I + lda buf + boff + I + sta SCREEN + (row0 + I) * 40 + 39 + .endrepeat + rts +.endmacro + +.macro SHIFT_R row0, nrows, buf, boff + ldx #38 +: + .repeat nrows, I + lda SCREEN + (row0 + I) * 40, x + sta SCREEN + (row0 + I) * 40 + 1, x + .endrepeat + dex + bpl :- + .repeat nrows, I + lda buf + boff + I + sta SCREEN + (row0 + I) * 40 + .endrepeat + rts +.endmacro + +clouds_l: SHIFT_L 0, 4, _nb_clouds, 0 +clouds_r: SHIFT_R 0, 4, _nb_clouds, 0 +mtns_l: SHIFT_L 6, 8, _nb_mtns, 1 +mtns_r: SHIFT_R 6, 8, _nb_mtns, 1 +hills_l: SHIFT_L 18, 5, _nb_hills, 3 +hills_r: SHIFT_R 18, 5, _nb_hills, 3 + +; void apply_scroll(void); - call once per frame, right after line 251. +; Top band first; each band's $D016 value is published right after its shift, +; ahead of the raster IRQ that loads it. +_apply_scroll: + lda _lay_pend+0 + beq @c + bmi :+ + jsr clouds_l + jmp @c0 +: jsr clouds_r +@c0: lda #0 + sta _lay_pend+0 +@c: lda _lay_xs+0 + sta _xs_clouds + sta VIC_D016 ; clouds start at line 51: set it directly + + lda _lay_pend+1 + beq @m + bmi :+ + jsr mtns_l + jmp @m0 +: jsr mtns_r +@m0: lda #0 + sta _lay_pend+1 +@m: lda _lay_xs+1 + sta _xs_mtns ; IRQ at line 89 + + lda _lay_pend+2 + beq @h + bmi :+ + jsr hills_l + jmp @h0 +: jsr hills_r +@h0: lda #0 + sta _lay_pend+2 +@h: lda _lay_xs+2 + sta _xs_hills ; IRQ at line 169 + rts diff --git a/projects/chopper/tools/gen_gfx.py b/projects/chopper/tools/gen_gfx.py new file mode 100644 index 0000000..a97e428 --- /dev/null +++ b/projects/chopper/tools/gen_gfx.py @@ -0,0 +1,473 @@ +#!/usr/bin/env python3 +""" +gen_gfx.py - build src/gfx.h (sprites + landscape tiles) for the chopper game. + + python projects/chopper/tools/gen_gfx.py + +Sprites + Each helicopter pose is two sprites stacked at the same position: + * outline: hi-res, black (sprite 0, in front) + * fill: multicolor (sprite 1, behind) body = own colour, + window = $D025, stripe = $D026 + Poses = facing (right, left) x tilt (level, nose-down, nose-up) x rotor (A, B) + + front view (A, B) used while turning around. + Tilts are produced by shearing the level art; left-facing art is a mirror. + +Tiles + Multicolor characters, 4 double-wide pixels x 8 rows: + '.' = 00 background ($D021) 'w' = 01 ($D022) + 'g' = 10 ($D023) '#' = 11 colour RAM +""" +import os + +W, H = 24, 21 + +# ---------------------------------------------------------------- helicopter art +# '#' outline 'b' body fill 'w' window 's' stripe ' ' empty +SIDE_BODY = [ + "", # 0 + "", # 1 + "", # 2 + "", # 3 rotor goes here + " ##", # 4 mast + " ######", # 5 + " # #bbbbbb##", # 6 + "#b# #bbbbbbbbb##", # 7 + "#b# #bbbbbbbbbbww#", # 8 + "#bb####bbbbbbbbbbwwww#", # 9 + "#bbbbbbbssssssssbbwwww#", # 10 + "#b######ssssssssbbbwwww#", # 11 + " # #bbbbbbbbbbbbbbb#", # 12 + " #bbbbbbbbbbbbb#", # 13 + " ##############", # 14 + " # #", # 15 + " ###############", # 16 skid +] +ROTOR_A = {3: "########################"} # full blade, side on +ROTOR_B = {3: " ############"} # blade turned toward us +TAIL_A = {4: " #", 5: " #", 12: "", 13: " #"} # tail rotor vertical +TAIL_B = {8: "#b#"} # (drawn by body) - horizontal hint +TAIL_B_EXTRA = [(0, 5), (2, 5), (0, 13), (2, 13)] + +FRONT_BODY = [ + "", "", "", "", + " ##", # 4 + " ########", # 5 + " #bbbbbbbb#", # 6 + " #wwwwwwwwww#", # 7 + " #wwwwwwwwwwww#", # 8 + " #wwwwwwwwwwww#", # 9 + " #bwwwwwwwwwwb#", # 10 + " #ssssssssssss#", # 11 + " #bbbbbbbbbb#", # 12 + " ##########", # 13 + " # #", # 14 + " ## ##", # 15 + " ### ###", # 16 +] +FRONT_ROTOR_A = {3: " ####################"} +FRONT_ROTOR_B = {3: " ########"} + + +def blank(): + return [[" "] * W for _ in range(H)] + + +def stamp(grid, rows, only_hash=False): + for y, line in rows.items() if isinstance(rows, dict) else enumerate(rows): + for x, ch in enumerate(line): + if ch != " " and x < W: + grid[y][x] = ch + + +def side_pose(rotor): + g = blank() + stamp(g, SIDE_BODY) + if rotor == 0: + stamp(g, ROTOR_A) + stamp(g, TAIL_A) + else: + stamp(g, ROTOR_B) + for x, y in TAIL_B_EXTRA: + g[y][x] = "#" + return g + + +def front_pose(rotor): + g = blank() + stamp(g, FRONT_BODY) + stamp(g, FRONT_ROTOR_A if rotor == 0 else FRONT_ROTOR_B) + return g + + +def shear(g, sign): + """sign=+1: nose (right side) down; sign=-1: nose up.""" + out = blank() + for y in range(H): + for x in range(W): + if g[y][x] == " ": + continue + d = ((x - 12) // 8) if sign > 0 else ((11 - x) // 8) + ny = y + d + assert 0 <= ny < H, (x, y, ny) + out[ny][x] = g[y][x] + return out + + +def mirror(g): + return [list(reversed(r)) for r in g] + + +def outline_sprite(g): + data = [] + for y in range(H): + bits = 0 + for x in range(W): + bits = (bits << 1) | (1 if g[y][x] == "#" else 0) + data += [(bits >> 16) & 255, (bits >> 8) & 255, bits & 255] + return data + [0] + + +# multicolor sprite pair codes: 01=$D025 (window), 10=own colour (body), 11=$D026 (stripe) +MC_CODE = {"w": 1, "b": 2, "s": 3} + + +def fill_sprite(g): + data = [] + for y in range(H): + bits = 0 + for px in range(12): + a, b = g[y][2 * px], g[y][2 * px + 1] + cands = [c for c in (a, b) if c in MC_CODE] + if not cands: + # pair is outline/empty: fill under outline so no gaps show + code = 2 if (" " not in (a, b) and inside(g, y, 2 * px)) else 0 + else: + code = MC_CODE[cands[0]] + bits = (bits << 2) | code + data += [(bits >> 16) & 255, (bits >> 8) & 255, bits & 255] + return data + [0] + + +def inside(g, y, x): + """True if (x..x+1, y) is an outline pixel with fill on both sides horizontally.""" + row = g[y] + left = any(c in MC_CODE for c in row[:x]) + right = any(c in MC_CODE for c in row[x + 2:]) + return left and right + + +def shadow_sprite(width): + data = [] + for y in range(H): + bits = 0 + if y < 3: + half = width // 2 - (2 if y != 1 else 0) + for x in range(W): + on = abs(x - 11.5) < half and ((x + y) & 1) == 0 + bits = (bits << 1) | (1 if on else 0) + data += [(bits >> 16) & 255, (bits >> 8) & 255, bits & 255] + return data + [0] + + +sprites = [] # list of 64-byte blocks +names = [] +for facing in range(2): # 0 right, 1 left + for tilt in range(3): # 0 level, 1 nose down, 2 nose up + for rotor in range(2): + g = side_pose(rotor) + if tilt == 1: + g = shear(g, +1) + elif tilt == 2: + g = shear(g, -1) + if facing == 1: + g = mirror(g) + sprites.append(outline_sprite(g)) + sprites.append(fill_sprite(g)) + names.append(f"f{facing} t{tilt} r{rotor}") +for rotor in range(2): + g = front_pose(rotor) + sprites.append(outline_sprite(g)) + sprites.append(fill_sprite(g)) + names.append(f"front r{rotor}") +sprites.append(shadow_sprite(20)) +sprites.append(shadow_sprite(12)) +assert len(sprites) == 30 + +# ---------------------------------------------------------------- hazards +# Hazards are single sprites. Hi-res art: '#' set. Multicolor art is drawn at +# 12 double-wide pixels: '.' 00, 'w' 01 ($D025 cyan), 'b' 10 own colour, 's' 11 ($D026 red). +def hires_sprite(rows): + data = [] + for y in range(H): + line = rows[y] if y < len(rows) else "" + assert len(line) <= W, line + bits = 0 + for x in range(W): + bits = (bits << 1) | (1 if x < len(line) and line[x] == "#" else 0) + data += [(bits >> 16) & 255, (bits >> 8) & 255, bits & 255] + return data + [0] + + +def mc_sprite(rows): + data = [] + for y in range(H): + line = rows[y] if y < len(rows) else "" + assert len(line) <= 12, line + bits = 0 + for x in range(12): + bits = (bits << 2) | ".wbs".index(line[x] if x < len(line) else ".") + data += [(bits >> 16) & 255, (bits >> 8) & 255, bits & 255] + return data + [0] + + +BIRD_UP = [""] * 6 + [ + " ## ##", + " ### ###", + " ### ###", + " ######", + " ####", +] +BIRD_DOWN = [""] * 8 + [ + " ######", + " #### ####", + " ### ###", + " ## ##", +] +BALLOON = [ + "...ssbbss...", + "..sbbbbbbs..", + ".sbwbsbbsbs.", + ".bwbbsbbsbb.", + "sbwbbsbbsbbs", + "sbbbbsbbsbbs", + "sbbbbsbbsbbs", + ".bbbbsbbsbb.", + ".sbbbsbbsbs.", + "..sbbsbbsb..", + "...sbbbbs...", + "....s..s....", + "....s..s....", + "....s..s....", + "....ssss....", + "....ssss....", +] +JET_L = [""] * 7 + [ + ".........b..", + "........bb..", + "..wwbbbbbbb.", + "bbbbbbbbbbbs", + ".bbsssbbbbb.", + "....bb......", +] +JET_L2 = JET_L[:10] + ["bbbbbbbbbbss"] + JET_L[11:] + + +def mirror12(rows): + return [r.ljust(12, ".")[::-1] for r in rows] + + +def explosion(radius, seed): + import random + rng = random.Random(seed) + rows = [] + for y in range(H): + line = "" + for x in range(12): + dx, dy = (x - 5.5) * 2, (y - 10) + d = (dx * dx + dy * dy) ** 0.5 + rng.uniform(-1.5, 1.5) + if d < radius * 0.45: + line += "b" # yellow core + elif d < radius * 0.8: + line += "s" # red fire + elif d < radius and rng.random() < 0.5: + line += "w" if rng.random() < 0.3 else "s" + else: + line += "." + rows.append(line) + return rows + + +def flames(height, seed): + """Flames over a tree canopy (rows up to 16): pointed tongues, red edge + ('s') around a yellow core ('b') that sits low in the fire.""" + import random + rng = random.Random(seed) + tops = [] + for x in range(12): + centre = 1.0 - abs(x - 5.5) / 6.0 # taller in the middle + h = height * (0.35 + centre) + rng.uniform(-1.0, 1.0) + if rng.random() < 0.35: + h += rng.uniform(2, 4) # a tongue licking up + tops.append(max(0, min(15, int(round(h))))) + rows = ["." * 12 for _ in range(H)] + for x in range(1, 11): + for k in range(tops[x]): + y = 16 - k + edge = (k >= tops[x] - 2 or x in (1, 10) or tops[x - 1] <= k or tops[x + 1] <= k + or k > tops[x] * 0.6) + ch = "s" if edge else "b" + rows[y] = rows[y][:x] + ch + rows[y][x + 1:] + return rows + + +def spray(phase): + """Water drops falling from the skids: cyan 'w' and white own colour 'b'.""" + rows = [] + for y in range(H): + line = "" + for x in range(12): + on = 3 <= x <= 8 and (x * 7 + y * 3 + phase * 5) % 4 == 0 + wide = y > 10 and x in (2, 9) and (y + phase) % 4 == 0 + line += ("b" if (x + y) % 3 == 0 else "w") if (on or wide) else "." + rows.append(line) + return rows + + +def steam(seed): + import random + rng = random.Random(seed) + rows = [] + for y in range(H): + line = "" + for x in range(12): + d = ((x - 5.5) ** 2 / 20 + (y - 12) ** 2 / 40) + line += ("b" if rng.random() < 0.7 else "w") if d < 1 and rng.random() < 0.75 else "." + rows.append(line) + return rows + + +hazard_sprites = [ + ("SP_BIRD", hires_sprite(BIRD_UP)), + ("SP_BIRD2", hires_sprite(BIRD_DOWN)), + ("SP_BALLOON", mc_sprite(BALLOON)), + ("SP_JET_L", mc_sprite(JET_L)), + ("SP_JET_L2", mc_sprite(JET_L2)), + ("SP_JET_R", mc_sprite(mirror12(JET_L))), + ("SP_JET_R2", mc_sprite(mirror12(JET_L2))), + ("SP_BOOM", mc_sprite(explosion(6, 1))), + ("SP_BOOM2", mc_sprite(explosion(9, 2))), + ("SP_BOOM3", mc_sprite(explosion(11, 3))), + ("SP_BLANK", [0] * 64), + ("SP_FIRE", mc_sprite(flames(8, 1))), + ("SP_FIRE2", mc_sprite(flames(8, 2))), + ("SP_FIRE_S", mc_sprite(flames(4, 3))), + ("SP_FIRE_S2", mc_sprite(flames(4, 4))), + ("SP_SPRAY", mc_sprite(spray(0))), + ("SP_SPRAY2", mc_sprite(spray(1))), + ("SP_STEAM", mc_sprite(steam(5))), + ("SP_STEAM2", mc_sprite(steam(6))), +] +sprite_names = {28: "SP_SHADOW_BIG", 29: "SP_SHADOW_SMALL"} +for n, blk in hazard_sprites: + sprite_names[len(sprites)] = n + sprites.append(blk) + +# ---------------------------------------------------------------- tiles +TILES = [ + ("T_CL_TL", ["....", "....", "....", "...w", "..ww", ".www", "wwww", "wwww"]), + ("T_CL_TM1", ["....", ".ww.", "wwww", "wwww", "wwww", "wwww", "wwww", "wwww"]), + ("T_CL_TM2", ["....", "....", "..ww", ".www", "wwww", "wwww", "wwww", "wwww"]), + ("T_CL_TR", ["....", "....", "w...", "ww..", "www.", "www.", "wwww", "wwww"]), + ("T_CL_BL", ["wwww", "wwww", ".www", ".wgg", "..gg", "....", "....", "...."]), + ("T_CL_BM", ["wwww", "wwww", "wwww", "gwgw", "gggg", "....", "....", "...."]), + ("T_CL_BR", ["wwww", "wwww", "www.", "ggw.", "gg..", "....", "....", "...."]), + + ("T_M_UP", ["...g", "...g", "..g#", "..g#", ".g##", ".g##", "g###", "g###"]), + ("T_M_DN", ["#...", "#...", "##..", "##..", "###.", "###.", "####", "####"]), + ("T_M_PEAK", [".g#.", ".g#.", ".g##", "g###", "g###", "g###", "g###", "####"]), + ("T_M_UP_S", ["...w", "...w", "..ww", "..w#", ".ww#", ".w##", "g###", "g###"]), + ("T_M_DN_S", ["w...", "w...", "ww..", "#w..", "#ww.", "##w.", "###.", "####"]), + ("T_M_PK_S", [".ww.", ".ww.", ".www", "wwww", "ww#w", "w###", "g###", "g###"]), + ("T_M_FL_S", ["wwww", "w#ww", "####", "##w#", "####", "####", "####", "####"]), + ("T_M_SOLID", ["####"] * 8), + ("T_M_SNOW", ["#w##", "####", "##w#", "####", "w###", "####", "####", "####"]), + ("T_M_ROCK", ["####", "##g#", "#g##", "####", "####", "####", "####", "####"]), + + ("T_H_UP", ["...w", "..ww", "..w#", ".w##", ".w##", "w###", "w###", "####"]), + ("T_H_DN", ["w...", "ww..", "#w..", "##w.", "##w.", "###w", "###w", "####"]), + ("T_H_PEAK", ["....", ".ww.", "wwww", "w##w", "####", "#w##", "####", "####"]), + ("T_H_FLAT", ["wwww", "####", "#w##", "####", "####", "##w#", "####", "####"]), + ("T_H_SOLID", ["####", "#w##", "####", "####", "###w", "####", "####", "####"]), + ("T_TREE_TOP", [".ww.", "w#w#", "ww##", "w###", "####", "####", ".##.", ".gg."]), + ("T_TREE_TRUNK", [".gg.", ".gg.", ".gg.", ".gg.", ".gg.", ".gg.", ".gg.", "gggg"]), + ("T_PINE_TOP", ["....", ".w#.", ".w#.", "ww##", "w###", ".w#.", "ww##", "w###"]), + ("T_PINE_MID", [".w#.", "ww##", "w###", "####", ".w#.", "ww##", "w###", "####"]), + ("T_PINE_TRUNK", [".gg.", ".gg.", ".gg.", ".gg.", ".gg.", ".gg.", ".gg.", "gggg"]), + ("T_GRASS1", ["w.w.", "wwww", "#w#w", "####", "##w#", "####", "#w##", "####"]), + ("T_GRASS2", [".w.w", "wwww", "w#w#", "####", "#w##", "####", "###w", "####"]), + ("T_DIRT1", ["gggg", "g#gg", "gggg", "ggg#", "g#gg", "gggg", "####", "####"]), + ("T_DIRT2", ["gggg", "gggg", "gg#g", "gggg", "#ggg", "gg#g", "####", "####"]), + ("T_BURNT_TREE", ["g..g", ".g.g", "g.g.", ".gg.", "g.g.", ".gg.", ".gg.", ".gg."]), + ("T_BURNT_PINE", ["..g.", ".gg.", "g.g.", ".gg.", ".ggg", "g.g.", ".gg.", ".gg."]), +] +PIX = {".": 0, "w": 1, "g": 2, "#": 3} + + +def tile_bytes(rows): + assert len(rows) == 8 + out = [] + for r in rows: + assert len(r) == 4, r + v = 0 + for ch in r: + v = (v << 2) | PIX[ch] + out.append(v) + return out + + +FIRST_TILE = 64 # screen codes 64..127 are graphics in the ROM set; letters stay intact + +here = os.path.dirname(os.path.abspath(__file__)) +dst = os.path.join(here, "..", "src", "gfx.h") +with open(dst, "w", newline="\n") as f: + f.write("/* Generated by tools/gen_gfx.py - do not edit by hand. */\n") + f.write("#ifndef GFX_H\n#define GFX_H\n\n") + f.write(f"#define FIRST_TILE {FIRST_TILE}\n#define NUM_TILES {len(TILES)}\n") + for i, (n, _) in enumerate(TILES): + f.write(f"#define {n} {FIRST_TILE + i}\n") + f.write("\nstatic const unsigned char tile_data[NUM_TILES * 8] = {\n") + for n, rows in TILES: + f.write(" " + ",".join(f"0x{b:02X}" for b in tile_bytes(rows)) + f", /* {n} */\n") + f.write("};\n\n") + f.write(f"#define NUM_SPRITES {len(sprites)}\n") + f.write("/* blocks: [pose*2] outline, [pose*2+1] fill; poses 0-11 = facing*6+tilt*2+rotor,\n" + " 12-13 front view, then shadow big (28) and shadow small (29). */\n") + for i, n in sorted(sprite_names.items()): + f.write(f"#define {n} {i}\n") + f.write("static const unsigned char sprite_data[NUM_SPRITES * 64] = {\n") + for i, blk in enumerate(sprites): + label = names[i // 2] + (" outline" if i % 2 == 0 else " fill") if i < 28 else sprite_names[i] + f.write(f" /* {i}: {label} */\n " + ",".join(f"{b}" for b in blk) + ",\n") + f.write("};\n\n#endif\n") + +with open(os.path.join(here, "..", "src", "gfx.inc"), "w", newline="\n") as f: + f.write("; Generated by tools/gen_gfx.py - do not edit by hand. Sprite block numbers.\n") + f.write("SPR_BASE = 16 ; block of sprite 0: ($8400 - $8000) / 64\n") + for i, n in sorted(sprite_names.items()): + f.write(f"{n} = {i}\n") + +# quick visual dump for checking the art +if os.environ.get("SHOW"): + for i in range(0, 28, 2): + print(names[i // 2]) + for y in range(H): + row = "" + for x in range(W): + o = (sprites[i][y * 3 + x // 8] >> (7 - x % 8)) & 1 + fb = sprites[i + 1][y * 3 + (x // 2) // 4] + c = (fb >> (6 - 2 * ((x // 2) % 4))) & 3 + row += "#" if o else ".wbs"[c] if c else " " + print(" |" + row + "|") +if os.environ.get("SHOW"): + for i in range(30, len(sprites)): + mc = sprite_names[i] not in ("SP_BIRD", "SP_BIRD2") + print(sprite_names[i]) + for y in range(H): + b = sprites[i][y * 3:y * 3 + 3] + v = (b[0] << 16) | (b[1] << 8) | b[2] + if mc: + print(" |" + "".join(" wbs"[(v >> (22 - 2 * x)) & 3] * 2 for x in range(12)) + "|") + else: + print(" |" + "".join("#" if (v >> (23 - x)) & 1 else " " for x in range(24)) + "|") +print(f"wrote {dst}: {len(TILES)} tiles, {len(sprites)} sprites")