I am trying to write an Instruction Set Simulator in C to simulate a machine running ARM.
I need to be able to represent 4GB memory efficiently and after some digging have come to the solution of creating an array of 1024 pointers each pointing to a block of 4MB which is dynamically allocated at its first use
#define MEMSIZE 1024 //1024 * 2Mb = 4Gb
#define PAGESIZE 4194304 //4 Mb
#define PAGEEXP 22 //2^PAGEEXP = PAGESIZE
uint32_t* mem[MEMSIZE];
My question is how do I access a certain address of memory?
What I have tried is breaking the address into index and offset as below but this seems to only return 0 for both index and offset. (memAdd is the address I am trying to access)
memIdx = memAdd >> PAGEEXP;
memOfs = memAdd & PAGESIZE;
the functions I use to read/write once I have the address are below:
void memWrite(uint32_t idx, uint32_t ofs, uint32_t val)
{
if(mem[idx] == 0)
mem[idx] = malloc(PAGESIZE);
*(mem[idx] + ofs) = *(mem[idx] + ofs) & val;
}
uint32_t memRead(uint32_t idx, uint32_t ofs)
{
if(mem[idx] == 0)
return 0;
else
return *(mem[idx] + ofs);
}
these seem right in my head however I am still not 100% comfortable with pointers so this may be wrong.
Sorry if this has already been discussed somewhere but I couldnt find anything relevent to what I need ( my keywords are pretty broad)
If
PAGESIZEis a power of 2, it has only 1 bit set. Hence AND-ing it with another value can only leave zero or one bit set in the result. Two possible values. But you’re using it as an array index.Also your
memWrite(uint32_t idx, uint32_t ofs, uint32_t val)function always ANDs in the value ofval. Hence for example ifvalisuint32_maxany call to this function will have no effect.Last, not only do you not check the result of
malloc()for failure, you don’t initialise the memory block which is returned.Try an approach like this (unfortunately I have been unable to test it, I have no compiler handy just now).