0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ChiselでSECDマシンに4倍精度浮動小数点計算を組み込む(バグはあるかも…)

Posted at

ハードウェア記述言語

世の中には様々な技術がありまして、FPGAなるものもあるようです。
今回は、テスト自体はしてませんが、chatGPTにコーディングを手伝ってもらいつつ、Chiselでタイトルにある通りのコードを眺めてみました。

import chisel3._
import chisel3.util._

// Define the SECD machine
class SECD extends Module {
  val io = IO(new Bundle {
    val input  = Input(UInt(8.W))
    val output = Output(UInt(8.W))
  })

  // Stack, Environment, Control, Dump
  val stack = RegInit(VecInit(Seq.fill(4)(0.U(8.W))))
  val env = RegInit(VecInit(Seq.fill(4)(0.U(8.W))))
  val control = RegInit(VecInit(Seq.fill(4)(0.U(8.W))))
  val dump = RegInit(VecInit(Seq.fill(4)(0.U(8.W))))

  // FullAdder
  val (sum, carry) = FullAdder(stack(0), stack(1), 0.U)

  // Bitwise Operations
  val andRes = stack(0) & stack(1)
  val orRes  = stack(0) | stack(1)
  val xorRes = stack(0) ^ stack(1)
  val shiftLeftRes = stack(0) << 1
  val shiftRightRes = stack(0) >> 1
  val bitInvRes = ~stack(0)

  // Control logic (simplified)
  switch(control(0)) {
    is(0.U) { // NOP
    }
    is(1.U) { // ADD
      stack(0) := sum
      stack(1) := carry
    }
    is(2.U) { // AND
      stack(0) := andRes
    }
    is(3.U) { // OR
      stack(0) := orRes
    }
    is(4.U) { // XOR
      stack(0) := xorRes
    }
    is(5.U) { // Shift Left
      stack(0) := shiftLeftRes
    }
    is(6.U) { // Shift Right
      stack(0) := shiftRightRes
    }
    is(7.U) { // Bit Inversion
      stack(0) := bitInvRes
    }
  }

  io.output := stack(0)
}

// FullAdder definition
def FullAdder(a: SInt, b: SInt, cin: SInt): (SInt, SInt) = {
  val sum = a ^ b ^ cin
  val carry = (a & b) | (cin & (a ^ b))
  (sum, carry)
}

たぶんバグはあるでしょうが、この程度のコードが秒で生成出来るとか恐ろしいものです…
人間の側も進歩したほうが良さそうですね…

関数型言語の設計中の思いつきなのでお遊び程度です💦
一種のポエムですので、お手柔らかにお願いします💦

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?