;
;       MemFlush 1.0
;
;       Public Domain Software, Courtesy of
;           Eric Schwertfeger
;       and Laser's Edge Software
;
;       MemFlush is a short Assembly language program who's purpose is to
;       force EXEC to free up the memory occupied by unused libraries,
;       fonts, and other shared resources.  Since this memory will get
;       freed as soon as it is needed anyway, this program is not useful
;       to non-programmers.  It's purpose is to flush unused resources
;       so that the programmer can be sure that his or her program is
;       freeing all resources upon exiting.  It does this merely by
;       Allocating a very large block of memory (256 Meg), freeing it
;       if it just happens to get it.
;
            XREF    _AbsExecBase
            XREF    _LVOOpenLibrary
            XREF    _LVOCloseLibrary
            XREF    _LVOOutput
            XREF    _LVOWrite
            XREF    _LVOAllocMem
            XREF    _LVOFreeMem
            XREF    _LVOExit

MEMF_CHIP   EQU     2
MEMF_FAST   EQU     4

MemFlush:
            move.l  _AbsExecBase,a6
            move.l  #0,d1
            sub.q   #1,d0
            bra.s   ParseDone
Parse:
            move.b  (a0)+,d2            ;Search for c, f, or ?
            cmp.b   #'?',d2
            beq.s   PrintHelp
            or.b    #32,d2              ;Make whatever it is lower case
            cmp.b   #'c',d2
            bne.s   NotChip
            or.l    #MEMF_CHIP,d1
            bra.s   ParseDone
NotChip:
            cmp.b   #'f',d2
            bne.s   NotFast
            or.l    #MEMF_FAST,d1
            bra.s   ParseDone
NotFast:
            cmp.b   #32,d2
            bne.s   PrintHelp
ParseDone:
            dbra    d0,Parse
            cmp.l   #MEMF_CHIP!MEMF_FAST,d1 ;can't get both Fast and Chip
            beq.s   PrintHelp
            move.l  #$FFFFFFF,d0
            jsr     _LVOAllocMem(a6)        ;flush unused fonts and
            tst.l   d0                      ;libraries
            beq.s   Done                    ;just in case the operating system
            move.l  d0,a1                   ;ever goes virtual
            move.l  #$FFFFFFF,d0
            jsr     _LVOFreeMem(a6)
Done:
            rts
;
;           Print the Help message
;
PrintHelp:
            move.l  #dosname,a1
            clr.l   d0
            jsr     _LVOOpenLibrary(a6)     ;get the dos library so we can Write
            move.l  a6,a5
            move.l  d0,a6
            beq     Done                    ;couldn't open dos.library
            jsr     _LVOOutput(a6)
            tst.l   d0
            beq.s   abort                   ;couldn't get Output
            move.l  d0,d1
            move.l  #Message,d2
            move.l  #MesLen,d3
            jsr     _LVOWrite(a6)
abort:
            move.l  a6,a1
            move.l  a5,a6
            jsr     _LVOCloseLibrary(a6)
            bra     Done

dosname     dc.b    'dos.library',0
Message     dc.b    'MemFlush 1.0, by Eric Schwertfeger',10
            dc.b    'USAGE:  MemFlush [c|f|?]',10
            dc.b    9,'MemFlush',9,'Frees as much RAM as possible.',10
            dc.b    9,'MemFlush c',9,'Frees as much Chip RAM as possible.',10
            dc.b    9,'MemFlush f',9,'Frees as much Fast RAM as possible.',10
            dc.b    9,'MemFlush ?',9,'Displays this help message.',10
EOM         ds      0
MesLen      EQU     EOM-Message

           END

