Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;trash80 - catch midiout for SIO
  2. ;
  3. ; Description:
  4. ; A byte is sent from the link port by storing it in _io_out and calling send_byte_slaved.
  5. ;
  6. ; send_byte_slaved checks to see if serial is idle using SLAVED_STATUS=0.
  7. ; if it is idle it sets the SLAVED_STATUS to 1, turns on the serial interrupt, sets
  8. ; the serial clock to external, puts the byte into the serial data register,
  9. ; and sets the serial register to send.
  10. ;
  11. ; If serial is not idle, it increments the serial buffer write position MIDI_WRITE_POS
  12. ; and puts the byte into the MIDI_BUFFER array.
  13. ;
  14. ; catch_midiout is called by the serial interrupt. It checks if the Midi buffer
  15. ; MIDI_READ_POS and MIDI_WRITE_POS positions match. If they Match, it sets the
  16. ; SLAVED_STATUS to 0 and turns off the serial interrupt.
  17. ;
  18. ; If they don't match it increments MIDI_READ_POS, gets the new byte from the MIDI_BUFFER
  19. ; array, sets the serial clock to external, puts the byte into the serial data register,
  20. ; and sets the serial register to send.
  21. ;
  22. ;
  23. ; Important:
  24. ; The serial interrupt must be turned off when idle so that the constant external clock
  25. ; does not trigger the interrupt with a incomming byte of 0.
  26. ;
  27. ; Since the external source does not know when a byte is comming in, it uses the first
  28. ; bit as a catch to know data is comming. So we are limited to the last 7 bits using this
  29. ; method, and the MSB must always be set. (0x80)
  30. ;
  31.  
  32. _catch_midiout::
  33.     ld  a,(_MIDI_WRITE_POS)          ; Buffer - Compare read and write buffer positions
  34.     ld  b,a                          ;
  35.     ld  a,(_MIDI_READ_POS)           ;
  36.     cp  a,b                          ;
  37.     jp z,.midiout_set_idle           ; Nothing to do, Return
  38.    
  39.     inc a                            ; Buffer - Increment read position
  40.     and #0x3f                        ; Buffer - mod 64
  41.     ld  (_MIDI_READ_POS),a           ; Buffer - Store read position
  42.     ld  b,a                          ; ... & save for later
  43.  
  44.     xor a                            
  45.     ldh (0x02),a                     ; Serial IO - Set to external clock
  46.    
  47.     ld  a,#<_MIDI_BUFFER             ; Get data in buffer
  48.     add b                            ;
  49.     ld  e,a                          ;
  50.     ld  a,#>_MIDI_BUFFER             ;
  51.     ld  d,a                          ;
  52.     ld  a,(de)                       ; ... load buffer data into a
  53.    
  54.     or #0x80                         ; Bit 7 needs to be set for edge detect
  55.    
  56.     ldh (0x01),a                     ; Serial IO - Load data into serial
  57.     ld  a,#0x80                      ; Serial IO - Bit 7 into ff02
  58.     ldh (0x02),a                     ; Serial IO - starts the transfer
  59.     ret
  60.  
  61. .midiout_set_idle::
  62.     xor a
  63.     ld  (_SLAVED_STATUS),a           ; SIO Status - Store to 0
  64.     ldh (0x0f),a                     ; Interrupts - FF0F Clear pending interrupts
  65.     ld  a,#0x07                      ; Interrupts - Turn off SIO
  66.     ldh (0xFF),a                     ; Interrupts - FFFF
  67.     ret
  68.  
  69.  
  70. ;trash80 - Send SIO data with external clock & buffer
  71. _send_byte_slaved::
  72.     di                               ; Interrupts - DI
  73.    
  74.     ld  a,(_SLAVED_STATUS)           ; Read SIO Status
  75.     bit 0,a                          ;
  76.     jr  z, .sb_send_byte             ; If status is idle, send incomming byte
  77.    
  78.     ld  a,(_MIDI_WRITE_POS)          ; Buffer - ld Write position
  79.     inc a                            ; Buffer - Increment Write position
  80.     and #0x3f                        ; Buffer - Mod 64
  81.     ld  (_MIDI_WRITE_POS),a          ; Buffer - Store Write position
  82.    
  83.     ld  b,#<_MIDI_BUFFER             ; Buffer - get array pointer
  84.     add a,b                          ;
  85.     ld  e,a                          ;
  86.     ld  a,#>_MIDI_BUFFER             ;
  87.     ld  d,a                          ;
  88.     ld  a,(__io_out)                 ; .... and load into a
  89.    
  90.     ;ld a,(_TEST_SIO)                 ; Profile Test - incrementing from 0 to 7f
  91.     ;inc a                            ; Profile Test  
  92.     ;ld (_TEST_SIO),a                 ; Profile Test
  93.    
  94.     ld  (de),a                       ; Buffer - Store incomming byte to buffer
  95.     reti   
  96.  
  97. .sb_send_byte::
  98.     ld  a,#0x01                      ; SIO Status - Set to 1
  99.     ld  (_SLAVED_STATUS),a           ; SIO Status - Store status
  100.    
  101.     xor a                            ; Interrupts - burp...
  102.     ldh (0x0f),a                     ; Interrupts - Clear pending interrupts
  103.     ld  a,#0x0c                      ; Interrupts - Enable SIO
  104.     ldh (0xff),a                     ; Interrupts - FFFF
  105.    
  106.     xor a                            ; Serial IO -
  107.     ldh (0x02),a                     ; Serial IO - Use external clock
  108.     ld  a,(__io_out)                 ; Serial IO - Get Data
  109.    
  110.     ;ld a,(_TEST_SIO)                 ; Profile Test - incrementing from 0 to your mom
  111.     ;inc a                            ; Profile Test
  112.     ;ld (_TEST_SIO),a                 ; Profile Test
  113.  
  114.     or #0x80                         ; Serial IO - Set bit 7 on data
  115.     ldh (0x01),a                     ; Serial IO - Set serial data register
  116.     ld  a,#0x80                      ; Serial IO - Bit 7 to send
  117.     ldh (0x02),a                     ; Serial IO - Set SIO send bit
  118.    
  119.     reti
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement