Inline Assembly, General

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
Obtuse
Member++
Posts: 29
Joined: Mon Jul 09, 2012 11:54 pm

Inline Assembly, General

Post by Obtuse »

Greetings,

While going through the tutorials and digging around in various files I noticed there were two PEEK statements available, XC and ASM.

Which look very handy, especially during testing and hardware debug, which lead to the question of how to use both them in a program, and promptly put up a roadblock to further study.

Although I have done some coding in C and assembler, I have never used inline assembly before, and of course XC is new. Therefore I have many questions but not sure which to ask first.

I have included a stripped down program for a XS1-L2 that works. All it does is toggle the defined 'out' ports hi and lo to produce the conditions of both on, both off, and XOR.

If not too much trouble could someone add both peeks to the code with perhaps a brief explanation as to why it is done that way. Maybe then I can form an intelligent question.

Code: Select all

/* PEEK
 *   July 2012
 *    Xcore member: Obtuse
 */

#include <xs1.h>
#include <platform.h>

#define TIMESLICE 50000000

on stdcore [1] : port in ACTIVE = XS1_PORT_1A;
on stdcore [1] : port in SEND = XS1_PORT_1B;
on stdcore [1] : port in READY = XS1_PORT_1C;
on stdcore [1] : port in SELECT = XS1_PORT_1D;
on stdcore [0] : port out CHECK = XS1_PORT_1K;
on stdcore [1] : port out TEST = XS1_PORT_1E;
on stdcore [1] : port in OKAY = XS1_PORT_1F;

void checktest (out port hilo, int timeslice);

int main (void) {
  par {
    on stdcore [0] : checktest (CHECK, TIMESLICE);

	on stdcore [1] : checktest (TEST, (TIMESLICE*3));
  }
  return 0;
}

void checktest (out port hilo, int timeslice)
{
  timer tst;
  unsigned cnt;

  while (1)
  {

    hilo <: 1;
    tst :> cnt;
    tst when timerafter (cnt+ timeslice ) :> void;

    hilo <: 0x0;
    tst :> cnt;
    tst when timerafter (cnt+ timeslice ) :> void;

  }
}
Thank you,

Len


richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

Obtuse wrote:While going through the tutorials and digging around in various files I noticed there were two PEEK statements available, XC and ASM.
Are you refering to the peek() function (in xs1.h)? This samples the current value on the pins. This is similar to an input statement (i.e. p :> x). The difference between peek and an input is:

* Peek doesn't change the direction of the port.
* Peek ignores the port clock and the deserialiser in the port and returns immediately

Most of the time it is better to use an input statement (p :> x) instead of peek(). With a port input the value is sampled at a well defined point (usually rising edge of the clock, although this can be configured) and you can do things like sample at a particular time / get the time at which a value was sampled. It's hard to know if using peek makes sense in your example without a bit more context. What are you trying to do?
User avatar
Obtuse
Member++
Posts: 29
Joined: Mon Jul 09, 2012 11:54 pm

Post by Obtuse »

Actually I'm not trying to really do anything at this time except get a handle on XC and using inline assembly.

I can add the XC peek() most anywhere in the lower section of code, out of 'main'. For example;

unsigned cnt;
unsigned p1k;

while (1)
{
p1k = peek(hilo)
hilo <: 1;
tst :> cnt;
tst when timerafter (cnt+ timeslice ) :> void;

Which builds and runs okay.

But,
1.) how would I XC peek() at one of the used ports without breaking the rules for parallel threads?

2.) Could this be done with inline asm?

I 'm having no luck at adding any ASM anywhere, at least XDE doesn't like it.

I'm not looking for program results, passing variables, or to do anything more than learn.

Len
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

Anywhere you say

Code: Select all

x = peek(p);
you can say

Code: Select all

asm("peek %0,res[%1]" : "=r"(x) : "r"(p));
User avatar
Obtuse
Member++
Posts: 29
Joined: Mon Jul 09, 2012 11:54 pm

Post by Obtuse »

richard, segher,

Thank you. The mental block is dissolving....slowly.
I've got some reading/program testing to do and will no doubt have more questions.

In the meantime, you will notice the two ports 1E and 1K are set as out ports on different cores running in parallel threads. When I noticed the peek() commands I wondered if either could be used to look at the port pins (for some reason?) then pass their condition to a subroutine or call another thread/program.

Much to learn.

Len