*
* Program VOTER.ASM
* Dwayne Kincaid WD8OYG
* Version 1.4, April 1996
*
*      Version History:
*         V1.0 - Basic Voter Displays. 
*         V1.1 - Added Serial EEROM.         
*         V1.2 - Added COR/DIS checks and display refresh.
*         V1.3 - Added A/D reads.
*         V1.3.2 - COR and Disable used to get active channel.
*         V1.3.3 - Added Hits count, Best and Least.
*         V1.3.4 - Added COR Averaging.
*         V1.3.5 - Changed menu order.
*         V1.3.6 - Added RS-232 Functions.
*         V1.3.7 - Added Hysterisis Function.
*         V1.3.8 - Set update rate to 60 Hz.
*         V1.4.0 - First shipped.
*
*         Code developed on the SBC-8K from LDG Electronics, 
*         1445 Parran Road, St. Leonard MD 20685, 410-586-2177.
*
* Registers definitions for 68HC11
*
regbas  equ     $1000
porta   equ     $00
portb   equ     $04
ddrc    equ     $07
pactl   equ     $26
baud    equ     $2b
sccr1   equ     $2c
sccr2   equ     $2d
scsr    equ     $2e
scdr    equ     $2f
adctl   equ     $30
adr1    equ     $31
adr2    equ     $32
adr3    equ     $33
adr4    equ     $34
option  equ     $39   
xcmsout equ     $44

* For Serial EEPROM Subroutines
PORTD           EQU     $08
READ_CMD        EQU     003
SPIF_BIT        EQU     $80
CE_BIT          EQU     $20
WREN_CMD        EQU     006
WRITE_CMD       EQU     002
RDSR_CMD        EQU     005
DUMMY           EQU     $FF
SPDR            EQU     $2A
SPSR            EQU     $29
SPCR            EQU     $28
DDRD            EQU     $09

* 
*  XICOR Registers
portpa          equ     $0410
portpb          equ     $0408
cfgreg          equ     $0420
ddrpa           equ     $48

*  Constants
eot     equ     $04
true    equ     $01
false   equ     $00
adpu    equ     $80
ccf     equ     $80
TDRE    equ     $80
RDRF    equ     $20
cr      equ     $0d
lf      equ     $0a


*  Variables
	org     $0000           ORG on first page. Bytes 0-256.

cor_in          rmb     1      COR input.     00000000 binary.
chan            rmb     1      Channel. 
best_ch         rmb     1      Best A/D read. 00000000 binary.
mode            rmb     1      Mode of the LCD. 0-9.
sn_mode         rmb     1      Mode of S/N display. 0 = bar. 1 = number. 
chan_sel        rmb     1      Channel Selection.
dis_sel         rmb     1      Disable Selection.
cur_pos         rmb     1      Current Position of the LCD cursor.
disabled_h      rmb     1      Channels that are disabled from hardware.
disabled_t      rmb     1      Channels disabled from hardware or software.
disabled_t_old  rmb     1
active          rmb     1      Channels that enabled and have COR.
active_old      rmb     1      Old data. Used for updating.
best_ad         rmb     1      Value of best channel. 0-255.
best_ave        rmb     1      Best COR average channel. Binary.
bar             rmb     1      Value of bar meter.    0-10.
bar_old         rmb     1            
cor_flag        rmb     1      Boolean.
best_site       rmb     1      1,2,3,4,5,6,7,8
best_site_old   rmb     1
least_site      rmb     1      1,2,3,4,5,6,7,8
least_site_old  rmb     1
signal          rmb     5      Signel to Noise string buffer.
hit_string      rmb     5      xxx string buffer.
update_valid    rmb     1      Update vaild. Boolean.
update_count    rmb     1      Count for update of displays.
ave_count       rmb     1      Average counter.
hyst_value      rmb     1      Value of Hyst number.
select_ch       rmb     1      Selected Channel for Hyst.
select_ad       rmb     1      Selected A/D for Hyst.
single_count    rmb     1      Counter for Active CORs.

AD1         rmb     1               A to D inputs.
AD2         rmb     1
AD3         rmb     1
AD4         rmb     1
AD5         rmb     1
AD6         rmb     1
AD7         rmb     1
AD8         rmb     1

hits_1      rmb     1          Hits on that channel out of the last 100.
hits_2      rmb     1
hits_3      rmb     1
hits_4      rmb     1
hits_5      rmb     1
hits_6      rmb     1
hits_7      rmb     1
hits_8      rmb     1

ave_1       rmb     1          COR hits on that channel out of last 256.
ave_2       rmb     1
ave_3       rmb     1
ave_4       rmb     1
ave_5       rmb     1
ave_6       rmb     1
ave_7       rmb     1
ave_8       rmb     1

*       Located in Serial EEPROM.
cr_state    rmb     1          COR Active state.
dis_state   rmb     1          Disable Active state.
hyst        rmb     1          Hysterisis.
disabled_s  rmb     1          Channels that are disabled from software.

* Program code
	org     $E200           start program at External eeprom.
	lds     #$FF            set up stack.
	ldx     #regbas         offset for control registers.
	
	ldaa    #ddrpa          Program XICOR config reg to PA=output.
	staa    cfgreg
	
	ldaa    #$3f            Set up D register for spi.
	staa    DDRD,x          Port D pins for output.
	ldaa    #$50
	staa    SPCR,x          Mode = 0, clk = 1mhz.

	ldaa    #adpu           Initialize A/Ds.
	staa    option,x

	ldaa    #$88
	staa    pactl,x         Set DDRA3 and 7 to output.

	clr     sccr1,x         Set up RS-232 port for 1200 baud.
	ldd     #$330c
	staa    baud,x
	stab    sccr2,x

	jsr     initIface       Initialize LCD

*
*    Start of Voter Code
*

* Initialize some variables that will be used by the voter.

	ldaa    #$01            
	staa    best_ch         
	staa    select_ch
	staa    dis_sel
	staa    chan_sel
	staa    best_site
	staa    least_site
	staa    update_valid
	staa    sn_mode
	staa    best_ave

	ldaa    #eot       Set up signal and hit_string eot. 
	staa    signal+3
	staa    hit_string+3

	clr     hits_1     Clear hits count on power up.
	clr     hits_2
	clr     hits_3
	clr     hits_4
	clr     hits_5
	clr     hits_6
	clr     hits_7
	clr     hits_8

	clr     update_count

	ldaa    #$FF
	staa    select_ad

* Load variables from Serial EEPROM.        
*

	ldy     #$0010
	jsr     RD_BYTE
	staa    cr_state
	
	ldy     #$0011
	jsr     RD_BYTE
	staa    dis_state
	
	ldy     #$0012            Check for hyst > 9.
	jsr     RD_BYTE
	cmpa    #$09
	blo     hyst_ok
	ldaa    #$00
hyst_ok staa    hyst
	
	ldy     #$0013
	jsr     RD_BYTE
	staa    disabled_s

*       Print Start up screens.

	jsr     line1
	ldy     #mess1     Print Line 1 intro. 
	jsr     outstr     
	
	jsr     line2
	ldy     #mess2     Print Line 2 intro.
	jsr     outstr
	jsr     wait
	
	jsr     line1
	ldy     #mess1a     Print Line 1a intro. 
	jsr     outstr     
	
	jsr     line2
	ldy     #mess2a     Print Line 2a intro.
	jsr     outstr
	jsr     wait
	jsr     wait

	ldaa    #$00
	staa    mode
	jsr     print_hyst Jump to get set up.
	jmp     cur_vote   Jump to display current Vote after Advertising.

