chiark / gitweb /
Send HELLO as a result of all slaves being online. Do not crash if slave is slow...
[trains.git] / detpic / program.asm
index 0d86f9500209448de645b29ec3ac43ed3ed668ed..48ae077e9effc7e915cebea8baf1e439f95dd93b 100644 (file)
 ;======================================================================
 ; MAIN PROGRAM AND GLUE
 
- include common.inc
- code
 include common.inc
 code
 
-;----------
-vector_reset
-
- set_f qqTRISA
- set_f qqTRISB
- set_f qqTRISC
- set_f qqTRISD
- set_f qqTRISE
- mov_lw 0xaa
- mov_wf qqLATA
- mov_wf qqLATB
- mov_wf qqLATC
- mov_wf qqLATD
- mov_wf qqLATE
+;======================================================================
+; COMMON TO MASTER AND SLAVE
 
+;----------------------------------------
+vector_reset @
        clr_f   INTCON
        bs_f    RCON, IPEN      ; interrupt priorities
 
        call    idlocs_init
 
-       tst_f_ifnz picno
-       goto    slave
-       bra     master
+       bt_f_if0 idloc1,idloc1_master
+       call    slave_init
+
+       bt_f_if1 idloc1,idloc1_master
+       call    master_init
+
+       bs_f    INTCON, GIEH
+       bs_f    INTCON, GIEL
+
+       bt_f_if1 idloc1,idloc1_master
+       goto    backgroundloop_master
+       bt_f_if1 idloc1,idloc1_boarddet
+       goto    backgroundloop_detectors
+       goto    backgroundloop_reversers
 
 ;----------
-i2cm_intrl
-; handles i2c interrupt using i2cm_interrupt[_definite],
-; according to the rules for <something>_intrl.
-       bt_f_if0 PIR1, SSPIF
+common_local_init
+; called by {master,slave}_init immediately after they've init'd i2c.
+       clr_f   PCLATU
+       call    bitnum2bit_init
+
+       call    points_local_init
+       call    polarity_local_init
+       call    detect_local_init
+       return
+
+;----------------------------------------
+panic_kill_hook @
+       call    power_panichook
+       call    cdu_panichook
        return
-       call    i2cm_interrupt_definite
-       intrl_handled_nostack
 
 ;----------
-intrl_handled_routine
+intrl_handled_routine @
        mov_ff  isr_low_save_stkptr, STKPTR
        intrl_handled_core
 
