This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Memory dump

I just wrote a simple memory dump routine declared as follows:

void hexdump(U16 start_address, U16 count, bit code_memory);

The idea is to use <code_memory> to select between dumping from XRAM (=0) or code memory (=1).

Inside the routine two pointers are declared:

U8 xdata *xram_pointer;
U8 code *code_pointer;

And initialized (could be done in one shot):

xram_pointer = (U8 xdata *) start_address;
code_pointer = (U8 code *) start_address;

With this in place the output look decides which pointer to use based on <code_memory>, builds the strings using sprintf, and outputs with printf one line at a time.

A couple of questions:

1- "U16 start_address" is this the best way to declare this argument?

2- Is there a way to do this using a single pointer (or "start_address") within the function?

Maybe, define a void pointer and then cast it into either XRAM or code?

Thanks.

Parents Reply Children
  • Fair enough. I did read it before posting and writing the code. What I gather is that, if you don't use memory-specific pointers you may very well pay a speed penalty, among other things.

    If I declare a generic pointer:

    U8 *generic_pointer;

    How do you propose to let a function like printf know whether to go to xdata or code space?

    printf(" %s", generic_pointer);

    won't work,

    printf(" %s", (U8 code *)generic_pointer);
    or
    printf(" %s", (U8 xdata *)generic_pointer);

    might work...but what penalty do we pay?

    I took the approach of making the cast once and then working with a memory specifc pointer for the rest of the function. Yet, wanting to make sure that this was a sensible approach I decided to post on the list.

    Are you of the opinion that I should use generics and simply cast when needed?

    Thanks.

  • "How do you propose to let a function like printf know whether to go to xdata or code space?"

    You don't. Most of the library routines deal with generic pointers. Again, read about generic pointers. They are 3-byte pointers encoded to specify the memory space they point to. Your dump routine could to likewise, taking a generic pointer and then "converting" it to a mspace-specific pointer for speed when necessary.

  • How do you propose to let a function like printf know whether to go to xdata or code space?

    That's exactly what's generic about generic pointers: they contain an extra byte to signal this difference. You may want to think of a generic pointer as a data type like

    struct generic_pointer {
        U8  memory_class;
        union {
          void code  *c;
          void xdata *x;
          void data  *d;
          void idata *i;
        } ptr;
    };
    which was versatile enough that it was implemented as an intrinsic data type supported by the compiler and linker.

    printf(" %s", generic_pointer);

    won't work,


    Says who?

    The "address" to your function should almost certainly be a generic pointer. That's always better than abusing an int as a pointer, and particularly better than using arguments to specify one thing.

    And BTW: worrying about efficiency while in the same sentence proposing to use printf() to print a string seems considerably self-contradictive to me. ;->