CEBPIC=                ../cebpic/
 PICNOS=                0 1 2
 
-PROGRAMS=              test-sofar
-OBJS_test-sofar=       vectors.o panic.o routines-led.o i2clib.o       \
+PROGRAMS=              program
+OBJS_program=          vectors.o panic.o routines-led.o i2clib.o       \
                        misc.o slave.o detect.o variables.o points.o    \
-                       syncwrite.o
-XCODEN_test-sofar=     morse
-XCODE1_test-sofar=     blank2 ours+pindata
+                       syncwrite.o reverse.o test-sofar.o
+XCODEN_program=        morse
+XCODE1_program=        blank2 ours+pindata
 
 INCLUDES=      common.inc                      \
                final.inc                       \
                vectors.fin                     \
                pindata.inc                     \
                detect.inc                      \
-               points.fin
+               points.fin                      \
+               program.fin                     \
+               reverse.fin
 
 VARSFILES=     variables
 
 
 ;   BSR                        Not used                Not used
 ;   t                  Low ISR                 Low ISR
 ;   TBLPTR*,TABLAT     Low ISR                 Low ISR
+;   PROD*              Low ISR                 Low ISR
 ;   FSR0               Low ISR                 Low ISR
 ;   FSR1               Low ISR                 High ISR (detect[1])
 ;   FSR2               Low ISR                 High ISR (detect[1])
 ;----------------------------------------
 ; For disabling all interrupts, to make a critical section:
 ; (for use from main program and Low ISR only)
+;
+;  GIEH                        modified appropriately
+;  everything else     preserved
 
 mask_int_high macro
        bc_f    INTCON,GIEH
 ;----------------------------------------
 ; For the fix specified in the silicon errata:
 ; silicon revision B4 issue 4
+;
+;                      Before          After
+;  TABLAT              any             data from flash
+;  TBLPTR*             correct         incremented/decremented
+;  everything else     any             preserved
 
 tblrd_postinc_fixup macro
        tblrd   *+
 ; For setting up TBLPTR according to the picno
 
 load_perpic_tblptr macro flash_map_base, perpic_entry_size
-       movlw   perpic_entry_size
+;
+;                      Before          After
+;  TBLPTR*             any             set
+;  W, STATUS, PROD*    any             undefined
+;  everything else     any             preserved
+;
+       mov_lw  perpic_entry_size
        mul_wf  picno
 
        mov_lw  flash_map_base & 0xff
 p0_booster_pwm         equ     1c
  radix dec
 
+;                      
+;  LAT*                        may be subject to read-modify-write, see below
+;  TRIS*               may be subject to read-modify-write, see below
+;  PORT*               may be read, see below
+;  everything else     untouched
+;
+;                      LAT*<bit>       TRIS*<bit>      PORT*
+;  pin_z               untouched       set             untouched
+;  pin_h               set             cleared         untouched
+;  pin_l               cleared         cleared         untouched
+;  pin_nz              untouched       cleared         untouched
+;  pin_vh              set             untouched       untouched
+;  pin_vl              cleared         untouched       untouched
+;  pin_ifh             untouched       untouched       read
+;  pin_ifl             untouched       untouched       read
+
 pin_z  macro   pinspec
        bs_f    TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
        endm
 
        code
 
 ;----------
-det_common_init
+detect_local_init
        mov_lw  b'111'  ; turn off comparator, or we can't use pins
        mov_wf  CMCON   ;   RD0-RD4 as digital inputs
        mov_lw  b'0110' ; turn off A/D except perhaps for pin
        ior_wff ADCON1  ;   AN0 ie SPARE ie RA0 (same reason as above)
-       return
 
-;----------
-det_slave_init
-       rcall   det_common_init
+       ; compute buf0_startval
+       clr_w
+       bt_f_if0 idloc1,idloc1_boarddet
+       mov_lw  0x14 ; see under reversers, below
+       mov_wf  buf0_startval
+
        set_f   scana
        set_f   scanb
        set_f   scanc
        set_f   scand
        set_f   scane
+       mov_ff  buf0_startval, buf0
 
-       clr_w
-
-       bt_f_if0 idloc1,idloc1_boarddet
-       mov_lw  0x14 ; see under reversers, below
-
-       mov_wf  buf0_startval
+       return
 
+;----------
+detect_slave_init
        mov_lfsr outbuf, 2
        clr_f   outmsg_end
 
        goto    reset_message_buffer
 
-;----------
-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
-
-;.....
 ;----------------------------------------
 slave_add_short_message
+; Queues a message byte for transmission to the master.
+; It will be transmitted as an extra message byte, when we are polled.
 ;  W           message         unchanged
 ;  GIEH                set             set
        mask_int_high
        bra_n   msg_copy_loop
 
        mov_ff  FSR2L, outmsg_end
+       mov_ff  buf0_startval, buf0
 
 reset_message_buffer
 ;  FSR1/buf0/message_buffer    any             set to empty
 ;  may be called from High ISR or during init
        mov_lfsr message_buffer, 1
