2021-10-08 23:41:41 +08:00
|
|
|
from inspect import isclass, getmodule
|
|
|
|
from functools import wraps
|
|
|
|
import sys
|
|
|
|
from numpy import int32, int64
|
|
|
|
|
|
|
|
import nac3artiq
|
|
|
|
|
2021-10-31 17:16:21 +08:00
|
|
|
__all__ = ["KernelInvariant", "extern", "kernel", "portable", "ms", "us", "ns",
|
|
|
|
"Core", "TTLOut", "parallel", "sequential"]
|
2021-10-08 23:41:41 +08:00
|
|
|
|
|
|
|
|
2021-10-10 17:45:38 +08:00
|
|
|
import device_db
|
|
|
|
core_arguments = device_db.device_db["core"]["arguments"]
|
2021-10-08 23:41:41 +08:00
|
|
|
|
2021-10-10 17:45:38 +08:00
|
|
|
nac3 = nac3artiq.NAC3(core_arguments["target"])
|
2021-10-08 23:41:41 +08:00
|
|
|
allow_module_registration = True
|
|
|
|
registered_ids = set()
|
|
|
|
|
2021-10-08 23:46:46 +08:00
|
|
|
def KernelInvariant(t):
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
2021-10-08 23:41:41 +08:00
|
|
|
def register_module_of(obj):
|
|
|
|
global registered_ids
|
|
|
|
assert allow_module_registration
|
|
|
|
module = getmodule(obj)
|
|
|
|
module_id = id(module)
|
|
|
|
if module_id not in registered_ids:
|
|
|
|
nac3.register_module(module)
|
|
|
|
registered_ids.add(module_id)
|
|
|
|
|
|
|
|
|
|
|
|
def extern(function):
|
|
|
|
register_module_of(function)
|
|
|
|
return function
|
|
|
|
|
|
|
|
|
|
|
|
def kernel(class_or_function):
|
|
|
|
register_module_of(class_or_function)
|
|
|
|
if isclass(class_or_function):
|
|
|
|
return class_or_function
|
|
|
|
else:
|
|
|
|
@wraps(class_or_function)
|
|
|
|
def device_only(*args, **kwargs):
|
|
|
|
raise RuntimeError("Kernels must not be called directly, use core.run(kernel_function) instead")
|
|
|
|
return device_only
|
|
|
|
|
2021-10-10 16:26:01 +08:00
|
|
|
|
2021-10-08 23:41:41 +08:00
|
|
|
def portable(function):
|
|
|
|
register_module_of(function)
|
|
|
|
return function
|
|
|
|
|
|
|
|
|
|
|
|
def get_defined_class(method):
|
|
|
|
return vars(sys.modules[method.__module__])[method.__qualname__.split('.')[0]]
|
|
|
|
|
|
|
|
|
2021-10-10 17:45:38 +08:00
|
|
|
ms = 1e-3
|
|
|
|
us = 1e-6
|
|
|
|
ns = 1e-9
|
|
|
|
|
2021-10-08 23:41:41 +08:00
|
|
|
@extern
|
|
|
|
def rtio_init():
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
|
|
|
@extern
|
|
|
|
def rtio_get_counter() -> int64:
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
|
|
|
@extern
|
|
|
|
def rtio_output(target: int32, data: int32):
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
|
|
|
@extern
|
|
|
|
def rtio_input_timestamp(timeout_mu: int64, channel: int32) -> int64:
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
|
|
|
|
|
|
|
@extern
|
|
|
|
def rtio_input_data(channel: int32) -> int32:
|
|
|
|
raise NotImplementedError("syscall not simulated")
|
|
|
|
|
2021-10-31 17:16:21 +08:00
|
|
|
def at_mu(_):
|
|
|
|
raise NotImplementedError("at_mu not simulated")
|
|
|
|
|
|
|
|
def now_mu() -> int32:
|
|
|
|
raise NotImplementedError("now_mu not simulated")
|
|
|
|
|
|
|
|
def delay_mu(_):
|
|
|
|
raise NotImplementedError("delay_mu not simulated")
|
2021-10-08 23:41:41 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
class Core:
|
2021-10-10 17:45:38 +08:00
|
|
|
ref_period: float
|
|
|
|
|
2021-10-09 15:51:47 +08:00
|
|
|
def __init__(self):
|
2021-10-10 17:45:38 +08:00
|
|
|
self.ref_period = core_arguments["ref_period"]
|
2021-10-09 15:51:47 +08:00
|
|
|
|
2021-10-08 23:41:41 +08:00
|
|
|
def run(self, method, *args, **kwargs):
|
|
|
|
global allow_module_registration
|
|
|
|
if allow_module_registration:
|
|
|
|
nac3.analyze()
|
|
|
|
allow_module_registration = False
|
2021-10-10 16:26:01 +08:00
|
|
|
|
|
|
|
if hasattr(method, "__self__"):
|
|
|
|
obj = method.__self__
|
|
|
|
name = method.__name__
|
|
|
|
else:
|
|
|
|
obj = method
|
|
|
|
name = ""
|
|
|
|
|
|
|
|
nac3.compile_method(obj, name, args)
|
2021-10-08 23:41:41 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def reset(self):
|
|
|
|
rtio_init()
|
|
|
|
at_mu(rtio_get_counter() + int64(125000))
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def break_realtime(self):
|
|
|
|
min_now = rtio_get_counter() + int64(125000)
|
|
|
|
if now_mu() < min_now:
|
|
|
|
at_mu(min_now)
|
|
|
|
|
2021-10-10 17:45:38 +08:00
|
|
|
@portable
|
|
|
|
def seconds_to_mu(self, seconds: float) -> int64:
|
|
|
|
return int64(round(seconds/self.ref_period))
|
|
|
|
|
|
|
|
@portable
|
|
|
|
def mu_to_seconds(self, mu: int64) -> float:
|
|
|
|
return float(mu)*self.ref_period
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def delay(self, dt: float):
|
|
|
|
delay_mu(self.seconds_to_mu(dt))
|
|
|
|
|
2021-10-08 23:41:41 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
class TTLOut:
|
2021-10-10 17:45:38 +08:00
|
|
|
core: Core
|
2021-10-08 23:41:41 +08:00
|
|
|
channel: int32
|
|
|
|
target_o: int32
|
|
|
|
|
2021-10-09 15:51:47 +08:00
|
|
|
@portable
|
2021-10-10 17:45:38 +08:00
|
|
|
def __init__(self, core: Core, channel: int32):
|
|
|
|
self.core = core
|
2021-10-08 23:41:41 +08:00
|
|
|
self.channel = channel
|
|
|
|
self.target_o = channel << 8
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def output(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def set_o(self, o: bool):
|
|
|
|
rtio_output(self.target_o, 1 if o else 0)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def on(self):
|
|
|
|
self.set_o(True)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def off(self):
|
|
|
|
self.set_o(False)
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse_mu(self, duration: int64):
|
|
|
|
self.on()
|
|
|
|
delay_mu(duration)
|
|
|
|
self.off()
|
2021-10-10 17:45:38 +08:00
|
|
|
|
|
|
|
@kernel
|
|
|
|
def pulse(self, duration: float):
|
|
|
|
self.on()
|
|
|
|
self.core.delay(duration)
|
|
|
|
self.off()
|
2021-10-31 17:16:21 +08:00
|
|
|
|
|
|
|
@portable
|
|
|
|
class KernelContextManager:
|
|
|
|
@kernel
|
|
|
|
def __enter__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@kernel
|
|
|
|
def __exit__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
parallel = KernelContextManager()
|
|
|
|
sequential = KernelContextManager()
|
|
|
|
|