Ejemplo n.º 1
0
    def __init__(self,
                 uart="crossover",
                 sys_clk_freq=int(125e6),
                 with_bist=False,
                 with_analyzer=False):
        platform = kc705.Platform()

        # SoCCore ----------------------------------------------------------------------------------
        SoCCore.__init__(self,
                         platform,
                         clk_freq=sys_clk_freq,
                         ident="LiteDRAM bench on KC705",
                         ident_version=True,
                         integrated_rom_size=0x10000,
                         integrated_rom_mode="rw",
                         uart_name=uart)

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform, sys_clk_freq)

        # DDR3 SDRAM -------------------------------------------------------------------------------
        self.submodules.ddrphy = s7ddrphy.K7DDRPHY(pads=PHYPadsReducer(
            platform.request("ddram"), [0, 1, 2, 3, 4, 5, 6, 7]),
                                                   memtype="DDR3",
                                                   nphases=4,
                                                   sys_clk_freq=sys_clk_freq)
        self.add_sdram("sdram",
                       phy=self.ddrphy,
                       module=MT8JTF12864(sys_clk_freq, "1:4"),
                       origin=self.mem_map["main_ram"],
                       with_bist=with_bist)

        # UARTBone ---------------------------------------------------------------------------------
        if uart != "serial":
            self.add_uartbone(name="serial",
                              clk_freq=100e6,
                              baudrate=115200,
                              cd="uart")

        # Etherbone --------------------------------------------------------------------------------
        self.submodules.ethphy = LiteEthPHY(
            clock_pads=self.platform.request("eth_clocks"),
            pads=self.platform.request("eth"),
            clk_freq=self.clk_freq)
        self.add_etherbone(phy=self.ethphy)

        # Analyzer ---------------------------------------------------------------------------------
        if with_analyzer:
            from litescope import LiteScopeAnalyzer
            analyzer_signals = [self.ddrphy.dfi]
            self.submodules.analyzer = LiteScopeAnalyzer(
                analyzer_signals,
                depth=256,
                clock_domain="sys",
                csr_csv="analyzer.csv")

        # Leds -------------------------------------------------------------------------------------
        from litex.soc.cores.led import LedChaser
        self.submodules.leds = LedChaser(pads=platform.request_all("user_led"),
                                         sys_clk_freq=sys_clk_freq)
Ejemplo n.º 2
0
    def __init__(self,
                 uart="crossover",
                 sys_clk_freq=int(125e6),
                 channel=0,
                 with_bist=False,
                 with_analyzer=False):
        platform = xcu1525.Platform()

        # SoCCore ----------------------------------------------------------------------------------
        SoCCore.__init__(self,
                         platform,
                         clk_freq=sys_clk_freq,
                         ident="LiteDRAM bench on XCU1525",
                         ident_version=True,
                         integrated_rom_size=0x10000,
                         integrated_rom_mode="rw",
                         uart_name=uart)

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform, sys_clk_freq, channel)

        # DDR4 SDRAM -------------------------------------------------------------------------------
        self.submodules.ddrphy = usddrphy.USPDDRPHY(pads=PHYPadsReducer(
            platform.request("ddram", channel), [0, 1, 2, 3, 4, 5, 6, 7]),
                                                    memtype="DDR4",
                                                    sys_clk_freq=sys_clk_freq,
                                                    iodelay_clk_freq=500e6)
        self.add_sdram("sdram",
                       phy=self.ddrphy,
                       module=MT40A512M8(sys_clk_freq, "1:4"),
                       origin=self.mem_map["main_ram"],
                       size=0x40000000,
                       with_bist=with_bist)
        # Workaround for Vivado 2018.2 DRC, can be ignored and probably fixed on newer Vivado versions.
        platform.add_platform_command(
            "set_property SEVERITY {{Warning}} [get_drc_checks PDCN-2736]")

        # UARTBone ---------------------------------------------------------------------------------
        if uart != "serial":
            self.add_uartbone(name="serial",
                              clk_freq=100e6,
                              baudrate=115200,
                              cd="uart")

        # Analyzer ---------------------------------------------------------------------------------
        if with_analyzer:
            from litescope import LiteScopeAnalyzer
            analyzer_signals = [self.ddrphy.dfi]
            self.submodules.analyzer = LiteScopeAnalyzer(
                analyzer_signals,
                depth=256,
                clock_domain="sys",
                csr_csv="analyzer.csv")

        # Leds -------------------------------------------------------------------------------------
        from litex.soc.cores.led import LedChaser
        self.submodules.leds = LedChaser(pads=platform.request_all("user_led"),
                                         sys_clk_freq=sys_clk_freq)
