2020: NameSpaces are going to be implemented this year to better separate content. OS-9 Al (talk) 11:18, 15 April 2020 (CDT)
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)
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.
Since the CoCo does not have a hardware Analog-To-Digital chip, the process is does using "Successive Approximation".
Here is the process:
- Mute the CoCo.
- Select a joystick.
- Write a zero to the sound register
- Compare the sound register to the value from the joystick.
- if the sound register is greater than or equal to the joystick value, then you have just discovered the joystick value
- Increment sound register.
- Sound register only goes to 64, so stop then you hit 65.
- 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.
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.
Four positions can be encoded in 2 bits:
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 $FF21 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 $FF20 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 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 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 LSRA ORA $80 Set bit 7 to turn it into a VDG graphic character STA $400 Put value onto the top left corner screen. SWI tell ZBUG we are done.