8/24/2016: MediaWiki software and PHP version updated. Please let me know if you find any problems.. Also, extensive Template cleanup is in progress based on new things I have learned on another wiki project. I will update the Help files when I get it all done. --OS-9 Al

8/30/2016: Massive re-work is being done on the InfoBox Templates. Read that page to keep up with the plan for that, and adding better keyword tags (categories) to all the pages. --OS-9 Al (talk) 15:28, 31 August 2016 (CDT)

Sampling

From CoCopedia - The Tandy/Radio Shack Color Computer Wiki
Jump to: navigation, search
WELCOME
Looking for CoCo help? If you are trying to do something with your old Color Computer, read this quick reference. Want to contribute to this wiki? Be sure to read this first. This CoCo wiki project was started on October 29, 2004. --OS-9 Al

See Recent Changes. | About this site. | Join the E-Mail List, Facebook Group or Google+ | Contact me with updates/questions.

This page was last updated on 09/10/2015. Total Pages: 530. Total Files: 760.


Home / Articles - Sampling


Joystick Sampling

Successive Linear Approximation on the Color Computer

The CoCo uses analog joysticks. A 5 volt signal is sent to the joystick and a smaller voltage is returned depending on the orientation of the stick. The job of reading the joystick is converting that analog voltage to a binary value. During this article it would be advisable to obtain a schematic of the Color Computer. Here is a detailed block diagram that will be good enough for this article. Also, a good understanding of the Peripheral Interface Adaptor will help you get the most out of machine language programming.

Since the CoCo does not have a hardware Analog-To-Digital converter chip, the process it uses is called successive linear approximation.

Here is the process:

  1. Mute the CoCo.
  2. Select a joystick.
  3. Write a zero to the sound register
  4. Compare the sound register to the value from the joystick.
  5. If the sound register is greater than or equal to the joystick value, then you have just discovered the joystick value
  6. Increment sound register.
  7. Sound register only goes to 64, so stop when you hit 65.
  8. Go back to step 4.

Mute the CoCo

During this process you are writing to the sound register. You must mute the CoCo so none of this is audible. The CoCo's mute switch is controlled by line CB2 of PIA 2. CB2 (like the other 19 lines) is a bidirectional line. This means the line can either output data or input data. For this excersize we need to setup CB2 to output data. We do this by setting bits 4 and 5 of control register B.

BEGIN   LDA  $FF23  Get current Control Register B value of PIA 2
        ORA  #$30   Set CB2 to be an output. (Set bits 4 and 5.)

Now the status of bit 3 of Control Register B will control the CB2 line. If bit 3 is low the line will be low. If bit 3 is high the line will be high. Setting CB2 low will mute the CoCo.

        ANDA  #$F7   Clear bit 3 - Mute CoCo
        STA   $FF23  Write value back to Control Register B

Select a joystick

There are two josticks with two axes each. This is four different joystick values to read.

Position Joystick
0 Right, Horiz
1 Right, Vert
2 Left, Horiz
3 Left, Vert

Four positions can be encoded in 2 bits:

Binary Decimal
00 0
01 1
10 2
11 3

There is a four position selector switch inside the CoCo. It is controlled by CA2 and CB2 of PIA 1. CA2 represents the most significant bit of the position, and CB2 represents the least significant bit. These lines will need to be configured as outputs and then the lines themselves will be need to be set to the proper value.

        LDA  $FF01  Get current Control Register A value of PIA 1
        LDB  $FF03  Get current Control Register B value of PIA 1
        ORA  #$30   Set CA2 to be an output. (Set bits 4 and 5 of CRA.)
        ORB  #$30   Set CB2 to be an output. (Set bits 4 and 5 of CRB.)
        ANDA #$F7   Set CA2 low. (Clear bit 3.)
        ORB  #$08   Set CB2 high. (Set bit 3.)
        STA  $FF01  Store value back in CRA.
        STB  $FF03  Store value back in CRA.

