cpu 6502
nam Basketball Game
page 128,128
	dribble = 2304
	st = 2691
	int_lo = $0314
	int_hi = $0315
	chrin  = $FFCF
	chrout = $FFD2
	rcplot = $FFF0

	getin  = $FFE4
	
	plot   = $FFF0
	spenbl = $D015
	sp1clr = $D027
	sp1x   = $D000
	sp1y   = $D001
	sp2clr = $D028
	sp3clr = $D029
	sp4clr = $D02A
	sp5clr = $D02B
	sp6clr = $D02C
	sp7clr = $D02D
	sp8clr = $D02E
	runstop = $FFE1

* = $3000			;Sprite 1
sprite1 db $0E, $00, $00, $11, $00, $00, $11, $00
	db $00, $0E, $00, $00, $04, $00, $00, $3F
	db $80, $00, $2E, $80, $00, $2E, $80, $00
	db $2E, $80, $00, $2E, $40, $00, $2E, $40
	db $00, $2A, $30, $00, $0A, $00, $00, $0A
	db $30, $00, $0A, $78, $00, $0A, $78, $00
	db $0A, $30, $00, $0A, $00, $00, $0A, $00
	db $00, $3B, $80, $00, $00, $00, $00, $00

* = $3040			;Sprite 2
sprite2 db $0E, $00, $00, $11, $00, $00, $11, $00
	db $00, $0E, $00, $00, $04, $00, $00, $3F
	db $80, $00, $2E, $80, $00, $2E, $80, $00
	db $2E, $80, $00, $2E, $40, $00, $2E, $40
	db $00, $2A, $30, $00, $0A, $00, $00, $0A
	db $00, $00, $0A, $30, $00, $0A, $78, $00
	db $0A, $78, $00, $0A, $30, $00, $0A, $00
	db $00, $3B, $80, $00, $00, $00, $00, $00

* = $3080			;Sprite 3
sprite3 db $0E, $00, $00, $11, $00, $00, $11, $00
	db $00, $0E, $00, $00, $04, $00, $00, $3F
	db $80, $00, $2E, $80, $00, $2E, $80, $00
	db $2E, $80, $00, $2E, $40, $00, $2E, $40
	db $00, $2A, $30, $00, $0A, $00, $00, $0A
	db $00, $00, $0A, $00, $00, $0A, $00, $00
	db $0A, $30, $00, $0A, $78, $00, $0A, $78
	db $00, $3B, $B0, $00, $00, $00, $00, $00

	
* = $0900  ; assemble at $900 (2304)
code
	;; Setup the Interrupt Wedge For Dribbling
	lda $0314
	sta old_vec_lo
	lda $0315
	sta old_vec_hi
	sei			; freeze interrupts
	lda #$47		; put the new routine into the old vector
	sta $0314		; These are HARDCODED
	lda #$09
	sta $0315
	cli			; unfreeze interrupts
	ldx #$0C		; put locations of sprites in sprite vectors
	stx $07F8
	inx
	sta $07F9
	inx
	sta $07FA

	lda #$90		; x & y locations of sprites
	sta $D000
	sta $D001
	
	lda #$07		; colours of sprites
	sta $D027
	
	lda #$01		; Sprite Enable
	sta $D015

	db $08, $48, $8A, $48, $98, $48 ;Save Registers
	jsr .game
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
	rts

	;; Dribble The Background Routine
.newrtn	ldx sprite_ptr
	lda which_sprite, x
	dex
	stx sprite_ptr
	sta $07F8
	bne .skip
	lda #$13
	sta sprite_ptr
.skip 	jmp (old_vec_lo)
	lda #$1F
	sta $07F8
	rts
	
	;; UNINSTALL (stop dribbling)
.uninst	sei
	lda old_vec_lo
	sta $0314
	lda old_vec_hi
	sta $0315
	cli
	rts

	;; LOCAL DATA
	old_vec_lo db $FF
	old_vec_hi db $FF
	sprite_ptr db $13
	;; This is the Order of the Animation for the Dribbler
	;; 5 frames of Sprite 1, 5 of Sprite 2, then 5 of Sprite 3,
	;; Then back to Sprite 2 for 5 frames
	which_sprite db $C0, $C0, $C0, $C0, $C0
		db $C1, $C1, $C1, $C1, $C1
		db $C2, $C2, $C2, $C2, $C2
		db $C1, $C1, $C1, $C1, $C1
	
.mright lda sp1x 		; move right
	cmp #$F0		; right boundary
	bcs .nright
	clc
	adc player_speed
	sta sp1x
.nright	rts

