Call "in a,(0x88)" then read "a" register from a C program

Post Reply
sblendorio
Member
Posts: 20
Joined: Tue Jan 19, 2021 9:03 am

Call "in a,(0x88)" then read "a" register from a C program

Post by sblendorio »

Hi,

I need to call from a C program, this ASM instruction

Code: Select all

in a,(0x88)
...then put the content of A in a variable. How should I do it?

Thanks in advance
User avatar
dom
Well known member
Posts: 1506
Joined: Sun Jul 15, 2007 10:01 pm

Re: Call "in a,(0x88)" then read "a" register from a C program

Post by dom »

There's several ways, however the most portable is:

Code: Select all

__sfr __at (0x88) PORT;

void func() {

        int l = PORT;
}
sblendorio
Member
Posts: 20
Joined: Tue Jan 19, 2021 9:03 am

Re: Call "in a,(0x88)" then read "a" register from a C program

Post by sblendorio »

Thank you, it works perfectly.

Is there a way to parametrize the port number at runtime?
User avatar
dom
Well known member
Posts: 1506
Joined: Sun Jul 15, 2007 10:01 pm

Re: Call "in a,(0x88)" then read "a" register from a C program

Post by dom »

Not using that method - the port is a const.

Given that the 8080 only has in a,(const) your only option is to if {} else or self modifying code I guess.
User avatar
dom
Well known member
Posts: 1506
Joined: Sun Jul 15, 2007 10:01 pm

Re: Call "in a,(0x88)" then read "a" register from a C program

Post by dom »

dom wrote: Mon Jan 25, 2021 6:47 pm Not using that method - the port is a const.

Given that the 8080 only has in a,(const) your only option is to if {} else or self modifying code I guess.
So for SMC, you could do something like this:

Code: Select all

int readport(int port) __z88dk_fastcall
{
#asm
    ld a,l
    ld (portsmc+1),a
portsmc:
    in a,(port)
    ld l,a
    ld h,0
#endasm
}
sblendorio
Member
Posts: 20
Joined: Tue Jan 19, 2021 9:03 am

Re: Call "in a,(0x88)" then read "a" register from a C program

Post by sblendorio »

In order to reduce the instructions executed, how to make something like this?

Code: Select all

{
    set_port(0x88);
    ...
    ...
    int value = read_port()
}
sblendorio
Member
Posts: 20
Joined: Tue Jan 19, 2021 9:03 am

Re: Call "in a,(0x88)" then read "a" register from a C program

Post by sblendorio »

Did it.

Code: Select all

void set_port(int port) __z88dk_fastcall {
#asm
    ld a,l
    ld (portsmc+1),a
#endasm
}

int read_port() __z88dk_fastcall {
#asm
portsmc:
    in a,(0x00)
    ld l,a
    ld h,0
#endasm
}
stefano
Well known member
Posts: 1695
Joined: Mon Jul 16, 2007 7:39 pm

Re: Call "in a,(0x88)" then read "a" register from a C program

Post by stefano »

Nice and clean :)

At your convenience, you could have both in a single function, or rename void set_port() to int in_port, and add 'jp portsmc' at its end.
Post Reply