<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>State Space &#187; Efficient C/C++</title>
	<atom:link href="http://embeddedgurus.com/state-space/category/efficient-cc/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/state-space</link>
	<description>A Blog by Miro Samek</description>
	<lastBuildDate>Mon, 26 Sep 2011 20:24:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Protothreads versus State Machines</title>
		<link>http://embeddedgurus.com/state-space/2011/06/protothreads-versus-state-machines/</link>
		<comments>http://embeddedgurus.com/state-space/2011/06/protothreads-versus-state-machines/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 17:28:53 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[Efficient C/C++]]></category>
		<category><![CDATA[event-driven programming]]></category>
		<category><![CDATA[state machines]]></category>
		<category><![CDATA[multitasking]]></category>
		<category><![CDATA[protothreads]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=92</guid>
		<description><![CDATA[For a number of years I&#8217;ve been getting questions regarding Protothreads and comparisons to state machines. Here is what I think. Protothreads are an attempt to write event-driven code in a sequential way. To do so, protothreads introduce a concept of &#8220;blocking abstraction&#8221; to event-driven programming&#8211;something that event-driven programming is trying to get rid of [...]]]></description>
			<content:encoded><![CDATA[<p>For a number of years I&#8217;ve been getting questions regarding <a title="Protothreads" href="http://en.wikipedia.org/wiki/Protothreads">Protothreads</a> and comparisons to state machines. Here is what I think.</p>
<p>Protothreads are an attempt to write event-driven code in a sequential way. To do so, protothreads introduce a concept of &#8220;blocking abstraction&#8221; to event-driven programming&#8211;something that event-driven programming is trying to get rid of in the first place.</p>
<p>Obviously, to be compatible with event-driven programming, protothreads cannot *really* block, at least not in the same sense as traditional threads of a conventional blocking RTOS can. Instead, a protothread is still called for every event and still *returns* to the caller without really blocking. However, when a given event does not match the &#8220;blocking abstraction&#8221;, the protothread returns without doing anything and without progressing. Only when the current event matches the &#8220;blocking abstraction&#8221; the protothread advances to the next &#8220;blocking abstraction&#8221; and also returns. Please note that protothreads allow standard flow control statements, such as IF-THEN-ELSE and WHILE loops between any two &#8220;blocking abstractions&#8221;. Therefore the program feels more &#8220;natural&#8221; for designers used to the traditional sequential programming style. You simply see the expected sequence of events.</p>
<p>Protothreads are indeed a simplification, but only for *sequential problems*, in which only specific sequences of events are valid and all other sequences are invalid. Examples for using protothreads include the sequence of events for initializing a radio modem.</p>
<p>However, protothreads lose their advantage entirely if there are many valid sequences of events. This is actually the most common situation in event-driven systems. State machines are capable to handle multiple sequences of events easily. In fact, state machines are getting simpler when the sequence of events matters less. In contrast, protothreads are getting more and more complex when they need to accept more sequences of events.</p>
<p>So, in the end, protothreads are an attempt to replace state machines, which are considered complex and messy by the inventors of the protothread mechanism. But, the &#8220;messiness&#8221; of state machines is obviously a very subjective statement. A good state machine implementation technique can remove many (most) accidental difficulties of coding state machines. I mean, if a state machine as depicted in a state diagram is simple, and if the code does not reflect this simplicity, the problem is with the implementation technique, not with the inherent complexity of a state machine.</p>
<p>The bottom line: good state machine implementation techniques eliminate most reasons for using protothreads. State machines are far more generic and flexible, because they can easily handle multiple sequences of events. In comparison, protothreads are intentionally crippled state machines that transition implicitly from one &#8220;blocking abstraction&#8221; to another executing code in between as the &#8220;actions&#8221; on such implicit transition.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2011/06/protothreads-versus-state-machines/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Free state machine tool for embedded systems</title>
		<link>http://embeddedgurus.com/state-space/2010/11/free-state-machine-tool-for-embedded-systems/</link>
		<comments>http://embeddedgurus.com/state-space/2010/11/free-state-machine-tool-for-embedded-systems/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 00:22:56 +0000</pubDate>
		<dc:creator>Miro Samek</dc:creator>
				<category><![CDATA[Efficient C/C++]]></category>
		<category><![CDATA[modeling tool]]></category>
		<category><![CDATA[state machines]]></category>
		<category><![CDATA[UML]]></category>
		<category><![CDATA[code generation]]></category>
		<category><![CDATA[graphical tool]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/?p=49</guid>
		<description><![CDATA[I realize that I stalled a little my series about RTOSes, event-driven programming, state machines and frameworks for embedded systems. I certainly will come back to this subject, but today I wanted to let you know about a new, free, graphical tool for drawing state machines and generating production-quality embedded code. Traditionally, such tools haven&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I realize that I stalled a little my series about RTOSes, event-driven programming, state machines and frameworks for embedded systems. I certainly will come back to this subject, but today I wanted to let you know about a new, free, graphical tool for drawing state machines and generating production-quality embedded code.</p>
<p>Traditionally, such tools haven&#8217;t particularly caught on in the embedded space, mainly because too often such tools fail to pull their own weight. Developers find themselves fighting the tool at every step of the way: from drawing the diagrams to trying to live in a straight jacket of the code structure it generates.</p>
<p><img src="http://www.state-machine.com/attachments/qm_gui.jpg" alt="" /></p>
<p>The new, free QM tool from <a href="http://www.state-machine.com">Quantum Leaps</a> (my company) is different, because it was designed from the ground up around the code-centric approach. Unlike other graphical tools, QM gives you complete control over the generated code structure, directory names, file names, and elements that go into every file. You can mix your own code with the synthesized code and use QM to generate as much or as little of the overall code as you see fit. At the low level, QM respects your graphical layout as much as possible and will not re-attach or re-route connectors, resize nodes, or adjust text annotations. You will find that you don&#8217;t need to fight the tool.</p>
<p>Even though a lot of effort went into making QM as UML-compliant, the tool is innovative and might work <strong>differently</strong> than other graphical state machine tools on the market. For example, QM does not use &#8220;pseudostates&#8221;, such as the initial pseudostate or choice point. Instead QM uses higher-level primitives of initial-transition and choice-segment, respectively. This simplifies state diagramming immensely, because you don&#8217;t need to separately position pseudostates and then connect them. Also, QM introduces a new notation for internal transitions, which allows actual drawing of internal transitions (in standard UML notation internal transitions are just text in the state body). This notation enables showing internal transitions with regular state transitions in a choice point&#8211;something that comes up very often in practice and was never addressed well in the standard UML.</p>
<p>The QM tool is available now for a <strong>free</strong> download and free, unrestricted use from <a href="http://www.state-machine.com/qm">state-machine.com/qm</a>. I&#8217;d appreciate any comments about the tool, comparisons to other similar tools, code generation, UML, state machines, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2010/11/free-state-machine-tool-for-embedded-systems/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Insects of the computer world</title>
		<link>http://embeddedgurus.com/state-space/2009/03/insects-of-the-computer-world/</link>
		<comments>http://embeddedgurus.com/state-space/2009/03/insects-of-the-computer-world/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 21:15:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Efficient C/C++]]></category>
		<category><![CDATA[MCUs]]></category>

		<guid isPermaLink="false">http://embeddedgurus.com/state-space/2009/03/09/insects-of-the-computer-world/</guid>
		<description><![CDATA[The recent Jack Ganssle&#8217;s &#8220;Breakpoints&#8221; blog on Embedded.com makes an excellent point that the same forces (the Moore&#8217;s law), which drive down the prices of high-end processors open even more market opportunities at the low-end of the price spectrum. I also agree that the most deciding factor for the price of a single-chip microcontroller (MCU) [...]]]></description>
			<content:encoded><![CDATA[<p>The recent Jack Ganssle&#8217;s <a href="http://www.embedded.com/columns/breakpoint/215801305">&#8220;Breakpoints&#8221; blog</a> on <a href="http://www.embedded.com">Embedded.com</a> makes an excellent point that the same forces (the Moore&#8217;s law), which drive down the prices of high-end processors open even more market opportunities at the low-end of the price spectrum. I also agree that the most deciding factor for the price of a single-chip microcontroller (MCU) is the efficiency of its memory use, in other words, the code density. This becomes obvious when one looks at the silicon die of any MCU, which is completely dominated by the ROM and RAM blocks, the CPU being almost insignificant somewhere in the corner.</p>
<p>But, I would disagree with Jack&#8217;s statement that &#8220;tiny (8-bit) processors make more efficient use of memory&#8221;. From my experience with several single-chip MCUs I draw a different conclusion: the CPU size (8-, 16-, 32-bits) almost doesn&#8217;t matter for the code density. The deciding factor is how old a design is, whereas the newer instruction set architectures (ISAs) generally far outperform the older ISAs.</p>
<p>To support the point, I present below a table that shows the code size of a tiny state machine framework written in C (called QP-nano), which has been compiled for a dozen or so very different single-chip MCUs. The code consists of a small hierarchical state machine processor (called QEP-nano), and a tiny framework (called QF-nano). The QEP-nano consists mostly of a conditional logic to execute hierarchical state machines. QF-nano contains an event queue, a timer module, and a simple event loop. I believe that this code is quite representative to typical projects that run on these small MCUs.<br />
<code><br />
CPU type          C Compiler                    QEP-nano    QF-nano</code></p>
<p><code> (bytes)    (bytes)<br />
---------------+-------------------+----------+---------<br />
PIC18                MPLAB-C18                          3,214     2,072</code></p>
<p><code> (student edition)</code></p>
<p><code> ---------------+-------------------+----------+---------<br />
8051 (SiLabs)             IAR EW8051                    952             603</code></p>
<p><code>---------------+-------------------+----------+---------</code></p>
<p><code>PSoC (M8C)               ImageCraft M8C            2,765     2,425</code></p>
<p><code>---------------+-------------------+----------+---------</code></p>
<p><code>68HC08                   CodeWarrior HC(S)08           957      660</code></p>
<p><code>---------------+-------------------+----------+---------</code></p>
<p><code>AVR (ATmega)             IAR EWAVR                        541      650</code></p>
<p><code>---------------+-------------------+----------+---------</code></p>
<p><code>AVR (ATmega)            WinAVR(GNU)                     998      810</code></p>
<p><code>---------------+-------------------+----------+---------</code></p>
<p><code>MSP430                         IAR EW430                552            460</code></p>
<p><code>---------------+-------------------+----------+---------</code></p>
<p><code>M16C                             HEW4/NC30                         984            969</code></p>
<p><code>---------------+-------------------+----------+---------</code></p>
<p><code>TMS320C28x                 C2000                       369 words  331 words (Piccolo)                                                     738 bytes  662 bytes</code></p>
<p><code>---------------+-------------------+----------+---------</code></p>
<p><code>ARM7(ARM/THUMB)        IAR EWARM              588(THUMB)  1,112(ARM)</code></p>
<p><code>---------------+-------------------+----------+---------</code></p>
<p><code>ARM Cortex-M3    IAR EWARM                  524               504 </code></p>
<p><code>(THUMB2)</code></p>
<p><code> ---------------+-------------------+----------+---------<br />
</code><br />
Interestingly, the winner is MSP430, which is a 16-bit architecture.<br />
It seems that the 16-bit ISA hits somehow the &#8220;sweet spot&#8221; for the best code density, perhaps because the addresses are also 16-bit wide and are handled in a single instruction. In contrast, 8-bitters need multiple instructions to handle 16-bit addresses.</p>
<p>I would also point out the excellent code density (and C-friendliness) of the new ARM Cortex-M3, which is a modern 32-bit ISA, and still far outperforms all 8-bitters, including the good ol&#8217;8051.</p>
<p>On the other hand, the venerable PIC architecture is by far the worst (or, C un-friendly). That&#8217;s interesting, because this is the 8-bit market leader. I honestly don&#8217;t understand how Microchip makes money when their chips require the most silicon for given functionality. Clearly some other forces than just technical merits must be at work here.</p>
<p>In conclusion, I understand that my data is highly subjective and different code sets (and different compilers) could perhaps produce different results. However, I believe that the general trend is true and this is an <strong>important lesson</strong> for engineers selecting MCUs.</p>
]]></content:encoded>
			<wfw:commentRss>http://embeddedgurus.com/state-space/2009/03/insects-of-the-computer-world/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

