Skip to content

Commit 262b1c1

Browse files
committed
Store UART clock divisor in UART module itself
1 parent ac61640 commit 262b1c1

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

src/main/scala/MemoryIOManager.scala

+7-10
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ class MemoryIOManager(bitWidth: Int = 32, clockFreq: Long, sizeBytes: Long = 102
6262
io.Timer0Port.dataIn := 0.U
6363
io.Timer0Port.writeEnable := false.B
6464

65-
val clockDivisor = RegInit(0.U(bitWidth.W))
66-
io.UART0Port.clockDivisor := clockDivisor
67-
io.UART0Port.txQueue.valid := false.B
68-
io.UART0Port.txQueue.bits := 0.U
69-
io.UART0Port.rxQueue.ready := false.B
65+
io.UART0Port.txQueue.valid := false.B
66+
io.UART0Port.txQueue.bits := 0.U
67+
io.UART0Port.rxQueue.ready := false.B
68+
io.UART0Port.clockDivisor.bits := 0.U
69+
io.UART0Port.clockDivisor.valid := false.B
7070

7171
io.DataMemPort.writeEnable := false.B
7272
io.DataMemPort.writeData := 0.U
@@ -117,10 +117,6 @@ class MemoryIOManager(bitWidth: Int = 32, clockFreq: Long, sizeBytes: Long = 102
117117
.elsewhen(readAddress(7, 0) === 0x0c.U) {
118118
dataOut := Cat(io.UART0Port.txFull, io.UART0Port.rxFull, io.UART0Port.txEmpty, io.UART0Port.rxEmpty)
119119
}
120-
/* Clock divisor */
121-
.elsewhen(readAddress(7, 0) === 0x10.U) {
122-
dataOut := clockDivisor
123-
}
124120
/* Invalid */
125121
.otherwise(dataOut := 0.U)
126122
}
@@ -133,7 +129,8 @@ class MemoryIOManager(bitWidth: Int = 32, clockFreq: Long, sizeBytes: Long = 102
133129
}
134130
/* clock divisor */
135131
.elsewhen(writeAddress(7, 0) === 0x10.U) {
136-
clockDivisor := io.MemoryIOPort.writeData(7, 0)
132+
io.UART0Port.clockDivisor.valid := true.B
133+
io.UART0Port.clockDivisor.bits := io.MemoryIOPort.writeData(7, 0)
137134
}
138135
}
139136
}

src/main/scala/Uart.scala

+8-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class UARTPort() extends Bundle {
3535
val txEmpty = Output(Bool())
3636
val rxFull = Output(Bool())
3737
val txFull = Output(Bool())
38-
val clockDivisor = Input(UInt(8.W))
38+
val clockDivisor = Flipped(Valid(UInt(8.W)))
3939
}
4040

4141
class Uart(val fifoLength: Int, val rxOverclock: Int) extends Module {
@@ -48,6 +48,11 @@ class Uart(val fifoLength: Int, val rxOverclock: Int) extends Module {
4848

4949
val sampleClk = RegInit(0.U(1.W))
5050
val sampleClkCounter = RegInit(0.U(8.W))
51+
val clockDivisor = RegInit(0.U(8.W))
52+
53+
when(io.dataPort.clockDivisor.valid) {
54+
clockDivisor := io.dataPort.clockDivisor.bits
55+
}
5156

5257
val txQueue = Module(new Queue(UInt(8.W), fifoLength))
5358
val rxQueue = Module(new Queue(UInt(8.W), fifoLength))
@@ -57,10 +62,10 @@ class Uart(val fifoLength: Int, val rxOverclock: Int) extends Module {
5762
io.dataPort.rxFull := (rxQueue.io.count === fifoLength.U)
5863
io.dataPort.txFull := (txQueue.io.count === fifoLength.U)
5964

60-
val uartEnabled = io.dataPort.clockDivisor.orR
65+
val uartEnabled = clockDivisor.orR
6166

6267
when(uartEnabled) {
63-
when(sampleClkCounter === io.dataPort.clockDivisor) {
68+
when(sampleClkCounter === clockDivisor) {
6469
sampleClk := 1.U
6570
sampleClkCounter := 0.U
6671
}.otherwise {

src/test/scala/UartSpec.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class UartSpec extends AnyFlatSpec with ChiselScalatestTester with should.Matche
5050
test(new Uart(64, rxOverclock)).withAnnotations(Seq(WriteVcdAnnotation)) { u =>
5151
u.clock.setTimeout(10000)
5252

53-
u.io.dataPort.clockDivisor.poke(divider.U)
53+
u.io.dataPort.clockDivisor.valid.poke(true.B)
54+
u.io.dataPort.clockDivisor.bits.poke(divider.U)
5455

5556
u.io.dataPort.rxQueue.ready.poke(false.B)
5657

verilator/chiselv.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ int main(int argc, char **argv)
7979
// if (top->io_terminate)
8080
// {
8181
// VL_PRINTF("Simulation terminated!\n");
82-
8382
// VL_PRINTF("REG%d %x" VL_PRI64 "X\r\n", 0, top->Toplevel__DOT__CPU__DOT__registerBank__DOT__regs_0);
8483
// VL_PRINTF("REG%d %x" VL_PRI64 "X\r\n", 1, top->Toplevel__DOT__CPU__DOT__registerBank__DOT__regs_1);
8584
// VL_PRINTF("REG%d %x" VL_PRI64 "X\r\n", 2, top->Toplevel__DOT__CPU__DOT__registerBank__DOT__regs_2);

0 commit comments

Comments
 (0)