Automatic Code Generation for Synchronous Reactive Communication * Guoqiang Wang University of California Berkeley, CA, USA geraldw@eecs.berkeley.edu Marco Di Natale Scuola Superiore Sant’Anna Pisa, Italy marco@sssup.it Pieter J. Mosterman The MathWorks Natick, MA, USA pieter.mosterman@mathworks.com Alberto Sangiovanni-Vincentelli University of California Berkeley, CA, USA alberto@eecs.berkeley.edu Abstract Synchronous Reactive models are used in Model-Based Design to define embedded control applications. The ad- vantage of Model-Based Design is that system proper- ties can be verified on the model and applied to its soft- ware implementation if the translation of the model into code preserves its semantics. In this paper, we present an automatic code generation framework for the semantics- preserving implementation of communication in multi-rate systems. The proposed solution applies to the widely used MATLAB R and Simulink R products. It leverages the Tar- get Language Compiler template language of Real-Time Workshop R and extends the applicability of available com- mercial code generators. The overhead in memory of the presented solution is analyzed and compared with other im- plementations. 1 Introduction Model-Based Design (MBD) for embedded real-time software aims at fostering reuse and improving quality by capturing the functionality of the design with a model of computation and deriving implementation code from this model automatically. Further, MBD enables design-time simulation and verification of properties on models. Among the MBD formalisms to capture design functionality, Syn- chronous Reactive (SR) models [1] are effectively used in the design of hardware logic and for modeling control- dominated embedded applications. SR models are very * This work was supported by the MARCO/DARPA Gigascale Systems Research Center (GSRC). Their support is gratefully acknowledged. Now with National Instruments Corporation. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trade- marks. popular because they permit the use of tools for fast simula- tion and formal verification of the system properties. They are typically characterized by the “perfect synchrony hy- pothesis”, which requires that the system completes the re- action to an event before the occurrence of any other event. When implementing a high-level SR model into code, it is important to preserve its semantics, so to retain the sim- ulation and verification results. At run time, a model of communicating SR functional blocks may be implemented by a concurrent program, in which tasks have a finite ex- ecution time and are possibly subject to preemption. Se- mantics preservation requires that the run-time behavior is provably equivalent to the “zero execution time” behavior of the model. This property subsumes the property that model communication flows are preserved in the program imple- mentation (stream preservation) [2]. In general, demon- strating that the implementation respects the semantics of the SR specification is non-trivial. This work addresses SR models that exhibit a subset of the full Simulink R functionality. Here, a system is a net- work of blocks communicating through ports. A block can be a reader, a writer, or both. All blocks in the model react in response to events that may be periodic, defining a sampling rate, but may also be aperiodic. Each block computes two functions: the output and the state update functions. Ac- cording to the synchronous assumption, the computation is considered to occur in zero time, meaning that the outputs and the state depend instantaneously on the input values. This implies that the input values must be determined at the time the block is activated. For each block b i , let b i (j) and a i (j) be its j th in- stance and the corresponding activation time, respectively. Under the SR semantics, given that the execution time is zero, a i (j) captures also the start and the finish times of b i (j). Assume writer w communicates with reader r i . Let 2009 International Conferences on Embedded Software and Systems 978-0-7695-3678-1/09 $25.00 © 2009 IEEE DOI 10.1109/ICESS.2009.70 40 2009 International Conferences on Embedded Software and Systems 978-0-7695-3678-1/09 $25.00 © 2009 IEEE DOI 10.1109/ICESS.2009.70 40 2009 International Conferences on Embedded Software and Systems 978-0-7695-3678-1/09 $25.00 © 2009 IEEE DOI 10.1109/ICESS.2009.70 40 2009 International Conferences on Embedded Software and Systems 978-0-7695-3678-1/09 $25.00 © 2009 IEEE DOI 10.1109/ICESS.2009.70 40 2009 International Conferences on Embedded Software and Systems 978-0-7695-3678-1/09 $25.00 © 2009 IEEE DOI 10.1109/ICESS.2009.70 40 2009 International Conference on Embedded Software and Systems 978-0-7695-3678-1/09 $25.00 © 2009 IEEE DOI 10.1109/ICESS.2009.70 40