home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / dev / asm / tbsource / iocontrol / ic.s
Text File  |  1993-12-21  |  8KB  |  434 lines

  1. *************************************************
  2. *                        *
  3. *         (C)opyright 1993        *
  4. *                        *
  5. *          Tomi Blinnikka        *
  6. *                        *
  7. *    Don't try to understand the code    *
  8. *                        *
  9. * Version 1.00    14/07/1993            *
  10. *                        *
  11. * BUGS:                        *
  12. *                        *
  13. * Version 1.01    17/07/1993            *
  14. *                        *
  15. * Renamed from CompleteControl to IOControl.    *
  16. * CompleteControl is now the intuition part    *
  17. * of the control system.            *
  18. *                        *
  19. * BUGS:    Missed a NULL from fstrl1.        *
  20. *                        *
  21. * Version 1.02    18/07/1993            *
  22. *                        *
  23. * BUGS:                        *
  24. *                        *
  25. *************************************************
  26.  
  27.  
  28.     INCLUDE    "JMPLibs.i"
  29.     INCLUDE    "libraries/dos.i"
  30.     INCLUDE    "exec/devices.i"
  31.     INCLUDE    "devices/serial.i"
  32.  
  33.     INCLUDE    "XREF:2.0.xref"
  34.     INCLUDE    "XREF:exec.xref"
  35.     INCLUDE    "XREF:dos.xref"
  36.  
  37. PROGVERSION:    macro
  38.         dc.b    "1.02 (18.07.93)"
  39.         endm
  40.  
  41. ;Port definitions
  42.  
  43. MIN_PORT:    EQU    1        ;Code supports ports 1-9
  44. MAX_PORT:    EQU    2
  45.  
  46. PORT_A:        EQU    0        ;Code supports ports A, B, C
  47. PORT_B:        EQU    1
  48. PORT_C:        EQU    2
  49.  
  50. MIN_STATUS:    EQU    0        ;Code supports values 0-255
  51. MAX_STATUS:    EQU    255
  52.  
  53. ;Rawkey codes
  54.  
  55. RAW_ENTER:    EQU    $43
  56. RAW_RETURN:    EQU    $44
  57. RAW_A:        EQU    $20
  58. RAW_B:        EQU    $35
  59. RAW_C:        EQU    $33
  60.  
  61. RAW_0:        EQU    $0a
  62. RAW_1:        EQU    $01
  63. RAW_2:        EQU    $02
  64. RAW_3:        EQU    $03
  65. RAW_4:        EQU    $04
  66. RAW_5:        EQU    $05
  67. RAW_6:        EQU    $06
  68. RAW_7:        EQU    $07
  69. RAW_8:        EQU    $08
  70. RAW_9:        EQU    $09
  71.  
  72. ;Other stuff
  73.  
  74. DELAY_AMOUNT:    EQU    6
  75.  
  76.         section    IOControl,CODE
  77.  
  78.         push    d2-d7/a2-a6
  79.  
  80.         sub.l    a1,a1            ;Find our task
  81.         lib    Exec,FindTask
  82.         move.l    d0,OurTask
  83.  
  84.         openlib    Dos,NoDos
  85.  
  86.         lib    Dos,Output
  87.         move.l    d0,_stdout
  88.  
  89.         lea.l    CLTemplate1,a0
  90.         move.l    a0,d1
  91.         lea.l    CLArray1,a0
  92.         move.l    a0,d2
  93.         clr.l    d3
  94.         lib    Dos,ReadArgs
  95.         move.l    d0,RDArgs1
  96.         beq    NoRDArgs
  97.  
  98. DoParsing:    tst.l    UnitPointer
  99.         beq    DoParsing1
  100.         move.l    UnitPointer,a0
  101.         move.l    (a0),SerUnit
  102. DoParsing1:    tst.l    SpeedPointer
  103.         beq    DoParsing2
  104.         move.l    SpeedPointer,a0
  105.         move.l    (a0),Speed
  106. DoParsing2:    tst.l    PortNumPointer
  107.         beq    BadNumber
  108.         move.l    PortNumPointer,a0
  109.         move.l    (a0),d0            ;Valid range = 1-2
  110.         cmp.l    #MIN_PORT,d0
  111.         bcs    BadNumber
  112.         cmp.l    #MAX_PORT,d0
  113.         bhi    BadNumber
  114.         move.b    d0,PortNum
  115. DoParsing3:    tst.l    PortABC
  116.         beq    BadTemplate
  117.         move.l    PortABC,a0
  118.         cmp.b    #'a',(a0)
  119.         beq    DoParsing3.1
  120.         cmp.b    #'A',(a0)
  121.         bne    DoParsing3.2
  122. DoParsing3.1:    move.b    #PORT_A,Port
  123.         bra    DoParsing4
  124.  
  125. DoParsing3.2:    cmp.b    #'b',(a0)
  126.         beq    DoParsing3.3
  127.         cmp.b    #'B',(a0)
  128.         bne    DoParsing3.4
  129. DoParsing3.3:    move.b    #PORT_B,Port
  130.         bra    DoParsing4
  131.  
  132. DoParsing3.4:    cmp.b    #'c',(a0)
  133.         beq    DoParsing3.5
  134.         cmp.b    #'C',(a0)
  135.         bne    BadTemplate
  136. DoParsing3.5:    move.b    #PORT_C,Port
  137.  
  138. DoParsing4:    tst.l    StatusPointer
  139.         beq    DoParsing5
  140.         move.l    StatusPointer,a0
  141.         move.l    (a0),d0            ;Valid range = 0-255
  142.         cmp.l    #MIN_STATUS,d0
  143.         bcs    BadNumber
  144.         cmp.l    #MAX_STATUS,d0
  145.         bhi    BadNumber
  146.         move.w    d0,Status
  147. DoParsing5:
  148.  
  149. ;Create reply port for serial.device (or modem0.device etc.)
  150.  
  151.         lib    Exec,CreateMsgPort
  152.         move.l    d0,SWPort
  153.         beq    NoMsgPort
  154.  
  155. ;Create IOReq for serial.device (or for other device, but size is EXTSER)
  156.  
  157.         move.l    #IOEXTSER_SIZE,d0
  158.         move.l    SWPort,a0
  159.         clr.b    LN_TYPE(a0)        ;Make sure CheckIO doesn't hang
  160.         lib    Exec,CreateIORequest
  161.         move.l    d0,IORequest
  162.         beq    NoIOReq
  163.  
  164. ;open serial.device
  165.  
  166.         move.l    SerName,a0
  167.         move.l    SerUnit,d0
  168.         move.l    IORequest,a1
  169.         move.b    #SERF_SHARED,IO_SERFLAGS(a1)
  170.         clr.l    d1                ;no flags
  171.         lib    Exec,OpenDevice
  172.         tst.l    d0
  173.         bne    NoSerial
  174.         move.w    #$1,SerOpen        ;just to tell if open
  175.  
  176.         tst.l    Speed
  177.         beq    SkipSetParams
  178.         move.l    IORequest,a1
  179.         clr.l    IO_LENGTH(a1)
  180.         clr.l    IO_DATA(a1)
  181.         move.w    #SDCMD_SETPARAMS,IO_COMMAND(a1)
  182.         move.l    Speed,IO_BAUD(a1)
  183.         lib    Exec,DoIO
  184.         tst.l    d0
  185.         beq    SkipSetParams
  186.         bsr    NoSetSer
  187.  
  188. SkipSetParams:    bsr    DoPortNum        ;Write port number
  189.         bsr    DoDelay            ;Wait for AT to act
  190.         bsr    DoPort            ;Write port A, B or C
  191.         bsr    DoStatus        ;Write status number
  192.  
  193.         bra    ShutDown
  194.  
  195. DoDelay:    move.l    #DELAY_AMOUNT,d1
  196.         lib    Dos,Delay
  197.         rts
  198.  
  199. DoPortNum:    move.b    PortNum,Buffer1
  200.         bsr    SendOneChar
  201.         rts
  202.  
  203. DoPort:        cmp.b    #PORT_A,Port
  204.         bne    DoPort2
  205.         move.b    #RAW_A,Buffer1
  206.         bra    DoPort_OUT
  207. DoPort2:    cmp.b    #PORT_B,Port
  208.         bne    DoPort3
  209.         move.b    #RAW_B,Buffer1
  210.         bra    DoPort_OUT
  211. DoPort3:    cmp.b    #PORT_C,Port
  212.         bne    DoPort_OUT
  213.         move.b    #RAW_C,Buffer1
  214. DoPort_OUT:    bsr    SendOneChar
  215.         rts
  216.  
  217. DoStatus:    lea.l    fstrl1,a0        ;HEX->ASCII
  218.         lea.l    Status,a1        ;Number2Print
  219.         lea.l    PutChProc,a2
  220.         lea.l    ASCIINumber1,a3        ;Destination
  221.         lib    Exec,RawDoFmt
  222.  
  223.         lea.l    ASCIINumber1,a0
  224. DoStatus1:    push    a0
  225.         bsr    DoDelay
  226.         pull    a0
  227.         tst.b    (a0)
  228.         beq    DoStatus_OUT
  229.         cmp.b    #'0',(a0)
  230.         beq    DoZero
  231.         sub.b    #$30,(a0)
  232.         move.b    (a0),Buffer1
  233.         bsr    SendOneChar2
  234.         add.l    #1,a0
  235.         bra    DoStatus1
  236.  
  237. DoZero:        move.b    #RAW_0,Buffer1
  238.         bsr    SendOneChar2
  239.         add.l    #1,a0
  240.         bra    DoStatus1
  241.  
  242. DoStatus_OUT:    move.b    #RAW_RETURN,Buffer1
  243.         bsr    SendOneChar2
  244.         rts
  245.  
  246.  
  247. SendOneChar:    move.b    #RAW_RETURN,Buffer1+1
  248.         clr.b    Buffer1+2
  249.         bsr    Writer
  250.         rts
  251.  
  252. ;Saves a0
  253.  
  254. SendOneChar2:    push    a0
  255.         clr.b    Buffer1+1
  256.         bsr    Writer
  257.         pull    a0
  258.         rts
  259.  
  260. ShutDown:    tst.w    SerOpen
  261.         beq    ShutDown9000
  262.         bsr    ClearSer
  263.         move.l    IORequest,a1
  264.         lib    Exec,CloseDevice
  265.  
  266. ShutDown9000:    move.l    IORequest,a0
  267.         lib    Exec,DeleteIORequest
  268.  
  269.         move.l    SWPort,a0
  270.         lib    Exec,DeleteMsgPort
  271.  
  272. ShutDown3000:    move.l    RDArgs1,d1
  273.         beq    ShutDown2000
  274.         lib    Dos,FreeArgs
  275.  
  276. ShutDown2000:
  277.  
  278. ShutDown1000:    closlib Dos
  279.  
  280.         pull    d2-d7/a2-a6
  281.         clr.l    d0
  282.         rts
  283.  
  284. ClearSer:    clr.w    Buffer1
  285.         move.l    IORequest,a1
  286.         lib    Exec,CheckIO
  287.         tst.l    d0
  288.         bne    ClearSer_OUT
  289.         move.l    IORequest,a1
  290.         lib    Exec,WaitIO
  291.         move.l    IORequest,a1
  292.         ABORTIO
  293. ClearSer_OUT:    rts
  294.  
  295. Writer:        lea.l    Buffer1,a1
  296.         bsr    GetLength
  297.         move.l    IORequest,a1
  298.         move.w    #CMD_WRITE,IO_COMMAND(a1)
  299.         move.l    d0,IO_LENGTH(a1)
  300.         lea.l    Buffer1,a0
  301.         move.l    a0,IO_DATA(a1)
  302.         lib    Exec,DoIO
  303.         rts
  304.  
  305. ;Get length of text in given address
  306. ;
  307. ;Input a1 = Address of null terminated text string
  308. ;
  309. ;Result d0 = Length
  310.  
  311. GetLength:    clr.l    d0
  312.         cmp.l    #$00,a1        ;fixes enforcer hit
  313.         beq    GetLength_OUT
  314. GetLength2:    add.l    #1,d0
  315.         tst.b    (a1)+
  316.         bne    GetLength2
  317.         sub.l    #1,d0        ;don't include NULL
  318. GetLength_OUT:    rts
  319.  
  320. ;Error etc. messages
  321.  
  322. NoDos:        pull    d2-d7/a2-a6
  323.         move.l    #RETURN_FAIL,d0
  324.         rts
  325.  
  326. NoRDArgs:    lib    Dos,IoErr
  327.         move.l    d0,d1
  328.         clr.l    d2
  329.         lib    Dos,PrintFault
  330.         bra    ShutDown
  331.  
  332. BadNumber:    move.l    #ERROR_BAD_NUMBER,d1
  333.         clr.l    d2
  334.         lib    Dos,PrintFault
  335.         bra    ShutDown
  336.  
  337. BadTemplate:    move.l    #ERROR_BAD_TEMPLATE,d1
  338.         clr.l    d2
  339.         lib    Dos,PrintFault
  340.         bra    ShutDown
  341.  
  342. NoMsgPort:    lea.l    NoMsgPortText1,a0
  343.         bsr    Printer
  344.         bra    ShutDown
  345.  
  346. NoIOReq:    lea.l    NoIOReqText1,a0
  347.         bsr    Printer
  348.         bra    ShutDown
  349.  
  350. NoSerial:    lea.l    NoSerialText1,a0
  351.         bsr    Printer
  352.         move.l    SerName,a0
  353.         bsr    Printer
  354.         lea.l    NoSerialText2,a0
  355.         bsr    Printer
  356.         bra    ShutDown
  357.  
  358. NoSetSer:    lea.l    NoSetSerText1,a0
  359.         bsr    Printer
  360.         rts
  361.  
  362. Printer:    printa    a0
  363.         rts
  364.  
  365. PutChProc:    tst.b    d0
  366.         beq    PutChProc_OUT
  367.         move.b    d0,(a3)+
  368. PutChProc_OUT:    rts
  369.  
  370. ;Library stuff
  371.  
  372.         libnames
  373.  
  374. ;Reservations
  375.  
  376.  
  377. ;Options
  378.  
  379. CLArray1:
  380. SerName:    dc.l    SerName2    ;A pointer!
  381. UnitPointer:    dc.l    0
  382. SpeedPointer:    dc.l    0
  383. PortNumPointer:    dc.l    0
  384. PortABC:    dc.l    0
  385. StatusPointer:    dc.l    0
  386.  
  387. ;Serial device stuff
  388.  
  389. SerUnit:    dc.l    0
  390. Speed:        dc.l    0
  391. SerOpen:    dc.w    0
  392.  
  393. ;I/O-card stuff
  394.  
  395. PortNum:    dc.b    0        ;Port 1 or 2
  396. Port:        dc.b    0        ;0=A, 1=B, 2=C
  397. Status:        dc.w    0
  398.  
  399. ;Other stuff, part I
  400.  
  401. OurTask:    dc.l    0
  402. RDArgs1:    dc.l    0
  403. _stdout:    dc.l    0
  404. SWPort:        dc.l    0
  405. IORequest:    dc.l    0
  406.  
  407. ;Strings, error
  408.  
  409. NoMsgPortText1:    dc.b    "ERROR: Couldn't get message port!",13,10,0
  410. NoIOReqText1:    dc.b    "ERROR: Couldn't get SerialIOReq!",13,10,0
  411. NoSetSerText1:    dc.b    "ERROR: Couldn't set parameters for device!",13,10,0
  412. NoSerialText1:    dc.b    "ERROR: Couldn't open ",0
  413. SerName2:    dc.b    "serial.device",0
  414. NoSerialText2:    dc.b    13,10,0
  415.  
  416. ;Strings, names
  417.  
  418. CLTemplate1:    dc.b    "DEV=DEVICE/K,UNIT/K/N,SPEED/K/N,PN=PORTNUM/K/N/A,PORT/K/A,ST=STATUS/K/N/A",0
  419. CCVersion:    dc.b    "$VER: IOControl "
  420.         PROGVERSION
  421.         dc.b    " (c) Copyright Tomi Blinnikka 1993",0
  422. fstrl1:        dc.b    "%d",0
  423.         ds.w    0
  424.  
  425.  
  426. ;Other stuff, part II
  427.  
  428. ;Buffer
  429.  
  430. Buffer1:    dcb.b    4,0
  431.  
  432. ASCIINumber1:    dcb.b    12,0
  433.         end
  434.