Ejemplo n.º 3
0
    def __init__(self,
                 sys_clk_freq=int(200e6),
                 with_pcie=False,
                 pcie_lanes=4,
                 **kwargs):
        platform = quad_hdmi_recorder.Platform()

        # SoCCore ----------------------------------------------------------------------------------
        kwargs["uart_name"] = "crossover"
        SoCCore.__init__(
            self,
            platform,
            sys_clk_freq,
            ident="LiteX SoC on Blackmagic Decklink Quad HDMI Recorder",
            **kwargs)

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform, sys_clk_freq)

        # JTAGBone  --------------------------------------------------------------------------------
        self.add_jtagbone()

        # DDR3 SDRAM -------------------------------------------------------------------------------
        if not self.integrated_main_ram_size:
            self.submodules.ddrphy = usddrphy.USDDRPHY(
                pads=PHYPadsReducer(platform.request("ddram"), [0, 1, 2, 3]),
                memtype="DDR3",
                sys_clk_freq=sys_clk_freq,
                iodelay_clk_freq=200e6)
            self.add_sdram("sdram",
                           phy=self.ddrphy,
                           module=MT41J256M16(sys_clk_freq, "1:4"),
                           l2_cache_size=kwargs.get("l2_size", 8192))

        # PCIe -------------------------------------------------------------------------------------
        # FIXME: Does not seem to be working when also enabling DRAM. Has been tested succesfully by
        # disabling DRAM with --integrated-main-ram-size=0x100.
        if with_pcie:
            data_width = {
                4: 128,
                8: 256,
            }[pcie_lanes]
            self.submodules.pcie_phy = USPCIEPHY(
                platform,
                platform.request(f"pcie_x{pcie_lanes}"),
                speed="gen3",
                data_width=data_width,
                bar0_size=0x20000)
            self.add_pcie(phy=self.pcie_phy, ndmas=1)
            # False Paths (FIXME: Improve integration).
            platform.toolchain.pre_placement_commands.append(
                "set_false_path -from [get_clocks sys_clk] -to [get_clocks pcie_clk_1]"
            )
            platform.toolchain.pre_placement_commands.append(
                "set_false_path -from [get_clocks pcie_clk_1] -to [get_clocks sys_clk]"
            )
Ejemplo n.º 4
0
    def __init__(self,
                 sys_clk_freq=int(125e6),
                 cpu_count=1,
                 with_ethernet=False,
                 **kwargs):
        VexRiscvSMP.litedram_width = 128
        VexRiscvSMP.ibus_width = 64
        VexRiscvSMP.dbus_width = 64
        VexRiscvSMP.coherent_dma = True

        platform = genesys2.Platform()

        # SoCCore ----------------------------------------------------------------------------------
        kwargs["integrated_rom_size"] = 0x10000
        kwargs["csr_data_width"] = 8
        SoCCore.__init__(self,
                         platform,
                         clk_freq=sys_clk_freq,
                         cpu_cls=VexRiscvSMP,
                         cpu_variant="default",
                         **kwargs)
        self.add_constant("config_cpu_count", cpu_count)  # for dts generation

        # PLIC ------------------------------------------------------------------------------------
        self.bus.add_slave("plic",
                           self.cpu.plicbus,
                           region=SoCRegion(origin=0xf0C00000,
                                            size=0x400000,
                                            cached=False))

        # CLINT ------------------------------------------------------------------------------------
        self.bus.add_slave("clint",
                           self.cpu.cbus,
                           region=SoCRegion(origin=0xf0010000,
                                            size=0x10000,
                                            cached=False))

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform, sys_clk_freq)

        # DDR3 SDRAM -------------------------------------------------------------------------------
        if not self.integrated_main_ram_size:
            self.submodules.ddrphy = s7ddrphy.K7DDRPHY(
                pads=PHYPadsReducer(platform.request("ddram"), [1, 2]),
                memtype="DDR3",
                nphases=4,
                sys_clk_freq=sys_clk_freq,
                cmd_latency=1)
            self.add_csr("ddrphy")
            self.add_sdram("sdram",
                           phy=self.ddrphy,
                           module=MT41J256M16(sys_clk_freq, "1:4"),
                           origin=self.mem_map["main_ram"],
                           size=kwargs.get("max_sdram_size", 0x40000000),
                           l2_cache_size=kwargs.get("l2_size", 8192),
                           l2_cache_min_data_width=kwargs.get(
                               "min_l2_data_width", 128),
                           l2_cache_reverse=True,
                           controller_settings=ControllerSettings(
                               cmd_buffer_buffered=False,
                               with_auto_precharge=False))

        # Ethernet ---------------------------------------------------------------------------------
        if with_ethernet:
            self.submodules.ethphy = LiteEthPHYRGMII(
                clock_pads=self.platform.request("eth_clocks"),
                pads=self.platform.request("eth"))
            self.add_csr("ethphy")
            self.add_ethernet(phy=self.ethphy)

        # JTAG ---------------------------------------------------------------------------------
        self.submodules.jtag = S7JTAG()
        self.comb += self.cpu.jtag_clk.eq(self.jtag.tck)
        self.comb += self.cpu.jtag_enable.eq(1)
        self.comb += self.cpu.jtag_capture.eq(self.jtag.capture)
        self.comb += self.cpu.jtag_shift.eq(self.jtag.shift)
        self.comb += self.cpu.jtag_update.eq(self.jtag.update)
        self.comb += self.cpu.jtag_reset.eq(self.jtag.reset)
        self.comb += self.cpu.jtag_tdi.eq(self.jtag.tdi)
        self.comb += self.jtag.tdo.eq(self.cpu.jtag_tdo)
