From 46143774fe508768d0dad7844f1d0e570307dc47 Mon Sep 17 00:00:00 2001 From: VoldsommeViggo Date: Fri, 27 Nov 2020 09:35:03 +0100 Subject: [PATCH] Update assertion --- .../assertionTiming/concurrentAssertion.scala | 65 ++++++------------- assertion/src/main/scala/mainOneHot.scala | 15 +++-- .../test/scala/concurrentAssertionTest.scala | 14 ++-- assertion/src/test/scala/oneHotTest.scala | 24 +++++-- 4 files changed, 59 insertions(+), 59 deletions(-) diff --git a/assertion/src/main/scala/assertionTiming/concurrentAssertion.scala b/assertion/src/main/scala/assertionTiming/concurrentAssertion.scala index c6ad0e5..1c985d9 100644 --- a/assertion/src/main/scala/assertionTiming/concurrentAssertion.scala +++ b/assertion/src/main/scala/assertionTiming/concurrentAssertion.scala @@ -9,6 +9,7 @@ import chisel3._ * the specified amount of clock cycles. If the condition evaluates to false, * the circuit simulation stops with an error. * + * @param dut the device under test * @param cond condition, assertion fires (simulation fails) when false * @param message optional format string to print when assertion fires * @param cycles optional amount of clock cycles for which the assertion is @@ -27,7 +28,7 @@ import chisel3._ * Checks for the argument condition to not be true in the number of cycles passed */ object assertNever { - def apply[T <: Module](dut: T, cond: () => Boolean, message: String, cycles: Int) = { + def apply[T <: Module](dut: T, cond: () => Boolean = () => true, cycles: Int = 1, message: String = "Error") = { // Assertion for single thread clock cycle 0 assert(cond(), message) @@ -44,7 +45,7 @@ object assertNever { * Checks for the argument condition to be true in the number of cycles passed */ object assertAlways { - def apply[T <: Module](dut: T, cond: () => Boolean = () => true, message: String = "Error", cycles: Int = 1) = { + def apply[T <: Module](dut: T, cond: () => Boolean = () => true, cycles: Int = 1, message: String = "Error") = { // Assertion for single thread clock cycle 0 assert(cond(), message) @@ -70,19 +71,11 @@ object assertAlways { * already executed (cycle zero) */ object assertEventually { - def apply[T <: Module](dut: T, cond: () => Boolean, message: String, cycles: Int) = { + def apply[T <: Module](dut: T, cond: () => Boolean = () => true, cycles: Int = 1, message: String = "Error") = { var i = 0 fork { - /*for (i <- 0 until cycles) { - if (cond()) { - breakOut - } else if (i == cycles - 1){ - assert(false, message) - } - dut.clock.step(1) - }*/ while (!cond()) { if (i == cycles) { assert(false, message) @@ -103,22 +96,12 @@ object assertEventually { * Must be joined */ object assertEventuallyAlways { - def apply[T <: Module](dut: T, cond: () => Boolean, message: String, cycles: Int) = { + def apply[T <: Module](dut: T, cond: () => Boolean = () => true, cycles: Int = 1, message: String = "Error") = { var i = 1 // Assertion for single thread clock cycle 0 assert(cond(), message) - /*for (i <- 0 until cycles) { - if (cond()) { - break - } else { - // Exception - assert(false, message) - } - k += 1 - }*/ - fork { while (!cond()) { if (i == cycles-1) { @@ -144,31 +127,25 @@ object assertEventuallyAlways { * one bit asserted high. In a datapath circuit the checker * can ensure that the enabling conditions for a bus do not * result in bus contention. + * This can be combined with any of the other assertions + * because it returns a boolean value. */ object assertOneHot { - def apply(cond: UInt, message: String, cycles: Int) { - /*var i = 0 - while (cond > 0.U) { - if ((cond & 1.U) == 1.U) { - i += 1 - } - cond >> 1 - } - assert(i == 1, "One Hot violation")*/ - - val a = 0x8000011; - var abin = a.toBinaryString - System.out.println(abin) - var j = 0 - val k = '1' - for(i <- 0 until 24){ - if(abin.charAt(i) == k){ - j += 1 - System.out.println("fuck") + def apply(signal: UInt, message: String = "Error") : Boolean = { + + var in = signal.litValue + var i = 0 + + while ((in > 0)) { + if ((in & 1) == 1) { + i = i + 1 } - if(j > 1){ - assert(false) + in = in >> 1 + if ((i > 1) == true) { + assert(false, message) } } + + return true } -} +} \ No newline at end of file diff --git a/assertion/src/main/scala/mainOneHot.scala b/assertion/src/main/scala/mainOneHot.scala index 2cbb6ca..8f4c218 100644 --- a/assertion/src/main/scala/mainOneHot.scala +++ b/assertion/src/main/scala/mainOneHot.scala @@ -6,13 +6,20 @@ import concurrent._ // Compiles, but simple test does not yet work class mainOneHot extends Module { val io = IO(new Bundle { - val s = Input(UInt(4.W)) + val s = Input(UInt(2.W)) val c = Output(UInt(4.W)) }) - reg := Mux(io.s, 4.U, 0.U) + - io.c := reg + val reg = RegInit(0.U(4.W)) - + switch (io.s) { + is ("b00".U) { reg := "b0001".U} + is ("b01".U) { reg := "b0010".U} + is ("b10".U) { reg := "b0100".U} + is ("b11".U) { reg := "b1000".U} + } + + io.c := reg } \ No newline at end of file diff --git a/assertion/src/test/scala/concurrentAssertionTest.scala b/assertion/src/test/scala/concurrentAssertionTest.scala index b253ea2..042cbe1 100644 --- a/assertion/src/test/scala/concurrentAssertionTest.scala +++ b/assertion/src/test/scala/concurrentAssertionTest.scala @@ -134,7 +134,7 @@ class concurrentAssertionTest extends FlatSpec with ChiselScalatestTester with M dut.io.s.poke(false.B) dut.clock.step(1) - val t = assertEventually(dut, () => dut.io.c.peek.litValue == 4, "Error", 5) + val t = assertEventually(dut, () => dut.io.c.peek.litValue == 4, 5, "Error") dut.clock.step(4) dut.io.s.poke(true.B) @@ -165,8 +165,8 @@ class concurrentAssertionTest extends FlatSpec with ChiselScalatestTester with M dut.io.a.poke(false.B) dut.io.b.poke(false.B) dut.clock.step(1) - val c = assertEventually(dut, () => dut.io.c.peek.litValue == 4, "Error1", 7) - val d = assertEventually(dut, () => dut.io.d.peek.litValue == 4, "Error2", 3) + val c = assertEventually(dut, () => dut.io.c.peek.litValue == 4, 7, "Error1") + val d = assertEventually(dut, () => dut.io.d.peek.litValue == 4, 3, "Error2") dut.clock.step(2) dut.io.b.poke(true.B) dut.clock.step(4) @@ -184,8 +184,8 @@ class concurrentAssertionTest extends FlatSpec with ChiselScalatestTester with M dut.io.a.poke(false.B) dut.io.b.poke(false.B) dut.clock.step(1) - val c = assertEventually(dut, () => dut.io.c.peek.litValue == 4, "Error1", 7) - val d = assertEventually(dut, () => dut.io.d.peek.litValue == 4, "Error2", 3) + val c = assertEventually(dut, () => dut.io.c.peek.litValue == 4, 7, "Error1") + val d = assertEventually(dut, () => dut.io.d.peek.litValue == 4, 3, "Error2") dut.clock.step(1) dut.io.b.poke(true.B) dut.clock.step(1) @@ -207,8 +207,8 @@ class concurrentAssertionTest extends FlatSpec with ChiselScalatestTester with M dut.io.a.poke(false.B) dut.io.b.poke(false.B) dut.clock.step(1) - val c = assertEventually(dut, () => dut.io.c.peek.litValue == 4, "Error1", 7) - val d = assertEventually(dut, () => dut.io.d.peek.litValue == 4, "Error2", 3) + val c = assertEventually(dut, () => dut.io.c.peek.litValue == 4, 7, "Error1") + val d = assertEventually(dut, () => dut.io.d.peek.litValue == 4, 3, "Error2") dut.clock.step(2) dut.io.b.poke(true.B) dut.clock.step(4) diff --git a/assertion/src/test/scala/oneHotTest.scala b/assertion/src/test/scala/oneHotTest.scala index 2f2568c..d47deb1 100644 --- a/assertion/src/test/scala/oneHotTest.scala +++ b/assertion/src/test/scala/oneHotTest.scala @@ -9,10 +9,26 @@ class oneHotTest extends FlatSpec with ChiselScalatestTester with Matchers { it should "test count of bitset in binary" in { test(new mainOneHot()) { dut => { - dut.io.s.poke(true.B) - assertOneHot(100.U, "Error", 10) - dut.io.c.expect(1.U) + dut.io.s.poke("b10".U) + dut.clock.step(1) + assertOneHot("b1000".U, "Error") + dut.io.c.expect("b0100".U) } } } -} \ No newline at end of file + + it should "test count of bitset in binary concurrently" in { + test(new mainOneHot()) { + dut => { + dut.io.s.poke("b10".U) + dut.clock.step(1) + assertAlways(dut, () => assertOneHot(dut.io.c.peek), 10) + dut.clock.step(2) + dut.io.s.poke("b11".U) + dut.clock.step(2) + dut.io.s.poke("b00".U) + } + } + } +} +