home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / filutl / pwd20.lbr / FSMACH.ZZ0 / FSMACH.Z80
Text File  |  1989-01-06  |  3KB  |  103 lines

  1. ;=================================================
  2.  
  3. ;Finite State Machine FSMACH
  4. ;Call with:
  5. ;    HL -> six byte data structure which
  6. ;          has the following structure.
  7. ;    ds 2    ;addr of initial state table
  8. ;    ds 2    ;addr of routine that returns next input byte
  9. ;    ds 2    ;addr of state table interpreter routine
  10.  
  11. ;exit:    AF, BC, DE, HL are undefined
  12.  
  13. ;If FSMACH is an independent module, these are the
  14. ;entry points needed to call and exit from FSMACH.
  15.     public    FSMACH,STM_X
  16.  
  17. FSMACH:
  18.     ex    de,hl        ;preserve initial data pointer
  19.     ld    hl,(nstate)    ;save the current FSMACH data
  20.     push    hl        ;on the stack in case of a
  21.     ld    hl,(getchr)    ;recursive call to FSMACH
  22.     push    hl
  23.     ld    hl,(fscase)
  24.     push    hl
  25.     ex    de,hl
  26.  
  27.     ld    de,nstate    ;set up initial pointer table
  28.     ld    bc,6        ;there are six bytes
  29.     ldir            ;supplied by caller
  30.  
  31. ;start of an infinite loop whose exit is
  32. ;accomplished by an action routine which
  33. ;jumps to or duplicates the functions of
  34. ;stm_x: (below). This exit restores the
  35. ;environment for the state machine, making
  36. ;recursive invocation possible.
  37.  
  38. STM_00::    ld    hl,$
  39.     push    hl        ; return to STM_00
  40.     call    getbyt        ; get the next character
  41.     call    do_stbl        ; get data from current state table
  42.     push    de        ; action routine address
  43.     ld    hl,nstate    ; send address of FSMACH pointer data
  44.     ret            ; call the action routine, which
  45.                 ; normally returns to STM_00.
  46.  
  47. ;-----------------------------------------
  48. ;The next two routines are the target of a
  49. ;call from STM_00 and implement an
  50. ;indirect call in each case.
  51. ;-----------------------------------------
  52. getbyt::
  53. ;this routine performs an indirect jump
  54. ;to the routine which returns the next
  55. ;character for the FSMACH to process.
  56.     ld    hl,(getchr)
  57.     jp    (hl)        ; an indirect jump
  58.  
  59. ;-----------------------------------------
  60. do_stbl::
  61. ;this called routine performs an indirect jump
  62. ;to the state table interpreter routine.
  63. ;The exit condition applies to the interpreter
  64. ;routine when it finally returns to STM_00.
  65.  
  66. ;exit:    DE -> addr of the next state table
  67.  
  68.     ld    hl,nstate    ; state table data structure
  69.     ld    bc,(fscase)    ; use BC to avoid eating HL
  70.     push    bc
  71.     ret            ; an indirect jump
  72.  
  73. ;-----------------------------------------
  74.  
  75. ; This routine is jumped to from a case table
  76. ; to exit the finite state machine.
  77. ; It restores the contents of NSTATE, GETCHR,
  78. ; and FSCASE to their values prior to the
  79. ; call to FSMACH in case this was a
  80. ; recursive call.
  81. stm_x::    pop    hl        ;dump the return to STM_00
  82.     pop    hl
  83.     ld    (fscase),hl    ;restore the table interpreter,
  84.     pop    hl
  85.     ld    (getchr),hl    ;the byte fetch routine,
  86.     pop    hl
  87.     ld    (nstate),hl    ;and the next state tbl addr.
  88.     ret            ;Return to caller of FSMACH
  89.  
  90. ;-----------------------------------------
  91.     dseg
  92.  
  93. ;this data is preserved and restored
  94. ;during recursive calls to FSMACH. It
  95. ;is the exact format of the structure
  96. ;pointed to by HL when calling FSMACH.
  97.  
  98. nstate::    ds    2        ;addr of next state table
  99. getchr::    ds    2        ;addr of routine to get a byte
  100. fscase::    ds    2        ;addr of table interpreter routine
  101.  
  102.     end
  103.