Jump to content
IGNORED

Code for reading 5200 joysticks


Recommended Posts

I'm trying to write code for reading 5200 joysticks. My code is simply:

 

void main() {

    for(;;) {

        uint8_t *ptr = (void*)0xE800;
        PrintNumber(12,y,*ptr++);
        PrintNumber(12,y,*ptr++);
        PrintNumber(12,y,*ptr++);
        PrintNumber(12,y,*ptr++);
        PrintNumber(12,y,*ptr++);
        PrintNumber(12,y,*ptr++);
        PrintNumber(12,y,*ptr++);
        PrintNumber(12,y,*ptr++);
    }
}

 

I've tried on MAME and Atari 800 and it's only returning 0. If I set the address to 0xC010 the triggers return 1 or 0, so I know the code can read and display values.

 

Any ideas on what I could be doing wrong? Do I have to set up the POKEY in some way for this to work?

Edited by Under4MHZ
Link to comment
Share on other sites

You can't just read the POT0-7 registers arbitrarily -- they take significant time to scan, most of the frame (228 scanlines out of 262). Typically, the system VBI handler will start the pot scan at the end of the system VBI once per frame, right after it copies the POT registers to page zero. If you read the POT registers while the scan is running, they won't have counted up to their full value. Try using the OS values at $11-18 instead.

 

Link to comment
Share on other sites

Thanks, that was it. The below code works:

 

void main()
{
    for(;;) {

        u8 *ptr;

        ptr = (void*)0xE800;    // POKEY Pot
        PrintNumber(12,0,*ptr++);
        PrintNumber(12,1,*ptr++);

        ptr = (void*)0xC010;   // Trigger (fire buttons)
        PrintNumber(8,0,*ptr++);
        PrintNumber(8,1,*ptr++);

        ptr = (void*)0xE80B;   // POTGO start scan sequence
        *ptr = 0x00;
        ptr = (void*)0xE80F;   // SKStat Fast pot scan
        *ptr = 0xff;
        
        for(int wait = 0; wait < 30000; wait++);
    }
}

 

I had to trigger the POTGO and enable the Fast POT scan, along with a delay between reads.

 

https://www.atariarchives.org/mapping/memorymap.php

 

for information on POTGO and SKSTAT.

 

This assumes the interrupts are disabled, or the interrupt vectors have been overridden.

 

Link to comment
Share on other sites

Writing to $E80F actually accesses SKCTL, not SKSTAT. The two registers share the same address but the first is write-only while the second is read-only.

 

You can't turn on fast pot scan when reading standard paddles or 5200 controllers. Fast pot scan needs a different circuit to work and at best you'll just read 1/228th of the range, if the pots update at all. Some emulators don't emulate this properly and will let you get away with setting this bit when they shouldn't.

 

The pot scan needs 228 scanlines to complete. You can either wait at least this long or poll ALLPOT to tell when the scan has completed. Additionally, the pot circuits need a few dozen cycles between scans to recover, but this typically isn't a problem.

 

  • Like 1
Link to comment
Share on other sites

  • 6 months later...

it is a little bit complicated. if you are inclined, you can look at these sources:

https://github.com/pkali/scorch_src/blob/master/Atari/lib/5200SYS.ASM  <-- definitions

https://github.com/pkali/scorch_src/blob/12eebaedecce6cd993163298bfd17cb41523b6a6/Atari/interrupts.asm#L245  <-- VBL routine to read controllers

https://github.com/pkali/scorch_src/blob/12eebaedecce6cd993163298bfd17cb41523b6a6/Atari/interrupts.asm#L318 <-- keyboard interrupt continuation

I am sure there is a simpler example out there.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
On 3/18/2024 at 11:19 PM, pirx said:

it is a little bit complicated. if you are inclined, you can look at these sources:

Thanks, that helps.

 

I'm trying to read KBCODE to get the keypad numbers by polling it in a loop. In MAME the numbers  keys register, but on real hardware it remains zero.  (it doesn't work in Altirra either)

 

Is there a technique with reading KBCODE? Something I have to do first like writing to CONSOL or a timing requirement as with reading POT?

Link to comment
Share on other sites

You do have to write to CONSOL to select the controller and allow enough time for the controller to poll, but the #1 mistake is writing %11 to the low bits of SKCTL. That's how you configure the keyboard for the computer, but the 5200 requires debounce to be off (%10).

 

  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...