nanoFORTH  v2.2
N4Asm Namespace Reference

Functions

U8 _find (U8 *tkn, IU *adr)
 search the keyword through colon word linked-list More...
 
void _add_word ()
 
void _add_branch (U8 op)
 
void _add_str ()
 
void _list_voc (U16 n)
 
void save (U8 autorun=0)
 persist user dictionary to EEPROM More...
 
U16 load (U8 autorun=0)
 restore user dictionary from EEPROM More...
 
IU reset ()
 reset internal pointers (for BYE) More...
 
IU query ()
 dictionary, string list scanners More...
 
N4OP parse (U8 *tkn, IU *rst, U8 run)
 Instruction decoder. More...
 
void compile (IU *rp0)
 Forth compiler. More...
 
void create ()
 meta compiler More...
 
void comma (DU v)
 compile a 16-bit value onto dictionary More...
 
void ccomma (DU v)
 

compile a 16-bit value onto dictionary

More...
 
void does (IU xt)
 

compile a 16-bit value onto dictionary

More...
 
void dot_str ()
 
void variable ()
 create a variable on dictionary More...
 
void constant (DU v)
 
void words ()
 display words in dictionary More...
 
void forget ()
 forgets word in the dictionary More...
 
void see ()
 decode colon word More...
 
IU trace (IU adr, U8 ir, char delim=0)
 print execution tracing info More...
 

Variables

U8last { NULL }
 pointer to last word, for debugging More...
 
U8here { NULL }
 top of dictionary (exposed to _vm for HRE, ALO opcodes) More...
 
U8 tab = 0
 

Detailed Description

LFA NFA XT=PFA +----—+----—+-----------------—+ | lnk | name | parameters...I_RET | +----—+----—+-----------------—+ \ \ \ 16-bits 3-bytes variable length parameters

Assembler class

Function Documentation

◆ _find()

U8 N4Asm::_find ( U8 tkn,
IU adr 
)

search the keyword through colon word linked-list

find colon word address of next input token

Returns
1 - token found
0 - token not found
  • return offset of dic

◆ _add_word()

void N4Asm::_add_word ( )

create name field with link back to previous word

fetch one token from console

create 3-byte name field

◆ _add_branch()

void N4Asm::_add_branch ( U8  op)

create branching for instructions

f IF...THN, f IF...ELS...THN BGN...f UTL, BGN...f WHL...RPT, BGN...f WHL...f UTL n1 n0 FOR...NXT

◆ _add_str()

void N4Asm::_add_str ( )

display the opcode name

◆ _list_voc()

void N4Asm::_list_voc ( U16  n)

list words in built-in vocabularies

◆ save()

void N4Asm::save ( U8  autorun)

persist user dictionary to EEPROM

persist dictionary from RAM into EEPROM

verify EEPROM capacity to hold user dictionary

create EEPROM dictionary header

copy user dictionary into EEPROM byte-by-byte

◆ load()

IU N4Asm::load ( U8  autorun)

restore user dictionary from EEPROM

restore dictionary from EEPROM into RAM

Returns
lnk: autorun address (of last word from EEPROM) LFA_END: no autorun or EEPROM not been setup yet

validate EEPROM contains user dictionary (from previous run)

retrieve metadata (sizes) of user dictionary

retrieve user dictionary byte-by-byte into memory

adjust user dictionary pointers

◆ reset()

IU N4Asm::reset ( )

reset internal pointers (for BYE)

reset internal pointers (called by VM::reset)

Returns
1: autorun last word from EEPROM 0: clean start

◆ query()

IU N4Asm::query ( )

dictionary, string list scanners

get address of next input token

get xt of next input token, 0 if not found

< lfa of word

check if token is in dictionary

  • not found, bail
  • xt = adr + lnk[2] + name[3]

◆ parse()

N4OP N4Asm::parse ( U8 tkn,
IU rst,
U8  run 
)

