******************************************************************************* * memory.device.S Written by Readysoft. * Copyright 1992,1993,1994 RS. All rights reserved. 1992.02.25.-1994.05.18. * v1.6. ******************************************************************************* ******************************************************************************* * Macros ******************************************************************************* ;------------------------------------------------------------------------------ ; MemoryName Macro ;------------------------------------------------------------------------------ MEM.NAM MACRO DC.B 'memory.device',0 EVEN ENDM ******************************************************************************* * DEV Macro ******************************************************************************* MEM.DEV MACRO ******************************************************************************* * Constant Definitions ******************************************************************************* ;------------------------------------------------------------------------------ ; MemHeader Structure ;------------------------------------------------------------------------------ RSRESET mem_mh_Next RS.L 1 ptr to next header mem_mh_Type RS.L 1 memory type mem_mh_Lower RS.L 1 lower address mem_mh_Upper RS.L 1 upper address mem_mh_Free RS.L 1 free bytes mem_mh_FirstBlk RS.L 1 ptr to first MemBlock mem_mh_SizeOf RS.B 0 ;------------------------------------------------------------------------------ ; MemBlock Structure ;------------------------------------------------------------------------------ RSRESET mem_mb_Next RS.L 1 ptr to next block mem_mb_Bytes RS.L 1 bytesize of block mem_mb_SizeOf RS.B 0 ;------------------------------------------------------------------------------ ; mh.Type Bit Definitions ;------------------------------------------------------------------------------ mem_ty_b_Chip EQU 0 mem_ty_b_Fast EQU 1 mem_ty_b_Clear EQU 16 ;------------------------------------------------------------------------------ ; mh.Type Values ;------------------------------------------------------------------------------ mem_ty_Chip EQU 1< ByteSize,Location(d0,a1) ;------------------------------------------------------------------------------ mem.AllocAbs movem.l d1-d2/a0-a3/a5,-(a7) lea memory.dev(pc),a5 move.l a1,d2 andi.l #7,d2 suba.l d2,a1 add.l d2,d0 addq.l #7,d0 andi.l #-8,d0 beq.s .Error lea mem_FirstHeader(a5),a0 .Loop move.l (a0),a0 next MemHeader cmpa.l #0,a0 end of list ? beq.s .Error cmpa.l mem_mh_Lower(a0),a1 bcs.s .Loop cmpa.l mem_mh_Upper(a0),a1 bcc.s .Loop cmp.l mem_mh_Free(a0),d0 bhi.s .Error move.l a1,a3 begin of Block move.l a1,d2 add.l d0,d2 end of Block lea mem_mh_FirstBlk(a0),a2 .Loop2 move.l (a2),a1 next MemBlock cmpa.l #0,a1 end of list ? beq.s .Error move.l mem_mb_Bytes(a1),d1 add.l a1,d1 end of MemBlock cmp.l d2,d1 bcc.s .Found move.l a1,a2 bra.s .Loop2 .Found cmpa.l a3,a1 bhi.s .Error sub.l d0,mem_mh_Free(a0) sub.l d2,d1 bne.s .After move.l (a1),a0 bra.s .AfterOK .After lea 0(a3,d0.l),a0 new next Block move.l (a1),(a0) move.l a0,(a1) move.l d1,mem_mb_Bytes(a0) .AfterOK cmpa.l a3,a1 beq.s .BeforeOK move.l a3,d1 sub.l a1,d1 size of previous Block move.l d1,mem_mb_Bytes(a1) bra.s .Exit .BeforeOK move.l a0,(a2) .Exit movem.l (a7)+,d1-d2/a0-a3/a5 rts .Error moveq #0,d0 bra.s .Exit ;------------------------------------------------------------------------------ ; AllocMem ByteSize,Requirements(d0,d1) --> ByteSize,Address(d0,a0) ;------------------------------------------------------------------------------ mem.AllocMem movem.l d1-d3/a1/a5,-(a7) lea memory.dev(pc),a5 move.l d0,d3 bytesize in d3 move.l d1,d2 requirements in d2 lea mem_FirstHeader(a5),a1 .Loop move.l (a1),a1 next memlist cmpa.l #0,a1 beq.s .Error move.l mem_mh_Type(a1),d0 and.w d2,d0 requirements OK ? beq.s .Loop move.l a1,a0 move.l d3,d0 bsr mem.Allocate try to allocate tst.l d0 error ? beq.s .Loop sub.l d3,mem_mh_Free(a1) move.l #mem_ty_Clear,d1 clear ? and.l d2,d1 beq.s .Exit move.l a0,a1 adda.l d0,a1 End = Start+ByteSize bsr mem.ClearMem .Exit movem.l (a7)+,d1-d3/a1/a5 rts .Error moveq #0,d0 bra.s .Exit ;------------------------------------------------------------------------------ ; FreeMem ByteSize,Location(d0,a1) --> ByteSize,Location(d0,a1) ;------------------------------------------------------------------------------ mem.FreeMem movem.l d1/a0/a5,-(a7) lea memory.dev(pc),a5 tst.l d0 beq.s .Error lea mem_FirstHeader(a5),a0 .Loop move.l mem_mh_Next(a0),a0 next MemHeader cmpa.l #0,a0 beq.s .Error cmpa.l mem_mh_Lower(a0),a1 bcs.s .Loop cmpa.l mem_mh_Upper(a0),a1 bcc.s .Loop bsr mem.DeAllocate .Exit movem.l (a7)+,d1/a0/a5 rts .Error moveq #0,d0 bra.s .Exit ;------------------------------------------------------------------------------ ; ClearMem Start,End(a0,a1) ;------------------------------------------------------------------------------ mem.ClearMem movem.l d0-d1/a0,-(a7) move.l a1,d0 sub.l a0,d0 lsr.l #2,d0 move.l d0,d1 swap d1 bra.s .Start .Loop clr.l (a0)+ .Start dbf d0,.Loop dbf d1,.Loop movem.l (a7)+,d0-d1/a0 rts ******************************************************************************* * Private Routines ******************************************************************************* ;------------------------------------------------------------------------------ ; AddHeader Start,End,Type(a0,a1,d0) ;------------------------------------------------------------------------------ mem.AddHeader movem.l d0-d1/a0-a2/a5,-(a7) lea memory.dev(pc),a5 move.l a0,a2 move.l d0,mem_mh_Type(a2) set parameters lea mem_mh_SizeOf(a2),a0 move.l a0,d0 addq.l #7,d0 and.l #-8,d0 move.l d0,a0 move.l a0,mem_mh_Lower(a2) move.l a1,d1 and.l #-8,d1 move.l d1,a1 move.l a1,mem_mh_Upper(a2) sub.l d0,d1 and.l #-8,d1 n*8 bytes move.l d1,mem_mh_Free(a2) move.l a0,mem_mh_FirstBlk(a2) first MemBlock clr.l mem_mb_Next(a0) set first MemBlock move.l d1,mem_mb_Bytes(a0) move.l mem_FirstHeader(a5),d0 move.l d0,mem_mh_Next(a2) insert MemoryHeader move.l a2,mem_FirstHeader(a5) into MemoryList movem.l (a7)+,d0-d1/a0-a2/a5 rts ;------------------------------------------------------------------------------ ; ChipMemTest Start,End(a0,a1) --> Top(d0) ;------------------------------------------------------------------------------ mem.ChipMemTest movem.l d1/a0-a2,-(a7) move.l (a0),-(a7) move.l a0,a2 clr.l (a0) move.l #$f2d4b698,d0 .Loop adda.l #$1000,a2 cmpa.l a2,a1 bls.s .Exit move.l (a2),d1 move.l d0,(a2) tst.l (a0) bne.s .Exit cmp.l (a2),d0 bne.s .Exit move.l d1,(a2) bra.s .Loop .Exit move.l a2,d0 move.l (a7)+,(a0) movem.l (a7)+,d1/a0-a2 rts ;------------------------------------------------------------------------------ ; FastMemTest Start,End(a0,a1) --> Top(d0) ;------------------------------------------------------------------------------ mem.FastMemTest movem.l d1/a0-a2,-(a7) move.l a0,a2 move.l #$f2d4b698,d0 .Loop cmpa.l a0,a1 bls.s .Exit move.l (a2),d1 move.l d0,(a2) cmp.l (a2),d0 bne.s .Exit move.l d1,(a2) adda.l #$1000,a2 bra.s .Loop .Exit move.l a2,d0 movem.l (a7)+,d1/a0-a2 rts ;------------------------------------------------------------------------------ ; Allocate ByteSize,MemHeader(d0,a0) --> ByteSize,Address(d0,a0) ;------------------------------------------------------------------------------ mem.Allocate movem.l d1/a1-a2/a5,-(a7) lea memory.dev(pc),a5 addq.l #7,d0 and.l #-8,d0 n*8 bytes beq.s .Error cmpi.l mem_mh_Free(a0),d0 bhi.s .Error lea mem_mh_FirstBlk(a0),a1 .Loop move.l (a1),a0 next MemBlock cmpa.l #0,a0 end of list ? beq.s .Error cmp.l mem_mb_Bytes(a0),d0 size OK ? bls.s .Found move.l a0,a1 bra.s .Loop .Found beq.s .Equal lea 0(a0,d0.l),a2 move.l (a0),(a2) move.l a2,(a1) move.l mem_mb_Bytes(a0),d1 sub.l d0,d1 move.l d1,mem_mb_Bytes(a2) bra.s .Exit .Equal move.l (a0),(a1) .Exit movem.l (a7)+,d1/a1-a2/a5 rts .Error moveq #0,d0 bra.s .Exit ;------------------------------------------------------------------------------ ; DeAllocate Size,MemHeader,Location(d0,a0,a1) --> Size,Location(d0,a1) ;------------------------------------------------------------------------------ mem.DeAllocate movem.l d2-d3/a2-a3,-(a7) move.l a1,d2 andi.l #7,d2 suba.l d2,a1 add.l d2,d0 addq.l #7,d0 n*8 bytes andi.l #-8,d0 beq.s .Error move.l a1,a3 lea mem_mh_FirstBlk(a0),a2 .Loop move.l (a2),a1 next MemBlock cmpa.l #0,a1 end of list ? beq.s .Found cmpa.l a1,a3 bcs.s .Found move.l a1,a2 bra.s .Loop .Found cmpa.l mem_mh_FirstBlk(a0),a1 first block ? beq.s .PrevOK no previous ! move.l mem_mb_Bytes(a2),d2 size add.l a2,d2 end of previous Block cmp.l a3,d2 bhi.s .Free bne.s .PrevOK add.l d0,mem_mb_Bytes(a2) move.l a2,a3 bra.s .CurrOK .PrevOK move.l (a2),(a3) move.l a3,(a2) move.l d0,mem_mb_Bytes(a3) .CurrOK tst.l (a3) beq.s .NextOK move.l mem_mb_Bytes(a3),d2 size add.l a3,d2 end of current Block cmp.l (a3),d2 next Block bhi.s .Free bne.s .NextOK move.l (a3),a2 move.l (a2),(a3) move.l mem_mb_Bytes(a2),d3 add.l d3,mem_mb_Bytes(a3) .NextOK add.l d0,mem_mh_Free(a0) .Exit movem.l (a7)+,d2-d3/a2-a3 rts .Error moveq #0,d0 bra.s .Exit .Free move.l #mem_er_Free,d0 deallocate free block jmp sys.FatalError ******************************************************************************* * DataArea ******************************************************************************* DC.W -1 ENDM