.mleft  lda sp1x		; move left
	cmp #$1F		; left boundary
	bcc .nleft
	clc
	sbc player_speed
	sta sp1x
.nleft	rts

.mup	lda sp1y		; move up
	cmp #$8C		; upper boundary
	bcc .nup
	clc
	sbc player_speed
	sta sp1y
.nup	rts

.mdown  lda sp1y		; move down
	cmp #$B5
	bcs .ndown
	clc
	adc player_speed
	sta sp1y
.ndown	rts

.shoot	nop			; shoot (not yet implemented)
	nop			; Here is where we should populate the question.
	jsr .showq		; Subroutine to show the question
	rts

	;; GAME
.game	lda $028B		; save and set kbd state
	sta kbd1		; so that all keys repeat quickly
	lda $028C
	sta kbd1
	lda $028A
	sta kbd3
	
	ldx #$01		;x=1
	stx $028B
	stx $028C
	dex
	stx $DC0F		;x=0 initializing clock
	stx $DC0A
	stx $DC09
	stx $DC08
	dex			;x=255
	stx $028A			
	
	;; setup screen
	db $08, $48, $8A, $48, $98, $48 ;Save Registers
	jsr .scrn
	jsr .tenmvs
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
	
	rts
	;; 
	;;  MAIN LOOP
	
.tenmvs	jsr .clocker
	jsr getin
	cmp #$00		; if nothing is available...
	beq .tenmvs		; then wait some more
	
	cmp #$51		;check for 'Q'
	beq .quit		; if 'Q' then quit

	cmp #$91		;cursor up
	beq .up

	cmp #$11
	beq .down

	cmp #$1D
	beq .right

	cmp #$9D
	beq .left

	cmp #$20
	beq .shot
	jmp .tenmvs
	
	;; default is 20
.shot	jsr .shoot
	jmp .tenmvs
.up	jsr .mup
	jmp .tenmvs
.down	jsr .mdown
	jmp .tenmvs
.left	jsr .mleft
	jmp .tenmvs
.right	jsr .mright
	jmp .tenmvs
.quit	lda kbd1		; restore kbd state
	sta $028B
	lda kbd2
	sta $028C
	lda kbd3
	sta $028A
	jsr .uninst		;stop dribbling
	rts
	
	;; ===================================================
	;; .plot : plot a character somewhere in screen memory
	;;         given the column (col), row, and ascii (chr)
	;;
	
.plot	ldx .ploty
	stx .int1
	ldx #$28		;40 Decimal
	stx .int2

	db $08, $48, $8A, $48, $98, $48 ;Save Registers
	jsr .mult
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
	
	lda .rl
	adc .plotx
	sta .pokel
	lda .rh
	adc #$04
	sta .pokeh
	ldx .plotc
	stx .pokec
	db $08, $48, $8A, $48, $98, $48 ;Save Registers
	jsr .poke
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
	rts
.plotx	db $00
.ploty	db $00
.plotc	db $00
	
	;; -----------------------------------
	;; Setup the basics of the screen
.scrn	lda #$01		;white
	sta color		; for character colors
	lda #$05		;green
	sta $D020
	lda #$00		;black
	sta $D021
	
	;; clear screen
	lda #$93
	jsr $FFD2
	
	;; setup hoop
	clc
	ldx #$00
	ldy hoop, x
.hop	sty .plotx
	inx
	ldy hoop, x
	sty .ploty
	inx
	ldy hoop, x
	sty .plotc
	inx
	db $08, $48, $8A, $48, $98, $48 ;Save Registers
	jsr .plot
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
	clc
	ldy hoop, x
	cpy #$FF
	bne .hop

	
	;; setup court
	clc
	ldx #$00
	ldy court, x
.crt	sty .plotx
	inx
	ldy court, x
	sty .ploty
	inx
	ldy court, x
	sty .plotc
	inx
	db $08, $48, $8A, $48, $98, $48 ;Save Registers
	jsr .plot
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
	clc
	ldy court, x
	cpy #$FF
	bne .crt

	rts

	;; check clock and update timer
.clocker lda $DC0A
	 cmp seconds
	 bne .diftim
	 rts
	
.diftim  sta seconds
	 rts
		
	;; PLOT DATA
	color	 db $00

	;; CLOCK INFO
	sys_time db $00
	seconds	db $00
	minutes db $00

	;; storage for keyboard stuff
	kbd1 	db $00
	kbd2 	db $00
	kbd3 	db $00
	keypress db $00
	player_speed db $05

.poke	db $A9
.pokec	db $00
	db $8D
.pokel	db $01
.pokeh	db $04
	db $60

.mult   lda #$00
	sta .rh
	sta .rl
	sta .mh
	sta .index_m
	lda .int1
	sta .ml		
	lda #$01	
	sta .whichbit
