Jump to content
  • 0

What is the kernel call to check if there is a character?


BruceMcF
 Share

Question

A lot of Forth's that are made to be portable have a simple model of console I/O that is oriented around three calls: KEY? KEY and EMIT. KEY? is the routine that returns either the currently available  character and TRUE or else NUL and FALSE, KEY is a loop on KEY? that keeps running until there is a key, and EMIT outputs a character to the screen.

In the C64, for both KEY? and KEY, the subroutine threaded DurexForth looks at $C6 to see whether there are any characters in the keyboard buffer:

+BACKLINK
    !byte    4
    !text    "key?"
    lda $c6 ; Number of characters in keyboard buffer
    beq +
.pushtrue
    lda #$ff
+   tay
    jmp pushya

    +BACKLINK
    !byte    3
    !text    "key"
-   lda $c6
    beq -
    stx W
    jsr $e5b4 ; Get character from keyboard buffer
    ldx W
    ldy #0
    jmp pushya

Since the internal addresses for such things are not documented and not necessarily stable for the CX16, what is the Kernel call to find out whether or not the Keyboard buffer is empty?

Or should SCNKEY be recycled for the kind of keyboard information that is now opaque ... like a bit vector of shift keys in A and the number of characters in the keyboard buffer in Y.

 

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0
1 hour ago, togster510 said:

GETIN ($FFE4) returns zero if there are no characters in the keyboard buffer, I think.

But, it returns the actual character if there is something in the buffer.

Therefore, portable code must call GETIN, save the character, then return TRUE.  It first must check to see if there already is a saved character, and return TRUE immediately if there is something saved.  The KEY routine must call KEY? until that one returns TRUE, then return (and clear) that saved character.

Link to comment
Share on other sites

  • 0
12 hours ago, Greg King said:

But, it returns the actual character if there is something in the buffer.

Therefore, portable code must call GETIN, save the character, then return TRUE.  It first must check to see if there already is a saved character, and return TRUE immediately if there is something saved.  The KEY routine must call KEY? until that one returns TRUE, then return (and clear) that saved character.

So (TOS in T, Y free):

KEYQ: LDA T+1 : PHA : LDA T : PHA : LDY #$FF
    LDA LASTIN : BNE + : JSR GETIN : CMP #0 : STA LASTIN : BNE + : INY
+ PHY : PHY : !next

KEY: LDA T+1 : PHA : LDA T : PHA : LDA LASTIN : BNE + : - JSR GETIN : CMP #0 : BEQ -

+ LDY #0 : STY LASTIN : PHY : PHA : !next

Edited by BruceMcF
GETIN doesn't necessarily set Z flag
Link to comment
Share on other sites

  • 0

There's another caveat:  GETIN is a normal file I/O function.  Therefore, portable code would need to call CLRCHN before using GETIN.  But, that makes it difficult to use KEY? in a file read/write loop.  That's why the C64 version of that Forth uses "hidden knowledge" about the ROM.

In prerelease version 37, the count of characters is stored in RAM bank zero at $A00A.  But, it's been at many other locations in previous releases!

Link to comment
Share on other sites

  • 0

I'm nowhere near FILE operations at this stage ... if I get any of the ports up to using BLOCKS out of the High RAM I'll be more than happy.

However, if I can redirect the interpreter to a file, then having an ENDFILE word, such as \\ that would perform the CLRCHN and return interpretation to the keyboard, that might be handy before getting a full set of FILE words worked up.

Edited by BruceMcF
Link to comment
Share on other sites

  • 0
11 hours ago, Greg King said:

JSR GETIN isn't guaranteed to set the processor's Z status flag.  Therefore, your code must test the accumulator (e.g., TAX) before branching.

Yes, of course. I've got CMP #0 added. And CLRCHN as IO! which initializes I/O in the eForth 1.0 model.

In eForth v1.0, the low level character input word is ?RX, which is ( 0 | c TRUE ), so it works with GETIN without needing a "LASTIN" stash for the last character ... the higher level KEY? does that stashing.

Edited by BruceMcF
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

Please review our Terms of Use