Thread: Accessing specific memory locations

  1. #16
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    What if I wanted to manipulate each of the individual bits of the specific address (ie I have 8 LEDs each one controlled by one bit? Will I need one pointer for each one?

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And I/O address 0x77 is really a memory address? Sometimes, for example on x86 processors, there are special instructions (IN and OUT for x86) that do I/O.

    Also, you may want to declare the variable as "volatile", as otherwise, the compiler may decide that a sequence of operations is not actually meaningful (because it is never used later on, or some set of operations "doesn't do anything").

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I can't answer that question. I've never done this kind of programming before and the only thing I have is the manual for the SBC I'm using.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    What if I wanted to manipulate each of the individual bits of the specific address (ie I have 8 LEDs each one controlled by one bit? Will I need one pointer for each one?
    Bits are accessed using the bitwise AND or OR operations. AND is used to clear bits (by setting the mask to those bits you don't want to use, e.g. 0xFE clears bit 0 - but we often use the ~ operator for this purpose, as ~1 is 0xFE, so we don't have to think - particularly if you want to clear bit 3, 4 and 6, it gets even harder to figure out which bits are set or cleared). OR is used to set bits, so *LED |= 1; will turn on the bit 0 LED control.

    Of course, all this assumes that there is no other register that needs to be modified (sometimes a I/O port needs to be CONFIGURED or SET UP by some other register).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    I can't answer that question. I've never done this kind of programming before and the only thing I have is the manual for the SBC I'm using.
    Perhaps you can give us a link to the manufacturers web-site, or tell us what particular processor it is using?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    That I can do

    Here

    Page 17 is the LED section (bottom)

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so that is an x86 based-processor, AMD Elan SC520 (and I was the first person in the world to boot DOS and Linux on that processor - I even ran processor that was pencil-marked #16, because they didn't have any silk-screen print on them).

    You need to use the IN/OUT instructions to access that port. What compiler are you using?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Also keep in mind that you may need to set up access to the port, as I/O access is often limited in userspace. If you are operating in kernel space or its equivalent, this shouldn't be a problem.

  9. #24
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Bladactania View Post
    Ok, I'm more confused now that I was before

    As for the code you put, it does not work. I even tried using the leftshif operator in case the LSB/MSB were in the reverse order.
    "1" is always the LSB. Atomic addressable units (i.e. bytes) have no "endianness."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #25
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I'm using Turbo C++ 3.0...

    I've been doing some more research and have come across the inportb() and outportb() functions and came up with the following...

    Code:
    #define LED 0x77  // Base LED Memory Address
    
    int main() {
      int x;
    
      for (x=0; x<10; x++) {
        sleep(2);
        if (x % 2 ==0) outportb(LED, LED | 0x01); else outportb(LED, LED & ~0x01);
      }
      return 0;
    }
    This successfully flashes the LED as I intended, but I'm not exactly sure why. And I'm even more uncertain about the inportb.

    Anyone know a good resource for explaining these funcitons in more detail?

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, you are not doing the right thing tho'. You are writing 0x77 with the lowest bit set or cleared.

    You should use inportb(LED) to find out the original value, so:
    Code:
    outportb(LED, inportb(LED) | 0x01);
    would be more appropriate.

    inport and outport translate to x86 instructions called IN and OUT, which are special I/O instructions. In the old days, that made it easier to connect I/O devices, since the full address space may be 16 or 32 bits wide, but when you have a pin that indicates whether it is I/O or MEMORY, the address decoding can be done with a combination of the I/O/MEMORY pin and a small subset of the data address. Saves a good deal of hassle for the hardware designer.

    Nowadays, hardware is often so complex that saving a few pins & gates is irrelevant, but the concept is still there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    So just
    Code:
      for (x=0; x<10; x++) {
        sleep(2);
        outportb(LED, inportb(LED) | 0x01);
      }

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    So just
    Code:
      for (x=0; x<10; x++) {
        sleep(2);
        outportb(LED, inportb(LED) | 0x01);
      }
    Well, that will of course only turn on the LED. if you want to flash it, you need code to alternate it. You could of course use XOR (^) to toggle the bit.

    I'm sorry if I was misleading - my point was to show ONE example of how you would modify the content of a I/O port address - in this case, enable the LED.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    So why is it necessary to get the original value?

    And what if I was using the 4th bit instead of the 0th bit?

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    So why is it necessary to get the original value?

    And what if I was using the 4th bit instead of the 0th bit?
    Well, what does the other 7 bits in register 0x77 mean? If they are not used, there's no point in writing ANYTHING to them. But I seem to remember that they were defined in the PDF you posted a link to.

    If you want to set other bits, then figure out the value, or use 1 << n, where n is the bit-number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Accessing a Specific Text Line Inside CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2002, 08:12 PM
  4. Writing to Specific Locations
    By Spex in forum C Programming
    Replies: 5
    Last Post: 02-14-2002, 10:08 PM
  5. Accessing memory directly
    By YALINI in forum C Programming
    Replies: 0
    Last Post: 08-30-2001, 11:56 PM