class MyClass(argS: String, argI: Int) {
val name = argS
println("Created " + argS)
}
로 선언해도, 클래스 자체는 된다.
val absX = Mux(x < 0.S, -x, x)
val invX = if (invert) -x else x
class MyXOR extends Module {
val io = IO(new Bundle {
val a = Input(Bool())
val b = Input(Bool())
val c = Output(Bool())
})
val myGate = io.a ^ io.b
io.c := myGate
}
// println(getVerilog(new MyXOR))
class MyXOR2 extends Module {
val io = IO(new Bundle {
val a = Input(Bool())
val b = Input(Bool())
val c = Output(Bool())
})
val myWire = Wire(Bool())
myWire := io.a ^ io.b
io.c := myWire
}
println(getVerilog(new MyXOR2))
class MyWMux(w: Int) extends Module {
val io = IO(new Bundle {
val s = Input(Bool())
val in0 = Input(UInt(w.W))
val in1 = Input(UInt(w.W))
val out = Output(UInt(w.W))
})
when (io.s) {
io.out := io.in1
} .otherwise {
io.out := io.in0
}
}
println(getVerilog(new MyWMux(8))
class LastC extends Module {
val io = IO(new Bundle {
val x = Input(Bool())
val y = Output(UInt())
})
val w = Wire(UInt())
w := 1.U
when (io.x) {
w := 7.U
}
io.y := w
}
println(getVerilog(new LastC))
class WhenAbs(w: Int) extends Module {
val io = IO(new Bundle {
val x = Input(SInt(w.W))
val absX = Output(SInt(w.W))
})
io.absX := io.x
when (io.x < 0.S) {
io.absX := -io.x
}
}
println(getVerilog(new WhenAbs(4)))
class MyAdder(w: Int) extends Module {
val io = IO(new Bundle {
val a = Input(UInt(w.W))
val b = Input(UInt(w.W))
val c = Output(UInt())
})
io.c := io.a + io.b // returns output [7:0] io_c
// io.c := io.a +% io.b
// io.c := io.a +& io.b // returns output [8:0] io_c
}
println(getVerilog(new MyAdder(8)))
class SignMagConv(w: Int) extends Module {
val io = IO(new Bundle {
val sign = Input(Bool())
val mag = Input(UInt(w.W))
val twos = Output(UInt((w+1).W))
})
when (io.sign) {
io.twos := ~io.mag +& 1.U
} .otherwise {
io.twos := io.mag
}
}
class SignExtender(win: Int, wout: Int) extends Module {
val io = IO(new Bundle {
val in = Input(UInt(win.W))
val out = Output(UInt(wout.W))
})
assert(win > 0)
assert(win < wout)
val signBit = io.in(win-1)
val extension = Fill(wout-win, signBit)
io.out := Cat(extension, io.in)
}
println(getVerilog(new SignExtender(4,8)))