Skip to content

SB2 File Format

ラ⭐ edited this page Jan 2, 2023 · 20 revisions

Layout

header
external_function_table_entry[header.external_function_count]
function_data_entry
  inst
  inst
  inst
  ...
function_local_data
function_data_entry
  inst
  inst
  inst
  ...
function_local_data

Parts

struct header
{
  char magic[4];                // "SB2\0"
  u32 unk_function_data_offset; // some function data entry of unknown importance (usually stubbed to return 0)
  u32 code_start_byte_offset;   // first function data entry
  u32 header_byte_count;        // end of the header (location of the function table)
  u32 external_function_count;  // number of functions exported in function table
  u32 unk0;                     // usually 0
  u32 global_variable_count;    //

  // these are all usually 0
  u32 unk2;
  u32 unk3;
  u32 unk4;
  u32 unk5;
  u32 unk6;
  u32 unk7;
  u32 unk8;
  u32 unk9;
  u32 unk10;
};

struct external_function_table_entry
{
  u32 id;                        // usually aligned to 50
  u32 function_data_byte_offset; // location of the func_data_entry
};

struct function_data_entry
{
  u32 code_start_byte_offset; // start of code
  u32 name_byte_offset;       // location of the name (usually stripped)
  u32 stack_size;             // 
  u32 argument_count;         // number of arguments for this function

  // these are usually 0
  u32 unk0;
  u32 unk1;
  u32 unk2;
  u32 unk3;
  u32 unk4;
  u32 unk5;
  u32 unk6;
  u32 unk7;
  u32 unk8;
  u32 unk9; 
};

Types

enum opcode : u32
{
  _end        = 0,
  _push_stack = 1,
  _push_ptr   = 2,
  _push       = 3,
  _pop        = 4,
  _deref      = 5,
  _add        = 6,
  _sub        = 7,
  _mul        = 8,
  _div        = 9,
  _mod        = 10,
  _neg        = 11,
  _itof       = 12,
  _ftoi       = 13,
  _cmp        = 14,
  _ret        = 15,
  _jmp        = 16,
  _bf         = 17,
  _bt         = 18,
  _call       = 19,
  _print      = 20,
  _ext        = 21,
  _nop        = 22,
  _yld        = 23,
  _and        = 24,
  _or         = 25,
  _not        = 26,
  _exit       = 27,
  _unk1       = 28,
  _sin        = 29,
  _cos        = 30,
  count
};

enum data_type : u32
{
  invalid,
  _int = 1,
  _flt = 2,
  _str = 3,
  _ptr = 4,
  count
};

// note: sub 40 from the arg for some reason
enum comparision_function : u32
{
  _eq = 0,
  _ne = 1,
  _lt = 2,
  _le = 3,
  _gt = 4,
  _ge = 5,
  count
};

Instruction Encodings

Empty
11               7                3                0
┌────────────────┬────────────────┬────────────────┐
│     unused     │     unused     │     opcode     │
└────────────────┴────────────────┴────────────────┘

Push Relative
11               7                3                0
┌────────────────┬────────────────┬────────────────┐
│      mode      │    address     │     opcode     │
└────────────────┴────────────────┴────────────────┘

Push Immediate
11               7                3                0
┌────────────────┬────────────────┬────────────────┐
│      value     │      type      │     opcode     │
└────────────────┴────────────────┴────────────────┘

Comparison
11               7                3                0
┌────────────────┬────────────────┬────────────────┐
│     unused     │    function    │     opcode     │
└────────────────┴────────────────┴────────────────┘

Branch
11               7                3                0
┌────────────────┬────────────────┬────────────────┐
│     restore    │     address    │     opcode     │
└────────────────┴────────────────┴────────────────┘

Jump
11               7                3                0
┌────────────────┬────────────────┬────────────────┐
│     unused     │     address    │     opcode     │
└────────────────┴────────────────┴────────────────┘

Integer
11               7                3                0
┌────────────────┬────────────────┬────────────────┐
│     unused     │      value     │     opcode     │
└────────────────┴────────────────┴────────────────┘
Opcode Name Encoding Type
0x00 end Empty
0x01 psh Push Relative
0x02 psh Push Relative
0x03 psh Push Immediate
0x04 pop Empty
0x05 drf Empty
0x06 add Empty
0x07 sub Empty
0x08 mul Empty
0x09 div Empty
0x0A mod Empty
0x0B neg Empty
0x0C itf Empty
Clone this wiki locally