Gowin Vol.3 第2部第8章 リスト8

object NetworkBytes {
def apply(value: UInt, from: Int, toInclusive: Int): UInt = {
Cat((from to toInclusive).map(i => value((i+1)*8-1, i*8)))
}
}
object FlipBytes {
def apply(value: UInt): UInt = {
val bytes = value.getWidth / 8
Cat((0 to bytes-1).map(i => value((i+1)*8-1, i*8)))
}
}
object Bytes {
def apply(value: UInt): Vec[UInt] = {
val bytes = value.getWidth / 8
VecInit((0 to bytes-1).map(i => value((i+1)*8-1, i*8)))
}
}

class EthernetHeader extends Bundle {
val source = UInt((8*6).W)
val destination = UInt((8*6).W)
val protocol = UInt((8*2).W)

def toUInt(): UInt = {
Cat(
FlipBytes(protocol),
FlipBytes(source),
FlipBytes(destination),
)
}
def toBytes(): Vec[UInt] = {
Bytes(this.toUInt())
}
}
object EthernetHeader {
def apply(value: UInt): EthernetHeader = {
val header = Wire(new EthernetHeader)
header.destination := NetworkBytes(value, 0, 5)
header.source := NetworkBytes(value, 6, 11)
header.protocol := NetworkBytes(value, 12, 13)
header
}
}