Skip to main content
gamedev.net
🔒 Locked

Free Virtual Machine!

Started by Max_Payne 13 replies 1.1k views
Original Post
Max_Payne
Max_Payne
I'm posting this here so that as many people as possible see it. So, yeah, some time ago, I remember seeing a tutorial about a virtual machine. The problem is, that virtual machine didnt do much. It only handled integers, and performed basic mathematical operations. I then decided to go on and code my own virtual machine as an experiment, and this VM of mine has come to a state where it can actually perform useful things. And I have decided to offer it for free. Features: - Complete C++ source code, fully commented - The ability to compile ASM script files (Lightweight ASM compiler) - The ability to run precompiled binaries - Bytecode execution (faster than an interpreter) - Very small bytecode size - Integer arithmetic - Floating-point arithmetic - 8 general purpose register - A general purpose data stack - A call stack to handle function calls - External procedure calls, to interface the VM with your projects To be implemented: - C/Basic like high level compiler - VM management of a "heap" memory area (currently, this could be done through external procedures only) - More stack memory management instructions - Fix more bugs that have yet to be found and will be found I am open to feedback on the code. You are all welcome to download it and check it out and use it. This is not released under the GPL either, so you can actually keep your projects closed source if you use it... Just please give me some credit ;) Download linky: http://www.xgameproject.com/downloads/xvm.zip [Edited by - Max_Payne on October 28, 2004 6:44:00 PM]

Looking for a serious game project?
www.xgameproject.com
Deslock1
Deslock1
Woah,
this is great!
I was looking to do a similar project few years ago, but never had time. Have you tought about using a Command design pattern with functors to handle the instructions and dynamicly keep track of the code flow? From that could emerge a self-writing, self-interpreting code for a basic AI system :)
Max_Payne
Max_Payne
Quote:
Original post by Deslock1
Woah,
this is great!
I was looking to do a similar project few years ago, but never had time. Have you tought about using a Command design pattern with functors to handle the instructions and dynamicly keep track of the code flow? From that could emerge, self-writing, self-interpreting code for a basic AI system :)


I'm not sure if I understand your point. If you mean using a function to handle each instruction individually, that seems like it would be quite slow. But the CodeStream class included in the source code allows for some code rewriting potential. The VM could actually rewrite some instructions and parameters as it executes it. And the CodeStream class could eventually be extended to insert new code portions without compromising the code execution (but you would have to take care of jump/data indices). You could still insert code after the end of the scripts without problems though.

I also found part of the articles I was reffering to, for comparison:
http://www.gamedev.net/reference/articles/article1633.asp
http://www.gamedev.net/reference/articles/article1686.asp
http://www.gamedev.net/reference/articles/article1877.asp

Looking for a serious game project?
www.xgameproject.com
Max_Payne
Max_Payne
Quote:
Original post by capn_midnight
hmm, fibonacci.bin does nothing when I run it. I'll see if I can make heads-or-tails of the code.


Someone else said the same thing yesterday. It actually runs, bug XVM being a console program, windows closes it when its execution is done, so you have to run it in command line to actually see the output. It stores the end result into register 0.

Looking for a serious game project?
www.xgameproject.com
Max_Payne
Max_Payne
Quote:
Original post by capn_midnight
ah, I see.

your assembly code is incredibly easy to figure out.


Is that a true impression or a sarcasm? I had never actually done assembly before. So my ASM compiler is rather simplistic. I tried to use standard instruction names as much as possible to avoid confusing people. The best way to understand the code is to look at the bytecode reference in codestream.h.

Looking for a serious game project?
www.xgameproject.com
capn_midnight
capn_midnight
Quote:
Original post by Max_Payne
Quote:
Original post by capn_midnight
ah, I see.

your assembly code is incredibly easy to figure out.


Is that a true impression or a sarcasm? I had never actually done assembly before. So my ASM compiler is rather simplistic. I tried to use standard instruction names as much as possible to avoid confusing people. The best way to understand the code is to look at the bytecode reference in codestream.h.

No, I'm serious. I think this might actually be a fairly good introductary ASM in the context of an intro to computer architecture class. It's very simple, and the syntax is easy to understand. Plus, manipulation of the registers and stack are quite easy, which I think are fundamental concepts that any beginner CS should understand.
Max_Payne
Max_Payne
Quote:
Original post by capn_midnight
Quote:
Original post by Max_Payne
Quote:
Original post by capn_midnight
ah, I see.

