MediaWiki:Sitenotice:
2024-03-02: The wiki ran out of disk space, so things were not working. This has been resolved by adding another 5GB of quota ;-) Thanks to Tim Lindner for reporting the issues. 2020-05-17: If a page gives you an error about some revision not being found, just EDIT the page and the old page should appear in the editor. If it does, just SAVE that and the page should be restored. OS-9 Al (talk) 12:22, 17 May 2020 (CDT)

Sampling: Difference between revisions

From CoCopedia - The Tandy/Radio Shack Color Computer Wiki
Jump to navigation Jump to search
Line 98: Line 98:
         LDA  $FF01  Load Control Register A of PIA 1
         LDA  $FF01  Load Control Register A of PIA 1
         ANDA #$FB    Engage Data Direction Register A. (Clear bit 2.)
         ANDA #$FB    Engage Data Direction Register A. (Clear bit 2.)
         STA  $FF21   Store value back in CRA.
         STA  $FF01   Store value back in CRA.
         LDA  $FF00  Load Data Direction Register A of PIA 1.
         LDA  $FF00  Load Data Direction Register A of PIA 1.
         ANDA #$7F    Set line PA7 to be an input. (Clear bit 7.)
         ANDA #$7F    Set line PA7 to be an input. (Clear bit 7.)
         STA  $FF20   Store value back in DDRA.
         STA  $FF00   Store value back in DDRA.


Modify the Control Register A to engage the Data Register A at $FF00.
Modify the Control Register A to engage the Data Register A at $FF00.
Line 108: Line 108:
         ORA  #$04    Engage Data Register A. (Set bit 2.)
         ORA  #$04    Engage Data Register A. (Set bit 2.)
         STA  $FF01  Store value back in CRA.
         STA  $FF01  Store value back in CRA.
== Compare the sound register to the value from the joystick ==
== Compare the sound register to the value from the joystick ==
Read the output from the comparator.
Read the output from the comparator.

Revision as of 15:55, 8 April 2005

Joystick Sampling

Successive 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 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 then 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.

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 configures 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.

        LDA  $FF20   Load Data Register A of PIA 2.
        ANDA #$03    Write a zero to the sound register. (Clear bits 2 thru 7.)
        STA  $FF20   Store value back in DRA.

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.

Compare the sound register to the value from the joystick

Read the output from the comparator.

LOOP   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.

       BCS DONE   Branch of carry condition is set to label DONE.

Increment sound register

Increment Sound register value by one.

        LDA  $FF20  Load Data Register A of PIA 2
        ADDA #$04   Increment Sound register by one value
        STA  $FF20   Store value back in register

Check for overflow. The sound register will overflow after 64 values.

        BVS  DONE  Branch to label DONE if overflow bit set.
        STA  $FF20  Store value back in DRA
        BRA  LOOP  Branch back to the label LOOP

We're done. The value of the joystick is in the sound register.

DONE LDA $FF20  Load the sound register into register A.

We need to shift the data over two bits to normalize it to 0 thru 63.

        LSRA     Logical shift right
        LSRA     Logical shift right

Write value to video memory to see it.

        ORA $80   Set bit 7 to turn it into a VDG graphic character
        STA $5FF  Put value into the bottom right corner 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 and will only return once it gets the same value twice from this process.