;**************************************************************************
;
; Filename : LED.ASM
; ------------------
;
; (c) 1990 by Thomas Lehmann
;
;
; Jackophone V5.00
;
;
; LED Routinen
;
;
;**************************************************************************


; =========================================================================
;
; Type	: Tabelle
;
;	: Referenztabelle der LED-Achsen zur Tastatur-Matrix
;	: Offset auf LED-RAM für die rechte (erste) LED
;
;
; (c) by Thomas Lehmann Feb. 1990
;
; =========================================================================

	; Offset für Y0
	; -------------

led_tab	.byte YL0	; X0/Y0 = XL0-1/YL0
	.byte YL0+2	; X1/Y0 = XL2-3/YL0
	.byte YL0+4	; X2/Y0 = XL4-5/YL0
	.byte YL0+6	; X3/Y0 = XL6-7/YL0
	.byte YL0+8	; X4/Y0 = XL8-9/YL0
	.byte YL0+$0a	; X5/Y0 = XLA-B/YL0
	.byte OFFS_NO_LED	; X6/Y0 = keine LED
	.byte OFFS_NO_LED	; X7/Y0 = keine LED
	.byte OFFS_NO_LED	; X8/Y0 = keine LED
	.byte OFFS_NO_LED	; X9/Y0 = keine LED
	.byte OFFS_NO_LED	; XA/Y0 = keine LED
	.byte OFFS_NO_LED	; XB/Y0 = keine LED
	.byte OFFS_NO_LED	; XC/Y0 = keine LED
	.byte OFFS_NO_LED	; XD/Y0 = keine LED
	.byte YL3+8	; XE/Y0 = XL8-9/YL3

	; Offset für Y1
	; -------------

	.byte YL1+$00	; X0/Y1 = XL0-1/YL1
	.byte YL1+$02	; X1/Y1 = XL2-3/YL1
	.byte YL1+$04	; X2/Y1 = XL4-5/YL1
	.byte YL1+$06	; X3/Y1 = XL6-7/YL1
	.byte YL1+$08	; X4/Y1 = XL8-9/YL1
	.byte YL1+$0a	; X5/Y1 = XLA-B/YL1
	.byte OFFS_NO_LED	; X6/Y1 = keine LED
	.byte OFFS_NO_LED	; X7/Y1 = keine LED
	.byte OFFS_NO_LED	; X8/Y1 = keine LED
	.byte OFFS_NO_LED	; X9/Y1 = keine LED
	.byte OFFS_NO_LED	; XA/Y1 = keine LED
	.byte OFFS_NO_LED	; XB/Y1 = keine LED
	.byte OFFS_NO_LED	; XC/Y1 = keine LED
	.byte OFFS_NO_LED	; XD/Y1 = keine LED
	.byte YL4+$08	; XE/Y1 = XL8-9/YL4

	; Offset für Y2
	; -------------

	.byte YL2+$00	; X0/Y2 = XL0-1/YL2
	.byte YL2+$02	; X1/Y2 = XL2-3/YL2
	.byte YL2+$04	; X2/Y2 = XL4-5/YL2
	.byte YL2+$06	; X3/Y2 = XL6-7/YL2
	.byte YL2+$08	; X4/Y2 = XL8-9/YL2
	.byte YL2+$0a	; X5/Y2 = XLA-B/YL2
	.byte OFFS_NO_LED	; X6/Y2 = keine LED
	.byte OFFS_NO_LED	; X7/Y2 = keine LED
	.byte OFFS_NO_LED	; X8/Y2 = keine LED
	.byte OFFS_NO_LED	; X9/Y2 = keine LED
	.byte OFFS_NO_LED	; XA/Y2 = keine LED
	.byte OFFS_NO_LED	; XB/Y2 = keine LED
	.byte OFFS_NO_LED	; XC/Y2 = keine LED
	.byte OFFS_NO_LED	; XD/Y2 = keine LED
	.byte YL5+$08	; XE/Y2 = XL8-9/YL5

	; Offset für Y3
	; -------------

	.byte YL3+$00	; X0/Y3 = XL0-1/YL3
	.byte YL3+$02	; X1/Y3 = XL2-3/YL3
	.byte YL3+$04	; X2/Y3 = XL4-5/YL3
	.byte YL3+$06	; X3/Y3 = XL6-7/YL3
	.byte OFFS_NO_LED	; X4/Y3 = keine LED
	.byte OFFS_NO_LED	; X5/Y3 = keine LED
	.byte OFFS_NO_LED	; X6/Y3 = keine LED
	.byte OFFS_NO_LED	; X7/Y3 = keine LED
	.byte OFFS_NO_LED	; X8/Y3 = keine LED
	.byte OFFS_NO_LED	; X9/Y3 = keine LED
	.byte OFFS_NO_LED	; XA/Y3 = keine LED
	.byte OFFS_NO_LED	; XB/Y3 = keine LED
	.byte OFFS_NO_LED	; XC/Y3 = keine LED
	.byte OFFS_NO_LED	; XD/Y3 = keine LED
	.byte YL3+$0a	; XE/Y3 = XLA-B/YL3

	; Offset für Y4
	; -------------

	.byte YL4+$00	; X0/Y4 = XL0-1/YL4
	.byte YL4+$02	; X1/Y4 = XL2-3/YL4
	.byte YL4+$04	; X2/Y4 = XL4-5/YL4
	.byte YL4+$06	; X3/Y4 = XL6-7/YL4
	.byte OFFS_NO_LED	; X4/Y4 = keine LED
	.byte OFFS_NO_LED	; X5/Y4 = keine LED
	.byte YL1+$0c	; X6/Y4 = XLC-D/YL1
	.byte YL1+$0e	; X7/Y4 = XLE-F/YL1
	.byte YL0+$0c	; X8/Y4 = XLC-D/YL0
	.byte YL0+$0e	; X9/Y4 = XLE-F/YL0
	.byte YL1+$0c	; XA/Y4 = XLC-D/YL1
	.byte YL1+$0e	; XB/Y4 = XLE-F/YL1
	.byte YL0+$0c	; XC/Y4 = XLC-D/YL0
	.byte YL0+$0e	; XD/Y4 = XLE-F/YL0
	.byte YL4+$0a	; XE/Y4 = XLA-B/YL4

	; Offset für Y5
	; -------------

	.byte YL5+$00	; X0/Y5 = XL0-1/YL5
	.byte YL5+$02	; X1/Y5 = XL2-3/YL5
	.byte YL5+$04	; X2/Y5 = XL4-5/YL5
	.byte YL5+$06	; X3/Y5 = XL6-7/YL5
	.byte OFFS_NO_LED	; X4/Y5 = keine LED
	.byte OFFS_NO_LED	; X5/Y5 = keine LED
	.byte YL3+$0c	; X6/Y5 = XLC-D/YL3
	.byte YL3+$0e	; X7/Y5 = XLE-F/YL3
	.byte YL2+$0c	; X8/Y5 = XLC-D/YL2
	.byte YL2+$0e	; X9/Y5 = XLE-F/YL2
	.byte YL3+$0c	; XA/Y5 = XLC-D/YL3
	.byte YL3+$0e	; XB/Y5 = XLE-F/YL3
	.byte YL2+$0c	; XC/Y5 = XLC-D/YL2
	.byte YL2+$0e	; XD/Y5 = XLE-F/YL2
	.byte YL5+$0a	; XE/Y5 = XLA-B/YL5

	; Offset für Y6
	; -------------

	.byte YL6+$00	; X0/Y6 = XL0-1/YL6
	.byte YL6+$02	; X1/Y6 = XL2-3/YL6
	.byte YL6+$04	; X2/Y6 = XL4-5/YL6
	.byte YL6+$06	; X3/Y6 = XL6-7/YL6
	.byte OFFS_NO_LED	; X4/Y6 = keine LED
	.byte OFFS_NO_LED	; X5/Y6 = keine LED
	.byte YL5+$0c	; X6/Y6 = XLC-D/YL5
	.byte YL5+$0e	; X7/Y6 = XLE-F/YL5
	.byte YL4+$0c	; X8/Y6 = XLC-D/YL4
	.byte YL4+$0e	; X9/Y6 = XLE-F/YL4
	.byte YL5+$0c	; XA/Y6 = XLC-D/YL5
	.byte YL5+$0e	; XB/Y6 = XLE-F/YL5
	.byte YL4+$0c	; XC/Y6 = XLC-D/YL4
	.byte YL4+$0e	; XD/Y6 = XLE-F/YL4
	.byte OFFS_NO_LED	; XE/Y6 = keine LED

	; Offset für Y7
	; -------------

	.byte OFFS_NO_LED	; X0/Y7 = keine LED
	.byte OFFS_NO_LED	; X1/Y7 = keine LED
	.byte OFFS_NO_LED	; X2/Y7 = keine LED
	.byte OFFS_NO_LED	; X3/Y7 = keine LED
	.byte OFFS_NO_LED	; X4/Y7 = keine LED
	.byte OFFS_NO_LED	; X5/Y7 = keine LED
	.byte OFFS_NO_LED	; X6/Y7 = XLC-D/YL7 , Sonder-Relais
	.byte OFFS_NO_LED	; X7/Y7 = XLE-F/YL7 , Sonder-Relais
	.byte OFFS_NO_LED	; X8/Y7 = XLC-D/YL6 , Sonder-Relais
	.byte OFFS_NO_LED	; X9/Y7 = XLE-F/YL6 , Sonder-Relais
	.byte OFFS_NO_LED	; XA/Y7 = XLC-D/YL7 , Sonder-Relais
	.byte OFFS_NO_LED	; XB/Y7 = XLE-F/YL7 , Sonder-Relais
	.byte OFFS_NO_LED	; XC/Y7 = XLC-D/YL6 , Sonder-Relais
	.byte OFFS_NO_LED	; XD/Y7 = XLE-F/YL6 , Sonder-Relais
	.byte OFFS_NO_LED	; XE/Y7 = keine LED


