Writing a RISC-V Emulator in Rust

(book.rvemu.app)

68 points | by signa11 8 hours ago

3 comments

  • timhh 58 minutes ago
    > RISC-V has either little-endian or big-endian byte order.

    Yeah though for instruction fetch it's always little endian. I honestly think they should remove support for big endian from the spec. As far as I know nobody has implemented it, the justification in the ISA manual is very dubious, and it adds unneeded complexity to the spec and to reference models.

    Plus it's embarrassing (see Linus's rant which I fully agree with).

  • quantummagic 6 hours ago
    Just a heads-up that only the first three chapters are available so far. Apparently there will be ten, when finished.
    • sylware 6 hours ago
      It's not the right move, better do it in assembly. I have a little rv64 interprer written in x86_64 assembly.
      • trollbridge 3 hours ago
        If you're going to make this argument, I'd consider arguing for Zig a little more substantiated; Rust is cross-platform and x86_64 assembly certainly isn't. Most of my day to day computing is done on ARM platforms as are some of my server resources, and I expect that to expand as time goes on.
        • sylware 2 hours ago
          Your use case is totally out of scope of my project.

          Look down below in the comments where I do reference one written in plain and simple C from the creator of ffmpeg and q-emu.

          • p_j_w 48 minutes ago
            > Your use case is totally out of scope of my project.

            You have a completely different use case from the OP, but still had no problem telling them that they were doing it wrong, so it’s pretty funny to see you use this line of defense for your choice.

        • timhh 2 hours ago
          I think assembly is probably a pretty bad choice for a RISC-V emulator. It's not portable, a nightmare to maintain, and not even as fast as binary translation.

          What kind of performance do you get?

          I guess it would be a great way to learn about the differences between x86 and RISC-V though!

          • sylware 2 hours ago
            I am not looking for performance (it will run natively on rv64 hardware), I am looking to protect the code against computer language syntax/compiler planned obsolescence (usually cycles of 5-10 years).

            Have a look a little bit below in the comments where I give a reference to another one, written by the creator of ffmpeg and q-emu.

            • jcranmer 1 hour ago
              Honestly, assembly language bitrots far faster than other programming languages. In my lifetime, the only thing that really comes close to qualifying as "compiler language syntax/compiler planned obsolescence" is Python 2 to Python 3. In contrast, with x86 alone, there's three separate generations of assembly language to go through in that same timeframe.
              • timhh 55 minutes ago
                Python still doesn't do very well on backwards compatibility & bitrot, even after 3. They're constantly deprecating and removing things.

                https://docs.python.org/3.12/deprecations/index.html

                This obviously improves Python, but also it means you absolutely shouldn't choose Python if you are looking for a low maintenance language.

              • timhh 1 hour ago
                I'm pretty sure Rust is going to outlast x86! C definitely will.
            • pjmlp 4 hours ago
              Real life example, in Android 7 Google re-introduced an interpreter for DEX bytecodes, manually written in Assembly, throwing away the old one that existed until Android 5, written in C.
              • sylware 2 hours ago
                Well, if true, those people at gogol did the right thing... if all the others could behave the same way...
                • pjmlp 2 hours ago
                  If true? I usually only comment stuff I can post profs on, so is the Internet nature.

                  > Interpreter performance significantly improved in the Android 7.0 release with the introduction of "mterp" - an interpreter featuring a core fetch/decode/interpret mechanism written in assembly language

                  From https://source.android.com/docs/core/runtime/improvements

              • andsoitis 4 hours ago
                > It's not the right move, better do it in assembly. I have a little rv64 interprer written in x86_64 assembly.

                Published your source code?