GalliumPhillips
GalliumPhillips

Reputation: 71

6502 Emulator Correct Implementation of LSR Instruction

I'm working on an emulator for the MOS6502, but I have been unable to convince myself of the correct implementation of the LSR instruction.

In the MOS Microcomputers Programming Manual on page 148 it says:

The shift right instruction either affects the accumulator by shifting it right 1 or is a read/modify/write instruction which changes a specified memory location but does not affect any internal registers.

This suggests to me that when the LSR instruction is used with accumulator addressing mode we can expect the processor status registers to be affected. However, in other addressing modes such as absolute, the status registers should be unaffected.

I have inspected code avaliable from other MOS6502 emulators but as far as I can tell they do not appear to implement this functionality.

Furthermore, I used this assembler and emulator to test the following assembly code:

LDA #$FF
STA $0010
LSR $0010

According to my interpretation of the programming manual this should not result in any status registers changing as I am using absolute addressing with the LSR instruction. However the carry flag is set and the negative flag is reset when the LSR $0010 instruction is executed.

Result of above code execution on emulator

I have likely interpreted the manual incorrectly but was wondering if anyone could clarify whether the processor status register is affected in all addressing modes or not.

Upvotes: 7

Views: 117

Answers (1)

JeremyP
JeremyP

Reputation: 86671

I have likely interpreted the manual incorrectly

Yes. Having read the manual you linked to, I am 100% confident that when it says it does not affect any internal registers, it is implicitly excluding the status register. This is born out by the first paragraph and the following paragraph on the affected flags:

This instruction shifts either the accumulator or a specified memory location 1 bit to the right, with the higher bit of the result always being set to 0 , and the low bit which is shifted out of the field being stored in the carry' flag.

The N flag is always reset. The Z flag is set if the result of the shift is 0 and reset otherwise. The carry is set equal to bit 0 of the input.

Furthermore, this is how I programmed real 6502s back in the day and how every emulator I have come across (or written) works including the Visual6502 simulator.

Upvotes: 1

Related Questions