
;Reading a value from a PSG register. 
;This routine can be used to read data from a PSG register. 

; entry conditions:
; c = register index
;
; exit conditions:
; a = register data
;
; b,c,f corrupt
; PPI port A is returned to output
;
; assumptions:
; PPI port A is set to output, PPI port C is set to output.

read_from_psg ld b,&f4   ; Setup register index on PPI port A
              out (c),c  ;

 ld  bc,&f6c0       ; Tell PSG to select register using data on PPI port A
 out (c),c          ;

 ld  bc,&f600       ; Put PSG into inactive state
 out (c),c

                    ;** Set PPI port A to input mode. **
 ld  b,&f7          ; 8255 PPI Control
 ld  c,%10010010    ; mode and port configuration
 out (c),c          ; Port A input, Port B input, Port C output
                    ; All operating in mode 0. (see Programming
                    ; 8255 PPI)

 ld  bc,&f640       ; Tell PSG to put the data of the selected register to PPI port A to
 out (c),c          ;

 ld b,&f4           ; Read data from PPI port A
 in a,(c)           ;

                    ;** Set PPI port A to output mode. *
 ld  b,&f7          ;8255 PPI control
 ld  c,%10000010
 out (c),c          ;Port A output, Port B input, Port C output

 ld  bc,&f600       ; Return PSG to inactive mode.
 out (c),c          ;
 ret


;Writing to a PSG register. 
;This routine can be used to write to registers on the PSG. 
;C contains the index of the PSG register to write.
;A contains the data to write to the PSG register. 

; entry conditions:
; C = register number
; A = register data
;
; exit conditions:
; b,C,F corrupt
;
; assumptions:
; PPI port A and PPI port C are setup in output mode.

write_to_psg ld  b,&f4  ; setup PSG register number on PPI port A
             out (c),c  ;

 ld  bc,&f6c0         ; Tell PSG to select register from data on PPI port A
 out (c),c            ;

 ld  bc,&f600         ; Put PSG into inactive state.
 out (c),c            ;

 ld  b,&f4            ; setup register data on PPI port A
 out (c),a            ;

 ld  bc,&f680         ; Tell PSG to write data on PPI port A into selected register
 out (c),c            ;

 ld  bc,&f600         ; Put PSG into inactive state
 out (c),c            ;
 ret

