libc8
CHIP-8 utility library
Loading...
Searching...
No Matches
symbol.h File Reference
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  C8_InstructionFormat
 Represents a valid instruction format. More...
 
struct  C8_Instruction
 Represents an instruction. More...
 
struct  C8_Label
 Represents a label. More...
 
struct  C8_LabelList
 Represents a list of labels. More...
 
struct  C8_Symbol
 Represents a symbol with a type, value, and line number. More...
 
struct  C8_SymbolList
 Represents a symbol with a type, value, and line number. More...
 

Macros

#define C8_INSTRUCTION_COUNT   64
 Initial instruction list ceiling.
 
#define C8_LABEL_CEILING   64
 Initial label list ceiling.
 
#define C8_LABEL_IDENTIFIER_SIZE   20
 Maximum size of label identifier.
 
#define C8_SYMBOL_CEILING   64
 Initial symbol list ceiling.
 
#define C8_S_CLS   "CLS"
 
#define C8_S_RET   "RET"
 
#define C8_S_JP   "JP"
 
#define C8_S_CALL   "CALL"
 
#define C8_S_SE   "SE"
 
#define C8_S_SNE   "SNE"
 
#define C8_S_LD   "LD"
 
#define C8_S_ADD   "ADD"
 
#define C8_S_OR   "OR"
 
#define C8_S_AND   "AND"
 
#define C8_S_SUB   "SUB"
 
#define C8_S_SHR   "SHR"
 
#define C8_S_SUBN   "SUBN"
 
#define C8_S_SHL   "SHL"
 
#define C8_S_RND   "RND"
 
#define C8_S_DRW   "DRW"
 
#define C8_S_SKP   "SKP"
 
#define C8_S_SKNP   "SKNP"
 
#define C8_S_XOR   "XOR"
 
#define C8_S_SCD   "SCD"
 
#define C8_S_SCR   "SCR"
 
#define C8_S_SCL   "SCL"
 
#define C8_S_EXIT   "EXIT"
 
#define C8_S_LOW   "LOW"
 
#define C8_S_HIGH   "HIGH"
 
#define C8_S_JP_V0   "JP V0,"
 
#define C8_S_K   "K"
 
#define C8_S_F   "F"
 
#define C8_S_B   "B"
 
#define C8_S_DT   "DT"
 
#define C8_S_ST   "ST"
 
#define C8_S_I   "I"
 
#define C8_S_IP   "[I]"
 
#define C8_S_DB   ".DB"
 
#define C8_S_DW   ".DW"
 
#define C8_S_DS   ".DS"
 
#define C8_S_HF   "HF"
 
#define C8_S_R   "R"
 

Enumerations

enum  C8_InstructionIdentifier {
  C8_I_NULL = -1 , C8_I_CLS , C8_I_RET , C8_I_JP ,
  C8_I_CALL , C8_I_SE , C8_I_SNE , C8_I_LD ,
  C8_I_ADD , C8_I_OR , C8_I_AND , C8_I_SUB ,
  C8_I_SHR , C8_I_SUBN , C8_I_SHL , C8_I_RND ,
  C8_I_DRW , C8_I_SKP , C8_I_SKNP , C8_I_XOR ,
  C8_I_SCD , C8_I_SCR , C8_I_SCL , C8_I_EXIT ,
  C8_I_LOW , C8_I_HIGH , C8_I_JP_V0
}
 Represents instruction types. More...
 
enum  C8_SymbolIdentifier {
  C8_SYM_NULL , C8_SYM_DT , C8_SYM_ST , C8_SYM_I ,
  C8_SYM_IP , C8_SYM_K , C8_SYM_F , C8_SYM_B ,
  C8_SYM_DB , C8_SYM_DW , C8_SYM_DS , C8_SYM_HF ,
  C8_SYM_R , C8_SYM_LABEL , C8_SYM_INT , C8_SYM_INT4 ,
  C8_SYM_INT8 , C8_SYM_INT12 , C8_SYM_STRING , C8_SYM_V ,
  C8_SYM_INSTRUCTION , C8_SYM_LABEL_DEFINITION
}
 Represents symbol types. More...
 

Functions

int c8_build_instruction (C8_Instruction *, C8_SymbolList *, int)
 Build an instruction from symbols beginning at idx.
 
int c8_is_comment (const char *)
 Check if the given string is a comment.
 
int c8_is_db (const char *)
 Check if given string is a DB identifier.
 
int c8_is_ds (const char *)
 Check if given string is a DS identifier.
 
int c8_is_dw (const char *)
 Check if given string is a DW identifier.
 
int c8_is_instruction (const char *)
 Check if the given string is an instruction.
 
int c8_is_label_definition (const char *)
 Check if the given string is a label definition.
 
int c8_is_label (const char *, const C8_LabelList *)
 Check if given string is a label reference.
 
int c8_is_register (const char *)
 Check if the given string represents a V register.
 
int c8_is_reserved_identifier (const char *)
 Check if given string is a reserved identifier.
 
C8_Symbolc8_next_symbol (C8_SymbolList *)
 Get the next symbol.
 