; =========================================================================
;
; Type	: Unterprg.
;	: LED`s schalten
;
; Eingang	: Offset auf Tastaturberechtigung in X-Reg
;	: LED Status im Accu:
;	: BIT 0-2 Blinkwert
;	: BIT 3   LED rechts
;	: BIT 4   LED links
;
; Ausgang	: zur Taste gehörige LED entsprechend geschaltet
;
; Register	: Accu
;
; (c) by Thomas Lehmann Feb. 1990
;
; =========================================================================

set_led	phx		; Offset auf Tastenberechtigung retten
	phy		; Register retten

	pha		; Statusregister retten
	txa		; Offset auf Tastenber. kopieren
	lsr		; Tastennr. berechnen
	tax
	ldy led_tab,x	; Offset auf LED-RAM holen
	pla		; Statusregister zurück
	bit #LD_R	; rechte LED bearbeiten ?
	beq set_led_l	; nein, linke LED testen

	jsr set_led_1	; LED setzen

set_led_l	bit #LD_L	; linke LED bearbeiten ?
	beq set_led_end	; nein, Ende

	iny		; Offset auf linke LED erzeugen
	jsr set_led_1	; LED setzen

set_led_end	ply		; Register zurück
	plx
	rts


	; LED schalten
	; ------------
set_led_1	pha		; Statusregister retten
	and #%00000111	; Blinkwert ausfiltern
	tax
	lda led_bl_tab,x	; Blinkwert laden
	sta led_ram,y	; und LED setzen
	pla		; Status zurück
	rts

led_bl_tab	.byte $00	; LED aus
	.byte $ff	; LED ein
	.byte %00001111	; LED blinkt normal
	.byte %00110011	; LED blinkt schnell
	.byte %01010101	; LED blinkt sehr schnell


; =========================================================================
;
; Type	: Unterprg.
;	: Taste aus Berechtigung ermitteln und LED`s der zugehörigen
;	: Tasten schalten
;
; Eingang	: Tastenwert im Accu
;	: LED Status in Y
;
; Ausgang	: zur Taste gehörige LED entsprechend geschaltet
;
; Register	: ------
;
; (c) by Thomas Lehmann März 1990
;
; =========================================================================

