AppleIIAsm-Collection/disks/disk3_math/T.RANDB
nathanriggs 16c1731e54 Strings 0.2.0 update
- bugfixes
- implemented required library
- commenting updates
- .min of every routine
2018-12-22 20:35:05 -05:00

192 lines
3.8 KiB
Plaintext

*
*``````````````````````````````*
* RANDB :: RANDOM BYTE *
*- -*
* RETURNS A RANDOM BYTE VALUE *
* BETWEEN THE HIGH AND LOW *
* BOUNDS PASSED TO ROUTINE. *
* NOTE THAT THIS ONLY WORKS *
* WITH UNSIGNED VALUES. *
*- -*
* CLOBBERS: *
* *
* FLAGS: ????---- REG: AXYM *
*- -*
* CYCLES: ??? *
* SIZE: *
*- -*
* USAGE: *
* *
* LDA #1 ; LOW BOUND *
* PHA *
* LDA #100 ; HIGH *
* PHA *
* JSR RANDB *
*- -*
* ENTRY *
* *
* TOP OF STACK *
* *
* LOW BYTE OF RETURN ADDRESS *
* HI BYTE OF RETURN ADDRESS *
*- -*
* EXIT *
* *
* TOP OF STACK *
* *
* LOW BYTE OF RETURN ADDRESS *
* HI BYTE OF RETURN ADDRESS *
* *
* .Y = COUNTER; TRASH *
* .X = COUNTER; TRASH *
* .A = LOW BYTE OF RET ADDR *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
RANDB
*
** GET RETURN ADDRESS
*
PLA
STA RETADR
PLA
STA RETADR+1
*
** GET PARAMETERS
*
PLA
STA :NEWMAX
PLA
STA :NEWMIN
*
** GET OLDMIN,OLDMAX,OLDVAL
*
LDA #1
STA :OLDMIN
LDA #255
STA :OLDMAX
JSR RAND8 ; RANDOM BYTE 0..255
STA :OLDVAL
*
** NEWVALUE = (((OLDVAL-NEWMIN) * (NEWMAX-NEWMIN) /
** (OLDMAX-OLDMIN)) + NEWMIN
*
** OR
*
** OLDRANGE = (OLDMAX-OLDMIN)
** NEWRANGE = (NEWMAX - NEWMIN)
** NEWVAL = (((OLDVAL-OLDMIN) * NEWRANGE) / OLDRANGE) + NEWMIN
*
** GET OLDRANGE: OLDMAX - OLDMIN
*
LDA :OLDMAX
SEC
SBC :OLDMIN
STA :OLDRANGE
*
** NOW GET NEWRANGE: NEWMAX - NEWMIN
*
LDA :NEWMAX
SEC
SBC :NEWMIN
STA :NEWRANGE
*
** NOW GET VALRANGE: OLDVAL - OLDMIN
*
LDA :OLDVAL
SEC
SBC :OLDMIN
STA :VALRANGE
*
** GET MULRANGE: VALRANGE * NEWRANGE
*
LDA #00
TAY
STY :NUM1HI
BEQ :ENTLP
*
:DOADD
CLC
ADC :VALRANGE
TAX
*
TYA
ADC :NUM1HI
TAY
TXA
*
:MLP
ASL :VALRANGE
ROL :NUM1HI
:ENTLP
LSR :NEWRANGE
BCS :DOADD
BNE :MLP
*
STA :MULRANGE
STY :MULRANGE+1
*
** NOW GET DIVRANGE: MULRANGE / OLDRANGE
*
:DIVIDE
LDA #0
STA :REMAIN
STA :REMAIN+1
LDX #16
*
:DIVLP
ASL :MULRANGE
ROL :MULRANGE+1
ROL :REMAIN
ROL :REMAIN+1
LDA :REMAIN
SEC
SBC :OLDRANGE
TAY
LDA :REMAIN+1
SBC :OLDRANGE+1
BCC :SKIP
*
STA :REMAIN+1
STY :REMAIN
INC :DIVRANGE
*
:SKIP DEX
BNE :DIVLP
*
** NOW ADD NEWMIN TO DIVRANGE
*
LDA :DIVRANGE ; LO BYTE ONLY
CLC
ADC :NEWMIN
STA :NEWVAL
*
** RESTORE RETURN ADDRESS
*
LDA RETADR+1
PHA
LDA RETADR
PHA
*
LDA :NEWVAL ; RETURN # IN A
STA RETURN
LDX #1
STX RETLEN
RTS
*
** DATA
*
:OLDRANGE DS 2
:NEWRANGE DS 2
:MULRANGE DS 2
:DIVRANGE DS 2
:VALRANGE DS 2
:OLDVAL DS 2
:NEWVAL DS 2
:OLDMIN DS 2
:NEWMIN DS 2
:OLDMAX DS 2
:NEWMAX DS 2
:NUM1HI DS 2
:REMAIN DS 2
*