home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / jsage / znode3 / uploads / zf11src.lbr / ZFSUBS1.ZZ0 / ZFSUBS1.Z80
Encoding:
Text File  |  1992-10-01  |  9.6 KB  |  437 lines

  1. .printx    Reading ZFSUBS1.Z80
  2. ;===========================================================================
  3. ;
  4. ; ZFSUBS1.Z80 - File Loading and Sorting and Ring Maintenance Routines
  5. ;
  6. ;===========================================================================
  7.  
  8.  
  9. ; FILELOAD loads the files into the buffer, setting up the ring
  10. ;    Return:    Z if load OK,
  11. ;        NZ if no files loaded
  12.  
  13. fileload:
  14.     call    getdpb        ; Get dpb values for f, gf, s commands
  15.     ld    hl,(ring)    ; Start --> current position of ring
  16.     ld    (ringpos),hl    ; Initialize ring pointers
  17.     ld    (ringend),hl    ; So ring is empty.
  18.     ld    (bufstart),hl    ; Preset now in case ring is empty
  19.     ld    hl,0        ; No files found (so far)
  20.     ld    (ringcnt),hl
  21.  
  22.     ld    a,1        ; 1 = get system mask.
  23.     call    filemask    ; Get system file spec.
  24.  
  25. ; Build ring with filename positioned in default FCB area
  26.  
  27.     xor    a        ; Clear search 'fcb'..
  28.     ld    (fcbext),a    ; Extent byte..
  29.     ld    (fcbrno),a    ; And record number.
  30.     ld    de,fcb        ; Default fcb for search
  31.     ld    c,srchf        ; Of first occurrence.
  32.     call    bdosptr
  33.     inc    a        ; 0ffh --> 00h if no file found
  34.     jr    nz,setring    ; If found, branch and build ring.
  35. noload:
  36.     inc    a        ; Indicate no files loaded.
  37.     ld    (canflg),a    ; Return non-zero for error.
  38.     ret
  39.  
  40.  
  41. ;---------------------------------------------------------------------------
  42.  
  43. ; RINGEMPT - Test ring empty, Return Zero if no files
  44.  
  45. ringempt:
  46.     push    hl        ; Save HL
  47.     ld    hl,(ringcnt)    ; Get the count
  48.     ld    a,h
  49.     or    l
  50.     pop    hl        ; Restore HL
  51.     ret
  52.  
  53. ;---------------------------------------------------------------------------
  54.  
  55. ; SETRING - Establish ring (circular list) of filenames
  56. ;     - put each found name in ring.
  57. ;     - A = offset into 'TBUFF' name storage
  58.  
  59. setring:
  60.     dec    a        ; Un-do 'inr' from above and below
  61.     rrca
  62.     rrca
  63.     rrca            ; Effectively A*32
  64.     add    a,tbuff        ; Add page offset and..
  65.     ld    l,a        ; Put address into hl.
  66.     ld    h,0
  67.  
  68.      if    remote
  69.     call    getwhl        ; If wheel is off, never show SYS files
  70.     jr    z,noshow
  71.      endif    ;remote
  72.  
  73.     ld    a,(nosysa)    ; Show $SYS files?
  74.     or    a
  75.     jr    z,showsys
  76.  
  77. noshow:
  78.     push    hl        ; Save HL
  79.     ld    de,10
  80.     add    hl,de        ; Point to $SYS attribute
  81.     ld    a,(hl)        ; Get the byte
  82.     pop    hl        ; Restore HL
  83.     rla            ; Rotate attribute into carry
  84.     ld    a,' '
  85.     jr    c,setring1    ; Skip $SYS files
  86.  
  87. showsys:
  88.     ld    a,(fcb)        ; Get drive/user designator and..
  89.     ld    (hl),a        ; Put into 'fcb' buffer.
  90.     ld    de,(ringpos)    ; Pointer to current load point in ring
  91. ;    ld    b,eltsiz-1    ; Move drive designator and name to ring
  92.     ld    b,12
  93.     call    movec        ; Move attributes as well
  94.     ex    de,hl        ; De contains next load point address
  95.     ld    a,' '
  96.     ld    (hl),a        ; Space for potential..
  97.     inc    hl        ; Tagging of files for mass copy.
  98.     ld    (ringpos),hl    ; Store and search..
  99.  
  100.     ld    a,(maxpage)    ; Get maximum page of memory allowed.
  101.     cp    h        ; About to overflow zcpr3?
  102.     jp    c,b$size1    ; Br if above valid range.
  103.  
  104.     ld    hl,(ringcnt)    ; Another file found.
  105.     inc    hl
  106.     ld    (ringcnt),hl    ; Update file count.
  107.  
  108. setring1:
  109.     ld    c,srchn        ; For next occurrence.
  110.     ld    de,fcb        ; Filename address field
  111.     call    bdosptr
  112.     inc    a        ; If all done, 0ffh --> 00h.
  113.     jr    nz,setring    ; If not, put next name into ring.
  114.     call    ringempt
  115.     jr    z,noload
  116.  
  117. ; All filenames in ring -- setup ring size and copy-buffer start point
  118.  
  119.     ld    hl,(ringpos)    ; Next load point of ring is start of buffer
  120.     ld    (ringend),hl    ; Set ring end..
  121.     ld    (bufstart),hl    ; And copy-buffer start.
  122.  
  123. ;---------------------------------------------------------------------------
  124.  
  125. ; Sort ring of file entries
  126. ;   - Shell sort algoritm used
  127.  
  128. sort:
  129.  
  130. ; Bypass sort if only 1 element in ring
  131.  
  132.     ld    hl,(ringcnt)    ; Get number of files in ring
  133.     dec    hl        ; Only 1 file?
  134.     ld    a,h
  135.     or    l
  136.     jr    z,tblinz    ; Bypass sort if only one file in ring.
  137.  
  138. ; Set GAP to (power of 2 nearest CNT) - 1
  139.  
  140.     ex    de,hl        ; De = cnt
  141.     inc    de
  142.     ld    hl,4        ; Set initial gap.
  143.     jr    sort02
  144. sort01:
  145.     add    hl,hl        ; Double gap
  146. sort02:
  147.     call    cmpdehl        ; Compare current gap to cnt
  148.     jr    nc,sort01    ; Br if gap < cnt
  149.     dec    hl        ; Set gap = gap-1
  150.     ld    (ringgap),hl
  151.  
  152. ;  DO WHILE (GAP>1)
  153.  
  154.     jr    sort07
  155. sort03:
  156.     ld    hl,(ringgap)    ; Gap = gap / 2
  157.     call    shftrh
  158.     ld    (ringgap),hl
  159.  
  160. ;    DO J = 0 TO (CNT-GAP)
  161.  
  162.     ld    hl,(ringcnt)
  163.     ld    de,(ringgap)
  164.     ld    a,l
  165.     sub    e
  166.     ld    l,a
  167.     ld    a,h
  168.     sbc    a,d
  169.     ld    h,a
  170.     ld    (ringdiff),hl    ; Save (cnt-gap) for inner loop.
  171.     ld    hl,0        ; J = 0
  172. sort04:
  173.     ld    (ringj),hl
  174.     ex    de,hl        ; De = j
  175.     ld    hl,(ringdiff)    ; Hl = (cnt-gap)
  176.     call    cmpdehl        ; J > (cnt-gap)?
  177.     jr    nc,sort07    ; Br if so.
  178.  
  179. ;      DO I = J TO 0 BY (-GAP) WHILE (ENTRY(I) > ENTRY(I+GAP))
  180.  
  181.     ld    hl,(ringj)    ; I = j
  182. sort05:
  183.     ld    (ringi),hl
  184.     ld    a,h        ; Exit if i = -1
  185.     and    l
  186.     inc    a
  187.     jr    z,sort06
  188.     ex    de,hl        ; De = i
  189.     ld    hl,(ringgap)    ; Hl = gap
  190.     add    hl,de        ; De = i, hl = i+gap
  191.     call    ringcmp        ; Compare elements for potential swap.
  192.     jr    c,sort06
  193.  
  194. ;     Swap ENTRY(I) and ENTRY(I+GAP)
  195.  
  196.     call    ringswap
  197.  
  198. ;      ENDDO (I = J TO 0)
  199.  
  200.     ld    de,(ringgap)    ; I = i - gap
  201.     ld    a,d
  202.     cpl
  203.     ld    d,a
  204.     ld    a,e
  205.     cpl
  206.     ld    e,a
  207.     inc    de
  208.     ld    hl,(ringi)
  209.     add    hl,de
  210.     jr    c,sort05
  211.  
  212. ;    ENDDO (DO J = 0 TO (CNT-GAP))
  213.  
  214. sort06:
  215.     ld    hl,(ringj)    ; J=j+1
  216.     inc    hl
  217.     jr    sort04
  218.  
  219. ;  ENDDO (DO WHILE (GAP>1))
  220.  
  221. sort07:
  222.     ld    de,-2        ; Gap > 1?
  223.     ld    hl,(ringgap)
  224.     add    hl,de
  225.     jr    c,sort03
  226.  
  227. ; Sort done -- initialize tables for fast CRC calculations
  228.  
  229. tblinz:
  230.     call    initcrc
  231.  
  232. ; Calculate buffer maximum available record capacity
  233.  
  234. b$size:
  235.     ld    hl,(bdosptr+1)    ; Get 'bdos' entry (fbase)
  236.  
  237.      if    not warmboot
  238.     ld    de,-ccp_ln
  239.     add    hl,de
  240.      endif            ; Not warmboot
  241.  
  242.     dec    hl
  243.     ex    de,hl        ; De = highest buffer address
  244.     ld    hl,(bufstart)    ; Hl = buffer start addr (end of ring list)
  245.     ld    a,e        ; Hl = de - hl = buffer size (bytes)
  246.     sub    l
  247.     ld    l,a
  248.     ld    a,d
  249.     sbc    a,h
  250.     ld    h,a
  251.     jr    c,b$size1    ; Error if start addr > end addr
  252.  
  253.     ld    b,7+1        ; Shift hl right 7 bits
  254.     call    shiftlp        ; To divide by 128.
  255.  
  256.     ld    a,h        ; Memory available for copy?
  257.     or    l
  258.     jr    nz,b$size2    ; Yes, buffer memory space available.
  259.  
  260. b$size1:
  261.     xor    a        ; Error code
  262.     inc    a
  263.     inc    a        ; Indicate no room for files selected.
  264.     ld    (canflg),a    ; Return non-zero for error.
  265.     ret
  266.  
  267. b$size2:
  268.     ld    (rec$max),hl    ; Store maximum record count.
  269.     xor    a        ; Return z for ok
  270.     ld    (canflg),a
  271.     ret
  272.  
  273. ;------------------------------
  274.  
  275. ; RINGCMP - Compare Ring Elements
  276. ;    - DE - First element number
  277. ;    - HL - Second element number
  278.  
  279. ringcmp:
  280.     call    ringaddr    ; Get address of element in hl
  281.     ex    de,hl
  282.     call    ringaddr    ; Get address of element in de
  283.     ex    de,hl
  284.     push    hl        ; Save position pointers..
  285.     push    de        ; For potential swap.
  286.     ld    a,(defalfa)    ; Check for type of alphabetization
  287.     or    a        ; If zero, alpha by type and name
  288.     jr    z,sorttn
  289.  
  290. ; sort by file name and type
  291.  
  292.     ld    b,12        ; # of characters to compare
  293.     call    cmpstr        ; Do comparison
  294.     jr    nocmp        ; Final test
  295.  
  296. ;------------------------------
  297.  
  298. ; sort by file type and name
  299.  
  300. sorttn:
  301.     push    hl        ; Save ptrs
  302.     push    de
  303.     ld    bc,9        ; Pt to type
  304.     add    hl,bc
  305.     ex    de,hl
  306.     add    hl,bc
  307.     ex    de,hl
  308.     ld    b,3        ; 3 chars in file type
  309.     call    cmpstr        ; Compare type
  310.     pop    de        ; Get ptrs
  311.     pop    hl
  312.     jr    nz,nocmp    ; Final test
  313.     push    hl
  314.     push    de
  315.     ld    b,8        ; 8 chars in file name
  316.     inc    hl        ; Pt to first
  317.     inc    de
  318.     call    cmpstr        ; Compare name
  319.     pop    de        ; Get ptrs
  320.     pop    hl
  321.     jr    nz,nocmp    ; Final test
  322.     call    cmpdh        ; Ignore attribute
  323.  
  324. ;------------------------------
  325.  
  326. ; final test for swapping purposes
  327.  
  328. nocmp:
  329.     pop    de
  330.     pop    hl
  331.     ret
  332.  
  333. ;------------------------------
  334.     ext    mulhd
  335.  
  336. ; RINGADDR  - Get address of Ring Element
  337. ;    - HL = Element number
  338. ;
  339.  
  340. ringaddr:
  341.     push    de
  342.     ld    de,eltsiz
  343.     call    MULHD
  344.     ld    de,(ring)    ; Get ring start address
  345.     add    hl,de        ; Point to array element
  346.     pop    de        ; Restore work regs
  347.     ret
  348.  
  349. ;------------------------------
  350.  
  351. ; RINGSWAP - Swap ring elements
  352. ;    - HL -> first element
  353. ;    - DE -> second element
  354.  
  355. ringswap:
  356.     ld    b,eltsiz    ; Length of element to swap
  357. swap:
  358.     ld    c,(hl)        ; Get character from one string..
  359.     ld    a,(de)        ; And one from other string.
  360.     ld    (hl),a        ; Second into first
  361.     ld    a,c        ; First into second
  362.     ld    (de),a
  363.     inc    hl        ; Bump swap pointers
  364.     inc    de
  365.     djnz    swap
  366.     ret
  367.  
  368. ;------------------------------
  369.  
  370. ; CMPSTR- left to right compare of two strings
  371. ;     DE -> to 'a' string,
  372. ;     HL -> to 'b' string,
  373. ;     B     contains string length.)
  374.  
  375. cmpstr:
  376.     call    cmpdh
  377.     ret    nz        ; If not equal, set flag.
  378.     inc    hl        ; Bump compare..
  379.     inc    de        ; Pointers and do next character.
  380.     djnz    cmpstr        ; If done compare, strings are equal
  381.     ret
  382.  
  383. ;------------------------------
  384.  
  385. ; CMPDH - Make comparison without regard to the attribute bit
  386.  
  387. cmpdh:
  388.     push    bc        ; Save BC
  389.     ld    c,7fh        ; Mask
  390.     ld    a,(hl)        ; B character
  391.     and    c        ; Strip attribute
  392.     ld    b,a        ; Save it
  393.     ld    a,(de)        ; A character
  394.     and    c        ; Strip attribute
  395.     cp    b        ; Set flags, carry if B > A
  396.     pop    bc        ; Restore BC
  397.     ret
  398.  
  399. ;---------------------------------------------------------------------------
  400.  
  401. ; FILELERR - Process File Load Error
  402.  
  403. ; - No files in current DIR (or not enough Storage to hold them)
  404. ;    Report it on the error line
  405.  
  406. filelerr:
  407.     ld    a,(canflg)    ; Get log-cancel flag
  408.     or    a
  409.     jr    nz,filerr2    ; Br if not from fileload
  410.     ld    hl,msg104    ; "List Empty"
  411.     jp    ermsg2        ; Due to movdone (canflag = 0)
  412.  
  413. filerr2:
  414.     dec    a        ; Was canflg = 1?
  415.     jr    nz,filerr3    ; Br if not
  416.     ld    hl,msg105    ; "No File Found"
  417.     jp    ermsg2        ; Due to fileload (canflg = 1)
  418.  
  419. filerr3:
  420.     ld    hl,msg106    ; "No Room for file list"
  421.     jp    ermsg2        ; Due to fileload (canflg = 2)
  422.  
  423. ;---------------------------------------------------------------------------
  424.  
  425. ; RINGFCB - Copy filename from RINGPOS to SFCB
  426. ;        - Initialize FCB
  427.  
  428. ringfcb:
  429.     ld    hl,(ringpos)    ; Move name from ring to source 'fcb'
  430.     ld    de,s$fcb    ; Place to move filename and..
  431.     ld    b,12        ; Amount to move (fall thru to move)
  432.     call    movec        ; Set the file name, type and attributes
  433.     ld    de,s$fcb    ; Get fcb address again
  434.     jp    initfcb        ; Initialize fcb and return.
  435.  
  436. ; End of ZFSUBS1.Z80
  437.