JohnEarnest / Octo

A Chip8 IDE
MIT License
683 stars 55 forks source link

Expected order of operations for vf #171

Closed r-downing closed 1 year ago

r-downing commented 1 year ago

Hello!

I noticed some potential inconsistencies between your implementation and others I've seen online for vf, so I was wondering which is correct, and if you have any concrete documentation on this.

Here's a short example program:

: main

if v3 > v4 then clear

In Octo it compiles to

8F40  # vf = v4
8F35  # vf -= v3
4F00  # if vf != 00, skip next
00E0  # clear

So if I'm interpreting correctly, you're

Looks like you expand these comparison expressions by using vf as a temporary reg for the subtraction and the no-borrow flag.

But is it guaranteed that vf gets updated to the no-borrow value after it's set to the result of subtraction?

In some chip8 emulators I've seen (and my own when I first implemented it), they first check for borrowing, then set vf, and then set vx to the subtraction result after. But in this case vf and vx are the same, so they'd be overwriting the borrow immediately.

I was wondering which way is correct? Or if there even is a correct/standard way, when vf is both the destination for the result and the carry/borrow

JohnEarnest commented 1 year ago

Octo's behavior for flag-results is designed to match CHIP8 on the COSMAC VIP. Try running this test program on a VIP emulator if you wish to confirm: https://github.com/JohnEarnest/Octo/blob/gh-pages/examples/tests/testquirks.8o#L38-L45

Years ago, this behavior had a configurable quirks-mode flag, but I removed it from the UI; Prior to Octo's existence there was little evidence of programs which depended on either interpretation, and I didn't want to encourage the creation of future CHIP8 programs which took a dissenting opinion.

r-downing commented 1 year ago

Good to know, thanks for the quick response!