{"id":201,"date":"2021-11-05T02:32:00","date_gmt":"2021-11-05T02:32:00","guid":{"rendered":"https:\/\/adventuresinretroprogramming.wordpress.com\/?p=201"},"modified":"2021-11-05T02:32:00","modified_gmt":"2021-11-05T02:32:00","slug":"11-enough-i-want-to-build-something","status":"publish","type":"post","link":"https:\/\/mrgadget.nexus\/t81t-dev\/2021\/11\/05\/11-enough-i-want-to-build-something\/","title":{"rendered":"#11: Enough! I want to build something"},"content":{"rendered":"\n<p>It&#8217;s high time we built something, right? Well let&#8217;s build something that is certainly for the Amiga, but doesn&#8217;t include hundreds of lines of code&#8230; Here is some code which we&#8217;ll describe later. For fun see if you can guess what it does before reading the description. I&#8217;ve actually been using this newly written code as a form of simple &#8220;test case&#8221; for the various environments I&#8217;ve built and re-built over the last few months when preparing this series of articles.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;dos\/dosextens.h&gt;\n\nextern struct DosLibrary *DOSBase;\n\nvoid reportstate (char *when)\n{\n  LONG flags = DOSBase-&gt;dl_Root-&gt;rn_Flags;\n  if (flags &amp; RNF_WILDSTAR)\n    printf (&quot;* %s on\\n&quot;, when);\n  else\n    printf (&quot;* %s off\\n&quot;, when);\n}\n\nint main (int argc, char *argv&#x5B;])\n{\n  if (argc != 2)\n    reportstate (&quot;is&quot;);\n  else\n  {\n    reportstate (&quot;was&quot;);\n    if (strcmp (argv&#x5B;1], &quot;on&quot;) == 0)\n      DOSBase-&gt;dl_Root-&gt;rn_Flags |= RNF_WILDSTAR;\n    if (strcmp (argv&#x5B;1], &quot;off&quot;) == 0)\n      DOSBase-&gt;dl_Root-&gt;rn_Flags &amp;= ~RNF_WILDSTAR;\n    reportstate (&quot;is now&quot;);\n  }\n  return 0;\n}\n<\/pre><\/div>\n\n\n<p>One thing to be aware of is that the compiler we have installed has a fairly limited C library, so you will note we use &#8220;strcmp&#8221; and test for &#8220;on&#8221; and &#8220;off&#8221; which would fail for &#8220;ON&#8221; or &#8220;On&#8221; or &#8220;oN&#8221;. The library sadly does not include a &#8220;strcasecmp&#8221; function. However this is a demonstration piece, so we&#8217;ll accept this limitation.<\/p>\n\n\n\n<p>Let&#8217;s go through this a bit&#8230; First of all let&#8217;s talk about libraries. The Amiga OS is a bit like a &#8220;micro-kernel&#8221; based system with a small &#8220;core&#8221; and lots of additional modules. The Amiga&#8217;s core is &#8220;exec.library&#8221;, and one of the additional modules is &#8220;dos.library&#8221; which is basically speaking the disk operating system providing access to files and so on. Because basically every program uses these libraries the normal &#8220;startup&#8221; for C programs &#8220;opens&#8221; these libraries, or more precisely &#8220;locates&#8221; the exec library and with that &#8220;opens&#8221; the dos library. These are usually ROM based on the Amiga except in very rare circumstances.<\/p>\n\n\n\n<p>We need to &#8220;talk&#8221; to the AmigaDOS so we need information about it and in this case a &#8220;pointer&#8221; to the library that the C startup code has opened.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;dos\/dosextens.h&gt;\nextern struct DosLibrary *DOSBase;\n<\/pre><\/div>\n\n\n<p>Starting with the &#8220;2.0&#8221; operating system there has been a &#8220;hidden&#8221; flag to enable the use of the asterisk (*) as the wildcard, instead of the original Amiga wildcard of &#8220;#?&#8221; quite literally translating to any number of any character. This is stored in the &#8220;root node&#8221; of the DOS library. If we want to &#8220;report&#8221; on the state we can fetch and test it as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nLONG flags = DOSBase-&gt;dl_Root-&gt;rn_Flags;\nif (flags &amp; RNF_WILDSTAR)\n<\/pre><\/div>\n\n\n<p>Due to differences in compilers and platforms, not least because the original Amiga OS was &#8220;cross-compiled&#8221; the Amiga OS defines its own variable types with &#8220;LONG&#8221; being a 32-bit integer. The DOSBase pointer set by the C startup code points to the DOS library data including a pointer to the &#8220;root node&#8221; structure and from the root node you can get to some flags. These structures are in the &#8220;dos\/dosextens.h&#8221; file from the NDK. The flag we want is termed &#8220;WILDSTAR&#8221; which is also defined in that include file. Amiga flags tend to be defined &#8220;&lt;Struct&gt;B_&lt;Name&gt;&#8221; for bit position and &#8220;&lt;Struct&gt;F_&lt;Name&gt;&#8221; for bit mask, so &#8220;RNF_WILDSTAR&#8221; is the bit mask of the &#8220;WILDSTAR&#8221; bit in the &#8220;root node&#8221;. Try searching for the definitions in the include file (probably easiest in the &#8220;XFER&#8221; area on your chosen host).<\/p>\n\n\n\n<p>The easiest and safest way to control flags is probably using bitwise assignment operators, so to turn the flag on you can OR the flag mask.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nDOSBase-&gt;dl_Root-&gt;rn_Flags |= RNF_WILDSTAR;\n<\/pre><\/div>\n\n\n<p>To turn the flag off you can AND the inverted flag mask.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nDOSBase-&gt;dl_Root-&gt;Flags &amp;= ~RNF_WILDSTAR;\n<\/pre><\/div>\n\n\n<p>Everything else is just basic C code.<\/p>\n\n\n\n<p>For working in the Amiga emulator with the Amiga set for an American keyboard let&#8217;s recap on some &#8220;special&#8221; characters and their locations.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# - shift-3\n&quot; - two keys right of L, shifted\n\\ - three keys right of L, unshifted\n| - three keys right of L, shifted\n~ - one key left of 1, shifted\n<\/pre><\/div>\n\n\n<p>Most other symbols for programming will probably match your keyboard. For future reference the &#8216;@&#8217; symbol is shift-2.<\/p>\n\n\n\n<p>Make a file called &#8220;sw.c&#8221; at the root level of the &#8220;Work&#8221; drive (let&#8217;s be untidy for now) and enter the code listed at the start of this post. I personally used &#8220;Vim&#8221; but you may find &#8220;Redit&#8221; easier. Then we can compile the program. If the &#8220;vc&#8221; command returns an error, check the code carefully against the listing.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncd Work:\nRedit sw.c\n\nvc sw.c -o sw\nsw\n<\/pre><\/div>\n\n\n<p>When the Amiga boots the flag will be off. What did the program report?<\/p>\n\n\n\n<p>Have you used C-Shell (prior post) since you booted the emulated Amiga?<\/p>\n\n\n\n<p>It turns out that C-Shell will by default turn the flag on when it starts but it never turns it off, so even if you &#8220;quit&#8221; the flag will be left switched on. C-Shell will also turn the flag on at other times. If you are using C-Shell type &#8220;quit&#8221; before the following test to demonstrate the effect.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsw off\ncopy *.c t:\ncopy #?.c t:\nsw on\ncopy *.c t:\ncopy #?.c t:\n<\/pre><\/div>\n\n\n<p>With the setting &#8220;off&#8221; trying to copy &#8220;*.c&#8221; will generate an error.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCan&#039;t open *.c for input - object not found\n<\/pre><\/div>\n\n\n<p>The second copy command is the traditional\/original AmigaDOS wildcard and will copy our source file to the Amiga&#8217;s &#8220;temp&#8221; folder.<\/p>\n\n\n\n<p>When the setting is switched on however BOTH commands will work. The setting doesn&#8217;t CHANGE the wildcard, it just adds a new one.<\/p>\n\n\n\n<p>There are scenarios where the star wildcard will not work, but it will work in several places.<\/p>\n\n\n\n<p>To recap what we have covered&#8230; We have pulled in an operating system include file and we pulled in a &#8220;library base pointer&#8221;. We didn&#8217;t need to &#8220;open&#8221; the library because &#8216;C&#8217; startup code did it for us. We went into a publicly defined operating system structure to fetch and report on a flag value, and also switched the flag on and off. We tested and witnessed the effect of the flag.<\/p>\n\n\n\n<p>When I compiled the code today the result was a 7,880 byte executable, which if you recall has to have the on\/off switch provided in lowercase. Still seems pretty good compared to modern systems, right?. I wrote something to do this in November 1992 in assembly language, using another AmigaDOS call called &#8220;ReadArgs&#8221; which provides support for command line help as well as being case-insensitive for the on\/off switches and the result was an 836 byte executable.<\/p>\n\n\n\n<p>It&#8217;s feels about time to dig into some T81T archives&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s high time we built something, right? Well let&#8217;s build something that is certainly for the Amiga, but doesn&#8217;t include hundreds of lines of code&#8230; Here is some code which we&#8217;ll describe later. For fun see if you can guess<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-201","post","type-post","status-publish","format-standard","hentry","category-amiga"],"_links":{"self":[{"href":"https:\/\/mrgadget.nexus\/t81t-dev\/wp-json\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mrgadget.nexus\/t81t-dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mrgadget.nexus\/t81t-dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mrgadget.nexus\/t81t-dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mrgadget.nexus\/t81t-dev\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":0,"href":"https:\/\/mrgadget.nexus\/t81t-dev\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"wp:attachment":[{"href":"https:\/\/mrgadget.nexus\/t81t-dev\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mrgadget.nexus\/t81t-dev\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mrgadget.nexus\/t81t-dev\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}