	; Small C compiler for Ubicom SX28

device	SX28,oschs1,turbo,stackx,optionx
irc_cal	IRC_SLOW
id	'--SX28--'
reset	reset_entry
freq	20_000_000

RA_latch	equ	%00000000	;port A output latch init 1=high 0=low
RA_tris	equ	%11111111	;port A direction register 1=input 0=output
RA_plp	equ	%00000000	;port A pull up register 1=off 0=on
RA_lvl	equ	%11111111	;port A level register 1=ttl 0=cmos

RB_latch	equ	%00000000	;port B output latch 1=high 0=low
RB_tris	equ	%11111111	;port B direction register 1=input 0=output
RB_plp	equ	%00000000	;port B pull up register 1=off 0=on
RB_lvl	equ	%00000000	;port B level register 1=ttl 0=cmos
RB_st	equ	%11111111	;port B schmitt trigger register 1=off 0=on
RB_wken	equ	%11111111	;port B wake up enable register 1=off 0=on
RB_wked	equ	%11111111	;port B wake up edge register 1=neg 0=pos

RC_latch	equ	%00000000	;port C output latch 1=high 0=low
RC_tris	equ	%11111111	;port C direction register 1=input 0=output
RC_plp	equ	%00000000	;port C pull up register 1=off 0=on
RC_lvl	equ	%11111111	;port C level register 1=ttl 0=cmos
RC_st	equ	%11111111	;port C schmitt trigger register 1=off 0=on

	;Leave globals $08 and $09 for RD and RE (SX48/52)
fA	equ	$0A	;auxiliary register
	;IND with fsr set to SP-1 is 2nd aux register
fL	equ	$0B	;primary register lo byte
fH	equ	$0C	;primary register hi byte
fE	equ	$0D	;secondary register lo byte
fD	equ	$0E	;secondary register hi byte
fSP	equ	$0F	;variables stack pointer

M_WKED	equ	$0A	;values for mode register
M_WKEN	equ	$0B
M_ST	equ	$0C
M_LVL	equ	$0D
M_PLP	equ	$0E
M_TRIS	equ	$0F

	; option register constants
RTCC_ON	equ	%10000000	;Enables RTCC at address $01 (RTW hi)
			;*WREG at address $01 (RTW lo) by default
RTCC_ID	equ	%01000000	;Disables RTCC edge interrupt (RTE_IE hi)
			;*RTCC edge interrupt (RTE_IE lo) enabled by default
RTCC_INC_EXT	equ	%00100000	;Sets RTCC increment on RTCC pin transition (RTS hi)
			;*RTCC increment on internal instruction (RTS lo) is defalut
RTCC_FE	equ	%00010000	;Sets RTCC to increment on falling edge (RTE_ES hi)
			;*RTCC to increment on rising edge (RTE_ES lo) is default
RTCC_PS_ON	equ	%00000000	;Assigns prescaler to RTCC (PSA lo)
RTCC_PS_OFF	equ	%00001000	;Assigns prescaler to WDT (PSA hi)
PS_000	equ	%00000000	;RTCC = 1:2, WDT = 1:1
PS_001	equ	%00000001	;RTCC = 1:4, WDT = 1:2
PS_010	equ	%00000010	;RTCC = 1:8, WDT = 1:4
PS_011	equ	%00000011	;RTCC = 1:16, WDT = 1:8
PS_100	equ	%00000100	;RTCC = 1:32, WDT = 1:16
PS_101	equ	%00000101	;RTCC = 1:64, WDT = 1:32
PS_110	equ	%00000110	;RTCC = 1:128, WDT = 1:64
PS_111	equ	%00000111	;RTCC = 1:256, WDT = 1:128

	; define storage macro, skipping over banks as needed
dfs	macro	1
local1 = \1
rept	local1
	ds	1
if ($ & 010h) = 0
	org	$+$10
endif
endr
endm

	; secret instruction pushPC
pushPC	macro
	dw	$4B
endm

	; load primary register macro
movHL	macro	1
	mov	fL,#\1&$FF
	mov	fH,#\1>>8
endm

	org	0

interrupt
	jmp	@_interrupt
reset_entry
	jmp	_init
start
	jmp	@main
pushHL
	jmp	_pushHL
popDE
	jmp	_popDE
exDEHL
	jmp	_exDEHL
exSPHL
	jmp	_exSPHL
switch
	jmp	_switch
eqDEHL
	jmp	_eqDEHL
neDEHL
	jmp	_neDEHL
