Skip to content

Commit b582253

Browse files
Add alu
1 parent 589aac4 commit b582253

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

src/main/alu.scala

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//> using scala "2.13.12"
2+
//> using dep "org.chipsalliance::chisel:6.7.0"
3+
//> using plugin "org.chipsalliance:::chisel-plugin:6.7.0"
4+
//> using options "-unchecked", "-deprecation", "-language:reflectiveCalls", "-feature", "-Xcheckinit", "-Xfatal-warnings", "-Ywarn-dead-code", "-Ywarn-unused", "-Ymacro-annotations"
5+
6+
import chisel3._
7+
import chisel3.util._
8+
import _root_.circt.stage.ChiselStage
9+
10+
object AluOperation extends ChiselEnum {
11+
val Add, Sub, Mul, Div = Value
12+
}
13+
14+
class Alu extends Module {
15+
val io = IO(new Bundle {
16+
val enable = Input(Bool())
17+
18+
val operation = Input(AluOperation())
19+
val compare = Input(Bool())
20+
21+
val rs = Input(UInt(8.W))
22+
val rt = Input(UInt(8.W))
23+
24+
val output = Output(UInt(8.W))
25+
})
26+
27+
io.output := 0.U
28+
29+
when(io.enable) {
30+
when(io.compare) {
31+
val gt = io.rs > io.rt
32+
val eq = io.rs === io.rt
33+
val lt = io.rs < io.rt
34+
35+
io.output := Cat(0.U(5.W), gt, eq, lt);
36+
}.otherwise {
37+
switch(io.operation) {
38+
is(AluOperation.Add) {
39+
io.output := io.rs + io.rt
40+
}
41+
42+
is(AluOperation.Sub) {
43+
io.output := io.rs - io.rt
44+
}
45+
46+
is(AluOperation.Mul) {
47+
io.output := io.rs * io.rt
48+
}
49+
50+
is(AluOperation.Div) {
51+
io.output := io.rs / io.rt
52+
}
53+
}
54+
}
55+
}
56+
}
57+
58+
object Main extends App {
59+
println(
60+
ChiselStage.emitSystemVerilog(
61+
gen = new Alu,
62+
firtoolOpts = Array("-disable-all-randomization", "-strip-debug-info")
63+
)
64+
)
65+
}

0 commit comments

Comments
 (0)