Skip to content

Commit

Permalink
Update assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Vict0rAH committed Nov 27, 2020
1 parent df7bde4 commit 4614377
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 59 deletions.
65 changes: 21 additions & 44 deletions assertion/src/main/scala/assertionTiming/concurrentAssertion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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
}
}
}
15 changes: 11 additions & 4 deletions assertion/src/main/scala/mainOneHot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions assertion/src/test/scala/concurrentAssertionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
24 changes: 20 additions & 4 deletions assertion/src/test/scala/oneHotTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

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)
}
}
}
}

0 comments on commit 4614377

Please sign in to comment.