Ejemplo n.º 5
0
    def __init__(self,
                 sys_clk_freq=int(100e6),
                 with_ethernet=False,
                 eth_phy="rgmii",
                 with_pcie=False,
                 **kwargs):
        platform = ac701.Platform()

        # SoCCore ----------------------------------------------------------------------------------
        SoCCore.__init__(self,
                         platform,
                         sys_clk_freq,
                         ident="LiteX SoC on AC701",
                         ident_version=True,
                         **kwargs)

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform, sys_clk_freq)

        # DDR3 SDRAM -------------------------------------------------------------------------------
        if not self.integrated_main_ram_size:
            self.submodules.ddrphy = s7ddrphy.A7DDRPHY(
                pads=PHYPadsReducer(platform.request("ddram"), [0, 1, 2, 3]),
                memtype="DDR3",
                nphases=4,
                sys_clk_freq=sys_clk_freq)
            self.add_sdram("sdram",
                           phy=self.ddrphy,
                           module=MT8JTF12864(sys_clk_freq, "1:4"),
                           origin=self.mem_map["main_ram"],
                           size=kwargs.get("max_sdram_size", 0x40000000),
                           l2_cache_size=kwargs.get("l2_size", 8192),
                           l2_cache_min_data_width=kwargs.get(
                               "min_l2_data_width", 128),
                           l2_cache_reverse=True)

        # Ethernet ---------------------------------------------------------------------------------
        if with_ethernet:
            # RGMII Ethernet PHY -------------------------------------------------------------------
            if eth_phy == "rgmii":
                # phy
                self.submodules.ethphy = LiteEthPHYRGMII(
                    clock_pads=self.platform.request("eth_clocks"),
                    pads=self.platform.request("eth"))

            # 1000BaseX Ethernet PHY ---------------------------------------------------------------
            if eth_phy == "1000basex":
                # phy
                self.comb += self.platform.request("sfp_mgt_clk_sel0", 0).eq(0)
                self.comb += self.platform.request("sfp_mgt_clk_sel1", 0).eq(0)
                self.comb += self.platform.request("sfp_tx_disable_n", 0).eq(0)
                qpll_settings = QPLLSettings(refclksel=0b001,
                                             fbdiv=4,
                                             fbdiv_45=5,
                                             refclk_div=1)
                refclk125 = self.platform.request("gtp_refclk")
                refclk125_se = Signal()
                self.specials += \
                    Instance("IBUFDS_GTE2",
                        i_CEB = 0,
                        i_I   = refclk125.p,
                        i_IB  = refclk125.n,
                        o_O   = refclk125_se)
                qpll = QPLL(refclk125_se, qpll_settings)
                self.submodules += qpll
                self.submodules.ethphy = A7_1000BASEX(
                    qpll_channel=qpll.channels[0],
                    data_pads=self.platform.request("sfp", 0),
                    sys_clk_freq=self.clk_freq)

            self.add_ethernet(phy=self.ethphy)

        # PCIe -------------------------------------------------------------------------------------
        if with_pcie:
            self.submodules.pcie_phy = S7PCIEPHY(platform,
                                                 platform.request("pcie_x4"),
                                                 data_width=128,
                                                 bar0_size=0x20000)
            self.add_pcie(phy=self.pcie_phy, ndmas=1)

        # Leds -------------------------------------------------------------------------------------
        self.submodules.leds = LedChaser(pads=platform.request_all("user_led"),
                                         sys_clk_freq=sys_clk_freq)
