wiz-lang / wiz

A high-level assembly language for writing homebrew software and games on retro console platforms.
http://wiz-lang.org/
Other
409 stars 40 forks source link

6502 + 65816: math utility functions for arithmetic shift right #57

Open Bananattack opened 5 years ago

Bananattack commented 5 years ago

Right now to do a single-bit arithetic shift right, can cmp(0x80) and then rotate right with carry a >>>>#= 1.

If you want to shift more, you can wrap the code in an inline for, but it's kinda inconvenient. Also, the unrolled shift loop becomes less efficient than other methods after the shift count exceeds 2 places.

(in 6502 asm), this is more efficient on space and cycle time when N is >= 3.

eor #$80
lsr a
lsr a
lsr a
lsr a
clc  ; optional if not rounding
adc #<-(128 >> N)

If there was a way to do this as an inline macro, or an opt-in feature to enable >> (rather than >>> logical right shift) to mean an optimized sequence of shifts to accomplish the result, this would be kind of neat.