home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / jsage / znode3 / util / trim.lbr / TRIM.ZZ0 / TRIM.Z80
Encoding:
Text File  |  1993-06-09  |  6.5 KB  |  234 lines

  1. ;Program: TRIM
  2. ;Version: 1.0
  3. ;Author: Bruce Morgen
  4. ;Date: June 25, 1988
  5.  
  6. ;Purpose: To truncate COM files at a requested address, used to
  7. ;      delete unneeded DSEG from the output of linkers like
  8. ;      DRI's LINK, L80, ZLINK, etc.  Replaces the following
  9. ;      ZEX script, adding DU:/DIR: response and much speed:
  10. ;
  11. ;        ^$ ? ?
  12. ;        a0:ddtz $1.com
  13. ;        k100 $2
  14. ;        y
  15. ;        g0
  16.  
  17. ;NOTE:    Should be linked to a high address (8000h or so).  TRIM
  18. ;    is totally useless at a standard-TPA tool (although it
  19. ;    could readily be re-written as one)!  ZCPR 3.3 or later
  20. ;    (or BGii 1.13 or later) is therefore required.
  21.  
  22. CR    EQU    13        ; ASCII
  23. LF    EQU    10        ;   "
  24. BEL    EQU    7        ;   "
  25. BDOSE    EQU    0005H        ; DOS entry vector
  26. PRNSTR    EQU    9        ; Print string function #
  27. CLOSEF    EQU    16        ; Close file      "     "
  28. TPA    EQU    100H        ; Normal Transient Program Area
  29. FCB1    EQU    05CH        ; Default FCB #1
  30. FCB2    EQU    06CH        ;    "     "  "2
  31. Z3ENV    EQU    00        ; Dummy value for Z3 environment
  32.  
  33.     PUBLIC    COUT        ; Make SYSLIB use ours
  34.  
  35.     EXTRN    Z3LOG,EPRINT,EPSTR,BOUT
  36.     EXTRN    PFN2,EVAL16,PUTUD,GETUD
  37.     EXTRN    F$OPEN,F$READ,F$DELETE
  38.     EXTRN    F$MOPEN,F$WRITE,SETDMA
  39.     EXTRN    FILLBC,INITFCB
  40.  
  41. ; TYPE 3 HEADER
  42.  
  43. ; Code modified as suggested by Charles Irvine to function correctly with
  44. ; interrupts enabled.  Program will abort with an error message when not
  45. ; loaded to the correct address (attempt to run it under CP/M or Z30).
  46.  
  47. ENTRY:
  48.     JR    START0        ; Must use relative jump
  49.     NOP            ; Filler
  50.     DB    'Z3ENV',3    ; Type-3 environment
  51. Z3EADR:
  52.     DW    Z3ENV        ; Filled in by Z33
  53.     DW    ENTRY        ; Intended load address
  54.  
  55. START0:
  56.     LD    HL,0        ; Point to warmboot entry
  57.     LD    A,(HL)        ; Save the byte there
  58.     DI            ; Protect against interrupts
  59.     LD    (HL),0C9H    ; Replace warmboot with a return opcode
  60.     RST    0        ; Call address 0, pushing RETADDR
  61.                 ; Onto stack
  62. RETADDR:
  63.     LD    (HL),A        ; Restore byte at 0
  64.     DEC    SP        ; Get stack pointer to point
  65.     DEC    SP        ; To the value of RETADDR
  66.     POP    HL        ; Get it into HL and restore stack
  67.     EI            ; We can allow interrupts again
  68.     LD    DE,RETADDR    ; This is where we should be
  69.     XOR    A        ; Clear carry flag
  70.     PUSH    HL        ; Save address again
  71.     SBC    HL,DE        ; Subtract -- we should have 0 now
  72.     POP    HL        ; Restore value of RETADDR
  73.     JR    Z,START        ; If addresses matched, begin real code
  74.  
  75.     LD    DE,NOTZ33MSG-RETADDR ; Offset to message
  76.     ADD    HL,DE
  77.     EX    DE,HL        ; Switch pointer to message into DE
  78. HLPEXT:    LD    C,PRNSTR
  79.     JP    BDOSE        ; Return via BDOS print string function
  80. NOTZ33MSG:
  81.     DB    'Not Z33+$'    ; Abort message if not Z33-compatible
  82.  
  83. START:    LD    A,(FCB1+1)    ; Point to first token
  84.     CP    '/'        ; Help query?
  85.     JR    Z,HELP        ; Then honor it
  86.     CP    ' '        ; Blank?
  87.     JR    NZ,NOHELP    ; No?  Proceed...
  88. HELP:    LD    DE,HLP        ; Otherwise point to help screen
  89.     JR    HLPEXT        ; BDOS-print and go home...
  90. ; CPR stack more than suffices, we need only 14 or so levels...
  91. NOHELP:    CALL    PUTUD        ; Save entry drive & user
  92.     LD    HL,FCB2+1    ; Point to second token
  93.     CALL    EVAL16        ; Compute to binary in DE, A = E
  94.     OR    D        ; Test for a zero result
  95.     JP    Z,NOZERO    ; Call that an error
  96.     LD    (HL),0        ; Null-terminate for printing
  97.     LD    HL,ENTRY    ; Get our origin
  98.     SBC    HL,DE        ; Subtract requested address
  99.     JP    C,TOOBIG    ; Request can't be past ENTRY
  100.     JP    Z,TOOBIG    ; Or right at it
  101.     LD    (SIZE),DE    ; Otherwise save it away
  102.     LD    HL,FCB1+9    ; Point to filename extension
  103.     LD    A,(HL)        ; Check for a blank one
  104.     CP    ' '
  105.     JR    NZ,GOTTYP    ; Assume "COM" if it's blank
  106.     LD    (HL),'C'
  107.     INC    HL
  108.     LD    (HL),'O'
  109.     INC    HL
  110.     LD    (HL),'M'
  111. GOTTYP:    CALL    EPRINT        ; Announce our function
  112.     DB    'Trimming ',0
  113.     LD    DE,FCB1+1    ; Point at and print filename
  114.     CALL    PFN2
  115.     CALL    EPRINT        ; Preface end address
  116.     DB    ' to 0',0
  117.     LD    HL,FCB2        ; Find first non-'0' digit
  118. FINDA0:    INC    HL
  119.     LD    A,(HL)
  120.     CP    '0'+1
  121.     JR    C,FINDA0
  122.     CALL    EPSTR        ; Print end address
  123.     LD    A,'h'        ; Say it's a hex number
  124.     CALL    BOUT
  125.     DEC    DE        ; Point to FCB
  126.     CALL    Z3LOG        ; Log in as per Z3 parse
  127.     CALL    INITFCB        ; Initialize for BDOS
  128.     CALL    F$OPEN        ; Open 'er up
  129.     JP    NZ,OPNERR    ; A = 0 and Z if no BDOS error
  130.     LD    HL,TPA        ; Point to TPA
  131.     LD    BC,ENTRY-TPA
  132.     CALL    FILLBC        ; Zero-fill BC bytes, saving HL
  133.     CALL    EPRINT        ; Announce next process, "   "
  134.     DB    CR,LF,'Reading ',0
  135.     JR    RDSTRT        ; Jump into read loop
  136. RDLOOP:    PUSH    HL        ; Save new DMA pointer
  137.     LD    BC,(SIZE)    ; Get end address
  138.     XOR    A        ; Clears carry flag
  139.     SBC    HL,BC        ; Subtract
  140.     POP    HL        ; Get back new DMA pointer
  141.     JR    NC,DODEL    ; Read done if bigger than end
  142. RDSTRT:    CALL    SETDMA        ; Set DMA address
  143.     CALL    DOT        ; Print a dot
  144.     LD    BC,128        ; Offset to next DMA address
  145.     ADD    HL,BC        ; Add it in for next loop around
  146.     CALL    F$READ        ; Read a record, saving regs.
  147.     OR    A        ; Zero means good read
  148.     JR    Z,RDLOOP
  149.     DEC    A        ; One means done reading file
  150.     JR    NZ,READERR    ; Anything else is a DOS snafu
  151.     CALL    DOT        ; Last dot...
  152. DODEL:    CALL    F$DELETE    ; Kill old file
  153.     CALL    INITFCB        ; Re-init FCB
  154.     CALL    F$MOPEN        ; Make new file and open it
  155.     JR    NZ,OPNERR    ; Branch on fatal error
  156.     CALL    EPRINT        ; Announce next process
  157.     DB    CR,LF,'Writing ',0
  158.     LD    HL,TPA        ; Start DMA at the bottom
  159. WRTLOOP:
  160.     CALL    SETDMA        ; Set DMA
  161.     CALL    DOT        ; DOT's right...
  162.     CALL    F$WRITE        ; Write a record
  163.     OR    A        ; Need a zero here
  164.     JR    NZ,WRITERR    ; or it's a DOS failure
  165.     LD    BC,128        ; Offset to next DMA pointer
  166.     ADD    HL,BC        ; Add it in for loop
  167.     PUSH    HL        ; Test it against end address
  168.     LD    BC,(SIZE)
  169.     XOR    A
  170.     SBC    HL,BC
  171.     POP    HL
  172.     JR    C,WRTLOOP    ; Loop if it's smaller
  173. DONE:    LD    C,CLOSEF    ; BDOS function #16
  174.     CALL    BDOSE        ; Call BDOS Bros.
  175.     INC    A        ; Test for FFh
  176.     JR    Z,CLSERR    ; That's an error
  177.     CALL    EPRINT        ; Announce success
  178.     DB    BEL,CR,LF,'Done!',0
  179. EXIT:    JP    GETUD        ; Reassert home drive & user,
  180.                 ; exeunt all...
  181.  
  182. DOT:    LD    A,'.'        ; Get a dot
  183. COUT:    JP    BOUT        ; Print via BDOS
  184.  
  185. ; ERROR EXIT ROUTINE
  186. ;
  187. NOZERO:    LD    HL,NZSTRG
  188.     JR    ERROR
  189. ;
  190. TOOBIG:    LD    HL,TBSTRG
  191.     JR    ERROR
  192. ;
  193. OPNERR:    LD    HL,OPNSTR
  194.     JR    ERROR
  195. ;
  196. READERR:LD    HL,READSTRG
  197.     JR    ERROR
  198. ;
  199. WRITERR:LD    HL,WRTSTRG
  200.     JR    ERROR
  201. ;
  202. CLSERR:    LD    HL,CLSSTR
  203. ;
  204. ERROR:    CALL    EPRINT        ; Say it's an error
  205.     DB    BEL,CR,LF,'Fatal error: ',0
  206.     CALL    EPSTR        ; Specify
  207.     JR    EXIT        ; Go home
  208. ;
  209. NZSTRG:    DB    'Zero-length requested, use SAVE',0
  210. ;
  211. TBSTRG:    DB    'Can''t create a file that large',0
  212. ;
  213. OPNSTR:    DB    'DOS failed to open file',0
  214. ;
  215. READSTRG:
  216.     DB    'DOS attempted to read unwritten data',0
  217. ;
  218. WRTSTRG:DB    'DOS failed to write',0
  219. ;
  220. CLSSTR:    DB    'DOS can''t close file',0
  221.  
  222. ; HELP SCREEN
  223. HLP:    DB    'TRIM, Version 1.0',CR,LF
  224.     DB    'Syntax:',CR,LF
  225.     DB    ' TRIM FILENAME HEXADDR',CR,LF
  226.     DB    'Saves a copy of "FILENAME" with',CR,LF
  227.     DB    'a highest address of "HEXADDR"',CR,LF,'$'
  228.  
  229. ; DATA AREA
  230.     DSEG            ; I know, why bother...
  231. SIZE:    DS    2        ; Requested end address
  232.  
  233.     END
  234.