space_bars: some initial cycle-counting

it would help if I could subtract
This commit is contained in:
Vince Weaver 2018-10-06 20:35:28 -04:00
parent 9a88bc3121
commit 757f9d9c3d
7 changed files with 710 additions and 59 deletions

View File

@ -2,6 +2,7 @@ include ../Makefile.inc
DOS33 = ../dos33fs-utils/dos33
PNG_TO_40x48D = ../gr-utils/png_to_40x48d
B2D = ../bmp2dhr/b2d
all: space_bars.dsk
@ -16,7 +17,8 @@ SPACE_BARS: space_bars.o
space_bars.o: space_bars.s instructions.s game.s \
game_over.s gr_copy.s text_print.s title.s \
spacebars_title.inc
spacebars_title.inc \
vapor_lock.s delay_a.s lz4_decode.s SB_BACKGROUNDC.BIN.lz4
ca65 -o space_bars.o space_bars.s -l space_bars.lst
####
@ -24,6 +26,14 @@ space_bars.o: space_bars.s instructions.s game.s \
spacebars_title.inc: ./images/spacebars_title_scaled.png
$(PNG_TO_40x48D) asm ./images/spacebars_title_scaled.png spacebars_title > spacebars_title.inc
####
SB_BACKGROUNDC.BIN.lz4: SB_BACKGROUNDC.BIN
lz4 -f -16 SB_BACKGROUNDC.BIN
SB_BACKGROUNDC.BIN: sb_background.bmp
$(B2D) sb_background.bmp HGR -d
###
clean:

25
space_bars/delay_a.s Normal file
View File

@ -0,0 +1,25 @@
; From http://6502org.wikidot.com/software-delay
; 25+A cycles (including JSR), 19 bytes (excluding JSR)
;
; The branches must not cross page boundaries!
;
; Cycles Accumulator Carry flag
; 0 1 2 3 4 5 6 (hex) 0 1 2 3 4 5 6
; jsr delay_a ; 6 6 6 6 6 6 6 00 01 02 03 04 05 06
dly0: sbc #7
delay_a:cmp #7 ; 2 2 2 2 2 2 2 00 01 02 03 04 05 06 0 0 0 0 0 0 0
bcs dly0 ; 2 2 2 2 2 2 2 00 01 02 03 04 05 06 0 0 0 0 0 0 0
lsr ; 2 2 2 2 2 2 2 00 00 01 01 02 02 03 0 1 0 1 0 1 0
bcs dly1 ; 2 3 2 3 2 3 2 00 00 01 01 02 02 03 0 1 0 1 0 1 0
dly1: beq dly2 ; 3 3 2 2 2 2 2 00 00 01 01 02 02 03 0 1 0 1 0 1 0
lsr ; 2 2 2 2 2 00 00 01 01 01 1 1 0 0 1
beq dly3 ; 3 3 2 2 2 00 00 01 01 01 1 1 0 0 1
bcc dly3 ; 3 3 2 01 01 01 0 0 1
dly2: bne dly3 ; 2 2 3 00 00 01 0 1 0
dly3: rts ; 6 6 6 6 6 6 6 00 00 00 00 01 01 01 0 1 1 1 0 0 1
;
; Total cycles: 25 26 27 28 29 30 31

View File

