1
0
mirror of https://github.com/KarolS/millfork.git synced 2025-01-07 16:29:59 +00:00
millfork/include/zp_reg.mfk

132 lines
2.2 KiB
Plaintext
Raw Normal View History

#if not(ARCH_6502)
#warn zp_reg module should be used only on 6502-compatible targets
#endif
2018-12-19 00:09:27 +00:00
noinline asm byte __mul_u8u8u8() {
? LDA #0
2018-06-18 20:40:32 +00:00
? JMP __mul_u8u8u8_start
__mul_u8u8u8_add:
? CLC
2019-06-28 15:57:26 +00:00
? ADC __reg
2018-06-18 20:40:32 +00:00
__mul_u8u8u8_loop:
2019-06-28 15:57:26 +00:00
? ASL __reg
2018-06-18 20:40:32 +00:00
__mul_u8u8u8_start:
2019-06-28 15:57:26 +00:00
? LSR __reg+1
? BCS __mul_u8u8u8_add
? BNE __mul_u8u8u8_loop
? RTS
}
2018-12-14 21:50:20 +00:00
2019-06-05 23:17:34 +00:00
// divide __reg[0]/__reg[1]
noinline asm byte __mod_u8u8u8u8() {
? LDA #0
? LDX #7
? CLC
__divmod_u8u8u8u8_start:
2019-06-28 15:57:26 +00:00
? ROL __reg
2019-06-05 23:17:34 +00:00
? ROL
2019-06-28 15:57:26 +00:00
? CMP __reg+1
2019-06-05 23:17:34 +00:00
? BCC __divmod_u8u8u8u8_skip
2019-06-28 15:57:26 +00:00
? SBC __reg+1
2019-06-05 23:17:34 +00:00
__divmod_u8u8u8u8_skip:
? DEX
? BPL __divmod_u8u8u8u8_start
2019-06-28 15:57:26 +00:00
? ROL __reg
2019-06-05 23:17:34 +00:00
? RTS
}
asm byte __div_u8u8u8u8() {
? JSR __mod_u8u8u8u8
2019-06-28 15:57:26 +00:00
? LDA __reg
2019-06-05 23:17:34 +00:00
? RTS
}
2018-12-14 21:50:20 +00:00
#if ZPREG_SIZE >= 3
2018-12-19 00:09:27 +00:00
noinline asm word __mul_u16u8u16() {
2018-12-14 21:50:20 +00:00
? LDA #0
? TAX
? JMP __mul_u16u8u16_start
__mul_u16u8u16_add:
? CLC
2019-06-28 15:57:26 +00:00
? ADC __reg
2018-12-14 21:50:20 +00:00
? TAY
? TXA
2019-06-28 15:57:26 +00:00
? ADC __reg+1
2018-12-14 21:50:20 +00:00
? TAX
? TYA
__mul_u16u8u16_loop:
2019-06-28 15:57:26 +00:00
? ASL __reg
? ROL __reg+1
2018-12-14 21:50:20 +00:00
__mul_u16u8u16_start:
2019-06-28 15:57:26 +00:00
? LSR __reg+2
2018-12-14 21:50:20 +00:00
? BCS __mul_u16u8u16_add
? BNE __mul_u16u8u16_loop
? RTS
}
2019-06-06 11:06:30 +00:00
// divide (__reg[1]:__reg[0])/__reg[2]
// remainder in A, quotient in (__reg[1]:__reg[0])
2019-06-06 11:06:30 +00:00
noinline asm byte __mod_u16u8u16u8() {
? LDX #15
? CLC
// TODO: decide when
#if OPTIMIZE_FOR_SPEED
? LDA __reg+2
? BMI __divmod_u16u8u16u8_variant2
? LDA #0
__divmod_u16u8u16u8_start: // for divisors <= 127
2019-06-28 15:57:26 +00:00
? ROL __reg
? ROL __reg+1
2019-06-06 11:06:30 +00:00
? ROL
2019-06-28 15:57:26 +00:00
? CMP __reg+2
2019-06-06 11:06:30 +00:00
? BCC __divmod_u16u8u16u8_skip
2019-06-28 15:57:26 +00:00
? SBC __reg+2
2019-06-06 11:06:30 +00:00
__divmod_u16u8u16u8_skip:
? DEX
? BPL __divmod_u16u8u16u8_start
2019-06-28 15:57:26 +00:00
? ROL __reg
? ROL __reg+1
2019-06-06 11:06:30 +00:00
? RTS
#endif
__divmod_u16u8u16u8_variant2:
? LDA #0
__divmod_u16u8u16u8_start2: // for divisors >= 128
? ROL __reg
? ROL __reg+1
? ROL
? BCS __divmod_u16u8u16u8_sub2
? CMP __reg+2
? BCC __divmod_u16u8u16u8_skip2
__divmod_u16u8u16u8_sub2:
? SBC __reg+2
? SEC
__divmod_u16u8u16u8_skip2:
? DEX
? BPL __divmod_u16u8u16u8_start2
? ROL __reg
? ROL __reg+1
? RTS
2019-06-06 11:06:30 +00:00
}
asm word __div_u16u8u16u8() {
? JSR __mod_u16u8u16u8
2019-06-28 15:57:26 +00:00
? LDA __reg
? LDX __reg+1
2019-06-06 11:06:30 +00:00
? RTS
2019-06-06 11:06:30 +00:00
}
2018-12-14 21:50:20 +00:00
#endif
2019-07-26 22:58:10 +00:00
#if ZPREG_SIZE >= 4
noinline asm word call(word ax) {
JMP ((__reg + 2))
}
#endif