;*
;* Draw player viewport
;*
DRAWVIEW
	STA	VIEWPORT
;*
;* Render ground from bottom -> up
;*	
DRAWGRND LDA	ANGLE	; Calc S,T,DS,DT table address
	AND	#$7F	; ST table offset = angle * horizon * 8
	LSR		; horizon * 8 = 16 * 8 = 128
	TAX		; angle * 128 = angle << 7 == (angle<<8) >> 1
	LDA	#$00	; 
	STA	SCANLN	; for later...
	ROR
;	CLC		; Carry = 0 from above
	ADC	#<ST_TBL ; Add ST table address to offset
	STA	STPTRL
	TXA
	ADC	#>ST_TBL
	STA	STPTRH
;*
;* Draw even and odd scanlines together
;*
DRAWGRNDSCN
	LDA	VIEWPORT ; Max scanline number/2
	SEC
	SBC	SCANLN
	TAX
	LDA	TXT_TBLL,X
	STA	GBASL
	LDA	TXT_TBLH,X
	ORA	GPAGE
	STA	GBASH
;*
;* Draw right half of odd scanline
;*
	LDY	#$00	; Copy and update working values from ST table
	STY	S_MIDFL	; Convert ST_TBL from 6.10 fixpt to 8.16 fixpt and add XYVIEW
	STY	T_MIDFL
	LDA	(STPTR),Y
	STA	S_MIDFH
	INY
	LDA	(STPTR),Y
	CMP	#$80
	ROR
	ROR	S_MIDFH
	ROR	S_MIDFL
	CMP	#$80
	ROR
	ROR	S_MIDFH
	ROR	S_MIDFL
	STA	S_MIDI
	LDA	S_MIDFH
	CLC
	ADC	XVIEWF
	STA	S_MIDFH
	LDA	S_MIDI
	ADC	XVIEWI
	STA	S_MIDI        
	INY
	LDA	(STPTR),Y
	STA	T_MIDFH
	INY
	LDA	(STPTR),Y
	CMP	#$80
	ROR
	ROR	T_MIDFH
	ROR	T_MIDFL
	CMP	#$80
	ROR
	ROR	T_MIDFH
	ROR	T_MIDFL
	STA	T_MIDI
	LDA	T_MIDFH
	CLC
	ADC	YVIEWF
	STA	T_MIDFH
	LDA	T_MIDI
	ADC	YVIEWI
	STA	T_MIDI
	INY
	LDA	(STPTR),Y
	STA	DS_FRACL
	LDX	#$00
	INY
	LDA	(STPTR),Y
	CMP	#$80
	BCC	+
	DEX
+	STA	DS_FRACH
	STX	DS_INT
	ROR		; S = S_MID + DS/2
	STA	S_FRACH
	LDA	DS_FRACL
	ROR
	CLC
	ADC	S_MIDFL
	STA	S_FRACL
	LDA	S_FRACH
	ADC	S_MIDFH
	STA	S_FRACH
	TXA
	ADC	S_MIDI
	STA	S_INT
	INY
	LDA	(STPTR),Y
	STA	DT_FRACL
	LDX	#$00
	INY
	LDA	(STPTR),Y
	CMP	#$80
	BCC	+
	DEX
+	STA	DT_FRACH
	STX	DT_INT
	ROR		; T = T_MID + DT/2
	STA	T_FRACH
	LDA	DT_FRACL
	ROR
	CLC
	ADC	T_MIDFL
	STA	T_FRACL
	LDA	T_FRACH
	ADC	T_MIDFH
	STA	T_FRACH
	TXA
	ADC	T_MIDI
	STA	T_INT
	TYA		; Update STPTR to next scanline
	SEC
	ADC	STPTRL
	STA	STPTRL
	BCC	+
	INC	STPTRH
+	LDY	#15	; Right-middle of screen
	BNE	GRNDRO
