aboutsummaryrefslogtreecommitdiffstats
path: root/manual/CHAPTER_Approach.tex
blob: 4b170ee0ae8f1d0d631121d9dd90ecdebaffaedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
\chapter{Approach}
\label{chapter:approach}

Yosys is a tool for synthesising (behavioural) Verilog HDL code to target architecture netlists. Yosys aims at a wide
range of application domains and thus must be flexible and easy to adapt to new tasks. This chapter covers the general
approach followed in the effort to implement this tool.

\section{Data- and Control-Flow}

The data- and control-flow of a typical synthesis tool is very similar to the data- and control-flow of a typical
compiler: different subsystems are called in a predetermined order, each consuming the data generated by the
last subsystem and generating the data for the next subsystem (see Fig.~\ref{fig:approach_flow}).

\begin{figure}[b]
	\hfil
	\begin{tikzpicture}
		\path (-1.5,3) coordinate (cursor);
		\draw[-latex] ($ (cursor) + (0,-1.5) $) -- ++(1,0);
		\draw[fill=orange!10] ($ (cursor) + (1,-3) $) rectangle node[rotate=90] {Frontend} ++(1,3) coordinate (cursor);
		\draw[-latex] ($ (cursor) + (0,-1.5) $) -- ++(1,0);
		\draw[fill=green!10] ($ (cursor) + (1,-3) $) rectangle node[rotate=90] {Pass} ++(1,3) coordinate (cursor);
		\draw[-latex] ($ (cursor) + (0,-1.5) $) -- ++(1,0);
		\draw[fill=green!10] ($ (cursor) + (1,-3) $) rectangle node[rotate=90] {Pass} ++(1,3) coordinate (cursor);
		\draw[-latex] ($ (cursor) + (0,-1.5) $) -- ++(1,0);
		\draw[fill=green!10] ($ (cursor) + (1,-3) $) rectangle node[rotate=90] {Pass} ++(1,3) coordinate (cursor);
		\draw[-latex] ($ (cursor) + (0,-1.5) $) -- ++(1,0);
		\draw[fill=orange!10] ($ (cursor) + (1,-3) $) rectangle node[rotate=90] {Backend} ++(1,3) coordinate (cursor);
		\draw[-latex] ($ (cursor) + (0,-1.5) $) -- ++(1,0);

		\path (-3,-0.5) coordinate (cursor);
		\draw (cursor) -- node[below] {HDL} ++(3,0) coordinate (cursor);
		\draw[|-|] (cursor) -- node[below] {Internal Format(s)} ++(8,0) coordinate (cursor);
		\draw (cursor) -- node[below] {Netlist} ++(3,0);

		\path (-3,3.5) coordinate (cursor);
		\draw[-] (cursor) -- node[above] {High-Level} ++(3,0) coordinate (cursor);
		\draw[-] (cursor) -- ++(8,0) coordinate (cursor);
		\draw[->] (cursor) -- node[above] {Low-Level} ++(3,0);

	\end{tikzpicture}
	\caption{General data- and control-flow of a synthesis tool}
	\label{fig:approach_flow}
\end{figure}

The first subsystem to be called is usually called a {\it frontend}. It does not process the data generated by
another subsystem but instead reads the user input---in the case of a HDL synthesis tool, the behavioural
HDL code.

The subsystems that consume data from previous subsystems and produce data for the next subsystems (usually in the
same or a similar format) are called {\it passes}.

The last subsystem that is executed transforms the data generated by the last pass into a suitable output
format and writes it to a disk file. This subsystem is usually called the {\it backend}.

In Yosys all frontends, passes and backends are directly available as commands in the synthesis script. Thus
the user can easily create a custom synthesis flow just by calling passes in the right order in a synthesis
script.

\section{Internal Formats in Yosys}

Yosys uses two different internal formats. The first is used to store an abstract syntax tree (AST) of a Verilog
input file. This format is simply called {\it AST} and is generated by the Verilog Frontend. This data structure
is consumed by a subsystem called {\it AST Frontend}\footnote{In Yosys the term {\it pass} is only used to
refer to commands that operate on the RTLIL data structure.}. This AST Frontend then generates a design in Yosys'
main internal format, the Register-Transfer-Level-Intermediate-Language (RTLIL) representation. It does that
by first performing a number of simplifications within the AST representation and then generating RTLIL from
the simplified AST data structure.

