+atari2600land Posted January 29, 2018 Share Posted January 29, 2018 (edited) 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 January 29, 2018 by atari2600land Quote Link to comment Share on other sites More sharing options...
carlsson Posted January 29, 2018 Share Posted January 29, 2018 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. Quote Link to comment Share on other sites More sharing options...
+atari2600land Posted January 29, 2018 Author Share Posted January 29, 2018 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? Quote Link to comment Share on other sites More sharing options...
+SpiceWare Posted January 29, 2018 Share Posted January 29, 2018 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.altervista.org/blog/an-introduction-to-6502-math-addiction-subtraction-and-more/ Quote Link to comment Share on other sites More sharing options...
+atari2600land Posted January 29, 2018 Author Share Posted January 29, 2018 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 Quote Link to comment Share on other sites More sharing options...
+SpiceWare Posted January 29, 2018 Share Posted January 29, 2018 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 Quote Link to comment Share on other sites More sharing options...
+atari2600land Posted January 29, 2018 Author Share Posted January 29, 2018 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 Quote Link to comment Share on other sites More sharing options...
+SpiceWare Posted January 29, 2018 Share Posted January 29, 2018 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.