/* mi(ps) forth, heavily based on hForth!
 *
 * mips4k, so 32b, once this is ok, implement mips16 ?
 */

/* notes on gnu-as, it is not case sensitive, and only does 1 pass for preprocessing !
 * also, some charactere like ! have to be escaped */
/* to compile is:
 * mips-elf-as --mips32 --alternate -almgs=toto.lst source.s
 */ 

/*.list

.global main*/

.data
USER_STATE: .byte 0	/* compil or not */
USER_BASE: .byte 10

.equ CELLsz, 2
.equ SLINK, 0
.equ FLINK, 0
.equ ENVLINK, 0

/******************************************************************************/
.int DictOver
.int DLoopNested
.int twRetStackUnder
.int twRetStackOver
.int twStackUnder
.int twStackOver
.int twAbort
ThrowIndexes:
twAbort:	.byte 5
	.ascii "ABORT"
twStackOver: .byte 10
	.ascii "stack overflow"
twStackUnder: .byte 10
	.ascii "stack underflow"
twRetStackOver: .byte 10
	.ascii "return stack overflow"
twRetStackUnder: .byte 10
	.ascii "return stack underflow"
twDLoopNested: .byte 10
	.ascii "do-loops nested too deeply during execution"
twDictOver: .byte 10
	.ascii "dictionary overflow"
twInvalidMem: .byte 10
	.ascii "invalid memory address"
twDivbyZero: .byte 10
	.ascii "division by zero"
twResOutOfRange: .byte 10
	.ascii "result out of range"
twArgMismatch: .byte 10
	.ascii "argument type mismatch"
twUndefWord: .byte 10
	.ascii "undefined word"
twInterpCompile: .byte 10
	.ascii "interpreting a compile-only word"
twInvalidForget: .byte 10
	.ascii "invalid FORGET"
twZeroLength: .byte 10
	.ascii "attempt to use zero-length string as a name"
twPictOver: .byte 10
	.ascii "pictured numeric output string overflow"
twParseStrOver: .byte 10
	.ascii "parsed string overflow"
twDefName: .byte 10
	.ascii "definition name too long"
twWriteExc: .byte 10
	.ascii "write to a read-only location"
twUnsupported: .byte 10
	.ascii "unsupported operation"
twControlMismatch: .byte 10
	.ascii "control structure mismatch"
twAddrAlign: .byte 10
	.ascii "address alignment exception"
twInvalidNum: .byte 10
	.ascii "invalid numeric argument"
twRetStackImbal: .byte 10
	.ascii "return stack imbalance"
twLoopParams: .byte 10
	.ascii "loop parameters unavailable"
twInvalidRec: .byte 18
	.ascii "invalid recursion"
twUserInt: .byte 15
	.ascii "user interrupt"
twCompilNest: .byte 17
	.ascii "compiler nesting"
twObsolFeat: .byte 20
	.ascii "obsolescent feature"
twBodyDef: .byte
	.ascii ">BODY used on non-CREATEd definition"
twInvalidArg: .byte 21
	.ascii "invalid name argument"
twBlockRead: .byte 20
	.ascii "block read exception"
twBlockWrite: .byte 22
	.ascii "block write exception"
twInvalidBlock: .byte 21
	.ascii "invalid block number"
twInvalidPos: .byte 22
	.ascii "invalid file position"
twFileExcept: .byte 19
	.ascii "file I/O exception"
twNonExist: .byte 18
	.ascii "non-existent file"
twUnEOF: .byte 23
	.ascii "unexpected end of file"
twInvalidBase: .byte 43
	.ascii "invalid BASE for floating point conversion"
twLossPrec: .byte 18
	.ascii "loss of precision"
/******************************************************************************/


.text
.org 0
	b main

	# initialize the memory
	# through a forth code
		# setting the BMx... for that
	
	# initialize the uart
	# and quit ?
	# forth code too

/* kernel ! or somewhat.. */

/* align is not need it think in 32b .. */
.macro Align
	.align CELLsz
.endm

