Skip to content

Commit

Permalink
Revert to multi cycle SM design which seems to work better.
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-g committed Aug 28, 2015
1 parent a50178b commit 0a31ea2
Showing 1 changed file with 109 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@
import com.maxeler.maxcompiler.v2.statemachine.manager.*;
import com.maxeler.maxcompiler.v2.managers.DFEManager;

import com.maxeler.maxcompiler.v2.statemachine.*;
import com.maxeler.maxcompiler.v2.statemachine.manager.*;
import com.maxeler.maxcompiler.v2.managers.DFEManager;

public class ParallelCsrReadControl extends ManagerStateMachine {

private enum Mode {
ReadingLength,
OutputtingCommands
}

private final DFEsmStateEnum<Mode> mode;

private final DFEsmPullInput iLength;
private final DFEsmStateValue iLengthReady;
private final DFEsmPushOutput oReadMask, oReadEnable, oRowFinished, oRowLength, oNnzCounter, oFirstReadPosition;
private final DFEsmStateValue readEnableOutValid, readMaskOutValid;
private final DFEsmStateValue readMaskOutValid;
private final DFEsmStateValue readEnableData, readMaskData, rowFinishedData, rowLengthData;
private final DFEsmStateValue cycleCounter;
private final DFEsmStateValue firstReadPosition;
Expand All @@ -23,128 +34,146 @@ public ParallelCsrReadControl(DFEManager owner, int inputWidth, boolean dbg) {
this.inputWidth = inputWidth;
this.dbg = dbg;

mode = state.enumerated(Mode.class, Mode.ReadingLength);
iLength = io.pullInput("length", dfeUInt(32));

oReadMask = io.pushOutput("readmask", dfeUInt(inputWidth), 1);
oReadEnable = io.pushOutput("readenable", dfeBool(), 1);
oRowFinished = io.pushOutput("rowFinished", dfeBool(), 1);
oReadMask = io.pushOutput("readmask", dfeUInt(32), 1);
oReadEnable = io.pushOutput("readenable", dfeUInt(32), 1);
oRowFinished = io.pushOutput("rowFinished", dfeUInt(32), 1);
oRowLength = io.pushOutput("rowLength", dfeUInt(32), 1);
oNnzCounter = io.pushOutput("cycleCounter", dfeUInt(32), 1);
oFirstReadPosition = io.pushOutput("firstReadPosition", dfeUInt(32), 1);

cycleCounter = state.value(dfeUInt(32), 0);
cycleCounter = state.value(dfeInt(32), 0);
crtPos = state.value(dfeUInt(32), 0);
firstReadPosition = state.value(dfeUInt(32), 0);
toread = state.value(dfeUInt(32), 0);
iLengthRead = state.value(dfeBool(), true);
readEnableOutValid = state.value(dfeBool(), false);
readEnableData = state.value(dfeBool(), false);
rowFinishedData = state.value(dfeBool(), false);
readEnableData = state.value(dfeUInt(32), false);
rowFinishedData = state.value(dfeUInt(32), true);
rowLengthData = state.value(dfeUInt(32), 0);
readMaskOutValid = state.value(dfeBool(), false);
readMaskData = state.value(dfeUInt(inputWidth));
readMaskData = state.value(dfeUInt(32));
iLengthReady = state.value(dfeBool(), false);
}

DFEsmValue outputNotStall() {
return ~oReadEnable.stall & ~oReadMask.stall
& ~oRowFinished.stall & ~oRowLength.stall
& ~oNnzCounter.stall & ~oFirstReadPosition.stall;
}

DFEsmValue iLengthReady() {
return iLengthRead & ~iLength.empty & ~oReadEnable.stall & ~oReadMask.stall;
return ~iLength.empty & outputNotStall() & mode === Mode.ReadingLength;
}

@Override
protected void nextState() {
iLengthReady.next <== iLengthReady();

readEnableOutValid.next <== false;
readMaskOutValid.next <== false;
iLengthReady.next <== iLengthReady();

DFEsmAssignableValue toreadCrt = assignable.value(dfeUInt(32));
IF (iLengthReady === true) {
toreadCrt <== iLength;
firstReadPosition.next <== crtPos;
IF (iLength !== 0) {
iLengthRead.next <== false;
rowLengthData.next <== iLength;
} ELSE {
// handle empty row
rowFinishedData.next <== 1;
readMaskData.next <== 0;
rowLengthData.next <== 0;
readEnableData.next <== 0;
cycleCounter.next <== 0;
readMaskOutValid.next <== true;
readEnableOutValid.next <== true;
}
} ELSE {
toreadCrt <== toread;
}

IF (toreadCrt !== 0) {
readEnableOutValid.next <== true;
readMaskOutValid.next <== true;

DFEsmAssignableValue canread = assignable.value(dfeUInt(32));
IF ( inputWidth - crtPos < toreadCrt) {
canread <== inputWidth - crtPos;
} ELSE {
canread <== toreadCrt;
}

readEnableData.next <== crtPos === 0;
IF (iLengthReady === true)
cycleCounter.next <== 0;
ELSE
cycleCounter.next <== cycleCounter + 1;

DFEsmAssignableValue pattern = assignable.value(dfeUInt(64));
pattern <== 0;
for (long i = 0; i <= inputWidth; i++)
IF (canread === i)
pattern <== (1l << i) - 1l;

for (int i = 0; i <= inputWidth; i++)
IF (crtPos === i)
readMaskData.next <== pattern.shiftLeft(i).cast(dfeUInt(inputWidth));

// crtPos = (crtPos + canread ) % inputWidth;
IF (crtPos + canread >= inputWidth) {
crtPos.next <== 0;
} ELSE {
crtPos.next <== crtPos + canread;
SWITCH (mode) {
CASE (Mode.ReadingLength) {
IF (iLengthReady === true) {
toread.next <== iLength;
rowLengthData.next <== iLength;
IF (iLength === 0) {
rowFinishedData.next <== 1;
readEnableData.next <== 0;
readMaskData.next <== 0;
rowLengthData.next <== 0;
cycleCounter.next <== 0;
firstReadPosition.next <== crtPos;
readMaskOutValid.next <== true;
//debug.simPrintf("Empty row \n");
} ELSE {
mode.next <== Mode.OutputtingCommands;
iLengthReady.next <== false;
cycleCounter.next <== -1;
//debug.simPrintf("Reading input %d\n", iLength);
}
}
}
toread.next <== toreadCrt - canread;
IF (toreadCrt - canread === 0) {
iLengthRead.next <== true;
rowFinishedData.next <== 1;
} ELSE {
rowFinishedData.next <== 0;
CASE (Mode.OutputtingCommands) {
IF (outputNotStall()) {
readMaskOutValid.next <== true;

DFEsmAssignableValue canread = assignable.value(dfeUInt(32));
IF ( inputWidth - crtPos < toread) {
canread <== inputWidth - crtPos;
} ELSE {
canread <== toread;
}

IF (crtPos === 0) {
readEnableData.next <== 1;
} ELSE {
readEnableData.next <== 0;
}

cycleCounter.next <== cycleCounter + 1;

DFEsmAssignableValue pattern = assignable.value(dfeUInt(64));
pattern <== 0;
for (long i = 0; i <= inputWidth; i++)
IF (canread === i)
pattern <== (1l << i) - 1l;

for (int i = 0; i <= inputWidth; i++)
IF (crtPos === i)
readMaskData.next <== pattern.shiftLeft(i).cast(dfeUInt(32));

// crtPos = (crtPos + canread ) % inputWidth;
IF (crtPos + canread >= inputWidth) {
crtPos.next <== 0;
} ELSE {
crtPos.next <== crtPos + canread;
}

//debug.simPrintf(
//"Next State -- readMaskOutValid %d, canread %d toread %d readMask %d pattern %d cycleCounter %d\n",
//readMaskOutValid, canread, toread, readMaskData, pattern, cycleCounter);

toread.next <== toread - canread;
IF (toread - canread === 0) {
iLengthRead.next <== true;
rowFinishedData.next <== 1;
mode.next <== Mode.ReadingLength;
// debug.simPrintf("iLengthRead.next = true ");
} ELSE {
iLengthRead.next <== false;
rowFinishedData.next <== 0;
// debug.simPrintf("iLengthRead.next = false ");
}
//debug.simPrintf( "Sm: iLengthRead %d outEnable: %d, readEnable: %d, readmask: %d toread %d crtPos %d\n",
//iLengthRead, readMaskOutValid, readEnableData, readMaskData, toread, crtPos);
}
}
//debug.simPrintf( "Sm: readEnable: %d, readmask: %d toread %d crtPos %d\n",
//readEnableData, readMaskData, toread, crtPos);
}
}

@Override
protected void outputFunction() {
iLength.read <== iLengthReady();

oReadEnable.valid <== readEnableOutValid;
oReadEnable.valid <== readMaskOutValid;
oReadMask.valid <== readMaskOutValid;
oRowLength.valid <== readMaskOutValid;
oRowFinished.valid <== readMaskOutValid;
oNnzCounter.valid <== readMaskOutValid;
oFirstReadPosition.valid <== readEnableOutValid;
oFirstReadPosition.valid <== readMaskOutValid;

oReadEnable <==readEnableData;
oReadMask <== readMaskData;
oRowLength <== rowLengthData;
oRowFinished <== rowFinishedData;
oNnzCounter <== cycleCounter;
oNnzCounter <== cycleCounter.cast(dfeUInt(32));
oFirstReadPosition <== firstReadPosition;

if (dbg)
IF (readEnableOutValid)
IF (readMaskOutValid)
debug.simPrintf(
"ReadControl SM -- readmask: %d, readeenable: %d toread: %d, crtPos: %d, rowLength %d, rowFinished %d cycleCounter %d\n",
readMaskData, readEnableData, toread, crtPos, rowLengthData, rowFinishedData, cycleCounter);
"ReadControl SM -- iLength %d, readmask: %d, readeenable: %d toread: %d, crtPos: %d, rowLength %d, rowFinished %d cycleCounter %d\n",
iLength, readMaskData, readEnableData, toread, crtPos, rowLengthData, rowFinishedData, cycleCounter);
}
}

0 comments on commit 0a31ea2

Please sign in to comment.