Problem for do work the INTERRUPT TIME for Sounds in assembly

By gasparrini

Champion (329)

gasparrini's picture

29-01-2014, 20:40

Hello Guys,

Problem for do work the INTERRUPT TIME for Sounds in assembly.
I would like do work these two routines in assembler at INTERRUPT TIME
and using HOOK FD9Fh. But unfortunately it doesn't work......

Therefore: I ask your help for me..... Thanks... ;-)

first routine

	.org $c000 - 7

	.db $fe
	.dw main,endProgram,main

main:
	jp installProgram ;Install HOOK
	jp stopProgram	;Uninstall HOOK
	
installProgram:
	di
	ld hl,$FD9F
	ld de,oldHook
	ld bc,5
	ldir
	
	ld a,$c3
	ld hl,startProgram
	ld de,$FD9F
	ld bc,5
	ldir
	ei
	ret
oldHook:	.ds 5 
stopProgram:
	di
	ld hl,oldHook
	ld de,$FD9F
	ld bc,5
	ldir
	ei
	ret		

startProgram:
    call    0090h
    ld      a,01h
    ld      e,00h
    call    0093h
    ld      a,08h
    ld      e,0bh
    call    0093h
    ld      bc,03e8h
LOOP1:
    ld      e,c
    ld      a,00h
    call    0093h
LOOP2:
    add     a,02h
    jr      nc,LOOP2
    dec     bc
    ld      a,b
    or      c
    jr      nz,LOOP1
    ld      a,08h
    ld      e,00h
    call    0093h
    ret 

endProgram:

.end

second routine

	.org $C000 - 7

	.db $fe
	.dw main,endProgram,main

main:
	jp installProgram ;Install HOOK
	jp stopProgram	;Uninstall HOOK
	
installProgram:
	di
	ld hl,$FD9F
	ld de,oldHook
	ld bc,5
	ldir
	
	ld a,$c3
	ld hl,startProgram
	ld de,$FD9F
	ld bc,5
	ldir
	ei
	ret
oldHook:	.ds 5 
stopProgram:
	di
	ld hl,oldHook
	ld de,$FD9F
	ld bc,5
	ldir
	ei
	ret		

startProgram:
   LD BC,$500
SOUND:
   LD A,7
   CALL $96
   OR 9
   LD E,A
   LD A,7
   CALL $93
   LD E,1
   LD A,1
   CALL $93
   LD E,$DE
   LD A,0
   CALL $93
   LD E,8
   LD A,13
   CALL  $93
   LD E,15
   LD A,11
   CALL $93
   LD E,0
   LD A,12
   CALL $93
   LD E,16
   LD A,8
   CALL $93
   LD A,7
   CALL $93
   AND $FE
   LD E,A
   LD A,7
   CALL $93
   DEC BC
   LD A,B
   OR C
   JR NZ,SOUND
   CALL $90
   RET
endProgram:

.end
Login or register to post comments

By anonymous

incognito ergo sum (116)

anonymous's picture

30-01-2014, 08:23

Your "installProgram" routine does not modify the $FD9F hook correctly.
First, you save the old hook content, that's OK.
But after the LDIR instruction, to install the new hook, you should do :

ld      hl,startProgram
ld      ($FD9F+1),hl
ld      a,$C3
ld      ($FD9F),a
ei
ret

By gasparrini

Champion (329)

gasparrini's picture

30-01-2014, 09:28

Hello Metalion,

I've done as you've said it, but all the same, yet the routine does not work!
Could you do the tests, and to please correct this routine?

org $c000 - 7

	.db $fe
	.dw main,endProgram,main

main:
	jp installProgram ;Install HOOK
	jp stopProgram	;Uninstall HOOK
	
installProgram:
	di
	ld hl,$FD9F
	ld de,oldHook
	ld bc,5
	ldir

      ld hl,startProgram
      ld ($FD9F+1),hl
      ld a,$C3
      ld ($FD9F),a
      ei
      ret

oldHook:	.ds 5 

stopProgram:
	di
	ld hl,oldHook
	ld de,$FD9F
	ld bc,5
	ldir
	ei
	ret		

startProgram:
    call    0090h
    ld      a,01h
    ld      e,00h
    call    0093h
    ld      a,08h
    ld      e,0bh
    call    0093h
    ld      bc,03e8h
LOOP1:
    ld      e,c
    ld      a,00h
    call    0093h
LOOP2:
    add     a,02h
    jr      nc,LOOP2
    dec     bc
    ld      a,b
    or      c
    jr      nz,LOOP1
    ld      a,08h
    ld      e,00h
    call    0093h
    ret 

endProgram:

.end

By WORP3

Paladin (864)

WORP3's picture

30-01-2014, 09:45

You are not using a startprogram hook, instead you are putting in a portion of your new irq handler on $FD9F which ins't working. A hook is a 5 byte program in which you can call or jump to your own irq routine.
You could use something like this which will jump to your new irq handler:

		LD		HL,startProgram	;Set new hook
		LD		A,0C3h			;JP instruction
		LD		(0FD9Fh),A
		LD		(0FDA0h),HL
		EI

Also do something between your main routine start and stop or the cpu won't have time to even call your irq handler.

By anonymous

incognito ergo sum (116)

anonymous's picture

30-01-2014, 11:42

The jump from the $FD9F hook is now correct.

So it means that the problem is in your "startProgram" routine. I'm no expert on PSG programming, but I see one potential problem : you have a loop calling 1016 ($3E8) times the WRTPSG routine. Depending on the length of the WRTPSG routine, your code could be too long for a vblank, which could mean that your routine is never fully executed, because it's always interrupted before the end (and provoking a stack overflow BTW).

If timing is not an issue (but I suppose it is, being a musical routine), you could try adding a DI at the start and a EI at the end of your routine, if only to check that the code is correct.