SCM Instruction

From GTAMods Wiki
Revision as of 17:43, 15 March 2021 by Squ1dd13 (talk | contribs) (Converted SA type reading code into more abstract pseudocode; LCS/VCS may need doing too)
Jump to navigation Jump to search
This article deals with the technical information on the SCM instruction format. For a list of opcodes along with descriptions, see list of opcodes.

An SCM instruction is a single operation in an SCM file. They are executed when the script is run, and are used to change what happens ingame.

Instruction format

Each instruction is comprised of an opcode and arguments.

  • The "opcode" (short for "operation code") is a number that tells the game which operation to perform. For example, the opcode 0001 tells the game to wait for a certain amount of time, 0003 shakes the game camera and 0053 creates a player. The opcode is a signed 16-bit integer.
  • "Arguments" are values sent with the instruction to change what it does. For example, you can change the amount of time to wait when using opcode 0001 by sending a different argument.

Before compilation, the instructions are written as text. For example, a wait instruction could look like this:

wait 0

where wait represents the opcode 0001 and the number 0 is an argument passed with the instruction. Different compilers may use different words to represent each opcode. When the code is compiled, the instruction is converted to raw bytes:

01 00 04 00
  • The first two bytes (01 00 hex) are the opcode bytes in little-endian order.
  • The third byte (04 hex) is the code for the data type.[*] 0x4 is the code for a signed byte.
  • The final byte (00 hex) is the argument value. This is often more than one byte: the size is determined by the data type.

While the opcode bytes are always present, there may not always be arguments passed, so there may be no argument bytes.

No GTA game uses all the available opcodes (32,767 or 0x7FFF). There are mods that add more instructions to the game, most notably the CLEO Library.

Opcodes are always positive, but they are sometimes compiled as negative numbers. This happens when the return value of the instruction is inverted (with the not logical operator). In the example a or b or not c, instructions a and b would have positive opcodes, while instruction c would have a negative opcode. The game takes the absolute value of the opcode before finding the associated instruction's implementation.

Arguments

Each instruction takes a certain number of arguments. If a script passes an incorrect number of arguments, the game will crash.

An argument could be one of the following types:

Concrete data types

Each of the types listed above can be represented in a number of ways in compiled code for various reasons:

  • Integer values can be signed or unsigned, and there are also different sizes of integer that the game can use.
  • There are multiple types of string that allow different numbers of characters.
  • Variables use different type codes based on the type of value that they are referencing.

The concrete type of an argument is determined by a single byte before the value bytes[*]. This byte tells the game what value is coming next so that it knows how many bytes to read, and how to treat the value once read.

III/VC/SA

Type code
(hex)
Value length
(bytes)
Support Description 
Typified
00 0 GTA III Vice City San Andreas End of argument list (EOAL, 004F or 0913 and similar)[*]
01 4 GTA III Vice City San Andreas Immediate 32-bit signed int
value := ReadInt32(bytes, programCounter)
programCounter += 4
02 2 GTA III Vice City San Andreas Global integer/floating-point variable
globalVariableOffset := ReadUInt16(bytes, programCounter)
programCounter += 2
03 2 GTA III Vice City San Andreas Local integer/floating-point variable
localVariableOffset := ReadUInt16(bytes, programCounter)
programCounter += 2
04 1 GTA III Vice City San Andreas Immediate 8-bit signed int
value := ReadInt8(bytes, programCounter)
programCounter++
05 2 GTA III Vice City San Andreas Immediate 16-bit signed int
value := ReadInt16(bytes, programCounter)
programCounter += 2
06 2 GTA III Immediate 16-bit fixed-point (see remark)
value := ToFloat32(ReadUInt16(bytes, programCounter)) / 16.0
programCounter += 2
06 4 Vice City San Andreas Immediate 32-bit floating-point
value := ReadFloat32(bytes, programCounter)
programCounter += 4
07 6 San Andreas Global integer/floating-point array
// Offset of the first global variable in the array.
firstGlobalOffset := ReadUInt16(bytes, programCounter)

// Offset of the variable containing the index.
indexVariableOffset := ReadUInt16(bytes, programCounter + 2)

arraySize := ReadUInt8(bytes, programCounter + 4)
elementType := ReadUInt8(bytes, programCounter + 5) & 0x7F

// Whether or not the index is a global variable.
indexIsGlobal := ReadUInt8(bytes, programCounter + 5) >> 7

programCounter += 6
08 6 San Andreas Local integer/floating-point array
// See above for value details.
firstLocalOffset := ReadUInt16(bytes, programCounter)
indexVariableOffset := ReadUInt16(bytes, programCounter + 2)

arraySize := ReadUInt8(bytes, programCounter + 4)
elementType := ReadUInt8(bytes, programCounter + 5) & 0x7F
indexIsGlobal := ReadUInt8(bytes, programCounter + 5) >> 7

