Code: Select all
#pragma redirect fputc_cons=_getch
Code: Select all
#pragma redirect fputc_cons=_getch
Code: Select all
#pragma redirect fputc_cons=_putch
//console direct i/o
unsigned char keyread, enablectrlcexit=1;
unsigned char c_rawio_get() __smallc __naked
{
__asm
ld c,6 //bdos function 6
ld e,0xff //bdos parameter return character without echoing
call 5 //call bdos
ld l,a //copy result a into l for return
ret
__endasm;
}
void c_rawio_put(char b) __naked __z88dk_fastcall
{
__asm
ld c,6 //bdos function 6
ld e,l //bdos output from l
call 5 //call bdos
ret
__endasm;
}
unsigned char kbhit()
{
unsigned char c1;
//if we've previously read a key then return 1
if (keyread)
return 1;
//let's get a key
c1=c_rawio_get();
//if it is zero then return 0, no key is ready
if (c1==0)
return 0;
//if not we do have a key, cache it in keyread and return 1
keyread=c1;
return 1;
}
unsigned char getch()
{
unsigned char c1;
//if we have a cached key, return it
if (keyread)
{
c1=keyread;
keyread=0;
return c1;
}
//we must wait on a key and return it
do
{
c1=c_rawio_get();
} while (c1==0);
return c1;
}
int putch(int c)
{
if (c<0xfc)
{
//pause for ctrl-s
kbhit();
if (keyread==19)
{
//clear the ctrl-s
keyread=0;
//pause and wait for a key, if it is ctrl-c leave it in the buffer
if (getch()==3)
goto quit;
}
else
if (keyread==3)
{
quit:
if (enablectrlcexit)
{
c_rawio_put('^');
c_rawio_put('C');
exit(1);
}
}
if (c=='\n')
c_rawio_put('\r');
c_rawio_put(c);
return c;
}
else return EOF;
}