Looking for a quick way to find odd / even numbers

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
Johnny Starr
Posts: 58
Joined: 30 May 2012
Location: Dallas, TX
Contact:

Looking for a quick way to find odd / even numbers

Post 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.
Aslak3
Posts: 258
Joined: 05 Aug 2013
Location: Southampton, UK
Contact:

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

Post 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?
8 bit fun and games: https://www.aslak.net/
User avatar
BigDumbDinosaur
Posts: 9426
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

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

Post 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
x86?  We ain't got no x86.  We don't NEED no stinking x86!
User avatar
Johnny Starr
Posts: 58
Joined: 30 May 2012
Location: Dallas, TX
Contact:

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

Post by Johnny Starr »

Of course! Shift right, check carry. That's what I get for overthinking it huh?
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

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

Post 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.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
User avatar
BigDumbDinosaur
Posts: 9426
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

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

Post 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.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

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

Post by BigEd »

But you can't do that on a proper 6502!
teamtempest
Posts: 443
Joined: 08 Nov 2009
Location: Minnesota
Contact:

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

Post 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'.
User avatar
BigDumbDinosaur
Posts: 9426
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

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

Post 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.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
White Flame
Posts: 704
Joined: 24 Jul 2012

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

Post 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.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

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

Post 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
Post Reply