There are currently two advanced methods of writing to a Sound Chip Register.
The AY register must be selected before the value may be written to it. The following code sets up the Volume register of channel A (Register 8).
LDA #08 STA via_porta LDA #ayc_Register STA via_pcr LDA #ayc_Inactive STA via_pcr
The standard way of writing the register value is as follows
LDA RegisterValue STA via_porta LDA #ayc_Write STA via_pcr LDA #ayc_Inactive STA via_pcr
The following chapters describe technique to reduce these lines of code. The techniques are only useful when writing repeatedly to the same AY register, such as when playing a sample.
Under Euphoric and some Real Machines simply setting the AY Control state to ayc_Write will allow an infinite stream of register values to be placed on via_porta.
However roughly 50% of real Oric Machines crash as a consequence of keeping the control line high for too long.
CA2 and CB2 are the VIA's control lines that tell the AY what the data is in via_porta. Internally to the VIA, CA2 is associated to Port A and CB2 to Port B. The following logic table defines the 4 states.
CA2 | CB2 | AY Data State |
---|---|---|
0 | 0 | Inactive |
0 | 1 | Write AY Register Data |
1 | 0 | Read AY Register Data |
1 | 1 | Write AY Register Number |
This says that when CB2 is high and CA2 is low, data placed on via_porta will be taken by the AY as data for the last register accessed.
Both CA2 and CB2's logic levels are set in via_pcr (VIA Peripheral Control Register).
Bits | Field |
---|---|
0 | CA1 Control |
1-3 | CA2 Control |
4 | CB1 Control |
5-7 | CB2 Control |
Bit Pattern | Operation | Effective Output |
---|---|---|
000 | Input Negative Active Edge | 1 |
001 | Independant Interrupt Input Negative Active Edge | 1 |
010 | Input Positive Active Edge | 1 |
011 | Independant Interrupt Input Positive Active Edge | 1 |
100 | Handshake Output | - |
101 | Pulse Output(Pulse Mode) | - |
110 | Low Output | 0 |
111 | High Output | 1 |
The Pulse Mode setting in via_pcr can be applied to CB2. This special mode means that whenever Port B is written to (or read from ) CB2 will be momentarily sent high.
We can therefore use this to optimise our code for sending data to the AY Register. However we will first need to set up the mode. We can do this as part of setting the register number.
LDA #08 STA via_porta LDA #ayc_Register STA via_pcr LDA #%10101100 STA via_pcr
Then to write to the Volume Register of Channel A.
LDA RegisterValue STA via_porta LDA via_portb
To disable this special mode.
LDA #ayc_Inactive STA via_pcr
With this technique it is possible to free the processor as much as just writing the value for the register to a single location. However this technique requires alot of extra lines of code to setup the system (in order to make it compatible with both the Euphoric Emulator and the real Machine).
Inside the VIA is a location called the Shift Register. This can shift out a series of bits onto CB2. The VIA's ACR (Auxilliary Control Register) or via_acr is used to control the behaviour of the Shift register.
Bits | Field |
---|---|
0 | Port A Latch |
1 | Port B Latch |
2-4 | Shift Register Control |
5 | Timer 2 Control |
6-7 | Timer 1 Control |
Bit Pattern | Operation |
---|---|
000 | Disabled |
001 | Shift in under control of T2 |
010 | Shift in under control of CPU clock |
011 | Shift in under control of external Clock |
100 | Shift out free running at T2 Rate |
101 | Shift out under control of T2 |
110 | Shift out under control of CPU clock |
111 | Shift out under control of external Clock |
We can select mode 100 which will continuously shift out the pattern of bits held in the shift register. Each bit that is shifted out will be automatically fed back into the left of the register constantly rotating its contents.
If we write 10101010 to the Shift register then a constant stream of pulses can be generated onto CB2. This means that we now only have to write the value to via_porta and the hardware will continuelly send it to the AY register.
Unfortunately the Euphoric Emulator does not support the Shift Register.
However once the shifting has been initiated it will override any change to the logic state of CB2 set in via_pcr.
So we can simply set CB2 high in via_pcr after starting the shift register. This means this technique will now work for all machines.
So first we set up the technique (which we will do as part of setting up the register).
;Set the Register Number LDA #08 STA via_porta LDA #ayc_Register STA via_pcr LDA #ayc_Inactive STA via_pcr ;Set Timer 2 very low LDA #1 STA via_t2ll STA via_t2ch ;Store value in shift register LDA #%10101010 STA via_sr ;Set ACR Shift mode to 100 (but don't upset other bits) LDA via_acr AND #%11100011 ORA #%00010000 STA via_acr ;Finally Set CB2 high in PCR LDA #ayc_Write STA via_pcr
And now we simply do the following to write the AY register value
LDA RegisterValue STA via_porta
To disable this special mode we must first disable the Shift register then set the control lines low.
LDA via_acr AND #%11100011 STA via_acr LDA #ayc_Inactive STA via_pcr