module pick1
Title 'Pick One Move from Four Possible'
"PICK1 device 'P22V10'; 

Declarations

" Inputs from TWOINHAF PLDs
WINA3..WINA0           pin 1..4;       "Winning moves in cells 11,12,13,21,22
WINB3..WINB0           pin 5..8;       "Winning moves in cells 11,12,13,21,22 of rotated grid
BLKA3..BLKA0           pin 9..11, 13;  "Blocking moves in cells 11,12,13,21,22
BLKB3..BLKB0           pin 14..16, 21; "Blocking moves in cells 11,12,13,21,22 of rotated grid
" Inputs from grid
X22, Y22               pin 22..23;     "Center cell; pick if no other moves and it's empty
MOVE3..MOVE0           pin 17..20 istype 'com';   " Move outputs to PICK2 PLD

" Sets
WINA = [WINA3..WINA0];
WINB = [WINB3..WINB0];
BLKA = [BLKA3..BLKA0];
BLKB = [BLKB3..BLKB0];
MOVE = [MOVE3..MOVE0];

" Non-rotated move input and output encoding
MOVE11 = [1,0,0,0]; MOVE12 = [0,1,0,0]; MOVE13 = [0,0,1,0];
MOVE21 = [0,0,0,1]; MOVE22 = [1,1,0,0]; MOVE23 = [0,1,1,1];
MOVE31 = [1,0,1,1]; MOVE32 = [1,1,0,1]; MOVE33 = [1,1,1,0];
NONE   = [0,0,0,0];

equations

WHEN WINA != NONE THEN MOVE = WINA;
ELSE WHEN WINB != NONE THEN MOVE = ![WINB0..WINB3];  " Map rotated coding
ELSE WHEN BLKA != NONE THEN MOVE = BLKA;
ELSE WHEN BLKB != NONE THEN MOVE = ![BLKB0..BLKB3];  " Map rotated coding
ELSE WHEN !X22 & !Y22 THEN MOVE = MOVE22;
ELSE MOVE = NONE;

end pick1
