3 # This file is Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
4 # This file is Copyright (c) 2020 Dolu1990 <charles.papon.90@gmail.com>
11 from litex
.build
.generic_platform
import *
12 from litex
.build
.sim
import SimPlatform
13 from litex
.build
.sim
.config
import SimConfig
15 from litex
.soc
.integration
.soc
import SoCRegion
16 from litex
.soc
.integration
.soc_core
import *
17 from litex
.soc
.integration
.builder
import *
19 from litedram
.modules
import MT41K128M16
20 from litedram
.phy
.model
import SDRAMPHYModel
21 from litedram
.core
.controller
import ControllerSettings
23 from litex
.tools
.litex_sim
import get_sdram_phy_settings
25 from vexriscv_smp
import VexRiscvSMP
27 # IOs ----------------------------------------------------------------------------------------------
30 ("sys_clk", 0, Pins(1)),
31 ("sys_rst", 0, Pins(1)),
33 Subsignal("source_valid", Pins(1)),
34 Subsignal("source_ready", Pins(1)),
35 Subsignal("source_data", Pins(8)),
37 Subsignal("sink_valid", Pins(1)),
38 Subsignal("sink_ready", Pins(1)),
39 Subsignal("sink_data", Pins(8)),
43 # Platform -----------------------------------------------------------------------------------------
45 class Platform(SimPlatform
):
47 SimPlatform
.__init
__(self
, "SIM", _io
)
49 # SoCSMP -------------------------------------------------------------------------------------------
51 class SoCSMP(SoCCore
):
52 def __init__(self
, cpu_variant
, init_memories
=False, with_sdcard
=False):
54 sys_clk_freq
= int(100e6
)
58 sdram_init
= get_mem_data({
59 "images/fw_jump.bin": "0x00f00000",
60 "images/Image": "0x00000000",
61 "images/dtb" : "0x00ef0000",
62 "images/rootfs.cpio": "0x01000000",
65 # SoCCore ----------------------------------------------------------------------------------
66 SoCCore
.__init
__(self
, platform
, clk_freq
=sys_clk_freq
,
67 cpu_type
= "vexriscv", cpu_variant
=cpu_variant
, cpu_cls
=VexRiscvSMP
,
69 integrated_rom_size
= 0x8000,
70 integrated_main_ram_size
= 0x00000000)
71 self
.platform
.name
= "sim"
72 self
.add_constant("SIM")
74 # PLIC ------------------------------------------------------------------------------------
75 self
.bus
.add_slave("plic", self
.cpu
.plicbus
, region
=SoCRegion(origin
=0xf0C00000, size
=0x400000, cached
=False))
76 interrupt_map
= {**SoCCore
.interrupt_map
, **{
80 # CLINT ------------------------------------------------------------------------------------
81 self
.bus
.add_slave("clint", self
.cpu
.cbus
, region
=SoCRegion(origin
=0xf0010000, size
=0x10000, cached
=False))
83 # CRG --------------------------------------------------------------------------------------
84 self
.submodules
.crg
= CRG(platform
.request("sys_clk"))
86 # SDRAM ------------------------------------------------------------------------------------
87 phy_settings
= get_sdram_phy_settings(
91 self
.submodules
.sdrphy
= SDRAMPHYModel(
92 module
= MT41K128M16(100e6
, "1:4"),
93 settings
= phy_settings
,
96 self
.add_sdram("sdram",
98 module
= MT41K128M16(100e6
, "1:4"),
99 origin
= self
.mem_map
["main_ram"],
100 controller_settings
= ControllerSettings(
101 cmd_buffer_buffered
= False,
102 with_auto_precharge
= True
106 self
.add_constant("MEMTEST_BUS_SIZE", 0) # Skip test if memory is initialized to avoid
107 self
.add_constant("MEMTEST_ADDR_SIZE", 0) # corrumpting the content.
108 self
.add_constant("MEMTEST_DATA_SIZE", 0)
109 self
.add_constant("ROM_BOOT_ADDRESS", 0x40f00000) # Jump to fw_jump.bin
111 self
.add_constant("MEMTEST_BUS_SIZE", 4096)
112 self
.add_constant("MEMTEST_ADDR_SIZE", 4096)
113 self
.add_constant("MEMTEST_DATA_SIZE", 4096)
115 # SDCard -----------------------------------------------------------------------------------
117 self
.add_sdcard("sdcard", use_emulator
=True)
119 # Build --------------------------------------------------------------------------------------------
122 parser
= argparse
.ArgumentParser(description
="Linux on LiteX-VexRiscv Simulation")
123 parser
.add_argument("--cpu-variant", default
="2c", help="Select CPU netlist variant")
124 parser
.add_argument("--sdram-init", action
="store_true", help="Init SDRAM with Linux images")
125 parser
.add_argument("--with-sdcard", action
="store_true", help="Enable SDCard support")
126 parser
.add_argument("--trace", action
="store_true", help="Enable VCD tracing")
127 parser
.add_argument("--trace-start", default
=0, help="Cycle to start VCD tracing")
128 parser
.add_argument("--trace-end", default
=-1, help="Cycle to end VCD tracing")
129 parser
.add_argument("--opt-level", default
="O3", help="Compilation optimization level")
130 args
= parser
.parse_args()
132 sim_config
= SimConfig(default_clk
="sys_clk")
133 sim_config
.add_module("serial2console", "serial")
136 soc
= SoCSMP(args
.cpu_variant
, args
.sdram_init
and i
!=0, args
.with_sdcard
)
137 builder
= Builder(soc
,
138 compile_gateware
= i
!=0,
139 csr_json
= "build/sim/csr.json")
140 builder
.build(sim_config
=sim_config
,
142 opt_level
= args
.opt_level
,
144 trace_start
= int(args
.trace_start
),
145 trace_end
= int(args
.trace_end
),
149 os
.system("./json2dts.py build/sim/csr.json > build/sim/dts") # FIXME
150 os
.system("dtc -O dtb -o images/dtb build/sim/dts") # FIXME
151 os
.system("cp verilog/*.bin build/sim/gateware/")
153 if __name__
== "__main__":