-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCoreTest.scala
102 lines (90 loc) · 2.66 KB
/
CoreTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package core
import chisel3._
import chisel3.iotesters.{Driver, PeekPokeTester}
import scala.io.Source
import java.io.{File, PrintWriter}
import io._
import sim._
import utils.ArgParser
class CoreWrapper(initFile: String) extends Module {
val io = IO(new DebugIO)
val core = Module(new Core)
val rom = Module(new ROM(initFile))
val ram = Module(new RAM)
core.io.irq.timer := false.B
core.io.irq.soft := false.B
core.io.irq.extern := false.B
core.io.rom <> rom.io
core.io.ram <> ram.io
core.io.debug <> io
}
class CoreUnitTester(c: CoreWrapper, traceFile: String, genTrace: Boolean)
extends PeekPokeTester(c) {
val endFlag = BigInt("deadc0de", 16)
// perform trace comparison
def runTrace(source: Source) = {
for (line <- source.getLines) {
val pc :: addr :: data :: Nil = line.split(' ').toList
do {
step(1)
} while (peek(c.io.regWen) == 0 || peek(c.io.regWaddr) == 0)
expect(c.io.pc, BigInt(pc, 16))
expect(c.io.regWaddr, BigInt(addr, 10))
expect(c.io.regWdata, BigInt(data, 16))
}
}
// generate trace using Fuxi core
def generateTrace(file: File) = {
val p = new PrintWriter(file)
try {
do {
step(1)
println(s"cycle: $t")
if (peek(c.io.regWen) != 0 && peek(c.io.regWaddr) != 0) {
p.print(f"${peek(c.io.pc)}%08x ")
p.print(f"${peek(c.io.regWaddr)}%02d ")
p.print(f"${peek(c.io.regWdata)}%08x\n")
}
} while (peek(c.io.regWen) == 0 || peek(c.io.regWdata) != endFlag)
}
finally {
p.close()
}
}
// print trace to console
def printTrace() = {
do {
step(1)
println(s"cycle: $t")
println(f" pc: 0x${peek(c.io.pc)}%x")
println(f" wen: ${peek(c.io.regWen)}%d")
println(f" addr: ${peek(c.io.regWaddr)}%d")
println(f" data: 0x${peek(c.io.regWdata)}%x")
} while (peek(c.io.regWen) == 0 || peek(c.io.regWdata) != endFlag)
}
if (traceFile.isEmpty) {
printTrace()
}
else if (!genTrace) {
runTrace(Source.fromFile(traceFile))
}
else {
generateTrace(new File(traceFile))
}
}
object CoreTest extends App {
var initFile = ""
var traceFile = ""
var genTrace = false
val manager = ArgParser(args, (o, v) => {
o match {
case Some("--init-file") | Some("-if") => initFile = v; true
case Some("--trace-file") | Some("-tf") => traceFile = v; true
case Some("--gen-trace") | Some("-gt") => genTrace = v != "0"; true
case _ => false
}
})
if (!Driver.execute(() => new CoreWrapper(initFile), manager) {
(c) => new CoreUnitTester(c, traceFile, genTrace)
}) sys.exit(1)
}