*                   
*       Main Loop. Checks switch inputs and jumps.
*
main_loop

	brclr   porta,x $03 reset_mode    Set Mode back to Current 
	brclr   porta,x $01 menu_sw       Update Menu switch.
	brclr   porta,x $02 sel_sh        Update Selection switch.
	brclr   porta,x $04 data_sh       Update Data switch.

	jsr     get_cor_dis               Get COR and DIS inputs.
	jsr     read_ad                   Read the A/D channels.
	jsr     set_current               Set Current voted on 4051s.
	
	jsr     update_display            Updates info on display.        
	bra     main_loop                 Go back to main loop.
	
sel_sh  jmp     sel_sw                    Needed for long jump.
data_sh jmp     data_sw                   Needed for long jump. 
rst_sh  jmp     reset_sel                 Needed for long jump. 

*
*       Next Switch. Checks for menu mode overflow and updates display.
*
menu_sw        
	ldaa    mode                  Load Mode.
	cmpa    #$09                  Check for last mode.
	beq     reset_mode            Jump to reset mode if last one.
	inca                          Increments mode if not last one.
	staa    mode                  Store it.
	bra     cur_vote              Jump to display 
reset_mode
	ldaa    #$00                  Reset to zero.
	staa    mode                  Store it.
	
cur_vote        
	ldaa    mode                  Check for mode = 00.
	cmpa    #$00
	bne     mode_01               Jump to next mode check if not.
	
	jsr     line2                 Go to line 2.
	ldy     #mess4                Load message Current Voted.
	jsr     outstr                Print it.

mode_01        
	ldaa    mode
	cmpa    #$01
	bne     mode_02
	
	jsr     line2
	ldy     #mess5                Print Current COR
	jsr     outstr

mode_02        
	ldaa    mode
	cmpa    #$02
	bne     mode_03
	
	jsr     line1                 Print S/N
	ldy     #mess13
	jsr     outstr
	
	jsr     line2
	ldy     #mess12
	jsr     outstr

mode_03       
	ldaa    mode
	cmpa    #$03
	bne     mode_04
	
	jsr     line2
	ldy     #mess7                Most Voted
	jsr     outstr

mode_04
	ldaa    mode
	cmpa    #$04
	bne     mode_05
	
	jsr     line2
	ldy     #mess8                Least Voted
	jsr     outstr

mode_05
	ldaa    mode
	cmpa    #$05
	bne     mode_06
	
	jsr     line1
	ldy     #mess16
	jsr     outstr

	jsr     line2
	ldy     #mess9                Print Hits per Channel
	jsr     outstr

mode_06
	ldaa    mode
	cmpa    #$06
	bne     mode_07
	
	jsr     line1
	ldy     #mess15                 Hyst
	jsr     outstr

	jsr     line2  
	ldy     #mess11
	jsr     outstr

mode_07        
	ldaa    mode
	cmpa    #$07
	bne     mode_08
	
	jsr     line2
	ldy     #mess6                 Print Disabled Channels
	jsr     outstr
	jsr     print_dis_cur

mode_08        
	ldaa    mode
	cmpa    #$08
	bne     mode_09
	
	jsr     line1
	ldy     #mess14               Disabled State.
	jsr     outstr
	
	jsr     line2
	ldy     #mess17
	jsr     outstr

mode_09       
	ldaa    mode
	cmpa    #$09
	bne     end_next
	
	jsr     line1
	ldy     #mess14               COR State.
	jsr     outstr

	jsr     line2
	ldy     #mess10
	jsr     outstr


end_next        
	ldaa    #true
	staa    update_valid
	jmp     main_loop

*
*     Select Switch. 
*

sel_sw
	ldaa    mode            If modes 00,01,03 or 04, do nothing.
	cmpa    #$00
	bne     sel_01
sel_01  
	cmpa    #$01
	bne     sel_02
sel_02  
	cmpa    #$02
	bne     sel_03
	jsr     sel_check
	bra     end_sel
sel_03  
	cmpa    #$03
	bne     sel_04
sel_04  
	cmpa    #$04
	bne     sel_05
sel_05
	cmpa    #$05
	bne     sel_06
	jsr     sel_check
	bra     end_sel
sel_06
	cmpa    #$06
	bne     sel_07
	jsr     hyst_check
	ldy     #$0012
	ldab    hyst
	jsr     WR_BYTE
	bra     end_sel

sel_07  
	cmpa    #$07
	bne     sel_08
	jsr     dis_check
	bra     end_sel
sel_08
	cmpa    #$08
	bne     sel_09
	
	jsr     sel_check
	bra     end_sel
sel_09
	cmpa    #$09
	bne     end_sel
	
	jsr     sel_check
	jsr     print_state
	bra     end_sel
	
reset_sel
	ldab    #$00            Reset channel selection.
	stab    chan_sel

end_sel   
	ldaa    #true
	staa    update_valid
	jsr     delay1
	jmp     main_loop

*
*     Data Switch. 
*
data_sw
	ldaa    mode            If modes 00,01,03,04,or 08, do nothing.
	cmpa    #$00
	bne     data_01
data_01 
	cmpa    #$01
	bne     data_02
data_02 
	cmpa    #$02
	bne     data_03
	jsr     sn_check
	bra     end_data
data_03 
	cmpa    #$03
	bne     data_04
data_04 
	cmpa    #$04
	bne     data_05
data_05
	cmpa    #$05
	bne     data_06
*        jsr     reset_hits        
	bra     end_data
data_06
	cmpa    #$06
	bne     data_07
	jsr     hyst_check
	ldy     #$0012
	ldab    hyst
	jsr     WR_BYTE
	bra     end_data
data_07 
	cmpa    #$07
	bne     data_08
	jsr     change_dis
	ldaa    disabled_t
	ldy     #$0013
	ldab    disabled_s
	jsr     WR_BYTE
	bra     end_data
data_08
	cmpa    #$08
	bne     data_09
	jsr     change_dis_state
	ldy     #$0011
	ldab    dis_state
	jsr     WR_BYTE
	bra     end_data
data_09
	cmpa    #$09
	bne     end_data
	jsr     change_state
	ldy     #$0010
	ldab    cr_state
	jsr     WR_BYTE
	bra     end_data
	
end_data   
	ldaa    #true
	staa    update_valid
	jsr     delay1
	jmp     main_loop


*   Check channel selection
sel_check
	lsl     chan_sel 
	ldab    chan_sel
	cmpb    #$00
	bne     sel_jmp
	ldab    #$01
	stab    chan_sel
sel_jmp jsr     print_sel
	rts

*   Check disabled selection
dis_check
	lsl     dis_sel 
	ldab    dis_sel
	cmpb    #$00
	bne     dis_jmp
	ldab    #$01
	stab    dis_sel
dis_jmp jsr     print_dis_cur
	rts

*   Check hysterisis selection
hyst_check
	inc     hyst 
	ldab    hyst
	cmpb    #$09
	blo     end_hyst_check
	ldab    #$00
	stab    hyst

end_hyst_check
	rts

*   Check S/N mode.    00 = Bar.      01 = Number.
sn_check
	ldy     #sn0               Clear bar from line.
	ldaa    #$07
	jsr     print_string
	
	ldaa    sn_mode
	cmpa    #$00
	bne     sn_check_1
	
	ldaa    #$01
	staa    sn_mode
	bra     end_sn_check

sn_check_1
	ldaa    #$00
	staa    sn_mode

end_sn_check
	rts

*
*       Print channels sent in accumulator A. For Cur_Voted, Cur_COR,
*                                             Disabled, Most, and Least.
print_chan
	staa    chan
	jsr     line1
	jsr     clearline

	
ck1     brclr   chan $01 ck2
	ldy     #char1
	ldaa    #$01
	jsr     print_string

ck2     brclr   chan $02 ck3
	ldy     #char2
	ldaa    #$03
	jsr     print_string

ck3     brclr   chan $04 ck4
	ldy     #char3
	ldaa    #$05
	jsr     print_string

