Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:07:45+00:00 2026-06-04T19:07:45+00:00

I am trying to write an Instruction Set Simulator in C to simulate a

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:07:46+00:00Added an answer on June 4, 2026 at 7:07 pm

    If PAGESIZE is 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 of val. Hence for example if val is uint32_max any 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).

    enum { SIM_PAGE_BITS = 22 };   // 2^22 = 4MiB
    enum { SIM_MEM_PAGES = 1024 }; // 1024 * 4MiB = 4GiB
    enum { SIM_PAGE_SIZE = (1<<SIM_PAGE_BITS) };
    enum { SIM_PAGE_MASK = SIM_PAGE_SIZE-1 };
    enum { UNINITIALISED_MEMORY_CONTENT = 0 };
    enum { WORD_BYTES = sizeof(uint32_t)/sizeof(unsigned char) };
    
    #define PAGE_OFFSET(addr) (SIM_PAGE_MASK & (uint32_t)addr)
    // cast to unsigned type to avoid sign extension surprises if addr<0
    #define PAGE_NUM(addr) (((uint32_t)addr) >> SIM_PAGE_BITS)
    #define IS_UNALIGNED(addr) (addr & (WORD_BYTES-1))
    
    unsigned char* mem[MEMSIZE];
    
    uint32_t memRead(uint32_t addr) {
        if (IS_UNALIGNED(addr)) return handle_unaligned_read(addr);
        const uint32_t page = PAGE_NUM(addr);
        if (mem[page]) {
            const unsigned char *p = mem[page] + PAGE_OFFSET(addr);
            return *(uint32_t*)p;
        } else {
            return UNINITIALISED_MEMORY_CONTENT;
        }
    }
    
    void memWrite(uint32_t addr, uint32_t val) {
        if (IS_UNALIGNED(addr)) return handle_unaligned_write(addr, val);
        const uint32_t page = PAGE_NUM(addr);
        if (!mem[page]) {
            if (val == UNINITIALISED_MEMORY_CONTENT) {
                return;
            }
            mem[page] = malloc(SIM_PAGE_SIZE);
            if (!mem[page]) {
                handle_out_of_memory();
            }
            // If UNINITIALISED_MEMORY_CONTENT is always 0 we can 
            // use calloc instead of malloc then memset.
            memset(mem[page], UNINITIALISED_MEMORY_CONTENT, SIM_PAGE_SIZE);
        } 
        const unsigned char *p = mem[page] + PAGE_OFFSET(addr);
        *(uint32_t*)p = val;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to write a Finite State Machine in VHDL code for a
I am trying write a function that generates simulated data but if the simulated
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write out syslog entries containing strings but they don't register. // person.name
Trying to write a chat, like on facebook, I wondered if two clients can
Trying to write a function to see how often an object exists and give
Trying to write a code at the moment that basically tests to see if
Trying to write a javascript bookmarklet to jump from a front end link into
Im trying to write a single byte at a certain location in a file.
Im trying to write something to get my images to show correctly. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.