Skip to content

Commit 89960fd

Browse files
committed
Style fixes for GowinPlatform
- div_frac.numerator instead of int(div_frac) for clarity - `import re` as in other platforms - use `re.match` instead of `re.search`+`len(match.groups())==n` - use r"" for regexps for syntax highlighting and to ensure invalid escapes do not break on later Python versions
1 parent cba831b commit 89960fd

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

amaranth/vendor/gowin.py

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import abstractproperty
22
from fractions import Fraction
3-
from re import search
3+
import re
44

55
from ..hdl import *
66
from ..lib.cdc import ResetSynchronizer
@@ -47,16 +47,17 @@ class GowinPlatform(TemplatedPlatform):
4747

4848
def parse_part(self):
4949
# These regular expressions match all >900 parts of Gowin device_info.csv
50-
reg_series = "(GW[12]{1}[AN]{1}[EFNRSZ]{0,3})-"
51-
reg_voltage = "(ZV|EV|LV|LX|UV|UX)"
52-
reg_size = "(1|2|4|9|18|55)"
53-
reg_subseries = "(?:(B|C|S|X|P5)?)"
54-
reg_package = "((?:PG|UG|EQ|LQ|MG|M|QN|CS|FN)(?:\d+)(?:P?)(?:A|E|M|CF|C|D|G|H|F|S|T|U|X)?)"
55-
reg_speed = "((?:C\d{1}/I\d{1})|ES|A\d{1}|I\d{1})"
56-
57-
match = search(reg_series+reg_voltage+reg_size+reg_subseries+reg_package+reg_speed, self.part)
58-
if len(match.groups()) != 6:
59-
raise ValueError("Supplied part name is invalid.")
50+
reg_series = r"(GW[12]{1}[AN]{1}[EFNRSZ]{0,3})-"
51+
reg_voltage = r"(ZV|EV|LV|LX|UV|UX)"
52+
reg_size = r"(1|2|4|9|18|55)"
53+
reg_subseries = r"(?:(B|C|S|X|P5)?)"
54+
reg_package = r"((?:PG|UG|EQ|LQ|MG|M|QN|CS|FN)(?:\d+)(?:P?)(?:A|E|M|CF|C|D|G|H|F|S|T|U|X)?)"
55+
reg_speed = r"((?:C\d{1}/I\d{1})|ES|A\d{1}|I\d{1})"
56+
57+
match = re.match(reg_series+reg_voltage+reg_size+reg_subseries+reg_package+reg_speed+"$",
58+
self.part)
59+
if not match:
60+
raise ValueError("Supplied part name is invalid")
6061

6162
self.series = match.group(1)
6263
self.voltage = match.group(2)
@@ -65,9 +66,9 @@ def parse_part(self):
6566
self.package = match.group(5)
6667
self.speed = match.group(6)
6768

68-
match = search(reg_series+reg_size+reg_subseries, self.family)
69-
if len(match.groups()) != 3:
70-
raise ValueError("Supplied device family name is invalid.")
69+
match = re.match(reg_series+reg_size+reg_subseries+"$", self.family)
70+
if not match:
71+
raise ValueError("Supplied device family name is invalid")
7172

7273
self.series_f = match.group(1)
7374
self.size_f = match.group(2)
@@ -76,9 +77,11 @@ def parse_part(self):
7677
# subseries_f is usually more reliable than subseries.
7778

7879
if self.series != self.series_f:
79-
raise ValueError("Series extracted from supplied part name does not match supplied family series")
80+
raise ValueError("Series extracted from supplied part name does not match "
81+
"supplied family series")
8082
if self.size != self.size_f:
81-
raise ValueError("Size extracted from supplied part name does not match supplied family size")
83+
raise ValueError("Size extracted from supplied part name does not match "
84+
"supplied family size")
8285

8386
# _chipdb_device is tied to available chipdb-*.bin files of nextpnr-gowin
8487
@property
@@ -172,7 +175,7 @@ def _osc_div(self):
172175
"steps of {}"
173176
.format(div_frac.numerator, div_frac.denominator, div_min, div_max, div_step))
174177

175-
return int(div_frac)
178+
return div_frac.numerator
176179

177180
# Common templates
178181

0 commit comments

Comments
 (0)