samedi 28 mars 2015

Addressing memory in a simulator


Writing a simple 32 bit simulator (in c++) but managing the memory is throwing me into a loop.


My plan was to have a 2D array, the first being pointers each pointing to blocks of memory which are created on first read/write into the block.


The issue I am having is how to address it properly, if I need, for example, 2^16 bits of addressable memory does that mean that.



  • I need 2048 words worth of memory (where each word is a 32bit uint)

  • Which could be split into 64 blocks of 32 words (each block being 1024 bytes, or 8192 bits)


I am under the impression this would appear something like as follows



uint32_t* m[64];
// When first read or write into a block create as follows
m[block] = new uint32_t[32];


This would mean the last accessible address would be 0xfffc (as 0x10000 is the total amount of addresses).


Now, if this is all correct (which I am not certain it is) I think actually getting to the memory would be as follows



uint32_t whichBlock = addr / (32 * 4);
uint32_t blockLoc = (addr % 32) / 4;


This seems to work for, say for example 0x80 as the address as whichBlock would be 1 and blockLoc would b 0 (i.e. the 1st word in the 2nd block) but is obviously wrong when the address is something like 0xff8c which gives whichBlock as 511 and blockLoc as 3.


Obviously I have gone completely wrong with my calculations somewhere, but I cant figure out where!




Aucun commentaire:

Enregistrer un commentaire