Skip to content

Commit

Permalink
fixed point module 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
js4ngu committed Sep 11, 2024
1 parent e7b98ac commit 8a2ec15
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 31 deletions.
6 changes: 6 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ scalacOptions ++= Seq(
addCompilerPlugin("edu.berkeley.cs" % "chisel3-plugin" % "3.5.6" cross CrossVersion.full)
libraryDependencies += "edu.berkeley.cs" %% "chisel3" % "3.5.6"
libraryDependencies += "edu.berkeley.cs" %% "chiseltest" % "0.5.6"

// fixedpoint library
resolvers ++= Seq(
Resolver.sonatypeRepo("snapshots"),
Resolver.sonatypeRepo("releases")
)
1 change: 0 additions & 1 deletion quartus/add.qpf

This file was deleted.

21 changes: 0 additions & 21 deletions quartus/add.qsf

This file was deleted.

9 changes: 1 addition & 8 deletions src/main/scala/vfrope/RoPEModule.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/*
* Dummy file to start a Chisel project.
*
* Author: Martin Schoeberl ([email protected])
*
*/

package vfrope

import chisel3._
Expand Down Expand Up @@ -33,7 +26,7 @@ class RoPEModule_Int(width:Int) extends Module{
sinCosLUT.io.angle := m_theta_i
sinVal := sinCosLUT.io.sinOut
cosVal := sinCosLUT.io.cosOut

outReg(0) := inReg(0) * cosVal - inReg(1) * sinVal
outReg(1) := inReg(1) * cosVal + inReg(0) * sinVal

Expand Down
25 changes: 25 additions & 0 deletions src/main/scala/vfrope/fixedpoint.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package vfrope

import chisel3._
import chisel3.experimental.FixedPoint
import chisel3.util.Cat

class FixedPointAdder(width: Int, binaryPoint: Int) extends Module {
val io = IO(new Bundle {
val a = Input(FixedPoint(width.W, binaryPoint.BP))
val b = Input(FixedPoint(width.W, binaryPoint.BP))
val out = Output(FixedPoint((width+1).W, binaryPoint.BP))
})

io.out := io.a + io.b
}

class FixedPointMultiplier(width: Int, binaryPoint: Int) extends Module {
val io = IO(new Bundle {
val a = Input(FixedPoint(width.W, binaryPoint.BP))
val b = Input(FixedPoint(width.W, binaryPoint.BP))
val out = Output(FixedPoint((2*width).W, (2*binaryPoint).BP))
})

io.out := io.a * io.b
}
1 change: 1 addition & 0 deletions src/main/scala/vfrope/sincosLUT.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package vfrope

import chisel3._
import chisel3.util._

Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/vfrope/RoPEModuleTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RoPEModuleTester extends AnyFlatSpec with ChiselScalatestTester {
dut.io.i.poke(0.U)
dut.io.in(0).poke(1.U)
dut.io.in(1).poke(1.U)
dut.clock.step(10)
dut.clock.step(2)

// Fetch io.out(0) and io.out(1) values and print them correctly
val out0 = dut.io.out(0).peek().litValue()
Expand Down
53 changes: 53 additions & 0 deletions src/test/scala/vfrope/fixedpointTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package vfrope

import chisel3._
import chiseltest._
import org.scalatest.flatspec.AnyFlatSpec
import chisel3.experimental.FixedPoint
import chisel3.util.Cat

class FixedPointArithmeticTest extends AnyFlatSpec with ChiselScalatestTester {
behavior of "FixedPointAdder"

it should "correctly add fixed point numbers" in {
test(new FixedPointAdder(16, 8)) { c =>
def fp(n: Double): BigInt = BigInt((n * (1 << 8)).round.toInt)

val testCases = Seq(
(1.5, 2.25, 3.75),
(-1.5, 2.25, 0.75),
(3.25, -1.75, 1.5),
(-2.5, -1.75, -4.25)
)

for ((a, b, expected) <- testCases) {
c.io.a.poke(FixedPoint.fromDouble(a, 16.W, 8.BP))
c.io.b.poke(FixedPoint.fromDouble(b, 16.W, 8.BP))
c.clock.step()
c.io.out.expect(FixedPoint.fromDouble(expected, 17.W, 8.BP))
}
}
}

behavior of "FixedPointMultiplier"

it should "correctly multiply fixed point numbers" in {
test(new FixedPointMultiplier(16, 8)) { c =>
def fp(n: Double): BigInt = BigInt((n * (1 << 8)).round.toInt)

val testCases = Seq(
(1.5, 2.25, 3.375),
(-1.5, 2.25, -3.375),
(3.25, -1.75, -5.6875),
(-2.5, -1.75, 4.375)
)

for ((a, b, expected) <- testCases) {
c.io.a.poke(FixedPoint.fromDouble(a, 16.W, 8.BP))
c.io.b.poke(FixedPoint.fromDouble(b, 16.W, 8.BP))
c.clock.step()
c.io.out.expect(FixedPoint.fromDouble(expected, 32.W, 16.BP))
}
}
}
}

0 comments on commit 8a2ec15

Please sign in to comment.