emu-russia / dmgcpu

DMG CPU Reverse Engineering
Creative Commons Zero v1.0 Universal
29 stars 4 forks source link

Value of registers are stored inverted. #240

Closed Rodrigodd closed 5 months ago

Rodrigodd commented 6 months ago

From #238. To fix it there, I removed the not operator from the assignment to Z_in and W_in, but I don't know how to confirm if this should be the actual fix, there are many other possible positions where the signal could be inverted or not (the registers could for example store the value of the registers inverted and then straight them when loading).

Below is my investigation of the flow of data from the databus to the registers, extracted from investigating.md in the linked PR, with added screenshots.


HL is inverted in the address bus

H and L are stored in ReqH/r3q and ReqL/r2q, respectively, inverted. H can go to abus, bbus or dbus, and L can go to abus, bbus or cbus. They go to the bus through an inverter, so they have they original value.

In this instruction, H goes to dbus and L goes to cbus. dbus cbus go through an BusKeeper, and them inverted again into the AdressBus (or A).

From the comments in IDU.v, the code expects that the internal register actually holds the straight value of the ISA registers, but they are inverted in the current form. This was also seen in the JP instruction, and I fix that there by inverting the Zbus, but maybe we need to invert something more fundamental, like the value read from the memory?

Path from databus to register A

So RegA/r1q contains the inverted value of A.

image

In screenshot, the execution of the instruction LD A, $BC (3e bc).

Path from databus to register HL

So HL are also stored inverted in the registers.

image

In screenshot, the execution of the instruction LD HL, $DE80 (21 80 de).

Path from databus to register SP

So SP is also stored inverted in the registers.

image

In screenshot, the execution of the instruction LD SP, $AACC (31 cc aa).

Path from databus to PC

So PC is also stored inverted in the registers.

image

In screenshot, the execution of the instruction JP $0112 (c3 12 01).

ogamespec commented 6 months ago

Looks like the error is indeed in decoder3. I downloaded old results comparing my decoder and Gekkio decoder and the difference is really in the x35 signal for opcode 0x77:

image

The x35 command is exactly used to output the value of the A register to the DV bus (Gekkio: s3_oe_areg_torbus). Crap ><

ogamespec commented 6 months ago

Fixed typo in decoder3, got better, but now the external data bus writes Zz :)

image

ogamespec commented 6 months ago

Also my decoder is now 1 to 1 with the Gekkio decoder:

image

ogamespec commented 6 months ago

253

@Rodrigodd That's all for now :) Please see how it turned out.

image

ogamespec commented 6 months ago

Oops, I got the D and DL mixed up. The external data bus is still HighZ.

ogamespec commented 6 months ago

It turns out I closed #239 early Now I have problems with the external data bus (Z) again. I'll open it back up and go back to the data_mux edits =)

ogamespec commented 5 months ago

@Rodrigodd Fixed, edits online.

ogamespec commented 5 months ago

On this Issue, I will do some more research on register and bus connections using your work (but that's for the wiki).

ogamespec commented 5 months ago

260

@Rodrigodd, please see I have analyzed circuits and found an additional inverter at the output of Z/W registers 🙈

Rodrigodd commented 5 months ago

Tested it in dmg-sim and everything appears to be working as expected. Thanks again!