@ -1,14 +1,10 @@
;================================
; Show some instructions
; return when a key is pressed
; spacebars gameplay
;================================
game:
;===================
; init screen
bit LORES
bit SET_GR
bit FULLGR
bit KEYRESET
;===================
@ -17,11 +13,233 @@ game:
lda #0
sta DRAW_PAGE
;=============================
; Load graphic hgr
lda #<background_hgr
sta LZ4_SRC
lda #>background_hgr
sta LZ4_SRC+1
lda #<(background_hgr_end-8) ; skip checksum at end
sta LZ4_END
lda #>(background_hgr_end-8) ; skip checksum at end
sta LZ4_END+1
lda #<$2000
sta LZ4_DST
lda #>$2000
sta LZ4_DST+1
jsr lz4_decode
;=============================
; Load graphic page0
lda #$0c
sta BASH
lda #$00
sta BASL ; load image to $c00
; lda #<fs
; sta GBASL
; lda #>fs
; sta GBASH
; jsr load_rle_gr
lda #4
sta DRAW_PAGE
jsr gr_copy_to_current ; copy to page1
; GR part
bit PAGE1
bit LORES ; 4
bit SET_GR ; 4
bit FULLGR ; 4
;=============================
; Load graphic page1
lda #$0c
sta BASH
lda #$00
sta BASL ; load image to $c00
; lda #<fs
; sta GBASL
; lda #>fs
; sta GBASH
; jsr load_rle_gr
;===================
; copy to page3
lda #0
jsr clear_gr
sta DRAW_PAGE
jsr gr_copy_to_current
; GR part
bit PAGE0
jsr wait_until_keypressed ; tail call?
;==============================
; setup graphics for vapor lock
;==============================
rts
jsr vapor_lock ; 6
; vapor lock returns with us at beginning of hsync in line
; 114 (7410 cycles), so with 5070 lines to go
; so we have 5070 + 4550 = 9620 to kill
jsr gr_copy_to_current ; 6+ 9292
; now we have 322 left
; GR part
bit LORES ; 4
bit SET_GR ; 4
bit FULLGR ; 4
; 322 - 12 = 310
; - 3 for jmp
; 307
; Try X=9 Y=6 cycles=307
ldy #6 ; 2
sbloopA:ldx #9 ; 2
sbloopB:dex ; 2
bne sbloopB ; 2nt/3
dey ; 2
bne sbloopA ; 2nt/3
jmp sb_begin_loop
.align $100
;================================================
; Spacebars Loop
;================================================
; each scan line 65 cycles
; 1 cycle each byte (40cycles) + 25 for horizontal
; Total of 12480 cycles to draw screen
; Vertical blank = 4550 cycles (70 scan lines)
; Total of 17030 cycles to get back to where was
sb_begin_loop:
sb_display_loop:
; 0-7 = text mode
; 1 2 3
;0123456789012345678901234567890123456789
;LEVEL: 6 LIVES: 2 SCORE: 01978 HI: 02018
; 8-47 = hgr
; 48 - 191 = split .. 144 = 36grlins
; 6 4 25+16+8+16 NNNNNNNN
; 7 6 25+15+10+15 LNNNNNN
; 8 8 25+14+12+14 NNNNNNN
; 9 10 25+13+14+13 LNNNNN
; 10 12 25+12+16+12
; 11 14 25+11+18+11
; 12 16 25+10+20+10
; 13 18 25+09+22+09
; 14 20 25+08+24+08
; 15 22 25+07+26+07
; 16 24 25+06+28+06
; 17 26 25+05+30+05
; 18 28 25+04+32+04
; 19 30 25+03+34+03
; 20 32 25+02+36+02
; 21 34 25+01+38+01
; 22 36 25+00
; 23 38 25+12
; 24 40 25+12
; 8 lines of text mode
ldy #8 ; 2
sb_text_loop:
bit SET_TEXT ; 4
lda #29 ; 2
jsr delay_a ; 25+29
dey ; 2
bne sb_text_loop ; 3
;================
; 65
; -1
sb_hgr_loop:
; delay 184*65 = 11960
; -2
; +1
; -8
;=========================
; 11951
bit SET_GR ; 4
bit HIRES ; 4
; Try X=22 Y=103 cycles=11949 R2
nop
ldy #103 ; 2
sbloopC:ldx #22 ; 2
sbloopD:dex ; 2
bne sbloopD ; 2nt/3
dey ; 2
bne sbloopC ; 2nt/3
;======================================================
; We have 4550 cycles in the vblank, use them wisely
;======================================================
; do_nothing should be 4550
; -10 keypress
; ===========
; 4540
; Try X=9 Y=89 cycles=4540
ldy #89 ; 2
sbloop1:ldx #9 ; 2
sbloop2:dex ; 2
bne sbloop2 ; 2nt/3
dey ; 2
bne sbloop1 ; 2nt/3
lda KEYPRESS ; 4
bpl sb_no_keypress ; 3
jmp sb_start_over
sb_no_keypress:
jmp sb_display_loop ; 3
sb_start_over:
bit KEYRESET ; clear keypress ; 4
rts ; 6
;.include "deater.inc"
background_hgr:
.incbin "SB_BACKGROUNDC.BIN.lz4",11
background_hgr_end:

213
space_bars/lz4_decode.s Normal file
View File

