Trying to write a function that stores I/O port's value in a shadow variable...
This didn't work:
byte reg[256];
inline void reg_write(word num, byte val)
{
if (num < sizeof(reg) / sizeof(reg[0])) reg[num] = val;
__sfr __banked __at (num) = val; // ERROR!!!
}
This does work, but requires a function call:
byte reg[256];
void reg_write(word num, byte val) __sdcccall(1)
{
__asm
ioi
ld (hl), a
; TODO if hl < 256
ex de, hl
ld hl, #_reg
add hl, de
ld (hl), a
__endasm;
}
I'd like to make this function inline to eliminate call/ret, so I've made it inline. However, at the call site eg:
void main()
{
reg_write(42, 69);
}
I do not see 42 and 69 being loaded into hl and a:
71 _main::
; NOTHING loaded into hl and a!!!
73 ioi
74 ld (hl), a
75 ex de, hl
76 ld hl, #_reg
77 add hl, de
78 ld (hl), a
Does anyone have any ideas how I can accomplish that?
Using some ugly macromancy, I came up with this:
So, now I can do things like:
If anyone can think of a better solution, please let me know.
Last edit: D-mo 2023-09-19