2020-01-28 06:07:18 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# This file is Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# License: BSD
|
|
|
|
|
2020-02-04 06:34:15 -05:00
|
|
|
import csv
|
2020-01-28 06:07:18 -05:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
from migen.genlib.misc import WaitTimer
|
|
|
|
|
|
|
|
from litex.build.sim.config import SimConfig
|
|
|
|
|
|
|
|
from litex.soc.interconnect.csr import *
|
|
|
|
from litex.soc.integration.soc_sdram import *
|
|
|
|
from litex.soc.integration.builder import *
|
|
|
|
|
|
|
|
from litex.tools.litex_sim import SimSoC
|
|
|
|
|
2020-02-04 06:34:15 -05:00
|
|
|
from litedram.frontend.bist import _LiteDRAMBISTGenerator, _LiteDRAMBISTChecker, \
|
|
|
|
_LiteDRAMPatternGenerator, _LiteDRAMPatternChecker
|
2020-01-28 06:07:18 -05:00
|
|
|
|
|
|
|
# LiteDRAM Benchmark SoC ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class LiteDRAMBenchmarkSoC(SimSoC):
|
|
|
|
def __init__(self,
|
|
|
|
sdram_module = "MT48LC16M16",
|
|
|
|
sdram_data_width = 32,
|
2020-01-28 09:03:36 -05:00
|
|
|
bist_base = 0x00000000,
|
|
|
|
bist_length = 1024,
|
|
|
|
bist_random = False,
|
2020-02-11 06:06:45 -05:00
|
|
|
bist_alternating = False,
|
2020-02-04 06:34:15 -05:00
|
|
|
pattern_init = None,
|
2020-01-28 06:07:18 -05:00
|
|
|
**kwargs):
|
|
|
|
|
|
|
|
# SimSoC -----------------------------------------------------------------------------------
|
|
|
|
SimSoC.__init__(self,
|
|
|
|
with_sdram = True,
|
|
|
|
sdram_module = sdram_module,
|
|
|
|
sdram_data_width = sdram_data_width,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2020-02-03 03:21:18 -05:00
|
|
|
# make sure that we perform at least one access
|
|
|
|
bist_length = max(bist_length, self.sdram.controller.interface.data_width // 8)
|
|
|
|
|
2020-02-04 06:34:15 -05:00
|
|
|
# BIST Generator / Checker -----------------------------------------------------------------
|
|
|
|
if pattern_init is None:
|
|
|
|
bist_generator = _LiteDRAMBISTGenerator(self.sdram.crossbar.get_port())
|
|
|
|
bist_checker = _LiteDRAMBISTChecker(self.sdram.crossbar.get_port())
|
|
|
|
|
|
|
|
generator_config = [
|
|
|
|
bist_generator.base.eq(bist_base),
|
|
|
|
bist_generator.length.eq(bist_length),
|
|
|
|
bist_generator.random.eq(bist_random),
|
|
|
|
]
|
|
|
|
checker_config = [
|
|
|
|
bist_checker.base.eq(bist_base),
|
|
|
|
bist_checker.length.eq(bist_length),
|
|
|
|
bist_checker.random.eq(bist_random),
|
|
|
|
]
|
2020-02-11 06:06:45 -05:00
|
|
|
|
|
|
|
assert not (bist_random and not bist_alternating), \
|
|
|
|
'Write to random address may overwrite previously written data before reading!'
|
2020-02-04 06:34:15 -05:00
|
|
|
else:
|
2020-02-11 06:06:45 -05:00
|
|
|
if not bist_alternating:
|
|
|
|
address_set = set()
|
|
|
|
for addr, _ in pattern_init:
|
|
|
|
assert addr not in address_set, \
|
|
|
|
'Duplicate address 0x%08x in pattern_init, write will overwrite previous value!' % addr
|
|
|
|
address_set.add(addr)
|
2020-02-04 06:34:15 -05:00
|
|
|
|
|
|
|
bist_generator = _LiteDRAMPatternGenerator(self.sdram.crossbar.get_port(), init=pattern_init)
|
|
|
|
bist_checker = _LiteDRAMPatternChecker(self.sdram.crossbar.get_port(), init=pattern_init)
|
|
|
|
generator_config = checker_config = []
|
2020-01-28 06:07:18 -05:00
|
|
|
|
2020-02-04 06:34:15 -05:00
|
|
|
self.submodules.bist_generator = bist_generator
|
2020-01-28 06:07:18 -05:00
|
|
|
self.submodules.bist_checker = bist_checker
|
|
|
|
|
|
|
|
# Sequencer --------------------------------------------------------------------------------
|
|
|
|
class LiteDRAMCoreControl(Module, AutoCSR):
|
|
|
|
def __init__(self):
|
|
|
|
self.init_done = CSRStorage()
|
|
|
|
self.init_error = CSRStorage()
|
|
|
|
self.submodules.ddrctrl = ddrctrl = LiteDRAMCoreControl()
|
|
|
|
self.add_csr("ddrctrl")
|
|
|
|
|
|
|
|
display = Signal()
|
|
|
|
finish = Signal()
|
|
|
|
self.submodules.fsm = fsm = FSM(reset_state="WAIT-INIT")
|
|
|
|
fsm.act("WAIT-INIT",
|
|
|
|
If(self.ddrctrl.init_done.storage, # Written by CPU when initialization is done
|
|
|
|
NextState("BIST-GENERATOR")
|
|
|
|
)
|
|
|
|
)
|
2020-02-11 06:06:45 -05:00
|
|
|
if bist_alternating:
|
|
|
|
fsm.act("BIST-GENERATOR",
|
|
|
|
bist_generator.start.eq(1),
|
|
|
|
bist_checker.start.eq(1),
|
|
|
|
# force generator to wait for checker and vice versa
|
|
|
|
bist_generator.run.eq(bist_checker.ready),
|
|
|
|
bist_checker.run.eq(bist_generator.ready),
|
|
|
|
*generator_config,
|
|
|
|
*checker_config,
|
|
|
|
If(bist_checker.done,
|
|
|
|
NextState("DISPLAY")
|
|
|
|
)
|
2020-01-28 06:07:18 -05:00
|
|
|
)
|
2020-02-11 06:06:45 -05:00
|
|
|
else:
|
|
|
|
fsm.act("BIST-GENERATOR",
|
|
|
|
bist_generator.start.eq(1),
|
|
|
|
bist_generator.run.eq(1),
|
|
|
|
*generator_config,
|
|
|
|
If(bist_generator.done,
|
|
|
|
NextState("BIST-CHECKER")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fsm.act("BIST-CHECKER",
|
|
|
|
bist_checker.start.eq(1),
|
|
|
|
bist_checker.run.eq(1),
|
|
|
|
*checker_config,
|
|
|
|
If(bist_checker.done,
|
|
|
|
NextState("DISPLAY")
|
|
|
|
)
|
2020-01-28 06:07:18 -05:00
|
|
|
)
|
|
|
|
fsm.act("DISPLAY",
|
|
|
|
display.eq(1),
|
|
|
|
NextState("FINISH")
|
|
|
|
)
|
|
|
|
fsm.act("FINISH",
|
|
|
|
finish.eq(1)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Simulation Results -----------------------------------------------------------------------
|
|
|
|
self.sync += [
|
|
|
|
If(display,
|
|
|
|
Display("BIST-GENERATOR ticks: %08d", bist_generator.ticks),
|
|
|
|
Display("BIST-CHECKER errors: %08d", bist_checker.errors),
|
|
|
|
Display("BIST-CHECKER ticks: %08d", bist_checker.ticks),
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
# Simulation End ---------------------------------------------------------------------------
|
|
|
|
end_timer = WaitTimer(2**16)
|
|
|
|
self.submodules += end_timer
|
|
|
|
self.comb += end_timer.wait.eq(finish)
|
|
|
|
self.sync += If(end_timer.done, Finish())
|
|
|
|
|
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
2020-02-05 06:38:05 -05:00
|
|
|
def load_access_pattern(filename):
|
|
|
|
with open(filename, newline='') as f:
|
|
|
|
reader = csv.reader(f)
|
|
|
|
pattern_init = [(int(addr, 0), int(data, 0)) for addr, data in reader]
|
|
|
|
return pattern_init
|
|
|
|
|
|
|
|
|
2020-01-28 06:07:18 -05:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description="LiteDRAM Benchmark SoC Simulation")
|
|
|
|
builder_args(parser)
|
|
|
|
soc_sdram_args(parser)
|
2020-02-04 06:34:15 -05:00
|
|
|
parser.add_argument("--threads", default=1, help="Set number of threads (default=1)")
|
|
|
|
parser.add_argument("--sdram-module", default="MT48LC16M16", help="Select SDRAM chip")
|
|
|
|
parser.add_argument("--sdram-data-width", default=32, help="Set SDRAM chip data width")
|
|
|
|
parser.add_argument("--trace", action="store_true", help="Enable VCD tracing")
|
|
|
|
parser.add_argument("--trace-start", default=0, help="Cycle to start VCD tracing")
|
|
|
|
parser.add_argument("--trace-end", default=-1, help="Cycle to end VCD tracing")
|
|
|
|
parser.add_argument("--opt-level", default="O0", help="Compilation optimization level")
|
|
|
|
parser.add_argument("--bist-base", default="0x00000000", help="Base address of the test (default=0)")
|
|
|
|
parser.add_argument("--bist-length", default="1024", help="Length of the test (default=1024)")
|
|
|
|
parser.add_argument("--bist-random", action="store_true", help="Use random data during the test")
|
2020-02-11 06:06:45 -05:00
|
|
|
parser.add_argument("--bist-alternating", action="store_true", help="Perform alternating writes/reads (WRWRWR... instead of WWW...RRR...)")
|
2020-02-04 06:34:15 -05:00
|
|
|
parser.add_argument("--access-pattern", help="Load access pattern (address, data) from CSV (ignores --bist-*)")
|
2020-01-28 06:07:18 -05:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
soc_kwargs = soc_sdram_argdict(args)
|
|
|
|
builder_kwargs = builder_argdict(args)
|
|
|
|
|
|
|
|
sim_config = SimConfig(default_clk="sys_clk")
|
|
|
|
sim_config.add_module("serial2console", "serial")
|
|
|
|
|
|
|
|
# Configuration --------------------------------------------------------------------------------
|
|
|
|
soc_kwargs["sdram_module"] = args.sdram_module
|
|
|
|
soc_kwargs["sdram_data_width"] = int(args.sdram_data_width)
|
2020-01-28 09:03:36 -05:00
|
|
|
soc_kwargs["bist_base"] = int(args.bist_base, 0)
|
|
|
|
soc_kwargs["bist_length"] = int(args.bist_length, 0)
|
|
|
|
soc_kwargs["bist_random"] = args.bist_random
|
2020-02-11 06:06:45 -05:00
|
|
|
soc_kwargs["bist_alternating"] = args.bist_alternating
|
2020-01-28 06:07:18 -05:00
|
|
|
|
2020-02-04 06:34:15 -05:00
|
|
|
if args.access_pattern:
|
2020-02-05 06:38:05 -05:00
|
|
|
soc_kwargs["pattern_init"] = load_access_pattern(args.access_pattern)
|
2020-02-04 06:34:15 -05:00
|
|
|
|
2020-01-28 06:07:18 -05:00
|
|
|
# SoC ------------------------------------------------------------------------------------------
|
|
|
|
soc = LiteDRAMBenchmarkSoC(**soc_kwargs)
|
|
|
|
|
|
|
|
# Build/Run ------------------------------------------------------------------------------------
|
|
|
|
builder_kwargs["csr_csv"] = "csr.csv"
|
|
|
|
builder = Builder(soc, **builder_kwargs)
|
|
|
|
vns = builder.build(
|
|
|
|
threads = args.threads,
|
|
|
|
sim_config = sim_config,
|
|
|
|
opt_level = args.opt_level,
|
|
|
|
trace = args.trace,
|
|
|
|
trace_start = int(args.trace_start),
|
|
|
|
trace_end = int(args.trace_end)
|
|
|
|
)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|