Question about LSR
xem opened this issue · comments
Hello,
The documentation I found online about LSR says:
LSR shifts all bits right one position. 0 is shifted into bit 7 and the original bit 0 is shifted into the Carry.
Flags Z and N are set according to the result.
your code does:
CPU6502.prototype.lsr = function() {
this.tmp = this.read(this.addr);
this.tmp = ((this.tmp & 1) << 8) | (this.tmp >> 1);
this.fnzc(this.tmp);
this.tmp &= 0xFF;
}
I kinda understand why it works with your implementation, but it seems pretty counter-intuitive.
Why not store bit 7 in C directly and call fnz, instead of left shifting bit 0 eight times and call fznc?
Also, why do you mask tmp with 0xFF at the end? rmw is called after lsr and does this masking anyway, so this line seems redundant...
But maybe it's just an optimization that I don't understand.
In this case, I'd like to know more about it.
Thanks! :)
Well, the code comes from an hardware design, where stages could be described as "read", "ALU", "update flags" (and "write" in this case), so, written this way, it reflects these hardware stages.
I guess there's room for improvement / optimisation at many places, but I'd rather keep consistency over efficiency for that very single reason.
Regards