	cpu LMM
	.module slave.c
	.area text(rom, con, rel)
	.dbfile ./slave.c
	.dbfunc e slave_allstop _slave_allstop fV
_slave_allstop::
	.dbline -1
	.dbline 8
; #include "ed_slave_int.h"
; #include "main.h"
; #include "slave.h"
; 
; void slave_waitack(void);
; 
; void slave_allstop(void)
; {
	.dbline 9
; 	slave_packet1('X');
	mov A,88
	push A
	xcall _slave_packet1
	add SP,-1
	.dbline -2
	.dbline 10
; }
L1:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e slave_requeststate _slave_requeststate fV
_slave_requeststate::
	.dbline -1
	.dbline 13
; 
; void slave_requeststate(void)
; {
	.dbline 14
; 	slave_packet1('*');
	mov A,42
	push A
	xcall _slave_packet1
	add SP,-1
	.dbline -2
	.dbline 15
; }
L2:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e slave_chksend _slave_chksend fc
;              c -> X-5
;            chk -> X-4
_slave_chksend::
	.dbline -1
	push X
	mov X,SP
	add SP,2
	.dbline 18
; 
; unsigned char slave_chksend(unsigned char chk, unsigned char c)
; {
	.dbline 19
; 	ED_SLAVE_putData(c);
	push X
	mov A,[X-5]
	xcall _ED_SLAVE_putData
	pop X
	.dbline 20
; 	return (chk<<1)+c+(chk&0x80 ? 1: 0);
	tst [X-4],-128
	jz L5
	mov [X+1],1
	mov [X+0],0
	xjmp L6
L5:
	mov [X+1],0
	mov [X+0],0
L6:
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	asl [__r1]
	mov A,[X-5]
	add [__r1],A
	mov A,[X+1]
	add [__r1],A
	mov A,[__r1]
	.dbline -2
L3:
	add SP,-2
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l c -5 c
	.dbsym l chk -4 c
	.dbend
	.dbfunc e slave_packet1 _slave_packet1 fV
;            chk -> X+0
;             b0 -> X-4
_slave_packet1::
	.dbline -1
	push X
	mov X,SP
	add SP,1
	.dbline 24
; }
; 
; void slave_packet1(unsigned char b0)
; {
	.dbline 25
; 	unsigned char chk=0;
	mov [X+0],0
	.dbline 26
; 	slave_ack = 0;
	mov REG[0xd0],>_slave_ack
	mov [_slave_ack],0
	.dbline 28
; 			
; 	chk = slave_chksend(chk, 237);
	mov A,-19
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-2
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 29
; 	chk = slave_chksend(chk, 5);
	mov A,5
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 30
; 	chk = slave_chksend(chk, 0xc0);
	mov A,-64
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-4
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 31
; 	chk = slave_chksend(chk, b0);
	mov A,[X-4]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 32
; 	slave_chksend(chk, chk);
	mov A,[X+0]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-4
	mov REG[0xd0],>__r0
	.dbline 34
; 
; 	slave_waitack();
	xcall _slave_waitack
	.dbline -2
	.dbline 35
; }
L7:
	add SP,-1
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l chk 0 c
	.dbsym l b0 -4 c
	.dbend
	.dbfunc e slave_packet3 _slave_packet3 fV
;            chk -> X+0
;             b2 -> X-6
;             b1 -> X-5
;             b0 -> X-4
_slave_packet3::
	.dbline -1
	push X
	mov X,SP
	add SP,1
	.dbline 38