ltDEHL
	jmp	_ltDEHL
leDEHL
	jmp	_leDEHL
gtDEHL
	jmp	_gtDEHL
geDEHL
	jmp	_geDEHL
ultDEHL
	jmp	_ultDEHL
uleDEHL
	jmp	_uleDEHL
ugtDEHL
	jmp	_ugtDEHL
ugeDEHL
	jmp	_ugeDEHL
addDEHL
	jmp	_addDEHL
subDEHL
	jmp	_subDEHL
mulDEHL
	jmp	_mulDEHL
divDEHL
	jmp	_divDEHL
orDEHL
	jmp	_orDEHL
xorDEHL
	jmp	_xorDEHL
andDEHL
	jmp	_andDEHL
asrDEHL
	jmp	_asrDEHL
aslDEHL
	jmp	_aslDEHL
negHL
	jmp	_negHL
lnegHL
	jmp	_lnegHL
comHL
	jmp	_comHL
boolHL
	jmp	_boolHL
incHL
	jmp	_incHL
decHL
	jmp	_decHL
writeL
	jmp	_writeL
writeHL
	jmp	_writeHL
readL
	jmp	_readL
readHL
	jmp	_readHL
callf
	jmp	_callf

_retfsr	;exit for routines that changed fsr
	mov	fsr,#$FF	;allow IND as extra register
	retp

_false	;HL=0 and Z set
	clr	fH
	clr	fL
	stz
	jmp	_retfsr

_true	;HL=1 and Z clear
	clr	fH
	clr	fL
	inc	fL
	clz
	jmp	_retfsr

_setfsr
	mov	fsr,w
	and	w,#$70
	add	fsr,w
	add	fsr,#$10
	retp

_incDE
	inc	fE
	snz
	inc	fD
	retp

_ireadDE
	mov	w,fD
	mov	m,w
	mov	w,fE
	iread
	retp

_pushHL
	mov	w,fSP
	call	_setfsr
	mov	indf,fH
	dec	fSP
	mov	w,fSP
	call	_setfsr
	mov	indf,fL
	dec	fSP
	jmp	_retfsr

_popDE
	inc	fSP
	mov	w,fSP
	call	_setfsr
	mov	fE,indf
	inc	fSP
	mov	w,fSP
	call	_setfsr
	mov	fD,indf
	jmp	_retfsr

_exDEHL
	mov	w,fL
	xor	fE,w
	xor	w,fE
	mov	fL,w
	xor	fE,w
	mov	w,fH
	xor	fD,w
	xor	w,fD
	mov	fH,w
	xor	fD,w
	retp

_exSPHL
	inc	fSP
	mov	w,fSP
	call	_setfsr
	mov	w,fL
	xor	indf,w
	xor	w,indf
	mov	fL,w
	xor	indf,w
	inc	fSP
	mov	w,fSP
	call	_setfsr
	mov	w,fH
	xor	indf,w
	xor	w,indf
	mov	fH,w
	xor	indf,w
	dec	fSP
	dec	fSP
	jmp	_retfsr

_switch
	call	popDE	;get table address
	call	pushHL	;compare value on stack
	call	_ireadDE	;read call address lowbyte
	mov	fL,w
	call	_incDE
	call	_ireadDE	;read call address highbyte
	mov	fH,w
	or	w,fL	;test for table end
	jz	:default
	call	exSPHL	;compare value in hl, call addr on stack
	call	_incDE
	call	_ireadDE	;read value lowbyte
	xor	w,fL	;compare values lowbyte
	jnz	:nomatch_early	;no match
	call	_incDE
	call	_ireadDE	;read value highbyte
	xor	w,fH	;compare values highbyte
	jnz	:nomatch
	call	_incDE
	call	exDEHL	;table address in hl
	call	exSPHL	;call addr in hl, table addr on stack
	mov	PC,fL	;PROBLEM !! How to JMP HL
:nomatch_early
	call	_incDE	;point to value highbyte
:nomatch
	call	_incDE	;point to next caae
	call	exSPHL	;call addr in hl, compare value on stack
	call	exDEHL
	call	exSPHL	;compare value in hl, table addr on stack
	jmp	_switch	;chack next case
:default
	call	_incDE
	call	_ireadDE	;read default call addr lowbyte
	mov	fL,w
	call	_incDE
	call	_ireadDE	;read default call addr highbyte
	mov	fH,w
	mov	PC,fL	;PROBLEM !! How to jmp HL

