Buffers


Contents


Introduction

The interrupt system on a RISC OS computer makes extensive use of buffers. These act as temporary holding areas for data after you (or a device) generate it, and before a device (or you) consume it. For example, whenever you type a character on the keyboard, that character is stored in the keyboard input buffer by the keyboard interrupt handler, and it remains there until your program is ready to use it.


Overview

The buffer manager

The buffer manager is a global buffer managing system used by DeviceFS to provide buffers for the various devices that can be accessed. It provides a set of calls for setting up a buffer, inserting and removing data from a buffer, and removing a buffer. For more details about the buffer manager see the chapter entitled The Buffer Manager.

Filing system buffers

We are not concerned with filing system buffers in this section. However, these are areas where RISC OS holds whole areas of files in memory to increase the efficiency of file access. The use of file buffers is generally invisible to you; there is no direct way of accessing their contents.


Technical details

Use of buffers

The buffers we are looking at are known as first-in first-out, or FIFO, buffers. This is because the characters are removed from the buffer in the same order in which they were inserted. Many operations on buffers are implicit. For example, when you send a character to the printer or RS423 port, a character is inserted into a buffer. When you read from the keyboard or RS423 port using OS_ReadC, a character is removed from the buffer.

Additionally, there are several explicit buffer operations available. These include:

  • inserting a character into a buffer
  • removing a character
  • counting the space in a buffer
  • examining the next character without removing it
  • purging a buffer (clearing its contents).

All these operations are implemented as OS_Bytes - see below.

The buffer is also purged implicitly when the escape condition is cleared - see the chapter entitled Character Input.

Details of buffers

There are ten buffers, numbered 0 - 9. Their uses are as follows:

NumberUseSize
0Keyboard255
1RS423 (input)255
2RS423 (output)191
3Printer1023
From RISC OS 3 onwards, the size of the printer buffer is configurable using *Configure PrinterBufferSize.
4Sound channel 03
5Sound channel 13
6Sound channel 23
7Sound channel 33
8Speech3
9Mouse63

Buffers 2 to 8 are output buffers. They hold data you generate until a device is ready to consume it. The others are input buffers. These store bytes generated by the keyboard, RS423 and mouse respectively until you are ready to read them.

Buffers 4 to 8

Currently, buffers 4 to 8 are not used by RISC OS. They are provided for compatibility with BBC Micro software. Sound buffering and speech are implemented differently on RISC OS hardware than they were on BBC hardware. These buffers are not considered further.

Data format

The format of data in all buffers in current use, except for the mouse buffer, is byte-oriented ASCII data, although top-bit-set characters are treated specially in the keyboard buffer (and optionally in the serial input buffer). See the chapter entitled Character Input for a description of buffer values for the keyboard buffer. The mouse buffer contents refer to buffered button clicks. The format is as follows:

OffsetContents
+0Mouse x coordinate low
+1Mouse x coordinate high
+2Mouse y coordinate low
+3Mouse y coordinate high
+4Button state
+5Time of button change, byte 0
+6Time of button change, byte 1
+7Time of button change, byte 2
+8Time of button change, byte 3

The bytes are listed in the order in which they would be removed using OS_Byte 145 - see OS_Byte 145.

Usually OS_Mouse reads data from the mouse buffer. If none is available, it returns the current state instead. The mouse buffer is 63 bytes long, so 7 entries may be held at once.

OS_Byte calls provided

The OS_Bytes used to control buffers are described below.

They are, in fact, just an interface to the vectored buffer routines described on InsV onwards of the Software vectors. Usually, the OS_Bytes are easier to use. However, there are times when it is preferable, or necessary (for example to read the number of bytes free in an input buffer) to use the vectors. They can be called directly using OS_CallAVector.

Changing buffer sizes

To use different sized system buffers under RISC OS 2, you must provide handlers for all of InsV, RemV and CnpV. You could do so in a module that replaces, say, the printer buffer with a much larger one. You would claim the memory for this from the relocatable module area. The module could have its own configuration byte held in CMOS RAM to specify the size of the buffer, which it would claim on initialisation.

Under later versions of RISC OS you can alter the size of the printer buffer using *Configure PrinterBufferSize. To alter the size of other system buffers, rather than claiming InsV, RemV and CnpV, you must instead use the buffer manager SWIs Buffer_Create or Buffer_Register.


SWI calls


OS_Byte 15
(SWI &6)

Flush all buffers, or input buffer
On entry
R0=15
R1=reason code :
ValueMeaning
0Flush all buffers
1Flush current input buffer (keyboard/serial)
On exit
R0preserved
R1corrupted
R2corrupted
Interrupts
Interrupts are undefined
Fast interrupts are enabled
Processor mode
Processor is in SVC mode
Re-entrancy
Not defined
Use

This call flushes either all the buffers, or only the current input buffer determined by R0.

The contents of the buffer(s) are discarded. Individual buffers may be flushed using OS_Byte 21.

Related SWIs
OS_Byte 21
Related vectors
ByteV

OS_Byte 21
(SWI &6)

Flush a specified buffer
On entry
R0=21
R1=buffer number
On exit
R0preserved
R1preserved
R2corrupted
Interrupts
Interrupts are undefined
Fast interrupts are enabled
Processor mode
Processor is in SVC mode
Re-entrancy
Not defined
Use