Ejemplo n.º 6
0
    def __init__(self,
                 sys_clk_freq=int(100e6),
                 with_etherbone=True,
                 eth_ip="192.168.1.50",
                 with_video_terminal=False,
                 with_video_framebuffer=False,
                 **kwargs):
        platform = sds1104xe.Platform()

        # SoCCore ----------------------------------------------------------------------------------
        if kwargs.get("uart_name", "serial") == "serial":
            kwargs["uart_name"] = "crossover"  # Defaults to Crossover UART.
        SoCCore.__init__(self,
                         platform,
                         sys_clk_freq,
                         ident="LiteX SoC on Siglent SDS1104X-E",
                         **kwargs)

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform,
                                   sys_clk_freq,
                                   with_ethernet=with_etherbone)

        # DDR3 SDRAM -------------------------------------------------------------------------------
        if not self.integrated_main_ram_size:
            self.submodules.ddrphy = s7ddrphy.A7DDRPHY(
                pads=PHYPadsReducer(platform.request("ddram"), [0, 1, 2, 3]),
                memtype="DDR3",
                nphases=4,
                sys_clk_freq=sys_clk_freq)
            self.add_sdram("sdram",
                           phy=self.ddrphy,
                           module=MT41K64M16(sys_clk_freq, "1:4"),
                           l2_cache_size=kwargs.get("l2_size", 8192))

        # Etherbone --------------------------------------------------------------------------------
        if with_etherbone:
            # FIXME: Simplify LiteEth Hybrid MAC integration.
            from liteeth.common import convert_ip
            from liteeth.mac import LiteEthMAC
            from liteeth.core.arp import LiteEthARP
            from liteeth.core.ip import LiteEthIP
            from liteeth.core.udp import LiteEthUDP
            from liteeth.core.icmp import LiteEthICMP
            from liteeth.core import LiteEthUDPIPCore
            from liteeth.frontend.etherbone import LiteEthEtherbone

            # Ethernet PHY
            self.submodules.ethphy = LiteEthPHYMII(
                clock_pads=self.platform.request("eth_clocks"),
                pads=self.platform.request("eth"))
            etherbone_ip_address = convert_ip("192.168.1.51")
            etherbone_mac_address = 0x10e2d5000001

            # Ethernet MAC
            self.submodules.ethmac = LiteEthMAC(phy=self.ethphy,
                                                dw=8,
                                                interface="hybrid",
                                                endianness=self.cpu.endianness,
                                                hw_mac=etherbone_mac_address)

            # Software Interface.
            self.add_memory_region("ethmac",
                                   getattr(self.mem_map, "ethmac", None),
                                   0x2000,
                                   type="io")
            self.add_wb_slave(self.mem_regions["ethmac"].origin,
                              self.ethmac.bus, 0x2000)
            if self.irq.enabled:
                self.irq.add("ethmac", use_loc_if_exists=True)

            # Hardware Interface.
            self.submodules.arp = LiteEthARP(self.ethmac,
                                             etherbone_mac_address,
                                             etherbone_ip_address,
                                             sys_clk_freq,
                                             dw=8)
            self.submodules.ip = LiteEthIP(self.ethmac,
                                           etherbone_mac_address,
                                           etherbone_ip_address,
                                           self.arp.table,
                                           dw=8)
            self.submodules.icmp = LiteEthICMP(self.ip,
                                               etherbone_ip_address,
                                               dw=8)
            self.submodules.udp = LiteEthUDP(self.ip,
                                             etherbone_ip_address,
                                             dw=8)
            self.add_constant(
                "ETH_PHY_NO_RESET"
            )  # Disable reset from BIOS to avoid disabling Hardware Interface.

            # Etherbone
            self.submodules.etherbone = LiteEthEtherbone(self.udp,
                                                         1234,
                                                         mode="master")
            self.add_wb_master(self.etherbone.wishbone.bus)

            # Timing constraints
            eth_rx_clk = self.ethphy.crg.cd_eth_rx.clk
            eth_tx_clk = self.ethphy.crg.cd_eth_tx.clk
            self.platform.add_period_constraint(eth_rx_clk,
                                                1e9 / self.ethphy.rx_clk_freq)
            self.platform.add_period_constraint(eth_tx_clk,
                                                1e9 / self.ethphy.tx_clk_freq)
            self.platform.add_false_path_constraints(self.crg.cd_sys.clk,
                                                     eth_rx_clk, eth_tx_clk)

        # Video ------------------------------------------------------------------------------------
        video_timings = ("800x480@60Hz", {
            "pix_clk": 33.3e6,
            "h_active": 800,
            "h_blanking": 256,
            "h_sync_offset": 210,
            "h_sync_width": 1,
            "v_active": 480,
            "v_blanking": 45,
            "v_sync_offset": 22,
            "v_sync_width": 1,
        })
        if with_video_terminal or with_video_framebuffer:
            self.submodules.videophy = VideoVGAPHY(platform.request("lcd"),
                                                   clock_domain="dvi")
            if with_video_terminal:
                self.add_video_terminal(phy=self.videophy,
                                        timings=video_timings,
                                        clock_domain="dvi")
            if with_video_framebuffer:
                self.add_video_framebuffer(phy=self.videophy,
                                           timings=video_timings,
                                           clock_domain="dvi")
