; Assumes 8_bit mem, 16-Bit index
; VAL32		= 3-bytes to convert from number to string
; TEMP		= 3 bytes, byte 1 = free, byte 2 = Length and size
;		  byte 3 = AFTER caculating = length. byte 2 AFTER calculating = amount of bytes
; DONE 		= temp space for adding up the numbers
; BUFF		= buffer to hold converted number
;
; NOTE: thi smay be a hard routine to catch off hand, but debug it and you'll understand it.
;
;#####################################
; Routine to convert Snes Hex values #
; To standard Decimal display values #
;#####################################
Hex2Asc .macro (VAL32,TEMP,DONE,BUFF) ; 32-Bit,16-Bit,32-Bit,9Byte buffer
 pha
 phy
 phx
 php			; push all regs settings

 rep #$10
 sep #$20


;#############################
; start to convert the value #
;#############################
 ldx #!$0000
 ldy #!$0000
 stx DONE
 stx DONE+2

 lda TEMP+1		; say TEMP = $83
 and #$f0		; $80 divided by 16 = $08, length of the ASCII string to store
 lsr : lsr
 lsr : lsr
 sta TEMP+2		; save the length of string
 lda TEMP+1		; say TEMP = $83
 and #$0f		; $03
 sta TEMP+1 		; the amount of bytes in the number. 3 = $FF,$FF,$FF


;#############################
; Byte loop area	     #
;#############################
_Hex2DEC_:
 lda VAL32,x
 phx			; compare THIS x value to counter
 tyx
 ldy #!$0008		; 8-Bit value per loop
 sta TEMP		; low byte = shift byte for adding
 

;#############################
; Main routine loop is here  #
;#############################
Addem_loop:
 clc
 lsr TEMP		; if the bit is set, add the number in decimal mode
 bcc _No_Add

 clc
 php
 rep #$20

 sed			; else, set decimal mode and add numbers
 lda Decdata,x		; load and add decimal values     
 adc DONE+2
 sta DONE+2

 lda Decdata+2,x	; load and add decimal values     
 adc DONE
 sta DONE
 cld 
 plp 


_No_Add:
 inx
 inx
 inx
 inx
 dey 
 bne Addem_loop
 
 txy 
 plx 			; the x counter compare value
 inx

 sep #$10		; 8-Bit X,Y
 cpx TEMP+1
 bcs Num2Ascii

 rep #$10 		; 16-Bit X,Y
 bra _Hex2Dec_


;#############################
; Convert the Num to String  #
;#############################
Num2Ascii:
 rep #$30
    
 lda DONE		; put the string in descending order
 xba
 sta DONE
 lda DONE+2
 xba
 sta DONE+2

 sep #$20
 ldx #!$0000
 ldy #!$0000
 

Num2Text:
 lda DONE,y
 pha
 lsr : lsr
 lsr : lsr
 clc
 adc #'0'
 sta BUFF,x

 pla
 and #$0f
 adc #'0'
 sta BUFF+1,x
 iny
 inx
 inx
 dec TEMP+2
 dec TEmp+2
 bne Num2Text
 bra _Got_Number


;#########################
; The decimal conversion #
;#########################
DecData
	.dcb $01,$00,$00,$00
	.dcb $02,$00,$00,$00
	.dcb $04,$00,$00,$00
	.dcb $08,$00,$00,$00
	.dcb $16,$00,$00,$00
	.dcb $32,$00,$00,$00
	.dcb $64,$00,$00,$00
	.dcb $28,$01,$00,$00
	.dcb $56,$02,$00,$00
	.dcb $12,$05,$00,$00
	.dcb $24,$10,$00,$00
	.dcb $48,$20,$00,$00
	.dcb $96,$40,$00,$00
	.dcb $92,$81,$00,$00
	.dcb $84,$63,$01,$00
	.dcb $68,$27,$03,$00
	.dcb $36,$55,$06,$00
	.dcb $72,$10,$13,$00
	.dcb $44,$21,$26,$00
	.dcb $88,$42,$52,$00
	.dcb $76,$85,$04,$01
	.dcb $52,$71,$09,$02
	.dcb $04,$43,$19,$04



;#######################
; We got the value done#
;#######################
_Got_Number:
 plp 
 plx
 ply
 pla
.endm