The RTLIL representation is used by all passes as input and outputs. This has the following advantages over
using different representational formats between different passes:

\begin{itemize}
\item The passes can be rearranged in a different order and passes can be removed or inserted.
\item Passes can simply pass-thru the parts of the design they don't change without the need
      to convert between formats. In fact Yosys passes output the same data structure they received
      as input and performs all changes in place.
\item All passes use the same interface, thus reducing the effort required to understand a pass
      when reading the Yosys source code, e.g.~when adding additional features.
\end{itemize}

The RTLIL representation is basically a netlist representation with the following additional features:

\begin{itemize}
\item An internal cell library with fixed-function cells to represent RTL datapath and register cells as well
as logical gate-level cells (single-bit gates and registers).
\item Support for multi-bit values that can use individual bits from wires as well as constant bits to
represent coarse-grain netlists.
\item Support for basic behavioural constructs (if-then-else structures and multi-case switches with
a sensitivity list for updating the outputs).
\item Support for multi-port memories.
\end{itemize}

The use of RTLIL also has the disadvantage of having a very powerful format
between all passes, even when doing gate-level synthesis where the more
advanced features are not needed. In order to reduce complexity for passes that
operate on a low-level representation, these passes check the features used in
the input RTLIL and fail to run when unsupported high-level constructs are
used. In such cases a pass that transforms the higher-level constructs to
lower-level constructs must be called from the synthesis script first.

\section{Typical Use Case}
\label{sec:typusecase}

The following example script may be used in a synthesis flow to convert the behavioural Verilog code
from the input file {\tt design.v} to a gate-level netlist {\tt synth.v} using the cell library
described by the Liberty file \citeweblink{LibertyFormat} {\tt cells.lib}:

\begin{lstlisting}[language=sh,numbers=left,frame=single]
# read input file to internal representation
read_verilog design.v

# convert high-level behavioral parts ("processes") to d-type flip-flops and muxes
proc

# perform some simple optimizations
opt

# convert high-level memory constructs to d-type flip-flops and multiplexers
memory

# perform some simple optimizations
opt

# convert design to (logical) gate-level netlists
techmap

# perform some simple optimizations
opt

# map internal register types to the ones from the cell library
dfflibmap -liberty cells.lib

# use ABC to map remaining logic to cells from the cell library
abc -liberty cells.lib

# cleanup
opt

# write results to output file
write_verilog synth.v
\end{lstlisting}

