#asm
|
#asm
|
;
|
;
|
;------------------------------------------------------------------
|
;------------------------------------------------------------------
|
; Small-C Run-time Librray
|
; Small-C Run-time Librray
|
;
|
;
|
; V4d As of July 16, 1980 (gtf)
|
; V4d As of July 16, 1980 (gtf)
|
; Added EXIT() function
|
; Added EXIT() function
|
;------------------------------------------------------------------
|
;------------------------------------------------------------------
|
;
|
;
|
;Fetch a single byte from the address in HL and sign extend into HL
|
;Fetch a single byte from the address in HL and sign extend into HL
|
ccgchar:
|
ccgchar:
|
ld a,(hl)
|
ld a,(hl)
|
ccsxt:
|
ccsxt:
|
ld l,a
|
ld l,a
|
rlca
|
rlca
|
sbc a
|
sbc a
|
ld h,a
|
ld h,a
|
ret
|
ret
|
;Fetch a full 16-bit integer from the address in HL
|
;Fetch a full 16-bit integer from the address in HL
|
ccgint:
|
ccgint:
|
ld a,(hl)
|
ld a,(hl)
|
inc hl
|
inc hl
|
ld h,(hl)
|
ld h,(hl)
|
ld l,a
|
ld l,a
|
ret
|
ret
|
;Store a single byte from HL at the address in DE
|
;Store a single byte from HL at the address in DE
|
ccpchar:
|
ccpchar:
|
ld a,l
|
ld a,l
|
ld (de),a
|
ld (de),a
|
ret
|
ret
|
;Store a 16-bit integer in HL at the address in DE
|
;Store a 16-bit integer in HL at the address in DE
|
ccpint:
|
ccpint:
|
ld a,l
|
ld a,l
|
ld (de),a
|
ld (de),a
|
inc de
|
inc de
|
ld a,h
|
ld a,h
|
ld (de),a
|
ld (de),a
|
ret
|
ret
|
;Inclusive "or" HL and DE into HL
|
;Inclusive "or" HL and DE into HL
|
ccor:
|
ccor:
|
ld a,l
|
ld a,l
|
or e
|
or e
|
ld l,a
|
ld l,a
|
ld a,h
|
ld a,h
|
or d
|
or d
|
ld h,a
|
ld h,a
|
ret
|
ret
|
;Exclusive "or" HL and DE into HL
|
;Exclusive "or" HL and DE into HL
|
ccxor:
|
ccxor:
|
ld a,l
|
ld a,l
|
xor e
|
xor e
|
ld l,a
|
ld l,a
|
ld a,h
|
ld a,h
|
xor d
|
xor d
|
ld h,a
|
ld h,a
|
ret
|
ret
|
;"And" HL and DE into HL
|
;"And" HL and DE into HL
|
ccand:
|
ccand:
|
ld a,l
|
ld a,l
|
and e
|
and e
|
ld l,a
|
ld l,a
|
ld a,h
|
ld a,h
|
and d
|
and d
|
ld h,a
|
ld h,a
|
ret
|
ret
|
;Test if HL = DE and set HL = 1 if true else 0
|
;Test if HL = DE and set HL = 1 if true else 0
|
cceq:
|
cceq:
|
call cccmp
|
call cccmp
|
ret z
|
ret z
|
dec hl
|
dec hl
|
ret
|
ret
|
;Test if DE ~= HL
|
;Test if DE ~= HL
|
ccne:
|
ccne:
|
call cccmp
|
call cccmp
|
ret nz
|
ret nz
|
dec hl
|
dec hl
|
ret
|
ret
|
;Test if DE > HL (signed)
|
;Test if DE > HL (signed)
|
ccgt:
|
ccgt:
|
ex de,hl
|
ex de,hl
|
call cccmp
|
call cccmp
|
ret c
|
ret c
|
dec hl
|
dec hl
|
ret
|
ret
|
;Test if DE <= HL (signed)
|
;Test if DE <= HL (signed)
|
ccle:
|
ccle:
|
call cccmp
|
call cccmp
|
ret z
|
ret z
|
ret c
|
ret c
|
dec hl
|
dec hl
|
ret
|
ret
|
;Test if DE >= HL (signed)
|
;Test if DE >= HL (signed)
|
ccge:
|
ccge:
|
call cccmp
|
call cccmp
|
ret nc
|
ret nc
|
dec hl
|
dec hl
|
ret
|
ret
|
;Test if DE < HL (signed)
|
;Test if DE < HL (signed)
|
cclt:
|
cclt:
|
call cccmp
|
call cccmp
|
ret c
|
ret c
|
dec hl
|
dec hl
|
ret
|
ret
|
; Signed compare of DE and HL
|
; Signed compare of DE and HL
|
; Performs DE - HL and sets the conditions:
|
; Performs DE - HL and sets the conditions:
|
; Carry reflects sign of difference (set means DE < HL)
|
; Carry reflects sign of difference (set means DE < HL)
|
; Zero/non-zero set according to equality.
|
; Zero/non-zero set according to equality.
|
cccmp:
|
cccmp:
|
ld a,e
|
ld a,e
|
sub l
|
sub l
|
ld e,a
|
ld e,a
|
ld a,d
|
ld a,d
|
sbc h
|
sbc h
|
ld hl,1
|
ld hl,1
|
jp m,cccmp1
|
jp m,cccmp1
|
or e ;"OR" resets carry
|
or e ;"OR" resets carry
|
ret
|
ret
|
cccmp1:
|
cccmp1:
|
or e
|
or e
|
scf ;set carry to signal minus
|
scf ;set carry to signal minus
|
ret
|
ret
|
;Test if DE >= HL (unsigned)
|
;Test if DE >= HL (unsigned)
|
ccuge:
|
ccuge:
|
call ccucmp
|
call ccucmp
|
ret nc
|
ret nc
|
dec hl
|
dec hl
|
ret
|
ret
|
;Test if DE < HL (unsigned)
|
;Test if DE < HL (unsigned)
|
ccult:
|
ccult:
|
call ccucmp
|
call ccucmp
|
ret c
|
ret c
|
dec hl
|
dec hl
|
ret
|
ret
|
;Test if DE > HL (unsigned)
|
;Test if DE > HL (unsigned)
|
ccugt:
|
ccugt:
|
ex de,hl
|
ex de,hl
|
call ccucmp
|
call ccucmp
|
ret c
|
ret c
|
dec hl
|
dec hl
|
ret
|
ret
|
;Test if DE <= HL (unsigned)
|
;Test if DE <= HL (unsigned)
|
ccule:
|
ccule:
|
call ccucmp
|
call ccucmp
|
ret z
|
ret z
|
ret c
|
ret c
|
dec hl
|
dec hl
|
ret
|
ret
|
;Routine to perform unsigned compare
|
;Routine to perform unsigned compare
|
;carry set if DE < HL
|
;carry set if DE < HL
|
;zero/nonzero set accordingly
|
;zero/nonzero set accordingly
|
ccucmp:
|
ccucmp:
|
ld a,d
|
ld a,d
|
cp h
|
cp h
|
jp nz,$+5
|
jp nz,$+5
|
ld a,e
|
ld a,e
|
cp l
|
cp l
|
ld hl,1
|
ld hl,1
|
ret
|
ret
|
;Shift DE arithmetically right by HL and return in HL
|
;Shift DE arithmetically right by HL and return in HL
|
ccasr:
|
ccasr:
|
ex de,hl
|
ex de,hl
|
ld a,h
|
ld a,h
|
rla
|
rla
|
ld a,h
|
ld a,h
|
rra
|
rra
|
ld h,a
|
ld h,a
|
ld a,l
|
ld a,l
|
rra
|
rra
|
ld l,a
|
ld l,a
|
dec e
|
dec e
|
jp nz,ccasr+1
|
jp nz,ccasr+1
|
ret
|
ret
|
;Shift DE arithmetically left by HL and return in HL
|
;Shift DE arithmetically left by HL and return in HL
|
ccasl:
|
ccasl:
|
ex de,hl
|
ex de,hl
|
add hl,hl
|
add hl,hl
|
dec e
|
dec e
|
jp nz,ccasl+1
|
jp nz,ccasl+1
|
ret
|
ret
|
;Subtract HL from DE and return in HL
|
;Subtract HL from DE and return in HL
|
ccsub:
|
ccsub:
|
ld a,e
|
ld a,e
|
sub l
|
sub l
|
ld l,a
|
ld l,a
|
ld a,d
|
ld a,d
|
sbc h
|
sbc h
|
ld h,a
|
ld h,a
|
ret
|
ret
|
;Form the two's complement of HL
|
;Form the two's complement of HL
|
ccneg:
|
ccneg:
|
call cccom
|
call cccom
|
inc hl
|
inc hl
|
ret
|
ret
|
;Form the one's complement of HL
|
;Form the one's complement of HL
|
cccom:
|
cccom:
|
ld a,h
|
ld a,h
|
cpl
|
cpl
|
ld h,a
|
ld h,a
|
ld a,l
|
ld a,l
|
cpl
|
cpl
|
ld l,a
|
ld l,a
|
ret
|
ret
|
;Multiply DE by HL and return in HL
|
;Multiply DE by HL and return in HL
|
ccmult:
|
ccmult:
|
ld b,h
|
ld b,h
|
ld c,l
|
ld c,l
|
ld hl,0
|
ld hl,0
|
ccmult1:
|
ccmult1:
|
ld a,c
|
ld a,c
|
rrca
|
rrca
|
jp nc,$+4
|
jp nc,$+4
|
add hl,de
|
add hl,de
|
xor a
|
xor a
|
ld a,b
|
ld a,b
|
rra
|
rra
|
ld b,a
|
ld b,a
|
ld a,c
|
ld a,c
|
rra
|
rra
|
ld c,a
|
ld c,a
|
or b
|
or b
|
ret z
|
ret z
|
xor a
|
xor a
|
ld a,e
|
ld a,e
|
rla
|
rla
|
ld e,a
|
ld e,a
|
ld a,d
|
ld a,d
|
rla
|
rla
|
ld d,a
|
ld d,a
|
or e
|
or e
|
ret z
|
ret z
|
jp ccmult1
|
jp ccmult1
|
;Divide DE by HL and return quotient in HL, remainder in DE
|
;Divide DE by HL and return quotient in HL, remainder in DE
|
ccdiv:
|
ccdiv:
|
ld b,h
|
ld b,h
|
ld c,l
|
ld c,l
|
ld a,d
|
ld a,d
|
xor b
|
xor b
|
push af
|
push af
|
ld a,d
|
ld a,d
|
or a
|
or a
|
call m,ccdeneg
|
call m,ccdeneg
|
ld a,b
|
ld a,b
|
or a
|
or a
|
call m,ccbcneg
|
call m,ccbcneg
|
ld a,16
|
ld a,16
|
push af
|
push af
|
ex de,hl
|
ex de,hl
|
ld de,0
|
ld de,0
|
ccdiv1:
|
ccdiv1:
|
add hl,hl
|
add hl,hl
|
call ccrdel
|
call ccrdel
|
jp z,ccdiv2
|
jp z,ccdiv2
|
call cccmpbcde
|
call cccmpbcde
|
jp m,ccdiv2
|
jp m,ccdiv2
|
ld a,l
|
ld a,l
|
or 1
|
or 1
|
ld l,a
|
ld l,a
|
ld a,e
|
ld a,e
|
sub c
|
sub c
|
ld e,a
|
ld e,a
|
ld a,d
|
ld a,d
|
sbc b
|
sbc b
|
ld d,a
|
ld d,a
|
ccdiv2:
|
ccdiv2:
|
pop af
|
pop af
|
dec a
|
dec a
|
jp z,ccdiv3
|
jp z,ccdiv3
|
push af
|
push af
|
jp ccdiv1
|
jp ccdiv1
|
ccdiv3:
|
ccdiv3:
|
pop af
|
pop af
|
ret p
|
ret p
|
call ccdeneg
|
call ccdeneg
|
ex de,hl
|
ex de,hl
|
call ccdeneg
|
call ccdeneg
|
ex de,hl
|
ex de,hl
|
ret
|
ret
|
ccdeneg:
|
ccdeneg:
|
ld a,d
|
ld a,d
|
cpl
|
cpl
|
ld d,a
|
ld d,a
|
ld a,e
|
ld a,e
|
cpl
|
cpl
|
ld e,a
|
ld e,a
|
inc de
|
inc de
|
ret
|
ret
|
ccbcneg:
|
ccbcneg:
|
ld a,b
|
ld a,b
|
cpl
|
cpl
|
ld b,a
|
ld b,a
|
ld a,c
|
ld a,c
|
cpl
|
cpl
|
ld c,a
|
ld c,a
|
inc bc
|
inc bc
|
ret
|
ret
|
ccrdel:
|
ccrdel:
|
ld a,e
|
ld a,e
|
rla
|
rla
|
ld e,a
|
ld e,a
|
ld a,d
|
ld a,d
|
rla
|
rla
|
ld d,a
|
ld d,a
|
or e
|
or e
|
ret
|
ret
|
cccmpbcde:
|
cccmpbcde:
|
ld a,e
|
ld a,e
|
sub c
|
sub c
|
ld a,d
|
ld a,d
|
sbc b
|
sbc b
|
ret
|
ret
|
|
|
#endasm
|
#endasm
|
|
|