home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD2.img / d4xx / d483 / med / med.lzh / MED / Programmers / MODPlayer / loadmod.a < prev    next >
Text File  |  1991-05-08  |  3KB  |  137 lines

  1. ;    MED module load routines by Teijo Kinnunen 1990
  2. ;    V3.00 module support added 22-Jan-1991
  3.  
  4. ;    Function: d0 = _LoadModule(a0)
  5. ;    a0 = module name
  6. ;    d0 = pointer to loaded module, zero if load failed
  7.  
  8.     xdef    _LoadModule
  9.     xdef    _UnLoadModule
  10.     code
  11. _LoadModule:
  12.     movem.l a2-a4/a6/d2-d6,-(sp)
  13.     moveq    #0,d6            ;d6 = return value (zero = error)
  14.     move.l  a0,a4            ;a4 = module name
  15.     movea.l 4,a6
  16.     lea     dosname(pc),a1
  17.     jsr     -$198(a6)    ;OldOpenLibrary()
  18.     tst.l   d0
  19.     beq     xlm1
  20.     move.l  d0,a3            ;a3 = DOSBase
  21.     move.l  d0,a6
  22.     move.l  a4,d1            ;name = d1
  23.     move.l  #1005,d2        ;accessmode = MODE_OLDFILE
  24.     jsr     -$1e(a6)        ;Open()
  25.     move.l  d0,d4            ;d4 = file handle
  26.     beq     xlm2
  27.     move.l  d4,d1
  28.     moveq   #0,d2
  29.     moveq   #1,d3            ;OFFSET_END
  30.     jsr     -$42(a6)        ;Seek(fh,0,OFFSET_END)
  31.     move.l  d4,d1
  32.     moveq    #0,d3
  33.     not.l   d3            ;OFFSET_BEGINNING
  34.     jsr     -$42(a6)        ;Seek(fh,0,OFFSET_BEGINNING)
  35.     move.l  d0,d5            ;d5 = file size
  36.     movea.l 4,a6
  37.     moveq   #2,d1            ;get chip mem
  38.     jsr     -$c6(a6)        ;AllocMem()
  39.     tst.l   d0
  40.     beq.s   xlm3
  41.     move.l  d0,a2            ;a2 = pointer to module
  42.     move.l  d4,d1    ;file
  43.     move.l  d0,d2    ;buffer
  44.     move.l  d5,d3    ;length
  45.     move.l  a3,a6
  46.     jsr     -$2a(a6)        ;Read()
  47.     cmp.l   d5,d0
  48.     bne.s   xlm4            ;something wrong...
  49.     cmp.l   #'MMD0',(a2)
  50.     bne.s   xlm4            ;this is not a module!!!
  51.     movea.l a2,a0
  52.     bsr.s   _RelocModule
  53.     move.l  a2,d6        ;no error...
  54.     bra.s   xlm3
  55. xlm4:    move.l  a2,a1        ;error: free the memory
  56.     move.l  d5,d0
  57.     movea.l 4,a6
  58.     jsr     -$d2(a6)    ;FreeMem()
  59. xlm3:    move.l  a3,a6        ;close the file
  60.     move.l  d4,d1
  61.     jsr     -$24(a6)        ;Close(fhandle)
  62. xlm2:    move.l  a3,a1        ;close dos.library
  63.     movea.l 4,a6
  64.     jsr     -$19e(a6)
  65. xlm1:    move.l  d6,d0            ;push return value
  66.     movem.l (sp)+,a2-a4/a6/d2-d6    ;restore registers
  67.     rts                ;and exit...
  68. dosname:    dc.b    'dos.library',0
  69.  
  70. ;    Function: _RelocModule(a0)
  71. ;    a0 = pointer to module
  72. relocentr:
  73.     tst.l   (a0)
  74.     beq.s   norel
  75.     add.l   d1,(a0)
  76. norel:    addq.l  #4,a0
  77.     rts
  78. _RelocModule:
  79.     movem.l    a2-a3/d2-d3,-(sp)
  80.     movea.l a0,a2
  81.     move.l  a2,d1        ;d1 = ptr to start of module
  82.     lea     8(a2),a0
  83.     bsr.s   relocentr    ;reloc song ptr
  84.     addq.l  #4,a0
  85.     bsr.s   relocentr    ;reloc blockarr ptr
  86.     addq.l  #4,a0
  87.     bsr.s   relocentr    ;reloc smplarr ptr
  88.     addq.l  #4,a0
  89.     bsr.s   relocentr    ;reloc expdata ptr
  90.     movea.l 24(a2),a0
  91.     movea.l 8(a2),a1
  92.     moveq   #0,d0
  93.     move.b  787(a1),d0    ;number of samples
  94.     subq.b  #1,d0
  95. relocs:    bsr.s   relocentr
  96.     move.l    -4(a0),d3    ;sample ptr
  97.     beq.s    nosyn
  98.     move.l    d3,a3
  99.     tst.w    4(a3)
  100.     bpl.s    nosyn        ;type >= 0
  101.     move.w    20(a3),d2    ;number of waveforms
  102.     lea    278(a3),a3    ;ptr to wf ptrs
  103.     subq.w    #1,d2
  104. relsyn:    add.l    d3,(a3)+
  105.     dbf    d2,relsyn
  106. nosyn:    dbf     d0,relocs
  107.     movea.l 16(a2),a0
  108.     move.w  504(a1),d0
  109.     subq.b  #1,d0
  110. relocb:    bsr.s   relocentr
  111.     dbf     d0,relocb
  112.     move.l    32(a2),d0    ;extension struct
  113.     beq.s    rel_ex
  114.     move.l    d0,a0
  115.     addq.l    #4,a0        ;pass "reserved"
  116.     bsr.s    relocentr    ;InstrExt...
  117.     addq.l    #4,a0        ;pass sizes of InstrExt
  118.     bsr.s    relocentr    ;annotxt
  119. rel_ex    movem.l    (sp)+,d2-d3/a2-a3
  120.     rts
  121.  
  122.  
  123. ;    Function: _UnLoadModule(a0)
  124. ;    a0 = pointer to module
  125. _UnLoadModule:
  126.     move.l  a6,-(sp)
  127.     move.l  a0,d0
  128.     beq.w   xunl
  129.     movea.l 4,a6
  130.     move.l  4(a0),d0
  131.     beq.s   xunl
  132.     movea.l a0,a1
  133.     jsr     -$d2(a6)    ;FreeMem()
  134. xunl:    move.l    (sp)+,a6
  135.     rts
  136.     end
  137.