Skip to content

Commit 4cab7eb

Browse files
committed
Implement TileBuffer
1 parent 4512a8f commit 4cab7eb

File tree

6 files changed

+95
-36
lines changed

6 files changed

+95
-36
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.bloop
22
.metals
33
.vscode
4+
.bsp
45
generated
56
project
67
target
7-
verilator/obj_dir
8+
verilator/obj_dir

src/main/scala/core/Fb.scala

+2-6
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ class FbWriter extends Module {
172172
val vram = new WrAxi(Vram.addrWidth, Vram.dataWidth)
173173
val fbId = Input(UInt(Fb.idWidth.W))
174174
val req = Flipped(Irrevocable(new FbWrReq))
175-
val idx = Output(UInt(log2Up(Fb.nrIndices).W))
176175
val done = Output(Bool())
177176
})
178177

@@ -208,15 +207,12 @@ class FbWriter extends Module {
208207
io.vram.data.bits.strb := Fill(Vram.dataBytes, 1.U)
209208
io.vram.data.bits.last := last
210209
io.req.ready := io.vram.data.ready
211-
io.idx := idx
212210
when (io.req.valid && io.vram.data.ready) {
213-
val nextIdx = WireDefault(idx + 1.U)
211+
idx := idx + 1.U
214212
when (last) {
215-
nextIdx := 0.U
213+
idx := 0.U
216214
addrBegan := false.B
217215
}
218-
idx := nextIdx
219-
io.idx := nextIdx
220216
}
221217

222218
io.vram.resp.ready := true.B

src/main/scala/display/Display.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Display extends Module {
1616
vgaSignal.io.currPos := vgaPos
1717
io.vga := vgaSignal.io.vga
1818

19-
val buffer = SyncReadMem(Fb.nrIndices, Vec(Fb.nrBanks, VgaRGB()))
19+
val buf = SyncReadMem(Fb.nrIndices, Vec(Fb.nrBanks, VgaRGB()))
2020
val fbReader = Module(new FbReader)
2121
val rdReqLine = RegInit(0.U(log2Up(Fb.height).W))
2222
io.vram <> fbReader.io.vram
@@ -36,9 +36,9 @@ class Display extends Module {
3636
pix(i).g := dither(VgaRGB.gWidth, fbReader.io.res.bits.pix(i).g, rdReqLine, i)
3737
pix(i).b := dither(VgaRGB.bWidth, fbReader.io.res.bits.pix(i).b, rdReqLine, i)
3838
}
39-
buffer.write(fbReader.io.res.bits.idx, pix)
39+
buf.write(fbReader.io.res.bits.idx, pix)
4040
}
4141

42-
val pixBanks = buffer.read(vgaSignal.io.nextPos.x >> log2Up(Fb.nrBanks))
42+
val pixBanks = buf.read(vgaSignal.io.nextPos.x >> log2Up(Fb.nrBanks))
4343
vgaSignal.io.pix := pixBanks(vgaPos.x(log2Up(Fb.nrBanks) - 1, 0))
4444
}

src/main/scala/display/Dithering.scala

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ object dither {
2525
bayer(i)(j) := c.U
2626
}
2727
}
28-
2928
val sum = x +& bayer(row)(col)
3029
res := sum >> dstWidth
3130
when (sum(x.getWidth)) {

src/main/scala/graphics/Graphics.scala

+27-17
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,34 @@ class Graphics extends Module {
1111
val done = Output(Bool())
1212
})
1313

14-
val line = RegInit(0.U(unsignedBitLength(Fb.height).W))
15-
val done = line === Fb.height.U
14+
val col = RegInit(0.U(log2Up(Tile.nrCols).W))
15+
val row = RegInit(0.U(unsignedBitLength(Tile.nrRows).W))
16+
when (RegNext(io.fbId) =/= io.fbId) {
17+
col := 0.U
18+
row := 0.U
19+
}
20+
21+
val tileBuffer = Module(new TileBuffer)
22+
val valid = row < Tile.nrRows.U
23+
tileBuffer.io.inReq.valid := valid
24+
when (valid && tileBuffer.io.inReq.ready) {
25+
col := col + 1.U
26+
when (col === (Tile.nrCols - 1).U) {
27+
col := 0.U
28+
row := row + 1.U
29+
}
30+
}
31+
for (i <- 0 until Tile.size) {
32+
for (j <- 0 until Tile.size) {
33+
tileBuffer.io.inReq.bits(i)(j).r := col << 2.U
34+
tileBuffer.io.inReq.bits(i)(j).g := row << 2.U
35+
tileBuffer.io.inReq.bits(i)(j).b := "hff".U
36+
}
37+
}
38+
1639
val fbWriter = Module(new FbWriter)
1740
io.vram <> fbWriter.io.vram
1841
fbWriter.io.fbId := io.fbId
19-
fbWriter.io.req.valid := !done
20-
val color = Wire(FbRGB())
21-
color.r := RegNext((fbWriter.io.idx >> 4) ## 0.U(4.W))
22-
color.g := color.r
23-
color.b := color.r
24-
fbWriter.io.req.bits.pix := VecInit(Seq.fill(Fb.nrBanks)(
25-
color.map(color => Mux(line < (Fb.height / 2).U, color, gammaCorrect(color)))
26-
))
27-
when (!done && fbWriter.io.req.ready && fbWriter.io.idx === 0.U) {
28-
line := line + 1.U
29-
}
30-
when (RegNext(io.fbId) =/= io.fbId) {
31-
line := 0.U
32-
}
33-
io.done := fbWriter.io.done
42+
fbWriter.io.req <> tileBuffer.io.outReq
43+
io.done := !valid && fbWriter.io.done
3444
}

