-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
th-5a.txt
2807 lines (2162 loc) · 116 KB
/
th-5a.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
MSX2 TECHNICAL HANDBOOK
-----------------------
Edited by: ASCII Systems Division
Published by: ASCII Coprporation - JAPAN
First edition: March 1987
Text file typed by: Nestor Soriano (Konami Man) - SPAIN
October 1997
Changes from the original:
- In Figure 5.2, unused bits are marked as "x", and inverted signals are
marked with "*", for easiest readability.
- Figure 5.17B was added.
- In List 5.4, the last line before the work area, "JR START", has been
corrected to "JR SCAN".
- In Figure 5.18, the addresses for GETPNT y PUTPNT were swapped. They have
been corrected.
- In description of BIOS routines PINLIN and INLIN, "BUF" address has been
corrected from F55DH to F55EH.
- In Figure 5.22 (B), "Arabaic mode display" has been changed to "Arabic or
kana mode display".
- In description of BIOS routine GTTRIG, the input needed for reading B
buttons has been added in the "Input" field.
- In Table 5.5, in the Note 4, "the trigger button of the mouse or the
trigger button" has been changed to "the trigger button of the mouse or the
trigger button of the track ball".
- In Figure 5.29, "1200 or 2400 hours" indication has been corrected to "12
or 24 hours".
- In Figure 5.32, "Register 3 #11" indication has been corrected to "Register
#11".
- In Figure 5.33, "Adjust Y (8 to +7)" has been corrected to "Adjust Y (-8 to
+7)".
- In description of BIOS routine WRTCLK, the input needed in the A register
has been added in the "Input" field.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
CHAPTER 5 - ACCESS TO PERIPHERALS THROUGH BIOS (Parts 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.
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
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.
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. Table
5.1 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, 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. 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.
1.2 Access to the PSG
For access the PSG from assembly language programs, several BIOS routines
described below are available.
* 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 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 | |
-----------------------------------------------------------------------------
* 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.
* 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.
* 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
=================================================================
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
---------
/ : \
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
=================================================================
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.
2.1 Baud Rate
The following two baud rates can be used by the MSX cassette interface (see
Table 5.2). 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)
2.2 One bit composition
One bit data, the basis of I/O, is recorded as shown in Figure 5.11. 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, 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)
+--+
2.3 One byte composition
One byte data is recorded in the array of bits as shown in Figure 5.12. 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
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 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) |
------------------------------------------------------------------
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
2.6 Access to cassette files
The following BIOS routines are offered to access cassette files.
* 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.
* 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.
* TAPIOF (00E7H/MAIN) ................. CLOSE for read
Input: ---
Output: ---
Function: ends reading from the tape. At this point, interrupts are
allowed.
* 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.
* 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.
* TAPOOF (00F0H/MAIN) ................. CLOSE writing
Input: ---
Output: ---
Function: ends writing the tape. At this point, interrupts are allowed.
* 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 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