@nataliapc/mcp-openmsx
Version:
Model context protocol server for openMSX automation and control
1,069 lines (801 loc) • 120 kB
Markdown
# CHAPTER 5 - ACCESS TO PERIPHERALS THROUGH BIOS (Sections 1 to 6)
The basic philosophy of MSX is to have a standard interface, independent of machines or versions, to access peripherals through BIOS. Thus, the user should get to know about using BIOS first. In chapter 5, accessing peripherals using BIOS and the structure used for each peripheral are described.
## Index
- [1. PSG AND SOUND OUTPUT](#1-psg-and-sound-output)
- [1.1. PSG functions](#11-psg-functions)
- [1.2 Access to the PSG](#12-access-to-the-psg)
- [1.3 Tone Generation by 1-bit Sound Port](#13-tone-generation-by-1-bit-sound-port)
- [1.4 Access to 1-bit Sound Port](#14-access-to-1-bit-sound-port)
- [2. CASSETTE INTERFACE](#2-cassette-interface)
- [2.1 Baud Rate](#21-baud-rate)
- [2.2 One bit composition](#22-one-bit-composition)
- [2.3 One byte composition](#23-one-byte-composition)
- [2.4 Header Composition](#24-header-composition)
- [2.5 File Formats](#25-file-formats)
- [2.6 Access to cassette files](#26-access-to-cassette-files)
- [3. KEYBOARD INTERFACE](#3-keyboard-interface)
- [3.1 Key Scanning](#31-key-scanning)
- [3.2 Character Input](#32-character-input)
- [3.3 Function Keys](#33-function-keys)
- [3.4 STOP Key During Interrupts](#34-stop-key-during-interrupts)
- [4. PRINTER INTERFACE](#4-printer-interface)
- [4.1 Print Interface Overview](#41-print-interface-overview)
- [4.2 Output to the MSX Standard Printer](#42-output-to-the-msx-standard-printer)
- [4.3 Access to the printer](#43-access-to-the-printer)
- [5. UNIVERSAL I/O INTERFACE](#5-universal-io-interface)
- [5.1 Functions of the Ports](#51-functions-of-the-ports)
- [5.2 Joystick Use](#52-joystick-use)
- [5.3 Paddle Use](#53-paddle-use)
- [5.4 Use of Touch Panel, Light Pen, Mouse, and Track Ball](#54-use-of-touch-panel-light-pen-mouse-and-track-ball)
- [6. CLOCK AND BATTERY-POWERED MEMORY](#6-clock-and-battery-powered-memory)
- [6.1 CLOCK-IC Functions](#61-clock-ic-functions)
- [6.2 Structure of the CLOCK-IC](#62-structure-of-the-clock-ic)
- [6.3 MODE Register Functions](#63-mode-register-functions)
- [6.4 TEST Register functions](#64-test-register-functions)
- [6.5 RESET Register Functions](#65-reset-register-functions)
- [6.6 Setting the Clock and Alarm](#66-setting-the-clock-and-alarm)
- [6.7 Contents of the Battery-powered Memory](#67-contents-of-the-battery-powered-memory)
- [6.8 Access to the CLOCK-IC](#68-access-to-the-clock-ic)
- [Changes from the original](#changes-from-the-original)
<p> </p>
## 1. PSG AND SOUND OUTPUT
MSX has the following three kinds of sound output functions, but function (3) is not installed in the standard MSX, so it is not described in this manual. This section describes functions (1) and (2).
(1) PSG sound output (3 channels, 8 octaves)
(2) Sound output by 1 bit I/O port
(3) Sound output by MSX-AUDIO (FM sound generator) - not described in this manual
<p> </p>
### 1.1. PSG functions
An AY-3-8910 compatible LSI is used for the MSX music play function and for BEEP tone generation. This LSI is referred to as the PSG (Programmable Sound Generator), and can generate complex music and varios tones. It has the following features:
* There are three tone generators, each of which can independently specify 4096 scales (equivalent to 8 octaves) and 16 volume levels.
* It can generate piano and organ tones by using envelope patterns. Note that, since there is only one envelope generator, the tone of only one channel can be modified fundamentally.
* With the noise generator inside, tones such as the wind or waves can easily be generated. Note that since there is only one noise generator, only one channel can generate the noise.
* Any necessary frequency, such as the tone or the envelope, is obtained by dividing the input clock (in MSX, it is defined that fc = 1.7897725 MHz). So there is no unsteady pitch or rythm.
##### _Figure 5.1 PSG block diagram_
```
R0, R1 R7 R8
-------------------- ------------- ------------------------------
| Tone generator A | --> | | ------> | Volume control amplifier A |
-------------------- | | +--> ---------------------------+--
| | | Channel A output <--+
R2, R3 | Three | | R9
-------------------- | | | ------------------------------
| Tone generator B | --> | Channel | ---:--> | Volume control amplifier B |
-------------------- | | +--> ---------------------------+--
| Mixer | | Channel B output <--+
R4, R5 | | | R9
-------------------- | | | ------------------------------
| Tone generator C | --> | | ---:--> | Volume control amplifier C |
-------------------- ------------- +--> ---------------------------+--
^ | Channel C output <--+
| |
R6 | | R11, R12, R13
--------------------- ------------------------
| Noise generator | | Envelope generator |
--------------------- ------------------------
```
The PSG has two additional I/O (input/output) ports used for other than tone generating functions, which are omitted in the block diagram above. MSX uses them as general-purpose I/O ports to connect to I/O devices such as joystick, a touch pad, a paddle, or a mouse. These general-purpose I/O ports are described in section 5.
#### PSG registers
Since the PSG generates tones, the CPU simply notifies PSG when the tone is to be changed. This is done by writing values in 16 8-bit registers inside the PSG as shown in [Figure 5.2](#figure-52--psg-register-structure).
Roles and uses of these registers are described below.
#### Setting the tone frequency (R0 to R5)
Each tone frequency of channel A, B, and C is set by R0 to R5. The input clock frequency (fc = 1.7897725 MHz) is divided by 16 and the result is the standard frequency. Each channel divides the standard frequency by the 12-bit data assigned for each, and the objective pitch is obtained. The following relation exists between 12-bit data (TP) and the tone frequency to be generated (ft).
```
ft = fc/(16 * TP)
= 0.11186078125/TP [MHz]
= 111860.78125/TP [Hz]
```
A 12-bit data TP is specified for each channel by 4 high order bit coarse tune CT and 8 low order bit fine tune value FT, as shown in [Figure 5.3](#figure-53--setting-the-pitch). [Table 5.1](#table-51--setting-the-tone-frequency-scale-data) shows the register settings to make the scales.
##### _Figure 5.2 PSG register structure_
```
-----------------------------------------------------------------------------
| Bit | | | | | | | | |
| | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
| Register | | | | | | | | |
|---------------------------+-----------------------------------------------|
| R0 | Channel A note | 8 low order bits |
|----------| |-----------------------------------------------|
| R1 | Dividing rate | x x x x | 4 high order bits |
|----------+----------------+-----------------------------------------------|
| R2 | Channel B note | 8 low order bits |
|----------| |-----------------------------------------------|
| R3 | Dividing rate | x x x x | 4 high order bits |
|----------+----------------+-----------------------------------------------|
| R4 | Channel C note | 8 low order bits |
|----------| |-----------------------------------------------|
| R5 | Dividing rate | x x x x | 4 high order bits |
|----------+----------------+-----------------------------------------------|
| R6 | Noise div. rate| x x x | |
|----------+----------------+-----------------------------------------------|
| | | IN*/OUT | NOISE* | TONE* |
| R7 | Enable* |-----------+-----------------+-----------------|
| | | IOB | IOA | C | B | A | C | B | A |
|----------+----------------+-----------------+-----+-----------------------|
| R8 | Chan. A volume | x x x | M | |
|----------+----------------+-----------------+-----+-----------------------|
| R9 | Chan. B volume | x x x | M | |
|----------+----------------+-----------------+-----+-----------------------|
| R10 | Chan. C volume | x x x | M | |
|----------+----------------+-----------------------------------------------|
| R11 | | 8 low order bits |
|----------| Envelope Cycle |-----------------------------------------------|
| R12 | | 8 high order bits |
|----------+----------------+-----------------------------------------------|
| R13 | Env. wave shape| x x x x | |
|----------+----------------+-----------------------------------------------|
| R14 | I/O port A | |
|----------+----------------+-----------------------------------------------|
| R15 | I/O port B | |
-----------------------------------------------------------------------------
NOTE: x = unused bit
* = inverted signal
```
##### _Figure 5.3 Setting the pitch_
```
-------------------------------------------------
R0, R2, R4 | 8 bits | --+
------------------------------------------------- |
------------------------------------------------- |
R0, R2, R4 | x x x x | 4 bits | |
------------------------------------------------- |
| |
----------------------------------------+ |
| |
V V
-----------------------------------------------------------------------
| Coarse Tune (CT) | Fine Tune (FT) |
-----------------------------------------------------------------------
| |
+-------------------------------- TP ---------------------------------+
[ Channel A - R0, R1 ]
[ Channel B - R2, R3 ]
[ Channel C - R4, R5 ]
```
##### _Table 5.1 Setting the tone frequency (scale data)_
```
----------------------------------------------------------------
| Octave | | | | | | | | |
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| Note | | | | | | | | |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| C | D5D | 6AF | 357 | 1AC | D6 | 6B | 35 | 1B |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| C# | C9C | 64E | 327 | 194 | CA | 65 | 32 | 19 |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| D | BE7 | 5F4 | 2FA | 17D | BE | 5F | 30 | 18 |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| D# | B3C | 59E | 2CF | 168 | B4 | 5A | 2D | 16 |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| E | A9B | 54E | 2A7 | 153 | AA | 55 | 2A | 15 |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| F | A02 | 501 | 281 | 140 | A0 | 50 | 28 | 14 |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| F# | 973 | 4BA | 25D | 12E | 97 | 4C | 26 | 13 |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| G | 8EB | 476 | 23B | 11D | 8F | 47 | 24 | 12 |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| G# | 88B | 436 | 21B | 10D | 87 | 43 | 22 | 11 |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| A | 7F2 | 3F9 | 1FD | FE | 7F | 40 | 20 | 10 |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| A# | 780 | 3C0 | 1E0 | F0 | 78 | 3C | 1E | F |
|--------------+-----+-----+-----+-----+-----+-----+-----+-----|
| B | 714 | 38A | 1C5 | E3 | 71 | 39 | 1C | E |
----------------------------------------------------------------
```
#### Setting the noise frequency (R6)
The noise generator is used for synthesizing explosion sounds or wave sounds. The PSG can send the noise output by the noise generator to channels A to C. Since there is only one noise generator, the same noise is sent to all channels. By changing the average frequency, various noise effects can be obtained and this is done by R6 register settings. The 5 low order bit data (NP) of this register is divides into the standard frequency (fc/16) and this determines the average frequency of the noise (fn).
##### _Figure 5.4 Setting the noise frequency_
```
-------------------------------------------------
R6 | x x x | |
-------------------------------------------------
| |
+------------- NP ------------+
```
The following relation exists between NP and fn.
```
fn = fc/(16 * NP)
= 0.11186078125/NP [MHz]
= 111860.78125/NP [Hz]
```
Since the value of NP is from 1 to 31, the average frequency of the noise can be set from 3.6kHz to 111.9kHz.
#### Mixing the sound (R7)
R7 is used to select the output of the tone and noise generator, or a mixture of both. As shown in [Figure 5.5](#figure-55--output-selection-for-each-channel), the 3 low order bits (B0 to B2) of R7 control the tone output and the next 3 bits (B3 to B5) control the noise output. In both cases, when the corresponding bit is 0, the output is ON and, when 1, it is OFF.
##### _Figure 5.5 Output selection for each channel_
```
-------------------------------------------------
R7 | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
-------------------------------------------------
|
|
V
B7 B6 B5 B4 B3 B2 B1 B0
----------------- ------------------------- -------------------------
| Input enable* | | Noise enable* | | Tone enable* |
|---------------| |-----------------------| |-----------------------|
| B | A | | C | B | A | | C | B | A |
----------------- ------------------------- -------------------------
I/O port Noise output Tone output
Input - 0 ON - 0 ON - 0
Output - 1 OFF - 1 OFF - 1
```
The 2 high order bits of R7 do not affect sound output. These are used to determine the direction of the data of two I/O ports which PSG has. When the corresponding bit is 0, the input mode is selected and, when 0, the output mode is selected. In MSX, port A is used for the input and port B for the output, so it should always be set so that bit 6 = "0" and bit 7 = "1".
#### Setting the volume (R8 to R10)
R8 to R10 are used to specify the volume of each channel. Two ways can be selected by these registers: specifying the fixed volume by 4-bit data (0 to 15) and generating sound effects such as vibrato or fade-out by using the envelope.
##### _Figure 5.6 Setting the volume_
```
-------------------------------------------------
R8, R9, R10 | x x x | B4 | B3 | B2 | B1 | B0 |
-------------------------------------------------
| | |
| +---------- L ----------+
|
V
Use envelope:
No - 0 (set volume by the value of L)
Yes - 1 (ignore the value of L)
```
When bit 4 of these registers is "0", the envelope is not used and the 4 low order bit value L (0 to 15) of the registers specify the volume. When bit 4 is "1", the volume depends on the envelope signals and the value L is ignored.
#### Setting the envelope cycle (R11, R12)
R11 and R12 specify the envelope cycle in 16-bit data. The 8 high order bits are set in R12 and the 8 low order bits are set in R11.
##### _Figure 5.7 Setting the envelope cycle_
```
-------------------------------------------------
R11 | | --+
------------------------------------------------- |
------------------------------------------------- |
R12 | | |
------------------------------------------------- |
| |
---------------------------------+ |
| |
V V
-----------------------------------------------------------------------------
| Coarse Tune (CT) | Fine Tune (FT) |
-----------------------------------------------------------------------------
| |
+----------------------------------- EP ------------------------------------+
```
The following relation exists between the envelope cycle T and 16-bit data EP.
```
T = (256 * EP) / fc
= (256 * EP) / 1.787725 [MHz]
= 143.03493 * EP [micro second]
```
#### Setting the envelope pattern (R13)
R13 sets the envelope pattern by the 4 low order bit data as shown in [Figure 5.8](#figure-58--setting-the-wave-forms-of-the-envelopes). The intervals of T specified in the figure correspond to the envelope cycle specified by R11 and R12.
##### _Figure 5.8 Setting the wave forms of the envelopes_
```
-------------------------------------------------
R13 | x x x x | B3 | B2 | B1 | B0 |
-------------------------------------------------
|
------------------------------------+
|
V
---------------------------------------------------------
| | :\ |
| 0 0 x x | __: \______________________ |
| | |
| | /: |
| 0 1 x x | __/ :______________________ |
| | |
| | :\ :\ :\ :\ :\ :\ :\ |
| 1 0 0 0 | __: \: \: \: \: \: \:_ |
| | |
| | :\ |
| 1 0 0 1 | __: \______________________ |
| | |
| | :\ / \ / \ / \ |
| 1 0 1 0 | __: \ / \ / \ / |
| | _____________________ |
| | :\ : |
| 1 0 1 1 | __: \: |
| | |
| | /: /: /: /: /: /: |
| 1 1 0 0 | __/ :/ :/ :/ :/ :/ :/ |
| | ______________________ |
| | / |
| 1 1 0 1 | __/ |
| | |
| | / \ / \ / \ |
| 1 1 1 0 | __/ \ / \ / \ / |
| | |
| | /: |
| 1 1 1 1 | __/ :______________________ |
| | |
---------------------------------------------------------
| |
+---+
T
```
#### I/O port (R14, R15)
R14 and R15 are the ports to send and receive 8-bit data in parallel. MSX uses these as the general-purpose I/O interface. For more information, see section 5.
<p> </p>
### 1.2 Access to the PSG
For access the PSG from assembly language programs, several BIOS routines described below are available.
<p> </p>
#### GICINI (0090H/MAIN) - PSG initialization
* **Input**: ---
* **Output**: ---
* **Function**: initializes PSG registers and does the initial settings of the work area in which PLAY statement of BASIC is executed. Each register of PSG is set to the value as shown in [Figure 5.9](#figure-59--initial-values-of-psg-registers).
##### _Figure 5.9 Initial values of PSG registers_
```
-----------------------------------------------------------------------------
| Bit | | | | | | | | |
| | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| Register | | | | | | | | |
|---------------------------+-----------------------------------------------|
| R0 | Channel A | 0 1 0 1 0 1 0 1 |
|----------| |-----------------------------------------------|
| R1 | frequency | 0 0 0 0 0 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R2 | Channel B | 0 0 0 0 0 0 0 0 |
|----------| |-----------------------------------------------|
| R3 | frequency | 0 0 0 0 0 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R4 | Channel C | 0 0 0 0 0 0 0 0 |
|----------| |-----------------------------------------------|
| R5 | frequency | 0 0 0 0 0 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R6 | Noise frequency| 0 0 0 0 0 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R7 | Channel setting| 1 0 1 1 1 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R8 | Chan. A volume | 0 0 0 0 0 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R9 | Chan. B volume | 0 0 0 0 0 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R10 | Chan. C volume | 0 0 0 0 0 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R11 | | 0 0 0 0 1 0 1 1 |
|----------| Envelope Cycle |-----------------------------------------------|
| R12 | | 0 0 0 0 0 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R13 | Env. pattern | 0 0 0 0 0 0 0 0 |
|----------+----------------+-----------------------------------------------|
| R14 | I/O port A | |
|----------+----------------+-----------------------------------------------|
| R15 | I/O port B | |
-----------------------------------------------------------------------------
```
<p> </p>
#### WRTPSG (0093H/MAIN) - writing data in PSG registers
* **Input**:
* A ⟵ PSG register number
* E ⟵ data to be written
* **Output**: ---
* **Function**: writes the contents of the E register in the PSG register whose number is specified by the A register.
<p> </p>
#### RDPSG (0096H/MAIN) - reading PSG register data
* **Input**: A ⟵ PSG register number
* **Output**: A ⟵ contents of the specified register
* **Function**: reads the contents of PSG register whose number is specified by the A register and stores the value in the A register.
<p> </p>
#### STRTMS (0099H/MAIN) - starting the music
* **Input**: (QUEUE) ⟵ MML which is translated into the intermediate language
* **Output**: ---
* **Function**: examines whether the music is played as the background task, and plays the music which is set in the queue, if the music has not yet been played.
##### _List 5.1 Single tone generation_
```
;************************************************
;
; List 5.1 440 Hz tone
;
;************************************************
;
WRTPSG EQU 0093H
ORG 0B000H
;----- program start -----
LD A,7 ;Select Channel
LD E,00111110B ;Channel A Tone := On
CALL WRTPSG
LD A,8 ;Set Volume
LD E,10
CALL WRTPSG
LD A,0 ;Set Fine Tune Channel A
LD E,0FEH ;Data 0FEH
CALL WRTPSG
LD A,1 ;Set Coarse Tune Channel A
LD E,0 ;Data 0H
CALL WRTPSG
RET
END
```
<p> </p>
### 1.3 Tone Generation by 1-bit Sound Port
MSX has another sound generator in addition to the PSG. This is a simple one that generates sound by turning ON/OFF the 1-bit I/O port output repeatedly using software.
##### _Figure 5.10 1-bit sound port_
```
bit 7 6 5 4 3 2 1 0
-----------------------------------------
| . | | | | | | | |
--+--------------------------------------
| PPI port C (I/O address 0AAH)
|
|
:::::::::::: V
: PSG : ---------
: output :::::::>| MIX |
:::::::::::: ---------
|
V
-----
/ \ Speaker
---------
/ : \
```
<p> </p>
### 1.4 Access to 1-bit Sound Port
To access to the 1-bit sound port, the following BIOS routine is offered.
#### CHGSND (0135H/MAIN)
* **Input**: A ⟵ specification of ON/OFF (0 = OFF, others = ON)
* **Output**: ---
* **Function**: calling this routine with setting 0 in the A register turns the bit of the sound port OFF; calling it with another value turns it ON.
##### _List 5.2 Reading from cassette tape_
```
;********************************************************
;
; List 5.2 Read from cassette tape
;
; Set music tape into tape-recorder
; and run this program.
; Then your MSX will replay it.
;
;********************************************************
;
CHGSNG EQU 0135H
STMOTR EQU 00F3H
RDPSG EQU 0096H
BREAKX EQU 00B7H
ORG 0B000H
;----- program start ----- Note: Play tape using 1-bit sound port.
START: LD A,1 ;motor on
CALL STMOTR
LBL01: LD A,14 ;register 14
CALL RDPSG ;read PSG
AND 80H ;check CSAR
CALL CHGSNG ;change SOUND PORT
CALL BREAKX ;check Ctrl-STOP
JR NC,LBL01
XOR A ;stop cassette motor
CALL STMOTR
RET
END
```
<p> </p>
## 2. CASSETTE INTERFACE
Cassette tape recorders are the least expensive external storage devices available for the MSX. Knowledge of the cassette interface is required to treat information in cassette tapes within assembly language programs. This section offers the necessary information.
<p> </p>
### 2.1 Baud Rate
The following two baud rates can be used by the MSX cassette interface (see [Table 5.2](#table-52--msx-baud-rate)). When BASIC is invoked, 1200bps is set by default.
##### _Table 5.2 MSX baud rate_
```
------------------------------------------------
| Baud rate | Characteristics |
|-------------+--------------------------------|
| 1200 bps | Low speed / high reliability |
|-------------+--------------------------------|
| 2400 bps | High speed / low reliability |
------------------------------------------------
```
The baud rate is specified by the fourth parameter of the SCREEN instruction or the second parameter of the CSAVE instruction. Once the baud rate is set, it stays at that value.
```
SCREEN ,,,<baud rate>
CSAVE "filename",<baud rate>
(<baud rate> is 1 for 1200bps, 2 for 2400 bps)
```
<p> </p>
### 2.2 One bit composition
One bit data, the basis of I/O, is recorded as shown in [Figure 5.11](#figure-511--one-bit-composition). The pulse width is determined by counting the T-STATE of the CPU, so, while the cassette interface is active, any interrupt is inhibited.
The bit data from the cassette can be read through the seventh bit of port B of the general-purpose I/O interface (register 15 of the PSG). This function was used in the program example of [List 5.3](list-53--listing-names-of-files-saved-in-the-cassette), section 1 of chapter 5.
##### _Figure 5.11 One bit composition_
```
--------------------------------------------------------------
| Baud rate | Bit | Wave form |
|-----------+-------+----------------------------------------|
| | | : ----------- |
| | 0 | : | | (1200Hz x 1) |
| 1200 | | :_________| | |
| |-------+--:-------------------------------------|
| baud | | : ------ ------ |
| | 1 | : | | | | (2400Hz x 2) |
| | | :____| |____| | |
|-----------+-------+--:-------------------------------------|
| | | : ------ : |
| | 0 | : | | : (2400Hz x 1) |
| 2400 | | :____| | : |
| |-------+--:-------------------:-----------------|
| baud | | : --- --- : |
| | 1 | : | | | | : (4800Hz x 2) |
| | | :__| |__| | : |
-----------------------:--:-:----:---------:------------------
| : : : | 2963 T-states (833 micro-sec)
+--:-:----:---------+
| : : | 1491 T-states (417 micro-sec)
+--:-:----+
| : | 746 T-states (208 micro-sec)
+--:-+
| | 373 T-states (104 micro-sec)
+--+
```
<p> </p>
### 2.3 One byte composition
One byte data is recorded in the array of bits as shown in [Figure 5.12](#figure-512--one-byte-composition). There is one "0" bit as the start bit, followed by the 8-bit data body from LSB to MSX and by two "1" bit as the stop bits, so 11 bits are used.
##### _Figure 5.12 One byte composition_
```
LSB MSB
-------------------------------------------------------------------------
| 0 | X | X | X | X | X | X | X | X | 1 : 1 |
-------------------------------------------------------------------------
| | | |
+-----+-----------------------------------------------+-----------+
Start bit Data Stop bit
```
<p> </p>
### 2.4 Header Composition
The header is the portion where the signal of the specific frequency is recorded on the tape for a certain period. This allows the cassette tape speed to stabilize after it is started, or divides two files. There is a long header and a short header. The long header is used to wait until the motor is stabilized. The baud rate at reading the tape is determined by reading the long header. The short header is used to divide file bodies. [Table 5.3](#table-53--header-composition) shows the compositions of both.
##### _Table 5.3 Header composition_
```
------------------------------------------------------------------
| Baud rate | Header | Header composition |
|-------------+--------------+-----------------------------------|
| | Long header | 2400 Hz x 16000 (about 6.7 sec) |
| 1200 baud |--------------+-----------------------------------|
| | Short header | 2400 Hz x 4000 (about 1.7 sec) |
|-------------+--------------+-----------------------------------|
| | Long header | 4800 Hz x 32000 (about 6.7 sec) |
| 2400 baud |--------------+-----------------------------------|
| | Short header | 4800 Hz x 8000 (about 1.7 sec) |
------------------------------------------------------------------
```
<p> </p>
### 2.5 File Formats
MSX BASIC supports the following three kinds of cassette format files.
_(1) BASIC text file_
BASIC programs saved with the CSAVE command are recorded in this format. The file is divided into the preceding file header and the succeeding the body.
##### _Figure 5.13 Binary file format_
```
6.7 sec 10 bytes 6 bytes
-------------------------------------------------------------------------
| | | |
| Long header | 0D3H x 10 | File name |
| | | |
-------------------------------------------------------------------------
| |
+----------+ +------------------------------------+
| |
-----------------------------------------------------------------------------
| | File header | | File body | |
-----------------------------------------------------------------------------
| |
+-------------------------------+ +------+
| |
-------------------------------------\ \--------------------------------
| Short | / / | |
| header | BASIC program \ \ | 00H x 7 |
| | / / | |
-------------------------------------\ \--------------------------------
1.7 sec Any length 7 bytes
```
In the file header, ten bytes each of the value 0D3H follow after the long header and six bytes containing the file name are placed after them. In the file body, program body follows the short header and the end of the file is indicated by seven bytes of 00H.
_(2) ASCII text file_
BASIC programs saved in ASCII format by the SAVE command and data files created by the OPEN command are recorded in this format.
##### _Figure 5.14 ASCII file format_
```
6.7 sec 10 bytes 6 bytes
-------------------------------------------------------------------------
| | | |
| Long header | 0EAH x 10 | File name |
| | | |
-------------------------------------------------------------------------
| |
+----------+ +------------------------------------+
| |
-----------------------------------------------------------------------------
| | File header | | File body | |
-----------------------------------------------------------------------------
| |
+-------------------------------+ +------+
| |
-----------------------------------------------\ \----------------------
| | | | / / | Last |
| Block 1 | Block 2 | Block 3 | ..... \ \ .... | block |
| | | | / / | . |
-----------------------------------------------\ \------------+---------
| | |
+-----------+ +-----------+ CTRL+Z (EOF)
| | is included in data
------------------------------------------------
| Short | |
| header | Data | .....
| | |
------------------------------------------------
1.7 sec 256 bytes
```
_(3) Machine code file_
Machine code files saved by the BSAVE command are recorded in the following format. In the file header, 10 bytes each of the value 0D0H follow after the long header and 6 bytes containing the file name are placed after them.
In the file body, the starting address, the end address, and the entry address are recorded in order after the short header, and the machine codes follow after them. Since the amount of data can be calculated from the starting and ending addresses, there is no special mark for the end of the file. The entry address is the address where the program is executed when the R option of the BLOAD command is used.
##### _Figure 5.15 Machine code file format_
```
6.7 sec 10 bytes 6 bytes
-------------------------------------------------------------------------
| | | |
| Long header | 0D0H x 10 | File name |
| | | |
-------------------------------------------------------------------------
| |
+----------+ +------------------------------------+
| |
-----------------------------------------------------------------------------
| | File header | | File body | |
-----------------------------------------------------------------------------
| |
+-------------------------------+ +------+
| |
-------------------------------------------------------------------------
| Short | Top | End | Starting | |
| header | address | address | address | Program body |
| | | | | |
-------------------------------------------------------------------------
1.7 sec 2 bytes 2 bytes 2 bytes
```
<p> </p>
### 2.6 Access to cassette files
The following BIOS routines are offered to access cassette files.
<p> </p>
#### TAPION (00E1H/MAIN) - OPEN for read
* **Input**: ---
* **Output**: CY flag = ON at abnormal terminations
* **Function**: starts the motor of the tape recorder and reads the long header or the short headet. At the same time, the baud rate in which the file is recorded is detected and the work area is set according to it. Interrupts are inhibited.
<p> </p>
#### TAPIN (00E4H/MAIN) - read one byte
* **Input**: ---
* **Output**:
* A ⟵ data which has been read
* CY flag = ON at abnormal terminations
* **Function**: reads one byte of data from the tape and stores it in the A register.
<p> </p>
#### TAPIOF (00E7H/MAIN) - CLOSE for read
* **Input**: ---
* **Output**: ---
* **Function**: ends reading from the tape. At this point, interrupts are allowed.
<p> </p>
#### TAPOON (00EAH/MAIN) - OPEN for write
* **Input**: A ⟵ type of header (0 = short header, others = long header)
* **Output**: CY flag = ON at abnormal terminations
* **Function**: starts the motor of the tape recorder and writes the header of the type specified in the A register to the tape. Interrupts are inhibited.
<p> </p>
#### TAPOUT (00EDH/MAIN) - write one byte
* **Input**: A ⟵ data to be written
* **Output**: CY flag = ON at abnormal terminations
* **Function**: writes the contents of the A register to the tape.
<p> </p>
#### TAPOOF (00F0H/MAIN) - CLOSE writing
* **Input**: ---
* **Output**: ---
* **Function**: ends writing the tape. At this point, interrupts are allowed.
<p> </p>
#### STMOTR (00F3/MAIN) - specify the actions of the motor
* **Input**: A ⟵ action (0 = stop, 1 = start, 255 = reverse the current status)
* **Output**: ---
* **Function**: sets the status of the motor according to the value specified in the A register.
When READ/WRITE routines for the cassette files are created using these BIOS calls, only READ or WRITE, without any other action, should be done. For example, reading data from the tape and displaying it on the CRT might cause a READ error.
[List 5.3](#list-53--listing-names-of-files-saved-in-the-cassette) is a sample program which uses BIOS routines.
##### _List 5.3 Listing names of files saved in the cassette_
```
;************************************************************
;
; List 5.3 Cassette files
;
; Set cassette tape into recorder and run this program.
; Then all the names and attributes of the programs
; in that tape will be listed.
;
;************************************************************
;
CHPUT EQU 00A2H
TAPION EQU 00E1H
TAPIN EQU 00E4H
TAPIOF EQU 00E7H
ORG 0C000H
;----- program start ----- Note: View program names on cassette tape.
START: CALL TAPION ;motor on and read header
LD B,16
LD HL,WORK ;work area address
LBL01: PUSH HL
PUSH BC
CALL TAPIN ;read a byte of data from tape
POP BC
POP HL
JR C,ERROR ;set carry flag if read error
LD (HL),A
INC HL
DJNZ LBL01
LD HL,FILNAM ;write file name
CALL PUTSTR
LD HL,WORK+10
CALL PUTSTR
CALL CRLF
LD A,(WORK) ;check file attributes
LD HL,BINFIL
CP 0D3H ;check binary file
JR Z,LBL03
LD HL,ASCFIL
CP 0EAH ;check ascii file
JR Z,LBL03
LD HL,MACFIL
CP 0D0H ;check machine code file
JR Z,LBL03
ERROR: LD HL,ERRSTR
LBL03: CALL PUTSTR
CALL TAPIOF
RET
;----- put CRLF -----
CRLF: LD HL,STCRLF
CALL PUTSTR
RET
;----- put string -----
PUTSTR: LD A,(HL) ;get a character from strings
CP '$' ;check end of strings
RET Z
CALL CHPUT ;write a character to CRT
INC HL
JR PUTSTR
;----- strings data -----
FILNAM: DB 'FILE NAME :$'
ASCFIL: DB 'ASCII FILE',0DH,0AH,'$'
BINFIL: DB 'BINARY FILE',0DH,0Ah,'$'
MACFIL: DB 'BSAVE FILE',0DH,0AH,'$'
ERRSTR: DB 'TAPE READ ERROR',0DH,0AH,'$'
STCRLF: DB 0DH,0AH,'$'
;----- WORK AREA -----
WORK: DS 16,0
DB '$' ;end of strings
END
```
<p> </p>
## 3. KEYBOARD INTERFACE
Altough the MSX2 keyboard has the same design as that of the MSX1, it is more convenient to use because of the Romand-to-kana translation available for kana input. This section describes the keyboard interface of the MSX2.
Descriptions of the key aarangement are based on the Japanese keyboard standard; note that data is slightly different for the international MSX versions.
<p> </p>
### 3.1 Key Scanning
MSX uses the key matrices as shown in [Figure 5.16](#figure-516--msx-usa-version-key-matrix), [Figure 5.17](#figure-517--msx-international-version-key-matrix) and [Figure 5.17B](#figure-517b--msx-european-version-key-matrix). The key status can be obtained in real time by examining this key matrix and is available for reading input.
Scanning the key matrix is done by the following BIOS routine.
#### SNSMAT (0141H/MAIN) - reads the specified line of the key matrix
* **Input**: A ⟵ key matrix line to be read (0 to 10)
* **Output**: A ⟵ status of the specified line of the key matrix (when pressed, the bit of the key is 0)
* **Function**: specifies a line of the key matrix shown in [Figure 5.16](#figure-516--msx-usa-version-key-matrix), [Figure 5.17](#figure-517--msx-international-version-key-matrix) or [Figure 5.17B](#figure-517b--msx-european-version-key-matrix) and stores its status in the A register. The bit corresponding with the key being pressed is "0", and "1" for the key not being pressed.
##### _Figure 5.16 MSX USA version key matrix_
```
MSB LSB
7 6 5 4 3 2 1 0
-----------------------------------------------------------------
0 | B | L | | / | 1 | S | X | , |
|-------+-------+-------+-------+-------+-------+-------+-------|
1 | V | J | = | ` | Q | A | C | N |
|-------+-------+-------+-------+-------+-------+-------+-------|
2 | G | 8 | 0 | ] | W | F | Z | M |
|-------+-------+-------+-------+-------+-------+-------+-------|
3 | T | I | ~ | ; | 2 | D | U | \ |
|-------+-------+-------+-------+-------+-------+-------+-------|
4 | 6 | K | P | ' | 3 | R | 7 | H |
|-------+-------+-------+-------+-------+-------+-------+-------|
5 | 5 | 0 | 9 | [ | 4 | E | Y | . |
|-------+-------+-------+-------+-------+-------+-------+-------|
6 | F3 | F2 | F1 | CODE | CAPS | GRAPH | CTRL | SHIFT |
|-------+-------+-------+-------+-------+-------+-------+-------|
7 | RETURN| SELECT| BS | STOP | TAB | ESC | F5 | F4 |
|-------+-------+-------+-------+-------+-------+-------+-------|
8 | RIGHT | DOWN | UP | LEFT | DEL | INS | HOME | SPACE |
-----------------------------------------------------------------
[TEN KEY]
-----------------------------------------------------------------
9 | 4 | 3 | 2 | 1 | 0 | option| option| option|
|-------+-------+-------+-------+-------+-------+-------+-------|
10 | . | , | - | 9 | 8 | 7 | 6 | 5 |
-----------------------------------------------------------------
```
##### _Figure 5.17 MSX International version key matrix_
```
MSB