How common is it to use V flag after adc, sbc, cmp, vs. bit?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by dougeff »

From...

http://www.6502.org/tutorials/vflag.html

"CLV clears V. Simple enough. It is most frequently used in conjunction with a BVC to simulate a forced branch (a.k.a a branch always) in relocatable routines. The 65C02 has a branch always instruction (BRA), but the 6502 does not."

Once you are sure V is clear, you can use BVC repeatedly, as long as there are no ADC,SBC,or BIT instructions. Is 1 byte shorter than JMP


Also, I've used the V flag in signed math, but I prefer not to (unsigned math is easier). I've mostly seen it for sprite zero checking, after BIT.

Edit - I couldn't find any example code where I used bvc or bvs for math. I retract that statement.
Last edited by dougeff on Fri Aug 25, 2017 4:42 am, edited 1 time in total.
nesdoug.com -- blog/tutorial on programming for the NES
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by Oziphantom »

tepples wrote:I learned EOR-ADC-EOR-BVC, though there were several places that could have used it.
What does the EOR-ADC-EOR-BVC trick achieve?
Pokun
Posts: 2675
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by Pokun »

I wanna know too.
rainwarrior wrote:
GradualGames wrote:I've actually grepped every single game's code that I've adapted to make sure it's not using bvs bvc except after bit.
That... sounds like a lot more effort than it would take to just implement V. O_o
Yeah just implement it already! lol
This thread is interesting to see how people use V, but other than that, it's not much to discuss whether or not to implement it I think.
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by tepples »

Oziphantom wrote:
tepples wrote:I learned EOR-ADC-EOR-BVC, though there were several places that could have used it.
What does the EOR-ADC-EOR-BVC trick achieve?
From my previous post: "I use it to determine whether an addition or subtraction involving a signed number overflowed. This is useful for ensuring an entity's position doesn't leave the bounds of the screen or map and wrap to the other side."

It distinguishes 129 + (-2) = 127, which in this context is desirable, from 1 + (-2) = 255, which in this context needs to be clamped to 0.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by dougeff »

cc65 uses bvc in both icmp.s and lcmp.s for signed comparisons.

https://github.com/cc65/cc65/tree/master/libsrc/runtime
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by GradualGames »

Pokun wrote:I wanna know too.
rainwarrior wrote:
GradualGames wrote:I've actually grepped every single game's code that I've adapted to make sure it's not using bvs bvc except after bit.
That... sounds like a lot more effort than it would take to just implement V. O_o
Yeah just implement it already! lol
This thread is interesting to see how people use V, but other than that, it's not much to discuss whether or not to implement it I think.
In most cases I simply asked the developer of the game I was adapting whether they ever used bvc or bvs and delegated the task of answering that question to them, so it didn't feel like much work. I did try to implement V initially, but couldn't figure it out at that time. Plus I had no games which were exercising it anyway, and didn't feel like writing a test rom for something which I didn't know would necessarily be used. Like I said I have a lot of other things I do with my time, like making games, music, my full time job, walking for an hour every day, playing games, watching tv, etc. ggvm was something I got done over winter when I had slightly fewer things occupying my time, now I barely have any time for it. You're welcome to add support for V to the github project if you want to help out ;) It has turned out to be useful to a number of folks. That reminds me actually, I don't think I ever finished implementation of indexed indirect instructions either, I never use them and again, neither did the 10+ games I've adapted...
User avatar
gauauu
Posts: 779
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by gauauu »

GradualGames wrote:I don't think I ever finished implementation of indexed indirect instructions either, I never use them and again, neither did the 10+ games I've adapted...
I've read different explanations of good uses of indexed indirect instructions...but I've never once used them.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by thefox »

The only use (for (zp,x)) I've found so far is to dereference some address when X already happens to be 0 or I don't want to destroy Y (required for (zp),y) for some reason.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by GradualGames »

thefox wrote:The only use (for (zp,x)) I've found so far is to dereference some address when X already happens to be 0 or I don't want to destroy Y (required for (zp),y) for some reason.
It's so rarely used too bad we can't go back in time and tell the chip designers to get rid of indexed indirect and instead make the rest of the instruction set symmetrical. Haha. I don't know how many times I've tried to use

sty blahblah,x

thinking I could because I also did

ldy blahblah,x

...

*edit* Wait, now I'm confused. Out of curiosity I checked my printed out 6502 reference. It says zp,x is available for sty. But I'm pretty sure ca65 yells at me when I try it. Oh wait....it's ABS,x that I'm trying..that's what isn't supported.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by tokumaru »

I once started a music engine where the streams for the different channels were accessed by consecutive pointers in ZP, and X was used to select one of them.
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by tepples »

tokumaru wrote:I once started a music engine where the streams for the different channels were accessed by consecutive pointers in ZP, and X was used to select one of them.
Same here. The only time I remember having actually used (dd,X) has been in my music engine Pently, where X equals 0, 4, 8, 12, or 16 for the five logical tracks (pulse 1, pulse 2, triangle, drum, attack) or the four APU PSG channels (pulse 1, pulse 2, triangle, noise). But GGVM's sampler is completely different from the 2A03's APU anyway.

I imagine that there are other uses of a pointer table on zero page. But 6502 coders who learned on Commodore 64 or Apple II won't have a lot of experience with it because Microsoft BASIC in ROM ate so much of zero page for itself.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by tokumaru »

tepples wrote:But 6502 coders who learned on Commodore 64 or Apple II won't have a lot of experience with it because Microsoft BASIC in ROM ate so much of zero page for itself.
And I guess they didn't even need ZP that much, since any absolute reference can be used as a pointer when code is running from RAM.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by dougeff »

I suppose you could use (zp,x) as pointers to pointers. (Or rather, to get the starting offset within a pointer table). But I would still prefer to use (zp), y or some other mode for that.

I made it pretty clear on my blog that I don't like (zp, x) mode, and don't use it.
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by rainwarrior »

tokumaru wrote:
tepples wrote:But 6502 coders who learned on Commodore 64 or Apple II won't have a lot of experience with it because Microsoft BASIC in ROM ate so much of zero page for itself.
And I guess they didn't even need ZP that much, since any absolute reference can be used as a pointer when code is running from RAM.
Well, in the case of (zp,X) the point is to be able to loop through or random-access a table of pointers. Self-modifying code doesn't do that very well (requires unrolled code, altering the pointers by index no longer possible, etc.)

tepples' canonical example for something that is convenient with a table of pointers is per-channel data streams in a music engine, and that's primarily where I've used it too.
User avatar
Bregalad
Posts: 8055
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: How common is it to use V flag after adc, sbc, cmp, vs.

Post by Bregalad »

thefox wrote:The only use (for (zp,x)) I've found so far is to dereference some address when X already happens to be 0 or I don't want to destroy Y (required for (zp),y) for some reason.
Well I use (zp,X) as a way to do a multi-channel music engine, and I also do this for a bytecode interpreter (basically improved SWEET-16). So I may use the instruction twice in the whole game code, but that particular instruction can be executed potentially hundreds of times per frame.
I made it pretty clear on my blog that I don't like (zp, x) mode, and don't use it.
It's up to you to like it or not, but refusing to use it in one of the rare cases where it could be handful it's not very bright.

As for BVS/BVC, I use it mainly exclusively in combination with BIT, but not only with $2002. BIT is an amazing instruction when it comes to read the top two bits of any variable, I do that all the time. It's also common I have routines which returns two boolean values, one in C and one in V.
Last edited by Bregalad on Sun Aug 27, 2017 12:51 pm, edited 1 time in total.
Post Reply