Move StateGen to own file, and ran make format.

Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
This commit is contained in:
Keith Rothman 2019-02-18 10:02:48 -08:00
parent d925198650
commit 4be219f7e5
3 changed files with 75 additions and 54 deletions

View File

@ -31,20 +31,18 @@ def main():
for param in ('PRESELECT_I0', ):
segmk.add_site_tag(
row['site'], '{}.Z{}'.format(base_name, param),
1 ^ row[param])
row['site'], '{}.Z{}'.format(base_name, param), 1 ^ row[param])
for param in ('PRESELECT_I1', ):
segmk.add_site_tag(
row['site'], '{}.{}'.format(base_name, param), row[param])
for param, tag in (('IS_CE0_INVERTED', 'ZINV_CE0'),
('IS_S0_INVERTED', 'ZINV_S0'),
('IS_CE1_INVERTED', 'ZINV_CE1'),
('IS_S1_INVERTED', 'ZINV_S1')):
for param, tag in (('IS_CE0_INVERTED', 'ZINV_CE0'), ('IS_S0_INVERTED',
'ZINV_S0'),
('IS_CE1_INVERTED', 'ZINV_CE1'), ('IS_S1_INVERTED',
'ZINV_S1')):
segmk.add_site_tag(
row['site'], '{}.{}'.format(base_name, tag),
1 ^ row[param])
row['site'], '{}.{}'.format(base_name, tag), 1 ^ row[param])
segmk.compile()
segmk.write()

View File

@ -2,8 +2,9 @@ import json
import os
import random
random.seed(int(os.getenv("SEED"), 16))
from prjxray import util
from prjxray.db import Database
from prjxray import util
from prjxray.state_gen import StateGen
def gen_sites():
@ -28,50 +29,6 @@ def gen_sites():
if sites:
yield tile_name, min(xs), min(ys), sorted(sites)
class StateGen(object):
def __init__(self, sites, states_per_site):
self.sites = sites
self.states_per_site = states_per_site
self.curr_site_idx = 0
self.curr_state = None
self.states = None
self.curr_site = None
def __iter__(self):
assert self.curr_state is None
assert self.states is None
assert self.curr_state is None
self.curr_site_idx = 0
self.curr_state = None
self.states = util.gen_fuzz_states(len(self.sites)*self.states_per_site)
self.curr_site = iter(self.sites)
return self
def __next__(self):
next_site = next(self.curr_site)
self.curr_site_idx += 1
if self.curr_state is not None:
while self.curr_state < self.states_per_site:
self.next_state()
assert self.curr_state == self.states_per_site, self.curr_state
self.curr_state = 0
return next_site
def next_state(self):
self.curr_state += 1
try:
state = next(self.states)
except StopIteration:
assert False, "Insufficent states, at state {} for site {}".format(self.curr_state, self.curr_site_idx)
return state
def main():
print('''
@ -79,7 +36,7 @@ module top();
''')
params_list = []
state_gen = StateGen(list(gen_sites()), states_per_site=12*16)
state_gen = StateGen(list(gen_sites()), states_per_site=12 * 16)
for tile_name, x_min, y_min, sites in state_gen:
for site, x, y in sites:
params = {}

66
prjxray/state_gen.py Normal file
View File

@ -0,0 +1,66 @@
from prjxray import util
class StateGen(object):
""" Manages fuzzer state generation across multiple sites.
sites - List of sites.
states_per_site - Maximum number of states used per site.
If states_per_site is too small, next_state may throw AssertionError.
StateGen should be used as a iterator for the sites given. Call next_state
within each site output loop. Once 'next' is called on StateGen, StateGen
will advance state output to the correct position, even if next_state was
called less than states_per_site.
"""
def __init__(self, sites, states_per_site):
self.sites = sites
self.states_per_site = states_per_site
self.curr_site_idx = 0
self.curr_state = None
self.states = None
self.curr_site = None
def __iter__(self):
assert self.curr_state is None
assert self.states is None
assert self.curr_state is None
self.curr_site_idx = 0
self.curr_state = None
self.states = util.gen_fuzz_states(
len(self.sites) * self.states_per_site)
self.curr_site = iter(self.sites)
return self
def __next__(self):
next_site = next(self.curr_site)
self.curr_site_idx += 1
if self.curr_state is not None:
while self.curr_state < self.states_per_site:
self.next_state()
assert self.curr_state == self.states_per_site, self.curr_state
self.curr_state = 0
return next_site
def next_state(self):
""" Returns next state within site.
Should only be called states_per_site for each site.
"""
self.curr_state += 1
try:
state = next(self.states)
except StopIteration:
assert False, "Insufficent states, at state {} for site {}".format(
self.curr_state, self.curr_site_idx)
return state