Select / case with "dynamic size"

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Select / case with "dynamic size"

Post by lilltroll »

Assume c is an array of size SIZE, e.g.

int main() {
chan c[Size];
....

Is it possible to write the following code in another way such as SIZE can be chosen at compile time ?
E.g.

#define SIZE 4

Code: Select all

do {
		t:>time;
		select {
			case slave {
				c[0]:>len;
				c[0]:>colour;
				for (int i = 0; i < len; i++)
				c[0]:>msg[i];
			}:
			{	cursorX,cursorY}=insert_str(cursorX, cursorY, msg,colour,cursorColour,table);
			break;
			case slave {
				c[1]:>len;
				c[1]:>colour;
				for (int i = 0; i < len; i++)
				c[1]:>msg[i];
			{	cursorX,cursorY}=insert_str(cursorX, cursorY, msg,colour,cursorColour,table);
			break;
			}
			case slave {
				c[2]:>len;
				c[2]:>colour;
				for (int i = 0; i < len; i++)
				c[2]:>msg[i];
			}:
			{	cursorX,cursorY}=insert_str(cursorX, cursorY, msg,colour,cursorColour,table);
			break;
			case slave {
				c[3]:>len;
				c[3]:>colour;
				for (int i = 0; i < len; i++)
				c[3]:>msg[i];
			}:
			{	cursorX,cursorY}=insert_str(cursorX, cursorY, msg,colour,cursorColour,table);
			break;
			case tc when timerafter(timec) :> timec:
...


Probably not the most confused programmer anymore on the XCORE forum.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

You can use a replicated case. A simple example is shown below.

Code: Select all

unsigned f(chanend c[], unsigned size)
{
  select {
  case (unsigned i = 0; i < size; i++)  c[i] :> int:
    return i;
  }
}
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Post by lilltroll »

:idea: :!: If I havn't missed it, it should be added to chapter
3.6 Parallel Replication or similar.

I assumed there was an way to do it, like this.
Probably not the most confused programmer anymore on the XCORE forum.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

lilltroll wrote::idea: :!: If I havn't missed it, it should be added to chapter
3.6 Parallel Replication or similar.

I assumed there was an way to do it, like this.
Select replicators are mentioned in the 2.8 Parameterised Selection section on page 28 of Programming XC on XMOS Devices

"A more concise way to specify the top-level select statement is to use a replicator..."
User avatar
lilltroll
XCore Expert
Posts: 956
Joined: Fri Dec 11, 2009 3:53 am
Location: Sweden, Eskilstuna

Post by lilltroll »

... and you if you need to guarantee the timing in one case , you may reserve some time for it like...

Code: Select all

			case t when timerafter(time) :> int _: //Do not run another event
			t when timerafter(time+TIMERESERVE) :> int _; //if more than TIMERESERVE CPU cycles are available
...
...
...
t:>time;
time+=DELAY-TIMERESERVE;
break;
where the TIMERESERVE can be calculated from the worst XTA timing from the other cases.
Probably not the most confused programmer anymore on the XCORE forum.