;################################################################################################
; Code compiled with x816 Assembler. All variables are from another game. Use your own variables# ;################################################################################################

;##########################
; Variables that exist    #
;##########################

 tile	.equ $0c80
 xpos	.equ $0c88
 char	.equ $0c8a
 bits	.equ $0cdc
 cntr	.equ $0cdd

 tmp1	.equ $7e0cde	; for adding shadow
 tmp2	.equ $7e0ce0	; for adding shadow
 ypos	.equ $0ce2	; $0ce2 + $0ce3
 fmem	.equ $8000	; actually = $7f8000

 fptr	.equ $ED0000
 wdth	.equ $ED0C00
;##########################
; Variables that exist    #
;##########################

 
Main_Proc:
.pad $ED0D00
 php
 rep #$30
 .mem 8			; this is so that I can control a 16-Bit load and x816 won't load 16-bit
 .index 16		; all the time.

 lda Char		; load the char pointer
 and #!$00ff		; manual 16-Bit load
 asl : asl
 asl : asl		; mutiply by 16 assuming it's 8x16 1-Bpp
 sta xpos
 
 sep #$20

;#####################################
; Start loading the font and adjust  #
; where needed. Simple routine.      #
;#####################################
VWF_routine:  
 lda bits		; check to see if any bits left on previous tile
 beq New_tile		; if none, then start a new tile

 cmp #$08		; was the last tile used completely?
 bne ++			; if not, store data from new font on the previous tile
 stz bits		; else, clear the bits when bits == #$08
++
 dec tile		; return to the previous tile

New_tile:		; New tile or VWF tile
 rep #$20
 lda tile 		; load the tile position
 and #!$001f
 asl : asl : asl	; 32 bytes per tile -> 8x16 tile - 2-Bpp
 asl : asl
 sta ypos		; Y = index to current TILE in memory


;######################################
; Universal font adjust If needed...  #
;######################################
Font_Load:
 ldy ypos		; get to tile data
 ldx xpos		; get to font data in ROM
 sep #$20
 lda bits		; test to see if any bits left from previous tile
 beq No_Shift		; if not, DON'T Adjust new font data on the current tile

 pha			; we have bits left, so shift tile
 lda #$08		; load a tile width
 sec
 sbc $01,s		; subtract the bits left to get the amount of bits to shift for adjusting
 pha			; push the shift amount for later uses
 lda #$10		; 16-loops per font -> Adjust a 8x16 - 2-Bpp font
 sta cntr		; loop counter

Adjust:
 pla			; pull shift amount 
 sta bits		; store TEMP in bits for loop purposes
 pha 			; push the shift amount again for loop purposes

 lda fptr,x : xba 	; load a byte from the font. Set it as the HIGH byte	
 lda #$00 : inx		; load a blank byte to save any bits shifted off of the current byte
			; increment X to the next byte

Shift:
 rep #$20		
 lsr			; this will shift a 16-Bit byte. EX: ( 01110000 00000000 )
 sep #$20
 dec bits		; decrement shift counter
 bne Shift 

 jsr Add_Shadow_ora	; add in old font data and store as new font data
			; ( SEE STORE IN THAT SUBROUTINE )
 sep #$20
 dec cntr		; decrement tile loop counter
 bne Adjust		; loop until cntr == #$00

 lda $02,s		; load bits left from stack, DON'T PULL YET!
 sta bits		; restore bits left
 lda #$00 : xba		; clear garbage
 lda char		; load char pointer value
 tax			; transfer that value to 16-Bit X
 lda wdth,x		; load width of that font
 cmp bits		; is it larger than what was left on the current tile?
 bcc NoOverFlow		; if not, then go do the math to setup for the next font on the same tile

 sec			; else, set the carry flag
 sbc $02,s		; subtract bits left from the char's width. value = Used bits on NEW tile
 sta $02,s		; save as bits to subtract from NEW tile size
 pla			; pull shift amount ( Garbage ). Leave bits used in NEW tile on stack
 lda #$08		; load tile width
 sec			
 sbc $01,s		; subtract used amount
 sta bits		; store as new bits left
 inc tile		; set pointer to NEW tile
 pla			; pull garbage
 bra Done_Font		; leave routine


;##################################
; Come here when current Char's   #
;width is <= Bits left on the tile#
;##################################
NoOverFlow:
 lda bits		; load the bits left
 sec			; set the carry flag	
 sbc wdth,x		; subtract the width of the NEWLY stored font from bits left
 sta bits 		; store that value as the NEW bits left
 pla : pla		; pull garbage
 bra Done_Font		; leave routine

;#########################################
; Do no shifting at all to the font data #
;#########################################
No_Shift:
 sep #$20
 lda #$10		; 16 loops
 sta cntr
; ldx xpos		; already loaded
; ldy NULL		; already loaded

----
 lda fptr,x : xba	; load font data
 lda #$00 : inx 	;

 jsr Add_Shadow_ora
 sep #$20
 dec cntr
 bne ----

 lda #$00 : xba
 lda char : tax
 lda #$08		; load tile width
 sec
 sbc wdth,x		; subtract font's width
 sta bits		; store as bits left

Done_Font:		; leave routine
 sep #$20
 inc tile
 plp
rtl
;/* END OF MAIN */


;############################
; Add in the shadow to font #
;############################
Add_Shadow_Ora: 	; assumes working RAM bank is $7f
 rep #$20		; set 16-Bit Accumulator
 pha			; save the adjusted WORD size data
 lsr			; set a shadow
 sta tmp1 		; store it in temp space
 pla			; pull the UNSHADOWED data 
 pha			; push it again

 ora tmp1 		; add in the shadow to make a full font + shadow
 sta tmp1		; save the shadow tile. DO NOT ALTER ANYMORE!! 
 pla			; Pull UNSHADOWED data
 eor tmp1		; clear the shadow from foreground of the font
 sta tmp2		; store the background detail. THIS IS THE SHADOW PIXEL(S)


;#############################
; Store the data correctly   #
;#############################
			; the SHADOW pixels is still in Accumulator
 sep #$20
 phb
 pea $7f7f
 plb : plb
 eor fmem+32,y		; clear the NEW shadow pixel from the existing tile. (NEW TILE ACTUALLY)
 sta fmem+32,y		; store the data. This is done so a second storage routine is not needed

 xba 			; get the byte for the Current tile
 eor fmem+00,y 		; clear the Shadowed data
 sta fmem+00,y		; store it in the current tile background
 iny 			; get to the foreground data

 plb
 rep #$20
 lda tmp1		; reload Actual tile data UNSHADOWED
 php			; save the processor status

;#############################
; Store the data correctly   #
;#############################
 sep #$20
 phb
 pea $7f7f
 plb : plb
 ora fmem+32,y		; save as above
 sta fmem+32,y
 xba
 ora fmem+00,y
 sta fmem+00,y 
 plb
 iny
 plp			; pull the processor status
rts
