Page 1 of 1

not understanding Klaus' test

Posted: Thu Mar 03, 2022 10:23 am
by flok
Hi,

I'm running Klaus' excellent functional test against my emulator.
At test 2Ah (42) something goes wrong:

Code: Select all

32f4[f8], 2a, a: 2a, x: 0e, y: ff, flags: 71, sp: 01ff, BS: 00 SED
32f5[a2], 2a, a: 2a, x: 0e, y: ff, flags: 79, sp: 01ff, BS: 00 LDX #$0e
32f7[a0], 2a, a: 2a, x: 0e, y: ff, flags: 79, sp: 01ff, BS: 00 LDY #$ff
32f9[a9], 2a, a: 2a, x: 0e, y: ff, flags: f9, sp: 01ff, BS: 00 LDA #$99
32fb[85], 2a, a: 99, x: 0e, y: ff, flags: f9, sp: 01ff, BS: 00 STA $0d
32fd[85], 2a, a: 99, x: 0e, y: ff, flags: f9, sp: 01ff, BS: 00 STA $0e
32ff[8d], 2a, a: 99, x: 0e, y: ff, flags: f9, sp: 01ff, BS: 00 STA $0203
3302[85], 2a, a: 99, x: 0e, y: ff, flags: f9, sp: 01ff, BS: 00 STA $0f
3304[a9], 2a, a: 99, x: 0e, y: ff, flags: f9, sp: 01ff, BS: 00 LDA #$01
3306[85], 2a, a: 01, x: 0e, y: ff, flags: 79, sp: 01ff, BS: 00 STA $0c
3308[85], 2a, a: 01, x: 0e, y: ff, flags: 79, sp: 01ff, BS: 00 STA $10
330a[a9], 2a, a: 01, x: 0e, y: ff, flags: 79, sp: 01ff, BS: 00 LDA #$00
330c[85], 2a, a: 00, x: 0e, y: ff, flags: 7b, sp: 01ff, BS: 00 STA $12
330e[8d], 2a, a: 00, x: 0e, y: ff, flags: 7b, sp: 01ff, BS: 00 STA $0204
3311[38], 2a, a: 00, x: 0e, y: ff, flags: 7b, sp: 01ff, BS: 00 SEC
3312[20], 2a, a: 00, x: 0e, y: ff, flags: 7b, sp: 01ff, BS: 00 JSR $339f
339f[08], 2a, a: 00, x: 0e, y: ff, flags: 7b, sp: 01fd, BS: 00 PHP              push status
33a0[a5], 2a, a: 00, x: 0e, y: ff, flags: 7b, sp: 01fc, BS: 00 LDA $0d
33a2[65], 2a, a: 99, x: 0e, y: ff, flags: f9, sp: 01fc, BS: 00 ADC $0e  [99]
33a4[08], 2a, a: 99, x: 0e, y: ff, flags: b9, sp: 01fc, BS: 00 PHP              push status
33a5[c5], 2a, a: 99, x: 0e, y: ff, flags: b9, sp: 01fb, BS: 00 CMP $0f  [99 versus 99]
33a7[d0], 2a, a: 99, x: 0e, y: ff, flags: 3b, sp: 01fb, BS: 00 BNE $33a7
33a9[68], 2a, a: 99, x: 0e, y: ff, flags: 3b, sp: 01fb, BS: 00 PLA              pull a
33aa[29], 2a, a: b9, x: 0e, y: ff, flags: b9, sp: 01fc, BS: 00 AND #$01
33ac[c5], 2a, a: 01, x: 0e, y: ff, flags: 39, sp: 01fc, BS: 00 CMP $10  [01 versus 01]
33ae[d0], 2a, a: 01, x: 0e, y: ff, flags: 3b, sp: 01fc, BS: 00 BNE $33ae
33b0[28], 2a, a: 01, x: 0e, y: ff, flags: 3b, sp: 01fc, BS: 00 PLP              pull status
33b1[08], 2a, a: 01, x: 0e, y: ff, flags: 7b, sp: 01fd, BS: 00 PHP              push status
33b2[a5], 2a, a: 01, x: 0e, y: ff, flags: 7b, sp: 01fc, BS: 00 LDA $0d
33b4[e5], 2a, a: 99, x: 0e, y: ff, flags: f9, sp: 01fc, BS: 00 SBC $12  [99 -= 00]
33b6[08], 2a, a: 98, x: 0e, y: ff, flags: b8, sp: 01fc, BS: 00 PHP
33b7[c5], 2a, a: 98, x: 0e, y: ff, flags: b8, sp: 01fb, BS: 00 CMP $0f  [98 versus 99]
33b9[d0], 2a, a: 98, x: 0e, y: ff, flags: b8, sp: 01fb, BS: 00 BNE $33b9
LOOP AT 33b9
at address 33b4 00h is substracted from a which is 99h at that time. The flag-register is F9h so decimal and carry are set.
My emulator calculates 98h of this, the test expects 99h though. I don't understand this.

Is it that maybe SBC does not do the subtraction if the value is 0, even though carry is set? Or something else?

Re: not understanding Klaus' test

Posted: Thu Mar 03, 2022 10:29 am
by John West
It looks like you're calculating A - operand - carry instead of A - operand - (not carry). SBC with an operand of 0 and carry set should not change A, whether it's in decimal mode or not.

Re: not understanding Klaus' test

Posted: Thu Mar 03, 2022 10:41 am
by flok
John West wrote:
It looks like you're calculating A - operand - carry instead of A - operand - (not carry). SBC with an operand of 0 and carry set should not change A, whether it's in decimal mode or not.
Oh indeed!
Thanks

Re: not understanding Klaus' test

Posted: Thu Mar 03, 2022 11:23 am
by flok
Very cool: it now runs upto test $f0, so all is fine. Pretty quick (I think) for a python program (with pypy): 4.6 seconds om a "11th Gen Intel(R) Core(TM) i5-11400H @ 2.70GHz" laptop.

Re: not understanding Klaus' test

Posted: Thu Mar 03, 2022 12:54 pm
by BigEd
Splendid - and welcome!

Re: not understanding Klaus' test

Posted: Mon Mar 07, 2022 10:04 am
by nelbr
flok wrote:
John West wrote:
It looks like you're calculating A - operand - carry instead of A - operand - (not carry). SBC with an operand of 0 and carry set should not change A, whether it's in decimal mode or not.
Oh indeed!
Thanks
Just a quick note Flok, when I reached this point I thought I was done for a while, and then I noticed how poor my emulator behaved on decimal mode. Klaus has incorporated a separate test for decimal mode behavior (including flags and response to non-valid BCD inputs) and that has helped me complete my testing (though I later found out I needed to emulate non-documented opcodes, but that was a different story).

The few challenges I faced were:

Klaus decimal test are only available in assembly code (no binaries). So you will need to download the code and the assembler and assemble it yourself.

The assembly code requires updating which CPU behavior you want to check (as 6502, 65C02 and the 16 bit version all behave differently) and also which tests you want to run (accumulator results and each of the carry, zero, negative and overflow flags). I have set all to true, so my emulation was more precise. But you can do one by one to check as you improve.

Lastly, you may find a description of how ADC and SBC behave for each of these calculations under decimal mode on this page: http://6502.org/tutorials/decimal_mode.html . It was incredibly useful (despite being a bit difficult to understand) and it does contain all the information you might need to do this.

Hope this is useful.

Best Regards,
Nelson