Skip to content

ALU

import dfhdl.* //import all the DFHDL goodness

enum ALUSel extends Encode:
  case ADD, SUB, SLL, SRL, SRA, AND, OR, XOR, SLT, SLTU, COPY1

class ALU extends DFDesign:
  val op1 = Bits(32) <> IN
  val op2 = Bits(32) <> IN
  val aluSel = ALUSel <> IN
  val aluOut = Bits(32) <> OUT

  private val shamt = op2(4, 0)

  import ALUSel.*
  val outCalc: Bits[32] <> VAL = aluSel match
    case ADD   => op1 + op2
    case SUB   => op1 - op2
    case AND   => op1 & op2
    case OR    => op1 | op2
    case XOR   => op1 ^ op2
    case SLT   => (op1.sint < op2.sint).extend
    case SLTU  => (op1 < op2).extend
    case SLL   => op1 << shamt
    case SRL   => op1 >> shamt
    case SRA   => (op1.sint >> shamt).bits
    case COPY1 => op1
    case _     => ?
  aluOut := outCalc
end ALU

////////////////////////////////////////////////////////////////////////////////////////////////
// DFHDL Compiler Options:                                                                    //
////////////////////////////////////////////////////////////////////////////////////////////////
// Enables printing the generated chosen backend code:
given options.CompilerOptions.PrintGenFiles = true
// Uncomment to select vhdl compilation (default is verilog):
// given options.CompilerOptions.Backend = backends.vhdl
// Uncomment to enable printing design code before compilation (after elaboration):
// given options.CompilerOptions.PrintDesignCodeBefore = true
// Uncomment to enable printing design code after compilation:
// given options.CompilerOptions.PrintDesignCodeAfter = true
////////////////////////////////////////////////////////////////////////////////////////////////

//The entry point to your compilation program starts here
@main def main = ALU().compile