.top_m	lda #$08	
	cmp .index_m	
	beq .end_m	
	lda .whichbit	
	bit .int2	
	beq .noone_m	
	clc		
	lda .rl		
	adc .ml		
	sta .rl		
	lda .rh
	adc #$00	
	sta .rh
	clc		
	lda .rh		
	adc .mh		
	sta .rh		
.noone_m	clc	
	lda .ml		
	asl		
	sta .ml		
	lda .mh		
 	rol		
			
	sta .mh		
	lda .whichbit	
	asl
	sta .whichbit
	inc .index_m
	clc
	jmp .top_m
.end_m	rts
	
.index_m	db $00
.whichbit 	db $01

.mh	db $00
.ml	db $00
	
.int1	db $00
.int2	db $00
.rh	db $00
.rl	db $00


	;;
	;; question
.showq  nop
	;; populate question (tbd)
	db $08, $48, $8A, $48, $98, $48 ;Save Registers
	jsr .rnd
	lda rval
	sta xval

	jsr .rnd
	lda rval
	sta yval
	lda rval30
	sta yis

	jsr .rnd
	lda rval
	sta xcval
	adc #$30
	sta xc

	jsr .rnd
	lda rval
	sta ycval
	adc #$30
	sta yc

	jsr .rnd
	lda rval
	cmp #$04
	bne .pls
	lda #$2d			; -
	ldx #$FF
	jmp .mns
	inx
	inx
.pls	lda #$2b			; +
	
.mns	sta sgn
	stx sgnval
	
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
	lda xval
	sta .nm
	jsr .cvrt
	lda .do
	
	;; display question
	ldx #$FF
.shq	lda txt, x

	db $08, $48, $8A, $48, $98, $48 ;Save Registers	
	jsr $FFD2
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
	clc
	inx
	ldy txt, x
	cpy #$FF
	bne .shq

	;; wait for response (tbd)
	nop
	;; If Correct Shoot/Score
	;; otherwise Shoot/Miss
	
	rts			
sgnval	db $00
	;; home s    o    l    v    e         f    o    r         x         w    h    e    n         y    =
txt	db $13, $53, $4F, $4C, $56, $45, $20, $46, $4F, $52, $20, $58, $20, $57, $48, $45, $4E, $20, $59, $3D 
	;;      :
yis	db $20, $3A, $20

	;; x coefficient
	;;      x
xc	db $20, $58
	;; Sign (+/-)
sgn	db $20
	;; y coefficient
	;;      y    =
yc	db $20, $59, $3D
	;; Solution
sltn	db $20, $20, $20, $20
	;;           x    =
	db $20, $20, $58, $3D, $FF
xval	db $00
yval	db $00
xcval	db $00
ycval	db $00
rval	db $00
rval30	db $00
	
	;; Random Number Generator
.rnd	ldx $0912
.rnd0	jsr .rnd1
	dex
	bne .rnd0
.rnd1	dey
	bne .rnd1
	lda $D012
	and #$07
	adc #$02
	sta rval
	adc #$30
	sta rval30
	rts

	;; Data Storage for Display Number Routine
.nm	db $00
.dh	db $20
.dt	db $20
.do	db $20
	
	;; Convert the hexidecimal number in .nm
	;; to 3 decimal digits in .dh,.dt, and .do 
.cvrt   db $08, $48, $8A, $48, $98, $48 ;Save Registers
.1	lda #$20
	sta .dh
	sta .dt
	sta .do
	clc
	;; Get the Hex Value and put it in reg-x
	ldx .nm

	;; Compare it with 200
	cpx #$C8

	;; If it's lt 200, check for lt 100
	bcc .lt200

	;; Otherwise, it's gt 200
	;; Si put a "2" in the hundreds place
	lda #$32
	sta .dh

	;; Put the Hex Value that's in x-reg
	;; into the Accumulator
	txa

	;; Subtract 200 from it
	sbc #$C8

	;; Store The result in the data storage
	sta .nm

	;; Clear the Carry Flag
	clc
	
	;; Store a Zero in the Tens Place for now
	;; Because we know that there's a hundreds
	;; digit that is at least a 2
	lda #$30
	sta .dt

	;; Because we've subtracted 200 from it
	;; it MUST be less than 100, so JMP there
	jmp .lt100
	
.lt200	ldx .nm
	cpx #$64
	bcc .lt100
	
	lda #$31
	sta .dh
	txa
	sbc #$64
	sta .nm
	clc
	;; Store a Zero in the Tens Place for now
	lda #$30
	sta .dt

