Date : Wed, 25 Nov 2009 11:47:15 +0000 (WET)
From : PCOGHLAN@... (Peter Coghlan)
Subject: Quine
>
>Open question to the floor:
>
>This is mostly a minor thing, but a matter of small interest.
>
>If a BRK instruction is executed, how/where/when is the B flag set? I
>have a source that says that BRK sets the B flag, and I have another
>that says the B flag doesn't actually exist, and it is only 'set' if
>necessary when the processor status is pushed to the stack.
>
>I don't have suitable code on my Beeb to experiment, and I don't want to
>use an emulator or simulator as that might depend upon what the
>programmer thought should happen instead of what should happen.
>
>
>This leads to another question. I, personally, like the "B flag doesn't
>really exist" theory, for ask yourself - what does the B flag represent?
>While the other flags set up a status that can be used in conditionals
>and such, or an option (Irq disable, Decimal mode), what does Break flag
>do except advise you of something?
>Thus... if you:
> Push PS to the stack
> Pull that into Acc
> Set the B flag
> Push that to the stack
> Pull it into PS
>(i.e. manually 'set' the B flag)
>What happens?
>What happens at the next interrupt?
>
>
This is a much misunderstood area (along with decimal mode).
Check the mailing list archives for the end of Feb 2008. Someone was
implementing a BBC on an FPGA. They used established 6502 emulation code as
a base and ran into problems with the BRK handling which turned out to be
incorrectly implemented, apparantly a common problem.
As far as I understand it, the argument that the B flag does not really
exist is correct and there is no register bit in the 6502 holding its value.
As far as I recall, if you execute a PHP followed by a PLA to, you normally
find the bit in the B flag position to be set. Again, as far as I recall,
if you should clear that bit in A and do a PHA followed by a PLP, no effects
result and if you do another PHP, PLA, you again find that the B bit is set.
I believe the B flag only exists in the form of what is automatically pushed
onto the stack in the case of a BRK or interrupt. In the case of a BRK, the
bit in the B flag position of the byte that is pushed on the stack is set
to one. In the case of a (maskable) interrupt, the B bit in the byte that is
pushed is set to zero.
This is where the guy with the FPGA implementation was having problems - the
6502 implementation he used implemented the B flag as a real flag and failed
to clear it in the event of an interrupt. Everything then ran fine until the
first BRK was executed. After that, all interrupts ended up getting routed
to the BRK handling code and things went rapidly downhill.
Regards,
Peter Coghlan.