_eqDEHL	;test if HL == DE
	mov	w,fL
	xor	w,fE
	jnz	_false
	mov	w,fH
	xor	w,fD
	jnz	_false
	jmp	_true

_neDEHL	;test if HL != DE
	mov	w,fL
	xor	w,fE
	jnz	_true
	mov	w,fH
	xor	w,fD
	jnz	_true
	jmp	_false

_geDEHL	;test if HL >= DE (signed)
	call	exDEHL
_ltDEHL	;test if HL < DE (signed)
	mov	w,fE
	mov	w,fL-w
	mov	w,fD
	sc
	movsz	w,++fD
	mov	w,fH-w
	jnc	_true
	jmp	_false

_leDEHL	;test if HL <= DE (signed)
	call	exDEHL
_gtDEHL	;test if HL > DE *signed)
	mov	w,fL
	mov	w,fE-w
	mov	w,fH
	sc
	movsz	w,++fH
	mov	w,fD-w
	jnc	_true
	jmp	_false

_ugeDEHL	;test if HL >= DE (unsigned)
	call	exDEHL
_ultDEHL	;test if HL < DE (unsigned)
	mov	w,fD
	mov	w,fH-w
	jc	_true
	jnz	_false
	mov	w,fE
	mov	w,fL-w
	jc	_true
	jmp	_false

_uleDEHL	;test if HL <= DE (unsigned)
	call	exDEHL
_ugtDEHL	;test if HL > DE (unsigned)
	mov	w,fH
	mov	w,fD-w
	jc	_true
	jnz	_false
	mov	w,fL
	mov	w,fE-w
	jc	_true
	jmp	_false

_addDEHL	;HL = HL + DE
	add	fL,fE
	snc
	inc	fH
	add	fH,fD
	retp

_subDEHL	;HL = HL - DE
	mov	w,fE
	mov	w,fL-w
	mov	fL,w
	snc
	dec	fH
	sub	fH,fD
	retp

_mulDEHL	;HL = DE * HL (signed)
	mov	fA,fL
	mov	indf,fH
	clr	fL	;initialize product
	clr	fH
:loop
	clrb	C	;clear carry again
	rr	indf	;shift multiplier down
	rr	fA
	sb	C	;skip if multiplier lsb not set
	jmp	:lop1
	mov	w,fE	;add multiplicand to product
	add	fL,w
	mov	w,fD
	snb	C
	mov	w,++fD
	add	fH,w
:lop1
	clrb	C	;clear carry
	rl	fE	;shift multiplicand up
	rl	fD
	mov	w,fA
	or	w,indf
	sb	Z	;test for early return
	jmp	:loop
	retp	;done, result in hl

_divDEHL	;HL = DE / HL (signed)
	and	ra,#$0F	;high nibble of ra to count 16 bits
	clr	fA	;clear remainder low byte
	clr	indf	;clear remainder high byte
	clr	w
	mov	m,w	;m.0 saves carry
:loop
	mov	w,m	;get carry
	stc	;assume set
	test	w
	snz
	clc
	rl	fE	;check the dividend
	rl	fD	;bit by bit
	rl	fA	;put it in the remainder for
	rl	indf	;trial subtraction
	stc	;prepare for subtraction, no borrow
	mov	w,fH	;do trial subtraction
	mov	w,indf-w	;from MSB first
	sz	;if two MSB equal, need to check LSB
	jmp	:check	;not equal, check which one is bigger
	;if we are here, then z=1, so c must be 1 too, since there is no
	;underflow, so we save a stc instruction
	mov	w,fL	;equal MSB, check LSB
	mov	w,fA-w	;which one is bigger?
:check
	sc	;partial dividend >a?
	jmp	:shift	;no, partial dividend < a, set a 0 into quotient
	;if we are here, then c must be 1, again, we save another stc instruction
	;yes, part. dividend > a, subtract a from it
	sub	fA,fL	;store part. dividend-a into a
	sub	indf,fH
	stc	;	shift a 1 into quotient
:shift
	mov	w,#1	;prepare to save carry
	sc
	clr	w
	mov	m,w	;save carry
	add	ra,#$10	;increment bit count
	jnc	:loop	;not all done yet
	call	exDEHL	;quotient in HL
	mov	fD,indf	;remainder in DE
	mov	fE,fA
	mov	m,#$0F	;restore mode register
	retp

_orDEHL	;HL = HL | DE
	or	fL,fE
	or	fH,fD
	retp

_xorDEHL	;HL = HL ^ DE
	xor	fL,fE
	xor	fH,fD
	retp