.lt100  ldx .nm
	cpx #$5A
	bcc .lt90
	lda #$39
	sta .dt
	txa
	sbc #$5A
	sta .nm
	clc

.lt90   ldx .nm
	cpx #$50
	bcc .lt80
	lda #$38
	sta .dt
	txa
	sbc #$50
	sta .nm
	clc
	
.lt80	ldx .nm
	cpx #$46
	bcc .lt70
	lda #$37
	sta .dt
	txa
	sbc #$46
	sta .nm
	clc
	
.lt70	ldx .nm
	cpx #$3C
	bcc .lt60
	lda #$36
	sta .dt
	txa
	sbc #$3C
	sta .nm
	clc
	
.lt60	ldx .nm
	cpx #$32
	bcc .lt50
	lda #$35
	sta .dt
	txa
	sbc #$32
	sta .nm
	clc
	
.lt50	ldx .nm
	cpx #$28
	bcc .lt40
	lda #$34
	sta .dt
	txa
	sbc #$28
	sta .nm
	clc
	
.lt40	ldx .nm
	cpx #$1E
	bcc .lt30
	lda #$33
	sta .dt
	txa
	sbc #$1E
	sta .nm
	clc
	
.lt30	ldx .nm
	cpx #$14
	bcc .lt20
	lda #$32
	sta .dt
	txa
	sbc #$14
	sta .nm
	clc
	
.lt20   ldx .nm
	cpx #$0A
	bcc .lt10
	lda #$31
	sta .dt
	txa
	sbc #$0A
	sta .nm
	clc
	
.lt10	lda .nm
	
	adc #$30
	sta .do
	sta .nm
	clc
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
.cvrt_	rts

.dispn	lda .dh
	cmp #$20
	beq .3
	jsr $FFD2
.3	lda .dt
	cmp #$20
	beq .4
	jsr $FFD2
.4	lda .do
	jsr $FFD2
	rts
	
	db $68, $A8, $68, $AA, $68, $28 ;Recall Registers
.dispn_	rts

	
hoop db $1C, $04, $4F, $1D, $04, $77, $1E, $04, $77, $1F, $04, $77, $20, $04, $50
     db $1C, $05, $74, $20, $05, $6A, $1C, $06, $74, $20, $06, $6A, $1C, $07, $74
     db $20, $07, $6A, $1C, $08, $74, $20, $08, $6A, $1C, $09, $4C, $1D, $09, $64
     db $1E, $09, $64, $1F, $09, $64, $20, $09, $7A, $1E, $0A, $5D, $1E, $0B, $5D
     db $1E, $0C, $5D, $1E, $0D, $5D, $1E, $0D, $5D, $1E, $0E, $5D, $1E, $0F, $5D
     db $FF, $FF, $FF
	
court db $00, $0C, $70, $01, $0C, $43, $02, $0C, $43, $03, $0C, $43, $04, $0C, $43
      db $05, $0C, $43, $06, $0C, $43, $07, $0C, $43, $08, $0C, $43, $09, $0C, $43
      db $0A, $0C, $43, $0B, $0C, $43, $0C, $0C, $43, $0D, $0C, $43, $0E, $0C, $43
      db $0F, $0C, $43, $10, $0C, $43, $11, $0C, $43, $12, $0C, $43, $13, $0C, $43
      db $14, $0C, $43, $15, $0C, $43, $16, $0C, $43, $17, $0C, $43, $18, $0C, $43
      db $19, $0C, $43, $1A, $0C, $43, $1B, $0C, $43, $1C, $0C, $6E, $1C, $0D, $5D
      db $1C, $0E, $5D, $1C, $0F, $5D, $1C, $10, $5D, $1C, $11, $5D, $1C, $12, $5D
      db $1C, $13, $7D, $1B, $13, $43, $1A, $13, $43, $19, $13, $43, $18, $13, $43
      db $18, $13, $43, $17, $13, $43, $16, $13, $43, $15, $13, $43, $14, $13, $43
      db $13, $13, $43, $12, $13, $43, $11, $13, $43, $10, $13, $43, $0F, $13, $43
      db $0E, $13, $43, $0D, $13, $43, $0C, $13, $43, $0B, $13, $43, $0A, $13, $43
      db $09, $13, $43, $08, $13, $43, $07, $13, $43, $06, $13, $43, $05, $13, $43
      db $04, $13, $43, $03, $13, $43, $02, $13, $43, $01, $13, $43, $00, $13, $6D
      db $00, $0D, $5D, $00, $0E, $5D, $00, $0F, $5D, $00, $10, $5D, $00, $11, $5D
      db $00, $12, $5D, $FF, $FF, $FF

code