-	LDA	#$00
	BEQ	+
GRNDRO	LDA	S_INT
	CMP	#$40
	BCS	-
	LDX	T_INT
	CPX	#$40
	BCS	-
	ORA	MAP_TBLL,X
	STA	RDTEXRO+1
	LDA	MAP_TBLH,X
	STA	RDTEXRO+2
RDTEXRO	LDA	MAP
	AND	#$F0
+	STA	(GBASE),Y
	LDA	S_FRACL	; S = S + DS
	CLC
	ADC	DS_FRACL
	STA	S_FRACL
	LDA	S_FRACH
	ADC	DS_FRACH
	STA	S_FRACH
	LDA	S_INT
	ADC	DS_INT
	STA	S_INT
	LDA	T_FRACL	; T = T + DT
	CLC
	ADC	DT_FRACL
	STA	T_FRACL
	LDA	T_FRACH
	ADC	DT_FRACH
	STA	T_FRACH
	LDA	T_INT
	ADC	DT_INT
	STA	T_INT
	INY
	CPY	#30
	BNE	GRNDRO
;*
;* Draw left half of odd scanline
;*
	LDA	DS_FRACL
	STA	TMP
	LDA	DS_FRACH
	CMP	#$80
	ROR		; S = S_MID - DS/2
	STA	TMP+1
	ROR	TMP
	LDA	S_MIDFL
	SEC
	SBC	TMP
	STA	S_FRACL
	LDA	S_MIDFH
	SBC	TMP+1
	STA	S_FRACH
	LDA	S_MIDI
	SBC	DS_INT
	STA	S_INT
	LDA	DT_FRACL
	STA	TMP
	LDA	DT_FRACH
	CMP	#$80
	ROR		; T = T_MID - DT/2
	STA	TMP+1
	ROR	TMP
	LDA	T_MIDFL
	SEC
	SBC	TMP
	STA	T_FRACL
	LDA	T_MIDFH
	SBC	TMP+1
	STA	T_FRACH
	LDA	T_MIDI
	SBC	DT_INT
	STA	T_INT
	LDY	#14	; Left-middle of screen
	BNE	GRNDLO
-	LDA	#$00
	BEQ	+
GRNDLO	LDA	S_INT
	CMP	#$40
	BCS	-
	LDX	T_INT
	CPX	#$40
	BCS	-
	ORA	MAP_TBLL,X
	STA	RDTEXLO+1
	LDA	MAP_TBLH,X
	STA	RDTEXLO+2
RDTEXLO	LDA	MAP
	AND	#$F0
+	STA	(GBASE),Y
	LDA	S_FRACL	; S = S + DS
	SEC
	SBC	DS_FRACL
	STA	S_FRACL
	LDA	S_FRACH
	SBC	DS_FRACH
	STA	S_FRACH
	LDA	S_INT
	SBC	DS_INT
	STA	S_INT
	LDA	T_FRACL	; T = T + DT
	SEC
	SBC	DT_FRACL
	STA	T_FRACL
	LDA	T_FRACH
	SBC	DT_FRACH
	STA	T_FRACH
	LDA	T_INT
	SBC	DT_INT
	STA	T_INT
	DEY
	BPL	GRNDLO
;*
;* Draw right half of even scanline
;*
	DEC	VDLY
	BNE	+
	BIT	SPEAKER
	LDA	VTONE
	STA	VDLY
