1

Is there a method to pass strings in Chisel? For example, I want to pass a string ATGC and get the output as 0 for A, 1 for T, 2 for G and 3 for C. Is this possible? If yes, can anyone please elucidate? Thank you.

Using the code in the answer gives these errors:

[error] java.lang.NoSuchMethodException:  problems.ATGCHandler.main([Ljava.lang.String;)
[error]     at java.lang.Class.getMethod(Class.java:1786)
[error]     at sbt.Run.getMainMethod(Run.scala:99)
[error]     at sbt.Run.run0(Run.scala:86)
[error]     at sbt.Run.execute$1(Run.scala:65)
[error]     at sbt.Run.$anonfun$run$3(Run.scala:69)
[error]     at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[error]     at scala.util.Try$.apply(Try.scala:209)
[error]     at sbt.Run.directExecute$1(Run.scala:69)
[error]     at sbt.Run.run(Run.scala:78)
[error]     at sbt.Defaults$.$anonfun$bgRunMainTask$6(Defaults.scala:1147)
[error]     at sbt.Defaults$.$anonfun$bgRunMainTask$6$adapted(Defaults.scala:1142)
[error]     at sbt.internal.BackgroundThreadPool.$anonfun$run$1(DefaultBackgroundJobService.scala:366)
[error]     at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[error]     at scala.util.Try$.apply(Try.scala:209)
[error]     at sbt.internal.BackgroundThreadPool$BackgroundRunnable.run(DefaultBackgroundJobService.scala:289)
[error]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[error]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[error]     at java.lang.Thread.run(Thread.java:748)
[error] (Test / runMain) java.lang.NoSuchMethodException: problems.ATGCHandler.main([Ljava.lang.String;)

1 Answer 1

3

Chisel has some limited support for Characters which can be used to do String-like work. With careful handling of the encoding, you can write stuff like:

import chisel3._
import chisel3.util._
class ATGCHandler extends Module {
  val io = IO(new Bundle {
    val char = Input(UInt(8.W))
    val encoding = Output(Valid(UInt(2.W)))
  })
  // Defaults
  io.encoding.valid := false.B
  io.encoding.bits := DontCare
  // Encode Valid Inputs
  switch (io.char) {
    is ('A'.U) {
      io.encoding.valid := true.B
      io.encoding.bits := 0.U
    }
    is ('T'.U) {
      io.encoding.valid := true.B
      io.encoding.bits := 1.U
    }
    is ('G'.U) {
      io.encoding.valid := true.B
      io.encoding.bits := 2.U
    }
    is ('C'.U) {
      io.encoding.valid := true.B
      io.encoding.bits := 3.U
    }
  }
}

Or if you're feeling particularly Chisel-y, you could write the encoding as something like:

  // Encode Valid Inputs
  val mapping = Map('A' -> 0, 'T' -> 1, 'G' -> 2, 'C' -> 3)
  mapping.foreach { case (key, value) =>
    when (io.char === key.U) {
      io.encoding.valid := true.B
      io.encoding.bits := value.U
    }
  }

EDIT: To build this, you can use the following main function:

object ATGCDecoderMain {
  def main(args: Array[String]): Unit = {
    chisel3.Driver.execute(args, () => new ATGCDecoder)
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but it shows quite a few errors. I have included them in the question. It would be great if you could help.
As the error indicates, you need a main function. I've added an example one that compiles the ATGCDecoer to Verilog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.