ck4     brclr   chan $08 ck5
	ldy     #char4
	ldaa    #$07
	jsr     print_string

ck5     brclr   chan $10 ck6
	ldy     #char5
	ldaa    #$09
	jsr     print_string

ck6     brclr   chan $20 ck7
	ldy     #char6
	ldaa    #$0B
	jsr     print_string

ck7     brclr   chan $40 ck8
	ldy     #char7
	ldaa    #$0D
	jsr     print_string

ck8     brclr   chan $80 end_print_chan
	ldy     #char8
	ldaa    #$0F
	jsr     print_string

end_print_chan

	jsr     clr_cur
	rts

*
*    Print String
*    A = address, Y = string

print_string
	jsr     setDDAdd
	jsr     outstr
	rts

*
*     Print chan selected. Most+Least Hits, COR state, DIS state, S/N.
*
print_sel
	
	jsr     line1

sel1    brclr   chan_sel $01 sel2
	ldy     #char1
	ldaa    #$05
	jsr     print_string
	bra     end_print_sel
sel2    brclr   chan_sel $02 sel3
	ldy     #char2
	ldaa    #$05
	jsr     print_string
	bra     end_print_sel
sel3    brclr   chan_sel $04 sel4
	ldy     #char3
	ldaa    #$05
	jsr     print_string
	bra     end_print_sel
sel4    brclr   chan_sel $08 sel5
	ldy     #char4
	ldaa    #$05
	jsr     print_string
	bra     end_print_sel
sel5    brclr   chan_sel $10 sel6
	ldy     #char5
	ldaa    #$05
	jsr     print_string
	bra     end_print_sel
sel6    brclr   chan_sel $20 sel7
	ldy     #char6
	ldaa    #$05
	jsr     print_string
	bra     end_print_sel
sel7    brclr   chan_sel $40 sel8
	ldy     #char7
	ldaa    #$05
	jsr     print_string
	bra     end_print_sel
sel8    brclr   chan_sel $80 end_print_sel
	ldy     #char8
	ldaa    #$05
	jsr     print_string
	bra     end_print_sel

end_print_sel
	jsr     clr_cur
	rts


*       Print Disable selection cursor
*
print_dis_cur
	jsr     line1
	jsr     set_cur

	brset   dis_sel $01  dis1
	brset   dis_sel $02  dis2
	brset   dis_sel $04  dis3
	brset   dis_sel $08  dis4
	brset   dis_sel $10  dis5
	brset   dis_sel $20  dis6
	brset   dis_sel $40  dis7
	brset   dis_sel $80  dis8
	
dis1    ldaa    #$01        
	jmp     set_dis   
dis2    ldaa    #$03        
	jmp     set_dis   
dis3    ldaa    #$05        
	jmp     set_dis   
dis4    ldaa    #$07        
	jmp     set_dis   
dis5    ldaa    #$09        
	jmp     set_dis   
dis6    ldaa    #$0B        
	jmp     set_dis   
dis7    ldaa    #$0D        
	jmp     set_dis   
dis8    ldaa    #$0F        
	jmp     set_dis   

set_dis        
	jsr     setDDAdd
	rts


*
*    Print COR Active state. Either HI or LO
*
print_state
	jsr     line1           
	ldaa    chan_sel        Check if Hi or Lo.
	anda    cr_state
	cmpa    #$00
	bne     st2
st1     
	ldy     #state1         HI  =1
	ldaa    #$0E
	jsr     print_string
	jmp     end_state
st2     
	ldy     #state2         LO  =0
	ldaa    #$0E
	jsr     print_string

end_state       
	jsr     clr_cur
	rts

*
*    Print Disable Active state. Either HI or LO
*
print_dis_state
	jsr     line1           
	ldaa    chan_sel        Check if Hi or Lo.
	anda    dis_state
	cmpa    #$00
	bne     dt2
dt1     
	ldy     #state1         HI  =1
	ldaa    #$0E
	jsr     print_string
	jmp     end_dis_state
dt2     
	ldy     #state2         LO  =0
	ldaa    #$0E
	jsr     print_string

end_dis_state       
	jsr     clr_cur
	rts

*
*      Change COR State.
*
change_state
	ldaa    cr_state
	anda    chan_sel
	cmpa    #$00
	bne     state_on
	
	ldaa    chan_sel        Sets chan_sel bit.   LO=1
	oraa    cr_state
	staa    cr_state

	jmp     ch_state_end

state_on 
	ldaa    chan_sel        Clears chan_sel bit.  HI=0.
	coma    
	anda    cr_state
	staa    cr_state

ch_state_end        
	rts

*
*     Change Disable State.
*
change_dis_state
	ldaa    dis_state
	anda    chan_sel
	cmpa    #$00
	bne     dis_state_on
	
	ldaa    chan_sel        Sets chan_sel bit.   LO=1
	oraa    dis_state
	staa    dis_state

	jmp     ch_dis_state_end

dis_state_on 
	ldaa    chan_sel        Clears chan_sel bit.  HI=0.
	coma    
	anda    dis_state
	staa    dis_state

ch_dis_state_end        
	rts
*
*               Change Disabled channels by software.
*
change_dis
	ldaa    disabled_s
	anda    dis_sel
	cmpa    #$00
	bne     dis_on
	
	ldaa    dis_sel        Sets chan_sel bit.   
	oraa    disabled_s
	staa    disabled_s
	
	jmp     ch_dis_end

dis_on  
	ldaa    dis_sel        Clears chan_sel bit.  
	coma    
	anda    disabled_s
	staa    disabled_s
ch_dis_end        
	rts

*
*       get_cor_dis   Get COR and Disable hardware inputs.
*
get_cor_dis
	
	ldaa    active         Save last setting.
	staa    active_old
	
	ldaa    disabled_t     Save last setting.
	staa    disabled_t_old

	bclr    porta,x #$08   Set COR/DIS select line to COR.
	jsr     wait_1         Slow update rate to 60 Hz.
	ldaa    portpb         Read port PB.
	staa    cor_in         Store in cor_in.
	eora    cr_state       Adjust for COR state.
	staa    cor_in

	bset    porta,x #$08   Set COR/DIS select line to DIS.
	jsr     wait_1                  
	ldaa    portpb         Read port PB.
	staa    disabled_h     Store in disabled by hardware.
	eora    dis_state      Adjust for Dis State.
	staa    disabled_h

	ldaa    disabled_s     Load disabled by software.
	oraa    disabled_h     Or with disabled by hardware.
	staa    disabled_t     Store in disable total.
	coma
	anda    cor_in
	staa    active
	cmpa    #$00           Check for no CORs.
	beq     cor_off

	bset    porta,x #$10   At Least one COR, turn on LED and Relay.
	jsr     get_average
	ldaa    #true
	staa    cor_flag       Set COR_flag to true.
	bra     end_cor_dis

cor_off
	ldaa    best_ave            Load the COR average.
	staa    select_ch 
	jsr     set_current         Set it just before COR = off.

	bclr    porta,x #$10        Turn off COR and LED.

	ldaa    cor_flag            Check COR flag.
	cmpa    #false
	beq     end_cor_dis         Branch if false.
	jsr     get_hits            Update hits count.
	ldaa    #false
	staa    cor_flag            Set the flag.

	jsr     COR_reset           Reset variables.

end_cor_dis
	ldaa    active              Check for new cor activity.
	cmpa    active_old
	beq     end_cor_dis2
	ldaa    #true
	staa    update_valid        Set update to true.

end_cor_dis2        
	ldaa    disabled_t          Check for new dis activity.
	cmpa    disabled_t_old
	beq     end_cor_dis3
	ldaa    #true
	staa    update_valid        Set update to true.

end_cor_dis3
	rts                         Go back to main loop.