+	LDY	#$00	; Copy and update working values from ST table
	STY	S_MIDFL	; Convert ST_TBL from 6.10 fixpt to 8.16 fixpt and add XYVIEW
	STY	T_MIDFL
	LDA	(STPTR),Y
	STA	S_MIDFH
	INY
	LDA	(STPTR),Y
	CMP	#$80
	ROR
	ROR	S_MIDFH
	ROR	S_MIDFL
	CMP	#$80
	ROR
	ROR	S_MIDFH
	ROR	S_MIDFL
	STA	S_MIDI
	LDA	S_MIDFH
	CLC
	ADC	XVIEWF
	STA	S_MIDFH
	LDA	S_MIDI
	ADC	XVIEWI
	STA	S_MIDI        
	INY
	LDA	(STPTR),Y
	STA	T_MIDFH
	INY
	LDA	(STPTR),Y
	CMP	#$80
	ROR
	ROR	T_MIDFH
	ROR	T_MIDFL
	CMP	#$80
	ROR
	ROR	T_MIDFH
	ROR	T_MIDFL
	STA	T_MIDI
	LDA	T_MIDFH
	CLC
	ADC	YVIEWF
	STA	T_MIDFH
	LDA	T_MIDI
	ADC	YVIEWI
	STA	T_MIDI
	INY
	LDA	(STPTR),Y
	STA	DS_FRACL
	LDX	#$00
	INY
	LDA	(STPTR),Y
	CMP	#$80
	BCC	+
	DEX
+	STA	DS_FRACH
	STX	DS_INT
	ROR		; S = S_MID + DS/2
	STA	S_FRACH
	LDA	DS_FRACL
	ROR
	CLC
	ADC	S_MIDFL
	STA	S_FRACL
	LDA	S_FRACH
	ADC	S_MIDFH
	STA	S_FRACH
	TXA
	ADC	S_MIDI
	STA	S_INT
	INY
	LDA	(STPTR),Y
	STA	DT_FRACL
	LDX	#$00
	INY
	LDA	(STPTR),Y
	CMP	#$80
	BCC	+
	DEX
+	STA	DT_FRACH
	STX	DT_INT
	ROR		; T = T_MID + DT/2
	STA	T_FRACH
	LDA	DT_FRACL
	ROR
	CLC
	ADC	T_MIDFL
	STA	T_FRACL
	LDA	T_FRACH
	ADC	T_MIDFH
	STA	T_FRACH
	TXA
	ADC	T_MIDI
	STA	T_INT
	TYA		; Update STPTR to next scanline
	SEC
	ADC	STPTRL
	STA	STPTRL
	BCC	+
	INC	STPTRH
+	LDY	#15	; Right-middle of screen
	BNE	GRNDRE
-	LDA	#$00
	BEQ	+
GRNDRE	LDA	S_INT
	CMP	#$40
	BCS	-
	LDX	T_INT
	CPX	#$40
	BCS	-
	ORA	MAP_TBLL,X
	STA	RDTEXRE+1
	LDA	MAP_TBLH,X
	STA	RDTEXRE+2
RDTEXRE	LDA	MAP
	AND	#$0F
+	ORA	(GBASE),Y
	STA	(GBASE),Y
	LDA	S_FRACL	; S = S + DS
	CLC
	ADC	DS_FRACL
	STA	S_FRACL
	LDA	S_FRACH
	ADC	DS_FRACH
	STA	S_FRACH
	LDA	S_INT
	ADC	DS_INT
	STA	S_INT
	LDA	T_FRACL	; T = T + DT
	CLC
	ADC	DT_FRACL
	STA	T_FRACL
	LDA	T_FRACH
	ADC	DT_FRACH
	STA	T_FRACH
	LDA	T_INT
	ADC	DT_INT
	STA	T_INT
	INY
	CPY	#30
	BNE	GRNDRE
;*
;* Draw left half of even scanline
;*
	LDA	DS_FRACL
	STA	TMP
	LDA	DS_FRACH
	CMP	#$80
	ROR		; S = S_MID - DS/2
	STA	TMP+1
	ROR	TMP
	LDA	S_MIDFL
	SEC
	SBC	TMP
	STA	S_FRACL
	LDA	S_MIDFH
	SBC	TMP+1
	STA	S_FRACH
	LDA	S_MIDI
	SBC	DS_INT
	STA	S_INT
	LDA	DT_FRACL
	STA	TMP
	LDA	DT_FRACH
	CMP	#$80
	ROR		; T = T_MID - DT/2
	STA	TMP+1
	ROR	TMP
	LDA	T_MIDFL
	SEC
	SBC	TMP
	STA	T_FRACL
	LDA	T_MIDFH
	SBC	TMP+1
	STA	T_FRACH
	LDA	T_MIDI
	SBC	DT_INT
	STA	T_INT
	LDY	#14	; Left-middle of screen
	BNE	GRNDLE
