2020-02-02 21:27:09 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-08-23 09:00:17 -04:00
|
|
|
#
|
|
|
|
# This file is part of LiteX-Boards.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2019 Sean Cross <sean@xobs.io>
|
|
|
|
# Copyright (c) 2018 David Shah <dave@ds0.me>
|
|
|
|
# Copyright (c) 2020 Piotr Esden-Tempski <piotr@esden.net>
|
|
|
|
# Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-03-13 04:43:43 -04:00
|
|
|
# The iCEBreaker is the first open source iCE40 FPGA development board designed for teachers and
|
|
|
|
# students: https://www.crowdsupply.com/1bitsquared/icebreaker-fpga
|
|
|
|
|
|
|
|
# This target file provides a minimal LiteX SoC for the iCEBreaker with a CPU, its ROM (in SPI Flash),
|
|
|
|
# its SRAM, close to the others LiteX targets. A more complete example of LiteX SoC for the iCEBreaker
|
|
|
|
# with more features, examples to run C/Rust code on the RISC-V CPU and documentation can be found
|
|
|
|
# at: https://github.com/icebreaker-fpga/icebreaker-litex-examples
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-05-05 09:11:38 -04:00
|
|
|
import os
|
2020-02-02 21:27:09 -05:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
|
|
|
|
2020-03-13 04:43:43 -04:00
|
|
|
from litex_boards.platforms import icebreaker
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2021-03-24 10:03:48 -04:00
|
|
|
from litex.soc.cores.ram import Up5kSPRAM
|
2020-03-13 04:43:43 -04:00
|
|
|
from litex.soc.cores.clock import iCE40PLL
|
|
|
|
from litex.soc.integration.soc_core import *
|
2020-09-01 06:49:44 -04:00
|
|
|
from litex.soc.integration.soc import SoCRegion
|
2020-03-13 04:43:43 -04:00
|
|
|
from litex.soc.integration.builder import *
|
2021-03-03 11:47:20 -05:00
|
|
|
from litex.soc.cores.video import VideoDVIPHY
|
2020-09-01 06:49:44 -04:00
|
|
|
from litex.soc.cores.led import LedChaser
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-03-13 04:43:43 -04:00
|
|
|
kB = 1024
|
|
|
|
mB = 1024*kB
|
2020-02-03 00:07:56 -05:00
|
|
|
|
2020-02-02 21:27:09 -05:00
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
|
|
|
|
2020-03-07 12:25:26 -05:00
|
|
|
class _CRG(Module):
|
2020-03-13 04:43:43 -04:00
|
|
|
def __init__(self, platform, sys_clk_freq):
|
2020-11-04 05:09:30 -05:00
|
|
|
self.rst = Signal()
|
2020-03-07 12:25:26 -05:00
|
|
|
self.clock_domains.cd_sys = ClockDomain()
|
2020-09-01 06:49:44 -04:00
|
|
|
self.clock_domains.cd_por = ClockDomain(reset_less=True)
|
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-09-01 06:49:44 -04:00
|
|
|
# Clk/Rst
|
2020-03-07 12:25:26 -05:00
|
|
|
clk12 = platform.request("clk12")
|
2020-03-13 04:43:43 -04:00
|
|
|
rst_n = platform.request("user_btn_n")
|
2020-02-02 23:32:16 -05:00
|
|
|
|
2020-03-07 12:25:26 -05:00
|
|
|
# Power On Reset
|
2020-09-01 06:49:44 -04:00
|
|
|
por_count = Signal(16, reset=2**16-1)
|
|
|
|
por_done = Signal()
|
|
|
|
self.comb += self.cd_por.clk.eq(ClockSignal())
|
|
|
|
self.comb += por_done.eq(por_count == 0)
|
|
|
|
self.sync.por += If(~por_done, por_count.eq(por_count - 1))
|
|
|
|
|
|
|
|
# PLL
|
|
|
|
self.submodules.pll = pll = iCE40PLL(primitive="SB_PLL40_PAD")
|
2021-05-20 03:14:54 -04:00
|
|
|
self.comb += pll.reset.eq(~rst_n) # FIXME: Add proper iCE40PLL reset support and add back | self.rst.
|
2020-09-01 06:49:44 -04:00
|
|
|
pll.register_clkin(clk12, 12e6)
|
|
|
|
pll.create_clkout(self.cd_sys, sys_clk_freq, with_reset=False)
|
|
|
|
self.specials += AsyncResetSynchronizer(self.cd_sys, ~por_done | ~pll.locked)
|
|
|
|
platform.add_period_constraint(self.cd_sys.clk, 1e9/sys_clk_freq)
|
2020-02-02 21:27:09 -05:00
|
|
|
|
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
2021-07-06 17:39:37 -04:00
|
|
|
def __init__(self, bios_flash_offset, sys_clk_freq=int(24e6), with_led_chaser=True,
|
|
|
|
with_video_terminal=False, **kwargs):
|
2020-11-12 12:07:28 -05:00
|
|
|
platform = icebreaker.Platform()
|
2020-09-01 06:49:44 -04:00
|
|
|
platform.add_extension(icebreaker.break_off_pmod)
|
2020-03-13 04:43:43 -04:00
|
|
|
|
|
|
|
# Disable Integrated ROM/SRAM since too large for iCE40 and UP5K has specific SPRAM.
|
|
|
|
kwargs["integrated_sram_size"] = 0
|
|
|
|
kwargs["integrated_rom_size"] = 0
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-03-13 04:43:43 -04:00
|
|
|
# SoCCore ----------------------------------------------------------------------------------
|
2020-06-30 12:11:04 -04:00
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq,
|
2022-01-18 11:13:02 -05:00
|
|
|
ident = "LiteX SoC on iCEBreaker",
|
2020-06-30 12:11:04 -04:00
|
|
|
**kwargs)
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-03-13 04:43:43 -04:00
|
|
|
# CRG --------------------------------------------------------------------------------------
|
|
|
|
self.submodules.crg = _CRG(platform, sys_clk_freq)
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2021-09-29 13:33:22 -04:00
|
|
|
# 128KB SPRAM (used as 64kB SRAM / 64kB RAM) -----------------------------------------------
|
2021-07-27 06:03:39 -04:00
|
|
|
self.submodules.spram = Up5kSPRAM(size=128*kB)
|
2021-09-30 03:32:26 -04:00
|
|
|
self.bus.add_slave("psram", self.spram.bus, SoCRegion(size=128*kB))
|
|
|
|
self.bus.add_region("sram", SoCRegion(
|
|
|
|
origin = self.bus.regions["psram"].origin + 0*kB,
|
|
|
|
size = 64*kB,
|
|
|
|
linker = True)
|
|
|
|
)
|
2021-09-29 13:33:22 -04:00
|
|
|
if not self.integrated_main_ram_size:
|
|
|
|
self.bus.add_region("main_ram", SoCRegion(
|
2021-09-30 03:32:26 -04:00
|
|
|
origin = self.bus.regions["psram"].origin + 64*kB,
|
2021-09-29 13:33:22 -04:00
|
|
|
size = 64*kB,
|
|
|
|
linker = True)
|
|
|
|
)
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-03-13 04:43:43 -04:00
|
|
|
# SPI Flash --------------------------------------------------------------------------------
|
2021-07-27 13:23:26 -04:00
|
|
|
from litespi.modules import W25Q128JV
|
|
|
|
from litespi.opcodes import SpiNorFlashOpCodes as Codes
|
|
|
|
self.add_spi_flash(mode="4x", module=W25Q128JV(Codes.READ_1_1_4), with_master=False)
|
2020-02-02 21:27:09 -05:00
|
|
|
|
2020-03-13 04:43:43 -04:00
|
|
|
# Add ROM linker region --------------------------------------------------------------------
|
2020-09-01 06:49:44 -04:00
|
|
|
self.bus.add_region("rom", SoCRegion(
|
2022-01-07 04:34:47 -05:00
|
|
|
origin = self.bus.regions["spiflash"].origin + bios_flash_offset,
|
2020-09-01 06:49:44 -04:00
|
|
|
size = 32*kB,
|
|
|
|
linker = True)
|
|
|
|
)
|
2022-01-07 09:19:23 -05:00
|
|
|
self.cpu.set_reset_address(self.bus.regions["rom"].origin)
|
2020-02-02 23:39:13 -05:00
|
|
|
|
2021-03-05 08:40:27 -05:00
|
|
|
# Video ------------------------------------------------------------------------------------
|
2021-03-03 10:21:04 -05:00
|
|
|
if with_video_terminal:
|
2021-03-03 11:47:20 -05:00
|
|
|
platform.add_extension(icebreaker.dvi_pmod)
|
|
|
|
self.submodules.videophy = VideoDVIPHY(platform.request("dvi"), clock_domain="sys")
|
2021-03-04 12:29:21 -05:00
|
|
|
self.add_video_terminal(phy=self.videophy, timings="640x480@75Hz", clock_domain="sys")
|
2021-03-03 10:21:04 -05:00
|
|
|
|
2020-03-13 04:43:43 -04:00
|
|
|
# Leds -------------------------------------------------------------------------------------
|
2021-07-06 17:39:37 -04:00
|
|
|
if with_led_chaser:
|
|
|
|
self.submodules.leds = LedChaser(
|
|
|
|
pads = platform.request_all("user_led"),
|
|
|
|
sys_clk_freq = sys_clk_freq)
|
2020-03-13 04:43:43 -04:00
|
|
|
|
|
|
|
# Flash --------------------------------------------------------------------------------------------
|
|
|
|
|
2021-05-20 03:14:54 -04:00
|
|
|
def flash(build_dir, build_name, bios_flash_offset):
|
2020-03-13 04:43:43 -04:00
|
|
|
from litex.build.lattice.programmer import IceStormProgrammer
|
|
|
|
prog = IceStormProgrammer()
|
2021-05-20 03:14:54 -04:00
|
|
|
prog.flash(bios_flash_offset, f"{build_dir}/software/bios/bios.bin")
|
|
|
|
prog.flash(0x00000000, f"{build_dir}/gateware/{build_name}.bin")
|
2020-02-02 21:27:09 -05:00
|
|
|
|
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description="LiteX SoC on iCEBreaker")
|
2022-01-05 11:06:22 -05:00
|
|
|
parser.add_argument("--build", action="store_true", help="Build bitstream.")
|
|
|
|
parser.add_argument("--load", action="store_true", help="Load bitstream.")
|
|
|
|
parser.add_argument("--flash", action="store_true", help="Flash Bitstream and BIOS.")
|
|
|
|
parser.add_argument("--sys-clk-freq", default=24e6, help="System clock frequency.")
|
|
|
|
parser.add_argument("--bios-flash-offset", default="0x40000", help="BIOS offset in SPI Flash.")
|
|
|
|
parser.add_argument("--with-video-terminal", action="store_true", help="Enable Video Terminal (with DVI PMOD).")
|
2020-02-02 21:27:09 -05:00
|
|
|
builder_args(parser)
|
|
|
|
soc_core_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-11-12 12:07:28 -05:00
|
|
|
soc = BaseSoC(
|
2021-12-20 15:41:12 -05:00
|
|
|
bios_flash_offset = int(args.bios_flash_offset, 0),
|
2021-03-03 10:21:04 -05:00
|
|
|
sys_clk_freq = int(float(args.sys_clk_freq)),
|
|
|
|
with_video_terminal = args.with_video_terminal,
|
2020-11-12 12:07:28 -05:00
|
|
|
**soc_core_argdict(args)
|
|
|
|
)
|
2020-03-13 04:43:43 -04:00
|
|
|
builder = Builder(soc, **builder_argdict(args))
|
2020-05-05 09:11:38 -04:00
|
|
|
builder.build(run=args.build)
|
|
|
|
|
|
|
|
if args.load:
|
|
|
|
prog = soc.platform.create_programmer()
|
2020-05-21 03:12:29 -04:00
|
|
|
prog.load_bitstream(os.path.join(builder.gateware_dir, soc.build_name + ".bin"))
|
2020-05-05 09:11:38 -04:00
|
|
|
|
|
|
|
if args.flash:
|
2021-05-20 03:14:54 -04:00
|
|
|
flash(builder.output_dir, soc.build_name, args.bios_flash_offset)
|
2020-02-02 21:27:09 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|