diff --git a/5.6_pipeline.ipynb b/5.6_pipeline.ipynb
index 5ed5bc2..314e7a0 100644
--- a/5.6_pipeline.ipynb
+++ b/5.6_pipeline.ipynb
@@ -110,7 +110,8 @@
"`riscv/src/main/scala/riscv/PipelineFlush.scala`, restart the kernel and run the notebook again\n",
"(until then, the cells that build the core stop with a `NotImplementedError`, as do those of 5.2-5.4\n",
"if their exercises are not done). Think about which instructions are younger than the one in EX,\n",
- "and than the one in MEM, and where each of them is."
+ "and than the one in MEM, and where each of them is. The test runs short programs on the core and\n",
+ "compares them with the golden model."
]
},
{
@@ -119,14 +120,16 @@
"metadata": {},
"outputs": [],
"source": [
- "simulate(new PipelineFlush) { c =>\n",
- " for (redirect <- Seq(false, true); halt <- Seq(false, true)) {\n",
- " c.io.redirect.poke(redirect.B)\n",
- " c.io.halt.poke(halt.B)\n",
- " c.io.flushIfId.expect((redirect || halt).B, s\"redirect $redirect, halt $halt\")\n",
- " c.io.flushIdEx.expect((redirect || halt).B, s\"redirect $redirect, halt $halt\")\n",
- " c.io.flushExMem.expect(halt.B, s\"redirect $redirect, halt $halt: the jump itself goes on\")\n",
- " }\n",
+ "// what the flushes are for: no instruction younger than a jump (in EX) or a halt (in MEM) may take\n",
+ "// effect; the nops give auipc's result time to reach the stores\n",
+ "simulate(new PipelinedSystem) { c =>\n",
+ " for (\n",
+ " (name, source) <- Seq(\n",
+ " \"stores on the wrong path\" -> \"auipc t0, 0\\nnop\\nnop\\nj 1f\\nsw t0, 0(t0)\\nsw t0, 4(t0)\\n1: ecall\",\n",
+ " \"stores behind a halt\" -> \"auipc t0, 0\\nnop\\nnop\\nebreak\\nsw t0, 0(t0)\\nsw t0, 4(t0)\\nsw t0, 8(t0)\",\n",
+ " \"a jump behind a halt\" -> \"auipc t0, 0\\nnop\\nnop\\nebreak\\nj 1f\\nsw t0, 0(t0)\\n1: sw t0, 4(t0)\"\n",
+ " )\n",
+ " ) CoSim.compare(c, Asm(source)).foreach(d => sys.error(s\"$name: $d\"))\n",
"}\n",
"println(\"SUCCESS!!\")"
]
diff --git a/5.7_hazards.ipynb b/5.7_hazards.ipynb
index 25eca72..aa6d852 100644
--- a/5.7_hazards.ipynb
+++ b/5.7_hazards.ipynb
@@ -191,7 +191,6 @@
" \"EX/MEM to EX\" -> \"li a0, 5\\naddi a1, a0, 1\\necall\",\n",
" \"MEM/WB to EX\" -> \"li a0, 5\\nnop\\naddi a1, a0, 1\\necall\",\n",
" \"the newest result wins\" -> \"li a0, 1\\nli a0, 2\\nadd a1, a0, a0\\necall\",\n",
- " \"x0 is not forwarded\" -> \"addi zero, zero, 7\\nadd a0, zero, zero\\necall\",\n",
" \"load-use\" -> \"la t0, d\\nlw a0, 0(t0)\\naddi a1, a0, 1\\necall\\n.data\\nd: .word 41\",\n",
" \"load then store\" -> \"la t0, d\\nlw a0, 0(t0)\\nsw a0, 4(t0)\\nlw a1, 4(t0)\\necall\\n.data\\nd: .word 41, 0\",\n",
" \"branch on a loaded value\" -> \"la t0, d\\nlw a0, 0(t0)\\nbnez a0, 1f\\nebreak\\n1: ecall\\n.data\\nd: .word 1\",\n",
@@ -294,8 +293,8 @@
"The pipeline needs more cycles than the single-cycle core, but each of its cycles is shorter: one\n",
"stage instead of the whole path, so ideally up to five times shorter, and less in practice (the\n",
"stages are not equally long, the stage registers add delay, and forwarding lengthens EX). Whether\n",
- "it runs a program in less time depends on those delays, which a synthesis tool reports (Module\n",
- "5.10).\n",
+ "it runs a program in less time depends on those delays, which place and route computes: Module\n",
+ "5.10 synthesizes the cores, and nextpnr, outside the image, reports how fast their clock can run.\n",
"\n",
"---\n",
"# You're done!\n",
diff --git a/5.8_bus_and_mmio.ipynb b/5.8_bus_and_mmio.ipynb
index 94423e6..3db08fe 100644
--- a/5.8_bus_and_mmio.ipynb
+++ b/5.8_bus_and_mmio.ipynb
@@ -122,7 +122,8 @@
"each bit. Restart the kernel and run the notebook again (the cells that build the transmitter or\n",
"the system stop with a `NotImplementedError` until then, as do the exercises of 5.2-5.7).\n",
"\n",
- "`UartReceiver` is a receiver in software: fed `txd` once per cycle, it returns each byte."
+ "`UartReceiver` is a receiver in software: fed `txd` once per cycle, it returns each byte. Once the\n",
+ "test passes, it also draws one frame of `txd` as a waveform (`riscv.Waves`), bit by bit."
]
},
{
@@ -132,25 +133,50 @@
"outputs": [],
"source": [
"simulate(new UartTx(divider = 4)) { c =>\n",
+ " val txdLog = scala.collection.mutable.ArrayBuffer[Boolean]() // txd and busy in every cycle\n",
+ " val busyLog = scala.collection.mutable.ArrayBuffer[Boolean]()\n",
" val receiver = new UartReceiver(4)\n",
" val text = \"Hi!\"\n",
" val out = new StringBuilder\n",
" var pending = text.toList\n",
- " var line = new StringBuilder // txd, one character per bit\n",
" for (cycle <- 0 until 150) {\n",
" val busy = c.io.busy.peek().litToBoolean\n",
" c.io.send.valid.poke((!busy && pending.nonEmpty).B)\n",
" c.io.send.bits.poke(pending.headOption.getOrElse(' ').toInt.U)\n",
" if (!busy && pending.nonEmpty) pending = pending.tail\n",
" val txd = c.io.txd.peek().litToBoolean\n",
- " if (cycle % 4 == 0 && cycle < 4 * 12) line += (if (txd) '1' else '0')\n",
+ " txdLog += txd\n",
+ " busyLog += busy\n",
" out ++= receiver.sample(txd)\n",
" c.clock.step()\n",
" }\n",
- " println(s\"txd, one sample per bit: $line\")\n",
+ " def bit(log: collection.Seq[Boolean])(cycle: Long): BigInt = if (log(cycle.toInt)) 1 else 0\n",
+ " println(s\"txd, one sample per bit: ${(0L until 4 * 12 by 4).map(bit(txdLog)).mkString}\")\n",
" println(\"idle 1, then 'H' (0x48 = 01001000): start 0, data 00010010 (least significant first), stop 1,\")\n",
" println(\"then the start bit of 'i' (each sample of 'H' is taken in the last cycle of its bit)\")\n",
" assert(out.toString == text, out.toString)\n",
+ "\n",
+ " // the frame of 'H' as a waveform, from just before its start bit to the start bit of 'i'\n",
+ " val start = txdLog.indexWhere(!_) // the first cycle of the start bit of 'H'\n",
+ " val next = txdLog.indexWhere(!_, busyLog.indexWhere(!_, start)) // and of 'i'\n",
+ " val frame = Seq(\"start\") ++ (0 to 7).map(i => s\"D$i\") ++ Seq(\"stop\")\n",
+ " val window = (start - 4L).max(0L) until (next + 4L)\n",
+ " def bitName(cycle: Long) = // no name while idle\n",
+ " if (!busyLog(cycle.toInt)) \"\" else frame.lift(((cycle - start) / 4).toInt).getOrElse(\"start of 'i'\")\n",
+ " almond.display\n",
+ " .Html(\n",
+ " Waves.svg(\n",
+ " window,\n",
+ " window.map(_.toString),\n",
+ " Seq(\n",
+ " Waves.Bit(\"txd\", bit(txdLog)),\n",
+ " Waves.Bus(\"bit\", bitName),\n",
+ " Waves.Bit(\"busy\", bit(busyLog))\n",
+ " ),\n",
+ " note = \"'H' = 0x48: a start bit, the data bits from the lowest, a stop bit; each bit lasts 4 cycles\"\n",
+ " )\n",
+ " )\n",
+ " .display()\n",
"}\n",
"println(\"SUCCESS!!\")"
]
diff --git a/5.9_debug_and_verify.ipynb b/5.9_debug_and_verify.ipynb
index 27e67fe..8c2df91 100644
--- a/5.9_debug_and_verify.ipynb
+++ b/5.9_debug_and_verify.ipynb
@@ -201,7 +201,9 @@
"`simulate(..., waves = \"name\")` dumps every signal of the design in every cycle into a VCD file\n",
"(Module 2.6), which a waveform viewer (GTKWave, Surfer) shows. `riscv.Vcd` reads one in Scala, so\n",
"this notebook can print the signals that matter as a table: one row per cycle, read just before\n",
- "the clock edge (what the registers take at that edge).\n",
+ "the clock edge (what the registers take at that edge). `riscv.Waves` then draws the same signals\n",
+ "as a viewer would: a lane per signal, one-bit signals high or low, wider ones as bands with their\n",
+ "values.\n",
"\n",
"The units' instances are `rs1Data_f` and `rs2Data_f` (one per source register), and the pipeline\n",
"registers keep their names (`idEx_bits_inst` is the instruction in EX). `vcd(name)` gives a\n",
@@ -220,24 +222,50 @@
" CoSim.load(c, Asm(twice))\n",
" CoSim.run(c)\n",
"}\n",
- "val vcd = Vcd.read(wavesFile(\"oldest-first\").get)\n",
+ "locally { // the helpers stay inside: Almond would print every top-level value\n",
+ " val vcd = Vcd.read(wavesFile(\"oldest-first\").get)\n",
"\n",
- "def signal(name: String) = vcd(s\"core.$name\")\n",
- "def unit(port: String) = signal(s\"rs1Data_f.io_$port\") // the forwarding unit of rs1\n",
- "def result(stage: String) = { // what the unit gets from a stage ahead: \"x10 = 2\", or dashes\n",
- " val (valid, rd, value) = (unit(s\"${stage}_valid\"), unit(s\"${stage}_bits_rd\"), unit(s\"${stage}_bits_value\"))\n",
- " (t: Long) => if (valid(t) == 1) f\"x${rd(t)}%-2s = ${value(t)}%-6s\" else \"-\" * 13\n",
- "}\n",
- "val (inEx, inst, pc) = (signal(\"idEx_valid\"), signal(\"idEx_bits_inst\"), signal(\"idEx_bits_pc\"))\n",
- "val (rs, forwarded) = (unit(\"rs\"), unit(\"value\"))\n",
- "val (exMem, memWb) = (result(\"exMem\"), result(\"memWb\"))\n",
- "def isEcall(t: Long) = Rv32i.decode(inst(t).toLong).exists(_.name == \"ecall\")\n",
- "val cycles = vcd.risingEdges(\"core.clock\").map(_ - 1).zipWithIndex.filter { case (t, _) => inEx(t) == 1 }\n",
- "assert(cycles.exists(c => isEcall(c._1)), \"the ecall never reached EX\")\n",
- "println(\"cycle in EX rs EX/MEM rd = value MEM/WB rd = value forwarded\")\n",
- "for ((t, cycle) <- cycles.take(cycles.indexWhere(c => isEcall(c._1)) + 1)) {\n",
- " val text = Rv32i.disassemble(inst(t).toLong, pc(t).toLong)\n",
- " println(f\"$cycle%5d $text%-24s x${rs(t)}%-2s ${exMem(t)}%19s ${memWb(t)}%19s ${forwarded(t)}\")\n",
+ " def signal(name: String) = vcd(s\"core.$name\")\n",
+ " def unit(port: String) = signal(s\"rs1Data_f.io_$port\") // the forwarding unit of rs1\n",
+ " def result(stage: String, none: String) = { // what the unit gets from a stage ahead: \"x10 = 2\", or none\n",
+ " val (valid, rd, value) = (unit(s\"${stage}_valid\"), unit(s\"${stage}_bits_rd\"), unit(s\"${stage}_bits_value\"))\n",
+ " (t: Long) => if (valid(t) == 1) s\"x${rd(t)} = ${value(t)}\" else none\n",
+ " }\n",
+ " val (inEx, inst, pc) = (signal(\"idEx_valid\"), signal(\"idEx_bits_inst\"), signal(\"idEx_bits_pc\"))\n",
+ " val (rs, forwarded) = (unit(\"rs\"), unit(\"value\"))\n",
+ " def is(name: String)(t: Long) = Rv32i.decode(inst(t).toLong).exists(_.name == name)\n",
+ " def text(t: Long) = Rv32i.disassemble(inst(t).toLong, pc(t).toLong)\n",
+ " val cycles = vcd.risingEdges(\"core.clock\").map(_ - 1).zipWithIndex.filter { case (t, _) => inEx(t) == 1 }\n",
+ " assert(cycles.exists(c => is(\"ecall\")(c._1)), \"the ecall never reached EX\")\n",
+ " // up to the ecall; twice never stalls, so these cycles follow one another\n",
+ " val shown = cycles.take(cycles.indexWhere(c => is(\"ecall\")(c._1)) + 1)\n",
+ " val (exMem, memWb) = (result(\"exMem\", \"-\" * 17), result(\"memWb\", \"-\" * 17))\n",
+ " println(\"cycle in EX rs EX/MEM rd = value MEM/WB rd = value forwarded\")\n",
+ " for ((t, cycle) <- shown) {\n",
+ " println(\n",
+ " f\"$cycle%5d ${text(t)}%-24s x${rs(t)}%-2s ${exMem(t)}%-20s${memWb(t)}%-20s${forwarded(t)}\"\n",
+ " )\n",
+ " }\n",
+ "\n",
+ " // the same cycles as a waveform; the cycle in which add is in EX is highlighted\n",
+ " val add = shown.indexWhere(c => is(\"add\")(c._1))\n",
+ " almond.display\n",
+ " .Html(\n",
+ " Waves.svg(\n",
+ " shown.map(_._1),\n",
+ " shown.map(_._2.toString),\n",
+ " Seq(\n",
+ " Waves.Bus(\"in EX\", text),\n",
+ " Waves.Bus(\"rs\", t => s\"x${rs(t)}\"),\n",
+ " Waves.Bus(\"EX/MEM rd = value\", result(\"exMem\", \"\")),\n",
+ " Waves.Bus(\"MEM/WB rd = value\", result(\"memWb\", \"\")),\n",
+ " Waves.Bus(\"forwarded\", t => forwarded(t).toString)\n",
+ " ),\n",
+ " mark = Set(add),\n",
+ " note = \"the unit for rs1, sampled just before each rising clock edge; no value: not valid\"\n",
+ " )\n",
+ " )\n",
+ " .display()\n",
"}"
]
},
@@ -245,11 +273,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "In the cycle in which `add a1, a0, a0` is in EX, both stages ahead hold a result for `x10` (`a0`):\n",
- "2 from the second `li`, one stage ahead, and 1 from the first. The unit forwards 1. With a bug in\n",
- "hand, a waveform is the place to confirm the cause; it is a poor place to search for one, as a\n",
- "long program has thousands of cycles and hundreds of signals. The trace narrows the search to one\n",
- "instruction, and so to a few cycles."
+ "In the cycle in which `add a1, a0, a0` is in EX (highlighted in the waveform), both stages ahead\n",
+ "hold a result for `x10` (`a0`): 2 from the second `li`, one stage ahead, and 1 from the first. The\n",
+ "unit forwards 1. With a bug in hand, a waveform is the place to confirm the cause; it is a poor\n",
+ "place to search for one, as a long program has thousands of cycles and hundreds of signals. The\n",
+ "trace narrows the search to one instruction, and so to a few cycles."
]
},
{
diff --git a/riscv/DESIGN.md b/riscv/DESIGN.md
index 1e2c68c..31c51be 100644
--- a/riscv/DESIGN.md
+++ b/riscv/DESIGN.md
@@ -193,7 +193,8 @@ disassembled by `Rv32i.disassemble` (as `objdump -M no-aliases` does, near enoug
assembles back to the same word, tested on every instruction of the programs but `fence`, which it
shows without operands), so `CoSim.compare`'s messages name the instruction that differs and what it
did. `Vcd` reads the VCD file (binary search per signal, so long tables stay fast) of `simulate(...,
-waves = ...)`, so a notebook can print signals per cycle without a waveform viewer. `Formal.prove`
+waves = ...)`, so a notebook can print signals per cycle without a waveform viewer, and `Waves` draws
+such per-cycle samples as an SVG timing diagram that a notebook displays inline. `Formal.prove`
emits a check module (`ForwardCheck.scala`: a unit next to its specification, written with
last-connect `when`s rather than a `MuxCase`, with an output `ok`) and runs Yosys's `sat -prove` on
it: a proof for every input of a combinational module, or a counterexample (a module with registers
diff --git a/riscv/src/main/scala/riscv/Waves.scala b/riscv/src/main/scala/riscv/Waves.scala
new file mode 100644
index 0000000..96c3e76
--- /dev/null
+++ b/riscv/src/main/scala/riscv/Waves.scala
@@ -0,0 +1,149 @@
+package riscv
+
+/** Draws per-cycle samples of signals (read from a `Vcd`, or logged in a test) as an SVG timing
+ * diagram, the picture a waveform viewer shows, small enough to sit in a notebook:
+ * `almond.display.Html(Waves.svg(...)).display()`.
+ *
+ * The caller picks the cycles and says when to sample each one (just before its rising clock
+ * edge, what the registers take at that edge); every lane then shows one value per cycle. A
+ * clock lane comes first. A `Bit` lane is drawn high or low, a `Bus` lane as a band with its
+ * value, split where the value changes; `mark` highlights cycles to look at.
+ */
+object Waves {
+ sealed trait Lane { def label: String }
+
+ /** A one-bit signal: high for 1, low for 0, a grey band at mid-level for x or z (-1, or any other
+ * value).
+ */
+ final case class Bit(label: String, value: Long => BigInt) extends Lane
+
+ /** A value per cycle, as text; an empty string draws no value (nothing valid there). */
+ final case class Bus(label: String, value: Long => String) extends Lane
+
+ // the widths of a character, for sizing the picture: of the 12 px monospace font of the values, of
+ // the 13 px font of the lane labels, and of the 12 px font of the caption
+ private val CharWidth = 7.4
+ private val LabelChar = 7.6
+ private val CaptionChar = 7.5 // generous: the font is proportional
+ private val RowHeight = 34
+ private val Header = 30
+ private val Mono = "'JetBrains Mono', 'DejaVu Sans Mono', monospace"
+
+ private def esc(s: String) = s.flatMap {
+ case '&' => "&"
+ case '<' => "<"
+ case '>' => ">"
+ case '"' => """
+ case c => if (c < ' ' && !"\t\n\r".contains(c)) "\uFFFD" else c.toString // XML allows no other control
+ }
+ // with a decimal point whatever the JVM's locale: SVG reads a comma as a separator
+ private def f(x: Double) = "%.1f".formatLocal(java.util.Locale.ROOT, x)
+
+ /** Runs of equal text: (first cycle, number of cycles, text). */
+ private def runs(texts: Seq[String]): List[(Int, Int, String)] =
+ texts.indices
+ .foldLeft(List.empty[(Int, Int, String)]) {
+ case ((s, k, t) :: rest, i) if texts(i) == t => (s, k + 1, t) :: rest
+ case (acc, i) => (i, 1, texts(i)) :: acc
+ }
+ .reverse
+
+ /** The diagram. `times`: when to sample each cycle; `names`: what to call each cycle (its
+ * number, say); `mark`: indexes into `times` of the cycles to highlight; `note`: a caption.
+ */
+ def svg(
+ times: Seq[Long],
+ names: Seq[String],
+ lanes: Seq[Lane],
+ mark: Set[Int] = Set.empty,
+ note: String = ""
+ ): String = {
+ require(times.nonEmpty && times.size == names.size, "one name per sampled cycle")
+ val n = times.size
+ val (ts, ns) = (times.toIndexedSeq, names.toIndexedSeq) // indexed by cycle below, which a List does slowly
+ val samples = lanes.map { // a bit per cycle, or the runs of a bus
+ case Bit(_, value) => Left(ts.map(value))
+ case Bus(_, value) => Right(runs(ts.map(value)))
+ }
+ val needed =
+ samples.collect { case Right(bands) => bands }.flatten.map { case (_, k, t) => (t.length * CharWidth + 16) / k }
+ val nameFit = ns.map(_.length * CharWidth + 8)
+ val cw = (needed :+ 24.0).max.min(220.0).max(if (n <= 24) nameFit.max else 0)
+ val labelW = (lanes.map(_.label.length * LabelChar + 28) :+ 110.0).max
+ val rows = lanes.size + 1 // the clock lane first
+ val bottom = Header + rows * RowHeight
+ val width = (labelW + n * cw + 16).max(note.length * CaptionChar + 28)
+ val height = bottom + (if (note.nonEmpty) 26 else 0) + 12
+ val colW = f(cw)
+ def x(i: Double) = f(labelW + i * cw)
+ def xy(i: Double, y: Double) = s"${x(i)},${f(y)}" // a point, at the start of cycle i
+ def top(row: Int) = Header + row * RowHeight + 7.0
+ def low(row: Int) = Header + (row + 1) * RowHeight - 7.0
+ def text(at: String, y: Double, size: Int, fill: String, content: String, extra: String = "") =
+ s"""${esc(content)}"""
+ def label(row: Int, s: String) = text("14", Header + row * RowHeight + 21, 13, "#2b3445", s)
+ def shade(i: Int, y: Double, height: Double, fill: String, opacity: Double) =
+ s""""""
+ val out = new StringBuilder
+ val w = f(width)
+ out ++= s""""
+ out.toString
+ }
+}
diff --git a/riscv/src/test/scala/riscv/DebugSpec.scala b/riscv/src/test/scala/riscv/DebugSpec.scala
index 56e1ff5..5f0370f 100644
--- a/riscv/src/test/scala/riscv/DebugSpec.scala
+++ b/riscv/src/test/scala/riscv/DebugSpec.scala
@@ -54,6 +54,46 @@ class DebugSpec extends AnyFlatSpec with ChiselSim {
assert(count(0) == -1 && count(2) == 0 && count(3) == 5 && vcd("TOP.dut.count")(3) == 5)
}
+ def parse(svg: String) = javax.xml.parsers.DocumentBuilderFactory
+ .newInstance()
+ .newDocumentBuilder()
+ .parse(new java.io.ByteArrayInputStream(svg.getBytes("UTF-8")))
+
+ "Waves" should "draw a clock, bits and buses as well-formed SVG" in {
+ val times = Seq(0L, 1L, 2L, 3L)
+ val svg = Waves.svg(
+ times,
+ times.map(_.toString),
+ Seq(
+ Waves.Bit("valid", t => if (t == 3) -1 else t % 2), // x in the last cycle
+ Waves.Bus("value", t => if (t < 2) "" else s"x10 = $t <&>") // to escape
+ ),
+ mark = Set(2),
+ note = "a note"
+ )
+ val doc = parse(svg)
+ val texts = doc.getElementsByTagName("text")
+ val shown = (0 until texts.getLength).map(texts.item(_).getTextContent)
+ assert(Seq("clock", "valid", "value", "a note", "x10 = 2 <&>", "x10 = 3 <&>").forall(shown.contains), shown)
+ assert(doc.getElementsByTagName("polygon").getLength == 2) // a band per value
+ assertThrows[IllegalArgumentException](Waves.svg(times, Seq("0"), Nil))
+ assertThrows[IllegalArgumentException](Waves.svg(Nil, Nil, Nil))
+
+ // one cycle, a value too long for its band, marks out of range, a decimal comma locale
+ val locale = java.util.Locale.getDefault
+ java.util.Locale.setDefault(java.util.Locale.GERMANY)
+ val one =
+ try Waves.svg(Seq(0L), Seq("0"), Seq(Waves.Bus("v", _ => "x" * 100)), mark = Set(-1, 5))
+ finally java.util.Locale.setDefault(locale)
+ val parsed = parse(one)
+ val number = "-?[0-9]+(\\.[0-9]+)?"
+ assert(parsed.getDocumentElement.getAttribute("width").matches(number), one)
+ assert(parsed.getElementsByTagName("rect").getLength == 1, one) // the frame, no highlight
+ val texts1 = parsed.getElementsByTagName("text")
+ val band = (0 until texts1.getLength).map(texts1.item(_).getTextContent).filter(_.startsWith("x"))
+ assert(band.size == 1 && band.head.endsWith("…") && band.head.length < 100, band)
+ }
+
"Co-simulation" should "find a forwarding unit that forwards the older result first" in {
simulate(new CoreSystem(new ForwardingCore(forwardUnit = new OldestFirst))) { c =>
val bug = CoSim.compare(c, Asm("li a0, 1\nli a0, 2\nadd a1, a0, a0\necall"))
diff --git a/riscv/src/test/scala/riscv/ForwardingSpec.scala b/riscv/src/test/scala/riscv/ForwardingSpec.scala
index 0cc4e40..5b6dc8c 100644
--- a/riscv/src/test/scala/riscv/ForwardingSpec.scala
+++ b/riscv/src/test/scala/riscv/ForwardingSpec.scala
@@ -48,7 +48,6 @@ class ForwardingSpec extends AnyFlatSpec with ChiselSim {
"WB to ID" -> "li a0, 5\nnop\nnop\naddi a1, a0, 1\necall",
"the newest result wins" -> "li a0, 1\nli a0, 2\nadd a1, a0, a0\necall",
"both operands" -> "li a0, 3\nli a1, 4\nsub a2, a1, a0\necall",
- "x0 is not forwarded" -> "addi zero, zero, 7\nadd a0, zero, zero\necall",
"load-use" -> "la t0, d\nlw a0, 0(t0)\naddi a1, a0, 1\necall\n.data\nd: .word 41",
"load-use, second operand" -> "la t0, d\nlw a0, 0(t0)\nadd a1, t0, a0\necall\n.data\nd: .word 41",
"load then store" -> "la t0, d\nlw a0, 0(t0)\nsw a0, 4(t0)\nlw a1, 4(t0)\necall\n.data\nd: .word 41, 0",
diff --git a/riscv/src/test/scala/riscv/PipelinedSpec.scala b/riscv/src/test/scala/riscv/PipelinedSpec.scala
index 3f225a1..8792888 100644
--- a/riscv/src/test/scala/riscv/PipelinedSpec.scala
+++ b/riscv/src/test/scala/riscv/PipelinedSpec.scala
@@ -1,6 +1,5 @@
package riscv
-import chisel3._
import chisel3.simulator.scalatest.ChiselSim
import org.scalatest.flatspec.AnyFlatSpec
@@ -10,18 +9,6 @@ import org.scalatest.flatspec.AnyFlatSpec
class PipelinedSpec extends AnyFlatSpec with ChiselSim {
val nops = 2
- "PipelineFlush" should "flush the wrong path after a jump, and everything younger after a halt" in {
- simulate(new PipelineFlush) { c =>
- for (redirect <- Seq(false, true); halt <- Seq(false, true)) {
- c.io.redirect.poke(redirect.B)
- c.io.halt.poke(halt.B)
- c.io.flushIfId.expect((redirect || halt).B)
- c.io.flushIdEx.expect((redirect || halt).B)
- c.io.flushExMem.expect(halt.B)
- }
- }
- }
-
// one feature each, and every halt cause (the halting instruction comes after padded ones too)
val programs = Seq(
"arithmetic" -> """
@@ -84,10 +71,12 @@ class PipelinedSpec extends AnyFlatSpec with ChiselSim {
}
// Unpadded, with the dependencies spaced by hand: younger instructions with side effects really sit
- // in the pipeline when a jump redirects it or an instruction halts it
+ // in the pipeline when a jump redirects it or an instruction halts it (a jump behind a halt: both
+ // at once). Together they catch any wrong output of PipelineFlush, as 5.6_pipeline.ipynb does.
val flushed = Seq(
- "a store in EX during a halt" -> "auipc t0, 0\nnop\nnop\nebreak\nsw t0, 0(t0)\nsw t0, 4(t0)",
- "stores on the wrong path" -> "auipc t0, 0\nnop\nnop\nj 1f\nsw t0, 0(t0)\nsw t0, 4(t0)\n1: ecall"
+ "stores on the wrong path" -> "auipc t0, 0\nnop\nnop\nj 1f\nsw t0, 0(t0)\nsw t0, 4(t0)\n1: ecall",
+ "stores behind a halt" -> "auipc t0, 0\nnop\nnop\nebreak\nsw t0, 0(t0)\nsw t0, 4(t0)\nsw t0, 8(t0)",
+ "a jump behind a halt" -> "auipc t0, 0\nnop\nnop\nebreak\nj 1f\nsw t0, 0(t0)\n1: sw t0, 4(t0)"
)
it should "let no younger instruction take effect after a jump or a halt" in {