CA2 low and CB2 high means we are selecting position 1 which corresponds to the vertical axis of the right joystick.

Write a zero to the sound register

The CoCo's sound register is 6 bits of side A of PIA 2. Specifically bits 2 thru 7 of the Data Register A. This corresponds to lines PA2 thru PA7. These lines need to be configured as outputs so their signals will go to the digital to analog converter.

        LDA  $FF21   Load Control Register A of PIA 2
        ANDA #$FB    Engage Data Direction Register A. (Clear bit 2.)
        STA  $FF21   Store value back in CRA.
        LDA  $FF20   Load Data Direction Register A of PIA 2.
        ORA  #$FC    Set lines PA2 thru PA7 as output. (Set bits 2 thru 7.)
        STA  $FF20   Store value back in DDRA.

Address $FF20 is both the Data Register A and the Data Direction Register A. It's function is controller by bit 2 of the Control Register A. So in order to actually write data to the CoCo's sound register we need to modify the control register so we can access the data register.

        LDA  $FF21   Load Control Register A of PIA 2
        ORA  #$04    Engage Data Register A. (Set bit 2.)
        STA  $FF21   Store value back in CRA.

Clearing bits 2 thru 7 of the Data Register A of PIA 2 will write a zero to the sound register.

        LDB  $FF20   Load Data Register A of PIA 2.
        ANDB #$03    Write a zero to the sound register. (Clear bits 2 thru 7.)

Compare the sound register to the value from the joystick

The comparator chip inside the CoCo has two analog inputs and one digital output. The two inputs are labeled plus and minus. The plus input is connected to the four position switch that connects to the four different joystick axis. The minus input is connected to the output of the digital to analog converted that is controlled by bits 2 thru 7 of side A of PIA 2. The comparator will output a high signal if the plus input is higher than the minus input. Otherwise the comparator will output a low signal. The comparator output is connected to line PA7 of PIA 1. We need to setup this line to be an input.

        LDA  $FF01   Load Control Register A of PIA 1
        ANDA #$FB    Engage Data Direction Register A. (Clear bit 2.)
        STA  $FF01   Store value back in CRA.
        LDA  $FF00   Load Data Direction Register A of PIA 1.
        ANDA #$7F    Set line PA7 to be an input. (Clear bit 7.)
        STA  $FF00   Store value back in DDRA.

Modify the Control Register A to engage the Data Register A at $FF00.

        LDA  $FF01   Load Control Register A of PIA 1
        ORA  #$04    Engage Data Register A. (Set bit 2.)
        STA  $FF01   Store value back in CRA.

Read the output from the comparator.

LOOP    STB  $FF20   Store value in Sound Register
        LDA  $FF00   Bit 7 of register A contains the output information from the comparator.

If bit 7 is high, then we have found the current value.

       BPL DONE   Branch if negative condition is set to label DONE.

Increment sound register

Increment Sound register value by one.

        ADDB #$04   Increment Sound register by one value

Check for a carry. The sound register will overflow after 64 tries.

        BCS  DONE    Branch to label DONE if carry bit set.
        BRA  LOOP    Branch back to the label LOOP

We're done. The value of the joystick is in the sound register. We need to shift the data over two bits to normalize it to 1 thru 64.

DONE    LSRB         Logical shift right
        LSRB         Logical shift right

Now subtract one to bring it to 0 thru 63.

        DECB

Write value to video memory to see it.

        ORB  #$80    Set bit 7 to turn it into a VDG graphic character
        STA  $500    Put value into middle of the 32 collum screen.
        SWI	     Return to ZBUG.
        END

Colophon

As you can see, reading the joystick is quite involved. It is interesting to note that Color BASIC will attempt to read the joystick value ten times but will immediately return once it gets the same value twice from this process. If all ten tries produce different results, the tenth is returned.

Another quirk is BASIC will sample all four joysticks when joystick zero is read. Reading joysticks 1 thru 3 will return values cached during the sampling done when issuing the JOYSTK(0) command.