;----------------------------------------------------------------------- ; KEBOARD Module Program [Z-80] ;----------------------------------------------------------------------- name z80main p65c02 ;##### MemoryEI/O MAP ##### program_area equ 00000h int_area equ 00038h work equ 04000h ram_area equ 04100h stack_pointer equ 5fffh uart0 equ 08000h uart1 equ 08001h pia0 equ 09000h pia1 equ 09001h pia2 equ 09002h pia3 equ 09003h dipsw equ 0a000h ext1 equ 0b000h ext2 equ 0c000h ;##### RAM Map ##### org work work0 ds 1 work1 ds 1 work2 ds 1 work3 ds 1 rxtop ds 1 rxend ds 1 rxdata ds 1 exclusive_flag ds 1 ex_id ds 1 ex_chno ds 1 ex_function ds 1 ex_group ds 1 ex_machine ds 1 ex_data ds 1 ex_eox ds 1 txtop ds 1 txend ds 1 txdata ds 1 channel ds 1 velocity ds 1 offset ds 1 sw_scan_phase ds 1 group_offset ds 1 group_event_bit ds 1 group_status ds 1 rsb ds 1 dcb ds 1 note_buffer ds 1 all_off_timer ds 1 all_off_flag ds 1 pedal_new ds 1 pedal_old ds 1 pedal_timer ds 1 group_low_offset ds 1 sw_group_1_phase_0 ds 1 sw_group_2_phase_0 ds 1 sw_group_3_phase_0 ds 1 sw_group_4_phase_0 ds 1 sw_group_5_phase_0 ds 1 sw_group_6_phase_0 ds 1 sw_group_7_phase_0 ds 1 sw_group_8_phase_0 ds 1 sw_group_1_phase_2 ds 1 sw_group_2_phase_2 ds 1 sw_group_3_phase_2 ds 1 sw_group_4_phase_2 ds 1 sw_group_5_phase_2 ds 1 sw_group_6_phase_2 ds 1 sw_group_7_phase_2 ds 1 sw_group_8_phase_2 ds 1 org ram_area rxfifo ds 256 txfifo ds 256 ;##### Macro Defines ##### macro &push db 0f5h ; push af endmac macro &pop db 0f1h ; pop af endmac macro &ex db 008h ; ex af,af' db 0d9h ; exx endmac macro &stack db 031h ; ld sp,nn db .low.\0 db .high.\0 endmac macro &lda db 03ah ; ld a,(nn) db .low.\0 db .high.\0 endmac macro &sta db 032h ; ld (nn),a db .low.\0 db .high.\0 endmac macro &movi db 03eh ; ld a,n db \0 db 032h ; ld (nn),a db .low.\1 db .high.\1 endmac macro &move db 03ah ; ld a,(nn) db .low.\0 db .high.\0 db 032h ; ld (nn),a db .low.\1 db .high.\1 endmac macro &movex db 03ah ; ld a,(nn) db .low.\0 db .high.\0 db 0eeh,0ffh ; xor a,0ffh db 032h ; ld (nn),a db .low.\1 db .high.\1 endmac macro &jsr db 0cdh ; call xx,nn db .low.\0 db .high.\0 endmac macro &jsrnz db 0c4h ; call nz,nn db .low.\0 db .high.\0 endmac macro &jmp db 0c3h ; jp xx,nn db .low.\0 db .high.\0 endmac macro &beq db 0cah ; jp z,nn db .low.\0 db .high.\0 endmac macro &bne db 0c2h ; jp nz,nn db .low.\0 db .high.\0 endmac macro &rts db 0c9h ; ret xx endmac macro &equal_return db 0c8h ; ret z endmac macro ¬_equal_return db 0c0h ; ret nz endmac macro &and db 0e6h ; and a,n db \0 endmac macro &eor db 0eeh ; xor a,n db \0 endmac macro &ora db 0f6h ; or a,n db \0 endmac macro &cmp db 0feh ; cp a,n db \0 endmac macro &inc db 03ch ; inc a endmac macro &dec db 03dh ; dec a endmac macro &seth db 026h ; ld h,n db .high.\0 endmac macro &setl db 03ah ; ld a,(nn) db .low.\0 db .high.\0 db 06fh ; ld l,a endmac macro &gethl db 07eh ; ld a,(hl) endmac macro &puthl db 077h ; ld (hl),a endmac macro &nop db 000h ; endmac ;##### Program Start ( <-- Power ON Reset ) ##### org program_area &stack stack_pointer db 0f3h ; db 0edh,56h ; &jmp main_routine ;##### MIDI INT Sequence ##### org int_area &ex &setl rxtop &lda uart0 &puthl db 2ch ; db 7dh ; &sta rxtop &ex db 0fbh ; db 0edh,4dh ; ;##### Main Program Area ##### main_routine: &jsr waiting &jsr io_initial loop: &jsr sw_scan &jsr pedal_scan &jsr all_note_off_check &jsr midi_rx_check &jsr midi_tx_sequence &jmp loop ;##### Pedal Check Routine ##### pedal_scan: &lda pedal_timer &inc &sta pedal_timer &cmp 10 ¬_equal_return &movi 0,pedal_timer &lda dipsw &and 00000001b &sta pedal_new db 47h ; &lda pedal_old db 0a8h ; &equal_return &seth txfifo &setl txtop &lda channel &and 00001111b &ora 10110000b &puthl db 2ch ; db 3eh,40h ; ld a,#40h &puthl db 2ch ; &lda pedal_new &cmp 0 &beq pedal_off db 3eh,7fh ; ld a,#7fh &jmp pedal_final pedal_off: db 0afh ; pedal_final: &puthl db 2ch ; db 7dh ; &sta txtop &move pedal_new,pedal_old &rts ;##### All Note Off Check Routine ##### all_note_off_check: &lda all_off_timer &inc &sta all_off_timer &cmp 50 ¬_equal_return &movi 0,all_off_timer &lda all_off_flag &cmp 0 &equal_return &lda sw_group_1_phase_0 &cmp 0 ¬_equal_return &lda sw_group_2_phase_0 &cmp 0 ¬_equal_return &lda sw_group_3_phase_0 &cmp 0 ¬_equal_return &lda sw_group_4_phase_0 &cmp 0 ¬_equal_return &lda sw_group_5_phase_0 &cmp 0 ¬_equal_return &lda sw_group_6_phase_0 &cmp 0 ¬_equal_return &lda sw_group_7_phase_0 &cmp 0 ¬_equal_return &lda sw_group_8_phase_0 &cmp 0 ¬_equal_return &seth txfifo &setl txtop &lda channel &and 00001111b &ora 10110000b &puthl db 2ch ; db 3eh,7bh ; ld a,#7bh &puthl db 2ch ; db 3eh,00h ; ld a,#0 &puthl db 2ch ; db 7dh ; &sta txtop &movi 0,all_off_flag &rts ;##### SW Event Check Routine ##### sw_scan_event_check: &lda group_event_bit &and 00000001b &cmp 0 &jsrnz sw_scan_001 &lda group_event_bit &and 00000010b &cmp 0 &jsrnz sw_scan_002 &lda group_event_bit &and 00000100b &cmp 0 &jsrnz sw_scan_003 &lda group_event_bit &and 00001000b &cmp 0 &jsrnz sw_scan_004 &lda group_event_bit &and 00010000b &cmp 0 &jsrnz sw_scan_005 &lda group_event_bit &and 00100000b &cmp 0 &jsrnz sw_scan_006 &lda group_event_bit &and 01000000b &cmp 0 &jsrnz sw_scan_007 &rts sw_scan_001: &movi 0,group_low_offset &jmp sw_scan_010 sw_scan_002: &movi 1,group_low_offset &jmp sw_scan_010 sw_scan_003: &movi 2,group_low_offset &jmp sw_scan_010 sw_scan_004: &movi 3,group_low_offset &jmp sw_scan_010 sw_scan_005: &movi 4,group_low_offset &jmp sw_scan_010 sw_scan_006: &movi 5,group_low_offset &jmp sw_scan_010 sw_scan_007: &movi 6,group_low_offset &jmp sw_scan_010 sw_scan_010: &seth txfifo &setl txtop &lda channel &and 00001111b &ora 10010000b &puthl db 2ch ; &lda group_offset db 47h ; &lda group_low_offset db 80h ; db 47h ; &lda offset db 80h ; db 47h ; &lda group_offset &cmp 0 &beq sw_scan_adjust db 05h ; sw_scan_adjust: db 78h ; &and 01111111b &puthl db 2ch ; &lda group_event_bit db 47h ; &lda group_status db 0a0h ; &cmp 0 &beq sw_scan_key_off &movi 1,all_off_flag &lda velocity &jmp sw_scan_final sw_scan_key_off: db 0afh ; sw_scan_final: &puthl db 2ch ; db 7dh ; &sta txtop &rts ;##### SW Scan Routine ##### sw_scan: &lda sw_scan_phase db 47h ; &inc &sta sw_scan_phase db 78h ; &cmp 1 &beq sw_scan_seq_1 &cmp 2 &beq sw_scan_seq_2 &cmp 3 &beq sw_scan_seq_3 &cmp 4 &beq sw_scan_seq_4 &cmp 5 &beq sw_scan_seq_5 &cmp 6 &beq sw_scan_seq_6 &cmp 7 &beq sw_scan_seq_7 &cmp 8 &beq sw_scan_seq_8 &movi 1,sw_scan_phase &rts sw_scan_seq_1: &movex pia0,sw_group_1_phase_2 &movi 11111101b,pia1 &lda sw_group_1_phase_2 db 47h ; &lda sw_group_1_phase_0 db 0a8h ; &equal_return &sta group_event_bit db 78h ; &sta sw_group_1_phase_0 &sta group_status &movi 0,group_offset &jsr sw_scan_event_check &rts sw_scan_seq_2: &movex pia0,sw_group_2_phase_2 &movi 11111011b,pia1 &lda sw_group_2_phase_2 db 47h ; &lda sw_group_2_phase_0 db 0a8h ; &equal_return &sta group_event_bit db 78h ; &sta sw_group_2_phase_0 &sta group_status &movi 7,group_offset &jsr sw_scan_event_check &rts sw_scan_seq_3: &movex pia0,sw_group_3_phase_2 &movi 11110111b,pia1 &lda sw_group_3_phase_2 db 47h ; &lda sw_group_3_phase_0 db 0a8h ; &equal_return &sta group_event_bit db 78h ; &sta sw_group_3_phase_0 &movi 13,group_offset &jsr sw_scan_event_check &rts sw_scan_seq_4: &movex pia0,sw_group_4_phase_2 &movi 11101111b,pia1 &lda sw_group_4_phase_2 db 47h ; &lda sw_group_4_phase_0 db 0a8h ; &equal_return &sta group_event_bit db 78h ; &sta sw_group_4_phase_0 &movi 19,group_offset &jsr sw_scan_event_check &rts sw_scan_seq_5: &movex pia0,sw_group_5_phase_2 &movi 11011111b,pia1 &lda sw_group_5_phase_2 db 47h ; &lda sw_group_5_phase_0 db 0a8h ; &equal_return &sta group_event_bit db 78h ; &sta sw_group_5_phase_0 &movi 25,group_offset &jsr sw_scan_event_check &rts sw_scan_seq_6: &movex pia0,sw_group_6_phase_2 &movi 10111111b,pia1 &lda sw_group_6_phase_2 db 47h ; &lda sw_group_6_phase_0 db 0a8h ; &equal_return &sta group_event_bit db 78h ; &sta sw_group_6_phase_0 &movi 31,group_offset &jsr sw_scan_event_check &rts sw_scan_seq_7: &movex pia0,sw_group_7_phase_2 &movi 01111111b,pia1 &lda sw_group_7_phase_2 db 47h ; &lda sw_group_7_phase_0 db 0a8h ; &equal_return &sta group_event_bit db 78h ; &sta sw_group_7_phase_0 &movi 37,group_offset &jsr sw_scan_event_check &rts sw_scan_seq_8: &movex pia0,sw_group_8_phase_2 &movi 11111111b,pia1 &lda sw_group_8_phase_2 db 47h ; &lda sw_group_8_phase_0 db 0a8h ; &equal_return db 78h ; &sta sw_group_8_phase_0 &movi 43,group_offset &jsr sw_scan_event_check &movi 11111110b,pia1 &movi 1,sw_scan_phase &rts ;##### MIDI Receive Data Check Sequence ##### midi_rx_check: &lda rxend db 47h ; &lda rxtop db 0b8h ; &equal_return db 78h ; &inc &sta rxend db 68h ; &seth rxfifo &gethl &sta rxdata &cmp 0f0h &bne midi_rx_not_exclusive_start_mark &movi 1,exclusive_flag &movi 0,rsb &rts midi_rx_not_exclusive_start_mark: &lda exclusive_flag &cmp 0 &bne midi_rx_exclusive &rts ;********************* Exclusive Only !!! &lda rxdata &and 10000000b &cmp 0 &beq midi_rx_running &lda rxdata &and 11110000b &cmp 0f0h &bne midi_rx_not_real_time &rts midi_rx_not_real_time: &cmp 90h &beq midi_rx_note_on_status midi_rx_inhibit_status: &movi 0,rsb &rts midi_rx_note_on_status: &lda rxdata &and 00001111b db 47h ; &lda channel db 0b8h ; &beq midi_rx_inhibit_status &move rxdata,rsb &movi 0,dcb &rts midi_rx_running: &lda rsb &cmp 0 &bne midi_rx_true_running &rts midi_rx_true_running: &lda dcb &cmp 0 &bne midi_rx_3rd_data &movi 1,dcb &move rxdata,note_buffer &rts midi_rx_3rd_data: &movi 0,dcb &seth txfifo &setl txtop &lda rsb &puthl db 2ch ; &lda note_buffer &puthl db 2ch ; &lda rxdata &puthl db 2ch ; db 7dh ; &sta txtop &rts midi_rx_exclusive: &lda exclusive_flag &cmp 1 &bne midi_exclusive_not_1st &movi 2,exclusive_flag &move rxdata,ex_id &rts midi_exclusive_not_1st: &cmp 2 &bne midi_exclusive_not_2nd &movi 3,exclusive_flag &move rxdata,ex_chno &rts midi_exclusive_not_2nd: &cmp 3 &bne midi_exclusive_not_3rd &movi 4,exclusive_flag &move rxdata,ex_function &rts midi_exclusive_not_3rd: &cmp 4 &bne midi_exclusive_not_4th &movi 5,exclusive_flag &move rxdata,ex_group &rts midi_exclusive_not_4th: &cmp 5 &bne midi_exclusive_not_5th &movi 6,exclusive_flag &move rxdata,ex_machine &rts midi_exclusive_not_5th: &cmp 6 &bne midi_exclusive_not_6th &movi 7,exclusive_flag &move rxdata,ex_data &rts midi_exclusive_not_6th: &move rxdata,ex_eox &lda ex_id &cmp 40h &bne midi_rx_exclusive_format_error &lda ex_chno &cmp 0fh &bne midi_rx_exclusive_format_error &lda ex_function &and 11110000b &cmp 70h &bne midi_rx_exclusive_format_error &lda ex_group &cmp 7fh &bne midi_rx_exclusive_format_error &lda ex_eox &cmp 0f7h &bne midi_rx_exclusive_format_error &lda ex_machine &cmp 0 ; MACHINE ID = <0> ********** &bne midi_rx_exclusive_format_error &lda ex_function &cmp 70h &beq midi_rx_exclusive_70_channel_set &cmp 71h &beq midi_rx_exclusive_71_velocity_set &cmp 72h &beq midi_rx_exclusive_72_offset_set &rts midi_rx_exclusive_70_channel_set: &move ex_data,channel &rts midi_rx_exclusive_71_velocity_set: &move ex_data,velocity &rts midi_rx_exclusive_72_offset_set: &move ex_data,offset &rts midi_rx_exclusive_format_error: &movi 0,exclusive_flag &rts ;##### MIDI Transmit Data Check Sequence ##### midi_tx_sequence: &lda txend db 47h ; &lda txtop db 0b8h ; &bne midi_tx_data_found &rts midi_tx_data_found: &lda uart1 &and 00000001b &cmp 0 &bne midi_tx_uart_ok &rts midi_tx_uart_ok: db 78h ; &inc &sta txend db 68h ; &seth txfifo &gethl &sta uart0 &rts ;##### System I/O Initialize ##### io_initial: ; &ex &seth rxfifo &ex ; &movi 0,work0 &seth work ram_clear_work: &setl work0 db 3eh,00h ; ld a,#0 &puthl &lda work0 &inc &sta work0 &cmp 0 &bne ram_clear_work &seth rxfifo ram_clear_rxfifo: &setl work0 db 3eh,00h ; ld a,#0 &puthl &lda work0 &inc &sta work0 &cmp 0 &bne ram_clear_rxfifo ; <71055 PIA Initialize> &movi 10010000b,pia3 ; Port<2>=<1>=Out,<0>=In &movi 11111110b,pia1 &movi 0ffh,pia2 ; <71051 UART Initialize> &movi 0,uart1 &movi 0,uart1 &movi 0,uart1 &movi 01000000b,uart1 ; Software Reset &movi 01001110b,uart1 ; Mode Set &movi 00010101b,uart1 ; Command Set ; &movi 1,all_off_flag &movi 36,offset &movi 14,channel &movi 7fh,velocity ; db 0fbh ; &rts ;##### Waiting Timer ##### waiting: db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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