{"id":2388,"date":"2025-03-13T02:28:52","date_gmt":"2025-03-13T00:28:52","guid":{"rendered":"https:\/\/www.julien-nevo.com\/arkostracker\/?page_id=2388"},"modified":"2025-08-22T11:29:13","modified_gmt":"2025-08-22T09:29:13","slug":"using-interruption-player-on-zx-spectrum","status":"publish","type":"page","link":"https:\/\/www.julien-nevo.com\/arkostracker\/index.php\/using-interruption-player-on-zx-spectrum\/","title":{"rendered":"Using interruption player on ZX Spectrum"},"content":{"rendered":"\n<p>The following code will allow you to play a music under interruption, on ZX Spectrum (without using the firmware).<\/p>\n\n\n\n<p>This code was kindly provided by&nbsp;<strong>Gusman<\/strong>. A big thanks to him!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PrepareMusic:\n    ld hl, MUSIC_ADDRESS ;load your song address\n    xor a ;subsong 0\n    call PLY_AKG_INIT ;call initialization routine\n    ret\n\nPlayMusic:\n    call Interrupt_Setup ;This function is totally unnecessary, just for readability of the code\n    ret\n\nStopMusic:\n    di ;disable interrupts\n    call PLY_AKG_STOP ;stop the player\n\n    IM 1 ;revert interrupt mode to mode 1\n    ei ;enable interrupts\n\n    ret\n\nPlayer:\n    ;your player+music code\n    #include \"music.z80asm\"\n\nJP_ADDRESS EQU #FDFD ;JP address\n\n;IM 2 mode must be used on the spectrum to have a custom interrupt handler.\n;The spectrum architecture makes impossible to know what will be on the bus when an interrupt is executed\n;so the handler must be in an address where it's high and low bytes are equal.\n;To be able to store your interrupt handler anywhere three bytes at an address\n;with that characteristic (FDFD in this case) are reserved and written with \"jp Interrupt_Handler\"\nInterrupt_Handler:\n    \n    push af ;store all registers\n    push bc\n    push de\n    push hl\n    push ix\n    push iy\n    exx\n    ex af, af'\n    push af\n    push bc\n    push de\n    push hl\n   \n    ;play music\n    call PLY_AKG_PLAY\n    pop hl\n   \n    ;restore all registers\n    pop de\n    pop bc\n    pop af\n    ex af, af'\n    exx\n    pop iy\n    pop ix\n    pop hl\n    pop de\n    pop bc\n    pop af\n    \n    ;reenable interrupts\n    ei\n    ;return from interrupt handler\n    ret\n\n.align 256\nInterrupt_Table:\n    ;interrupt table must be aligned at page boundary\n    ;with 256 + 1 bytes, all with the same value\n    .defs 257\n\nInterrupt_Setup:\n    di ;Disable interrupts\n    ld de, Interrupt_Table ;load interrupt table address\n    ld hl, JP_ADDRESS ;load \"JP\" address\n    ld a, d\n    ld i, a ;load I with the interrupt table high byte\n    ld a, l ;load a with lower byte of JP address (indifferent to use H or L, both must be equal)\n\nInterrupt_Table_Loop:\n    ld (de), a ;fill the table\n    inc e\n    jr nz, Interrupt_Table_Loop\n    inc d ;write the 257th byte of the table\n    ld (de), a\n    ld (hl), #C3 ;write JP\n    inc l\n    ld (hl), low(Interrupt_Handler)\n  \n    ;write interrupt handler address\n    inc l\n    ld (hl), high(Interrupt_Handler)\n  \n    im 2 ;set interrupt mode to 2\n    ei ;enable interrupts\n\n    ret<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following code will allow you to play a music under interruption, on ZX Spectrum (without using the firmware). This code was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":209,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_vp_format_video_url":"","_vp_image_focal_point":[],"footnotes":""},"class_list":["post-2388","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.julien-nevo.com\/arkostracker\/index.php\/wp-json\/wp\/v2\/pages\/2388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.julien-nevo.com\/arkostracker\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.julien-nevo.com\/arkostracker\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.julien-nevo.com\/arkostracker\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.julien-nevo.com\/arkostracker\/index.php\/wp-json\/wp\/v2\/comments?post=2388"}],"version-history":[{"count":3,"href":"https:\/\/www.julien-nevo.com\/arkostracker\/index.php\/wp-json\/wp\/v2\/pages\/2388\/revisions"}],"predecessor-version":[{"id":2391,"href":"https:\/\/www.julien-nevo.com\/arkostracker\/index.php\/wp-json\/wp\/v2\/pages\/2388\/revisions\/2391"}],"wp:attachment":[{"href":"https:\/\/www.julien-nevo.com\/arkostracker\/index.php\/wp-json\/wp\/v2\/media?parent=2388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}