We show on this page examples of Cx code so you can better understand what writing Cx can feel like.

Connecting tasks

On the home page we said that a Cx application is a set of tasks that communicate together. More precisely, an application is a network that instantiates tasks and connects these instances together. Connections are established between ports by describing what each instance reads or writes.

An instance can read from the output ports of other instances as well as from this network's input ports. And conversely, an instance can write to other instances' input ports (and to this network's output ports). This may seem complicated, but in practice this is quite simple:

package uart;

  network UartTop {
    import uart.UartClockGenerator, uart.UartSerialRx, uart.StoreBytes;

    in bool rxData;

    clock_gen = new UartClockGenerator();
    this.writes(clock_gen.rawData);

    rx = new UartSerialRx();
    rx.reads(clock_gen.syncBit);
    rx.writes(store.decodedByte);

    store = new StoreBytes();
  }

The following image shows what this network looks like:

Note that we never said what the store instance reads or writes. We don't need to because the StoreBytes task has a single input port, which is already written to by the rx instance.


Inner tasks

A network can also define inner tasks, which are tasks that are confined within that network and cannot be instantiated outside of it. An inner task can do everything a task can do, and it can access its outer network's ports, as well as the ports of other instances (including other inner tasks).

package uart;

  network UartTop {
    in bool rxData;

    clock_gen = new task {
      u10 count;
      void loop() {
        rx.bit.write(rxData.read());
        count = 868; // 115,200 bps with a 100 MHz clock
        while (count != 0) { count--; }
      }
    };

    rx = new UartSerialRx();

  }

Like in the first example, it is not necessary to define what the rx instance reads, since it is already written to by the clock_gen inner task.

Also, this code is just an example, in a real-world implementation you would probably want to detect the beginning of a start bit (transition from one to zero) and adjust the clock accordingly instead of using a fixed clock divisor.


Instruction-level parallelism

Cx is a sequential language, in the sense that one instruction is executed after the other. Cx is also a cycle-accurate language targeting hardware, the language guarantees that the effect of an instruction, such as incrementing a state variable, is visible immediately in the same cycle, but it does not require that the instruction be effectively committed immediately, nor does it require that expressions be evaluated sequentially. For example:

void inc() {
        res.write(a * b);
        i++;
        dout.write(i);
      }

In this example, all three instructions occur during the same cycle. Since the result of i++; is visible immediately, but it is not required that i be updated immediately, this is equivalent to the following code where all instructions run concurrently during the same cycle (store is a pseudo-instruction that will update i at the beginning of the next cycle):

res.write(a * b);
      dout.write(i + 1);
      store(i, i + 1);
      

Parallel conditions

This is an example of how you can describe conditions in parallel in a single cycle. The idea of this code is to emit true to the o output port when the task has received something on all three ports a, b, and c, but not necessarily during the same cycle. The code below can be executed in one cycle. Conditions are expanded so that all tests are computed in parallel.

task ParallelIfs {
        in sync bool a, sync b, sync c; out sync bool o;

        bool a_ok, b_ok, c_ok;

        void loop() {
          if (a.available) { a_ok = true; }
          if (b.available) { b_ok = true; }
          if (c.available) { c_ok = true; }
        
          if (a_ok && b_ok && c_ok) {
            o.write(true);
            a_ok = 0; b_ok = 0; c_ok = 0;
          }
        }
      }

More examples and applications?

You want more? Visit the marketplace on our website.