Instruction decoder.

parse given token into actionable item

  • WRD - is a colon word? [lnk(2),name(3)]
  • IMM - is a immediate word?
  • PRM - is a primitives?
  • NUM - is a number literal?
  • ERR - unknown token
Parameters
tkntoken to be parsed
rstparsed result
runrun mode flag (1: run mode, 0: compile mode)

◆ compile()

void N4Asm::compile ( IU rp0)

Forth compiler.

Forth assembler (creates word onto dictionary)

fetch token, create name field linked to previous word

loop til exhaust all tokens (tkn==NULL)

trace assembler progress if enabled

determine type of operation, and keep opcode in tmp

an immediate command?

  • add branching opcode
  • semi-colon i.e. end of word
  • clear token to exit compile mode

debug memory dump, if enabled

a colon word? [addr + lnk(2) + name(3)]

  • call subroutine

a built-in primitives?

  • add found primitive opcode
  • do extra, if it's a ." (dot_string) or S" (do_string) command

a literal (number)?

  • 1-byte literal, or
  • 3-byte literal

extended words, not implemented yet

then, token type not found

  • restore last, here pointers
  • reset tib and token parser
  • bail, terminate loop!
Parameters
rp0memory address to be used as assembler return stack

◆ create()

void N4Asm::create ( )

meta compiler

meta compiler

create a word name field

create a word header (link + name field)

fetch token, create name field linked to previous word

< address to variable storage

1-byte literal

or, 3-byte literal

◆ comma()

void N4Asm::comma ( DU  v)

compile a 16-bit value onto dictionary

◆ ccomma()

void N4Asm::ccomma ( DU  v)

compile a 16-bit value onto dictionary

compile a 8-it value onto dictionary

◆ does()

void N4Asm::does ( IU  xt)

compile a 16-bit value onto dictionary

metaprogrammer (jump to definding word DO> section)

metaprogrammer (jump to definding word DO> section)

start walking back

shift down parameters by 2 bytes

adjust the PFA

replace NOP with a JMP,

and a NOP, (not necessary but nice to SEE)

extra 2 bytes due to shift

◆ dot_str()

void N4Asm::dot_str ( )

< keep current HERE

  • fill string on PAD
  • print the string
  • restore HERE

◆ variable()

void N4Asm::variable ( )

create a variable on dictionary

create a variable on dictionary

  • note: 8 or 10-byte per variable

add actual literal storage area

◆ constant()

void N4Asm::constant ( DU  v)

create a constant on dictionary

  • note: 8 or 10-byte per variable

create a constant on dictionary

fetch token, create name field linked to previous word

handle 1-byte constant

or, constant stored as 3-byte literal

◆ words()

void N4Asm::words ( )

display words in dictionary

display words in dictionary

wraping width

from last, loop through dictionary

optionally show address

show word name

◆ forget()

void N4Asm::forget ( )

forgets word in the dictionary

drop words from the dictionary

< cfa of word

  • bail if word not found

word found, rollback here

< pointer to word's link

  • reset last word address
  • reset current pointer

◆ see()

void N4Asm::see ( )

decode colon word

decode colon word

< cfa of word

  • not found in colon words

word found, walk parameter field

< pointer to word's name

◆ trace()

U16 N4Asm::trace ( IU  a,
U8  ir,
char  delim 
)

print execution tracing info

execution tracer (debugger, can be modified into single-stepper)

is a jump instruction?

is a primitive?

and a number (i.e. 1-byte literal)

Parameters
adraddress to word to be executed
irinstruction register value
delimtoken delimiter

Variable Documentation

◆ last

U8 * N4Asm::last { NULL }

pointer to last word, for debugging

◆ here

U8 * N4Asm::here { NULL }

top of dictionary (exposed to _vm for HRE, ALO opcodes)

◆ tab

U8 N4Asm::tab = 0

tracing indentation counter