Skip to content

Commit f899d3f

Browse files
committed
fixed compile errors, made problem templates match solutions, trimmed unneeded imports, fixed testcase logic for DynamicMemorySearch
1 parent 26a94ea commit f899d3f

File tree

12 files changed

+54
-49
lines changed

12 files changed

+54
-49
lines changed

problems/Accumulator.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package TutorialProblems
22

33
import Chisel._
4-
import Node._
54

65
class Accumulator extends Module {
76
val io = new Bundle {
@@ -19,7 +18,7 @@ class AccumulatorTests(c: Accumulator) extends Tester(c) {
1918
val in = rnd.nextInt(2)
2019
poke(c.io.in, in)
2120
step(1)
22-
expect(c.io.out, tot)
2321
if (in == 1) tot += 1
22+
expect(c.io.out, tot)
2423
}
2524
}

problems/Counter.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package TutorialProblems
22

33
import Chisel._
4-
import Node._
54
import Counter._
65

76
object Counter {
@@ -14,7 +13,7 @@ object Counter {
1413
// amt only when en is asserted
1514
// ---------------------------------------- \\
1615

17-
def counter(max: UInt, en: Bool, amt: UInt) = {
16+
def counter(max: UInt, en: Bool, amt: UInt): UInt = {
1817
val x = Reg(init=UInt(0, max.getWidth))
1918
x := wrapAround(x + UInt(1), max)
2019
x
@@ -44,7 +43,7 @@ class CounterTest(c: Counter) extends Tester(c) {
4443

4544
// let it spin for a bit
4645
for (i <- 0 until 5) {
47-
step(vars, isTrace = false)
46+
step(1)
4847
}
4948

5049
for (i <- 0 until 10) {
@@ -53,8 +52,8 @@ class CounterTest(c: Counter) extends Tester(c) {
5352
poke(c.io.inc, if (inc) 1 else 0)
5453
poke(c.io.amt, amt)
5554
step(1)
56-
expect(c.io.tot, curCnt)
5755
curCnt = if(inc) intWrapAround(curCnt + amt, 255) else curCnt
56+
expect(c.io.tot, curCnt)
5857
}
5958
}
6059

problems/DynamicMemorySearch.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ class DynamicMemorySearch extends Module {
1212
val done = Bool(OUTPUT)
1313
}
1414
val index = Reg(init = UInt(0, width = 3))
15-
val memVal = UInt(0) // TODO REPLACE HERE WITH MEM
15+
val memVal = UInt(0) // TODO replace here with mem
1616
val done = !io.en && ((memVal === io.data) || (index === UInt(7)))
17+
// TODO handle writes to memory (isWr, wrAddr, data)
1718
when (io.en) {
1819
index := UInt(0)
1920
} .elsewhen (done === Bool(false)) {
@@ -24,19 +25,19 @@ class DynamicMemorySearch extends Module {
2425
}
2526

2627
class DynamicMemorySearchTests(c: DynamicMemorySearch) extends Tester(c) {
27-
val list = Array.fill(8){ 0 }
28+
val list = Array.fill(8){ 0 }
2829
for (k <- 0 until 16) {
2930
// WRITE A WORD
30-
poke(c.io.en, 0)
31+
poke(c.io.en, 0)
3132
poke(c.io.isWr, 1)
32-
val wrAddr = rnd.nextInt(8-1)
33-
val data = rnd.nextInt(16)
33+
val wrAddr = rnd.nextInt(8-1)
34+
val data = rnd.nextInt(15) + 1 // can't be 0
3435
poke(c.io.wrAddr, wrAddr)
3536
poke(c.io.data, data)
3637
step(1)
3738
list(wrAddr) = data
3839
// SETUP SEARCH
39-
val target = rnd.nextInt(16)
40+
val target = if (k>12) rnd.nextInt(16) else data
4041
poke(c.io.isWr, 0)
4142
poke(c.io.data, target)
4243
poke(c.io.en, 1)
@@ -46,7 +47,9 @@ class DynamicMemorySearchTests(c: DynamicMemorySearch) extends Tester(c) {
4647
step(1)
4748
} while (peek(c.io.done) == 0)
4849
val addr = peek(c.io.target).toInt
49-
expect(addr == list.length || list(addr) == target,
50-
"LOOKING FOR " + target + " FOUND " + addr)
50+
if (list contains target)
51+
expect(list(addr) == target, "LOOKING FOR " + target + " FOUND " + addr)
52+
else
53+
expect(addr==(list.length-1), "LOOKING FOR " + target + " FOUND " + addr)
5154
}
5255
}

problems/LFSR16.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class LFSR16Tests(c: LFSR16) extends Tester(c) {
1717
val inc = rnd.nextInt(2)
1818
poke(c.io.inc, inc)
1919
step(1)
20-
expect(c.io.out, res)
2120
if (inc == 1) {
2221
val bit = ((res >> 0) ^ (res >> 2) ^ (res >> 3) ^ (res >> 5) ) & 1;
2322
res = (res >> 1) | (bit << 15);
2423
}
24+
expect(c.io.out, res)
2525
}
2626
}

problems/MaxN.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class MaxN(val n: Int, val w: Int) extends Module {
77
private def Max2(x: UInt, y: UInt) = Mux(x > y, x, y)
88

99
val io = new Bundle {
10-
val in = Vec.fill(n){ UInt(INPUT, w) }
10+
val ins = Vec.fill(n){ UInt(INPUT, w) }
1111
val out = UInt(OUTPUT, w)
1212
}
13-
io.out := io.in.reduceLeft(Max2)
13+
io.out := io.ins.reduceLeft(Max2)
1414
}
1515

1616
class MaxNTests(c: MaxN) extends Tester(c) {

problems/Mux4.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package TutorialProblems
22

33
import Chisel._
4-
import scala.math._
54

65
class Mux4 extends Module {
76
val io = new Bundle {

problems/RealGCD.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@ class RealGCDTests(c: RealGCD) extends Tester(c) {
2323

2424
var i = 0
2525
do {
26+
var transfer = false
2627
do {
2728
poke(c.io.in.bits.a, inputs(i)._1)
2829
poke(c.io.in.bits.b, inputs(i)._2)
2930
poke(c.io.in.valid, 1)
31+
transfer = (peek(c.io.in.ready) == 1)
3032
step(1)
31-
} while (t < 100 && peek(c.io.in.ready) == 0);
33+
} while (t < 100 && !transfer)
3234

3335
do {
3436
poke(c.io.in.valid, 0)
3537
step(1)
36-
} while (t < 100 && peek(c.io.out.valid) == 0);
38+
} while (t < 100 && (peek(c.io.out.valid) == 0))
3739
expect(c.io.out.bits, outputs(i))
3840
i += 1;
39-
} while (t < 100 && i < 3);
40-
if (t >= 100) ok = false;
41+
} while (t < 100 && i < 3)
42+
if (t >= 100) ok = false
4143
}

problems/VendingMachine.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@ class VendingMachine extends Module {
1717
}
1818

1919
class VendingMachineTests(c: VendingMachine) extends Tester(c) {
20-
var money = 0
20+
var money = 0
21+
var isValid = false
2122
for (t <- 0 until 20) {
2223
val coin = rnd.nextInt(3)*5
2324
val isNickel = coin == 5
2425
val isDime = coin == 10
26+
27+
// Advance circuit
2528
poke(c.io.nickel, Bool(isNickel).litValue())
26-
poke(c.io.dime, Bool(isDime).litValue())
29+
poke(c.io.dime, Bool(isDime).litValue())
2730
step(1)
28-
val isValid = money >= 20
29-
expect(c.io.valid, Bool(isValid).litValue())
31+
32+
// Advance model
3033
money = if (isValid) 0 else (money + coin)
34+
isValid = money >= 20
35+
36+
// Compare
37+
expect(c.io.valid, Bool(isValid).litValue())
3138
}
3239
}

solutions/Counter.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package TutorialSolutions
22

33
import Chisel._
44
import Counter._
5-
import scala.collection.mutable.ArrayBuffer
65

76
object Counter {
87

solutions/DynamicMemorySearch.scala

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ class DynamicMemorySearch extends Module {
88
val wrAddr = UInt(INPUT, 3)
99
val data = UInt(INPUT, 4)
1010
val en = Bool(INPUT)
11-
val elt = UInt(OUTPUT, 4)
12-
val target = UInt(OUTPUT, 4)
11+
val target = UInt(OUTPUT, 3)
1312
val done = Bool(OUTPUT)
1413
}
15-
val index = Reg(init = UInt(0, width = 3))
16-
val list = Mem(UInt(width = 4), 8)
17-
val elt = list(index)
18-
io.elt := elt
19-
val done = !io.en && ((elt === io.data) || (index === UInt(7)))
14+
val index = Reg(init = UInt(0, width = 3))
15+
val list = Mem(UInt(width = 4), 8)
16+
val memVal = list(index)
17+
val done = !io.en && ((memVal === io.data) || (index === UInt(7)))
2018
when (io.isWr) {
2119
list(io.wrAddr) := io.data
2220
} .elsewhen (io.en) {
@@ -32,28 +30,28 @@ class DynamicMemorySearchTests(c: DynamicMemorySearch) extends Tester(c) {
3230
val list = Array.fill(8){ 0 }
3331
for (k <- 0 until 16) {
3432
// WRITE A WORD
35-
poke(c.io.en, 0)
33+
poke(c.io.en, 0)
3634
poke(c.io.isWr, 1)
3735
val wrAddr = rnd.nextInt(8-1)
38-
val data = rnd.nextInt(16)
36+
val data = rnd.nextInt(15) + 1 // can't be 0
3937
poke(c.io.wrAddr, wrAddr)
4038
poke(c.io.data, data)
4139
step(1)
4240
list(wrAddr) = data
4341
// SETUP SEARCH
44-
// val target = rnd.nextInt(16)
45-
val target = data
42+
val target = if (k>12) rnd.nextInt(16) else data
4643
poke(c.io.isWr, 0)
4744
poke(c.io.data, target)
48-
poke(c.io.en, 1)
45+
poke(c.io.en, 1)
4946
step(1)
5047
do {
5148
poke(c.io.en, 0)
5249
step(1)
53-
peek(c.index); peek(c.io.elt)
5450
} while (peek(c.io.done) == 0)
5551
val addr = peek(c.io.target).toInt
56-
expect(addr == list.length || list(addr) == target,
57-
"LOOKING FOR " + target + " FOUND " + addr)
52+
if (list contains target)
53+
expect(list(addr) == target, "LOOKING FOR " + target + " FOUND " + addr)
54+
else
55+
expect(addr==(list.length-1), "LOOKING FOR " + target + " FOUND " + addr)
5856
}
5957
}

0 commit comments

Comments
 (0)