7 from litex
.build
.generic_platform
import *
8 from litex
.build
.sim
import SimPlatform
9 from litex
.build
.sim
.config
import SimConfig
11 from litex
.soc
.integration
.soc_core
import *
12 from litex
.soc
.integration
.builder
import *
14 from litex
.tools
.litex_sim
import Platform
16 from libresoc
import LibreSoC
17 from microwatt
import Microwatt
19 # LibreSoCSim --------------------------------------------------------------------------------------
21 class LibreSoCSim(SoCCore
):
22 def __init__(self
, cpu
="libresoc", debug
=False):
23 assert cpu
in ["libresoc", "microwatt"]
25 sys_clk_freq
= int(1e6
)
27 # SoCCore ----------------------------------------------------------------------------------
28 SoCCore
.__init
__(self
, platform
, clk_freq
=sys_clk_freq
,
29 cpu_type
= "microwatt",
30 cpu_cls
= LibreSoC
if cpu
== "libresoc" else Microwatt
,
32 integrated_rom_size
= 0x10000,
33 integrated_main_ram_size
= 0x10000000) # 256MB
34 self
.platform
.name
= "sim"
36 # CRG --------------------------------------------------------------------------------------
37 self
.submodules
.crg
= CRG(platform
.request("sys_clk"))
39 # Debug ------------------------------------------------------------------------------------
42 self
.sync
+= uptime
.eq(uptime
+ 1)
43 self
.sync
+= If(self
.cpu
.ibus
.stb
& self
.cpu
.ibus
.ack
&
45 Display("[%06x] iadr: %8x, s %01x w %016x",
52 self
.sync
+= If(self
.cpu
.ibus
.stb
& self
.cpu
.ibus
.ack
&
54 Display("[%06x] iadr: %8x, s %01x r %016x",
61 self
.sync
+= If(self
.cpu
.dbus
.stb
& self
.cpu
.dbus
.ack
,
62 Display("[%06x] dadr: %8x, we %d s %01x w %016x r: %016x",
71 # Stop after 20000 cycles
72 self
.sync
+= If(uptime
== 100000, Finish())
74 # Build --------------------------------------------------------------------------------------------
77 parser
= argparse
.ArgumentParser(description
="LiteX LibreSoC CPU Simulation")
78 parser
.add_argument("--cpu", default
="libresoc", help="CPU to use: libresoc (default) or microwatt")
79 parser
.add_argument("--debug", action
="store_true", help="Enable debug traces")
80 parser
.add_argument("--trace", action
="store_true", help="Enable tracing")
81 parser
.add_argument("--trace-start", default
=0, help="Cycle to start FST tracing")
82 parser
.add_argument("--trace-end", default
=-1, help="Cycle to end FST tracing")
83 args
= parser
.parse_args()
85 sim_config
= SimConfig(default_clk
="sys_clk")
86 sim_config
.add_module("serial2console", "serial")
89 soc
= LibreSoCSim(cpu
=args
.cpu
, debug
=args
.debug
)
90 builder
= Builder(soc
,compile_gateware
= i
!=0)
91 builder
.build(sim_config
=sim_config
,
94 trace_start
= int(args
.trace_start
),
95 trace_end
= int(args
.trace_end
),
99 if __name__
== "__main__":