+;======================================================================
+; MASTER
+
+;----------------------------------------
+master_init
+; Master-specific initialisation.
+       call    memory_erase
+       clr_f   flags
+       call    serial_init
+       call    i2cm_init
+       call    serialtxbuf_init
+       call    polarity_master_init
+       call    points_master_init
+       call    common_local_init
+       call    nmra_init
+       call    serialtxfc_init
+       call    mascan_init
+       call    tick_init
+       call    cdu_init
+       call    power_polarising_init
+       call    power_fault_init
+       call    watchdog_init
+       call    i2c_consider_restartread ; sends hello when all slaves ack'd
+       return
+
+;----------------------------------------
+master_interrupt_low @
+       enter_interrupt_low
+       Dl 0x80
+       Df      INTCON
+       Df      INTCON2
+       Df      INTCON3
+       Df      PIR1
+       Df      PIR2
+       Df      PIR3
+       Dl 0x8d
+       call    tick_intrl
+       call    nmra_serialrx_intrl
+       call    power_fault_intrl
+       call    serialtxfc_intrl
+       call    serialtx_intrl
+       call    points_local_intrl
+       call    i2cm_intrl
+       Dl 0x8e
+       Df      INTCON
+       Df      INTCON2
+       Df      INTCON3
+       Df      PIR1
+       Df      PIR2
+       Df      PIR3
+       Dl 0x8f
+       panic   morse_IL
+
+;----------------------------------------------------------------------
+serialrx_table_section code    0x2000
+
+;--------------------
+command_tellmode
+       mov_lw  0x09
+       goto    serial_addbyte
+
+;--------------------
+serialrx_generalmsg @
+;command_<something>  has same calling convention:
+;
+;  FSR0 ->     start of message                undefined
+;  *FSR0       message                         undefined
+;
+       mov_fw  INDF0
+       bra_n   serialrx_if_multibyte
+       bra_z   command_crashed
+
+       xor_lw  0x11
+       bra_z   command_power_on
+       xor_lw  0x10 ^ 0x11
+       bra_z   command_power_off
+       xor_lw  0x0a ^ 0x10
+       bra_z   command_tellmode
+       bra     serialrx_bad
+
+;-----
+serialrx_if_multibyte
+       rr_fw   INDF0           ; W =   ii MM ww ww ww ww ii ii
+       and_lw  0x3c            ; W =   zz zz ww ww ww ww zz zz
+       bt_f_if0 PCL, 0 ; always true, but reads PCL
+       add_wff PCL
+       ; <--- here is zero
+
+               goto    serialrx_bad            ; 1 0000 xxx
+       goto    command_ping            ; 1 0001 xxx
+       goto    command_polarity        ; 1 0010 xxx
+       goto    command_watchdog        ; 1 0011 xxx
+       goto    command_point           ; 1 0100 xxx
+       goto    serialrx_bad            ; 1 0101 xxx
+       goto    serialrx_bad            ; 1 0110 xxx
+               goto    serialrx_bad            ; 1 0111 xxx
+       goto    serialrx_bad            ; 1 1000 xxx
+       goto    serialrx_bad            ; 1 1001 xxx
+       goto    serialrx_bad            ; 1 1010 xxx
+       goto    serialrx_bad            ; 1 1011 xxx
+       goto    serialrx_bad            ; 1 1100 xxx
+       goto    serialrx_bad            ; 1 1101 xxx
+       goto    serialrx_bad            ; 1 1110 xxx
+       goto    serialrx_bad            ; 1 1111 xxx
+
+;-----
+serialrx_bad
+       mov_ff  INDF0, t
+       panic   morse_HX
+
+code2 code
+;--------------------
+command_ping
+       mov_fw  POSTINC0
+       mov_ff  INDF0, t
+       call    serial_addbyte
+       mov_fw  t
+       bra_n   command_ping_bad
+       goto    serial_addbyte_another
+
+command_ping_bad panic morse_HP
+
+;======================================================================
+; SLAVE
+
+;----------------------------------------
+slave_init
+; Slave-specific initialisation.
+       clr_f   flags
+
+       mov_fw  picno
+       call    i2cs_init
+
+       bc_f    T0CON, TMR0ON
+
+       call    common_local_init
+       call    detect_slave_init
+       return
+
+;----------------------------------------
+slave_interrupt_low @
+       enter_interrupt_low
+       call    points_local_intrl
+       panic   morse_IL
+
+;----------------------------------------
+i2csu_write_begin
+       return
+
+near_local_do code
+;----------------------------------------
+i2csu_write_data
+       call    led_green
+
+       bt_w_if0 7
+       bra     i2csu_write_if_special
+       bt_w_if1 6
+       bra     polarity_local_do
+       bt_w_if0 5
+       bra     point_local_do
+i2csu_write_if_bad
+       mov_wf  t
+       panic   morse_DX
+
+i2csu_write_if_special
+       tst_w_ifnz
+       bra     i2csu_write_if_bad
+       goto    panic_crashread_commanded
+
+code3 code
 ;======================================================================
 ; MASTER/SLAVE deviations
 
 ;----------
-message_for_master
+message_for_master @
 ; Either transmits the message to the master, or if we are the master,
 ; handles it as an incoming message from the notional `slave 0'.
 ;  W           message         unchanged
 ;  GIEH                set             set
-       bt_f_if1 idloc1,idloc1_master
-       goto    loopback_read_byte
+       bt_f_if0 idloc1,idloc1_master
        goto    slave_add_short_message
+       goto    loopback_read_byte
 
 ;======================================================================
-; UNIMPLEMENTED STUFF
-
-;----------
-loopback_read_byte
-       panic   morse_UL
-
- include final.inc
+  include final.inc