_andDEHL	;HL = HL & DE
	and	fL,fE
	and	fH,fD
	retp

_asrDEHL	;HL = DE >> HL
	call	exDEHL
	test	fD
	jnz	_false
	test	fE
	snz
	retp
_asr
	clc
	rr	fH
	rr	fL
	dec	fE
	jnz	_asr
	retp

_aslDEHL	;HL = DE << HL
	call	exDEHL
	test	fD
	jnz	_false
	test	fE
	snz
	retp
_asl
	clc
	rl	fL
	rl	fH
	dec	fE
	jnz	_asl
	retp

_negHL	;HL = -HL
	not	fL
	not	fH
	jmp	incHL

_lnegHL	;HL = !HL
	mov	w,fL
	or	w,fH
	jnz	_false
	jmp	_true

_comHL	;HL = ~HL
	not	fL
	not	fH
	retp

_boolHL	;HL = HL != 0
	mov	w,fL
	or	w,fH
	jnz	_true
	jmp	_false

_incHL	;HL++
	inc	fL
	snz
	inc	fH
	retp

_decHL	;HL--
	test	fL
	snz
	dec	fH
	dec	fL
	retp

_writeL
	mov	w,fE
	call	_setfsr
	mov	indf,fL
	jmp	_retfsr

_writeHL
	mov	w,fE
	call	_setfsr
	mov	indf,fL
	mov	w,++fE
	call	_setfsr
	mov	indf,fH
	jmp	_retfsr

_readL
	mov	w,fE
	call	_setfsr
	mov	fL,indf
	clr	fH
	jmp	_retfsr

_readHL
	mov	w,fE
	call	_setfsr
	mov	fL,indf
	mov	w,++fE
	call	_setfsr
	mov	fH,indf
	jmp	_retfsr

_callf
	mov	m,fD
	mov	w,fE
	pushPC
	reti
	;we never return here but after call @callf

	; init ports, clear ram, setup option register
_init
	mov	m,#M_WKED	; point mode to WKED
	mov	!rb,#RB_wked
	mov	m,#M_WKEN	; point mode to WKEN
	mov	!rb,#RB_wken
	mov	m,#M_ST	; point mode to ST
	mov	!rc,#RC_st
	mov	!rb,#RB_st
	mov	m,#M_LVL	; point mode to LVL
	mov	!rc,#RC_lvl
	mov	!rb,#RB_lvl
	mov	!ra,#RA_lvl
	mov	m,#M_PLP	; point mode to PLP
	mov	!rc,#RC_plp
	mov	!rb,#RB_plp
	mov	!ra,#RA_plp
	mov	rc,#RC_latch	; init latches
	mov	rb,#RB_latch
	mov	ra,#RA_latch
	mov	m,#M_TRIS	; point mode to TRIS
	mov	!rc,#RC_tris
	mov	!rb,#RB_tris
	mov	!ra,#RA_tris
	; clear all ram
	mov	fsr,#$08
:zero_global
	clr	indf
	inc	fsr
	cjne	fsr,#$10,:zero_global
:zero_ram
	clr	indf
	inc	fsr
	jz	:zero_end
	setb	fsr.4
	jmp	:zero_ram
:zero_end
	mov	fSP,#126	;set variables stack to highest ram - 1
	;All runtime routines leave fsr at $FF
	mov	fsr,#$FF	;set fsr to last ram byte
	;setup option register
	mov	w,#RTCC_PS_OFF
	mov	!option,w
	jmp	start

_interrupt
	reti

	;///--- code ---///