; 
; void slave_packet3(unsigned char b0, unsigned char b1, unsigned char b2)
; {
	.dbline 39
; 	unsigned char chk=0;
	mov [X+0],0
	.dbline 40
; 	slave_ack = 0;
	mov REG[0xd0],>_slave_ack
	mov [_slave_ack],0
	.dbline 42
; 		
; 	chk = slave_chksend(chk, 237);
	mov A,-19
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-2
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 43
; 	chk = slave_chksend(chk, 7);
	mov A,7
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 44
; 	chk = slave_chksend(chk, 0xc0);
	mov A,-64
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-4
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 45
; 	chk = slave_chksend(chk, b0);
	mov A,[X-4]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 46
; 	chk = slave_chksend(chk, b1);
	mov A,[X-5]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-4
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 47
; 	chk = slave_chksend(chk, b2);
	mov A,[X-6]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 48
; 	slave_chksend(chk, chk);
	mov A,[X+0]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-4
	mov REG[0xd0],>__r0
	.dbline 50
; 
; 	slave_waitack();
	xcall _slave_waitack
	.dbline -2
	.dbline 51
; }
L8:
	add SP,-1
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l chk 0 c
	.dbsym l b2 -6 c
	.dbsym l b1 -5 c
	.dbsym l b0 -4 c
	.dbend
	.dbfunc e slave_packet4 _slave_packet4 fV
;            chk -> X+0
;             b3 -> X-7
;             b2 -> X-6
;             b1 -> X-5
;             b0 -> X-4
_slave_packet4::
	.dbline -1
	push X
	mov X,SP
	add SP,1
	.dbline 54
; 
; void slave_packet4(unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3)
; {
	.dbline 55
; 	unsigned char chk=0;
	mov [X+0],0
	.dbline 57
; 	
; 	slave_ack=0;
	mov REG[0xd0],>_slave_ack
	mov [_slave_ack],0
	.dbline 59
; 	
; 	chk = slave_chksend(chk, 237);
	mov A,-19
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 60
; 	chk = slave_chksend(chk, 8);
	mov A,8
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-4
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 61
; 	chk = slave_chksend(chk, 0xc0);
	mov A,-64
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 62
; 	chk = slave_chksend(chk, b0);
	mov A,[X-4]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-4
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 63
; 	chk = slave_chksend(chk, b1);
	mov A,[X-5]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 64
; 	chk = slave_chksend(chk, b2);
	mov A,[X-6]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-4
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 65
; 	chk = slave_chksend(chk, b3);
	mov A,[X-7]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	mov REG[0xd0],>__r0
	mov [X+0],A
	.dbline 66
; 	slave_chksend(chk, chk);
	mov A,[X+0]
	push A
	mov A,[X+0]
	push A
	xcall _slave_chksend
	add SP,-4
	mov REG[0xd0],>__r0
	.dbline 68
; 	
; 	slave_waitack();
	xcall _slave_waitack
	.dbline -2
	.dbline 69
; }
L9:
	add SP,-1
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l chk 0 c
	.dbsym l b3 -7 c
	.dbsym l b2 -6 c
	.dbsym l b1 -5 c
	.dbsym l b0 -4 c
	.dbend
	.dbfunc e slave_waitack _slave_waitack fV
;      starttime -> X+0
_slave_waitack::
	.dbline -1
	push X
	mov X,SP
	add SP,4
	.dbline 73
; 
; 
; void slave_waitack()
; {
	.dbline 74
; 	long starttime=sleepcounter;
	mov REG[0xd0],>_sleepcounter
	mov A,[_sleepcounter+1]
	mov [X+3],A
	mov A,[_sleepcounter]
	mov [X+2],A
	mov [X+1],0
	mov [X+0],0
	xjmp L12
L11:
	.dbline 77
; 	
; 	while (!slave_ack)
; 	{
	.dbline 78
; 		if ((sleepcounter-starttime)>SLAVE_TIMEOUT)
	mov REG[0xd0],>_sleepcounter
	mov A,[_sleepcounter+1]
	mov REG[0xd0],>__r0
	mov [__r3],A
	mov REG[0xd0],>_sleepcounter
	mov A,[_sleepcounter]
	mov REG[0xd0],>__r0
	mov [__r2],A
	mov [__r1],0
	mov [__r0],0
	mov A,[X+3]
	sub [__r3],A
	mov A,[X+2]
	sbb [__r2],A
	mov A,[X+1]
	sbb [__r1],A
	mov A,[X+0]
	sbb [__r0],A
	mov A,100
	sub A,[__r3]
	mov A,0
	sbb A,[__r2]
	mov A,0
	sbb A,[__r1]
	mov A,[__r0]
	xor A,-128
	mov [__rX],A
	mov A,(0 ^ 0x80)
	sbb A,[__rX]
	jnc L14
X0:
	.dbline 79
; 		{
	.dbline 80
; 			commerror(5);
	mov A,5
	push A
	xcall _commerror
	add SP,-1
	.dbline 81
; 			break;
	xjmp L13
L14:
	.dbline 83
	xcall _idle
	.dbline 84
L12:
	.dbline 76
	mov REG[0xd0],>_slave_ack
	cmp [_slave_ack],0
	jz L11
L13:
	.dbline 86
; 		}
; 		idle();
; 	}
; 
; 	slave_ack=0;
	mov REG[0xd0],>_slave_ack
	mov [_slave_ack],0
	.dbline -2
	.dbline 87
; }
L10:
	add SP,-4
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l starttime 0 L
	.dbend
	.dbfunc e setmotor _setmotor fV
