Most efficient way to toggle a bit (flag) in memory
Re: Most efficient way to toggle a bit (flag) in memory
Yes it does add jitter, it does save a few cycles .
Re: Most efficient way to toggle a bit (flag) in memory
I wanted to thank everyone for their help. I got a bit distracted form this project for a few weeks but implemented the changes suggest by Chromatix and it works quite well. The number of cycles though each path is also very close. I started producing a video about playing DIGIs on the C64 which I hope to finish up this weekend.
Yesterday I had an idea of how to implement an interpolating scheme. Hopefully resulting in a playback quality very similar to the 8kz sample rate but with half the memory footprint. I'll start a new thread on that subject though.
Yesterday I had an idea of how to implement an interpolating scheme. Hopefully resulting in a playback quality very similar to the 8kz sample rate but with half the memory footprint. I'll start a new thread on that subject though.
Re: Most efficient way to toggle a bit (flag) in memory
Sorry, I haven't been so active here lately.
I had the same problem some time ago, and the fastest approach is always selfmodifying code. At least if you want to shift between two instances with a flag. If you can allow things to be in ZP, then you get it down to 7.5 cycles (8.5 cycles if not in ZP):
Just remember to modify LDX# so that it is according to what you expect the carry flag to be (or you can use BNE/BEQ or something else to suit your code). The actual flag here resides in the instruction, e.g. at $A3 in the code above.
I had the same problem some time ago, and the fastest approach is always selfmodifying code. At least if you want to shift between two instances with a flag. If you can allow things to be in ZP, then you get it down to 7.5 cycles (8.5 cycles if not in ZP):
Code: Select all
*=$a3
BCC next ;2.5c
LDX #$90 ;2c
STX $a3 ;3c set to BCC
(your code here)
next
LDX #$B0 ;2c
STX $a3 ;3c set to BCSJust remember to modify LDX# so that it is according to what you expect the carry flag to be (or you can use BNE/BEQ or something else to suit your code). The actual flag here resides in the instruction, e.g. at $A3 in the code above.
-
Stonemonkey
- Posts: 8
- Joined: 19 Jul 2016
Re: Most efficient way to toggle a bit (flag) in memory
This relies on V being clear and Y is clobbered so maybe not so good when it comes to debugging.
I think it's 7.5 or 8.5 depending on zp or not.
I think it's 7.5 or 8.5 depending on zp or not.
Code: Select all
......
.flag BVC next
LSR flag
.......
Code 1 here
.......
RTS
.next ASL flag
.......
Code 2 here
........
RTS
Re: Most efficient way to toggle a bit (flag) in memory
Personally, I prefer using "LSR flag" instead of "ASL flag".
The reason being is that in the initialization routine, there is almost always some register set to zero and it will save 2 bytes as an extra instruction to load the $55 or $AA is unneeded. For me, it is not only just about saving bytes but also saving bits as only the 0-bit and 1-bit ever comes into play.
LSR flag
BCS next:
INC flag
...
RTS
next:
will work just well.
Also note that if the carry needs to be preserved when entering a routine, the
LDA flag
EOR #$80
STA flag
BMI routine2
might also be the better option
The reason being is that in the initialization routine, there is almost always some register set to zero and it will save 2 bytes as an extra instruction to load the $55 or $AA is unneeded. For me, it is not only just about saving bytes but also saving bits as only the 0-bit and 1-bit ever comes into play.
LSR flag
BCS next:
INC flag
...
RTS
next:
will work just well.
Also note that if the carry needs to be preserved when entering a routine, the
LDA flag
EOR #$80
STA flag
BMI routine2
might also be the better option
Re: Most efficient way to toggle a bit (flag) in memory
A bit late to this party but since I have been looking at the best way to do this right now so wanted to give my 2 cents in case it helps someone down the road.
I settled on flipping the bit 7 as a flag particularly for the reason that if you need to check the flag later on in the code, you can simply use the BIT flag instruction without using any registers for the check. If you use bit 0 as the flag then you have to load/use a register for subsequent checks.
So for the case where you toggle a flag and then you need to check it multiple times later on I think the win is..
Toggle
LDA flag
EOR #$80
STA flag
And for later checks
BIT flag
BMI or BPL
I settled on flipping the bit 7 as a flag particularly for the reason that if you need to check the flag later on in the code, you can simply use the BIT flag instruction without using any registers for the check. If you use bit 0 as the flag then you have to load/use a register for subsequent checks.
So for the case where you toggle a flag and then you need to check it multiple times later on I think the win is..
Toggle
LDA flag
EOR #$80
STA flag
And for later checks
BIT flag
BMI or BPL
Re: Most efficient way to toggle a bit (flag) in memory
radek wrote:
A bit late to this party but since I have been looking at the best way to do this right now so wanted to give my 2 cents in case it helps someone down the road.
I settled on flipping the bit 7 as a flag particularly for the reason that if you need to check the flag later on in the code, you can simply use the BIT flag instruction without using any registers for the check. If you use bit 0 as the flag then you have to load/use a register for subsequent checks.
So for the case where you toggle a flag and then you need to check it multiple times later on I think the win is..
Toggle
LDA flag
EOR #$80
STA flag
And for later checks
BIT flag
BMI or BPL
I settled on flipping the bit 7 as a flag particularly for the reason that if you need to check the flag later on in the code, you can simply use the BIT flag instruction without using any registers for the check. If you use bit 0 as the flag then you have to load/use a register for subsequent checks.
So for the case where you toggle a flag and then you need to check it multiple times later on I think the win is..
Toggle
LDA flag
EOR #$80
STA flag
And for later checks
BIT flag
BMI or BPL
LDA flag
EOR #$40
STA FLAG
BIT flag
BVS or BVC
Re: Most efficient way to toggle a bit (flag) in memory
Welcome, radek!
-- Jeff
-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
https://laughtonelectronics.com/Arcana/ ... mmary.html