Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;trash80 - catch midiout for SIO
- ;
- ; Description:
- ; A byte is sent from the link port by storing it in _io_out and calling send_byte_slaved.
- ;
- ; send_byte_slaved checks to see if serial is idle using SLAVED_STATUS=0.
- ; if it is idle it sets the SLAVED_STATUS to 1, turns on the serial interrupt, sets
- ; the serial clock to external, puts the byte into the serial data register,
- ; and sets the serial register to send.
- ;
- ; If serial is not idle, it increments the serial buffer write position MIDI_WRITE_POS
- ; and puts the byte into the MIDI_BUFFER array.
- ;
- ; catch_midiout is called by the serial interrupt. It checks if the Midi buffer
- ; MIDI_READ_POS and MIDI_WRITE_POS positions match. If they Match, it sets the
- ; SLAVED_STATUS to 0 and turns off the serial interrupt.
- ;
- ; If they don't match it increments MIDI_READ_POS, gets the new byte from the MIDI_BUFFER
- ; array, sets the serial clock to external, puts the byte into the serial data register,
- ; and sets the serial register to send.
- ;
- ;
- ; Important:
- ; The serial interrupt must be turned off when idle so that the constant external clock
- ; does not trigger the interrupt with a incomming byte of 0.
- ;
- ; Since the external source does not know when a byte is comming in, it uses the first
- ; bit as a catch to know data is comming. So we are limited to the last 7 bits using this
- ; method, and the MSB must always be set. (0x80)
- ;
- _catch_midiout::
- ld a,(_MIDI_WRITE_POS) ; Buffer - Compare read and write buffer positions
- ld b,a ;
- ld a,(_MIDI_READ_POS) ;
- cp a,b ;
- jr z,.midiout_set_idle ; Nothing to do, Return
- inc a ; Buffer - Increment read position
- and #0x3f ; Buffer - mod 64
- ld (_MIDI_READ_POS),a ; Buffer - Store read position
- ld b,a ; ... & save for later
- xor a
- ldh (0x02),a ; Serial IO - Set to external clock
- ld a,#<_MIDI_BUFFER ; Get data in buffer
- add b ;
- ld e,a ;
- ld a,#>_MIDI_BUFFER ;
- ld d,a ;
- ld a,(de) ; ... load buffer data into a
- or #0x80 ; Bit 7 needs to be set for edge detect
- ldh (0x01),a ; Serial IO - Load data into serial
- ld a,#0x80 ; Serial IO - Bit 7 into ff02
- ldh (0x02),a ; Serial IO - starts the transfer
- ret
- .midiout_set_idle::
- xor a
- ld (_SLAVED_STATUS),a ; SIO Status - Store to 0
- ldh (0x0f),a ; Interrupts - FF0F Clear pending interrupts
- ld a,#0x07 ; Interrupts - Turn off SIO
- ldh (0xFF),a ; Interrupts - FFFF
- ret
- ;trash80 - Send SIO data with external clock & buffer
- _send_byte_slaved::
- di ; Interrupts - DI
- ld a,(_SLAVED_STATUS) ; Read SIO Status
- bit 0,a ;
- jr z, .sb_send_byte ; If status is idle, send incomming byte
- ld a,(_MIDI_WRITE_POS) ; Buffer - ld Write position
- inc a ; Buffer - Increment Write position
- and #0x3f ; Buffer - Mod 64
- ld (_MIDI_WRITE_POS),a ; Buffer - Store Write position
- ld b,#<_MIDI_BUFFER ; Buffer - get array pointer
- add a,b ;
- ld e,a ;
- ld a,#>_MIDI_BUFFER ;
- ld d,a ;
- ld a,(__io_out) ; .... and load into a
- ;ld a,(_TEST_SIO) ; Profile Test - incrementing from 0 to 7f
- ;inc a ; Profile Test
- ;ld (_TEST_SIO),a ; Profile Test
- ld (de),a ; Buffer - Store incomming byte to buffer
- reti
- .sb_send_byte::
- ld a,#0x01 ; SIO Status - Set to 1
- ld (_SLAVED_STATUS),a ; SIO Status - Store status
- xor a ; Interrupts - burp...
- ldh (0x0f),a ; Interrupts - Clear pending interrupts
- ld a,#0x0c ; Interrupts - Enable SIO
- ldh (0xff),a ; Interrupts - FFFF
- xor a ; Serial IO -
- ldh (0x02),a ; Serial IO - Use external clock
- ld a,(__io_out) ; Serial IO - Get Data
- ;ld a,(_TEST_SIO) ; Profile Test - incrementing from 0 to your mom
- ;inc a ; Profile Test
- ;ld (_TEST_SIO),a ; Profile Test
- or #0x80 ; Serial IO - Set bit 7 on data
- ldh (0x01),a ; Serial IO - Set serial data register
- ld a,#0x80 ; Serial IO - Bit 7 to send
- ldh (0x02),a ; Serial IO - Set SIO send bit
- reti
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement