xref: /XiangShan/src/main/scala/device/AXI4VGA.scala (revision 43002b0176da822a77bcb2afd037ec5800f31e6f)
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
36096a786aSZihao Yuclass VGACtrlBundle extends Bundle {
37096a786aSZihao Yu  val sync = Output(Bool())
38096a786aSZihao Yu}
39096a786aSZihao Yu
40096a786aSZihao Yuclass VGACtrl extends AXI4SlaveModule(new AXI4Lite, new VGACtrlBundle) with HasVGAConst {
418b16d276SZihao Yu  val fbSizeReg = Cat(FBWidth.U(16.W), FBHeight.U(16.W))
428b16d276SZihao Yu  val sync = in.aw.fire()
43096a786aSZihao Yu
44096a786aSZihao Yu  val mapping = Map(
45096a786aSZihao Yu    RegMap(0x0, fbSizeReg, RegMap.Unwritable),
46096a786aSZihao Yu    RegMap(0x4, sync, RegMap.Unwritable)
47096a786aSZihao Yu  )
48096a786aSZihao Yu
49096a786aSZihao Yu  RegMap.generate(mapping, raddr(3,0), in.r.bits.data,
50096a786aSZihao Yu    waddr(3,0), in.w.fire(), in.w.bits.data, MaskExpand(in.w.bits.strb))
51096a786aSZihao Yu
52096a786aSZihao Yu  io.extra.get.sync := sync
538b16d276SZihao Yu}
548b16d276SZihao Yu
55*43002b01SZihao Yuclass FBHelper extends BlackBox with HasBlackBoxInline {
56*43002b01SZihao Yu  val io = IO(new Bundle {
57*43002b01SZihao Yu    val clk = Input(Clock())
58*43002b01SZihao Yu    val valid = Input(Bool())
59*43002b01SZihao Yu    val pixel = Input(UInt(32.W))
60*43002b01SZihao Yu    val sync = Input(Bool())
61*43002b01SZihao Yu  })
62*43002b01SZihao Yu
63*43002b01SZihao Yu  setInline("FBHelper.v",
64*43002b01SZihao Yu    s"""
65*43002b01SZihao Yu      |import "DPI-C" function void put_pixel(input int pixel);
66*43002b01SZihao Yu      |import "DPI-C" function void vmem_sync();
67*43002b01SZihao Yu      |
68*43002b01SZihao Yu      |module FBHelper (
69*43002b01SZihao Yu      |  input clk,
70*43002b01SZihao Yu      |  input valid,
71*43002b01SZihao Yu      |  input [31:0] pixel,
72*43002b01SZihao Yu      |  input sync
73*43002b01SZihao Yu      |);
74*43002b01SZihao Yu      |
75*43002b01SZihao Yu      |  always@(posedge clk) begin
76*43002b01SZihao Yu      |    if (valid) put_pixel(pixel);
77*43002b01SZihao Yu      |    if (sync) vmem_sync();
78*43002b01SZihao Yu      |  end
79*43002b01SZihao Yu      |
80*43002b01SZihao Yu      |endmodule
81*43002b01SZihao Yu     """.stripMargin)
82*43002b01SZihao Yu}
83*43002b01SZihao Yu
84*43002b01SZihao Yuclass AXI4VGA(sim: Boolean = false) extends Module with HasVGAConst {
85466a6a49SZihao Yu  val AXIidBits = 2
868b16d276SZihao Yu  // need a 50MHz clock
878b16d276SZihao Yu  val io = IO(new Bundle {
888b16d276SZihao Yu    val in = new Bundle {
8911348640SZihao Yu      val fb = Flipped(new AXI4Lite)
908b16d276SZihao Yu      val ctrl = Flipped(new AXI4Lite)
918b16d276SZihao Yu    }
928b16d276SZihao Yu    val vga = new VGABundle
938b16d276SZihao Yu  })
948b16d276SZihao Yu
958b16d276SZihao Yu  val ctrl = Module(new VGACtrl)
968b16d276SZihao Yu  io.in.ctrl <> ctrl.io.in
9711348640SZihao Yu  val fb = Module(new AXI4RAM(new AXI4Lite, memByte = FBPixels * 4))
988b16d276SZihao Yu  // writable by axi4lite
998b16d276SZihao Yu  // but it only readable by the internel controller
1008b16d276SZihao Yu  fb.io.in.aw <> io.in.fb.aw
1018b16d276SZihao Yu  fb.io.in.w <> io.in.fb.w
1028b16d276SZihao Yu  io.in.fb.b <> fb.io.in.b
1038b16d276SZihao Yu  io.in.fb.ar.ready := true.B
1048b16d276SZihao Yu  io.in.fb.r.bits.data := 0.U
1058b16d276SZihao Yu  io.in.fb.r.bits.resp := AXI4Parameters.RESP_OKAY
1068b16d276SZihao Yu  io.in.fb.r.valid := BoolStopWatch(io.in.fb.ar.fire(), io.in.fb.r.fire(), startHighPriority = true)
1078b16d276SZihao Yu
1088b16d276SZihao Yu  def inRange(x: UInt, start: Int, end: Int) = (x >= start.U) && (x < end.U)
1098b16d276SZihao Yu
1108b16d276SZihao Yu  val (hCounter, hFinish) = Counter(true.B, HTotal)
1118b16d276SZihao Yu  val (vCounter, vFinish) = Counter(hFinish, VTotal)
1128b16d276SZihao Yu
1138b16d276SZihao Yu  io.vga.hsync := hCounter >= HFrontPorch.U
1148b16d276SZihao Yu  io.vga.vsync := vCounter >= VFrontPorch.U
1158b16d276SZihao Yu
1168b16d276SZihao Yu  val hInRange = inRange(hCounter, HActive, HBackPorch)
1178b16d276SZihao Yu  val vInRange = inRange(vCounter, VActive, VBackPorch)
1188b16d276SZihao Yu  val videoValid = hInRange && vInRange
1198b16d276SZihao Yu
1208b16d276SZihao Yu  val hCounterIsOdd = hCounter(0)
1219904078bSZihao Yu  val hCounterIs2 = hCounter(1,0) === 2.U
1228b16d276SZihao Yu  val vCounterIsOdd = vCounter(0)
1239904078bSZihao Yu  // there is 2 cycle latency to read block memory,
1249904078bSZihao Yu  // so we should issue the read request 2 cycle eariler
1258b16d276SZihao Yu  val nextPixel = inRange(hCounter, HActive - 1, HBackPorch - 1) && vInRange && hCounterIsOdd
1268b16d276SZihao Yu  val fbPixelAddrV0 = Counter(nextPixel && !vCounterIsOdd, FBPixels)._1
1278b16d276SZihao Yu  val fbPixelAddrV1 = Counter(nextPixel &&  vCounterIsOdd, FBPixels)._1
1288b16d276SZihao Yu
1298b16d276SZihao Yu  // each pixel is 4 bytes
13011348640SZihao Yu  fb.io.in.ar.bits.prot := 0.U
1318b16d276SZihao Yu  fb.io.in.ar.bits.addr := Cat(Mux(vCounterIsOdd, fbPixelAddrV1, fbPixelAddrV0), 0.U(2.W))
1329904078bSZihao Yu  fb.io.in.ar.valid := RegNext(nextPixel) && hCounterIs2
1338b16d276SZihao Yu
1348b16d276SZihao Yu  fb.io.in.r.ready := true.B
1359904078bSZihao Yu  val data = HoldUnless(fb.io.in.r.bits.data, fb.io.in.r.fire())
1369904078bSZihao Yu  val color = Mux(hCounter(1), data(63, 32), data(31, 0))
1378b16d276SZihao Yu  io.vga.r := Mux(videoValid, color(23, 20), 0.U)
1388b16d276SZihao Yu  io.vga.g := Mux(videoValid, color(15, 12), 0.U)
1398b16d276SZihao Yu  io.vga.b := Mux(videoValid, color(7, 4), 0.U)
140*43002b01SZihao Yu
141*43002b01SZihao Yu  if (sim) {
142*43002b01SZihao Yu    val fbHelper = Module(new FBHelper)
143*43002b01SZihao Yu    fbHelper.io.clk := clock
144*43002b01SZihao Yu    fbHelper.io.valid := videoValid
145*43002b01SZihao Yu    fbHelper.io.pixel := color
146*43002b01SZihao Yu    fbHelper.io.sync := ctrl.io.extra.get.sync
147*43002b01SZihao Yu  }
1488b16d276SZihao Yu}
149