set_prgled	phx		; Register retten
	ldx #00		; Offset auf Tastenberechtigung, erster TW

set_prg_lp	cmp tast_tab,x	; Tastenwert gefunden ?
	bne set_prg_nxt	; nein, nächste Taste einstellen

	pha		; Tastenwert retten
	tya		; LED-Status laden
	jsr set_led	; LED schalten
	pla		; Tastenwert zurück

set_prg_nxt	inx		; nächste Taste einstellen
	inx
	cpx #TAST_BER	; alle Tasten getestet ?
	bcc set_prg_lp	; nein, nächsten Tastenwert testen

	plx		; Register zurück
	rts		; alle berechtigten Tasten getestet



; =========================================================================
;
; Type	: Unterprg.
;	: komplettes LED-RAM mit Füllwert beschreiben
;
; Eingang	: Füllwert im Accu
;
; Ausgang	: LED-RAM mit Füllwert beschrieben
;
; Register	: X, Y
;
; (c) by Thomas Lehmann Feb. 1990
;
; =========================================================================

fill_ledram	ldx #<led_ram	; low Byte des LED-RAM`s laden
	stx ind_adr	; und setzen
	ldx #>led_ram	; dito high Byte
	stx ind_adr+1	; und setzen
	ldx #<LED_RAM_END	; low Byte LED-RAM Ende
	ldy #>LED_RAM_END	; dito high
	jmp fill_ram	; RAM füllen

