Can I define something like MACRO like C?

73 views
Skip to first unread message

Kentaro Sano

unread,
Oct 28, 2014, 1:42:38 PM10/28/14
to chisel...@googlegroups.com
Hi,

I am making a command class extending Bundle, which contains "op" (operation).
In a code using this class, I want to compare "op" with defined MACROs like:


class bWrDMA_comm extends Bundle {
  val addr      = UInt(width=40)
  val bytes     = UInt(width=16)
  val op        = UInt(width=4)
}

// This is in C, but shows what I want to do.
#define NOP   UInt("h_0", width=4)
#define WRITE UInt("h_1", width=4)
#define CLEAR UInt("h_2", width=4)

...
var cmd = new bWrDMA_comm

when (cmd.op === NOP) {
  ...
} .elsewhen (cmd.op === WRITE) {
  ...
} .elsewhen (cmd.op === CLEAR) {
  ...
} .otherwise {
  ...
}

However, I don't know what should I write in Chisel/scala.
First, I try to use "def" like:

class bWrDMA_comm extends Bundle {
  val addr      = UInt(width=40)
  val bytes     = UInt(width=16)
  val op        = UInt(width=4)
  def NOP()     = UInt("h_0", width=4)
  def WRITE()   = UInt("h_1", width=4)
  def CLEAR()   = UInt("h_2", width=4)
}

But this code failed in exeption error in executing an emulation.
Could you tell me the best way for this?

======== ERROR MESSAGES ========
[info] Running StdModules.mRemoteMM.main mWrDMA --genHarness --compile --test --backend c
[error] (run-main-0) Chisel.ChiselException: Node.getWidth() for node /*? in class StdModules.mRemoteMM.mWrDMA*/ Chisel.UInt(OUTPUT, width=None, connect to 0 inputs: ()) returns unknown width
Chisel.ChiselException: Node.getWidth() for node /*? in class StdModules.mRemoteMM.mWrDMA*/ Chisel.UInt(OUTPUT, width=None, connect to 0 inputs: ()) returns unknown width
        at StdModules.ioAvlST_src.<init>(StdQsysIO.scala:27)
[trace] Stack trace suppressed: run 'last compile:run' for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
        at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run 'last compile:run' for the full output.
[error] (compile:run) Nonzero exit code: 1
[error] Total time: 6 s, completed 2014/10/29 2:39:59


Thanks in advance.

Kentaro

Simon Scott

unread,
Oct 28, 2014, 3:04:38 PM10/28/14
to chisel...@googlegroups.com
Hi Kentaro

I personally just use scala vals, such as:
 val NOP = UInt(0, width=4)

Hope this helps.
Simon

--
You received this message because you are subscribed to the Google Groups "chisel-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chisel-users...@googlegroups.com.
To post to this group, send email to chisel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chisel-users/c56df9cc-78ab-46dd-9f2e-62a3242e1225%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kentaro Sano

unread,
Oct 29, 2014, 2:33:51 AM10/29/14
to chisel...@googlegroups.com
Hi Simon,

Thank you for your suggestion.
I tried to use "val" in the extended Bundle class:

class bWrDMA_comm extends Bundle {
  val addr      = UInt(width=40)
  val bytes     = UInt(width=16)
  val op        = UInt(width=4)
  val op_NOP    = UInt("h_0", width=4)  // assertion error
  val op_WRITE  = UInt("h_1", width=4) // assertion error
}

This also failed in the assertion error.
[error] (run-main-0) java.lang.AssertionError: assertion failed

When I wrote this in another class which uses the bWrDMA class, it worked.
However, if possible, I want to define the constant signals such as "NOP", "WRITE", and "CLEAR" within the bWrDMA class.
Is it not allowd to write literals in the Bundle class?

--
Kentaro





2014年10月29日水曜日 4時04分38秒 UTC+9 Simon Scott:

Simon Scott

unread,
Oct 29, 2014, 8:26:35 PM10/29/14
to chisel...@googlegroups.com
Hi Kentaro

I don't know of any way to define constants/literals within a bundle. I just usually create a constant class and store my constants in there.

Since bundles are usually used for defining I/O ports, I'm not sure how much sense it makes to define constant literals in them. However, maybe someone else knows a way to make it work.

Kentaro Sano

unread,
Oct 29, 2014, 9:29:22 PM10/29/14
to chisel...@googlegroups.com
Hi Simon,

> I just usually create a constant class and store my constants in there.

I will also try this approach.

> I'm not sure how much sense it makes to define constant literals in them.

This is based just on an idea that definition of constants (operation codes) is attached to the command class extended from a bundle class which includes a operation signal. I beleive that this makes the class more portable.
For now, I will degfine a constant class  dedicated to the command class.

Thank you!

--
Kentaro

2014年10月30日木曜日 9時26分35秒 UTC+9 Simon Scott:
Reply all
Reply to author
Forward
0 new messages