Skip to content

Commit

Permalink
added d-flipflop detection to DetermineJKStateMachine.java
Browse files Browse the repository at this point in the history
  • Loading branch information
hneemann committed Jul 12, 2016
1 parent edd1b8e commit 2e6f4fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package de.neemann.digital.analyse;

import de.neemann.digital.analyse.expression.Constant;
import de.neemann.digital.analyse.expression.Expression;
import de.neemann.digital.analyse.expression.ExpressionException;
import de.neemann.digital.analyse.expression.Operation;
import de.neemann.digital.analyse.expression.*;
import de.neemann.digital.analyse.expression.format.FormatToExpression;
import de.neemann.digital.analyse.expression.format.FormatterException;
import de.neemann.digital.lang.Lang;
Expand All @@ -23,6 +20,7 @@
public class DetermineJKStateMachine {
private Expression j = null;
private Expression nk = null;
private boolean isDFF;

/**
* Creates a new instance
Expand All @@ -33,7 +31,7 @@ public class DetermineJKStateMachine {
* @throws FormatterException FormatterException
*/
public DetermineJKStateMachine(String name, Expression e) throws ExpressionException, FormatterException {
String notName = "¬" + name;
String notName = FormatToExpression.FORMATTER_UNICODE.format(Not.not(new Variable(name)));

boolean wasK = false;
boolean wasJ = false;
Expand Down Expand Up @@ -77,6 +75,7 @@ public DetermineJKStateMachine(String name, Expression e) throws ExpressionExcep
if (wasK) nk = Constant.ONE;
else nk = Constant.ZERO;
}
isDFF = !wasJ && !wasK;
}

private Iterable<Expression> getOrs(Expression e) {
Expand Down Expand Up @@ -114,6 +113,16 @@ public Expression getK() {
return not(nk);
}

/**
* Returns true if it is logical a D flipflop.
* This means that K = not(J).
*
* @return true if it is logical a D flipflop
*/
public boolean isDFF() {
return isDFF;
}

private static final class AsIterable<T> implements Iterable<T> {
private final T item;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testSimple() throws Exception {
DetermineJKStateMachine jk = new DetermineJKStateMachine("a", e);
assertEquals(toStr(notb), toStr(jk.getJ()));
assertEquals(toStr(notc), toStr(jk.getK()));

assertFalse(jk.isDFF());
}

private String toStr(Expression expression) throws FormatterException {
Expand All @@ -54,7 +54,7 @@ public void testSimple2() throws Exception {
DetermineJKStateMachine jk = new DetermineJKStateMachine("a", e);
assertEquals("(b ∧ c) ∨ ¬b", toStr(jk.getJ()));
assertEquals("¬((b ∧ c) ∨ c)", toStr(jk.getK()));

assertFalse(jk.isDFF());
}

public void testSimple3() throws Exception {
Expand All @@ -63,6 +63,7 @@ public void testSimple3() throws Exception {
DetermineJKStateMachine jk = new DetermineJKStateMachine("a", e);
assertEquals("1", toStr(jk.getJ()));
assertEquals("1", toStr(jk.getK()));
assertFalse(jk.isDFF());
}

public void testSimple4() throws Exception {
Expand All @@ -71,6 +72,16 @@ public void testSimple4() throws Exception {
DetermineJKStateMachine jk = new DetermineJKStateMachine("a", e);
assertEquals("0", toStr(jk.getJ()));
assertEquals("0", toStr(jk.getK()));
assertFalse(jk.isDFF());
}

public void testSimpleD() throws Exception {
Expression e = or(and(a, b), and(nota, notb));

DetermineJKStateMachine jk = new DetermineJKStateMachine("c", e);
assertEquals("(¬a ∧ ¬b) ∨ (a ∧ b)", toStr(jk.getJ()));
assertEquals("(¬a ∧ ¬b) ∨ (a ∧ b)", toStr(jk.getNK()));
assertTrue(jk.isDFF());
}

}

0 comments on commit 2e6f4fe

Please sign in to comment.