home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / z80mr-a.lbr / PHASE.DZC / PHASE.DOC
Encoding:
Text File  |  1988-04-05  |  10.3 KB  |  272 lines

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