racebeam: add initial code

This commit is contained in:
Vince Weaver 2019-03-06 12:44:31 -05:00
parent 5af319f7ec
commit d61b02b2cc
10 changed files with 801 additions and 0 deletions

34
racebeam/Makefile Normal file
View File

@ -0,0 +1,34 @@
include ../Makefile.inc
DOS33 = ../dos33fs-utils/dos33
B2D = ../bmp2dhr/b2d
PNG_TO_40x48D = ../gr-utils/png_to_40x48d
all: racebeam.dsk
racebeam.dsk: HELLO RACEBEAM
$(DOS33) -y racebeam.dsk SAVE HELLO
$(DOS33) -y racebeam.dsk BSAVE -a 0x1000 RACEBEAM
####
RACEBEAM: racebeam.o
ld65 -o RACEBEAM racebeam.o -C ../linker_scripts/apple2_1000.inc
racebeam.o: racebeam.s
ca65 -o racebeam.o racebeam.s -l racebeam.lst
#background_final.inc: background_final.png
# $(PNG_TO_40x48D) asm background_final.png bg_final > background_final.inc
####
HELLO: hello.bas
../asoft_basic-utils/tokenize_asoft < hello.bas > HELLO
clean:
rm -f *~ *.o *.lst *.inc RACEBEAM HELLO

6
racebeam/README Normal file
View File

@ -0,0 +1,6 @@
Apple II Racing the Beam demo
by Vince "Deater" Weaver
vince@deater.net
http://www.deater.net/weave/vmwprod/

25
racebeam/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

58
racebeam/gr_copy.s Normal file
View File

@ -0,0 +1,58 @@
;=========================================================
; gr_copy_to_current, 40x48 version
;=========================================================
; copy 0xc00 to DRAW_PAGE
;
; 45 + 2 + 120*(8*9 + 5) -1 + 6 = 9292
.align $100
gr_copy_to_current:
lda DRAW_PAGE ; 3
clc ; 2
adc #$4 ; 2
sta gr_copy_line+5 ; 4
sta gr_copy_line+11 ; 4
adc #$1 ; 2
sta gr_copy_line+17 ; 4
sta gr_copy_line+23 ; 4
adc #$1 ; 2
sta gr_copy_line+29 ; 4
sta gr_copy_line+35 ; 4
adc #$1 ; 2
sta gr_copy_line+41 ; 4
sta gr_copy_line+47 ; 4
;===========
; 45
ldy #119 ; for early ones, copy 120 bytes ; 2
gr_copy_line:
lda $C00,Y ; load a byte (self modified) ; 4
sta $400,Y ; store a byte (self modified) ; 5
lda $C80,Y ; load a byte (self modified) ; 4
sta $480,Y ; store a byte (self modified) ; 5
lda $D00,Y ; load a byte (self modified) ; 4
sta $500,Y ; store a byte (self modified) ; 5
lda $D80,Y ; load a byte (self modified) ; 4
sta $580,Y ; store a byte (self modified) ; 5
lda $E00,Y ; load a byte (self modified) ; 4
sta $600,Y ; store a byte (self modified) ; 5
lda $E80,Y ; load a byte (self modified) ; 4
sta $680,Y ; store a byte (self modified) ; 5
lda $F00,Y ; load a byte (self modified) ; 4
sta $700,Y ; store a byte (self modified) ; 5
lda $F80,Y ; load a byte (self modified) ; 4
sta $780,Y ; store a byte (self modified) ; 5
dey ; decrement pointer ; 2
bpl gr_copy_line ; ; 2nt/3
rts ; 6

40
racebeam/gr_hline.s Normal file
View File

@ -0,0 +1,40 @@
;==================================
; HLINE
;==================================
; Color in A
; Y has which line
hline:
pha ; 3
ldx gr_offsets,y ; 4+
stx hline_loop+1 ; 4
lda gr_offsets+1,y ; 4+
clc ; 2
adc DRAW_PAGE ; 3
sta hline_loop+2 ; 4
pla ; 4
ldx #39 ; 2
hline_loop:
sta $5d0,X ; 38 ; 5
dex ; 2
bpl hline_loop ; 2nt/3
rts ; 6
;==========================
; Clear gr screen
;==========================
; Color in A
clear_gr:
ldy #46
clear_page_loop:
jsr hline
dey
dey
bpl clear_page_loop
rts
gr_offsets:
.word $400,$480,$500,$580,$600,$680,$700,$780
.word $428,$4a8,$528,$5a8,$628,$6a8,$728,$7a8
.word $450,$4d0,$550,$5d0,$650,$6d0,$750,$7d0

