From d6ad0ad9e0227034668ec7ea8e97db5de0d7ce25 Mon Sep 17 00:00:00 2001 From: Martin Schoeberl Date: Tue, 23 Feb 2021 10:14:08 +0100 Subject: [PATCH] lab2: remove solutions --- lab2/src/test/scala/Accutest.scala | 68 ------ .../src/test/scala/AluAccuTest_Vict0rAH.scala | 195 ------------------ lab2/src/test/scala/AluAccuTester.scala | 132 ------------ .../src/test/scala/AluAccuTesterAndreas.scala | 110 ---------- lab2/src/test/scala/Tester.scala | 190 ----------------- 5 files changed, 695 deletions(-) delete mode 100644 lab2/src/test/scala/Accutest.scala delete mode 100644 lab2/src/test/scala/AluAccuTest_Vict0rAH.scala delete mode 100644 lab2/src/test/scala/AluAccuTester.scala delete mode 100644 lab2/src/test/scala/AluAccuTesterAndreas.scala delete mode 100644 lab2/src/test/scala/Tester.scala diff --git a/lab2/src/test/scala/Accutest.scala b/lab2/src/test/scala/Accutest.scala deleted file mode 100644 index 6dc186b..0000000 --- a/lab2/src/test/scala/Accutest.scala +++ /dev/null @@ -1,68 +0,0 @@ -import chisel3._ -import chisel3.iotesters._ -import Types._ -import org.scalatest._ - -class AccuTest(dut: AluAccuChisel)extends PeekPokeTester(dut) { - - poke(dut.io.ena, 1) - - /* - poke(dut.io.ena, 1) - poke(dut.io.din, w) - poke(dut.io.op, nop) - step(1) - expect(dut.io.accu, 0) - poke(dut.io.din, w) - poke(dut.io.op, add) - step(1) - poke(dut.io.din, 6) - poke(dut.io.op, add) - } - step(1) - expect(dut.io.accu, 10) */ - - - - var expectval = 0 - - /* poke(dut.io.ena, 1) - for(w <- 1 until 10){ - poke(dut.io.din, w) - poke(dut.io.op, add) - expectval = expectval + w - step(1) - expect(dut.io.accu, expectval) - } */ - - - for(w <- 0 until 10) { - val r = new scala.util.Random - val random = r.nextInt(1000000) - - poke(dut.io.din, random) - poke(dut.io.op, add) - expectval = expectval + random - println(random.toString) - step(1) - expect(dut.io.accu, expectval) - } - - - - -} - -object AccuTest extends App { - chisel3.iotesters.Driver (() => new AluAccuChisel(32)) { c => new AccuTest(c) - - } -} - -class AccuScalaTest extends FlatSpec with Matchers { - "Tester" should "pass" in { - chisel3.iotesters.Driver(() => new AluAccuChisel(32)) { - c => new AccuTest(c) - } should be (true) - } -} \ No newline at end of file diff --git a/lab2/src/test/scala/AluAccuTest_Vict0rAH.scala b/lab2/src/test/scala/AluAccuTest_Vict0rAH.scala deleted file mode 100644 index abbfd32..0000000 --- a/lab2/src/test/scala/AluAccuTest_Vict0rAH.scala +++ /dev/null @@ -1,195 +0,0 @@ -import chisel3._ -import chisel3.iotesters._ -import chisel3.util._ -import scala.util._ -import org.scalatest._ - -//The code below is using iotesters -class AluAccuTest(dut: AluAccuChisel) extends PeekPokeTester(dut) { - val i = 0 - val j = 0 - var temp = BigInt(0) - val size = 16 - val MaxUInt = (1 << 16) - 1 - val test = 256 - //Generating random numbers - var rgen = new Random(1472) - var r = rgen.nextInt(100) - - //Corner cases - val corner = Array(MaxUInt, 0) - - //Resets accumulator to zero - def reset() { - poke(dut.io.op, 6) - poke(dut.io.din, 0) - step(1) - expect(dut.io.accu, 0) - } - - //Tests the chosen corner values - def cornerTest() = { - reset() - step(1) - poke(dut.io.ena, true) - for (i <- 0 to 1) { - for (j <- 0 to 7) { - j match { - case 0 => { - poke(dut.io.op, 0) - expect(dut.io.accu, peek(dut.io.accu)) - } - case 1 => { - poke(dut.io.op, 6) - poke(dut.io.din, corner(i)) - step(1) - poke(dut.io.op, 1) - poke(dut.io.din, r) - step(1) - if ((corner(i) + r) > MaxUInt){ - expect(dut.io.accu, (corner(i) + r) - (1 << 16)) - } else { - expect(dut.io.accu, corner(i) + r) - } - } - case 2 => { - poke(dut.io.op, 6) - poke(dut.io.din, corner(i)) - step(1) - poke(dut.io.op, 2) - poke(dut.io.din, r) - step(1) - if ((corner(i) - r) < 0) { - expect(dut.io.accu, (1 << 16) - (corner(i) + r)) - } else { - expect(dut.io.accu, corner(i) - r) - } - } - case 3 => { - poke(dut.io.op, 6) - poke(dut.io.din, corner(i)) - step(1) - poke(dut.io.op, 3) - poke(dut.io.din, r) - step(1) - expect(dut.io.accu, corner(i) & r) - } - case 4 => { - poke(dut.io.op, 6) - poke(dut.io.din, corner(i)) - step(1) - poke(dut.io.op, 4) - poke(dut.io.din, r) - step(1) - expect(dut.io.accu, corner(i) | r) - } - case 5 => { - poke(dut.io.op, 6) - poke(dut.io.din, corner(i)) - step(1) - poke(dut.io.op, 5) - poke(dut.io.din, r) - step(1) - expect(dut.io.accu, corner(i) ^ r) - } - case 6 => { - poke(dut.io.op, 6) - poke(dut.io.din, corner(i)) - step(1) - expect(dut.io.accu, corner(i)) - } - case 7 => { - poke(dut.io.op, 6) - poke(dut.io.din, corner(i)) - step(1) - poke(dut.io.op, 7) - step(1) - expect(dut.io.accu, corner(i) >> 1) - } - case _ => { - expect(dut.io.accu, 0) - } - } - } - } - } - - poke(dut.io.ena, 1) - reset() - - //Testing add function - for (i <- 0 to test) { - poke(dut.io.op, 1) - poke(dut.io.din, i) - step(1) - expect(dut.io.accu, i*(i+1)/2) - } - - //Testing subtract function - poke(dut.io.op, 6) - poke(dut.io.din, test*(test+1)/2) - for (i <- 0.U to test) { - poke(dut.io.op, 2) - poke(dut.io.din, i) - println(peek(dut.io.din).toString) - step(1) - expect(dut.io.accu, (test*(test+1)/2)-i*(i+1)/2) - } - //Testing AND function - poke(dut.io.op, 6) - poke(dut.io.din, "b0101010101".U) - for (i <- 0 to test) { - temp = peek(dut.io.accu) - poke(dut.io.op, 3) - poke(dut.io.din, i) - step(1) - expect(dut.io.accu, i & temp) - } - - //Testing OR function - poke(dut.io.op, 6) - poke(dut.io.din, "b0101010101".U) - for (i <- 0 to test) { - temp = peek(dut.io.accu) - poke(dut.io.op, 4) - poke(dut.io.din, i) - step(1) - expect(dut.io.accu, i | temp) - } - - //Testing XOR function - poke(dut.io.op, 6) - poke(dut.io.din, "b0101010101".U) - for (i <- 0 to test) { - temp = peek(dut.io.accu) - poke(dut.io.op, 5) - poke(dut.io.din, i) - step(1) - expect(dut.io.accu, i ^ temp) - } - - //Testing SHIFT function - for (i <- 0 to test) { - poke(dut.io.op, 6) - poke(dut.io.din, i) - step(1) - poke(dut.io.op, 7) - step(1) - expect(dut.io.accu, i >> 1) - } - - cornerTest() -} - -object AluAccuTest extends App { - chisel3.iotesters.Driver(() => new AluAccuChisel(16)) - { c => new AluAccuTest(c)} -} - -class AluAccuScalaTest extends FlatSpec with Matchers { - "ALU tester" should "pass" in { - chisel3.iotesters.Driver(() => new AluAccuChisel(16)) { - c => new AluAccuTest(c) - } should be(true) - } -} \ No newline at end of file diff --git a/lab2/src/test/scala/AluAccuTester.scala b/lab2/src/test/scala/AluAccuTester.scala deleted file mode 100644 index 0cf993f..0000000 --- a/lab2/src/test/scala/AluAccuTester.scala +++ /dev/null @@ -1,132 +0,0 @@ -import chisel3._ -import chisel3.iotesters._ - -import scala.util.Random - -import org.scalatest._ - -// Select the operand size of the AluAccu under test and the number -// of randomly generated inputs to test for -object Test { - val sizes = List(16, 32, 64, 128) - val numtests = 1024 -} - -class AluAccuTester(dut: AluAccuChisel, size: Int) extends PeekPokeTester(dut) { - // This function runs a simple reset sequence to ensure the accumulator - // is zeroed out before the tests - def reset() = { // ld 0 - poke(dut.io.op, 6) - poke(dut.io.din, 0) - poke(dut.io.ena, true) - step(1) - expect(dut.io.accu, 0) - } - - // Test the enable functionality of the AluAccu - def enableTest() = { - poke(dut.io.op, 6) - poke(dut.io.din, 1) - poke(dut.io.ena, true) - step(1) - poke(dut.io.op, 1) - poke(dut.io.din, 2) - poke(dut.io.ena, false) - step(1) - expect(dut.io.accu, 1) - } - - // Test some corner cases of the accumulator input - def cornerTest() = { - val maxUint = (BigInt(1) << size) - 1 - // Relevant edge cases are 0 and max for UInt types - // Underflow - poke(dut.io.op, 6) - poke(dut.io.din, 0) - poke(dut.io.ena, true) - step(1) - poke(dut.io.op, 2) - poke(dut.io.din, 1) - step(1) - expect(dut.io.accu, func(2)(BigInt(0), BigInt(1)) & maxUint) - - // Overflow - poke(dut.io.op, 1) - step(1) - expect(dut.io.accu, 0) - } - - // This function runs through the generated inputs and operations and - // ensures their outputs are as expected - def test(inputs: Array[BigInt], ops: Array[Int], results: Array[BigInt]) = { - assert(inputs.length == ops.length) - assert(ops.length == results.length, "test: all arrays must be the same length!") - poke(dut.io.ena, true) - for (i <- 0 until results.length) { - poke(dut.io.op, ops(i)) - poke(dut.io.din, inputs(i)) - step(1) - expect(dut.io.accu, results(i).asUInt(size.W), "Op " + ops(i) + " failed") - } - } - - // Depending on the operation to be performed, given as input, returns a - // function corresponding to that of the AluAccu - def func(op: Int): (BigInt, BigInt) => BigInt = { - op match { - case 0 => (a, _) => a - case 1 => (a, b) => a + b - case 2 => (a, b) => a - b - case 3 => (a, b) => a & b - case 4 => (a, b) => a | b - case 5 => (a, b) => a ^ b - case 6 => (_, b) => b - case 7 => (a, _) => a >> 1 - case _ => (_, _) => BigInt(0) - } - } - - // Generate a bunch of random numbers - val rng = new Random(12345678) - val mask = (BigInt(1) << size) - 1 - val inputs = Array.fill(Test.numtests)(BigInt.apply(size, rng)).map(_ & mask) - val ops = Array.fill(Test.numtests)(rng.nextInt()).map(_ & 0x7) - val results = inputs.zip(ops).foldLeft(Array[BigInt](0)) { - (acc, tup) => - tup match { - case (i, op) => acc :+ (func(op)(acc.last, i) & mask) - } - }.drop(1) - - // Reset the accumulator - reset() - - // Test enable - enableTest() - reset() - - // Test corner case inputs - cornerTest() - reset() - - // Run through all the operations with random inputs - test(inputs, ops, results) -} - -object AluAccuTester extends App { - for (size <- Test.sizes) { - chisel3.iotesters.Driver(() => new AluAccuChisel(size)) { - c => new AluAccuTester(c, size) - } - } -} - -class AluAccuSTester extends FlatSpec with Matchers { - for (size <- Test.sizes) { - size + "-bit tester" should "pass" in { - chisel3.iotesters.Driver(() => new AluAccuChisel(size)) { - c => new AluAccuTester(c, size) - } should be (true) - } - } -} diff --git a/lab2/src/test/scala/AluAccuTesterAndreas.scala b/lab2/src/test/scala/AluAccuTesterAndreas.scala deleted file mode 100644 index 4ee01dd..0000000 --- a/lab2/src/test/scala/AluAccuTesterAndreas.scala +++ /dev/null @@ -1,110 +0,0 @@ -import chisel3._ -import chisel3.iotesters._ -import org.scalatest._ -import scala.util.Random - -class AluAccuTesterAndreas(dut: AluAccuChisel, bitSize: Int) extends PeekPokeTester(dut) { - val maxValueMask = (1 << bitSize) - 1 - val rng = new Random(643) - var aluReg = 0 - - def exeExpected(op: UInt, in: Int) : Int = { - val result = op match { - case Types.nop => aluReg - case Types.add => aluReg + in - case Types.sub => aluReg - in - case Types.and => aluReg & in - case Types.or => aluReg | in - case Types.xor => aluReg ^ in - case Types.ld => in - case Types.shr => aluReg >>> 1 - case _ => throw new Exception("Unknown operation: " + op) - } - return result & maxValueMask - } - - def exeActual(op: UInt, in: Int, run: Boolean) : Int = { - poke(dut.io.ena, run) - poke(dut.io.op, op) - poke(dut.io.din, in) - step(1) - - return peek(dut.io.accu).intValue() - } - - def testOp(op: UInt, input: Int, run: Boolean) = { - val beforeExe = peek(dut.io.accu).intValue() - - val expected = exeExpected(op, input) - val actual = exeActual(op, input, run) - - if (run) { - assert(expected == actual, "Expected: " + expected + ", Actual: " + actual + ", Op: " + op) - aluReg = expected - } - else { - val afterExe = peek(dut.io.accu).intValue() - assert(beforeExe == afterExe, "Accu register changed while not being enabled. Before: " + beforeExe + " Actual: " + afterExe + ", Op: " + op) - } - } - - def testRandomOps(count: Int, opTypes: List[UInt]) = { - var remainingOps = count - while (remainingOps > 0) { - val op = opTypes(rng.nextInt(opTypes.size - 1)) - val input = rng.nextInt(maxValueMask) - val run = rng.nextBoolean() - testOp(op, input, run) - - if (run) { - remainingOps = remainingOps - 1 - } - } - } -} - -class AluAccuTesterAndreasa extends FlatSpec with Matchers { - def runTest(bitSize: Int, callback: (AluAccuTesterAndreas) => Unit) = { - chisel3.iotesters.Driver(() => new AluAccuChisel(bitSize)) { - c => { - val tester = new AluAccuTesterAndreas(c, bitSize) - callback(tester) - tester - } - } - } - val enabledOptions = List(true, false) - val bitSizes = List(1, 3, 6, 10, 27) - val opTypes = List(Types.nop, Types.add, Types.sub, Types.and, Types.or, Types.xor, Types.ld, Types.shr) - val inputValues = List(0, 1, 2, 3, 4) - - for (isEnabled <- enabledOptions) { - for (bitSize <- bitSizes) { - val maxValue = (1 << bitSize) - 1 - for (opType <- opTypes) { - for (input <- inputValues) { - //can't give an input that's larger than what the alu can use - if (maxValue >= input) { - s"enabled: ${isEnabled}, bits: ${bitSize}, op: ${opType}, input: ${input}" should "pass" in { - runTest(bitSize, tester => { tester.testOp(opType, input, isEnabled) }) - } - } - } - //if the max value hasn't already been tested then make such a test - if (!inputValues.contains(maxValue)) - { - s"enabled: ${isEnabled}, bits: ${bitSize}, op: ${opType}, input: ${maxValue}" should "pass" in { - runTest(bitSize, tester => { tester.testOp(opType, maxValue, isEnabled) }) - } - } - } - } - } - - val randomOpCounts = List(10, 100, 10000) - for (bitSize <- bitSizes) { - for (opsCount <- randomOpCounts) { - s"${opsCount} random ops with ${bitSize} bit" should "pass" in { runTest(bitSize, tester => { tester.testRandomOps(opsCount, opTypes) })} - } - } -} \ No newline at end of file diff --git a/lab2/src/test/scala/Tester.scala b/lab2/src/test/scala/Tester.scala deleted file mode 100644 index 587e1b6..0000000 --- a/lab2/src/test/scala/Tester.scala +++ /dev/null @@ -1,190 +0,0 @@ -//Kishan's Tester - -import java.math.BigInteger -import Types._ -import org.scalatest._ -import chisel3._ -import chisel3.fromBigIntToLiteral -import chisel3.util._ -import chisel3.iotesters._ -import scala.util.Random.nextInt -import scala.math.BigInt -import scala.math.Numeric.BigIntIsIntegral.abs - -object Test2{ - println("Bit Size: ") - val size:Int = scala.io.StdIn.readLine.toInt - println("Number of Tests: ") - val n_tests:Int= scala.io.StdIn.readLine.toInt - def maxval(): Int = { - if (size>31){Int.MaxValue} - else{(1 << size) - 1 } - } -} - - -//enable and reset test - -class ena_tester (dut: AluAccuChisel) extends PeekPokeTester(dut){ - //enable test - var i=0 - poke(dut.io.ena,1) - poke(dut.io.op,1) - poke(dut.io.din,2) - step(1) - //enable=0 - poke(dut.io.ena,0) - step(1) - if(expect(dut.io.accu,2)){ - i+=1 - } - //enable=1 test - poke(dut.io.ena,1) - step(1) - if(expect(dut.io.accu,4)){ - i+=1 - } - if(i==2) println("Enable test Passed") - step(1) - reset(1) - if(expect(dut.io.accu,0)){ - i+=1 - } - if(i==3) println("Reset test Passed") -} - -object ena_tester extends App { - chisel3.iotesters.Driver (() => new AluAccuChisel(Test2.size)) { c => - new ena_tester (c) - } -} - -//complete tester -class Tester (dut: AluAccuChisel) extends PeekPokeTester(dut){ - //Random variable generation for inputs - var count = 0 - val r= scala.util.Random - var din=scala.util.Random - val l=List(Types.nop,Types.add,Types.sub,Types.and,Types.or,Types.xor,Types.ld,Types.shr) - //Always enabled - poke(dut.io.ena, 1) - reset(1) - reset(0) - for (i <- 0 until Test2.n_tests){ - println("Test " + i.toString) - //Maximum value for the respective type of bits - val max_val:BigInt=(BigInt(1) << Test2.size) - BigInt(1) - println("max val is is :" + max_val.toString) -// if(Test2.size > 31) { val o = Int.MaxValue -// println("Test2>31 path "+ o.toString)} -// else{val o = (1 << Test2.size) - (1) -// println("Test<31 path "+o.toString)} -// println("max input(t) is: "+o.toString) - val o = Test2.maxval() - //Random operation gen - val op: Int = r.nextInt(7) - //Random Input gen - val i_din:BigInt = din.nextInt(o) - println("Input is: " + i_din.toString) - poke(dut.io.din,i_din) - poke(dut.io.op,l(op)) - - - //Output of previous operation - val output = peek(dut.io.accu)//fromBigIntToLiteral(peek(dut.io.accu)).asUInt(64.W) - println("Accumulator value of previous operation is: " + peek(dut.io.accu).toString) - println("dut.io.op is: " + op.toString) - step(1) - - //Result of current operation - println("Accumulator value of current operation is: " + peek(dut.io.accu).toString) - val s:BigInt = op match{ - case 0 => output - case 1 => output + (i_din) - case 2 => output - i_din //output.asUInt(64.W) - i.asUInt(64.W) - case 3 => output & i_din - case 4 => output | i_din - case 5 => output ^ (i_din) - case 6 => i_din - case 7 => output >> 1 - } - //println("Maximum value is: " + t.toString) - //val t= (1.U(65.W) << 63) - 1.U(65.W) - //val t=scala.math.pow(2,Test2.size).toLong - if (s < 0) { - println("s is <0 path: "+ s.toString) - val r = max_val - abs(s) + 1 - println("Calculated value of output is: " + r.toString) - if(expect(dut.io.accu,r)){ - println("Test " + i.toString +" Passed\n\n") - count+=1 - } - } - else{ - if (s > max_val){ - println("s is >max path: "+ s.toString) - val q = s - max_val -1 - println("Calculated value of output is: " + q.toString) - if(expect(dut.io.accu,q)){ - count+=1 - println("Test " + i.toString +" Passed\n\n") - } - } - else{ - println("s is none path: "+ s.toString) - println("Calculated value of operation is: " + s.toString) - if(expect(dut.io.accu,s)){ - count+=1 - println("Test " + i.toString +" Passed\n\n") - } - } - } - } - if (Test2.n_tests == count) println("All Tests Passed!") -} - -object Tester extends App { - chisel3.iotesters.Driver (() => new AluAccuChisel(Test2.size)) { c => - new Tester (c) - } -} - - -//Maximum value cases -class MaxCases(dut: AluAccuChisel) extends PeekPokeTester(dut){ - //test counter - var i=0 - //clearing register - poke(dut.io.ena,1) - //poke(dut.io.din,0) - //poke(dut.io.op,Types.ld) - reset(1) - reset(0) - val l=List(Types.nop,Types.add,Types.sub) - val maxvalue:BigInt = (BigInt(1) << Test2.size) - 1 - println("maximum val" + maxvalue.toString) - poke(dut.io.din,maxvalue) - poke(dut.io.op,Types.sub) - step(1) - if(expect(dut.io.accu,1)) i+=1 - reset(1) - step(1) - reset(0) - poke(dut.io.din,1) - poke(dut.io.op,Types.sub) - step(1) - step(1) - if(expect(dut.io.accu,maxvalue)) i+=1 - step(1) - poke(dut.io.op,Types.add) - poke(dut.io.din,maxvalue) - //println("output val is:" + peek(dut.io.accu).toString) - if(expect(dut.io.accu,maxvalue - 1)) i+=1 - if(i==3) println("All Tests Passed!") -} -object MaxCases extends App { - chisel3.iotesters.Driver (() => new AluAccuChisel(Test2.size)) { c => - new MaxCases (c) - } -} -