top of page

[Chisel]ポリモーフィズムは使わない


やりたいこと

  • 類似したユニットが複数ある

  • これらの処理は大部分が共通で一部のみ異なる

  • 条件に応じて適切なユニットを1つ選び、処理を実行させる


やろうとしたこと

これを実現するために

  • 親クラスを定義し、共通処理はここに記述

  • 各ユニットは上記を継承する子クラスとして定義し、独自の処理はここに記述

  • どのユニットに処理を実行させるかを決定

  • 「実行するユニット」を表す変数を定義。型は親クラス。

  • 選ばれたユニットを「実行するユニット」に割り当て、処理を実行させる


ということをしようとした。いわゆるポリモーフィズムというものだ。

class UnitBase{

}

class Unit1 extends UnitBase{

}

class Unit2 extends UnitBase{

}

val selectedUnit = Module(new UnitBase)
when(...){
  selectedUnit := Module(new Unit1)  //※
}

ただしこれは※のところで「型が違う」というコンパイルエラーになる。chiselコンパイラの型チェックは厳密らしい。



解決策

以前の「ループを途中で抜けたい」の時と同じで、これも「ハードウェアをどう操作するか」という観点で考えられていないから起こる。

現実の回路で「『実行ユニット』の枠が1つあって、条件に応じてどれか一つがそこに当てはめられる」なんて動作はしない。


「やりたいこと」をやるならば、

  • 全てのユニットに必要な信号は接続しておく

  • さらに「処理を行うか否か」の有効信号を用意する。

  • 選択されたユニットの有効信号のみをオンにする

というようにすればよい。


class UnitBase{
  val io = IO(new Bundle {
    val sig = Input(UInt(32.W))
    val enb = Input(Bool())
  }
}

val units:Seq[UnitBase] = Seq(Module(new Unit1), Module(new Unit2))
Seq.tabule(4){i => {
    units(i).sig := ...
    units(i).enb := Mux(..., ture.B, false.B)
  }
}

Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Inquiries: Please contact us on Twitter

  • Twitter
bottom of page