home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / zsys / znode-12 / s / z80mr.lbr / PHASE.DZC / PHASE.DOC
Encoding:
Text File  |  1993-06-12  |  8.5 KB  |  262 lines

  1.  
  2. PHASED code
  3.  
  4. One of the limitations of this assembler is the lack of a .PHASE directive.
  5. This directive causes the assembler to generate addresses for a different
  6. section of memory for labels than the actual place the code is to be
  7. loaded.  This is important for the Kaypro since when the ROM is called, the
  8. lower 32K of memory is no longer available. Simply ORGing at higher
  9. location later in the program and jumping there will cause the entire area
  10. of memory between 100H and the end point of the program to be saved
  11. resulting in a huge .COM file when loaded with LOAD.COM.
  12.  
  13. There are many ways to phase code and still end up with a reasonable sized
  14. .COM file. Here I will present two of the most common methods.
  15.  
  16. Label+OFFSET Method
  17.  
  18. In order for code to be assembled in one area to run in another, our only
  19. concern is how the addresses are calculated by the assembler. Normally, an
  20. assembler sets a location counter when it sees an ORG pseudo-op. As it
  21. produces each byte of the it increments the location counter to calculate
  22. the next address. If it finds a label, it sets the label's address
  23. according to this location counter. The programmer has another method of
  24. setting the address of labels, with EQU. If every label in the program
  25. takes the form of Label+OFFSET where the offset is a constant, then the
  26. assembler will produce the code to run in high memory while creating a file
  27. that will load in low memory. The following short program which re-loads
  28. your monitor ROM from the ROM to RAM illustrates this.
  29.  
  30. ;**************************************************************************
  31. ;**    Rom Save Program                                                 **
  32. ;**    Run and then enter SAVE 16 ROM.COM                               **
  33. ;**    ROM.COM will contain object code of your monitor ROM             **
  34. ;**************************************************************************
  35.  
  36.     ORG    100H
  37.  
  38. TRUE    EQU    0FFH
  39. FALSE    EQU    0
  40.  
  41. OLD    EQU    TRUE        ; TRUE IF NOT 10 OR 4-84
  42.  
  43.     IF    OLD
  44. SYSPRT    EQU    1CH        ; old Kaypro 2's and fours
  45.     ELSE
  46. SYSPRT    EQU    14H        ; tens, 4-84's, 2-84's
  47.     ENDIF
  48.  
  49. OFFSET    EQU    8000h        ; offset for calculating high addresses
  50.  
  51.     LD    DE,HISTRT    ; the destination of the High memory code
  52.     LD    HL,LODEND+1    ; the source code is just beyond this loader
  53.     LD    BC,HIEND-HISTRT    ; the number of bytes to move (end - start)
  54.     LDIR            ; move it up there
  55.     JP    HISTRT        ; jump to it
  56. LODEND:    NOP            ; to calculate end of loader/start of high
  57.                 ; memory code segment
  58.  
  59. HISTRT    EQU    $+OFFSET    ; BEGIN USING OFFSET    
  60.  
  61.     IN    A,(SYSPRT)    ; get present sysport data
  62.     SET    7,A        ; bank select bit
  63.     OUT    (SYSPRT),A
  64.     CALL    MOVIT        ; move the code routine
  65.     IN    A,(SYSPRT)
  66.     RES    7,A        ; back to ram bank
  67.     OUT    (SYSPRT),A
  68.     CALL    TPAMOV        ; now move to 100H for save
  69.     JP    0        ; back to CP/M
  70.  
  71. MOVIT    EQU    $+OFFSET
  72.  
  73.     LD    HL,0        ; Source is at 0 (in ROM )
  74.     LD    DE,OFFSET+1000H    ; Load above us
  75.     LD    BC,1000h    ; pick up 4K (2732)
  76.     LDIR
  77.     RET
  78.  
  79. TPAMOV    EQU    $+OFFSET
  80.  
  81.     LD    HL,OFFSET+1000H    ; destination becomes source
  82.     LD    DE,100H        ; move to TPA start
  83.     LD    BC,1000H    ; 4K bytes to move
  84.     LDIR
  85.     RET
  86.  
  87. HIEND    EQU    $+OFFSET    ; end of code to be relocated
  88.  
  89.     END
  90.  
  91. Assemble this program (the source is in the library as PHASE1.SRC so you
  92. don't have to type it in). Examine the listing file. Notice that the
  93. assembler generated high memory addresses though the program loads low.
  94.  
  95. Using DDT.COM
  96.  
  97. With this method you would split the above program into two parts, the
  98. loader and the code that is to be relocated. You can assemble the loader
  99. and pick an arbitrary source address for the code to be relocated (say
  100. 200h).  Then you can assemble the relocatable portion with a high ORG.
  101. (8000H say).  Now you can join the two HEX files together with DDT.COM
  102. reading in the high portion with an OFFSET. To get the offset use DDT's Hex
  103. sum and difference command in the form of:
  104.  
  105. H<desired load address>,<ORG address>
  106.  
  107. The second number will be the OFFSET. The program we wrote above would go
  108. together like this.
  109.  
  110.  
  111. ;**************************************************************************
  112. ;** Loader.azm             Loads relocating code to its destination     ***
  113. ;**************************************************************************
  114.  
  115.     ORG    100H
  116.  
  117. BYTCNT    EQU    100H        ; we can either supply a value here that we
  118.                 ; know will load enough code or assemble
  119.                 ; the high code first and let the assembler
  120.                 ; give us this value (examine print file)
  121.  
  122.  
  123. HISTRT    EQU    8000H        ; where the relocation code goes
  124. HILOAD    EQU    200H        ; where the other file goes
  125.  
  126.     LD    DE,HISTRT    ; the destination of the High memory code
  127.     LD    HL,HILOAD    ;the source code is just beyond this loader
  128.     LD    BC,BYTCNT    ;the number of bytes to move (end - start)
  129.     LDIR            ; move it up there
  130.     JP    HISTRT        ; jump to it
  131.                 ; memory code segment
  132.  
  133.     END
  134.  
  135.  
  136. ;**************************************************************************
  137. ;** Phase2.azm                                                          ***
  138. ;** Relocate with DDT                                                   ***
  139. ;**************************************************************************
  140.  
  141. TRUE    EQU    0FFH
  142. FALSE    EQU    0
  143.  
  144. OLD    EQU    FALSE
  145.  
  146.     IF    OLD
  147. SYSPRT    EQU    1CH        ; old kaypro 2's and fours
  148.     ELSE
  149. SYSPRT    EQU    14H        ; tens, 4-84's, 2-84's
  150.     ENDIF
  151.  
  152. BYTCNT    EQU    HIEND-HISTRT
  153.  
  154.     ORG    8000h    
  155.  
  156. HISTRT:    IN    A,(SYSPRT)    ; get present sysport data
  157.     SET    7,A        ; bank select bit
  158.     OUT    (SYSPRT),A
  159.     CALL    MOVIT        ; move the code routine
  160.     IN    A,(SYSPRT)
  161.     RES    7,A        ; back to ram bank
  162.     OUT    (SYSPRT),A
  163.     CALL    TPAMOV        ; now move to 100H for save
  164.     JP    0        ; back to CP/M
  165.  
  166. MOVIT:    LD    HL,0        ; Source is at 0 (in ROM )
  167.     LD    DE,9000H    ; load above us
  168.     LD    BC,1000h    ; pick up 4K (2732)
  169.     LDIR
  170.     RET
  171.  
  172. TPAMOV:    LD    HL,9000H    ;destination becomes source
  173.     LD    DE,100H        ; move to TPA start
  174.     LD    BC,1000H    ; 4K bytes to move
  175.     LDIR
  176.     RET
  177.  
  178. HIEND    EQU    $
  179.  
  180.     END
  181.  
  182.  
  183. Now we can use DDT to join the two files.
  184.  
  185. DDT LOADER.HEX
  186. H200,8000
  187.  
  188. 8200 8200        ; in this case both numbers are the same (we want
  189.             ; second
  190. IPHASE2.HEX        ; prepare to load file
  191. R8200            ; read in with offset
  192. ^C            ; exit to CPM
  193.  
  194. SAVE 1 PHASE3.COM    ; and the com file is created
  195.  
  196.  
  197.             
  198. ORGing High ( CP/M modifications ).........................................
  199.  
  200. DDT.COM can be used to load .HEX files anywhere in memory despite where the
  201. load point (ORG) was set. It does this by reading the file in with a
  202. negative offset with the R command. Usually CP/M is modified by saving the
  203. SYSGEN image and then overlaying the image with the modified section and re-
  204. SYSGENing.
  205.  
  206. Let's say you wrote a new BIOS for your Kaypro. The BIOS for the old Kaypros
  207. ORGed at FA00H. You assemble it with the assembler with the ORG at FA00H. 
  208. The object file is KBIOS.HEX
  209.  
  210. If you want to overlay the present bios with your new bios immediately to
  211. see if it works it is done as follows:
  212.  
  213. Enter DDT by entering
  214.  
  215.     DDT <carriage return>
  216.     IKBIOS.HEX        ; this sets up DDT for a file read
  217.     R            ; actually reads the file in and overlays
  218.                 ; your old bios with the binary code
  219.     ^C            ; returns you to CPM and if all is well
  220.                 ; with your new bios you will warm boot
  221.  
  222. Now you want a permanent copy of your new bios on your system tracks. Once
  223. done you will be able to copy your new system onto any disk with SYSGEN. 
  224. SYSGEN copies the system into memory but not at the same place the system
  225. runs. The BIOS image actually begins at 1F80H in the SYSGEN image. We must
  226. read the file in at 1F80H even though it ORGs at FA00H. We can do this with
  227. DDT also. First we must save the SYSGEN image to a file.
  228.  
  229. sysgen
  230. KAYPRO SYSGEN V2.2
  231. SOURCE DRIVE (OR RETURN TO SKIP)a    ; get the system from A: 
  232. SOURCE ON A THEN RETURN            ; enter a carriage return
  233. DESTINATION DRIVE (OR RETURN TO REBOOT)    ; enter a carriage return
  234.  
  235. save 40 cpm.com                ; save it to a file    
  236.  
  237. Now we use DDT to work on it
  238.  
  239.     ddt cpm.com <carriage return>
  240.     h1f80,fa00        ; we ask for the sum and difference
  241.                 ; of desired address - load address
  242.                 ; DDT prints this. The second number is the
  243.                 ; offset
  244.     ikbios.hex
  245.     r
  246.     ^C
  247.  
  248. Now when we return to CP/M we run sysgen to save the new image to system
  249. tracks.
  250.  
  251. SYSGEN <carriage return>
  252. KAYPPRO SYSGEN V2.2
  253. SOURCE DRIVE (OR RETURN TO SKIP)       ;enter return (use the memory image)
  254. DESTINATION DRIVE (OR RETURN TO REBOOT)  ;we will put it on B 
  255. DESTINATION ON B THEN RETURN         ; another return
  256. FUNCTION COMPLETE
  257. DESTINATION DRIVE (OR RETURN TO REBOOT)  ; one more return
  258.  
  259. Now when when we boot with the disk in drive B the new system will be loaded.
  260.  VE (OR RETURN TO REBOOT)  ; one more return
  261.  
  262. Now when when we boot with the disk in drive B the new s