xref: /XiangShan/src/main/scala/device/AXI4VGA.scala (revision 9904078bf5f48629f268f00f4b73df6edb219e96)
18b16d276SZihao Yupackage device
28b16d276SZihao Yu
38b16d276SZihao Yuimport chisel3._
48b16d276SZihao Yuimport chisel3.util._
58b16d276SZihao Yu
68b16d276SZihao Yuimport bus.axi4._
78b16d276SZihao Yuimport utils._
88b16d276SZihao Yu
98b16d276SZihao Yutrait HasVGAConst {
108b16d276SZihao Yu  // these are only fit for 800x600
118b16d276SZihao Yu  val ScreenW = 800
128b16d276SZihao Yu  val ScreenH = 600
138b16d276SZihao Yu
148b16d276SZihao Yu  val HFrontPorch = 56
158b16d276SZihao Yu  val HActive = HFrontPorch + 120
168b16d276SZihao Yu  val HBackPorch = HActive + ScreenW
178b16d276SZihao Yu  val HTotal = HBackPorch + 64
188b16d276SZihao Yu  val VFrontPorch = 37
198b16d276SZihao Yu  val VActive = VFrontPorch + 6
208b16d276SZihao Yu  val VBackPorch = VActive + ScreenH
218b16d276SZihao Yu  val VTotal = VBackPorch + 23
228b16d276SZihao Yu
238b16d276SZihao Yu  val FBWidth = ScreenW / 2
248b16d276SZihao Yu  val FBHeight = ScreenH / 2
258b16d276SZihao Yu  val FBPixels = FBWidth * FBHeight
268b16d276SZihao Yu}
278b16d276SZihao Yu
288b16d276SZihao Yuclass VGABundle extends Bundle {
298b16d276SZihao Yu  val r = Output(UInt(4.W))
308b16d276SZihao Yu  val g = Output(UInt(4.W))
318b16d276SZihao Yu  val b = Output(UInt(4.W))
328b16d276SZihao Yu  val hsync = Output(Bool())
338b16d276SZihao Yu  val vsync = Output(Bool())
348b16d276SZihao Yu}
358b16d276SZihao Yu
368b16d276SZihao Yuclass VGACtrl extends AXI4SlaveModule(new AXI4Lite) with HasVGAConst {
378b16d276SZihao Yu  // actually this is a constant
388b16d276SZihao Yu  val fbSizeReg = Cat(FBWidth.U(16.W), FBHeight.U(16.W))
398b16d276SZihao Yu  // we always return fbSizeReg to axi4lite
408b16d276SZihao Yu  in.r.bits.data := fbSizeReg
418b16d276SZihao Yu  val sync = in.aw.fire()
428b16d276SZihao Yu}
438b16d276SZihao Yu
448b16d276SZihao Yuclass AXI4VGA extends Module with HasVGAConst {
45466a6a49SZihao Yu  val AXIidBits = 2
468b16d276SZihao Yu  // need a 50MHz clock
478b16d276SZihao Yu  val io = IO(new Bundle {
488b16d276SZihao Yu    val in = new Bundle {
49466a6a49SZihao Yu      val fb = Flipped(new AXI4(idBits = AXIidBits))
508b16d276SZihao Yu      val ctrl = Flipped(new AXI4Lite)
518b16d276SZihao Yu    }
528b16d276SZihao Yu    val vga = new VGABundle
538b16d276SZihao Yu  })
548b16d276SZihao Yu
558b16d276SZihao Yu  val ctrl = Module(new VGACtrl)
568b16d276SZihao Yu  io.in.ctrl <> ctrl.io.in
57466a6a49SZihao Yu  val fb = Module(new AXI4RAM(new AXI4(idBits = AXIidBits), memByte = FBPixels * 4))
588b16d276SZihao Yu  // writable by axi4lite
598b16d276SZihao Yu  // but it only readable by the internel controller
608b16d276SZihao Yu  fb.io.in.aw <> io.in.fb.aw
618b16d276SZihao Yu  fb.io.in.w <> io.in.fb.w
628b16d276SZihao Yu  io.in.fb.b <> fb.io.in.b
638b16d276SZihao Yu  io.in.fb.ar.ready := true.B
648b16d276SZihao Yu  io.in.fb.r.bits.data := 0.U
658b16d276SZihao Yu  io.in.fb.r.bits.resp := AXI4Parameters.RESP_OKAY
668b16d276SZihao Yu  io.in.fb.r.valid := BoolStopWatch(io.in.fb.ar.fire(), io.in.fb.r.fire(), startHighPriority = true)
6740f96c68SZihao Yu  io.in.fb.r.bits.id := io.in.fb.ar.bits.id
6840f96c68SZihao Yu  io.in.fb.r.bits.user := io.in.fb.ar.bits.user
6940f96c68SZihao Yu  io.in.fb.r.bits.last := true.B
708b16d276SZihao Yu
718b16d276SZihao Yu  def inRange(x: UInt, start: Int, end: Int) = (x >= start.U) && (x < end.U)
728b16d276SZihao Yu
738b16d276SZihao Yu  val (hCounter, hFinish) = Counter(true.B, HTotal)
748b16d276SZihao Yu  val (vCounter, vFinish) = Counter(hFinish, VTotal)
758b16d276SZihao Yu
768b16d276SZihao Yu  io.vga.hsync := hCounter >= HFrontPorch.U
778b16d276SZihao Yu  io.vga.vsync := vCounter >= VFrontPorch.U
788b16d276SZihao Yu
798b16d276SZihao Yu  val hInRange = inRange(hCounter, HActive, HBackPorch)
808b16d276SZihao Yu  val vInRange = inRange(vCounter, VActive, VBackPorch)
818b16d276SZihao Yu  val videoValid = hInRange && vInRange
828b16d276SZihao Yu
838b16d276SZihao Yu  val hCounterIsOdd = hCounter(0)
84*9904078bSZihao Yu  val hCounterIs2 = hCounter(1,0) === 2.U
858b16d276SZihao Yu  val vCounterIsOdd = vCounter(0)
86*9904078bSZihao Yu  // there is 2 cycle latency to read block memory,
87*9904078bSZihao Yu  // so we should issue the read request 2 cycle eariler
888b16d276SZihao Yu  val nextPixel = inRange(hCounter, HActive - 1, HBackPorch - 1) && vInRange && hCounterIsOdd
898b16d276SZihao Yu  val fbPixelAddrV0 = Counter(nextPixel && !vCounterIsOdd, FBPixels)._1
908b16d276SZihao Yu  val fbPixelAddrV1 = Counter(nextPixel &&  vCounterIsOdd, FBPixels)._1
918b16d276SZihao Yu
928b16d276SZihao Yu  // each pixel is 4 bytes
93*9904078bSZihao Yu  fb.io.in.ar.bits := DontCare
94*9904078bSZihao Yu  fb.io.in.ar.bits.len   := 0.U
95*9904078bSZihao Yu  fb.io.in.ar.bits.size  := "b11".U
96*9904078bSZihao Yu  fb.io.in.ar.bits.burst := AXI4Parameters.BURST_INCR
978b16d276SZihao Yu  fb.io.in.ar.bits.addr := Cat(Mux(vCounterIsOdd, fbPixelAddrV1, fbPixelAddrV0), 0.U(2.W))
98*9904078bSZihao Yu  fb.io.in.ar.valid := RegNext(nextPixel) && hCounterIs2
998b16d276SZihao Yu
1008b16d276SZihao Yu  fb.io.in.r.ready := true.B
101*9904078bSZihao Yu  val data = HoldUnless(fb.io.in.r.bits.data, fb.io.in.r.fire())
102*9904078bSZihao Yu  val color = Mux(hCounter(1), data(63, 32), data(31, 0))
1038b16d276SZihao Yu  io.vga.r := Mux(videoValid, color(23, 20), 0.U)
1048b16d276SZihao Yu  io.vga.g := Mux(videoValid, color(15, 12), 0.U)
1058b16d276SZihao Yu  io.vga.b := Mux(videoValid, color(7, 4), 0.U)
1068b16d276SZihao Yu}
107