Skip to content

Commit

Permalink
[capture] Set num_segments to max
Browse files Browse the repository at this point in the history
If no num_segments is provided in the config file for Husky,
the scope driver automatically sets it to num_segments_max.

For WaveRunner, an error is raised if no num_segments max is
provided.

Closes lowRISC#277.

Signed-off-by: Pascal Nasahl <[email protected]>
  • Loading branch information
nasahlpa committed Jan 15, 2024
1 parent 0399c5c commit 6cf6140
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 46 deletions.
19 changes: 9 additions & 10 deletions capture/capture_aes.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,21 @@ def setup(cfg: dict, project: Path):

logger.info(f"Initializing scope {scope_type} with a sampling rate of {cfg[scope_type]['sampling_rate']}...") # noqa: E501

# Determine if we are in batch mode or not.
batch = False
if "batch" in cfg["test"]["which_test"]:
batch = True

# Create scope config & setup scope.
scope_cfg = ScopeConfig(
scope_type = scope_type,
batch_mode = batch,
acqu_channel = cfg[scope_type].get("channel"),
ip = cfg[scope_type].get("waverunner_ip"),
num_samples = cfg[scope_type]["num_samples"],
offset_samples = cfg[scope_type]["offset_samples"],
sampling_rate = cfg[scope_type].get("sampling_rate"),
num_segments = cfg[scope_type]["num_segments"],
num_segments = cfg[scope_type].get("num_segments"),
sparsing = cfg[scope_type].get("sparsing"),
scope_gain = cfg[scope_type].get("scope_gain"),
pll_frequency = cfg["target"]["pll_frequency"],
Expand Down Expand Up @@ -389,24 +395,17 @@ def main(argv=None):

# Determine the capture mode and configure the current capture.
mode = "aes_fvsr_key"
batch = False
if "aes_random" in cfg["test"]["which_test"]:
mode = "aes_random"
if "batch" in cfg["test"]["which_test"]:
batch = True
else:
# For non-batch mode, make sure that num_segments = 1.
cfg[cfg["capture"]["scope_select"]]["num_segments"] = 1
logger.info("num_segments needs to be 1 in non-batch mode. Setting num_segments=1.")

# Setup the target, scope and project.
target, scope, project = setup(cfg, args.project)

# Create capture config object.
capture_cfg = CaptureConfig(capture_mode = mode,
batch_mode = batch,
batch_mode = scope.scope_cfg.batch_mode,
num_traces = cfg["capture"]["num_traces"],
num_segments = cfg[cfg["capture"]["scope_select"]]["num_segments"],
num_segments = scope.scope_cfg.num_segments,
output_len = cfg["target"]["output_len_bytes"],
text_fixed = cfg["test"]["text_fixed"],
key_fixed = cfg["test"]["key_fixed"],
Expand Down
19 changes: 9 additions & 10 deletions capture/capture_kmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,21 @@ def setup(cfg: dict, project: Path):

logger.info(f"Initializing scope {scope_type} with a sampling rate of {cfg[scope_type]['sampling_rate']}...") # noqa: E501

# Determine if we are in batch mode or not.
batch = False
if "batch" in cfg["test"]["which_test"]:
batch = True

# Create scope config & setup scope.
scope_cfg = ScopeConfig(
scope_type = scope_type,
batch_mode = batch,
acqu_channel = cfg[scope_type].get("channel"),
ip = cfg[scope_type].get("waverunner_ip"),
num_samples = cfg[scope_type]["num_samples"],
offset_samples = cfg[scope_type]["offset_samples"],
sampling_rate = cfg[scope_type].get("sampling_rate"),
num_segments = cfg[scope_type]["num_segments"],
num_segments = cfg[scope_type].get("num_segments"),
sparsing = cfg[scope_type].get("sparsing"),
scope_gain = cfg[scope_type].get("scope_gain"),
pll_frequency = cfg["target"]["pll_frequency"],
Expand Down Expand Up @@ -423,24 +429,17 @@ def main(argv=None):

# Determine the capture mode and configure the current capture.
mode = "kmac_fvsr_key"
batch = False
if "kmac_random" in cfg["test"]["which_test"]:
mode = "kmac_random"
if "batch" in cfg["test"]["which_test"]:
batch = True
else:
# For non-batch mode, make sure that num_segments = 1.
cfg[cfg["capture"]["scope_select"]]["num_segments"] = 1
logger.info("num_segments needs to be 1 in non-batch mode. Setting num_segments=1.")

# Setup the target, scope and project.
target, scope, project = setup(cfg, args.project)

# Create capture config object.
capture_cfg = CaptureConfig(capture_mode = mode,
batch_mode = batch,
batch_mode = scope.scope_cfg.batch_mode,
num_traces = cfg["capture"]["num_traces"],
num_segments = cfg[cfg["capture"]["scope_select"]]["num_segments"],
num_segments = scope.scope_cfg.num_segments,
output_len = cfg["target"]["output_len_bytes"],
text_fixed = cfg["test"]["text_fixed"],
key_fixed = cfg["test"]["key_fixed"],
Expand Down
15 changes: 6 additions & 9 deletions capture/capture_otbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,13 @@ def setup(cfg: dict, project: Path):
f"Initializing scope {scope_type} with a sampling rate of {cfg[scope_type]['sampling_rate']}..." # noqa: E501
)

# Determine if we are in batch mode or not.
batch = cfg["test"]["batch_mode"]

# Create scope config & setup scope.
scope_cfg = ScopeConfig(
scope_type=scope_type,
batch_mode = batch,
acqu_channel=cfg[scope_type].get("channel"),
ip=cfg[scope_type].get("waverunner_ip"),
num_samples=cfg[scope_type]["num_samples"],
Expand Down Expand Up @@ -689,23 +693,16 @@ def main(argv=None):

# Determine the capture mode and configure the current capture.
mode = cfg["test"]["app"]
batch = cfg["test"]["batch_mode"]
if batch is False:
# For non-batch mode, make sure that num_segments = 1.
cfg[cfg["capture"]["scope_select"]]["num_segments"] = 1
logger.info(
"num_segments needs to be 1 in non-batch mode. Setting num_segments=1."
)

# Setup the target, scope and project.
target, scope, project = setup(cfg, args.project)

# Create capture config object.
capture_cfg = CaptureConfig(
capture_mode=mode,
batch_mode=batch,
batch_mode=scope.scope_cfg.batch_mode,
num_traces=cfg["capture"]["num_traces"],
num_segments=cfg[cfg["capture"]["scope_select"]]["num_segments"],
num_segments=scope.scope_cfg.num_segments,
output_len=cfg["target"]["output_len_bytes"],
text_fixed=bytearray(),
key_fixed=bytearray(),
Expand Down
19 changes: 9 additions & 10 deletions capture/capture_sha3.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,21 @@ def setup(cfg: dict, project: Path):

logger.info(f"Initializing scope {scope_type} with a sampling rate of {cfg[scope_type]['sampling_rate']}...") # noqa: E501

# Determine if we are in batch mode or not.
batch = False
if "batch" in cfg["test"]["which_test"]:
batch = True

# Create scope config & setup scope.
scope_cfg = ScopeConfig(
scope_type = scope_type,
batch_mode = batch,
acqu_channel = cfg[scope_type].get("channel"),
ip = cfg[scope_type].get("waverunner_ip"),
num_samples = cfg[scope_type]["num_samples"],
offset_samples = cfg[scope_type]["offset_samples"],
sampling_rate = cfg[scope_type].get("sampling_rate"),
num_segments = cfg[scope_type]["num_segments"],
num_segments = cfg[scope_type].get("num_segments"),
sparsing = cfg[scope_type].get("sparsing"),
scope_gain = cfg[scope_type].get("scope_gain"),
pll_frequency = cfg["target"]["pll_frequency"],
Expand Down Expand Up @@ -397,24 +403,17 @@ def main(argv=None):

# Determine the capture mode and configure the current capture.
mode = "sha3_fvsr_data"
batch = False
if "sha3_random" in cfg["test"]["which_test"]:
mode = "sha3_random"
if "batch" in cfg["test"]["which_test"]:
batch = True
else:
# For non-batch mode, make sure that num_segments = 1.
cfg[cfg["capture"]["scope_select"]]["num_segments"] = 1
logger.info("num_segments needs to be 1 in non-batch mode. Setting num_segments=1.")

# Setup the target, scope and project.
target, scope, project = setup(cfg, args.project)

# Create capture config object.
capture_cfg = CaptureConfig(capture_mode = mode,
batch_mode = batch,
batch_mode = scope.scope_cfg.batch_mode,
num_traces = cfg["capture"]["num_traces"],
num_segments = cfg[cfg["capture"]["scope_select"]]["num_segments"],
num_segments = scope.scope_cfg.num_segments,
output_len = cfg["target"]["output_len_bytes"],
text_fixed = cfg["test"]["text_fixed"],
text_len_bytes = cfg["test"]["text_len_bytes"],
Expand Down
21 changes: 16 additions & 5 deletions capture/scopes/chipwhisperer/husky.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@


class Husky:
def __init__(self, scope_gain, num_samples, num_segments, offset_samples,
sampling_rate, pll_frequency):
def __init__(self, scope_gain, batch_mode, num_samples, num_segments,
offset_samples, sampling_rate, pll_frequency):
check_version.check_husky("1.5.0")
self.scope = None
self.scope_gain = scope_gain
self.batch_mode = batch_mode
# In our setup, Husky operates on the PLL frequency of the target and
# multiplies that by an integer number to obtain the sampling rate.
# Note that the sampling rate must be at most 200 MHz.
Expand Down Expand Up @@ -73,18 +74,28 @@ def initialize_scope(self):
self.scope = scope

def configure_batch_mode(self):
if self.num_segments != 1:
if self.batch_mode:
self.scope = CwSegmented(num_samples=self.num_samples,
offset_samples=self.offset_samples,
scope_gain=self.scope.gain.db,
scope=self.scope,
clkgen_freq=self.scope.clock.clkgen_freq,
adc_mul=self.adc_mul)

# Determine max. possible number of segments.
num_segments_max = (self.scope._scope.adc.oa.hwMaxSegmentSamples //
self.scope._scope.adc.samples)

# If num_segments is not provided in the config file, set it to
# max. number of segments.
if self.num_segments is None:
self.num_segments = num_segments_max
print(
f'Info: num_segments not provided, setting to '
f'num_segments_max={num_segments_max}.')
self.scope.num_segments = self.num_segments
# Sanity check manually set num_segments. Check, if we can keep the
# num_segements * num_samples in memory.
num_segments_max = (self.scope._scope.adc.oa.hwMaxSegmentSamples //
self.scope._scope.adc.samples)
if (self.num_segments > num_segments_max):
raise RuntimeError("num_segments too large, cannot keep\
samples in CW Husky sample memory.")
Expand Down
25 changes: 23 additions & 2 deletions capture/scopes/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class ScopeConfig:
Stores information about the scope.
"""
scope_type: str
num_segments: int
batch_mode: bool
acqu_channel: Optional[str] = None
ip: Optional[str] = None
num_samples: Optional[int] = 0
offset_samples: Optional[int] = 0
num_segments: Optional[int] = 1
sparsing: Optional[int] = 1
scope_gain: Optional[float] = 1
pll_frequency: Optional[int] = 1
Expand All @@ -44,8 +45,12 @@ def _init_scope(self):
Configure Husky or WaveRunner scope with the user provided scope
settings.
"""
# Check if num_segments is 1 in non-batch mode.
self._sanitize_num_segments()
# Configure Scopes.
if self.scope_cfg.scope_type == "husky":
scope = Husky(scope_gain = self.scope_cfg.scope_gain,
batch_mode = self.scope_cfg.batch_mode,
num_samples = self.scope_cfg.num_samples,
num_segments = self.scope_cfg.num_segments,
offset_samples = self.scope_cfg.offset_samples,
Expand All @@ -54,6 +59,8 @@ def _init_scope(self):
)
scope.initialize_scope()
scope.configure_batch_mode()
# Update num_segments.
self.scope_cfg.num_segments = scope.num_segments
return scope
elif self.scope_cfg.scope_type == "waverunner":
if self.scope_cfg.ip:
Expand All @@ -63,11 +70,15 @@ def _init_scope(self):
setup_data = scope._ask("PANEL_SETUP?")
scope._get_and_print_cmd_error()
tmp_sampling_rate = int(re.findall(r'SampleRate = \d+', setup_data)[0][13:])
# Sanitize inputs.
if self.scope_cfg.sampling_rate is not None:
if self.scope_cfg.sampling_rate != tmp_sampling_rate:
raise RuntimeError("WAVERUNNER: Error: WaveRunner sampling "
"rate does not match given configuration!")

if self.scope_cfg.num_segments is None:
raise RuntimeError("WAVERUNNER: Error: num_segments needs to "
"be provided!")
# Configure WaveRunner.
scope.configure_waveform_transfer_general(
num_segments = self.scope_cfg.num_segments,
sparsing = self.scope_cfg.sparsing,
Expand All @@ -81,6 +92,16 @@ def _init_scope(self):
else:
raise RuntimeError("Error: Scope not supported!")

def _sanitize_num_segments(self) -> None:
""" Sanitize num_segments.
When in non-batch mode, num_segments needs to be 1.
"""
if not self.scope_cfg.batch_mode and self.scope_cfg.num_segments != 1:
self.scope_cfg.num_segments = 1
print("Warning: num_segments needs to be 1 in non-batch mode. "
"Setting num_segments=1.")

def arm(self) -> None:
""" Arm the scope.
"""
Expand Down

0 comments on commit 6cf6140

Please sign in to comment.