	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
	.dbfunc e formatInt _formatInt fV
;              b -> X+0
;     fieldwidth -> X-8
;            buf -> X-7
;              v -> X-5
_formatInt::
	.dbline -1
	push X
	mov X,SP
	add SP,1
	.dbline 47
; }
; 
; void formatInt(int v, unsigned char *buf, unsigned char fieldwidth)
; {
	.dbline 50
; 	unsigned char b;
; 	
; 	for (b=0;b<fieldwidth;b++)
	mov [X+0],0
	xjmp L23
L20:
	.dbline 51
	mov REG[0xd0],>__r0
	mov A,[X+0]
	mov [__r1],A
	mov [__r0],0
	mov A,[X-6]
	add [__r1],A
	mov A,[X-7]
	adc [__r0],A
	mov A,[__r0]
	mov REG[0xd5],A
	mov A,32
	mvi [__r1],A
L21:
	.dbline 50
	inc [X+0]
L23:
	.dbline 50
	mov A,[X+0]
	cmp A,[X-8]
	jc L20
X10:
	.dbline 53
; 		buf[b]=' ';
; 	
; 	buf[fieldwidth]=0;
	mov REG[0xd0],>__r0
	mov A,[X-8]
	mov [__r1],A
	mov [__r0],0
	mov A,[X-6]
	add [__r1],A
	mov A,[X-7]
	adc [__r0],A
	mov A,[__r0]
	mov REG[0xd5],A
	mov A,0
	mvi [__r1],A
	.dbline 55
; 
; 	if (v<0)
	mov A,[X-4]
	sub A,0
	mov A,[X-5]
	xor A,-128
	sbb A,(0 ^ 0x80)
	jnc L24
X11:
	.dbline 56
; 		{
	.dbline 57
; 		buf[0]='-';
	mov REG[0xd0],>__r0
	mov A,[X-6]
	mov [__r1],A
	mov A,[X-7]
	mov REG[0xd5],A
	mov A,45
	mvi [__r1],A
	.dbline 58
; 		v=-v;
	mov A,[X-4]
	cpl A
	sub A,-1
	mov [X-4],A
	mov A,[X-5]
	cpl A
	sbb A,-1
	mov [X-5],A
	.dbline 59
; 		}
	xjmp L25
L24:
	.dbline 61
; 	else
; 		buf[0]=' ';
	mov REG[0xd0],>__r0
	mov A,[X-6]
	mov [__r1],A
	mov A,[X-7]
	mov REG[0xd5],A
	mov A,32
	mvi [__r1],A
L25:
	.dbline 63
; 	
; 	buf[fieldwidth-1]='0'; // in case the number is zero...
	mov REG[0xd0],>__r0
	mov A,[X-8]
	mov [__r1],A
	mov [__r0],0
	sub [__r1],1
	sbb [__r0],0
	mov A,[X-6]
	add [__r1],A
	mov A,[X-7]
	adc [__r0],A
	mov A,[__r0]
	mov REG[0xd5],A
	mov A,48
	mvi [__r1],A
	xjmp L27
L26:
	.dbline 66
; 	
; 	while (v>0 && fieldwidth>0)
; 		{
	.dbline 67
; 		b=v%10;
	mov REG[0xd0],>__r0
	mov A,0
	push A
	mov A,10
	push A
	mov A,[X-5]
	push A
	mov A,[X-4]
	push A
	xcall __divmod_16X16_16
	add SP,-2
	pop A
	mov [__r1],A
	pop A
	mov A,[__r1]
	mov [X+0],A
	.dbline 68
; 		if (b<0)
	cmp [X+0],0
	jnc L29
X12:
	.dbline 69
; 			b+=10;
	add [X+0],10
L29:
	.dbline 71
	mov REG[0xd0],>__r0
	mov A,[X+0]
	add A,48
	mov [__r0],A
	mov A,[X-8]
	mov [__r3],A
	mov [__r2],0
	sub [__r3],1
	sbb [__r2],0
	mov A,[X-6]
	add [__r3],A
	mov A,[X-7]
	adc [__r2],A
	mov A,[__r2]
	mov REG[0xd5],A
	mov A,[__r0]
	mvi [__r3],A
	.dbline 72
	mov A,0
	push A
	mov A,10
	push A
	mov A,[X-5]
	push A
	mov A,[X-4]
	push A
	xcall __divmod_16X16_16
	pop A
	mov [X-4],A
	pop A
	mov [X-5],A
	add SP,-2
	.dbline 73
	dec [X-8]
	.dbline 74
L27:
	.dbline 65
	mov A,0
	sub A,[X-4]
	mov A,[X-5]
	xor A,-128
	mov REG[0xd0],>__r0
	mov [__rX],A
	mov A,(0 ^ 0x80)
	sbb A,[__rX]
	jnc L31
X13:
	mov A,0
	cmp A,[X-8]
	jc L26
X14:
L31:
	.dbline -2
	.dbline 75
; 		
; 		buf[fieldwidth-1]='0'+b;
; 		v=v/10;
; 		fieldwidth--;
; 		}
; }
L19:
	add SP,-1
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l b 0 c
	.dbsym l fieldwidth -8 c
	.dbsym l buf -7 pc
	.dbsym l v -5 I
	.dbend