programCounter += 6
Tipified (strings)
09 8 San Andreas Immediate 8-byte string
string := ReadString(bytes, programCounter, 8)
programCounter += 8
0A 2 San Andreas Global 8-byte string variable (see 02)
0B 2 San Andreas Local 8-byte string variable (see 03)
0C 6 San Andreas Global 8-byte string array (see 07)
0D 6 San Andreas Local 8-byte string array (see 08)
0E 1+n San Andreas Immediate variable-length string (non null-terminated)
length := ReadInt8(bytes, programCounter)
string := ReadString(bytes, programCounter + 1, length) + StrPad("\0", 40 - length)
programCounter += length + 1
0F 16 San Andreas Immediate 16-byte string
string := ReadString(bytes, programCounter, 16)
programCounter += 16
10 2 San Andreas Global 16-byte string variable (see 02)
11 2 San Andreas Local 16-byte string variable (see 03)
12 6 San Andreas Global 16-byte string array (see 07)
13 6 San Andreas Local 16-byte string array (see 08)
Untypified
N/A 8 GTA III Vice City Immediate 8-byte string[*]
string := ReadString(bytes, programCounter, 8)
programCounter += 8
N/A 128 San Andreas Immediate 128-byte string
string := ReadString(bytes, programCounter, 128)
programCounter += 128

LCS/VCS

Type codes for Liberty City Stories and Vice City Stories are very different from other games:

  • In some instances, the type code itself denotes the argument value. For example:
    • 0x1 represents the integer value 0
    • 0x2 represents 0.0
  • The type code can sometimes denote a variable.
  • Floating point values are packed (8, 16 or 24 bits as opposed to the more common 32 bits).
Type code
(hex)
Value length
(bytes)
Support Description 
Typified
00 0 Liberty City Stories Vice City Stories End of argument list (EOAL)
01 0 Liberty City Stories Vice City Stories Immediate 8-bit signed integer constant 0
value = 0
02 0 Liberty City Stories Vice City Stories Immediate 8-bit floating-point constant 0.0
value = 0.0
03 1 Liberty City Stories Vice City Stories Immediate 8-bit packed floating-point
value = AsFloat32(ReadUInt8(Bytecode, PC) << 24)
PC += 1
04 2 Liberty City Stories Vice City Stories Immediate 16-bit packed floating-point
value = AsFloat32((ReadUInt8(Bytecode, PC) << 16)
                | (ReadUInt8(Bytecode, PC+1) << 24))
PC += 2
05 3 Liberty City Stories Vice City Stories Immediate 24-bit packed floating-point
value = AsFloat32((ReadUInt8(Bytecode, PC) << 8)
                | (ReadUInt8(Bytecode, PC+1) << 16)
                | (ReadUInt8(Bytecode, PC+2) << 24))
PC += 3
06 4 Liberty City Stories Vice City Stories Immediate 32-bit signed integer
value = ReadInt32(Bytecode, PC)
PC += 4
07 1 Liberty City Stories Vice City Stories Immediate 8-bit signed integer
value = SignExtend32(ReadInt8(Bytecode, PC))
PC += 1
08 2 Liberty City Stories Vice City Stories Immediate 16-bit signed integer
value = SignExtend32(ReadInt16(Bytecode, PC))
PC += 2
09 4 Liberty City Stories Vice City Stories Immediate 32-bit floating-point
value = ReadFloat32(Bytecode, PC)
PC += 4
0A n+NUL Vice City Stories Immediate null-terminated string[*]
textLabel = ReadString(Bytecode, PC)
PC += StrLen(textLabel)+1
Typified (script variables)
0A..0B 1 Liberty City Stories Local timers (TIMERA, TIMERB)
localVar = ReadUInt8(Bytecode, PC) + 0x5E
PC += 1
0B..0C 1 Vice City Stories Local timers (TIMERA, TIMERB)
localVar = ReadUInt8(Bytecode, PC) + 0x5D
PC += 1
0C..6B 1 Liberty City Stories Local integer/floating-point variable
localVar = ReadUInt8(Bytecode, PC) - 0x0C
PC += 1
0D..6C 1 Vice City Stories Local integer/floating-point variable
localVar = ReadUInt8(Bytecode, PC) - 0x0D
PC += 1
6C..CB 3 Liberty City Stories Local integer/floating-point array
localVar = ReadUInt8(Bytecode, PC) - 0x6C
arrayIndexVar = ReadUInt8(Bytecode, PC+1)
arraySize = ReadUInt8(Bytecode, PC+2)
PC += 3
6D..CC 3 Vice City Stories Local integer/floating-point array
localVar = ReadUInt8(Bytecode, PC) - 0x6D
arrayIndexVar = ReadUInt8(Bytecode, PC+1)
arraySize = ReadUInt8(Bytecode, PC+2)
PC += 3
CC..E5 2 Liberty City Stories Global integer/floating-point variable[*]
globalVar = ByteSwap16(ReadUInt16(Bytecode, PC) - 0xCC)
PC += 2
CD..E5 2 Vice City Stories Global integer/floating-point variable[*]
globalVar = ByteSwap16(ReadUInt16(Bytecode, PC) - 0xCD)
PC += 2
E6..FF 4 Liberty City Stories Vice City Stories Global integer/floating-point array
globalVar = ByteSwap16(ReadUInt16(Bytecode, PC) - 0xE6)
arrayIndexVar = ReadUInt8(Bytecode, PC+2)
arraySize = ReadUInt8(Bytecode, PC+3)
PC += 4
Untipified
N/A 8 Liberty City Stories Immediate 8-byte string
textLabel = ReadString(Bytecode, PC, 8)
PC += 8