int c8_populate_labels (C8_LabelList *)
 Populate label list from lines.
 
int c8_resolve_labels (C8_SymbolList *, C8_LabelList *)
 Get byte indexes of label definitions from completed symbol table.
 
int c8_shift (uint16_t)
 Find the bits needed to shift to OR a parameter into an instruction.
 
int c8_substitute_labels (C8_SymbolList *, C8_LabelList *)
 Substitute label symbols with their corresponding int value.
 

Variables

const char * c8_instructionStrings []
 
const char * c8_identifierStrings []
 
C8_InstructionFormat c8_formats []
 

Detailed Description

Note
NOT EXPORTED

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

Macro Definition Documentation

◆ C8_INSTRUCTION_COUNT

#define C8_INSTRUCTION_COUNT   64

Initial instruction list ceiling.

◆ C8_LABEL_CEILING

#define C8_LABEL_CEILING   64

Initial label list ceiling.

◆ C8_LABEL_IDENTIFIER_SIZE

#define C8_LABEL_IDENTIFIER_SIZE   20

Maximum size of label identifier.

◆ C8_S_ADD

#define C8_S_ADD   "ADD"

◆ C8_S_AND

#define C8_S_AND   "AND"

◆ C8_S_B

#define C8_S_B   "B"

◆ C8_S_CALL

#define C8_S_CALL   "CALL"

◆ C8_S_CLS

#define C8_S_CLS   "CLS"

◆ C8_S_DB

#define C8_S_DB   ".DB"

◆ C8_S_DRW

#define C8_S_DRW   "DRW"

◆ C8_S_DS

#define C8_S_DS   ".DS"

◆ C8_S_DT

#define C8_S_DT   "DT"

◆ C8_S_DW

#define C8_S_DW   ".DW"

◆ C8_S_EXIT

#define C8_S_EXIT   "EXIT"

◆ C8_S_F

#define C8_S_F   "F"

◆ C8_S_HF

#define C8_S_HF   "HF"

◆ C8_S_HIGH

#define C8_S_HIGH   "HIGH"

◆ C8_S_I

#define C8_S_I   "I"

◆ C8_S_IP

#define C8_S_IP   "[I]"

◆ C8_S_JP

#define C8_S_JP   "JP"

◆ C8_S_JP_V0

#define C8_S_JP_V0   "JP V0,"

◆ C8_S_K

#define C8_S_K   "K"

◆ C8_S_LD

#define C8_S_LD   "LD"

◆ C8_S_LOW

#define C8_S_LOW   "LOW"

◆ C8_S_OR

#define C8_S_OR   "OR"

◆ C8_S_R

#define C8_S_R   "R"

◆ C8_S_RET

#define C8_S_RET   "RET"

◆ C8_S_RND

#define C8_S_RND   "RND"

◆ C8_S_SCD

#define C8_S_SCD   "SCD"

◆ C8_S_SCL

#define C8_S_SCL   "SCL"

◆ C8_S_SCR

#define C8_S_SCR   "SCR"

◆ C8_S_SE

#define C8_S_SE   "SE"

◆ C8_S_SHL

#define C8_S_SHL   "SHL"

◆ C8_S_SHR

#define C8_S_SHR   "SHR"

◆ C8_S_SKNP

#define C8_S_SKNP   "SKNP"

◆ C8_S_SKP

#define C8_S_SKP   "SKP"

◆ C8_S_SNE

#define C8_S_SNE   "SNE"

◆ C8_S_ST

#define C8_S_ST   "ST"

◆ C8_S_SUB

#define C8_S_SUB   "SUB"

◆ C8_S_SUBN

#define C8_S_SUBN   "SUBN"

◆ C8_S_XOR

#define C8_S_XOR   "XOR"

◆ C8_SYMBOL_CEILING

#define C8_SYMBOL_CEILING   64

Initial symbol list ceiling.

Enumeration Type Documentation

◆ C8_InstructionIdentifier

Represents instruction types.

This enumeration defines all possible CHIP-8 instructions.

NOTE: values to be kept in same order as instructionStrings

Enumerator
C8_I_NULL 
C8_I_CLS 
C8_I_RET 
C8_I_JP 
C8_I_CALL 
C8_I_SE 
C8_I_SNE 
C8_I_LD 
C8_I_ADD 
C8_I_OR 
C8_I_AND 
C8_I_SUB 
C8_I_SHR 
C8_I_SUBN 
C8_I_SHL 
C8_I_RND 
C8_I_DRW 
C8_I_SKP 
C8_I_SKNP 
C8_I_XOR 
C8_I_SCD 
C8_I_SCR 
C8_I_SCL 
C8_I_EXIT 
C8_I_LOW 
C8_I_HIGH 
C8_I_JP_V0 

◆ C8_SymbolIdentifier

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
C8_SYM_NULL 
C8_SYM_DT 
C8_SYM_ST 
C8_SYM_I 
C8_SYM_IP 
C8_SYM_K 
C8_SYM_F 
C8_SYM_B 
C8_SYM_DB 
C8_SYM_DW 
C8_SYM_DS 
C8_SYM_HF 
C8_SYM_R 
C8_SYM_LABEL 
C8_SYM_INT 
C8_SYM_INT4 
C8_SYM_INT8 
C8_SYM_INT12 
C8_SYM_STRING 
C8_SYM_V 
C8_SYM_INSTRUCTION 
C8_SYM_LABEL_DEFINITION 

Function Documentation

◆ c8_build_instruction()

int c8_build_instruction ( C8_Instruction ins,
C8_SymbolList 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

◆ c8_is_comment()

int c8_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

◆ c8_is_db()

int c8_is_db ( const char *  s)

Check if given string is a DB identifier.

Returns
1 if true, 0 if false

◆ c8_is_ds()

int c8_is_ds ( const char *  s)

Check if given string is a DS identifier.

Returns
1 if true, 0 if false

◆ c8_is_dw()

int c8_is_dw ( const char *  s)

Check if given string is a DW identifier.

Returns
1 if true, 0 if false

◆ c8_is_instruction()

int c8_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

◆ c8_is_label()

int c8_is_label ( const char *  s,
const C8_LabelList labels 
)

Check if given string is a label reference.

This function checks if the given string is a label reference by comparing it against the label list. It returns the index of the label in the label list if it is found, or -1 if it is not.

This function assumes that the label list has been populated (e.g. populate_labels() has been called).

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

◆ c8_is_label_definition()

int c8_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

◆ c8_is_register()

int c8_is_register ( const char *  s)

Check if the given string represents a V register.

This function checks if the given string starts with 'V' or 'v' and is followed by a valid hexadecimal digit (0-9, A-F, a-f). It returns the register number if it is a valid V register, or -1 if it is not.

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

◆ c8_is_reserved_identifier()

int c8_is_reserved_identifier ( const char *  s)

Check if given string is a reserved identifier.

This function checks if the given string is one of the reserved identifiers defined in c8_identifierStrings. It returns the index of the identifier if it is found, or -1 if it is not.

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

◆ c8_next_symbol()

C8_Symbol * c8_next_symbol ( C8_SymbolList symbols)

Get the next symbol.

This function retrieves the next available symbol in the symbol list. If the symbol list is empty, it initializes the first symbol. If the symbol list is full, it reallocates the symbol list to accommodate more symbols.

If symbols is NULL or the symbol list is NULL, it returns NULL.

Parameters
symbolssymbol list to get next symbol from
Returns
first empty symbol in symbol table

◆ c8_populate_labels()

int c8_populate_labels ( C8_LabelList labels)

Populate label list from lines.

This function scans through the lines of code and identifies label definitions. It checks each line for a label definition (ending with a colon) and adds it to the label list.

If a duplicate label definition is found, it throws a C8_DUPLICATE_LABEL_EXCEPTION.

If too many labels are defined, it throws a C8_TOO_MANY_LABELS_EXCEPTION.

Parameters
labelslabel list to populate
Returns
1 if success, 0 or error code otherwise

◆ c8_resolve_labels()

int c8_resolve_labels ( C8_SymbolList symbols,
C8_LabelList labels 
)

Get byte indexes of label definitions from completed symbol table.

This function resolves label definitions in the symbol list and populates the label list with the byte index of each label definition. It iterates through the symbols, keeping track of the current byte position in the program. When it encounters a label definition, it records the current byte position in the label list. It also increments the byte position based on the type of symbol encountered (e.g., SYM_DB increments by 1, SYM_INSTRUCTION and SYM_DW increment by 2).

This function assumes that the symbol list has been populated and that the symbols have been parsed correctly. It also assumes that the label list has been initialized.

This function will return 1 if all labels were resolved successfully, or 0 if there was a failure (e.g., if the label list is not fully populated).

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

◆ c8_shift()

int c8_shift ( uint16_t  fmt)

Find the bits needed to shift to OR a parameter into an instruction.

This function finds the bits needed to shift to OR a parameter into an instruction. It is used to determine the position of parameters in the instruction bytecode.

This is a workaround for the fact that the instruction formats are not defined in a way that allows for easy bit manipulation.

Parameters
fmtC8_InstructionFormat pformat to check
Returns
number of bits to shift

◆ c8_substitute_labels()

int c8_substitute_labels ( C8_SymbolList symbols,
C8_LabelList labels 
)

Substitute label symbols with their corresponding int value.

This function replaces all symbols of type C8_SYM_LABEL in the symbol list with their corresponding byte value from the label list. It checks if the label exists and throws an C8_INVALID_SYMBOL_EXCEPTION if it does not.

This function assumes that the label list has been populated and that their byte values have been set correctly.

Parameters
symbolssymbols to search
labelslabels to search

Variable Documentation

◆ c8_formats

C8_InstructionFormat c8_formats[]
extern

All valid instruction formats

◆ c8_identifierStrings

const char* c8_identifierStrings[]
extern

Reserved identifier strings. Has to match Symbol.

◆ c8_instructionStrings

const char* c8_instructionStrings[]
extern

Instruction strings. Has to match Instruction.