ALU

Let's say we want to design an ALU that takes two data inputs, and one control input. The output should be the sum of the two data inputs if the control is zero, the difference if the control is one, the logical AND if the control is two, and the logical OR if the control is three.

The process will take in two 32-bit data inputs; so we can declare those input ports as:

chan?(int) A, B;

The control input can take four values, so a two-bit integer suffices:

chan?(int<2>) ctrl;

Finally, the output is also a 32-bit integer:

chan!(int) O;

The ALU process can be defined as follows:

defproc alu (chan?(int) A, B; chan?(int<2>) ctrl; chan!(int) O)
{
   int a, b;
   int<2> c;
 
   chp {
      *[ A?a, B?b, ctrl?c;
          [ c = 0 -> O!(a+b)
          [] c = 1 -> O!(a-b)
          [] c = 2 -> O!(a&b)
          [] c = 3 -> O!(a|b)
       ] ]
    }
}

Using enumerations

Rather than hard-coding the control values, a user-defined enumeration can be used to make the ACT more readable.

defenum alu_ctrl {
   ADD, SUB, AND, OR
};   
 
defproc alu (chan?(int) A, B; chan?(alu_ctrl) ctrl; chan!(int) O)
{
   int a, b;
   alu_ctrl c;
 
   chp {
      *[ A?a, B?b, ctrl?c;
          [ c = alu_ctrl.ADD -> O!(a+b)
          [] c = alu_ctrl.SUB -> O!(a-b)
          [] c = alu_ctrl.AND -> O!(a&b)
          [] c = alu_ctrl.OR -> O!(a|b)
       ] ]
    }
}