VIC-20 joystick - which is the real way?

Basic and Machine Language

Moderator: Moderators

Post Reply
allenhuffman
Vic 20 Amateur
Posts: 46
Joined: Fri May 21, 2010 11:50 pm
Website: http://www.subethasoftware.com
Location: Des Moines, Iowa, US
Occupation: Embedded programmer.

VIC-20 joystick - which is the real way?

Post by allenhuffman »

I recently obtained a copy of my first-ever published program (in a newsletter, heh) and found it odd I chose to use "Up" and "Down" on the joystick to move left and right. I did some research and discovered the routine I was using was incorrect, and never read the byte that contained the "right" direction. I must not have been able to get it to work, and just switched to using up and down, since those read for me.

I tried to figure out what magazine I got my joystick code from, and the only hit in Archive.org was some Toronto Pet (The TORPET) newsletter. It was so early in the VIC-20 era that they had an article mention that "15,000" had been sold so far ;-) Wild. Here is the VIC-20 column I found (page 31, or 46 in the PDF):

https://archive.org/details/TORPET_The_ ... 0++4+%2B32

And their code:

Code: Select all

9000 POKE 37154,127
9010 JO= (NOT((PEEK(37152)AND128) /8+( PEEK(37151)AND60)/4)) +32
9020 POKE 37154,255
9030 RETURN
This was identical to the code I had found, except they used 37152 and 37151 and my program had a typo where it was using 37152 twice.

BUT, as I searched to figure out the problem, I found an Atari Magazine reference to reading these two values:

Code: Select all

10 PRINT PEEK(37137), PEEK(37152) : GOTO 10
Those two match what I'd expect from the memory map. I see that 37137 is "Port A output register" and 37152 is "Port B output register".

37151 is listed as "Port A (Sense cassette switch)" and I don't see anything about the joystick there.

BUT ... the code works when using these two locations, OR the 37151/37152 locations. My test:

Code: Select all

10 rem fixed torpet code
20 gosub 9000
30 if jo and 1 then print "up ";
40 if jo and 2 then print "down ";
50 if jo and 4 then print "left ";
60 if jo and 8 then print "fire ";
70 if jo and 16 then print "right";
80 if jo<>0 then print
90 goto 20
9000 poke 37154,127
9010 rem jo=(not((peek(37152)and128)/8+(peek(37137)and60)/4))+32
9015 jo=(not((peek(37152)and128)/8+(peek(37151)and60)/4))+32
9020 poke 37154,255
9030 return
I have found examples showing the use of both sets of locations. The wiki here shows it that way as well:

http://sleepingelephant.com/denial/wiki ... p/Joystick

Code: Select all

1 S=PEEK(37151):POKE37154,127:T=PEEK(37152):POKE37154,255
2 IF (128ANDT)=0 THEN PRINT "RIGHT"
3 IF (16ANDS)=0 THEN PRINT "LEFT"
4 IF (8ANDS)=0 THEN PRINT "DOWN"
5 IF (4ANDS)=0 THEN PRINT "UP"
6 IF (32ANDS)=0 THEN PRINT "FIRE"
7 GOTO 1
I did not know much about VIC-20 hardware during the time I had one, but on my Radio Shack Color Computer I am aware that there were certain I/O registers that were ghosted, and would appear in multiple places. Is something like this happening here?

After finding so many places saying "use A and B" and other places saying "use A and C" and they both work, I'm a bit confused ;-)

Thanks, much.
User avatar
Soloman
Vic 20 Amateur
Posts: 65
Joined: Fri Sep 22, 2023 1:46 am
Location: Bilthoven, Netherlan
Occupation: Data-analyst

Re: VIC-20 joystick - which is the real way?

Post by Soloman »

I saw a ping-pong-game with paddles. That is a step further in coding. :?
User avatar
Mike
Herr VC
Posts: 5130
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: VIC-20 joystick - which is the real way?

Post by Mike »

allenhuffman wrote:I did not know much about VIC-20 hardware during the time I had one, but on my Radio Shack Color Computer I am aware that there were certain I/O registers that were ghosted, [...]
... or mirrored ...
[...] and would appear in multiple places. Is something like this happening here?
Not here, in a certain sense. The addresses in question belong to the same 16 byte register block of VIA #1, $9110..$911F.

37137 ($9111) and 37151 ($911F) both contain the ORA, i.e. the port A data register, with the same meaning of the port bits. Their difference is that $9111 controls the data handshake, whereas $911F has no effect on the data handshake. For more details, please refer to one of the original data sheets.

