home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 325.lha / keys / ks.asm < prev    next >
Assembly Source File  |  1989-12-26  |  8KB  |  416 lines

  1. *------------------------------------------------------------------------------
  2. *
  3. *  ks.asm - startup code for k.asm
  4. *  
  5. *------------------------------------------------------------------------------
  6.  
  7.     Section    Startup_Trash,code
  8.  
  9. ******* Included Files *************************************************
  10.  
  11.     INCLUDE "keys.i"
  12.  
  13.  
  14. ******* Imported *******************************************************
  15.  
  16.     xref    IHandler    
  17.     xref    ConIOBlock
  18.     xref    InpIOBlock
  19.     xref    IntRec
  20.     xref    TheTitle
  21.     xref    DefinedKeys
  22.     xref    StringBuf
  23.     xref    AddKey
  24.     xref    FileReadChar
  25.     xref    _bopen
  26.     xref    _bclose
  27.     xref    _main
  28.  
  29.  
  30. ******* Exported *******************************************************
  31.  
  32.     xdef    _SysBase
  33.     xdef    _DOSBase
  34.     xdef    _IntuitionBase    
  35.     xdef    _exit
  36.  
  37.     xdef    KeyFileName        ; pointer to keyfilename to load.
  38.     xdef    NWPointer        ; pointer to NewWindow structure.
  39.     xdef    FreePort        ; a small town in Maine.
  40.  
  41. *------------------------------------------------------------------------------
  42. startup:
  43.     movem.l    a0/d0,-(sp)
  44.  
  45.     ;------ get Exec's library base pointer:
  46.     move.l    4,a6
  47.     move.l    a6,_SysBase
  48.  
  49.     ;------ Open the needed libraries
  50.     bsr    OpenLibs
  51.     beq    _abort0
  52.  
  53.     ;------ if a key filename was specified, fetch it.
  54.     movem.l    (sp)+,d0/a0
  55.     bsr    LoadKeyFile
  56.     beq    _abort1
  57.  
  58.     ;------ Allocate the NewWindow structure for later use...
  59.     bsr    AllocNewWindow
  60.     beq    _abort2
  61.  
  62.     ;------ Launch the process.
  63.     IFEQ    Debug
  64.     bsr    LaunchProc
  65.     beq    _abort3
  66.     clr.l    d0        ; gimme a zero return code
  67.     ENDC
  68.  
  69.     IFNE    Debug
  70.     jsr    _main
  71.     jmp    _exit
  72.     ENDC
  73.     rts
  74.  
  75. *------------------------------------------------------------------------------
  76. LoadKeyFile     
  77.  
  78.     ;------ if the user specified a key filename on the command line,
  79.     ;------ read in the file.
  80.     
  81.     ;------ skip leading whitespace.
  82. 0$    move.b    (a0)+,d1        ; Fetch a character
  83.     subq.l    #1,d0            ; Subtract Command string length
  84.     ble.s    8$            ; if zero, leave
  85.     cmp.b    #' ',d1            ; Skip spaces
  86.     ble.s    0$
  87.  
  88.     ;------ At this point, we have something on the command line. 
  89.     ;------ Copy it to StringBuf, and null-terminate it.
  90.     lea    -1(a0),a0
  91.     lea    StringBuf,a1
  92.  
  93. 2$    move.b    (a0)+,(a1)+
  94.     cmp.b    #' ',-1(a1)
  95.     bgt.s    2$
  96.  
  97.     clr.b    -1(a1)
  98.  
  99.     ;------ Attempt to open the file
  100.     move.l    #StringBuf,d1
  101.     move.l    #MODE_OLDFILE,d2
  102.     move.l    #2048,d3
  103.     jsr    _bopen
  104.     move.l    d0,d4
  105.     bne.s    4$
  106.  
  107.     rts
  108.     
  109. 4$    move.b    #' ',d5
  110.     lea    StringBuf,a2
  111.     bsr    FileReadItem
  112.     beq.s    6$
  113.  
  114.     jsr    AddKey
  115.     cmpa.l    #0,a0
  116.     beq.s    6$
  117.  
  118.     move.b    #10,d5
  119.     lea    kr_MacStr(a0),a2
  120.     bsr    FileReadItem
  121.     bra.s    4$
  122.  
  123. 6$    move.l    d4,d1
  124.     jsr    _bclose
  125. 8$    moveq    #1,d0
  126.     rts
  127.  
  128. *------------------------------------------------------------------------------
  129. FileReadItem
  130.     ;------ This routine will read either the key def or the macro
  131.     ;------ def from the file depending on the delimiter.
  132.  
  133.     clr.l    d6        ; Keep a character count.
  134.  
  135. 0$    jsr    FileReadChar
  136.     tst.l    d0
  137.     beq.s    4$
  138.  
  139.     cmp.b    #76,d6
  140.     bge.s    4$
  141.  
  142.     cmp.b    (a2),d5
  143.     beq.s    2$
  144.  
  145.     addq.l    #1,a2
  146.     addq.l    #1,d6
  147.     bra.s    0$
  148.     
  149. 2$    clr.b    (a2)
  150.     moveq    #1,d0
  151. 4$    rts
  152.  
  153.  
  154. *------------------------------------------------------------------------------
  155. OpenLibs    
  156.     ;------ Open dos.lib and intuition.lib
  157.  
  158.     lea    DOSName(pc),A1
  159.     clr.l    d0
  160.     ExecF    OpenLibrary
  161.     move.l    d0,_DOSBase
  162.     beq.s    0$
  163.     
  164.     lea    IntName(pc),A1
  165.     clr.l    d0
  166.     ExecF    OpenLibrary
  167.     move.l    d0,_IntuitionBase
  168. 0$    rts
  169.  
  170.  
  171. *------------------------------------------------------------------------------
  172. AllocNewWindow
  173.     ;------ Allocate a NewWindow structure and set up some of its fields.
  174.  
  175.     ;------ First, get some mem!
  176.     move.l    #nw_SIZE,d0
  177.     move.l    #(MEMF_PUBLIC!MEMF_CLEAR!MEMF_CHIP),d1
  178.     ExecF    AllocMem
  179.     
  180.     ;------ Make sure we got the memory
  181.     tst.l    d0
  182.     bne.s    0$
  183.     
  184.     rts            ; Return with error
  185.  
  186. 0$    move.l    d0,a0
  187.     
  188.     move.w    #640,nw_Width(a0)
  189.     move.w    #22,nw_Height(a0)
  190.     move.b    #1,nw_BlockPen(a0)
  191.     move.l    #(ACTIVATE!SMART_REFRESH),nw_Flags(a0)
  192.     move.l    #TheTitle,nw_Title(a0)
  193.     move.l    a0,NWPointer
  194.     rts
  195.     
  196.  
  197. *------------------------------------------------------------------------------
  198. LaunchProc
  199.     ;------ Patch the segment list, and create the process. We will leave 
  200.     ;------ d3 pointing the segment after this one. While we are at it, 
  201.     ;------ fetch a pointer to the command name into d1. This will become
  202.     ;------ the name of the process.
  203.     
  204.     move.l    ThisTask(a6),a0        ; Fetch a ptr to our Tcb.
  205.     move.l    pr_CLI(a0),a0        ; Fetch the bptr to the cli struct
  206.     adda.l    a0,a0
  207.     adda.l    a0,a0
  208.     move.l    cli_CommandName(a0),a1
  209.     add.l    a1,a1            ; bcpl pointer conversion
  210.     add.l    a1,a1
  211.  
  212.     ;------ Now copy the command name (which is a bstr) to cmdname,
  213.     ;------ and make it a C string in the process.
  214.     lea    cmdname(pc),a2
  215.     move.l    a2,d1            ; Save ptr in d1
  216.     clr.l    d0
  217.     move.b    (a1)+,d0        ; Get the string length
  218.     bra.s    2$
  219.  
  220. 0$    move.b    (a1)+,(a2)+
  221. 2$    dbf    d0,0$
  222.     
  223.     clr.b    (a2)            ; Null-terminate the string.
  224.  
  225.     move.l    cli_Module(a0),a0    ; Fetch the bptr to the seg-list.
  226.     adda.l    a0,a0
  227.     adda.l    a0,a0
  228.     move.l    (a0),d3            ; Fetch ptr to next segment.
  229.     clr.l    (a0)            ; Terminate the list.
  230.     
  231.     ;------ Save the new start of the segment list so we can unload
  232.     ;------ it later.
  233.     move.l    d3,StartSeg
  234.  
  235.     ;------ Create the process....
  236.     clr.l    d2            ; Priority of zero.
  237.     move.l    #2048,d4        ; Stack size of 2k bytes.
  238.  
  239.     DosF    CreateProc
  240.     tst.l    d0
  241.     rts
  242.  
  243.  
  244.  
  245. *------------------------------------------------------------------------------
  246. * _abortx
  247.     ;------ This routine is called if we encounter any errors before the
  248.     ;------ process is started. It will free all allocated resources and
  249.     ;------ return an error code.
  250.     
  251. _abort3
  252.     ;------ Cannot create process
  253.     add.l    #1,rc
  254.     jsr    FreeNewWindow
  255.  
  256. _abort2
  257.     ;------ Cannot allocate a NewWindow structure
  258.     add.l    #1,rc
  259.     jsr    KillKeys
  260.  
  261. _abort1
  262.     ;------ Cannot open specified key file.
  263.     add.l    #1,rc
  264.  
  265. _abort0
  266.     ;------ Cannot open librarie(s)
  267.     add.l    #1,rc
  268.     jsr    CloseLibs
  269.     move.l    rc,d0
  270.     rts
  271.  
  272.  
  273. cmdname        ds.b    34    
  274. rc        dc.l    100
  275. DOSName        dc.b    'dos.library',0
  276. IntName        dc.b    'intuition.library',0
  277.  
  278.  
  279. *------------------------------------------------------------------------------
  280. *
  281. *  This is the first segment of resident code.
  282. *
  283. *------------------------------------------------------------------------------
  284.     Section    KeyCode,code
  285.  
  286.     ;------ Go to the program's main entry point
  287.     bsr    _main
  288.     
  289. _exit:
  290.     bsr    KillKeys
  291.  
  292.     bsr    RemoveInputHandler
  293.     bsr    CloseInputDevice
  294.     
  295.     move.l    InpIOBlock,a0
  296.     bsr    FreeIOBlock
  297.  
  298.     move.l    ConIOBlock,a0
  299.     bsr    FreeIOBlock
  300.  
  301.     bsr    FreeNewWindow
  302.     
  303.     bsr    CloseLibs
  304.  
  305.     ;------ Unload the code.
  306.     move.l    StartSeg(pc),d1
  307.     DosF    UnLoadSeg
  308.  
  309.     clr.l    d0
  310.     rts
  311.  
  312. *------------------------------------------------------------------------------
  313. KillKeys
  314.     ;------ Delete the macro list.
  315.     lea.l    DefinedKeys(pc),a2
  316.     move.l    kr_Next(a2),a2
  317.  
  318. 0$    cmpa.l    #0,a2
  319.     beq.s    2$
  320.  
  321.     move.l    kr_Next(a2),a3
  322.     
  323.     move.l    a2,a1
  324.     move.l    #kr_SIZEOF,d0    
  325.     ExecF    FreeMem
  326.  
  327.     move.l    a3,a2
  328.     bra.s    0$
  329.  
  330. 2$    rts
  331.  
  332. *------------------------------------------------------------------------------
  333. RemoveInputHandler
  334.     ;------ Tell the input.device to stop sending us stuff.
  335.     move.l    InpIOBlock(pc),a1
  336.     move.w    #IND_REMHANDLER,IO_COMMAND(a1)
  337.     move.l    #IntRec,IO_DATA(a1)
  338.  
  339.     ;------ Goodbye handler!
  340.     ExecF    DoIO
  341.     rts
  342.     
  343.  
  344. *------------------------------------------------------------------------------
  345. CloseInputDevice
  346.     ;------ Adios device!
  347.     move.l    InpIOBlock(pc),a1
  348.     ExecF    CloseDevice
  349.     rts
  350.  
  351. *------------------------------------------------------------------------------
  352. FreePort
  353.     move.l    a0,a2
  354.  
  355.     move.b    #-1,LN_TYPE(a2)
  356.     move.l    #-1,LH_HEAD(a2)
  357.  
  358.     clr.l    d0
  359.     move.b    MP_SIGBIT(a2),d0
  360.     LibF    FreeSignal
  361.     
  362.     move.l    a2,a1
  363.     move.l    #MP_SIZE,d0    
  364.     LibF    FreeMem
  365.     rts
  366.  
  367. *------------------------------------------------------------------------------
  368. FreeIOBlock
  369.     move.l    a0,a3
  370.     move.l    MN_REPLYPORT(a3),a0
  371.     bsr    FreePort
  372.  
  373.     move.l    a3,a1
  374.     move.l    #IOSTD_SIZE,d0
  375.     ExecF    FreeMem
  376.     rts
  377.  
  378. *------------------------------------------------------------------------------
  379. FreeNewWindow
  380.     move.l    NWPointer(pc),a1
  381.     move.l    #nw_SIZE,d0
  382.     ExecF    FreeMem
  383.     rts
  384.  
  385.  
  386. *------------------------------------------------------------------------------
  387. CloseLibs
  388.     move.l    _DOSBase(pc),d0
  389.     beq.s    0$
  390.  
  391.     move.l    d0,a1
  392.     ExecF    CloseLibrary
  393.     
  394. 0$    move.l    _IntuitionBase(pc),d0
  395.     beq.s    2$
  396.  
  397.     move.l    d0,a1
  398.     ExecF    CloseLibrary
  399.  
  400. 2$    rts
  401.  
  402.  
  403.  
  404.  
  405. _SysBase     dc.l    0
  406. _DOSBase    dc.l    0
  407. _IntuitionBase    ds.l    1    
  408.  
  409. StartSeg    ds.l    1        ; Pointer to procs first segment.
  410. KeyFileName    ds.l    1        ; Pointer to keyfilename to load.
  411. NWPointer    ds.l    1        ; Pointer to NewWindow structure.
  412.    END
  413.  
  414.  
  415.  
  416.