home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 09 / bascii.asm next >
Assembly Source File  |  1992-03-29  |  18KB  |  465 lines

  1.             page    55,132
  2.             title   "BASCII - ASCII Protocol Module"
  3.  
  4. ; ---------------------------------------------------------------------
  5. ;
  6. ; Maintenance Log
  7. ;
  8. ; Version    Date   Description                        Who
  9. ; -------  -------  ---------------------------------- ----------------
  10. ;   1.0    08Feb91  Initial version complete           Flanders/Holmes
  11. ;
  12. ; ---------------------------------------------------------------------
  13.  
  14.  
  15. protocol    segment para public 'code'      ; Ascii module
  16.             assume  cs:protocol
  17.  
  18. FARRET      macro                           ; far return macro
  19.             db      0cbh
  20.             endm
  21.  
  22. ; ---------------------------------------------------------------------
  23. ;   protocol header
  24. ; ---------------------------------------------------------------------
  25.  
  26. start:      jmp     bpmascii                ; jump to start
  27.             db      "BD"                    ; backdown protocol ID
  28.             db      1                       ; nbr of protocols
  29.             db      "A"                     ; invocation letter
  30.             db      "Ascii V1.0      ", 0   ; name to display
  31.             db      1                       ; protocol nbr
  32.  
  33. ; ---------------------------------------------------------------------
  34. ;   local stack
  35. ; ---------------------------------------------------------------------
  36.  
  37.             even
  38. bpmstack    db      32 dup ("St")           ; local stack area
  39. bpmstackend db      "Es"                    ; End of local stack
  40.  
  41. stackoffset dw      offset bpmstackend      ; New stack pointer
  42.  
  43. ; ---------------------------------------------------------------------
  44. ;   data areas
  45. ; ---------------------------------------------------------------------
  46.  
  47. state       dw      -1                      ; current state
  48. ticks       dw      ?                       ; entry nbr ticks
  49.  
  50. jmptab      dw      init                    ; 0: initialization call
  51.             dw      fncok                   ; 2: last function ok
  52.             dw      fncfail                 ; 4: last function failed
  53.             dw      commchar                ; 6: character received
  54.             dw      kbdchar                 ; 8: keystroke encountered
  55.  
  56. MAXCODE     equ     8                       ; maximum entry code
  57. TIMEOUT     equ     ((182*30)/10)           ; 30 seconds in ticks
  58. DSPTIMEOUT  equ     (TIMEOUT-5)             ; 29.7 seconds left
  59.  
  60. filename    db      65 dup (0)              ; space for filename
  61.  
  62. writebuf    db      0                       ; address for write buf
  63. dispbuf     db      82 dup (?)              ; buffer for display chars
  64.  
  65. oldds       dw      ?                       ; caller's ds
  66.  
  67. oldsp       dw      ?                       ; caller's sp
  68. oldss       dw      ?                       ; caller's ss
  69.  
  70. init2x      db      13, 'ASCII: Init called twice', 13, 0
  71. badfnc      db      13, 'ASCII: Bad function code', 13, 0
  72. openerr     db      13, 'ASCII: Unable to open that file..'
  73. fileprompt  db      13, 'ASCII: Enter filename, <CR> to exit: ', 0
  74. nofile      db      13, 'ASCII: No filename, protocol aborted', 13, 0
  75. fileopened  db      13, 'ASCII: File opened.. starting capture', 13
  76.             db      13, '       Press "End" to stop capturing', 13, 0
  77. timedout    db      13, 'ASCII: No chars seen for 30 seconds, '
  78.             db          'protocol ended', 13, 0
  79. notinited   db      13, 'ASCII: BD did not call init first', 13, 0
  80. donebyreq   db      13, 'ASCII: Download complete', 13, 0
  81. writerr     db      13, 'ASCII: Write error.. protocol aborted', 13, 0
  82. staterr0    db      13, 'ASCII: State error 0', 13, 0
  83. staterr1    db      13, 'ASCII: State error 1', 13, 0
  84.  
  85.  
  86.  
  87. ; ---------------------------------------------------------------------
  88. ;   protocol mainline
  89. ; ---------------------------------------------------------------------
  90.  
  91. bpmascii    proc
  92.             push    es                      ; save caller's regs
  93.             push    ds
  94.  
  95.             mov     cs:oldds, ds            ; keep copy of caller's ds
  96.  
  97.             mov     cs:oldss, ss            ; save caller's stack
  98.             mov     cs:oldsp, sp
  99.  
  100.             mov     cs:ticks, ax            ; save entry ticks
  101.  
  102.             mov     ax, cs                  ; ax -> our segment
  103.             mov     ds, ax                  ; ds -> our segment
  104.             mov     es, ax                  ; es -> our segment
  105.  
  106.             cli                             ; no interrupts ..
  107.             mov     ss, ax                  ; ss -> our segment
  108.             mov     sp, stackoffset         ; sp -> end of stack
  109.             sti                             ; .. allow ints again
  110.  
  111.             cmp     di, MAXCODE             ; q. is code ok?
  112.             ja      bpmascii90              ; a. no .. return "done"
  113.  
  114.             test    di, 1                   ; q. is code even
  115.             jnz     bpmascii90              ; a. no .. return "done"
  116.  
  117.             jmp     jmptab[di]              ; .. call requested routine
  118.  
  119. bpmascii90: lea     bx, badfnc              ; bx -> bad function code
  120.             jmp     proto_err               ; issue error and leave
  121.  
  122. bpmascii    endp
  123.  
  124.  
  125.  
  126. ; ---------------------------------------------------------------------
  127. ;   init - initialization call
  128. ;
  129. ;   entry: ds:bx -> operands (filename)
  130. ; ---------------------------------------------------------------------
  131.  
  132. init        proc
  133.  
  134.             cmp     state, -1               ; q. already init'd?
  135.             je      init00                  ; a. no .. continue
  136.  
  137.             lea     bx, init2x              ; bx -> error message
  138.             jmp     proto_err               ; .. kill the protocol
  139.  
  140. init00:     mov     ds, oldds               ; ds -> callers segment
  141.  
  142.             cmp     byte ptr [bx], 0        ; q. any filename given?
  143.             je      init10                  ; a. no .. get the file name
  144.  
  145.             lea     di, filename            ; di -> area for filename
  146.             mov     si, bx                  ; si -> input filename
  147.             cld                             ; .. move upward
  148.  
  149. init05:     movsb                           ; move in a byte
  150.  
  151.             cmp     di, offset filename+64  ; q. end of area?
  152.             je      init20                  ; a. yes .. continue
  153.  
  154.             cmp     byte ptr [si-1],0       ; q. end of string?
  155.             jne     init05                  ; a. no .. continue
  156.  
  157.             push    cs                      ; save our segment
  158.             pop     ds                      ; .. ds -> our segment
  159.             jmp     short init20            ; .. open the file
  160.  
  161. init10:     lea     bx, fileprompt          ; bx -> filename prompt
  162.  
  163. init15:     mov     di, 12                  ; di =  display ASCIIZ
  164.             call    callback                ; .. ask bd to display
  165.  
  166.             lea     bx, filename            ; bx -> filename
  167.             mov     di, 16                  ; di =  get a line
  168.             call    callback                ; .. ask bd to get it
  169.  
  170.             cmp     filename, ' '           ; q. first char non-blank?
  171.             ja      init20                  ; a. yes .. continue
  172.  
  173.             lea     bx, nofile              ; bx = no file given
  174.             jmp     proto_err               ; .. kill the protocol
  175.  
  176. init20:     lea     bx, filename            ; bx -> filename
  177.             mov     di, 2                   ; di = open file
  178.             call    callback                ; q. file open ok?
  179.             jnc     init25                  ; a. yes .. continue
  180.  
  181.             lea     bx, openerr             ; bx -> open error
  182.             jmp     init15                  ; Tell user .. ask for next
  183.  
  184. init25:     lea     bx, fileopened          ; bx -> file opened msg
  185.             mov     di, 12                  ; di = display string
  186.             call    callback                ; .. ask bd to do it
  187.  
  188.             mov     dispbuf, 0              ; .. zero out display buffer
  189.  
  190.             mov     bx, TIMEOUT             ; bx = ticks for 30 seconds
  191.             mov     di, 22                  ; di = set tick downcounter
  192.             call    callback                ; .. set the down counter
  193.  
  194.             mov     bl, 0dh                 ; bl = char to send
  195.             mov     di, 20                  ; di = send char
  196.             call    callback                ; .. send the character
  197.  
  198.             jmp     proto_ok                ; .. and init is done
  199.  
  200. init        endp
  201.  
  202.  
  203.  
  204. ; ---------------------------------------------------------------------
  205. ;   fncok - last function executed ok -or- simple dispatch
  206. ;
  207. ;   state = 0: simple dispatch
  208. ;   state = 1: state return
  209. ;   -- Carry is cleared
  210. ; ---------------------------------------------------------------------
  211.  
  212. fncok       proc
  213.  
  214.             cmp     state, -1               ; q. init'd yet?
  215.             jne     fncok05                 ; a. yes .. continue
  216.  
  217.             lea     bx, notinited           ; bx -> not init'd msg
  218.             jmp     proto_err               ; .. kill the protocol
  219.  
  220. fncok05:    cmp     state, 0                ; q. simple dispatch?
  221.             je      fncok10                 ; a. yes.. run around
  222.  
  223.             clc                             ; show function
  224.             ret                             ; .. rtn to callback caller
  225.  
  226. fncok10:    cmp     ticks, 0                ; q. timeout?
  227.             jnl     fncok20                 ; a. no .. check if display
  228.  
  229.             lea     bx, timedout            ; bx -> timeout message
  230.             jmp     proto_err               ; .. we are done!
  231.  
  232. fncok20:    cmp     ticks, DSPTIMEOUT       ; q. display timeout?
  233.             ja      fncok90                 ; a. no .. tell 'em all is ok
  234.  
  235.             cmp     dispbuf, 0              ; q. anything to display?
  236.             je      fncok90                 ; a. no .. exit
  237.  
  238.             call    dsp_n_wrt               ; q. display & write it ok?
  239.             jnc     fncok90                 ; a. yes .. return ok
  240.  
  241.             lea     bx, writerr             ; bx -> write error message
  242.             jmp     proto_err               ; .. kill the protocol
  243.  
  244. fncok90:    jmp     proto_ok                ; .. return all is ok
  245.  
  246. fncok       endp
  247.  
  248.  
  249.  
  250. ; ---------------------------------------------------------------------
  251. ;   fncfail - previous function failed                 
  252. ;
  253. ;   Carry = failure
  254. ; ---------------------------------------------------------------------
  255.  
  256. fncfail     proc                            ; last function failed
  257.  
  258.             cmp     state, 1                ; q. waiting on return
  259.             je      fncfail10               ; a. yes .. continue
  260.  
  261.             lea     bx, staterr1            ; bx -> state error msg
  262.             jmp     proto_err               ; .. tell user & die
  263.  
  264. fncfail10:  stc                             ; set carry to show fail
  265.             ret                             ; return to callback caller
  266.  
  267. fncfail     endp
  268.  
  269.  
  270.  
  271. ; ---------------------------------------------------------------------
  272. ;   commchar - process communications character  
  273. ;
  274. ;   entry: dl = character
  275. ; ---------------------------------------------------------------------
  276.  
  277. commchar    proc
  278.  
  279.             cmp     state, 0                ; q. no function requested?
  280.             je      commchar10              ; a. yes .. continue
  281.  
  282.             lea     bx, staterr0            ; bx -> state error msg
  283.             jmp     proto_err               ; .. tell user & die
  284.  
  285. commchar10: or      dl, dl                  ; q. nul character?
  286.             jz      commchar90              ; a. yes .. exit
  287.  
  288.             inc     dispbuf                 ; increment buffer cnt
  289.  
  290.             xor     dh, dh                  ; dh = 0
  291.  
  292.             mov     bl, dispbuf             ; bl = display buffer len
  293.             xor     bh, bh                  ; .. upper bits off
  294.  
  295.             mov     word ptr dispbuf[bx], dx; save char & ASCIIZ
  296.  
  297.             mov     bx, TIMEOUT             ; bx = timeout count
  298.             mov     di, 22                  ; di = set tick downcounter
  299.             call    callback                ; .. ask BD to do it
  300.  
  301.             cmp     dispbuf, 80             ; q. buffer full?
  302.             jb      commchar90              ; a. no .. exit
  303.  
  304.             call    dsp_n_wrt               ; q. display & write ok?
  305.             jnc     commchar90              ; a. yes .. done for now
  306.  
  307.             lea     bx, writerr             ; bx -> write error message
  308.             jmp     short proto_err         ; .. kill the protocol
  309.  
  310. commchar90: jmp     short proto_ok          ; function worked ok
  311.  
  312. commchar    endp
  313.  
  314.  
  315.  
  316. ; ---------------------------------------------------------------------
  317. ;   kbdchar - process keyboard character               
  318. ;
  319. ;   entry: dl = character
  320. ; ---------------------------------------------------------------------
  321.  
  322. kbdchar     proc
  323.  
  324.             cmp     state, 0                ; q. no function requested?
  325.             je      kbdchar00               ; a. yes .. continue
  326.  
  327.             lea     bx, staterr0            ; bx -> state error msg
  328.             jmp     short proto_err         ; .. tell user & die
  329.  
  330. kbdchar00:  or      dl, dl                  ; q. special key?
  331.             jnz     kbdchar20               ; a. no .. send it out
  332.  
  333.             cmp     dh, 4fh                 ; q. END key?
  334.             jne     kbdchar90               ; a. no .. ignore it
  335.  
  336.             cmp     dispbuf, 0              ; q. any chars in buffer?
  337.             je      kbdchar10               ; a. no .. end now
  338.  
  339.             call    dsp_n_wrt               ; else display & write
  340.  
  341. kbdchar10:  lea     bx, donebyreq           ; bx -> done by request
  342.             jmp     short proto_err         ; .. end the protocol
  343.  
  344. kbdchar20:  mov     di, 20                  ; di = send one char
  345.             mov     bl, dl                  ; bl = character to send
  346.             call    callback                ; .. send it out
  347.  
  348. kbdchar90:  jmp     short proto_ok          ; .. done for now
  349.  
  350. kbdchar     endp
  351.  
  352.  
  353.  
  354. ; ---------------------------------------------------------------------
  355. ;   dsp_n_wrt - dispay and write out buffer
  356. ; ---------------------------------------------------------------------
  357.  
  358. dsp_n_wrt   proc
  359.  
  360.             lea     bx, dispbuf+1           ; bx -> chars to display
  361.             mov     di, 24                  ; di = display ASCIIZ
  362.             call    callback                ; .. ask BD to do it
  363.  
  364.             mov     ax, word ptr writebuf   ; ax = littlendian value
  365.             xchg    ah, al                  ; .. make it intel format
  366.             mov     word ptr writebuf, ax   ; .. and save it
  367.  
  368.             lea     bx, writebuf            ; bx -> buffer to write
  369.             mov     di, 4                   ; di =  write the buffer
  370.             call    callback                ; q. BD write it ok?
  371.             jnc     dsp_n_wrt90             ; a. yes .. clean up & exit
  372.  
  373.             lea     bx, writerr             ; bx -> write error msg
  374.             jmp     short proto_err         ; .. write note & die
  375.  
  376. dsp_n_wrt90:mov     writebuf, 0             ; Zero first byte of wbuf
  377.             mov     word ptr dispbuf, 0     ; .. and len, first of dsp
  378.  
  379.             ret                             ; return to caller
  380.  
  381. dsp_n_wrt   endp
  382.  
  383.  
  384.  
  385. ; ---------------------------------------------------------------------
  386. ;   callback - setup to "call" backdown                 
  387. ;
  388. ;   entry: di = return code
  389. ; ---------------------------------------------------------------------
  390.  
  391. callback    proc
  392.  
  393.             mov     state, 1                ; show were in callback mode
  394.  
  395.             jmp     short proto_exit        ; return to caller
  396.  
  397. callback    endp
  398.  
  399.  
  400. ; ---------------------------------------------------------------------
  401. ;   proto_err - display error, return done               
  402. ;
  403. ;   entry: bx -> string
  404. ; ---------------------------------------------------------------------
  405.  
  406. proto_err   proc
  407.  
  408.             mov     di, 12                  ; di = display string
  409.             call    callback                ; .. display it
  410.  
  411.             jmp     short proto_done        ; tell bd we are done
  412.  
  413. proto_err   endp
  414.  
  415.  
  416.  
  417. ; ---------------------------------------------------------------------
  418. ;   proto_done - protocol done - leave forever
  419. ; ---------------------------------------------------------------------
  420.  
  421. proto_done  proc
  422.  
  423.             mov     di, 10                  ; di = done code
  424.             jmp     short proto_exit        ; ..and return to caller
  425.  
  426. proto_done  endp
  427.  
  428.  
  429.  
  430. ; ---------------------------------------------------------------------
  431. ;   proto_ok - exit with the OK code
  432. ; ---------------------------------------------------------------------
  433.  
  434. proto_ok    proc
  435.  
  436.             mov     state, 0                ; zero out the state
  437.             mov     di, 0                   ; di = ok return code
  438.             jmp     short proto_exit        ; restore regs & exit
  439.  
  440. proto_ok    endp
  441.  
  442.  
  443. ; ---------------------------------------------------------------------
  444. ;   proto_exit - protcol exit routine
  445. ; ---------------------------------------------------------------------
  446.  
  447. proto_exit  proc
  448.  
  449.             mov     stackoffset, sp         ; save our stack offset
  450.  
  451.             cli                             ; no interrupts
  452.             mov     ss, oldss               ; ..restore callers ss
  453.             mov     sp, oldsp               ; ..and sp
  454.             sti                             ; ints ok again
  455.  
  456.             pop     ds                      ; restore caller's regs
  457.             pop     es
  458.  
  459.             FARRET                          ; return to caller
  460.  
  461. proto_exit  endp
  462.  
  463. protocol    ends
  464.             end     start
  465.