2
racebeam/hello.bas Normal file
View File

@ -0,0 +1,2 @@
5 HOME
200 PRINT CHR$ (4)"RACEBEAM"

BIN
racebeam/racebeam.dsk Normal file

Binary file not shown.

279
racebeam/racebeam.s Normal file
View File

@ -0,0 +1,279 @@
; Race the Beam
; try for 40x192 15-color graphics on stock Apple II / Apple II+
; this means, no double-hires
; by racing the beam we can possibly update at least 10 blocks
; per line to the 192-line resolution
; we can get 40x96 resolution simply by page flipping
; this all requires vapor lock cycle-counting
; by deater (Vince Weaver) <vince@deater.net>
; Zero Page
FRAMEBUFFER = $00 ; $00 - $0F
YPOS = $10
YPOS_SIN = $11
CH = $24
CV = $25
GBASL = $26
GBASH = $27
BASL = $28
BASH = $29
FRAME = $60
WAITING = $62
LETTERL = $63
LETTERH = $64
LETTERX = $65
LETTERY = $66
LETTERD = $67
LETTER = $68
BLARGH = $69
HGR_COLOR = $E4
STATE = $ED
DRAW_PAGE = $EE
LASTKEY = $F1
PADDLE_STATUS = $F2
TEMP = $FA
WHICH = $FB
; Soft Switches
KEYPRESS= $C000
KEYRESET= $C010
SPEAKER = $C030
SET_GR = $C050 ; Enable graphics
SET_TEXT= $C051 ; Enable text
FULLGR = $C052 ; Full screen, no text
TEXTGR = $C053 ; Split screen
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
;HGR = $F3E2
;HPLOT0 = $F457
;HGLIN = $F53A
;COLORTBL= $F6F6
TEXT = $FB36 ;; Set text mode
HOME = $FC58 ;; Clear the text screen
WAIT = $FCA8 ;; delay 1/2(26+27A+5A^2) us
;==================================
;==================================
setup_background:
;===================
; init screen
jsr TEXT
jsr HOME
bit LORES
bit SET_GR
bit FULLGR
bit PAGE0
bit KEYRESET
;===================
; init vars
lda #0
sta DRAW_PAGE
sta STATE
sta WAITING
;=============================
; Load graphic page0
; lda #$0c
; sta BASH
; lda #$00
; sta BASL ; load image to $c00
; lda #<bg_final_low
; sta GBASL
; lda #>bg_final_low
; 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
; jsr wait_until_keypressed
;=============================
; Load graphic page1
; lda #$0c
; sta BASH
; lda #$00
; sta BASL ; load image to $c00
; lda #<bg_final_high
; sta GBASL
; lda #>bg_final_high
; sta GBASH
; jsr load_rle_gr
; lda #0
; sta DRAW_PAGE
; jsr gr_copy_to_current
; GR part
; bit PAGE0
; jsr wait_until_keypressed
;==============================
; setup graphics for vapor lock
;==============================
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
loopA:
ldx #9 ; 2
loopB:
dex ; 2
bne loopB ; 2nt/3
dey ; 2
bne loopA ; 2nt/3
jmp display_loop ; 3
.align $100
;================================================
; Display 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
; We want to alternate between page1 and page2 every 65 cycles
; vblank = 4550 cycles to do scrolling
; 2 + 48*( (4+2+25*(2+3)) + (4+2+23*(2+3)+4+5)) + 9)
; 48*[(6+125)-1] + [(6+115+10)-1]
display_loop:
;===================================
; 192 lines, alternate PAGE0/PAGE1 every 2 lines
;===================================
ldy #96 ; *2=192 lines ; 2
; we set PAGE0 (4) then want to NOP (61) for a total of 65
bouter_loop:
bit PAGE0 ; 4
ldx #12 ; 65 cycles with PAGE0 ; 2
bpage0_loop: ; delay 61+bit
dex ; 2
bne bpage0_loop ; 2/3
;=============
; 6+(12*5)-1=65
; we set PAGE1 (4) as well as dey (2) and bne (3) then nop (55)
;
bit PAGE1 ; 4
ldx #11 ; 65 cycles with PAGE1 ; 2
bpage1_loop:
dex ; 2
bne bpage1_loop ; 2/3
;=============
; 6+(11*5)-1=60
dey ; 2
bne bouter_loop ; 2/3
;==============
; 5 to make 65
;======================================================
; We have 4550 cycles in the vblank, use them wisely
;======================================================
; 4550
; +1 fallthrough
; -2 for ldy in previous
; -35 call through jumptable
; -7 keyboard
; -3 jmp
; ========
; 4504
;========================
; each subunit should take 4504 cycles
firework_state_machine:
check_keyboard:
lda KEYPRESS ; 4
bpl no_keypress ; 3
jmp restart
no_keypress:
jmp display_loop ; 3
restart:
jmp setup_background
.include "gr_hline.s"
;.include "../asm_routines/gr_unrle.s"
;.include "../asm_routines/keypress.s"
.include "gr_copy.s"
;.include "random16.s"
.include "vapor_lock.s"
.include "delay_a.s"

