Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions lowlevel_framework/source/src/io/StateMachineWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package io;

import lowlevel.Cluster;
import lowlevel.State;
import lowlevel.StateMachine;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Set;

/**
* Created by Julian Käuser on 03.06.2017.
*/
public class StateMachineWriter {

/**
*
* @param fsm the StateMachine object to write out
* @param destination the directory where the written .kiss2
* file shall be placed
*/
public static void writeFSM(StateMachine fsm, String destination){

if (fsm==null || destination==null){
System.out.println("no fsm written - destination or fsm unknown");
return;
}
// catch the "fsm-has-no-name"-case
if (fsm.name==null){
System.out.println("fsm has no name, name set to time+date of execution");
fsm.name=new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
}
StringBuilder bld = new StringBuilder();
bld.append("# "+fsm.name +" encoded cluster-wise\n");
bld.append(".model "+fsm.name);

bld.append(buildInputs(fsm));

// latch initilizations
String[] latches = getLatches(fsm);
for (String l : latches){
bld.append(".latch "+l+"\n");
}

bld.append(".start_kiss");
bld.append(".i "+fsm.getNumInputs()+"\n");
bld.append(".o "+fsm.getNumOutputs()+"\n");
// states and transitions
//bld.append(".p "+fsm.getNumTransistions()+"\n");
//bld.append(".s "+fsm.getNumStates()+"\n");
//bld.append(".r "+fsm.getResetState());


bld.append(".end_kiss\n");
//latch mapping
bld.append(".latch_order ");


for (int ii=0; ii<latches.length; ii++){
bld.append(latches[ii]);
}
bld.append("\n");

// print codes of states
for (Cluster c : fsm.getClusters()){
for (State s : c.getStateArray()){
bld.append(".code "+s.getName()+" "+c.getCode()+s.getCode()+"\n");
}
}

bld.append(".end");
destination = (destination.endsWith(System.lineSeparator())) ? destination : (destination + System.lineSeparator());
destination += fsm.name+"_clusterEncoded.kiss2";

try (FileWriter out = new FileWriter(destination)) {
BufferedWriter buf = new BufferedWriter(out);
buf.write(bld.toString());
buf.flush();
}
catch (IOException e){
e.printStackTrace();
}
}

private static String[] getLatches(StateMachine fsm){
String[] latches = null;
if (fsm==null) {
latches = new String[1];
latches[0] = "";
return latches;
}


return latches;

}

private static String[] getExternalClusterLatches(StateMachine fsm){
String[] latches;
Set<Cluster> clusters = fsm.getClusters();
latches = new String[clusters.size()];
int ii=0;
for (Cluster cluster : clusters){
latches[ii] = "v"+cluster.getID()+ " ";
}
return latches;
}

private static String[] getClusterInternalLatches(Cluster cluster){
String[] latches = null;
int len;
return latches;

}

private static String buildInputs(StateMachine fsm){
String ins = "";
return ins;

}
}
103 changes: 103 additions & 0 deletions lowlevel_framework/source/src/lowlevel/Cluster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package lowlevel;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
* Created by Julian Käuser on 03.06.2017.
*/
public class Cluster {

private Set<State> states;
private Set<Transition> transitions;

// should be either "binary" or "onehot"
private String internalEncoding;

private long id;

private int numInputs;

public Cluster(){
this.states = new HashSet<State>();
this.transitions = new HashSet<Transition>();
}

public boolean addState(State state){
// System.out.println("-----TEST-----");
long[][] crappy_transitions= state.getTransitions();
//ACHTUNG, war NICHT dokumentiert und ist super unsicher
for(int i=0;i<crappy_transitions.length;i++){
State originState=state;
long input=crappy_transitions[i][1];
State targetState= state.getNextState((int)input);
Transition myTransition= new Transition(input, targetState, originState, this);
this.transitions.add(myTransition);
}
// System.out.println("ENDE");
return states.add(state);
}

public State[] getStateArray(){
return (State[]) states.toArray();
}

public boolean removeState(State state){
return states.remove(state);
}

public String getEncoding(){
return internalEncoding;
}

public void setEncoding(String enc){
internalEncoding=enc;
}

public void setID(long id){
this.id = id;
}

public long getID(){
return id;
}

public int getNumStates(){
return states.size();
}

public int getInputs(){
return numInputs;
}

public Set<Transition> getTransitions(){
return this.transitions;
}

public int getNumOfTransitions(){
return this.transitions.size();
}

public String getEncodedCluster(){
StringBuilder bld = new StringBuilder();

// ClusterEncoder enc = new ClusterEncoder();
HashMap<State, String> map = null;
switch (getEncoding()) {
case "binary":
//map = enc.encodeBinary(this.getStateArray(), this.getInputs());
case "onehot":
//map = enc.encodeOneHot(this.getStateArray(), this.getInputs());
}
for (State s : map.keySet()) {
String str = ".code "+s.getName()+" "+map.get(s)+"\n";
bld.append(str);
}
return bld.toString();
}

public String getCode(){
return "";
}
}
6 changes: 3 additions & 3 deletions lowlevel_framework/source/src/lowlevel/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ public long[][] getTransitions(){

int i=0;
for(Map.Entry<Long, State> entry : nextStateMap.entrySet()){
transitions[i][0] = this.code;
transitions[i][1] = entry.getKey();
transitions[i][2] = entry.getValue().getCode();
transitions[i][0] = this.code; //current State in code ?
transitions[i][1] = entry.getKey(); //Input Code ?
transitions[i][2] = entry.getValue().getCode(); //next State Code ?
i++;
}

Expand Down
15 changes: 15 additions & 0 deletions lowlevel_framework/source/src/lowlevel/StateMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lowlevel;

import java.util.Set;

/**
* Created by Julian Käuser on 03.06.2017.
*/
public class StateMachine {

private Set<Cluster> clusters;

public String getEncoding(){

}
}
53 changes: 53 additions & 0 deletions lowlevel_framework/source/src/lowlevel/Transition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package lowlevel;

/**
* Created by theChaoS on 04.06.2017.
*/
public class Transition {
private long input;

private State targetState; //Ziel
private State originState;
private Cluster targetCluster;
private Cluster originCluster;

public Transition(long input, State targetState, State startState, Cluster targetCluster, Cluster startCluster){
this.input=input;
this.targetState=targetState;
this.originState=startState;
this.originCluster=startCluster;
this.targetCluster=targetCluster;
}

public Transition(long input, State targetState, State startState, Cluster currentCluster){
this.input=input;
this.targetState=targetState;
this.originState=startState;
this.originCluster=currentCluster;
this.targetCluster=currentCluster;
}

public long getInput(){
return this.input;
}

public String getInputAsBinnary(){
return Long.toBinaryString(this.input);
}

public State getTargetState(){
return this.targetState;
}

public State getOriginState(){
return this.originState;
}

public Cluster getTargetCluster(){
return this.targetCluster;
}

public Cluster getOriginCluster(){
return this.originCluster;
}
}
44 changes: 42 additions & 2 deletions lowlevel_framework/source/src/lowlevel/cluster_encoder.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
package lowlevel;

import java.util.HashMap;

/**
* Julians Idee für diese Klasse: Nehme bis zu drei States, und setze ein One-Hot-Encoding ins Slice.
* -> Ergo 3 Flipflops
* Created by theChaoS on 03.06.2017.
*/
public class clusterEncoder {
public class ClusterEncoder {
final static int k=6;
final static int nFF=4;


public ClusterEncoder(){

}

/**
* Takes the selected states for a cluster, namely in the array states, and packs them into a LUT-feasible
* format. Returns the encoding for this state cluster in one hot
* @param states the states in the cluster, each entry non-null
* @return A mapping of states to strings containing their one-hot value (as chars of {0,1})
* if encoding is not possible (too many states), return null
*/
public HashMap<State, String> encodeOneHot (State[] states, int nIn){
HashMap<State, String> result = new HashMap<State, String>();

/*
precondition: states have yet been chosen regarding their clusterability
*/




}

/**
* Takes the selected states for a cluster, namely in the array states, and packs them into a LUT-feasible
* format. Returns the encoding for this state cluster in binary encoding
* @param states the states in the cluster
* @return A mapping of states to strings containing their binary value (as chars of {0,1})
*/
public HashMap<State, String> encodeBinary(State[] states, int nIn){
HashMap<State, String> result = new HashMap<State, String>();
}

}


public class oneHotEncoder extends clusterEncoder{
public class oneHotEncoder extends ClusterEncoder{

}

Expand Down
Loading