-	LDA	#$00
	BEQ	+
GRNDLE	LDA	S_INT
	CMP	#$40
	BCS	-
	LDX	T_INT
	CPX	#$40
	BCS	-
	ORA	MAP_TBLL,X
	STA	RDTEXLE+1
	LDA	MAP_TBLH,X
	STA	RDTEXLE+2
RDTEXLE	LDA	MAP
	AND	#$0F
+	ORA	(GBASE),Y
	STA	(GBASE),Y
	LDA	S_FRACL	; S = S + DS
	SEC
	SBC	DS_FRACL
	STA	S_FRACL
	LDA	S_FRACH
	SBC	DS_FRACH
	STA	S_FRACH
	LDA	S_INT
	SBC	DS_INT
	STA	S_INT
	LDA	T_FRACL	; T = T + DT
	SEC
	SBC	DT_FRACL
	STA	T_FRACL
	LDA	T_FRACH
	SBC	DT_FRACH
	STA	T_FRACH
	LDA	T_INT
	SBC	DT_INT
	STA	T_INT
	DEY
	BPL	GRNDLE
	DEC	VDLY
	BNE	+
	BIT	SPEAKER
	LDA	VTONE
	STA	VDLY
+	INC	SCANLN	; Move on to next even/odd scanlines
	LDY	SCANLN
	CPY	#HORIZON/2
	BEQ	DRAWBACK
	JMP	DRAWGRNDSCN
;*
;* Draw background
;*
DRAWBACK LDA	VIEWPORT ; Max scanline number/2
	SEC
	SBC	SCANLN
	TAY
	LDA	TXT_TBLL,Y
	STA	STBCK1+1
	LDA	TXT_TBLH,Y
	ORA	GPAGE
	STA	STBCK1+2
	LDA	TXT_TBLL-1,Y
	STA	STBCK2+1
	LDA	TXT_TBLH-1,Y
	ORA	GPAGE
	STA	STBCK2+2
	LDY	#29
	LDA	ANGLE
	CLC
	ADC	#15
-	AND	#$7F
	TAX
	LDA	WALLS,X
STBCK1	STA	$0400,Y
	AND	#$F0
STBCK2	STA	$0400,Y
	DEX
	TXA
	DEY
	BPL	-
;*
;* Draw top-down map view
;*
DRAWMAP	LDA	VIEWPORT
	TAY
	STY	SCANLN
	SEC
	SBC	#9	; 20 - 2
	STA	VIEWPORT
DRAWMAPSCN
	LDA	TXT_TBLL,Y
	STA	STMAP+1
	LDA	TXT_TBLH,Y
	ORA	GPAGE
	STA	STMAP+2
	LDX	#$80
	TYA
	CLC
	ADC	YVIEWI
	SEC
	SBC	#$05
;	SEC
	SBC	VIEWPORT
	CMP	#$40
	BCS	+	; Scanline is off the map, so force black coordinates
	TAX
	LDA	MAP_TBLL,X
	STA	LDMAP+1
	LDA	MAP_TBLH,X
	STA	LDMAP+2
	LDA	XVIEWI
	SEC
	SBC	#$04
	TAX
+	LDY	#31
-	CPX	#$40
	BCC	LDMAP
	LDA	#$00
	BEQ	STMAP
LDMAP	LDA	MAP,X
STMAP	STA	$0400,Y
	INX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