Passing a variable onto a code on a different page

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
jordi
Member
Posts: 61
Joined: Sun Oct 28, 2018 3:35 pm

Passing a variable onto a code on a different page

Post by jordi »

My code has

Code: Select all

 uint8_t current_siren;
Which starts as current_siren = 1

The algorithm is: When current_siren=1, then fxSirena1 is chosen. When current_siren=2, then fxSirena2 is chosen, and so on.

I created this func:

Code: Select all

void sonido2Sirena() {
__asm
    extern playSirena
    extern Load_Fx
    extern _current_siren
    extern enable_bank_6
    extern restore_bank_0
    ;ld a, 6
    ld d, _current_siren
    call enable_bank_6
      call playSirena
    call restore_bank_0
__endasm;
}
As you see, I try to set on registry d the value of current_siren (which could be 1,2,3,4 or 5).

Enable bank is a funcion that alvin created for me in misifu (thankyou!) long ago:

Code: Select all

enable_bank_6:
    ld a, 6
enable_bank_n:

   ; return address

   pop hl

   ; move stack pointer

   ld (temp_sp),sp
   ld sp,0

   ; enable bank

   and 0x07
   or 0x10

   ld bc,0x7ffd
   out (c),a

   ; return

   jp (hl)

As you might imagine, playSirena is on a different bank. It's on bank6, and it looks like:

Code: Select all

playSirena:
      ld a, d          ; recover current_siren (1,2,3,4,5) value
      cp 5
      jr c, lessThan5
      ld hl, fxSirena5
      ld a, 2 ;; border red 
      out (254), a ;; border set
      jr endIfStatement
      lessThan5:
        cp 4
        jr c, lessThan4
        ld hl, fxSirena4
        jr endIfStatement
        lessThan4:
          cp 3
          jr c, lessThan3
          ld hl, fxSirena3
          jr endIfStatement
          lessThan3:
            cp 2
            jr c, lessThan2
            ld hl, fxSirena2
            jp endIfStatement
            lessThan2:
              ld a, 1 ; border blue
              out (254), a ; border set
              ld hl, fxSirena1
      endIfStatement:
      ld a, 2
      call FxStop
	  call Load_Fx
	  ret
I know I am changing 'a' value with the border change, but border is set directly to Red no matter I do. So it's setting fxSirena 5, always.

Is there a better way to share a variable value between my C code and ASM? I did this if else wrongly?
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: Passing a variable onto a code on a different page

Post by dom »

d is taking the low byte of the address of the current_siren variable NOT the value of that variable.
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: Passing a variable onto a code on a different page

Post by jorgegv »

Also, isn't your enable_bank_n function corrupting SP? I see that it stores the return address in HL for later jumping to it (avoiding a RET) but it's also saving SP to a safe place, and then setting it to 0, but I don't see it restored before the JP (HL)...

Unless the saved SP is restored in the calling function (which would be weird), at the moment the calling function executes a RET, it will try to get the return address from address 0, and then... crash?
Post Reply