;===================================================================
L0CC1:
;subroutine, display "1/2 PLAYER" and "arrow"
	lodi,r0	0x2D
	stra,r0	X1FC1
	stra,r0	X1FC2		;set all object colours to 101 (displays as green)
	lodi,r0	0xAA
	stra,r0	X1FC0		;set all object sizes to 10 (32H,40V)
	lodi,r1	0x0E		;use r1 as loop counter and index
L0CD0:
	loda,r0	X0E6D,r1,-
	stra,r0	X1F10,r1		;load object 2 from 0E6D
	loda,r0	X0E7B,r1
	stra,r0	X1F20,r1		;load object 3 from 0E7B
	loda,r0	X0E89,r1
	stra,r0	X1F40,r1		;load object 4 from 0E89
	loda,r2	X1FAC			
	tmi,r2	0x01			;if bit 01 of 1FAC is 0
	bctr,eq	L0CEE
	loda,r0	X0E51,r1			;then load object 1 with "1" (bit 01 was 0)
	bctr,un	L0CF1		
L0CEE:
	loda,r0	X0E5F,r1			;else load object 1 with "2" (bit 01 was 1)
L0CF1:
	stra,r0	X1F00,r1		;save r0 in object 1
	brnr,r1	L0CD0		;loop until r1=0


	bsta,un	L0D9B		;wait for object 4 to be completed
	lodi,r2	0x20		
	loda,r1	X1FA3		;which player?
	tmi,r1	0x80		
	bcfr,eq	L0D05		;if bit 80 = 1
	loda,r2	X1F50		
L0D05:
	stra,r2	X1F0B		;then object 1 horizontal position is = 20H, 
				;else = 1F50
	lodi,r1	0x0A		;set up index register to move 0A bytes
	loda,r2	X1F4E		;1F4E?????
	andi,r2	0x08
	bctr,eq	L0D16		;if bit 08 = 1
	lodi,r3	0xFF		
	stra,r3	X1FC1		;then object 1&2 colour=111
L0D16:
	loda,r0	X0D24,r1,-
	stra,r0	X1F00,r1	;load down arrow into object 1
	brnr,r1	L0D16
	lodi,r1	0x55
	stra,r1	X1FC0		set all object sizes to 01
	retc,un
;==========================================================================
X0D24:				;down arrow
0D20             08 08 08 00
0D28 08 08 63 36 1C 08
;=========================================================================
					
;subroutine 
;inputs r2 1F96 1F97 1F98  datatable F54
;outputs object2 object3 object4
;X0D2E  loda,r0 X1F96			??????????????
;	addz	r2
	loda,r0	X0F54,r0,+
	stra,r0	X1F12,r2,+

	loda,r0	X1F97
	addz	r2
	loda,r0	X0F54,r0
	stra,r0	X1F22,r2

	loda,r0	X1F98
	addz	r2
	loda,r0	X0F54,r0
	stra,r0	X1F42,r2

	comi,r2	0x04
	retc,eq
	comi,r2	0x07
	bcfr,eq	L0D2E		;if not 0, go back and repeat
	retc,un
;
; =============================================

; subroutine - wait for vertical reset to clear
; called from 0034, 02B0, 0AF5, 0B1C, 0C3E, 0C98, 0CAE

L0D54:
	cpsl	0x02    ;set COM bit to arithmetic compare
L0D56:
	spsu		;test upper PSW (wait for Sense bit to be set? Vertical reset?)
	bctr,lt	L0D56
L0D59:
	spsu		;test upper PSW (wait for Sense bit clear)
	bcfr,lt	L0D59
	ppsl	0x02	;set COM to logical compare
	retc,un
; ==================================================
L0D5F:	;subroutine reprogram duplicate objects
	;enter with r2 set ready for call to 0D90
	bsta,un	L0D90		;subroutine  - set object colours and size 
	lodi,r2	0xFF		;increments to 0 before indexed address is calculated
L0D64:
	loda,r0	X0E97,r2,+	;gets data from E97+ and stores it in
	stra,r0	X1F10,r2		;object 2
	stra,r0	X1F20,r2		;object 3
	stra,r0	X1F40,r2		;object 4
	comi,r2	0x01		
	bcfr,eq	L0D64		;only loops twice?
	bsta,un	L0DC0		;subroutine DC0, set horizontal pos of duplicate objects 2,3,4
	bsta,un	L0D9B		;subroutine D9B, wait for object 4 to be completed
	lodi,r3	0x54
	stra,r3	X1FC0		;object 2,3,4 are size 1, object 1 is size 0
L0D7F:
	loda,r0	X0E97,r2,+	
	stra,r0	X1F10,r2
	stra,r0	X1F20,r2
	stra,r0	X1F40,r2
	comi,r2	0x05
	bcfr,eq	L0D7F		;loads 4 more bytes from E97 into object
	retc,un
;========================================================
; subroutine - set object colours and size
; loads three bytes of data to set object colours and sizes
; fetches data from table at EA6
; enter with r2 as index pointer to last byte + 1
				;0EA0                   00 28
				;0EA8 00 00 2D 2D FC 29 09 00
				;0EB0 29 09 50 2B 3F 54 2B 20
				;0EB8 00 2E 00 00 28 30 00 28
				;0EC0 06 66 5A 3C 42 7E 24