;            dir -> X+0
;         dirpwm -> X-6
;           port -> X-4
_setmotor::
	.dbline -1
	push X
	mov X,SP
	add SP,1
	.dbline 91
; 
; 
; void setmotor(unsigned char port, int dirpwm)
; {
	.dbline 92
; 	unsigned char dir=0;
	mov [X+0],0
	.dbline 94
; 	
; 	if (dirpwm>255)
	mov A,-1
	sub A,[X-5]
	mov A,[X-6]
	xor A,-128
	mov REG[0xd0],>__r0
	mov [__rX],A
	mov A,(0 ^ 0x80)
	sbb A,[__rX]
	jnc L17
X1:
	.dbline 95
; 		dirpwm=255;
	mov [X-5],-1
	mov [X-6],0
L17:
	.dbline 96
; 	if (dirpwm<-255)
	mov A,[X-5]
	sub A,1
	mov A,[X-6]
	xor A,-128
	sbb A,(-1 ^ 0x80)
	jnc L19
X2:
	.dbline 97
; 		dirpwm=-255;
	mov [X-5],1
	mov [X-6],-1
L19:
	.dbline 99
; 	
; 	if (dirpwm>=0)
	mov A,[X-5]
	sub A,0
	mov A,[X-6]
	xor A,-128
	sbb A,(0 ^ 0x80)
	jc L21
X3:
	.dbline 100
; 		dir=1;
	mov [X+0],1
L21:
	.dbline 102
; 	
; 	if (dirpwm<0)
	mov A,[X-5]
	sub A,0
	mov A,[X-6]
	xor A,-128
	sbb A,(0 ^ 0x80)
	jnc L23
X4:
	.dbline 103
; 	{
	.dbline 104
; 		dir=2;
	mov [X+0],2
	.dbline 105
; 		dirpwm=-dirpwm;
	mov A,[X-5]
	cpl A
	sub A,-1
	mov [X-5],A
	mov A,[X-6]
	cpl A
	sbb A,-1
	mov [X-6],A
	.dbline 106
; 	}	
L23:
	.dbline 108
; 	
; 	slave_packet4('M', port, dir, dirpwm);
	mov REG[0xd0],>__r0
	mov A,[X-5]
	push A
	mov A,[X+0]
	push A
	mov A,[X-4]
	push A
	mov A,77
	push A
	xcall _slave_packet4
	add SP,-4
	.dbline -2
	.dbline 109
; }
L16:
	add SP,-1
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l dir 0 c
	.dbsym l dirpwm -6 I
	.dbsym l port -4 c
	.dbend
	.dbfunc e getmotoractualsignedpwm _getmotoractualsignedpwm fI
;            dir -> X+2
;            pwm -> X+0
;           port -> X-4
_getmotoractualsignedpwm::
	.dbline -1
	push X
	mov X,SP
	add SP,3
	.dbline 112