119
racebeam/random16.s Normal file
View File

@ -0,0 +1,119 @@
; 16-bit 6502 Random Number Generator (cycle-invariant version)
; Linear feedback shift register PRNG by White Flame
; http://codebase64.org/doku.php?id=base:small_fast_16-bit_prng
; The Apple II KEYIN routine increments SEEDL:SEEDH
; while waiting for keypress
SEEDL = $4E
SEEDH = $4F
XOR_MAGIC = $7657 ; "vW"
;=============================
; random16
;=============================
; takes:
; not 0, cs = 6(r16)+12(lnz)+5(nop)+ 19(deo) = 42
; not 0, cc = 6(r16)+14(lnz)+2(nop)+ 20(neo) = 42
; $0000 = 6(r16)+ 6(loz)+11nops+ 19(deo) = 42
; $8000 = 6(r16)+ 6(loz)+ 4(ceo) + 6nops+ 20(neo) = 42
; $XX00 cc = 6(r16)+ 6(loz)+4(ceo)+2(cep) +4nops+ 20(neo) = 42
; $XX00 cs = 6(r16)+ 6(loz)+4(ceo)+4(cep) +3nops+ 19(deo) = 42*
random16:
lda SEEDL ; 3
beq low_zero ; $0000 and $8000 are special values ; 3
;==========
; 6
lownz:
; -1
asl SEEDL ; Do a normal shift ; 5
lda SEEDH ; 3
rol ; 2
bcs five_cycle_do_eor ; 3
;===========
; 12
bcc two_cycle_no_eor ; 3
;==========
; 12+3-1 = 14
;===================================================================
eleven_cycle_do_eor:
nop ; 2
nop ; 2
nop ; 2
five_cycle_do_eor:
nop ; 2
three_cycle_do_eor:
sta SEEDH ; nop ; 3
do_eor:
; high byte is in A
eor #>XOR_MAGIC ; 2
sta SEEDH ; 3
lda SEEDL ; 3
eor #<XOR_MAGIC ; 2
sta SEEDL ; 3
eor_rts:
rts ; 6
;===========
; 19
;=========================================================================
six_cycles_no_eor:
nop ; 2
four_cycle_no_eor:
nop ; 2
two_cycle_no_eor:
nop ; 2
no_eor:
nop ; 2
nop ; 2
nop ; 2
nop ; 2
sta SEEDH ; 3
jmp eor_rts ; 3+6
;===========
; 20
;======================================================================
;======================================================================
low_zero:
lda SEEDH ; 3
beq eleven_cycle_do_eor ; High byte is also zero ; 3
; so apply the EOR
;============
; 6
ceo:
; -1
; wasn't zero, check for $8000
asl ; 2
beq six_cycles_no_eor ; if $00 is left after the shift; 3
; then it was $80
;===========
; 4
; else, do the EOR based on the carry
cep:
; -1
bcc four_cycle_no_eor ; 3
;============
; 2
bcs three_cycle_do_eor ; 2+3-1 = 4

238
racebeam/vapor_lock.s Normal file
View File

@ -0,0 +1,238 @@
; 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 bottom half yellow $dd
lda #$aa
ldy #24
jsr clear_page_loop ; make middle grey2 $aa
lda #$ee
ldy #10
jsr clear_page_loop ; make top half aqua $ee
; 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
;btt:
; jmp btt
; 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
; *NOTE* sometimes we end up going one (or rarely, two??) lines too far
; so instead try going 7 lines ahead, and if still dd then one more
; so single step until we get a rainbow color
; go to next line, -10
lda #28 ; 2
jsr delay_a ; delay 25+28 = 53
; total delay = 55
vl_try_again:
lda #29 ; 2
jsr delay_a ; delay 25+29 = 54
; total delay = 56
lda $C051 ; 4
cmp #$dd ; 2
beq vl_try_again ; 3
; -1
; now near end of line 112
;lda $0 ; nop to match old code ; 3
; nop ; nop to match old code ; 2
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