/* args(0-n): label string */
.macro Str label string
LOCAL StrD, StrE
\label:
	.int StrE-StrD
StrD:
	.ascii string
StrE:
	Align
.endm

/* args: string */
.macro throwmsg string
LOCAL TM, TMd, TMe
TM:
	.int TMe-TMd
TMd:
	.ascii string
TMe:
	.set _THROW, _THROW + CELLsz
	.org ThrowIndexesPost + _THROW
	.int TM
.endm
/* this macro is too much for gnu as...redone all by hand.. */

/* args: lex name label link */
.macro Code lex name label link
LOCAL Cthere
	Align
	.int link
Cthere:
	.int lex
	.ascii name
	Align
	.int Cthere
\label:
.endm

/* args: lex name label link */
.macro Colon lex name label link
	Code lex name label link
	nop
	b		doList
.endm

/* args: lex name label value link */
.macro Const lex name label value link
	Code lex name label link
	nop
	b		doConst
	.int value	
.endm

/* args: lex name label value link */
.macro Value lex name label value link
	Code lex name label link
	nop
	b		doValue
	.int value
.endm

/* args: lex name label n_cells link */
.macro Var lex name label n_cells link
	Code lex name label link
	nop
	b		doVar
	.fill n_cells, 1
.endm

/* args: lex name label offset link */
.macro User lex name label offset link
	Code lex name label link
	nop
	b		doUser
	.int offset
.endm

/* args: string */
.macro InStr string
LOCAL ISe, ISd
	.int doLit
	.int ISe-ISd
	.int doSQuote
ISd:
	.ascii string
ISe:
	Align
.endm

/* args: lex name */
.macro Environ lex name
LOCAL Ed
	Align
	.int envlink
Ed:
	.int lex
	.ascii name
	Align
	.int Ed
	nop
	b		doList
.endm

/* args: none ! */
/* because it has to jump to the next code address */
.macro Next
	sw	$a0,9
	b		$a0
	Align
.endm

/******************************************************************************/
/* System dependant ! - but heh, a lot in fact! */

/* is there data on console ? */
/* RX? ( -- flag ) */
Code 3 "RX?" RXQ SLINK
Next

/* get one byte */
/* RX@ ( -- u ) */
Code 3 "RX@" RXFetch SLINK
Next

/* ready to send? */
/* TX? ( -- flag ) */
Const 3 "TX?" TXQ TRUE SLINK
/* this was that for dos, should be different for uart of pic32 */

/* TX! ( u -- ) */
Code 3 "TX" TXStore SLINK
nop
Next

/* CR ( -- ) */
Colon 2 "CR" CR FLINK
.int DoLit, CRR, EMIT, DoLit, LFF, EMIT, EXIT

Code 3 "BYE" BYE FLINK
ALIGN
Next /* should never be reached! */

/* get some common information */
/* hi ( -- ) */
Colon 2 "hi" HI SLINK
.int CR
InStr "hForth "
.int TYPEE
InStr "CPU "
.int ENVIRONMENTQuery, DROP, TYPEE, SPACE
InStr "Model "
.int ENVIRONMENTQuery, DROP, TYPEE, SPACE, DoLit, 'v', EMIT
InStr "Version "
.int ENVIRONMENTQuery, DROP, TYPEE
InStr "intensively based on Wonyong Koh, 1997"
.int TYPEE, CR
InStr "All noncommercial and commercial uses are granted."
.int TYPEE, CR, EXIT

/* cold boot */
/* COLD ( -- ) */ 
Colon 4 "COLD" COLD SLINK
.int SPZero, SPStore, RPZero, RPStore
.int TickInit_IO, EXECUTE, TickBoot, EXECUTE
.int QUIT

/* choose where is console */
/* set-i/o ( -- ) */
Colon 7 "set-i/o" Set_IO SLINK
InStr "CON"
.int STDIN, EXIT

/******************************************************************************/
/* here are the more or less dependant words to do things after a cold boot */


/******************************************************************************/

/* une fois cette partie teste ci-dessus, ca devrait etre rapide.. */
main:
	b		COLD

