• entries
    222
  • comments
    608
  • views
    588242

QuakeC VM

Sign in to follow this  
benryves

1266 views

I've started serious work on the QuakeC virtual machine.



The bytecode is stored in a single file, progs.dat. It is made up of a number of different sections:

  • Definitions data - an unformatted block of data containing a mixture of floating point values, integers and vectors.
  • Statements - individual instructions, each made up of four short integers. Each statement has an operation code and up to three arguments. These arguments are typically pointers into the definitions data block.
  • Functions - these provide a function name, a source file name, storage requirements for local variables and the address of the first statement.


On top of that are two tables that break down the definitions table into global and field variables (as far as I'm aware this is only used to print "nice" names for variables when debugging, as it just attaches a type and name to each definition) and a string table.

The first few values in the definition data table are used for predefined values, such as function parameters and return value storage.

Now, a slight problem is how to handle these variables. My initial solution was to read and write types strictly as particular types using the definitions table, but this idea got scrapped when I realised that the QuakeC bytecode uses the vector store opcode to copy string pointers, and a vector isn't much use when you need to print a string.

I now use a special VariablePointer class that internally stores the pointer inside the definition data block, and provides properties for reading and writing using the different formats.

/// Defines a variable.
public class VariablePointer {

private readonly uint Offset;

private readonly QuakeC Source;

private void SetStreamPos() { this.Source.DefinitionsDataReader.BaseStream.Seek(this.Offset, SeekOrigin.Begin); }

public VariablePointer(QuakeC source, uint offset) {
this.Source = source;
this.Offset = offset;
}

#region Read/Write Properties

/// Gets or sets a floating-point value.
public float Float {
get { this.SetStreamPos(); return this.Source.DefinitionsDataReader.ReadSingle(); }
set { this.SetStreamPos(); this.Source.DefinitionsDataWriter.Write(value); }
}

/// Gets or sets an integer value.
public int Integer {
get { this.SetStreamPos(); return this.Source.DefinitionsDataReader.ReadInt32(); }
set { this.SetStreamPos(); this.Source.DefinitionsDataWriter.Write(value); }
}

/// Gets or sets a vector value.
public Vector3 Vector {
get { this.SetStreamPos(); return new Vector3(this.Source.DefinitionsDataReader.BaseStream); }
set {
this.SetStreamPos();
this.Source.DefinitionsDataWriter.Write(value.X);
this.Source.DefinitionsDataWriter.Write(value.Y);
this.Source.DefinitionsDataWriter.Write(value.Z);
}
}

#endregion

#region Extended Properties

public bool Boolean {
get { return this.Float != 0f; }
set { this.Float = value ? 1f : 0f; }
}

#endregion

#region Read-Only Properties

/// Gets a string value.
public string String {
get { return this.Source.GetString((uint)this.Integer); }
}

public Function Function {
get { return this.Source.Functions[this.Integer]; }
}

#endregion
}

Not too elegant, but it works!

If the offset for a statement is negative in a function, that means that the function being called is an internally-implemented one. The source code for the test application in the screenshot at the top of this entry is as follows:
float testVal;

void() test = {
dprint("This is a QuakeC VM test...\n");

testVal = 100;
dprint(ftos(testVal * 10));
dprint("\n");

while (testVal > 0) {
dprint(ftos(testVal));
testVal = testVal - 1;
dprint("...\n");
}
dprint("Lift off!");

};

Both dprint and ftos are internal functions; I use a simple array of delegates to reference them.

There's a huge amount of work to be done here, especially when it comes to entities (not something I've looked at at all). All I can say is that I'm very thankful that the .qc source code is available and the DOS compiler runs happily under Windows - they're going to be handy for testing.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now