libc8
CHIP-8 utility library
symbol.h File Reference
#include <stdint.h>
#include <stdlib.h>

Go to the source code of this file.

Data Structures

struct  instruction_format_t
 Represents a valid instruction format. More...
 
struct  instruction_t
 Represents an instruction. More...
 
struct  label_t
 Represents a label. More...
 
struct  label_list_t
 Represents a list of labels. More...
 
struct  symbol_t
 Represents a symbol with a type, value, and line number. More...
 
struct  symbol_list_t
 Represents a symbol with a type, value, and line number. More...
 

Macros

#define INSTRUCTION_COUNT   64
 
#define LABEL_CEILING   64
 
#define LABEL_IDENTIFIER_SIZE   20
 
#define SYMBOL_CEILING   64
 
#define S_CLS   "CLS"
 
#define S_RET   "RET"
 
#define S_JP   "JP"
 
#define S_CALL   "CALL"
 
#define S_SE   "SE"
 
#define S_SNE   "SNE"
 
#define S_LD   "LD"
 
#define S_ADD   "ADD"
 
#define S_OR   "OR"
 
#define S_AND   "AND"
 
#define S_SUB   "SUB"
 
#define S_SHR   "SHR"
 
#define S_SUBN   "SUBN"
 
#define S_SHL   "SHL"
 
#define S_RND   "RND"
 
#define S_DRW   "DRW"
 
#define S_SKP   "SKP"
 
#define S_SKNP   "SKNP"
 
#define S_XOR   "XOR"
 
#define S_SCD   "SCD"
 
#define S_SCR   "SCR"
 
#define S_SCL   "SCL"
 
#define S_EXIT   "EXIT"
 
#define S_LOW   "LOW"
 
#define S_HIGH   "HIGH"
 
#define S_K   "K"
 
#define S_F   "F"
 
#define S_B   "B"
 
#define S_DT   "DT"
 
#define S_ST   "ST"
 
#define S_I   "I"
 
#define S_IP   "[I]"
 
#define S_DB   ".DB"
 
#define S_DW   ".DW"
 
#define S_DS   ".DS"
 
#define S_HF   "HF"
 
#define S_R   "R"
 

Enumerations

enum  Instruction {
  I_NULL = -1 , I_CLS , I_RET , I_JP ,
  I_CALL , I_SE , I_SNE , I_LD ,
  I_ADD , I_OR , I_AND , I_SUB ,
  I_SHR , I_SUBN , I_SHL , I_RND ,
  I_DRW , I_SKP , I_SKNP , I_XOR ,
  I_SCD , I_SCR , I_SCL , I_EXIT ,
  I_LOW , I_HIGH
}
 Represents instruction types. More...
 
enum  Symbol {
  SYM_NULL , SYM_DT , SYM_ST , SYM_I ,
  SYM_IP , SYM_K , SYM_F , SYM_B ,
  SYM_DB , SYM_DW , SYM_DS , SYM_HF ,
  SYM_R , SYM_LABEL , SYM_INT , SYM_INT4 ,
  SYM_INT8 , SYM_INT12 , SYM_STRING , SYM_V ,
  SYM_INSTRUCTION , SYM_LABEL_DEFINITION
}
 Represents symbol types. More...
 

Functions

int build_instruction (instruction_t *, symbol_list_t *, int)
 Build an instruction from symbols beginning at idx. More...
 
int is_comment (const char *)
 Check if the given string is a comment. More...
 
int is_db (const char *)
 Check if given string is a DB identifier. More...
 
int is_dw (const char *)
 Check if given string is a DW identifier. More...
 
int is_instruction (const char *)
 Check if the given string is an instruction. More...
 
int is_label_definition (const char *)
 Check if the given string is a label definition. More...
 
int is_label (const char *, label_list_t *)
 Check if given string is a label reference. More...
 
int is_register (const char *)
 Check if the given string represents a V register. More...
 
int is_reserved_identifier (const char *)
 Check if given string is a reserved identifier. More...
 
symbol_tnext_symbol (symbol_list_t *)
 Get the next symbol. More...
 
int populate_labels (char **, int, label_list_t *)
 Populate label list from lines. More...
 
int resolve_labels (symbol_list_t *, label_list_t *)
 Get byte indexes of label definitions from completed symbol table. More...
 
int substitute_labels (symbol_list_t *, label_list_t *)
 Substitute label symbols with their corresponding int value. More...
 

Variables

const char * instructionStrings []
 
const char * identifierStrings []
 
instruction_format_t formats []
 

Detailed Description

Note
NOT EXPORTED

Stuff for parsing symbols and instructions for encoding CHIP-8 "assembly".

Macro Definition Documentation

◆ INSTRUCTION_COUNT

#define INSTRUCTION_COUNT   64

◆ LABEL_CEILING

#define LABEL_CEILING   64

◆ LABEL_IDENTIFIER_SIZE

#define LABEL_IDENTIFIER_SIZE   20

◆ S_ADD

#define S_ADD   "ADD"

◆ S_AND

#define S_AND   "AND"

◆ S_B

#define S_B   "B"

◆ S_CALL

#define S_CALL   "CALL"

◆ S_CLS

#define S_CLS   "CLS"

◆ S_DB

#define S_DB   ".DB"

◆ S_DRW

#define S_DRW   "DRW"

◆ S_DS

#define S_DS   ".DS"

◆ S_DT

#define S_DT   "DT"

◆ S_DW

#define S_DW   ".DW"

◆ S_EXIT

#define S_EXIT   "EXIT"

◆ S_F

#define S_F   "F"

