C and C++ Access to Hardware Resources

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
jonathan
Respected Member
Posts: 377
Joined: Thu Dec 10, 2009 6:07 pm
Contact:

C and C++ Access to Hardware Resources

Post by jonathan »

If I write a program entirely in C/C++ but want to use some of the hardware resources (such as channels or ports), is it possible to do this by calling pieces of assembler from my C/C++ code? Can someone post some examples?


Image
User avatar
snowman
Member
Posts: 13
Joined: Fri Dec 11, 2009 10:51 am
Contact:

Post by snowman »

XMOS tools 9.9.1 come with LLVM GCC 4.2.1. According to http://article.gmane.org/gmane.comp.com ... announce/8, LLVM GCC has good support for GCC-style inline assembly since version 4.0.

GCC-style inline assembly is documented at http://www.ibiblio.org/gferg/ldp/GCC-In ... HOWTO.html.

XMOS instruction set is documented at http://www.xmos.com/published/xs1_en and XMOS assembler at http://www.xmos.com/published/xas_en.
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Post by lilltroll »

I tried a little of it, but since you need to use more than the IN and OUT instruction, you have to setup the ports as well I didn't continue, but I believe that i could work without anything strange.

Since it is not so much documentation (that I understand) I used to cheat with help of the debugging mode and XTA. That way I can read the ASM code created by the compiler and use it as a foundation for inlining.
Probably not the most confused programmer anymore on the XCORE forum.
User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

I think I remember seeing legacy C functions to access some of the hardware resources (but not sure).
User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am
Contact:

Post by paul »

It's possible to just write the appropriate functions in XC and call them if you want to avoid the hassle of assembly.

Some basic ASM channel examples are:

Code: Select all

#ifndef STREAMING_CHANS_H_
#define STREAMING_CHANS_H_

#include <xs1.h>
#include <xccompat.h>

#ifdef __XC__
inline unsigned streaming_chan_inuint(streaming chanend c)
#else
inline unsigned streaming_chan_inuint(chanend c)
#endif
{
  unsigned value;
  __asm__ __volatile__ (
    "in %0, res[%1]" :
    "=r"(value) :
    "r"(c)
  );
  return value;
}

#ifdef __XC__
inline void streaming_chan_outuint(streaming chanend c, unsigned value)
#else
inline void streaming_chan_outuint(chanend c, unsigned value)
#endif
{
  __asm__ __volatile__ (
    "out res[%0], %1" :
    /* no outputs */ :
    "r"(c), "r"(value)
  );
}

#ifdef __XC__
inline unsigned char streaming_chan_inuchar(streaming chanend c)
#else
inline unsigned char streaming_chan_inuchar(chanend c)
#endif
{
  unsigned value;
  __asm__ __volatile__ (
    "int %0, res[%1]" :
    "=r"(value) :
    "r"(c)
  );
  return value;
}

#ifdef __XC__
inline void streaming_chan_outuchar(streaming chanend c, unsigned char value)
#else
inline void streaming_chan_outuchar(chanend c, unsigned char value)
#endif
{
  __asm__ __volatile__ (
    "outt res[%0], %1" :
    /* no outputs */ :
    "r"(c), "r"(value)
  );
}




#endif /*STREAMING_CHANS_H_*/

As snowman mentioned, the manuals are also going to be a big help here.
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

Paul, I'm slightly confused - I was told that the XC compiler can't inline function calls from C?

Here's the relevant thread: http://www.xmoslinkers.org/forum/viewto ... ?f=6&t=505
User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am
Contact:

Post by paul »

Notice the ASM functions are in a .h file - this means it isn't cross module (which is what can't be done). So if you include the .h file then they are visible to the compiler and so can be inlined.
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
User avatar
Andy
Respected Member
Posts: 279
Joined: Fri Dec 11, 2009 1:34 pm

Post by Andy »

paul wrote:Notice the ASM functions are in a .h file - this means it isn't cross module (which is what can't be done). So if you include the .h file then they are visible to the compiler and so can be inlined.
Ah of course... this is very useful and should be documented somewhere (the upcoming Wiki??)
Post Reply