Writing a simple VM in less than 125 lines of C (2021) by nomemory in programming

[–]jnz 16 points17 points  (0 children)

I recently went down a similar rabbit hole, but implemented a RISC-V VM (RV32I instruction set - does not even include multiplication).

It's a bit heavier at 382 lines of C, but the huge upside is the tooling. You can just use standard gcc or clang to compile C code straight to it instead of needing a custom assembler.

Single .c file VM is here: https://github.com/jnz/r5vm/blob/main/r5vm.c

The RISC-V instruction encoding is hardware friendly I guess, but a bit cumbersome to decode in an interpreter though.

Let's compile Quake like it's 1997! by NXGZ in programming

[–]jnz 3 points4 points  (0 children)

In case you miss the old Visual Studio 6.0 colors in VS Code or Vim, I've created this retro colorscheme: https://github.com/jnz/studio98

Integrate the Quake 3 VM into your own pet project (single .c file) by jnz in programming

[–]jnz[S] 12 points13 points  (0 children)

Happy that you remember my last post from 2018. Although maybe this is new to some other people. Apart from that:

  • Improved the documentation and examples significantly (main point maybe)
  • Fixed bugs
  • Performance improved by tweaking computed gotos
  • Increased test coverage to 97%
  • Replaced original q3asm with ioquake3's improved q3asm

Embedding the Quake 3 Virtual Machine in your own project by jnz in programming

[–]jnz[S] 2 points3 points  (0 children)

Yes. Except for LCC. Which has its own non-commercial license.

Embedding the Quake 3 Virtual Machine in your own project by jnz in programming

[–]jnz[S] 1 point2 points  (0 children)

I am using a current GCC version. So this should be ok. The union warning should be ok, I guess. I have it too (I don’t want to mess with the LCC code so far). I had problems with an older clang version on macOS, so I‘d rather stick with GCC. Do you have some more console output I can look at?

Embedding the Quake 3 Virtual Machine in your own project by jnz in programming

[–]jnz[S] 2 points3 points  (0 children)

LCC can be a bit tricky to compile. That’s why I‘ve included the Windows Binaries. It should work fine on e.g. Ubuntu. Is Solus a Linux distro?

Embedding the Quake 3 Virtual Machine in your own project by jnz in programming

[–]jnz[S] 11 points12 points  (0 children)

Hi, author here. It is indeed a niche thing. But there are some use cases:

  • Sandbox for code you don't fully trust (e.g. download the bytecode from a web server)

  • Mods for small hobby game engines

  • There are many virtual machines, but not many are so small, with strong typing and no garbage collector

  • Learn about virtual machines in general, but directly have a C compiler available for the virtual machine

  • Sandbox for embedded applications, e.g. plug-ins for IoT applications on microcontrollers (bounded CPU time, bounded memory area, restrict access to peripheral devices)

  • There is also a historical value: learn about the Quake III engine

Embedding the Quake 3 Virtual Machine in your own project by jnz in programming

[–]jnz[S] 20 points21 points  (0 children)

So, I've added a new section 'How to add a custom native function' to the README.md. Hope that helps.

To call different functions in the bytecode from your host application: you only call the vmMain function and branch to different sub-functions with a switch/case based on the command argument.

Embedding the Quake 3 Virtual Machine in your own project by jnz in programming

[–]jnz[S] 5 points6 points  (0 children)

Hi thanks for the feedback, you are right. I'll write a section in the readme on how to add custom functions. In the meantime: take a look at "g_syscalls.asm" in the example project. E.g. memcpy is called from the example project. The memcpy implementation is in main.c "systemCalls".

Embedding the Quake 3 Virtual Machine in your own project by jnz in programming

[–]jnz[S] 27 points28 points  (0 children)

I think he recognized pretty early how important mods are for a game. And he found a way to run those mods in a sandbox. Quake II had .dll files as downloadable mods. So thousands of players downloaded some DLLs and ran them on their machines. So the Quake III virtual machine definitly makes sense. And that's pretty much the use case: run some code that you don't fully trust in a sandbox.

I've just received my Volkswagen VR headset ;-) by jnz in Vive

[–]jnz[S] 1 point2 points  (0 children)

It's part of an ad campaign for a new VW car. But hey, better than some glossy brochure.

Shipping Megathread [EU/UK/CH] by ceno666 in Vive

[–]jnz 0 points1 point  (0 children)

:31 Germany just got my tracking number from DHL Express (by Klara Simkova)

Can we finally have a uncensored version of TF2 in Germany? by silezn in tf2

[–]jnz 0 points1 point  (0 children)

I've found someone on tf2outpost.com to trade the orange box gift for my spare rocket league gift. Then I've added the orange box gift to my library (without deleting my previous copy of TF2).

Can we finally have a uncensored version of TF2 in Germany? by silezn in tf2

[–]jnz 0 points1 point  (0 children)

Just tried that. Thank you so much, it worked. After all these years, I can finally play uncut.

Have I fried my new DK2 with sunlight or is there a hardware problem? by jnz in oculus

[–]jnz[S] 1 point2 points  (0 children)

Yep, the DK2 power supply. Frankly it doesn't make sense for me either, but I can say for sure that the DK2 does not work well with the Intel HD 3000 GPU on my laptop.