Skip to main content
gamedev.net
🔒 Locked

Memory operations for VM

Started by kapru Nov 21, 2004 at 1:47 AM 2 replies 650+ views
Original Post
kapru
kapru
Ive got the stack for my VM working ok. Now all i need to to create a memory space for variables to live in. Each program will have a max of 128k to work with. It should be more than enough for ingame scripts. Now how would i declair a block of memory 128k in size and not have to assign a certail type for it e.g i dont want 128k worth of int's i want to be able too access the memory like you do with x86 assembler
stevie56
stevie56
I used an array of byte, coupled with a variables table which held the names, types, sizes and pointers into the array. Strings were given blocks of bytes to a given maximum length.

This works for global variables well enough, but not local variables. For them, I used a second variable table and byte array. Not very efficient, I know, but I was working to a College deadline.

Hope this helps.
StevieDon't follow me, I'm lost.
kapru
kapru
Any chance you can give an example on how to create the memory array and store and retrieve data from it.

I'll be implementing variables pritty soon. My assembler currently is extracting and processing 44000 lines of code in about 9 seconds. ive got 2 more passes to code and i've made my first very basic assembler.
Then its off to make the runtime environment.

THen i get to make a basic compiler for it
evolutional
evolutional
unsigned char *pMemoryBlock = new unsigned char[ SIZE_OF_BLOCK ];

That will allocate a block of memory sized at SIZE_OF_BLOCK - probbaly best to define a constant for that value.

Tou can then store a lookup table for your variables:

enum vmVariableType{  TYPE_BYTE,  TYPE_INT,  TYPE_FLOAT,  TYPE_STRING,  TYPE_USER};struct vmVariable{  vmVariableType type;  unsigned int stackOffset;  unsigned int size;};


You can store your variables in a std::map structure with a char* name as a key and get away without storing name in the variable;

When assigning a variable, you would create the relevant vmVariable structure - allocate a portion of your memory top the variable - say your stack pointer is at 0;

vmVariable *var = new vmVariable;var->type = TYPE_INT;var->size = sizeof(int);var->stackOffset = nStackOffset;// Ensure you move onto your next available block of stack memorynStackOffset += sizeof(int);// Assigning the variable:pMemoryBlock[ var->stackOffset ] = 1234;


Of course, this is a very simple example and wouldn't work too well in the real world - there's no checking for overflows or anything like that. I'd recommend that you wrap the memory object in a class and make it self managing. You'll need to design a system that manages the memory, maintaining lists of free/allocated areas and making sure that things are correctly free'd when a variable is deleted or allocated.

Check out the C++ Scripting machine tutorial in the articles section - it's useful to understand what you're trying to achieve.

Topic Locked

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

Sign in to reply to this topic.