your assembly code is incredibly easy to figure out.


Is that a true impression or a sarcasm? I had never actually done assembly before. So my ASM compiler is rather simplistic. I tried to use standard instruction names as much as possible to avoid confusing people. The best way to understand the code is to look at the bytecode reference in codestream.h.

No, I'm serious. I think this might actually be a fairly good introductary ASM in the context of an intro to computer architecture class. It's very simple, and the syntax is easy to understand. Plus, manipulation of the registers and stack are quite easy, which I think are fundamental concepts that any beginner CS should understand.


Really? Well thanks. I actually got inspired from RISC processors. I actually store opcodes as single bytes in the bytecode (enforcing the fact that there should never be more than 256 instructions). The instruction set is relatively small. I don't consider it fully complete yet (I will add at least an instruction to copy strings of bytes from the stack to the stack). But thats pretty much it, I'm not going to be adding much else, to keep it simple. Overall I would say that my VM is pretty simple, but thats part of the point. It should be simple and complete.

I will eventually make a C/Basic like compiler for the VM. I am thinking of actually making it output ASM code while preserving the comment structure (this will make it easier to debug).

If you want to use the VM for any of your projects, feel free to do it, as long as you give me some credit somewhere ;)

Looking for a serious game project?
www.xgameproject.com
Max_Payne
Max_Payne
Only two people bothered to look at the code? Come on, I need some constructive criticism here.

Looking for a serious game project?
www.xgameproject.com
Ravuya
Ravuya
You should look into QEMU. It has blazingly fast emulation of pretty much everything on the x86 architecture; I can emulate around what I estimate to be a P3/400 on my 1.33GHz G4.
Max_Payne
Max_Payne
Quote:
Original post by Ravuya
You should look into QEMU. It has blazingly fast emulation of pretty much everything on the x86 architecture; I can emulate around what I estimate to be a P3/400 on my 1.33GHz G4.


Well, I wanted to program a VM to see how it was done and have full control over it. Just using something thats already implemented wouldn't have allowed me to learn as much. If I was aiming for performance, I suppose the most optized design would have been a JIT compiler.

Has anyone else checked the source code? I would be interested in getting feedback about my design.

Looking for a serious game project?
www.xgameproject.com
evolutional
evolutional
How's it handle external (host) function calling? If anything, I'd change the CAssembler class and add a function to accept a pre-read string buffer. Does it handle multiple VM contexts (or threads) - the ability to keep compiled scripts and their current execution state would be a useful feature for a VM. Also, your CAssembler::Compile function is huge - it's a shame you can't split it up to handle each opcode, perhaps an lookup table of functors and an in-built FSM would make the code more managable. That said, your code is nicely laid out and commented and isn't too hard to figure out. I'd be interested to see how you implement a higher level language to run on this [smile]

Looks decent so far - keep on rollin' !
grekster
grekster
ill have a look at it but it sounds far more complex then i could possibly make a useful comment on at the moment.

How come the three times I read your thread title I read "Free Virtual Memory" :S!!
Quote: Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
Max_Payne
Max_Payne
Quote:
Original post by evolutional
How's it handle external (host) function calling? If anything, I'd change the CAssembler class and add a function to accept a pre-read string buffer. Does it handle multiple VM contexts (or threads) - the ability to keep compiled scripts and their current execution state would be a useful feature for a VM. Also, your CAssembler::Compile function is huge - it's a shame you can't split it up to handle each opcode, perhaps an lookup table of functors and an in-built FSM would make the code more managable. That said, your code is nicely laid out and commented and isn't too hard to figure out. I'd be interested to see how you implement a higher level language to run on this [smile]

Looks decent so far - keep on rollin' !


It handles external functions through a system similar to window's GetProcAddress(). You can request the "address" of a procedure, and store it. Once you have it, you can call the procedure by its Id number and pass it parameters through the stack. Procedures can also return parameters on the stack.

As for threads, I've thought about it, but the VM is simpler without support for that. You can simply create multiple VM instances and run them in separate threads. Scripts can communicate with each other through custom external procedures.

I don't see how the ASM compile function is a real problem though. Each opcode is handled in order in an easily understandable manner. Obviously, for my C/Basic compiler, I will have to use multiple functions, but for ASM... I wanted to keep things simple. Plus the compiling is performed in a single pass.

Looking for a serious game project?
www.xgameproject.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.