2020/c64/math.inc

170 lines
2.4 KiB
PHP

// vim: filetype=kickass
.macro i8_div5_a() {
sta zp_temp
lsr
adc #13
adc zp_temp
ror
lsr
lsr
adc zp_temp
ror
adc zp_temp
ror
lsr
lsr
}
.macro i16_div8(lo, hi) {
lsr cursor_pointer_hi
ror cursor_pointer_lo
lsr cursor_pointer_hi
ror cursor_pointer_lo
lsr cursor_pointer_hi
ror cursor_pointer_lo
}
//
// u16_div10
//
// Divides an unsigned 16-bit integer by 10.
//
// d = n * (1 / 10)
// = floor(n * floor(65536 / 10) / 65536)
// = floor(n * 6553 / 65536)
// = floor(n * 6553 >> 16)
//
// Input:
// - $03..$04 - 16-bit unsigned value
// - Generated multiplication table
//
// Output:
// - $24..$25 - 16-bit unsigned result
//
// Destroys: a, x, $22..$25, $fd, $fe
//
.macro u16_div10() {
lda #$19
sta $fe
lda #$99
sta $fd
sec
jsr multiply_16bit_unsigned
}
.macro i8_mul5_a() {
sta zp_temp
asl
asl
clc
adc zp_temp
}
// Destroys: lo, hi
.macro i16_mul2(lo, hi) {
asl lo
rol hi
}
// Destroys: lo, hi
.macro i16_mul4(lo, hi) {
i16_mul2(lo, hi)
i16_mul2(lo, hi)
}
// Destroys: lo, hi
.macro i16_mul8(lo, hi) {
i16_mul4(lo, hi)
i16_mul2(lo, hi)
}
// Destroys: a, lo, hi, $fd, $fe
.macro i16_mul10(lo, hi) {
i16_mul2(lo, hi)
lda lo
sta $fd
lda hi
sta $fe
i16_mul4(lo, hi)
i16_i16_add(lo, hi, $fd, $fe)
}
// Destroys: a, dst_lo, dst_hi
.macro i16_i8_add_a(dst_lo, dst_hi) {
clc
adc dst_lo
sta dst_lo
bcc !cc+
inc dst_hi
!cc:
}
// Destroys: a, dst_lo, dst_hi
.macro i16_i16_add(dst_lo, dst_hi, src_lo, src_hi) {
clc
lda dst_lo
adc src_lo
sta dst_lo
lda dst_hi
adc src_hi
sta dst_hi
}
// Destroys: a, dst_lo, dst_hi
.macro i16_inc(lo, hi) {
lda #1
i16_i8_add_a(lo, hi)
}
.macro i16_i16_sub_imm(dst_lo, dst_hi, src_lo, src_hi, imm) {
sec
lda src_lo
sbc #<imm
sta dst_lo
lda src_hi
sbc #>imm
sta dst_hi
}
// Destroys: a
.macro i16_i16_sub(dst_lo, dst_hi, a_lo, a_hi, b_lo, b_hi) {
sec
lda a_lo
sbc b_lo
sta dst_lo
lda a_hi
sbc b_hi
sta dst_hi
}
.macro i16_imm_i16_sub(dst_lo, dst_hi, imm, src_lo, src_hi) {
lda src_lo
eor #$ff
sec
adc #<imm
sta dst_lo
lda src_hi
eor #$ff
adc #>imm
sta dst_hi
}
.macro i16_i16_cmp_bne(a_lo, a_hi, b_lo, b_hi, target) {
lda a_lo
cmp b_lo
bne target
lda a_hi
cmp b_hi
bne target
}