UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on April 28, 2023, 01:01:33 AM

Title: Reorder Memory - Copy Memory
Post by: kevin on April 28, 2023, 01:01:33 AM
  Reorder Memory

 This example is reordering bytes...    


[pbcode]

   Message$    ="This is a string"

  Size        =Len(Message$)
             
  SrcBank      = newBank(size)
  DestBank     = newBank(size)

  PokeBankString SrcBank,0,Message$,Len(Message$)


  for Testlp=0 to 4
     OrderBank   = newBank(size*4)

     select TestLP
     
           // ------------------------------------------
           case 1 //  backwards
           // ------------------------------------------

              for lp =0 to Size-1
                    pokebankint OrderBANK,lp*4,(Size-1)-lp      
              next
     
 
           // ------------------------------------------
           case 2 //  Wrap Bytes
           // ------------------------------------------

              for lp =0 to Size-2
                    pokebankint OrderBANK, lp*4      ,lp+1      
              next
              pokebankint OrderBANK, lp*4      ,Size-1      
 
       

           // ------------------------------------------
           case 3 //  
           // ------------------------------------------

               Top      =Size-1
               Bottom    = 0
              for lp =0 to Size/2
                    pokebankint OrderBANK, lp*4      ,lp
              next

              for lp =lp to Size-1
                    pokebankint OrderBANK, lp*4      ,Top
                    Top--
              next
       

           // ------------------------------------------
           case 4 //  
           // ------------------------------------------

               Dim Indexes(Size)
               
              for lp =0 to Size
                    Indexes(lp)=lp
              next

              for lp =0 to Size
                    Index=rnd(Size)
                    A=Indexes(lp)
                    B=Indexes(Index)
                   
                    Indexes(lp)      =B
                    Indexes(Index)   =A
                   
              next

              for lp =0 to Size-1
                    pokebankint OrderBANK, lp*4      ,INdexes(lp)
              next

       
           // ------------------------------------------
           default
           // ------------------------------------------
     
              ; create the ordering tabel..  
     
              for lp =0 to Size-1
                    pokebankint OrderBANK,lp*4,lp      
              next
     
     endselect


     ; suffle the memory from the source buffer to the target
     ReorderMemory GetBankPtr(SrcBank),GetBankPtr(DestBank),GetBankPtr(OrderBank), Size
 
     Message2$=PeekBankString(DestBank,0,Size)
 
     print " Input Message:"+Message$
     print "Output Message:"+Message2$
     print ""

     deletebank OrderBank
     Sync

  next

  waitkey


Function ReorderMemory(SrcPtr,DestPtr,ReorderTablePTR, Size)
 
     For lp=0 to Size-1
           Position=PeekInt(ReorderTablePTR)
           POkeByte DestPTR+Position, PeekByte(SrcPTR+lp)
           ReorderTablePTR+=4
     next
     
EndFunction



[/pbcode]




Remap Memory and Reorder Memory

    This example includes a second version of the function will copymemory but while reading and writign to the user defined offsets.   This allows the programming to convert the byte pattern during the copy.

[pbcode]


      Test_ReMapMemory()
      Test_ReorderMemory()

      waitkey
      


Function Test_ReorderMemory()
   
   print ""
   
   Message$    ="This is a string"

   Size        =Len(Message$)
               
   SrcBank      = newBank(size)
   DestBank     = newBank(size)

   PokeBankString SrcBank,0,Message$,Len(Message$)

   for Testlp=0 to 4
      OrderBank   = newBank(size*4)

      select TestLP
     
            // ------------------------------------------
            case 1 //  backwards
            // ------------------------------------------

               for lp =0 to Size-1
                     pokebankint OrderBANK,lp*4,(Size-1)-lp     
               next
     
            // ------------------------------------------
            case 2 //  Wrap Bytes
            // ------------------------------------------

               for lp =0 to Size-2
                     pokebankint OrderBANK, lp*4      ,lp+1     
               next
               pokebankint OrderBANK, lp*4      ,Size-1     
   
         
            // ------------------------------------------
            case 3 // 
            // ------------------------------------------

               Top      =Size-1
               Bottom    = 0
               for lp =0 to Size/2
                     pokebankint OrderBANK, lp*4      ,lp
               next

               for lp =lp to Size-1
                     pokebankint OrderBANK, lp*4      ,Top
                     Top--
               next
         

            // ------------------------------------------
            case 4 // 
            // ------------------------------------------

               Dim Indexes(Size)
               
               for lp =0 to Size
                     Indexes(lp)=lp
               next

               for lp =0 to Size
                     Index=rnd(Size)
                     A=Indexes(lp)
                     B=Indexes(Index)
                     
                     Indexes(lp)      =B
                     Indexes(Index)   =A
                     
               next

               for lp =0 to Size-1
                     pokebankint OrderBANK, lp*4      ,INdexes(lp)
               next

         
            // ------------------------------------------
            default
            // ------------------------------------------
     
               ; create the ordering tabel.. 
     
               for lp =0 to Size-1
                     pokebankint OrderBANK,lp*4,lp     
               next
     
      endselect


      ; suffle the memory from the source buffer to the target
      ReorderMemory GetBankPtr(SrcBank),GetBankPtr(DestBank),GetBankPtr(OrderBank), Size
   
      Message2$=PeekBankString(DestBank,0,Size)
   
      print " Input Message:"+Message$
      print "Output Message:"+Message2$
      print ""

      deletebank OrderBank
      Sync

   next