Ejemplo n.º 7
0
    def __init__(self,
                 uart="crossover",
                 sys_clk_freq=int(125e6),
                 with_bist=False,
                 with_analyzer=False):
        platform = kcu105.Platform()

        # SoCCore ----------------------------------------------------------------------------------
        SoCCore.__init__(self,
                         platform,
                         clk_freq=sys_clk_freq,
                         ident="LiteDRAM bench on KCU105",
                         ident_version=True,
                         integrated_rom_size=0x10000,
                         integrated_rom_mode="rw",
                         uart_name=uart)

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform, sys_clk_freq)

        # DDR4 SDRAM -------------------------------------------------------------------------------
        self.submodules.ddrphy = usddrphy.USDDRPHY(pads=PHYPadsReducer(
            platform.request("ddram"), [0, 1, 2, 3, 4, 5, 6, 7]),
                                                   memtype="DDR4",
                                                   sys_clk_freq=sys_clk_freq,
                                                   iodelay_clk_freq=200e6)
        self.add_sdram("sdram",
                       phy=self.ddrphy,
                       module=EDY4016A(sys_clk_freq, "1:4"),
                       origin=self.mem_map["main_ram"],
                       size=0x40000000,
                       with_bist=with_bist)

        # UARTBone ---------------------------------------------------------------------------------
        if uart != "serial":
            self.add_uartbone(name="serial",
                              clk_freq=100e6,
                              baudrate=115200,
                              cd="uart")

        # Etherbone --------------------------------------------------------------------------------
        self.submodules.ethphy = KU_1000BASEX(self.crg.cd_eth.clk,
                                              data_pads=self.platform.request(
                                                  "sfp", 0),
                                              sys_clk_freq=self.clk_freq)
        self.comb += self.platform.request("sfp_tx_disable_n", 0).eq(1)
        self.platform.add_platform_command(
            "set_property SEVERITY {{Warning}} [get_drc_checks REQP-1753]")
        self.add_etherbone(phy=self.ethphy)

        # Analyzer ---------------------------------------------------------------------------------
        if with_analyzer:
            from litescope import LiteScopeAnalyzer
            analyzer_signals = [self.ddrphy.dfi]
            self.submodules.analyzer = LiteScopeAnalyzer(
                analyzer_signals,
                depth=256,
                clock_domain="sys",
                csr_csv="analyzer.csv")

        # Leds -------------------------------------------------------------------------------------
        from litex.soc.cores.led import LedChaser
        self.submodules.leds = LedChaser(pads=platform.request_all("user_led"),
                                         sys_clk_freq=sys_clk_freq)