src/main/scala/graphics/TileBuffer.scala

+61-8
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ import chisel3._
55
import chisel3.util._
66

77
object Tile {
8-
val width = 32
8+
val size = 16
99

10-
val nrRows = (Fb.width + width - 1) / width * width
11-
val nrCols = (Fb.height + width - 1) / width * width
10+
val nrCols = (Fb.width + size - 1) / size
11+
val nrRows = (Fb.height + size - 1) / size
12+
13+
val width = nrCols * size
14+
val height = nrRows * size
15+
16+
def apply() = new Tile
1217
}
1318

1419
class Tile extends Bundle {
15-
val elem = Vec(Tile.width, Vec(Tile.width, FbRGB()))
20+
val elem = Vec(Tile.size, Vec(Tile.size, FbRGB()))
1621

17-
def apply(i: Int, j: Int) = elem(i)(j)
18-
def apply(i: Int, j: UInt) = elem(i)(j)
19-
def apply(i: UInt, j: Int) = elem(i)(j)
20-
def apply(i: UInt, j: UInt) = elem(i)(j)
22+
def apply(idx: Int) = elem(idx)
23+
def apply(idx: UInt) = elem(idx)
2124
}
2225

2326
class TileBuffer extends Module {
@@ -26,5 +29,55 @@ class TileBuffer extends Module {
2629
val outReq = Irrevocable(new FbWrReq)
2730
})
2831

32+
val buf = SyncReadMem(2 * Tile.nrCols, Tile())
33+
val fron = RegInit(0.U(log2Up(buf.length).W))
34+
val rear = RegInit(0.U(log2Up(buf.length).W))
35+
val size = RegInit(0.U(unsignedBitLength(buf.length).W))
36+
37+
val nextRear = Mux(rear =/= (buf.length - 1).U, rear + 1.U, 0.U)
38+
val full = nextRear === fron
39+
io.inReq.ready := !full
40+
when (!full && io.inReq.valid) {
41+
buf.write(rear, io.inReq.bits)
42+
rear := nextRear
43+
size := size + 1.U
44+
}
2945

46+
val col = RegInit(0.U(log2Up(Tile.nrCols).W))
47+
val row = RegInit(0.U(log2Up(Tile.size).W))
48+
val idx = RegInit(0.U(log2Up(Tile.size / Fb.nrBanks).W))
49+
val nextFron = WireDefault(fron)
50+
val nextCol = WireDefault(col)
51+
val nextRow = WireDefault(row)
52+
val nextIdx = WireDefault(idx)
53+
val writing = size >= Tile.nrCols.U
54+
io.outReq.valid := writing
55+
val pix = Wire(Vec(Fb.nrBanks, FbRGB()))
56+
for (i <- 0 until Fb.nrBanks) {
57+
pix(i) := buf.read(nextFron + nextCol)(nextRow)(nextIdx << log2Up(Fb.nrBanks) | i.U)
58+
}
59+
io.outReq.bits.pix := pix
60+
when (writing && io.outReq.ready) {
61+
nextIdx := idx + 1.U
62+
idx := nextIdx
63+
when (idx === (Tile.size / Fb.nrBanks - 1).U) {
64+
nextIdx := 0.U
65+
nextCol := col + 1.U
66+
col := nextCol
67+
when (col === (Tile.nrCols - 1).U) {
68+
nextCol := 0.U
69+
nextRow := row + 1.U
70+
row := nextRow
71+
when (row === (Tile.size - 1).U) {
72+
row := 0.U
73+
nextFron := fron + Tile.nrCols.U
74+
fron := nextFron
75+
when (fron === (buf.length - Tile.nrCols).U) {
76+
nextFron := 0.U
77+
}
78+
size := size - Tile.nrCols.U
79+
}
80+
}
81+
}
82+
}
3083
}

0 commit comments

Comments
 (0)