; 
; int getmotoractualsignedpwm(unsigned char port)
; {
	.dbline 113
; 	int pwm=getmotoractualpwm(port);
	mov A,[X-4]
	push A
	xcall _getmotoractualpwm
	mov REG[0xd0],>__r0
	mov [X+1],A
	mov [X+0],0
	.dbline 114
; 	unsigned char dir=getmotoractualdir(port);
	mov A,[X-4]
	push A
	xcall _getmotoractualdir
	add SP,-2
	mov REG[0xd0],>__r0
	mov [X+2],A
	.dbline 116
; 	
; 	if (dir==1)
	cmp [X+2],1
	jnz L26
	.dbline 117
; 		return pwm;
	mov A,[X+1]
	mov [__r1],A
	mov A,[X+0]
	mov [__r0],A
	xjmp L25
L26:
	.dbline 118
; 	else if (dir==2)
	cmp [X+2],2
	jnz L28
	.dbline 119
; 		return -pwm;
	mov REG[0xd0],>__r0
	mov A,[X+1]
	cpl A
	sub A,-1
	mov [__r1],A
	mov A,[X+0]
	cpl A
	sbb A,-1
	mov [__r0],A
	xjmp L25
L28:
	.dbline 120
; 	return pwm;
	mov REG[0xd0],>__r0
	mov A,[X+1]
	mov [__r1],A
	mov A,[X+0]
	mov [__r0],A
	.dbline -2
L25:
	add SP,-3
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l dir 2 c
	.dbsym l pwm 0 I
	.dbsym l port -4 c
	.dbend
	.dbfunc e getmotoractualpwm _getmotoractualpwm fc
;           port -> X-4
_getmotoractualpwm::
	.dbline -1
	push X
	mov X,SP
	.dbline 124
; }
; 
; unsigned char getmotoractualpwm(unsigned char port)
; {
	.dbline 125
; 	return slavestate[13+1+port*3];
	mov REG[0xd0],>__r0
	mov [__r1],3
	mov [__r0],0
	mov A,[X-4]
	mov [__r3],A
	mov [__r2],0
	mov [__rX],0
	mov [__rY],0
X5:
	cmp [__r0],0
	jnz X8
	cmp [__r1],0
	jz X7
X8:
	and F,-5
	rrc [__r0]
	rrc [__r1]
	jnc X6
	mov REG[0xd0],>__r0
	mov A,[__r3]
	add [__rX],A
	mov A,[__r2]
	adc [__rY],A
X6:
	asl [__r3]
	rlc [__r2]
	jmp X5
X7:
	mov [__r1],[__rX]
	mov [__r0],[__rY]
	mov REG[0xd0],>__r0
	add [__r1],<_slavestate+14
	adc [__r0],>_slavestate+14
	mov A,[__r0]
	mov REG[0xd4],A
	mvi A,[__r1]
	.dbline -2
L30:
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l port -4 c
	.dbend
	.dbfunc e getmotorgoalpwm _getmotorgoalpwm fc
;           port -> X-4
_getmotorgoalpwm::
	.dbline -1
	push X
	mov X,SP
	.dbline 129
; }
; 
; unsigned char getmotorgoalpwm(unsigned char port)
; {
	.dbline 130
; 	return slavestate[13+port*3];
	mov REG[0xd0],>__r0
	mov [__r1],3
	mov [__r0],0
	mov A,[X-4]
	mov [__r3],A
	mov [__r2],0
	mov [__rX],0
	mov [__rY],0
X9:
	cmp [__r0],0
	jnz X12
	cmp [__r1],0
	jz X11
X12:
	and F,-5
	rrc [__r0]
	rrc [__r1]
	jnc X10
	mov REG[0xd0],>__r0
	mov A,[__r3]
	add [__rX],A
	mov A,[__r2]
	adc [__rY],A
X10:
	asl [__r3]
	rlc [__r2]
	jmp X9
X11:
	mov [__r1],[__rX]
	mov [__r0],[__rY]
	mov REG[0xd0],>__r0
	add [__r1],<_slavestate+13
	adc [__r0],>_slavestate+13
	mov A,[__r0]
	mov REG[0xd4],A
	mvi A,[__r1]
	.dbline -2
L32:
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l port -4 c
	.dbend
	.dbfunc e getmotoractualdir _getmotoractualdir fc
;              v -> X+0
;           port -> X-4
_getmotoractualdir::
	.dbline -1
	push X
	mov X,SP
	add SP,1
	.dbline 134
; }
; 
; unsigned char getmotoractualdir(unsigned char port)
; {
	.dbline 136
; 	unsigned char v;
; 	v=slavestate[25+port/2];
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	mov A,0
	push A
	mov A,2
	push A
	mov A,0
	push A
	mov A,[__r1]
	push A
	xcall __divmod_16X16_16
	pop A
	mov [__r1],A
	pop A
	mov [__r0],A
	add SP,-2
	add [__r1],<_slavestate+25
	adc [__r0],>_slavestate+25
	mov A,[__r0]
	mov REG[0xd4],A
	mvi A,[__r1]
	mov [X+0],A
	.dbline 138
; 	
; 	if ((port&1)==0)
	tst [X-4],1
	jnz L36
	.dbline 139
; 		return (v>>2)&0x03;
	mov A,[X+0]
	mov [__r0],A
	asr [__r0]
	asr [__r0]
	and [__r0],63
	and [__r0],3
	mov A,[__r0]
	xjmp L34
L36:
	.dbline 140
; 	return (v>>6)&0x03;
	mov A,[X+0]
	mov REG[0xd0],>__r0
	asr A
	asr A
	asr A
	asr A
	asr A
	asr A
	and A,3
	mov [__r0],A
	and [__r0],3
	mov A,[__r0]
	.dbline -2
L34:
	add SP,-1
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l v 0 c
	.dbsym l port -4 c
	.dbend
	.dbfunc e getmotorcurrent _getmotorcurrent fi
;             vi -> X+4
;              v -> X+0
;           port -> X-4
_getmotorcurrent::
	.dbline -1
	push X
	mov X,SP
	add SP,6
	.dbline 144
; }
; 
; unsigned int getmotorcurrent(unsigned char port)
; {
	.dbline 145
; 	unsigned int vi=(slavestate[27+port*2]<<8) + slavestate[27+1+port*2];
	mov REG[0xd0],>__r0
	mov A,[X-4]
	mov [__r1],A
	mov [__r0],0
	asl [__r1]
	rlc [__r0]
	mov A,[__r1]
	add A,<_slavestate+28
	mov [__r3],A
	mov A,[__r0]
	adc A,>_slavestate+28
	mov REG[0xd4],A
	mvi A,[__r3]
	mov [__r2],A
	add [__r1],<_slavestate+27
	adc [__r0],>_slavestate+27
	mov A,[__r0]
	mov REG[0xd4],A
	mvi A,[__r1]
	mov [X+4],A
	mov A,[__r2]
	mov [X+5],A
	.dbline 146
; 	long v=vi;
	mov A,[X+5]
	mov [X+3],A
	mov A,[X+4]
	mov [X+2],A
	mov [X+1],0
	mov [X+0],0
	.dbline 149
; 
; 	// 1000*5.0/65535/0.18 = 0.4238
; 	v=(v*434)>>10;
	mov A,[X+0]
	push A
	mov A,[X+1]
	push A
	mov A,[X+2]
	push A
	mov A,[X+3]
	push A
	mov A,0
	push A
	push A
	mov A,1
	push A
	mov A,-78
	push A
	xcall __mul_32X32_32
	add SP,-4
	pop A
	mov [__r3],A
	pop A
	mov [__r2],A
	pop A
	mov [__r1],A
	pop A
	mov [__r0],A
	mov A,10
X13:
	mov REG[0xd0],>__r0
	asr [__r0]
	rrc [__r1]
	rrc [__r2]
	rrc [__r3]
	dec A
	jnz X13
	mov A,[__r0]
	mov [X+0],A
	mov A,[__r1]
	mov [X+1],A
	mov A,[__r2]
	mov [X+2],A
	mov A,[__r3]
	mov [X+3],A
	.dbline 150
; 	return v; 
	mov A,[X+3]
	mov [__r1],A
	mov A,[X+2]
	mov [__r0],A
	.dbline -2
L38:
	add SP,-6
	pop X
	.dbline 0 ; func end
	ret
	.dbsym l vi 4 i
	.dbsym l v 0 L
	.dbsym l port -4 c
	.dbend
