danglingPortsOkay; GameOfLife is { parameter rows; parameter columns; parameter initialStates; /** * initialStates should be a row x columns matrix */ actor delay = ptolemy.domains.sdf.lib.SampleDelay; actor expression = ptolemy.actor.lib.Expression; /* * Create delays */ for a initially [[0]] [[a < rows]] { for b initially [[0]] [[b < columns]] { relation delayIn[[rows * a + b]]; relation delayOut[[rows * a + b]]; delay(input := delayIn[[rows * a + b]], output := delayOut[[rows * a + b]], initialOutputs := [[{initialStates(a, b)}]]); outport output[[rows * a + b]]; this(output[[rows * a + b]] := delayOut[[rows * a + b]]); } next [[b + 1]] } next [[a + 1]] /** * Create expressions */ for a initially [[0]] [[a < rows]] { for b initially [[0]] [[b < columns]] { expression( center := delayOut[[rows * a + b]], up := delayOut[[ (a < 1) ? (rows * (rows - 1) + b) : (rows * (a - 1) + b) ]], down := delayOut[[ (a == (rows - 1)) ? (rows * 0 + b) : (rows * (a + 1) + b) ]], left := delayOut[[ (b < 1) ? (rows * a + (columns - 1)) : (rows * a + (b - 1)) ]], right := delayOut[[ (b == (columns - 1)) ? (rows * a + 0) : (rows * a + (b + 1)) ]], upLeft := delayOut[[ (a < 1) ? ((b < 1)? (rows * (rows - 1) + (columns - 1)) : (rows * (rows - 1) + (b - 1)) ) : ((b < 1) ? (rows * (a - 1) + (columns - 1)) : (rows * (a - 1) + (b - 1)) ) ]], upRight := delayOut[[ (a < 1) ? ((b == columns - 1)? (rows * (rows - 1) + 0) : (rows * (rows - 1) + (b + 1)) ) : ((b == columns - 1) ? (rows * (a - 1) + 0) : (rows * (a - 1) + (b + 1)) ) ]], downLeft := delayOut[[ (a == rows - 1) ? ((b < 1)? (rows * 0 + (columns - 1)) : (rows * 0 + (b - 1)) ) : ((b < 1) ? (rows * (a + 1) + (columns - 1)) : (rows * (a + 1) + (b - 1)) ) ]], downRight := delayOut[[ (a == rows - 1) ? ((b == columns - 1)? (rows * 0 + 0) : (rows * 0 + (b + 1)) ) : ((b == columns - 1) ? (rows * (a + 1) + 0) : (rows * (a + 1) + (b + 1)) ) ]], expression := [[ (center == 1)? (((up + down + left + right + upLeft + upRight + downLeft + downRight == 3) || (up + down + left + right + upLeft + upRight + downLeft + downRight == 2))? 1 : 0 ) : ((up + down + left + right + upLeft + upRight + downLeft + downRight == 3)? 1 : 0 ) ]], output := delayIn[[rows * a + b]] ); } next [[b + 1]] } next [[a + 1]] }