<commit_before># -*- coding: utf-8 -*-

# Copyright (c) 2017-2020, XESS Corp. The MIT License (MIT).

from __future__ import absolute_import, division, print_function, unicode_literals

import functools
from builtins import dict, int, str, super

from future import standard_library
from nmigen.sim import Tick

from ..peekerbase import *

standard_library.install_aliases()


class Peeker(PeekerBase):

    def __init__(self, signal, name=None):

        # Get the name from the signal if it's not explicitly given.
        if not name:
            name = signal.name

        super().__init__(signal, name)

        # Set the width of the signal.
        self.trace.num_bits = len(signal)

    @classmethod
    def assign_simulator(cls, simulator):
        """Assign nmigen Simulator to all Peekers."""

        def peek_process():
            while True:
                for peeker in cls.peekers():
                    peeker.trace.store_sample((yield peeker.signal), simulator._engine.now)
                yield Tick()

        simulator.add_process(peek_process)
<commit_msg>Sampling nmigen signals now uses a VCD writer.<commit_after># -*- coding: utf-8 -*-

# Copyright (c) 2017-2020, XESS Corp. The MIT License (MIT).

from __future__ import absolute_import, division, print_function, unicode_literals

import functools
from builtins import dict, int, str, super

from future import standard_library

from ..peekerbase import *

standard_library.install_aliases()


class Peeker(PeekerBase):

    def __init__(self, signal, name=None):

        # Get the name from the signal if it's not explicitly given.
        if not name:
            name = signal.name

        super().__init__(signal, name)

        # Set the width of the signal.
        self.trace.num_bits = len(signal)

    @classmethod
    def assign_simulator(cls, simulator):
        """Capture traces by assigning Peeker as a VCD Writer to nmigen."""

        simulator._engine._vcd_writers.append(cls)

    @classmethod
    def update(cls, time, signal, value):
        """Called during VCD writing to record signal value."""

        for peeker in cls.peekers():
            if id(peeker.signal) == id(signal):
                print(f"store: {peeker.trace.name} {value} @ {time}")
                peeker.trace.store_sample(value, time)
                return

{'fraction_non_alphanumeric': 0.09464668094218416, 'fraction_numerical': 0.007708779443254818, 'mean_word_length': 2.6729559748427674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2820683', 'n_tokens_mistral': 724, 'n_tokens_neox': 657, 'n_words': 215}