| |
mankeli
Registered: Oct 2010 Posts: 146 |
How to flags?
I want to set a flag in NMI, basically
lda flags
ora #setbits
sta flags
then I want to check & clear the flag in main routine, and branch if the flag was not set. But since it's not possible to suppress nmi by SEI, I can't make a critical section around the handler. There will be always several instructions.
It's preferrable that the checking code would be only in one place. Currently i have
lda flags
and #testbits
php
eor flags
sta flags
plp
beq skip
..
skip:
I suspect there's a way to do this without the critical section but just wondering if someone knows already. |
|
| |
ChristopherJam
Registered: Aug 2004 Posts: 1409 |
If you can live with just having a single flag per byte, then use a read-modify-write instruction in main to check and clear the flag, eg if the flag is stored in bit zero of sb then
lsr sb
will atomically move the flag to carry and set sb to zero. |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
cool idea CJ. another way would be to have a flag handler flag, so in main code:
inc dontmesswiththeflagnmiplease
; handle flag here
dec dontmesswiththeflagnmiplease
nmi: lda dontmesswiththeflagnmiplease
beq skip
lda flags
ora #setbits
sta flags
skip
another way would be changing the nmi handler low byte before / after the critical code, to an nmi handler which doesnt touches the flag.
ps, well the second one, better inc / dec the high byte of the nmi vector |
| |
oziphantom
Registered: Oct 2014 Posts: 490 |
if you can afford it, use inc/dec to modify $01/$00 and hence you bank in the KERNAL and use its vector to get another NMI |
| |
mankeli
Registered: Oct 2010 Posts: 146 |
ChristopherJam's idea is neat, but uses byte per flag. Oswalds would work if nmi sets the flag constantly but I only set it once and that's why it can't be missed if nmi happens in wrong spot.
There really isn't a way how to do this with multiple flags packed into a byte? |
| |
alwyz
Registered: Dec 2011 Posts: 32 |
If you can't suppress NMI by turning the timer on and off, can you reset the timer repeatedly during instructions to buy yourself enough cycles to do the routines you need to, then when they finish, the timer can count down fully and the NMI can continue?
Not quite what you're aiming for probably, but it's a hack that might work in your situation. |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
ok maybe this is the XYZ problem? you got X problem but you want to solve it Z way, so what is the original problem ? maybe we can find a better solution than nmi/flags/supressing nmi
https://en.wikipedia.org/wiki/XY_problem |
| |
ChristopherJam
Registered: Aug 2004 Posts: 1409 |
Quoting mankeliThere really isn't a way how to do this with multiple flags packed into a byte?
Well it's a bit fiddly, but if main uses a single "fetch bit x from byte y" routine, then the NMI could check the call stack to see if that routine is in progress, and adjust the return address &/or register contents accordingly...
But yes, what Oswald said. |
| |
mankeli
Registered: Oct 2010 Posts: 146 |
original problem was to set flags in nmi (only once), check them in main prog and clear them after checking and pack several of them to a byte to conserve memory.
But yeah, in this particular case it can be worked around by having byte per flag. But this was a bit of "code golf" type question if this be possible to do at all without disabling the nmi. ("lockless" way) |
| |
Oswald
Registered: Apr 2002 Posts: 5094 |
thats the same problem again, what is the problem that you want to solve with the flagging ? |
| |
Zirias
Registered: Jan 2014 Posts: 48 |
Often enough in such situations, you can get away with a "don't care" approach, because your code will always pick up the flag way before the next NMI that would potentially set it is triggered. Did you check that, is there an *actual* (not only potential) race condition? |
... 8 posts hidden. Click here to view all posts.... |
Previous - 1 | 2 - Next |