News for hackers. Stuff that matters.
Re:Nestur: NES Emulator in Rust by mkagenius on Tuesday January 07, 2020 @12:48AM (#21977285)
ADC = A + V + C

SBC = A - V - (1 - C)

SBC = A + (-1 * (V - (1 - C)))

SBC = A + (-V + (1 - C))

SBC = A + -V + 1 + C

I am not able to follow the mathemagic behind the third line. Is it normal algebra or its in 1's complement?

Re:Nestur: NES Emulator in Rust by carlmr on Tuesday January 07, 2020 @01:21AM (#21977448)
I think it looks odd, too. If you go with this:

    ADC = A + V + C
    SBC = A - V - (1 - C)
    SBC = A + (-1 * (V - (C - 1)))
    SBC = A + (-V + (C - 1))
    SBC = A + -(V + 1) + C
"The SBC instruction subtracts a value from the accumulator register, with an extra 1 subtracted from the result if the carry flag is NOT set."

The explanation fits though. If the carry flag is not set (C == 0) then you subtract an extra 1. If C == 1 then it's A-V.

Re:Nestur: NES Emulator in Rust by ltriant on Tuesday January 07, 2020 @01:14AM (#21977419)
Wow, yeah, that's a bit off. And incorrect. It should end up being equivalent to:

SBC = A - V - 1 + C

Thanks. I'll fix this up :)

Re:Nestur: NES Emulator in Rust by mkagenius on Tuesday January 07, 2020 @01:24AM (#21977471)
> but the - 1 is taken care of because of the fact that ADC and SBC do the opposite thing with the carry flag.

Also, this line is bothersome, they do opposite things but that's why you have already done (1 - C).

Maybe the fact that -V is actually (!V + 1) (in 2's complement) that eats up the extra -1 is the correct explanation?

So,

  SBC = A - V - 1 + C

  SBC = A + !V + 1 - 1 + C

  SBC = A + !V + C
Re:Nestur: NES Emulator in Rust by ltriant on Tuesday January 07, 2020 @01:45AM (#21977589)
Ah yeah you’re right. I had a brain fart and got my -V and !V confused.

At the time of writing I was sure I had it right too!

I’ll fix that up. Thanks :)