home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff312.lzh / TrackSalve / Source / switch.a < prev    next >
Text File  |  1990-02-12  |  9KB  |  228 lines

  1. *  Switch.a  --  TrackSalve
  2. *
  3. *   #    date    by    Comment
  4. *  -- --------- ---- ---------------------
  5. *   0 29-Oct-89  DWR   Created for Tracksalve
  6. *
  7.  
  8. *               nolist
  9.                 INCLUDE "Call.i"
  10.                 INCLUDE "ts.i"
  11.                 INCLUDE "exec/tasks.i"
  12.                 INCLUDE "exec/resident.i"
  13. *               list
  14.  
  15.  
  16.  
  17.                 CSECT   text,code,0,1,2                 Any xref's after this are 16-bit reloc
  18.  
  19. *********************************************************
  20. *
  21. *  PCtoTS
  22. *
  23. *  We need to catch Trackdisk in its highest task-loop.  Otherwise we could lose control
  24. *  after an rts.  This loop is rather small:  7 instuctions.  One of the instructions is a
  25. *  jsr _LVOWait(a6). Trackdisk is waiting here for a timer or an IORequest and will be here
  26. *  most of the time.  If we change the return address to our code, Trackdisk smoothly enters
  27. *  our code.  We change this address only if TD is at this point.
  28. *
  29. *  Input:       a0      UnitData structure
  30. *
  31. *  Output:      d0      0 if caught else E_CATCHTD
  32. *
  33. *  Usage:       d2      PC of TD-task at the point where we want to change it
  34. *               d6      Timeout counter
  35. *               d7      Error
  36. *
  37. *               a2      TSControl structure
  38. *               a3      UnitData structure
  39. *               a5      Task stucture of Trackdisk unit task
  40. *
  41. *  NB!  We are accessing the TSControl structure in a way that needs protection.
  42. *
  43.  
  44.                 Func    PCtoTS,@PCtoTS
  45.                 movem.l d2/d3/d6/d7/a2/a3/a5,-(a7)
  46.  
  47.                 moveq.l #0,d7                           No error for now
  48.                 move.l  a0,a3                           UnitData structure
  49.  
  50.                 move.l  UD_TDReq+IO_UNIT(a3),a5         Lea Unit structure of the Trackdisk IORequest
  51.                 move.b  TDU_UnitNr(a5),d3               Get unit number (to set InUse flag later)
  52.                 move.l  MP_SIGTASK(a5),a5               Lea Task control block associated with this port
  53.                 move.l  UD_TSControl(a3),a2             Control structure
  54.                 move.l  TSC_TDTag(a2),d2                Begin of TD code
  55.                 add.l   #$1592,d2                       Add offset of desired return address
  56.                 moveq.l #25,d6                          We put a limit here, so we will not hang forever if TD is dead
  57.  
  58. CheckPC         ExCall  Forbid                          Prevent TD to become active
  59.                 move.l  TC_SPUPPER(a5),a0               Initial stackpointer of TD
  60.                 cmp.l   WaitRtnPC(a0),d2                Is TD on the desired spot?
  61.                 beq.s   ChangePC                        Yes, change the return address..
  62.                 LibCall Permit                          No, give TD a chance to change its situation
  63.                 moveq.l #10,d1                          We will wait for a fifth of a second
  64.                 DOSCall Delay                           Sleeping
  65.                 dbra    d6,CheckPC                      And try again..
  66.                 moveq.l #E_CATCHTD,d7                   But not forever, so set error
  67.                 bra.s   PCtoTSRtn                       Quit..
  68.  
  69. ChangePC        move.l  d2,UD_ReturnTD(a3)              Save original TD address
  70.                 move.l  TSC_TSCode(a2),WaitRtnPC(a0)    Change return addres to adress in our code
  71.                 move.l  TC_Userdata(a5),UD_Userdata(a3) Keep original value (although TD does not use it)
  72.                 move.l  a3,TC_Userdata(a5)              Make UnitData structure known to TD-task
  73.                 bset.b  d3,TSC_InUse(a2)                Set flag to prevent freeing the TSControl structure
  74.                 LibCall Permit                          Multitasking on again
  75.  
  76. PCtoTSRtn       move.l  d7,d0                           Error
  77.                 movem.l (a7)+,d2/d3/d6/d7/a2/a3/a5
  78.                 rts
  79.  
  80.  
  81.  
  82. *********************************************************
  83. *
  84. *  RelocateTS
  85. *
  86. *  Modify position dependend values in copied TSCode
  87. *
  88. *  Input:       a0      TSControl structure
  89. *
  90.  
  91.                 Func    RelocateTS,@RelocateTS
  92.                 move.l  TSC_TSCode(a0),a0
  93.                 lea.l   TSCodeRelocTable(pc),a1
  94. 10$             move.w  (a1)+,d0
  95.                 beq.s   20$
  96.                 move.l  a0,d1
  97.                 add.l   0(a0,d0.w),d1
  98.                 move.l  d1,0(a0,d0.w)
  99.                 bra.s   10$
  100. 20$             rts
  101.  
  102.                 xref    TSCodeRelocTable
  103.  
  104.  
  105. *********************************************************
  106. *
  107. *  RelocateTD
  108. *
  109. *  Modify position dependend values in relocated Trackdisk code versions 33.127 and 34.1
  110. *
  111. *  Input:       a0      TSControl structure
  112. *
  113.  
  114.                 ifd     __stdargs
  115.                 Func    _RelocateTD
  116.                 move.l  4(a7),a0
  117.                 endc
  118.  
  119.                 Func    RelocateTD,@RelocateTD
  120.                 move.l  TSC_TSTag(a0),a1                Begin of copy of Trackdisk code
  121.                 move.l  TSC_TDTag(a0),d0                Begin of Trackdisk code (in ROM)
  122. *               cmp.l   a1,RT_MATCHTAG(a1)              Does our copy has its self-pointer right?
  123. *               beq.s   RelocateRtn                     Yes, already relocated..
  124.                 sub.l   a1,d0                           TD org minus TD copy = relocation value
  125.                 lea.l   RelocTable(pc),a0               Table of relative addresses to relocate
  126.                 bra.s   Relocate20                      Enter loop..
  127. Relocate10      sub.l   d0,0(a1,d1.w)                   Only Amiga makes it possible!
  128. Relocate20      move.w  (a0)+,d1                        Get offset in TD-code
  129.                 bne.s   Relocate10
  130. RelocateRtn     rts
  131.  
  132.  
  133.  
  134. *--     You can obtain this table by comparing version 1.2 and 1.3 and do some editing on the result
  135.  
  136. RelocTable      dc.w    $0002
  137.                 dc.w    $0006
  138.                 dc.w    $000E
  139.                 dc.w    $0012
  140.                 dc.w    $0016
  141.                 dc.w    $0324
  142.                 dc.w    $032A
  143.                 dc.w    $03C0
  144.                 dc.w    $091C
  145.                 dc.w    $0940
  146.                 dc.w    $0966
  147.                 dc.w    $0980
  148.                 dc.w    $0994
  149.                 dc.w    $09B6
  150.                 dc.w    $09C4
  151.                 dc.w    $09CC
  152.                 dc.w    $0A00
  153.                 dc.w    $0A04
  154.                 dc.w    $0A08
  155.                 dc.w    $0A0C
  156.                 dc.w    $0A10
  157.                 dc.w    $0A14
  158.                 dc.w    $0A1C
  159.                 dc.w    $0A20
  160.                 dc.w    $0A24
  161.                 dc.w    $0A28
  162.                 dc.w    $0A2C
  163.                 dc.w    $0A30
  164.                 dc.w    $0A34
  165.                 dc.w    $0A38
  166.                 dc.w    $0A3C
  167.                 dc.w    $0A40
  168.                 dc.w    $0A44
  169.                 dc.w    $0A48
  170.                 dc.w    $0A4C
  171.                 dc.w    $0A50
  172.                 dc.w    $0A54
  173.                 dc.w    $0A58
  174.                 dc.w    $0A5C
  175.                 dc.w    $0A60
  176.                 dc.w    $0A64
  177.                 dc.w    $0A68
  178.                 dc.w    $0A6C
  179.                 dc.w    $0A70
  180.                 dc.w    $127E
  181.                 dc.w    $12EA
  182.                 dc.w    $1316
  183.                 dc.w    $1352
  184.                 dc.w    $1386
  185.                 dc.w    $13E4
  186.                 dc.w    $1446
  187.                 dc.w    $1690
  188.                 dc.w    $16B8
  189.                 dc.w    $195A
  190.                 dc.w    $19A2
  191.                 dc.w    $19B2
  192.                 dc.w    0
  193.  
  194.  
  195.  
  196. *********************************************************
  197. *
  198. *  PatchMem
  199. *
  200. *  Change memory according to a table
  201. *
  202. *  Input:       a0      Memory to change
  203. *               a1      Patchtable  (Offset.w, Size.w, Patch.w[Size])
  204. *
  205.  
  206.                 Func    PatchMem,@PatchMem
  207.                 movem.l a2,-(a7)
  208.                 move.l  a0,a2                           Lea begin of memory
  209.  
  210. GetNextPatch    move.l  a2,a0                           Begin of memory
  211.                 move.w  (a1)+,d0                        Get offset in code to put patch
  212.                 add.w   d0,a0                           Lea destination of patch
  213.                 move.w  (a1)+,d1                        Get size of patch in words
  214.                 beq.s   PatchMemRtn                     No patch, done..
  215.                 bra.s   CopyPatch10                     Enter copyloop
  216. CopyPatch       move.w  (a1)+,(a0)+                     Copy word of code
  217. CopyPatch10     dbra    d1,CopyPatch                    Loop ..
  218.                 bra.s   GetNextPatch                    Examine next entry in Patchtable
  219.  
  220. PatchMemRtn     movem.l (a7)+,a2
  221.                 rts
  222.  
  223.  
  224. *********************************************************
  225.  
  226.                 END
  227.  
  228.