This call flushes the specified buffer.

Related SWIs
OS_Byte 15
Related vectors
ByteV

OS_Byte 128
(SWI &6)

Gets mouse coordinates, or number of bytes in an input buffer, or number of free bytes in an output buffer
On entry
R0=128
R1=reason code :
ValueMeaning
7mouse x position
8mouse y position
246number of bytes in the mouse buffer
252number of bytes free in the printer buffer
253number of bytes free in the RS423 output buffer
254number of bytes in the RS423 input buffer
255number of bytes in the keyboard buffer
On exit
R0preserved
R1=bits 0 - 7 = low 8 bits of answer
R2=bits 0 - 23 = high 24 bits of answer
Interrupts
Interrupts are undefined
Fast interrupts are enabled
Processor mode
Processor is in SVC mode
Re-entrancy
Not defined
Use

The action of this call depends upon the reason code in R1. It returns either the current x or y position of the mouse, or the number of bytes in a particular input buffer, or how many bytes there are free in a particular output buffer.

Obviously we are more concerned with the calls where R1 >= 246 here. Note that R1 = (255 - buffer number) in these cases. If you want, you can also calculate this as { (- (buffer number + 1)) AND &FF }.

The calls to read the mouse position are considered obsolete, and are unreliable because they read the buffered position rather than the actual position. You should use OS_Mouse instead.

Related vectors
ByteV, CnpV

OS_Byte 138
(SWI &6)

Insert a byte into a buffer
On entry
R0=138
R1=buffer number
R2=byte to insert
On exit
R0preserved
R1preserved
R2preserved
Cset if the buffer was full
Cclear if the character was inserted
Interrupts
Interrupts are undefined
Fast interrupts are enabled
Processor mode
Processor is in SVC mode
Re-entrancy
Not defined
Use

This call inserts the byte specified in R2 into the buffer identified by R1. If C=1 on exit, the byte was not inserted as there was no room.

Inserting bytes into the mouse buffer isn't recommended, but if you must, you should be careful to insert all nine bytes with interrupts disabled, to prevent a real mouse transition from entering data into the middle of your data. You must do so as quickly as possible to reduce latency in the interrupt system.

Related SWIs
OS_Byte 145, OS_Byte 152, OS_Byte 153
Related vectors
ByteV, InsV

OS_Byte 145
(SWI &6)

Extract a byte from a buffer
On entry
R0=145
R1=buffer number
On exit
R0preserved
R1preserved
R2=byte extracted
Cset if the buffer was empty
Cclear if the character was read
Interrupts
Interrupts are undefined
Fast interrupts are enabled
Processor mode
Processor is in SVC mode
Re-entrancy
Not defined
Use

This call extracts the next byte from a specified buffer. If the buffer was empty then the C flag is set, and R2 will be invalid.

This call assumes the buffer number is correct; it does not generate an error if passed a bad buffer number, and its behaviour is undefined.

Related SWIs
OS_Byte 138, OS_Byte 152, OS_Byte 153
Related vectors
ByteV, RemV

OS_Byte 152
(SWI &6)

Examine next character in buffer without removal
On entry
R0=152
R1=buffer number
On exit
R0preserved
R1preserved
R2=next byte in buffer, or corrupted if buffer was empty
Cset if the buffer was empty
Cclear if the character was read
Interrupts
Interrupts are undefined
Fast interrupts are enabled
Processor mode
Processor is in SVC mode
Re-entrancy
Not defined
Use

This call returns the status of a specified buffer; the carry flag is set if the buffer is empty. If a byte is available, it is returned in R2 but is not removed from the buffer.

This call assumes the buffer number is correct; it does not generate an error if passed a bad buffer number, and its behaviour is undefined.

Related SWIs
OS_Byte 138, OS_Byte 145, OS_Byte 153
Related vectors
ByteV, RemV

OS_Byte 153
(SWI &6)

Insert a byte into an input buffer
On entry
R0=153
R1=buffer number :
ValueMeaning
0insert byte into the keyboard buffer
1insert byte into the RS423 input buffer
R2=byte to insert
On exit
R0preserved
R1corrupted
R2corrupted
Cset if the buffer was full
Cclear if the character was inserted
Interrupts
Interrupts are undefined
Fast interrupts are enabled
Processor mode
Processor is in SVC mode
Re-entrancy
Not defined
Use

This call enables bytes to be inserted into one of the two input buffers.

If the buffer was full and a byte could not be inserted, then the C flag is set on return.

If the current escape character (usually ASCII 27) is inserted, then appropriate action is taken; see the chapter entitled Character Input.

Related SWIs
OS_Byte 138, OS_Byte 145, OS_Byte 152
Related vectors
ByteV, InsV

Document information

Maintainer(s): RISCOS Ltd <developer@riscos.com>
History:
RevisionDateAuthorChanges
1ROLInitial version
Disclaimer:

Copyright © Pace Micro Technolgy plc, 2002.
Portions copyright © RISCOS Ltd, 2002.
Published by RISCOS Limited.
No part of this publication may be reproduced or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, or stored in any retrieval system of any nature, without the written permission of the copyright holder and the publisher, application for which shall be made to the publisher.