Rnd

From Maze

Revision as of 14:32, 28 April 2010 by NoahVawter (Talk | contribs)
Jump to: navigation, search

this page is for collecting information about the random function (RND) as used in the commodore 64 program 10 print chr$(79+int(rnd(0)*2));:goto 10

some useful links:

C64-specific

Brief descrip of command

disassembly of C64 ROMs

C64 prog. ref. manual (zero page addresses)


Dartmouth BASIC

Dartmouth BASIC code


Survey of Random Number algorithms

specific LCG algorithms and quality

LCG

classic random algorithms, e.g. in Fortran

random in Java

random in python


Notes

Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.

(Java) The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)


C64 Rnd(0) Disassembly

(most disassembly from http://www.ffd2.com/fridge/docs/c64-diss.html)

float numbers for RND

E08D .BY $98,$35,$44,$7A,$00 E092 .BY $68,$28,$B1,$46,$00


(http://www.devili.iki.fi/Computers/Commodore/C64/Programmers_Reference/page_iii.html)

FACEXP 0061 97 Floating-Point Accumulator #1
Exponent
FACHO 0062-0065 98-101 Floating Accum. #1
Mantissa
FACSGN 0066 102 Floating Accum. #1
Sign
FACOV 0070 112 Floating Accum. #1. Low-Order (Rounding)


RND function
With RND(0) the C64 generates the random number with the internal clock. (http
//www.c64-wiki.com/index.php/RND)


E097 20 2B BC JSR $BC2B ; get sign of accumulator E09A 30 37 BMI $E0D3 ; do negative E09C D0 20 BNE $E0BE ; positive

rnd(0)

E09E 20 F3 FF JSR $FFF3 ; read base address of I/O device E0A1 86 22 STX $22 ; INDEX 0022-0025 34-37 Utility Pointer Area E0A3 84 23 STY $23 E0A5 A0 04 LDY #$04 E0A7 B1 22 LDA ($22),Y E0A9 85 62 STA $62 E0AB C8 INY E0AC B1 22 LDA ($22),Y E0AE 85 64 STA $64 E0B0 A0 08 LDY #$08 E0B2 B1 22 LDA ($22),Y E0B4 85 63 STA $63 E0B6 C8 INY E0B7 B1 22 LDA ($22),Y E0B9 85 65 STA $65 E0BB 4C E3 E0 JMP $E0E3

positive number, e.g. rnd(1)

E0BE A9 8B LDA #$8B ; E0C0 A0 00 LDY #$00 E0C2 20 A2 BB JSR $BBA2 ; retrieve float accu from index XY

E0C5 A9 8D LDA #$8D ; load generator seeds E0C7 A0 E0 LDY #$E0 E0C9 20 28 BA JSR $BA28 ; move float indexed by AY into second float accu

E0CC A9 92 LDA #$92 ; load more generator seeds E0CE A0 E0 LDY #$E0 E0D0 20 67 B8 JSR $B867 ; add float indexed by AY to float accu

negative operand, e.g. rnd(-1)

E0D3 A6 65 LDX $65 ; swap $65 and $62 E0D5 A5 62 LDA $62 E0D7 85 65 STA $65 E0D9 86 62 STX $62

E0DB A6 63 LDX $63 ; swap $63 and $64 E0DD A5 64 LDA $64 E0DF 85 63 STA $63 E0E1 86 64 STX $64

common entry point for rnd

E0E3 A9 00 LDA #$00 ; clear sign of float acc #1 E0E5 85 66 STA $66

E0E7 A5 61 LDA $61 ; more float acc #1 stuff (exponent and rounding) E0E9 85 70 STA $70

E0EB A9 80 LDA #$80 E0ED 85 61 STA $61 E0EF 20 D7 B8 JSR $B8D7 ; a floating-point routine


E0F2 A2 8B LDX #$8B E0F4 A0 00 LDY #$00 E0F6 4C D4 BB JMP $BBD4 ; store float accu in index XY




add float indexed by AY to float accu

B867 20 8C BA JSR $BA8C


a floating-point routine

B8D7 A0 00 LDY #$00 B8D9 98 TYA B8DA 18 CLC

B8DB A6 62 LDX $62 B8DD D0 4A BNE $B929

B8DF A6 63 LDX $63 B8E1 86 62 STX $62

B8E3 A6 64 LDX $64 B8E5 86 63 STX $63

B8E7 A6 65 LDX $65 B8E9 86 64 STX $64

B8EB A6 70 LDX $70 B8ED 86 65 STX $65

B8EF 84 70 STY $70 B8F1 69 08 ADC #$08 B8F3 C9 20 CMP #$20 B8F5 D0 E4 BNE $B8DB

B8F7 A9 00 LDA #$00 B8F9 85 61 STA $61 B8FB 85 66 STA $66

B8FD 60 RTS


BA28 20 8C BA JSR $BA8C

move float indexed by AY into second float accu

BA8C 85 22 STA $22 BA8E 84 23 STY $23 BA90 A0 04 LDY #$04 BA92 B1 22 LDA ($22),Y BA94 85 6D STA $6D BA96 88 DEY BA97 B1 22 LDA ($22),Y BA99 85 6C STA $6C BA9B 88 DEY BA9C B1 22 LDA ($22),Y BA9E 85 6B STA $6B BAA0 88 DEY BAA1 B1 22 LDA ($22),Y BAA3 85 6E STA $6E BAA5 45 66 EOR $66 BAA7 85 6F STA $6F BAA9 A5 6E LDA $6E BAAB 09 80 ORA #$80 BAAD 85 6A STA $6A BAAF 88 DEY BAB0 B1 22 LDA ($22),Y BAB2 85 69 STA $69 BAB4 A5 61 LDA $61 BAB6 60 RTS


retrieve float accu from index XY

BBA2 85 22 STA $22 BBA4 84 23 STY $23

BBA6 A0 04 LDY #$04 BBA8 B1 22 LDA ($22),Y BBAA 85 65 STA $65 BBAC 88 DEY BBAD B1 22 LDA ($22),Y BBAF 85 64 STA $64 BBB1 88 DEY BBB2 B1 22 LDA ($22),Y BBB4 85 63 STA $63 BBB6 88 DEY BBB7 B1 22 LDA ($22),Y BBB9 85 66 STA $66 BBBB 09 80 ORA #$80 BBBD 85 62 STA $62 BBBF 88 DEY BBC0 B1 22 LDA ($22),Y BBC2 85 61 STA $61 BBC4 84 70 STY $70 BBC6 60 RTS



store float accu in index XY

BBD4 20 1B BC JSR $BC1B ; round float accu according to guard bit BBD7 86 22 STX $22 BBD9 84 23 STY $23

BBDB A0 04 LDY #$04 BBDD A5 65 LDA $65 BBDF 91 22 STA ($22),Y BBE1 88 DEY BBE2 A5 64 LDA $64 BBE4 91 22 STA ($22),Y BBE6 88 DEY BBE7 A5 63 LDA $63 BBE9 91 22 STA ($22),Y BBEB 88 DEY BBEC A5 66 LDA $66 BBEE 09 7F ORA #$7F BBF0 25 62 AND $62 BBF2 91 22 STA ($22),Y BBF4 88 DEY BBF5 A5 61 LDA $61 BBF7 91 22 STA ($22),Y BBF9 84 70 STY $70 BBFB 60 RTS


round float accu according to guard bit

BC1B A5 61 LDA $61 BC1D F0 FB BEQ $BC1A BC1F 06 70 ASL $70 BC21 90 F7 BCC $BC1A

BC23 20 6F B9 JSR $B96F

BC26 D0 F2 BNE $BC1A BC28 4C 38 B9 JMP $B938


get sign of float accu in A

BC2B A5 61 LDA $61 BC2D F0 09 BEQ $BC38 BC2F A5 66 LDA $66

BC31 2A ROL

BC32 A9 FF LDA #$FF BC34 B0 02 BCS $BC38 BC36 A9 01 LDA #$01 BC38 60 RTS


FFF3 4C 00 E5 JMP $E500 ; read base address of I/O devices


read base address of I/O device into XY
D000-DFFF 53248-57343 Input/Output Devices and Color RAM or Character Generator ROM or RAM - 4096 Bytes
DC00 56320  Data Port A (Keyboard, Joystick, Paddles, Light-Pen) 7-0 Write Keyboard Column Values for Keyboard Scan 7-6 Read Paddles on Port A / B (01 = Port A, 10 = Port B) 4 Joystick A Fire Button
1 = Fire 3-2 Paddle Fire Buttons 3-0 Joystick A Direction (0-15)


E500 A2 00 LDX #$00 ; low DC00 E502 A0 DC LDY #$DC ; high DC00 E504 60 RTS

Personal tools