L0D90:
	lodi,r1	0x03		;r1=3
L0D92:
	loda,r0	X0EA6,r2,-	;
	stra,r0	X1FC0,r1,-	;object colours, (and size??)
	brnr,r1	L0D92		;repeat until r1=0
	retc,un
;=======================================================
; subroutine wait for object 4 to be completed
; called from D64
L0D9B:
	loda,r3	X1FCA		;object status
	tmi,r3	0x01		;test bit 01, object 4 complete
	bcfa,eq	L0D9B		;branch if not equal
	retc,un
;===========================================================
; subroutine, copy data from vertical bar definition and save in first two bytes of objects 2,3,4 
; argument  r3 is index. 
; The only two calls enter with r3 equal 86 and 8C pointing to the vertical bar definition.
L0DA4:			
	loda,r0	X1F00,r3	;copy byte from vertical bar definition
	stra,r0	X1F10
	stra,r0	X1F11		;and save in first two bytes of object 2 shape
	loda,r0	X1F01,r3	;copy next byte from vertical bar definition
	stra,r0	X1F20
	stra,r0	X1F21		;and save in first two bytes of object 3 shape
	loda,r0	X1F02,r3	;copy next byte from vertical bar definition
	stra,r0	X1F40
	stra,r0	X1F41		;and save in first two bytes of object 4 shape
	retc,un
;===================================================================
;subroutine set horizontal pos of duplicate objects 2,3,4

L0DC0:
	loda,r0	X1F00,r3   ;what is in r3?
	stra,r0	X1F1B		;set horizontal pos of duplicate object 2
	loda,r0	X1F01,r3
	stra,r0	X1F2B		;set horizontal pos of duplicate object 3
	loda,r0	X1F02,r3
	stra,r0	X1F4B		;set horizontal pos of duplicate object 4
	retc,un
;===================================================================
;subroutine, add r3 to score
;arguments:
;r3 is added to score
;bit 08 of 1FA3 determines which pair of score bytes to add too (which player?)
;score from either 1FA4&1FA5, or 1FA6&1FA7
L0DD3:
	ppsl	0x08		;set with/wo carry
	cpsl	0x01		;clear carry
	loda,r2	X1FA3		
	tmi,r2	0x08		;test bit 08 of 1FA3
	bctr,eq	L0DE6		;if 0 goto 0DE6
	loda,r0	X1FA4		
	loda,r1	X1FA5		;get variables 1FA4 and 1FA5
	bctr,un	L0DEC
;
L0DE6:				;else
	loda,r0	X1FA6
	loda,r1	X1FA7		;get variables 1FA6 and 1FA7
L0DEC:
	addi,r0	0x66		
	addi,r1	0x66		;get ready for decimal arithmetic
	cpsl	0x01
	addz	r3		;add r3 to it
	dar,r0			;adjust low byte
	addi,r1	0x00		; add carry to high byte?
	dar,r1			;adjust high byte
	tpsl	0x01		
	bcfr,eq	L0E00		;if carry set?
	iori,r2	0x01
	stra,r2	X1FA3		;set bit 01 of 1FA3
L0E00:
	tmi,r2	0x08		;else test bit 08 of 1FA3
	bctr,eq	L0E0D		;if 0 goto E0D
	stra,r0	X1FA4
	stra,r1	X1FA5		;save variables 1FA4 and 1FA5
	cpsl	0x08		;clear WC
	retc,un
L0E0D:
	stra,r0	X1FA6
	stra,r1	X1FA7		;save variables 1FA6 and 1FA7
	cpsl	0x08		;clear WC
	retc,un
;=============================================================
;subroutine, test/adjust score digits in r0 and r1 and outputs to PVI
; argument r0 r1
L0E16:
	brnr,r1	L0E25		;if r1 not 0 goto E25
	lodi,r1	0xFF		;else r1=ff
	brnr,r0	L0E1D		;if r0 not 0 goto E1D
	lodz	r1		;r0=r1
L0E1D:
	comi,r0	0x0F		
	bctr,gt	L0E2B		;if r0>0F goto E2B
	iori,r0	0xF0		; r0= r0 OR F0
	bctr,un	L0E2B		;go to E2B
;
L0E25:
	comi,r1	0x0F
	bctr,gt	L0E2B		;if r1>0F goto E2B
	iori,r1	0xF0		; r1= r1 OR F0
L0E2B:
	stra,r1	X1FC8		;score 1&2
	stra,r0	X1FC9		;score 3&4
	retc,un
;=============================================================
;subroutine initialize PVI
L0E32:
	lodi,r3	0x0A
L0E34:
	loda,r0	X0E9C,r3,-
	stra,r0	X1FC0,r3	
	brnr,r3	L0E34		;load 10 bytes to control in pvi
				;0E98             00 2D 2D 03
				;0EA0 00 00 08 00 FF FF

	lodi,r3	0x04
	stra,r3	X1E80		;store r3 to ROM ???? This might be activating CE2 which reads the switches?
	lodi,r3	0x0E
	stra,r3	X1FA8		; 0E to horizontal bar 
	lodi,r3	0xFF
	stra,r3	X1F1E		; FF to RAM 1f1e
X0E4B:
	lodi,r3	0x33
	stra,r3	X1F1F		; 33 to RAM 1f1f
	retc,un
;======================================================