I prefer the use of 37151 in a joystick routine simply because it is in closer proximity to the other two necessary registers in VIA #2, 37152 (ORB, $9120, contains the joystick right direction) and 37154 (DDRB, $9122, bit 7 - value 128 - temporarily set to input).
allenhuffman
Vic 20 Amateur
Posts: 46
Joined: Fri May 21, 2010 11:50 pm
Website: http://www.subethasoftware.com
Location: Des Moines, Iowa, US
Occupation: Embedded programmer.

Re: VIC-20 joystick - which is the real way?

Post by allenhuffman »

Soloman wrote: Sun Feb 11, 2024 12:58 pm I saw a ping-pong-game with paddles. That is a step further in coding. :?
I remember using my Uncle’s Atari VCS (as it was known back then) joystick in my VIC, but I never owned a joystick for it. I do not recall ever playing any paddle games, or even realizing the VIC-20 could support the Atari paddles. I believe they were rotary encoders—was that even possible to use from BASIC on the VIC?
allenhuffman
Vic 20 Amateur
Posts: 46
Joined: Fri May 21, 2010 11:50 pm
Website: http://www.subethasoftware.com
Location: Des Moines, Iowa, US
Occupation: Embedded programmer.

Re: VIC-20 joystick - which is the real way?

Post by allenhuffman »

Mike wrote: Sun Feb 11, 2024 2:37 pm
allenhuffman wrote:I did not know much about VIC-20 hardware during the time I had one, but on my Radio Shack Color Computer I am aware that there were certain I/O registers that were ghosted, [...]
... or mirrored ...
[...] and would appear in multiple places. Is something like this happening here?
Not here, in a certain sense. The addresses in question belong to the same 16 byte register block of VIA #1, $9110..$911F.

37137 ($9111) and 37151 ($911F) both contain the ORA, i.e. the port A data register, with the same meaning of the port bits. Their difference is that $9111 controls the data handshake, whereas $911F has no effect on the data handshake. For more details, please refer to one of the original data sheets.

I prefer the use of 37151 in a joystick routine simply because it is in closer proximity to the other two necessary registers in VIA #2, 37152 (ORB, $9120, contains the joystick right direction) and 37154 (DDRB, $9122, bit 7 - value 128 - temporarily set to input).
I love how terms are sort of localized such as “ghosted” versus “mirrored”. When I had my VIC, I remember a kid in Houston showing me his VIC-20 “monitor” cartridge. On the CoCo, it was an “assembler” cartridge. I wish I had learned assembly/machine language earlier. I don’t think I touched it until 1984-85 on my CoCo’s 6809.

For the data sheet, which part is this going to? I’ll go find it.

And if I understand: Both officially and correctly have that I/O bit, but one is a R/W and the other is just a R?
Merytsetesh
Vic 20 Amateur
Posts: 41
Joined: Sat Mar 02, 2024 8:57 pm
Location: Canada

Re: VIC-20 joystick - which is the real way?

Post by Merytsetesh »

allenhuffman wrote: Wed Mar 13, 2024 8:34 am I remember using my Uncle’s Atari VCS (as it was known back then) joystick in my VIC, but I never owned a joystick for it. I do not recall ever playing any paddle games, or even realizing the VIC-20 could support the Atari paddles. I believe they were rotary encoders—was that even possible to use from BASIC on the VIC?
Yes. $9008 (36872) gives paddle X and $9009 gives Y. Denial page all about paddles.
User avatar
JonBrawn
Vic 20 Devotee
Posts: 296
Joined: Sat Sep 11, 2021 10:47 pm
Website: http://youtube.com/@vicenary
Location: Austin TX USA
Occupation: CPU design engineer

Re: VIC-20 joystick - which is the real way?

Post by JonBrawn »

allenhuffman wrote: Wed Mar 13, 2024 8:34 am ... realizing the VIC-20 could support the Atari paddles. I believe they were rotary encoders—was that even possible to use from BASIC on the VIC?
Not rotary encoders, but variable resistors. There's a capacitor on the motherboard for each paddle, and that capacitor charges faster or slower depending on the resistance inside the paddle. The capacitor is connected to the VIC chip, which compares the voltage on the capacitor with a voltage reference (I've used 2.5V) and counts how long it takes to reach that point. The capacitors are discharged on every 8th raster line, and the counter is reset to zero.

There is a substantial amount of jitter in the values that you read from the paddle register of the VIC chip; some of this comes from inside the VIC, and some comes from the capacitor and variable resistor picking up noise from around the VIC circuit board.
Working on FPGA replacement for 6560/6561
https://youtube.com/@vicenary
Post Reply