Problems with arbitrary assembler numbers 6502

I play with assembler 6502 here: http://skilldrick.imtqy.com/easy6502

I did this to just put a random color pixel in a random place on the screen:

LDY #$00 ; Clear Y loop: JSR genPos ; Put new position in memory LDA $fe ; Get random number for color STA ($10), Y ; Put pixel on screen JMP loop genPos: STA $10 ; Store accumulator in low LDA $fe ; Get new random number (WHY, oh why?) AND #$03 ; Mask out low two bits (=numbers 0-3) CLC ; Clear carry flag ADC #2 ; Add 2 (= numbers 2-5) STA $11 ; Store number in high RTS 

I try to use as few instructions as possible. My problem is that if I do not add the extra LDA $fe to the genPos subroutine, the pixels are drawn in a very strange pattern, where if I have an extra LDA, the code works fine. I can't understand why - can anyone give me a hint?

Regards, Jacob

+5
source share
2 answers

He already does the shallow low byte! This line:

 LDA $fe ; Get new random number (WHY, oh why?) 

continues to determine the high byte, and if you do not create a new random number, the y value will depend on the lower two bits of the x value, causing the diagonals you see: the x & 3 value is always equal to which segment of the screen the value is drawn on, which means that you get a template like

 β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ \ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | x & 3 == 0 in $200-$2FF β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ / β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ \ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | x & 3 == 1 in $300-$3FF β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ / β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ \ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | x & 3 == 2 in $400-$4FF β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ / β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ \ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | x & 3 == 3 in $500-$5FF β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ | β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ / 
+9
source

The algorithm used with the LDA:

 [random 1] -> [11:10] [random 1] -> [10] [random 2]&3 + 2 -> [11] (repeat) 

If you do not reload a from $ fe, you do:

 [random 1] -> [11:10] [ramdom 1] -> [10] [random 1]&3 + 2 -> [11] (repeat) 

Therefore, the low and high byte of the address pixel is a function of the same random number. They are not independent. This creates a correlation between them. What appears in the pattern that you see. If your fake screen was 256 pixels wide, it would be a straight diagonal; based on the fact that you see 8 stripes, I can conclude that your output should be 256/8 = 32 pixels wide.

The problem is not that you are mistaken to say that "a random number in the battery ... should make a small low byte for the screen address." it's that you also use it for high byte. You use it for both. High byte is a direct function of low byte. So there is a bunch of big bytes that you will never hit because they do not satisfy the constraint, which is high=(low&3)+2 . These pixels remain inactive.

+5
source

All Articles