{"id":841,"date":"2014-06-08T17:46:23","date_gmt":"2014-06-08T17:46:23","guid":{"rendered":"http:\/\/retroramblings.net\/?p=841"},"modified":"2014-06-08T17:46:43","modified_gmt":"2014-06-08T17:46:43","slug":"reading-a-sega-6-button-pad-2","status":"publish","type":"post","link":"http:\/\/retroramblings.net\/?p=841","title":{"rendered":"Reading a Sega 6-button pad"},"content":{"rendered":"<p><strong>Part 2: some code<\/strong><\/p>\n<p>In my earlier post I mentioned that I was going to handle reading the Sega 6-button joypad in software, using an interrupt routine, and use programmable logic to handle masquerading as a CD32 pad.<\/p>\n<p>The six inputs from the Sega pad (up, down, left, right, button1, button2) are mapped to PSOC pins P2.0 to P2.5.\u00a0 Since these are adjacent, we can read and write to them easily as a single entity.\u00a0 We create a &#8220;pins&#8221; component with six inputs, named Sega_Inputs, and PSOC creator automatically generate headers and stubs so that we can read their status in main.c simply by calling Sega_Inputs_Read().\u00a0 Similarly, we create an output pin, Sega_Select on pin P2.6, which we can write in the code using Sega_Select_Write().<\/p>\n<p><!--more--><\/p>\n<p>The code currently looks like this:<\/p>\n<pre>#include &lt;project.h&gt;\r\n#include &lt;stdio.h&gt;\r\n\r\nstatic int Sega_SixButton;\r\nstatic int Sega_DirBC;\r\nstatic int Sega_AStart;\r\nstatic int Sega_XYZMode;\r\nstatic int CD32_Buttons;\r\nstatic int CD32_Directions;\r\n\r\nchar buf[64];\r\n\r\nCY_ISR(timerISR)\r\n{\r\n    static int framectr=255;\r\n    static int sixbutton;\r\n    FrameTimer_ReadStatusRegister();\r\n\r\n    switch(framectr)\r\n    {\r\n        case 8:\r\n            \/\/ Read regular button statuses here, immediately prior to first low pulse\r\n            Sega_DirBC=Sega_Inputs_Read();\r\n            sixbutton=1; \/\/ Assume we're dealing with a six-button controller unless proved otherwise\r\n            break;\r\n        case 7:\r\n            \/\/ Read regular button statuses here, on trailing edge of first low pulse\r\n            Sega_AStart=Sega_Inputs_Read();\r\n            break;\r\n\r\n            \/\/ Nothing exciting happens during the second low pulse\r\n            \/\/ (fall through)\r\n        case 6:\r\n        case 5:\r\n            \/\/ Third low pulse\r\n        case 4:\r\n            break;\r\n\r\n        case 3:\r\n            \/\/ If we have a six-button pad, then the direction lines will be low here\r\n            if(Sega_Inputs_Read() &amp; 0xf)\r\n                sixbutton=0;    \/\/ Not a six-button pad\r\n            break;\r\n        case 2:\r\n            \/\/ After the trailing edge of the third low pulse, XYZ and mode are readable.\r\n            Sega_XYZMode=Sega_Inputs_Read();\r\n            break;\r\n\r\n        case 1:\r\n            \/\/ Finally, after the last low pulse, the four direction lines should all read high.\r\n            if((~Sega_Inputs_Read()) &amp; 0xf)\r\n                sixbutton=0;    \/\/ Not a six-button pad            \r\n            Sega_SixButton=sixbutton;\r\n\r\n            \/\/ Now send the button status to the CD32 Shifter.\r\n            CD32_Directions=Sega_DirBC&amp;0xf;\r\n            CD32_Buttons=(Sega_DirBC&lt;&lt;2)&amp;0xc0;\r\n            CD32_Buttons|=Sega_AStart&amp;0x30;\r\n            if(sixbutton)\r\n                CD32_Buttons|=Sega_XYZMode&amp;0xf;\r\n            else\r\n                CD32_Buttons|=0xf;\r\n            CD32Shifter_1_Data_Reg=CD32_Buttons;\r\n            CD32Shifter_1_Fire_Reg=(Sega_DirBC&amp;0x10) ? 0x00 : 0xff;\r\n            break;\r\n\r\n        default:\r\n            break;\r\n    }\r\n\r\n    \/\/ Send four low pulses\r\n    if(framectr&lt;9)\r\n        Sega_Select_Write(framectr&amp;1);\r\n\r\n    --framectr;\r\n    if(framectr==0)\r\n        framectr=63;\r\n}\r\n\r\nint main()\r\n{\r\n    Isr_Timer_StartEx(timerISR);\r\n    FrameTimer_Start();\r\n    CyGlobalIntEnable;\r\n    CySysPmSleep();\r\n    return(0);\r\n}\r\n\r\n\/* [] END OF FILE *\/<\/pre>\n<p>I will probably want to remap the buttons somewhat once I have the CD32Shifter side of the project working, but reading the Sega pad seems to be working well.<\/p>\n<p>I&#8217;ve started a GitHub repository to hold the project files, <a href=\"http:\/\/https:\/\/github.com\/robinsonb5\/SegaToCD32\">which can be found here.<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Part 2: some code In my earlier post I mentioned that I was going to handle reading the Sega 6-button joypad in software, using an interrupt routine, and use programmable logic to handle masquerading as a CD32 pad. The six &hellip; <a href=\"http:\/\/retroramblings.net\/?p=841\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,8],"tags":[],"class_list":["post-841","post","type-post","status-publish","format-standard","hentry","category-amiga","category-hardware"],"_links":{"self":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=841"}],"version-history":[{"count":3,"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/841\/revisions"}],"predecessor-version":[{"id":845,"href":"http:\/\/retroramblings.net\/index.php?rest_route=\/wp\/v2\/posts\/841\/revisions\/845"}],"wp:attachment":[{"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=841"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/retroramblings.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}