Skip to content

Commit

Permalink
add cologneChip GateMate toolchain support
Browse files Browse the repository at this point in the history
  • Loading branch information
trabucayre authored and olofk committed Jul 6, 2022
1 parent 2b81736 commit 2aaf23b
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 0 deletions.
112 changes: 112 additions & 0 deletions edalize/gatemate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Copyright edalize contributors
# Licensed under the 2-Clause BSD License, see LICENSE for details.
# SPDX-License-Identifier: BSD-2-Clause

import os.path
import re

from edalize.edatool import Edatool
from edalize.utils import EdaCommands
from edalize.yosys import Yosys


class Gatemate(Edatool):

argtypes = ["vlogdefine", "vlogparam"]

@classmethod
def get_doc(cls, api_ver):
if api_ver == 0:
options = {
"lists": [
{
"name": "p_r_options",
"type": "string",
"desc": "Additional option for p_r",
},
],
"members": [
{
"name": "device",
"type": "String",
"desc": "Required device option for p_r command (e.g. CCGM1A1)",
},
],
}

Edatool._extend_options(options, Yosys)

return {
"description": "backend for CologneChip GateMate.",
"members": options["members"],
"lists": options["lists"],
}

def configure_main(self):
(src_files, incdirs) = self._get_fileset_files()
synth_out = self.name + "_synth.v"

device = self.tool_options.get("device")
if not device:
raise RuntimeError("Missing required option 'device' for p_r")

match = re.search("^CCGM1A([1-9]{1,2})$", device)
if not match:
raise RuntimeError("{} is not known device name".format(device))

device_number = match.groups()[0]
if device_number not in ["1", "2", "4", "9", "16", "25"]:
raise RuntimeError("Rel. size {} is not unsupported".format(device_number))

ccf_file = None

for f in src_files:
if f.file_type == "CCF":
if ccf_file:
raise RuntimeError(
"p_r only supports one ccf file. Found {} and {}".format(
ccf_file, f.name
)
)
else:
ccf_file = f.name

# Pass trellis tool options to yosys
self.edam["tool_options"] = {
"yosys": {
"arch": "gatemate",
"output_format": "verilog",
"output_name": synth_out,
"yosys_synth_options": self.tool_options.get("yosys_synth_options", []),
"yosys_as_subtool": True,
"yosys_template": self.tool_options.get("yosys_template"),
},
}

yosys = Yosys(self.edam, self.work_root)
yosys.configure()

# Write Makefile
commands = EdaCommands()
commands.commands = yosys.commands

# PnR & image generation
targets = self.name + "_00.cfg.bit"
command = [
"p_r",
"-A",
device_number,
"-i",
synth_out,
"-o",
self.name,
"-lib",
"ccag",
" ".join(self.tool_options.get("p_r_options", "")),
]
if ccf_file is not None:
command += ["-ccf", ccf_file]
commands.add(command, [targets], [synth_out])

commands.set_default_target(targets)
commands.write(os.path.join(self.work_root, "Makefile"))
11 changes: 11 additions & 0 deletions tests/mock_commands/p_r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3
import os
import sys

output_file = sys.argv[sys.argv.index("-o") + 1]
with open(output_file, "a"):
# set the access and modified times to the current time
os.utime(output_file, None)

with open("p_r.cmd", "w") as f:
f.write(" ".join(sys.argv[1:]) + "\n")
97 changes: 97 additions & 0 deletions tests/test_gatemate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import os
import pytest
from edalize_common import make_edalize_test


def run_gatemate_test(tf):
tf.backend.configure()

tf.compare_files(
["Makefile", "edalize_yosys_procs.tcl", "edalize_yosys_template.tcl"]
)

tf.backend.build()
tf.compare_files(["yosys.cmd", "p_r.cmd"])


def test_gatemate(make_edalize_test):
tool_options = {
"device": "CCGM1A1",
"yosys_synth_options": ["some", "yosys_synth_options"],
"p_r_options": ["some", "p_r_synth_options"],
}
tf = make_edalize_test(
"gatemate", param_types=["vlogdefine", "vlogparam"], tool_options=tool_options
)

run_gatemate_test(tf)


def test_gatemate_minimal(make_edalize_test):
tool_options = {
"device": "CCGM1A1",
}
tf = make_edalize_test(
"gatemate",
param_types=[],
files=[],
tool_options=tool_options,
ref_dir="minimal",
)

run_gatemate_test(tf)


def test_gatemate_multiple_ccf(make_edalize_test):
files = [
{"name": "ccf_file.ccf", "file_type": "CCF"},
{"name": "ccf_file2.ccf", "file_type": "CCF"},
]
tool_options = {
"device": "CCGM1A1",
}
tf = make_edalize_test(
"gatemate",
param_types=[],
files=files,
tool_options=tool_options,
)

with pytest.raises(RuntimeError) as e:
tf.backend.configure()
assert (
"p_r only supports one ccf file. Found ccf_file.ccf and ccf_file2.ccf"
in str(e.value)
)


