4

Why AND instruction updates flags in the fetch step?

Others logic instructions like ORA and EOR update flags in the same step that they update accumulator, in the decode step.

Is it a bug of visual6502? Is it a bug of 6502? Is it the normal behaviour?

Example for AND:

cycle   ab      db  rw  Fetch   pc      a   x   y   s   p
----------------------------------------------------------------
0       0000    29  1   AND #   0000    aa  00  00  fd  nv‑BdIZc ; AND #$FE
0       0000    29  1   AND #   0000    aa  00  00  fd  nv‑BdIZc
1       0001    f0  1           0001    aa  00  00  fd  nv‑BdIZc
1       0001    f0  1           0001    aa  00  00  fd  nv‑BdIZc
2       0002    ea  1   NOP     0002    aa  00  00  fd  Nv‑BdIzc ; Fetch opcode, Update flags here
2       0002    ea  1   NOP     0002    aa  00  00  fd  Nv‑BdIzc
3       0003    ea  1           0003    a0  00  00  fd  Nv‑BdIzc ; Decode opcode, Update accumulator here
3       0003    ea  1           0003    a0  00  00  fd  Nv‑BdIzc

Example for EOR:

cycle   ab      db  rw  Fetch   pc      a   x   y   s   p
----------------------------------------------------------------
0       0000    49  1   EOR #   0000    aa  00  00  fd  nv‑BdIZc ; EOR #$FF
0       0000    49  1   EOR #   0000    aa  00  00  fd  nv‑BdIZc
1       0001    ff  1           0001    aa  00  00  fd  nv‑BdIZc
1       0001    ff  1           0001    aa  00  00  fd  nv‑BdIZc
2       0002    ea  1   NOP     0002    aa  00  00  fd  nv‑BdIZc ; Fetch opcode
2       0002    ea  1   NOP     0002    aa  00  00  fd  nv‑BdIZc
3       0003    ea  1           0003    55  00  00  fd  nv‑BdIzc ; Decode opcode, Update accumulator and flags here
3       0003    ea  1           0003    55  00  00  fd  nv‑BdIzc

Example for ORA:

cycle   ab      db  rw  Fetch   pc      a   x   y   s   p
----------------------------------------------------------------
0       0000    09  1   ORA #   0000    aa  00  00  fd  nv‑BdIZc ; ORA #$FF
0       0000    09  1   ORA #   0000    aa  00  00  fd  nv‑BdIZc
1       0001    ff  1           0001    aa  00  00  fd  nv‑BdIZc
1       0001    ff  1           0001    aa  00  00  fd  nv‑BdIZc
2       0002    ea  1   NOP     0002    aa  00  00  fd  nv‑BdIZc ; Fetch opcode
2       0002    ea  1   NOP     0002    aa  00  00  fd  nv‑BdIZc
3       0003    ea  1           0003    ff  00  00  fd  Nv‑BdIzc ; Decode opcode, Update accumulator and flags here
3       0003    ea  1           0003    ff  00  00  fd  Nv‑BdIzc
7

AND works like any other instruction. The effect you see is due the data setup used. It obscures the change in your example. Try clearing A first, like here, and you'll see the update happens exactly like with OR/EOR:

cycle  ab    db  rw  Fetch  pc    a   x   y   s   p
  0    0000  a9  1   LDA #  0000  aa  00  00  fd  nv‑BdIZc
  0    0000  a9  1   LDA #  0000  aa  00  00  fd  nv‑BdIZc
  1    0001  00  1          0001  aa  00  00  fd  nv‑BdIZc
  1    0001  00  1          0001  aa  00  00  fd  nv‑BdIZc
  2    0002  29  1   AND #  0002  00  00  00  fd  nv‑BdIZc
  2    0002  29  1   AND #  0002  00  00  00  fd  nv‑BdIZc
  3    0003  ff  1          0003  00  00  00  fd  nv‑BdIZc
  3    0003  ff  1          0003  00  00  00  fd  nv‑BdIZc
  4    0004  ea  1   NOP    0004  00  00  00  fd  Nv‑BdIzc  <- "Intermediate" update
  4    0004  ea  1   NOP    0004  00  00  00  fd  Nv‑BdIzc
  5    0005  ea  1          0005  00  00  00  fd  nv‑BdIZc  <- "Real" update with result
  5    0005  ea  1          0005  00  00  00  fd  nv‑BdIZc

While in this case simply clearing A did already show it, a valid test setup should not only use defined values but as well vary them to show relation.

| improve this answer | |
  • The strange thing is that intermediate update seems to be flags updated with the loaded value (here $FF), but this intermediate update occurs only on AND, not on other logical instructions. – Johnmph Feb 10 at 10:59
  • All of them of course work different on values. Since there are only two flags involved (Zero and Negative), only combinations of values that affect these flags different from their previous setting will show in both updates as different. Draw up a truth table for this and you'll see why only AND will make the effect that easy to detect - keep in mind, you're only noticing an update if it changes either flag. In all other ceases it still happens, but isn't visible. – Raffzahn Feb 10 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.