6 from migen
import Signal
, FSM
8 from litex
.build
.generic_platform
import Pins
, Subsignal
9 from litex
.build
.sim
import SimPlatform
10 from litex
.build
.io
import CRG
11 from litex
.build
.sim
.config
import SimConfig
13 from litex
.soc
.integration
.soc
import SoCRegion
14 from litex
.soc
.integration
.soc_core
import SoCCore
15 from litex
.soc
.integration
.builder
import Builder
17 from litex
.tools
.litex_sim
import Platform
19 from libresoc
import LibreSoC
20 from microwatt
import Microwatt
22 # LibreSoCSim -----------------------------------------------------------------
24 class LibreSoCSim(SoCCore
):
25 def __init__(self
, cpu
="libresoc", debug
=False):
26 assert cpu
in ["libresoc", "microwatt"]
28 sys_clk_freq
= int(1e6
)
30 # SoCCore -------------------------------------------------------------
31 SoCCore
.__init
__(self
, platform
, clk_freq
=sys_clk_freq
,
32 cpu_type
= "microwatt",
33 cpu_cls
= LibreSoC
if cpu
== "libresoc" \
36 integrated_rom_size
= 0x10000,
37 integrated_main_ram_size
= 0x10000000) # 256MB
38 self
.platform
.name
= "sim"
40 # CRG -----------------------------------------------------------------
41 self
.submodules
.crg
= CRG(platform
.request("sys_clk"))
43 # Debug ---------------------------------------------------------------
46 self
.sync
+= uptime
.eq(uptime
+ 1)
47 self
.sync
+= If(self
.cpu
.ibus
.stb
& self
.cpu
.ibus
.ack
&
49 Display("[%06x] iadr: %8x, s %01x w %016x",
56 self
.sync
+= If(self
.cpu
.ibus
.stb
& self
.cpu
.ibus
.ack
&
58 Display("[%06x] iadr: %8x, s %01x r %016x",
65 self
.sync
+= If(self
.cpu
.dbus
.stb
& self
.cpu
.dbus
.ack
,
66 Display("[%06x] dadr: %8x, we %d s %01x w %016x r: %016x",
75 # Stop after 20000 cycles
76 self
.sync
+= If(uptime
== 100000, Finish())
78 # Build -----------------------------------------------------------------------
81 parser
= argparse
.ArgumentParser(description
="LiteX LibreSoC CPU Sim")
82 parser
.add_argument("--cpu", default
="libresoc",
83 help="CPU to use: libresoc (default) or microwatt")
84 parser
.add_argument("--debug", action
="store_true",
85 help="Enable debug traces")
86 parser
.add_argument("--trace", action
="store_true",
87 help="Enable tracing")
88 parser
.add_argument("--trace-start", default
=0,
89 help="Cycle to start FST tracing")
90 parser
.add_argument("--trace-end", default
=-1,
91 help="Cycle to end FST tracing")
92 args
= parser
.parse_args()
94 sim_config
= SimConfig(default_clk
="sys_clk")
95 sim_config
.add_module("serial2console", "serial")
98 soc
= LibreSoCSim(cpu
=args
.cpu
, debug
=args
.debug
)
99 builder
= Builder(soc
,compile_gateware
= i
!=0)
100 builder
.build(sim_config
=sim_config
,
103 trace_start
= int(args
.trace_start
),
104 trace_end
= int(args
.trace_end
),
108 if __name__
== "__main__":