Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a value of an hard coded address in C++?

Tags:

c++

pc104

I am looking to read the value that is located in address 302H. The purpose is to read an input from hardware (a part of a 104pc stack). When I run the following code a get this error: Unhandled exception at 0x004134b9 in setOutput.exe: 0xC0000005: Access violation reading location 0x00000302.

#include <stdlib.h> 

#define PORTBASE 0x302
int _tmain(int argc, char *argv[])
{
    int value;
    int volatile * port = (int *) PORTBASE;
    printf("port = %d\n", port);
    value = *port;
    printf("port value = %d\n", value);
}

EDIT:

I am running this under widows xp. Only Documentation I can find on the board is belowenter image description here

EDIT:

From your answers below, I can see that I need to write a driver for the board. Can someone point me to a resource on how to do so?

like image 817
Richard Avatar asked Mar 01 '11 17:03

Richard


2 Answers

In order to access physical memory directly under Windows, you need to develop a driver. I suggest you read up on Virtual Address Space to see why. The short story: The memory addresses you see from a usermode process has no relation to physical memory addresses, and the addresses where hardware lives are protected by the OS to prevent usermode applications from messing up things.

like image 57
Erik Avatar answered Oct 02 '22 17:10

Erik


I'm assuming your program is running as a normal user. To prevent you from damaging the OS and crashing the system, modern OSes&CPUs prevent you from accessing memory that doesn't belong to your program.

In order to access such device memory you'll need to run in kernel CPU mode rather than user mode. The usual way to user such devices is to write a low level device driver that runs in kernel mode and use it as the interface to your user mode program.

like image 31
Mark B Avatar answered Oct 02 '22 16:10

Mark B