2020-02-02 21:27:09 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# This file is Copyright (c) 2019 Sean Cross <sean@xobs.io>
|
|
|
|
# This file is Copyright (c) 2018 David Shah <dave@ds0.me>
|
|
|
|
# This file is Copyright (c) 2020 Piotr Esden-Tempski <piotr@esden.net>
|
|
|
|
# License: BSD
|
|
|
|
|
|
|
|
# This target was originally based on the Fomu target.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
|
|
|
|
2020-02-03 00:07:56 -05:00
|
|
|
from litex.soc.cores import up5kspram, spi_flash
|
2020-02-02 21:27:09 -05:00
|
|
|
from litex.soc.integration.soc_core import SoCCore
|
|
|
|
from litex.soc.integration.builder import Builder, builder_argdict, builder_args
|
|
|
|
from litex.soc.integration.soc_core import soc_core_argdict, soc_core_args
|
|
|
|
from litex.soc.integration.doc import AutoDoc
|
|
|
|
|
2020-03-05 03:11:19 -05:00
|
|
|
from litex_boards.platforms.icebreaker import Platform
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-02-03 00:33:29 -05:00
|
|
|
from litex.soc.interconnect import wishbone
|
2020-02-02 23:32:16 -05:00
|
|
|
from litex.soc.cores.uart import UARTWishboneBridge
|
2020-03-05 03:11:19 -05:00
|
|
|
from litex.soc.cores.gpio import GPIOOut
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-02-05 17:49:05 -05:00
|
|
|
|
2020-02-03 00:07:56 -05:00
|
|
|
class JumpToAddressROM(wishbone.SRAM):
|
|
|
|
def __init__(self, size, addr):
|
|
|
|
data = [
|
2020-02-05 17:49:05 -05:00
|
|
|
0x00000537 | ((addr & 0xfffff000) << 0), # lui a0,%hi(addr)
|
|
|
|
0x00052503 | ((addr & 0x00000fff) << 20), # lw a0,%lo(addr)(a0)
|
|
|
|
0x000500e7, # jalr a0
|
2020-02-03 00:07:56 -05:00
|
|
|
]
|
|
|
|
wishbone.SRAM.__init__(self, size, read_only=True, init=data)
|
|
|
|
|
2020-02-02 21:27:09 -05:00
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
|
|
|
|
2020-03-07 12:25:26 -05:00
|
|
|
class _CRG(Module):
|
2020-02-02 21:27:09 -05:00
|
|
|
def __init__(self, platform):
|
2020-03-07 12:25:26 -05:00
|
|
|
self.clock_domains.cd_sys = ClockDomain()
|
2020-02-02 23:32:16 -05:00
|
|
|
self.clock_domains.cd_por = ClockDomain()
|
|
|
|
|
2020-03-07 12:25:26 -05:00
|
|
|
self.reset = Signal()
|
2020-02-02 23:32:16 -05:00
|
|
|
|
2020-03-07 12:25:26 -05:00
|
|
|
# # #
|
2020-02-02 23:32:16 -05:00
|
|
|
|
2020-03-07 12:25:26 -05:00
|
|
|
reset_delay = Signal(12, reset=4095)
|
2020-02-02 23:32:16 -05:00
|
|
|
|
2020-03-07 12:25:26 -05:00
|
|
|
# Clocks
|
|
|
|
clk12 = platform.request("clk12")
|
|
|
|
platform.add_period_constraint(clk12, 1e9/12e6)
|
2020-02-02 23:32:16 -05:00
|
|
|
self.comb += self.cd_sys.clk.eq(clk12)
|
2020-03-07 12:25:26 -05:00
|
|
|
self.comb += self.cd_por.clk.eq(clk12)
|
|
|
|
self.comb += self.cd_sys.rst.eq(reset_delay != 0)
|
2020-02-02 23:32:16 -05:00
|
|
|
|
2020-03-07 12:25:26 -05:00
|
|
|
# Power On Reset
|
|
|
|
self.sync.por += If(reset_delay != 0, reset_delay.eq(reset_delay - 1))
|
2020-02-02 23:32:16 -05:00
|
|
|
self.specials += AsyncResetSynchronizer(self.cd_por, self.reset)
|
2020-02-02 21:27:09 -05:00
|
|
|
|
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
|
|
|
"""A SoC on iCEBreaker, optionally with a softcore CPU"""
|
|
|
|
|
2020-03-06 09:11:33 -05:00
|
|
|
# Statically-define the memory map, to prevent it from shifting across various litex versions.
|
2020-02-02 21:27:09 -05:00
|
|
|
SoCCore.mem_map = {
|
2020-02-03 00:07:56 -05:00
|
|
|
"rom": 0x00000000, # (default shadow @0x80000000)
|
|
|
|
"sram": 0x10000000, # (default shadow @0xa0000000)
|
|
|
|
"spiflash": 0x20000000, # (default shadow @0xa0000000)
|
|
|
|
"csr": 0xe0000000, # (default shadow @0x60000000)
|
|
|
|
"vexriscv_debug": 0xf00f0000,
|
2020-02-02 21:27:09 -05:00
|
|
|
}
|
|
|
|
|
2020-03-06 07:35:29 -05:00
|
|
|
def __init__(self, debug=True, boot_vector=0x2001a000, **kwargs):
|
2020-03-05 03:11:19 -05:00
|
|
|
"""Create a basic SoC for iCEBreaker.
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-03-05 03:11:19 -05:00
|
|
|
Create a basic SoC for iCEBreaker. The `sys` frequency will run at 12 MHz.
|
2020-02-02 21:27:09 -05:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Newly-constructed SoC
|
|
|
|
"""
|
|
|
|
platform = Platform()
|
|
|
|
|
2020-03-06 09:11:33 -05:00
|
|
|
kwargs["cpu_variant"] = "lite"
|
|
|
|
kwargs["cpu_reset_address"] = boot_vector
|
|
|
|
if debug:
|
|
|
|
kwargs["uart_name"] = "crossover"
|
|
|
|
kwargs["cpu_variant"] = "lite+debug"
|
2020-02-02 21:27:09 -05:00
|
|
|
|
|
|
|
clk_freq = int(12e6)
|
|
|
|
|
2020-02-02 23:32:16 -05:00
|
|
|
# Force the SRAM size to 0, because we add our own SRAM with SPRAM
|
2020-02-02 21:27:09 -05:00
|
|
|
kwargs["integrated_sram_size"] = 0
|
2020-03-06 09:11:33 -05:00
|
|
|
kwargs["integrated_rom_size"] = 0
|
2020-02-02 23:39:13 -05:00
|
|
|
|
2020-03-06 09:11:33 -05:00
|
|
|
SoCCore.__init__(self, platform, clk_freq, **kwargs)
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-02-03 00:07:56 -05:00
|
|
|
# If there is a VexRiscv CPU, add a fake ROM that simply tells the CPU
|
|
|
|
# to jump to the given address.
|
|
|
|
if hasattr(self, "cpu") and self.cpu.name == "vexriscv":
|
|
|
|
self.add_memory_region("rom", 0, 16)
|
|
|
|
self.submodules.rom = JumpToAddressROM(16, boot_vector)
|
|
|
|
|
2020-02-02 21:27:09 -05:00
|
|
|
self.submodules.crg = _CRG(platform)
|
|
|
|
|
|
|
|
# UP5K has single port RAM, which is a dedicated 128 kilobyte block.
|
|
|
|
# Use this as CPU RAM.
|
|
|
|
spram_size = 128 * 1024
|
|
|
|
self.submodules.spram = up5kspram.Up5kSPRAM(size=spram_size)
|
|
|
|
self.register_mem("sram", self.mem_map["sram"], self.spram.bus, spram_size)
|
|
|
|
|
2020-02-03 00:07:56 -05:00
|
|
|
# The litex SPI module supports memory-mapped reads, as well as a bit-banged mode
|
|
|
|
# for doing writes.
|
|
|
|
spi_pads = platform.request("spiflash4x")
|
2020-02-03 00:15:41 -05:00
|
|
|
self.submodules.lxspi = spi_flash.SpiFlash(spi_pads, dummy=6, endianness="little")
|
2020-02-03 00:07:56 -05:00
|
|
|
self.register_mem("spiflash", self.mem_map["spiflash"], self.lxspi.bus, size=16 * 1024 * 1024)
|
|
|
|
self.add_csr("lxspi")
|
|
|
|
|
|
|
|
# In debug mode, add a UART bridge. This takes over from the normal UART bridge,
|
|
|
|
# however you can use the "crossover" UART to communicate with this over the bridge.
|
2020-02-02 23:32:16 -05:00
|
|
|
if debug:
|
|
|
|
self.submodules.uart_bridge = UARTWishboneBridge(platform.request("serial"), clk_freq, baudrate=115200)
|
|
|
|
self.add_wb_master(self.uart_bridge.wishbone)
|
|
|
|
if hasattr(self, "cpu") and self.cpu.name == "vexriscv":
|
|
|
|
self.register_mem("vexriscv_debug", 0xf00f0000, self.cpu.debug_bus, 0x100)
|
|
|
|
|
2020-03-06 07:42:26 -05:00
|
|
|
self.submodules.leds = GPIOOut(Cat(
|
|
|
|
platform.request("user_ledr_n"),
|
|
|
|
platform.request("user_ledg_n")))
|
2020-03-05 03:11:19 -05:00
|
|
|
self.add_csr("leds")
|
|
|
|
|
|
|
|
# self.add_memory_region("rom", 0x2001a000, 16 * 1024 * 1024 - 0x1a000, type="cached+linker")
|
|
|
|
# self.add_memory_region("boot", 0, 16, type="cached+linker")
|
|
|
|
# self.mem_regions["rom"] = SoCMemRegion(0x2001a000, 16 * 1024 * 1024 - 0x1a000, "cached")
|
|
|
|
# self.mem_regions["boot"] = SoCMemRegion(0, 16, "cached")
|
2020-02-03 00:33:29 -05:00
|
|
|
|
2020-03-06 07:35:29 -05:00
|
|
|
def set_yosys_nextpnr_settings(self, nextpnr_seed=0, nextpnr_placer="heap"):
|
|
|
|
"""Set Yosys/Nextpnr settings by overriding default LiteX's settings.
|
|
|
|
Args:
|
|
|
|
nextpnr_seed (int): Seed to use in Nextpnr
|
|
|
|
nextpnr_placer (str): Placer to use in Nextpnr
|
|
|
|
"""
|
|
|
|
assert hasattr(self.platform.toolchain, "yosys_template")
|
|
|
|
assert hasattr(self.platform.toolchain, "build_template")
|
|
|
|
self.platform.toolchain.yosys_template = [
|
|
|
|
"{read_files}",
|
|
|
|
"attrmap -tocase keep -imap keep=\"true\" keep=1 -imap keep=\"false\" keep=0 -remove keep=0",
|
|
|
|
# Use "-relut -dffe_min_ce_use 4" to the synth_ice40 command. The "-reult" adds an additional
|
|
|
|
# LUT pass to pack more stuff in, and the "-dffe_min_ce_use 4" flag prevents Yosys from
|
|
|
|
# generating a Clock Enable signal for a LUT that has fewer than 4 flip-flops. This increases
|
|
|
|
# density, and lets us use the FPGA more efficiently.
|
|
|
|
"synth_ice40 -json {build_name}.json -top {build_name} -relut -abc2 -dffe_min_ce_use 4 -relut",
|
|
|
|
]
|
|
|
|
self.platform.toolchain.build_template = [
|
|
|
|
"yosys -q -l {build_name}.rpt {build_name}.ys",
|
|
|
|
"nextpnr-ice40 --json {build_name}.json --pcf {build_name}.pcf --asc {build_name}.txt"
|
|
|
|
+ " --pre-pack {build_name}_pre_pack.py --{architecture} --package {package}"
|
|
|
|
+ " --seed {}".format(nextpnr_seed)
|
|
|
|
+ " --placer {}".format(nextpnr_placer),
|
|
|
|
# Disable final deep-sleep power down so firmware words are loaded onto softcore's address bus.
|
|
|
|
"icepack -s {build_name}.txt {build_name}.bin"
|
|
|
|
]
|
2020-02-02 21:27:09 -05:00
|
|
|
|
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description="LiteX SoC on iCEBreaker")
|
2020-03-06 09:11:33 -05:00
|
|
|
parser.add_argument("--nextpnr-seed", default=0, help="Seed to use in Nextpnr")
|
2020-03-06 07:35:29 -05:00
|
|
|
parser.add_argument("--nextpnr-placer", default="heap", choices=["sa", "heap"], help="Placer implementation to use in Nextpnr")
|
2020-02-02 21:27:09 -05:00
|
|
|
builder_args(parser)
|
|
|
|
soc_core_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-03-06 09:11:33 -05:00
|
|
|
soc = BaseSoC(debug=True, **soc_core_argdict(args))
|
2020-03-06 07:35:29 -05:00
|
|
|
soc.set_yosys_nextpnr_settings(nextpnr_seed=args.nextpnr_seed, nextpnr_placer=args.nextpnr_placer)
|
2020-02-03 00:07:56 -05:00
|
|
|
|
2020-03-06 09:11:33 -05:00
|
|
|
# Don't build software -- we don't include it since we just jump to SPI flash.
|
|
|
|
builder_kwargs = builder_argdict(args)
|
|
|
|
builder_kwargs["compile_software"] = False
|
|
|
|
builder = Builder(soc, **builder_kwargs)
|
2020-02-02 21:27:09 -05:00
|
|
|
builder.build()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|