*
*          Update Display. From main menu.
*
update_display

	ldaa    update_count          Update display 0.5 seconds.
	cmpa    #$20
	blo     update_disp_1
	
	clr     update_count          Reset update counter.

	ldaa    select_ch               Send RS-232 info at 2 Hz.
	jsr     out_byte
	
	jmp     set_update

update_disp_1        
	inc     update_count

	ldaa    update_valid          Check for valid update.
	cmpa    #true
	beq     set_update
	jmp     end_mode              Jump to end if false.
	
set_update
	ldaa    #false                Set update to false.
	staa    update_valid

mode_0                              
	ldaa    mode                  Check for mode = 00.
	cmpa    #$00
	bne     mode_1                Jump to next mode check if not.
	
	ldaa    select_ch             Load current channel to be displayed.
	jsr     print_chan            Display it.
	jmp     end_mode

mode_1                              
	ldaa    mode
	cmpa    #$01
	bne     mode_2
	
	ldaa    cor_in       
	jsr     print_chan
	jmp     end_mode
	
mode_2                              
	ldaa    mode
	cmpa    #$02
	bne     mode_3
	
	jsr     print_sel                Print selected channel.
	
	ldaa    sn_mode                  Check S/N mode.
	cmpa    #$01
	bne     bar_pr

	jsr     print_sn                 Print number.
	bra     end_mode

bar_pr  jsr     print_bar                Print bar.
	bra     end_mode
	
mode_3                              
	ldaa    mode
	cmpa    #$03
	bne     mode_4
	
	ldaa    best_site
	jsr     print_chan               Print Best site.
	bra     end_mode

mode_4                              
	ldaa    mode
	cmpa    #$04
	bne     mode_5
	
	ldaa    least_site               Print Least site.
	jsr     print_chan
	bra     end_mode

mode_5                              
	ldaa    mode
	cmpa    #$05
	bne     mode_6

	jsr     print_sel               
	jsr     print_hits               Print Hits for each site.
	bra     end_mode

mode_6        
	ldaa    mode                     
	cmpa    #$06
	bne     mode_7

	jsr     print_hyst               Print Hyterisis Level. 
	bra     end_mode
	
mode_7
	ldaa    mode
	cmpa    #$07                    
	bne     mode_8
	
	ldaa    disabled_t
	jsr     print_chan
	jsr     print_dis_cur            Print disabled cursor
	bra     end_mode

mode_8       
	ldaa    mode                    
	cmpa    #$08
	bne     mode_9
	
	jsr     print_sel                Print selected channel.
	jsr     print_dis_state          Print Disabled State.
	

mode_9        
	ldaa    mode                    
	cmpa    #$09
	bne     end_mode
	
	jsr     print_sel                Print selected channel.
	jsr     print_state              Print COR State
	bra     end_mode

end_mode        

	rts                              Return to main loop.


*
*       Get A to Ds. Store in AD1-8. Then get Best signal with Hyst.
*
read_ad
	  ldaa    #$10             Start multi conversion with no scan
	  staa    adctl,x          on first set of 4 a/d's
ad_loop1  brclr   adctl,x  #ccf ad_loop1          

	  ldaa    adr1,x
	  staa    AD1
	  ldaa    adr2,x
	  staa    AD2
	  ldaa    adr3,x
	  staa    AD3
	  ldaa    adr4,x
	  staa    AD4
	  
	  ldaa    #$14             Start multi conversion with no scan
	  staa    adctl,x          on second set of 4 a/d's
ad_loop2  brclr   adctl,x  #ccf ad_loop2
	  
	  ldaa    adr1,x
	  staa    AD5
	  ldaa    adr2,x
	  staa    AD6
	  ldaa    adr3,x
	  staa    AD7
	  ldaa    adr4,x
	  staa    AD8

*         
*             Check for activity. Set S/N to FF if inactive.
*
	  ldaa    #$ff             load  into A.
ad_1      brset   active $01 ad_2  Check for active.
	  staa    AD1              Load FF if COR not active
ad_2      brset   active $02 ad_3  
	  staa    AD2              
ad_3      brset   active $04 ad_4  
	  staa    AD3              
ad_4      brset   active $08 ad_5  
	  staa    AD4              
ad_5      brset   active $10 ad_6  
	  staa    AD5              
ad_6      brset   active $20 ad_7  
	  staa    AD6              
ad_7      brset   active $40 ad_8  
	  staa    AD7              
ad_8      brset   active $80 ad_end
	  staa    AD8              
ad_end

*
*               Check for single site active.
*
single_check
	  clr     single_count

sc_1      brclr   active $01 sc_2
	  inc     single_count
sc_2      brclr   active $02 sc_3
	  inc     single_count
sc_3      brclr   active $04 sc_4
	  inc     single_count
sc_4      brclr   active $08 sc_5
	  inc     single_count
sc_5      brclr   active $10 sc_6
	  inc     single_count
sc_6      brclr   active $20 sc_7
	  inc     single_count
sc_7      brclr   active $40 sc_8
	  inc     single_count
sc_8      brclr   active $80 sc_end
	  inc     single_count
sc_end

	  ldaa    single_count       Check for only one site.
	  cmpa    #$01
	  bne     sc_end_2
	  ldaa    active
	  staa    select_ch          Set best chan to active chan.

	  ldaa    #$FF               Reset a/d for hyst selector.
	  staa    select_ad
	  
	  jmp     end_best_last      Jump out to RTS.
sc_end_2
	  ldaa    single_count
	  cmpa    #$00
	  bne     get_best
	  
	  jsr     COR_reset
	  jmp     end_best_last      Jump if zero sites.

*
*               Get Best. Finds best Channel of those active.
*
get_best
	  ldaa    #$ff
	  staa    best_ad          Set best_ad to force new find.
	  
best_1    brclr   active $01 best_2 
	  ldaa    AD1              Load A/D noise. Higher is more noise.
	  cmpa    best_ad          Compare to what was best.
	  bhi     best_2           Get out if not better.
	  staa    best_ad          Store if is better.
	  ldaa    #$01             Set the Channel too.
	  staa    best_ch

best_2    brclr   active $02 best_3
	  ldaa    AD2
	  cmpa    best_ad
	  bhi     best_3
	  staa    best_ad
	  ldaa    #$02             Set the Channel.
	  staa    best_ch

best_3    brclr   active $04 best_4
	  ldaa    AD3
	  cmpa    best_ad
	  bhi     best_4
	  staa    best_ad
	  ldaa    #$04             Set the Channel.
	  staa    best_ch

best_4    brclr   active $08 best_5
	  ldaa    AD4
	  cmpa    best_ad
	  bhi     best_5
	  staa    best_ad
	  ldaa    #$08             Set the Channel.
	  staa    best_ch

best_5    brclr   active $10 best_6
	  ldaa    AD5
	  cmpa    best_ad
	  bhi     best_6
	  staa    best_ad
	  ldaa    #$10             Set the Channel.
	  staa    best_ch

best_6    brclr   active $20 best_7
	  ldaa    AD6
	  cmpa    best_ad
	  bhi     best_7
	  staa    best_ad
	  ldaa    #$20             Set the Channel.
	  staa    best_ch

best_7    brclr   active $40 best_8
	  ldaa    AD7
	  cmpa    best_ad
	  bhi     best_8
	  staa    best_ad
	  ldaa    #$40             Set the Channel.
	  staa    best_ch

best_8    brclr   active $80 end_best
	  ldaa    AD8
	  cmpa    best_ad
	  bhi     end_best
	  staa    best_ad
	  ldaa    #$80             Set the Channel.
	  staa    best_ch

end_best

*
*               Get Selected Channel A/D. 
*
get_sel_ad
	  
sel_ad_1  brclr   select_ch $01 sel_ad_2 
	  ldaa    AD1                      
	  staa    select_ad

