;================================================= ;Finite State Machine FSMACH ;Call with: ; HL -> six byte data structure which ; has the following structure. ; ds 2 ;addr of initial state table ; ds 2 ;addr of routine that returns next input byte ; ds 2 ;addr of state table interpreter routine ;exit: AF, BC, DE, HL are undefined ;If FSMACH is an independent module, these are the ;entry points needed to call and exit from FSMACH. public FSMACH,STM_X FSMACH: ex de,hl ;preserve initial data pointer ld hl,(nstate) ;save the current FSMACH data push hl ;on the stack in case of a ld hl,(getchr) ;recursive call to FSMACH push hl ld hl,(fscase) push hl ex de,hl ld de,nstate ;set up initial pointer table ld bc,6 ;there are six bytes ldir ;supplied by caller ;start of an infinite loop whose exit is ;accomplished by an action routine which ;jumps to or duplicates the functions of ;stm_x: (below). This exit restores the ;environment for the state machine, making ;recursive invocation possible. STM_00:: ld hl,$ push hl ; return to STM_00 call getbyt ; get the next character call do_stbl ; get data from current state table push de ; action routine address ld hl,nstate ; send address of FSMACH pointer data ret ; call the action routine, which ; normally returns to STM_00. ;----------------------------------------- ;The next two routines are the target of a ;call from STM_00 and implement an ;indirect call in each case. ;----------------------------------------- getbyt:: ;this routine performs an indirect jump ;to the routine which returns the next ;character for the FSMACH to process. ld hl,(getchr) jp (hl) ; an indirect jump ;----------------------------------------- do_stbl:: ;this called routine performs an indirect jump ;to the state table interpreter routine. ;The exit condition applies to the interpreter ;routine when it finally returns to STM_00. ;exit: DE -> addr of the next state table ld hl,nstate ; state table data structure ld bc,(fscase) ; use BC to avoid eating HL push bc ret ; an indirect jump ;----------------------------------------- ; This routine is jumped to from a case table ; to exit the finite state machine. ; It restores the contents of NSTATE, GETCHR, ; and FSCASE to their values prior to the ; call to FSMACH in case this was a ; recursive call. stm_x:: pop hl ;dump the return to STM_00 pop hl ld (fscase),hl ;restore the table interpreter, pop hl ld (getchr),hl ;the byte fetch routine, pop hl ld (nstate),hl ;and the next state tbl addr. ret ;Return to caller of FSMACH ;----------------------------------------- dseg ;this data is preserved and restored ;during recursive calls to FSMACH. It ;is the exact format of the structure ;pointed to by HL when calling FSMACH. nstate:: ds 2 ;addr of next state table getchr:: ds 2 ;addr of routine to get a byte fscase:: ds 2 ;addr of table interpreter routine end