jc-SpaceXp / cNES

NES emulator written in C and targeted for Linux.
zlib License
6 stars 4 forks source link

Simplify execute functions #12

Closed jc-SpaceXp closed 1 year ago

jc-SpaceXp commented 1 year ago

Currently, the execute functions are a bit all over the place in terms of clarity. See: the accumulator versions of instructions. Since most execute functions are just the final cycle they should be a short/simple as possible.

jc-SpaceXp commented 1 year ago

Examples:

Good: https://github.com/jc-SpaceXp/cNES/blob/dd8098cce783804012a1bef9c545c66e5838c915/src/cpu.c#L1215-L1223

jc-SpaceXp commented 1 year ago

Examples:

Bad: https://github.com/jc-SpaceXp/cNES/blob/dd8098cce783804012a1bef9c545c66e5838c915/src/cpu.c#L1412-L1434

avoid branching if possible: https://github.com/jc-SpaceXp/cNES/blob/dd8098cce783804012a1bef9c545c66e5838c915/src/cpu.c#L1452-L1464

jc-SpaceXp commented 1 year ago

Goal is to simplify the decoders (currently in progress, see: #7 and #8) and then the execute functions should be more consistent.

jc-SpaceXp commented 1 year ago

A lot of the issues stem from this decoder

https://github.com/jc-SpaceXp/cNES/blob/dd8098cce783804012a1bef9c545c66e5838c915/src/cpu.c#L798-L809

All other decoders let the execution function perform the read, however immediate address mode performs the read hence the weird check against the immediate address mode in the execute functions.

jc-SpaceXp commented 1 year ago

Other awkward issue is due to the accumulator, since the accumulator isn't a memory address there has to be a branch to either load the accumulator or the value from memory (whilst also writing back to that region). So far no suitable function can handle this behaviour hence the "awkwardness" in the execute functions.

This (18f1c52033a18af951aaabf7a4eb5dbd94b2fa90) was created to solve the accumulator issue, does require further changes for it to work properly. The idea is that if the address mode is the accumulator we read from there, otherwise read from memory (or X/Y registers).

https://github.com/jc-SpaceXp/cNES/blob/450c2d25ff7bafba3c4a09a56615db06cc92fc74/src/cpu.c#L282-L293

A similar function would be needed for writing back to the accumulator or memory (or X/Y).

jc-SpaceXp commented 1 year ago

Simplified instructions which support the immediate address

5b3cb3ccb7d3ce613bac63667f06f1fe086d9d28

jc-SpaceXp commented 1 year ago

Simplified instructions which support the accumulator address mode

8323a503a475e2f8f006323f2c46b53a830bfba5