diff --git a/gcd.j b/gcd.j new file mode 100644 index 0000000..9ec623a --- /dev/null +++ b/gcd.j @@ -0,0 +1,457 @@ +.class public gcd/GCDImpl +.super java/lang/Object + +.field public values [I + +.method public values()[I + .limit stack 2 + .limit locals 1 + + aload_0 + getfield gcd/GCDImpl/values [I + areturn +.end method + + +.method public ([I)V + .limit stack 3 + .limit locals 2 + + aload_0 + aload_1 + putfield gcd/GCDImpl/values [I + aload_0 + invokespecial java/lang/Object/()V + + return +.end method + +.method public add(II)I + .limit stack 3 + .limit locals 3 + iload_1 + iload_2 + invokestatic firrtl_interpreter/executable/OperationImplementations/addInt(II)I + ireturn +.end method + +.method public step()V + .limit stack 100 + .limit locals 12 + + ; AssignIntValuesNode(6, GetIntValuesNode(5)) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 6 + + ; GetIntValuesNode(5) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 5 + iaload + + ; finish AssignIntValuesNode(6, GetIntValuesNode(5)) + iastore + + ; AssignIntValuesNode(8, GetIntValuesNode(7)) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 8 + + ; GetIntValuesNode(7) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 7 + iaload + + ; finish AssignIntValuesNode(8, GetIntValuesNode(7)) + iastore + + ; AssignIntValuesNode(9, GtIntValuesNode(GetIntValuesNode(6),GetIntValuesNode(8))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 9 + + ; GtIntValuesNode(GetIntValuesNode(6), GetIntValuesNode(8)) + + ; GetIntValuesNode(6) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 6 + iaload + + + ; GetIntValuesNode(8) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 8 + iaload + + ; finish GtIntValuesNode(GetIntValuesNode(6), GetIntValuesNode(8)) + invokestatic firrtl_interpreter/executable/OperationImplementations/gtInt(II)I + + ; finish AssignIntValuesNode(9, GtIntValuesNode(GetIntValuesNode(6),GetIntValuesNode(8))) + iastore + + ; AssignIntValuesNode(10, SubIntValuesNode(GetIntValuesNode(6),GetIntValuesNode(8))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 10 + + ; SubIntValuesNode(GetIntValuesNode(6), GetIntValuesNode(8)) + + ; GetIntValuesNode(6) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 6 + iaload + + + ; GetIntValuesNode(8) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 8 + iaload + + ; finish SubIntValuesNode(GetIntValuesNode(6), GetIntValuesNode(8)) + invokestatic firrtl_interpreter/executable/OperationImplementations/subInt(II)I + + ; finish AssignIntValuesNode(10, SubIntValuesNode(GetIntValuesNode(6),GetIntValuesNode(8))) + iastore + + ; AssignIntValuesNode(11, TailIntValuesNode(GetIntValuesNode(10),GetIntValuesConstantNode(1))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 11 + + ; TailIntValuesNode(GetIntValuesNode(10), GetIntValuesConstantNode(1)) + + ; GetIntValuesNode(10) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 10 + iaload + + + ; GetIntValuesConstantNode(1) + iconst_1 + + ; finish TailIntValuesNode(GetIntValuesNode(10), GetIntValuesConstantNode(1)) + invokestatic firrtl_interpreter/executable/OperationImplementations/tailInt(II)I + + ; finish AssignIntValuesNode(11, TailIntValuesNode(GetIntValuesNode(10),GetIntValuesConstantNode(1))) + iastore + + ; AssignIntValuesNode(13, EqIntValuesNode(GetIntValuesNode(9),GetIntValuesConstantNode(0))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 13 + + ; EqIntValuesNode(GetIntValuesNode(9), GetIntValuesConstantNode(0)) + + ; GetIntValuesNode(9) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 9 + iaload + + + ; GetIntValuesConstantNode(0) + iconst_0 + + ; finish EqIntValuesNode(GetIntValuesNode(9), GetIntValuesConstantNode(0)) + invokestatic firrtl_interpreter/executable/OperationImplementations/eqInt(II)I + + ; finish AssignIntValuesNode(13, EqIntValuesNode(GetIntValuesNode(9),GetIntValuesConstantNode(0))) + iastore + + ; AssignIntValuesNode(14, SubIntValuesNode(GetIntValuesNode(8),GetIntValuesNode(6))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 14 + + ; SubIntValuesNode(GetIntValuesNode(8), GetIntValuesNode(6)) + + ; GetIntValuesNode(8) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 8 + iaload + + + ; GetIntValuesNode(6) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 6 + iaload + + ; finish SubIntValuesNode(GetIntValuesNode(8), GetIntValuesNode(6)) + invokestatic firrtl_interpreter/executable/OperationImplementations/subInt(II)I + + ; finish AssignIntValuesNode(14, SubIntValuesNode(GetIntValuesNode(8),GetIntValuesNode(6))) + iastore + + ; AssignIntValuesNode(15, TailIntValuesNode(GetIntValuesNode(14),GetIntValuesConstantNode(1))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 15 + + ; TailIntValuesNode(GetIntValuesNode(14), GetIntValuesConstantNode(1)) + + ; GetIntValuesNode(14) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 14 + iaload + + + ; GetIntValuesConstantNode(1) + iconst_1 + + ; finish TailIntValuesNode(GetIntValuesNode(14), GetIntValuesConstantNode(1)) + invokestatic firrtl_interpreter/executable/OperationImplementations/tailInt(II)I + + ; finish AssignIntValuesNode(15, TailIntValuesNode(GetIntValuesNode(14),GetIntValuesConstantNode(1))) + iastore + + ; AssignIntValuesNode(17, EqIntValuesNode(GetIntValuesNode(8),GetIntValuesConstantNode(0))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 17 + + ; EqIntValuesNode(GetIntValuesNode(8), GetIntValuesConstantNode(0)) + + ; GetIntValuesNode(8) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 8 + iaload + + + ; GetIntValuesConstantNode(0) + iconst_0 + + ; finish EqIntValuesNode(GetIntValuesNode(8), GetIntValuesConstantNode(0)) + invokestatic firrtl_interpreter/executable/OperationImplementations/eqInt(II)I + + ; finish AssignIntValuesNode(17, EqIntValuesNode(GetIntValuesNode(8),GetIntValuesConstantNode(0))) + iastore + + ; AssignIntValuesNode(18, MuxIntValuesNode(GetIntValuesNode(9),GetIntValuesNode(11),GetIntValuesNode(6))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 18 + + ; MuxIntValuesNode(GetIntValuesNode(9), GetIntValuesNode(11), GetIntValuesNode(6)) + + ; GetIntValuesNode(9) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 9 + iaload + + + ; GetIntValuesNode(11) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 11 + iaload + + + ; GetIntValuesNode(6) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 6 + iaload + + ; finish MuxIntValuesNode(GetIntValuesNode(9), GetIntValuesNode(11), GetIntValuesNode(6)) + invokestatic firrtl_interpreter/executable/OperationImplementations/muxInt(III)I + + ; finish AssignIntValuesNode(18, MuxIntValuesNode(GetIntValuesNode(9),GetIntValuesNode(11),GetIntValuesNode(6))) + iastore + + ; AssignIntValuesNode(19, MuxIntValuesNode(GetIntValuesNode(13),GetIntValuesNode(15),GetIntValuesNode(8))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 19 + + ; MuxIntValuesNode(GetIntValuesNode(13), GetIntValuesNode(15), GetIntValuesNode(8)) + + ; GetIntValuesNode(13) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 13 + iaload + + + ; GetIntValuesNode(15) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 15 + iaload + + + ; GetIntValuesNode(8) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 8 + iaload + + ; finish MuxIntValuesNode(GetIntValuesNode(13), GetIntValuesNode(15), GetIntValuesNode(8)) + invokestatic firrtl_interpreter/executable/OperationImplementations/muxInt(III)I + + ; finish AssignIntValuesNode(19, MuxIntValuesNode(GetIntValuesNode(13),GetIntValuesNode(15),GetIntValuesNode(8))) + iastore + + ; AssignIntValuesNode(3, GetIntValuesNode(6)) + aload_0 + getfield gcd/GCDImpl/values [I + iconst_3 + + ; GetIntValuesNode(6) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 6 + iaload + + ; finish AssignIntValuesNode(3, GetIntValuesNode(6)) + iastore + + ; AssignIntValuesNode(4, GetIntValuesNode(17)) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 4 + + ; GetIntValuesNode(17) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 17 + iaload + + ; finish AssignIntValuesNode(4, GetIntValuesNode(17)) + iastore + + ; AssignIntValuesNode(5, GetIntValuesNode(17)) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 5 + + ; GetIntValuesNode(17) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 17 + iaload + + ; finish AssignIntValuesNode(5, GetIntValuesNode(17)) + iastore + + ; AssignIntValuesNode(5, MuxIntValuesNode(GetIntValuesNode(2),GetIntValuesNode(0),GetIntValuesNode(18))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 5 + + ; MuxIntValuesNode(GetIntValuesNode(2), GetIntValuesNode(0), GetIntValuesNode(18)) + + ; GetIntValuesNode(2) + aload_0 + getfield gcd/GCDImpl/values [I + iconst_2 + iaload + + + ; GetIntValuesNode(0) + aload_0 + getfield gcd/GCDImpl/values [I + iconst_0 + iaload + + + ; GetIntValuesNode(18) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 18 + iaload + + ; finish MuxIntValuesNode(GetIntValuesNode(2), GetIntValuesNode(0), GetIntValuesNode(18)) + invokestatic firrtl_interpreter/executable/OperationImplementations/muxInt(III)I + + ; finish AssignIntValuesNode(5, MuxIntValuesNode(GetIntValuesNode(2),GetIntValuesNode(0),GetIntValuesNode(18))) + iastore + + ; AssignIntValuesNode(7, MuxIntValuesNode(GetIntValuesNode(2),GetIntValuesNode(1),GetIntValuesNode(19))) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 7 + + ; MuxIntValuesNode(GetIntValuesNode(2), GetIntValuesNode(1), GetIntValuesNode(19)) + + ; GetIntValuesNode(2) + aload_0 + getfield gcd/GCDImpl/values [I + iconst_2 + iaload + + + ; GetIntValuesNode(1) + aload_0 + getfield gcd/GCDImpl/values [I + iconst_1 + iaload + + + ; GetIntValuesNode(19) + aload_0 + getfield gcd/GCDImpl/values [I + ldc 19 + iaload + + ; finish MuxIntValuesNode(GetIntValuesNode(2), GetIntValuesNode(1), GetIntValuesNode(19)) + invokestatic firrtl_interpreter/executable/OperationImplementations/muxInt(III)I + + ; finish AssignIntValuesNode(7, MuxIntValuesNode(GetIntValuesNode(2),GetIntValuesNode(1),GetIntValuesNode(19))) + iastore + + + return + + + +;; ; get 0 +;; aload_0 +;; getfield gcd/GCDImpl/values [I +;; iconst_1 +;; iaload +;; istore_1 +;; +;; aload_0 +;; getfield gcd/GCDImpl/values [I +;; iconst_0 +;; iaload +;; istore_2 +;; +;; iload_1 +;; iload_2 +;; invokestatic firrtl_interpreter/executable/OperationImplementations/addInt(II)I +;; istore_2 +;; +;; aload_0 +;; getfield gcd/GCDImpl/values [I +;; iconst_2 +;; iload_2 +;; iastore +;; +;; return +.end method + +; .method public static main([Ljava/lang/String;)V +; .limit stack 3 +; .limit locals 1 +; +; +; getstatic java/lang/System/out Ljava/io/PrintStream; +; aload_0 +; invokevirtual java/io/PrintStream/println([I)V +; +; return +; .end method + diff --git a/src/main/scala/firrtl_interpreter/executable/ConcreteCircuit.scala b/src/main/scala/firrtl_interpreter/executable/ConcreteCircuit.scala index 39fdf41..953ba20 100644 --- a/src/main/scala/firrtl_interpreter/executable/ConcreteCircuit.scala +++ b/src/main/scala/firrtl_interpreter/executable/ConcreteCircuit.scala @@ -2,14 +2,16 @@ package firrtl_interpreter.executable +import gcd._ + import scala.collection.mutable trait BaseValue { - def name: String + // def name: String def size: Int } -case class IntValue(name: String, isSigned: Boolean, size: Int) extends BaseValue { +case class IntValue(size: Int) extends BaseValue { var value: Int = 0 def apply(): Int = value } @@ -18,65 +20,209 @@ case class BigValue(name: String, isSigned: Boolean, size: Int) extends BaseValu var value: BigInt = 0 } -class ConcreteCircuit { - val names: mutable.HashMap[String, IntValue] = new mutable.HashMap[String, IntValue] +class ConcreteCircuit(n: Int) { + val names: mutable.HashMap[String, Int] = new mutable.HashMap[String, Int] + val widths = new Array[Int](n) + val values = new Array[Int](n) - def getIndex(name: String): IntValue = names(name) + def getIndex(name: String): Int = names(name) + def get(name: String): Int = values(names(name)) + def get(idx: Int): Int = values(idx) def header: String = { names.keys.toArray.sorted.map { name => f"$name%10.10s" }.mkString("") } override def toString: String = { - names.keys.toArray.sorted.map(names(_).value).map { b => f"$b%10d" }.mkString("") + names.keys.toArray.sorted.map(get(_)).map { b => f"$b%10d" }.mkString("") + } +} + +object OperationImplementations { + def main(arg: Array[String]): Unit = { + val a = new GCDImpl(Array(1,2,3,4,5)) + println(a.values) + } + def getIntConst(n: Int): String = { + if (n < 4) { + s"iconst_$n" + } else { + s"ldc $n" + } + } + def outputJasmin(node: Node): String = node match { + case GetIntValuesConstantNode(n) => + s""" + ; GetIntValuesConstantNode($n) + ${getIntConst(n)} + """ + case GetIntValuesNode(idx) => + s""" + ; GetIntValuesNode($idx) + aload_0 + getfield gcd/GCDImpl/values [I + ${getIntConst(idx)} + iaload + """ + case AddIntValuesNode(n1, n2) => + s""" + ; AddIntValuesNode($n1, $n2) + ${outputJasmin(n1)} + ${outputJasmin(n2)} + ; finish AddIntValuesNode($n1, $n2) + invokestatic firrtl_interpreter/executable/OperationImplementations/addInt(II)I + """ + case SubIntValuesNode(n1, n2) => + s""" + ; SubIntValuesNode($n1, $n2) + ${outputJasmin(n1)} + ${outputJasmin(n2)} + ; finish SubIntValuesNode($n1, $n2) + invokestatic firrtl_interpreter/executable/OperationImplementations/subInt(II)I + """ + case TailIntValuesNode(n1, n2) => + s""" + ; TailIntValuesNode($n1, $n2) + ${outputJasmin(n1)} + ${outputJasmin(n2)} + ; finish TailIntValuesNode($n1, $n2) + invokestatic firrtl_interpreter/executable/OperationImplementations/tailInt(II)I + """ + case MuxIntValuesNode(n1, n2, n3) => + s""" + ; MuxIntValuesNode($n1, $n2, $n3) + ${outputJasmin(n1)} + ${outputJasmin(n2)} + ${outputJasmin(n3)} + ; finish MuxIntValuesNode($n1, $n2, $n3) + invokestatic firrtl_interpreter/executable/OperationImplementations/muxInt(III)I + """ + case EqIntValuesNode(n1, n2) => + s""" + ; EqIntValuesNode($n1, $n2) + ${outputJasmin(n1)} + ${outputJasmin(n2)} + ; finish EqIntValuesNode($n1, $n2) + invokestatic firrtl_interpreter/executable/OperationImplementations/eqInt(II)I + """ + case GtIntValuesNode(n1, n2) => + s""" + ; GtIntValuesNode($n1, $n2) + ${outputJasmin(n1)} + ${outputJasmin(n2)} + ; finish GtIntValuesNode($n1, $n2) + invokestatic firrtl_interpreter/executable/OperationImplementations/gtInt(II)I + """ + case AssignIntValuesNode(index, expr) => + s""" + ; AssignIntValuesNode($index, $expr) + aload_0 + getfield gcd/GCDImpl/values [I + ${getIntConst(index)} + ${outputJasmin(expr)} + ; finish AssignIntValuesNode($index, $expr) + iastore + """ + } + def outputJasmin(ast: Seq[Node]): String = { + ast.foldLeft(""){case (str, node) => str + outputJasmin(node) } + "\n\nreturn" + } + def addInt(in1: Int, in2: Int): Int = { + in1 + in2 + } + def subInt(in1: Int, in2: Int): Int = { + in1 - in2 + } + def tailInt(in1: Int, in2: Int): Int = { + in1 + } + def muxInt(sel: Int, trueBranch: Int, falseBranch: Int): Int = { + if (sel != 0) trueBranch else falseBranch + } + def eqInt(in1: Int, in2: Int): Int = { + if (in1 == in2) 1 else 0 + } + def gtInt(in1: Int, in2: Int): Int = { + if (in1 > in2) 1 else 0 } } -case class GetIntValuesConstant(n: Int) { +sealed trait Node +case class GetIntValuesConstantNode(n: Int) extends Node +class GetIntValuesConstant(n: Int) { def apply(): Int = n } +object GetIntValuesConstant { + def apply(n: Int) = new GetIntValuesConstant(n) +} -case class GetIntValues(state: ConcreteCircuit, intValue: IntValue) { +case class GetIntValuesNode(idx: Int) extends Node +case class GetIntValues(state: ConcreteCircuit, idx: Int) { val apply: () => Int = { if(true) nakedGetIntValues else verboseGetIntValues } def nakedGetIntValues(): Int = { - intValue.value + state.values(idx) } def verboseGetIntValues(): Int = { - println(s"getting int from index ${intValue.value}") - intValue.value + println(s"getting int from index $idx = ${state.values(idx)}") + state.values(idx) } } -case class AddIntValues(f1: () => Int, f2: () => Int) { +case class AddIntValuesNode(f1: Node, f2: Node) extends Node +class AddIntValues(f1: () => Int, f2: () => Int) { def apply(): Int = f1() + f2() } +object AddIntValues { + def apply(f1: () => Int, f2: () => Int) = new AddIntValues(f1, f2) +} -case class SubIntValues(f1: () => Int, f2: () => Int) { +case class SubIntValuesNode(f1: Node, f2: Node) extends Node +class SubIntValues(f1: () => Int, f2: () => Int) { def apply(): Int = f1() - f2() } +object SubIntValues { + def apply(f1: () => Int, f2: () => Int) = new SubIntValues(f1, f2) +} -case class TailIntValues(f1: () => Int, f2: () => Int) { +case class TailIntValuesNode(f1: Node, f2: Node) extends Node +class TailIntValues(f1: () => Int, f2: () => Int) { def apply(): Int = f1() } +object TailIntValues { + def apply(f1: () => Int, f2: () => Int) = new TailIntValues(f1, f2) +} -case class MuxIntValues(condition: () => Int, trueClause: () => Int, falseClause: () => Int) { +case class MuxIntValuesNode(cond: Node, trueClause: Node, falseClause: Node) extends Node +class MuxIntValues(condition: () => Int, trueClause: () => Int, falseClause: () => Int) { def apply(): Int = if(condition() > 0) trueClause() else falseClause() } +object MuxIntValues { + def apply(condition: () => Int, trueClause: () => Int, falseClause: () => Int) = new MuxIntValues(condition, trueClause, falseClause) +} -case class EqIntValues(f1: () => Int, f2: () => Int) { +case class EqIntValuesNode(f1: Node, f2: Node) extends Node +class EqIntValues(f1: () => Int, f2: () => Int) { def apply(): Int = if(f1() == f2()) 1 else 0 } +object EqIntValues { + def apply(f1: () => Int, f2: () => Int) = new EqIntValues(f1, f2) +} -case class GtIntValues(f1: () => Int, f2: () => Int) { +case class GtIntValuesNode(f1: Node, f2: Node) extends Node +class GtIntValues(f1: () => Int, f2: () => Int) { def apply(): Int = if(f1() > f2()) 1 else 0 } +object GtIntValues { + def apply(f1: () => Int, f2: () => Int) = new GtIntValues(f1, f2) +} -case class AssignIntValues(state: ConcreteCircuit, index: IntValue, expression: () => Int) extends Assigner { +case class AssignIntValuesNode(index: Int, expression: Node) extends Node +case class AssignIntValues(state: ConcreteCircuit, index: Int, expression: () => Int) extends Assigner { def apply(): Unit = { // println(s"assign index $index ${state.names.values.find(_.index == index).get.name} ${expression()}") - index.value = expression() + state.values(index) = expression() } } @@ -86,7 +232,7 @@ object ConcreteCircuit { val (bigWireCount, intWireCount) = nameMap.values.foldLeft((0, 0)) { case ((aCount, bCount), wireValue) => if(wireValue.bitSize > 32) (aCount + 1, bCount) else (aCount, bCount + 1) } - new ConcreteCircuit + new ConcreteCircuit(nameMap.size) } def runOnce(values: Seq[(Int, Int, Int)]): Unit ={ @@ -94,163 +240,275 @@ object ConcreteCircuit { def newNextWire() = { nextWire += 1; nextWire } val wires = Seq( - IntValue("io_a", isSigned = false, 32), - IntValue("io_b", isSigned = false, 32), - IntValue("io_e", isSigned = false, 32), - IntValue("io_z", isSigned = false, 32), - IntValue("io_v", isSigned = false, 32), - IntValue("reg_x_in", isSigned = false, 32), - IntValue("reg_x_out", isSigned = false, 32), - IntValue("reg_y_in", isSigned = false, 32), - IntValue("reg_y_out", isSigned = false, 32), - IntValue("t_13", isSigned = false, 32), - IntValue("t_14", isSigned = false, 32), - IntValue("t_15", isSigned = false, 32), - IntValue("t_16", isSigned = false, 32), - IntValue("t_17", isSigned = false, 32), - IntValue("t_18", isSigned = false, 32), - IntValue("t_19", isSigned = false, 32), - IntValue("t_20", isSigned = false, 32), - IntValue("t_21", isSigned = false, 32), - IntValue("gen_0", isSigned = false, 32), - IntValue("gen_1", isSigned = false, 32) + "io_a" -> IntValue(32), + "io_b" -> IntValue(32), + "io_e" -> IntValue(32), + "io_z" -> IntValue(32), + "io_v" -> IntValue(32), + "reg_x_in" -> IntValue(32), + "reg_x_out" -> IntValue(32), + "reg_y_in" -> IntValue(32), + "reg_y_out" -> IntValue(32), + "t_13" -> IntValue(32), + "t_14" -> IntValue(32), + "t_15" -> IntValue(32), + "t_16" -> IntValue(32), + "t_17" -> IntValue(32), + "t_18" -> IntValue(32), + "t_19" -> IntValue(32), + "t_20" -> IntValue(32), + "t_21" -> IntValue(32), + "gen_0" -> IntValue(32), + "gen_1" -> IntValue(32) ) - val state = new ConcreteCircuit - wires.foreach { wire => state.names(wire.name) = wire} + val state = new ConcreteCircuit(wires.length) + wires.zipWithIndex.foreach { case ((name, wire), idx) => + state.names(name) = idx + state.values(idx) = 0 + state.widths(idx) = wire.size + } // println(s"state 0 $state") - val instructions = Seq( - AssignIntValues(state, state.getIndex("t_13"), - GtIntValues( - GetIntValues(state, state.getIndex("reg_x_out")).apply, - GetIntValues(state, state.getIndex("reg_y_out")).apply).apply _ - ), - AssignIntValues(state, state.getIndex("t_14"), - SubIntValues( - GetIntValues(state, state.getIndex("reg_x_out")).apply, - GetIntValues(state, state.getIndex("reg_y_out")).apply).apply _ - ), - AssignIntValues(state, state.getIndex("t_15"), - TailIntValues( - GetIntValues(state, state.getIndex("t_14")).apply, - GetIntValuesConstant(1).apply _ - ).apply _ - ), - AssignIntValues(state, state.getIndex("t_17"), - EqIntValues( - GetIntValues(state, state.getIndex("t_13")).apply, - GetIntValuesConstant(0).apply _ - ).apply _ - ), - AssignIntValues(state, state.getIndex("t_18"), - SubIntValues( - GetIntValues(state, state.getIndex("reg_y_out")).apply, - GetIntValues(state, state.getIndex("reg_x_out")).apply).apply _ - ), - AssignIntValues(state, state.getIndex("t_19"), - TailIntValues( - GetIntValues(state, state.getIndex("t_18")).apply, - GetIntValuesConstant(1).apply _ - ).apply _ - ), - AssignIntValues(state, state.getIndex("t_21"), - EqIntValues( - GetIntValues(state, state.getIndex("reg_y_out")).apply, - GetIntValuesConstant(0).apply _ - ).apply _ - ), - AssignIntValues(state, state.getIndex("gen_0"), - MuxIntValues( - GetIntValues(state, state.getIndex("t_13")).apply, - GetIntValues(state, state.getIndex("t_15")).apply, - GetIntValues(state, state.getIndex("reg_x_out")).apply - ).apply _ - ), - AssignIntValues(state, state.getIndex("gen_1"), - MuxIntValues( - GetIntValues(state, state.getIndex("t_17")).apply, - GetIntValues(state, state.getIndex("t_19")).apply, - GetIntValues(state, state.getIndex("reg_y_out")).apply - ).apply _ - ), - AssignIntValues(state, state.getIndex("io_z"), + val destinations = Array( + state.getIndex("t_13"), + state.getIndex("t_14"), + state.getIndex("t_15"), + state.getIndex("t_17"), + state.getIndex("t_18"), + state.getIndex("t_19"), + state.getIndex("t_21"), + state.getIndex("gen_0"), + state.getIndex("gen_1"), + state.getIndex("io_z"), + state.getIndex("io_v"), + state.getIndex("reg_x_in"), + state.getIndex("reg_x_in"), + state.getIndex("reg_y_in"), + state.getIndex("t_19"), + state.getIndex("t_21"), + state.getIndex("gen_0"), + state.getIndex("gen_1"), + state.getIndex("io_z"), + state.getIndex("io_v"), + state.getIndex("reg_x_in"), + state.getIndex("reg_x_in"), + state.getIndex("reg_y_in") + ) + val instructions = Array( + GtIntValues( + GetIntValues(state, state.getIndex("reg_x_out")).apply, + GetIntValues(state, state.getIndex("reg_y_out")).apply).apply _, + SubIntValues( + GetIntValues(state, state.getIndex("reg_x_out")).apply, + GetIntValues(state, state.getIndex("reg_y_out")).apply).apply _, + TailIntValues( + GetIntValues(state, state.getIndex("t_14")).apply, + GetIntValuesConstant(1).apply _ + ).apply _, + EqIntValues( + GetIntValues(state, state.getIndex("t_13")).apply, + GetIntValuesConstant(0).apply _ + ).apply _, + SubIntValues( + GetIntValues(state, state.getIndex("reg_y_out")).apply, + GetIntValues(state, state.getIndex("reg_x_out")).apply).apply _, + TailIntValues( + GetIntValues(state, state.getIndex("t_18")).apply, + GetIntValuesConstant(1).apply _ + ).apply _ , + EqIntValues( + GetIntValues(state, state.getIndex("reg_y_out")).apply, + GetIntValuesConstant(0).apply _ + ).apply _ , + MuxIntValues( + GetIntValues(state, state.getIndex("t_13")).apply, + GetIntValues(state, state.getIndex("t_15")).apply, GetIntValues(state, state.getIndex("reg_x_out")).apply - ), - AssignIntValues(state, state.getIndex("io_v"), - GetIntValues(state, state.getIndex("t_21")).apply - ), - AssignIntValues(state, state.getIndex("reg_x_in"), - GetIntValues(state, state.getIndex("t_21")).apply - ), - AssignIntValues(state, state.getIndex("reg_x_in"), - MuxIntValues( - GetIntValues(state, state.getIndex("io_e")).apply, - GetIntValues(state, state.getIndex("io_a")).apply, - GetIntValues(state, state.getIndex("gen_0")).apply - ).apply _ - ), - AssignIntValues(state, state.getIndex("reg_y_in"), - MuxIntValues( - GetIntValues(state, state.getIndex("io_e")).apply, - GetIntValues(state, state.getIndex("io_b")).apply, - GetIntValues(state, state.getIndex("gen_1")).apply - ).apply _ - ) + ).apply _ , + MuxIntValues( + GetIntValues(state, state.getIndex("t_17")).apply, + GetIntValues(state, state.getIndex("t_19")).apply, + GetIntValues(state, state.getIndex("reg_y_out")).apply + ).apply _ , + GetIntValues(state, state.getIndex("reg_x_out")).apply, + GetIntValues(state, state.getIndex("t_21")).apply, + GetIntValues(state, state.getIndex("t_21")).apply, + MuxIntValues( + GetIntValues(state, state.getIndex("io_e")).apply, + GetIntValues(state, state.getIndex("io_a")).apply, + GetIntValues(state, state.getIndex("gen_0")).apply + ).apply _ , + MuxIntValues( + GetIntValues(state, state.getIndex("io_e")).apply, + GetIntValues(state, state.getIndex("io_b")).apply, + GetIntValues(state, state.getIndex("gen_1")).apply + ).apply _ ) - val regNextInstructions = Seq( - AssignIntValues(state, state.getIndex("reg_x_out"), - GetIntValues(state, state.getIndex("reg_x_in")).apply - ), - AssignIntValues(state, state.getIndex("reg_y_out"), - GetIntValues(state, state.getIndex("reg_y_in")).apply - ) + val regNextDestinations = Array( + state.getIndex("reg_x_out"), + state.getIndex("reg_y_out") + ) + val regNextInstructions = Array( + GetIntValues(state, state.getIndex("reg_x_in")).apply, + GetIntValues(state, state.getIndex("reg_y_in")).apply ) + val instructionList = Seq[Node]( + AssignIntValuesNode( + state.getIndex("reg_x_out"), + GetIntValuesNode(state.getIndex("reg_x_in"))), + AssignIntValuesNode( + state.getIndex("reg_y_out"), + GetIntValuesNode(state.getIndex("reg_y_in"))), + AssignIntValuesNode( + state.getIndex("t_13"), + GtIntValuesNode( + GetIntValuesNode(state.getIndex("reg_x_out")), + GetIntValuesNode(state.getIndex("reg_y_out")))), + AssignIntValuesNode( + state.getIndex("t_14"), + SubIntValuesNode( + GetIntValuesNode(state.getIndex("reg_x_out")), + GetIntValuesNode(state.getIndex("reg_y_out")))), + AssignIntValuesNode( + state.getIndex("t_15"), + TailIntValuesNode( + GetIntValuesNode(state.getIndex("t_14")), + GetIntValuesConstantNode(1))), + AssignIntValuesNode( + state.getIndex("t_17"), + EqIntValuesNode( + GetIntValuesNode(state.getIndex("t_13")), + GetIntValuesConstantNode(0))), + AssignIntValuesNode( + state.getIndex("t_18"), + SubIntValuesNode( + GetIntValuesNode(state.getIndex("reg_y_out")), + GetIntValuesNode(state.getIndex("reg_x_out")))), + AssignIntValuesNode( + state.getIndex("t_19"), + TailIntValuesNode( + GetIntValuesNode(state.getIndex("t_18")), + GetIntValuesConstantNode(1))), + AssignIntValuesNode( + state.getIndex("t_21"), + EqIntValuesNode( + GetIntValuesNode(state.getIndex("reg_y_out")), + GetIntValuesConstantNode(0))), + AssignIntValuesNode( + state.getIndex("gen_0"), + MuxIntValuesNode( + GetIntValuesNode(state.getIndex("t_13")), + GetIntValuesNode(state.getIndex("t_15")), + GetIntValuesNode(state.getIndex("reg_x_out")))), + AssignIntValuesNode( + state.getIndex("gen_1"), + MuxIntValuesNode( + GetIntValuesNode(state.getIndex("t_17")), + GetIntValuesNode(state.getIndex("t_19")), + GetIntValuesNode(state.getIndex("reg_y_out")))), + AssignIntValuesNode( + state.getIndex("io_z"), + GetIntValuesNode(state.getIndex("reg_x_out"))), + AssignIntValuesNode( + state.getIndex("io_v"), + GetIntValuesNode(state.getIndex("t_21"))), + AssignIntValuesNode( + state.getIndex("reg_x_in"), + GetIntValuesNode(state.getIndex("t_21"))), + AssignIntValuesNode( + state.getIndex("reg_x_in"), + MuxIntValuesNode( + GetIntValuesNode(state.getIndex("io_e")), + GetIntValuesNode(state.getIndex("io_a")), + GetIntValuesNode(state.getIndex("gen_0")))), + AssignIntValuesNode( + state.getIndex("reg_y_in"), + MuxIntValuesNode( + GetIntValuesNode(state.getIndex("io_e")), + GetIntValuesNode(state.getIndex("io_b")), + GetIntValuesNode(state.getIndex("gen_1")))) + ) + // println(OperationImplementations.outputJasmin(instructionList)) + + val imp = new GCDImpl(new Array[Int](20)) + def pokeIdx(idx: Int, value: Int): Unit = { + imp.values(idx) = value + //state.values(idx) = value + } def poke(name: String, value: Int): Unit = { - state.names(name).value = value + pokeIdx(state.getIndex(name), value) + } + def peekIdx(idx: Int): Int = { + imp.values(idx) + // state.values(idx) } def peek(name: String): Int = { - state.names(name).value + peekIdx(state.getIndex(name)) + } + def expectIdx(idx: Int, value: Int, msg: => String) = { + assert(peekIdx(idx) == value, + s"${peekIdx(idx)} did not equal $value, $msg") } def expect(name: String, value: Int, msg: => String) = { - assert(peek(name) == value, - s"${peek(name)} did not equal $value, $msg") + expectIdx(state.getIndex(name), value, msg) } var cycle = 0 def step(): Unit = { - regNextInstructions.foreach { inst => inst() } - instructions.foreach { inst => inst() } + imp.step() + // var i = 0 + // while (i < regNextInstructions.length) { + // state.values(regNextDestinations(i)) = regNextInstructions(i)() + // i += 1 + // } + // i = 0 + // while (i < instructions.length) { + // // instructions(i)() + // state.values(destinations(i)) = instructions(i)() + // i += 1 + // } cycle += 1 } def show(): Unit = { - println(f"state $cycle%6d $state") + println(s"cycle $cycle") + println(s"state ${imp.values.map(_.toString).reduce(_+" "+_)}") + println() + + // println(f"state $cycle%6d $state") } // println(f"state ${""}%6.6s ${state.header}") + val io_a = state.getIndex("io_a") + val io_b = state.getIndex("io_b") + val io_e = state.getIndex("io_e") + val io_v = state.getIndex("io_v") + val io_z = state.getIndex("io_z") + val startTime = System.nanoTime() values.foreach { case (x, y, z) => - poke("io_a", x) - poke("io_b", y) - poke("io_e", 1) + + pokeIdx(io_a, x) + pokeIdx(io_b, y) + pokeIdx(io_e, 1) step() - poke("io_e", 0) + pokeIdx(io_e, 0) step() - while(peek("io_v") != 1) { + while(peekIdx(io_v) != 1) { step() } - expect("io_z", z, s"$x, $y") - // show() + expectIdx(io_z, z, s"$x, $y") + // show() } @@ -280,15 +538,15 @@ object ConcreteCircuit { } val values = - for {x <- 1 to 1000 - y <- 1 to 1000 + for {x <- 1 to 4000 + y <- 1 to 4000 } yield (x, y, computeGcd(x, y)._1) runOnce(values) runOnce(values) runOnce(values) - ExecutableCircuit.runOnce(values) - ExecutableCircuit.runOnce(values) - ExecutableCircuit.runOnce(values) + // ExecutableCircuit.runOnce(values) + // ExecutableCircuit.runOnce(values) + // ExecutableCircuit.runOnce(values) } } diff --git a/src/main/scala/firrtl_interpreter/executable/ExecutableCircuit.scala b/src/main/scala/firrtl_interpreter/executable/ExecutableCircuit.scala index 1b5b8f7..5b43d00 100644 --- a/src/main/scala/firrtl_interpreter/executable/ExecutableCircuit.scala +++ b/src/main/scala/firrtl_interpreter/executable/ExecutableCircuit.scala @@ -72,7 +72,7 @@ object ExecutableCircuit { // println(s"state 0 $state") - val instructions = Seq( + val instructions = Array[Assigner]( AssignInt(state, state.getUInt("t_13"), GtInts( GetInt(state, state.getUInt("reg_x_out")).apply, @@ -145,7 +145,7 @@ object ExecutableCircuit { ) ) - val regNextInstructions = Seq( + val regNextInstructions = Array( AssignInt(state, state.getUInt("reg_x_out"), GetInt(state, state.getUInt("reg_x_in")).apply), AssignInt(state, state.getUInt("reg_y_out"), GetInt(state, state.getUInt("reg_y_in")).apply) ) @@ -166,8 +166,16 @@ object ExecutableCircuit { var cycle = 0 def step(): Unit = { - regNextInstructions.foreach { inst => inst() } - instructions.foreach { inst => inst() } + var i = 0 + while (i < regNextInstructions.length) { + regNextInstructions(i)() + i += 1 + } + i = 0 + while (i < instructions.length) { + instructions(i)() + i += 1 + } cycle += 1 } @@ -193,10 +201,10 @@ object ExecutableCircuit { (x, depth) } - val values = - for {x <- 1 to 1000 - y <- 1 to 1000 - } yield (x, y, computeGcd(x, y)._1) + // val values = + // for {x <- 1 to 1000 + // y <- 1 to 1000 + // } yield (x, y, computeGcd(x, y)._1) val startTime = System.nanoTime() @@ -246,8 +254,8 @@ object ExecutableCircuit { } val values = - for {x <- 1 to 1000 - y <- 1 to 1000 + for {x <- 1 to 1500 + y <- 1 to 1500 } yield (x, y, computeGcd(x, y)._1) runOnce(values)