artiq/artiq/coredevice/core.py

148 lines
5.0 KiB
Python
Raw Normal View History

import os, sys
from pythonparser import diagnostic
from artiq import __artiq_dir__ as artiq_dir
2014-12-03 18:20:30 +08:00
from artiq.language.core import *
from artiq.language.types import *
2015-08-20 03:37:31 +08:00
from artiq.language.units import *
2014-12-03 18:20:30 +08:00
from artiq.compiler.module import Module
from artiq.compiler.embedding import Stitcher
from artiq.compiler.targets import OR1KTarget
# Import for side effects (creating the exception classes).
from artiq.coredevice import exceptions
def _render_diagnostic(diagnostic, colored):
def shorten_path(path):
return path.replace(artiq_dir, "<artiq>")
2016-01-27 07:23:35 +08:00
lines = [shorten_path(path) for path in diagnostic.render(colored=colored)]
return "\n".join(lines)
colors_supported = os.name == "posix"
class _DiagnosticEngine(diagnostic.Engine):
def render_diagnostic(self, diagnostic):
sys.stderr.write(_render_diagnostic(diagnostic, colored=colors_supported) + "\n")
class CompileError(Exception):
def __init__(self, diagnostic):
self.diagnostic = diagnostic
def __str__(self):
# Prepend a newline so that the message shows up on after
# exception class name printed by Python.
return "\n" + _render_diagnostic(self.diagnostic, colored=colors_supported)
2014-11-03 18:44:30 +08:00
2016-06-29 02:37:39 +08:00
@syscall
def rtio_init() -> TNone:
2016-06-29 02:37:39 +08:00
raise NotImplementedError("syscall not simulated")
@syscall(flags={"nounwind", "nowrite"})
def rtio_get_counter() -> TInt64:
raise NotImplementedError("syscall not simulated")
2015-07-14 04:08:20 +08:00
class Core:
"""Core device driver.
:param ref_period: period of the reference clock for the RTIO subsystem.
On platforms that use clock multiplication and SERDES-based PHYs,
this is the period after multiplication. For example, with a RTIO core
clocked at 125MHz and a SERDES multiplication factor of 8, the
reference period is 1ns.
The time machine unit is equal to this period.
:param external_clock: whether the core device should switch to its
external RTIO clock input instead of using its internal oscillator.
:param ref_multiplier: ratio between the RTIO fine timestamp frequency
and the RTIO coarse timestamp frequency (e.g. SERDES multiplication
factor).
:param comm_device: name of the device used for communications.
"""
kernel_invariants = {
2016-03-29 16:19:03 +08:00
"core", "ref_period", "coarse_ref_period", "ref_multiplier",
"external_clock",
}
def __init__(self, dmgr, ref_period, external_clock=False,
ref_multiplier=8, comm_device="comm"):
2015-07-14 04:08:20 +08:00
self.ref_period = ref_period
self.external_clock = external_clock
self.ref_multiplier = ref_multiplier
self.coarse_ref_period = ref_period*ref_multiplier
self.comm = dmgr.get(comm_device)
2014-12-03 18:20:30 +08:00
self.first_run = True
self.dmgr = dmgr
2014-11-21 04:38:52 +08:00
self.core = self
self.comm.core = self
def compile(self, function, args, kwargs, set_result=None, attribute_writeback=True):
try:
engine = _DiagnosticEngine(all_errors_are_fatal=True)
stitcher = Stitcher(engine=engine, core=self, dmgr=self.dmgr)
stitcher.stitch_call(function, args, kwargs, set_result)
2015-08-11 01:25:57 +08:00
stitcher.finalize()
module = Module(stitcher,
ref_period=self.ref_period,
attribute_writeback=attribute_writeback)
target = OR1KTarget()
2015-08-10 20:12:22 +08:00
library = target.compile_and_link([module])
stripped_library = target.strip(library)
2016-05-16 22:30:21 +08:00
return stitcher.embedding_map, stripped_library, \
lambda addresses: target.symbolize(library, addresses), \
lambda symbols: target.demangle(symbols)
except diagnostic.Error as error:
raise CompileError(error.diagnostic) from error
def run(self, function, args, kwargs):
result = None
def set_result(new_result):
nonlocal result
result = new_result
2016-05-16 22:30:21 +08:00
embedding_map, kernel_library, symbolizer, demangler = \
self.compile(function, args, kwargs, set_result)
2015-08-09 07:17:19 +08:00
if self.first_run:
self.comm.check_ident()
self.comm.switch_clock(self.external_clock)
self.first_run = False
2015-08-10 20:12:22 +08:00
self.comm.load(kernel_library)
self.comm.run()
2016-05-16 22:30:21 +08:00
self.comm.serve(embedding_map, symbolizer, demangler)
2014-11-21 04:38:52 +08:00
return result
2015-02-19 00:56:30 +08:00
@kernel
2015-07-02 04:22:53 +08:00
def get_rtio_counter_mu(self):
return rtio_get_counter()
2015-02-19 00:56:30 +08:00
2016-06-29 02:37:39 +08:00
@kernel
def reset(self):
"""Clear RTIO FIFOs, release RTIO PHY reset, and set the time cursor
at the current value of the hardware RTIO counter plus a margin of
125000 machine units."""
rtio_init()
at_mu(rtio_get_counter() + 125000)
2014-11-21 04:38:52 +08:00
@kernel
2015-05-03 20:42:42 +08:00
def break_realtime(self):
2016-06-29 02:37:39 +08:00
"""Set the time cursor after the current value of the hardware RTIO
counter plus a margin of 125000 machine units.
If the time cursor is already after that position, this function
does nothing."""
min_now = rtio_get_counter() + 125000
2015-08-17 23:41:21 +08:00
if now_mu() < min_now:
at_mu(min_now)