	cpu LMM
	.module calib.c
	.area text(rom, con, rel)
	.dbfile ./calib.c
	.dbfunc e calib_idle _calib_idle fV
_calib_idle::
	.dbline -1
	.dbline 30
; #include "config.h"
; #include "util.h"
; #include "glcd.h"
; #include "fnt58.h"
; #include <stdlib.h>
; #include <string.h>
; 
; #define STICKBUTTON 1
; #define MENUBUTTON 2
; #define STOPBUTTON 4
; 
; extern void UpdateAnalog(void);
; extern void UpdateButtons(void);
; extern void DrawBatteryVoltage(unsigned char x, unsigned char y);
; 
; extern int joyx, joyy;
; extern params_t params;
; extern unsigned char buttons;
; extern long 		  batteryUpdateCount;
; 
; unsigned char calibrate_joy(void);
; unsigned char calibrate_battery(void);
; void waitbuttonsrelease(void);
; void calib_lcd_clear(void);
; 
; 
; params_t  cal_params;
; 
; void calib_idle()
; {
	.dbline 31
; 	UpdateButtons();
	xcall _UpdateButtons
	.dbline 32
; 	UpdateAnalog();
	xcall _UpdateAnalog
	.dbline -2
	.dbline 33
; }
L2:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e getbuttons _getbuttons fc
;              b -> X+0
_getbuttons::
	.dbline -1
	push X
	mov X,SP
	add SP,1
	.dbline 36
; 
; unsigned char getbuttons()
; {
	.dbline 37
; 	unsigned char b=0;
	mov [X+0],0
	xjmp L5
L4:
	.dbline 40
	.dbline 41
	mov REG[0xd0],>_buttons
	mov A,[X+0]
	or A,[_buttons]
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 42
	xcall _calib_idle
	.dbline 43
L5:
	.dbline 39
; 	
; 	while (b==0)
	cmp [X+0],0
	jz L4
	xjmp L8
L7:
	.dbline 46
	.dbline 47
	mov REG[0xd0],>_buttons
	mov A,[X+0]
	or A,[_buttons]
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 48
	xcall _calib_idle
	.dbline 49
L8:
	.dbline 45
; 	{
; 		b|=buttons;
; 		calib_idle();
; 	}
; 		
; 	while (buttons!=0)
	mov REG[0xd0],>_buttons
	cmp [_buttons],0
	jnz L7
	.dbline 51
; 		{
; 		b|=buttons;
; 		calib_idle();
; 		}
; 		
; 	return b;
	mov A,[X+0]
	mov REG[0xd0],>__r0
	.dbline -2
L3:
	add SP,-1
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l b 0 c
	.dbend
	.dbfunc e waitbuttonsrelease _waitbuttonsrelease fV
_waitbuttonsrelease::
	.dbline -1
	.dbline 55
; }
; 
; void waitbuttonsrelease()
; {
	xjmp L12
L11:
	.dbline 57
	.dbline 58
	xcall _calib_idle
	.dbline 59
L12:
	.dbline 56
; 	while (buttons!=0)
	mov REG[0xd0],>_buttons
	cmp [_buttons],0
	jnz L11
	.dbline -2
	.dbline 60
; 		{
; 		calib_idle();
; 		}
; }
L10:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e pollbuttons _pollbuttons fc
_pollbuttons::
	.dbline -1
	.dbline 63
