News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

What's a Memory Bank

Started by kevin, November 13, 2013, 08:37:26 PM

Previous topic - Next topic

kevin

QuoteWhat's a memory bank ?   Is it like a variable??

   Nah, they're more like arrays.  

   An array is nothing more than a chunk of memory we can access (read from, write to) via it's name and a position index within the array.


   Ie,

PlayBASIC Code: [Select]
    Dim MyData(100)
MyData(50) = 123456

print MyData(50)

Sync
Waitkey




   Above we're created an Integer array called MyData() and stored a single integer value inside this array at position 50.  It's not the only thing in the array though, all the positions are initialized to zero,  so if we print out what's at position 49 say, we'd get a zero.

   But what does it do ?  Nothing really, arrays are just a user firendly interface between your program and the computers memory.   The programmer associates  any meaning with what the data within an array represents.

    eg,

PlayBASIC Code: [Select]
    Dim HighScores(10)

HighScores(1) = 10000
HighScores(2) = 9000
HighScores(3) = 7500
HighScores(4) = 6000
HighScores(5) = 4500
HighScores(6) = 3000
HighScores(7) = 1000
HighScores(8) = 700
HighScores(9) = 400
HighScores(10) =100

For lp=1 to 10
print HighScores(lp)
next

Sync
Waitkey




   In this example we've used an array to store our games high score table.   But really the computer doesn't have any clue what a high score is and why we're using this array in our program, it's just data.   You see internally the array is just a chunk of computer memory.   The language is giving us a nice way to access this information stored within this chunky of memory.

   So A Memory BANK  is way a programmer can ask the computer for a chunk of memory.   Computer memory is completely ambiguous.  You can store anything you like in this chunk, from say picture data, sounds, text through to machine code.  There's nothing special about it.  


   One fundamental difference between the Array() and Bank interfaces is that Integer Arrays house integer values, where banks house raw bytes.    An integer is made up of 4 bytes, so an array of 100 elements is actually 100*4 bytes (plus whatever header data is stored in front of that).  The languages also limits what types of data the programmer can read/write to any array.  Where a BANK has no such limit.  

  So to emulate the high scores table in a BANK, we get something like this..
 
PlayBASIC Code: [Select]
  // allocate BANK #1  of size (10*4)
// the memory in this block should be clear to zero
CreateBank 1, 10 * 4


// fill our bank memory with out list of scores
// eahc score is a 32bit integer, so we need to store
// them 4 bytes apart to emulate our array example
PokeBankInt 1, 0*4 , 10000
PokeBankInt 1, 1*4 , 9000
PokeBankInt 1, 2*4 , 7500
PokeBankInt 1, 3*4 , 6000
PokeBankInt 1, 4*4 , 4500
PokeBankInt 1, 5*4 ,3000

PokeBankInt 1, 6*4,1000
PokeBankInt 1, 7*4,700
PokeBankInt 1, 8*4,400
PokeBankInt 1, 9*4,100

For lp=1 to 10
// compute the offset of the score we want to access in this bank
Offset = (lp-1)*4
// read an integer from bank #1 at this offset
print PeekBankInt(1,offset)
next
Sync
Waitkey
Waitnokey

Cls

// Show the contents of the me block in bytes
For lp=0 to getBankSize(1)-1
// read a each byte within the mem block and display it
print PeekBankByte(1,lp)
next

Sync
Waitkey







    The BANK  commands are a high level approach to memory access.  The language hides the physical location of the memory from you (you can get those if you really need) and gives you a safe way to read and write information into them.   By safe, I mean that if you attempt to access memory outside of the chunks defined size, the language will give you and run time error.   Such protections aren't possible with Pointer styled access for example.



    See PlayBASIC documentation Memory Management