A detailed description of the commands available in Yosys can be found in App.~\ref{commandref}.
gs_t sts = 0; /* Note, SR register bit definitions are equal for all UARTs so using the UART1 definitions is fine.*/ if (sr & UART1_SR_OR) sts |= SD_OVERRUN_ERROR; if (sr & UART1_SR_NF) sts |= SD_NOISE_ERROR; if (sr & UART1_SR_FE) sts |= SD_FRAMING_ERROR; if (sr & UART1_SR_PE) sts |= SD_PARITY_ERROR; chSysLockFromIsr(); chIOAddFlagsI(sdp, sts); chSysUnlockFromIsr(); } #if STM8S_SERIAL_USE_UART1 || defined(__DOXYGEN__) static void notify1(GenericQueue *qp) { (void)qp; UART1->CR2 |= UART1_CR2_TIEN; } /** * @brief UART1 initialization. * * @param[in] config architecture-dependent serial driver configuration */ static void uart1_init(const SerialConfig *config) { UART1->BRR2 = (uint8_t)(((uint8_t)(config->sc_brr >> 8) & (uint8_t)0xF0) | ((uint8_t)config->sc_brr & (uint8_t)0x0F)); UART1->BRR1 = (uint8_t)(config->sc_brr >> 4); UART1->CR1 = (uint8_t)(config->sc_mode & SD_MODE_PARITY); /* PIEN included. */ UART1->CR2 = UART1_CR2_RIEN | UART1_CR2_TEN | UART1_CR2_REN; UART1->CR3 = (uint8_t)(config->sc_mode & SD_MODE_STOP); UART1->CR4 = 0; UART1->CR5 = 0; UART1->PSCR = 1; (void)UART1->SR; (void)UART1->DR; } /** * @brief UART1 de-initialization. */ static void uart1_deinit(void) { UART1->CR1 = UART1_CR1_UARTD; UART1->CR2 = 0; UART1->CR3 = 0; UART1->CR4 = 0; UART1->CR5 = 0; UART1->PSCR = 0; } #endif /* STM8S_SERIAL_USE_UART1 */ #if STM8S_SERIAL_USE_UART2 || defined(__DOXYGEN__) static void notify2(GenericQueue *qp) { (void)qp; UART2->CR2 |= UART2_CR2_TIEN; } /** * @brief UART2 initialization. * * @param[in] config architecture-dependent serial driver configuration */ static void uart2_init(const SerialConfig *config) { UART2->BRR2 = (uint8_t)(((uint8_t)(config->sc_brr >> 8) & (uint8_t)0xF0) | ((uint8_t)config->sc_brr & (uint8_t)0x0F)); UART2->BRR1 = (uint8_t)(config->sc_brr >> 4); UART2->CR1 = (uint8_t)(config->sc_mode & SD_MODE_PARITY); /* PIEN included. */ UART2->CR2 = UART2_CR2_RIEN | UART2_CR2_TEN | UART2_CR2_REN; UART2->CR3 = (uint8_t)(config->sc_mode & SD_MODE_STOP); UART2->CR4 = 0; UART2->CR5 = 0; UART2->CR6 = 0; UART2->PSCR = 1; (void)UART2->SR; (void)UART2->DR; } /** * @brief UART1 de-initialization. */ static void uart2_deinit(void) { UART2->CR1 = UART2_CR1_UARTD; UART2->CR2 = 0; UART2->CR3 = 0; UART2->CR4 = 0; UART2->CR5 = 0; UART2->CR6 = 0; UART2->PSCR = 0; } #endif /* STM8S_SERIAL_USE_UART1 */ #if STM8S_SERIAL_USE_UART3 || defined(__DOXYGEN__) static void notify3(GenericQueue *qp) { (void)qp; UART3->CR2 |= UART3_CR2_TIEN; } /** * @brief UART3 initialization. * * @param[in] config architecture-dependent serial driver configuration */ static void uart3_init(const SerialConfig *config) { UART3->BRR2 = (uint8_t)(((uint8_t)(config->sc_brr >> 8) & (uint8_t)0xF0) | ((uint8_t)config->sc_brr & (uint8_t)0x0F)); UART3->BRR1 = (uint8_t)(config->sc_brr >> 4); UART3->CR1 = (uint8_t)(config->sc_mode & SD_MODE_PARITY); /* PIEN included. */ UART3->CR2 = UART3_CR2_RIEN | UART3_CR2_TEN | UART3_CR2_REN; UART3->CR3 = (uint8_t)(config->sc_mode & SD_MODE_STOP); UART3->CR4 = 0; UART3->CR6 = 0; (void)UART3->SR; (void)UART3->DR; } /** * @brief UART3 de-initialization. */ static void uart3_deinit(void) { UART3->CR1 = UART3_CR1_UARTD; UART3->CR2 = 0; UART3->CR3 = 0; UART3->CR4 = 0; UART3->CR6 = 0; } #endif /* STM8S_SERIAL_USE_UART3 */ /*===========================================================================*/ /* Driver interrupt handlers. */ /*===========================================================================*/ #if STM8S_SERIAL_USE_UART1 || defined(__DOXYGEN__) /** * @brief IRQ 17 service routine. * * @isr */ CH_IRQ_HANDLER(17) { msg_t b; CH_IRQ_PROLOGUE(); chSysLockFromIsr(); b = sdRequestDataI(&SD1); chSysUnlockFromIsr(); if (b < Q_OK) UART1->CR2 &= (uint8_t)~UART1_CR2_TIEN; else UART1->DR = (uint8_t)b; CH_IRQ_EPILOGUE(); } /** * @brief IRQ 18 service routine. * * @isr */ CH_IRQ_HANDLER(18) { uint8_t sr = UART1->SR; CH_IRQ_PROLOGUE(); if ((sr = UART1->SR) & (UART1_SR_OR | UART1_SR_NF | UART1_SR_FE | UART1_SR_PE)) set_error(&SD1, sr); chSysLockFromIsr(); sdIncomingDataI(&SD1, UART1->DR); chSysUnlockFromIsr(); CH_IRQ_EPILOGUE(); } #endif /* STM8S_SERIAL_USE_UART1 */ #if STM8S_SERIAL_USE_UART2 || defined(__DOXYGEN__) /** * @brief IRQ 20 service routine. * * @isr */ CH_IRQ_HANDLER(20) { msg_t b; CH_IRQ_PROLOGUE(); chSysLockFromIsr(); b = sdRequestDataI(&SD2); chSysUnlockFromIsr(); if (b < Q_OK) UART2->CR2 &= (uint8_t)~UART2_CR2_TIEN; else UART2->DR = (uint8_t)b; CH_IRQ_EPILOGUE(); } /** * @brief IRQ 21 service routine. * * @isr */ CH_IRQ_HANDLER(21) { uint8_t sr = UART2->SR; CH_IRQ_PROLOGUE(); if ((sr = UART2->SR) & (UART2_SR_OR | UART2_SR_NF | UART2_SR_FE | UART2_SR_PE)) set_error(&SD2, sr); chSysLockFromIsr(); sdIncomingDataI(&SD2, UART2->DR); chSysUnlockFromIsr(); CH_IRQ_EPILOGUE(); } #endif /* STM8S_SERIAL_USE_UART2 */ #if STM8S_SERIAL_USE_UART3 || defined(__DOXYGEN__) /** * @brief IRQ 20 service routine. * * @isr */ CH_IRQ_HANDLER(20) { msg_t b; CH_IRQ_PROLOGUE(); chSysLockFromIsr(); b = sdRequestDataI(&SD3); chSysUnlockFromIsr(); if (b < Q_OK) UART3->CR2 &= (uint8_t)~UART3_CR2_TIEN; else UART3->DR = (uint8_t)b; CH_IRQ_EPILOGUE(); } /** * @brief IRQ 21 service routine. * * @isr */ CH_IRQ_HANDLER(21) { uint8_t sr = UART3->SR; CH_IRQ_PROLOGUE(); if ((sr = UART3->SR) & (UART3_SR_OR | UART3_SR_NF | UART3_SR_FE | UART3_SR_PE)) set_error(&SD3, sr); chSysLockFromIsr(); sdIncomingDataI(&SD3, UART3->DR); chSysUnlockFromIsr(); CH_IRQ_EPILOGUE(); } #endif /* STM8S_SERIAL_USE_UART3 */ /*===========================================================================*/ /* Driver exported functions. */ /*===========================================================================*/ /** * @brief Low level serial driver initialization. * * @notapi */ void sd_lld_init(void) { #if STM8S_SERIAL_USE_UART1 sdObjectInit(&SD1, NULL, notify1); CLK->PCKENR1 |= CLK_PCKENR1_UART1; /* PCKEN12, clock source. */ UART1->CR1 = UART1_CR1_UARTD; /* UARTD (low power). */ #endif #if STM8S_SERIAL_USE_UART2 sdObjectInit(&SD2, NULL, notify2); CLK->PCKENR1 |= CLK_PCKENR1_UART2; /* PCKEN13, clock source. */ UART2->CR1 = UART2_CR1_UARTD; /* UARTD (low power). */ #endif #if STM8S_SERIAL_USE_UART3 sdObjectInit(&SD3, NULL, notify3); CLK->PCKENR1 |= CLK_PCKENR1_UART3; /* PCKEN13, clock source. */ UART3->CR1 = UART3_CR1_UARTD; /* UARTD (low power). */ #endif } /** * @brief Low level serial driver configuration and (re)start. * * @param[in] sdp pointer to a @p SerialDriver object * @param[in] config the architecture-dependent serial driver configuration. * If this parameter is set to @p NULL then a default * configuration is used. * * @notapi */ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config) { if (config == NULL) config = &default_config; #if STM8S_SERIAL_USE_UART1 if (&SD1 == sdp) { uart1_init(config); return; } #endif #if STM8S_SERIAL_USE_UART2 if (&SD2 == sdp) { uart2_init(config); return; } #endif #if STM8S_SERIAL_USE_UART3 if (&SD3 == sdp) { uart3_init(config); return; } #endif } /** * @brief Low level serial driver stop. * @details De-initializes the USART, stops the associated clock, resets the * interrupt vector. * * @param[in] sdp pointer to a @p SerialDriver object * * @notapi */ void sd_lld_stop(SerialDriver *sdp) { #if STM8S_SERIAL_USE_UART1 if (&SD1 == sdp) { uart1_deinit(); return; } #endif #if STM8S_SERIAL_USE_UART2 if (&SD2 == sdp) { uart2_deinit(); return; } #endif #if STM8S_SERIAL_USE_UART3 if (&SD3 == sdp) { uart3_deinit(); return; } #endif } #endif /* HAL_USE_SERIAL */ /** @} */