Skip to content

Commit

Permalink
Style fixes for GowinPlatform
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
whitequark committed Aug 8, 2023
1 parent cba831b commit 89960fd
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions amaranth/vendor/gowin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import abstractproperty
from fractions import Fraction
from re import search
import re

from ..hdl import *
from ..lib.cdc import ResetSynchronizer
Expand Down Expand Up @@ -47,16 +47,17 @@ class GowinPlatform(TemplatedPlatform):

def parse_part(self):
# These regular expressions match all >900 parts of Gowin device_info.csv
reg_series = "(GW[12]{1}[AN]{1}[EFNRSZ]{0,3})-"
reg_voltage = "(ZV|EV|LV|LX|UV|UX)"
reg_size = "(1|2|4|9|18|55)"
reg_subseries = "(?:(B|C|S|X|P5)?)"
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)?)"
reg_speed = "((?:C\d{1}/I\d{1})|ES|A\d{1}|I\d{1})"

match = search(reg_series+reg_voltage+reg_size+reg_subseries+reg_package+reg_speed, self.part)
if len(match.groups()) != 6:
raise ValueError("Supplied part name is invalid.")
reg_series = r"(GW[12]{1}[AN]{1}[EFNRSZ]{0,3})-"
reg_voltage = r"(ZV|EV|LV|LX|UV|UX)"
reg_size = r"(1|2|4|9|18|55)"
reg_subseries = r"(?:(B|C|S|X|P5)?)"
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)?)"
reg_speed = r"((?:C\d{1}/I\d{1})|ES|A\d{1}|I\d{1})"

match = re.match(reg_series+reg_voltage+reg_size+reg_subseries+reg_package+reg_speed+"$",
self.part)
if not match:
raise ValueError("Supplied part name is invalid")

self.series = match.group(1)
self.voltage = match.group(2)
Expand All @@ -65,9 +66,9 @@ def parse_part(self):
self.package = match.group(5)
self.speed = match.group(6)

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

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

if self.series != self.series_f:
raise ValueError("Series extracted from supplied part name does not match supplied family series")
raise ValueError("Series extracted from supplied part name does not match "
"supplied family series")
if self.size != self.size_f:
raise ValueError("Size extracted from supplied part name does not match supplied family size")
raise ValueError("Size extracted from supplied part name does not match "
"supplied family size")

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

return int(div_frac)
return div_frac.numerator

# Common templates

Expand Down

0 comments on commit 89960fd

Please sign in to comment.