Jump to content

Photo

Getting random value between 10-110


7 replies to this topic

#1 atari2600land ONLINE  

atari2600land

    Channel F Viewer

  • 11,305 posts
  • Location:Salem, Oregon

Posted Sun Jan 28, 2018 8:54 PM

I am having trouble getting a number between 10-110. This is my code I have now:

Get_random_value
        jsr Random  ; get value between
        and #127    ; 0-127
        adc #10      ; add ten
        sta NextValueXPos   ; save value

        cpx #110   ; check if value is above 110
        bcs Lower_X_Pos  ; if so, go to the lowering part of the code
        
        rts  ; end this section
        
Lower_X_Pos
        lda NextValueXPos    ; load value
        sbc #57    ; decrease by 57
        sta NextValueXPos        ; save value
        rts  ; end this section

Sometimes a value will turn the object up at the extreme right part of the code where it is unreachable, even though I had set it to decrease the value of the x position by a whole bunch. Or am I doing something wrong?

Random:
        lda Rand
        lsr
 ifconst Rand16
        rol Rand16
 endif
        bcc noeor
        eor #$B4
noeor
        sta Rand
 ifconst Rand16
        eor Rand16
 endif
        rts   

EDIT: Argh! I am such a dummy! It should be this:

        cmp #110   ; check if value is above 110
        bcs Lower_X_Pos  ; if so, go to the lowering part of the code

Since it's saved in a, not x.

EDIT #2: Hmm. This isn't working as I thought it would, either.


Edited by atari2600land, Sun Jan 28, 2018 9:01 PM.


#2 carlsson ONLINE  

carlsson

    Metagalactic Mule

  • 7,131 posts
  • Location:Västerås, Sweden

Posted Mon Jan 29, 2018 3:31 AM

What is Rand initialized to, and are you using the Rand16 extension or not? Not that it should matter, as the main routine should cap the value anyway.

 

You might be interested in the carry flag that is set by Random, and will affect the ADC in the main routine. Since the addition won't cause the carry flag to be set again, you are subtracting 57 without carry later on. Sometimes it is fine to take shortcuts if you know the value of flags, but sometimes you can get bitten by them too.



#3 atari2600land ONLINE  

atari2600land

    Channel F Viewer

  • Topic Starter
  • 11,305 posts
  • Location:Salem, Oregon

Posted Mon Jan 29, 2018 2:58 PM

Rand is set to #40. It was 0 (in that I forgot to.) But I still have the problem anyway. Is there a way I can add a certain number to a value without carrying?



#4 SpiceWare OFFLINE  

SpiceWare

    Draconian

  • 12,270 posts
  • Medieval Mayhem
  • Location:Planet Houston

Posted Mon Jan 29, 2018 3:30 PM

Is there a way I can add a certain number to a value without carrying?

 

 

Make sure to do a CLC before addition.  Likewise do an SEC before subtraction.

 

http://retro64.alter...ction-and-more/



#5 atari2600land ONLINE  

atari2600land

    Channel F Viewer

  • Topic Starter
  • 11,305 posts
  • Location:Salem, Oregon

Posted Mon Jan 29, 2018 4:06 PM

I did. I still get the same problem, though. Also, I don't need a value above 255 so the carry should be unimportant, right? That's why I asked if there was a way to add to numbers without it.

Get_random_value

        clc
        jsr Random
        and #127
        adc #10    
        sta NextValueXPos    

        cmp #100
        bcs Lower_X_Pos
        rts
        
Lower_X_Pos
        sec
        lda NextValueXPos
        sbc #40
        sta NextValueXPos        
        rts


#6 SpiceWare OFFLINE  

SpiceWare

    Draconian

  • 12,270 posts
  • Medieval Mayhem
  • Location:Planet Houston

Posted Mon Jan 29, 2018 5:08 PM

jsr Random will change the carry so you have it at the wrong spot.
 
It helps to comment the range of the accumulator values.



Get_random_value:
        jsr Random     ; A = 0-255
        clc
        and #127       ; A = 0-127
        adc #10        ; A = 10-137
        sta NextValueXPos    

        cmp #111       ; need to compare with 111, else the range ends up being 10-109
        bcs Lower_X_Pos ; C will be set if A >= 111
        rts
        
Lower_X_Pos:               ; A = 111-127
;        sec               ; not needed as we know C is set due to the CMP #110
;        lda NextValueXPos ; not needed as A is already NextValueXPos
        sbc #40            ;  A = 71-97
        sta NextValueXPos        
        rts


#7 atari2600land ONLINE  

atari2600land

    Channel F Viewer

  • Topic Starter
  • 11,305 posts
  • Location:Salem, Oregon

Posted Mon Jan 29, 2018 5:17 PM

I tested this and it doesn't work either. So something must be wrong with my sense of x positioning values. What is the farthest x value a 4-pixel wide missile can be without it going off the screen and warping to the left? Also, I have this code for a Y value and it seems to work okay.

        jsr Random
        and #15   ; 0-15
        adc #231  ; add 231 to make it 231-246
        sta ValueY  


#8 SpiceWare OFFLINE  

SpiceWare

    Draconian

  • 12,270 posts
  • Medieval Mayhem
  • Location:Planet Houston

Posted Mon Jan 29, 2018 5:35 PM

Hard to say when just seeing this one function out of context, perhaps something's wrong with your positioning routine.

Best thing to do is use Stella's debugger and step through the code to see what its doing vs what you think it should be doing.




0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users