◆ S_HF

#define S_HF   "HF"

◆ S_HIGH

#define S_HIGH   "HIGH"

◆ S_I

#define S_I   "I"

◆ S_IP

#define S_IP   "[I]"

◆ S_JP

#define S_JP   "JP"

◆ S_K

#define S_K   "K"

◆ S_LD

#define S_LD   "LD"

◆ S_LOW

#define S_LOW   "LOW"

◆ S_OR

#define S_OR   "OR"

◆ S_R

#define S_R   "R"

◆ S_RET

#define S_RET   "RET"

◆ S_RND

#define S_RND   "RND"

◆ S_SCD

#define S_SCD   "SCD"

◆ S_SCL

#define S_SCL   "SCL"

◆ S_SCR

#define S_SCR   "SCR"

◆ S_SE

#define S_SE   "SE"

◆ S_SHL

#define S_SHL   "SHL"

◆ S_SHR

#define S_SHR   "SHR"

◆ S_SKNP

#define S_SKNP   "SKNP"

◆ S_SKP

#define S_SKP   "SKP"

◆ S_SNE

#define S_SNE   "SNE"

◆ S_ST

#define S_ST   "ST"

◆ S_SUB

#define S_SUB   "SUB"

◆ S_SUBN

#define S_SUBN   "SUBN"

◆ S_XOR

#define S_XOR   "XOR"

◆ SYMBOL_CEILING

#define SYMBOL_CEILING   64

Enumeration Type Documentation

◆ Instruction

Represents instruction types.

This enumeration defines all possible CHIP-8 instructions.

NOTE: values to be kept in same order as instructionStrings

Enumerator
I_NULL 
I_CLS 
I_RET 
I_JP 
I_CALL 
I_SE 
I_SNE 
I_LD 
I_ADD 
I_OR 
I_AND 
I_SUB 
I_SHR 
I_SUBN 
I_SHL 
I_RND 
I_DRW 
I_SKP 
I_SKNP 
I_XOR 
I_SCD 
I_SCR 
I_SCL 
I_EXIT 
I_LOW 
I_HIGH 

◆ Symbol

enum Symbol

Represents symbol types.

This enumeration defines all symbol types found during the first assembler pass.

NOTE: values before label need to be kept in same order as identifierStrings

Enumerator
SYM_NULL 
SYM_DT 
SYM_ST 
SYM_I 
SYM_IP 
SYM_K 
SYM_F 
SYM_B 
SYM_DB 
SYM_DW 
SYM_DS 
SYM_HF 
SYM_R 
SYM_LABEL 
SYM_INT 
SYM_INT4 
SYM_INT8 
SYM_INT12 
SYM_STRING 
SYM_V 
SYM_INSTRUCTION 
SYM_LABEL_DEFINITION 

Function Documentation

◆ build_instruction()

int build_instruction ( instruction_t ins,
symbol_list_t symbols,
int  idx 
)

Build an instruction from symbols beginning at idx.

This function builds an instruction from a completely parsed set of symbols (with labels expanded).

Parameters
insinstruction_t to store instruction contents
symbolssymbol list
idxsymbols index of start of instruction
Returns
instruction bytecode

◆ is_comment()

int is_comment ( const char *  s)

Check if the given string is a comment.

Parameters
sthe string to check
Returns
1 if true, 0 if false

◆ is_db()

int is_db ( const char *  s)

Check if given string is a DB identifier.

Returns
1 if true, 0 if false

◆ is_dw()

int is_dw ( const char *  s)

Check if given string is a DW identifier.

Returns
1 if true, 0 if false

◆ is_instruction()

int is_instruction ( const char *  s)

Check if the given string is an instruction.

Parameters
sthe string to check
Returns
instruction enumerator if true, -1 if false

◆ is_label()

int is_label ( const char *  s,
label_list_t labels 
)

Check if given string is a label reference.

Parameters
sstring to check
labelslabel list to check from
Returns
label index if true, -1 otherwise

◆ is_label_definition()

int is_label_definition ( const char *  s)

Check if the given string is a label definition.

Parameters
sthe string to check
Returns
1 if true, 0 if false

◆ is_register()

int is_register ( const char *  s)

Check if the given string represents a V register.

Parameters
sstring to check
Returns
V register number if true, -1 otherwise

◆ is_reserved_identifier()

int is_reserved_identifier ( const char *  s)

Check if given string is a reserved identifier.

Parameters
sstring to check
Returns
type of identifier if true, -1 otherwise

◆ next_symbol()

symbol_t * next_symbol ( symbol_list_t symbols)

Get the next symbol.

Returns
first empty symbol in symbol table

◆ populate_labels()

int populate_labels ( char **  lines,
int  lineCount,
label_list_t labels 
)

Populate label list from lines.

Parameters
lineslines to search
lineCountnumber of lines to search
labelslabel list to populate
Returns
1 if success, 0 if failure

◆ resolve_labels()

int resolve_labels ( symbol_list_t symbols,
label_list_t labels 
)

Get byte indexes of label definitions from completed symbol table.

Parameters
symbolslist of symbols
labelslist of labels
Returns
1 if success, 0 if failure

◆ substitute_labels()

int substitute_labels ( symbol_list_t symbols,
label_list_t labels 
)

Substitute label symbols with their corresponding int value.

Parameters
symbolssymbols to search
labelslabels to search

Variable Documentation

◆ formats

instruction_format_t formats[]
extern

All valid instruction formats

◆ identifierStrings

const char* identifierStrings[]
extern

◆ instructionStrings

const char* instructionStrings[]
extern