Skip to content

Commit

Permalink
[CGRASim] fixed control decode issue :)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmcp88 committed May 17, 2023
1 parent 5b70e69 commit bf642b4
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 71 deletions.
47 changes: 33 additions & 14 deletions CGRASim/src/pt/up/specs/cgra/microcontroler/ControlDecoder.java
Expand Up @@ -13,14 +13,19 @@

package pt.up.specs.cgra.microcontroler;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pt.up.specs.cgra.control.PEControlSetting;
import pt.up.specs.cgra.structure.pes.PEType;
import pt.up.specs.cgra.structure.pes.ProcessingElement;
import pt.up.specs.cgra.structure.pes.alu.ALUControlSetting;
import pt.up.specs.cgra.structure.pes.alu.ALUElement;
import pt.up.specs.cgra.structure.pes.loadstore.LSControlSetting;
import pt.up.specs.cgra.structure.pes.loadstore.LSElement;

public class ControlDecoder {

Expand All @@ -29,29 +34,43 @@ public class ControlDecoder {
* (turns integer value into appropriate enum key)
*/
interface ControlDecode {
PEControlSetting apply(int value);
boolean apply(ProcessingElement pe, int value);
}

private static final Map<Class<? extends ProcessingElement>, ControlDecode> CtrlDecoders;
private static final Map<PEType, ControlDecode> CtrlDecoders;
static {
var amap = new HashMap<Class<? extends ProcessingElement>, ControlDecode>();
amap.put(ALUElement.class, ControlDecoder::ALUDecoder);
// amap.put("set_io", InstructionDecoder::set_io);

var amap = new HashMap<PEType, ControlDecode>();
amap.put(PEType.ALUElement, ControlDecoder::ALUDecoder);
amap.put(PEType.LSElement, ControlDecoder::LSDecoder);
CtrlDecoders = Collections.unmodifiableMap(amap);
}

public static ALUControlSetting ALUDecoder(int value) {
public static boolean applyControl(ProcessingElement pe, int value) {
var func = CtrlDecoders.get(pe.getType());
if (func == null)
throw new RuntimeException("ControlDecoder: attempted to control PE w/o control setting: " + pe.getAt());

ALUControlSetting ectrl = null;
return func.apply(pe, value);
}

for (var e : ALUControlSetting.values()) {
ectrl = e;
if (value == ectrl.getValue()) {
return ectrl;
}
}
private static PEControlSetting resolve(List<? extends PEControlSetting> list, int value) {
for (var e : list)
if (value == e.getValue())
return e;
return null;
}

private static boolean ALUDecoder(ProcessingElement pe, int value) {
var list = Arrays.asList(ALUControlSetting.values());
var ctrl = (ALUControlSetting) ControlDecoder.resolve(list, value);
var alu = (ALUElement) pe;
return alu.setOperation(ctrl);
}

private static boolean LSDecoder(ProcessingElement pe, int value) {
var list = Arrays.asList(LSControlSetting.values());
var ctrl = (LSControlSetting) ControlDecoder.resolve(list, value);
var ls = (LSElement) pe;
return ls.setOperation(ctrl);
}
}
Expand Up @@ -79,10 +79,9 @@ private static boolean set(SpecsCGRA cgra, List<Integer> ops) {
return false;
}

// TODO: requires resolving specific PE isntance in order to cast control to concrete class

var pe = cgra.getMesh().getProcessingElement(ops.get(0), ops.get(1));
var ctrl = Integer.valueOf(ops.get(2));
return cgra.getMesh().getProcessingElement(ops.get(0), ops.get(1)).setOperation(ctrl);
return ControlDecoder.applyControl(pe, ctrl);
}

private static boolean set_io(SpecsCGRA cgra, List<Integer> ops) {
Expand Down
Expand Up @@ -226,8 +226,9 @@ public PEData execute() {
}

@Override
public String toString() {
return this.getClass().getSimpleName();
public final String toString() {
return this.getType().toString();
// return this.getClass().getSimpleName();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions CGRASim/src/pt/up/specs/cgra/structure/pes/NullPE.java
Expand Up @@ -19,7 +19,7 @@
public class NullPE implements ProcessingElement {

@Override
public String toString() {
return "Null";
public PEType getType() {
return PEType.NullPE;
}
}
24 changes: 24 additions & 0 deletions CGRASim/src/pt/up/specs/cgra/structure/pes/PEType.java
@@ -0,0 +1,24 @@
/**
* Copyright 2023 SPeCS.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. under the License.
*/

package pt.up.specs.cgra.structure.pes;

public enum PEType {

NullPE,
ALUElement,
AdderElement,
MultiplierElement,
LSElement,
UnaryBitwiseOr
}
Expand Up @@ -20,6 +20,8 @@

public interface ProcessingElement {

public PEType getType();

public default List<ProcessingElementPort> getPorts() {
return null;
}
Expand Down
Expand Up @@ -21,6 +21,7 @@
import pt.up.specs.cgra.control.PEControl;
import pt.up.specs.cgra.dataypes.PEData;
import pt.up.specs.cgra.dataypes.PEDataNull;
import pt.up.specs.cgra.structure.pes.PEType;
import pt.up.specs.cgra.structure.pes.ProcessingElement;
import pt.up.specs.cgra.structure.pes.binary.BinaryProcessingElement;

Expand Down Expand Up @@ -148,7 +149,7 @@ protected ALUElement getThis() {
}

@Override
public String toString() {
return "ALU";
public PEType getType() {
return PEType.ALUElement;
}
}
Expand Up @@ -14,6 +14,7 @@
package pt.up.specs.cgra.structure.pes.binary;

import pt.up.specs.cgra.dataypes.PEData;
import pt.up.specs.cgra.structure.pes.PEType;
import pt.up.specs.cgra.structure.pes.ProcessingElement;

public class AdderElement extends BinaryProcessingElement {
Expand Down Expand Up @@ -48,7 +49,7 @@ protected AdderElement getThis() {
}

@Override
public String toString() {
return "Adder";
public PEType getType() {
return PEType.AdderElement;
}
}
Expand Up @@ -14,6 +14,7 @@
package pt.up.specs.cgra.structure.pes.binary;

import pt.up.specs.cgra.dataypes.PEData;
import pt.up.specs.cgra.structure.pes.PEType;
import pt.up.specs.cgra.structure.pes.ProcessingElement;

public class MultiplierElement extends BinaryProcessingElement {
Expand Down Expand Up @@ -46,7 +47,7 @@ protected MultiplierElement getThis() {
}

@Override
public String toString() {
return "Mul";
public PEType getType() {
return PEType.MultiplierElement;
}
}
52 changes: 10 additions & 42 deletions CGRASim/src/pt/up/specs/cgra/structure/pes/loadstore/LSElement.java
Expand Up @@ -5,6 +5,7 @@
import pt.up.specs.cgra.dataypes.PEData;
import pt.up.specs.cgra.dataypes.PEDataNull;
import pt.up.specs.cgra.dataypes.PEInteger;
import pt.up.specs.cgra.structure.pes.PEType;
import pt.up.specs.cgra.structure.pes.binary.BinaryProcessingElement;

public class LSElement extends BinaryProcessingElement implements PEControl<LSControlSetting> {
Expand Down Expand Up @@ -86,7 +87,7 @@ private PEData store() {

} else {
data = new PEDataNull();
System.out.printf("LS at %d %d STOREd NULL value \n", this.getX(), this.getY());
this.debug(this.getAt() + "STOREd NULL value " + addr);
}
return data;
}
Expand All @@ -104,31 +105,13 @@ else if (ctrl == LSControlSetting.STORE)
throw new RuntimeException("LSElement: invalid control setting");
}

// reset ao contador
public boolean reset_counter() {
cload = 0;
cstore = 0;
if (cload == 0 && cstore == 0)
return true;
else
return false;
}
/*
private PEData store(int idx) {
if (this.getMesh().getCGRA().store_liveins(this, idx, this.getRegisterFile().get(0)))
return this.getRegisterFile().get(0);
else
return null;
@Override
public void reset() {
this.ctrl = LSControlSetting.NULL;
this.cload = 0;
this.cstore = 0;
super.reset();
}
private PEData store_next() {
store(cstore);
cstore++;
return this.getRegisterFile().get(0);
}*/

@Override
public boolean setOperation(LSControlSetting ctrl) {
Expand All @@ -138,28 +121,13 @@ public boolean setOperation(LSControlSetting ctrl) {
return true;
}

/*
public boolean setControl(int ctrl) {
PEControlSetting ectrl = null;
for (var e : LSControlSetting.values()) {
ectrl = e;
if (ctrl == ectrl.getValue()) {
return this.setControl((LSControlSetting) ectrl);
}
}
return false;
}*/

@Override
protected LSElement getThis() {
return this;
}

@Override
public String toString() {
return "LSU";
public PEType getType() {
return PEType.LSElement;
}
}
@@ -1,6 +1,7 @@
package pt.up.specs.cgra.structure.pes.unary;

import pt.up.specs.cgra.dataypes.PEData;
import pt.up.specs.cgra.structure.pes.PEType;
import pt.up.specs.cgra.structure.pes.ProcessingElement;
import pt.up.specs.cgra.structure.pes.binary.AdderElement;

Expand Down Expand Up @@ -35,7 +36,7 @@ protected UnaryBitwiseOr getThis() {
}

@Override
public String toString() {
return "UnaryBitwiseOr";
public PEType getType() {
return PEType.UnaryBitwiseOr;
}
}

0 comments on commit bf642b4

Please sign in to comment.