tree5673 wrote:
My recent projects used a while loop that ran until a halt code.
The loop would contain if statements that would use bit-wise operations as conditions to
read the opcode and find out which instruction to execute. The body of the if statements
would be the instructions themselves. The Body could further decode the instruction.
I used this model in my 8080 emulator, which was successful.
That's always the idea (loop till halt).
An obvious way to optimize is to plan for a decoder cache. As you don't want to repeatedly decode the same instructions, especially for something as complex as x86. AFAIK even the real hardware make use of this method, i.e. they decode the architectural instructions and cache the resulting uops.
tree5673 wrote:
For my 8086 project, this method seems too difficult and error prone.
How should I implement the instruction set?
Why not steal another idea from the real hardware? Implement uops (aka simple helper functions that aren't too difficult and error prone), decode x86 instructions into them and run them instead. Would fit nicely with the cache idea as well later if you want to take that route.
Also take a look here:
https://codegolf.stackexchange.com/ques ... l-8086-cpuMany fancy ways to "abuse" various language features, most answers are only a few hundred SLOC with proper spacing and comments.
But I decided to cheat instead