\documentclass[12pt,a4paper]{article}

\usepackage{amstext}
%\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{stmaryrd}
\usepackage{enumerate}
\usepackage{alltt}
\usepackage[latin1]{inputenc}
\usepackage{cancel}
\usepackage{fancyhdr}
\usepackage{listings}
\usepackage{gastex}
\usepackage{pstricks}
\usepackage{pst-node}
%\usepackage{ngerman}

\title{\textsc{Embedded Systems \\ \large Assignment 1}}
\author{\shortstack[c]{Jan Hendrik Dithmar\\ \scriptsize 2031259} \and \shortstack[c]{Pascal Gwosdek\\ \scriptsize 2505221}}
\date{}

\pagestyle{fancy}

\lhead{Assignment 1}
\chead{}
\rhead{Dithmar (2031259), Gwosdek (2505221)}
\lfoot{Embedded Systems}
\cfoot{Page \thepage}
\rfoot{SS 2007}

\begin{document}

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

\newcommand{\exercise}[1]{\subsection{#1}}
\renewcommand{\theenumi}{\textbf{\alph{enumi}}}

\newcommand{\AND}{\ensuremath{\wedge}}
\newcommand{\OR}{\ensuremath{\vee}}
\newcommand{\SetR}{\ensuremath{\mathbb{R}}}
\newcommand{\SetN}{\ensuremath{\mathbb{N}}}
\newcommand{\SetC}{\ensuremath{\mathbb{C}}}
\newcommand{\SetZ}{\ensuremath{\mathbb{Z}}}
\newcommand{\SetQ}{\ensuremath{\mathbb{Q}}}
\newcommand{\SetB}{\ensuremath{\mathbb{B}}}
\newcommand{\QED}{\ensuremath{\hspace*{\fill}\square}}
\newcommand{\FLASH}{\ensuremath{\hspace*{\fill}\lightning}}
\newcommand{\OnotO}{\ensuremath{\mathcal{O}}}
\newcommand{\bild}[4]{
	\begin{figure}[htpb]
		\centering
			\includegraphics[width=#1cm]{#2}\\[0.2cm]
			\caption{#3}
			\label{#4}
	\end{figure}
	}

\newcommand{\m}[1]{\begin{pmatrix}#1\end{pmatrix}}
\newcommand{\+}{\phantom{-}}
\newcommand{\f}{\frac}

\maketitle

\thispagestyle{empty}

\newpage
\setcounter{page}{1}
\setcounter{section}{1}

\exercise{A Vending Machine}
\begin{enumerate}
\item 
\begin{itemize}
\item Initially, the automata are in states 0 and A.
\item The user inserts a coin, the environment generates the event \textsf{coin\_in}, $A_1$ moves to state 1 and the event \textsf{ok} is generated.
\item $A_2$ consumes the event \textsf{ok} and moves to state B.
\item The user requests a tea, the environment generates the event \textsf{req\_tea}, $A_2$ moves to state D and the event \textsf{start\_tea} is generated.
\item The event \textsf{drink\_ready} is generated by the environment, $A_2$ moves to state A and the event \textsf{done} is generated.
\item $A_1$ consumes the event \textsf{done} and moves to state 0.
\end{itemize}

\item Imagine that the user presses the cancel-button while the coffee or tea is prepared, then the user can get the drink and the money out of
the machine, because there is no lock on the cancel-button while the drink is being prepared.

\item \ \\
\begin{center}
\begin{picture}(140,80)(0,0)
\gasset{Nadjust=w,Nadjustdist=2,Nh=6,Nmr=1}
\node[Nmarks=ir](0A)(10,50){\sffamily 0A}
\node(1A)(40,50){\sffamily 1A}
\node(1B)(70,50){\sffamily 1B}
\node(1C)(100,70){\sffamily 1C}
\node(1D)(100,30){\sffamily 1D}
\node(0C)(130,70){\sffamily 0C}
\node(0D)(130,30){\sffamily 0D}

\drawedge(0A,1A){\scriptsize \sffamily coin\_in}
\drawedge(1A,1B){\scriptsize \sffamily ok}
\drawedge[curvedepth=5](1B,0A){\scriptsize \sffamily cancel}
\drawedge[ELside=r](1B,1C){\scriptsize \sffamily req\_coffee}
\drawedge(1B,1D){\scriptsize \sffamily req\_tea}
\drawedge(1C,0C){\scriptsize \sffamily cancel}
\drawedge(1D,0D){\scriptsize \sffamily cancel}
\drawedge[curvedepth=-10](1C,0A){\scriptsize \sffamily drink\_ready}
\drawedge[curvedepth=-25,ELside=r](0C,0A){\scriptsize \sffamily drink\_ready}
\drawedge[curvedepth=10,ELside=r](1D,0A){\scriptsize \sffamily drink\_ready}
\drawedge[curvedepth=25](0D,0A){\scriptsize \sffamily drink\_ready}
\end{picture}
\newpage
\end{center}
\item \ \\
\begin{center}
\begin{picture}(140,190)(0,0)
\gasset{Nadjust=w,Nadjustdist=2,Nh=6,Nmr=1}
\drawoval(50,140,100,120,5)
\drawoval(50,180,90,30,5)
\node[Nframe=n](A1)(10,190){\sffamily $A_1$}
\node[Nmarks=r](0)(15,180){\sffamily 0}
\node(1)(50,180){\sffamily 1}
\node(2)(80,180){\sffamily 2}
\drawoval(50,120,90,65,5)
\node[Nframe=n](A2)(10,147){\sffamily $A_2$}
\node[Nmarks=r](A)(15,120){\sffamily A}
\node(B)(50,120){\sffamily B}
\node(C)(80,145){\sffamily C}
\node(D)(80,95){\sffamily D}

\drawedge[syo=1,eyo=1](0,1){\scriptsize \sffamily coin\_in/ok}
\drawedge[syo=-1,eyo=-1](1,0){\scriptsize \sffamily cancel/coin\_out, reset}
\drawedge(1,2){\scriptsize \sffamily req/}
\drawedge[curvedepth=10,sxo=6,exo=-6](2,0){\scriptsize \sffamily done/}

\drawedge[syo=1,eyo=1](A,B){\scriptsize \sffamily ok/}
\drawedge[syo=-1,eyo=-1](B,A){\scriptsize \sffamily reset/}
\drawedge[ELside=r](B,C){\scriptsize \sffamily req\_coffee/start\_coffee, req}
\drawedge(B,D){\scriptsize \sffamily req\_tea/start\_tea, req}
\drawedge[syo=1,eyo=1,ELside=r](C,A){\scriptsize \sffamily drink\_ready/done}
\drawedge[syo=-1,eyo=-1](D,A){\scriptsize \sffamily drink\_ready/done}

\node[Nframe=n](coinin)(18,210){\scriptsize \sffamily coin\_in}
\node[Nframe=n](cancel)(48,210){\scriptsize \sffamily cancel}
\node[Nframe=n](coinout)(78,210){\scriptsize \sffamily coin\_out}
\node[Nframe=n](coinin1)(18,192){}
\node[Nframe=n](cancel1)(48,192){}
\node[Nframe=n](coinout1)(78,192){}

\drawedge(coinin,coinin1){}
\drawedge(cancel,cancel1){}
\drawedge(coinout,coinout1){}

\node[Nframe=n](oka)(18,168){}
\node[Nframe=n](oka1)(18,150){}
\node[Nframe=n](reseta)(38,168){}
\node[Nframe=n](reseta1)(38,150){}
\node[Nframe=n](donea)(58,168){}
\node[Nframe=n](donea1)(58,150){}
\node[Nframe=n](reqa)(78,168){}
\node[Nframe=n](reqa1)(78,150){}

\drawedge(oka,oka1){\scriptsize \sffamily ok}
\drawedge(reseta,reseta1){\scriptsize \sffamily reset}
\drawedge[ELside=r](donea1,donea){\scriptsize \sffamily done}
\drawedge[ELside=r](reqa1,reqa){\scriptsize \sffamily req}

\node[Nframe=n](reqsth)(18,70){\scriptsize \sffamily req\_coffee, req\_tea}
\node[Nframe=n](reqsth1)(18,90){}
\node[Nframe=n](drinkready)(48,70){\scriptsize \sffamily drink\_ready}
\node[Nframe=n](drinkready1)(48,90){}
\node[Nframe=n](startsth)(78,70){\scriptsize \sffamily start\_coffee, start\_tea}
\node[Nframe=n](startsth1)(78,90){}

\drawedge(reqsth,reqsth1){}
\drawedge(drinkready,drinkready1){}
\drawedge(startsth1,startsth){}
\end{picture}
\end{center}
\item \ \\
\begin{center}
\begin{picture}(140,165)(0,25)
\gasset{Nadjust=w,Nadjustdist=2,Nh=6,Nmr=1}
\drawoval(50,120,100,160,5)
\drawoval(50,160,90,70,5)
\node[Nframe=n](A1)(10,190){\sffamily $A_1$}
\node[Nmarks=r](0)(55,180){\sffamily 0}
\node(1)(85,180){\sffamily 1}
\drawoval(50,80,90,65,5)
\node[Nframe=n](A2)(10,107){\sffamily $A_2$}
\node[Nmarks=r](A)(15,80){\sffamily A}
\node(B)(50,80){\sffamily B}
\node(C)(80,105){\sffamily C}
\node(D)(80,55){\sffamily D}
\node(50)(65,140){\sffamily 50}
\node(20)(45,140){\sffamily 20}
\node(10)(25,140){\sffamily 10}
\node(5)(15,160){\sffamily 5}

\drawloop[loopangle=60](0){\tiny \sffamily (*)}
\drawedge(0,1){\tiny \sffamily req/}
\drawloop[loopangle=300,ELdist=-5](50){\tiny \sffamily [x $\geqslant$ 50]/x:=x-50, coin\_out\_50}
\drawedge[ELside=r](50,20){\tiny \sffamily [x $<$ 50]/}
\drawloop[loopangle=90,ELdist=4](20){\tiny \sffamily [x $\geqslant$ 20]/x:=x-20,}
\drawloop[loopangle=90](20){\tiny \sffamily coin\_out\_20}
\drawedge[ELside=r](20,10){\tiny \sffamily [x $<$ 20]/}
\drawloop[loopangle=240,ELpos=39](10){\tiny \sffamily [x $\geqslant$ 10]/x:=x-10, coin\_out\_10}
\drawedge[ELside=r](10,5){\tiny \sffamily [x $<$ 10]/}
\drawloop[loopangle=90,ELpos=58,ELdist=-2](5){\tiny \sffamily [x $\geqslant$ 5]/x:=x-5,coin\_out\_5}
\drawedge[ELside=r](5,0){\tiny \sffamily x=0/}
\drawedge[curvedepth=8](1,50){\tiny \sffamily done/}
\drawedge(0,50){\tiny \sffamily cancel/reset}

\drawedge[syo=1,eyo=1](A,B){\scriptsize \sffamily [x $>$ 0]/}
\drawedge[syo=-1,eyo=-1](B,A){\scriptsize \sffamily reset/}
\drawedge[ELpos=80,ELside=r](B,C){\scriptsize \sffamily req\_coffee [x $\geqslant$ 75]/}
\drawedge[ELpos=70,ELside=r](B,C){\scriptsize \sffamily start\_coffee, req, x:=x-75}
\drawedge[ELpos=55](B,D){\scriptsize \sffamily req\_tea [x $\geqslant$ 50]/}
\drawedge[ELpos=75](B,D){\scriptsize \sffamily start\_tea, req, x:=x-50}
\drawedge[syo=1,eyo=1,ELside=r](C,A){\scriptsize \sffamily drink\_ready/done}
\drawedge[syo=-1,eyo=-1](D,A){\scriptsize \sffamily drink\_ready/done}

\node[Nframe=n](coinin5)(10,210){\scriptsize \sffamily coin\_in\_5}
\node[Nframe=n](coinins)(20,210){\scriptsize \sffamily ...}
\node[Nframe=n](coinin50)(30,210){\scriptsize \sffamily coin\_in\_50}
\node[Nframe=n](cancel)(50,210){\scriptsize \sffamily cancel}
\node[Nframe=n](coinout5)(70,210){\scriptsize \sffamily coin\_out\_5}
\node[Nframe=n](coinouts)(80,210){\scriptsize \sffamily ...}
\node[Nframe=n](coinout50)(90,210){\scriptsize \sffamily coin\_out\_50}
\node[Nframe=n](coinin51)(10,192){}
\node[Nframe=n](coinins1)(20,192){}
\node[Nframe=n](coinin501)(30,192){}
\node[Nframe=n](cancel1)(50,192){}
\node[Nframe=n](coinout51)(70,192){}
\node[Nframe=n](coinouts1)(80,192){}
\node[Nframe=n](coinout501)(90,192){}

\drawedge(coinin5,coinin51){}
\drawedge(coinins,coinins1){}
\drawedge(coinin50,coinin501){}
\drawedge(cancel,cancel1){}
\drawedge(coinout51,coinout5){}
\drawedge(coinouts1,coinouts){}
\drawedge(coinout501,coinout50){}

\node[Nframe=n](reseta)(28,128){}
\node[Nframe=n](reseta1)(28,110){}
\node[Nframe=n](donea)(48,128){}
\node[Nframe=n](donea1)(48,110){}
\node[Nframe=n](reqa)(68,128){}
\node[Nframe=n](reqa1)(68,110){}

\drawedge(reseta,reseta1){\scriptsize \sffamily reset}
\drawedge[ELside=r](donea1,donea){\scriptsize \sffamily done}
\drawedge[ELside=r](reqa1,reqa){\scriptsize \sffamily req}

\node[Nframe=n](reqsth)(18,30){\scriptsize \sffamily req\_coffee, req\_tea}
\node[Nframe=n](reqsth1)(18,50){}
\node[Nframe=n](drinkready)(48,30){\scriptsize \sffamily drink\_ready}
\node[Nframe=n](drinkready1)(48,50){}
\node[Nframe=n](startsth)(78,30){\scriptsize \sffamily start\_coffee, start\_tea}
\node[Nframe=n](startsth1)(78,50){}

\drawedge(reqsth,reqsth1){}
\drawedge(drinkready,drinkready1){}
\drawedge(startsth1,startsth){}
\end{picture}
\end{center}
For the sake of readability, the loops -- marked with (*) -- for state 0 are left out. The loops are:
\begin{itemize}
\item \textsf{coin\_in\_5/x:=x+5}
\item \textsf{coin\_in\_10/x:=x+10}
\item \textsf{coin\_in\_20/x:=x+20}
\item \textsf{coin\_in\_50/x:=x+50}
\end{itemize}
\end{enumerate}

\exercise{Statecharts}
\begin{center}
\begin{pspicture}(0,0)(12,7)
\psframe[framearc=.05](0,1)(8,6)
\rput(2,5){\rnode{AC}{\psframebox[framearc=.5]{AC}}}
\rput(6,5){\rnode{BC}{\psframebox[framearc=.5]{BC}}}
\rput(6,2){\rnode{BD}{\psframebox[framearc=.5]{BD}}}
\psline{**->}(3.5,6.5)(4,6)
\rput(4.7,6.4){\scriptsize \sffamily /x:=1; y:=0}
\psline{**->}(0.9,5)(1.6,5)
\ncarc[arcangleA=0,arcangleB=0]{->}{AC}{BC}\Aput{\scriptsize \sffamily AC/x:=0}
\ncarc{->}{BC}{BD}\Aput{\scriptsize \sffamily BC/y:=1}
\ncarc{->}{BD}{BC}\Aput{\scriptsize \sffamily BD/y:=0}
\pscurve{->}(8,4.7)(9,5)(8,5.3)
\rput(10,5){\scriptsize \sffamily AC/x:=x+1}
\pscurve{<-}(8,2.7)(9,3)(8,3.3)
\rput(10,3){\scriptsize \sffamily BC/x:=x+1}
\end{pspicture}
\end{center}

\exercise{Statecharts}
\begin{center}
\begin{picture}(100,60)(0,0)
\gasset{Nadjust=w,Nadjustdist=2,Nh=6,Nmr=1}
\node[Nmarks=i](D1)(30,50){\sffamily D1}
\node(E1)(30,30){\sffamily E1}
\node(A)(60,50){\sffamily A}
\node(B)(60,30){\sffamily B}
\node(C)(60,10){\sffamily C}
\node(D2)(90,50){\sffamily D2}
\node(E2)(90,30){\sffamily E2}

\drawedge[sxo=-1,exo=-1,ELside=r](D1,E1){\scriptsize \sffamily u}
\drawedge[sxo=1,exo=1,ELside=r](E1,D1){\scriptsize \sffamily v}
\drawedge[syo=1,eyo=1](D1,A){\scriptsize \sffamily s}
\drawedge[syo=-1,eyo=-1](A,D1){\scriptsize \sffamily t}
\drawedge[sxo=-1,exo=-1,ELside=r](A,B){\scriptsize \sffamily w}
\drawedge[sxo=1,exo=1,ELside=r](B,A){\scriptsize \sffamily x}
\drawedge[ELside=r](B,C){\scriptsize \sffamily y}
\drawedge[curvedepth=8](C,A){\scriptsize \sffamily x}
\drawedge[sxo=-1,exo=-1,ELside=r](D2,E2){\scriptsize \sffamily u}
\drawedge[sxo=1,exo=1,ELside=r](E2,D2){\scriptsize \sffamily v}
\drawedge[sxo=-2,exo=-2,ELside=r](D2,B){\scriptsize \sffamily s}
\drawedge[sxo=2,exo=1,ELside=r](B,D2){\scriptsize \sffamily t}
\drawedge(C,D2){\scriptsize \sffamily t}
\end{picture}
\end{center}

\end{document}
