Page 2 of 2

Re: CTRL-C processing

Posted: Tue Feb 22, 2022 1:26 pm
by dom
Argh, that's a really bad typo by me

Code: Select all

#pragma redirect fputc_cons=_getch

Re: CTRL-C processing

Posted: Tue Feb 22, 2022 2:41 pm
by alank2
No worries, I got it now:

Final code that uses the direct i/o functions only which solves the issue with cp/m 2.2:

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;
}

Re: CTRL-C processing

Posted: Thu Feb 24, 2022 11:22 am
by nuc1e0n
Is it worth adding new raw console I/O variants of fputc_cons and fgetc_cons to the standard cpm target libraries for folks to use then? IIRC the getk function in the libs is using raw io and the other ones are using the 'cooked'(?) console bdos calls. Reading this thread it seems mixing them can cause problems on CP/M 2.2

Re: CTRL-C processing

Posted: Thu Feb 24, 2022 12:44 pm
by stefano
I originally decided to keep the BDOS approach where possible because it sounded more 'universal', e.g. working also on the crap emulators.
Obviously it is perfectly fine to replace it with something more consistent.

Re: CTRL-C processing

Posted: Thu Feb 24, 2022 6:08 pm
by nuc1e0n
It wouldn't need to be replaced of course. Both the current version and any new raw io version could exist side by side in the libraries, similar to how they do on the zx spectrum target currently (https://github.com/z88dk/z88dk/wiki/Pla ... rom-driver).

The only problem with the cp/m console functions I can see in the libraries currently is that getk is the only one using raw i/o, and that was changed from a different implementation in 2019.