2018-09-15 14:21:05 +00:00
|
|
|
; Prog8 internal library routines - always included by the compiler
|
2017-12-30 12:34:52 +00:00
|
|
|
;
|
2018-01-08 02:31:23 +00:00
|
|
|
; Written by Irmen de Jong (irmen@razorvine.net) - license: GNU GPL 3.0
|
2018-01-13 13:17:18 +00:00
|
|
|
;
|
2017-12-30 12:34:52 +00:00
|
|
|
; indent format: TABS, size=8
|
|
|
|
|
|
|
|
|
2018-09-15 14:21:05 +00:00
|
|
|
~ prog8_lib {
|
2017-12-30 19:03:19 +00:00
|
|
|
; note: the following ZP scratch registers must be the same as in c64lib
|
2018-10-21 21:03:15 +00:00
|
|
|
memory ubyte SCRATCH_ZP1 = $02 ; scratch register #1 in ZP
|
|
|
|
memory ubyte SCRATCH_ZP2 = $03 ; scratch register #2 in ZP
|
|
|
|
memory uword SCRATCH_ZPWORD1 = $fb ; scratch word in ZP ($fb/$fc)
|
|
|
|
memory uword SCRATCH_ZPWORD2 = $fd ; scratch word in ZP ($fd/$fe)
|
2018-12-06 00:26:38 +00:00
|
|
|
const uword ESTACK_LO = $ce00
|
|
|
|
const uword ESTACK_HI = $cf00
|
2017-12-30 12:34:52 +00:00
|
|
|
|
|
|
|
|
2018-08-12 23:30:33 +00:00
|
|
|
%asm {{
|
2017-12-30 12:34:52 +00:00
|
|
|
|
2018-10-21 21:03:15 +00:00
|
|
|
; 16-bit rotate right (as opposed to the 6502's usual 17-bit rotate with carry)
|
|
|
|
; the word is placed in SCRATCH_ZPWORD1
|
|
|
|
ror2_word
|
|
|
|
lsr SCRATCH_ZPWORD1+1
|
|
|
|
ror SCRATCH_ZPWORD1
|
2018-02-03 00:44:14 +00:00
|
|
|
bcc +
|
2018-10-21 21:03:15 +00:00
|
|
|
lda SCRATCH_ZPWORD1+1
|
|
|
|
ora #$80
|
|
|
|
sta SCRATCH_ZPWORD1+1
|
2018-02-04 21:00:08 +00:00
|
|
|
+ rts
|
|
|
|
|
2018-10-23 23:39:52 +00:00
|
|
|
|
|
|
|
|
2018-12-06 00:26:38 +00:00
|
|
|
; @todo: implement stubs!
|
2018-10-25 21:17:10 +00:00
|
|
|
; @todo: move float operations to their own library (only included when floats are enabled)
|
2018-10-23 23:39:52 +00:00
|
|
|
|
|
|
|
ub2float
|
|
|
|
rts
|
|
|
|
|
2018-10-26 22:34:42 +00:00
|
|
|
b2float
|
|
|
|
rts
|
|
|
|
|
2018-10-23 23:39:52 +00:00
|
|
|
uw2float
|
|
|
|
rts
|
|
|
|
|
2018-10-26 22:34:42 +00:00
|
|
|
w2float
|
|
|
|
rts
|
|
|
|
|
2018-10-23 23:39:52 +00:00
|
|
|
push_float
|
2018-12-06 00:26:38 +00:00
|
|
|
; ---- push mflpt5 in A/Y onto stack
|
|
|
|
; (taking 3 stack positions = 6 bytes of which 1 is padding)
|
|
|
|
sta SCRATCH_ZPWORD1
|
|
|
|
sty SCRATCH_ZPWORD1+1
|
|
|
|
ldy #0
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta ESTACK_LO,x
|
|
|
|
iny
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta ESTACK_HI,x
|
|
|
|
dex
|
|
|
|
iny
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta ESTACK_LO,x
|
|
|
|
iny
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta ESTACK_HI,x
|
|
|
|
dex
|
|
|
|
iny
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta ESTACK_LO,x
|
|
|
|
dex
|
2018-10-23 23:39:52 +00:00
|
|
|
rts
|
2018-12-02 22:53:09 +00:00
|
|
|
|
2018-12-06 00:26:38 +00:00
|
|
|
|
2018-12-02 22:53:09 +00:00
|
|
|
push_float_from_indexed_var
|
|
|
|
rts
|
2018-10-23 23:39:52 +00:00
|
|
|
|
2018-12-06 00:26:38 +00:00
|
|
|
pop_float
|
|
|
|
; ---- pops mflpt5 from stack to memory A/Y @TODO CHECK ORDER OF POPS
|
|
|
|
; (frees 3 stack positions = 6 bytes of which 1 is padding)
|
|
|
|
sta SCRATCH_ZPWORD1
|
|
|
|
sty SCRATCH_ZPWORD1+1
|
|
|
|
ldy #4
|
|
|
|
inx
|
|
|
|
lda ESTACK_LO,x
|
|
|
|
sta SCRATCH_ZPWORD1,y
|
|
|
|
dey
|
|
|
|
inx
|
|
|
|
lda ESTACK_LO,x
|
|
|
|
sta SCRATCH_ZPWORD1,y
|
|
|
|
dey
|
|
|
|
lda ESTACK_HI,x
|
|
|
|
sta SCRATCH_ZPWORD1,y
|
|
|
|
dey
|
|
|
|
inx
|
|
|
|
lda ESTACK_LO,x
|
|
|
|
sta SCRATCH_ZPWORD1,y
|
|
|
|
lda ESTACK_HI,x
|
|
|
|
dey
|
|
|
|
sta SCRATCH_ZPWORD1,y
|
2018-10-23 23:39:52 +00:00
|
|
|
rts
|
2018-12-02 22:53:09 +00:00
|
|
|
|
|
|
|
pop_float_to_indexed_var
|
|
|
|
rts
|
2018-10-23 23:39:52 +00:00
|
|
|
|
2018-11-20 23:02:02 +00:00
|
|
|
pop_mem_float
|
|
|
|
rts
|
|
|
|
|
2018-10-23 23:39:52 +00:00
|
|
|
copy_float
|
2018-12-06 00:26:38 +00:00
|
|
|
; -- copies the 5 bytes of the mflt value pointed to by SCRATCH_ZPWORD1,
|
|
|
|
; into the 5 bytes pointed to by A/Y. Clobbers Y.
|
|
|
|
sta SCRATCH_ZPWORD2
|
|
|
|
sty SCRATCH_ZPWORD2+1
|
|
|
|
ldy #0
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta SCRATCH_ZPWORD2,y
|
|
|
|
iny
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta SCRATCH_ZPWORD2,y
|
|
|
|
iny
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta SCRATCH_ZPWORD2,y
|
|
|
|
iny
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta SCRATCH_ZPWORD2,y
|
|
|
|
iny
|
|
|
|
lda SCRATCH_ZPWORD1,y
|
|
|
|
sta SCRATCH_ZPWORD2,y
|
2018-10-23 23:39:52 +00:00
|
|
|
rts
|
|
|
|
|
|
|
|
inc_var_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
dec_var_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
div_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
add_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
sub_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
mul_f
|
|
|
|
rts
|
|
|
|
|
2018-10-25 21:17:10 +00:00
|
|
|
neg_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
|
2018-11-20 23:02:02 +00:00
|
|
|
add_w
|
|
|
|
rts ; @todo inline?
|
|
|
|
add_uw
|
|
|
|
rts ; @todo inline?
|
|
|
|
|
|
|
|
sub_w
|
|
|
|
rts ; @todo inline?
|
|
|
|
sub_uw
|
|
|
|
rts ; @todo inline?
|
|
|
|
|
|
|
|
mul_b
|
|
|
|
rts
|
|
|
|
mul_ub
|
|
|
|
rts
|
|
|
|
mul_w
|
|
|
|
rts
|
|
|
|
mul_uw
|
|
|
|
rts
|
|
|
|
|
|
|
|
div_b
|
|
|
|
rts
|
|
|
|
div_ub
|
|
|
|
rts
|
|
|
|
div_w
|
|
|
|
rts
|
|
|
|
div_uw
|
|
|
|
rts
|
|
|
|
|
2018-12-04 21:30:35 +00:00
|
|
|
remainder_b
|
|
|
|
rts
|
|
|
|
remainder_ub
|
|
|
|
rts
|
|
|
|
remainder_w
|
|
|
|
rts
|
|
|
|
remainder_uw
|
|
|
|
rts
|
|
|
|
remainder_f
|
|
|
|
rts
|
2018-11-20 23:02:02 +00:00
|
|
|
|
2018-12-04 21:30:35 +00:00
|
|
|
equal_ub
|
|
|
|
rts
|
|
|
|
|
|
|
|
equal_b
|
|
|
|
rts
|
|
|
|
|
|
|
|
equal_w
|
|
|
|
rts
|
|
|
|
|
|
|
|
equal_uw
|
|
|
|
rts
|
|
|
|
|
|
|
|
equal_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
less_ub
|
|
|
|
rts
|
|
|
|
|
|
|
|
less_b
|
|
|
|
rts
|
|
|
|
|
|
|
|
less_w
|
|
|
|
rts
|
|
|
|
|
|
|
|
less_uw
|
|
|
|
rts
|
|
|
|
|
|
|
|
less_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
lesseq_ub
|
|
|
|
rts
|
|
|
|
|
|
|
|
lesseq_b
|
|
|
|
rts
|
|
|
|
|
|
|
|
lesseq_w
|
|
|
|
rts
|
|
|
|
|
|
|
|
lesseq_uw
|
|
|
|
rts
|
|
|
|
|
|
|
|
lesseq_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
greater_ub
|
|
|
|
rts
|
|
|
|
|
|
|
|
greater_b
|
|
|
|
rts
|
|
|
|
|
|
|
|
greater_w
|
|
|
|
rts
|
|
|
|
|
|
|
|
greater_uw
|
|
|
|
rts
|
|
|
|
|
|
|
|
greater_f
|
|
|
|
rts
|
|
|
|
|
|
|
|
greatereq_ub
|
|
|
|
rts
|
|
|
|
|
|
|
|
greatereq_b
|
|
|
|
rts
|
|
|
|
|
|
|
|
greatereq_w
|
|
|
|
rts
|
|
|
|
|
|
|
|
greatereq_uw
|
|
|
|
rts
|
|
|
|
|
|
|
|
greatereq_f
|
|
|
|
rts
|
|
|
|
|
2018-10-25 21:17:10 +00:00
|
|
|
func_sin
|
|
|
|
rts
|
|
|
|
func_cos
|
|
|
|
rts
|
|
|
|
func_abs
|
|
|
|
rts
|
|
|
|
func_acos
|
|
|
|
rts
|
|
|
|
func_asin
|
|
|
|
rts
|
|
|
|
func_tan
|
|
|
|
rts
|
|
|
|
func_atan
|
|
|
|
rts
|
|
|
|
func_ln
|
|
|
|
rts
|
|
|
|
func_log2
|
|
|
|
rts
|
|
|
|
func_log10
|
|
|
|
rts
|
|
|
|
func_sqrt
|
|
|
|
rts
|
|
|
|
func_rad
|
|
|
|
rts
|
|
|
|
func_deg
|
|
|
|
rts
|
|
|
|
func_round
|
|
|
|
rts
|
|
|
|
func_floor
|
|
|
|
rts
|
|
|
|
func_ceil
|
|
|
|
rts
|
|
|
|
func_max
|
|
|
|
rts
|
|
|
|
func_min
|
|
|
|
rts
|
|
|
|
func_avg
|
|
|
|
rts
|
|
|
|
func_sum
|
|
|
|
rts
|
|
|
|
func_len
|
|
|
|
rts
|
|
|
|
func_any
|
|
|
|
rts
|
|
|
|
func_all
|
|
|
|
rts
|
2018-12-06 00:26:38 +00:00
|
|
|
|
2018-10-25 21:17:10 +00:00
|
|
|
func_rnd
|
2018-12-06 00:26:38 +00:00
|
|
|
; -- put a random ubyte on the estack
|
|
|
|
jsr math.randbyte
|
|
|
|
sta ESTACK_LO,x
|
|
|
|
dex
|
2018-10-25 21:17:10 +00:00
|
|
|
rts
|
2018-12-06 00:26:38 +00:00
|
|
|
|
2018-10-25 21:17:10 +00:00
|
|
|
func_rndw
|
2018-12-06 00:26:38 +00:00
|
|
|
; -- put a random uword on the estack
|
|
|
|
jsr math.randword
|
|
|
|
sta ESTACK_LO,x
|
|
|
|
tya
|
|
|
|
sta ESTACK_HI,x
|
|
|
|
dex
|
2018-10-25 21:17:10 +00:00
|
|
|
rts
|
2018-12-06 00:26:38 +00:00
|
|
|
|
2018-10-25 21:17:10 +00:00
|
|
|
func_rndf
|
2018-12-06 00:26:38 +00:00
|
|
|
; -- put a random floating point value on the stack
|
|
|
|
stx SCRATCH_ZP1
|
|
|
|
lda #39
|
|
|
|
jsr c64.RNDA ; rng into fac1
|
|
|
|
ldx #<_rndf_rnum5
|
|
|
|
ldy #>_rndf_rnum5
|
|
|
|
jsr c64.FTOMEMXY ; fac1 to mem X/Y
|
|
|
|
ldx SCRATCH_ZP1
|
|
|
|
lda #<_rndf_rnum5
|
|
|
|
ldy #>_rndf_rnum5
|
|
|
|
jmp push_float
|
|
|
|
_rndf_rnum5 .fill 5
|
|
|
|
|
|
|
|
|
2018-10-25 21:17:10 +00:00
|
|
|
func_wrd
|
|
|
|
rts
|
|
|
|
func_uwrd
|
|
|
|
rts
|
|
|
|
func_str2byte
|
|
|
|
rts
|
|
|
|
func_str2ubyte
|
|
|
|
rts
|
|
|
|
func_str2word
|
|
|
|
rts
|
|
|
|
func_str2uword
|
|
|
|
rts
|
|
|
|
func_str2float
|
|
|
|
rts
|
|
|
|
|
2018-08-12 23:30:33 +00:00
|
|
|
}}
|
2017-12-30 12:34:52 +00:00
|
|
|
}
|