^ This type was introduced in VCS due to the presence of string variables.

^ Given the data type range limit the largest global variable in LCS is 6655, in VCS is 6399.

Integer numbers

An integer is a number without a decimal or fractional component.

Size
(bytes)
Range
Signed Name Unsigned Name
1 -128 to 127 INT8, CHAR 0 to 255 UINT8, BYTE
2 -32,768 to 32,767 INT16, SHORT 0 to 65,535 UINT16, WORD, USHORT
4 -2,147,483,648 to 2,147,483,647 INT32, LONG 0 to 4,294,967,295 UINT32, DWORD, ULONG

Floating-point numbers

A floating point number is a number with a decimal component.

Size
(bytes)
Range Name
4 ±1.1754944×10-38 to ±3.4028234×1038 SINGLE, FLOAT

Strings

A string is a piece of text. Strings can include letters, numbers and symbols.

There are two kinds of string:

  • Fixed-length. This is the most common type of string and has been used since GTA 3. The string length is fixed. When compiled these strings occupy a certain number of bytes (8 or 16) even if the text is actually shorter (any unused bytes are filled with null values).
  • Variable-length (SA only). Variable-length strings are encoded as a single byte specifying the length followed by the string character bytes. These strings are not null-terminated. The maximum length depends on the instruction[*]. The longest in the original game is 40 characters.
String value Compiled bytes
"MAIN" 09   4D 41 49 4E 00 00 00 00
"MODDING" 09   4D 4F 44 44 49 4E 47 00
"SAVE_OUR_SOULS!" 0F   53 41 56 45 5F 4F 55 52 5F 53 4F 55 4C 53 21 00
"Variable length string" 0E   16   56 61 72 69 61 62 6C 65 20 6C 65 6E 67 74 68 20 73 74 72 69 6E 67

Arrays

In GTA SA, an array is a static reference to a group of successive variables. There is no actual representation of an array value as a concrete type, they do have type codes. When an "array" is passed as an argument to an instruction, what is actually passed is a specific variable in that array. For this reason, array elements are passed as variables of the type in the array – that is to say, a value from an array of global integer variables would be passed as a global integer variable. The game simply performs the extra step of retrieving the variable from the array before using it in the instruction.

This section describes the format of array accesses in GTA SA. Vice City also has arrays, but in a different format.

// Example: not real code.
// Applies only to GTA: SA.
struct ArrayAccess {
    enum ElementType : uint8_t {
        Int,
        Float,
        String8,
        String16
    };
    
    // Offset of first variable in the array.
    uint16_t startOffset;
    
    // Index being accessed ("array[index]").
    // This can be a local variable ("array[someLocalVar]") or a global variable ("array[someGlobalVar]").
    uint16_t index;
    
    // Array length.
    int8_t length;
    
    // Array element type. 7 bits.
    ElementType type : 7;
    
    // Determines whether the index is a global variable (true) or a local variable (false).
    // Only 1 bit.
    bool globalIndex : 1;
};

The array offset is a variable which should have a value more than or equal to 0 and smaller than the array size. Global and local variables can be used as index variables – the purpose of the globalIndex field is to tell the game which has been used. The first element in the array is at index 0, and the last is at length - 1.

Notes

^ In GTA 3, Vice City and Liberty City Stories, short strings (8 bytes) have no type code. If the first byte of an argument does not fit data type range (0x0 - 0x6 for GTA 3 and VC), it's recognized as the beginning of a string and the remaining 8 bytes of the string are read.

^ Some instructions have a variable number of parameters. One such instruction is 004F that starts a new script and takes a variable number of arguments to allow extra script setup. The game uses the special data type to end such argument lists. The number of input/output parameters an instruction can collect/store at a time (separately) is 16 for GTA 3 and VC and 32 for SA, LCS and VCS. Variadic instructions allows for passing additional arguments as much as the amount of local variables minus timers which is 16 for GTA 3 and VC, 32 for SA and 96 for LCS and VCS.

^ In San Andreas, 05B6 is a special instruction that defines a table. Immediately after the opcode a 128 byte stream of data follows, without a type code.

^ Post.png GTAForums: Post by Seemann describing limits for the long strings in SA

See also