How common is it to use V flag after adc, sbc, cmp, vs. bit?
Moderator: Moderators
Re: How common is it to use V flag after adc, sbc, cmp, vs.
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.
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
-
- Posts: 1565
- Joined: Tue Feb 07, 2017 2:03 am
Re: How common is it to use V flag after adc, sbc, cmp, vs.
What does the EOR-ADC-EOR-BVC trick achieve?tepples wrote:I learned EOR-ADC-EOR-BVC, though there were several places that could have used it.
Re: How common is it to use V flag after adc, sbc, cmp, vs.
I wanna know too.
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.
Yeah just implement it already! lolrainwarrior wrote:That... sounds like a lot more effort than it would take to just implement V. O_oGradualGames 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.
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.
Re: How common is it to use V flag after adc, sbc, cmp, vs.
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."Oziphantom wrote:What does the EOR-ADC-EOR-BVC trick achieve?tepples wrote:I learned EOR-ADC-EOR-BVC, though there were several places that could have used it.
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.
Re: How common is it to use V flag after adc, sbc, cmp, vs.
cc65 uses bvc in both icmp.s and lcmp.s for signed comparisons.
https://github.com/cc65/cc65/tree/master/libsrc/runtime
https://github.com/cc65/cc65/tree/master/libsrc/runtime
nesdoug.com -- blog/tutorial on programming for the NES
- 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.
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...Pokun wrote:I wanna know too.
Yeah just implement it already! lolrainwarrior wrote:That... sounds like a lot more effort than it would take to just implement V. O_oGradualGames 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.
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.
Re: How common is it to use V flag after adc, sbc, cmp, vs.
I've read different explanations of good uses of indexed indirect instructions...but I've never once used them.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...
My games: http://www.bitethechili.com
Re: How common is it to use V flag after adc, sbc, cmp, vs.
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
- 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.
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 usethefox 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.
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.
Re: How common is it to use V flag after adc, sbc, cmp, vs.
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.
Re: How common is it to use V flag after adc, sbc, cmp, vs.
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.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.
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.
Re: How common is it to use V flag after adc, sbc, cmp, vs.
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.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.
Re: How common is it to use V flag after adc, sbc, cmp, vs.
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.
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
- rainwarrior
- Posts: 8734
- 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.
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.)tokumaru wrote: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.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.
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.
Re: How common is it to use V flag after adc, sbc, cmp, vs.
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.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 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.I made it pretty clear on my blog that I don't like (zp, x) mode, and don't use it.
As for BVS/BVC, I use it
Last edited by Bregalad on Sun Aug 27, 2017 12:51 pm, edited 1 time in total.