home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Workbench / Dvices / 8N1V3725.LHA / 8n1_040.s < prev    next >
Encoding:
Text File  |  1996-11-30  |  57.7 KB  |  1,899 lines

  1. ********************************************************************************
  2. **                                          **
  3. **  Name       : 8n1_040.s                              **
  4. **  Copyright  : © Copyright 96                           **
  5. **  Author     : Iain Barclay                              **
  6. **  Created    : 30 Nov 96                              **
  7. **  Version    : 37.25                                  **
  8. **                                          **
  9. ********************************************************************************
  10.      ;
  11.      ;      SNMA specific options
  12.      ;
  13.      IFD      SNMA
  14.      ;
  15.      CPU      M68040
  16.      ;
  17.      SNMAOPT  Q,A,M,T,E-,P,R,B
  18.      ;
  19.      ELSE
  20.      machine 68040
  21.      OPT !
  22.      ENDC
  23.  
  24.      INCDIR   "dcc:Ainclude/"
  25.      ;
  26.      ;
  27.      ;
  28. DEBUG     SET      0
  29. NEWCODE  SET      1
  30. EOFCODE  SET      1
  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.     IFNE NEWCODE
  783.      tst.l      cr_IOReq(pc)                  ; have an active request?
  784.     ELSE
  785.      move.l   cr_IOReq(pc),d1              ; have an active request?
  786.     ENDC
  787.      bne.b      50$                      ; yep, go queue this one
  788.      ;
  789.      ;      If we don't have enough bytes to satisfy this request then go
  790.      ;      queue it.
  791.      ;
  792.      cmp.l      i_InCnt(pc),d0              ; length > current bytes?
  793.      bgt.b      50$                      ; yep, go queue it
  794.      ;
  795.      ;      Setup fields and go copy the data
  796.      ;
  797.      move.l   IO_DATA(a1),cr_OutPtr           ; get/set output ptr
  798.      move.l   IO_LENGTH(a1),cr_Length          ; get/set output count
  799.      bsr      copyData                  ; go copy 'em
  800.      moveq      #0,d0                   ; I/O complete
  801.      ;
  802.      ;      We're done, so back off the disable counter.
  803.      ;
  804. 10$     subq.b   #1,disableRead              ; decr disable count
  805.      ;
  806.      ;      Return to caller
  807.      ;
  808. 20$     rts                          ; return
  809.      ;
  810.      ;      Just set flags and queue.  The read interrupt will handle it.
  811.      ;
  812. 50$     ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)    ; indicate queued
  813.      ;
  814.      ;      Add this request to the end.
  815.      ;
  816.      DISABLE                      ; disable interrupts
  817.      lea.l      readQ(pc),a0                  ; get pointer to read queue
  818.      move.l   a1,-(sp)                  ; save I/O request
  819.      jsr      _LVOAddTail(a6)              ; and queue it
  820.      movea.l  (sp)+,a1                  ; restore I/O request
  821.      ENABLE                       ; enable interrupts
  822.      ;
  823.      ;      Indicate that this request was not handled immediatly.
  824.      ;
  825.      moveq      #1,d0                   ; I/O not complete
  826.      bra.b      10$                      ; branch to return
  827.      ;
  828.      ;      Process a CMD_WRITE request.
  829.      ;
  830.      cnop 0,4 ; align for 020+
  831. cmd_Write:
  832.      clr.l      IO_ACTUAL(a1)               ; clear bytes written
  833.     IFNE NEWCODE
  834.      tst.l      IO_LENGTH(a1)               ; get length and test
  835.     ELSE
  836.      move.l   IO_LENGTH(a1),d0              ; get length and test
  837.     ENDC
  838.      beq.b      wbexit                  ; yep, leave
  839.      ;
  840.      ;      Entry point for Break command and fall through from cmd_Write.
  841.      ;
  842. sdcmd_Break:
  843.      ;
  844.      ;      Just set flags and queue. The TBE interrupt will handle it.
  845.      ;
  846.      ori.b      #1<<IOSERB_QUEUED,IO_FLAGS(a1)     ; indicate queued
  847.      ;
  848.      ;      Protect.
  849.      ;
  850.      DISABLE                      ; disable interrupts
  851.      ;
  852.      ;      Add request to end of queue.
  853.      ;
  854.      lea.l      writeQ(pc),a0               ; get queue list ptr
  855.      move.l   a1,-(sp)                  ; save I/O request
  856.      jsr      _LVOAddTail(a6)              ; and queue it
  857.      movea.l  (sp)+,a1                  ; restore I/O request
  858.      ;
  859.      ;      If we have an active request, don't force interrupt.
  860.      ;
  861.     IFNE NEWCODE
  862.      tst.l      cw_IOReq(pc)                  ; have an active request?
  863.     ELSE
  864.      move.l   cw_IOReq(pc),d0              ; have an active request?
  865.     ENDC
  866.      bne.b      10$                      ; yep, branch
  867.      ;
  868.      ;      Force a TBE interrupt to get the writes going.
  869.      ;
  870.      move.w   #INTF_SETCLR|INTF_TBE,_intreq       ; make TBE pending
  871.      ;
  872.      ;      Enable, set RC and return to caller.
  873.      ;
  874. 10$     ENABLE                       ; enable interrupts
  875.      moveq      #1,d0                   ; I/O not complete
  876. wbexit     rts                          ; return
  877.      ;
  878.      ;      Resets serial read buffer
  879.      ;
  880.      ;      Since this routine is called internally, it must NOT reference
  881.      ;      the I/O request.
  882.      ;
  883.      cnop 0,4 ; align for 020+
  884. cmd_Clear:
  885.      DISABLE                      ; disable interrupts
  886.      ;
  887.      ;      Load registers
  888.      ;
  889.      move.l   vb_CurRBuf(a5),d0              ; get internal buffer ptr
  890.      move.l   vb_CurRBufLen(a5),d1              ; and internal buffer len
  891.      lea.l      i_BufPtr(pc),a0              ; get ptr internal control
  892.      ;
  893.      ;      Initialize global buffer variables
  894.      ;
  895.      move.l   d0,(a0)                  ; store buffer ptr
  896.      move.l   d0,4(a0)                  ; set current input ptr
  897.      move.l   d0,8(a0)                  ; set current output ptr
  898.      add.l      d1,d0                   ; add buffer length
  899.      move.l   d0,12(a0)                  ; store ptr to end of buffer
  900.      clr.l      16(a0)                  ; clear byte cnt
  901.      move.l   vb_CurBaud(a5),d0              ; get internal baud
  902.      lsr.l      #4,d0                   ; divide by 16
  903.      sub.l      d0,d1                   ; subtract from length
  904.      move.l   d1,20(a0)                  ; set threshold
  905.      ;
  906.      ;      Enable, set RC and return.
  907.      ;
  908.      ENABLE                       ; enable interrupts
  909.      moveq      #0,d0                   ; I/O complete
  910.      rts                          ; return
  911.      ;
  912.      ;
  913.      ;
  914.      cnop 0,4 ; align for 020+
  915. cmd_Invalid
  916.      move.b   #IOERR_NOCMD,IO_ERROR(a1)          ; set bad status
  917.      moveq      #0,d0                   ; I/O complete
  918.      rts                          ; return
  919.      ;
  920.      ;      Returns number of bytes currently in internal buffer and
  921.      ;      current serial port status.
  922.      ;
  923.      ;      NOTE:  Not completely compatible with standard serial.device
  924.      ;      since it doesn't return the upper byte of IO_STATUS.
  925.      ;
  926.      cnop 0,4 ; align for 020+
  927. sdcmd_Query:
  928.      DISABLE                      ; disable interrupts
  929.      moveq      #0,d0                   ; clear d0
  930.      move.b   _ciabpra,d0                  ; get PR register
  931.      andi.w   #~PRTMASK,d0                  ; zap printer bits
  932.      ;
  933.      ;      To use this Set SETDSR to one.  This was done
  934.      ;      for a user whose DSR pin did not function.
  935.      ;
  936.     IFNE SETDSR
  937.      andi        #~(1<<CIAB_COMDSR),d0              ; set DSR
  938.     ENDC
  939.      ;
  940.      ;
  941.      ;
  942.      move.w   d0,IO_STATUS(a1)              ; store status
  943.      move.l   i_InCnt(pc),IO_ACTUAL(a1)          ; byte left in buffer
  944.      ;
  945.      ;      Enable, set RC and return.
  946.      ;
  947.      ENABLE                       ; enable interrupts
  948.      moveq      #0,d0                   ; I/O complete
  949.      rts                          ; return
  950.      ;
  951.      ;
  952.      ;
  953.      cnop 0,4 ; align for 020+
  954. sdcmd_SetParams:
  955.      ;
  956.      ;      Validate the read, write, and stop bit lengths.
  957.      ;
  958.      moveq      #8,d0                   ; get char length
  959.      cmp.b      IO_READLEN(a1),d0              ; 8 bit chars for read?
  960.      bne.b      40$                      ; nope, branch
  961.      cmp.b      IO_WRITELEN(a1),d0              ; 8 bit chars for write?
  962.      bne.b      40$                      ; nope, branch
  963.      moveq      #1,d0                   ; get stop bits
  964.      cmp.b      IO_STOPBITS(a1),d0              ; 1 stop bit?
  965.      bne.b      40$                      ; nope, branch
  966.      ;
  967.      ;      Get and validate the baud rate.
  968.      ;
  969.      move.l   IO_BAUD(a1),d1              ; get baud from I/O req
  970.      bne.b      20$                      ; specified?
  971.      move.l   vb_CurBaud(a5),d1              ; get current baud from base
  972.      bne.b      20$                      ; specified?
  973.      move.l   vb_DefBaud(a5),d1              ; get default baud from base
  974. 20$     cmpi.l   #110,d1                  ; too low?
  975.      blt.b      40$                      ; error
  976.      cmpi.l   #292000,d1                  ; too high?
  977.      bgt.b      40$                      ; error
  978.      ;
  979.      ;      Get and validate the buffer length.
  980.      ;
  981.      move.l   IO_RBUFLEN(a1),d0              ; get buffer length
  982.      bne.b      30$                      ; specified?
  983.      move.l   vb_CurRBufLen(a5),d0              ; get current from base
  984.      bne.b      30$                      ; specified?
  985.      move.l   vb_DefRBufLen(a5),d0              ; get default from base
  986.      ;
  987. 30$     bsr.b      internalReset               ; go init baud and buffer
  988. 35$     move.b   d0,IO_ERROR(a1)              ; set error code
  989.      bne.b      39$
  990.      ;
  991.      ;      If the 7wire bit is not on, we will only use 3-wire protocol
  992.      ;
  993.      moveq      #0,d0                   ; clear flag
  994.      btst.b   #SERB_7WIRE,IO_SERFLAGS(a1)          ; use 7wire handshaking?
  995.      beq.b      36$                      ; nope, branch
  996.      moveq      #1,d0                   ; set flag
  997. 36$     move.b   d0,Handshake                  ; store flag
  998.      ;
  999.      ;      Set RC and return.
  1000.      ;
  1001. 39$     moveq      #0,d0                   ; I/O complete
  1002.      rts                          ; return
  1003.      ;
  1004.      ;      Invalid parm detected.
  1005.      ;
  1006. 40$     moveq      #SerErr_InvParam,d0              ; set error
  1007.      bra.b      35$                      ; go return
  1008.      ;
  1009.      ;      Reset the buffer and baud rate
  1010.      ;
  1011.      ;      Registers:  D0 = Buffer length
  1012.      ;              D1 = Baud rate
  1013.      ;
  1014.      cnop 0,4 ; align for 020+
  1015. internalReset:
  1016.      ;
  1017.      ;      Disable interrupts.
  1018.      ;
  1019.      DISABLE                      ; disable interrupts
  1020.      ;
  1021.      ;      Save buffer length and go set serper.
  1022.      ;
  1023.      move.l   d0,-(sp)                  ; save D0
  1024.      move.l   d1,d0                   ; get baud rate
  1025.      bsr.b      setPeriod                  ; go set the serper register
  1026.      move.l   (sp)+,d0                  ; restore D0
  1027.      ;
  1028.      ;      Determine if the buffer length is adequate for the selected CPS.
  1029.      ;      If not, use 64K for the length.
  1030.      ;
  1031.      move.l   vb_CurBaud(a5),d1              ; get current baud
  1032.      lsr.l      #$3,d1                  ; divide by 8
  1033.      cmp.l      d1,d0                   ; buflen > CPS
  1034.      bhi.b      10$                      ; yep, branch
  1035.      move.l   #65536,d0                  ; else use 64K
  1036.      ;
  1037. 10$     bsr.b      allocBuf                  ; go allocate a new buffer
  1038.      ;
  1039.      ;      Enable and return to caller.
  1040.      ;
  1041.      ENABLE                       ; enable interrupts
  1042.      rts                          ; return (D0 has status)
  1043.      ;
  1044.      ;      Set serial period register
  1045.      ;
  1046.      cnop 0,4 ; align for 020+
  1047. setPeriod:
  1048.      cmp.l      vb_CurBaud(a5),d0              ; current baud = new baud?
  1049.      beq.b      40$                      ; yep, just exit
  1050.      move.l   d0,vb_CurBaud(a5)              ; save new baud
  1051.      move.l   d0,d1                   ; save again
  1052.      lsl.l      #3,d0                   ; baud *= 8
  1053.      sub.l      d1,d0                   ; baud -= saved baud
  1054.      move.l   #25000000,d1                  ; get NTSC base
  1055.      cmpi.b   #50,PowerSupplyFrequency(a6)          ; PAL machine?
  1056.      bne.b      10$                      ; nope, branch
  1057.      move.l   #24772416,d1                  ; get PAL base
  1058. 10$     cmpi.l   #$FFFF,d0                  ; Divide
  1059.      ble.b      20$                      ;
  1060.      lsr.l      #5,d0                   ;
  1061.      divu.w   d0,d1                   ;
  1062.      andi.l   #$FFFF,d1                  ;
  1063.      lsr.l      #5,d1                   ;
  1064.      bra.b      30$                      ;
  1065.      cnop 0,4 ; align for 020+
  1066. 20$     divu.w   d0,d1                   ;
  1067. 30$     move.w   d1,_serper                  ; set period value
  1068. 40$     rts
  1069.      ;
  1070.      ;      Allocate new internal buffer
  1071.      ;
  1072.      cnop 0,4 ; align for 020+
  1073. allocBuf:
  1074.      cmp.l      vb_CurRBufLen(a5),d0              ; len same as previous?
  1075.      beq.b      10$                      ; yep, so no need to alloc
  1076.      move.l   d0,d1                   ; save length
  1077.      movem.l  d1/a1,-(sp)                  ; save registers
  1078.      moveq      #MEMF_PUBLIC,d1              ; public memory
  1079.      jsr      _LVOAllocMem(a6)              ; go allocate it
  1080.      movem.l  (sp)+,d1/a1                  ; restore registers
  1081.      tst.l      d0                      ; did we get it?
  1082.      beq.b      20$                      ; if zero, error
  1083.      bsr.b      freeBuf                  ; go free previous buffer
  1084.      move.l   d0,vb_CurRBuf(a5)              ; store new ptr
  1085.      move.l   d1,vb_CurRBufLen(a5)              ; and length
  1086.      bsr      cmd_Clear                  ; go setup buffer
  1087. 10$     moveq      #0,d0                   ; success
  1088. 15$     rts                          ; return
  1089. 20$     moveq      #SerErr_BufErr,d0              ; set error status
  1090.      bra.b      15$                      ; return
  1091.      ;
  1092.      ;      Free internal buffer
  1093.      ;
  1094.      cnop 0,4 ; align for 020+
  1095. freeBuf:
  1096.      movem.l  d0-d1/a0-a1,-(sp)              ; save registers
  1097.      move.l   vb_CurRBuf(a5),d0              ; is one there?
  1098.      beq.b      10$                      ; no so branch
  1099.      movea.l  d0,a1                   ; get ptr
  1100.      move.l   vb_CurRBufLen(a5),d0              ; get length
  1101.      clr.l      vb_CurRBuf(a5)              ; clear
  1102.      clr.l      vb_CurRBufLen(a5)              ; clear
  1103.      jsr      _LVOFreeMem(a6)              ; free it
  1104. 10$     movem.l  (sp)+,d0-d1/a0-a1              ; restore registers
  1105.      rts                          ; return
  1106.      ;
  1107.      ;      Checks CTS status and if clear generates a TBE interrupt or
  1108.      ;      requeues the timer request.
  1109.      ;
  1110.      ;      Entered from Exec using the MsgPort callback.
  1111.      ;
  1112.      ;      Input:   a6 = ExecBase
  1113.      ;      Output:  none
  1114.      ;
  1115.      ;      No need to preserve d0/d1/a0/a1
  1116.      ;
  1117.      cnop 0,4 ; align for 020+
  1118. timerRtn:
  1119.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1120.      jsr      _LVORemove(a6)              ; remove request
  1121.      ;
  1122.      ;      If we were breaking, reset adkcon.
  1123.      ;
  1124.      lea      _custom,a0                  ; get ptr to custom regs
  1125.      btst      #ADKB_UARTBRK,_adkconr(a0)          ; were we breaking?
  1126.      beq.b      10$                      ; nope, skip reset
  1127.      ;
  1128.      move.w   #ADKF_UARTBRK,_adkcon(a0)          ; stop breaking
  1129.      ;
  1130.      ;
  1131.      ;
  1132.     IFNE NEWCODE
  1133. 10$     tst.b      Handshake(pc)               ; are we handshaking?
  1134.     ELSE
  1135. 10$     move.b   Handshake(pc),d0              ; are we handshaking?
  1136.     ENDC
  1137.      beq.b      20$                      ; nope, generate interrupt
  1138.      btst      #CIAB_COMCTS,_ciabpra           ; clear to send?
  1139.      beq.b      20$                      ; yep, go start writing
  1140.      ;
  1141.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1142.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1143.      jsr      _LVOSendIO(a6)              ; go queue it
  1144.      bra.b      30$                      ; branch to return
  1145.      ;
  1146.      ;      CTS is clear so generate TBE interrupt to restart writing
  1147.      ;
  1148.      cnop 0,4 ; align for 020+
  1149. 20$     move.w   #INTF_SETCLR|INTF_TBE,_intreq(a0)   ; set TBE interrupt
  1150. 30$     rts                          ; return
  1151.      ;
  1152.      ;      Non serial interrupt
  1153.      ;
  1154.      ;  a0 - custom chips base
  1155.      ;  a1 - is_Data
  1156.      cnop 0,4 ; align for 020+
  1157. level1n:
  1158.      movem.l  d0-d1/a0-a1/a5-a6,-(sp)          ; save registers
  1159.      lea      _custom,a0                  ; get ptr to custom regs
  1160.      move.w   intenar(a0),d1              ; get enabled interrupts
  1161.      and.w      intreqr(a0),d1              ; and in requested interrupts
  1162.      movea.l  SysBase(pc),a6              ; get ExecBase
  1163.      ;
  1164.      btst      #INTB_DSKBLK,d1              ; Disk block done?
  1165.      beq.b      10$                      ; nope, branch
  1166.      ;
  1167.      movem.l  IVDSKBLK(a6),a1/a5              ; get data and code ptrs
  1168.      pea.l      20$(pc)                  ; push return address
  1169.      jmp      (a5)                      ; jump to routine
  1170.      ;
  1171. 10$     btst      #INTB_SOFTINT,d1              ; software interrupt?
  1172.      beq.b      20$                      ; nope, branch
  1173.      ;
  1174.      movem.l  IVSOFTINT(a6),a1/a5              ; get data and code ptrs
  1175.      pea.l      20$(pc)                  ; push return address
  1176.      jmp      (a5)                      ; jump to routine
  1177.      ;
  1178. 20$     movem.l  (sp)+,d0-d1/a0-a1/a5-a6          ; restore registers
  1179.      rte                          ; return
  1180.      ;
  1181.      ;      Level 1 interrupt handler
  1182.      ;
  1183.      ;  a1 - is_Data
  1184.      ;
  1185.      cnop 0,4 ; align for 020+
  1186. level1:
  1187.      btst      #INTB_INTEN,_intenar              ; interrupts enabled?
  1188.      beq.b      35$                      ; nope, ignore
  1189.      btst      #INTB_SOFTINT,_intreqr+1          ; software interrupt?
  1190.      bne.b      level1n                  ; nope, branch
  1191.      btst      #INTB_TBE,_intreqr+1              ; xmit buffer empty?
  1192.      beq.b      level1n                  ; nope, invoke old handler
  1193.      ;
  1194.      ;      Handle "Transmit Buffer Empty" interrupt (write)
  1195.      ;
  1196.      move.w   #INTF_TBE,_intreq              ; clear interrupt
  1197.      move.l   d0,-(sp)                  ; save D0 (faster than MOVEM)
  1198.      move.l   a0,-(sp)                  ; save A0 (faster than MOVEM)
  1199.      ;
  1200.      ;      If we're not handshaking, bypass it.
  1201.      ;
  1202.     IFNE NEWCODE
  1203. 10$     tst.b      Handshake(pc)               ; are we handshaking?
  1204.     ELSE
  1205. 10$     move.b   Handshake(pc),d0              ; are we handshaking?
  1206.     ENDC
  1207.      beq.b      20$                      ; nope, skip CTS test
  1208.      btst      #CIAB_COMCTS,_ciabpra           ; clear to send?
  1209.      bne.b      40$                      ; nope, branch
  1210.      ;
  1211.      ;      If cw_Length goes negative here, we are either done with a
  1212.      ;      request or we were called as a result of a fake interrupt
  1213.      ;      to force us to get the next request going.
  1214.      ;
  1215. 20$     subq.l   #1,cw_Length                  ; decr write length
  1216.      blt.b      60$                      ; < zero, done, branch
  1217.      ;
  1218.      ;      Currently processing a request.
  1219.      ;
  1220.      movea.l  cw_Buffer(pc),a0              ; get buffer ptr
  1221.      move.w   #256,d0                  ; set stop bit
  1222.      move.b   (a0)+,d0                  ; get next byte
  1223.      move.l   a0,cw_Buffer                  ; store buffer ptr
  1224.      move.w   d0,_serdat                  ; store in serdat reg
  1225.      ;
  1226. 30$     movea.l  (sp)+,a0                  ; restore registers
  1227.      move.l   (sp)+,d0                  ; restore registers
  1228. 35$     rte                          ; return
  1229.      ;
  1230.      ;      Queue a timer request to recheck CTS status
  1231.      ;
  1232. 40$     movem.l  d1/a1/a6,-(sp)              ; save registers
  1233.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1234.      move.l   #1000,IOTV_TIME+TV_MICRO(a1)          ; wait for .001 seconds
  1235.      movea.l  SysBase(pc),a6              ; get ExecBase
  1236.      jsr      _LVOSendIO(a6)              ; queue the request
  1237.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1238.      bra.b      30$                      ; go return
  1239.      ;
  1240.      ;      There aren't anymore requests, so clear and exit
  1241.      ;
  1242.      cnop 0,4 ; align for 020+
  1243. 50$     clr.l      cw_Length-Start(a6)              ; clear length
  1244.      clr.l      cw_IOReq-Start(a6)              ; clear
  1245.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1246.      bra.b      30$                      ; go return
  1247.      ;
  1248.      ;      Write request completed
  1249.      ;
  1250.      cnop 0,4 ; align for 020+
  1251. 60$     movem.l  d1/a1/a6,-(sp)              ; save registers
  1252.      ;
  1253.      move.l   cw_IOReq(pc),d0              ; active I/O request?
  1254.      beq.b      70$                      ; nope, branch
  1255.      ;
  1256.      ;      Reply it and setup for next
  1257.      ;
  1258.      movea.l  d0,a1                   ; get I/O request
  1259.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1260.      clr.b      IO_ERROR(a1)                  ; no error
  1261.      movea.l  SysBase(pc),a6              ; get ExecBase
  1262.      jsr      _LVOReplyMsg(a6)              ; return I/O request
  1263.      ;
  1264. 70$     lea.l      Start(pc),a6                  ; get section base
  1265.      ;
  1266.      lea.l      writeQ(pc),a1               ; get ptr to write queue
  1267.      move.l   (a1),a0                  ; get head of list
  1268.      move.l   (a0),d0                  ; get successor
  1269.      beq.b      50$                      ; end of list? yep, branch
  1270.      ;
  1271.      ;      Remove the node from the list
  1272.      ;
  1273.      move.l   d0,(a1)                  ; make new head
  1274.      exg.l      d0,a0                   ; swap nodes
  1275.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1276.      ;
  1277.      move.l   d0,a1                   ; get I/O request
  1278.      ;
  1279.      ;      If it's a BREAK, then branch to process as such.
  1280.      ;
  1281.      cmpi.w   #SDCMD_BREAK,IO_COMMAND(a1)          ; BREAK command?
  1282.      beq.b      100$                      ; yep, branch
  1283.      ;
  1284.      ;      Check for absolute length
  1285.      ;
  1286.      move.l   IO_DATA(a1),a0              ; get data ptr
  1287.      moveq      #-1,d1                  ; get value
  1288.      move.l   IO_LENGTH(a1),d0              ; get length
  1289.      cmp.l      d1,d0                   ; length = -1?
  1290.      bne.b      90$                      ; nope, skip scan
  1291.      ;
  1292.      ;      Scan the data for null to calc the length
  1293.      ;
  1294.      move.l   a0,d1                   ; save data ptr
  1295.     ; Buffer size is multiple of 64bytes so we can safely move a long
  1296. 80$     move.l   (a0),d0                  ; move a long into d0
  1297.      addq.l   #1,a0                   ; update pointer
  1298.      tst.b      d0                      ; does it equal 0? (first byte)
  1299.      beq.b      81$                      ; yep, finish
  1300.      addq.l   #1,a0                   ; update pointer
  1301.      lsl.l      #8,d0                   ; shift too second byte
  1302.      tst.b      d0                      ; does it equal 0? (second byte)
  1303.      beq.b      81$                      ; yep, finish
  1304.      addq.l   #1,a0                   ; update pointer
  1305.      lsl.l      #8,d0                   ; shift too third byte
  1306.      tst.b      d0                      ; does it equal 0? (third byte)
  1307.      beq.b      81$                      ; yep, finish
  1308.      addq.l   #1,a0                   ; update pointer
  1309.      lsl.l      #8,d0                   ; shift too fourth byte
  1310.      tst.b      d0                      ; does it equal 0? (fourth byte)
  1311.      beq.b      81$                      ; yep, finish
  1312.      bra.b      80$                      ; loop
  1313.      cnop 0,4 ; align for 020+
  1314. 81$
  1315.      suba.l   d1,a0                   ; calc # of bytes
  1316.      move.l   a0,d0                   ; xfer
  1317.      movea.l  d1,a0                   ; get data ptr
  1318.      ;
  1319. 90$     move.l   d0,IO_ACTUAL(a1)              ; go ahead and set it
  1320.      move.b   IO_FLAGS(a1),d1
  1321.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1322.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1323.      move.b   d1,IO_FLAGS(a1)
  1324.      move.l   d0,cw_Length-Start(a6)          ; store length
  1325.      move.l   a0,cw_Buffer-Start(a6)          ; store buffer ptr
  1326.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1327.      ;
  1328.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1329.      bra      10$                      ; go start request
  1330.      ;
  1331.      ;      Start the BREAK.
  1332.      ;
  1333.      cnop 0,4 ; align for 020+
  1334. 100$     move.b   IO_FLAGS(a1),d1
  1335.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1336.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1337.      move.b   d1,IO_FLAGS(a1)
  1338.      clr.l      cw_Length-Start(a6)              ; clear length
  1339.      move.l   a1,cw_IOReq-Start(a6)           ; store I/O request ptr
  1340.      move.w   #ADKF_SETCLR|ADKF_UARTBRK,_adkcon   ; start break
  1341.      move.l   IO_BRKTIME(a1),d0              ; get break time
  1342.      lea.l      timerReq(pc),a1              ; get ptr to timer request
  1343.      move.l   d0,IOTV_TIME+TV_MICRO(a1)          ; set the timeout
  1344.      movea.l  SysBase(pc),a6              ; get ExecBase
  1345.      jsr      _LVOSendIO(a6)              ; queue the request
  1346.      movem.l  (sp)+,d1/a1/a6              ; restore registers
  1347.      bra      30$                      ; go exit
  1348.      ;
  1349.      ;      Non serial interrupt
  1350.      ;
  1351.      ;  a0 - custom chips base
  1352.      ;  a1 - is_Data
  1353.      ;
  1354.      cnop 0,4 ; align for 020+
  1355. level5n:
  1356.      movem.l  d0-d1/a0-a1/a5-a6,-(sp)          ; save registers
  1357.      lea      _custom,a0                  ; get ptr to custom regs
  1358.      move.w   intenar(a0),d1              ; get enabled interrupts
  1359.      and.w      intreqr(a0),d1              ; and in requested interrupts
  1360.      movea.l  SysBase(pc),a6              ; get ExecBase
  1361.      btst      #INTB_DSKSYNC,d1              ; Disk synchronized?
  1362.      beq.b      10$                      ; nope, branch
  1363.      movem.l  IVDSKSYNC(A6),a1/a5              ; get data and code ptrs
  1364.      jsr      (a5)                      ; branch to routine
  1365. 10$     movem.l  (sp)+,d0-d1/a0-a1/a5-a6          ; restore registers
  1366.      rte                          ; return
  1367.      ;
  1368.      ;      Default Level 5 handler
  1369.      ;
  1370.      ;  a1 - is_Data
  1371.      ;
  1372.      cnop 0,4 ; align for 020+
  1373. level5:
  1374.      btst      #INTB_INTEN,_intenar              ; interrupts enabled?
  1375.      beq.b      45$                      ; nope, ignore
  1376.      btst      #INTB_RBF,_intreqr              ; receive buffer full?
  1377.      beq.b      level5n                  ; nope, invoke old handler
  1378.      ;
  1379.      move.l   a0,-(sp)                  ; save registers
  1380.      move.l   a1,-(sp)                  ; save registers
  1381.      lea      _custom,a1                  ; get ptr to custom regs
  1382.      ;
  1383. 10$     btst      #7,_serdatr(a1)              ; Overrun?
  1384.      bne.b      50$                      ; yep, branch
  1385.      ;
  1386. 20$     movea.l  i_BufIn(pc),a0              ; get current ptr
  1387.      move.b   _serdatr+1(a1),(a0)+              ; store received byte
  1388.      move.w   #INTF_RBF,_intreq(a1)           ; clear RBF interrupt
  1389.      addq.l   #1,i_InCnt                  ; incr bytes in buffer
  1390.      ;
  1391.      cmpa.l   i_BufEnd(pc),a0              ; hit end of buffer?
  1392.      beq.b      60$                      ; yep, branch
  1393. 30$     move.l   a0,i_BufIn                  ; store input ptr
  1394.      ;
  1395.      subq.l   #1,i_Thresh                  ; close to full buffer?
  1396.      beq.b      70$                      ; yep, branch
  1397.      ;
  1398. 40$     btst      #INTB_RBF,_intreqr(a1)          ; receive buffer full?
  1399.      bne.b      10$                      ; yep, go get another byte
  1400.      ;
  1401.      movea.l  (sp)+,a1                  ; restore registers
  1402.      movea.l  (sp)+,a0                  ; restore registers
  1403. 45$     rte                          ; return
  1404.      ;
  1405.      ;      We've missed some data, so set overrun flag.
  1406.      ;
  1407. 50$     addq.b   #1,Overrun                  ; set overrun flag
  1408.      bra.b      20$                      ; continue
  1409.      ;
  1410.      ;      Hit physical end of buffer, so wrap to the start of the buffer.
  1411.      ;
  1412.      cnop 0,4 ; align for 020+
  1413. 60$     movea.l  i_BufPtr(pc),a0              ; get buffer ptr
  1414.      bra.b      30$                      ; continue
  1415.      ;
  1416.      ;      Hit buffer threshold, so tell other end not to send any more
  1417.      ;      data.
  1418.      ;
  1419.      cnop 0,4 ; align for 020+
  1420. 70$     tst.b      Handshake(pc)               ; are we handshaking?
  1421.      beq.b      40$                      ; nope, skip RTS
  1422.      ori.b      #1<<CIAB_COMRTS,_ciabpra          ; block further input
  1423.      bra.b      40$                      ; go return
  1424.      ;
  1425.      ;
  1426.      ;  a1 - IS_DATA
  1427.      ;  a5 - jump vector register
  1428.      ;
  1429.      cnop 0,4 ; align for 020+
  1430. level2:
  1431.      ;
  1432.      ;      If there's nothing in the buffer, there's no point in going
  1433.      ;      any further.
  1434.      ;
  1435.     IFNE NEWCODE
  1436.      tst.l      i_InCnt(pc)                  ; anything in the buffer?
  1437.     ELSE
  1438.      move.l   i_InCnt(pc),d0              ; anything in the buffer?
  1439.     ENDC
  1440.      bne.b      20$                      ; yep, branch
  1441. 10$     moveq      #0,d0                   ; set Z flag
  1442.      rts                          ; return
  1443.      ;
  1444.      ;      If we've been "disabled" then get out.
  1445.      ;
  1446.     IFNE NEWCODE
  1447. 20$     tst.b      disableRead(pc)              ; internally disabled?
  1448.     ELSE
  1449. 20$     move.b   disableRead(pc),d0              ; internally disabled?
  1450.     ENDC
  1451.      bge.b      10$                      ; yep, get out of here
  1452.      lea.l      Start(pc),a5                  ; get base
  1453.      ;
  1454.      ;      If we have an active request, branch down and try to fulfill it.
  1455.      ;
  1456.      move.l   cr_IOReq(pc),d0              ; get and test active I/O
  1457.      bne.b      30$                      ; nzero, active, branch
  1458.      ;
  1459.      ;      Get first node in list and test if empty.
  1460.      ;
  1461.      move.l   (a1),a0                  ; get head of list
  1462.      move.l   (a0),d0                  ; get successor
  1463.      beq.b      10$                      ; end of list? yep, branch
  1464.      ;
  1465.      ;      Remove the node from the list
  1466.      ;
  1467.      move.l   d0,(a1)                  ; make new head
  1468.      exg.l      d0,a0                   ; swap nodes
  1469.      move.l   a1,LN_PRED(a0)              ; store predecessor
  1470.      ;
  1471.      ;      Setup fields for processing a read request
  1472.      ;
  1473.      movea.l  d0,a1                   ; get I/O request
  1474.      move.b   IO_FLAGS(a1),d1
  1475.      andi.b   #~(1<<IOSERB_QUEUED),d1          ; no longer queued
  1476.      ori.b      #1<<IOSERB_ACTIVE,d1              ; make it active
  1477.      move.b   d1,IO_FLAGS(a1)
  1478.      move.l   a1,cr_IOReq-Start(a5)           ; store I/O request
  1479.      move.l   IO_DATA(a1),cr_OutPtr-Start(a5)     ; get/set output ptr
  1480.      move.l   IO_LENGTH(a1),cr_Length-Start(a5)   ; get/set output count
  1481.      ;
  1482.      ;      Process an active I/O request
  1483.      ;
  1484. 30$     movea.l  SysBase(pc),a6              ; get ExecBase
  1485.      move.l   d0,a1                   ; get I/O request
  1486.      bsr      copyData                  ; go copy 'em
  1487.      tst.l      d0                      ; done with request?
  1488.      beq.b      10$                      ; nope, branch
  1489.      ;
  1490.      ;      the request has been satisfied, so return it.
  1491.      ;
  1492.      clr.l      cr_IOReq-Start(a5)              ; clear request
  1493.      andi.b   #~(1<<IOSERB_ACTIVE),IO_FLAGS(a1)   ; no longer active
  1494.      jsr      _LVOReplyMsg(a6)              ; return I/O
  1495.      bra.b      10$
  1496.      ;
  1497.      ;      Registers:
  1498.      ;      Entry:   A1        Ptr to I/O Request
  1499.      ;           A6        ExecBase
  1500.      ;      Exit:    D0        ZERO - request not done, do not reply it
  1501.      ;                NZERO - request done, reply it
  1502.      ;
  1503.      cnop 0,4 ; align for 020+
  1504. copyData:
  1505.      ;
  1506.      ;      Process an active I/O request (or fall through from above)
  1507.      ;
  1508.      movem.l  d2-d5/a1-a5,-(sp)              ; save registers
  1509.      lea.l      Start(pc),a5                  ; get base
  1510.      movea.l  a1,a4                   ; get I/O request
  1511.      ;
  1512.      movea.l  i_BufOut(pc),a2              ; get current bufout ptr
  1513.      movea.l  cr_OutPtr(pc),a3              ; get current output ptr
  1514.      move.l   cr_Length(pc),d2              ; get current bytes needed
  1515.      move.l   i_InCnt(pc),d3              ; get current bytes in buffer
  1516.      ;
  1517.      ;      If we don't have enough bytes to satisfy the request,
  1518.      ;      set the length to the number of bytes we do have.
  1519.      ;
  1520.      cmp.l      d2,d3                   ; enuf to satisfy request?
  1521.      bge.b      15$                      ; yep, so branch
  1522.      move.l   d3,d2                   ; # to copy = # in buffer
  1523.      bra.b      15$                      ; branch to loop entry
  1524.      ;
  1525.      ;      Start of copy loop.
  1526.      ;
  1527.      cnop 0,4 ; align for 020+
  1528. 10$     movea.l  i_BufPtr(pc),a2              ; reset bufout to start
  1529.      ;
  1530.      ;      Entry point of copy loop.
  1531.      ;
  1532. 15$     move.l   d2,d3                   ; xfer # of bytes to copy
  1533.      ;
  1534.      ;      If the copy will extend past the end of the buffer, we can
  1535.      ;      only copy the number of bytes to the end this go around.
  1536.      ;
  1537.      move.l   i_BufEnd(pc),d0              ; get ptr to end of buf
  1538.      sub.l      a2,d0                   ; calc # of bytes to end
  1539.      cmp.l      d0,d3                   ; # to copy < # to end?
  1540.      blt.b      20$                      ; yep, branch
  1541.      move.l   d0,d3                   ; get bytes to end
  1542.      ;
  1543.      ;      Registers:
  1544.      ;
  1545.      ;      A2 = pointer from which data will be copied
  1546.      ;      A3 = pointer to which data will be copied
  1547.      ;      D2 = number of bytes that need to be copied
  1548.      ;      D3 = number of bytes to copy this iteration
  1549.      ;
  1550.     IFNE EOFCODE
  1551. 20$     btst      #SERB_EOFMODE,IO_SERFLAGS(a4)       ; EOFMODE requested?
  1552.      beq.b      30$                      ; nope, just go copy
  1553.      ;
  1554.      ;      EOFMODE was specified so copy characters 1 at a time until
  1555.      ;      we hit an EOF character, the output butter has filled, or
  1556.      ;      the input buffer has drained.
  1557.      ;
  1558.      move.l   d3,d0                   ; get length
  1559.      ;
  1560.      lea.l      IO_TERMARRAY(a4),a1              ; get termarry ptr
  1561. 21$     move.b   (a2)+,d1                  ; get byte
  1562.      ; This way the term char IS copied (if term char is not copied eofmode will fail)
  1563.      move.b   d1,(a3)+                  ; put in output buffer
  1564.      ;
  1565.      cmp.b      (a1),d1                  ; found term char?
  1566.      bge.b      22$                      ; possibly, branch
  1567.      cmp.b      1(a1),d1                  ; found term char?
  1568.      bge.b      22$                      ; possibly, branch
  1569.      cmp.b      2(a1),d1                  ; found term char?
  1570.      bge.b      22$                      ; possibly, branch
  1571.      cmp.b      3(a1),d1                  ; found term char?
  1572.      bge.b      22$                      ; possibly, branch
  1573.      cmp.b      4(a1),d1                  ; found term char?
  1574.      bge.b      22$                      ; possibly, branch
  1575.      cmp.b      5(a1),d1                  ; found term char?
  1576.      bge.b      22$                      ; possibly, branch
  1577.      cmp.b      6(a1),d1                  ; found term char?
  1578.      bge.b      22$                      ; possibly, branch
  1579.      cmp.b      7(a1),d1                  ; found term char?
  1580.      bgt.b      23$                      ; nope, branch
  1581. 22$     beq.b      24$                      ; term char found?
  1582.      ;
  1583.      ;      Didn't find a term character, so continue with the copy loop
  1584.      ;
  1585. 23$
  1586.      subq.l   #1,d0                   ; decr length counter
  1587.      bne.b      21$                      ; continue if more
  1588.      bra      40$                      ; done with copy, branch
  1589.      ;
  1590.      ;      We've found a termination character.
  1591.      ;
  1592.      cnop 0,4 ; align for 020+
  1593. 24$     clr.l      cr_Length-Start(a5)              ; done with request
  1594.      bra      50$                      ; branch
  1595.      cnop 0,4 ; align for 020+
  1596.     ELSE
  1597. 20$
  1598.     ENDC
  1599.      ;
  1600.      ;      EOFMODE not specified, so just do a bulk copy.
  1601.      ;
  1602.      ;      XXX POSSIBLE SPEEDUP XXX
  1603.      ;
  1604.      ;      For short copies it would be quicker to have a simple inline
  1605.      ;      loop, but what's short???  It would be different by CPU.
  1606.      ;
  1607. 30$
  1608.      move.l   d3,d0                   ; get length
  1609.      movea.l  a3,a1                   ; where to put it
  1610.      movea.l  a2,a0                   ; where to get it
  1611.  
  1612.      movem.l d2-d7/a2-a6,-(sp)
  1613. ; we assume buffers are aligned
  1614. ; NB: buffer size is multiple of 64bytes (not contents size)
  1615. ;Copy should fit on 256 Kb Inst Cache.
  1616. ;Copy loop for 1K blocks
  1617. 35$     cmp.l     #1024,d0
  1618.      blo     100$
  1619. ;Copy 1008 bytes
  1620.      movem.l (a0)+,d1-d7/a2-a6
  1621.      movem.l d1-d7/a2-a6,(a1)
  1622.      movem.l (a0)+,d1-d7/a2-a6
  1623.      movem.l d1-d7/a2-a6,48(a1)
  1624.      movem.l (a0)+,d1-d7/a2-a6
  1625.      movem.l d1-d7/a2-a6,96(a1)
  1626.      movem.l (a0)+,d1-d7/a2-a6
  1627.      movem.l d1-d7/a2-a6,144(a1)
  1628.      movem.l (a0)+,d1-d7/a2-a6
  1629.      movem.l d1-d7/a2-a6,192(a1)
  1630.      movem.l (a0)+,d1-d7/a2-a6
  1631.      movem.l d1-d7/a2-a6,240(a1)
  1632.      movem.l (a0)+,d1-d7/a2-a6
  1633.      movem.l d1-d7/a2-a6,288(a1)
  1634.      movem.l (a0)+,d1-d7/a2-a6
  1635.      movem.l d1-d7/a2-a6,336(a1)
  1636.      movem.l (a0)+,d1-d7/a2-a6
  1637.      movem.l d1-d7/a2-a6,384(a1)
  1638.      movem.l (a0)+,d1-d7/a2-a6
  1639.      movem.l d1-d7/a2-a6,432(a1)
  1640.      movem.l (a0)+,d1-d7/a2-a6
  1641.      movem.l d1-d7/a2-a6,480(a1)
  1642.      movem.l (a0)+,d1-d7/a2-a6
  1643.      movem.l d1-d7/a2-a6,528(a1)
  1644.      movem.l (a0)+,d1-d7/a2-a6
  1645.      movem.l d1-d7/a2-a6,576(a1)
  1646.      movem.l (a0)+,d1-d7/a2-a6
  1647.      movem.l d1-d7/a2-a6,624(a1)
  1648.      movem.l (a0)+,d1-d7/a2-a6
  1649.      movem.l d1-d7/a2-a6,672(a1)
  1650.      movem.l (a0)+,d1-d7/a2-a6
  1651.      movem.l d1-d7/a2-a6,720(a1)
  1652.      movem.l (a0)+,d1-d7/a2-a6
  1653.      movem.l d1-d7/a2-a6,768(a1)
  1654.      movem.l (a0)+,d1-d7/a2-a6
  1655.      movem.l d1-d7/a2-a6,816(a1)
  1656.      movem.l (a0)+,d1-d7/a2-a6
  1657.      movem.l d1-d7/a2-a6,864(a1)
  1658.      movem.l (a0)+,d1-d7/a2-a6
  1659.      movem.l d1-d7/a2-a6,912(a1)
  1660.      movem.l (a0)+,d1-d7/a2-a6
  1661.      movem.l d1-d7/a2-a6,960(a1)
  1662.      lea.l     1008(a1),a1
  1663. ;Copy 16 bytes
  1664.      move16  (a0)+,(a1)+
  1665.      sub.l     #1024,d0
  1666.      bne     35$
  1667.      bra.b     105$
  1668.      cnop 0,4 ; align for 020+
  1669. 100$     move.l  SysBase(pc),a6
  1670.      jsr     _LVOCopyMem(a6)
  1671. 105$     movem.l (sp)+,d2-d7/a2-a6
  1672.      adda.l   d3,a2                   ; update bufout
  1673.      adda.l   d3,a3                   ; update outptr
  1674.      ;
  1675.      ;      Fall through and entered from EOFMODE loop
  1676.      ;
  1677.      ;      If the following calculation results in a value greater than
  1678.      ;      zero, then we have a buffer wrap and need to process the
  1679.      ;      remaining bytes at the beginning of the buffer.
  1680.      ;
  1681. 40$     sub.l      d3,d2                   ; calc bytes left to copy
  1682.      bgt      10$                      ; >0, more to copy, branch
  1683.      ;
  1684.      ;
  1685.      ;
  1686. 50$     move.l   a3,d1                   ; get outptr
  1687.      sub.l      cr_OutPtr(pc),d1              ; calc length
  1688.      move.l   a3,cr_OutPtr-Start(a5)          ; update outptr
  1689.      ;
  1690.      ;      Update output ptr and I/O request
  1691.      ;
  1692.      move.l   a2,i_BufOut-Start(a5)           ; store bufout ptr
  1693.      add.l      d1,IO_ACTUAL(a4)              ; update I/O request
  1694.      ;
  1695.      ;      Update number of bytes left in the buffer.
  1696.      ;
  1697.      sub.l      d1,i_InCnt-Start(a5)              ; calc bytes left in buffer
  1698.      ;
  1699.      ;      If the threshold becomes positive here, then, if requested,
  1700.      ;      tell the other end that it's okay to start sending more data.
  1701.      ;
  1702.      add.l      d1,i_Thresh-Start(a5)           ; calc thresh and test
  1703.      ble.b      60$                      ; <= 0, need more data, skip
  1704.      tst.b      Handshake(pc)               ; are we handshaking?
  1705.      beq.b      60$                      ; nope, skip RTS
  1706.      andi.b   #~(1<<CIAB_COMRTS),_ciabpra          ; ready to recieve more data
  1707.      ;
  1708.      ;      Set error if we've had any overruns.
  1709.      ;
  1710. 60$     tst.b      Overrun(pc)                  ; did overrun occur?
  1711.      beq.b      70$                      ; nope, branch
  1712.      clr.b      Overrun-Start(a5)              ; reset overrun flag
  1713.      move.b   #SerErr_LineErr,IO_ERROR(a4)          ; set error code
  1714.      ;
  1715.      ;      Calc number of bytes left to copy.  If the result is greater
  1716.      ;      than zero, then we have more to copy so return an incomplete
  1717.      ;      status.
  1718.      ;
  1719. 70$     moveq      #0,d0                   ; assume I/O incomplete
  1720.      sub.l      d1,cr_Length-Start(a5)          ; update length and test
  1721.      bgt.b      90$                      ; >0, more to do, branch
  1722.      ;
  1723.      ;      We've completed the I/O request either by copying the requested
  1724.      ;      of bytes or by finding an EOFMODE character, so return a "reply"
  1725.      ;      status.
  1726.      ;
  1727.      moveq      #1,d0                   ; indicate reply
  1728.      ;
  1729.      ;      Restore and exit
  1730.      ;
  1731. 90$     movem.l  (sp)+,d2-d5/a1-a5              ; restore registers
  1732.      rts                          ; return (status in D0)
  1733.      ;
  1734.      ;      Align data
  1735.      ;
  1736.      CNOP      0,4
  1737.      ;
  1738.      ;
  1739.      ;
  1740. Init:
  1741.      DC.L      sizeof_Base8n1
  1742.      DC.L      funcTab
  1743.      DC.L      dataTab
  1744.      DC.L      InitRoutine
  1745.      ;
  1746.      ;
  1747.      ;
  1748. funcTab:
  1749.      DC.W      -1
  1750.      DC.W      dev_Open-funcTab
  1751.      DC.W      dev_Close-funcTab
  1752.      DC.W      dev_Expunge-funcTab
  1753.      DC.W      dev_Null-funcTab
  1754.      DC.W      dev_BeginIO-funcTab
  1755.      DC.W      dev_AbortIO-funcTab
  1756.      DC.W      -1
  1757.      ;
  1758.      ;
  1759.      ;
  1760. dataTab:
  1761.      INITBYTE LN_TYPE,NT_DEVICE
  1762.      INITLONG LN_NAME,Name
  1763.      INITBYTE LIB_FLAGS,LIBF_SUMUSED|LIBF_CHANGED
  1764.      INITWORD LIB_VERSION,VERSION
  1765.      INITWORD LIB_REVISION,REVISION
  1766.      INITLONG LIB_IDSTRING,IdString
  1767.      DC.W      0
  1768.      ;
  1769.      ;      String Constants
  1770.      ;
  1771. miscresource:
  1772.      DC.B      "misc.resource",0
  1773. timerdevice:
  1774.      DC.B      "timer.device",0
  1775. intuitlib:
  1776.      DC.B      "intuition.library",0
  1777. Name:
  1778.      DC.B      "8n1.device",0
  1779. IdString:
  1780.      VSTRING
  1781.      ;
  1782.      ;      End of checksummed area.  (Realigns data too!)
  1783.      ;
  1784. ENDTag:
  1785.      CNOP      0,4
  1786.      ;
  1787.      ;      Global SysBase (Use instead of AbsExecBase for speed)
  1788.      ;
  1789. SysBase:
  1790.      DC.L      0
  1791.      ;
  1792.      ;      Internal buffer tracking (DO NOT CHANGE THE ORDER!!!!)
  1793.      ;
  1794. i_BufPtr:
  1795.      DC.L      0
  1796. i_BufIn:
  1797.      DC.L      0
  1798. i_BufOut:
  1799.      DC.L      0
  1800. i_BufEnd:
  1801.      DC.L      0
  1802. i_InCnt:
  1803.      DC.L      0
  1804. i_Thresh:
  1805.      DC.L      0
  1806.      ;
  1807.      ;      Used while processing a read request.
  1808.      ;
  1809. cr_IOReq:
  1810.      DC.L      0
  1811. cr_OutPtr:
  1812.      DC.L      0
  1813. cr_Length:
  1814.      DC.L      0
  1815.      ;
  1816.      ;      List head for read requests
  1817.      ;
  1818. readQ:
  1819.      DC.L      readQ+MLH_TAIL
  1820.      DC.L      0
  1821.      DC.L      readQ
  1822.      ;
  1823.      ;      Write control.
  1824.      ;
  1825. cw_Length:
  1826.      DC.L      0
  1827. cw_Buffer:
  1828.      DC.L      0
  1829. cw_IOReq:
  1830.      DC.L      0
  1831.      ;
  1832.      ;      List head for write requests
  1833.      ;
  1834. writeQ:
  1835.      DC.L      writeQ+MLH_TAIL
  1836.      DC.L      0
  1837.      DC.L      writeQ
  1838.      ;
  1839.      ;
  1840.      ;
  1841. timerPort:
  1842.      DC.L      0                      ; LN_SUCC
  1843.      DC.L      0                      ; LN_PRED
  1844.      DC.B      NT_MSGPORT                  ; LN_TYPE
  1845.      DC.B      0                      ; LN_PRI
  1846.      DC.L      0                      ; LN_NAME
  1847.      DC.B      3                      ; MP_FLAGS (undoc'ed)
  1848.      DC.B      0                      ; MP_SIGBIT
  1849.      DC.L      timerRtn                  ; MP_SIGTASK
  1850.      DC.L      timerPort+MP_MSGLIST+LH_TAIL          ; LH_HEAD
  1851.      DC.L      0                      ; LH_TAIL
  1852.      DC.L      timerPort+MP_MSGLIST              ; LH_TAILPRED
  1853.      DC.B      0                      ; LH_TYPE
  1854.      DC.B      0                      ; LH_pad
  1855.      DC.W      0                      ; long align
  1856.      ;
  1857.      ;
  1858.      ;
  1859. timerReq:
  1860.      DC.L      0                      ; LN_SUCC
  1861.      DC.L      0                      ; LN_PRED
  1862.      DC.B      NT_MESSAGE                  ; LN_TYPE
  1863.      DC.B      0                      ; LN_PRI
  1864.      DC.L      0                      ; LN_NAME
  1865.      DC.L      timerPort                  ; MN_REPLYPORT
  1866.      DC.W      IOTV_SIZE                  ; MN_LENGTH
  1867.      DC.L      0                      ; IO_DEVICE
  1868.      DC.L      0                      ; IO_UNIT
  1869.      DC.W      TR_ADDREQUEST               ; IO_COMMAND
  1870.      DC.B      0                      ; IO_FLAGS
  1871.      DC.B      0                      ; IO_ERROR
  1872.      DC.L      0                      ; TV_SECS
  1873.      DC.L      0                      ; TV_MICROS
  1874.      ;
  1875.      ;
  1876.      ;
  1877. VBInterrupt:
  1878.      DC.L      0                      ; LN_SUCC
  1879.      DC.L      0                      ; LN_PRED
  1880.      DC.B      NT_INTERRUPT                  ; LN_TYPE
  1881.      DC.B      0                      ; LN_PRI
  1882.      DC.L      Name                      ; LN_NAME
  1883.      DC.L      readQ                   ; IS_DATA
  1884.      DC.L      level2                  ; IS_CODE
  1885.      ;
  1886.      ;      Global flags
  1887.      ;
  1888. Overrun:
  1889.      DC.B      0
  1890. Handshake:
  1891.      DC.B      1
  1892. disableRead:
  1893.      DC.B      -1
  1894.      ;
  1895.      ;
  1896.      ;
  1897.      END
  1898.  
  1899.