Skip to content

Run an opcode returning an expression - #2

Open
jkoudys wants to merge 13 commits into
DarylPinto:mainfrom
jkoudys:main
Open

Run an opcode returning an expression#2
jkoudys wants to merge 13 commits into
DarylPinto:mainfrom
jkoudys:main

Conversation

@jkoudys

@jkoudys jkoudys commented Jan 7, 2021

Copy link
Copy Markdown
Contributor

Here's a neat way to keep it clean. Deeply nested control flow (eg returns that are 2-match-levels deep) are hard to follow and create fast-outs that can be tricky to handle. We can use a Result so that opcodes that don't match can always be returned. This separates the formatting and branching of an error into something you can format and handle as you see fit.

Since we're already making a Result, rather than a Result<(), Error> (or possibly an Option<()> that maps an error if no op was found), we might as well return something on that expression. PC updates seem to be the big thing chip8's figuring out in these calls, so we can make that an expression and lighten the number of mutations we need to figure out. Using an Option<()> in there might save us from the default pc + 2 being written a bunch, but in the grand scheme it's not a huge deal.

I noticed you were inconsistent with some 0x00 hex noting, with a u16 as 0x00 instead of 0x0000. It's a common style preference for writing emulators, but if not consistent, just make them 0.

Threw in some named params on the debug formatter. which keeps the output string readable on its own, separate from the input data. Also saves a dozen ?s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant