libc8
CHIP-8 utility library
symbol.c File Reference
#include "symbol.h"
#include "../encode.h"
#include "../defs.h"
#include "exception.h"
#include "util.h"
#include <ctype.h>
#include <string.h>

Functions

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

Variables

const char * c8_identifierStrings []
 
const char * c8_instructionStrings []
 
instruction_format_t formats []
 

Detailed Description

Note
NOT EXPORTED

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

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

◆ c8_identifierStrings

const char* c8_identifierStrings[]
Initial value:
= {
"",
S_I,
S_K,
S_F,
S_B,
S_R,
NULL,
}
#define S_I
Definition: symbol.h:52
#define S_ST
Definition: symbol.h:51
#define S_DT
Definition: symbol.h:50
#define S_IP
Definition: symbol.h:53
#define S_DS
Definition: symbol.h:56
#define S_K
Definition: symbol.h:47
#define S_DB
Definition: symbol.h:54
#define S_B
Definition: symbol.h:49
#define S_R
Definition: symbol.h:58
#define S_F
Definition: symbol.h:48
#define S_HF
Definition: symbol.h:57
#define S_DW
Definition: symbol.h:55

Reserved identifier strings. Has to match Symbol.

◆ c8_instructionStrings

const char* c8_instructionStrings[]
Initial value:
= {
NULL,
}
#define S_LOW
Definition: symbol.h:43
#define S_SHR
Definition: symbol.h:31
#define S_SCR
Definition: symbol.h:40
#define S_SUB
Definition: symbol.h:30
#define S_JP
Definition: symbol.h:22
#define S_AND
Definition: symbol.h:29
#define S_CALL
Definition: symbol.h:23
#define S_RND
Definition: symbol.h:34
#define S_ADD
Definition: symbol.h:27
#define S_SCL
Definition: symbol.h:41
#define S_SCD
Definition: symbol.h:39
#define S_EXIT
Definition: symbol.h:42
#define S_SNE
Definition: symbol.h:25
#define S_SKNP
Definition: symbol.h:37
#define S_XOR
Definition: symbol.h:38
#define S_SKP
Definition: symbol.h:36
#define S_RET
Definition: symbol.h:21
#define S_OR
Definition: symbol.h:28
#define S_DRW
Definition: symbol.h:35
#define S_LD
Definition: symbol.h:26
#define S_SE
Definition: symbol.h:24
#define S_SHL
Definition: symbol.h:33
#define S_SUBN
Definition: symbol.h:32
#define S_CLS
Definition: symbol.h:20
#define S_HIGH
Definition: symbol.h:44

Instruction strings. Has to match Instruction.

◆ formats

All valid instruction formats