;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;Range'ed RNG, By GreenHammerBro. ; ;This routine allows you to have custom RNG within a specified range rather than 0~255. ;(but smaller, due to a multiplication limit of 65535; it uses a cross-multiply) The output ;minimum is always 0. ; ;If you want to move the range, you would have to add/subtract from the result number ;(Example: from 4 to 7 inclusively, you would load 3 into A, and after the subroutine, add ;A by 4). ; ;Input: ;$00 = (8-bit) the maximum value output. ; ;Output: ;A = (8-bit) output from the RNG ;Example: ; LDA #$02 ; JSR RNG ; BEQ .Possibility1 ;>#$00 ; CMP #$01 ; BEQ .Possibility1 ;>#$01 ; CMP #$02 ; BEQ .Possibility1 ;>#$02 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; RNG: if !SA1 == 0 STZ $01 ;\No high bytes. STZ $03 ;/ JSL $01ACF9 ;>Execute random LDA $148D ;\Random nuber... STA $02 ;/ REP #$20 BEQ .LoadZero ;>Fix multiplication bug with the 2nd multiplicand JSR Multiply ;...Times the maximum number the user wants. BRA .Skip .LoadZero LDA #$0000 .Skip STA $00 ;\Result divide by 255 LDA #$00FF ;| STA $02 ;| JSR MathDiv ;/ REP #$30 LDX $00 ;>Load result ; LDA #$00FF ; LSR LDA #$007F ;\If 1/2 of denomator is bigger than remainder CMP $02 ;|(remainder smaller than 1/2), then don't round BCS .NoRoundUp ;/ INX .NoRoundUp TXA SEP #$30 else LDA #$00 ;\Multiply mode STA $2250 ;/ JSL $01ACF9 ;>Execute random LDA $148D+!Base2 ;\Random number... STA $2251 ;| STZ $2252 ;/ LDA $00 ;\...Times the maximum number STA $2253 ;|the user wants... STZ $2254 ;/ NOP ;\Wait till calculation is done. BRA $00 ;/ REP #$20 ;\In case if you put a number into LDA $2306 ;|$2250 would change $2306. STA $00 ;|The result of multiplication SEP #$20 ;/ LDA #$01 ;\Divide mode STA $2250 ;/ REP #$30 ; LDA $00 ;\...Divide by 255... STA $2251 ;| LDA #$00FF ;| STA $2253 ;/ NOP ;\Wait till calculation is done. BRA $00 ;/ LDX $2306 ;>Division Result LDA $2253 ; LSR ;\If 1/2 of denomator is bigger than remainder CMP $2308 ;|(remainder smaller than 1/2), then don't round BCS .NoRoundUp ;/ INX ;>Round 1/2 up. .NoRoundUp endif TXA ;\Shared for normal and SA-1. SEP #$30 ;/ RTS if !SA1 == 0 Multiply: ;\multiplication subroutine STA $04 ;| .Loop LSR $02 ;| BEQ .end ;| BCC .Next ;| CLC ;| ADC $00 ;| .Next ASL $00 ;| BRA .Loop ;| .end ;| CLC ;| ADC $00 ;| SEC ;|\remove 1-extra multiplication. SBC $04 ;// RTS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 16bit / 16bit Division ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Arguments ; $00-$01 : Dividend ; $02-$03 : Divisor ; Return values ; $00-$01 : Quotient ; $02-$03 : Remainder ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; MathDiv: REP #$20 ASL $00 LDY #$0F LDA.w #$0000 - ROL A CMP $02 BCC + SBC $02 + ROL $00 DEY BPL - STA $02 SEP #$20 RTS endif