chiark / gitweb /
only autoflush if the message ends with \n
[trains.git] / iwjpictest / harness.asm
1 ; -*- fundamental -*-
2
3 ; This program sets the LED Green and then
4 ; responds to instructions on the serial port
5 ;
6 ; state kept
7 ;   E   current entry
8 ;   R   current address for reading  (Al = low byte; Ah = high_nybble)
9 ;   S   current address for setting  (Bl = low byte; Ah = high_nybble)
10 ;   T   top address nybble for next address set
11 ;   B   bitmask for bit operations
12 ;   F   flags: flag name   weight in FT
13 ;              printset    0x80           print every value we write into *S
14 ;
15 ; prompt is NL CR > SPC initially, then SPC after each command
16 ;
17 ;  data entry
18 ;   0-9a-f    hex digit                      E := (E & 0x0f)<<4 | (digit value)
19 ;   =         confirm value                  print "=", E
20 ;   &         set bitmask                    B := E; print "&", B
21 ;
22 ;  address operations
23 ;   .         set setting address (low byte)   S := TE; print R, ",", S
24 ;   ,         set reading address (low byte)   R := TE; print R, ",", S
25 ;   '         set top address nybble           T := E & 0x0f
26 ;   `         set flags                        F := E; print "`", F
27 ;   @         confirm addresses and bitmask    print R, ",", S, "&", B
28 ;   ~         confirm flags and high nybble    print "`", F, "'", T
29 ;
30 ;  predefined addresses and operations
31 ;   A B C D E set setting address to PORTx     S := 0xf80-0xf84, print op
32 ;   H L Z T   set TRIS and LAT rel.to PORT     modify *(S+0x09), *(S+0x92)
33 ;               according to B                  according to B, and print op
34 ;               (T: toggle LAT, clear TRIS)     NB address addition no carry
35 ;
36 ;  byte operations
37 ;   SPC       read and display                 print *R
38 ;   RET, NL   set whole byte                   printset "S", (*S := E)
39 ;   TAB       read from setting address        print "s", *S
40 ;
41 ;  bitwise operations
42 ;   ?         read and mask                    print "?", (*R & B)
43 ;   _         read and mask from setting addr  printset "_", (*S & B)
44 ;   +         bitwise or (set bits)            printset "+", (*S := *S | B)
45 ;   -         bitwise and-not (clear bits)     printset "-", (*S := *S & ~B)
46 ;   ^         bitwise xor (toggle bits)        printset "^", (*S := *S ^ B)
47 ;
48 ; `printset' means print if `printset' flag is set, otherwise just evaluate
49
50         include         /usr/share/gputils/header/p18f458.inc
51         radix           dec
52
53 clock equ mclock
54         include         insn-aliases.inc
55         include         clockvaries.inc
56 ;       list
57
58 ;------------------------------------------------------------
59 ; variable declarations and RAM memory map
60
61 r               equ     FSR0L
62 rh              equ     FSR0H
63 star_r          equ     INDF0
64
65 s               equ     FSR1L
66 sh              equ     FSR1H
67 star_s          equ     INDF1
68
69  udata_acs
70  res 2 ; skip 0 and 1
71 perpicled_bit   res 1
72
73 sec_etbf udata_acs 0x30
74 e               res 1
75 t               res 1
76 b               res 1
77 f               res 1
78
79 sec_temps udata_acs 0x38
80 hex_temp        res 1
81 original_op     res 1
82 value_temp      res 1
83
84 f_printset_bit  equ     7
85
86 sec_a5 udata_acs 0x40
87 test_loc_a5     res 1
88
89 sec_5a udata_acs 0x42
90 test_loc_5a     res 1
91
92  code
93
94 ;----------------------------------------
95 serial_literal macro serial_literal_macro_char
96 ;       transmits char through the serial port
97 ; W             undefined       undefined
98         mov_lw          serial_literal_macro_char
99         rcall           serial_write_char
100         endm
101
102 start
103         rcall           led_green
104         rcall           serial_setup
105
106         clr_f           e
107         mov_lw          0x80 ; PORTA
108         mov_wf          r
109         set_f           rh
110         mov_wf          s
111         set_f           sh
112         set_f           t
113         mov_lw          (1<<f_printset_bit)
114         mov_wf          f
115
116         mov_lw          0x5a
117         mov_wf          test_loc_5a
118         mov_lw          0xa5
119         mov_wf          test_loc_a5
120
121         serial_literal  10
122         serial_literal  13
123         serial_literal  '>'
124
125 command_loop
126         serial_literal  ' '
127         rcall           serial_read_char
128
129         mov_wf          original_op
130
131 check_last set 0
132 checkequal macro check_value, check_label
133         local check_ifnot
134         add_lw          check_last - check_value
135         bra_nz          check_ifnot
136         bra             check_label
137 check_ifnot
138 check_last set check_value
139         endm
140
141 checkatleast macro check_minvalue, check_label
142                                 ; if it takes, W gets char - check_minvalue
143         add_lw          check_last-check_minvalue
144         local check_ifnot
145         bra_n           check_ifnot
146         bra             check_label
147 check_ifnot
148 check_last set check_minvalue
149         endm
150
151         ; data entry
152         checkequal      '=',    command_byte_confirm
153         checkequal      '&',    command_byte_bitmask_set
154         ; address operations
155         checkequal      '.',    command_address_set_setting
156         checkequal      ',',    command_address_set_reading
157         checkequal      39,     command_address_set_high
158         checkequal      '`',    command_flags_set
159         checkequal      '@',    command_address_confirm
160         checkequal      '~',    command_confirm_flags_highnyb
161         ; byte operations
162         checkequal      ' ',    command_byte_read
163         checkequal      10,     command_byte_set
164         checkequal      13,     command_byte_set
165         checkequal      9,      command_byte_read_fromset
166         ; bit operations
167         checkequal      '?',    command_bitwise_read_mask
168         checkequal      '_',    command_bitwise_read_mask_fromset
169         checkequal      '+',    command_bitwise_or
170         checkequal      '-',    command_bitwise_and_not
171         checkequal      '^',    command_bitwise_xor
172
173         ; TRIS/LAT operations
174         checkequal      'H',    command_trislat_h
175         checkequal      'L',    command_trislat_l
176         checkequal      'Z',    command_trislat_z
177         checkequal      'T',    command_trislat_t
178
179         ; these checkatleast ranges have to be in descending
180         ; order because of the way that checkatleast works
181
182         checkatleast    'f'+1, command_ifnot_letterhexdigit
183         checkatleast    'a', command_letterhexdigit
184 command_ifnot_letterhexdigit
185
186         checkatleast    'E'+1, command_ifnot_portaddr
187         checkatleast    'A', command_portaddr
188 command_ifnot_portaddr
189
190         checkatleast    '9'+1, command_ifnot_digit
191         checkatleast    '0', command_digit
192 command_ifnot_digit
193
194 command_wrong
195         mov_lw          '?'
196         rcall           serial_write_char
197 command_endswitch
198         bra             command_loop
199
200 ;--------------------
201 ; command digits
202
203 command_letterhexdigit
204         add_lw          10
205 command_digit
206         mov_wf          hex_temp
207         swap_fw         e
208         and_lw          0xf0
209         ior_wfw         hex_temp
210         mov_wf          e
211         bra             command_loop
212
213 ;--------------------
214 ; common routines for command loop
215
216 command_endswitch_set
217         mov_wf          star_s
218         bt_f_if0        f, f_printset_bit
219         bra             command_endswitch_set_ifnot_print
220         mov_wf          value_temp
221         mov_fw          original_op
222         rcall           serial_write_char
223         mov_fw          value_temp
224 command_endswitch_phex
225         rcall           serial_write_hex
226 command_endswitch_set_ifnot_print
227         bra             command_endswitch
228
229 ;--------------------
230 ; address commands
231
232 command_address_set_setting
233         mov_ff          t,sh
234         mov_ff          e,s
235         bra             command_address_confirm
236
237 command_address_set_reading
238         mov_ff          t,rh
239         mov_ff          e,r
240         bra             command_address_confirm
241
242 command_address_set_high
243         mov_ff          e,t
244         bra             command_endswitch
245
246 command_flags_set
247         mov_ff          e,f
248         rcall           confirm_flags
249         bra             command_endswitch
250
251 command_address_confirm
252         rcall           confirm_address
253         rcall           confirm_bitmask
254         bra             command_endswitch
255
256 command_portaddr
257         add_lw          PORTA & 0x0ff
258         mov_wf          s
259         set_f           sh
260         mov_fw          original_op
261         rcall           serial_write_char
262         bra             command_endswitch
263
264 ;--------------------
265 ; trislat commands
266
267 command_trislat_h
268         rcall           trislat_setup_indf2_lat
269         ior_wff         INDF2
270 command_trislat_nz
271         rcall           trislat_setup_indf2_tris
272         com_w
273         and_wff         INDF2
274 command_trislat_echo
275         mov_fw          original_op
276         rcall           serial_write_char
277         bra             command_endswitch
278
279 command_trislat_z
280         rcall           trislat_setup_indf2_tris
281         ior_wff         INDF2
282         bra             command_trislat_echo
283
284 command_trislat_l
285         rcall           trislat_setup_indf2_lat
286         com_w
287         and_wff         INDF2
288         bra             command_trislat_nz
289
290 command_trislat_t
291         rcall           trislat_setup_indf2_lat
292         xor_wff         INDF2
293         bra             command_trislat_nz
294
295 ;----------
296 trislat_setup_indf2_lat
297 ; W             undefined       B
298 ; INDF2*        undefined       relevant LAT
299         mov_lw          0x09
300         bra             trislat_setup_indf2_any
301
302 ;----------
303 trislat_setup_indf2_tris
304 ; W             undefined       B
305 ; INDF2*        undefined       relevant TRIS
306         mov_lw          0x12
307         bra             trislat_setup_indf2_any
308
309 ;----------
310 trislat_setup_indf2_any
311 ; W             REG-PORT        B
312 ; INDF2*        undefined       relevant REG
313         add_wfw         s
314         mov_wf          FSR2L
315         set_f           FSR2H
316         mov_fw          b
317         return
318
319 ;----------
320 confirm_address
321 ;       prints R, ",", S
322 ; W             undefined       undefined
323 ; hex_temp      undefined       undefined
324         mov_fw          rh
325         rcall           serial_write_hex_digit
326         mov_fw          r
327         rcall           serial_write_hex
328         mov_lw          ','
329         rcall           serial_write_char
330         mov_fw          sh
331         rcall           serial_write_hex_digit
332         mov_fw          s
333         rcall           serial_write_hex
334         return
335
336 command_confirm_flags_highnyb
337         rcall           confirm_flags
338         serial_literal  39
339         mov_fw          t
340         rcall           serial_write_hex_digit
341         bra             command_endswitch
342
343 ;----------
344 confirm_bitmask
345 ;       prints "&", B
346 ; W             undefined       undefined
347 ; hex_temp      undefined       undefined
348         serial_literal  '&'
349         mov_fw          b
350         rcall           serial_write_hex
351         return
352
353 ;----------
354 confirm_flags
355 ;       prints "`", F
356 ; W             undefined       undefined
357 ; hex_temp      undefined       undefined
358         serial_literal  '`'
359         mov_fw          f
360         rcall           serial_write_hex
361         return
362
363 ;--------------------
364 ; byte operations
365
366 command_byte_confirm
367         serial_literal  '='
368         mov_fw          e
369         bra             command_endswitch_phex
370
371 command_byte_bitmask_set
372         mov_ff          e, b
373         rcall           confirm_bitmask
374         bra             command_endswitch
375
376 command_byte_read
377         mov_fw          star_r
378         bra             command_endswitch_phex
379
380 command_byte_set
381         mov_lw          'S'
382         mov_wf          original_op
383         mov_fw          e
384         bra             command_endswitch_set
385
386 command_byte_read_fromset
387         serial_literal  's'
388         mov_fw          star_s
389         bra             command_endswitch_phex
390
391 ;--------------------
392 ; bit operations
393
394 command_bitwise_read_mask
395         serial_literal  '?'
396         mov_fw          b
397         and_wfw         star_r
398         bra             command_endswitch_phex
399
400 command_bitwise_read_mask_fromset
401         serial_literal  '_'
402         mov_fw          b
403         and_wfw         star_s
404         bra             command_endswitch_phex
405
406 command_bitwise_or
407         mov_fw          b
408         ior_wfw         star_s
409         bra             command_endswitch_set
410
411 command_bitwise_xor
412         mov_fw          b
413         xor_wfw         star_s
414         bra             command_endswitch_set
415
416 command_bitwise_and_not
417         mov_fw          b
418         com_w
419         and_wfw         star_s
420         bra             command_endswitch_set
421
422 ;----------------------------------------
423 led_green
424         bc_f            TRISD, perpicled_bit
425         bs_f            LATD, perpicled_bit
426         return
427
428
429 ;----------------------------------------
430 serial_setup
431 ; W             undefined       undefined
432         mov_lw          (1<<TXEN) | serial_brgh | (1<<TRMT)
433         mov_wf          TXSTA   ; asynch xmit enabled, high baud rate, 8-bit,
434         movlw_movwf_spbrg
435 serial_receive_reset ;from serial_read_if_error
436         mov_lw          (1<<SPEN) | (1<<CREN)
437         mov_wf          RCSTA   ; enable serial port, continuous rx, 8-bit
438         return
439
440 ;----------------------------------------
441 serial_read_char
442 ; on errors, sets LED red and transmits *
443 ; W             undefined       character read
444 serial_read_char_loop
445         bt_f_if0        PIR1, RCIF
446         bra             serial_read_char_loop
447         mov_fw          RCSTA
448         and_lw          (1<<CREN) | (1<<FERR) | (1<<OERR)
449         xor_lw          (1<<CREN)
450         bra_nz          serial_read_if_error
451         mov_fw          RCREG
452         return
453
454 serial_read_if_error
455         mov_lw          '*'
456         rcall           serial_write_char
457         rcall           serial_receive_reset
458         bra             serial_read_char_loop
459
460  include syncwrite.inc
461  end