home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Workbench / Dvices / NEW8N1.LHA / 8n1_020.s < prev    next >
Encoding:
Text File  |  1996-11-20  |  57.5 KB  |  1,883 lines

  1. ********************************************************************************
  2. **                                          **
  3. **  Name       : 8n1_020.s                              **
  4. **  Copyright  : © Copyright 96                           **
  5. **  Author     : Iain Barclay                              **
  6. **  Created    : 19 Nov 96                              **
  7. **  Version    : 37.22                                  **
  8. **                                          **
  9. ********************************************************************************
  10.      ;
  11.      ;      SNMA specific options
  12.      ;
  13.      IFD      SNMA
  14.      ;
  15.      CPU      M68030
  16.      ;
  17.      SNMAOPT  Q,A,M,T,E-,P,R,B
  18.      ;
  19.      ELSE
  20.      machine 68030
  21.      OPT !
  22.      ENDC
  23.  
  24.      INCDIR   "dcc:Ainclude/"
  25.      ;
  26.      ;
  27.      ;
  28. DEBUG     SET      0
  29. NEWCODE  SET      1
  30. EOFCODE  SET      0
  31. SETDSR     SET      0
  32. NCOMM     SET      0
  33.      ;
  34.      ;
  35.      ;
  36.      SECTION  text,CODE
  37.      ;
  38.      ;
  39.      ;
  40. DEVICES_SERIAL_I_OBSOLETE  EQU        1
  41.      ;
  42.      ;
  43.      ;
  44.      INCLUDE  "exec/lists.i"
  45.      INCLUDE  "exec/memory.i"
  46.      INCLUDE  "exec/resident.i"
  47.      INCLUDE  "exec/devices.i"
  48.      INCLUDE  "exec/execbase.i"
  49.      INCLUDE  "exec/io.i"
  50.      INCLUDE  "exec/ports.i"
  51.      INCLUDE  "exec/errors.i"
  52.      INCLUDE  "exec/initializers.i"
  53.      INCLUDE  "intuition/preferences.i"
  54.      INCLUDE  "devices/timer.i"
  55.      INCLUDE  "devices/serial.i"
  56.      INCLUDE  "hardware/custom.i"
  57.      INCLUDE  "hardware/cia.i"
  58.      INCLUDE  "hardware/intbits.i"
  59.      INCLUDE  "hardware/adkbits.i"
  60.      INCLUDE  "resources/misc.i"
  61.      INCLUDE  "exec/alerts.i"
  62.      INCLUDE  "exec/macros.i"
  63.      INCLUDE  "8n1.device_rev.i"
  64.      ;
  65.      ;      Define hardware references
  66.      ;
  67.      XREF      _AbsExecBase
  68.      XREF      _custom
  69.      XREF      _intena,_intenar,_intreq,_intreqr
  70.      XREF      _ciab,_ciabpra
  71.      XREF      _serper,_serdat,_serdatr
  72.      XREF      _adkcon,_adkconr
  73.      ;
  74.      ;      Exec Functions
  75.      ;
  76.      XREF      _LVORemDevice,_LVOOpenDevice,_LVOCloseDevice
  77.      XREF      _LVOSupervisor
  78.      XREF      _LVORemove,_LVORemHead,_LVOAddTail
  79.      XREF      _LVOAllocMem,_LVOFreeMem,_LVOCopyMem
  80.      XREF      _LVOReplyMsg
  81.      XREF      _LVOSendIO,_LVOAbortIO
  82.      XREF      _LVODisable,_LVOEnable
  83.      XREF      _LVOFindName
  84.      XREF      _LVOOpenResource
  85.      XREF      _LVOAddIntServer,_LVORemIntServer
  86. ;      XREF       _LVOSetIntVector,_LVOObtainQuickVector
  87.      XREF      _LVOAlert
  88.      XREF      _LVOOpenLibrary,_LVOCloseLibrary
  89.      ;
  90.      ;      Misc Resource Functions
  91.      ;
  92.      XREF      _LVOAllocMiscResource,_LVOFreeMiscResource
  93.      ;
  94.      ;      Intuition Functions
  95.      ;
  96.      XREF      _LVOGetPrefs
  97.      ;
  98.      ;      Mask used to get rid of the printer bits.
  99.      ;
  100. PRTMASK  EQU      (CIAF_PRTRPOUT|CIAF_PRTRBUSY)
  101.      ;
  102.      ;      Autovector offsets
  103.      ;
  104. LVL1VEC  EQU      (1-1)*4+$64
  105. LVL5VEC  EQU      (5-1)*4+$64
  106.      ;
  107.      ;      Device base
  108.      ;
  109.      STRUCTURE Base8n1,LIB_SIZE
  110.      UBYTE      vb_SaveDDRA
  111.      UBYTE      vb_SavePRA
  112.      APTR      vb_MiscBase
  113.      APTR      vb_OldLevel1
  114.      APTR      vb_OldLevel5
  115.      ULONG      vb_SegList
  116.      ULONG      vb_DefBaud
  117.      ULONG      vb_DefRBufLen
  118.      ULONG      vb_CurRBuf
  119.      ULONG      vb_CurRBufLen
  120.      ULONG      vb_CurBaud
  121.      UBYTE      vb_SerFlags
  122.      UBYTE      vb_Initialized
  123.      LABEL      sizeof_Base8n1
  124.      ;
  125.      ;
  126.      ;
  127. DISABLE  MACRO
  128.      IFNC      '\1',''
  129.      move.w   #INTF_\1,_intena
  130.      ELSE
  131.      jsr      _LVODisable(a6)
  132.      ENDC
  133.      ENDM
  134.      ;
  135. ENABLE     MACRO
  136.      IFNC      '\1',''
  137.      move.w   #INTF_SETCLR|INTF_\1,_intena
  138.      ELSE
  139.      jsr      _LVOEnable(a6)
  140.      ENDC
  141.      ENDM
  142.      ;
  143.      ;
  144.      ;
  145. Start:
  146.      moveq      #-1,d0                  ; set return code
  147.      rts                          ; return
  148.      ;
  149.      ;      RamLib looks for this romtag
  150.      ;
  151. ROMTag     DC.W      RTC_MATCHWORD               ; RT_MATCHWORD
  152.      DC.L      ROMTag                  ; RT_MATCHTAG
  153.      DC.L      ENDTag                  ; RT_ENDSKIP
  154.      DC.B      RTF_AUTOINIT                  ; RT_FLAGS
  155.      DC.B      VERSION                  ; RT_VERSION
  156.      DC.B      NT_DEVICE                  ; RT_TYPE
  157.      DC.B      0                      ; RT_PRI
  158.      DC.L      Name                      ; RT_NAME
  159.      DC.L      IdString                  ; RT_IDSTRING
  160.      DC.L      Init                      ; RT_INIT
  161.      ;
  162.      ;      Perform device initialization
  163.      ;
  164.     cnop 0,4    ; align for 020+
  165. InitRoutine:
  166.      exg      d0,a0                   ; swap seglist and base
  167.      move.l   d0,vb_SegList(a0)              ; store seglist in base
  168.      move.l   a6,SysBase                  ; store in global storage
  169.      exg      a0,d0                   ; swap them back
  170.      rts                          ; return
  171.      ;
  172.      ;
  173.      ;
  174.     cnop 0,4 ; align for 020+
  175. dev_Open:   ; (a1 iorequest, a5 will be Base8n1, a6 sysbase )
  176.      move.l   a5,-(sp)                  ; save registers
  177.      movea.l  a6,a5                   ; save base
  178.      movea.l  SysBase(pc),a6              ; get ExecBase
  179.      ;
  180.      tst.l      d0                      ; unit 0 specified?
  181.      bne.b      50$                      ; nope, error (who cares?)
  182.      ;
  183.      tst.w      LIB_OPENCNT(a5)              ; currently open?
  184.      bne.b      10$                      ; yep, go process
  185.      ;
  186.      move.b   IO_SERFLAGS(a1),vb_SerFlags(a5)     ; save flags
  187.      ;
  188.      tst.b      vb_Initialized(a5)              ; already initialized?
  189.      bne.b      40$                      ; yep, skip initialization
  190.      ;
  191.      bsr      initResources               ; go alloc resources
  192.      tst.l      d0                      ; initialized?
  193.      bne.b      50$                      ; nope, error
  194.      ;
  195.      bra.b      40$                      ; go exit
  196.      ;
  197.      cnop 0,4 ; align for 020+
  198. 10$     moveq      #SerErr_DevBusy,d0              ; preset error status
  199.      btst.b   #SERB_SHARED,vb_SerFlags(a5)          ; opened shared?
  200.      beq.b      50$                      ; nope, error
  201.      btst.b   #SERB_SHARED,IO_SERFLAGS(a1)          ; requesting shared?
  202.      beq.b      50$                      ; nope, error
  203.      ;
  204.      ;      Initialize I/O request
  205.      ;
  206. 40$     moveq      #8,d0                   ; get char size
  207.      move.b   d0,IO_READLEN(a1)              ; set read length
  208.      move.b   d0,IO_WRITELEN(a1)              ; set write length
  209.      moveq      #1,d0                   ; get stop bits
  210.      move.b   d0,IO_STOPBITS(a1)              ; set stop bits
  211.      move.b   IO_SERFLAGS(a1),d0
  212.      ori.b      #SERF_XDISABLED|SERF_RAD_BOOGIE|SERF_QUEUEDBRK|SERF_7WIRE,d0 ;flags
  213.      andi.b   #~(SERF_PARTY_ODD|SERF_PARTY_ON),d0 ; not used
  214.      move.b   d0,IO_SERFLAGS(a1)
  215.      move.l   vb_CurBaud(a5),IO_BAUD(a1)          ; set baud
  216.      move.l   vb_CurRBufLen(a5),IO_RBUFLEN(a1)    ; set read buffer length
  217.      ;
  218.      addq.w   #1,LIB_OPENCNT(a5)              ; incr open count
  219.      andi.w   #~(1<<LIBB_DELEXP),LIB_FLAGS(a5)    ; clear expunge bit
  220.      moveq      #0,d0                   ; no error
  221.      ;
  222. 50$     move.b   d0,IO_ERROR(a1)              ; store error code
  223.      movea.l  a5,a6                   ; restore base
  224.      move.l   (sp)+,a5                  ; restore registers
  225.      rts                          ; return
  226.      ;
  227.      ;      Attempt to allocate one of the serial resources.
  228.      ;
  229.      cnop 0,4 ; align for 020+
  230. allocResource:
  231.      move.l   a6,-(sp)                  ; save base pointer
  232.      move.l   d0,-(sp)                  ; save unit
  233.      lea.l      Name(pc),a1                  ; get lock name
  234.      move.l   vb_MiscBase(a5),a6              ; get MiscBase
  235.      jsr      _LVOAllocMiscResource(a6)          ; go allocate it
  236.      tst.l      d0                      ; did we get it?
  237.      beq.b      20$                      ; yep, branch
  238.      ;
  239.      ;      It's in use, so we try to locate the device using the string
  240.      ;      returned and attempt to remove it.
  241.      ;
  242.      movea.l  SysBase(pc),a6              ; get ExecBase
  243.      ;
  244.      movea.l  d0,a1                   ; get ptr to serial name
  245.      lea.l      DeviceList(a6),a0              ; get ptr to device list
  246.      jsr      _LVOFindName(a6)              ; go find it
  247.      tst.l      d0                      ; found?
  248.      beq.b      10$                      ; nope, branch
  249.      ;
  250.      movea.l  d0,a1                   ; xfer device ptr
  251.      jsr      _LVORemDevice(a6)              ; remove it
  252.      ;
  253.      ;      We then retry the allocate.
  254.      ;
  255. 10$     move.l   (sp),d0                  ; get unit
  256.      lea.l      Name(pc),a1                  ; get lock name
  257.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  258.      jsr      _LVOAllocMiscResource(a6)          ; go allocate it
  259.      ;
  260. 20$     addq.l   #4,sp                   ; restore stack ptr
  261.      movea.l  (sp)+,a6                  ; restore base ptr
  262.      rts                          ; return
  263.      ;
  264.      ;
  265.      ;
  266.      cnop 0,4 ; align for 020+
  267. initResources:    ; (a1 iorequest, a5 Base8n1, a6 sysbase )
  268.      move.l   a1,-(sp)                  ; save register
  269.      ;
  270.      lea.l      miscresource(pc),a1              ; ptr to resource name
  271.      jsr      _LVOOpenResource(a6)              ; go open it
  272.      move.l   d0,vb_MiscBase(a5)              ; save base
  273.      ;
  274.      moveq      #MR_SERIALPORT,d0              ; set unit number
  275.      bsr.b      allocResource               ; go allocate it
  276.      tst.l      d0                      ; did we get it?
  277.      bne.b      20$                      ; nope, error
  278.      ;
  279.      moveq      #MR_SERIALBITS,d0              ; set unit number
  280.      bsr.b      allocResource               ; go allocate it
  281.      tst.l      d0                      ; did we get it?
  282.      bne.b      10$                      ; nope, error
  283.      ;
  284.      bsr      getPrefs                  ; get default preferences
  285.      tst.l      d0                      ; got 'em?
  286.      beq.b      30$                      ; yep, branch
  287.      ;
  288.      moveq      #MR_SERIALBITS,d0              ; set unit
  289.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  290.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  291.      ;
  292. 10$     moveq      #MR_SERIALPORT,d0              ; set unit
  293.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  294.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  295.      movea.l  SysBase(pc),a6              ; restore ExecBase
  296.      ;
  297. 20$     moveq      #SerErr_DevBusy,d0              ; set error status
  298.      bra      40$                      ; go return
  299.      ;
  300.      cnop 0,4 ; align for 020+
  301. 30$     moveq      #0,d1                   ; clear flags
  302.      moveq      #UNIT_VBLANK,d0              ; set unit
  303.      lea.l      timerReq(pc),a1              ; ptr to timer request
  304.      lea.l      timerdevice(pc),a0              ; ptr to device name
  305.      jsr      _LVOOpenDevice(a6)              ; go open it
  306.      ;
  307.  
  308.      DISABLE                      ; disable interrupts
  309.      ;
  310.      lea.l      _ciab,a1                  ; get ptr to ciab
  311.      move.b   ciaddra(a1),d0
  312.      move.b   d0,vb_SaveDDRA(a5)              ; save DDR value
  313.      andi.b   #PRTMASK,d0                  ; make serial bits input
  314.      move.b   ciapra(a1),d1
  315.      ori.b      #CIAF_COMDTR|CIAF_COMRTS,d0          ; make DTR/RTS output
  316.      move.b   d1,vb_SavePRA(a5)              ; save PR value
  317.      andi.b   #CIAF_COMDTR|CIAF_COMRTS|PRTMASK,d0 ; turn on DTR/RTS
  318.      move.b   d0,ciaddra(a1)
  319.      andi.b   #CIAF_COMCTS|CIAF_COMDSR|PRTMASK,d1 ; make CTS/DSR input
  320.      move.b   d1,ciapra(a1)
  321.      ;
  322.      moveq      #INTB_PORTS,d0              ; get interrupt number
  323.      lea.l      VBInterrupt(pc),a1              ; get interrupt ptr
  324.      jsr      _LVOAddIntServer(a6)              ; add it to the list
  325.      ;
  326.      bsr      getVBR                  ; get vector base (in A0)
  327.      ;
  328.      move.l   LVL1VEC(a0),vb_OldLevel1(a5)          ; save original vector
  329.      lea.l      level1(pc),a1               ; get new vector ptr
  330.      move.l   a1,LVL1VEC(a0)              ; set new vector
  331.      ;
  332.      move.l   LVL5VEC(a0),vb_OldLevel5(a5)          ; save original vector
  333.      lea.l      level5(pc),a1               ; get new vector ptr
  334.      move.l   a1,LVL5VEC(a0)              ; set new vector
  335.      ;
  336.      lea.l      _custom,a1                  ; get ptr to custom chips
  337.      move.w   #INTF_RBF|INTF_TBE,intreq(a1)       ; clear pending interrupts
  338.      move.w   #INTF_SETCLR|INTF_RBF|INTF_TBE,intena(a1) ; enable RBF & TBE
  339.      ;
  340.      addq.b   #1,vb_Initialized(a5)           ; set flag
  341.      moveq      #0,d0                   ; set good status
  342.      ;
  343.      ENABLE                       ; enable interrupts
  344.      ;
  345. 40$     movea.l  (sp)+,a1                  ; restore register
  346.      rts                          ; return
  347.      ;
  348.      ;      Get system preferences
  349.      ;
  350.      cnop 0,4 ; align for 020+
  351. getPrefs:
  352.      move.l   a1,-(sp)                  ; save registers
  353.      ;
  354.      lea.l      intuitlib(pc),a1              ; ptr to library name
  355.      moveq      #0,d0                   ; can't use OldOpenLibrary as this will not be supported.
  356.      jsr      _LVOOpenLibrary(a6)              ; go open it (any version)
  357.      movea.l  d0,a6                   ; get intuition base
  358.      ;
  359.      move.l   #(pf_SerParShk+3)&$fffffffc,d0      ; size we need (aligned)
  360.      suba.l   d0,sp                   ; reserve space
  361.      ;
  362.      movea.l  sp,a0                   ; set data area ptr
  363.      jsr      _LVOGetPrefs(a6)              ; get preferences
  364.      ;
  365.      movea.l  a6,a1                   ; get intuition base
  366.      movea.l  SysBase(pc),a6              ; restore ExecBase
  367.      jsr      _LVOCloseLibrary(a6)              ; close it
  368.      ;
  369.      moveq      #$0f,d1                  ; set mask
  370.      and.b      pf_SerStopBuf(sp),d1              ; get bufsize index
  371.      addq.l   #8,d1                   ; calc shift value
  372.      moveq      #2,d0                   ; get 1<<1
  373.      lsl.l      d1,d0                   ; get default bufsize
  374.      move.l   d0,vb_DefRBufLen(a5)              ; and store
  375.      ;
  376.      moveq      #0,d1                   ; clear upper half
  377.      move.w   pf_BaudRate(sp),d1              ; get baud rate
  378.      move.w   baudTable(pc,d1.w*2),d1          ; get baud rate
  379.      move.l   d1,vb_DefBaud(a5)              ; and store
  380.      ;
  381.      bsr      internalReset               ; go init baud and buffer
  382.      ;
  383.      lea.l      (pf_SerParShk+3)&$fffffffc(sp),sp   ; restore stack
  384.      movea.l  (sp)+,a1                  ; restore registers
  385.      rts                          ; return ( status in D0 )
  386.      ;
  387.      ;      Preferences baud lookup table
  388.      ;
  389. baudTable:
  390.      dc.w      112,300,1200,2400,4800,9600,19200,31250,38400,57600,62400,64800
  391.      ;
  392.      ;      Set exception vectors
  393.      ;
  394.      cnop 0,4 ; align for 020+
  395. getVBR:
  396.      move.l   a5,-(sp)                  ; save registers
  397.      lea.l      20$(pc),a5                  ; ptr to routine
  398.      jsr      _LVOSupervisor(a6)              ; get into supervisor state
  399. 10$     movea.l  (sp)+,a5                  ; restore register
  400.      rts                          ; return
  401. 20$     movec.l  vbr,a0                  ; get vector base
  402.      rte                          ; return
  403.      ;
  404.      ;
  405.      ;
  406.      cnop 0,4 ; align for 020+
  407. freeResources:
  408.      move.l   a1,-(sp)                  ; save registers
  409.      ;
  410.      DISABLE                      ; disable interrupts
  411.      ;
  412.      bsr      getVBR                  ; get vector base (in A0)
  413.      ;
  414.      moveq      #1,d0                   ; set not restored code
  415.      ;
  416.      lea.l      level1(pc),a1               ; get our vector ptr
  417.      cmpa.l   LVL1VEC(a0),a1              ; do they match?
  418.      bne      10$                      ; nope, can't restore
  419.      ;
  420.      lea.l      level5(pc),a1               ; get our vector ptr
  421.      cmpa.l   LVL5VEC(a0),a1              ; do they match?
  422.      bne      10$                      ; nope, can't restore
  423.      ;
  424.      move.l   vb_OldLevel1(a5),LVL1VEC(a0)          ; restore original vector
  425.      move.l   vb_OldLevel5(a5),LVL5VEC(a0)          ; restore original vector
  426.      ;
  427.      moveq      #INTB_PORTS,d0              ; get interrupt number
  428.      lea.l      VBInterrupt(pc),a1              ; get interrupt ptr
  429.      jsr      _LVORemIntServer(a6)              ; remove it from the list
  430.      ;
  431.      lea.l      _custom,a0                  ; get custom base
  432.      move.w   #INTF_RBF|INTF_TBE,d0           ; indicate serial interrupts
  433.      move.w   d0,_intena(a0)              ; disable interrupts
  434.      move.w   d0,_intreq(a0)              ; clear pending interrupts
  435.      ;
  436.      bsr      freeBuf                  ; free allocated buffers
  437.      ;
  438.      lea.l      _ciab,a0                  ; get pointer to ciab
  439.      move.b   ciaddra(a0),d0              ; get DDR value
  440.      andi.b   #PRTMASK,d0                  ; save printer bits
  441.      ori.b      #~PRTMASK,d0                  ; set serial bits to output
  442.      move.b   d0,ciaddra(a0)              ; store value
  443.      ;
  444.      move.b   ciapra(a0),d0               ; get PR value
  445.      andi.b   #PRTMASK,d0                  ; mask out serial bits
  446.      move.b   vb_SavePRA(a5),d1              ; get saved value
  447.      andi.b   #~PRTMASK,d1                  ; mask out printer bits
  448.      or.b      d1,d0                   ; combine the two
  449.      move.b   d0,ciapra(a0)               ; and store
  450.      ;
  451.      move.b   ciaddra(a0),d0              ; get DDR value
  452.      andi.b   #PRTMASK,d0                  ; mask out serial bits
  453.      move.b   vb_SaveDDRA(a5),d1              ; get saved value
  454.      andi.b   #~PRTMASK,d1                  ; mask out printer bits
  455.      or.b      d1,d0                   ; combine the two
  456.      move.b   d0,ciaddra(a0)              ; and store
  457.      ;
  458.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  459.      jsr      _LVOCloseDevice(a6)              ; go close it
  460.      ;
  461.      movea.l  vb_MiscBase(a5),a6              ; get MiscBase
  462.      moveq      #MR_SERIALBITS,d0              ; set unit
  463.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  464.      ;
  465.      moveq      #MR_SERIALPORT,d0              ; set unit
  466.      jsr      _LVOFreeMiscResource(a6)          ; release the resource
  467.      movea.l  SysBase(pc),a6              ; restore ExecBase
  468.      ;
  469.      ENABLE                       ; enable interrupts
  470.      ;
  471.      subq.b   #1,vb_Initialized(a5)           ; clear flag
  472.      moveq      #0,d0                   ; free up everything
  473.      ;
  474. 10$     movea.l  (sp)+,a1                  ; restore registers
  475.      rts                          ; return
  476.      ;
  477.      ;      Device Close routine
  478.      ;
  479.      cnop 0,4 ; align for 020+
  480. dev_Close:
  481.      ;
  482.      moveq      #-1,d0                  ; invalidate
  483.      move.l   d0,IO_DEVICE(a1)              ;   device
  484.      ;
  485.      subq.w   #1,LIB_OPENCNT(a6)              ; decr open count
  486.      bne.b      dev_Null                  ; still open? yep, branch
  487.      ;
  488.      move.l   a5,-(sp)                  ; save registers
  489.      movea.l  a6,a5                   ; save base
  490.      movea.l  SysBase(pc),a6              ; get ExecBase
  491.      ;
  492.      bsr      freeResources               ; free allocated resources
  493.      ;
  494.      movea.l  a5,a6                   ; restore base
  495.      movea.l  (sp)+,a5                  ; restore registers
  496.      ;
  497.      tst.l      d0                      ; freed?
  498.      bne.b      dev_Null                  ; nope, can't expunge, exit
  499.      ;
  500.      clr.b      vb_SerFlags(a6)              ; clear flags
  501.      ;
  502.      btst.b   #LIBB_DELEXP,LIB_FLAGS(a6)          ; delayed expunge set?
  503.      beq.b      dev_Null                  ; nope, go exit
  504.      bra.b      dev_Expunge
  505.      ;
  506.      ;      Device Expunge routine (also fall through from dev_Close)
  507.      ;
  508.      cnop 0,4 ; align for 020+
  509. dev_Expunge:
  510.      ori.w      #1<<LIBB_DELEXP,LIB_FLAGS(a6)       ; Set expunge flag
  511.      tst.w      LIB_OPENCNT(a6)              ; currently open?
  512.      bne.b      dev_Null                  ; yep, so just exit
  513.      ;
  514.      move.l   vb_SegList(a6),d0              ; get seglist ptr
  515.      movem.l  d0/a5/a6,-(sp)              ; save registers (save D0!)
  516.      ;
  517.      movea.l  a6,a5                   ; save base
  518.      movea.l  SysBase(pc),a6              ; get ExecBase
  519.      movea.l  a5,a1                   ; get base
  520.      jsr      _LVORemove(a6)              ; Remove it
  521.      ;
  522.      movea.l  a5,a1                   ; get base
  523.      moveq      #0,d0                   ; clear work
  524.      move.w   LIB_NEGSIZE(a5),d0              ; calculate
  525.      suba.w   d0,a1                   ;   memory address
  526.      add.w      LIB_POSSIZE(a5),d0              ;     and size
  527.      jsr      _LVOFreeMem(a6)              ; free it
  528.      ;
  529.      movem.l  (sp)+,d0/a5/a6              ; restore registers
  530.      rts                          ; return (seglist in D0!)
  531.      ;
  532.      ;      Device "ExtFunc" routine
  533.      ;
  534.      cnop 0,4 ; align for 020+
  535. dev_Null:
  536.      moveq      #0,d0                   ; set return code
  537.      rts                          ; return
  538.      ;
  539.      ;
  540.      cnop 0,4 ; align for 020+
  541. cmdTable dc.w      cmd_Invalid-cmdTable              ; CMD_INVALID
  542.      dc.w      cmd_Reset-cmdTable              ; CMD_RESET
  543.      dc.w      cmd_Read-cmdTable              ; CMD_READ
  544.      dc.w      cmd_Write-cmdTable              ; CMD_WRITE
  545.      dc.w      cmd_Invalid-cmdTable              ; CMD_UPDATE
  546.      dc.w      cmd_Clear-cmdTable              ; CMD_CLEAR
  547.      dc.w      cmd_Invalid-cmdTable              ; CMD_STOP
  548.      dc.w      cmd_Invalid-cmdTable              ; CMD_START
  549.      dc.w      cmd_Flush-cmdTable              ; CMD_FLUSH
  550.      dc.w      sdcmd_Query-cmdTable              ; SDCMD_QUERY
  551.      dc.w      sdcmd_Break-cmdTable              ; SDCMD_BREAK
  552. endTable dc.w      sdcmd_SetParams-cmdTable          ; SDCMD_SETPARAMS
  553.      ;
  554.      ;      Device BeginIO routine
  555.      ;
  556.      cnop 0,4 ; align for 020+
  557. dev_BeginIO:
  558.      move.l   a5,-(sp)                  ; save register
  559.      movea.l  a6,a5                   ; save base
  560.      movea.l  SysBase(pc),a6              ; get ExecBase
  561.      ;
  562.      move.b   #NT_MESSAGE,LN_TYPE(a1)          ; set type
  563.      clr.b      IO_ERROR(a1)                  ; clear error
  564.      ;
  565.      andi.b   #~(IOSERF_QUEUED|IOSERF_ACTIVE),IO_FLAGS(a1) ; clear flags
  566.      ;
  567.      move.w   IO_COMMAND(a1),d0              ; get command
  568.      add.w      d0,d0                   ; multiply by 2
  569.      cmpi.w   #endTable-cmdTable,d0           ; in range?
  570.      bhi.b      30$                      ; nope, error
  571.      ;
  572.      move.w   cmdTable(pc,d0.w),d0              ; get routine offset
  573.      jsr      cmdTable(pc,d0.w)              ; go do it
  574.      tst.l      d0                      ; I/O completed?
  575.      bne.b      19$                      ; nope, go return
  576.      ;
  577. 10$
  578.      btst.b   #IOB_QUICK,IO_FLAGS(a1)          ; need to reply?
  579.      bne.b      20$                      ; nope, branch
  580. 15$     jsr      _LVOReplyMsg(a6)              ; send it back
  581.      ;
  582. 19$     andi.b   #~(1<<IOB_QUICK),IO_FLAGS(a1)       ; clear quick bit
  583. 20$     movea.l  a5,a6                   ; restore base
  584.      movea.l  (sp)+,a5                  ; restore register
  585.      rts                          ; return
  586.      ;
  587. 30$     move.b   #IOERR_NOCMD,IO_ERROR(a1)          ; invalid command
  588.      bra.b      10$                      ; branch
  589.      ;
  590.      ;      Device AbortIO routine
  591.      ;
  592.      cnop 0,4 ; align for 020+
  593. dev_AbortIO:
  594.      move.l   a5,-(sp)                  ; save registers
  595.      movea.l  a6,a5                   ; save base
  596.      movea.l  SysBase(pc),a6              ; get ExecBase
  597.      ;
  598.      DISABLE                      ; disable interrupts
  599.      ;
  600.      move.b   IO_FLAGS(a1),d1              ; get flags
  601.      btst.b   #IOSERB_QUEUED,d1              ; queued request?
  602.      bne.b      40$                      ; yep, branch
  603.      ;
  604.      btst.b   #IOSERB_ACTIVE,d1              ; active request?
  605.      bne.b      10$                      ; nope, just exit
  606.      ;
  607.      move.w   IO_COMMAND(a1),d0              ; get command
  608.      subq.w   #CMD_READ,d0                  ; was it a read?
  609.      beq.b      20$                      ; yep, go process
  610.      ;
  611.      subq.w   #CMD_WRITE-CMD_READ,d0          ; was it a write?
  612.      beq.b      30$                      ; yep, go process
  613.      ;
  614.      subq.w   #SDCMD_BREAK-CMD_WRITE,d0          ; was it a break?
  615.      beq.b      30$                      ; yep, go process
  616.      ;
  617.      ;      Fall through or enter from below
  618.      ;
  619. 10$     ENABLE                       ; enable ints and return
  620. 15$     movea.l  a5,a6                   ; restore base
  621.      movea.l  (sp)+,a5                  ; restore registers
  622.      rts                          ; return
  623.      ;
  624.      ;      Abort an active read request
  625.      ;
  626. 20$     clr.l      cr_IOReq                  ; no longer active
  627.      bra.b      50$                      ; go set flags
  628.      ;
  629.      ;      Abort an active write request
  630.      ;
  631.      cnop 0,4 ; align for 020+
  632. 30$     clr.l      cw_Length                  ; no longer active
  633.      clr.l      cw_IOReq                  ; no longer active
  634.      move.l   cw_Buffer(pc),d0              ; get buffer ptr
  635.      sub.l      IO_DATA(a1),d0              ; calc number of bytes xfer'd
  636.      move.l   d0,IO_ACTUAL(a1)              ; store
  637.      ;
  638.      ;      Force a TBE interrupt to get the next write going.
  639.      ;
  640.      move.w   #INTF_SETCLR|INTF_TBE,_intreq       ; make TBE pending
  641.      bra.b      50$                      ; go set flags
  642.      ;
  643.      ;      Remove I/O from queue.
  644.      ;
  645.      cnop 0,4 ; align for 020+
  646. 40$     move.l   a1,-(sp)                  ; save ptr
  647.      jsr      _LVORemove(a6)              ; remove it
  648.      movea.l  (sp)+,a1                  ; restore ptr
  649.      ;
  650.      ;      Set error and return I/O
  651.      ;
  652. 50$     move.b   #IOERR_ABORTED,IO_ERROR(a1)          ; set error code
  653.      move.b   IO_FLAGS(a1),d1              ; get flags
  654.      ori.b      #1<<IOSERB_ABORT,d1              ; set abort flag
  655.      andi.b   #~(IOSERF_QUEUED|IOSERF_ACTIVE),d1  ; clear flags
  656.      move.b   d1,IO_FLAGS(a1)              ; store flags
  657.      btst.b   #IOB_QUICK,d1               ; need to reply?
  658.      bne.b      10$                      ; nope, branch
  659.      jsr      _LVOReplyMsg(a6)              ; send it back
  660.      bra.b      10$                      ; branch to return
  661.      ;
  662.      ;      Abort all active/queued commands and reset internal state
  663.      ;
  664.      cnop 0,4 ; align for 020+
  665. cmd_Reset:
  666.      move.l   a1,-(sp)                  ; save I/O request
  667.      bsr.b      cmd_Flush                  ; go abort queued requests
  668.      ;
  669.      DISABLE                      ; disable interrupts
  670.      ;
  671.      ;      This must follow the DISABLE
  672.      ;
  673.      exg      a5,a6                   ; exchange base and ExecBase
  674.      ;
  675.      move.l   cr_IOReq(pc),d0              ; active read?
  676.      beq.b      10$                      ; nope, branch
  677.      movea.l  d0,a1                   ; get I/O request
  678.      bsr      dev_AbortIO                  ; go abort it
  679.      ;
  680.      ;
  681.      ;
  682. 10$     move.l   cw_IOReq(pc),d0              ; active write?
  683.      beq.b      20$                      ; nope, branch
  684.      movea.l  d0,a1                   ; get I/O request
  685.      bsr      dev_AbortIO                  ; go abort it
  686.      ;
  687. 20$     exg      a5,a6                   ; restore base and ExecBase
  688.      movea.l  (sp)+,a1                  ; restore I/O request
  689.      moveq      #8,d0                   ; get char size
  690.      move.b   d0,IO_READLEN(a1)              ; set read length
  691.      move.b   d0,IO_WRITELEN(a1)              ; set write length
  692.      moveq      #1,d0                   ; get stop bits
  693.      move.b   d0,IO_STOPBITS(a1)              ; set stop bits
  694.      move.l   vb_DefBaud(a5),IO_BAUD(a1)          ; set to default baud
  695.      move.l   vb_DefRBufLen(a5),IO_RBUFLEN(a1)    ; set to default buflen
  696.      bsr      internalReset               ; go set/verify parameters
  697.      move.b   d0,IO_ERROR(a1)              ; set error code
  698.      ENABLE                       ; enable interrupts
  699.      ;
  700.      ;      Set RC and return
  701.      ;
  702.      moveq      #0,d0                   ; I/O complete
  703.      rts                          ; return
  704.      ;
  705.      ;      Abort all "queued" requests, leaving all active alone.
  706.      ;
  707.      cnop 0,4 ; align for 020+
  708. cmd_Flush:
  709.      movem.l  a1/a2,-(sp)                  ; save registers
  710.      DISABLE                      ; disable interrupts
  711.      ;
  712.      ;      Abort all queued read requests.
  713.      ;
  714.      lea.l      readQ(pc),a2                  ; get ptr to read queue
  715.      bsr.b      20$                      ; branch to abort
  716.      ;
  717.      ;      Abort all queued write requests.
  718.      ;
  719.      lea.l      writeQ(pc),a2               ; get ptr to write queue
  720.      bsr.b      20$                      ; branch to abort
  721.      ;
  722.      ;      Enable, restore, and return to caller
  723.      ;
  724.      ENABLE                       ; enable interrupts
  725.      movem.l  (sp)+,a1/a2                  ; restore registers
  726.      ;
  727.      ;      Set RC and return
  728.      ;
  729.      moveq      #0,d0                   ; I/O complete
  730.      ;
  731.      ;      !!!NOTE!!!  In a mad attempt to save 2 bytes, this RTS is used
  732.      ;      by the subroutine below.  Why waste 'em?  B-)
  733.      ;
  734. 10$     rts                          ; return ( used below too!! )
  735.      ;
  736.      ;      Subroutine to remove and reply each I/O request.
  737.      ;
  738.      cnop 0,4 ; align for 020+
  739. 20$     movea.l  a2,a0                   ; get list ptr
  740.      jsr      _LVORemHead(a6)              ; get I/O request
  741.      tst.l      d0                      ; end of list?
  742.      beq.b      10$                      ; yep, branch to return
  743.      ;
  744.      movea.l  d0,a1                   ; get I/O request
  745.      andi.b   #~(1<<IOSERB_QUEUED),IO_FLAGS(a1)   ; no longer queued
  746.      moveq      #IOERR_ABORTED,d0              ; indicate aborted
  747.      move.b   d0,IO_ERROR(a1)              ; store status
  748.      ;
  749.      jsr      _LVOReplyMsg(a6)              ; send it back
  750.      bra.b      20$                      ; continue with next
  751.      ;
  752.      ;      Process a CMD_READ request.
  753.      ;
  754.      cnop 0,4 ; align for 020+
  755. cmd_Read:
  756.      ;
  757.      ;      Zero length requests just get returned.
  758.      ;
  759.      clr.l      IO_ACTUAL(a1)               ; clear bytes read
  760.      move.l   IO_LENGTH(a1),d0              ; get length and test
  761.      beq.b      20$                      ; yep, leave
  762.      ;
  763.      ;      This can be used to circumvent a bug in NComm 3.0 which
  764.      ;      references the buffer even when there was nothing read.
  765.      ;
  766.     IFNE     NCOMM
  767.      move.l   IO_DATA(A1),a0              ; get data pointer
  768.      clr.b      (a0)                      ; clear first byte in buffer
  769.     ENDC
  770.      ;
  771.      ;      The disable counter works just like exec's TDNestCnt field.  It's
  772.      ;      initialized to -1.  After incrementing, if it is 0, then we
  773.      ;      can attempt to process this request immediately.  If it's > 0,
  774.      ;      then we're already disabled and we must queue this request.
  775.      ;
  776.      addq.b   #1,disableRead              ; incr disable count
  777.      bgt.b      50$                      ; >0, already disabled
  778.      ;
  779.      ;      If we're already processing an request, this one has to wait
  780.      ;      until that one is done, so go queue it.
  781.      ;
  782.      move.l   cr_IOReq(pc),d1              ; have an active request?
  783.      bne.b      50$                      ; yep, go queue this one
  784.      ;
  785.      ;      If we don't have enough bytes to satisfy this request then go
  786.      ;      queue it.
  787.      ;
  788.      cmp.l      i_InCnt(pc),d0              ; length > current bytes?
  789.      bgt.b      50$                      ; yep, go queue it
  790.      ;
  791.      ;      Setup fields and go copy the data
  792.      ;
  793.      move.l   IO_DATA(a1),cr_OutPtr           ; get/set output ptr
  794.      move.l   IO_LENGTH(a1),cr_Length          ; get/set output count
  795.      bsr      copyData                  ; go copy 'em
  796.      moveq      #0,d0                   ; I/O complete
  797.      ;
  798.      ;      We're done, so back off the disable counter.
  799.      ;
  800. 10$     subq.b   #1,disableRead              ; decr disable count
  801.      ;
  802.      ;      Return to caller
  803.      ;
  804. 20$     rts                          ; return
  805.      ;
  806.      ;      Just set flags and queue.  The read interrupt will handle it.
  807.      ;
  808. 50$     ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)    ; indicate queued
  809.      ;
  810.      ;      Add this request to the end.
  811.      ;
  812.      DISABLE                      ; disable interrupts
  813.      lea.l      readQ(pc),a0                  ; get pointer to read queue
  814.      move.l   a1,-(sp)                  ; save I/O request
  815.      jsr      _LVOAddTail(a6)              ; and queue it
  816.      movea.l  (sp)+,a1                  ; restore I/O request
  817.      ENABLE                       ; enable interrupts
  818.      ;
  819.      ;      Indicate that this request was not handled immediatly.
  820.      ;
  821.      moveq      #1,d0                   ; I/O not complete
  822.      bra.b      10$                      ; branch to return
  823.      ;
  824.      ;      Process a CMD_WRITE request.
  825.      ;
  826.      cnop 0,4 ; align for 020+
  827. cmd_Write:
  828.      clr.l      IO_ACTUAL(a1)               ; clear bytes written
  829.      move.l   IO_LENGTH(a1),d0              ; get length and test
  830.      beq.b      wbexit                  ; yep, leave
  831.      ;
  832.      ;      Entry point for Break command and fall through from cmd_Write.
  833.      ;
  834. sdcmd_Break:
  835.      ;
  836.      ;      Just set flags and queue. The TBE interrupt will handle it.
  837.      ;
  838.      ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)     ; indicate queued
  839.      ;
  840.      ;      Protect.
  841.      ;
  842.      DISABLE                      ; disable interrupts
  843.      ;
  844.      ;      Add request to end of queue.
  845.      ;
  846.      lea.l      writeQ(pc),a0               ; get queue list ptr
  847.      move.l   a1,-(sp)                  ; save I/O request
  848.      jsr      _LVOAddTail(a6)              ; and queue it
  849.      movea.l  (sp)+,a1                  ; restore I/O request
  850.      ;
  851.      ;      If we have an active request, don't force interrupt.
  852.      ;
  853.      move.l   cw_IOReq(pc),d0              ; have an active request?
  854.      bne.b      10$                      ; yep, branch
  855.      ;
  856.      ;      Force a TBE interrupt to get the writes going.
  857.      ;
  858.      move.w   #INTF_SETCLR|INTF_TBE,_intreq       ; make TBE pending
  859.      ;
  860.      ;      Enable, set RC and return to caller.
  861.      ;
  862. 10$     ENABLE                       ; enable interrupts
  863.      moveq      #1,d0                   ; I/O not complete
  864. wbexit     rts                          ; return
  865.      ;
  866.      ;      Resets serial read buffer
  867.      ;
  868.      ;      Since this routine is called internally, it must NOT reference
  869.      ;      the I/O request.
  870.      ;
  871.      cnop 0,4 ; align for 020+
  872. cmd_Clear:
  873.      DISABLE                      ; disable interrupts
  874.      ;
  875.      ;      Load registers
  876.      ;
  877.      move.l   vb_CurRBuf(a5),d0              ; get internal buffer ptr
  878.      move.l   vb_CurRBufLen(a5),d1              ; and internal buffer len
  879.      lea.l      i_BufPtr(pc),a0              ; get ptr internal control
  880.      ;
  881.      ;      Initialize global buffer variables
  882.      ;
  883.      move.l   d0,(a0)                  ; store buffer ptr
  884.      move.l   d0,4(a0)                  ; set current input ptr
  885.      move.l   d0,8(a0)                  ; set current output ptr
  886.      add.l      d1,d0                   ; add buffer length
  887.      move.l   d0,12(a0)                  ; store ptr to end of buffer
  888.      clr.l      16(a0)                  ; clear byte cnt
  889.      move.l   vb_CurBaud(a5),d0              ; get internal baud
  890.      lsr.l      #4,d0                   ; divide by 16
  891.      sub.l      d0,d1                   ; subtract from length
  892.      move.l   d1,20(a0)                  ; set threshold
  893.      ;
  894.      ;      Enable, set RC and return.
  895.      ;
  896.      ENABLE                       ; enable interrupts
  897.      moveq      #0,d0                   ; I/O complete
  898.      rts                          ; return
  899.      ;
  900.      ;
  901.      ;
  902.      cnop 0,4 ; align for 020+
  903. cmd_Invalid
  904.      move.b   #IOERR_NOCMD,IO_ERROR(a1)          ; set bad status
  905.      moveq      #0,d0                   ; I/O complete
  906.      rts                          ; return
  907.      ;
  908.      ;      Returns number of bytes currently in internal buffer and
  909.      ;      current serial port status.
  910.      ;
  911.      ;      NOTE:  Not completely compatible with standard serial.device
  912.      ;      since it doesn't return the upper byte of IO_STATUS.
  913.      ;
  914.      cnop 0,4 ; align for 020+
  915. sdcmd_Query:
  916.      DISABLE                      ; disable interrupts
  917.      moveq      #0,d0                   ; clear d0
  918.      move.b   _ciabpra,d0                  ; get PR register
  919.      andi.w   #~PRTMASK,d0                  ; zap printer bits
  920.      ;
  921.      ;      To use this Set SETDSR to one.  This was done
  922.      ;      for a user whose DSR pin did not function.
  923.      ;
  924.     IFNE SETDSR
  925.      andi        #~(1<<CIAB_COMDSR),d0              ; set DSR
  926.     ENDC
  927.      ;
  928.      ;
  929.      ;
  930.      move.w   d0,IO_STATUS(a1)              ; store status
  931.      move.l   i_InCnt(pc),IO_ACTUAL(a1)          ; byte left in buffer
  932.      ;
  933.      ;      Enable, set RC and return.
  934.      ;
  935.      ENABLE                       ; enable interrupts
  936.      moveq      #0,d0                   ; I/O complete
  937.      rts                          ; return
  938.      ;
  939.      ;
  940.      ;
  941.      cnop 0,4 ; align for 020+
  942. sdcmd_SetParams:
  943.      ;
  944.      ;      Validate the read, write, and stop bit lengths.
  945.      ;
  946.      moveq      #8,d0                   ; get char length
  947.      cmp.b      IO_READLEN(a1),d0              ; 8 bit chars for read?
  948.      bne.b      40$                      ; nope, branch
  949.      cmp.b      IO_WRITELEN(a1),d0              ; 8 bit chars for write?
  950.      bne.b      40$                      ; nope, branch
  951.      moveq      #1,d0                   ; get stop bits
  952.      cmp.b      IO_STOPBITS(a1),d0              ; 1 stop bit?
  953.      bne.b      40$                      ; nope, branch
  954.      ;
  955.      ;      Get and validate the baud rate.
  956.      ;
  957.      move.l   IO_BAUD(a1),d1              ; get baud from I/O req
  958.      bne.b      20$                      ; specified?
  959.      move.l   vb_CurBaud(a5),d1              ; get current baud from base
  960.      bne.b      20$                      ; specified?
  961.      move.l   vb_DefBaud(a5),d1              ; get default baud from base
  962. 20$     cmpi.l   #110,d1                  ; too low?
  963.      blt.b      40$                      ; error
  964.      cmpi.l   #292000,d1                  ; too high?
  965.      bgt.b      40$                      ; error
  966.      ;
  967.      ;      Get and validate the buffer length.
  968.      ;
  969.      move.l   IO_RBUFLEN(a1),d0              ; get buffer length
  970.      bne.b      30$                      ; specified?
  971.      move.l   vb_CurRBufLen(a5),d0              ; get current from base
  972.      bne.b      30$                      ; specified?
  973.      move.l   vb_DefRBufLen(a5),d0              ; get default from base
  974.      ;
  975. 30$     bsr.b      internalReset               ; go init baud and buffer
  976. 35$     move.b   d0,IO_ERROR(a1)              ; set error code
  977.      bne.b      39$
  978.      ;
  979.      ;      If the 7wire bit is not on, we will only use 3-wire protocol
  980.      ;
  981.      moveq      #0,d0                   ; clear flag
  982.      btst.b   #SERB_7WIRE,IO_SERFLAGS(a1)          ; use 7wire handshaking?
  983.      beq.b      36$                      ; nope, branch
  984.      moveq      #1,d0                   ; set flag
  985. 36$     move.b   d0,Handshake                  ; store flag
  986.      ;
  987.      ;      Set RC and return.
  988.      ;
  989. 39$     moveq      #0,d0                   ; I/O complete
  990.      rts                          ; return
  991.      ;
  992.      ;      Invalid parm detected.
  993.      ;
  994. 40$     moveq      #SerErr_InvParam,d0              ; set error
  995.      bra.b      35$                      ; go return
  996.      ;
  997.      ;      Reset the buffer and baud rate
  998.      ;
  999.      ;      Registers:  D0 = Buffer length
  1000.      ;              D1 = Baud rate
  1001.      ;
  1002.      cnop 0,4 ; align for 020+
  1003. internalReset:
  1004.      ;
  1005.      ;      Disable interrupts.
  1006.      ;
  1007.      DISABLE                      ; disable interrupts
  1008.      ;
  1009.      ;      Save buffer length and go set serper.
  1010.      ;
  1011.      move.l   d0,-(sp)                  ; save D0
  1012.      move.l   d1,d0                   ; get baud rate
  1013.      bsr.b      setPeriod                  ; go set the serper register
  1014.      move.l   (sp)+,d0                  ; restore D0
  1015.      ;
  1016.      ;      Determine if the buffer length is adequate for the selected CPS.
  1017.      ;      If not, use 64K for the length.
  1018.      ;
  1019.      move.l   vb_CurBaud(a5),d1              ; get current baud
  1020.      lsr.l      #$3,d1                  ; divide by 8
  1021.      cmp.l      d1,d0                   ; buflen > CPS
  1022.      bhi.b      10$                      ; yep, branch
  1023.      move.l   #65536,d0                  ; else use 64K
  1024.      ;
  1025. 10$     bsr.b      allocBuf                  ; go allocate a new buffer
  1026.      ;
  1027.      ;      Enable and return to caller.
  1028.      ;
  1029.      ENABLE                       ; enable interrupts
  1030.      rts                          ; return (D0 has status)
  1031.      ;
  1032.      ;      Set serial period register
  1033.      ;
  1034.      cnop 0,4 ; align for 020+
  1035. setPeriod:
  1036.      cmp.l      vb_CurBaud(a5),d0              ; current baud = new baud?
  1037.      beq.b      40$                      ; yep, just exit
  1038.      move.l   d0,vb_CurBaud(a5)              ; save new baud
  1039.      move.l   d0,d1                   ; save again
  1040.      lsl.l      #3,d0                   ; baud *= 8
  1041.      sub.l      d1,d0                   ; baud -= saved baud
  1042.      move.l   #25000000,d1                  ; get NTSC base
  1043.      cmpi.b   #50,PowerSupplyFrequency(a6)          ; PAL machine?
  1044.      bne.b      10$                      ; nope, branch
  1045.      move.l   #24772416,d1                  ; get PAL base
  1046. 10$     cmpi.l   #$FFFF,d0                  ; Divide
  1047.      ble.b      20$                      ;
  1048.      lsr.l      #5,d0                   ;
  1049.      divu.w   d0,d1                   ;
  1050.      andi.l   #$FFFF,d1                  ;
  1051.      lsr.l      #5,d1                   ;
  1052.      bra.b      30$                      ;
  1053.      cnop 0,4 ; align for 020+
  1054. 20$     divu.w   d0,d1                   ;
  1055. 30$     move.w   d1,_serper                  ; set period value
  1056. 40$     rts
  1057.      ;
  1058.      ;      Allocate new internal buffer
  1059.      ;
  1060.      cnop 0,4 ; align for 020+
  1061. allocBuf:
  1062.      cmp.l      vb_CurRBufLen(a5),d0              ; len same as previous?
  1063.      beq.b      10$                      ; yep, so no need to alloc
  1064.      move.l   d0,d1                   ; save length
  1065.      movem.l  d1/a1,-(sp)                  ; save registers
  1066.      moveq      #MEMF_PUBLIC,d1              ; public memory
  1067.      jsr      _LVOAllocMem(a6)              ; go allocate it
  1068.      movem.l  (sp)+,d1/a1                  ; restore registers
  1069.      tst.l      d0                      ; did we get it?
  1070.      beq.b      20$                      ; if zero, error
  1071.      bsr.b      freeBuf                  ; go free previous buffer
  1072.      move.l   d0,vb_CurRBuf(a5)              ; store new ptr
  1073.      move.l   d1,vb_CurRBufLen(a5)              ; and length
  1074.      bsr      cmd_Clear                  ; go setup buffer
  1075. 10$     moveq      #0,d0                   ; success
  1076.      rts                          ; return
  1077. 20$     moveq      #SerErr_BufErr,d0              ; set error status
  1078.      rts                          ; return
  1079.      ;
  1080.      ;      Free internal buffer
  1081.      ;
  1082.      cnop 0,4 ; align for 020+
  1083. freeBuf:
  1084.      movem.l  d0-d1/a0-a1,-(sp)              ; save registers
  1085.      move.l   vb_CurRBuf(a5),d0              ; is one there?
  1086.      beq.b      10$                      ; no so branch
  1087.      movea.l  d0,a1                   ; get ptr
  1088.      move.l   vb_CurRBufLen(a5),d0              ; get length
  1089.      clr.l      vb_CurRBuf(a5)              ; clear
  1090.      clr.l      vb_CurRBufLen(a5)              ; clear
  1091.      jsr      _LVOFreeMem(a6)              ; free it
  1092. 10$     movem.l  (sp)+,d0-d1/a0-a1              ; restore registers
  1093.      rts                          ; return
  1094.      ;
  1095.      ;      Checks CTS status and if clear generates a TBE interrupt or
  1096.      ;      requeues the timer request.
  1097.      ;
  1098.      ;      Entered from Exec using the MsgPort callback.
  1099.      ;
  1100.      ;      Input:   a6 = ExecBase
  1101.      ;      Output:  none
  1102.      ;
  1103.      ;      No need to preserve d0/d1/a0/a1
  1104.      ;
  1105.      cnop 0,4 ; align for 020+
  1106. timerRtn:
  1107.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1108.      jsr      _LVORemove(a6)              ; remove request
  1109.      ;
  1110.      ;      If we were breaking, reset adkcon.
  1111.      ;
  1112.      lea      _custom,a0                  ; get ptr to custom regs
  1113.      btst      #ADKB_UARTBRK,_adkconr(a0)          ; were we breaking?
  1114.      beq.b      10$                      ; nope, skip reset
  1115.      ;
  1116.      move.w   #ADKF_UARTBRK,_adkcon(a0)          ; stop breaking
  1117.      ;
  1118.      ;
  1119.      ;
  1120. 10$     move.b   Handshake(pc),d0              ; are we handshaking?
  1121.      beq.b      20$                      ; nope, generate interrupt
  1122.      btst      #CIAB_COMCTS,_ciabpra(a0)          ; clear to send?
  1123.      beq.b      20$                      ; yep, go start writing
  1124.      ;
  1125.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1126.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1127.      jsr      _LVOSendIO(a6)              ; go queue it
  1128.      bra.b      30$                      ; branch to return
  1129.      ;
  1130.      ;      CTS is clear so generate TBE interrupt to restart writing
  1131.      ;
  1132.      cnop 0,4 ; align for 020+
  1133. 20$     move.w   #INTF_SETCLR|INTF_TBE,_intreq(a0)   ; set TBE interrupt
  1134. 30$     rts                          ; return
  1135.      ;
  1136.      ;      Non serial interrupt
  1137.      ;
  1138.      ;  a0 - custom chips base
  1139.      ;  a1 - is_Data
  1140.      cnop 0,4 ; align for 020+
  1141. level1n:
  1142.      movem.l  d0-d1/a0-a1/a5-a6,-(sp)          ; save registers
  1143.      lea      _custom,a0                  ; get ptr to custom regs
  1144.      move.w   intenar(a0),d1              ; get enabled interrupts
  1145.      and.w      intreqr(a0),d1              ; and in requested interrupts
  1146.      movea.l  SysBase(pc),a6              ; get ExecBase
  1147.      ;
  1148.      btst      #INTB_DSKBLK,d1              ; Disk block done?
  1149.      beq.b      10$                      ; nope, branch
  1150.      ;
  1151.      movem.l  IVDSKBLK(a6),a1/a5              ; get data and code ptrs
  1152.      pea.l      20$(pc)                  ; push return address
  1153.      jmp      (a5)                      ; jump to routine
  1154.      ;
  1155. 10$     btst      #INTB_SOFTINT,d1              ; software interrupt?
  1156.      beq.b      20$                      ; nope, branch
  1157.      ;
  1158.      movem.l  IVSOFTINT(a6),a1/a5              ; get data and code ptrs
  1159.      pea.l      20$(pc)                  ; push return address
  1160.      jmp      (a5)                      ; jump to routine
  1161.      ;
  1162. 20$     movem.l  (sp)+,d0-d1/a0-a1/a5-a6          ; restore registers
  1163.      rte                          ; return
  1164.      ;
  1165.      ;      Level 1 interrupt handler
  1166.      ;
  1167.      ;  a1 - is_Data
  1168.      ;
  1169.      cnop 0,4 ; align for 020+
  1170. level1:
  1171.      btst      #INTB_INTEN,_intenar              ; interrupts enabled?
  1172.      beq.b      35$                      ; nope, ignore
  1173.      btst      #INTB_SOFTINT,_intreqr+1          ; software interrupt?
  1174.      bne.b      level1n                  ; nope, branch
  1175.      btst      #INTB_TBE,_intreqr+1              ; xmit buffer empty?
  1176.      beq.b      level1n                  ; nope, invoke old handler
  1177.      ;
  1178.      ;      Handle "Transmit Buffer Empty" interrupt (write)
  1179.      ;
  1180.      move.w   #INTF_TBE,_intreq              ; clear interrupt
  1181.      move.l   d0,-(sp)                  ; save D0 (faster than MOVEM)
  1182.      move.l   a0,-(sp)                  ; save A0 (faster than MOVEM)
  1183.      ;
  1184.      ;      If we're not handshaking, bypass it.
  1185.      ;
  1186. 10$     move.b   Handshake(pc),d0              ; are we handshaking?
  1187.      beq.b      20$                      ; nope, skip CTS test
  1188.      btst      #CIAB_COMCTS,_ciabpra           ; clear to send?
  1189.      bne.b      40$                      ; nope, branch
  1190.      ;
  1191.      ;      If cw_Length goes negative here, we are either done with a
  1192.      ;      request or we were called as a result of a fake interrupt
  1193.      ;      to force us to get the next request going.
  1194.      ;
  1195. 20$     subq.l   #1,cw_Length                  ; decr write length
  1196.      blt.b      60$                      ; < zero, done, branch
  1197.      ;
  1198.      ;      Currently processing a request.
  1199.      ;
  1200.      movea.l  cw_Buffer(pc),a0              ; get buffer ptr
  1201.      move.w   #256,d0                  ; set stop bit
  1202.      move.b   (a0)+,d0                  ; get next byte
  1203.      move.l   a0,cw_Buffer                  ; store buffer ptr
  1204.      move.w   d0,_serdat                  ; store in serdat reg
  1205.      ;
  1206. 30$     movea.l  (sp)+,a0                  ; restore registers
  1207.      move.l   (sp)+,d0                  ; restore registers
  1208. 35$     rte                          ; return
  1209.      ;
  1210.      ;      Queue a timer request to recheck CTS status
  1211.      ;
  1212. 40$     movem.l  d1/a1/a6,-(sp)              ; save registers
  1213.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1214.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1215.      movea.l  SysBase(pc),a6              ; get ExecBase
  1216.      jsr      _LVOSendIO(a6)              ; queue the request
  1217.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1218.      bra.b      30$                      ; go return
  1219.      ;
  1220.      ;      There aren't anymore requests, so clear and exit
  1221.      ;
  1222.      cnop 0,4 ; align for 020+
  1223. 50$     clr.l      cw_Length-Start(a6)              ; clear length
  1224.      clr.l      cw_IOReq-Start(a6)              ; clear
  1225.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1226.      bra.b      30$                      ; go return
  1227.      ;
  1228.      ;      Write request completed
  1229.      ;
  1230.      cnop 0,4 ; align for 020+
  1231. 60$     movem.l  d1/a1/a6,-(sp)              ; save registers
  1232.      ;
  1233.      move.l   cw_IOReq(pc),d0              ; active I/O request?
  1234.      beq.b      70$                      ; nope, branch
  1235.      ;
  1236.      ;      Reply it and setup for next
  1237.      ;
  1238.      movea.l  d0,a1                   ; get I/O request
  1239.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1240.      clr.b      IO_ERROR(a1)                  ; no error
  1241.      movea.l  SysBase(pc),a6              ; get ExecBase
  1242.      jsr      _LVOReplyMsg(a6)              ; return I/O request
  1243.      ;
  1244. 70$     lea.l      Start(pc),a6                  ; get section base
  1245.      ;
  1246.      lea.l      writeQ(pc),a1               ; get ptr to write queue
  1247.      move.l   (a1),a0                  ; get head of list
  1248.      move.l   (a0),d0                  ; get successor
  1249.      beq.b      50$                      ; end of list? yep, branch
  1250.      ;
  1251.      ;      Remove the node from the list
  1252.      ;
  1253.      move.l   d0,(a1)                  ; make new head
  1254.      exg.l      d0,a0                   ; swap nodes
  1255.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1256.      ;
  1257.      move.l   d0,a1                   ; get I/O request
  1258.      ;
  1259.      ;      If it's a BREAK, then branch to process as such.
  1260.      ;
  1261.      cmpi.w   #SDCMD_BREAK,IO_COMMAND(a1)          ; BREAK command?
  1262.      beq.b      100$                      ; yep, branch
  1263.      ;
  1264.      ;      Check for absolute length
  1265.      ;
  1266.      move.l   IO_DATA(a1),a0              ; get data ptr
  1267.      moveq      #-1,d1                  ; get value
  1268.      move.l   IO_LENGTH(a1),d0              ; get length
  1269.      cmp.l      d1,d0                   ; length = -1?
  1270.      bne.b      90$                      ; nope, skip scan
  1271.      ;
  1272.      ;      Scan the data for null to calc the length
  1273.      ;
  1274.      move.l   a0,d1                   ; save data ptr
  1275.     IFNE NEWCODE
  1276.     ; Buffer size is multiple of 64bytes so we can safely move a long
  1277. 80$     move.l   (a0),d0                  ; move a long into d0
  1278.      tst.b      d0                      ; does it equal 0? (first byte)
  1279.      beq.b      84$                      ; yep, finish
  1280.      lsr.l      #8,d0                   ; shift too second byte
  1281.      tst.b      d0                      ; does it equal 0? (second byte)
  1282.      beq.b      83$                      ; yep, finish
  1283.      lsr.l      #8,d0                   ; shift too third byte
  1284.      tst.b      d0                      ; does it equal 0? (third byte)
  1285.      beq.b      82$                      ; yep, finish
  1286.      lsr.l      #8,d0                   ; shift too fourth byte
  1287.      tst.b      d0                      ; does it equal 0? (fourth byte)
  1288.      beq.b      81$                      ; yep, finish
  1289.      addq.l   #4,a0                   ; update pointer
  1290.      bra.b      80$                      ; loop
  1291.      cnop 0,4 ; align for 020+
  1292. 81$
  1293.      addq.l   #1,a0                   ; inc pointer
  1294. 82$
  1295.      addq.l   #1,a0                   ; inc pointer
  1296. 83$
  1297.      addq.l   #1,a0                   ; inc pointer
  1298. 84$
  1299.      addq.l   #1,a0                   ; inc pointer
  1300.      suba.l   d1,a0                   ; calc # of bytes
  1301.      move.l   a0,d0                   ; xfer
  1302.     ELSE
  1303. 80$     tst.b      (a0)+                   ; does it equal 0?
  1304.      bne.b      80$                      ; nope, so continue
  1305.      suba.l   d1,a0                   ; calc # of bytes+1
  1306.      move.l   a0,d0                   ; xfer
  1307.      subq.l   #1,d0                   ; calc # of bytes
  1308.     ENDC
  1309.      movea.l  d1,a0                   ; get data ptr
  1310.      ;
  1311. 90$     move.l   d0,IO_ACTUAL(a1)              ; go ahead and set it
  1312.      move.b   IO_FLAGS(a1),d1
  1313.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1314.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1315.      move.b   d1,IO_FLAGS(a1)
  1316.      move.l   d0,cw_Length-Start(a6)          ; store length
  1317.      move.l   a0,cw_Buffer-Start(a6)          ; store buffer ptr
  1318.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1319.      ;
  1320.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1321.      bra      10$                      ; go start request
  1322.      ;
  1323.      ;      Start the BREAK.
  1324.      ;
  1325.      cnop 0,4 ; align for 020+
  1326. 100$     move.b   IO_FLAGS(a1),d1
  1327.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1328.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1329.      move.b   d1,IO_FLAGS(a1)
  1330.      clr.l      cw_Length-Start(a6)              ; clear length
  1331.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1332.      move.w   #ADKF_SETCLR|ADKF_UARTBRK,_adkcon   ; start break
  1333.      move.l   IO_BRKTIME(a1),d0              ; get break time
  1334.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1335.      move.l   d0,IOTV_TIME+TV_MICRO(a1)          ; set the timeout
  1336.      movea.l  SysBase(pc),a6              ; get ExecBase
  1337.      jsr      _LVOSendIO(a6)              ; queue the request
  1338.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1339.      bra      30$                      ; go exit
  1340.      ;
  1341.      ;      Non serial interrupt
  1342.      ;
  1343.      ;  a0 - custom chips base
  1344.      ;  a1 - is_Data
  1345.      ;
  1346.      cnop 0,4 ; align for 020+
  1347. level5n:
  1348.      movem.l  d0-d1/a0-a1/a5-a6,-(sp)          ; save registers
  1349.      lea      _custom,a0                  ; get ptr to custom regs
  1350.      move.w   intenar(a0),d1              ; get enabled interrupts
  1351.      and.w      intreqr(a0),d1              ; and in requested interrupts
  1352.      movea.l  SysBase(pc),a6              ; get ExecBase
  1353.      btst      #INTB_DSKSYNC,d1              ; Disk synchronized?
  1354.      beq.b      10$                      ; nope, branch
  1355.      movem.l  IVDSKSYNC(A6),a1/a5              ; get data and code ptrs
  1356.      jsr      (a5)                      ; branch to routine
  1357. 10$     movem.l  (sp)+,d0-d1/a0-a1/a5-a6          ; restore registers
  1358.      rte                          ; return
  1359.      ;
  1360.      ;      Default Level 5 handler
  1361.      ;
  1362.      ;  a1 - is_Data
  1363.      ;
  1364.      cnop 0,4 ; align for 020+
  1365. level5:
  1366.      btst      #INTB_INTEN,_intenar              ; interrupts enabled?
  1367.      beq.b      45$                      ; nope, ignore
  1368.      btst      #INTB_RBF,_intreqr              ; receive buffer full?
  1369.      beq.b      level5n                  ; nope, invoke old handler
  1370.      ;
  1371.      move.l   a0,-(sp)                  ; save registers
  1372.      move.l   a1,-(sp)                  ; save registers
  1373.      lea      _custom,a1                  ; get ptr to custom regs
  1374.      ;
  1375. 10$     btst      #7,_serdatr(a1)              ; Overrun?
  1376.      bne.b      50$                      ; yep, branch
  1377.      ;
  1378. 20$     movea.l  i_BufIn(pc),a0              ; get current ptr
  1379.      move.b   _serdatr+1(a1),(a0)+              ; store received byte
  1380.      move.w   #INTF_RBF,_intreq(a1)           ; clear RBF interrupt
  1381.      addq.l   #1,i_InCnt                  ; incr bytes in buffer
  1382.      ;
  1383.      cmpa.l   i_BufEnd(pc),a0              ; hit end of buffer?
  1384.      beq.b      60$                      ; yep, branch
  1385. 30$     move.l   a0,i_BufIn                  ; store input ptr
  1386.      ;
  1387.      subq.l   #1,i_Thresh                  ; close to full buffer?
  1388.      beq.b      70$                      ; yep, branch
  1389.      ;
  1390. 40$     btst      #INTB_RBF,_intreqr(a1)          ; receive buffer full?
  1391.      bne.b      10$                      ; yep, go get another byte
  1392.      ;
  1393.      movea.l  (sp)+,a1                  ; restore registers
  1394.      movea.l  (sp)+,a0                  ; restore registers
  1395. 45$     rte                          ; return
  1396.      ;
  1397.      ;      We've missed some data, so set overrun flag.
  1398.      ;
  1399. 50$     addq.b   #1,Overrun                  ; set overrun flag
  1400.      bra.b      20$                      ; continue
  1401.      ;
  1402.      ;      Hit physical end of buffer, so wrap to the start of the buffer.
  1403.      ;
  1404.      cnop 0,4 ; align for 020+
  1405. 60$     movea.l  i_BufPtr(pc),a0              ; get buffer ptr
  1406.      bra.b      30$                      ; continue
  1407.      ;
  1408.      ;      Hit buffer threshold, so tell other end not to send any more
  1409.      ;      data.
  1410.      ;
  1411.      cnop 0,4 ; align for 020+
  1412. 70$     tst.b      Handshake(pc)               ; are we handshaking?
  1413.      beq.b      40$                      ; nope, skip RTS
  1414.      ori.b      #1<<CIAB_COMRTS,_ciabpra          ; block further input
  1415.      bra.b      40$                      ; go return
  1416.      ;
  1417.      ;
  1418.      ;  a1 - IS_DATA
  1419.      ;  a5 - jump vector register
  1420.      ;
  1421.      cnop 0,4 ; align for 020+
  1422. level2:
  1423.      ;
  1424.      ;      If there's nothing in the buffer, there's no point in going
  1425.      ;      any further.
  1426.      ;
  1427.      move.l   i_InCnt(pc),d0              ; anything in the buffer?
  1428.      bne.b      20$                      ; yep, branch
  1429. 10$     moveq      #0,d0                   ; set Z flag
  1430.      rts                          ; return
  1431.      ;
  1432.      ;      If we've been "disabled" then get out.
  1433.      ;
  1434. 20$     move.b   disableRead(pc),d0              ; internally disabled?
  1435.      bge.b      10$                      ; yep, get out of here
  1436.      lea.l      Start(pc),a5                  ; get base
  1437.      ;
  1438.      ;      If we have an active request, branch down and try to fulfill it.
  1439.      ;
  1440.      move.l   cr_IOReq(pc),d0              ; get and test active I/O
  1441.      bne.b      30$                      ; nzero, active, branch
  1442.      ;
  1443.      ;      Get first node in list and test if empty.
  1444.      ;
  1445.      move.l   (a1),a0                  ; get head of list
  1446.      move.l   (a0),d0                  ; get successor
  1447.      beq.b      10$                      ; end of list? yep, branch
  1448.      ;
  1449.      ;      Remove the node from the list
  1450.      ;
  1451.      move.l   d0,(a1)                  ; make new head
  1452.      exg.l      d0,a0                   ; swap nodes
  1453.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1454.      ;
  1455.      ;      Setup fields for processing a read request
  1456.      ;
  1457.      movea.l  d0,a1                   ; get I/O request
  1458.      move.b   IO_FLAGS(a1),d1
  1459.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1460.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1461.      move.b   d1,IO_FLAGS(a1)
  1462.      move.l   a1,cr_IOReq-Start(a5)           ; store I/O request
  1463.      move.l   IO_DATA(a1),cr_OutPtr-Start(a5)     ; get/set output ptr
  1464.      move.l   IO_LENGTH(a1),cr_Length-Start(a5)   ; get/set output count
  1465.      ;
  1466.      ;      Process an active I/O request
  1467.      ;
  1468. 30$     movea.l  SysBase(pc),a6              ; get ExecBase
  1469.      move.l   d0,a1                   ; get I/O request
  1470.      bsr      copyData                  ; go copy 'em
  1471.      tst.l      d0                      ; done with request?
  1472.      beq.b      10$                      ; nope, branch
  1473.      ;
  1474.      ;      the request has been satisfied, so return it.
  1475.      ;
  1476.      clr.l      cr_IOReq-Start(a5)              ; clear request
  1477.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1478.      jsr      _LVOReplyMsg(a6)              ; return I/O
  1479.      bra.b      10$
  1480.      ;
  1481.      ;      Registers:
  1482.      ;      Entry:   A1        Ptr to I/O Request
  1483.      ;           A6        ExecBase
  1484.      ;      Exit:    D0        ZERO - request not done, do not reply it
  1485.      ;                NZERO - request done, reply it
  1486.      ;
  1487.      cnop 0,4 ; align for 020+
  1488. copyData:
  1489.      ;
  1490.      ;      Process an active I/O request (or fall through from above)
  1491.      ;
  1492.      movem.l  d2-d5/a1-a5,-(sp)              ; save registers
  1493.      lea.l      Start(pc),a5                  ; get base
  1494.      movea.l  a1,a4                   ; get I/O request
  1495.      ;
  1496.      movea.l  i_BufOut(pc),a2              ; get current bufout ptr
  1497.      movea.l  cr_OutPtr(pc),a3              ; get current output ptr
  1498.      move.l   cr_Length(pc),d2              ; get current bytes needed
  1499.      move.l   i_InCnt(pc),d3              ; get current bytes in buffer
  1500.      ;
  1501.      ;      If we don't have enough bytes to satisfy the request,
  1502.      ;      set the length to the number of bytes we do have.
  1503.      ;
  1504.      cmp.l      d2,d3                   ; enuf to satisfy request?
  1505.      bge.b      15$                      ; yep, so branch
  1506.      move.l   d3,d2                   ; # to copy = # in buffer
  1507.      bra.b      15$                      ; branch to loop entry
  1508.      ;
  1509.      ;      Start of copy loop.
  1510.      ;
  1511.      cnop 0,4 ; align for 020+
  1512. 10$     movea.l  i_BufPtr(pc),a2              ; reset bufout to start
  1513.      ;
  1514.      ;      Entry point of copy loop.
  1515.      ;
  1516. 15$     move.l   d2,d3                   ; xfer # of bytes to copy
  1517.      ;
  1518.      ;      If the copy will extend past the end of the buffer, we can
  1519.      ;      only copy the number of bytes to the end this go around.
  1520.      ;
  1521.      move.l   i_BufEnd(pc),d0              ; get ptr to end of buf
  1522.      sub.l      a2,d0                   ; calc # of bytes to end
  1523.      cmp.l      d0,d3                   ; # to copy < # to end?
  1524.      blt.b      20$                      ; yep, branch
  1525.      move.l   d0,d3                   ; get bytes to end
  1526.      ;
  1527.      ;      Registers:
  1528.      ;
  1529.      ;      A2 = pointer from which data will be copied
  1530.      ;      A3 = pointer to which data will be copied
  1531.      ;      D2 = number of bytes that need to be copied
  1532.      ;      D3 = number of bytes to copy this iteration
  1533.      ;
  1534.     IFNE EOFCODE
  1535. 20$     btst      #SERB_EOFMODE,IO_SERFLAGS(a4)       ; EOFMODE requested?
  1536.      beq.b      30$                      ; nope, just go copy
  1537.      ;
  1538.      ;      EOFMODE was specified so copy characters 1 at a time until
  1539.      ;      we hit an EOF character, the output butter has filled, or
  1540.      ;      the input buffer has drained.
  1541.      ;
  1542.      move.l   d3,d0                   ; get length
  1543.      ;
  1544. 21$     move.b   (a2)+,d1                  ; get byte
  1545.      ; This way the term char IS copied (if term char is not copied eofmode will fail)
  1546.      move.b   d1,(a3)+                  ; put in output buffer
  1547.      ;
  1548.      lea.l      IO_TERMARRAY(a4),a1              ; get termarry ptr
  1549.      cmp.b      (a1),d1                  ; found term char?
  1550.      bge.b      22$                      ; possibly, branch
  1551.      cmp.b      1(a1),d1                  ; found term char?
  1552.      bge.b      22$                      ; possibly, branch
  1553.      cmp.b      2(a1),d1                  ; found term char?
  1554.      bge.b      22$                      ; possibly, branch
  1555.      cmp.b      3(a1),d1                  ; found term char?
  1556.      bge.b      22$                      ; possibly, branch
  1557.      cmp.b      4(a1),d1                  ; found term char?
  1558.      bge.b      22$                      ; possibly, branch
  1559.      cmp.b      5(a1),d1                  ; found term char?
  1560.      bge.b      22$                      ; possibly, branch
  1561.      cmp.b      6(a1),d1                  ; found term char?
  1562.      bge.b      22$                      ; possibly, branch
  1563.      cmp.b      7(a1),d1                  ; found term char?
  1564.      bgt.b      23$                      ; nope, branch
  1565. 22$     beq.b      24$                      ; term char found?
  1566.      ;
  1567.      ;      Didn't find a term character, so continue with the copy loop
  1568.      ;
  1569. 23$
  1570.      subq.l   #1,d0                   ; decr length counter
  1571.      bne.b      21$                      ; continue if more
  1572.      bra      40$                      ; done with copy, branch
  1573.      ;
  1574.      ;      We've found a termination character.
  1575.      ;
  1576.      cnop 0,4 ; align for 020+
  1577. 24$     clr.l      cr_Length-Start(a5)              ; done with request
  1578.      bra      50$                      ; branch
  1579.      cnop 0,4 ; align for 020+
  1580.     ELSE
  1581. 20$
  1582.     ENDC
  1583.      ;
  1584.      ;      EOFMODE not specified, so just do a bulk copy.
  1585.      ;
  1586.      ;      XXX POSSIBLE SPEEDUP XXX
  1587.      ;
  1588.      ;      For short copies it would be quicker to have a simple inline
  1589.      ;      loop, but what's short???  It would be different by CPU.
  1590.      ;
  1591. 30$
  1592.      move.l   d3,d0                   ; get length
  1593.      movea.l  a3,a1                   ; where to put it
  1594.      movea.l  a2,a0                   ; where to get it
  1595.  
  1596.      movem.l d2-d7/a2-a6,-(sp)
  1597. ; we assume buffers are aligned
  1598. ; NB: buffer size is multiple of 64bytes (not contents size)
  1599. ;Copy should fit on 256 byte Inst Cache.
  1600. ;Copy loop for 1K blocks
  1601. 35$     cmp.l     #1024,d0
  1602.      blo     100$
  1603. ;Copy 1008 bytes
  1604.      movem.l (a0)+,d1-d7/a2-a6
  1605.      movem.l d1-d7/a2-a6,(a1)
  1606.      movem.l (a0)+,d1-d7/a2-a6
  1607.      movem.l d1-d7/a2-a6,48(a1)
  1608.      movem.l (a0)+,d1-d7/a2-a6
  1609.      movem.l d1-d7/a2-a6,96(a1)
  1610.      movem.l (a0)+,d1-d7/a2-a6
  1611.      movem.l d1-d7/a2-a6,144(a1)
  1612.      movem.l (a0)+,d1-d7/a2-a6
  1613.      movem.l d1-d7/a2-a6,192(a1)
  1614.      movem.l (a0)+,d1-d7/a2-a6
  1615.      movem.l d1-d7/a2-a6,240(a1)
  1616.      movem.l (a0)+,d1-d7/a2-a6
  1617.      movem.l d1-d7/a2-a6,288(a1)
  1618.      movem.l (a0)+,d1-d7/a2-a6
  1619.      movem.l d1-d7/a2-a6,336(a1)
  1620.      movem.l (a0)+,d1-d7/a2-a6
  1621.      movem.l d1-d7/a2-a6,384(a1)
  1622.      movem.l (a0)+,d1-d7/a2-a6
  1623.      movem.l d1-d7/a2-a6,432(a1)
  1624.      movem.l (a0)+,d1-d7/a2-a6
  1625.      movem.l d1-d7/a2-a6,480(a1)
  1626.      movem.l (a0)+,d1-d7/a2-a6
  1627.      movem.l d1-d7/a2-a6,528(a1)
  1628.      movem.l (a0)+,d1-d7/a2-a6
  1629.      movem.l d1-d7/a2-a6,576(a1)
  1630.      movem.l (a0)+,d1-d7/a2-a6
  1631.      movem.l d1-d7/a2-a6,624(a1)
  1632.      movem.l (a0)+,d1-d7/a2-a6
  1633.      movem.l d1-d7/a2-a6,672(a1)
  1634.      movem.l (a0)+,d1-d7/a2-a6
  1635.      movem.l d1-d7/a2-a6,720(a1)
  1636.      movem.l (a0)+,d1-d7/a2-a6
  1637.      movem.l d1-d7/a2-a6,768(a1)
  1638.      movem.l (a0)+,d1-d7/a2-a6
  1639.      movem.l d1-d7/a2-a6,816(a1)
  1640.      movem.l (a0)+,d1-d7/a2-a6
  1641.      movem.l d1-d7/a2-a6,864(a1)
  1642.      movem.l (a0)+,d1-d7/a2-a6
  1643.      movem.l d1-d7/a2-a6,912(a1)
  1644.      movem.l (a0)+,d1-d7/a2-a6
  1645.      movem.l d1-d7/a2-a6,960(a1)
  1646. ;Copy 16 bytes
  1647.      movem.l (a0)+,d1-d4
  1648.      movem.l d1-d4,1008(a1)
  1649.      lea.l     1024(a1),a1
  1650.      sub.l     #1024,d0
  1651.      bne     35$
  1652.      bra.b     105$
  1653.      cnop 0,4 ; align for 020+
  1654. 100$     jsr     _LVOCopyMem(a6)
  1655. 105$     movem.l (sp)+,d2-d7/a2-a6
  1656.      adda.l   d3,a2                   ; update bufout
  1657.      adda.l   d3,a3                   ; update outptr
  1658.      ;
  1659.      ;      Fall through and entered from EOFMODE loop
  1660.      ;
  1661.      ;      If the following calculation results in a value greater than
  1662.      ;      zero, then we have a buffer wrap and need to process the
  1663.      ;      remaining bytes at the beginning of the buffer.
  1664.      ;
  1665. 40$     sub.l      d3,d2                   ; calc bytes left to copy
  1666.      bgt      10$                      ; >0, more to copy, branch
  1667.      ;
  1668.      ;
  1669.      ;
  1670. 50$     move.l   a3,d1                   ; get outptr
  1671.      sub.l      cr_OutPtr(pc),d1              ; calc length
  1672.      move.l   a3,cr_OutPtr-Start(a5)          ; update outptr
  1673.      ;
  1674.      ;      Update output ptr and I/O request
  1675.      ;
  1676.      move.l   a2,i_BufOut-Start(a5)           ; store bufout ptr
  1677.      add.l      d1,IO_ACTUAL(a4)              ; update I/O request
  1678.      ;
  1679.      ;      Update number of bytes left in the buffer.
  1680.      ;
  1681.      sub.l      d1,i_InCnt-Start(a5)              ; calc bytes left in buffer
  1682.      ;
  1683.      ;      If the threshold becomes positive here, then, if requested,
  1684.      ;      tell the other end that it's okay to start sending more data.
  1685.      ;
  1686.      add.l      d1,i_Thresh-Start(a5)           ; calc thresh and test
  1687.      ble.b      60$                      ; <= 0, need more data, skip
  1688.      tst.b      Handshake(pc)               ; are we handshaking?
  1689.      beq.b      60$                      ; nope, skip RTS
  1690.      andi.b   #~(1<<CIAB_COMRTS),_ciabpra          ; ready to recieve more data
  1691.      ;
  1692.      ;      Set error if we've had any overruns.
  1693.      ;
  1694. 60$     tst.b      Overrun(pc)                  ; did overrun occur?
  1695.      beq.b      70$                      ; nope, branch
  1696.      clr.b      Overrun-Start(a5)              ; reset overrun flag
  1697.      move.b   #SerErr_LineErr,IO_ERROR(a4)          ; set error code
  1698.      ;
  1699.      ;      Calc number of bytes left to copy.  If the result is greater
  1700.      ;      than zero, then we have more to copy so return an incomplete
  1701.      ;      status.
  1702.      ;
  1703. 70$     moveq      #0,d0                   ; assume I/O incomplete
  1704.      sub.l      d1,cr_Length-Start(a5)          ; update length and test
  1705.      bgt.b      90$                      ; >0, more to do, branch
  1706.      ;
  1707.      ;      We've completed the I/O request either by copying the requested
  1708.      ;      of bytes or by finding an EOFMODE character, so return a "reply"
  1709.      ;      status.
  1710.      ;
  1711.      moveq      #1,d0                   ; indicate reply
  1712.      ;
  1713.      ;      Restore and exit
  1714.      ;
  1715. 90$     movem.l  (sp)+,d2-d5/a1-a5              ; restore registers
  1716.      rts                          ; return (status in D0)
  1717.      ;
  1718.      ;      Align data
  1719.      ;
  1720.      CNOP      0,4
  1721.      ;
  1722.      ;
  1723.      ;
  1724. Init:
  1725.      DC.L      sizeof_Base8n1
  1726.      DC.L      funcTab
  1727.      DC.L      dataTab
  1728.      DC.L      InitRoutine
  1729.      ;
  1730.      ;
  1731.      ;
  1732. funcTab:
  1733.      DC.W      -1
  1734.      DC.W      dev_Open-funcTab
  1735.      DC.W      dev_Close-funcTab
  1736.      DC.W      dev_Expunge-funcTab
  1737.      DC.W      dev_Null-funcTab
  1738.      DC.W      dev_BeginIO-funcTab
  1739.      DC.W      dev_AbortIO-funcTab
  1740.      DC.W      -1
  1741.      ;
  1742.      ;
  1743.      ;
  1744. dataTab:
  1745.      INITBYTE LN_TYPE,NT_DEVICE
  1746.      INITLONG LN_NAME,Name
  1747.      INITBYTE LIB_FLAGS,LIBF_SUMUSED|LIBF_CHANGED
  1748.      INITWORD LIB_VERSION,VERSION
  1749.      INITWORD LIB_REVISION,REVISION
  1750.      INITLONG LIB_IDSTRING,IdString
  1751.      DC.W      0
  1752.      ;
  1753.      ;      String Constants
  1754.      ;
  1755. miscresource:
  1756.      DC.B      "misc.resource",0
  1757. timerdevice:
  1758.      DC.B      "timer.device",0
  1759. intuitlib:
  1760.      DC.B      "intuition.library",0
  1761. Name:
  1762.      DC.B      "8n1.device",0
  1763. IdString:
  1764.      VSTRING
  1765.      ;
  1766.      ;      End of checksummed area.  (Realigns data too!)
  1767.      ;
  1768. ENDTag:
  1769.      CNOP      0,4
  1770.      ;
  1771.      ;      Global SysBase (Use instead of AbsExecBase for speed)
  1772.      ;
  1773. SysBase:
  1774.      DC.L      0
  1775.      ;
  1776.      ;      Internal buffer tracking (DO NOT CHANGE THE ORDER!!!!)
  1777.      ;
  1778. i_BufPtr:
  1779.      DC.L      0
  1780. i_BufIn:
  1781.      DC.L      0
  1782. i_BufOut:
  1783.      DC.L      0
  1784. i_BufEnd:
  1785.      DC.L      0
  1786. i_InCnt:
  1787.      DC.L      0
  1788. i_Thresh:
  1789.      DC.L      0
  1790.      ;
  1791.      ;      Used while processing a read request.
  1792.      ;
  1793. cr_IOReq:
  1794.      DC.L      0
  1795. cr_OutPtr:
  1796.      DC.L      0
  1797. cr_Length:
  1798.      DC.L      0
  1799.      ;
  1800.      ;      List head for read requests
  1801.      ;
  1802. readQ:
  1803.      DC.L      readQ+MLH_TAIL
  1804.      DC.L      0
  1805.      DC.L      readQ
  1806.      ;
  1807.      ;      Write control.
  1808.      ;
  1809. cw_Length:
  1810.      DC.L      0
  1811. cw_Buffer:
  1812.      DC.L      0
  1813. cw_IOReq:
  1814.      DC.L      0
  1815.      ;
  1816.      ;      List head for write requests
  1817.      ;
  1818. writeQ:
  1819.      DC.L      writeQ+MLH_TAIL
  1820.      DC.L      0
  1821.      DC.L      writeQ
  1822.      ;
  1823.      ;
  1824.      ;
  1825. timerPort:
  1826.      DC.L      0                      ; LN_SUCC
  1827.      DC.L      0                      ; LN_PRED
  1828.      DC.B      NT_MSGPORT                  ; LN_TYPE
  1829.      DC.B      0                      ; LN_PRI
  1830.      DC.L      0                      ; LN_NAME
  1831.      DC.B      3                      ; MP_FLAGS (undoc'ed)
  1832.      DC.B      0                      ; MP_SIGBIT
  1833.      DC.L      timerRtn                  ; MP_SIGTASK
  1834.      DC.L      timerPort+MP_MSGLIST+LH_TAIL          ; LH_HEAD
  1835.      DC.L      0                      ; LH_TAIL
  1836.      DC.L      timerPort+MP_MSGLIST              ; LH_TAILPRED
  1837.      DC.B      0                      ; LH_TYPE
  1838.      DC.B      0                      ; LH_pad
  1839.      DC.W      0                      ; long align
  1840.      ;
  1841.      ;
  1842.      ;
  1843. timerReq:
  1844.      DC.L      0                      ; LN_SUCC
  1845.      DC.L      0                      ; LN_PRED
  1846.      DC.B      NT_MESSAGE                  ; LN_TYPE
  1847.      DC.B      0                      ; LN_PRI
  1848.      DC.L      0                      ; LN_NAME
  1849.      DC.L      timerPort                  ; MN_REPLYPORT
  1850.      DC.W      IOTV_SIZE                  ; MN_LENGTH
  1851.      DC.L      0                      ; IO_DEVICE
  1852.      DC.L      0                      ; IO_UNIT
  1853.      DC.W      TR_ADDREQUEST               ; IO_COMMAND
  1854.      DC.B      0                      ; IO_FLAGS
  1855.      DC.B      0                      ; IO_ERROR
  1856.      DC.L      0                      ; TV_SECS
  1857.      DC.L      0                      ; TV_MICROS
  1858.      ;
  1859.      ;
  1860.      ;
  1861. VBInterrupt:
  1862.      DC.L      0                      ; LN_SUCC
  1863.      DC.L      0                      ; LN_PRED
  1864.      DC.B      NT_INTERRUPT                  ; LN_TYPE
  1865.      DC.B      0                      ; LN_PRI
  1866.      DC.L      Name                      ; LN_NAME
  1867.      DC.L      readQ                   ; IS_DATA
  1868.      DC.L      level2                  ; IS_CODE
  1869.      ;
  1870.      ;      Global flags
  1871.      ;
  1872. Overrun:
  1873.      DC.B      0
  1874. Handshake:
  1875.      DC.B      1
  1876. disableRead:
  1877.      DC.B      -1
  1878.      ;
  1879.      ;
  1880.      ;
  1881.      END
  1882.  
  1883.