Playing With Electricity: Channels and Jobs

See all articles from ZX-Appeal Jun 88
Author(s): Harvey Taylor
Publication: ZX-Appeal
Pub Details: ZX-Appeal Jun 88
Date: June 1988
Pages: 13-15

-May 31/88

A chance remark in a conversation last month reminded me how utterly confusing I found Channels & Jobs when I first started pulling and pushing the guts of the QL. Well, in general, channels are how QDOS keeps track of which Job (=Multitasking Program) is using which system resource for how long. What does this mean in the particular?

When you first start to program the QL in SuperBasic, you are introduced to the construction “#n” — where n is a Superbasic channel number; eg. PRINT#0, ‘Hello’. In SuperBasic you can even get around this by using the default output channel #1. In machine language, everything is done with channel numbers – everything QDOS legal, that is. So what is a channel number?

If you take a look at the various System Variables which QDOS uses, you will come across one labelled SV_CHBAS which is described as ‘a pointer to base of channel table’. Now we have two questions; what is the channel table? There is a related SV_CHTOP which points to the top of the channel table.

When you have a question about a computer (system), you can look things up in a reference book, such as the QL Technical Reference Guide, or you can get the system to tell you itself. If you write a short program such as Listing 1, you will get a couple of addresses and a whole bunch of negative numbers.

100 REMark Listing 1
110 REMark Look at Channel Table
120 SV_CHBAS=163960
130 SV_CHTOP=163964
140 Base_of_chan_table = PEEK_L(SV_CHBAS)
150 Top_of_chan_table = PEEK_L(SV_CHTOP)-4
160 FOR ADDR = Base_of_chan_table TO Top_of_chan_table STEP 4
170 PRINT 'Addr =';ADDR,' contains ';PEEK_L(ADDR)
180 END FOR ADDR
190 STOP

The negative numbers indicate that the location is unused by QDOS. The addresses on the other hand are pointers to Channel Definition Blocks. Now we have three questions; what are Channel Definition Blocks? Let’s take a look.

100 REMark Listing 2
110 REMark Look at Channel Definition Blocks
120 SV_CHBAS=163960
130 SV_CHTOP=163964
140 Base_of_chan_table = PEEK_L(SV_CHBAS)
150 Top_of_chan_table = PEEK_L(SV_CHTOP)-4
160 FOR ADDR = Base_of_chan_table TO Top_of_chan_table STEP 4
170 CDB=PEEK_L(ADDR)
180 IF CDB < 0: GO TO 300
190 PRINT 'Addr =';ADDR,' contains ';CDB
200 PRINT 'Channel Definition Table @'; CDB
210 PRINT 'CDB Length =';PEEK_L(CDB)
220 PRINT 'Driver addr=';PEEK_L(CDB+4)
230 PRINT 'Owner Job =';PEEK_L(CDB+8)
240 PRINT 'ReleaseFlag=';PEEK_L(CDB+12)
250 PRINT 'Channel Tag=';PEEK_W(CDB+16)
260 PRINT 'Chan Status=';PEEK(CDB+18)
270 PRINT 'Stored Act =';PEEK(CDB+19)
280 PRINT 'Waiting Job=';PEEK_L(CDB+20)
290 PRINT '-----------'
300 END FOR ADDR
310 STOP

Imbedded in Listing_2 is the structure of the basic Channel Definition Block. Some channels such as Serial have added information tacked onto this basic structure. Devices with a Directory structure have other information as well.

We are nearing the heart of QDOS. Notice that one of the elements of the CDB is called Channel Tag. Everytime QDOS opens a channel, it assigns that channel a unique tag. This tag begins at 0 and counts up to 32K. The Tag is used as part of the Channel Number. Note that the Channel Number is also called the Channel ID. The other part of the Channel Number is an offset in the QDOS Channel Table.

ie Channel ID = Channel_Tag : Offset_in_Channel_Table
where both the Tag and the Offset are 16 bits, ie. WORDs.

Now we know what a Channel Number is to QDOS, but what of the “#n” construction used by Superbasic? This number is an Offset, as well, but in a table which Superbasic maintains. In general, the base of the entry for Superbasic Channel “#n” is at [(n*Entry_length) + Base_of_SuperBasic_chan_table]. Note there are two channel tables involved; the QDOS channel table & the Superbasic channel table. The length of each entry in the SB Table is $28 (=40d). The first element in the SuperBasic Channel entry is the QDOS Channel ID.

If you were writing a machine language extension to Superbasic & you wanted to be able to use Superbasic Channel numbers with your extension, how would you get a hold of the appropriate Channel ID?

*
START MOVEQ #1,D6 * DEFAULT CHANNEL
CMPA.L A3,A5 * ANY PARAMETERS?
BEQ.S GET_CHID * IF No: USE DEFAULT
MOVE.W CA_GTINT,A2 * USE A VECTOR UTILITY
JSR (A2) * TO GET THE NUMBER
BNE ERROR
MOVE.W 0(A6,A1.L),D6 * GET THE CHAN "#n" FROM STK
*
GET_CHID
MULU #$28,D0 * CALC OFFSET FROM CH_BASE
ADD.L BV_CHBAS(A6),D6 * D6= OFFSET FROM BV_START
CMP.L BV_CHTOP(A6),D6 * IS IT STILL IN THE TABLE?
BHI ERROR
MOVE.L 0(A6,D6.L),A0 * Get the CHAN_ID
*

There are a couple of things about this fragment I should explain. First off, this code will be executed by SuperBasic and so the Register A6 will point to the Base of that JOB. This allows all the SuperBasic addresses to be referenced relative to A6 and then QDOS can move the job around and all it has to do is change A6. Secondly, on entry to a machine language extension A5 points to the End of the parameters and A3 points to the Start. CA_GTINT is a QDOS utility which will fetch an integer parameter(s) from the Superbasic interpreter for you.

I want to try to give you an idea of how the system works. Let us say that you are opening a Superbasic channel with a command like “Open#3,scr_”.

You may have noticed that one of the elements of the Channel Definition Block was labelled Device Driver. Each device type (eg. SER, MDV, NET, PIPE) in the machine has a Device Driver which is assigned when the device is initialized & which QDOS keeps track of in a list in the system variables. Associated with each Device Driver, as well, is another definition block called a Physical Definition Block. The PDB and CDB are manipulated by the Device Driver only. There is one PDB per device & one CDB per channel. There is thus a hierarchy of Devices, PDB’s, Device Drivers & CDB’s which QDOS orchestrates for the Jobs.

When you issue your open command, it will be interpreted by SuperBasic & at some point, after the line is checked & all the parameters collected, a system call will be made using TRAP #2 – IO_OPEN with certain values in the registers. This Trap will invoke the Device Driver for each device until one of them is successful or until it reaches the end of the list. The Device Driver must contain several sections, one of which handles opening channels, another of which closes channels & another of which handles input & output. If you are dealing with a Directory Device, the driver needs to be able to do a Format, as well as take care of some of QDOS’s private concerns (freeing slave blocks). The end result of the IO_OPEN trap, if succesful, will be to return the Channel ID number to the calling program; in this case SuperBasic. This Channel ID is then used by all subsequent system calls which need to access that device. In this way the device and the program are uniquely linked by QDOS.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.