; 
; unsigned char pollbuttons()
; {
	.dbline 64
; 	calib_idle();
	xcall _calib_idle
	.dbline 65
; 	return buttons;
	mov REG[0xd0],>_buttons
	mov A,[_buttons]
	mov REG[0xd0],>__r0
	.dbline -2
L14:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e calibrate _calibrate fV
;              b -> X+0
_calibrate::
	.dbline -1
	push X
	mov X,SP
	add SP,1
	.dbline 69
; }
; 	
; void calibrate()
; {
	.dbline 72
; 	unsigned char b;
; 	
; 	cal_params=params; // make backup of parameters.
	mov REG[0xd0],>__r0
	mov [__r1],<_params
	mov [__r0],>_params
	mov [__r3],<_cal_params
	mov [__r2],>_cal_params
	mov A,[__r0]
	mov REG[0xd4],A
	mov A,[__r2]
	mov REG[0xd5],A
	push X
	mov X,14
X0:
	mov REG[0xd0],>__r0
	mvi A,[__r1]
	mvi [__r3],A
	dec X
	jnz X0
	pop X
	.dbline 74
; 
; 	if (!calibrate_joy())
	xcall _calibrate_joy
	mov REG[0xd0],>__r0
	cmp A,0
	jnz L16
	.dbline 75
; 		goto abort;
	xjmp L18
L16:
	.dbline 76
; 	waitbuttonsrelease();
	xcall _waitbuttonsrelease
	.dbline 78
; 	
; 	if (!calibrate_battery())
	xcall _calibrate_battery
	mov REG[0xd0],>__r0
	cmp A,0
	jnz L19
	.dbline 79
; 		goto abort;
	xjmp L18
L19:
	.dbline 80
; 	waitbuttonsrelease();
	xcall _waitbuttonsrelease
	.dbline 83
; 		
; 	
; 	calib_lcd_clear();
	xcall _calib_lcd_clear
	.dbline 85
; 
; 	GLCD_DrawStringC(0,16,"Configuration complete.", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L21
	push A
	mov A,<L21
	push A
	mov A,16
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	add SP,-6
	mov REG[0xd0],>__r0
	.dbline 86
; 	GLCD_DrawStringC(0,24,"hit Menu to save, or", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L22
	push A
	mov A,<L22
	push A
	mov A,24
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	mov REG[0xd0],>__r0
	.dbline 87
; 	GLCD_DrawStringC(0,32,"Stop to cancel", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L23
	push A
	mov A,<L23
	push A
	mov A,32
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	add SP,-12
	mov REG[0xd0],>__r0
	.dbline 89
; 
; 	b=getbuttons();
	xcall _getbuttons
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 90
; 	if (b&STOPBUTTON)
	tst [X+0],4
	jz L24
	.dbline 91
; 		goto abort;
	xjmp L18
L24:
	.dbline 93
; 			
; 	calib_lcd_clear();
	xcall _calib_lcd_clear
	.dbline 96
; 
; 
; 	calib_lcd_clear();
	xcall _calib_lcd_clear
	.dbline 97
; 	GLCD_DrawStringC(0,16, "Writing configuration...", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L26
	push A
	mov A,<L26
	push A
	mov A,16
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	add SP,-6
	mov REG[0xd0],>__r0
	.dbline 99
; 
; 	if (savePrefs())
	xcall _savePrefs
	mov REG[0xd0],>__r0
	cmp A,0
	jz L27
	.dbline 100
; 		GLCD_DrawStringC(0,24, "... success!", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L29
	push A
	mov A,<L29
	push A
	mov A,24
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	add SP,-6
	mov REG[0xd0],>__r0
	xjmp L28
L27:
	.dbline 102
; 	else
; 		GLCD_DrawStringC(0,24, "... failure!", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L30
	push A
	mov A,<L30
	push A
	mov A,24
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	add SP,-6
	mov REG[0xd0],>__r0
L28:
	.dbline 104
; 	
; 	GLCD_DrawStringC(68,56, "[press menu]", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L31
	push A
	mov A,<L31
	push A
	mov A,56
	push A
	mov A,68
	push A
	xcall _GLCD_DrawStringC
	add SP,-6
	mov REG[0xd0],>__r0
	.dbline 105
; 	getbuttons();
	xcall _getbuttons
	mov REG[0xd0],>__r0
	.dbline 107
; 
; 	GLCD_Clear();
	xcall _GLCD_Clear
	.dbline 108
; 	batteryUpdateCount=0;
	mov REG[0xd0],>_batteryUpdateCount
	mov [_batteryUpdateCount],0
	mov [_batteryUpdateCount+1],0
	mov [_batteryUpdateCount+2],0
	mov [_batteryUpdateCount+3],0
	.dbline 110
; 
; 	return;
	xjmp L15
L18:
	.dbline 113
; 	
; abort:
; 	params=cal_params;
	mov REG[0xd0],>__r0
	mov [__r1],<_cal_params
	mov [__r0],>_cal_params
	mov [__r3],<_params
	mov [__r2],>_params
	mov A,[__r0]
	mov REG[0xd4],A
	mov A,[__r2]
	mov REG[0xd5],A
	push X
	mov X,14
X1:
	mov REG[0xd0],>__r0
	mvi A,[__r1]
	mvi [__r3],A
	dec X
	jnz X1
	pop X
	.dbline 114
; 	GLCD_Clear();
	xcall _GLCD_Clear
	.dbline 115
; 	batteryUpdateCount=0;
	mov REG[0xd0],>_batteryUpdateCount
	mov [_batteryUpdateCount],0
	mov [_batteryUpdateCount+1],0
	mov [_batteryUpdateCount+2],0
	mov [_batteryUpdateCount+3],0
	.dbline 116
; 	return;
	.dbline -2
L15:
	add SP,-1
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l b 0 c
	.dbend
	.dbfunc e calib_lcd_clear _calib_lcd_clear fV
_calib_lcd_clear::
	.dbline -1
	.dbline 120
; }
; 
; void calib_lcd_clear()
; {
	.dbline 121
; 	GLCD_Clear();
	xcall _GLCD_Clear
	.dbline 122
; 	GLCD_DrawCharXor=255;
	mov REG[0xd0],>_GLCD_DrawCharXor
	mov [_GLCD_DrawCharXor],-1
	.dbline 124
; 	//                     1234567890123456789012345
; 	GLCD_DrawStringC(0, 0," OrcPad Calibration      ", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L33
	push A
	mov A,<L33
	push A
	mov A,0
	push A
	push A
	xcall _GLCD_DrawStringC
	add SP,-6
	.dbline 125
; 	GLCD_DrawCharXor=0;
	mov REG[0xd0],>_GLCD_DrawCharXor
	mov [_GLCD_DrawCharXor],0
	.dbline -2
	.dbline 127
; 
; }
L32:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e calibrate_joy _calibrate_joy fc
;              b -> X+7
;              z -> X+6
;              s -> X+0
_calibrate_joy::
	.dbline -1
	push X
	mov X,SP
	add SP,8
	.dbline 130
; 
; unsigned char calibrate_joy()
; {
	.dbline 135
; 	unsigned char b;
; 	unsigned char s[6];
; 	unsigned char z;
; 	
; 	calib_lcd_clear();
	xcall _calib_lcd_clear
	.dbline 138
; 	
; 	//                     1234567890123456789012345
; 	GLCD_DrawStringC(0,16,"Move through full range,", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L35
	push A
	mov A,<L35
	push A
	mov A,16
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	add SP,-6
	mov REG[0xd0],>__r0
	.dbline 139
; 	GLCD_DrawStringC(0,24,"center knob, hit 'menu'.", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L36
	push A
	mov A,<L36
	push A
	mov A,24
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	mov REG[0xd0],>__r0
	.dbline 141
; 			
; 	GLCD_DrawStringC(20, 40, "cur", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L37
	push A
	mov A,<L37
	push A
	mov A,40
	push A
	mov A,20
	push A
	xcall _GLCD_DrawStringC
	add SP,-12
	mov REG[0xd0],>__r0
	.dbline 142
; 	GLCD_DrawStringC(60, 40, "min", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L38
	push A
	mov A,<L38
	push A
	mov A,40
	push A
	mov A,60
	push A
	xcall _GLCD_DrawStringC
	mov REG[0xd0],>__r0
	.dbline 143
; 	GLCD_DrawStringC(100, 40, "max", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L39
	push A
	mov A,<L39
	push A
	mov A,40
	push A
	mov A,100
	push A
	xcall _GLCD_DrawStringC
	add SP,-12
	.dbline 145
; 
; 	params.joyymin=9999;
	mov REG[0xd0],>_params
	mov [_params+8+1],15
	mov [_params+8],39
	.dbline 146
; 	params.joyymax=0;
	mov [_params+10+1],0
	mov [_params+10],0
	.dbline 147
; 	params.joyxmin=9999;
	mov [_params+4+1],15
	mov [_params+4],39
	.dbline 148
; 	params.joyxmax=0;
	mov [_params+6+1],0
	mov [_params+6],0
	.dbline 150
; 	
; 	waitbuttonsrelease();
	xcall _waitbuttonsrelease
	xjmp L45
L44:
	.dbline 153
; 	
; 	while(1)
; 		{
	.dbline 154
; 		if (joyx>params.joyxmax)
	mov REG[0xd0],>_params
	mov A,[_params+6+1]
	mov REG[0xd0],>_joyx
	sub A,[_joyx+1]
	mov A,[_joyx]
	xor A,-128
	mov REG[0xd0],>__r0
	mov [__rX],A
	mov REG[0xd0],>_params
	mov A,[_params+6]
	xor A,-128
	mov REG[0xd0],>__r0
	sbb A,[__rX]
	jnc L47
X3:
	.dbline 155
; 			params.joyxmax=joyx;
	mov REG[0xd0],>_joyx
	mov A,[_joyx+1]
	push A
	mov A,[_joyx]
	mov REG[0xd0],>_params
	mov [_params+6],A
	pop A
	mov [_params+6+1],A
L47:
	.dbline 156
; 		if (joyx<params.joyxmin)
	mov REG[0xd0],>_joyx
	mov A,[_joyx+1]
	mov REG[0xd0],>_params
	sub A,[_params+4+1]
	mov A,[_params+4]
	xor A,-128
	mov REG[0xd0],>__r0
	mov [__rX],A
	mov REG[0xd0],>_joyx
	mov A,[_joyx]
	xor A,-128
	mov REG[0xd0],>__r0
	sbb A,[__rX]
	jnc L51
X4:
	.dbline 157
; 			params.joyxmin=joyx;
	mov REG[0xd0],>_joyx
	mov A,[_joyx+1]
	push A
	mov A,[_joyx]
	mov REG[0xd0],>_params
	mov [_params+4],A
	pop A
	mov [_params+4+1],A
L51:
	.dbline 159
; 			
; 		if (joyy>params.joyymax)
	mov REG[0xd0],>_params
	mov A,[_params+10+1]
	mov REG[0xd0],>_joyy
	sub A,[_joyy+1]
	mov A,[_joyy]
	xor A,-128
	mov REG[0xd0],>__r0
	mov [__rX],A
	mov REG[0xd0],>_params
	mov A,[_params+10]
	xor A,-128
	mov REG[0xd0],>__r0
	sbb A,[__rX]
	jnc L55
X5:
	.dbline 160
; 			params.joyymax=joyy;
	mov REG[0xd0],>_joyy
	mov A,[_joyy+1]
	push A
	mov A,[_joyy]
	mov REG[0xd0],>_params
	mov [_params+10],A
	pop A
	mov [_params+10+1],A
L55:
	.dbline 161
; 		if (joyy<params.joyymin)
	mov REG[0xd0],>_joyy
	mov A,[_joyy+1]
	mov REG[0xd0],>_params
	sub A,[_params+8+1]
	mov A,[_params+8]
	xor A,-128
	mov REG[0xd0],>__r0
	mov [__rX],A
	mov REG[0xd0],>_joyy
	mov A,[_joyy]
	xor A,-128
	mov REG[0xd0],>__r0
	sbb A,[__rX]
	jnc L59
X6:
	.dbline 162
; 			params.joyymin=joyy;
	mov REG[0xd0],>_joyy
	mov A,[_joyy+1]
	push A
	mov A,[_joyy]
	mov REG[0xd0],>_params
	mov [_params+8],A
	pop A
	mov [_params+8+1],A
L59:
	.dbline 164
; 		
; 		GLCD_DrawStringC(0, 48, "X:", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L63
	push A
	mov A,<L63
	push A
	mov A,48
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	mov REG[0xd0],>__r0
	.dbline 165
; 		GLCD_DrawStringC(0, 56, "Y:", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L64
	push A
	mov A,<L64
	push A
	mov A,56
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	add SP,-12
	.dbline 167
; 		
; 		itoa(s, joyx, 10);
	mov A,0
	push A
	mov A,10
	push A
	mov REG[0xd0],>_joyx
	mov A,[_joyx]
	push A
	mov A,[_joyx+1]
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	xcall _itoa
	.dbline 168
; 		z=GLCD_DrawString(20, 48, s, fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	mov A,48
	push A
	mov A,20
	push A
	xcall _GLCD_DrawString
	add SP,-12
	mov REG[0xd0],>__r0
	mov [X+6],A
	.dbline 169
; 		GLCD_DrawStringC(20+z, 48, "   ", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L65
	push A
	mov A,<L65
	push A
	mov A,48
	push A
	mov A,[X+6]
	add A,20
	push A
	xcall _GLCD_DrawStringC
	.dbline 171
; 			
; 		itoa(s, params.joyxmin, 10);
	mov A,0
	push A
	mov A,10
	push A
	mov REG[0xd0],>_params
	mov A,[_params+4]
	push A
	mov A,[_params+4+1]
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	xcall _itoa
	add SP,-12
	.dbline 172
; 		z=GLCD_DrawString(60, 48, s, fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	mov A,48
	push A
	mov A,60
	push A
	xcall _GLCD_DrawString
	mov REG[0xd0],>__r0
	mov [X+6],A
	.dbline 173
; 		GLCD_DrawStringC(60+z, 48, "   ", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L65
	push A
	mov A,<L65
	push A
	mov A,48
	push A
	mov A,[X+6]
	add A,60
	push A
	xcall _GLCD_DrawStringC
	add SP,-12
	.dbline 175
; 			
; 		itoa(s, params.joyxmax, 10);
	mov A,0
	push A
	mov A,10
	push A
	mov REG[0xd0],>_params
	mov A,[_params+6]
	push A
	mov A,[_params+6+1]
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	xcall _itoa
	.dbline 176
; 		z=GLCD_DrawString(100, 48, s, fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	mov A,48
	push A
	mov A,100
	push A
	xcall _GLCD_DrawString
	add SP,-12
	mov REG[0xd0],>__r0
	mov [X+6],A
	.dbline 177
; 		GLCD_DrawStringC(100+z, 48, "   ", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L65
	push A
	mov A,<L65
	push A
	mov A,48
	push A
	mov A,[X+6]
	add A,100
	push A
	xcall _GLCD_DrawStringC
	.dbline 179
; 		
; 		itoa(s, joyy, 10);
	mov A,0
	push A
	mov A,10
	push A
	mov REG[0xd0],>_joyy
	mov A,[_joyy]
	push A
	mov A,[_joyy+1]
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	xcall _itoa
	add SP,-12
	.dbline 180
; 		z=GLCD_DrawString(20, 56, s, fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	mov A,56
	push A
	mov A,20
	push A
	xcall _GLCD_DrawString
	mov REG[0xd0],>__r0
	mov [X+6],A
	.dbline 181
; 		GLCD_DrawStringC(20+z, 56, "   ", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L65
	push A
	mov A,<L65
	push A
	mov A,56
	push A
	mov A,[X+6]
	add A,20
	push A
	xcall _GLCD_DrawStringC
	add SP,-12
	.dbline 183
; 		
; 		itoa(s, params.joyymin, 10);
	mov A,0
	push A
	mov A,10
	push A
	mov REG[0xd0],>_params
	mov A,[_params+8]
	push A
	mov A,[_params+8+1]
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	xcall _itoa
	.dbline 184
; 		z=GLCD_DrawString(60, 56, s, fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	mov A,56
	push A
	mov A,60
	push A
	xcall _GLCD_DrawString
	add SP,-12
	mov REG[0xd0],>__r0
	mov [X+6],A
	.dbline 185
; 		GLCD_DrawStringC(60+z, 56, "   ", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L65
	push A
	mov A,<L65
	push A
	mov A,56
	push A
	mov A,[X+6]
	add A,60
	push A
	xcall _GLCD_DrawStringC
	.dbline 187
; 			
; 		itoa(s, params.joyymax, 10);
	mov A,0
	push A
	mov A,10
	push A
	mov REG[0xd0],>_params
	mov A,[_params+10]
	push A
	mov A,[_params+10+1]
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	xcall _itoa
	add SP,-12
	.dbline 188
; 		z=GLCD_DrawString(100, 56, s, fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov REG[0xd0],>__r0
	mov A,7
	push A
	push X
	mov A,56
	push A
	mov A,100
	push A
	xcall _GLCD_DrawString
	mov REG[0xd0],>__r0
	mov [X+6],A
	.dbline 189
; 		GLCD_DrawStringC(100+z, 56, "   ", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L65
	push A
	mov A,<L65
	push A
	mov A,56
	push A
	mov A,[X+6]
	add A,100
	push A
	xcall _GLCD_DrawStringC
	add SP,-12
	mov REG[0xd0],>__r0
	.dbline 191
; 		
; 		b=pollbuttons();
	xcall _pollbuttons
	mov REG[0xd0],>__r0
	mov [X+7],A
	.dbline 192
; 		if (b&STOPBUTTON)
	tst [X+7],4
	jz L70
	.dbline 193
; 			return 0;
	mov A,0
	xjmp L34
L70:
	.dbline 195
; 			
; 		if (b&MENUBUTTON)
	tst [X+7],2
	jz L72
	.dbline 196
; 		{
	.dbline 197
; 			params.joycy=joyy;
	mov REG[0xd0],>_joyy
	mov A,[_joyy+1]
	push A
	mov A,[_joyy]
	mov REG[0xd0],>_params
	mov [_params+2],A
	pop A
	mov [_params+2+1],A
	.dbline 198
; 			params.joycx=joyx;
	mov REG[0xd0],>_joyx
	mov A,[_joyx+1]
	push A
	mov A,[_joyx]
	mov REG[0xd0],>_params
	mov [_params],A
	pop A
	mov [_params+1],A
	.dbline 199
; 			return 1;
	mov REG[0xd0],>__r0
	mov A,1
	xjmp L34
L72:
	.dbline 201
L45:
	.dbline 152
	xjmp L44
X2:
	.dbline 203
; 		}
; 	}
; 		
; 	return 1;
	mov REG[0xd0],>__r0
	mov A,1
	.dbline -2
L34:
	add SP,-8
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l b 7 c
	.dbsym l z 6 c
	.dbsym l s 0 A[6:6]c
	.dbend
	.dbfunc e calibrate_battery _calibrate_battery fc
;              z -> X+11
;              s -> X+5
;              b -> X+4
;   joyMoveState -> X+0
_calibrate_battery::
	.dbline -1
	push X
	mov X,SP
	add SP,12
	.dbline 207
; }
; 
; unsigned char calibrate_battery(void)
; {
	.dbline 213
; 	unsigned char s[6];
; 	unsigned char b;
; 	long          joyMoveState;
; 	unsigned char z;
; 	
; 	calib_lcd_clear();
	xcall _calib_lcd_clear
	.dbline 216
; 
; 	//                        1234567890123456789012345
; 	GLCD_DrawStringC(0,16,"Use joystick to set,", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L76
	push A
	mov A,<L76
	push A
	mov A,16
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	add SP,-6
	mov REG[0xd0],>__r0
	.dbline 217
; 	GLCD_DrawStringC(0,24,"voltage below to match", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L77
	push A
	mov A,<L77
	push A
	mov A,24
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	mov REG[0xd0],>__r0
	.dbline 218
; 	GLCD_DrawStringC(0,32,"the true supply voltage.", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L78
	push A
	mov A,<L78
	push A
	mov A,32
	push A
	mov A,0
	push A
	xcall _GLCD_DrawStringC
	add SP,-12
	mov REG[0xd0],>__r0
	.dbline 220
; 			
; 	joyMoveState=0;
	mov [X+0],0
	mov [X+1],0
	mov [X+2],0
	mov [X+3],0
	xjmp L80
L79:
	.dbline 223
; 	
; 	while(1)
; 	{		
	.dbline 224
; 		b=pollbuttons();
	xcall _pollbuttons
	mov REG[0xd0],>__r0
	mov [X+4],A
	.dbline 225
; 		if (b&STOPBUTTON)
	tst [X+4],4
	jz L82
	.dbline 226
; 			return 0;
	mov A,0
	xjmp L75
L82:
	.dbline 227
; 		if (b&MENUBUTTON)
	tst [X+4],2
	jz L84
	.dbline 228
; 			return 1;
	mov REG[0xd0],>__r0
	mov A,1
	xjmp L75
L84:
	.dbline 230
; 
; 		DrawBatteryVoltage(40,56);
	mov A,56
	push A
	mov A,40
	push A
	xcall _DrawBatteryVoltage
	.dbline 232
; 		
; 		itoa(s, params.batsensedivide, 10);
	mov A,0
	push A
	mov A,10
	push A
	mov REG[0xd0],>_params
	mov A,[_params+12]
	push A
	mov A,[_params+12+1]
	push A
	mov REG[0xd0],>__r0
	mov [__r1],X
	add [__r1],5
	mov A,7
	push A
	mov A,[__r1]
	push A
	xcall _itoa
	add SP,-8
	.dbline 233
; 		z=GLCD_DrawString(100, 56, s, fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov REG[0xd0],>__r0
	mov [__r1],X
	add [__r1],5
	mov A,7
	push A
	mov A,[__r1]
	push A
	mov A,56
	push A
	mov A,100
	push A
	xcall _GLCD_DrawString
	mov REG[0xd0],>__r0
	mov [X+11],A
	.dbline 234
; 		GLCD_DrawStringC(100+z, 56, "   ", fnt58);
	mov A,>_fnt58
	push A
	mov A,<_fnt58
	push A
	mov A,>L65
	push A
	mov A,<L65
	push A
	mov A,56
	push A
	mov A,[X+11]
	add A,100
	push A
	xcall _GLCD_DrawStringC
	add SP,-12
	.dbline 236
; 
; 		if (joyy>(params.joycy-30) && joyy<(params.joycy+30))
	mov REG[0xd0],>_params
	mov A,[_params+2+1]
	sub A,30
	mov REG[0xd0],>__r0
	mov [__r1],A
	mov REG[0xd0],>_params
	mov A,[_params+2]
	sbb A,0
	mov REG[0xd0],>__r0
	mov [__r0],A
	mov A,[__r1]
	mov REG[0xd0],>_joyy
	sub A,[_joyy+1]
	mov A,[_joyy]
	xor A,-128
	mov REG[0xd0],>__r0
	mov [__rX],A
	mov A,[__r0]
	xor A,-128
	sbb A,[__rX]
	jnc L87
X8:
	mov REG[0xd0],>_params
	mov A,[_params+2+1]
	add A,30
	mov REG[0xd0],>__r0
	mov [__r1],A
	mov REG[0xd0],>_params
	mov A,[_params+2]
	adc A,0
	mov REG[0xd0],>__r0
	mov [__r0],A
	mov REG[0xd0],>_joyy
	mov A,[_joyy+1]
	mov REG[0xd0],>__r0
	sub A,[__r1]
	mov A,[__r0]
	xor A,-128
	mov [__rX],A
	mov REG[0xd0],>_joyy
	mov A,[_joyy]
	xor A,-128
	mov REG[0xd0],>__r0
	sbb A,[__rX]
	jnc L87
X9:
	.dbline 237
; 			continue;
	xjmp L80
L87:
	.dbline 239
; 		
; 		joyMoveState+=(joyy-params.joycy)/32*(joyy-params.joycy)/32*sgn(joyy-params.joycy);
	mov REG[0xd0],>_joyy
	mov A,[_joyy+1]
	mov REG[0xd0],>_params
	sub A,[_params+2+1]
	mov REG[0xd0],>__r0
	mov [__r1],A
	mov REG[0xd0],>_joyy
	mov A,[_joyy]
	mov REG[0xd0],>_params
	sbb A,[_params+2]
	mov REG[0xd0],>__r0
	push A
	mov A,[__r1]
	push A
	xcall _sgn
	add SP,-2
	mov REG[0xd0],>_joyy
	mov A,[_joyy+1]
	mov REG[0xd0],>_params
	sub A,[_params+2+1]
	mov REG[0xd0],>__r0
	mov [__r3],A
	mov REG[0xd0],>_joyy
	mov A,[_joyy]
	mov REG[0xd0],>_params
	sbb A,[_params+2]
	mov REG[0xd0],>__r0
	mov [__r2],A
	mov REG[0xd0],>_joyy
	mov A,[_joyy+1]
	mov REG[0xd0],>_params
	sub A,[_params+2+1]
	mov REG[0xd0],>__r0
	mov [__r5],A
	mov REG[0xd0],>_joyy
	mov A,[_joyy]
	mov REG[0xd0],>_params
	sbb A,[_params+2]
	mov REG[0xd0],>__r0
	mov [__r4],A
	mov A,0
	push A
	mov A,32
	push A
	mov A,[__r4]
	push A
	mov A,[__r5]
	push A
	xcall __divmod_16X16_16
	pop A
	mov [__r5],A
	pop A
	mov [__r4],A
	add SP,-2
	mov [__rX],0
	mov [__rY],0
X10:
	cmp [__r4],0
	jnz X13
	cmp [__r5],0
	jz X12
X13:
	and F,-5
	rrc [__r4]
	rrc [__r5]
	jnc X11
	mov REG[0xd0],>__r0
	mov A,[__r3]
	add [__rX],A
	mov A,[__r2]
	adc [__rY],A
X11:
	asl [__r3]
	rlc [__r2]
	jmp X10
X12:
	mov [__r3],[__rX]
	mov [__r2],[__rY]
	mov REG[0xd0],>__r0
	mov A,0
	push A
	mov A,32
	push A
	mov A,[__r2]
	push A
	mov A,[__r3]
	push A
	xcall __divmod_16X16_16
	pop A
	mov [__r3],A
	pop A
	mov [__r2],A
	add SP,-2
	mov [__rX],0
	mov [__rY],0
X14:
	cmp [__r2],0
	jnz X17
	cmp [__r3],0
	jz X16
X17:
	and F,-5
	rrc [__r2]
	rrc [__r3]
	jnc X15
	mov REG[0xd0],>__r0
	mov A,[__r1]
	add [__rX],A
	mov A,[__r0]
	adc [__rY],A
X15:
	asl [__r1]
	rlc [__r0]
	jmp X14
X16:
	mov [__r1],[__rX]
	mov [__r0],[__rY]
	mov REG[0xd0],>__r0
	mov A,[__r1]
	mov [__r3],A
	mov A,[__r0]
	mov [__r2],A
	tst [__r2],-128
	jz X18
	mov [__r1],-1
	mov [__r0],-1
	jmp X19
X18:
	mov REG[0xd0],>__r0
	mov [__r1],0
	mov [__r0],0
X19:
	mov REG[0xd0],>__r0
	mov A,[__r3]
	add [X+3],A
	mov A,[__r2]
	adc [X+2],A
	mov A,[__r1]
	adc [X+1],A
	mov A,[__r0]
	adc [X+0],A
	xjmp L95
L94:
	.dbline 242
	.dbline 243
	mov REG[0xd0],>_params
	inc [_params+12+1]
	adc [_params+12],0
	.dbline 244
	sub [X+3],-48
	sbb [X+2],7
	sbb [X+1],0
	sbb [X+0],0
	.dbline 245
L95:
	.dbline 241
; 		
; 		while (joyMoveState>2000)
	mov A,-48
	sub A,[X+3]
	mov A,7
	sbb A,[X+2]
	mov A,0
	sbb A,[X+1]
	mov A,[X+0]
	xor A,-128
	mov REG[0xd0],>__r0
	mov [__rX],A
	mov A,(0 ^ 0x80)
	sbb A,[__rX]
	jc L94
X20:
	.dbline 246
; 			{
; 			params.batsensedivide++;
; 			joyMoveState-=2000;
; 			}
; 		if (joyMoveState<-2000)
	mov A,[X+3]
	sub A,48
	mov A,[X+2]
	sbb A,-8
	mov A,[X+1]
	sbb A,-1
	mov A,[X+0]
	xor A,-128
	sbb A,(255 ^ 0x80)
	jnc L98
X21:
	.dbline 247
; 			{
	.dbline 248
; 			params.batsensedivide--;
	mov REG[0xd0],>_params
	dec [_params+12+1]
	sbb [_params+12],0
	.dbline 249
; 			joyMoveState+=2000;
	add [X+3],-48
	adc [X+2],7
	adc [X+1],0
	adc [X+0],0
	.dbline 250
; 			}			
L98:
	.dbline 251
L80:
	.dbline 222
	xjmp L79
X7:
	.dbline 253
; 	}
; 	
; 	return 1;
	mov REG[0xd0],>__r0
	mov A,1
	.dbline -2
L75:
	add SP,-12
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l z 11 c
	.dbsym l s 5 A[6:6]c
	.dbsym l b 4 c
	.dbsym l joyMoveState 0 L
	.dbend
	.area data(ram, con, rel)
	.dbfile ./calib.c
_cal_params::
	.word 0,0,0,0,0
	.byte 0,0,0,0
	.dbsym e cal_params _cal_params X
	.area lit(rom, con, rel)
L78:
	.byte 't,'h,'e,32,'t,'r,'u,'e,32,'s,'u,'p,'p,'l,'y,32
	.byte 'v,'o,'l,'t,'a,'g,'e,46,0
L77:
	.byte 'v,'o,'l,'t,'a,'g,'e,32,'b,'e,'l,'o,'w,32,'t,'o
	.byte 32,'m,'a,'t,'c,'h,0
L76:
	.byte 'U,'s,'e,32,'j,'o,'y,'s,'t,'i,'c,'k,32,'t,'o,32
	.byte 's,'e,'t,44,0
L65:
	.byte 32,32,32,0
L64:
	.byte 'Y,58,0
L63:
	.byte 'X,58,0
L39:
	.byte 'm,'a,'x,0
L38:
	.byte 'm,'i,'n,0
L37:
	.byte 'c,'u,'r,0
L36:
	.byte 'c,'e,'n,'t,'e,'r,32,'k,'n,'o,'b,44,32,'h,'i,'t
	.byte 32,39,'m,'e,'n,'u,39,46,0
L35:
	.byte 'M,'o,'v,'e,32,'t,'h,'r,'o,'u,'g,'h,32,'f,'u,'l
	.byte 'l,32,'r,'a,'n,'g,'e,44,0
L33:
	.byte 32,'O,'r,'c,'P,'a,'d,32,'C,'a,'l,'i,'b,'r,'a,'t
	.byte 'i,'o,'n,32,32,32,32,32,32,0
L31:
	.byte 91,'p,'r,'e,'s,'s,32,'m,'e,'n,'u,93,0
L30:
	.byte 46,46,46,32,'f,'a,'i,'l,'u,'r,'e,33,0
L29:
	.byte 46,46,46,32,'s,'u,'c,'c,'e,'s,'s,33,0
L26:
	.byte 'W,'r,'i,'t,'i,'n,'g,32,'c,'o,'n,'f,'i,'g,'u,'r
	.byte 'a,'t,'i,'o,'n,46,46,46,0
L23:
	.byte 'S,'t,'o,'p,32,'t,'o,32,'c,'a,'n,'c,'e,'l,0
L22:
	.byte 'h,'i,'t,32,'M,'e,'n,'u,32,'t,'o,32,'s,'a,'v,'e
	.byte 44,32,'o,'r,0
L21:
	.byte 'C,'o,'n,'f,'i,'g,'u,'r,'a,'t,'i,'o,'n,32,'c,'o
	.byte 'm,'p,'l,'e,'t,'e,46,0