;/* FILE TEST.C */
;/* test for sccsx - smallc for sx28 */
;char _c;       /*   1 */  /* bytes allocated (127-stacksize max) */
;char _ca[14];  /*  14 */
;int _i,_j;     /*   4 */
;int _ia[10];   /*  20 */
;/* to do:
;  special #defines:
;   stacksize,device,oschs1,turbo,stackx,optionx,freq,id
;*/
;main (argc, argv) int argc; char *argv[]; {
main 
;  char _k; int _p, _q, _r[6]; int *_fp;
	call	@exDEHL
	movHL	-1
	add	fSP,fL
	call	@exDEHL
	call	@exDEHL
	movHL	-16
	add	fSP,fL
	call	@exDEHL
	call	@exDEHL
	movHL	-2
	add	fSP,fL
	call	@exDEHL
;  (*_fp)(_p,_q);
	movHL	0
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@exDEHL
	jmp	@_2
	org	512
_2
	call	@readHL
	call	@pushHL
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@exSPHL
	call	@pushHL
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@exSPHL
	call	@pushHL
	mov	fA,#2
	call	@popDE
	call	@callf
	call	@exDEHL
	movHL	4
	add	fSP,fL
	call	@exDEHL
;  _c = 'A';
	movHL	65
	call	@pushHL
	movHL	_c
	call	@popDE
	call	@exDEHL
	call	@writeL
;  _i = 12;
	movHL	12
	call	@pushHL
	movHL	_i
	call	@popDE
	call	@exDEHL
	call	@writeHL
;  /* try all operators */
;  _ia[5] = _i + _j;
	movHL	_ia
	call	@pushHL
	movHL	5
	clc
	rl	fL
	rl	fH
	call	@popDE
	call	@addDEHL
	call	@pushHL
	movHL	_i
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	_j
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@addDEHL
	call	@popDE
	call	@writeHL
;  _ia[5] = _i - _j;
	movHL	_ia
	call	@pushHL
	movHL	5
	clc
	rl	fL
	rl	fH
	call	@popDE
	call	@addDEHL
	call	@pushHL
	movHL	_i
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	_j
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@subDEHL
	call	@popDE
	call	@writeHL
;  _ia[5] = _i * _j;
	movHL	_ia
	call	@pushHL
	movHL	5
	clc
	rl	fL
	rl	fH
	call	@popDE
	call	@addDEHL
	call	@pushHL
	movHL	_i
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	_j
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@mulDEHL
	call	@popDE
	call	@writeHL
;  _ia[5] = _i / _j;
	movHL	_ia
	call	@pushHL
	movHL	5
	clc
	rl	fL
	rl	fH
	call	@popDE
	call	@addDEHL
	call	@pushHL
	movHL	_i
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	_j
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@divDEHL
	call	@popDE
	call	@writeHL
;  _ia[5] = _i % _j;
	movHL	_ia
	call	@pushHL
	movHL	5
	clc
	rl	fL
	rl	fH
	call	@popDE
	call	@addDEHL
	call	@pushHL
	movHL	_i
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	_j
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@divDEHL
	call	@exDEHL
	call	@popDE
	call	@writeHL
;  _ia[5] = _i ^ _j;
	movHL	_ia
	call	@pushHL
	movHL	5
	clc
	rl	fL
	rl	fH
	call	@popDE
	call	@addDEHL
	call	@pushHL
	movHL	_i
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	_j
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@xorDEHL
	call	@popDE
	call	@writeHL
;  _ia[5] = _i << _j;
	movHL	_ia
	call	@pushHL
	movHL	5
	clc
	rl	fL
	rl	fH
	call	@popDE
	call	@addDEHL
	call	@pushHL
	movHL	_i
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	_j
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@aslDEHL
	call	@popDE
	call	@writeHL
;  _ia[5] = _i >> _j;
	movHL	_ia
	call	@pushHL
	movHL	5
	clc
	rl	fL
	rl	fH
	call	@popDE
	call	@addDEHL
	call	@pushHL
	movHL	_i
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	_j
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@asrDEHL
	call	@popDE
	call	@writeHL
;  if (_p < _q) {_p = 20;}
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@ltDEHL
	jz	_3
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	20
	call	@popDE
	call	@writeHL
;  if (_p <= _q) {_p = 21;}
_3 
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	jmp	@_5
	org	1024
_5
	call	@popDE
	call	@leDEHL
	jz	_4
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	21
	call	@popDE
	call	@writeHL
;  if (_p == _q) {_p = 22;}
_4 
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@eqDEHL
	jz	_6
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	22
	call	@popDE
	call	@writeHL
;  if (_p >= _q) {_p = 23;}
_6 
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@geDEHL
	jz	_7
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	23
	call	@popDE
	call	@writeHL
;  if (_p > _q) {_p = 24;}
_7 
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@gtDEHL
	jz	_8
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	24
	call	@popDE
	call	@writeHL
;  if (_p != _q) {_p = 25;}
_8 
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@neDEHL
	jz	_9
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	25
	call	@popDE
	call	@writeHL
;  do {
_9 
_10 
;    _p = 26;
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	26
	call	@popDE
	call	@writeHL
;  } while (_q != 0);
_11 
	movHL	14
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	0
	call	@popDE
	call	@neDEHL
	jnz	_10
_12 
;  for (_q=0; _q<10; _q++) {
	movHL	14
	add	fL,fSP
	call	@pushHL
	movHL	0
	call	@popDE
	call	@writeHL
_13 
	movHL	14
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	10
	call	@popDE
	call	@ltDEHL
	jnz	_15
	jmp	@_16
_14 
	movHL	14
	add	fL,fSP
	call	@pushHL
	call	@exDEHL
	call	@readHL
	call	@incHL
	call	@popDE
	call	@writeHL
	call	@decHL
	jmp	@_13
_15 
;    _p = 27;
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	27
	call	@popDE
	call	@writeHL
;  }
	jmp	@_14
_16 
;  while (_q > 0) {
_17 
	movHL	14
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	0
	call	@popDE
	call	@gtDEHL
	jz	_18
;    _p = 28;
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	28
	call	@popDE
	call	@writeHL
;  }
	jmp	@_17
_18 
;  switch (_q) {
	movHL	_19
	call	@pushHL
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	jmp	@switch
;    case 0: _p = 29;
_21 
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	29
	call	@popDE
	call	@writeHL
;            break;
	jmp	@_20
;    case 1: _p = 30;
_22 
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	30
	call	@popDE
	call	@writeHL
;            break;
	jmp	@_20
;    default: _p = 0;
_23 
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	0
	call	@popDE
	call	@writeHL
;  }
	jmp	@_20
	;///--- data ---///
_19 
	dw	_21&$FF,_21>>8,0,0
	dw	_22&$FF,_22>>8,1,0
	dw	0,0,_23&$FF,_23>>8
	;///--- code ---///
_20 
;  _p = _q;
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	16
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@writeHL
;  _p += _q;
	movHL	16
	add	fL,fSP
	call	@pushHL
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@addDEHL
	call	@popDE
	call	@writeHL
;  _p -= _q;
	movHL	16
	add	fL,fSP
	call	@pushHL
	call	@exDEHL
	call	@readHL
	call	@pushHL
	jmp	@_24
	org	1536
_24
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@subDEHL
	call	@popDE
	call	@writeHL
;  _p &= _q;
	movHL	16
	add	fL,fSP
	call	@pushHL
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@andDEHL
	call	@popDE
	call	@writeHL
;  _p |= _q;
	movHL	16
	add	fL,fSP
	call	@pushHL
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@orDEHL
	call	@popDE
	call	@writeHL
;  _p ^= _q;
	movHL	16
	add	fL,fSP
	call	@pushHL
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@xorDEHL
	call	@popDE
	call	@writeHL
;  _p *= _q;
	movHL	16
	add	fL,fSP
	call	@pushHL
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@mulDEHL
	call	@popDE
	call	@writeHL
;  _p /= _q;
	movHL	16
	add	fL,fSP
	call	@pushHL
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@divDEHL
	call	@popDE
	call	@writeHL
;  _p %= _q;
	movHL	16
	add	fL,fSP
	call	@pushHL
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	18
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@divDEHL
	call	@exDEHL
	call	@popDE
	call	@writeHL
;  _p = testsub (_k,_p,_ca);
	movHL	16
	add	fL,fSP
	call	@pushHL
	movHL	20
	add	fL,fSP
	call	@exDEHL
	call	@readL
	call	@pushHL
	movHL	20
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	_ca
	call	@pushHL
	mov	fA,#3
	movHL	testsub
	call	@exDEHL
	call	@callf
	call	@exDEHL
	movHL	6
	add	fSP,fL
	call	@exDEHL
	call	@popDE
	call	@writeHL
;}
_1 
	call	@exDEHL
	movHL	19
	add	fSP,fL
	call	@exDEHL
	retp
;testsub (p,q,r) char p; int q; char *r; {
testsub 
;  if (p < q) return 1;
	movHL	6
	add	fL,fSP
	call	@exDEHL
	call	@readL
	call	@pushHL
	movHL	6
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@popDE
	call	@ltDEHL
	jz	_26
	movHL	1
	jmp	@_25
;  *r = 'A';
_26 
	movHL	2
	add	fL,fSP
	call	@exDEHL
	call	@readHL
	call	@pushHL
	movHL	65
	call	@popDE
	call	@writeL
;  return 0;
	movHL	0
	jmp	@_25
;}
_25 
	retp
;/* end */
	;///--- data ---///
	org	$10
	;values are offsets in available ram
_c 	equ	0
_ca 	equ	1
_i 	equ	15
_j 	equ	17
_ia 	equ	19
	;///--- code ---///
	org	$7FF
	;jmp	reset_entry
end

;0 error(s) in compilation
;literal pool:0
;global pool:126
;Macro pool:37
