home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ZSYS / SIMTEL20 / SYSLIB / SLIB1.LBR / SALLOC.Z80 < prev    next >
Text File  |  2000-06-30  |  3KB  |  111 lines

  1. ;
  2. ; SYSLIB Module Name:  SALLOC
  3. ; Author:  Richard Conn
  4. ; SYSLIB Version Number:  3.6
  5. ; Module Version Number:  1.1
  6.  
  7.     public    ialloc,alloc
  8.  
  9. ;
  10. ;  BDOS ENTRY POINT
  11. ;
  12. BDOSE    EQU    5
  13.  
  14. ;
  15. ;  SYSLIB FUNCTIONS
  16. ;
  17.     EXT    CODEND
  18.     EXT    COMPHD
  19.  
  20. ;
  21. ;    ALLOC and IALLOC support dynamic memory allocation.  IALLOC is
  22. ; used to initialize the system, specifying where the first byte of
  23. ; the dynamic buffer is and where the last byte of this buffer is.
  24. ; The user may explicitly give zero, one, or both of these values, and,
  25. ; for those values omitted, IALLOC selects the area just after the
  26. ; user program for the beginning of the buffer and the bottom of the CCP
  27. ; for the end of the buffer.  ALLOC, then, is used to obtain buffers
  28. ; from this area.  ALLOC is called with the desired space in DE, and it
  29. ; returns a pointer to the first byte of the allocated buffer in HL
  30. ; with a flag (Z means buffer not allocated due to memory overflow).
  31. ;
  32.  
  33. ;
  34. ;    IALLOC -- Initialize the Allocation Process
  35. ;
  36. ;    On Input, HL = Starting Address of Buffer Area
  37. ;          DE = End Address of Buffer Area
  38. ;          A  = Flag:
  39. ;            Bit 0 - if set, set starting address from HL
  40. ;                if clear, set starting address from CODEND
  41. ;            Bit 1 - if set, set ending address from DE
  42. ;                if clear, set ending address from CCP
  43. ;
  44. IALLOC:
  45.     PUSH    AF    ; SAVE REGS
  46.     PUSH    HL
  47.     PUSH    DE
  48.     PUSH    BC
  49.     LD    B,A    ; SAVE CODE
  50.     AND    1    ; SET FROM HL?
  51.     JP    NZ,IAL1
  52.     CALL    CODEND    ; GET STARTING ADDRESS FROM CODEND
  53. IAL1:
  54.     LD    (NEXTBYTE),HL    ; SET PTR TO NEXT BYTE
  55.     EX    DE,HL        ; USE DE NOW
  56.     LD    A,B    ; GET CODE
  57.     AND    2    ; SET FROM HL?
  58.     JP    NZ,IAL2
  59.     LD    HL,(BDOSE+1)    ; GET BDOS BASE ADDRESS FROM ENTRY POINT
  60.     LD    A,H
  61.     SUB    8    ; PT TO CCP
  62.     LD    H,A
  63.     LD    L,0    ; HL PTS TO BASE OF CCP
  64.     DEC    HL    ; ONE BYTE LOWER
  65. IAL2:
  66.     LD    (LASTBYTE),HL    ; SET PTR TO LAST BYTE
  67.     POP    BC    ; RESTORE REGS
  68.     POP    DE
  69.     POP    HL
  70.     POP    AF
  71.     RET
  72.  
  73. ;
  74. ;  ALLOC -- Allocate Buffer for user
  75. ;    On Input, DE = number of bytes requested
  76. ;    On Output, HL = address of first byte of buffer
  77. ;           A = Flag:
  78. ;            A = 0 and Z Flag if CCP Overflow (HL now invalid)
  79. ;            A = 0FFH and NZ Flag if OK
  80. ;
  81. ALLOC:
  82.     PUSH    DE
  83.     LD    HL,(NEXTBYTE)    ; SET NEXT BYTE
  84.     ADD    HL,DE    ; PT TO AFTER LAST BYTE
  85.     EX    DE,HL
  86.     LD    HL,(LASTBYTE)    ; OVERFLOW?
  87.     CALL    COMPHD    ; OVERFLOW?
  88.     JP    C,ALERR    ; ALLOCATION ERROR
  89.     LD    HL,(NEXTBYTE)    ; GET PTR TO NEXT BYTE
  90.     EX    DE,HL        ; HL PTS TO AFTER LAST BYTE
  91.     LD    (NEXTBYTE),HL    ; NEW NEXT BYTE
  92.     EX    DE,HL        ; HL CONTAINS DESIRED VALUE
  93.     LD    A,0FFH    ; SET FLAG FOR OK
  94.     OR    A
  95.     POP    DE    ; RESTORE DE
  96.     RET
  97. ALERR:
  98.     XOR    A    ; ERROR CODE
  99.     POP    DE
  100.     RET
  101.  
  102. ;
  103. ;  BUFFERS
  104. ;
  105. NEXTBYTE:
  106.     DS    2    ; ADDRESS OF NEXT BYTE IN DYNAMIC BUFFER
  107. LASTBYTE:
  108.     DS    2    ; ADDRESS OF LAST BYTE IN DYNAMIC BUFFER
  109.  
  110.     END
  111.