News:

NEW GAME and special BOOK (and more!) to come from Côté Gamers! ! Check this topic!

Main Menu

Random number getting WITH timer

Started by Chris!, October 19, 2023, 05:49:01 AM

Previous topic - Next topic

Chris!

So I want to use the "timer" to get a way better looking playfield. But I also want random number generating which apparently can't be done with it. I got to thinking of a work around and I got to thinking of a small snippet of code that I might be able to use to make the numbers psuedo-random:
https://codebase64.org/doku.php?id=base:small_fast_8-bit_prng
The only problem is that it's not written in Odyssey assembly. Could someone please "translate" the following so I can use it in my code?

        lda seed
        beq doEor
         asl
         beq noEor ;if the input was $80, skip the EOR
         bcc noEor
doEor:    eor #$1d
noEor:  sta seed

That's the code. It is supposed to get random numbers between 0-255, which is more than enough for my game. Doesn't this look lovely?
All my Odyssey2 projects, now in one place!
http://www.atari2600land.com/odyssey2.html

TedFoolery

I had to google it to find the source. It's 6502 assembly. Here's the translation to 8048, with the original in comments. Note -- "seed" should be set to some internal ram location. (it's not tested, so good luck):

;lda seed
mov r0,seed
mov a,@r0
;beq doEor
jz doEor
;asl
clr c
rlc
;beq noEor
jz noEor
;bcc noErr
jc noEor
;eor #$1d
doEor: xrl a,#01dh
;sta seed
noEor: mov @r0,a

Chris!

I had to change "rlc" to "rlc a" but otherwise it works fine. Thank you so much! Now I can have random numbers and fancy graphics at the same time.
All my Odyssey2 projects, now in one place!
http://www.atari2600land.com/odyssey2.html

Chris!

Unfortunately I can't use this. Someone help me. I'm trying to make a number between 020h and 080h with this and it just can't work. This is what I had right before I gave up:
get_new_o
call RNG     ; get seed

mov r0,#seed 
mov a,@r0
mov r1,#o_y  ; put in the y position
add a,#020h
mov @r1,a

mov r1,#o_y 
mov a,@r1
add a,#0d0h   ; check to see if variable is too low
jnc add_o_y

check_bottom
mov r1,#o_y
mov a,@r1
add a,#080h   ; check to see if variable is too high
jc subtract_o_y

jmp draw_o

add_o_y
mov r1,#o_y
mov a,@r1
add a,#020h     ;  add 20h to the variable.
mov @r1,a

jmp check_bottom

subtract_o_y
mov r1,#o_y
mov a,@r1
add a,#0d0h    ; subtract 30h to the variable
mov @r1,a

draw_o
mov r1,#o_y
mov a,@r1
mov r4,a   ; put it into r4 for character y position
All my Odyssey2 projects, now in one place!
http://www.atari2600land.com/odyssey2.html

Chris!

All my Odyssey2 projects, now in one place!
http://www.atari2600land.com/odyssey2.html

danlb_2000

If it's any help there are random number generation routines in the bios. The documentation came from Crash Course in
G7000 Games Programming By Graham Thomasson


Routine      RNDM
Function   Random Number Generator
Address      2A4H

Input Parameters    R5=seed (0 to F)
         R7=-(max value +1) or returned nibble
Output Parameters    R3 and ACC = resulting value, right justified
Registers Altered    R3,R5

Called By      RNDM2


RANDM         random Number
This routine returns a random number in the range 0 to N. N can be in the range 1 to 15 and is implied by setting R7 to –(N+1) in the calling sequence. R5 must be supplied as a seed (0 to 15) and the returned value in R5 can be used as the next seed. The routine then has a cycle of N random numbers, in which each number occurs just once. This is rather poor.
The method is simply to  cyclically access the entry in a table of 16 random nibbles. Where N < 15 , rejection of out-of-range values  takes place by returning to the start of the routine.


Routine      RNDM2
Function   Random Number Generator using the Timer
Address      293H

Input Parameters    R2=-(max value+ 1) for the most significant nibble
         R7=-(max value +1) for the least significant nibble
Output Parameters    R3 and ACC = most significant nibble, right justified
         R2 =         = least significant nibble, right justified
Registers Altered    R2, R3, R4, R5, R7
8048 Control Read   Time

Calls         RNDM
   

RNDM2         Random Number Generator using the Timer

This routine returns two random nibbles. The range of each nibble can be separately determined. The routine works by making two calls to RNDM using seeds based on the two nibbles of the timer value. If the timer is not in use, or only uses small counts, then RNDM2 will not produce proper random numbers.
The following call obtains two random nibbles, one in the range 0-7 and the other in the range 0-F
   MOV R2,#0F8H
   MOV R7, #0F0H
   CALL RNDM2
   
      Lsb R2 = random number
         In the range 0 to 7
      Msb R3 and ACC = random number
         In the range 0 to 15

Both the lsb and msb are right-justified.