sel_ad_2  brclr   select_ch $02 sel_ad_3 
	  ldaa    AD2                      
	  staa    select_ad

sel_ad_3  brclr   select_ch $04 sel_ad_4 
	  ldaa    AD3                      
	  staa    select_ad

sel_ad_4  brclr   select_ch $08 sel_ad_5 
	  ldaa    AD4                      
	  staa    select_ad

sel_ad_5  brclr   select_ch $10 sel_ad_6 
	  ldaa    AD5                      
	  staa    select_ad

sel_ad_6  brclr   select_ch $20 sel_ad_7 
	  ldaa    AD6                      
	  staa    select_ad

sel_ad_7  brclr   select_ch $40 sel_ad_8 
	  ldaa    AD7                      
	  staa    select_ad

sel_ad_8  brclr   select_ch $80 end_sel_ad 
	  ldaa    AD8                      
	  staa    select_ad

end_sel_ad

	ldaa    best_ad                  A = best_ad
	ldab    select_ad                B = select_ad
	subb    hyst_value               A = select_ad + hyst_value
	cba                              Compare A and B
	blo     end_best_03              Branch if A < B

	bra     end_best_last

end_best_03            
	
	ldaa    best_ch              Set selection to best.  
	staa    select_ch
	
	ldaa    #true                Set update to true
	staa    update_valid
	
end_best_last
	
	rts                                                                  

*
*       Set current voted channel on the 4051 output bus.
*           Uses bits 5,6,7 of Port A.
*
set_current

	brset   select_ch $01  cur1
	brset   select_ch $02  cur5
	brset   select_ch $04  cur3
	brset   select_ch $08  cur7
	brset   select_ch $10  cur2
	brset   select_ch $20  cur6
	brset   select_ch $40  cur4
	brset   select_ch $80  cur8
	
cur1    bset    porta,x  #$00
	bclr    porta,x  #$E0
	jmp     end_set_cur   
cur2    bset    porta,x  #$20
	bclr    porta,x  #$C0
	jmp     end_set_cur   
cur3    bset    porta,x  #$40
	bclr    porta,x  #$A0
	jmp     end_set_cur   
cur4    bset    porta,x  #$60
	bclr    porta,x  #$80
	jmp     end_set_cur   
cur5    bset    porta,x  #$80
	bclr    porta,x  #$60
	jmp     end_set_cur   
cur6    bset    porta,x  #$A0
	bclr    porta,x  #$40
	jmp     end_set_cur
cur7    bset    porta,x  #$C0
	bclr    porta,x  #$20
	jmp     end_set_cur   
cur8    bset    porta,x  #$E0
	bclr    porta,x  #$00
	jmp     end_set_cur

end_set_cur
	
	rts

*
*       Print Signal to Noise Ratio Number for selected channel.
*
print_sn

pr_sn1  brclr   chan_sel $01 pr_sn2
	ldab    AD1
	bra     end_pr_sn        
pr_sn2  brclr   chan_sel $02 pr_sn3
	ldab    AD2
	bra     end_pr_sn        
pr_sn3  brclr   chan_sel $04 pr_sn4
	ldab    AD3
	bra     end_pr_sn        
pr_sn4  brclr   chan_sel $08 pr_sn5
	ldab    AD4
	bra     end_pr_sn        
pr_sn5  brclr   chan_sel $10 pr_sn6
	ldab    AD5
	bra     end_pr_sn        
pr_sn6  brclr   chan_sel $20 pr_sn7
	ldab    AD6
	bra     end_pr_sn        
pr_sn7  brclr   chan_sel $40 pr_sn8
	ldab    AD7
	bra     end_pr_sn        
pr_sn8  brclr   chan_sel $80 end_pr_sn
	ldab    AD8
	
end_pr_sn        
	comb
	jsr     line1
	jsr     ad_to_ascii
	
	ldy     #signal
	ldaa    #$09
	jsr     print_string
	jsr     clr_cur

	rts

*
*       Print Bar Graph for Signal to Noise Ratio for selected channel.
*
print_bar
	ldaa    bar              Save bar for display update check.
	staa    bar_old

pr_bar1 brclr   chan_sel $01 pr_bar2
	ldaa    AD1
	bra     end_pr_bar        
pr_bar2 brclr   chan_sel $02 pr_bar3
	ldaa    AD2
	bra     end_pr_bar        
pr_bar3 brclr   chan_sel $04 pr_bar4
	ldaa    AD3
	bra     end_pr_bar        
pr_bar4 brclr   chan_sel $08 pr_bar5
	ldaa    AD4
	bra     end_pr_bar        
pr_bar5 brclr   chan_sel $10 pr_bar6
	ldaa    AD5
	bra     end_pr_bar        
pr_bar6 brclr   chan_sel $20 pr_bar7
	ldaa    AD6
	bra     end_pr_bar        
pr_bar7 brclr   chan_sel $40 pr_bar8
	ldaa    AD7
	bra     end_pr_bar        
pr_bar8 brclr   chan_sel $80 end_pr_sn
	ldaa    AD8

end_pr_bar

ck_sn9  cmpa    #$B0
	blo     ck_sn8
	ldy     #sn0
	jmp     sn_end

ck_sn8  cmpa    #$A0
	blo     ck_sn7
	ldy     #sn1
	jmp     sn_end

ck_sn7  cmpa    #$90
	blo     ck_sn6
	ldy     #sn2
	bra     sn_end

ck_sn6  cmpa    #$80
	blo     ck_sn5
	ldy     #sn3
	bra     sn_end

ck_sn5  cmpa    #$70
	blo     ck_sn4
	ldy     #sn4
	bra     sn_end

ck_sn4  cmpa    #$60
	blo     ck_sn3
	ldy     #sn5
	bra     sn_end

ck_sn3  cmpa    #$50
	blo     ck_sn2
	ldy     #sn6
	bra     sn_end

ck_sn2  cmpa    #$40
	blo     ck_sn1
	ldy     #sn7
	bra     sn_end

ck_sn1  cmpa    #$30
	blo     ck_sn0
	ldy     #sn8
	bra     sn_end

ck_sn0  ldy     #sn9

sn_end  
	jsr     line1
	ldaa    #$07
	jsr     print_string
	jsr     clr_cur

	rts

*
*       convert byte in B to ascii in Signal 0,1,2
*
ad_to_ascii
	pshx
	
	ldaa    #$00
	ldx     #$000A
	idiv
	addb    #$30
	stab    signal+2
	cmpb    #$00
	beq     end_B2A
	xgdx
	ldx     #$000A
	idiv    
	addb    #$30
	stab    signal+1
	cmpb    #$00
	beq     end_B2A
	xgdx
	ldx     #$000A
	idiv
	addb    #$30
	stab    signal+0

end_B2A     
	pulx
	rts                              

*
*               Get Hits. Finds the total number hits for each channel.
*
get_hits


hit_1   brclr best_ave $01 hit_2      If channel active, increment hits.
	ldaa    hits_1
	cmpa    #$64
	beq     hit_2
	inc     hits_1
hit_2   brclr best_ave $02 hit_3
	ldaa    hits_2
	cmpa    #$64
	beq     hit_3
	inc     hits_2
hit_3   brclr best_ave $04 hit_4
	ldaa    hits_3
	cmpa    #$64
	beq     hit_4
	inc     hits_3
hit_4   brclr best_ave $08 hit_5
	ldaa    hits_4 
	cmpa    #$64
	beq     hit_5
	inc     hits_4
hit_5   brclr best_ave $10 hit_6
	ldaa    hits_5
	cmpa    #$64
	beq     hit_6
	inc     hits_5
hit_6   brclr best_ave $20 hit_7
	ldaa    hits_6
	cmpa    #$64
	beq     hit_7
	inc     hits_6
