
LookupBits: 		.db %00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000
LookupMaskBits: 	.db %11111110,%11111101,%11111011,%11110111,%11101111,%11011111,%10111111,%01111111
printhex:
        pha
                clc
                and #%11110000
                ror
                ror
                ror
                ror
                jsr printhexAgain
        pla
        pha
                and #%00001111
                jsr printhexAgain
        pla
        rts
printhexAgain
        cmp #10
        bcs printhex_OverNine
        clc
        adc #'0'
        jsr PrintChar
        rts
printhex_OverNine
        clc
        adc #55
        ;'adc #'A'
        jsr PrintChar
        rts

    

PrintString
        
PrintString_again
        lda [z_HL]
        cmp #255
        beq PrintString_Done
        jsr PrintChar
        jsr IncHLU
        jmp PrintString_again
PrintString_Done
        rts

; WaitChar
        ; tya
        ; pha

; WaitChar_Again
        ; jsr $FFE4       ;ReadChar
        ; beq WaitChar_Again

        ; sta t_A
        ; pla
        ; tay
        ; lda t_A
        ; rts

SwapXY
        pha
        txa
        pha
        tya

        tax
        pla
        tay
        pla
        
        rts

;NewLine
        ;lda #13
        ;jmp $FFD2       ;PrintChar


;Locate
        ;jsr SwapXY
        ;clc
        ;jsr $E50A
        ;rts

; GetPos
        
        ; sec     ;set carry
        ; jsr $E50A
        ; jsr SwapXY
        ; rts
		
CLDIR:	;Clear LDIR
		ldy #0
		sta (z_HL),y
		lda z_L
		clc
		adc #1
		sta z_E
		lda #0
		adc z_H
		sta z_D
		
LDIR:
;        jsr MemDump
;        word z_HL	      ;Address
;        byte $1          ;Lines
			
        lda [z_HL]
        sta [z_DE]
	
		jsr IncHLU
		jsr IncDEU
		
		jsr DecBCU

		lda z_B
		ora z_C
		ora z_BCU 
		bne LDIR
LDIR_Done:
		rts

IncHL:
	stz z_HLU
IncHLU:
	INC z_L
	BNE	IncHL_Done
	INC	z_H
	BNE	IncHL_Done
	INC	z_HLU
IncHL_Done:
	rts
	
IncBC:
	stz z_BCU
IncBCU:
	INC z_C
	BNE	IncBC_Done
	INC	z_B
	BNE	IncBC_Done
	INC	z_BCU
IncBC_Done:
	rts
	
IncDE:
	stz z_DEU
IncDEU:
	INC z_E
	BNE	IncDE_Done
	INC	z_D
	BNE	IncDE_Done
	INC	z_DEU
IncDE_Done:
	rts	
	
DecHL:					
	stz z_HLU
DecHLU:		
	pha
		lda z_L
		bne DecHL_l
		lda z_H
		bne DecHL_h
		dec z_HLU
DecHL_h:
		DEC z_H
DecHL_l:	
		DEC z_L
	pla
	rts

DecDE:					
	stz z_DEU
DecDEU:		
	pha
		lda z_E
		bne DecDE_e
		lda z_D
		bne DecDE_d
		dec z_DEU
DecDE_d:
		DEC z_D
DecDE_e:	
		DEC z_E
	pla
	rts
	
DecBC:					
	stz z_BCU
DecBCU:		
	pha
		lda z_C
		bne DecBC_C
		lda z_B
		bne DecBC_B
		dec z_BCU
DecBC_B:
		DEC z_B
DecBC_C:	
		DEC z_C
	pla
	rts

	
	
AddHL_DE				;Add DE to HL
		clc
		lda z_E			;Add E to L
		adc z_L
		sta z_L
		lda z_D			;Add D to H (with any carry)
		adc z_H
		sta z_H
		lda z_DEU			;Add D to H (with any carry)
		adc z_HLU
		sta z_HLU
		rts
		
AddHL_0C:		
		lda #0
		sta z_B
AddHL_BC				;Add BC to HL
		clc
		lda z_L
		adc z_C
		sta z_L
		lda z_H
		adc z_B
		sta z_H
		lda z_HLU
		adc z_BCU
		sta z_HLU
		rts
SubHL_BC				;Subtract BC to HL
		sec
		lda z_L
		sbc z_C
		sta z_L
		lda z_H
		sbc z_B
		sta z_H
		lda z_HLU
		sbc z_BCU
		sta z_HLU
		rts
SubHL_DE				;Subtract BC to HL
		sec
		lda z_L		;Subract E from L
		sbc z_E
		sta z_L
		lda z_H		;Subtract D from H (with any carry)
		sbc z_D
		sta z_H
		lda z_HLU		;Subtract D from H (with any carry)
		sbc z_DEU
		sta z_HLU
		rts
AddDE_BC				;Add DE to HL
		clc
		lda z_C
		adc z_E
		sta z_E
		
		lda z_B
		adc z_D
		sta z_D
		lda z_BCU
		adc z_DEU
		sta z_DEU
		rts
SwapNibbles				;$AB -> $BA
		ASL ;(shift left - bottom bit zero)
		ADC #$80 ;(pop top bit off)
		ROL ;(shift carry in)
		ASL ;(shift left - bottom bit zero)
		ADC #$80 ;(pop top bit off)
		ROL ;(shift carry in)
		rts