def test_gatemate_no_device(make_edalize_test):
tf = make_edalize_test("gatemate", param_types=[])

with pytest.raises(RuntimeError) as e:
tf.backend.configure()
assert "Missing required option 'device' for p_r" in str(e.value)


def test_gatemate_wrong_device(make_edalize_test):
tool_options = {
"device": "CCGM2A1",
}

tf = make_edalize_test("gatemate", param_types=[], tool_options=tool_options)

with pytest.raises(RuntimeError) as e:
tf.backend.configure()
assert "CCGM2A1 is not known device name" in str(e.value)


def test_gatemate_wrong_device_size(make_edalize_test):
tool_options = {
"device": "CCGM1A13",
}

tf = make_edalize_test("gatemate", param_types=[], tool_options=tool_options)

with pytest.raises(RuntimeError) as e:
tf.backend.configure()
assert "Rel. size 13 is not unsupported" in str(e.value)
9 changes: 9 additions & 0 deletions tests/test_gatemate/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#Auto generated by Edalize

all: test_gatemate_0_00.cfg.bit

test_gatemate_0_synth.v: edalize_yosys_template.tcl
$(EDALIZE_LAUNCHER) yosys -l yosys.log -p 'tcl edalize_yosys_template.tcl'

test_gatemate_0_00.cfg.bit: test_gatemate_0_synth.v
$(EDALIZE_LAUNCHER) p_r -A 1 -i test_gatemate_0_synth.v -o test_gatemate_0 -lib ccag some p_r_synth_options
31 changes: 31 additions & 0 deletions tests/test_gatemate/edalize_yosys_procs.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
proc read_files {} {
read_verilog -sv {sv_file.sv}
source {tcl_file.tcl}
read_verilog {vlog_file.v}
read_verilog {vlog05_file.v}
read_verilog -sv {another_sv_file.sv}
}

proc set_defines {} {
set defines {{vlogdefine_bool True} {vlogdefine_int 42} {vlogdefine_str hello}}

foreach d ${defines} {
set key [lindex $d 0]
set val [lindex $d 1]
verilog_defines "-D$key=$val"
}}

proc set_incdirs {} {
verilog_defaults -add -I.}

proc set_params {} {
chparam -set vlogparam_bool 1 top_module
chparam -set vlogparam_int 42 top_module
chparam -set vlogparam_str {"hello"} top_module}

proc synth {top} {
synth_gatemate some yosys_synth_options -top $top
}

set top top_module
set name test_gatemate_0
16 changes: 16 additions & 0 deletions tests/test_gatemate/edalize_yosys_template.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
yosys -import
source edalize_yosys_procs.tcl

verilog_defaults -push
verilog_defaults -add -defer

set_defines
set_incdirs
read_files
set_params

verilog_defaults -pop

synth $top

write_verilog test_gatemate_0_synth.v
9 changes: 9 additions & 0 deletions tests/test_gatemate/minimal/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#Auto generated by Edalize

all: test_gatemate_0_00.cfg.bit

test_gatemate_0_synth.v: edalize_yosys_template.tcl
$(EDALIZE_LAUNCHER) yosys -l yosys.log -p 'tcl edalize_yosys_template.tcl'

test_gatemate_0_00.cfg.bit: test_gatemate_0_synth.v
$(EDALIZE_LAUNCHER) p_r -A 1 -i test_gatemate_0_synth.v -o test_gatemate_0 -lib ccag
25 changes: 25 additions & 0 deletions tests/test_gatemate/minimal/edalize_yosys_procs.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
proc read_files {} {

}

proc set_defines {} {
set defines {}

foreach d ${defines} {
set key [lindex $d 0]
set val [lindex $d 1]
verilog_defines "-D$key=$val"
}}

proc set_incdirs {} {
}

proc set_params {} {
}

proc synth {top} {
synth_gatemate -top $top
}

set top top_module
set name test_gatemate_0
16 changes: 16 additions & 0 deletions tests/test_gatemate/minimal/edalize_yosys_template.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
yosys -import
source edalize_yosys_procs.tcl

verilog_defaults -push
verilog_defaults -add -defer

set_defines
set_incdirs
read_files
set_params

verilog_defaults -pop

synth $top

write_verilog test_gatemate_0_synth.v
1 change: 1 addition & 0 deletions tests/test_gatemate/minimal/p_r.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-A 1 -i test_gatemate_0_synth.v -o test_gatemate_0 -lib ccag
1 change: 1 addition & 0 deletions tests/test_gatemate/minimal/yosys.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-l yosys.log -p tcl edalize_yosys_template.tcl
1 change: 1 addition & 0 deletions tests/test_gatemate/p_r.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-A 1 -i test_gatemate_0_synth.v -o test_gatemate_0 -lib ccag some p_r_synth_options
1 change: 1 addition & 0 deletions tests/test_gatemate/yosys.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-l yosys.log -p tcl edalize_yosys_template.tcl

0 comments on commit 2aaf23b

Please sign in to comment.