Page 1 of 1

Looking for a quick way to find odd / even numbers

Posted: Mon Mar 10, 2014 7:13 pm
by Johnny Starr
I'm working on a routine for an Atari 2600 game. The portion of the code is the 192 visible scanlines. I'm attempting to achieve a visual effect by updating the background every other scanline.

The scanline loop simply counts from 192 to 0. In this scheme, #192 represents the top line, #0 the bottom or last line. It seems that there should be a way to determine odd / even numbers to accomplish this. I'm not sure if this is considered "parity" or not, but I haven't found the right flag for odds / evens.

Re: Looking for a quick way to find odd / even numbers

Posted: Mon Mar 10, 2014 7:37 pm
by Aslak3
Johnny Starr wrote:
I'm working on a routine for an Atari 2600 game. The portion of the code is the 192 visible scanlines. I'm attempting to achieve a visual effect by updating the background every other scanline.

The scanline loop simply counts from 192 to 0. In this scheme, #192 represents the top line, #0 the bottom or last line. It seems that there should be a way to determine odd / even numbers to accomplish this. I'm not sure if this is considered "parity" or not, but I haven't found the right flag for odds / evens.
Parity is a count of the number of set bits. For oddness/eveness, just test the lowest bit. If it is 0, it's even. if it is 1 it is odd.

Edit: But from the sounds of it you just want to update all the even lines (say) then the odd ones. It is so simple I must be misunderstanding, but can't you simply add 2 on each loop iteration?

Re: Looking for a quick way to find odd / even numbers

Posted: Mon Mar 10, 2014 7:43 pm
by BigDumbDinosaur
Johnny Starr wrote:
I'm working on a routine for an Atari 2600 game. The portion of the code is the 192 visible scanlines. I'm attempting to achieve a visual effect by updating the background every other scanline.

The scanline loop simply counts from 192 to 0. In this scheme, #192 represents the top line, #0 the bottom or last line. It seems that there should be a way to determine odd / even numbers to accomplish this. I'm not sure if this is considered "parity" or not, but I haven't found the right flag for odds / evens.
It's very simple:

Code: Select all

         lda scanline          ;current scan line number
         lsr a                 ;shift bit 0 to carry
         bcc is_even           ;even number
         bcs is_odd            ;odd number
Alternatively, with the 65C02/65C816:

Code: Select all

         lda scanline          ;current scan line number
         bit #%00000001        ;is bit 0 set?
         beq is_even           ;no, even number
         bne is_odd            ;yes, odd number

Re: Looking for a quick way to find odd / even numbers

Posted: Mon Mar 10, 2014 8:06 pm
by Johnny Starr
Of course! Shift right, check carry. That's what I get for overthinking it huh?

Re: Looking for a quick way to find odd / even numbers

Posted: Mon Mar 10, 2014 8:35 pm
by GARTHWILSON
or AND#1, BNE/BEQ. The AND# takes 2 clocks, and if the conditional branch is not taken, it's 2 more, or if the branch is taken, it's usually 3 more instead of 2. You can probably get away with one branch instruction, as the drop-thru is automatically the other condition.

Re: Looking for a quick way to find odd / even numbers

Posted: Mon Mar 10, 2014 9:39 pm
by BigDumbDinosaur
GARTHWILSON wrote:
or AND#1, BNE/BEQ. The AND# takes 2 clocks, and if the conditional branch is not taken, it's 2 more, or if the branch is taken, it's usually 3 more instead of 2. You can probably get away with one branch instruction, as the drop-thru is automatically the other condition.
BIT immediate is the least destructive and produces the same result.

Re: Looking for a quick way to find odd / even numbers

Posted: Mon Mar 10, 2014 10:47 pm
by BigEd
But you can't do that on a proper 6502!

Re: Looking for a quick way to find odd / even numbers

Posted: Tue Mar 11, 2014 2:45 am
by teamtempest
If all you're interested in is odd/even and not the particular scanline, you can do this on all 65xx-family processors:

Code: Select all

  LDA #%00000001
  BIT  scanline
  BEQ doeven
  BNE do odd
It's not as fast as load-and-shift, though.

BTW, 0..192 is 193 lines, not 192. Just sayin'.

Re: Looking for a quick way to find odd / even numbers

Posted: Tue Mar 11, 2014 4:57 am
by BigDumbDinosaur
BigEd wrote:
But you can't do that on a proper 6502!
Right. I was thinking 'C02 or '816. BIT immediate is one of the many reasons to not use the NMOS 6502 if you have a choice.

Re: Looking for a quick way to find odd / even numbers

Posted: Wed Mar 12, 2014 4:57 am
by White Flame
If you store the value 1 to a zero page memory location const1, then you can do "BIT const1" to perform the test without modifying the accumulator on ye olde 6502.

Re: Looking for a quick way to find odd / even numbers

Posted: Wed Mar 12, 2014 3:24 pm
by barrym95838
I was thinking the same thing, White Flame. Or just find a 1 somewhere in your program or the firmware ... there are bound to be several. Code-as-constants is a tried-and-true tradition in the 65xx world, even more so than self-modifying.

Mike