News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Reorder Memory - Copy Memory

Started by kevin, April 28, 2023, 01:01:33 AM

Previous topic - Next topic

kevin

  Reorder Memory

 This example is reordering bytes...    


PlayBASIC Code: [Select]
   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









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.

PlayBASIC Code: [Select]
      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


Login required to view complete source code