{"id":1097,"date":"2026-07-07T23:19:29","date_gmt":"2026-07-07T23:19:29","guid":{"rendered":"http:\/\/tlindner.macmess.org\/?p=1097"},"modified":"2026-07-07T23:19:29","modified_gmt":"2026-07-07T23:19:29","slug":"coco-buttons","status":"publish","type":"post","link":"https:\/\/tlindner.macmess.org\/?p=1097","title":{"rendered":"CoCo Buttons"},"content":{"rendered":"<h2>Reading CoCo Joystick Buttons in Color BASIC<\/h2>\n<p>\nThe joystick buttons on the TRS-80 Color Computer 1 (CoCo 1) are connected to the\nPIA (Peripheral Interface Adapter). They can be read from memory location\n<code>$FF00<\/code>, which is decimal <code>65280<\/code>.\n<\/p>\n<p>\nIn Color BASIC, the value can be read using <code>PEEK<\/code>:\n<\/p>\n<pre><code>B=PEEK(65280)<\/code><\/pre>\n<p>\nThe joystick buttons are <strong>active low<\/strong>. This means that a button that\nis not pressed reads as a <code>1<\/code>, and a pressed button pulls the bit to\n<code><\/code>.\n<\/p>\n<p>\nThe button bits are:\n<\/p>\n<table>\n<tr>\n<th>Bit<\/th>\n<th>Mask<\/th>\n<th>Button<\/th>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td><code>1<\/code><\/td>\n<td>Right joystick button<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td><code>2<\/code><\/td>\n<td>Left joystick button<\/td>\n<\/tr>\n<\/table>\n<p>\nColor BASIC supports bitwise <code>AND<\/code>, which makes checking the buttons\nstraightforward:\n<\/p>\n<pre><code>10 B=PEEK(65280)\n20 IF (B AND 1)=0 THEN PRINT &quot;RIGHT BUTTON&quot;\n30 IF (B AND 2)=0 THEN PRINT &quot;LEFT BUTTON&quot;\n40 GOTO 10<\/code><\/pre>\n<p>\nThe parentheses are important. Color BASIC&#8217;s operator precedence can produce\nsurprising results if you write:\n<\/p>\n<pre><code>PEEK(65280) AND 128 = 0<\/code><\/pre>\n<p>\nThis is not interpreted as:\n<\/p>\n<pre><code>(PEEK(65280) AND 128) = 0<\/code><\/pre>\n<p>\nInstead, the comparison happens first, effectively making it:\n<\/p>\n<pre><code>PEEK(65280) AND (128 = 0)<\/code><\/pre>\n<p>\nSince <code>128 = 0<\/code> is false, the expression becomes an AND with zero,\nwhich will always produce zero.\n<\/p>\n<p>\nThe correct way to test a bit is to put the bit operation inside parentheses:\n<\/p>\n<pre><code>(PEEK(65280) AND 128) = 0<\/code><\/pre>\n<p>\nThe evaluation order is:\n<\/p>\n<ol>\n<li><code>PEEK(65280)<\/code> reads the hardware register.<\/li>\n<li><code>AND 128<\/code> masks the desired bit.<\/li>\n<li><code>= 0<\/code> tests whether that bit is clear.<\/li>\n<\/ol>\n<p>\nThis is a good habit for Color BASIC programming: whenever using bit masks in\na comparison, put the masked expression in parentheses.\n<\/p>\n<p>\nThe CoCo&#8217;s joystick interface is simple, but remembering that the buttons are\nactive low and that Color BASIC&#8217;s operator precedence is different from many\nmodern languages will save a lot of debugging time.\n<\/p>","protected":false},"excerpt":{"rendered":"<p>Reading CoCo Joystick Buttons in Color BASIC The joystick buttons on the TRS-80 Color Computer 1 (CoCo 1) are connected to the PIA (Peripheral Interface Adapter). They can be read from memory location $FF00, which is decimal 65280. In Color &hellip; <a href=\"https:\/\/tlindner.macmess.org\/?p=1097\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1097","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=\/wp\/v2\/posts\/1097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1097"}],"version-history":[{"count":2,"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=\/wp\/v2\/posts\/1097\/revisions"}],"predecessor-version":[{"id":1099,"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=\/wp\/v2\/posts\/1097\/revisions\/1099"}],"wp:attachment":[{"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tlindner.macmess.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}