@ -0,0 +1,213 @@
; LZ4 data decompressor for Apple II
; Code by Peter Ferrie (qkumba) (peter.ferrie@gmail.com)
; "LZ4 unpacker in 143 bytes (6502 version) (2013)"
; http://pferrie.host22.com/misc/appleii.htm
; This is that code, but with comments and labels added for clarity.
; I also found a bug when decoding with runs of multiples of 256
; which has since been fixed upstream.
; For LZ4 reference see
; https://github.com/lz4/lz4/wiki/lz4_Frame_format.md
; LZ4 summary:
;
; HEADER:
; Should: check for magic number 04 22 4d 18
; FLG: 64 in our case (01=version, block.index=1, block.checksum=0
; size=0, checksum=1, reserved
; MAX Blocksize: 40 (64kB)
; HEADER CHECKSUM: a7
; BLOCK HEADER: 4 bytes (le) length If highest bit set, uncompressed!
; data (see below), followed by checksum?
; BLOCKS:
; Token byte. High 4-bits literal length, low 4-bits copy length
; + If literal length==15, then following byte gets added to length
; If that byte was 255, then keep adding bytes until not 255
; + The literal bytes follow. There may be zero of them
; + Next is block copy info. little-endian 2-byte offset to
; be subtracted from current read position indicating source
; + The low 4-bits of the token are the copy length, which needs
; 4 added to it. As with the literal length, if it is 15 then
; you read a byte and add (and if that byte is 255, keep adding)
; At end you have 4 byte end-of-block marker (all zeros?) then
; 4 bytes of checksum (if marked in flags)
; our code does that, so be sure to set end -8
;LZ4_SRC EQU $00
;LZ4_DST EQU $02
;LZ4_END EQU $04
;COUNT EQU $06
;DELTA EQU $08
;UNPACK_BUFFER EQU $5E00 ; offset of first unpacked byte
;======================
; LZ4 decode
;======================
; input buffer in LZ4_SRC
; output buffer hardcoded still
; size in ENDH:ENDL
lz4_decode:
; lda LZ4_SRC ; packed data offset
; clc
; adc LZ4_END
; sta LZ4_END
; lda LZ4_SRC+1
; adc LZ4_END+1
; sta LZ4_END+1
; lda #>UNPACK_BUFFER ; original unpacked data offset
; sta LZ4_DST+1
; lda #<UNPACK_BUFFER
; sta LZ4_DST
unpmain:
ldy #0 ; used to index, always zero
parsetoken:
jsr getsrc ; get next token
pha ; save for later (need bottom 4 bits)
lsr ; number of literals in top 4 bits
lsr ; so shift into place
lsr
lsr
beq copymatches ; if zero, then no literals
; jump ahead and copy
jsr buildcount ; add up all the literal sizes
; result is in ram[count+1]-1:A
tax ; now in ram[count+1]-1:X
jsr docopy ; copy the literals
lda LZ4_SRC ; 16-bit compare
cmp LZ4_END ; to see if we have reached the end
lda LZ4_SRC+1
sbc LZ4_END+1
bcs done
copymatches:
jsr getsrc ; get 16-bit delta value
sta DELTA
jsr getsrc
sta DELTA+1
pla ; restore token
and #$0f ; get bottom 4 bits
; match count. 0 means 4
; 15 means 19+, must be calculated
jsr buildcount ; add up count bits, in ram[count+1]-:A
clc
adc #4 ; adjust count by 4 (minmatch)
tax ; now in ramp[count+1]-1:X
beq copy_no_adjust ; BUGFIX, don't increment if
; exactly a multiple of 0x100
bcc copy_no_adjust
inc COUNT+1 ; increment if we overflowed
copy_no_adjust:
lda LZ4_SRC+1 ; save src on stack
pha
lda LZ4_SRC
pha
sec ; subtract delta
lda LZ4_DST ; from destination, make new src
sbc DELTA
sta LZ4_SRC
lda LZ4_DST+1
sbc DELTA+1
sta LZ4_SRC+1
jsr docopy ; do the copy
pla ; restore the src
sta LZ4_SRC
pla
sta LZ4_SRC+1
jmp parsetoken ; back to parsing tokens
done:
pla
rts
;=========
; getsrc
;=========
; gets byte from src into A, increments pointer
getsrc:
lda (LZ4_SRC), Y ; get a byte from src
inc LZ4_SRC ; increment pointer
bne done_getsrc ; update 16-bit pointer
inc LZ4_SRC+1 ; on 8-bit overflow
done_getsrc:
rts
;============
; buildcount
;============
buildcount:
ldx #1 ; high count starts at 1
stx COUNT+1 ; (loops at zero?)
cmp #$0f ; if LITERAL_COUNT < 15, we are done
bne done_buildcount
buildcount_loop:
sta COUNT ; save LITERAL_COUNT (15)
jsr getsrc ; get the next byte
tax ; put in X
clc
adc COUNT ; add new byte to old value
bcc bc_8bit_oflow ; if overflow, increment high byte
inc COUNT+1
bc_8bit_oflow:
inx ; check if read value was 255
beq buildcount_loop ; if it was, keep looping and adding
done_buildcount:
rts
;============
; getput
;============
; gets a byte, then puts the byte
getput:
jsr getsrc
; fallthrough to putdst
;=============
; putdst
;=============
; store A into destination
putdst:
sta (LZ4_DST), Y ; store A into destination
inc LZ4_DST ; increment 16-bit pointer
bne putdst_end ; if overflow, increment top byte
inc LZ4_DST+1
putdst_end:
rts
;=============================
; docopy
;=============================
; copies ram[count+1]-1:X bytes
; from src to dst
docopy:
docopy_loop:
jsr getput ; get/put byte
dex ; decrement count
bne docopy_loop ; if not zero, loop
dec COUNT+1 ; if zero, decrement high byte
bne docopy_loop ; if not zero, loop
rts

View File

@ -8,6 +8,16 @@
; Zero Page
FRAMEBUFFER = $00 ; $00 - $0F
;; LZ4 addresses
LZ4_SRC = $00
LZ4_DST = $02
LZ4_END = $04
COUNT = $06
DELTA = $08
YPOS = $10
YPOS_SIN = $11
CH = $24
@ -29,14 +39,18 @@ OUTH = $FF
KEYPRESS= $C000
KEYRESET= $C010
SET_GR = $C050 ; Enable graphics
SET_TEXT= $C051 ; Enable text
FULLGR = $C052 ; Full screen, no text
PAGE0 = $C054 ; Page0
PAGE1 = $C055 ; Page1
LORES = $C056 ; Enable LORES graphics
HIRES = $C057 ; Enable HIRES graphics
PADDLE_BUTTON0 = $C061
PADDL0 = $C064
PTRIG = $C070
; ROM routines
TEXT = $FB36 ;; Set text mode
@ -129,6 +143,9 @@ gr_offsets:
.include "game.s"
.include "text_print.s"
.include "game_over.s"
.include "vapor_lock.s"
.include "delay_a.s"
.include "lz4_decode.s"
.include "spacebars_title.inc"

View File

@ -65,55 +65,7 @@ title_screen:
;==============================
; setup graphics for vapor lock
;==============================
; Clear Page0
lda #$0
sta DRAW_PAGE
lda #$44
jsr clear_gr
; Make screen half green
lda #$11
ldy #24
jsr clear_page_loop
;=====================================================
; attempt vapor lock
; by reading the "floating bus" we can see most recently
; written value of the display
; we look for $55 (which is the grey line)
;=====================================================
; See:
; Have an Apple Split by Bob Bishop
; Softalk, October 1982
; Challenges: each scan line scans 40 bytes.
; The blanking happens at the *beginning*
; So 65 bytes are scanned, starting at adress of the line - 25
; the scan takes 8 cycles, look for 4 repeats of the value
; to avoid false positive found if the horiz blanking is mirroring
; the line (max 3 repeats in that case)
vapor_lock_loop: ; first make sure we have all zeroes
LDA #$11
zxloop:
LDX #$04
wiloop:
CMP $C051
BNE zxloop
DEX
BNE wiloop
LDA #$44 ; now look for our border color (4 times)
zloop:
LDX #$04
qloop:
CMP $C051
BNE zloop
DEX
BNE qloop
jsr vapor_lock
; found first line of black after green, at up to line 26 on screen
; so we want roughly 22 lines * 4 = 88*65 = 5720 + 4550 = 10270

216
space_bars/vapor_lock.s Normal file
View File

@ -0,0 +1,216 @@
; This took a while to track down
; On Apple II/II+ the horiz blanking addr are $1000 higher than on IIe
; So on II+ were outside video area, so unlikely to be our set value
; (unless I foolishly use $ff which some uninitialized mem is set to)
; Lots of this color fiddling is to make sure you don't accidentally
; get runs of colors on IIe due to the horiz blank
; 0-5 aqua 6-12 = grey, 13 - 20 = yellow, 21-23 = aqua rainbow 14
;
;
;16 0 YA
;17 1 YA
;18 2 YA
;19 3 YA
;20 4 YA
;21 5 AA
;22 6 AG
;23 7 AG
;0 8 AG
;1 9 AG
;2 10 AG
;3 11 AG
;4 12 AG
;5 13 AY ****
;6 14 GY RAINBOW
;7 15 GY
;8 16 GY
;9 17 GY
;10 18 GY
;11 19 GY
;12 20 GY
;13 21 YA
;14 22 YA
;15 23 YA
;==============================
; setup graphics for vapor lock
;==============================
vapor_lock:
; Clear Page0
lda #$0
sta DRAW_PAGE
lda #$ee ; full screen white $ff
jsr clear_gr
lda #$dd
ldy #40
jsr clear_page_loop ; make top half grey2 $aa
lda #$aa
ldy #24
jsr clear_page_loop ; make top half grey2 $aa
lda #$ee
ldy #10
jsr clear_page_loop ; make top half grey2 $aa
; set up a rainbow to aid in exact lock
ldy #00
rainbow_loop:
tya
sta $728+20,Y
iny
cpy #20
bne rainbow_loop
;btt:
; jmp btt
;=====================================================
; attempt vapor lock
; by reading the "floating bus" we can see most recently
; written value of the display
;=====================================================
; See:
; Have an Apple Split by Bob Bishop
; Softalk, October 1982
; Challenges: each scan line scans 40 bytes.
; The blanking happens at the *beginning*
; So 65 bytes are scanned, starting at adress of the line - 25
; the scan takes 8 cycles, look for 4 repeats of the value
; to avoid false positive found if the horiz blanking is mirroring
; the line (max 3 repeats in that case)
vapor_lock_loop:
; first make sure we have a full line of $aa
lda #$aa ; 2
zxloop:
ldx #$04 ; 2
wiloop:
cmp $C051 ; read the floating bus ; 4
bne zxloop ; if not, start from scratch ; 2/3
dex ; we were, dec ; 2
bne wiloop ; if not 4 of them, restart ; 3/2
; if we get here we read 4 proper pixels, 11 apart (2+4+2+2+3)
; 0 11 22 33, clock at 34
; 1 12 23 34, clock at 35
; 2 13 24 35, clock at 36
; 3 14 25 36, clock at 37
; 4 15 26 37, clock at 38
; 5 16 27 38, clock at 39
; 6 17 28 39, clock at 40
; X X X X
; X X X X
; X X X X
; X X X X
; X X X X
; X X X X
; X X X X
; 0123456789012345678901234 0123456789012345678901234567890123456789
; 1 2 1 2 3
; hsync pixels
; XXXXXXXXXXXXXXXXXXXXXXXXX 4444444444444444444444444444440123456789
; now look for the color change that
; happens at line 13*8 = 104
lda #$dd ; 2
zloop:
ldx #$04 ; 2
qloop:
cmp $C051 ; read floating bus ; 4
bne zloop ; 2/3
dex ; 2
bne qloop ; 3/2
;============
; 11
; Found it!
; if we get here we read 4 proper pixels, 11 apart (2+4+2+2+3)
; 0 11 22 33, clock at 34
; 1 12 23 34, clock at 35
; 2 13 24 35, clock at 36
; 3 14 25 36, clock at 37
; 4 15 26 37, clock at 38
; 5 16 27 38, clock at 39
; 6 17 28 39, clock at 40
; In theory near end of line 104
; now skip ahead 8 lines and read from the rainbow pattern we set
; up to find our exact location
; delay 65 * 8 = 520
; we back off a few to make sure we're not in the horiz blank
; try to delay 510
lda #230 ; 2
jsr delay_a ; delay 25+230 = 255
lda #226 ; 2
jsr delay_a ; delay 25+226 = 251
; now near end of line 112
lda $C051 ; 4
;kbb:
; jmp kbb
; we are in theory on line $728 = 14*8 = 112
; so 112*65 = 7280 cycles from start
; we are actualy 25+20+A pixels in
; 7325+A
; Our goal is line 114 at 7410 cycles
; 7410 - 7325 = 85
; so kill 85-A cycles
; -6 to do subtraction
; -6 for rts
; -25 for delay_a overhead
eor #$ff ; 2
sec ; 2
adc #48 ; 2
jsr delay_a ; should total 48 cycles
done_vapor_lock:
rts ; 6
; Some random related work
; Docs:
; Lancaster
; Bishop
; Sather
; Vaguely relevant but no help with the Apple II+ issue
;
; Eamon: Screen display and timing synchronization
; on the Apple IIe and Apple IIgs
;
; Adams: Visually presented verbal stimuli by assembly
; language on the Apple II computer.
; Cavanagh and Anstis: Visual psychophysics on the
; Apple II: Getting started