home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / source / ovs.a < prev    next >
Encoding:
Text File  |  1996-12-24  |  12.3 KB  |  325 lines

  1.         TTL     AmigaDOS 68000 overlay supervisor
  2. **********************************************************
  3. *                                                        *
  4. * This version of the overlay supervisor is callable     *
  5. * from C or Assembler; it will not work for MCC Pascal   *
  6. * or BCPL which use a calling sequence where the called  *
  7. * routine's address is saved on the stack. A different   *
  8. * version of the overlay supervisor is available for     *
  9. * case.                                                  *
  10. *                                                        *
  11. * The main overlay algorithm used here was originally    *
  12. * described by Richard Evans at Cambridge University     *
  13. * in about 1979. This code implementation was done by    *
  14. * Tim King of Metacomco.                                 *
  15. *                                                        *
  16. * Two main data structures are used:                     *
  17. *                                                        *
  18. *   1. The hunk table                                    *
  19. *                                                        *
  20. *        This table, giving the base addresses of all    *
  21. *          hunks currently loaded, is manufactured by    *
  22. *          the loader.  Its address is planted in HTAB,  *
  23. *          and its first word is the address of hunk 0.  *
  24. *          Note that the addresses it contains are those *
  25. *          of the link word of each hunk - not the first *
  26. *          data word.                                    *
  27. *                                                        *
  28. *   2. The overlay table                                 *
  29. *                                                        *
  30. *        This table is manufactured by the linkage       *
  31. *          editor, and its address is planted in OVTAB   *
  32. *          by the loader.   It consists of two main      *
  33. *          parts, the ordinate table, and the overlay    *
  34. *          data table.  The former is a table which is   *
  35. *          used to remember the ordinate number of each  *
  36. *          segment that is currently loaded. If n is the *
  37. *          maximum level number, then the table contains *
  38. *          n+1 entries, and the first word is n+2. The   *
  39. *          extra entry is required because a scan        *
  40. *          through the table below is terminated by a    *
  41. *          zero entry.  All the entries are initially    *
  42. *          cleared by the linkage editor.  The address   *
  43. *          of the overlay table, plus the contents of    *
  44. *          its first word (n+2), gives the address of    *
  45. *          the overlay data table, which is indexed by   *
  46. *          overlay number.  Each entry in this table     *
  47. *          contains the following information, not       *
  48. *          necessarily in this order:                    *
  49. *                                                        *
  50. *            1. A file mark, generated by 'note', giving *
  51. *                the position of the segment to be       *
  52. *                loaded.                                 *
  53. *            2. The overlay level of the segment.        *
  54. *            3. The overlay ordinate of the segment.     *
  55. *            4. The initial hunk number of the segment.  *
  56. *            5. The number of the hunk containing the    *
  57. *                symbol.                                 *
  58. *            6. The offset of the symbol in the hunk.    *
  59. *                                                        *
  60. * If an error is detected during overlaying, there is a  *
  61. * limited choice of sensible actions, since the overlay  *
  62. * supervisor can make no assumptions concerning the      *
  63. * environment it was entered from.  The action on an     *
  64. * error is to call an Alert with the code for invalid    *
  65. * overlay request.                                       *
  66. *                                                        *
  67. * Note that the value in HTAB is a long word address,    *
  68. * whilst that in OVTAB is a machine address.  Each entry *
  69. * in the table is four bytes long.                       *
  70. *                                                        *
  71. **********************************************************
  72. *  Change History:
  73. *
  74. *       CARL 05SEP85
  75. *            1) Changes made (suggested by Paul) to the handling
  76. *               of seek and loadseg errors.  When an error
  77. *               occurs, the program will retry until it is
  78. *               successful.
  79. *            2) Changed bra.s to just bra in OVENT and OVENT2
  80. *               macro definitions.
  81. *            3) Added the OVERLAYS equate and the necessary OVENT2
  82. *               table entries (conditionally assembled).
  83. *            4) Eliminated unnecessary include files.
  84. *       CARL 06SEP85
  85. *            1) CALL macro modified.
  86. *            2) CALL use modified.
  87. *       DTM  15JUN87
  88. *            1) modified to either define or use static OCVs, or
  89. *               Overlay Call Vectors, as we now call them.
  90. *            2) changed entry name to "_ovlyMgr" (note that the
  91. *               underscore is added automatically, and case IS
  92. *               very important
  93. *            3) added instruction to make sure that only lower
  94. *               16 bits of d0 are used in indexing into table;
  95. *               the new dynamic OCVs only pass a word in d0,
  96. *               although it's still multiplied by 8 and > 0.
  97. *            4) Basically all of this increase the total number
  98. *               of OCVs to 8k, and furthermore makes it
  99. *               possible to put this code into a library
  100.  
  101.         IFD     NEWOCV
  102.     IDNT    newovs
  103.         xdef    @ovlyMgr
  104.         ELSE
  105.     IDNT    ovs
  106.         xdef    ovlyMgr
  107.         ENDC
  108.  
  109.  
  110.         section NTRYHUNK,CODE
  111.  
  112.         INCLUDE "exec/types.i"
  113.         INCLUDE "exec/tasks.i"
  114.         INCLUDE "exec/libraries.i"
  115.         INCLUDE "exec/alerts.i"
  116.         INCLUDE "libraries/dos.i"
  117.  
  118.         
  119.  
  120. FUNCDEF         MACRO   * function
  121. _LVO\1          EQU     FUNC_CNT
  122. FUNC_CNT        SET     FUNC_CNT-6
  123.                 ENDM
  124.  
  125. FUNC_CNT        SET     LIB_NONSTD
  126.  
  127.         INCLUDE "exec/exec_lib.i"
  128.         INCLUDE "libraries/dos_lib.i"
  129.  
  130.  
  131. * Offsets within the overlay table.
  132. SysBase EQU         4
  133.  
  134. OTMARK  EQU         0                   File position
  135. OTLEVL  EQU        12                   Level
  136. OTORD   EQU        16                   Ordinate
  137. OTIHNK  EQU        20                   Initial hunk for load
  138. OTRHNK  EQU        24                   Hunk containing symbol
  139. OTOFF   EQU        28                   Offset of symbol
  140. LIBWORD EQU        23456
  141.  
  142.  
  143. * A macro to call a LIBRARY routine:
  144. CALLS   MACRO
  145.         MOVEA.L    SysBase.w,A6
  146.         JSR        _LVO\1(A6)
  147.         ENDM
  148.  
  149. * Call a library, save and restore A6.
  150. CALL    MACRO      ; CALL <library_vector_offset>,<library_pointer>
  151.         MOVE.L     A6,-(SP)
  152.         MOVE.L     \2,A6
  153.         JSR        _LVO\1(A6)
  154.         MOVE.L     (SP)+,A6
  155.         ENDM
  156.  
  157.  
  158. * Now for the first word of the program.
  159.  
  160. FIRST   BRA.W NextModule
  161.  
  162. * This next word serves to identify the overlay
  163. * supervisor to 'unloader'.
  164.  
  165.         DC.L       $ABCD                Special value
  166.  
  167.  
  168. * The loader plants values in the next locations.
  169.  
  170. STREAM  DC.L       0                    Overlay input stream
  171. OVTAB   DC.L       0                    Overlay table (Machine address)
  172. HTAB    DC.L       0                    Hunk table    (BCPL address)
  173. GLBVEC  DC.L       0                    Global vector (Machine address)
  174.  
  175.  
  176. *
  177. * The main code of the program.
  178. *
  179.         CNOP       0,4
  180.         DC.L       LIBWORD
  181.         DC.B       7,'Overlay'
  182.  
  183.       IFD        NEWOCV
  184. @ovlyMgr
  185. OV1     movem.l    a0-a6/d0-d7,-(sp)
  186.       ELSE
  187. ovlyMgr
  188. OV1     MOVEM.L    A2-A7/D2-D7,-(SP)
  189.       ENDC          
  190.         MOVE.L     OVTAB(PC),A3         Address of overlay table
  191.         MOVEA.L    A3,A4
  192.       IFD        NEWOCV
  193.         moveq.l    #0,d0
  194.         move.l     4*15(a7),a0
  195.         move.w     (a0),d0
  196.       ELSE
  197.         AND.L      #$0FFFF,D0           Make SURE that upper word is ZERO
  198.       ENDC
  199.         ADD.L      (A3),D0              Add value in first word of table
  200.         LSL.L      #2,D0
  201.         ADDA.L     D0,A3                Address of overlay entry
  202.         MOVE.L     OTLEVL(A3),D0        Get required level
  203.         LSL.L      #2,D0
  204.         ADDA.L     D0,A4                Pointer to entry in ordinate table
  205.         MOVE.L     OTORD(A3),D0         Get required ordinate
  206.         CMP.L      (A4),D0              Compare with actual
  207.         BEQ        GOTSEG               Branch if no load required
  208.  
  209. * Now clear other entries in the ordinate table
  210.  
  211.         MOVE.L     D0,(A4)+
  212. OV2     TST.L      (A4)                 Table terminated by zero
  213.         BEQ.S      OV3
  214.         CLR.L      (A4)+
  215.         BRA.S      OV2
  216. *
  217. OV3     MOVE.L     OTIHNK(A3),D0        First hunk number of overlay segment
  218.         ADD.L      HTAB(PC),D0
  219.         LSL.L      #2,D0                Address of entry in hunk table
  220.         MOVEA.L    D0,A4
  221.         MOVE.L     -4(A4),D0            Previous hunk
  222.         BEQ.S      OV5
  223.         LSL.L      #2,D0
  224.         MOVEA.L    D0,A6
  225.         CLR.L      (A6)                 Clear link field in case of error
  226.  
  227. * Now free all the unwanted hunks ...
  228.  
  229. OV4     TST.L      (A4)                 Scan is terminated by zero
  230.         BEQ        OV5
  231.         MOVE.L     (A4)+,D1             Address as argument for 'freevec'
  232. FREEVEC   TST.L      D1
  233.           BEQ.S      FVC1             Return if V = 0
  234.           ASL.L      #2,D1            Back to mc address
  235.           MOVEA.L    D1,A1
  236.           MOVE.L     -(A1),D0         D0 = size
  237.           MOVE.L     D0,D1            Take copy
  238.           ANDI.L     #$FF000003,D1    Top byte and bottom 2 bits..
  239.           BNE        OVERR            .. must be clear to be valid length
  240.           CALL       FreeMem,SysBase.w
  241. FVC1      BRA.S      OV4              and again
  242.  
  243. * Now save CIS and point the file
  244.  
  245. OV5
  246.         LEA.L   LIBNAME(PC),A1
  247.         MOVEQ   #0,D0
  248.         CALL    OpenLibrary,SysBase.w
  249.         MOVE.L  D0,D7
  250.         BEQ.S   OVERR
  251.  
  252.         ADDA.L     #OTMARK,A3           Address of file mark
  253.  
  254. * Now seek to the segment and load it.  If we fail (probably because
  255. * the user removed the disk, then retry until successful.  This has
  256. * the effect of continually reprompting the user to insert the disk
  257. * until he obeys.
  258.  
  259. OV_RETRY:
  260.         MOVE.L     STREAM(PC),D1            New stream
  261.         MOVE.L     (A3),D2
  262.         MOVEQ      #OFFSET_BEGINNING,D3 Offset to be from beginning
  263.         CALL       Seek,D7                 Call 'point(scb,mark,o)'
  264.         TST.L      D0                   Check result
  265.         BMI.S      OV_RETRY             Branch if error
  266.  
  267.         ; Now call the loader again:
  268.         MOVE.L     HTAB(PC),D2          Hunk table (second parameter)
  269.         MOVEQ      #0,D1                Zero parameter for overlay
  270.         MOVE.L     STREAM(PC),D3
  271.         CALL       LoadSeg,D7
  272.         TST.L      D0                   Check result
  273.         BMI.S      OV_RETRY             Branch if error
  274.  
  275. * Add new list to chain after previous one
  276.  
  277.         MOVE.L     D1,(A6)              Add new chain
  278.  
  279. * Here when segment is in store: A3 holds table entry, A1 and D0 are free
  280.  
  281. GOTSEG  MOVE.L     OTRHNK(A3),D0        Number of hunk containing symbol
  282.         ADD.L      HTAB(PC),D0
  283.         LSL.L      #2,D0                Machine address of entry in hunk table
  284.         MOVEA.L    D0,A4
  285.         MOVE.L     (A4),D0              BCPL address of hunk
  286.         LSL.L      #2,D0
  287.         ADD.L      OTOFF(A3),D0         Add in offset of symbol
  288.  
  289.       IFD      NEWOCV
  290. * The address to jump to is now in D0. Place it on the stack so the RTS uses it.
  291.         move.l     d0,15*4(a7)
  292.         movem.l    (sp)+,a0-a6/d0-d7    * restore regs
  293.         RTS                             * Jump to new code
  294.       ELSE
  295. * The address to jump to is now in D0. Place it in A1 and jump to it.
  296.         MOVEA.L    D0,A1
  297.         MOVEM.L    (SP)+,A2-A7/D2-D7    restore regs
  298.         JMP        (A1)                 Jump to new code
  299.       ENDC
  300.       
  301. * Come here on an error during the overlay
  302.  
  303. OVERR 
  304.         MOVE.L #AN_BadOverlay,D7
  305.         CALLS  Alert
  306.         RTS
  307.  
  308. * The code here will cause us to jump to the next segment in the list,
  309. * which should be the intended entry point.
  310.  
  311. NextModule
  312.         LEA.L      FIRST(PC),A3         Pointer to start of module
  313.         MOVE.L     -4(A3),D7            Next BCPL word ptr
  314.         ASL.L      #2,D7                MC ptr to next module
  315.         MOVE.L     D7,A3                Into address register
  316.         JMP        4(A3)                Tally ho!
  317.  
  318. * Data Area
  319.  
  320. LIBNAME DC.B       'dos.library',0
  321.         CNOP       0,4
  322.  
  323.  
  324.         END
  325.