Memory Module

Share your advanced PureBasic knowledge/code with the community.
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Memory Module

Post by coco2 »

Here's my module for viewing memory in different formats, I've found it handy and I think I copied most of it from someone so thank you to whoever that was.

Code: Select all

DeclareModule Cc2Mem
  
  Declare.s DebugMemory(*b,length.i, type.i=0)  
  Declare CopyMemDec(*source, *dest, length.i, start.i)
  Declare CopyMem(*source, *dest, length.i, start.i=1)
  
EndDeclareModule

Module Cc2Mem
  
  CompilerIf #PB_Compiler_IsMainFile
    PurifierGranularity(1,1,1,1)
    EnableExplicit
  CompilerEndIf  
  
  
  Procedure.s DebugMemory(*b,length.i, type.i=0)
    ; types: 0-hex 1-dec 2-bin
    Protected i.i, l.i, s.s=""
    If *b
      l.i=length-1
      Select type
        Case 0 ; hex
          For i=0 To l
            s = s + RSet(Hex(PeekA(*b+i)),2,"0")+" "
          Next i
        Case 1 ;dec
          For i=0 To l
            s = s + RSet(Str(PeekA(*b+i)),3,"0")+" "
          Next i
        Case 2 ;bin
          For i=0 To l
            s = s + RSet(Bin(PeekA(*b+i),#PB_Ascii),8,"0")+" "
          Next i          
      EndSelect 
    Else
      s="DebugMemory Error: null pointer passed"
    EndIf
    ProcedureReturn S  
  EndProcedure
  
  Procedure.w SwapEndianW(value.w)
    EnableASM
    ROL value, 8
    DisableASM
    ProcedureReturn value
  EndProcedure  
  
  Procedure.l SwapEndianL(value.l)
    EnableASM
    MOV Eax,value
    BSWAP Eax
    DisableASM
    ProcedureReturn
  EndProcedure
  
  Procedure CopyMem(*source, *dest, length.i, start.i=1)
    ; copys a variable to another from the lowest to highest byte
    ; start: 1 lowest byte
    Protected i.i, a.a
    For i=0 To length-1
      a=PeekA(*source+i+start-1):PokeA(*dest+i,a)
    Next i    
  EndProcedure
  
  Procedure CopyMemDec(*source, *dest, length.i, start.i)
    ; copys a variable to another from the highest to lowest byte
    ; start: 1 lowest byte
    Protected i.i, c.i=0, a.a
    For i=length-1 To length-start Step -1
      a=PeekA(*source+i+start-length):PokeA(*dest+c,a):c+1
    Next i    
  EndProcedure
  
  
EndModule