-       mov_ff  buf0_startval, buf0
        clr_f   INDF1
        return
 
 
- extern det_slave_init
- extern message_for_master
+ extern detect_local_init
+ extern detect_slave_init
+ extern slave_add_short_message
  extern backgroundloop_reversers
  extern backgroundloop_detectors
 
- extern slave
- extern loopback_read_byte
+ extern test_sofar_slave_startup_hook
+ extern trackpower_decide
+ extern serial_interrupt
 
  include vectors.fin
  include i2clib.inc
  include variables+vars.fin
  include detect.inc
  include ../iwjpictest/syncwrite.inc
+ include reverse.fin
+ include program.fin
 
        end
 
        bra_nn  init_bitnum2bit_loop
        return
 
-;======================================================================
-; MASTER/SLAVE deviations
-
  include final.inc
 
 X      test-sofar:ch           ; bad character received from host
 
 # Unimplemented things
-UP             ; unimplemented point timer setting
-UL             ; unimplemented master loopback read
+UP
+UL
+UM
+UE
 
 ZM     i2clib:st,:sspstat,:sspcon1,:sspcon2 ; for testing
 ZS     i2clib:st,:sspstat,:sspcon1,:st_orig ; for testing
 
 ;      pointmsg        undefined       message from master
 
 ptix2bitlat equ 0x300
- udata ptix2bitlat
+ptix2bitlat_section udata ptix2bitlat
  res maxpoint * 2      ; bit and LAT*
                        ; for unused point, 0x00 and 0x00
 
 
--- /dev/null
+;======================================================================
+; MAIN PROGRAM AND GLUE
+
+ include common.inc
+ code
+
+;----------
+vector_reset
+       clr_f   INTCON
+       bs_f    RCON, IPEN      ; interrupt priorities
+
+       call    idlocs_init
+
+       tst_f_ifnz picno
+       goto    slave
+       bra     master
+
+;----------
+master_interrupt_low
+       enter_interrupt_low
+       call    i2cm_interrupt
+       call    serial_interrupt
+       return_interrupt_low
+
+;======================================================================
+; MASTER/SLAVE deviations
+
+;----------
+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
+       goto    slave_add_short_message
+
+;======================================================================
+; UNIMPLEMENTED STUFF
+
+;----------
+loopback_read_byte
+       panic   morse_UL
+
+ include final.inc
 
--- /dev/null
+ extern slave
+ extern master
+ extern message_for_master
 
 ;======================================================================
 ; REVERSERS
 
+ include common.inc
+
 ;----------------------------------------
 ; LOCAL REVERSERS - Variables
 
 ;----------------------------------------
 ; MASTER - Access bank variables and other sections
 
-board0                         res     1
-gather                         res     1
-
-polarities_waiting             res     1
-       ; no of 11... entries in reversers_commanded_buffer
-
  udata
 polarities_commanded_buffer    res     maxpic+1
        ; Each byte is:
        rcall   polarity_local_do
 
        com_fw  maska
-       and_wf  TRISA
+       and_wff TRISA
 
        com_fw  maske
-       and_wf  TRISE
+       and_wff TRISE
 
        return
 
 ;              is a for LSb in message to PIC, b for next bit,
 ;              and so on until f for bit 5.  (See polarity_do_here, below.)
 ;
