5

The routine at 0xffd2 which is called chrout will print a character to some output stream. I would like to use this call to print a message to the screen, but I have a file open for output on a disk. According to the documentation, chrout will send my character there.

How can I direct chrout to the screen? Must I close the file first?

3 Answers 3

6

Must I close the file first?

No, not unless you already have all 10 possible file entries used up, It depends more on your intention to write a 'clean' program or just want to hack your way ahead (*1).

How can I direct chrout to the screen?

The compatible way is by setting the device the following output is meant to go to. And setting it back afterwards. It's eventually exactly what the BASIC command CMD does when redirecting output.

The function called is CHKOUT (at $FFC9) with the logical file number to be supplied in X (3 in case of the screen).


*1 - Even though this was intended as a rhetorical question, there is always a chance to go brutal ... err ... use an inflexible low level approach. Here it would be using screen functions directly. Here outputting a char (in A) is $E716.

5
  • 1
    When calling CHKOUT the device number for the screen is 3.
    – JeremyP
    Commented Apr 4, 2019 at 8:36
  • @JeremyP Added. (I considered that obvious when already digging that deep)
    – Raffzahn
    Commented Apr 4, 2019 at 11:15
  • 1
    @JeremyP: Aren't the file number and the device number two different things?
    – TeaRex
    Commented Mar 30, 2020 at 13:35
  • The value sent in the X register to CHKOUT is the channel number (aka logical file number), not the device number. You also have to actually OPEN the channel first; there is no channel already open to the screen or keyboard ("stdin" and "stdout" are anonymous). For example, you can't PRINT#3 or CMD3 from BASIC without explicitly OPENing channel 3 first. So calling CHKOUT with X=3 doesn't do what you think it's doing, The correct way to reset input and output to their default devices is to call CLRCHN ($FFCC).
    – Mark Reed
    Commented Oct 11 at 3:37
  • (In fact, if you use SETMSG to turn on KERNAL-level error messages, you'll see that your call to CHKOUT with X=3 results in an "I/O Error #3", which is "File not open". But any I/O error results in CLRCHN being called, which is why it "works"!)
    – Mark Reed
    Commented Oct 11 at 4:19
3

I haven't tried this myself, but I noted that Commodore Peripheral Bus: Part 2: Bus Arbitration, TALK/LISTEN, under KERNAL Channel I/O API, says that device 3 maps to the screen.

Therefore, it should be possible to use CHROUT with a target device of 3 to print to the screen.

I would not imagine that closing the file is necessary as long as you target the correct device, though you may need to mind system state if you are targetting different devices at the same time. Closing and reopening the file may be the cleaner approach.

3

If you have activated a logical file as the current output channel (using CHKOUT or $FFC9), you reset both active input and output channels to default using CLRCHN a.k.a. CLRCH, $FFCC.

After that, BSOUT $FFD2 will print to the screen.

This will not have closed the file, it is still open. You can re-activate it again with CHKOUT and eventually close it with CLOSE, $FFC3.

There is no need to use any of the bus-related functions such as TALK or LISTEN: they are the wrong abstraction level here. They come in handy if you want to talk to a disk drive or printer but not use the file administration for it.

Another answer confuses the device number for the screen (which is 3) with logical file numbers. If you open a file, you basically create a mapping from a LFN (logical file number) to a device number (and possibly a secondary address, but those are too far out of the scope of printing to the screen).

There are various books available about machine language programming on the 64, such as "(Compute's) Programming the Commodore 64, The definitive guide" by Raeto Collin West. I have "Programming the PET/CBM" by the same author and it is very thorough.

Here on archive.org are some more.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .