Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (38 sloc) 1.08 KB
// See LICENSE.txt for license details.
package solutions
import chisel3._
import chisel3.util._
// Problem:
//
// Create a composition (chain) of two filters:
//
// SingleFilter - indicates that input is single decimal digit
// (i.e. is less or equal to 9)
//
// EvenFilter - indicates that input is even number
//
abstract class Filter[T <: Data](dtype: T) extends Module {
val io = IO(new Bundle {
val in = Input(Valid(dtype))
val out = Output(Valid(dtype))
})
}
class PredicateFilter[T <: Data](dtype: T, f: T => Bool) extends Filter(dtype) {
io.out.valid := io.in.valid && f(io.in.bits)
io.out.bits := io.in.bits
}
object SingleFilter {
def apply[T <: UInt](dtype: T) =
Module(new PredicateFilter(dtype, (x: T) => x <= 9.U))
}
object EvenFilter {
def apply[T <: UInt](dtype: T) =
Module(new PredicateFilter(dtype, (x: T) => x(0).toBool))
}
class SingleEvenFilter[T <: UInt](dtype: T) extends Filter(dtype) {
val single = SingleFilter(dtype)
val even = EvenFilter(dtype)
single.io.in := io.in
even.io.in := single.io.out
io.out := even.io.out
}