How about a RISC-V disassembler in 200 lines of C99?<p><a href="https://github.com/andportnoy/riscv-disassembler/blob/master/das.c">https://github.com/andportnoy/riscv-disassembler/blob/master...</a>
Why stick with c89? Can't think of any compilers that doesn't support c99 nowadays. The major benefit is that you can use uint8_t and friends directly and don't need to define your own wrapper types.
Seeing the RISC-V instructions implemented in the emulator like that, it comes to my mind that RISC-V is really a reduced instruction set CPU.<p>When compared to AVR 16-bit RISC instruction set, RISC-V looks so much simpler. (You may be indirectly familiar with AVR architecture by the household name "Arduino".)<p>The intriguing part is that AVR is just a microcontroller, while RISC-V is intended to be a full-blown CPU.
I feel myself descending into old-fartitude more and more with every year. My wife and I were recently involved in a car accident (no one was hurt). While I was being checked out I overheard a 20-year-old firefighter exchange Facebook information with an 18-year-old EMT. I was like, "wait a minute, you guys seem really young and you still use Facebook? I thought Facebook was for your grandparents and all the kids now use Snapchat or TikTok?"<p>I get that same feeling now. This kid is 20 and still using C89? Shouldn't people his age have been reared entirely in the crystal-spires-and-togas utopia of Rust, with raw pointers and buffer overruns being mere legends of their people's benighted past told to them by their elders?<p>It's kind of comforting to see young programmers embracing the old ways, even if it's for hack value only.
Question: do the implementation of single instructions compile to single instructions if targeting RISC-V with optimisations enabled? That would be really awesome if compilers realise what your code is doing and replace the implementations of instructions with those instructions.
'switch' is a really, really nice language construct that was fully implemented long before C89. Using lots of nested 'if's instead is not a good idea.
This proves that at the core. The things we rely on to achieve great software and life impacting technologies are extremely simple. The complexity is that how to make them.
A bigger implementation, but has 64bits support:<p><a href="https://bellard.org/tinyemu/" rel="nofollow">https://bellard.org/tinyemu/</a>
Besides not using a switch() for the main instruction decode, there's nothing surprising here. Anyone who has worked with emulators before will find this code straightforward to read. RISC-V really is the new MIPS.