hit_7   brclr best_ave $40 hit_8
	ldaa    hits_7
	cmpa    #$64
	beq     hit_8
	inc     hits_7
hit_8   brclr best_ave $80 hit_9
	ldaa    hits_8
	cmpa    #$64
	beq     hit_9
	inc     hits_8

hit_9

hit_01  brset best_ave $01 hit_02         If channel not active, decrement.
	ldaa    hits_1
	cmpa    #$00
	beq     hit_02
	dec     hits_1
hit_02  brset best_ave $02 hit_03
	ldaa    hits_2
	cmpa    #$00
	beq     hit_03
	dec     hits_2
hit_03  brset best_ave $04 hit_04
	ldaa    hits_3
	cmpa    #$00
	beq     hit_04
	dec     hits_3
hit_04  brset best_ave $08 hit_05
	ldaa    hits_4
	cmpa    #$00
	beq     hit_05
	dec     hits_4
hit_05  brset best_ave $10 hit_06
	ldaa    hits_5
	cmpa    #$00
	beq     hit_06
	dec     hits_5
hit_06  brset best_ave $20 hit_07
	ldaa    hits_6
	cmpa    #$00
	beq     hit_07
	dec     hits_6
hit_07  brset best_ave $40 hit_08
	ldaa    hits_7
	cmpa    #$00
	beq     hit_08
	dec     hits_7
hit_08  brset best_ave $80 end_hits
	ldaa    hits_8
	cmpa    #$00
	beq     end_hits
	dec     hits_8

end_hits              
	jsr     get_best_site
	jsr     get_least_site
	rts


*
*           Get Best Site. Compares hit counts for each channel.
*
get_best_site

	ldaa    best_site            Store site into old.
	staa    best_site_old

	ldab    #$01                 Set site to 1.
	stab    best_site

check_site_1
	ldaa    hits_1               Check if 1 > 2.
	cmpa    hits_2
	bhi     check_site_3
	ldab    #$02                 Store if 2 is greater.
	stab    best_site
	ldaa    hits_2
check_site_3
	cmpa    hits_3               Check for 3 is greater.
	bhi     check_site_4
	ldab    #$04    
	stab    best_site
	ldaa    hits_3
check_site_4
	cmpa    hits_4
	bhi     check_site_5
	ldab    #$08    
	stab    best_site
	ldaa    hits_4
check_site_5
	cmpa    hits_5
	bhi     check_site_6
	ldab    #$10  
	stab    best_site
	ldaa    hits_5
check_site_6
	cmpa    hits_6
	bhi     check_site_7
	ldab    #$20    
	stab    best_site
	ldaa    hits_6
check_site_7
	cmpa    hits_7
	bhi     check_site_8
	ldab    #$40    
	stab    best_site
	ldaa    hits_7
check_site_8
	cmpa    hits_8
	bhi     end_best_site
	ldab    #$80    
	stab    best_site

end_best_site
	ldaa    best_site               Check for new update.
	cmpa    best_site_old     
	beq     end_site
	ldaa    #true   
	staa    update_valid            Set display update true.
end_site
	rts

*
*           Get Least Site. Compares hit counts for each channel.
*
get_least_site

	ldaa    least_site           Store site into old.
	staa    least_site_old

	ldaa    #$00                 Set site to nothing.
	staa    least_site
	ldaa    #$ff                 Set A to lots of hits.

check_site_08
	brset   disabled_t $80 check_site_07
	ldaa    hits_8               Check if 8 < 7.
	ldab    #$80                 Set site to 8.
	stab    least_site
check_site_07
	brset   disabled_t $40 check_site_06
	cmpa    hits_7               Check for 7 is lower.
	blo     check_site_06
	ldab    #$40    
	stab    least_site
check_site_06
	brset   disabled_t $20 check_site_05 
	cmpa    hits_6               Check for 6 is lower.
	blo     check_site_05
	ldab    #$20    
	stab    least_site
check_site_05
	brset   disabled_t $10 check_site_04 
	cmpa    hits_5
	blo     check_site_04
	ldab    #$10    
	stab    least_site
check_site_04
	brset   disabled_t $08 check_site_03 
	cmpa    hits_4
	blo     check_site_03
	ldab    #$08  
	stab    least_site
check_site_03
	brset   disabled_t $04 check_site_02 
	cmpa    hits_3
	blo     check_site_02
	ldab    #$04    
	stab    least_site
check_site_02
	brset   disabled_t $02 check_site_01 
	cmpa    hits_2
	blo     check_site_01
	ldab    #$02    
	stab    least_site
check_site_01
	brset   disabled_t $01 end_least_site
	cmpa    hits_1
	blo     end_least_site
	ldab    #$01    
	stab    least_site

end_least_site
	ldaa    least_site               Check for new update.
	cmpa    least_site_old     
	beq     end_least
	ldaa    #true   
	staa    update_valid            Set display update true.
end_least
	rts

*
*       get_average. Finds average COR during COR cycle.
*

get_average

	ldaa    ave_count         Set counter to $20.
	cmpa    #$20              0.5 second update.
	bls     get_ave_2
	
	clr     ave_count         Clear when $20 is reached.
	jmp     ave1

get_ave_2
	inc     ave_count
	jmp     end_get_ave     Jump around if not time.

ave1    brclr select_ch $01 ave2
	ldaa    ave_1
	cmpa    #$ff
	beq     ave2
	inc     ave_1
ave2    brclr select_ch $02 ave3
	ldaa    ave_2
	cmpa    #$ff
	beq     ave3
	inc     ave_2
ave3    brclr select_ch $04 ave4
	ldaa    ave_3
	cmpa    #$ff
	beq     ave4
	inc     ave_3
ave4    brclr select_ch $08 ave5
	ldaa    ave_4
	cmpa    #$ff
	beq     ave5
	inc     ave_4
ave5    brclr select_ch $10 ave6
	ldaa    ave_5
	cmpa    #$ff
	beq     ave6
	inc     ave_5
ave6    brclr select_ch $20 ave7
	ldaa    ave_6
	cmpa    #$ff
	beq     ave7
	inc     ave_6
ave7    brclr select_ch $40 ave8
	ldaa    ave_7
	cmpa    #$ff
	beq     ave8
	inc     ave_7
ave8    brclr select_ch $80 end_get_ave
	ldaa    ave_8
	cmpa    #$ff
	beq     end_get_ave
	inc     ave_8

end_get_ave

	ldaa    ave_1                  Check for all 00s in counts.
	adda    ave_2
	adda    ave_3
	adda    ave_4
	adda    ave_5
	adda    ave_6
	adda    ave_7
	adda    ave_8

	cmpa    #$06
	bhi     get_best_ave           If all 00s, use select_ch.
	ldaa    select_ch
	staa    best_ave
	jmp     end_check_ave

get_best_ave        
	
	ldaa    #$01
	staa    best_ave

check_ave_1    
	ldaa    ave_1
	cmpa    ave_2
	bhi     check_ave_3
	ldaa    #$02
	staa    best_ave
check_ave_3
	cmpa    ave_3
	bhi     check_ave_4
	ldaa    #$04
	staa    best_ave
check_ave_4
	cmpa    ave_4
	bhi     check_ave_5
	ldaa    #$08
	staa    best_ave
check_ave_5
	cmpa    ave_5
	bhi     check_ave_6
	ldaa    #$10
	staa    best_ave
check_ave_6
	cmpa    ave_6
	bhi     check_ave_7
	ldaa    #$20
	staa    best_ave
check_ave_7
	cmpa    ave_7
	bhi     check_ave_8
	ldaa    #$40
	staa    best_ave
check_ave_8
	cmpa    ave_8
	bhi     end_check_ave
	ldaa    #$80
	staa    best_ave