EndFunction


   
      
Function ReorderMemory(SrcPtr,DestPtr,ReorderTablePTR, Size)
      For lp=0 to Size-1
            Position=PeekInt(ReorderTablePTR)
            POkeByte DestPTR+Position, PeekByte(SrcPTR+lp)
            ReorderTablePTR+=4
      next
EndFunction



Function Test_RemapMemory()
   
   
   print ""
   
   Message$     = "Lets test remap memory function"

   
   for Testlp=0 to 2

      Size         = Len(Message$)

      SrcBank      = newBank(size*10)
      DestBank     = newBank(size*10)

      OrderBank   = newBank(size*10)

      PokeBankString SrcBank,0,Message$,Size


      select TestLP
     
         
            // ------------------------------------------
            Case 0
            // ------------------------------------------
     
               ; create the ordering table: 
               
               // This
               TableSize =0               
               for lp =0 to Size-1

                     //  Source Byte to read
                     pokebankWord OrderBANK,TableSize,lp     
                     TableSize+=2

                     // output byte
                     pokebankWord OrderBANK,TableSize,lp     
                     TableSize+=2
               next
     

            // ------------------------------------------
            Case 1   //  Bytes are reversed
             // ------------------------------------------
     
               ; create the ordering table: 
               
               // This
               TableSize =0               
               for lp =0 to Size-1

                     //  Source Byte to read
                     pokebankWord OrderBANK,TableSize,lp     
                     TableSize+=2

                     // output byte
                     pokebankWord OrderBANK,TableSize,(Size-1)-lp     
                     TableSize+=2
               next




            // ------------------------------------------
            Case 1   //  Bytes are reversed
             // ------------------------------------------
     
               ; create the ordering table: 
               
               // This
               TableSize =0               
               for lp =0 to Size-1

                     //  Source Byte to read
                     pokebankWord OrderBANK,TableSize,lp     
                     TableSize+=2

                     // output byte
                     pokebankWord OrderBANK,TableSize,(Size-1)-lp     
                     TableSize+=2
               next


            // ------------------------------------------
            Case 2   //  Double BYTES
             // ------------------------------------------
     
               ; create the ordering table: 
               
               TableSize =0               
               OutputByte = 0
               for lp =0 to Size-1

                     //  Source Byte to read
                     pokebankWord OrderBANK,TableSize,lp            : TableSize+=2
                     pokebankWord OrderBANK,TableSize,OutputByte      :   TableSize+=2
                     OutputByte++
                           //  Source Byte to read
                     pokebankWord OrderBANK,TableSize,lp            : TableSize+=2
                     pokebankWord OrderBANK,TableSize,OutputByte      :   TableSize+=2
                     OutputByte++
         
               next

               Size =OutputByte                  

      endselect


      ; suffle the memory from the source buffer to the target
      RemapMemory GetBankPtr(SrcBank),GetBankPtr(DestBank),GetBankPtr(OrderBank), Size
   
      Message2$   =PeekBankString(DestBank,0,Size)
   
      print " Input Message:"+Message$
      print "Output Message:"+Message2$
      print ""

      deletebank SrcBank
      deleteBank DestBank
      deletebank OrderBank
      Sync

   next


EndFunction



      // -----------------------------------------   
      //  Remap Mmemory
      // -----------------------------------------   

      //  SrcMemory
      //  DestMemory Pointer
      //  RemapBuffer Pointer.
      //        This is a table of 16bit word values,  are are offsets 
      //        The offsets are in pairs, the first word is the src offset point offset
      //          and the second is the output data pointer

      // -----------------------------------------   


Function ReMapMemory(SrcBufferPtr,DestBufferPtr, RemapBufferPointer, Size)

      For lp=0 to Size-1
               SrcPosition    = PeekWord(RemapBufferPointer) : RemapBufferPointer+=2
               DestPosition = PeekWord(RemapBufferPointer) : RemapBufferPointer+=2
               PokeByte DestBufferPTR+DestPosition, PeekByte(SrcBufferPTR+SrcPosition)
      next
     
EndFunction



[/pbcode]