Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions vivado/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_verilog//verilog:defs.bzl", "VerilogModuleInfo")


VivadoInfo = provider(
doc = "Info pertaining to Vivado target.",
fields = ["part"],
)


def _vivado_bitstream_impl(ctx):
name, ext = paths.split_extension(ctx.label.name)
if not ext:
ext = ".bit"
ext = ".bit"

common_args = ["-p", ctx.attr.part]

Expand All @@ -20,13 +18,14 @@ def _vivado_bitstream_impl(ctx):
synth_args = ctx.actions.args()
synth_args.add("synth")
synth_args.add_all(common_args)
synth_args.add_all("--tcl", ctx.files.pre_synth_tcl)
synth_args.add("-t", ctx.attr.module[VerilogModuleInfo].top)
synth_args.add_all("-v", ctx.attr.module[VerilogModuleInfo].files)
synth_args.add("-o", post_synth)

ctx.actions.run(
outputs = [post_synth],
inputs = ctx.attr.module[VerilogModuleInfo].files,
inputs = ctx.files.pre_synth_tcl + ctx.attr.module[VerilogModuleInfo].files.to_list(),
executable = ctx.attr._vivado_client[DefaultInfo].files_to_run,
arguments = [synth_args],
mnemonic = "VivadoSynth",
Expand All @@ -38,13 +37,14 @@ def _vivado_bitstream_impl(ctx):
place_args = ctx.actions.args()
place_args.add("place")
place_args.add_all(common_args)
place_args.add_all("--tcl", ctx.files.pre_place_tcl)
place_args.add_all("-c", ctx.files.io_constraints)
place_args.add("-i", post_synth)
place_args.add("-o", post_place)

ctx.actions.run(
outputs = [post_place],
inputs = ctx.files.io_constraints + [post_synth],
inputs = ctx.files.io_constraints + [post_synth] + ctx.files.pre_place_tcl,
executable = ctx.attr._vivado_client[DefaultInfo].files_to_run,
arguments = [place_args],
mnemonic = "VivadoPlace",
Expand All @@ -56,12 +56,13 @@ def _vivado_bitstream_impl(ctx):
route_args = ctx.actions.args()
route_args.add("route")
route_args.add_all(common_args)
route_args.add_all("--tcl", ctx.files.pre_route_tcl)
route_args.add("-i", post_place)
route_args.add("-o", post_route)

ctx.actions.run(
outputs = [post_route],
inputs = [post_place],
inputs = [post_place] + ctx.files.pre_route_tcl,
executable = ctx.attr._vivado_client[DefaultInfo].files_to_run,
arguments = [route_args],
mnemonic = "VivadoRoute",
Expand Down Expand Up @@ -92,7 +93,6 @@ def _vivado_bitstream_impl(ctx):
VivadoInfo(part = ctx.attr.part),
]


vivado_bitstream = rule(
implementation = _vivado_bitstream_impl,
doc = "Generate Vivado bitstream.",
Expand All @@ -112,6 +112,18 @@ vivado_bitstream = rule(
allow_empty = False,
allow_files = [".xdc"],
),
"pre_synth_tcl": attr.label_list(
doc = "Tcl sources to run before synthesis.",
allow_files = [".tcl"],
),
"pre_place_tcl": attr.label_list(
doc = "Tcl sources to run before placement.",
allow_files = [".tcl"],
),
"pre_route_tcl": attr.label_list(
doc = "Tcl sources to run before routing.",
allow_files = [".tcl"],
),
"bitstream_constraints": attr.label_list(
doc = "Xilinx constraints for bitstream generation.",
mandatory = True,
Expand All @@ -127,11 +139,10 @@ vivado_bitstream = rule(
},
)


def _vivado_config_memory_impl(ctx):
name, ext = paths.split_extension(ctx.label.name)
if not ext:
ext = ".bin"
ext = ".bin"

config_memory = ctx.actions.declare_file("{}{}".format(name, ext))

Expand All @@ -156,7 +167,6 @@ def _vivado_config_memory_impl(ctx):
ctx.attr.bitstream[VivadoInfo],
]


vivado_config_memory = rule(
implementation = _vivado_config_memory_impl,
doc = "Generate Vivado configuration memory.",
Expand All @@ -178,8 +188,16 @@ vivado_config_memory = rule(
doc = "Configuration memory interface.",
mandatory = True,
values = [
"SMAPx8", "SMAPx16", "SMAPx32", "SERIALx1",
"SPIx1", "SPIx2", "SPIx4", "SPIx8", "BPIx8", "BPIx16"
"SMAPx8",
"SMAPx16",
"SMAPx32",
"SERIALx1",
"SPIx1",
"SPIx2",
"SPIx4",
"SPIx8",
"BPIx8",
"BPIx16",
],
),
"_vivado_client": attr.label(
Expand All @@ -191,19 +209,18 @@ vivado_config_memory = rule(
},
)


def _vivado_load_impl(ctx):
shell_cmd = [
ctx.executable._vivado_client.short_path,
"load",
"-p",
ctx.attr.bitstream[VivadoInfo].part,
"-i",
" ".join([f.short_path for f in ctx.attr.bitstream[DefaultInfo].files.to_list()])
" ".join([f.short_path for f in ctx.attr.bitstream[DefaultInfo].files.to_list()]),
]

script = ctx.actions.declare_file("{}.sh".format(ctx.label.name))
ctx.actions.write(script, ' '.join(shell_cmd), is_executable = True)
ctx.actions.write(script, " ".join(shell_cmd), is_executable = True)

runfiles = ctx.runfiles(
transitive_files = depset(
Expand All @@ -215,7 +232,6 @@ def _vivado_load_impl(ctx):
)
return [DefaultInfo(executable = script, runfiles = runfiles)]


vivado_load = rule(
implementation = _vivado_load_impl,
doc = "Load bitstream onto part.",
Expand All @@ -239,7 +255,6 @@ vivado_load = rule(
executable = True,
)


def _vivado_flash_impl(ctx):
shell_cmd = [
ctx.executable._vivado_client.short_path,
Expand All @@ -249,11 +264,11 @@ def _vivado_flash_impl(ctx):
"--memory",
ctx.attr.memory_pn,
"-i",
" ".join([f.short_path for f in ctx.attr.config[DefaultInfo].files.to_list()])
" ".join([f.short_path for f in ctx.attr.config[DefaultInfo].files.to_list()]),
]

script = ctx.actions.declare_file("{}.sh".format(ctx.label.name))
ctx.actions.write(script, ' '.join(shell_cmd), is_executable = True)
ctx.actions.write(script, " ".join(shell_cmd), is_executable = True)

runfiles = ctx.runfiles(
transitive_files = depset(
Expand All @@ -265,7 +280,6 @@ def _vivado_flash_impl(ctx):
)
return [DefaultInfo(executable = script, runfiles = runfiles)]


vivado_flash = rule(
implementation = _vivado_flash_impl,
doc = "Load configuration memory onto part.",
Expand Down Expand Up @@ -293,9 +307,15 @@ vivado_flash = rule(
executable = True,
)


def vivado_project(name, module, part, io_constraints, bitstream_constraints, memory_size,
memory_interface, memory_pn):
def vivado_project(
name,
module,
part,
io_constraints,
bitstream_constraints,
memory_size,
memory_interface,
memory_pn):
vivado_bitstream(
name = "{}.bit".format(name),
module = module,
Expand Down
Loading