-       mov_lfsr message_buffer_start,0
-       mov_lfsr reversers_commanded_buffer,1
+; we accumulate (`gather') the `g' bits in t.
+;
+
+ panic morse_UM
+;;     mov_lfsr message_buffer_start,0
+
+
+       mov_lfsr polarities_commanded_buffer,1
        mov_fw  POSTINC0        ; W =           10010RRR
-       mov_wf  gather          ; gather =      10010ggg
-       xor_lw  0b10001000      ; gather =      00011ggg
-loop ; exit from this loop is done by board popping and returning
+       mov_wf  t               ; t =           10010ggg
+       xor_lw  b'10001000'     ; t =           00011ggg
+loop ; Exit from this loop is done by board popping, doing some
+     ; final stuff, and returning.   See board_next_none, below.
        rrc_fw  INDF0           ; W =           ?Mhhhhhh  C = g
-       rlc_f   gather          ; gather =      0*11g+    C = ?
+       rlc_f   t               ; t =           0*11g+    C = ?
        bra_nn  if_not_gathered
-       ;                       ; gather =      11gggggg
-       mov_fw  gather
+       ;                       ; t =           11gggggg
+       mov_fw  t
        rcall   board
-       mov_lw  0b00000011      ; W =           00000011
-       mov_wf  gather          ; gather =      00000011
+       mov_lw  b'000000011'    ; W =           00000011
+       mov_wf  t               ; t =           00000011
 if_not_gathered                        ; *INDF0 =      Mhhhhhhg
        rr_fw   INDF0           ; W =           gMhhhhhh
-       or_lw   0b11000000      ; W =           11hhhhhh
+       ior_lw  b'011000000'    ; W =           11hhhhhh
        rcall   board
        bt_f_if1 POSTINC0,7     ; *POSTINC0 :   M.......
        bra     loop
        ; otherwise:
-       panic   morse_PS
+       panic   morse_RS
 
 ;----------
 board
        bra     board_next_none_more_message
 ;...
 ; now we're exiting from the loop in when_reverse_message_found
-       pop
-       return
+       pop      ; that disposes of the call to `board'
+
+       goto    trackpower_decide
+       ; that will turn it off, since polarities_waiting is nonzero
+
+;----------
+board_next_none_more_message
+       panic   morse_RL
 
+;----------
+trackpower_decide
+       panic   morse_UE
+
+;======================================================================
+ include final.inc
 
--- /dev/null
+ extern polarity_local_init
 
 slave
        mov_fw  picno
        call    i2cs_init
+       call    test_sofar_slave_startup_hook
        call    bitnum2bit_init
        call    points_local_init
-       call    det_slave_init
+       call    polarity_local_init
+       call    detect_local_init
+       call    detect_slave_init
 
        mov_lw  (1<<GIEH) | (1<<GIEL)
        mov_wf  INTCON
 
 
        code
 
-;----------
-vector_reset
-       clr_f   INTCON
-       bs_f    RCON, IPEN      ; interrupt priorities
-
-       call    idlocs_init
-
-       tst_f_ifnz picno
-       goto    slave
-       bra     master
-
-;----------
-master_interrupt_low
-       enter_interrupt_low
-       call    i2cm_interrupt
-       call    serial_interrupt
-       return_interrupt_low
-
 ;----------
 master_interrupt_high
        panic   morse_IP
        call    led_green
        bra     m_infinite
 
-;----------
-loopback_read_byte
-       panic   morse_UL
-
 ;----------
 serialu_read_char
 ;      W       character read          any
        pin_l   p0_cdu_enable
        return
 
+;----------------------------------------------------------------------
+; SLAVE  TESTING VERSION
+; buffer is at 0x100..0x107
+; FSR0 is write pointer
+; FSR1 is read pointer
+
+;----------
+test_sofar_slave_startup_hook
+       mov_fw  picno
+       mov_wf  t
+;...
+s_shownum_loop
+       call    s_delay
+       call    led_red
+       call    s_delay
+       call    led_black
+
+       dec_f_ifnz t
+       bra     s_shownum_loop
+; Exit loop:
+
+       mov_lw  0x07
+       mov_wf  qqTRISE
+       set_f   qqTRISD
+       set_f   qqTRISC
+       set_f   qqTRISB
+       mov_lw  0x7f
+       mov_wf  qqTRISA
+
+       mov_lw  0x5
+       mov_wf  qqLATE
+       mov_lw  0x55
+       mov_wf  qqLATD
+       mov_wf  qqLATC
+       mov_wf  qqLATB
+       mov_wf  qqLATA
+
+       mov_lw  0x1a
+       mov_wf  qqTRISA-1
+       mov_lw  0x2b
+       mov_wf  qqTRISE+1
+       mov_lw  0x3c
+       mov_wf  qqLATE+1
+       mov_lw  0x4d
+       mov_wf  test_sofar_slave_buf-1
+       mov_wf  test_sofar_slave_buf-2
+       mov_wf  test_sofar_slave_buf-3
+       mov_wf  test_sofar_slave_buf-4
+
+;...
+;----------
+s_buffer_reset
+       mov_lfsr test_sofar_slave_buf, 0
+       mov_lfsr test_sofar_slave_buf, 1
+       return
+
+;----------
+QQ_i2csu_read_begin
+       rcall   led_green
+QQ_i2csu_read_another
+       mov_fw  POSTINC1
+       bc_f    FSR1L, test_sofar_s_bufbit
+       goto    i2cs_read_data
+
+
+;----------
+s_panic_req
+       clr_f   ch
+       panic   morse_X
+
 ;======================================================================
 
 m_delay4 rcall m_delay2
 
 t              res     1       ; general temporary
 outmsg_end     res     1       ; first empty byte in outbuf
 
-isr_low_save_w         res     1
-isr_low_save_status    res     1
+isr_low_save_w         res     1 ; see {enter,return}_interrupt_low
+isr_low_save_status    res     1 ;  in common.inc
+
+polarities_waiting     res     1
+ ; no of 11... entries in polarities_commanded_buffer, see reversers.asm
 
 outbuf_section udata 0x200
 outbuf_szln2   equ     7
 bitnum2bit_section udata 0x0f8
 bitnum2bit     res     8       ; bitnum2bit[x] = 1<<x
 
+test_sofar_slave_buf_section udata 0x180
+
+test_sofar_s_bufbit equ 4 ; ln2(buffer size)
+test_sofar_slave_buf
+ res 1
+qqTRISA res 1
+qqTRISB res 1
+qqTRISC res 1
+qqTRISD res 1
+qqTRISE res 1
+ res 1
+qqLATA res 1
+qqLATB res 1
+qqLATC res 1
+qqLATD res 1
+qqLATE res 1
+ res 4
+
  include final.inc
 
 ;======================================================================