	cpu LMM
	.module util.c
	.area text(rom, con, rel)
	.dbfile ./util.c
	.dbfunc e charToInt _charToInt fS
;              c -> X-4
_charToInt::
	.dbline -1
	push X
	mov X,SP
	.dbline 4
; #include "util.h"
; 
; short charToInt(char c)
; {
	.dbline 5
; 	if (c>='A' && c<='F')
	cmp [X-4],65
	jc L2
X0:
	mov A,70
	cmp A,[X-4]
	jc L2
X1:
	.dbline 6
; 		return c-'A'+10;
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	mov [__r0],0
	sub [__r1],65
	sbb [__r0],0
	add [__r1],10
	adc [__r0],0
	xjmp L1
L2:
	.dbline 7
; 	if (c>='a' && c<='f')
	cmp [X-4],97
	jc L4
X2:
	mov A,102
	cmp A,[X-4]
	jc L4
X3:
	.dbline 8
; 		return c-'a'+10;
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	mov [__r0],0
	sub [__r1],97
	sbb [__r0],0
	add [__r1],10
	adc [__r0],0
	xjmp L1
L4:
	.dbline 9
; 	if (c>='0' && c<='9')
	cmp [X-4],48
	jc L6
X4:
	mov A,57
	cmp A,[X-4]
	jc L6
X5:
	.dbline 10
; 		return c-'0';
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	mov [__r0],0
	sub [__r1],48
	sbb [__r0],0
	xjmp L1
L6:
	.dbline 11
; 	return -1;
	mov REG[0xd0],>__r0
	mov [__r1],-1
	mov [__r0],-1
	.dbline -2
L1:
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l c -4 c
	.dbend
	.dbfunc e char2ToInt _char2ToInt fI
;              b -> X+2
;              a -> X+0
;            buf -> X-5
_char2ToInt::
	.dbline -1
	push X
	mov X,SP
	add SP,4
	.dbline 15
; }
; 
; int char2ToInt(char *buf)
; {
	.dbline 18
; 	short a,b;
; 	
; 	a=charToInt(buf[0]);
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	mov A,[X-5]
	mov REG[0xd4],A
	mvi A,[__r1]
	push A
	xcall _charToInt
	mov REG[0xd0],>__r0
	mov A,[__r1]
	mov [X+1],A
	mov A,[__r0]
	mov [X+0],A
	.dbline 19
; 	b=charToInt(buf[1]);
	mov A,[X-4]
	add A,1
	mov [__r1],A
	mov A,[X-5]
	adc A,0
	mov REG[0xd4],A
	mvi A,[__r1]
	push A
	xcall _charToInt
	add SP,-2
	mov REG[0xd0],>__r0
	mov A,[__r1]
	mov [X+3],A
	mov A,[__r0]
	mov [X+2],A
	.dbline 21
; 	
; 	if (a<0 || b<0)
	mov A,[X+1]
	sub A,0
	mov A,[X+0]
	xor A,-128
	sbb A,(0 ^ 0x80)
	jc L11
X6:
	mov A,[X+3]
	sub A,0
	mov A,[X+2]
	xor A,-128
	sbb A,(0 ^ 0x80)
	jnc L9
X7:
L11:
	.dbline 22
; 		return -1;
	mov REG[0xd0],>__r0
	mov [__r1],-1
	mov [__r0],-1
	xjmp L8
L9:
	.dbline 24
; 	
; 	return a*16+b;
	mov REG[0xd0],>__r0
	mov A,[X+1]
	mov [__r1],A
	mov A,[X+0]
	mov [__r0],A
	asl [__r1]
	rlc [__r0]
	asl [__r1]
	rlc [__r0]
	asl [__r1]
	rlc [__r0]
	asl [__r1]
	rlc [__r0]
	mov A,[X+3]
	add [__r1],A
	mov A,[X+2]
	adc [__r0],A
	.dbline -2
L8:
	add SP,-4
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l b 2 S
	.dbsym l a 0 S
	.dbsym l buf -5 pc
	.dbend
	.dbfunc e char4ToInt _char4ToInt fI
;            tot -> X+3
;              a -> X+1
;              c -> X+0
;            buf -> X-5
_char4ToInt::
	.dbline -1
	push X
	mov X,SP
	add SP,5
	.dbline 28
; }
; 
; int char4ToInt(char *buf)
; {
	.dbline 33
; 	short a;
; 	int tot;
; 	unsigned char c;
; 	
; 	tot=0;
	mov [X+4],0
	mov [X+3],0
	.dbline 34
; 	for (c=0;c<4;c++)
	mov [X+0],0
	xjmp L16
L13:
	.dbline 35
; 	{
	.dbline 36
; 		a=charToInt(buf[c]);
	mov REG[0xd0],>__r0
	mov A,[X+0]
	mov [__r1],A
	mov [__r0],0
	mov A,[X-4]
	add [__r1],A
	mov A,[X-5]
	adc [__r0],A
	mov A,[__r0]
	mov REG[0xd4],A
	mvi A,[__r1]
	push A
	xcall _charToInt
	add SP,-1
	mov REG[0xd0],>__r0
	mov A,[__r1]
	mov [X+2],A
	mov A,[__r0]
	mov [X+1],A
	.dbline 37
; 		if (a<0)
	mov A,[X+2]
	sub A,0
	mov A,[X+1]
	xor A,-128
	sbb A,(0 ^ 0x80)
	jnc L17
X8:
	.dbline 38
; 			return 0;
	mov REG[0xd0],>__r0
	mov [__r1],0
	mov [__r0],0
	xjmp L12
L17:
	.dbline 40
	mov REG[0xd0],>__r0
	mov A,[X+4]
	mov [__r1],A
	mov A,[X+3]
	mov [__r0],A
	asl [__r1]
	rlc [__r0]
	asl [__r1]
	rlc [__r0]
	asl [__r1]
	rlc [__r0]
	asl [__r1]
	rlc [__r0]
	mov A,[__r1]
	add A,[X+2]
	mov [X+4],A
	mov A,[__r0]
	adc A,[X+1]
	mov [X+3],A
	.dbline 41
L14:
	.dbline 34
	inc [X+0]
L16:
	.dbline 34
	cmp [X+0],4
	jc L13
X9:
	.dbline 43
; 		
; 		tot=tot*16+a;
; 	}
; 	
; 	return tot;
	mov REG[0xd0],>__r0
	mov A,[X+4]
	mov [__r1],A
	mov A,[X+3]
	mov [__r0],A
	.dbline -2
L12:
	add SP,-5
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l tot 3 I
	.dbsym l a 1 S
	.dbsym l c 0 c
	.dbsym l buf -5 pc
	.dbend