Ejemplo n.º 8
0
    def __init__(self, platform, core_config, **kwargs):
        platform.add_extension(get_common_ios())

        # Parameters -------------------------------------------------------------------------------
        sys_clk_freq = core_config["sys_clk_freq"]
        cpu_type     = core_config["cpu"]
        csr_expose   = core_config.get("csr_expose", False)
        csr_align    = core_config.get("csr_align", 32)
        if cpu_type is None:
            kwargs["integrated_rom_size"]  = 0
            kwargs["integrated_sram_size"] = 0
            kwargs["l2_size"]              = 0
            kwargs["min_l2_data_width"]    = 0
            kwargs["with_uart"]            = False
            kwargs["with_timer"]           = False
            kwargs["with_ctrl"]            = False
            kwargs["with_wishbone"]        = False
        else:
            kwargs["l2_size"]           = 0
            kwargs["min_l2_data_width"] = 0

        # SoCSDRAM ---------------------------------------------------------------------------------
        SoCSDRAM.__init__(self, platform, sys_clk_freq,
            cpu_type       = cpu_type,
            #cpu_variant    = "lite",
            csr_alignment  = csr_align,
            max_sdram_size = 0x01000000, # Only expose 16MB to the CPU, enough for Init/Calib.
            **kwargs)

        # CRG --------------------------------------------------------------------------------------
        if core_config["sdram_phy"] in [litedram_phys.ECP5DDRPHY]:
            self.submodules.crg = crg = LiteDRAMECP5DDRPHYCRG(platform, core_config)
        if core_config["sdram_phy"] in [litedram_phys.A7DDRPHY, litedram_phys.K7DDRPHY, litedram_phys.V7DDRPHY]:
            self.submodules.crg = LiteDRAMS7DDRPHYCRG(platform, core_config)
        if core_config["sdram_phy"] in [litedram_phys.USDDRPHY, litedram_phys.USPDDRPHY]:
            self.submodules.crg = LiteDRAMUSDDRPHYCRG(platform, core_config)

        # DRAM -------------------------------------------------------------------------------------
        # platform.add_extension(get_dram_ios(core_config))
        platform.add_extension(get_dram_ios(core_config))
        # ECP5DDRPHY
        if core_config["sdram_phy"] in  [litedram_phys.ECP5DDRPHY]:
            assert core_config["memtype"] in ["DDR3"]
            self.submodules.ddrphy = core_config["sdram_phy"](
                pads         = platform.request("ddram"),
                sys_clk_freq = sys_clk_freq)
            self.comb += crg.stop.eq(self.ddrphy.init.stop)
            self.add_constant("ECP5DDRPHY")
            sdram_module = core_config["sdram_module"](sys_clk_freq, "1:2")
        # S7DDRPHY
        if core_config["sdram_phy"] in [litedram_phys.A7DDRPHY, litedram_phys.K7DDRPHY, litedram_phys.V7DDRPHY]:
            assert core_config["memtype"] in ["DDR2", "DDR3"]
            self.submodules.ddrphy = core_config["sdram_phy"](
                pads             = platform.request("ddram"),
                memtype          = core_config["memtype"],
                nphases          = 4 if core_config["memtype"] == "DDR3" else 2,
                sys_clk_freq     = sys_clk_freq,
                iodelay_clk_freq = core_config["iodelay_clk_freq"],
                cmd_latency      = core_config["cmd_latency"])
            self.add_constant("CMD_DELAY", core_config["cmd_delay"])
            if core_config["memtype"] == "DDR3":
                self.ddrphy.settings.add_electrical_settings(
                    rtt_nom = core_config["rtt_nom"],
                    rtt_wr  = core_config["rtt_wr"],
                    ron     = core_config["ron"])
        # USDDRPHY
        if core_config["sdram_phy"] in [litedram_phys.USDDRPHY, litedram_phys.USPDDRPHY]:
            assert core_config["memtype"] in ["DDR3", "DDR4"]
            ddram_pads = platform.request("ddram")
            # TODO make it configurable
            ddram_pads = PHYPadsReducer(ddram_pads, modules=[0, 1, 2, 3])
            self.submodules.ddrphy = core_config["sdram_phy"](
                pads             = ddram_pads,
                memtype          = core_config["memtype"],
                sys_clk_freq     = sys_clk_freq,
                iodelay_clk_freq = core_config["iodelay_clk_freq"],
                cmd_latency      = core_config["cmd_latency"])
            self.add_constant("USDDRPHY_DEBUG")

        self.add_csr("ddrphy")

        sdram_module = core_config["sdram_module"](sys_clk_freq,
            "1:4" if core_config["memtype"] == "DDR3" else "1:2")
        controller_settings = controller_settings=ControllerSettings(
            cmd_buffer_depth=core_config["cmd_buffer_depth"])

        #print("Before register, submodules are")
        #print(self._submodules)

        self.register_sdram(self.ddrphy,
            geom_settings       = sdram_module.geom_settings,
            timing_settings     = sdram_module.timing_settings,
            controller_settings = controller_settings)


        #feig Print out the PHY submodule
        # verilog.convert(self.ddrphy).write("DDRPHY.v")        
        #print("After register, submodules are")
        #print(self._submodules)

        # DRAM Initialization ----------------------------------------------------------------------
        self.submodules.ddrctrl = LiteDRAMCoreControl()
        self.add_csr("ddrctrl")
        self.comb += [
            platform.request("init_done").eq(self.ddrctrl.init_done.storage),
            platform.request("init_error").eq(self.ddrctrl.init_error.storage)
        ]

        #print("Before setting up controller, submodules are")
        #print(self._submodules)

        print("Crossbar has {} master ports".format(len(self.sdram.crossbar.masters)))

        # CSR port ---------------------------------------------------------------------------------
        if csr_expose:
            csr_port = csr_bus.Interface(
                address_width = self.csr_address_width,
                data_width    = self.csr_data_width)
            self.add_csr_master(csr_port)
            platform.add_extension(get_csr_ios(self.csr_address_width, self.csr_data_width))
            _csr_port_io = platform.request("csr_port", 0)
            self.comb += [
                csr_port.adr.eq(_csr_port_io.adr),
                csr_port.we.eq(_csr_port_io.we),
                csr_port.dat_w.eq(_csr_port_io.dat_w),
                _csr_port_io.dat_r.eq(csr_port.dat_r),
            ]
            if self.cpu_type == None:
                csr_base = core_config.get("csr_base", 0)
                self.shadow_base = csr_base;

        # User ports -------------------------------------------------------------------------------
        self.comb += [
            platform.request("user_clk").eq(ClockSignal()),
            platform.request("user_rst").eq(ResetSignal())
        ]
        for name, port in core_config["user_ports"].items():
            # Native -------------------------------------------------------------------------------
            if port["type"] == "native":
                user_port = self.sdram.crossbar.get_port()
                platform.add_extension(get_native_user_port_ios(name,
                    user_port.address_width,
                    user_port.data_width))
                _user_port_io = platform.request("user_port_{}".format(name))
                self.comb += [
                    # cmd
                    user_port.cmd.valid.eq(_user_port_io.cmd_valid),
                    _user_port_io.cmd_ready.eq(user_port.cmd.ready),
                    user_port.cmd.we.eq(_user_port_io.cmd_we),
                    user_port.cmd.addr.eq(_user_port_io.cmd_addr),

                    # wdata
                    user_port.wdata.valid.eq(_user_port_io.wdata_valid),
                    _user_port_io.wdata_ready.eq(user_port.wdata.ready),
                    user_port.wdata.we.eq(_user_port_io.wdata_we),
                    user_port.wdata.data.eq(_user_port_io.wdata_data),

                    # rdata
                    _user_port_io.rdata_valid.eq(user_port.rdata.valid),
                    user_port.rdata.ready.eq(_user_port_io.rdata_ready),
                    _user_port_io.rdata_data.eq(user_port.rdata.data),
                ]
            # Wishbone -----------------------------------------------------------------------------
            elif port["type"] == "wishbone":
                user_port = self.sdram.crossbar.get_port()
                wb_port = wishbone.Interface(
                    user_port.data_width,
                    user_port.address_width)
                wishbone2native = LiteDRAMWishbone2Native(wb_port, user_port)
                self.submodules += wishbone2native
                platform.add_extension(get_wishbone_user_port_ios(name,
                        len(wb_port.adr),
                        len(wb_port.dat_w)))
                _wb_port_io = platform.request("user_port_{}".format(name))
                self.comb += [
                    wb_port.adr.eq(_wb_port_io.adr),
                    wb_port.dat_w.eq(_wb_port_io.dat_w),
                    _wb_port_io.dat_r.eq(wb_port.dat_r),
                    wb_port.sel.eq(_wb_port_io.sel),
                    wb_port.cyc.eq(_wb_port_io.cyc),
                    wb_port.stb.eq(_wb_port_io.stb),
                    _wb_port_io.ack.eq(wb_port.ack),
                    wb_port.we.eq(_wb_port_io.we),
                    _wb_port_io.err.eq(wb_port.err),
                ]
            # AXI ----------------------------------------------------------------------------------
            elif port["type"] == "axi":
                user_port = self.sdram.crossbar.get_port()
                axi_port  = LiteDRAMAXIPort(
                    user_port.data_width,
                    user_port.address_width + log2_int(user_port.data_width//8),
                    port["id_width"])
                axi2native = LiteDRAMAXI2Native(axi_port, user_port)
                self.submodules += axi2native
                platform.add_extension(get_axi_user_port_ios(name,
                        axi_port.address_width,
                        axi_port.data_width,
                        port["id_width"]))
                _axi_port_io = platform.request("user_port_{}".format(name))
                self.comb += [
                    # aw
                    axi_port.aw.valid.eq(_axi_port_io.awvalid),
                    _axi_port_io.awready.eq(axi_port.aw.ready),
                    axi_port.aw.addr.eq(_axi_port_io.awaddr),
                    axi_port.aw.burst.eq(_axi_port_io.awburst),
                    axi_port.aw.len.eq(_axi_port_io.awlen),
                    axi_port.aw.size.eq(_axi_port_io.awsize),
                    axi_port.aw.id.eq(_axi_port_io.awid),

                    # w
                    axi_port.w.valid.eq(_axi_port_io.wvalid),
                    _axi_port_io.wready.eq(axi_port.w.ready),
                    axi_port.w.last.eq(_axi_port_io.wlast),
                    axi_port.w.strb.eq(_axi_port_io.wstrb),
                    axi_port.w.data.eq(_axi_port_io.wdata),

                    # b
                    _axi_port_io.bvalid.eq(axi_port.b.valid),
                    axi_port.b.ready.eq(_axi_port_io.bready),
                    _axi_port_io.bresp.eq(axi_port.b.resp),
                    _axi_port_io.bid.eq(axi_port.b.id),

                    # ar
                    axi_port.ar.valid.eq(_axi_port_io.arvalid),
                    _axi_port_io.arready.eq(axi_port.ar.ready),
                    axi_port.ar.addr.eq(_axi_port_io.araddr),
                    axi_port.ar.burst.eq(_axi_port_io.arburst),
                    axi_port.ar.len.eq(_axi_port_io.arlen),
                    axi_port.ar.size.eq(_axi_port_io.arsize),
                    axi_port.ar.id.eq(_axi_port_io.arid),

                    # r
                    _axi_port_io.rvalid.eq(axi_port.r.valid),
                    axi_port.r.ready.eq(_axi_port_io.rready),
                    _axi_port_io.rlast.eq(axi_port.r.last),
                    _axi_port_io.rresp.eq(axi_port.r.resp),
                    _axi_port_io.rdata.eq(axi_port.r.data),
                    _axi_port_io.rid.eq(axi_port.r.id),
                ]
            # FIFO ---------------------------------------------------------------------------------
            elif port["type"] == "fifo":
                platform.add_extension(get_fifo_user_port_ios(name, user_port.data_width))
                _user_fifo_io = platform.request("user_fifo_{}".format(name))
                fifo = LiteDRAMFIFO(
                    data_width      = user_port.data_width,
                    base            = port["base"],
                    depth           = port["depth"],
                    write_port      = self.sdram.crossbar.get_port("write"),
                    write_threshold = port["depth"] - 32, # FIXME
                    read_port       = self.sdram.crossbar.get_port("read"),
                    read_threshold  = 32 # FIXME
                )
                self.submodules += fifo
                self.comb += [
                    # in
                    fifo.sink.valid.eq(_user_fifo_io.in_valid),
                    _user_fifo_io.in_ready.eq(fifo.sink.ready),
                    fifo.sink.data.eq(_user_fifo_io.in_data),

                    # out
                    _user_fifo_io.out_valid.eq(fifo.source.valid),
                    fifo.source.ready.eq(_user_fifo_io.out_ready),
                    _user_fifo_io.out_data.eq(fifo.source.data),
                ]
            else:
                raise ValueError("Unsupported port type: {}".format(port["type"]))
        
            print("Crossbar has {} master ports".format(len(self.sdram.crossbar.masters)))
        
        ## ComputeDRAM ports
        # print (self._submodules)
        self.comb += [
            self.sdram.R1.eq(platform.request("ComputeDRAM_R1")),
            self.sdram.R2.eq(platform.request("ComputeDRAM_R2")),
            self.sdram.T1.eq(platform.request("ComputeDRAM_T1")),
            self.sdram.T2.eq(platform.request("ComputeDRAM_T2")),
            self.sdram.vld.eq(platform.request("ComputeDRAM_vld")),
            platform.request("ComputeDRAM_rdy").eq(self.sdram.rdy)
        ]
    def __init__(self,
                 sys_clk_freq=int(75e6),
                 cpu_count=1,
                 with_ethernet=False,
                 **kwargs):
        VexRiscvSMP.litedram_width = 128
        VexRiscvSMP.ibus_width = 64
        VexRiscvSMP.dbus_width = 64
        VexRiscvSMP.coherent_dma = True

        platform = trellisboard.Platform(toolchain="trellis")

        # SoCCore ----------------------------------------------------------------------------------
        kwargs["integrated_rom_size"] = 0x10000
        kwargs["csr_data_width"] = 8
        SoCCore.__init__(self,
                         platform,
                         clk_freq=sys_clk_freq,
                         cpu_cls=VexRiscvSMP,
                         cpu_variant="default",
                         **kwargs)
        self.add_constant("config_cpu_count", cpu_count)  # for dts generation

        # PLIC ------------------------------------------------------------------------------------
        self.bus.add_slave("plic",
                           self.cpu.plicbus,
                           region=SoCRegion(origin=0xf0C00000,
                                            size=0x400000,
                                            cached=False))

        # CLINT ------------------------------------------------------------------------------------
        self.bus.add_slave("clint",
                           self.cpu.cbus,
                           region=SoCRegion(origin=0xf0010000,
                                            size=0x10000,
                                            cached=False))

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform, sys_clk_freq)

        # DDR3 SDRAM -------------------------------------------------------------------------------
        if not self.integrated_main_ram_size:
            self.submodules.ddrphy = ECP5DDRPHY(pads=PHYPadsReducer(
                platform.request("ddram"), [0, 1]),
                                                sys_clk_freq=sys_clk_freq)
            self.comb += self.crg.stop.eq(self.ddrphy.init.stop)
            self.comb += self.crg.reset.eq(self.ddrphy.init.reset)
            self.add_csr("ddrphy")
            self.add_sdram("sdram",
                           phy=self.ddrphy,
                           module=MT41J256M16(sys_clk_freq, "1:2"),
                           origin=self.mem_map["main_ram"],
                           size=kwargs.get("max_sdram_size", 0x40000000),
                           l2_cache_size=kwargs.get("l2_size", 8192),
                           l2_cache_min_data_width=kwargs.get(
                               "min_l2_data_width", 128),
                           l2_cache_reverse=True,
                           controller_settings=ControllerSettings(
                               cmd_buffer_buffered=False,
                               with_auto_precharge=False))

        # Ethernet ---------------------------------------------------------------------------------
        if with_ethernet:
            self.submodules.ethphy = LiteEthPHYRGMII(
                clock_pads=self.platform.request("eth_clocks"),
                pads=self.platform.request("eth"))
            self.add_csr("ethphy")
            self.add_ethernet(phy=self.ethphy)