Intercept hardware access 
Author Message
 Intercept hardware access

I'm writing embedded software, and for test purposes, I wanne intercept
the accesses to hardware, and deviate them with functions calls.  The
first step is already made (while coding) by using a macro :
#define M_WRITE(add, dta) (add)=(dta)
#define M_READ(add) (add)

So, somewhere in my code, I would write something like :
  ...
M_WRITE(0x01A1, 0xAA);
Alpha = M_READ(0x01ABC);
  ....

Using a compiler flag, I would replace the original macro's
by something like this :
#define M_WRITE(add, dta) InterceptWrite(add, dta)
#define M_READ(add) InterceptRead(add)

Now the hard part, the operands in the macro's are not always the
same, i.e. they can be structures, longs, constants, ...
For example :
  ....
  T_MyStruct *Pointer, Data;
  Data.a = 1;
  Data.b = 2;
  Pointer = 0x1AB;
  M_WRITE(*Pointer, Data);

See, in this case you could pass a pointer in the function call,
but it is also possible to work with constants :
   ....
  M_WRITE(0xA1ABB, 0xCC)
   ...
Here, I can not pass a pointer (note, the address is ok, it's
always passed as a pointer, it's the data which gives me difficulties).

So, my question, what's the best way to pass a structure and constant, when
you do not know in advance what to pass?

Thanks

Manuel

--

Manuel Borowski
VJ63
Alcatel
Antwerp Belgium
tel : +32 3 240 7978



Fri, 11 Jan 2002 03:00:00 GMT  
 Intercept hardware access
Standard C doesn't know about hardware. You need to ask this in your
compiler's newsgroup.

--

Paul Lutus
www.arachnoid.com


Quote:
>I'm writing embedded software, and for test purposes, I wanne intercept
>the accesses to hardware, and deviate them with functions calls.  The
>first step is already made (while coding) by using a macro :
>#define M_WRITE(add, dta) (add)=(dta)
>#define M_READ(add) (add)

>So, somewhere in my code, I would write something like :
>  ...
>M_WRITE(0x01A1, 0xAA);
>Alpha = M_READ(0x01ABC);
>  ....

>Using a compiler flag, I would replace the original macro's
>by something like this :
>#define M_WRITE(add, dta) InterceptWrite(add, dta)
>#define M_READ(add) InterceptRead(add)

>Now the hard part, the operands in the macro's are not always the
>same, i.e. they can be structures, longs, constants, ...
>For example :
>  ....
>  T_MyStruct *Pointer, Data;
>  Data.a = 1;
>  Data.b = 2;
>  Pointer = 0x1AB;
>  M_WRITE(*Pointer, Data);

>See, in this case you could pass a pointer in the function call,
>but it is also possible to work with constants :
>   ....
>  M_WRITE(0xA1ABB, 0xCC)
>   ...
>Here, I can not pass a pointer (note, the address is ok, it's
>always passed as a pointer, it's the data which gives me difficulties).

>So, my question, what's the best way to pass a structure and constant, when
>you do not know in advance what to pass?

>Thanks

>Manuel

>--

>Manuel Borowski
>VJ63
>Alcatel
>Antwerp Belgium
>tel : +32 3 240 7978




Fri, 11 Jan 2002 03:00:00 GMT  
 Intercept hardware access
On Mon, 26 Jul 1999 14:29:04 +0200, Manuel Borowski VJ63

Quote:

>I'm writing embedded software, and for test purposes, I wanne intercept
>the accesses to hardware, and deviate them with functions calls.  The
>first step is already made (while coding) by using a macro :
>#define M_WRITE(add, dta) (add)=(dta)
>#define M_READ(add) (add)

I don't see how these would work in the way you describe. I'd expect
something more like

  #define M_WRITE(add, dta) (*(add) = (dta))
  #define M_READ(add)       (*(add))

assuming that 'add' is a pointer (address) and dta is the data to
write.

Quote:
>M_WRITE(0x01A1, 0xAA);

But neither version would work with this, because 0x01A1 is an
integer, not a pointer. You'd have to use something like

  M_WRITE( (int*)0x01A1, 0xAA );

to give 'add' a suitable type. Performing int -> pointer conversions
like this is intrinsically non-portable, of course, but most compilers
will do something sensible.

Quote:
>Alpha = M_READ(0x01ABC);

  Alpha = M_READ(0x01ABC);

Quote:
>Using a compiler flag, I would replace the original macro's
>by something like this :
>#define M_WRITE(add, dta) InterceptWrite(add, dta)
>#define M_READ(add) InterceptRead(add)

>Now the hard part, the operands in the macro's are not always the
>same, i.e. they can be structures, longs, constants, ...
>For example :
>  ....
>  T_MyStruct *Pointer, Data;
>  Data.a = 1;
>  Data.b = 2;
>  Pointer = 0x1AB;
>  M_WRITE(*Pointer, Data);

>See, in this case you could pass a pointer in the function call,
>but it is also possible to work with constants :
>   ....
>  M_WRITE(0xA1ABB, 0xCC)
>   ...
>Here, I can not pass a pointer (note, the address is ok, it's
>always passed as a pointer, it's the data which gives me difficulties).

>So, my question, what's the best way to pass a structure and constant, when
>you do not know in advance what to pass?

There is no neat way to do this in C. In C++ you could use templates
and function overloading to (almost) automagically create a family of
functions for reading and writing different types of data. In C, you
must do such things manually. One approach would be to create a family
of suitable functions, e.g.

  void InterceptWrite_int(int *add, int data);
  void InterceptWrite_T_MyStruct(T_MyStruct *add, T_MyStruct data);
  ...

Some of these could be defined semi-automatically with macros, others
(such as those taking structs) would require more work.

Then, you could define M_WRITE and M_READ to take a type as an
argument, e.g. for the final versions

  #define M_WRITE(typ,add,dta) (*(typ *)(add) = (dta))
  #define M_READ(typ,add) (*(typ *)add))

and for the test versions

  #define M_WRITE(typ,add,dta) \
    InterceptWrite_ ## typ ((typ *)add, dta);
  #define M_READ(typ, add) \
    InterceptRead_ ## typ ((typ *)add);

I may have got some of the preprocessor magic wrong there, but
hopefully you get the idea: append the type to the root function name
to give the full function name.

Hope this gives you some hints,

-- Mat.



Fri, 11 Jan 2002 03:00:00 GMT  
 Intercept hardware access
Paul Lutus schrieb:

Quote:

> Standard C doesn't know about hardware. You need to ask this in your
> compiler's newsgroup.

Hmmm, I would phrase that differently: Standard C knows about hardware,
but prefers to ignore it :-)

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sat, 12 Jan 2002 03:00:00 GMT  
 Intercept hardware access
I stand corrected :)

--

Paul Lutus
www.arachnoid.com


Quote:
> Paul Lutus schrieb:

> > Standard C doesn't know about hardware. You need to ask this in your
> > compiler's newsgroup.

> Hmmm, I would phrase that differently: Standard C knows about hardware,
> but prefers to ignore it :-)

> Stephan
> (initiator of the campaign against grumpiness in c.l.c)



Sat, 12 Jan 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How do i intercept a hardware IRQ?

2. How to intercept hardware button events?

3. Intercept Access To Registry

4. Hardware accessing

5. Accessing Hardware Port

6. Accessing Hardware!!!

7. Accessing the hardware

8. access to hardware.

9. Fast access to memory mapped hardware

10. access violations and Direct3D with hardware graphics

11. Hardware-Access + Interrupt-Handling in Win32

12. port hardware access

 

 
Powered by phpBB® Forum Software