{"id":3583,"date":"2025-04-18T12:49:03","date_gmt":"2025-04-18T12:49:03","guid":{"rendered":"https:\/\/webprojects.cloud\/wordpress\/splatco\/?post_type=spl_knowledgebase&#038;p=3583"},"modified":"2025-06-10T11:57:11","modified_gmt":"2025-06-10T11:57:11","slug":"example-advanced-indexing-semaphores","status":"publish","type":"spl_knowledgebase","link":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/","title":{"rendered":"Example (Advanced): Indexing semaphores"},"content":{"rendered":"\n<p>Some semaphore instructions can be used with the index register. This allows you to devise quite sophisticated schemes. The most likely use for indexing would be where you are writing a program that works on several identical channels, like maybe a 4-channel lighting control system.<\/p>\n\n\n\n<p>The advantage of using indexed addressing is that the same code can be used for all 4 channels, with a huge reduction in the size of the code. The alternative would be to have 4 almost identical copies of the program running simultaneously. That can certainly be done, but becomes a headache if you need to make any changes.<\/p>\n\n\n\n<p>The down-side is that the code becomes more complicated, and in particular that the planning of memory storage becomes absolutely crucial to the success.<\/p>\n\n\n\n<p>In indexed semaphore instructions the index register is added to the base address argument (not to the semaphore number). That fits in very nicely with the concept that you use the index register as the channel number. Here are the memory allocation directives for a hypothetical 4-channel lighting control system:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">;Start the indexed memory at location 76 (arbitrary!)\nSems0       mEQU      76      ;Room for 8 semaphores\nSems1       mEQU      77      ;Room for 8 semaphores\nSems2       mEQU      78      ;Room for 8 semaphores\nSems3       mEQU      79      ;Room for 8 semaphores\nCycleCount0 mEQU      80      ;A single byte variable\nCycleCount1 mEQU      81      ;A single byte variable\nCycleCount2 mEQU      82      ;A single byte variable\nCycleCount3 mEQU      82      ;A single byte variable\n\n;Define the semaphore bits\nsemA        sEQU      0\nsemB        sEQU      1\nsemC        sEQU      2\nsemD        sEQU      3\nsemE        sEQU      4\n\n;Define outputs\nLamp0       oEQU      10\nLamp1       oEQU      11\nLamp2       oEQU      12\nLamp3       oEQU      13\n<\/pre>\n\n\n\n<p>Given these definition, the following subroutine code will count down any non-zero cycle count for those channels whose semaphore D is true and turn on its output if it hits zero (probably a total nonsense operation, but what the heck!).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Nonsense:<br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/index-register-instructions\/loadi-nn\/\">LoadI<\/a>     4            ;Easier to work backwards, gets DEC'd B4 1st use<\/code><br><code>NonsLoop:<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/index-register-instructions\/itox\/\">ItoX<\/a>                   ;Test index register<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/goto-gosub-and-related-instructions\/retifz\/\">RetIfZ<\/a>                 ;R\/ just did 0<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/index-register-instructions\/deci\/\">DecI<\/a><\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/index-register-instructions\/recall-mm\/\">iRecallS<\/a>  semD,Sems0   ;This will access bit D of byte Sems0+Index<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/goto-gosub-and-related-instructions\/goifnz-llll\/\">GoIfF<\/a>     NonsLoop     ;Loop back if the semaphore is false<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/index-register-instructions\/recall-mm\/\">iRecall<\/a>   CycleCount0  ;Get the I'th cycle counter<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/stack-instructions\/push\/\">Push<\/a>                   ;Make a duplicate<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/goto-gosub-and-related-instructions\/goifnz-llll\/\">GoIfZ<\/a>     NonsLoop     ;Loop back if already at zero<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/counting-and-related-instructions\/decx\/\">DecX<\/a><\/code><br><code>            Push                   ;Make a duplicate<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/index-register-instructions\/store-mm\/\">iStore<\/a>    CycleCount0  ;Save the new value<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/number-comparison-and-testing-instructions\/comparison-and-testing-for-byte-values\/goifnz-llll\/\">GoIfNZ<\/a>    NonsLoop     ;Loop if non-zero<\/code><br><code>            <a href=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/programming-reference\/instructions-arranged-by-function\/index-register-instructions\/itox\/\">iON<\/a>       Lamp0        ;turn on the lamp<\/code><br><code>            GoTo      NonsL<\/code>oop     ;Go and process the next one<\/pre>\n\n\n\n<p>A couple of points about this code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is easier to do the loop as a count down loop with a test for 0 than a count up loop with a test for 3.<\/li>\n\n\n\n<li>It&#8217;s easiest to do test for completion at the top of the loop, because the code loops back to that common point from 3 separate places. Hence, I is initialized to 4 rather than 3, because it gets decremented once before it is used for the first time.<\/li>\n\n\n\n<li>While several instructions refer to the channel 0 datum, they are all indexed so they actually access the I&#8217;th channel&#8217;s data.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Some semaphore instructions can be used with the index register. This allows you to devise quite sophisticated schemes. The most likely use for indexing would&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3578,"menu_order":4,"template":"","class_list":["post-3583","spl_knowledgebase","type-spl_knowledgebase","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Example (Advanced): Indexing semaphores - SPLat Controls<\/title>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Example (Advanced): Indexing semaphores - SPLat Controls\" \/>\n<meta property=\"og:description\" content=\"Some semaphore instructions can be used with the index register. This allows you to devise quite sophisticated schemes. The most likely use for indexing would...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/\" \/>\n<meta property=\"og:site_name\" content=\"SPLat Controls\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-10T11:57:11+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/\",\"name\":\"Example (Advanced): Indexing semaphores - SPLat Controls\",\"isPartOf\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website\"},\"datePublished\":\"2025-04-18T12:49:03+00:00\",\"dateModified\":\"2025-06-10T11:57:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials, application notes and white papers\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Tutorial: Semaphores\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Semaphores: Programming examples\",\"item\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Example (Advanced): Indexing semaphores\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/\",\"name\":\"SPLat Controls\",\"description\":\"OEM Embedded Machine Controllers\",\"publisher\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization\",\"name\":\"SPLat Controls\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg\",\"contentUrl\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg\",\"caption\":\"SPLat Controls\"},\"image\":{\"@id\":\"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Example (Advanced): Indexing semaphores - SPLat Controls","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Example (Advanced): Indexing semaphores - SPLat Controls","og_description":"Some semaphore instructions can be used with the index register. This allows you to devise quite sophisticated schemes. The most likely use for indexing would...","og_url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/","og_site_name":"SPLat Controls","article_modified_time":"2025-06-10T11:57:11+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/","name":"Example (Advanced): Indexing semaphores - SPLat Controls","isPartOf":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website"},"datePublished":"2025-04-18T12:49:03+00:00","dateModified":"2025-06-10T11:57:11+00:00","breadcrumb":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/example-advanced-indexing-semaphores\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/"},{"@type":"ListItem","position":2,"name":"Tutorials, application notes and white papers","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/"},{"@type":"ListItem","position":3,"name":"Tutorial: Semaphores","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/"},{"@type":"ListItem","position":4,"name":"Semaphores: Programming examples","item":"https:\/\/webprojects.cloud\/wordpress\/splatco\/knowledgebase\/tutorials-application-notes-and-white-papers\/tutorial-semaphores\/semaphores-programming-examples\/"},{"@type":"ListItem","position":5,"name":"Example (Advanced): Indexing semaphores"}]},{"@type":"WebSite","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#website","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/","name":"SPLat Controls","description":"OEM Embedded Machine Controllers","publisher":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webprojects.cloud\/wordpress\/splatco\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#organization","name":"SPLat Controls","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/","url":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg","contentUrl":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-content\/uploads\/2024\/10\/logo.svg","caption":"SPLat Controls"},"image":{"@id":"https:\/\/webprojects.cloud\/wordpress\/splatco\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/spl_knowledgebase\/3583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/spl_knowledgebase"}],"about":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/types\/spl_knowledgebase"}],"author":[{"embeddable":true,"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/users\/1"}],"up":[{"embeddable":true,"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/spl_knowledgebase\/3578"}],"wp:attachment":[{"href":"https:\/\/webprojects.cloud\/wordpress\/splatco\/wp-json\/wp\/v2\/media?parent=3583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}