;----------------------------------------------------------- ; ; 8 sensors --> MIDI, MIDI --> 16 LEDs : June 2005 ; ; for MAS2005 20Tora Ishikawa ; ;----------------------------------------------------------- ;##### Port Defines ##### smr .equ h'0fffb0 brr .equ h'0fffb1 scr .equ h'0fffb2 tdr .equ h'0fffb3 ssr .equ h'0fffb4 rdr .equ h'0fffb5 p1ddr .equ h'0fffc0 p1dr .equ h'0fffc2 p3ddr .equ h'0fffc4 p3dr .equ h'0fffc6 p5ddr .equ h'0fffc8 p5dr .equ h'0fffca p7dr .equ h'0fffce paddr .equ h'0fffd1 padr .equ h'0fffd3 pbddr .equ h'0fffd4 pbdr .equ h'0fffd6 iprb .equ h'0ffff8 ;##### Vector Defines ##### .section vector,data,locate=h'000000 .data.l start .org h'0000d0 .data.l int_rx_error .data.l int_rx_full ;##### Work RAM Data Defines ##### .section ram,data,locate=h'0fef10 timer1 .res.w 1 timer2 .res.w 1 timer3 .res.w 1 tx_top .res.w 1 tx_end .res.w 1 rx_top .res.w 1 rx_end .res.w 1 velo .res.b 16 rsb .res.b 1 channel .res.b 1 dcb .res.b 1 keyno .res.b 1 data .res.b 1 counter .res.b 1 led .res.b 1 old_sw .res.b 1 dens .res.b 1 pa_buff .res.b 1 pb_buff .res.b 1 .org h'0ff400 rx_fifo .res.b 1024 .org h'0ff800 tx_fifo .res.b 1024 ;***** Reset --> Initialize --> Main Loop ***** .section program,code,locate=h'001000 start: mov.l #h'0fff0f,er7 ; stack pointer set mov.l #h'0fef10,er2 mov.w #h'0fe0,r1 mov.b #0,r0l _ram_clear: mov.b r0l,@er2 inc.l #1,er2 dec.w #1,r1 bne _ram_clear jsr @sci0_init ; SCI initialize mov.b #b'00000000,r0l mov.b r0l,@p1ddr ; set : Port[1] all input mov.b r0l,@p3ddr ; set : Port[3] all input mov.b #b'11111111,r0l mov.b r0l,@pbddr ; set : Port[B] all output mov.b r0l,@paddr ; set : Port[A] all output mov.b r0l,@p5ddr ; set : Port[5] all output mov.b #b'11111111,r0l mov.b r0l,@pbdr mov.b r0l,@padr ; LED all OFF ! jsr @wait_500msec mov.b #b'01110000,r0l ; tx/rx start ! mov.b r0l,@scr andc.b #b'01111111,ccr ; interrupt enable mov.b @p3dr,r0l and.b #b'00000011,r0l cmp.b #0,r0l beq loop1 cmp.b #1,r0l beq loop2 cmp.b #2,r0l beq loop3 bra loop4 loop1: jsr @timer_check_1 ; jsr @tx_midi_check bra loop1 loop2: jsr @timer_check_2 ; jsr @tx_midi_check bra loop2 loop3: jsr @timer_check_3 ; jsr @tx_midi_check bra loop3 loop4: jsr @timer_check_4 jsr @tx_midi_check jsr @rx_midi_check bra loop4 ;***** Common Sub-Routines ***** sci0_init: mov.b #b'00000000,r0l mov.b r0l,@scr mov.b #b'00000000,r0l mov.b r0l,@smr mov.b #15,r0l mov.b r0l,@brr mov.w #500,r0 _sci0_wait: dec.w #1,r0 bne _sci0_wait mov.b @ssr,r0l ; (dummy read) mov.b #0,r0l mov.b r0l,@ssr mov.b #b'00001000,r0l mov.b r0l,@iprb ; SCI0-int priority UP ! rts int_rx_error: bclr #5,@ssr bclr #4,@ssr rte int_rx_full: push.w r0 push.l er5 btst #6,@ssr bclr #6,@ssr mov.w @rx_top,r5 mov.w #0,e5 mov.b @rdr,r0l mov.b r0l,@(rx_fifo,er5) inc.w #1,r5 bclr #2,r5h mov.w r5,@rx_top pop.l er5 pop.w r0 rte tx_midi_check: mov.w @tx_top,r1 mov.w @tx_end,r6 cmp.w r1,r6 bne _tx_exist rts _tx_exist: btst #7,@ssr ; test TRDE bne _tx_seq rts _tx_seq: mov.w #0,e6 mov.b @(tx_fifo,er6),r0l mov.b r0l,@tdr bclr #7,@ssr ; Transmit ! inc.w #1,r6 bclr #2,r6h mov.w r6,@tx_end rts tx_fifo_set: mov.w @tx_top,r6 mov.w #0,e6 mov.b r0h,@(tx_fifo,er6) ; transmit data = [r0h] inc.w #1,r6 bclr #2,r6h mov.w r6,@tx_top rts wait_500msec: mov.l #500,er1 _wait_1: jsr @wait_1msec sub.l #1,er1 bne _wait_1 rts wait_1msec: mov.l #2048,er2 _wait_2: sub.l #1,er2 bne _wait_2 rts ;***** Mode [00] ***** timer_check_1: mov.w @timer1,r1 inc.w #1,r1 mov.w r1,@timer1 cmp.w #2000,r1 beq _timer00_1 rts _timer00_1: mov.w #0,r1 mov.w r1,@timer1 jsr @sw_scan_00 mov.w @timer2,r1 inc.w #1,r1 mov.w r1,@timer2 cmp.w #50,r1 beq _timer00_2 rts _timer00_2: mov.w #0,r1 mov.w r1,@timer2 mov.b @counter,r0l inc.b r0l and.b #b'01111111,r0l mov.b r0l,@counter mov.b @led,r0l bnot #1,r0l mov.b r0l,@led mov.b r0l,@p5dr ; write to Port[5] rts sw_scan_00: mov.b @p1dr,r0l mov.b r0l,@pbdr mov.b r0l,@padr rts ;***** Mode [01] ***** timer_check_2: mov.w @timer1,r1 inc.w #1,r1 mov.w r1,@timer1 cmp.w #2000,r1 beq _timer01_1 rts _timer01_1: mov.w #0,r1 mov.w r1,@timer1 mov.w @timer2,r1 inc.w #1,r1 mov.w r1,@timer2 cmp.w #100,r1 beq _timer01_2 rts _timer01_2: mov.w #0,r1 mov.w r1,@timer2 mov.b @counter,r0l inc.b r0l and.b #b'01111111,r0l mov.b r0l,@counter mov.b @led,r0l bnot #1,r0l mov.b r0l,@led mov.b r0l,@p5dr ; write to Port[5] mov.b @counter,r0l and.b #b'00000001,r0l beq mode01_1 bra mode01_2 mode01_1: mov.b #b'01010101,r0h bra mode01_3 mode01_2: mov.b #b'10101010,r0h mode01_3: mov.b r0h,@pbdr mov.b r0h,@padr rts ;***** Mode [10] ***** timer_check_3: mov.w @timer1,r1 inc.w #1,r1 mov.w r1,@timer1 jsr @dens_test mov.w @timer1,r1 cmp.w #5000,r1 beq _timer10_1 rts _timer10_1: mov.w #0,r1 mov.w r1,@timer1 mov.b @counter,r0l inc.b r0l and.b #b'01111111,r0l mov.b r0l,@counter mov.w @timer2,r1 inc.w #1,r1 mov.w r1,@timer2 cmp.w #50,r1 beq _timer10_2 rts _timer10_2: mov.w #0,r1 mov.w r1,@timer2 mov.b @led,r0l bnot #1,r0l mov.b r0l,@led mov.b r0l,@p5dr ; write to Port[5] rts dens_test: mov.b @dens,r0l inc.b r0l and.b #b'01111111,r0l mov.b r0l,@dens mov.b @counter,r1l cmp.b r0l,r1l ; dens - counter ? bmi all_off bra all_on all_off: mov.b #b'11111111,r0l mov.b r0l,@pbdr mov.b r0l,@padr rts all_on: mov.b #b'00000000,r0l mov.b r0l,@pbdr mov.b r0l,@padr rts ;***** Mode [11] ***** timer_check_4: mov.w @timer1,r1 inc.w #1,r1 mov.w r1,@timer1 jsr @blue_control mov.w @timer1,r1 cmp.w #2000,r1 beq _timer11_1 rts _timer11_1: mov.w #0,r1 mov.w r1,@timer1 jsr @sw_scan_11 mov.w @timer2,r1 inc.w #1,r1 mov.w r1,@timer2 cmp.w #50,r1 beq _timer11_2 rts _timer11_2: mov.w #0,r1 mov.w r1,@timer2 mov.b @led,r0l bnot #1,r0l mov.b r0l,@led mov.b r0l,@p5dr ; write to Port[5] rts sw_scan_11: mov.b @p1dr,r1h ; [R1H] current sensor status mov.b @old_sw,r2h ; [R2H] old sensor status cmp.b r1h,r2h bne _sensor_event rts _sensor_event: mov.b r1h,@old_sw ; renew status buffer xor.b r1h,r2h ; [R2H] event bits mov.b #b'00000001,r3h ; [R3H] bit mask mov.b #0,r3l ; loop counter _sensor_loop: mov.b r3h,r0l and.b r2h,r0l ; event ? beq _sensor_2 mov.b #h'90,r0h jsr @tx_fifo_set mov.b r3l,r0h shll.b r0h add.b #60,r0h jsr @tx_fifo_set mov.b r3h,r0l and.b r1h,r0l ; ON/OFF ? beq _sensor_on bra _sensor_off _sensor_on: mov.b #127,r0h bra _sensor_go _sensor_off: mov.b #0,r0h _sensor_go: jsr @tx_fifo_set _sensor_2: shll.b r3h inc.b r3l cmp.b #8,r3l bne _sensor_loop rts ;***** MIDI Receive Routine ***** rx_midi_check: mov.w @rx_top,r1 mov.w @rx_end,r5 cmp.w r1,r5 bne _rx_exist rts _rx_exist: mov.w #0,e5 mov.b @(rx_fifo,er5),r0h ; received data = [r0h] inc.w #1,r5 bclr #2,r5h mov.w r5,@rx_end btst #7,r0h beq running mov.b r0h,r0l and.b #b'11111000,r0l cmp.b #b'11111000,r0l bne _lower_f8 rts _lower_f8: and.b #b'11110000,r0l cmp.b #b'11110000,r0l bne _lower_f0 mov.b #0,r0l mov.b r0l,@rsb rts _lower_f0 mov.b r0h,r0l and.b #b'00001111,r0l mov.b r0l,@channel mov.b r0h,r0l and.b #b'11110000,r0l mov.b r0l,@rsb mov.b #0,r0l mov.b r0l,@dcb rts running: mov.b @rsb,r0l bne _normal rts _normal: cmp.b #b'11000000,r0l beq _2byte cmp.b #b'11010000,r0l beq _2byte mov.b @dcb,r1l bne _3byte inc.b r1l mov.b r1l,@dcb mov.b r0h,@keyno rts _2byte: rts _3byte: mov.b #0,r1h mov.b r1h,@dcb mov.b r0h,@data mov.b @rsb,r0l cmp.b #h'90,r0l ; 9* event ? beq _hit_01 cmp.b #h'80,r0l ; 8* event ? beq _hit_03 rts _hit_01: mov.b @channel,r0l cmp.b #0,r0l ; MIDI 1 Channel ? beq _hit_02 rts _hit_02: mov.w #0,e1 mov.b #0,r1h mov.b @keyno,r1l and.b #b'00001111,r1l ; NoteNum % 16 mov.b @data,r0l mov.b r0l,@(velo,er1) rts _hit_03: mov.b @channel,r0l cmp.b #0,r0l ; MIDI 1 Channel ? beq _hit_04 rts _hit_04: mov.b #0,r0l mov.b r0l,@data bra _hit_02 blue_control: mov.b @pa_buff,r0l xor.b #b'11111111,r0l mov.b r0l,@padr mov.b @pb_buff,r0l xor.b #b'11111111,r0l mov.b r0l,@pbdr mov.b @dens,r0l inc.b r0l and.b #b'01111111,r0l mov.b r0l,@dens _check_start: mov.w #0,e2 mov.b #0,r2h mov.b #b'00000001,r3h ; [R3H] bit mask mov.b #0,r3l ; loop counter mov.b r3l,@pb_buff _blue_b_loop: mov.b r3l,r2l mov.b @(velo,er2),r1h mov.b @dens,r1l cmp.b r1l,r1h ; velo - dens bls _pass_b mov.b @pb_buff,r0h add.b r3h,r0h ; LED off mov.b r0h,@pb_buff _pass_b: shll.b r3h inc.b r3l cmp.b #8,r3l bne _blue_b_loop mov.b #b'00000001,r3h ; [R3H] bit mask mov.b #0,r3l ; loop counter mov.b r3l,@pa_buff _blue_a_loop: mov.b r3l,r2l add.b #8,r2l ; 9-16 mov.b @(velo,er2),r1h mov.b @dens,r1l cmp.b r1l,r1h ; velo - dens bls _pass_a mov.b @pa_buff,r0h add.b r3h,r0h ; LED off mov.b r0h,@pa_buff _pass_a: shll.b r3h inc.b r3l cmp.b #8,r3l bne _blue_a_loop rts .end