end_check_ave
	rts

*        
*    Print hysterisis
*

print_hyst

	ldy     #char0
hy0     ldaa    hyst            Load value for hyst.
	cmpa    #$00            Check value.
	bne     hy1             Branch if not equal.
	ldaa    #$01
	staa    hyst_value
	ldy     #char0           Load character to print.
	jmp     end_hy          Branch to print it.
hy1     ldaa    hyst
	cmpa    #$01
	bne     hy2
	ldaa    #$03
	staa    hyst_value
	ldy     #char1
	jmp     end_hy
hy2     ldaa    hyst
	cmpa    #$02
	bne     hy3
	ldaa    #$06
	staa    hyst_value
	ldy     #char2
	bra     end_hy
hy3     ldaa    hyst
	cmpa    #$03
	bne     hy4
	ldaa    #$09
	staa    hyst_value
	ldy     #char3
	bra     end_hy
hy4     ldaa    hyst
	cmpa    #$04
	bne     hy5
	ldaa    #$0C
	staa    hyst_value
	ldy     #char4
	bra     end_hy
hy5     ldaa    hyst
	cmpa    #$05
	bne     hy6
	ldaa    #$0F
	staa    hyst_value
	ldy     #char5
	bra     end_hy
hy6     ldaa    hyst
	cmpa    #$06
	bne     hy7
	ldaa    #$12
	staa    hyst_value
	ldy     #char6
	bra     end_hy
hy7     ldaa    hyst
	cmpa    #$07
	bne     hy8
	ldaa    #$15
	staa    hyst_value
	ldy     #char7
	bra     end_hy
hy8     ldaa    hyst
	cmpa    #$08
	bne     hy8
	ldaa    #$18
	staa    hyst_value
	ldy     #char8
	bra     end_hy
hy9     ldaa    hyst
	cmpa    #$09
	bne     end_hy
	ldaa    #$18
	staa    hyst_value
	ldy     #char9

end_hy  ldaa    #$09                  Load character position.
	jsr     print_string          Print it.
	jsr     clr_cur

	rts

*
*      Print Hits. Prints number of hits for for each channel.
*
print_hits

print_hit_1     brclr   chan_sel $01 print_hit_2
		ldab    hits_1
		bra     end_print_hits

print_hit_2     brclr   chan_sel $02 print_hit_3
		ldab    hits_2
		bra     end_print_hits

print_hit_3     brclr   chan_sel $04 print_hit_4
		ldab    hits_3
		bra     end_print_hits

print_hit_4     brclr   chan_sel $08 print_hit_5
		ldab    hits_4
		bra     end_print_hits

print_hit_5     brclr   chan_sel $10 print_hit_6
		ldab    hits_5
		bra     end_print_hits

print_hit_6     brclr   chan_sel $20 print_hit_7
		ldab    hits_6
		bra     end_print_hits

print_hit_7     brclr   chan_sel $40 print_hit_8
		ldab    hits_7
		bra     end_print_hits

print_hit_8     brclr   chan_sel $80 end_print_hits
		ldab    hits_8
		bra     end_print_hits


end_print_hits  jsr     hit_to_ascii
		ldy     #hit_string
		ldaa    #$0D
		jsr     print_string
		jsr     clr_cur
		rts


*
*       convert byte in B to ascii in hit_string+0,1,2
*
hit_to_ascii
	pshx
	
	ldaa    #$00
	ldx     #$000A
	idiv
	addb    #$30
	stab    hit_string+2
	cmpb    #$00
	beq     end_B2B
	xgdx
	ldx     #$000A
	idiv    
	addb    #$30
	stab    hit_string+1
	cmpb    #$00
	beq     end_B2B
	xgdx
	ldx     #$000A
	idiv
	addb    #$30
	stab    hit_string+0

end_B2B     
	pulx
	rts
*        
*    Reset after COR. Reset variables after CORs drop.
*
COR_reset
	
	clr     ave_1
	clr     ave_2
	clr     ave_3
	clr     ave_4
	clr     ave_5
	clr     ave_6
	clr     ave_7
	clr     ave_8

	ldaa    #$20
	staa    ave_count

	ldaa    #$FF
	staa    select_ad

	rts


***************************************************************
*
*       RS-232 subroutines.
*
*       Convert the byte in A to two ASCII characters ond output.
out_byte
	  psha                save copy.
	  bsr     outlhlf     output left half.
	  pula                retrieve copy.
	  bsr     outrhlf     output right half.
	  rts

*       Send A to RS-232 port.  
output
	  brclr   scsr,x  #TDRE   output
	  staa    scdr,x
	  rts

*     Convert a from binary to ASCII and output.
outlhlf
	  lsra     shift data to right
	  lsra
	  lsra
	  lsra
outrhlf
	  anda    #$0f     mask top half
	  adda    #$30     convert to ascii
	  cmpa    #$39
	  ble     outa     jump if 0-9
	  adda    #$07     convert to hex A-F
outa      jsr     output   output character
	  rts


**********************************************
*
* Subroutines for LCD Display 
*
*

initIface:
	jsr     w5ms
	jsr     w5ms
	ldaa    #$3
	jsr     dispIOut
	jsr     w5ms
	ldaa    #$3
	jsr     dispIOut
	ldaa    #$3
	jsr     dispIOut
	ldaa    #$2             set 4 bit mode
	jsr     dispIOut
	jsr     function2LSC
	jsr     displayOnCursBl
	jsr     clear
	jsr     modeIS
	rts

dispDOut:
	jsr     w5ms
	psha
	lsra
	lsra
	lsra
	lsra
	anda    #$0f
	oraa    #$40
	staa    portpa
	oraa    #$10
	staa    portpa
	anda    #$4f
	staa    portpa
	pula
	anda    #$0f
	oraa    #$40
	staa    portpa
	oraa    #$10
	staa    portpa
	anda    #$4f
	staa    portpa
	rts

dispIOut:
	jsr     w5ms
	anda    #$0f
	staa    portpa
	oraa    #$10
	staa    portpa
	anda    #$0f
	staa    portpa
	rts
dispIIn:
	ldaa    #$20
	staa    portpa
	oraa    #$10
	staa    portpa
	ldaa    portpa
	psha
	anda    #$20
	staa    portpa
	pula                    a now has busy in bit 3
	rts

*
*    wait   5 milli seconds
*
w5ms:
	pshy
	ldy     #2000
w5msL:
	nop
	dey
	bne     w5msL
	puly
	rts
*** Clear Display
clear:
	psha
	ldaa    #$0
	jsr     dispIOut
	ldaa    #$1
	jsr     dispIOut
	pula
	rts

* Clear Current Line
clearline
	ldy     #blankline
	jsr     outstr
	rts

***   Set Cursor On
set_cur:
	psha
	ldaa    #$0
	jsr     dispIOut
	ldaa    #$E
	jsr     dispIOut
	pula
	rts
***   Clear Cursor
clr_cur:
	ldaa    #$10
	jsr     setDDAdd
	rts
***   Set Line Number
line1:
	ldaa    #$00
	jsr     setDDAdd
	rts
line2:
	ldaa    #$40
	jsr     setDDAdd
	rts
*** Return home
return:
	psha
	ldaa    #$0
	jsr     dispIOut
	ldaa    #$2
	jsr     dispIOut
	pula
	rts
*** Entry mode set - increment with shift
modeIS:
	psha
	ldaa    #$0
	jsr     dispIOut
	ldaa    #$6
	jsr     dispIOut
	pula
	rts
*** display on/off control - on, cursor, blink
displayOnCursBl:        
	psha
	ldaa    #$0
	jsr     dispIOut
	ldaa    #$f
	jsr     dispIOut
	pula
	rts
