chiark / gitweb /
reserve /^ [^ ;]/ for statements inserted for debugging purposes
[trains.git] / detpic / serout.asm
1 ;======================================================================
2 ; SERIAL PORT - TRANSMISSION TO HOST
3
4   include common.inc
5   code
6 ;----------------------------------------
7 serial_addbyte
8 ;  W                    byte to xmit to host    trashed
9 ;  FSR0                 any                     set for serial_addbyte_another
10 ;  outbuf, outmsg_*     buffer not full         adjusted appropriately
11 ;  STATUS               any                     trashed
12 ;  all others           any                     not interfered with
13 ;
14         mov_lfsr outbuf, 0
15         mov_ff  outmsg_end, FSR0L
16         bs_f    PIR1, TXIE ; ensure we get interrupted and try to xmit
17 ;...
18 serial_addbyte_another
19 ;  W                    byte to xmit to host    trashed
20 ;  FSR0                 from _addbyte[_another] updated for ..._another again
21 ;  outbuf, outmsg_*     buffer not full         adjusted appropriately
22 ;  STATUS               any                     trashed
23 ;  all others           any                     not interfered with
24 ;
25         mov_wf  POSTINC0
26         bc_f    FSR0L, 7
27         mov_fw  FSR0L
28         mov_wf  outmsg_end
29         cmp_fw_ifne outmsg_begin
30         return
31         ; too many
32         panic morse_HB
33
34 ;----------------------------------------------------------------------
35 serialtx_intrl
36         bt_f_if1 INTCON, RBIF
37         rcall   portb_read ; check flow control
38
39         ; are we ready to transmit ?
40         bt_f_if0 PIR1, TXIF
41         return
42         bt_f_if0 PIR1, TXIE
43         return
44
45         mov_lfsr outbuf, 0
46         mov_fw  outmsg_begin
47         mov_wf  FSR0L
48         xor_wfw outmsg_end
49         bra_z   tx_bufempty
50
51         mov_fw  INDF0
52         mov_wf  TXREG
53         bra_n   tx_justsent_noacknmra
54         ; we've just sent the last byte of some message:
55         ; maybe we push an NMRADONE message on the front
56         mov_fw  acknmra
57         bra_nz  tx_acknmra_insert
58 tx_justsent_noacknmra
59         inc_f   outmsg_begin
60         bc_f    outmsg_begin, outbuf_szln2
61         call    led_green ; we're transmitting
62         goto    i2c_consider_restartread
63
64 tx_bufempty
65         ; maybe we send an NMRADONE
66         mov_fw  acknmra
67         bra_nz  tx_acknmra_send
68         ; nothing at all to do:
69         bc_f    PIR1, TXIE
70         goto    led_black ; we're not transmitting
71
72 tx_acknmra_insert
73         sub_wff acknmra ; we're acking these now
74         mov_wf  INDF0 ; write it over the message we just sent
75         goto    led_green
76
77 tx_acknmra_send
78         sub_wff acknmra ; we're acking these now
79         mov_wf  TXREG
80         goto    led_green
81
82 ;----------------------------------------
83 portb_read
84 ;  W                    undefined       value from PORTB
85         mov_fw  PORTB
86         bc_f    INTCON, RBIF
87
88         bt_w_if0  p0_rs232_fcin >> 4
89         bra     tx_fcdisable
90         ; tx enable:
91
92         bs_f    PIR1, TXIE
93         bc_f    INTCON, RBIE
94         return
95
96 tx_fcdisable
97         bc_f    PIR1, TXIE
98         bs_f    INTCON, RBIE
99         goto    led_red ; flow control forces us not to transmit
100
101 ;----------------------------------------------------------------------
102 serialtxfc_init
103         bc_f    INTCON2, RBIP
104         bs_f    INTCON, RBIE
105         rcall   portb_read
106         return
107
108 ;----------------------------------------------------------------------
109 serialtxbuf_init
110         clr_f   outmsg_end
111         clr_f   outmsg_begin
112         mov_lw  b'00001001'
113         goto    serial_addbyte
114
115 ;======================================================================
116   include final.inc