<?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>Sufficiently Small &#187; 8 bit</title>
	<atom:link href="http://www.smallshire.org.uk/sufficientlysmall/category/8-bit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.smallshire.org.uk/sufficientlysmall</link>
	<description>sin(x) = x</description>
	<lastBuildDate>Sun, 11 Apr 2010 19:36:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>OWL BASIC Control Flow Graph</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2008/11/23/owl-basic-control-flow-graph/</link>
		<comments>http://www.smallshire.org.uk/sufficientlysmall/2008/11/23/owl-basic-control-flow-graph/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 19:55:52 +0000</pubDate>
		<dc:creator>Robert Smallshire</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[8 bit]]></category>
		<category><![CDATA[OWL BASIC]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/2008/11/23/owl-basic-control-flow-graph/</guid>
		<description><![CDATA[Since BBC BASIC does not use C-like nested scopes marked by braces to delineate loops and other blocks, the structure of such blocks is not captured in the Abstract Syntax Tree (AST); that is to say that the statements within a loop will not be child nodes of the opening loop statement such as FOR [...]]]></description>
			<content:encoded><![CDATA[<p>Since BBC BASIC does not use C-like nested scopes marked by braces to delineate loops and other blocks, the structure of such blocks is not captured in the Abstract Syntax Tree (AST); that is to say that the statements within a loop will not be child nodes of the opening loop statement such as FOR or WHILE.  Furthermore, there is not necessarily a one-to-one correspondence between loop entry points such as FOR, and loop ends such as NEXT &#8211; a FOR loop may contain more than one  NEXT statement, and what is more, in convoluted cases involving GOTOs, a single NEXT statement may be re-used by several FOR loops. <a href="http://www.smallshire.org.uk/sufficientlysmall/2007/07/02/next-please-compiling-iteration-structures-in-bbc-basic/">Next please! Compiling Iteration Structures in BBC BASIC</a> for more information.</p>
<p>It is therefore required to pair each closing loop statement (<i>e.g.</i> NEXT or ENDWHILE) with its corresponding opening loop statement(s), and this is turn requires that the possible paths of execution through the program can be determined statically (<i>i.e.</i> at compile time) so that for each closing statement we can trace backwards through the program execution to the previous opening statement.  This also makes it possible to identify illegal constructs, such as ending a WHILE loop with a NEXT statement.</p>
<p>To achieve this, we must build a data structure called the Control Flow Graph (CFG). For each statement in the AST we determine all possible following statement. Ordinarily this is simply the next statement in the program, but we must also resolve line number references in GOTO, GOSUB, <i>etc.</i> and at each IF statement control flow may go in either of two directions. We do not resolve function or procedure calls when building the CFG, so each function or procedure becomes a separate connected-component in the resulting graph.</p>
<p>I&#8217;ve been working with the code to Acornsoft&#8217;s <i>Sphinx Adventure</i> from 1982 as a real-world BBC BASIC test program for the BBC Micro.</p>
<p><img src="http://www.smallshire.org.uk/images/BBC_Sphinx_Adventure.jpg" alt="Acornsoft Sphinx Adventure" /></p>
<p>I don&#8217;t know who the author is, but I&#8217;m willing to bet they never imagined their by now long-forgotten code would undergo the sort of analysis its about to receive!  Maybe the code in Sphinx Adventure is intentionally obfuscated, or maybe its just badly written, but some of the non-structured programming techniques such as jumping out of the middle of procedures never to return, are &#8216;interesting&#8217; to say the least.</p>
<p><img src="http://www.smallshire.org.uk/images/Sphinx_bbc_cfg.png" alt="Control Flow Graph for Acornsoft Sphinx Adventure" /></p>
<p>Each node in this image is a single statement in the program, and the arrows between the nodes indicate the program flow. Each connected group of nodes is a separate procedure, and the largest group of nodes to the top-left of the chart is the main program starting from the first line through a long trail of DATA statements.</p>
<p>The <a href="http://www.smallshire.org.uk/Sphinx.graphml">Sphinx.graphml</a> is browsable with <a href="http://www.yworks.com/en/products_yed_about.html">yEd</a>, which was also used to produce these diagrams.</p>
<p>Zooming in, we can see the CFG for a single procedure &#8211; in this case PROCF,</p>
<pre class="brush: bbc;">
462 DEF PROCF(W): IFW=28 THEN PROCR(21): GOTO465
463 IFO?W &lt;&gt; L  ANDO?W &lt;&gt; 1 THEN PROCR(7): PRINTA2$;&quot; here.&quot;: GOTO465 ELSE IFO?W2 &lt;&gt; 1 THEN PROCR(8): PRINTA2$;&quot;.&quot;: GOTO465
464 IFW=27 ANDL=O?28 ANDWA=0 THEN PRINT&quot;Your bottle is now full of water.&quot;:WA=1 ELSE IFW=27 ANDL=O?28 THEN PRINT&quot;your bottle is already full.&quot; ELSE PROCR(21)
465 ENDPROC
</pre>
<p><img src="http://www.smallshire.org.uk/images/Sphinx_bbc_procf_cfg.png" alt="Control Flow Graph for Acornsoft Sphinx Adventure PROCF" /></p>
<p>The label in each node indicates its logical line number in the original code and the type of statement the node represents at the abstract syntax level.</p>
<p>Now we have the basic CFGs we can analyse them to match up corresponding loop statements, and optionally perform optimizations such as moving invariant code out of loops and eliminating dead (unreachable) code fragments.</p>
<hr/>Copyright &copy; 2010 <strong><a href="http://www.smallshire.org.uk/sufficientlysmall">Sufficiently Small</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@smallshire.org.uk so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.smallshire.org.uk/sufficientlysmall/2008/11/23/owl-basic-control-flow-graph/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing a BBC BASIC compiler for the CLR</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2007/06/10/writing-a-bbc-basic-compiler-for-the-clr/</link>
		<comments>http://www.smallshire.org.uk/sufficientlysmall/2007/06/10/writing-a-bbc-basic-compiler-for-the-clr/#comments</comments>
		<pubDate>Sun, 10 Jun 2007 22:26:05 +0000</pubDate>
		<dc:creator>Robert Smallshire</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[8 bit]]></category>
		<category><![CDATA[OWL BASIC]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/2007/06/10/writing-a-bbc-basic-compiler-for-the-clr/</guid>
		<description><![CDATA[Of late, I&#8217;ve picked up an interest in the CLR through learning C#. After being impressed by the performance of the .NET runtime (less so for Mono, although that&#8217;s another story) I decided that an effective way to really get to grips with the system would be to write a compiler which targets the CLR.
During [...]]]></description>
			<content:encoded><![CDATA[<p>Of late, I&#8217;ve picked up an interest in the CLR through learning C#. After being impressed by the performance of the .NET runtime (less so for Mono, although that&#8217;s another story) I decided that an effective way to really get to grips with the system would be to write a compiler which targets the CLR.</p>
<p>During a nostalgic poke around the web relating to <a href="http://en.wikipedia.org/wiki/Acorn_Computers">Acorn</a> hardware, over 12 years since my last contact with BBC BASIC, I stumbled across Richard Russell&#8217;s BBC BASIC for Windows (BB4W) which reminded me of my formative years learning to code on the BBC Model B and later various Acorn Archimedes computers under <a href="http://en.wikipedia.org/wiki/RISC_OS">RISC OS</a>. This led me to start down the road of implementing a BBC BASIC <em>compiler</em> for the CLR, although the original BBC BASIC, and BB4W are <em>interpreted</em>.  The original BBC BASIC II resided in only 16 kB of ROM, so it can&#8217;t be that much work. Can it?</p>
<p>The <a href="http://www.bbcbasic.co.uk/bbcwin/bbcwin.html">BBC Basic for Windows</a> implementation, from Russell actually goes to long lengths to provide good backwards compatibility with software originally developed for the Beeb or RISC OS, such as providing a Teletext MODE 7 equivalent, and &#8216;emulated&#8217; sound and graphics support.</p>
<p>I will be taking a different tack with this project. Rather than focus on getting old code reliant on obsolete hardware features to run, I will focus on making a viable BBC BASIC compiler for the CLR, which could be used to port existing programs to .NET/Mono. It is likely that OS or machine specific functionality such as sound and graphics will be implemented in significiantly different ways compared to the originals, for example, by giving good language-level integration with the <code>System.Console</code> class in .NET, rather than trying to emulate screen modes from Acorn hardware.</p>
<p>The toolset I settled on is as follows:</p>
<ul>
<li><a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython">IronPython</a> in which to write the compiler</li>
<li><a href="http://www.dabeaz.com/ply/">PLY</a> &#8211; the Python Lex/Yacc-a-like</li>
<li><a href="http://www.mikebuk.dsl.pipex.com/beebem/">BeebEm</a> &#8211; BBC Micro emulator with BBC BASIC II</li>
<li><a href="http://b-em.bbcmicro.com/arculator/">Arculator</a> &#8211; Acorn Archimedes emulator with BBC BASIC V</li>
<li>Documentation in the form of BBC Micro and Archimedes user manuals, and various other information and examples on the web</li>
</ul>
<p>Mostly its an experiment, and for the fun of writing a compiler. I don&#8217;t expect anybody to start creating large software systems in BBC BASIC as a result of my efforts. Far from it; there are <em>much </em>better tools today.  However, I must admit to a secret aim of producing the fastest BBC BASIC implementation yet!</p>
<hr/>Copyright &copy; 2010 <strong><a href="http://www.smallshire.org.uk/sufficientlysmall">Sufficiently Small</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@smallshire.org.uk so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.smallshire.org.uk/sufficientlysmall/2007/06/10/writing-a-bbc-basic-compiler-for-the-clr/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>BBC Computer 32K</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2005/11/27/bbc-computer-32k/</link>
		<comments>http://www.smallshire.org.uk/sufficientlysmall/2005/11/27/bbc-computer-32k/#comments</comments>
		<pubDate>Sun, 27 Nov 2005 00:14:04 +0000</pubDate>
		<dc:creator>Robert Smallshire</dc:creator>
				<category><![CDATA[8 bit]]></category>
		<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=4</guid>
		<description><![CDATA[That single purchase ... changed my life greater than any other single purchase made before or since.]]></description>
			<content:encoded><![CDATA[<p><img src="/sufficientlysmall/wp-content/bbc_computer_32k.png" width="639" height="195" alt="BBC Computer 32K" /><br />
The BBC Model B computer. The computer was bought by my father in 1984  using some money which he received from the death of his father. I was nine years old. This was our grandfather&#8217;s legacy for us. That single purchase for £400 in Beatties department store  in Wolverhampton 21 years ago changed my life greater than any other single purchase made before or since. It wholly responsible for my career since that time.</p>
<p>In terms of the hardware of the time, the BBC Microcomputer was something special. It had 32 kB of RAM for programs and the display and 32 kB of ROM containing the operating system and BBC BASIC, occupying the 64 kB address space of the 6502 CPU. We had a somewhat unreliable cassette deck for storing programs.</p>
<p>I probably spent thousands of hours over the next four years learning to program, mostly in the fantastic BBC BASIC (real prodecures with parameters, REPEAT..UNTIL loops and no real need to use GOTO, and of course integrated 6502 assembler) which gave me an excellent grounding in structured, procedural programming. I had a good line in writing software for my father to help in his land and engineering surveying work, which was my first introduction to writing mathematical and engineering software &#8211; all in BBC BASIC. </p>
<p>I also spent time fiddling with the hardware side of things, very much encouraged by the massive expandability of the BBC Micro. Usuually this would involve connecting home-made devices, motor controllers and the like,  to the User Port (pretty raw access to a 6522 VIA chip) or to the Analogue Port. All of this was inspired by a series of articles by Mike Cook in <a href="http://myweb.tiscali.co.uk/themicrouser/">The Micro User</a> (later Acorn Computing)  Magazine called <a href="http://myweb.tiscali.co.uk/themicrouser/bodybuilding/bodybuilding.htm">The Beeb Body Building Course</a>.</p>
<p>So why the nostalgia?</p>
<p>Well, two reasons. The first is that this early exposure computing is largely responsible for my career path, and the fact today that I now work as a software developer. The second reason is that last week I downloaded an impressively good BBC Micro emulator for my PC called <a href="http://www.mikebuk.dsl.pipex.com/beebem/">BeebEm</a>. </p>
<p>Obviously, a BBC emulator isn&#8217;t much use to me these days for software development, and all the interesting hardware aspects of the Beeb are of course not relevant. Nonetheless, it did give me the opportunity to indulge in some serious nostalgia playing old games such as Thrust, Elite and Exile.</p>
<p> <img src="/sufficientlysmall/wp-content/bbc_exile.png" width="513" height="509" alt="" /></p>
<p><img src="/sufficientlysmall/wp-content/bbc_thrust_1.png" width="638" height="512" alt="" /></p>
<hr/>Copyright &copy; 2010 <strong><a href="http://www.smallshire.org.uk/sufficientlysmall">Sufficiently Small</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@smallshire.org.uk so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.smallshire.org.uk/sufficientlysmall/2005/11/27/bbc-computer-32k/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
