kosarev / z80

Fast and flexible Z80/i8080 emulator with C++ and Python APIs
MIT License
65 stars 10 forks source link

Support the SCF/CCF behaviour discovered by Patrik Rak #42

Open kosarev opened 3 years ago

kosarev commented 3 years ago

The test: https://github.com/raxoft/z80test/blob/master/src/z80ccf.asm

The original discussion on WoS: https://worldofspectrum.org/forums/discussion/41704/scf-ccf-flags-new-discovery

kosarev commented 2 years ago

After 912d1bf, it seems we now can formally prove that at least what concerns the A and F register values, Patrik and Woody were right.

$ ./z80sim.py
ccf reg_f3                                
old state f3                              
new state (or f3 a3)                      
expected f3                               
22-07-10 22:41:22       Traceback (most recent call last):
  File "./z80sim.py", line 1997, in <module>
    main()
...
kosarev commented 2 years ago

Hmm, the simulation indicates that not all instructions updating flags cause the following scf/ccf to set flags 3 and 5 to A instead of (A | F), e.g., cpl and rlca/rrca/rla/rra: https://github.com/kosarev/z80/blob/a36c40a51da9a5fa8459e2977136deaddbc71ea0/tests/z80sim/z80sim.py#L2178

But in the same time somehow the tests from Patrik do seem to test these instructions and (I presume) still pass: https://github.com/raxoft/z80test/blob/master/src/tests.asm#L73

This clearly needs some more looking into it.

kosarev commented 2 years ago

Simulation indicates that whether the corresponding bits of F participate in the values of flags 3 and 5 of scf/ccf depends on the opcode the instruction latch contains at the beginning of execution of these instructions. Symbolising the latch further confirms that amongst unprefixed instructions F does not contribute to the flags only after scf/ccf themselves, alu r/n, inc/dec r and add hl, rp. What happens with the latch during processing of prefixed instructions is yet to be understood.

The way things look at the moment, the logic has little to do with updating flags 3 and 5 or any other flags and is dictated by the contents of the latch.

https://github.com/kosarev/z80/blob/0b56aea80b96084c1381a626e19a91997571d903/tests/z80sim/z80sim.py#L2253

redcode commented 2 years ago

Hello, we have spent the last year researching the flag behavior of the ccf/scf instructions. Please, join our discord server, we would like to talk with you: https://discord.gg/N3exr4Z6

Patrik, Woody, Hoglet, TonyB and others are on our server.

kosarev commented 2 years ago

This clearly needs some more looking into it.

https://github.com/kosarev/z80/issues/51#issue-1410254943 may probably explain this.