*** function - 4 bit 2 line 5*7 character
function2LSC:
	psha
	ldaa    #$2
	jsr     dispIOut
	ldaa    #$8
	jsr     dispIOut
	pula
	rts
*** set Display data ram address
setDDAdd:
	oraa    #$80
	psha
	lsra
	lsra
	lsra
	lsra
	jsr     dispIOut
	pula
	jsr     dispIOut
	rts

*
outstr  ldaa   0,y       load first character, old contents of a unimportant
	 cmpa   #eot
	 beq    stringEnd
	 jsr    dispDOut
	 iny
	 bra    outstr   go for next character
stringEnd         
	 rts            go to get next message.

*
*
*
wait:     
	  ldaa  #00
	  ldy   #$FFFF        Set up to delay the output to slow it down.
dloop     dey                 Inner loop is about 1/2 seconds.
	  cpy   #$00
	  bne   dloop          Loop for delay.
	  inca
	  cmpa  #$01
	  blt   dloop
	  rts
*
*       delay about .1 seconds
*
delay1:     
	  ldaa  #00
	  ldy   #$5000        Set up to delay the output to slow it down.
dloop1    dey                 Inner loop is about 1/2 seconds.
	  cpy   #$00
	  bne   dloop1          Loop for delay.
	  inca
	  cmpa  #$01
	  blt   dloop1
	  rts
*
*              Wait .001 Second
*
wait_1
	pshy
	ldy     #2000
wait_lp
	nop
	dey
	bne     wait_lp
	puly
	rts
	  
**********************************************************************
*       Subroutines for Serial EEPROM
*

* Writes a byte to the EEPROM. Uses address contained Y.
* Writes byte stored in B.

WR_BYTE 
	JSR     EE_WREN                 Send write enable command
	BCLR    PORTD,X #CE_BIT         Activate CE
	PSHY
	TSY
	LDAA    0,Y                     Get MSB
	asla        
	asla
	asla
	oraa    #WRITE_CMD
	jsr     OUT_BYT
	ldaa    1,y                     Get LSB
	jsr     OUT_BYT
	tba
	jsr     OUT_BYT
	bset    PORTD,X #CE_BIT         De-activate CE
	puly
	
	jsr     ACK_POLL        is the write complete?
	RTS

* Reads a byte from the EEPROM. Uses address in y.
* Returns byte in A.

RD_BYTE 
	BCLR    PORTD,X #CE_BIT         Activate CE
	PSHY
	TSY
	LDAA    0,Y                     Get MSB
	asla
	asla
	asla
	oraa    #READ_CMD
	JSR     OUT_BYT
	LDAA    1,Y                     Get LSB
	JSR     OUT_BYT
	LDAA    #DUMMY                  Shift data
	JSR     OUT_BYT
	LDAA    SPDR,X                  Load byte from SPI
	BSET    PORTD,X #CE_BIT         De-activate CE
	PULY
	RTS


* Verifies the EEPROM is ready
ACK_POLL        BCLR    PORTD,X #CE_BIT         Activate CE
		LDAA    #RDSR_CMD               Load Status command
		JSR     OUT_BYT                 Send it
		LDAA    #DUMMY
		JSR     OUT_BYT                 Send dummy data
		BSET    PORTD,X #CE_BIT         de-activate CE
		LDAA    SPDR,X                  Load byte from SPI
		ASRA                            Shift Right
		BCC     ACK_POLL                Branch if Carry Clear
		RTS
*
OUT_BYT: 
	 STAA    SPDR,X                  Sends a byte to the EEPROM
out_byt1 BRCLR  SPSR,X #SPIF_BIT out_byt1
		RTS

*
EE_WREN BCLR    PORTD,X #CE_BIT         Activates the CE
	LDAA    #WREN_CMD               loads write enable command
	JSR     OUT_BYT                 send it to the chip
	BSET    PORTD,X #CE_BIT         De-activates the CE
	RTS

************************************************************************
* Message Storage Area.
*
*                0123456789012345
*         org    $F000             org data strings at $F000

blankline    
	 FCC    '                '
	 FCB    eot
mess1    
	 FCC    'Repeater Voting '
	 FCB    eot
mess2    
	 FCC    ' System  RVS-8  '
	 FCB    eot
mess1a    
	 FCC    '  Version  1.4  '
	 FCB    eot
mess2a                               
	 FCC    '   April 1996   '
	 FCB    eot
mess3    
	 FCC    ' 1 2 3 4 5 6 7 8'
	 FCB    eot
mess4    
	 FCC    '  Current Voted '
	 FCB    eot
mess5    
	 FCC    '  Current CORs  '
	 FCB    eot
mess6    
	 FCC    ' Disabled Chans '
	 FCB    eot
mess7    
	 FCC    '   Most Voted   '
	 FCB    eot
mess8    
	 FCC    '   Least Voted  '
	 FCB    eot
mess9    
	 FCC    '  Hits per Chan '
	 FCB    eot
mess10   
	 FCC    'COR Active State'
	 FCB    eot
mess11   
	 FCC    '   Hysteresis   '
	 FCB    eot
mess12   
	 FCC    ' Signal / Noise '
	 FCB    eot
mess13   
	 FCC    'Chan            '
	 FCB    eot
mess14   
	 FCC    'Chan   Active   '
	 FCB    eot
mess15  
	 FCC    'Value =         '
	 FCB    eot
mess16  
	 FCC    'Chan   Hits     '
	 FCB    eot
mess17   
	 FCC    'Dis Active State'
	 FCB    eot


char0    
	 FCC    '0'
	 FCB    eot
char1    
	 FCC    '1'
	 FCB    eot
char2    
	 FCC    '2'
	 FCB    eot
char3    
	 FCC    '3'
	 FCB    eot
char4    
	 FCC    '4'
	 FCB    eot
char5    
	 FCC    '5'
	 FCB    eot
char6    
	 FCC    '6'
	 FCB    eot
char7    
	 FCC    '7'
	 FCB    eot
char8    
	 FCC    '8'
	 FCB    eot
char9    
	 FCC    '9'
	 FCB    eot
char10   
	 FCC    '10'
	 FCB    eot

sn0    
	 FCC    '          '
	 FCB    eot
sn1    
	 FCC    'x         '
	 FCB    eot
sn2    
	 FCC    'xx        '
	 FCB    eot
sn3    
	 FCC    'xxx       '
	 FCB    eot
sn4    
	 FCC    'xxxx      '
	 FCB    eot
sn5    
	 FCC    'xxxxx     '
	 FCB    eot
sn6    
	 FCC    'xxxxxx    '
	 FCB    eot
sn7    
	 FCC    'xxxxxxx   '
	 FCB    eot
sn8    
	 FCC    'xxxxxxxx  '
	 FCB    eot
sn9    
	 FCC    'xxxxxxxxx '
	 FCB    eot
sn10    
	 FCC    'xxxxxxxxxx'
	 FCB    eot

state1   
	 FCC    'HI'
	 FCB    eot
state2   
	 FCC    'LO'
	 FCB    eot

*
*    End of EEPROM. Set up Jump Vectors.
*
	org     $FFE0

	FDB     $0000,$0000,$0000,$0000
	FDB     $0000,$0000,$0000,$0000
	FDB     $0000,$0000,$0000,$0000
	FDB     $0000,$0000,$0000,$E200
	
	org     $FFE0
	
	FDB     $0000,$0000,$0000,$0000
	FDB     $0000,$0000,$0000,$0000
	FDB     $0000,$0000,$0000,$0000
	FDB     $0000,$0000,$0000,$E200
	
	org     $FFE0
	
	FDB     $0000,$0000,$0000,$0000
	FDB     $0000,$0000,$0000,$0000
	FDB     $0000,$0000,$0000,$0000
	FDB     $0000,$0000,$0000,$E200
	
	end
