artiq/artiq/coredevice/core.py

84 lines
2.6 KiB
Python
Raw Normal View History

import os
from pythonparser import diagnostic
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 import Stitcher, Module
from artiq.compiler.targets import OR1KTarget
# Import for side effects (creating the exception classes).
from artiq.coredevice import exceptions
class CompileError(Exception):
def __init__(self, diagnostic):
self.diagnostic = diagnostic
def render_string(self, colored=False):
def shorten_path(path):
return path.replace(os.path.normpath(os.path.join(__file__, "..", "..")), "<artiq>")
lines = [shorten_path(path) for path in self.diagnostic.render(colored=colored)]
return "\n".join(lines)
def __str__(self):
# Prepend a newline so that the message shows up on after
# exception class name printed by Python.
return "\n" + self.render_string(colored=True)
2014-11-03 18:44:30 +08:00
@syscall
def rtio_get_counter() -> TInt64:
raise NotImplementedError("syscall not simulated")
2015-07-14 04:08:20 +08:00
class Core:
def __init__(self, dmgr, ref_period=8*ns, external_clock=False):
self.comm = dmgr.get("comm")
self.ref_period = ref_period
self.external_clock = external_clock
2014-12-03 18:20:30 +08:00
self.first_run = True
2014-11-21 04:38:52 +08:00
self.core = self
self.comm.core = self
def compile(self, function, args, kwargs, with_attr_writeback=True):
try:
engine = diagnostic.Engine(all_errors_are_fatal=True)
stitcher = Stitcher(engine=engine)
stitcher.stitch_call(function, args, kwargs)
2015-08-11 01:25:57 +08:00
stitcher.finalize()
module = Module(stitcher, ref_period=self.ref_period)
target = OR1KTarget()
2015-08-10 20:12:22 +08:00
library = target.compile_and_link([module])
stripped_library = target.strip(library)
return stitcher.object_map, stripped_library, \
2015-08-10 20:12:22 +08:00
lambda addresses: target.symbolize(library, addresses)
except diagnostic.Error as error:
raise CompileError(error.diagnostic) from error
def run(self, function, args, kwargs):
object_map, kernel_library, symbolizer = self.compile(function, args, kwargs)
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()
self.comm.serve(object_map, symbolizer)
2014-11-21 04:38:52 +08:00
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
2014-11-21 04:38:52 +08:00
@kernel
2015-05-03 20:42:42 +08:00
def break_realtime(self):
at_mu(rtio_get_counter() + 125000)