; SPDX-License-Identifier: MIT ; Bank 0: the frame loop, the game logic and the sound. .segment "BANK0" .org $D000 .include "gen/logic.inc" ; --------------------------------------------------------------------------- Init: sei cld ldx #0 txa @clr: sta $00,x ; TIA registers and RAM dex bne @clr dex txs sta SWACNT ; ports as inputs (A = 0) sta SWBCNT lda #$A7 sta rngLo lda #$1D sta rngHi lda SWCHB sta swOld lda #1 sta kernelMode lda #ST_TITLE sta gameState lda #24 sta titleX lda #200 sta ballY MainLoop: ; VSYNC: three lines lda #2 sta VBLANK sta VSYNC sta WSYNC sta WSYNC sta WSYNC lda #0 sta VSYNC .if VBDELAY > 0 SLEEP VBDELAY .endif lda #VBTIM sta TIM64T lda #$82 sta VBLANK jsr VBlankLogic WaitVB: bit TIMINT ; D7: the timer has passed zero bpl WaitVB sta WSYNC lda kernelMode bne @title lda floor bne @top jsr ToLow jmp @os @top: jsr ToTop jmp @os @title: jsr ToTitle @os: .if OSDELAY > 0 SLEEP OSDELAY .endif lda #OSTIM sta TIM64T jsr OverscanLogic WaitOS: bit TIMINT bpl WaitOS sta WSYNC jmp MainLoop ; --------------------------------------------------------------------------- ; Random: 16-bit Galois LFSR (taps $B400). Random: lsr rngHi ror rngLo bcc @n lda rngHi eor #$B4 sta rngHi @n: lda rngLo rts ; --------------------------------------------------------------------------- VBlankLogic: inc frame jsr Random ; console switches: t0 = switches pressed this frame (1 = newly pressed) lda SWCHB tax eor #$FF and swOld sta t0 stx swOld ; paddle button (paddle 0 = SWCHA D7, 0 = pressed): t1 = $80 if newly pressed lda SWCHA eor #$FF and #$80 tax ; X = $80 while held lda btnOld eor #$80 sta t1 txa and t1 sta t1 stx btnOld ; Game Reset: start a game from anywhere lda t0 lsr bcc @noreset jsr StartGame jmp @states @noreset: ; Game Select: back to the title, then step the game number lda swOld and #2 bne @nosel lda gameState beq @selTitle lda #ST_TITLE ; leave the game for the title sta gameState lda #1 sta kernelMode lda #30 sta stateTimer jmp @states @selTitle: lda t0 and #2 bne @step ; pressed this frame dec stateTimer ; held: repeat every 30 frames bne @states @step: lda #30 sta stateTimer inc gameNum lda gameNum and #3 sta gameNum jmp @states @nosel: @states: lda gameState cmp #ST_TITLE bne @notTitle ; title: the button starts a game (after a short pause after a game) lda stateTimer beq @tready lda swOld and #2 beq @tdone ; Select held: its repeat timer dec stateTimer jmp @tdone @tready: lda t1 beq @tdone jsr StartGame @tdone: jmp @prep @notTitle: cmp #ST_SERVE bne @notServe lda t1 bne @launch dec stateTimer bne @prep @launch: jsr Launch jmp @prep @notServe: cmp #ST_PLAY bne @prep lda t1 beq @prep lda kick bne @prep lda #KICKLEN sta kick ldx #SFX_SWISH jsr Sfx0 @prep: jsr PaddleToBat jsr Sound jmp Prep KICKLEN = 24 ; kick timer on a press KICKWIN = 14 ; a bat hit while kick >= KICKWIN is a kick KICKSHOW = 16 ; the bat is drawn raised while kick >= KICKSHOW ; --------------------------------------------------------------------------- ; The paddle: padY is the line counter of the last line on which the capacitor ; was still charging (line 59 on). L = PADINIT - padY lines; turning the knob ; clockwise (less resistance) charges sooner and moves the bat right. PaddleToBat: lda gameState cmp #ST_SERVE beq @go cmp #ST_PLAY bne @done @go: ; bat width from this player's difficulty switch (A = narrow, a set bit). ; SWCHB D6 is the left switch (P0), D7 the right (P1): DOC, Stella ; Programmer's Guide, "Console Switches"; a2600-python docs/start-here.md. ; (Fixed 2026-11-21: player 1 had read D7, the right switch.) lda swOld ldx curPlayer bne @p1 asl ; player 1: D6 after two shifts @p1: asl ; player 2: D7 after one lda #24 bcc @w lda #16 @w: sta batW ; target x = (144 - batW) - clamp(L - PADOFF, 0, 128 - batW) lda #PADINIT sec sbc padY ; L sec sbc #PADOFF bcs @pos lda #0 @pos: sta t0 lda #128 sec sbc batW cmp t0 bcs @inr sta t0 ; clamp @inr: lda #144 sec sbc batW sec sbc t0 ; target ; smoothing: steps of one clock are taken only on the second frame ; in a row (a real paddle's reading wanders by a line or so) sta t2 sec sbc batX beq @same cmp #1 beq @small cmp #$FF beq @small lda t2 sta batX @same: lda #0 sta t3 rts @small: inc t3 lda t3 cmp #2 bcc @done lda t2 sta batX lda #0 sta t3 @done: rts PADOFF = 2 ; --------------------------------------------------------------------------- StartGame: lda gameNum and #1 sta twoPlayer ; player block: score 0, 5 balls, level 0 (2 in games 3-4) ldx #PBLK-1 lda #0 @z: sta pStart,x dex bpl @z lda #5 sta balls lda gameNum and #2 sta level jsr FillBricks jsr ResetBalloons lda #$FF sta drops ldx #PBLK-1 @c: lda pStart,x sta stash,x dex bpl @c lda #0 sta curPlayer sta kernelMode sta msgT lda #64 sta clownX lda #1 sta clownDir jsr ServeBall ldx #SFX_START jsr Sfx1 lda #MSG_P1 jmp ShowMsg ; bricks: rows filled = min(4 + level, 6), from the bottom row (5) up FillBricks: lda level clc adc #4 cmp #6 bcc @ok lda #6 @ok: sta t0 ldx #5 @f: lda #$FF ldy t0 bne @set lda #0 @set: sta bricks,x lda t0 beq @nx dec t0 @nx: dex bpl @f rts ; ResetBalloons: three full rows at their starting places (a new game, Big Top). ResetBalloons: lda #$3F sta balloons sta balloons+1 sta balloons+2 lda #16 sta rowX lda #40 sta rowX+1 lda #56 sta rowX+2 lda #0 sta balT rts ServeBall: ; A row still sliding in (rowX >= ENTRY_MIN) may hold only the balloons ; already past the right wall; after a turn change the balloons belong to ; the other player, so such a row is put at the right end of its normal ; range, where any mask is drawable. ldx #2 @cl: lda rowX,x cmp #ENTRY_MIN bcc @ok lda #ENTRY_MIN-1 sta rowX,x @ok: dex bpl @cl lda #0 sta multK ; a new ball: multiplier 1x sta dropT ; (MoveDrops restarts a wait if the targets are down) lda #ST_SERVE sta gameState lda #240 sta stateTimer ; launches by itself after 4 seconds lda #0 sta floor sta kick sta cool jsr BaseSpeed sta speed rts ; base speed = min(level, 4) BaseSpeed: lda level cmp #4 bcc @r lda #4 @r: rts Launch: lda #ST_PLAY sta gameState jsr Random and #$80 ; left or right sta t1 jsr Random and #1 clc adc #1 ; angle class 1 or 2 jsr SetVelocity ; up ldx #SFX_BAT jmp Sfx0 ; SetVelocity: A = angle class (0 steep .. 3 shallow), speed in `speed`, ; t1 D7 = left, D6 = down. SetVelocity: sta t0 lda speed asl asl clc adc t0 tax lda VXLO,x sta vxL lda VXHI,x sta vxH lda VYLO,x sta vyL lda VYHI,x sta vyH bit t1 bpl @right jsr NegVX @right: bvs @down jsr NegVY @down: rts NegVX: sec lda #0 sbc vxL sta vxL lda #0 sbc vxH sta vxH rts NegVY: sec lda #0 sbc vyL sta vyL lda #0 sbc vyH sta vyH rts ; --------------------------------------------------------------------------- ; Messages (six glyph offsets each), shown in the HUD instead of the score. Messages: MSG_P1 = * - Messages .byte CH_P, CH_L, CH_Y, CH_R, CH_SP, CH_1 MSG_P2 = * - Messages .byte CH_P, CH_L, CH_Y, CH_R, CH_SP, CH_2 MSG_CLEAR = * - Messages .byte CH_C, CH_L, CH_E, CH_A, CH_R, CH_BANG MSG_BIGTOP = * - Messages .byte CH_B, CH_I, CH_G, CH_T, CH_O, CH_P MSG_OVER = * - Messages .byte CH_SP, CH_O, CH_V, CH_E, CH_R, CH_SP MSG_XBALL = * - Messages .byte CH_X, CH_B, CH_A, CH_L, CH_L, CH_BANG ShowMsg: sta msgIdx lda #100 sta msgT rts ; --------------------------------------------------------------------------- OverscanLogic: lda kick beq @k dec kick @k: lda flash beq @f dec flash @f: lda msgT beq @m dec msgT @m: lda cool beq @c dec cool @c: lda gameState cmp #ST_TITLE bne @nt ; title: balloons slide. clownDir is theirs here: MoveClown must not ; run on the title, or the two would flip it against each other and ; push titleX out of range (FOUND by scripts/check.sh: after a game ; over, titleX ran away and title frames grew to 263-264 lines). lda frame and #1 bne @tx lda clownDir ; reuse the clown's direction bit for the title row bmi @tl inc titleX lda titleX cmp #56 bcc @tx lda #$FF sta clownDir rts @tl: dec titleX lda titleX cmp #17 bcs @tx lda #1 sta clownDir @tx: rts @nt: jsr MoveClown jsr MoveBalloons jsr MoveDrops lda gameState cmp #ST_SERVE bne @ns ; the ball rides on the bat lda batW lsr clc adc batX sec sbc #1 sta ballX lda #BATTOP-BH sta ballY rts @ns: cmp #ST_PLAY bne @np jmp Physics @np: cmp #ST_LOST bne @nl dec stateTimer bne @r jmp NextTurn @nl: cmp #ST_OVER bne @r dec stateTimer bne @r lda #ST_TITLE sta gameState lda #1 sta kernelMode lda #60 sta stateTimer @r: rts MoveClown: lda clownHit beq @walk dec clownHit rts @walk: ; level 0 steps every other frame; later levels every frame; a cleared ; middle floor adds a step every fourth frame lda level bne @step lda frame and #1 bne @extra @step: jsr @one @extra: lda cleared and #2 beq @r lda frame and #3 bne @r @one: lda clownX clc adc clownDir sta clownX cmp #112 bcc @lo lda #$FF sta clownDir rts @lo: cmp #33 bcs @r lda #1 sta clownDir @r: rts ; Balloon rows slide between x = 16 and 57 (the row spans 88 clocks). ; Row r moves every BalPeriod frames; rows 0 and 2 start rightwards. ; ; Refill (2026-11-21, Aubrey's playtest: the top floor went empty and the game ; stalled there): once every row is popped (the top floor's net opens and the ; ball falls), the top row waits BalDelay frames, then a full row drifts in from ; behind the right wall. (Refilling each row as soon as it emptied was tried ; first: the floor never emptied, the bot's ball spent 56-71% of its time there ; and no Big Top came in six 20-minute runs.) While it enters, rowX runs ; down from ENTRY_X to ENTRY_MIN-1, one clock every other frame, and balloon k is ; added as its left edge reaches x = 147 - 16k (RevealX), so every balloon in the ; mask sits at x <= 147 and its copy's positioning value stays inside BalC/BalF ; (x <= 149, gen_tables.py). Behind the wall (x >= 144) the playfield hides it. ; The top row is refilled first; while one row is entering, the next empty row ; waits only BAL_STAGGER frames, so a whole set comes back row after row. One ; timer (balT) serves all rows. Values provisional, tuned with the bot. ENTRY_X = 148 ENTRY_MIN = 58 ; rowX >= this: the row is still entering BAL_STAGGER = 40 MoveBalloons: ; refill only once the whole set is gone (the net opens and the ball ; drops first), or while a set is already coming back in ldy #2 @any: lda rowX,y cmp #ENTRY_MIN bcs @look ; a row is entering: carry on with the set dey bpl @any lda balloons ora balloons+1 ora balloons+2 bne @idle ; balloons up and none entering: nothing to do @look: ; find the first empty row at rest (top row first) ldx #0 @find: lda balloons,x bne @nf lda rowX,x cmp #ENTRY_MIN bcc @empty @nf: inx cpx #3 bcc @find @idle: lda #0 sta balT ; nothing to refill beq @moverows @empty: lda balT bne @tick ; start the wait: BAL_STAGGER while another row enters, else BalDelay ldy #2 @ent: lda rowX,y cmp #ENTRY_MIN bcs @stag dey bpl @ent lda level cmp #3 bcc @dl lda #3 @dl: tay lda BalDelay,y bne @setT @stag: lda #BAL_STAGGER/4 @setT: sta balT bne @moverows @tick: lda frame ; balT counts in fours of frames and #3 bne @moverows dec balT bne @moverows lda #ENTRY_X ; X = the empty row: it starts in from the right sta rowX,x lda rowDirs ora BitTab,x sta rowDirs @moverows: ldx #2 @row: lda rowX,x cmp #ENTRY_MIN bcc @normal ; entering: one clock left every other frame, balloons appearing lda frame and #1 bne @next dec rowX,x lda rowX,x ldy #5 @rv: cmp RevealX,y beq @reveal dey bpl @rv bmi @next @reveal: lda BalBit,y ora balloons,x sta balloons,x jmp @next @normal: lda level cmp #3 bcc @lv lda #3 @lv: asl asl sta t0 ; level * 4 (capped) txa clc adc t0 tay lda BalPeriod,y ; frames between steps (a power of two minus one) and frame bne @next lda rowDirs ; bit r set: row r moving left and BitTab,x bne @left inc rowX,x lda rowX,x cmp #56 bcc @next lda rowDirs ora BitTab,x sta rowDirs jmp @next @left: dec rowX,x lda rowX,x cmp #17 bcs @next lda BitTab,x eor #$FF and rowDirs sta rowDirs @next: dex bpl @row rts .ifndef BALDELAY ; (-D BALDELAY=n overrides level 0's, for tuning) BALDELAY = 150 .endif ; Drop targets come back (2026-11-22, Aubrey's second playtest; provisional): once ; all eight are down, they wait as long as an emptied balloon set (BalDelay), then ; return one column every 4 frames from the left. dropT counts the wait plus the ; 8-step sweep in fours of frames; a clear counts only while dropT is 0 (HitMiddle). MoveDrops: lda dropT bne @run lda drops bne @r ; targets up: nothing to do lda level cmp #3 bcc @lv lda #3 @lv: tay lda BalDelay,y clc adc #8 sta dropT @r: rts @run: lda frame and #3 bne @r dec dropT lda dropT cmp #8 bcs @r eor #7 ; 7 - dropT: column 0 first tax lda BitTab,x ora drops sta drops rts BalDelay: ; fours of frames an emptied set waits before refilling, by level (capped at 3) .byte BALDELAY/4, BALDELAY/5, BALDELAY/6, BALDELAY*2/15 RevealX: ; rowX at which balloon k (0-5) appears while a row enters .byte 147, 131, 115, 99, 83, 67 BalPeriod: ; [level*4 + row] (level capped at 3): and-masks for frame .byte 3, 7, 3, 0, 1, 3, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0 BitTab: .byte 1, 2, 4, 8, 16, 32, 64, 128 .include "physics.s"