COMAL 80 For The Commodore 64
COMAL 80 For The Commodore 64
COMAL
for Commodore 64
English Edition
o Commodore Data A/S, Horsens , Denmark
Prepared by: Frank Bason & Leo H0j sholt -Poulsen
Consultants : UniComal ApS , Jels , Denmark
Editor: Jan Nymand , Commodore Data A/S
Cover and Illustrations: Fejltrok , Silkeborg
Layout : Silkeborg Soldata, Denmark
Printing: Tekst & tryk , Hors ens, Denmark
A Danish Edition , copyright 1985
o Commodore Data A/S, Horsens, Denmark
is published simultaneously_
COMAL
for Commodore 64
RESPONSIBILITY
Neither Commodore Data A/S or any of this company's dealers
or distributors give any guarantee expressed or implied
concerning the COMAL computer language as described in this
manual and tutorial. The language and documentation are
sold "as is" with no claim being made as to its quality or
suitability for specific tasks. The risk concerning the
quality and performance of this product rests with the
buyer. Should this product prove defective after purchase,
it is the buyer (and neither the producer UniComal A/S,
Commodore Data A/S, nor any distributor or dealer) who must
take full responsibility for service, repair and any other
costs accrued due to errors or defects. This is true even
if the producer of the program has been given prior notice
of the possible existence of such errors or defects.
COPYRIGHT
The computer language COMAL for the Commodore 64 is covered
by the following copyright:
o Commodore Data A/S and UniComal ApS 1985
This manual is covered by copyright 0Commodore Data A/S,
Denmark, 1985. No part of the system, the program cartridge
or this manual may be reproduced, stored in a data retrieval
system or in any way be transmitted electronically or
mechanically, photocopied or be duplicated in any other way
without the prior written permission of the owner of the
copyright. Copying the COMAL cartridge is forbidden;
however the Demonstration diskette or tape may be copied
freely.
ASSISTANCE
If you have any comments concering this COMAL manual or the
language itself, please pass them along to your
dealer. Co~odore Data A/S has made every effort to assure
that the contents of this manual are correct and complete
and than the programming language itself functions as it
should.
Every error discovered by users will be corrected
as soon as possible.
Your help in this connection will be
sincerely appreciated, not least by other COMAL users.
programm~ng
-5-
TABLE
OF
CONTENTS
INTRODUCTION
What is COMAL?
- The origins of COMAL
COMAL and Commodore
- Using this Tutorial
Chapt.r 11 SETTING UP YOUR COMPUTER
- Your Computer and Accessories
- Installing your COMAL Cartridge
Connecting the TV or Monitor
- The Commodore Keyboard
- Running the Demonstration Program
Using the Datassette Unit
Using the Disk Drive
- Preparing a Storage Diskette
- Review
Chapt.r 21 LET'S BET STARTED!
Why learn to program?
Direct Execution of COMAL Orders
A Quick Look at Turtle Graphics
What is a program?
Repeating Instructions
COMAL Procedures
Saving Programs and Procedures
Using the Datassette
Using the Disk Drive
- Review
9
9
10
10
11
15
15
17
19
20
21
23
42
25
25
27
29
32
35
37
40
42
45
45
46
49
50
52
55
59
61
63
67
70
73
74
76
80
82
85
88
CONTENTS
-6-
CONTENTS
93
93
94
96
97
98
100
101
102
102
102
104
105
105
106
107
109
110
112
112
113
114
116
116
117
119
119
120
121
122
122
122
123
124
125
127
128
129
131
132
- 7-
CONTENTS
LOG - EXP
CHR$ - STR$ - SPC$
ORD - VAL - LEN
TRUE - FALSE
TIME
RANDOMIZE - RND
ESC - TRAP ESC
Operators:
DIV - MOD
- Logical operators:
NOT - AND - AND THEN - OR - OR ELSE
IN
BITAND - BITOR - BITXOR
Other orders:
II
TRACE
DIM
PEEK
POKE
SYS
NULL
STOP - END
Chapt.r
~I
COMAL PACKAGES
133
133
134
135
135
136
136
137
138
140
140
141
142
142
143
144
144
144
147
What is a package?
The English Package
The Danish Package
Graphics with COMAL
Graphics Overview
In Depth Look at Graphics Orders
Sprites
The sprite is enlarged.
- More Sprites
- Two sprites collide.
- Saving a Drawing to Disk
- Sprites Used with Other Graphics
- Sprite Cartoons
- A Multi-colored Sprite
- Sprite Overview
- Special Rules for Multi-colored Sprites
Sound and Music
- Sound Orders in Depth
- Packages for Using the Control Ports
paddles
joysticks
lightpen
overview of the light pen package
- The System Package
- The Font Package
example of a character replacement
replacing an entire character set
font package orders in depth
147
148
148
148
150
152
165
167
167
167
168
168
169
171
173
175
184
191
197
197
199
200
204
206
213
21.4
219
CONTENTS
- What is a file?
- Saving Programs and Procedures
Sequential Files - an Address List
- Random files - an Inventory Program
- Moving a Sequential File
- File Types
215
216
219
220
222
232
235
236
CONTENTS
-8-
CONTENT8
237
238
238
238
241
Introduction
The RS-232C Interface
IEEE Cartridges
The Parallel Port
File Transfer Between Computers
The Control Ports
Review
241
241
245
245
250
251
254
255
255
257
257
257
258
260
261
264
266
266
267
268
269
275
279
281
283
287
291
299
301
315
-9-
I:NTRODUCTI:ON
WHAT IS COI'1AL?
Welcome to the world of CONAL programming!
Manv feel that
COMAL is close to being an ideal programming language for
microcomputers, incorporating as it does the best features
of both BaSic, Logo and Pascal.
You are about to learn a
programming language which offers, among other things, the
following features:
REPEAT - UNTIL
WHILE - DO - ENDWHILE
flexible conditionals lIke
IF - THEN
CASE - OF
ELIF
WHEN
ELSE - ENDIF
OTHERWISE - ENDCASE
INTRODUCTION
-10-
INTRODUCTION
-11-
INTRODUCTION
-12-
INTRODUCTION
-13-
difficult to master.
-14-
Ch~pt.r
-15-
1 -
CHAPTER
SETTZNG
UP
YOUR
SETTING UP
COMPUTER
*
*
*
*
*
*
-16-
Chapter 1 -
CAME
POWER
POWER
SETTING UP
CARTRIDGE CHANNEL
T'/
AUOIOIVIOEO SERIAL CASSETU USER
SLOT
SELECTOR CONNECTOR CONNECTOR PORT INTERFACE PORT
-17-
SETTING UP
,~
.."" ...
:.:
~
'<!" <>
'"
".,.,
............
....
l' ~
.......
,
,
.,.
"'''''''''''
....
__
"
"'"
""'0-
~_"'''''''''-W
Figure 1.3: An ideal setup for learning and using the CONAL
programming language includes a Commodore 64
computer equipped with a printer, 1541 disk
drive and a color TV or monitor.
Don"t turn anything on yet.
We will have to install the
COMAL cartridge before continuing!
-18-
SETTING UP
-19-
Chapt.,. 1 -
SETTING UP
~: ; ;,
L
no:[]
\0-
I-,il\
POWER SUPPLY
The output jacks for these signals are shown in Figure 1.1.
Because the SX-64 has its own color monitor. the following
discussion will only apply to the Commodore 64.
If you will
be using the SX-64, you can proceed directly to the next
section on running the demonstratlon program.
A color monitor is the most desirable choice of display for
your Commodore 64, because it will give you the sharpest
image.
If you have a Commodore monltor, just attach one end
of the connector cable supplied with the monltor to the 8pole connector on the rear panel of the Commodore 64.
Plug
the connectors at the other end of the cable into the three
phono jacks on the rear panel of the monitor.
If you will
be using a different type of monitor, your dealer will be
able to assist you to find the proper cable.
A TV connector cable is supplied with your Commodore 64 for
those users who will be using a color (or B/W) television
set for their display.
If you will be using a television
set, insert the phono plug end of the cable into the phono
jack on the rear panel of your Commodore 64, and plug the
other end into the antenna input jack on your television
receiver.
You must also connect the Commodore transformer to your
computer.
The cable from the power supply is inserted on
the right hand side of your computer (towards the rear,
right next to the power switch).
When all connections have been properly made and all
-20-
Chapter 1 -
SETTING UP
p~esent
$$$
-21-
SETTING UP
<A)
<5>
(0"
"c "
<RETURN>
-22-
Chapt.r 1 -
SETTING UP
Di
II.
I
WRITE
PROTECT
NOTCH
=
=
c:::J
<
-23-
SETTING UP
load "d.moprogram"
and press <RETURN).
This order will transfer a copy
of the program from the diskette to your computer's
memory.
The activity indicator on the drive should
light up, and the drive will operate for a few seconds.
Whichever means you have used to load the demonstration
program, you can now type run and press <RETURN>.
Then
sit back, relax and enjoy the show!
Be sure your TV or
monitor sound volume is turned up slightly so music and
sound effects can be heard.
Chapter 1 -
-24-
SETTING UP
REVIEW
Your equipment should now be set up and
ready to use. You have mounted the
COMAL cartridge, powered up, and
familiarized yourself with the keyboard.
You have also read in a demonstration
program and run it to check out your
system.
The program has given you a previe"J of the impressive
potential of this programming language. Finally, if you
will be using a disk drive, you have formatted a diskette
which can be used for storage of programs as you work
through the tutorial chapters which follow.
-25-
CHAPTER
LET'S
GET
STARTED
2~43
+ 9320
-26-
GETTING STARTED
*
*
*
*
*
*
*
*
*
-27-
GETTING STARTED
*
*
*
*
*
*
-28-
GETTING STARTED
217+30~
-29-
GETTING STARTED
,217+30~
-~-
Chaptar 2 -
GETTING STARTED
r1ght(90)
<RETURN)
<RETURN>
pan down
for"ard(~O)
~
,.,
-TI-
Chapter 2 -
GETTING STARTED
forward (50)
setheadlng(45)
forward (70)
What does each order do? Do some exoerimentlng yourself to
understand how to move the turtle and make It draw.
You
might want to try the following sequence:
for side-l to 4 do
farward(~O)lleft(90)
for
<RETURN>
for -
<RETURN>
for i -
<RETURN>
e~rar
hideturtle
shawturtle
If you have a color display you can also experlment with
background number
pencalarnumber
where <number> is a color code.
See Appendlx B for a
list of the graphics color codes.
The table which follows shows turtle graphiCS orders WIth a
short form for each and a brief description.
When you glve
the order use turtle from the keyboard or in a program,
all these orders as well as all the commands In the
graphiCS package become available for you to use.
-32-
Chapt.,. 2 -
TURTLE
ORDER
SHORT
FORM
back (U
forward(L)
background (C)
clearscreen
home
hideturtle
showturtle
pencolor(C)
pend own
penup
left (0)
right(O)
setheading(H)
bk(U
fd(U
bg(C)
cs
GETTING STARTED
DESCRIPTION
*
*
*
*
*
*
~
~
-33-
Chapt.,. 2 -
GETTING STARTED
"modules"
or "bullding
blocks"
WhlCh must
First be sure you are using the text screen (press <f1>
if you have bean using graphics).
Be sure that no other
COMAL program is in memory (type n.w <RETURN.
You will
probably want to clear the screen and move the cursor to the
top left side of the screen.
Press the <SHIFT> key and
the <CLR/HOME> key at the same time to do this.
-~-
GETTING STARTED
0010
u turtl.
-35-
Chapter 2 0020
0030
0040
GETTING STARTED
splitscreen
forward(7~)
right (90)
OO~O
forward(7~)
0060
0070
0080
0090
0100
0110
right(90)
forward(7~)
right(90)
forward(7~)
right(90)
while k.y.-~hr$(O) do null
II program. SQUARE
II by. <your name>
use turtle
splitscr n
for sides.-l to 4 do
Ch.pt.r 2 0060
0070
OOSO
0090
0100
-36-
GETTING STARTED
II progr.ml SQUARE
II bYI <your n.m.>
USE turtl.
splitscr n
FOR sid.s.- 1 TO 4 DO
forw.rd(7:5)
right(90)
ENDFOR .id
WHILE KEV$-CHR$(O) DO NULL
END II of progr.m
~~!~~~~~~~;:~I~:~t~:~~~~~~~~~~:~~~~:~~:~~i-~~
lower case.
Let CO MAL do the extra work of
prov1d1ng a nice listing for you!
-\!
-,
-37-
GETTING STARTED
I I proQram. polYQon
I I by.
<your nama)
PAGE I I claar tha screan
-~-
GETTING STARTED
,~
Add this procedure to your program, and replace line 120 by:
Chapter- 2 -
-39-
GETTIN6 STARTED
polygon.
The polygon procedure m1ght be typed 1n
1200
1210
1220
1230
1240
1230
1260
11~e
this:
s~ould
appear
I I pr-ogr-am: polygon
II by.
(your name>
PAGE
USE turtle
.plit.cr- n
INPUT "How many sides? "I number
INPUT "Length of each? u: length
I I MAIN PROGRAM
polygon (numb.r,length)
wait'k.y
END II of MAIN PR06RAM
PRoC wait'key
WHILE KEV$~CHR$(O) DO NULL
ENDPROC wait'k.y
PROC polygon(number-,length)
FOR .id I-l to numberfor-war-d <length)
r-ight(360/numb.r)
ENDFOR .ide.
ENDPROC polygon
-40-
GETTING STARTED
.av.
"cs.polygon" <RETURN)
Chapter 2 -
-41-
GETTING STARTED
& play
on Tape WIll
snould appear.
-42-
GETTING STARTED
REVIEW
In th1S chapter you have been presented w1th information to
nelp you:
*
*
*
*
*
*
.~
*
*
*
-43...
...
...
...
GETTING STARTED
-44-
-45-
PROGRAMMXNG
WXTH
COMAL
4It
Chapter 3 -
-46 -
PROGRAMMING
A FIRST CALCULATION
The first example illustrates how the computer handles
numbers:
Program 11
ne...
auto 100
Chapt.,. 3 -
-47 -
PROGRAMMING
0110
0120
0130
0140
01:50
0160
0170
numb.,.a.-7
numb.,.b.-l~
av.,.a; -Cnumb.,.a+numb.,.bl/2
PRINT "Th. av.,.a;. of the numb.,.s"
PRINT numb.,.a,"and",numb.,.b
PRINT "is";av.,.a;.
END
Chaptllr :5 -
-48 -
PROBRAHI1INB
printed first.
Then
valua of numbarb.
between the numbers
printed. but it is
the different parts of
- 49-
PROGRAMMING
which is printed.
In line 170 the program is terminated by the statement END.
50
105 PAGE
What effect does this order
4.
have~
II computinQ an avaraQa
INPUT "Ent.r th. 1. numbar "I numbara
INPUT "Ent.r th. 2. numb.r "I numb.rb
av.rag.I-(numb.ra+numbarbI/2
PRINT "Tha av.raQ. of th. numb.ra"
PRINT numb.ra,"and",numb.rb
PRINT "ia"lav.raQa
END ".nd!"
Chapt.r 3 -
-50-
PROGRAMMING
it
El<erc:i :
1.
2.
numbers.
The numbers should be read in using INPUT
statements.
CIRCLES
The output from a program can also be in the form of a
drawing.
The next program draws circles.
Program 31
new
auto 100
0100
0110
0120
0130
0140
01:50
0160
0170
USE graphic.
graphic.creen(l)
-51 0180
0190
0200
0210
0220
0230
PROGRAMMING
circl.(160,100,radiusa)
circl.(160,100,radiusb)
circl.(160,100,sumradius)
WHILE KEV$-CHR$(O) DO NULL
END
sumradius'
2. Experiment with the use of other arithmetic operations
in line 140.
3. Move the centers of the circles.
4. Add instructions so that more circles with other radii
and centra are drawn.
5. The center of the circles can also be read in as an
input statement.
For example add the line:
Chapter
::s -
- 52-
PROGRAMMING
"I ICc,yc
leo circle(xc,yc,radiu.a)
190 circle(lCc,yc,radiu.b)
200 circl.(xC,yc,.umradiu.)
Run the program.
Note that it is necessary to respond with two values
separated by a comma <,) in the new INPUT statement.
6. The circles can be filled with colors. Use the order
fill(lC,y) to do this, where (IC,y) must be the
coordinates of a point Inside the closed figure which
is to be colored in.
For example if Pro;ram
::s
202 pencolor(2)
204 fill (160,100)
the innermost circle will be colored red.
Try it!
PROCEDURES I
When writing extensive COI1AL programs, it is particularly
important to make use of orocedures:
A procedure is a "subprogram" which can be called from the
main program or from another procedure.
It can perhaps best
be illustrated by means of some examples.
Pro;rAm 4 is
available on the demo diskette (and tape), or it may be
typed in:
PrOQrAm 41
ne"
auto 100
Chapter 3 -
-53-
PROGRAl'lHING
0100
0110
0120
0130
0140
01~0
draw'circle(12~.7~.20,orange)
draw'squareC100,~O,~0.~O,purple)
0160
0170 WHILE KEyS- .... DO NULL
01BO END
0190
0200
0210 PROC start'graphic.
USE graphics
0220
graphic.creen(l)
0230
0240
brown.-B
02~0
yellow.-7
0260
purple.-4
oranQe.-l0
0270
02BO ENDPROC start 'graphics
0290
0300 PROC draw'.quare(xmin,ymin.xside.yside,color)
pencolor(color)
0310
0320
moveto(xmin.ymin)
draw(xside,O)
0330
0340
draw(O.yside)
o:s~o
draN(-x.ide,O)
0360
draw(O.-ysid.)
0370
xpoint.xmin+.~*xsid.
ypoint ymin+.~*yside
0380
paint (xpoint.ypoint)
0390
0400 ENDPROC draw'square
0410
0420 PROC draw'circle(xcenter,ycenter.radius.color)
p.ncolor(color)
0430
0440
circle (xcenter,ycent.r.radius)
0460 ENDPROC draw'circle
Run the program: afterwards we"ll take a look at how the
program works.
Program 4 consists of:
Th. main program
Three procedures:
start 'graphic.
draw'.quare
draN'circle
(1
i nes 100-180)
ines 210-280)
(1 i nes 300-400)
\lines 420-460)
(l
- 54-
Chapt..r 3 -
PROGRAMMING
PROC <name)a>.<b).<c) )
<statement 1)
<statement. 2)
ENDPROC <name>
Recall that sharp brackets < > around a word
mean t.hat the word and t.he brackets can be replaced by names or statements of the users choice:
E.g. <name) could be replaced by the name
~
start.';raphics. printout or something else
describing t.he purpose of the procedure.
The notatlon <statement no> stands for a legal COMAL
statement.
The main program consists of a comment line followed by 5
lines which all call procedures.
In line 110 the main program just calls the procedure with
the name st.art.';raphics, and the computer proceeds to
execute the statement in this procedure.
When the computer has carrled out the statement.s in the
procedure. 1t returns to the ma1n program and goes on to the
next line.
In line 120
called.
In
with a pair
numbers are
-$-
PROGRAMMING
(100 - 160)
(190
260)
(280
460)
:s -
Ch.pt.r
- 56-
PROGRAI'II'1IN6
. .
)
*
*
~
~
Chapter 3 -
- 57 -
PROGRAMMING
(NB: <C- 0> means: hold down the Commodore key, while
pressing the o-key.)
Program :51
new
auto 100
0100
0110
0120
0130
0140
01:50
0160
0170
0180
0190
0200
0210
0220
0230
0240
02:50
0260
0270
0280
0290
0300
0310
0320
0330
0340
0350
0360
0370
0380
0390
0400
0410
0420
0430
0440
0450
0460
PROC r d'in
PAGE
PRINT "Write a message:"
INPUT "The l.tter i. to ", nam.$
INPUT "The letter is from ": from$
PRINT "Th. m g. can filion. lin "
INPUT "Start herel"l text$
ENDPROC read' in
PROC print 'out
PAGE
PRINT
PRINT "
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT AT 4,61 "To "In.m.$
PRINT AT 6,61 t.xt$
PRINT AT 8,61 "Be.t reCjlard."
PRINT AT 9,61 from$
CURSOR 20,1
ENDPROC print'out
Chapter 3 -
-~-
PROGRAMMING
PRINT AT <line>,<column>.
E.g. in line 440, where the from$ text is specified
to begin on line 9, in column 6.
This syntax makes it
possible to place text or numbers anywhere on the
screen.
~
~
*
*
Chapt.r 3 -
- 59 -
PROGRAMMING
0100
0110
0120
0130
0140
0130
0160
0170
0180
0190
0200
0210
0220
I I f i nd the maximum
PAGE
PRINT "Th. maximum of two numbersl"
PRINT
INPUT "Writ. the 1 numb.r "I a
INPUT "Writ. the 2. numb.r "I b
maximuml-a
IF maximum<b THEN maximuml-b
PRINT
PRINT "Maximum is " ,maximum
END
IF - THEN
Chapter 3 .;.
- 60-
PROGRAMMING
IF <condition> THEN
<statement 1>
<statement 2>
ELSE
<statement a>
<statement b>
ENDIF
Lines 170 - 180 in Pro;ram 6 could thus also be written as
follows using this IF-construction:
170 IF a<b THEN
172
maximuml-b
174 ELSE
176
malCimuml-a
180 ENDIF
Proqram 71
new
auto 100
0100 II ri;ht or wron;
0110
0120
0130
0140
OH50
0160
0170
01BO
0190
0200
0210
0220
0230
0240
02:50
0260
0270
02BO
0290
0300
DIM text$ OF 10
PAGE
PRINT "Bue my numberl 1, 2 or 3"
INPUT "Try your luck "Ian.wer
RANDOMIZE
my'numberl-RND(1,3)
IF an.wer-my'number THEN
text$I-"CDRRECT"
ELSE
text$I-"WRONG"
ENDIF
PRINT
PRINT "My number lola. ",my'number
PRINT "The gue _ . ",an.wer
PRINT
PRINT "Bo the ;ue lola. ",text$
END
- 61 -
PROGRAMMING
- 62-
PROGRAMMING
r ult
r ult
r ult
r ult
Progr.m 81
n ....
uto 100
0100
0110
0120
0130
0140
01:50
0160
0170
0180
0190
0200
0210
0220
0230
0240
02:50
0260
0270
0280
0290
0300
0310
0320
0330
0340
03:50
0360
0370
0380
0390
0400
0410
0420
0430
0440
04:50
0460
0470
0480
0490
I I Comput.tion .K.rci
PAGE
PRINT "Choo n .x.rci "
PRINT
INPUT "Which numb.r (1 - 4) ". numb.r
CASE numb.r OF
WHEN 1
.xerci l
WHEN 2
.K.rci 2
WHEN 3
.x.rci 3
WHEN 4
.K.rci 4
OTHERWISE
PRINT "You h.ve cho n .n incorr.ct numb.r."
ENDCASE
END
PROC .x.rci l
PRINT
INPUT "INT(7.3+3.2 DIV 2) corr.ct.-INT(7.3+3.2 DIV 2)
r ult(corr.ct n .....r)
ENDPROC .x.rci 1
"I
PRDC .x.rci 2
PRINT
INPUT "3-30/2+12 - " n .....r
corr.ct.-3-30/2+12
r ult(corr.ct n .....r)
ENDPRDC .x.rci 2
PROC .K.rci 3
PRINT
INPUT "4.2:5+2.:5/:5*2
" n .....r
corr.ct.-4.2:5+2.:5/:5*2
.n .....r
Ch.pter :5 0:500
0:510
0:520
0:530
0:540
0:5:50
0:560
0:570
0:580
0:590
0600
0610
0620
0630
0640
06:50
0660
0670
0680
0690
0700
0710
0720
- 63-
PROGRAMI'IING
r.sult(corr.ct,.nsw.r)
ENDPROC .x.rcis.:5
PROC ex.rcis.4
PRINT
INPUT "34 MOD 10-2*:5 - " nsw.r
corr.ct.-34 MOD 10-2*:5
r.sult(corr.ct,.nsw.r)
ENDPROC .x.rcis.4
PROC r.sult(corr.ct,.nsw.r)
PRINT
PRINT "The .nsw.r is. ";answer
PRINT "Th. correct .nsw.r is. ".corr.ct
PRINT
IF .nsw.r-corr.ct THEN
PRINT "You answ.r is right! ..
ELSE
PRINT "Wrong. Plea.e try .g.in .....
PRINT "Ch.ck Appendix C. c.lcul.ting with COMAL."
ENDIF
ENDPROC r.sult
Not.s.
A procedure may be called from another procedure, as well as
from the main program.
For example re.ult is called from
the .x.rci procedures.
Ex.rcis.s:
1. Try responding to some of the exercises in the program.
2. Create a new exercise 5:
.'
-64-
PROBRAI'II'1INB
<no>
equals
<end>
Progr.m 91
n ....
uto 100
0100
0110
0120
0130
0140
01:50
0160
0170
0180
0190
0200
0210
0220
0230
USE tjlr.phic.
tjlr.phic.cr n(O)
... r.p
... indo... CO, 1000.-10. 10)
mov.toCl000,0), dr .... to(O,O)
FOR nOI-O TO 1000 DO
numb.rl-RNDC-10, 10)
mov.toCno,O) , dr .... CO.numb.r)
ENDFOR no
WHILE KEV$-CHR$CO) DO NULL
END
Chapter
- 65-
;$ -
PROGRAHI'IING
REPEAT
<statement a>
<statement b)
UNTIL <condition>
The statement list is repeated until the' <condition> is
fulfi lled.
In the next example, Prooram 10, this type of loop
determines how long the user can continue to guess the
letters in a "secret" word. The example also illustrates
the use of strings in COMAL.
The prooram structure.
The main proGram
.elect'word
new 'letter
-66-
PROGRAMMING
Program 101
ne~
auto 100
0100 I I ward guessing
0110 PAGE
0120 select 'ward
0130 numberl-O
0140
01:50 REPEAT
numberl-number+1
0160
ne~ 'letter
0170
0180 UNTIL answer$-remember$
0190
0200 PRINT AT 20,51 "Now finished"
0210 PRINT AT 21,51 number I "letters have been used."
0220 END
0230
0240
02:50 PROC select'word
DIM name$ OF 20, letter$ OF
0260
0270
DIM used$ OF 200
INPUT "New wordl "I name$
0280
0290
lenljlthl-LEN(name$)
DIM
answer$
OF
length, remember$ OF lenljlth
0300
.n
.....
r - 11 _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ..
0310
0330
U d$l_ft.'
0::540
PAGE
PRINT "GUESS THIS",lengthl"LETTER WORD"
03:50
PRINT AT 8,51 "Wordl ",answer$
0::560
0370 ENDPROC select'word
0::580
0390 PROC new' lett.r
INPUT AT 10,5,11 "New letter "I letter$
0400
u d$l-u.ed$+l.tter$
0410
0420
0430
position:-Ietter$ IN name$
0440
IF position>O AND position<-lenljlth THEN
0450
answer$(position)l-letter$
0460
name$(position) 1-"."
0470
ENDIF
0480
PRINT AT 10,171 II "
0490
PRINT AT 8,51 "wordl ",answer$
0500
PRINT AT 12,11 used$
OSlO
0520 ENDPROC new'letter
Lines 150-180: the REPEAT - UNTIL loop:
When the user has the answer which the computer remembers,
the program continues in line 190.
Notesl
- 67-
PROGRAf1/'IING
- 68 -
PROGRAMMING
names.
Sometimes at least 100 variable names may be needed
when solving one of the following problems, for example:
*
*
)(2).-71
)(3)."'-12.45
)(49):=6
)(50):=0.8:52
In the next program example we wIll work wIth indexed
variables whIch are aSSIgned values by means of an INPUT
statement.
The program draws line segments through the coordinates of a
number of points.
ProQram 11 consists of:
a read-in .ection
a Qraphics s.ction
(line. 110-220)
(I i ne. 270-300)
Prooram 111
0100
0110
0120
0130
0140
01:50
0160
0170
I I line seoments
DIM )(:50), y(50)
PAGE
PRINT "A line is draNn throuoh the points.
PRINT
REPEAT
INPUT "Number of points. ". number
UNTIL number)-2 AND number(-50
Ch .. pt.r 3 0180
0190
0200
0210
0220
0230
0240
02:50
0260
0270
0280
0290
0300
0310
0320
- 69 -
PROGRAMMING
PRINT
FOR no.-1 TO numb.r DO
PRINT "Ent.r x(",no,"),y(",no,").",
INPUT .... , x(no),y(no)
ENDFOR no
PRINT
PRINT "Pre. . . . ny k.y to draw the .figure."
WHILE KEY$-CHR$(O) DO NULL
USE Qr .. phic:.
Qr .. phic:.c:r n(O)
mov.to(x(l),y(l
FOR no,-2 TO number DO drawto(x(no),y(no
WHILE KEY$-CHR$(O) DO NULL
END
Not
-70 -
Ch.pter 3 -
PROGRAMMING
17
-3
72
94
66
0.:5 14
-6
7B
and
bookc (3,1)-B
M()
numb.r>"
Chapter 3 -
-71-
PROGRAMMING
read number,item$,x,p01nts
data 17,"doll",-346,10
repla~e
numberl-17
item$I-"doll"
xl--346
pointlu"'10
Notice here that numbers and strings can be mixed in the
same DATA and READ statements.
The following program consists of,
Lines 120-250: dimensIoning and assIgnments
Lines 270-350: printout of information which agrees
with the search code
Lines 380-500: DATA statements
Program 121
new
auto 100
0100
0110
0120
0130
0140
0150
0160
0170
0180
0190
0200
0210
0220
0230
0240
02:50
0260
0270
0280
0290
0300
0310
0320
0330
0340
03:50
0360
0370
0380
0390
I I address list
PAGE
numberl-50; nOI-O
DIM person$(number,4) OF 30, text$ OF 30
DIM found(number)
REPEAT
nOI+1
FOR informationa-l TO 4 DO READ person$(no,information)
UNTIL EOD
numbera-no
-720400
0410
0420
0430
0440
04:50
0460
0470
0480
0490
0:500
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
PROGRAMMING
Not.5'
EKarcis.s:
1. Tryout the program.
Try tp understand how it
operates. Trv responding to S rch for' with just
<RETURN>.
Add new DATA statements.
2. Replace the values in the DATA statements for others of
your own choosing. The program can of course also be
used to file any information vou may choose.
For
example you might exchange the variable person$ with
a new variable it.m$ which could represent items in
an inventorv.
For example:
it.m$(no,l).-"w.r.hou "
item$(no,2),-"stor.g r "
it.m$(no,3),-".h.lf"
it.m$(no,4),-"it.m"
3. Add a line to the program which prints out the
classification number of the person or item along with
the other information.
4. Add further information about each person in the
address list:
DIM p.r.on$(numb.r.:5)
where for example:
p.r.on$(no,:5).-"<prof ion)"
Chapter
:s -
- 73-
PROGRAMMING
PROCEDURES II
In the section PROCEDURES I we became acquaInted with two
differ-ent ways of using pr-o'cedur-es:
PROC nama
<statemenLS;'
ENDPROC nama
PROC nama(numbar,taKt$)
<.. statements>
ENDPROC nama
If ther-e is a tr-ansfer- of par-ameter-s 1n parenLneses. then
the numoer- and type must Oe 1n agr-eement.:
nama (4 ,"John", fr-om, K () ,logo$)
PROC nama(numbar,taKt$,start,no(),str-ing$)
The number- and type of the actual par-ameter-s 1n the
pr-ocedur-e call must correspond to the number and tvpe of the
formal par-ameters 1n the procedure's parentheses.
4,"John",fr-om,K() ,logos are the act.ual paramet:ers.
numbar,taKt$,start,no(),str-ing$ ar-e the formal par-ameters.
If the parameters ar-e in agr-eement with respect to numoer
and type, they need not have the same name.
We have emphasized that procedures should be used when
building up pr-ograms, because:
Chapter 3 -
- 74-
PROGRAMMING
*
*
*
0100
0110
0120
0130
0140
01:50
0160
0170
0180
0190
I I local variable.
a.-lrb.-l
PRINT arb
local'vlobal(4)
PRINT a,b
END
PROC local'vlobal(a)
PRINT alb
ENDPROC local 'global
-75 -
PROGRAMMING
loo~
lIke
Ilmain program
tl"'23
.:--41
minmax(t-s,t+s,minimum,maximum)
PAGE
PRINT "t-. "";t-s;"og";t+. -";t+s
PRINT "Minimum, maximuml"lminimum;m&ximum
END
Exerei s:
minmax(t,.)
is deleted
- 76-
Chapter 3 -
PROGRAMMING
in the procedure.
Now the procedure can no longer
minmax(67,78) with constants in
be used in the form minmax(x,y)
and y have been given values in
sc:an
x-1236Iy-2~1
this-(x+y)/xlthat-(x-y)/y
minmax(thls,that)
print "Minimum, maximum. ",this,that
Experiment with the legal as well as the illegal
version.
new
auto 100
0100
0110
0120
0130
0140
draw'C:irc:le(160,100,100,2)
01~0
0160
0170
0180
0190
0200
0210
0220
0230
0240
0250
0260
WHILE
END
KEY$~CHR$(O)
DO NULL
PROC draw'c:irc:le(xc:,yc:,r,c:olor)
penc:olor(c:olor)
c:irc:le(xc:,yc:,r)
p&int(xc:,yc:)
IF r)10 THEN draw'C:irc:le(xc:,yc:,r-l0,c:olor+l)
ENDPROC draw'c:irc:le
yoU~
PROGRAMMING
ENDFUNC
Ove~view:
main program
function round
function f
\lines 100-350)
(lines 380-400)
\lines 421J-440i
midpoint method.
2. The program is designed to find a solution to the
equation f(x)-O, whe~e f is a function which is
continuous in the region of interest.
3. The user must be able to provide an Initial guess of
two numbers a and b with the property that f(a)
and f<b) have opposite signs.
See the flgu~e which
follows.
If this condition is not fulf1lled. the
-78 -
ehapt..,- 3 -
PROGRAMMING
x
F(B)
5.
8.
P,-og,-am 161
n.w
aut.o 100
0100
0110
0120
0130
0140
01:50
0160
.'-'-0,-,-1.-04
REPEAT
INPUT "End point. valu A,B. ", a,b
UNTIL SGN(f(a--SGN(f(b
Chapter 3 0170
0180
0190
0200
0210
0220
0230
0240
0250
0260
0270
0280
0290
0300
0310
0320
0330
0340
0350
0360
0370
0380
0390
0400
0410
0420
0430
0440
-79 -
PROGRAMMING
LOOP
.i;n'al-SGN(f(a
.ign'bl-SGN(f(b
ICmidl-(a+b) 12
ymidl-f(ICm1d)
IF ABS(ymid)(error THEN
PRINT
PRINT "A solution to the equation =",round(lCmid)
STOP
ELSE
PRINT ..... ,
IF SGN(ymid)-.i;n'a THEN
a:-ICmid
ELSE
b:-ICmid
ENDIF
ENDIF
END LOOP
END
FUNC round(number)
RETURN INT(number*10000+.S)/10000
ENDFUNC round
FUNC fIx)
RETURN 3*x*x+2*x-5
ENDFUNC f
- 80-
PROGRAMMING
It
- 81 -
PROGRAMMING
ll
..
-~-
PROGRAMMING
CLOSED PROCEDURES
If you want to be completely certain that any name conflicts
between variable names in procedures and the main program
will be avoided, then you can CLOSE your procedures or
functions.
When you do so, you make all variable names in
the procedure or function local.
Only those values which
are given in parentheses after the name of the procedure are
allowed in or out.
This is accomplished by using the order CLOSED. For
example:
PROC nAme(number,text$) CLOSED
It can be very useful to be able to close a procedure.
This
is particularly true when you want to save a very general
procedure in a procedure library and use it in many
different situations.
It can be difficult to remember the
names of all the variables which were used.
By closing the
procedure you can get around this problem.
The next program illustrates a general procedure which can
be used to sort any series of numbers.
The numbers will be
sorted so that they are ordered by increaSing value. For
example 4, 3, 7, -1 are sorted to -1, 3, 4, 7.
The sorting method is called the bubble sort.
There are many algorithms available for sorting.
For
example on the demonstration diskette and on the tape you
will find the program quick.sort.
It is a fast and
efficient sortIng program.
The bubble sort used in ProgrAm 18 in not the most
efficient method, but it is interesting and easy to
understand:
Consider the numbers in pairs starting at the beginning of
the sequence.
(You might find it useful to imagine small
bubbles surrounding these pairs.)
If a larger number
precedes a smaller one, then they will be swapped.
Now the
next pair (the second and third) is considered.
These two
numbers are swapped, if the largest number comes first and
so on down the sequence.
The procedure is repeated until no
more swaps occur.
Here is a brief illustration of the
process:
1.
run-through:
7 -1
4 3
3
4 7
-1
3 4
7 -1
2. run-through:
-1
7
3 4
7
3
4 -1
-1
3
4 7
3. run-through:
4 7
3 -1
-1
7
3 4
-1
3
4 7
is changed to
no change
is changed to
no change
is changed to
no change
is changed to
no change
no change
-1
3 4
3
-1 3
-1 7
-1 4
~
~
- 83 -
Chapt.r 3 -
PROGRAMMING
DATA 2,4,78,45,23,-2,56,45,199,43
DATA 3,0,100,34,-19,34,67,89,4,10
I I d ... t ... r ....d-in
DIM po.ition(100)
no."-O
REPEAT
no." +1
READ po.ition(no.)
UNTIL EOD
PAGE
PRINT "Unsorted numberl"
print'out(no*,position(
PRINT
PRINT
bubble'sort(no*,position(
PRINT "Sorted numb.r ."
print'out(no*,po.ition(
END
PROC print'out(tot ... l,number(
CLOSED
I I tot ... l number in .equ.nc. numb.r() is printed out
ZONE B
FOR no -1 TO tot ... l DO PRINT numb.r(no*),
ZONE 0
ENDPROC print'out
PROC ....... p(REF .,REF b) CLOSED
II ...... nd b ... r w... pp.d
r.m.mb.rc ... ' ..... b, b.-r.m.mb.r
ENDPROC .w... p
PROC bubbl.'.ort (tot ... l ,REF numb.r( CLOSED
II numb.r() i ort.d in incr ..... ing num.ric ... l ord.r
IMPORT
p
.w. .
- 84 -
PROGRAMMING
REPEAT
no' ..... pl-TRUE
O~OO
FOR nO*I-1 TO tot.l-1 DO
IF numb.r(no*numb.r(no*+l) THEN
0~10
no' ..... pl-FALSE
0~20
..... p(numb.r(no*>,numb.r(no*+l
0530
0540
ENDIF
O~~O
ENDFOR no.
UNTIL no' ..... p
0~60
0570 ENDPROC bubbl.'.ort
0480
0490
Exerci.e.:
1. Tryout the program with the values provided.
Then trv
WIth your own values.
You should also try the program
with special cases like DATA 2 or DATA 3,3,3,3,3,3.
2. This exercise deals with external procedures:
If a disk drive is available. procedures can be saved
on diskette individually.
Later on they can be brought
in to be used in other programs when needed.
After use
they are removed from a program.
Such procedures are termed external when they are
available outside program memory. as on a d1skette.
There are two conditions which external procedures must
fulfill:
a. They must be CLOSED
b. They must not contain IMPORT statements.
Now remove all other program lines from progr.m 18
except for the procedure print 'out and save
print'out on diskette as a prQ file under the file
name .xt.print'out:
v. ".xt.print'out"
The prefix .xt. has been added to distingu1sh this
type of file from other information in the d1sk
directory.
Then delete the procedure print'out from progr.m
Chapt_,. 3 -
- 85 -
PROGRAMMING
~10
Chapt.r 3 -
-86-
PRD6RAI'U'IINB
Chapter 3 -
-87 -
PROGRAI'1I'IING
0120
0130
0140
-88-
PROGRAMMING
Chapter 3 -
- 89 -
PROGRAMMING
TRAP
(statements in which errors are expected)
HANDLER
(statements to be executed in case of an error)
END TRAP
If an error occurs in the statements between TRAP and
HANDLER, the computer will jump to the sta~ements between
HANDLER and ENDTRAF'.
At the same ti me an ERR code wi 11 be
generated.
The ERR code can be used to determine which of
the statements in the HANDLER-section should be executed.
In the next program example an error handling structure has
been placed in the LOOP - ENDLOOP loop which we used
earlier.
This loop assures that the INPUT-statement wiil be
executed again if input errors are detected.
Note the following about the various COMAL ioop-structures:
IS
placed
LOOP
EXIT WHEN <condition) (or just EXIT w.th no condition)
ENDLOOP
The program consists of a general read-ln procedure w.th
error handling and a brlef maln program used to check out
the procedure.
Program 20.
ne",
auto 100
0100PROC number'input(line,pos,dpos,teKt$,REF number)
0110 II number-safe input
-~-
0120
0130
0140
OHIO
0160
0170
0180
0190
0200
0210
0220
0230
0240
02:50
0260
0270
0280
0290
0300
0310
0320
0330
0340
03:50
0360
0370
0380
0390
0400
0410
0420
0430
0440
0450
0460
0470
0480
0490
0:500
PROGRAMMING
LOOP
TRAP
PRINT AT line,pos. SPC.(LEN(text.)+dpos)," *"
INPUT AT line,pos,dpos. text number
EXIT I I if the input is OK
HANDLER
CASE ERR OF
WHEN 2
PRINT AT 24,11
WHEN 206
PRINT AT 24,11
OTHERWISE
PRINT AT 24,11
ENDCASE
FOR pause.-l TO
PRINT AT 24,11
END TRAP
ENDLOOP
ENDPROC number'input
I I test of input errors
PAGE
REPEAT
number'input<10,3,10,"Type in a number: ",number)
PRINT AT 12,3: SPC.(lS)
PRINT AT 12,3: number
UNTIL FALSE
END
Notes:
two
Chapter 3 -
-ffi-
PROGRAMMING
no'errorl-FALSE
REPEAT
no'errarl-TRUE
UNTIL no'error
Where should these lines be inserted?
3. Replace the CASE error texts the the system error
message ERRTEXT$: PRINT AT 24,11 ERRTEXT$.
4. Your final examination:
The character * 1n line 180 is a special detail.
What can happen, if th1s character is left out~
Experiment!
After working through this tutorial chapter you should be
well prepared to continue developing your skill with the
COMAL programming language.
Of course there is still much
more to be learned, and you can run into situations which
have not been covered here.
In Chapter 4 you will find a complete reference section
treating all of the many commands and statements in COMAL.
In Chapter 4 you will find explanations of each order wlth
examples to illustrate its use
-92-
-93-
COMAL
OVERVXEW
AUTO
IS a command which sets UP automatic line numbering
durIng program entry.
The range of legal line numbers
IS: 1 - 9999.
During program entry each line should be
terminated bv pressing (RETURN>.
The svstem will
automatically print the next line number on the screen.
AUTO can be disengaged by pressin9 <RUN/STOP>.
If AUTO
is engaged again (or engaged after manual entrv of part
of a program), automatic line numbering will begIn with
the last line number in the program + 10.
EMampl
AUTO
AUTO 1000
AUTO 100,2
Not
Line numbering with intervals of 10 is often
appropriate. for it allows the insertion of several
extra lines between existing line numbers.
If a line number alreadv exists. the number will appear
in reversed characters to warn the user against
unwanted overwriting of existing code.
RENUM
is a command which provides the program in working
memory with new line numbers.
Renumbering can begIn
from any line in the program.
RENUM
New numbering.
RENUM 2000,S
10. 20, 30
Chapt.r- 4 -
-94 -
2010
RENUM 300,4000,10
EDIT 130
EDIT 210-290
EDIT colorcod
Note:
The EDIT command can only be used for printout to the
screen or to a printer.
FIND
is a command used during editing to find a name or text
segment in a program.
When the text segment has been
found. the system prints out the program line with the
cursor placed on the first character of the text.
After possible corrections press <RETURN>. and the
system will search for the next occurence of the text.
FIND "John"
COt1AL OVERVIEW
CHANGE
a command which is used to search for and replace a
text segment. When the text segment to be changed has
been found, the system prints out the program line with
the text segment blinking like a cursor.
1S
DEL
1S
EKampl
DEL 20
DEL 40.200-280
DEL printout
Line 20 is deleted.
Lines 40 and 200"- 280 are deleted.
The procedure printout is deleted.
SCAN
is a command which causes the svstem to run through the
program in the working memorv.
This process is also
called making a prepass. The program structure is
checked for possible errors. and any error in structure
is reported. After a SCAN without any error messages,
approved procedures and functions can be executed
directly from the keyboard like commands.
-95-
Chapt.r 4 -
COI'1AL OVERVIEW
Program as entered:
0100
0110
0120
0130
0140
01~0
numb.r-O
r.peat
print number
numb.rl+2
print "You saw .om v.n numb.rs."
.nd
SCAN
The system wi 11 report:
add the line:
13~
until numb.r>20
numberl-O
REPEAT
PRINT numb.r
number: +2
UNTIL numb.r>20
PRINT "You .aw .ome even numb.r ....
END
OTHER COMMANDS.
SETEXEC
is a command which has two distinct formats: SETEXECand SETEXEC+.
During the initiation of the system, a SETEXEC- is
executed. This causes the keyword EXEC to be omitted
from procedure calls.
After a SETEXEC+ command EXEC will be printed before
all procedure calls.
PROC r.ad'in
0160
INPUT "Writ. the numb.rl HI numb.r
0170 ENDPROC r.ad'in
0180 PROC print'out
0190
PRINT numb.r
0200 ENDPROC print'out
01~0
After SETEXEC+:
- 97 0100
0110
0120
0130
COI'IAL OVERVIEW
wo~king
SIZE
System ~esponse:
prog
data
free
13501 02466 14747
CAT
is a command which causes a catalogue of the contents
of the diskette to be p~intet.
If seve~al disk d~ives
a~e connected. then the station numbe~ can be included
in the command.
Exampiesl
CAT
CAT IIt*1t
CAT "?est7?"
CAT "2:"
a~e
listed.
Not.1
P~essing the space ba~ will stop the p~intout of the
disk catalogue. P~essing it again will allow it to
continue. <STOP) will end it.
DIR
may be used as a command o~ as a statement.
Like CAT
this orde~ causes the contents of the diskette in the
drive selected to be printed out.
Unlike CAT, OIR can
be used as a statement in a program if desi~ed.
-~-
COMAL OVERVIEW
Examples.
LIST
LIST 200-400
LIST 300-
LIST demoproc
-93-
COMAL OVERVIEW
ENTER
is a command which fetches a program which has
previously been saved to diskette or cassette tape
using the LIST command into working memory. NBI
ENTER acts differently than MERGE.
If there is already
a program in working memory, ENTER will erase it.
The program
lst.circum'ference 1S
fetched from diskette and
added to the existing
program with 11ne numbers
starting after the end of
the current program.
DISPLAY
is a command which lists a program or a program segment
with NO LINE NUMBERS in the listing.
Example
DISPLAY
-100-
COMAL OVERVIEW
90 is
on the
with no line
p~inted
linep~inte~
numbe~s.
DISPLAY
so~t
"dsp.sort"
Note.
A p~og~am which has been saved on diskette (o~ tape)
with the DISPLAY commarld can not be fetched again using
ENTER o~ MERGE. However it can be read in as an
o~dinary sequential ASCII file using the o~der INPUT
FILE.
SAVE
LOAD
SAVE
1S a command which saves a copy of the program in
working memory to diskette or tape in compact binary
form.
A SAVEd program can be fetched later using one
of the following: LOAD, RUN or CHAIN.
Exampl.s.
SAVE "program name" The program in working memory 1S
saved to disk under the file name
program name.
SAVE "cslracetrack" The program is saved to cassette
tape under the file name
racetrack.
-101-
COI1AL OVERVIEW
RUN
CHAIN
can be used as a statement or as a command.
It fetches
a copy of a program from diskette or from cassette tape
and starts it running.
Any existing program in working
memory will be deleted first.
Used as a command
"(f i le name)u.
Chapter 4 -
-102-
COMAL OVERVIEW
variables is permitted.
However new variable names may
not be added, and the program may not be changed.
No
line may be altered, and no new lines may be added to
the program while it is interrupted.
If this is done,
execution cannot be continued using the CON command.
STATUS - STATUS$
STATUS is a command which causes the system to report
on the status of the disk operating system and zero the
error flag.
STATUS$ is a string function which
contains the status report.
STATUS performs the same
operation as PRINTSTATUS$.
Example:
Right after the system is turned on
STATUS
will caLIse the system to answer
73,cbm dos v2.6 1541,00,00
VERIFY
is a command which can be used to check that the
program on the diskette or cassette tape (saved using
the SAVE command) is identical to the program which is
currently in the working memory of the computer.
Warnings Take care not to change the program in
working memory before using VERIFY (spell correctly!).
Example:
VERIFY "test prog"
e,;actly alike.
COpy - DELETE - RENAME - PASS
COpy
can be used as a command or a statement for copying
diskette files.
Examples:
COPY "old'file","new'fil."
-103-
COMAL OVERVIEW
DELETE
may be used as a command or a statement to delete files
on a diskette.
DELETE "t tdata"
DELETE "t t* ..
RENAME
is used as a command or a statement to change the name
of a fi Ie.
EKampl
RENAME .. old ..... n .... "
PASS
can be used as a command or a statement to send orders
to the disk operating system.
EKamples:
Formats a new
diskette on disk drIVE
0.
This diskette gets
the name proc.durebib
and the identificatIon
number a1.
PASS "n2Idiskname.01".9
PASS "v"
Not
There are additional orders which can be transferred to
the disk operating system using PASS. But there are
more suitable COMAL-instructions for accomplishing the
same functions.
-104-
COMAL OVERVIEW
Keyboard input.
As
with the start up or
restart of the COMAL
system.
checkfil. will be
read in as if it came
directly from the
keyboard.
SELECT OUTPUT/SELECT
can 'be used as a command or as a statement.
It is used
to select the unit to which subsequent output will be
sent.
If one simply writes SELECT, the system will
automatically add OUTPUT in the program listing after
the program has been scanned or run.
SELECT OUTPUT "ds:"
Printout is directed to
the printer.
Not
SELECT OUTPUT can be abbreviated to SELECT. The
system automatically adds OUTPUT after a scan or a
run.
Printout will automatically return to the screen after
the LIST command has been executed.
Chapt.r 4 -
-105-
COHAL OVERVIEW
USE graphics
1S
activated.
LINK
is a command which fetches a file with a machine code
package from diskette and transfers a COpy into working
memory.
The name of the package can then be made known
to the program by means of the USE order.
Exampl
LINK "obj.driv.r"
USE driv.r
Not
A machine code program which is associated with a COMAL
ChApter 4 -
-106-
COMAL OVERVIEW
DISCARD
is a command which removes all machine code program
packages from working memory.
The COMAL program is not lost, but the interpreters
name table is only intact again after a RUN or a SCAN
has been performed.
STATEMENTS USED DURING READ-IN AND PRINTOUT
INPUT -
INPUT AT - KEY.
INPUT
Several numbers
can be entered In
the same INPUT
statement.
"I
"I
x,y
(I) or (,)
after the variable
name suppresses the
carriage return
after the answer.
no;
INPUT AT
acts like INPUT with the added possibility of placing
the input field anywhere on the 25 lines and 40 columns
of the screen.
EXAmplesl
INPUT AT 4,10. "Nullber - ". no
"I
text.
Chapter 4 -
-107-
COHAL OVERVIEW
7.
The input field is
limited to the 15
following spaces which
are protected from other
uses.
KEV$
is a function which reads the keyboard input buffer to
determine the last character activated.
If no
character has been sent, then the function returns the
value chr$(O) or ""0''''. Program executIon is not
stopped in contrast to the INPUT statement and the
function inkey$ (In the system package).
Examples of usel
DIM answe,.. OF 1
PRINT "Answer yes/no"
REPEAT
answer -KEY.
UNTIL answer. IN "yYnN"
PRINT - PRINT AT - PRINT USING
The program
'hangs' in the same
line until the user
presses any key.
PRINT
may be used as a command or a statement.
It is used to
print data on the screen or send it to other output
devices. If the PRINT line contains several items, they
can be separated by a semicolon <".
This will cause
a single space to be printed between each item.
If a
comma (,) is used, the the number of spaces between
the beginning of each item is determined by the ZONE
order. During program coding PRINT can be abbreviated
to I.
-108-
CCJt1AL OVERVIEW
Exampl
text and numbers
can be mixed in the
printout.
PRINT
PRINT AT
can be used as a command or as a statement.
It makes
it possible to print"numbers or text at any character
position on the screen. Line numbers may range from 1
- 25, and column numbers from 1 - 40.
Example.
PRINT AT 3,12. "Name i.", name$
The printout
begins in the 3.
I ine, column 12.
SpeCial ca
A
as line or position number means pre.ent or
current.
Example.
PRINT AT 0,30. "CPI'1AL"
PRINT USING
can be used as a command or a statement.
It is used
for printing numbers in a well defined format.
Example
PRINT USING "Pricl! ........... ". pric.
The amount is
written in the
format determined by
the" signs and
the decimal point.
In this example
there is room for 3
digits before the
decimal point and 2
digits after it.
-109-
Ch.pt.... 4 -
COHAL OVERVIEW
TAB
II
It.mnumb..... ",TAB(2:S),no
PRINT 23,:S6,89
23
89
sp.cing.-ZONE
PAGE - CURSOR
PAGE
can be used as a command or a statement.
It is used to
clear the screen.
If a printer has been selected as
the output device, a form feed order ... ill be sent to
-llO-
COMAL OVERVIEW
the printer.
CURSOR
can be used as a command or a statement.
It can be
used to position the cursor on the screen.
The
character position 1,1 is in the upper left-hand
corner, and 25,40 is in the lower right-hand corner.
Examplelil
CURSOR 1:5,30
CURSOR 0,10
,) .
Example:
READ name$,street$,no,postno,town$
DATA
is a statement which contains the values which the
variable names in a READ statement are assigned.
DATA
statements are not executed.
For this reason they can
be placed anywhere in a program.
However DATA
statements are local within a procedure or a function.
Example
The DATA statement can contain both text and numbers.
Text must be enclosed within Quotation marks ":
DATA "John Smith","Eaaton","Pennaylvania"
DATA 230,.e6,Xlll00ll0
Chapt.,. 4 -
-111-
COt1AL OVERVIEW
RESTORE
can be used as a command or as a statement.
It sets
the DATA pOinter to point at the first DATA statement
in a program or to the first statement right after a
label.
label a
is a freely chosen name which is used to specify an
entry point at some line in the program.
The label is
not eKecuted like an order.
It can be used in
connection with RESTORE (and GOTO). See the summary
example after the definition of EOO.
EOD
is a boolean (logical) system function which is used
during a READ from DATA statements. EOD means End
Of Data. As long as DATA-values remain in the
list, EOD is FALSE.
When the last DATA-value has been
read, then EOD is set to TRUE.
Summary example.
DATA ".cre_",112,"nail.",:50
toys:
DATA "car.",220,"dolls",::S:5
DATA "ball.",76,"jump rop .... ,24
DIM name$ of 20
RESTORE toys
WHILE NOT EOD
READ na_$,total
PRINT "Th.re are .. ;t.ot.al,name$,"left.."
ENDWHILE
Nate
It is usually convenient to place DATA statements near
the beginning of the program, so they are easy to find
and revise.
A label toys. has been placed just before the DATA
statements contai ni ng' t.he 1 i st of tOYs.
RESTORE toy. assures that READ begins in the
following line.
Read-in and printout of t.he toy inventory continues
untii EOD is set equal to TRUE. This happens when
there is no DATA left in the list.
Chapt.r 4 -
-112-
CoMAL OVERVIEW
CREATE
can be used as a command or as a statement.
It creates
a file on diskette. A file can also be created using
the OPEN order, but communication with the file can be
carried out about 10 times faster, when the file has
been CREATEd first.
EK.mplel
CREATE "t.Ktf1Ie",300,42
-113-
Chapter 4 -
COMAL OVERVIEW
stream number 3 is
reserved for this file,
until the file is closed
by means of a CLOSE FIL.E
3 order.
An already existing
sequential disk file with
the name data is opened
for addition of new
data following the
existing data on the
fi Ie.
The fi Ie is
ldentified by the stream
number 15.
PRINT FIL.E -
INPUT FIL.E
PRINT FIL.E
can be used as a command or as a statement.
It is used
for sending data in ASCII-format to a file on diskette,
cassette tape or other peripheral.
The file must have
been previously opened by means of the OPEN order.
The
file is identified by its stream number.
Chapter 4 -
-114-
COI1AL OVERVIEW
INPUT FILE
is a command or a statement used to read data from a
file which has been opened with OPEN no,name$,READ or
RANDOM. The file must contain data in ASCII format,
written with the PRINT FILE order.
Example
INPUT FILE 21 itemS
-115-
COMAL OVERVIEW
EKampl.s.
WRITE FILE 2. first$,last$,tel
READ FILE
:.s a command or- a statement which 1S used to r-ead data
frdm a file Wh1Ch has prev10usly been opened using the
order OPEN no,name$,READ or- RANDOM.
The file must
contain data In binary for-m, wr-ltten wIth the order
WRITE FILE.
EKamples.
READ FILE 21 first$,last$,tel
-116-
Chapter 4 -
COI'IAL OVERVIEW
CLOSE FILE/CLOSE
can be used as a command or as a statement.
It closes
files which have been opened with the OPEN order.
Serious errors can ar~se if one attempts to copy or
rearrange open files.
If the word FILE is omitted when
this order is used as a statement, it will be added
automatically by the system after a SCAN or RUN.
Example
CLOSE
CLOSE FILE 2
UNIT - UNIU
UNIT
can be used as a command or as a statement.
It is used
to specify which unit is to be used for file operations
when the file name does not contain this information.
When COMAL is started, the disk drive number 0 is
automatically selected as the unit.
See Chapter 7 on
Peripheral Equipment for further information.
The follow1ng uQits may be selected:
CS.
O.
1.
2.
cassette
disk drive no 0 (default)
disk drive no 1
extra disk drive (usual choice)
UNIT$
is a system function which returns the name of the unit
to be used, if no other specification is given in the
file name.
Example.
PRINT UNIT.
-117-
COMAL OVERVIEW
PROGRAMMING STRUCTURES
Conditionals
Loops
Error Handling
Procedures and functions
CONDITIONAL.S
IF - THEN - ELIF - ELSE - ENDIF
are statements which are used in IF-THEN structures.
An IF-THEN statement can be formulated in many
different ways.
The fundament-al principle is. however.
quite clear: If a <logical .xpr.ssion> is tru.,
then the associated statements will be executed.
Another way of expressing the same thing is to say that
if a given <condition> is fulfilled. then the
associated statements will be executed.
IF numb.r>-O THEN
squar. 'root.-SQR (numb.r)
PRINT "The square root of",numb.rl"is",.quar.'root
ENDIF
-118-
Ch.pt.,. 4 -
COMAL OVERVIEW
,.$.
Ex.mpl. 4.
IF <condition!> THEN
<statement>
ELIF <condition2>
<statement>
<statement>
ENDIF
IF numb.,.-O THEN
add'dat.
ELIF numb.,.-1
d.l.t.'data
ELIF numb.,.-2
p,.int'data
ENDIF
IF <condition!> THEN
<statement>
ELIF <condition2>
<statement>
If no condition
is fulfilled, then the
statements between
ELSE and ENDIF are
executed.
ELSE
<statement>
ENDIF
IF a$-"mail" AND b$-"box" THEN
PRINT "V ind d!"
PRINT "Th ....o,.d should b a$+b$
ELIF .$-"box" AND b$-"mail"
PRINT "T,.y ,..v.,.sinQ the wo,.d ....
ELSE
PRINT "Th. wo,.d. don't .g,._."
PRINT "Look . t the d,..wing .g.ln,"
PRINT ".nd t,.y .gA1n!"
ENDIF
-119-
Chaptltr' 4 -
COMAL OVERVIEW
CASE TRUE OF
WHEN denominator)O
PRINT "Positive denominator"
WHEN denominator-O
PRINT "Be careful!"
PRINT "o.nominator is zero."
WHEN denominator<O
PRINT "Neaative denominator"
PRINT "The sian is changed!"
denominatorl--denominator
ENDCASE
LOOP STATEMENTS
REPEAT - UNTIL
are statements which are used in the REPEATstructure.
The statements within the REPEAT-UNTIL
loop are repeated until the logical (boolean)
expression in the UNTIL statement is true.
-120-
COI'IAL OVERVIEW
Multi-line version:
The statements between REPEAT
and UNTIL run until the
logical expression is true.
REPEAT
INPUT "N.... numb.r
UNTIL a<O
REPEAT
<statement>
"I
As long as <expression>
is true, the statements
between DO and ENDWHILE
continue to be executed.
b.-l
WHILE KEV$-""O"" DO
-121-
COI'IAL OVERVIEW
b.-2*b
PRINT lib
ENDWHILE
As indicated by
the STEP parameter,
.n;le will take on
the values:
0,0.1, ,6.3
i.
-122-
COMAL OVERVIEW
LOOP
<statement>
EXIT WHEN (logical expression>
<statement>
ENDLOOP
LOOP
INPUT "T.xt "I t.xt$
EXIT WHEN t.xt$-".nd"
WRITE FILE 31 t.xt$
do't t
ENDLOOP
ERROR HANDLING
TRAP - HANDLER - END TRAP
are statements which are used to control program
execution after errors are encountered.
If errors
occur in the statements between TRAP and HANDLER
(called the TRAP part), then the statements between
HANDLER and ENDTRAP (the HANDLER part) are executed.
Otherwise the program continues with the line after
ENDTRAP.
In this way one can avoid having the program
stop e.g. due to a user data-entry error.
TRAP
INPUT "No. "I no
HANDLER
ch.ck '.rror
END TRAP
ERR - ERRFILE
ERRTEXT$
-123-
COI1AL OVERVIEW
TRAP
INPUT "Expon.nt " xpon.nt
PRINT 10~.xpon.nt
HANDLER
PRINT ERRTEXT.
CASE ERR OF
WHEN 2
PRINT "Expon.nt too l .. r;."
WHEN 206
PRINT "EKpon.nt i . . . numb.r"
OTHERWISE
PRINT .. Pl ..... try ..; .. in!"
ENDCASE
END TRAP
TRAP
INPUT "Fil.n ..m ".n.m.$
OPEN 2,n .. m ,READ
OPEN 3, ....v.fil ... ,WRITE
tr.nsfer (n ..me., ....v.fi 1.")
HANDLER
CLOSE
IF ERRFILE-2 THEN
PRINT "Error in re.d-in"
ELIF ERRFILE-3
PRINT "Error durin; print-out"
ELSE
PRINT "Not .. n input/output error"
ENDIF
PRINT ERR,ERRTEXT$
END TRAP
REPORT .rrorno
Chapt.r 4 -
-124-
CoMAL. OVERVIEW
SoTo - <L.abel,)
GoTo
is a statement which causes program execution to
continue at a predetermined place.
This place is given
by a <L.abel), i.e. a name followed by a colon (a).
It is not possible to jump out of a procedure or into a
closed program structure using GOTO.
Examplea
FOR no,-l TO 10 DO
READ FIL.E 2a number
IF number<le-37 THEN BoTo too'small
PRINT l/number
ENDFoR no
too 'small ,
PRINT "Divisor too small."
<L.abel')
is a name which is used to identify a program line.
The program line is not executed.
Execution continues
in the line following <L.abel,).
Labels are used in
connection with GO TO and RESTORE.
-125-
CDMAL OVERVIEW
PROCEDURES
PROC - ENDPROC
are statements which are used to form the PROC-ENDPROC
structur.. PROC-ENDPROC surround a number of
statements which toge~her form a proc.dur..
A
procedure is a program module, recognized by a name
stated in the procedure heading: PROC <nam.>.
The
procedure is carried out only if it is called from
somewhere else in the program using the same name that
appears in the PROC heading.
CO MAL programs should be created using procedures.
In
their simplest form, they can be used to break a larger
program down into smaller, easy to handle units.
More
advanced uses with parameter transfer and use of the
options REF, CLOSED, IMPORT and EXTERNAL make
procedures a programming tool 'of substantial value.
I I MAIN PROGRAM
<statement>
<nam.l>
<statement>
<nam.2>
<statement>
<nam.l>
<statement>
END
II
MAIN PROGRAM
PROC <nam.1>
<statement>
ENDPROC <nalll.1>
PROC <nam.2>
<statement>
ENDPROC <nam.2>
The statements of the procedure are enclosed in PROC
<nam.> and ENDPROC <nam.>.
The procedure can be
called "by name" from various places in the main
program.
/I I'1AINPROGRAI'1
start 'up
r.ad'1n
Chapter 4 -
-126-
COMAL OVERVIEW
PROC start"up
USE system
teKtcalarsCO,2,1)
DIM number ClO)
PAGE
ENDPROC start"up
PROC read"in
FOR na.-1 TO 10 DO
PRINT "Read in age (",na,")
I NPUT II II , number (na).'
ENDFOR na
ENDPROC re.d"in
II
<statement>
~
,.,
-127REF - CLOSED -
COI'IAL OVERVIEW
IMPORT
REF
is a parameter type which is used in a procedure call.
A REF preceding a parameter in the procedure heading
indicates that the name will only be synonomous with
the corresponding name in the procedure call.
It is
called by reference.
No room is reserved in the
computer's working memory for a new name and value.
The value receives only a new, temporary name.
Both
names r.fer to the same value.
In this way room is
saved in storage, execution speed is increased, and
parameter values can be passed both ways: into and out
of the procedure.
EM.mph..
<statement>
r d'in(cl ,n.m.$(
<statement)
PROC r d'in(REF no,REF .$(
INPUT "Which cl sl ": no
PRINT "Writ tud.nt n.me ....
i
.-0
REPEAT
i .+1
INPUT "N.me. " $ (i)
UNTIL .$ (i) .....
ENDPROC re.d'in
While the procedure read'in is carried out, the names
cia and no will refer to the same value because
of the REF in front of no. The same is true for the
names name$ and a$.
Both refer to the string
values in a one-dimensional array.
CLOSED
-128 -
COMAL OVERVIEW
-129-
COI1AL OVERVIEW
Let
PROC t t(A,b$,REF ch.ck) CLOSED
IF A-O AND b$ IN "Abed" THEN ch.ck.-TRUE
ENDPROC t.st
The procedure t.st is CLOSED and SAVEd on diskette
with the command SAVE t t
"elet.t t".
It can be used later in another program.
I I Program start
<statement>
t t(no,t.xt$,.rror)
<statement>
PROC t t(no,t.xt$,REF error) EXTERNAL ".let.t t"
I I Program end
This program will fetch the procedure t.st from
diskette, use it and "forget" it again.
The line with the EXTERNAL declaration can be placed
anywhere in the program.
MAIN
is a command which is used to bring the system back to
the main program, if it should stop during the
execution of an EXTERNAL procedure.
If execution is
stopped in an external procedure, LIST and other
editing orders will work only on the external
procedure, until MAIN removes it and brings back the
main program.
FUNCTIONS
FUNC - ENDFUNC - RETURN
are statements which are used in the FUNC-ENDFUNC
.tructur..
This structure consists of a number of
statements which together compose a u t-d.fin.d
function.
Functions must be introduced with FUNC
<nAm.> and terminated by ENDFUNC <nAm.>.
The value
which the function returns must be given in a
RETURN-statement.
Functions can be real functions, integer functions or
string functions.
A function is computed only if it is
called somewhere in the program by the same name which
is indicated in the function heading (FUNC (name.
Functions can be associated with the same properties
which were available for procedures: REF, CLOSED,
IMPORT and pArAm.t.r li.t. See also under these
keywords in Chapter 4.
In addition you will find that
Chapt.,. 4 -
-130-
COMAL OVERVIEW
II M.in progr.m
II r.al function
<statements>
PRINT .v.r.g.C b)
<statements>
FUNC .v.r.g.(x.y)
RETURN (x+y) 12
ENDFUNC .v.r.g.
II M.in progr.m
II int.g.r function
<statements>
f i r.tth .vo.... l ("COMAL")
cond vo.... l ( ...nd function.")
<statements>
FUNC vo .... I Ct.xt.) CLOSED
FOR i -1 TO LEN(t.xt.) DO
IF t.xt.H i.) IN .... iouAEIOU" THEN numb.r +1
ENDFOR i.
RETURN numb.r.
ENDFUNC vo.... l .
II M.in progr.m
II .tring function
<statements>
PRINT my.tical. ( ....cr.t")
<statements>
FUNC my.tic.l.( )
doubl 2*LEN( )
DIM b. OF 1. c. OF double
c
FOR i.-1 TO LEN( ) STEP 2 DO
b -CHR.(RND(6~.q3
c c.(.i'+b.+c.(i+1.)
ENDFOR i
RETURN c.
ENDFUNC my.tic.l.
-131-
COI'IAL OVERVIEW
FUNC Qrab$(first,a$)
1.nQth.-LEN(a$)
IF 1.nQth>1 THEN
IF firwt THEN
RETURN a$ (2. )
ELSE
RETURN a$(ll.nQth-1)
ENDIF
ELSE
RETURN 1111
ENDIF
ENDFUNC Qrab$
OTHER FUNCTIONS
ABS - INT - SGN - SQR - PI
ABS
is a function which calculates the absolute value of an
expression.
It is sometimes called the numerical
value.
If the numerical value of the expression is
negative, the sign is changed to positive.
A positive
value remains unchanged.
Exampl.sl
ABS(3.2:5)
ABS(-7.46)
ABS(x-7)
3.25
equals 7.46
the result depends on the value of x.
equals
INT
is a function which calculates the integer part of the
value of an expression, i.e. the largest integer (whole
number) which is less than or equal to the value of the
given expression.
Exampl.s.
INT(3.2:5)
INT(-7.46)
INT(1/2)
equals
equals
equals
3
-8
SGN
is a function which assumes the-value +1, 0 or -1, when
the value of a given expression is positive, zero or
negative respectively.
Exampl.w.
SGN(327.:54)
SGN(-4:5.7)
SGN(O)
SGN(x/7-y)
equals +1
equals -1
equals 0
the result depends on x
~nd
y.
-132-
Chapt.,. 4 -
SQR
is a function which returns the square root.
The
argument must be non negati ve ('i. e. posi ti ve or zero).
Exampl
SQR( 16)
SQR (4. 9.+09)
SQR(x .... 2+y .... 2)
equals 4
equals 70000
the result depends on x and y.
PI
is a system constant which is assigned the value
3.14159266. PI is particularly useful in connection
with the use of angles in radian measure, where PI
radians corresponds to 180 degrees.
CDS
CDS
is a function which calculates the cosine of a number.
This number must be expressed in radians.
x
x
degrees
radians
x*PI/180 radians
x*180/PI degrees
Exampl
CDS(PI/2)
CDS(2.:5)
CDS (v*PI/1S0)
equals 0
equals -0.801143616
the result depends on the value of v.
SIN
is a function which calculates the sine of a number.
This number must be expressed in radians.
See under
COS.
Exampl
SIN(PI/6)
BIN(angl.)
equals 0.5
the result depends on the value of angl
TAN
is a function which calculates the tangent of a number.
This number must be expressed in radians.
See under
COS.
Exampl
TAN (-PII4)
TANCl.S)
equals
equals
-1
-4.28626168
-133-
COMAL OVERVIEW
ATN
is a function which calculates the arc-tangent
(inverse tangent) of a number.
The result is a number,
expressed in radians.
Exampl.s.
ATNCI)
ATNC-200)
LOG
equals
equals
0.785398163
-1.56579637
(PI/4)
EXP
LOG
is a function which calculates the natural logarithm of
a positive number.
LOG represents logarithms to the
base e, where e is equal to 2.71828183. LOG is the
inverse function of EXP.
LOG(1)
LOG (10)
LOGC-2)
LOG(EXPCx
equals o
equals 2.30258509
is not defined
equals x
EXP
represents the exponential function.
EXP(x) ~ e raised
to the x'th power, where e i~ the base of the natural
logarithms.
EXP i~ the inverse function to LOG .
- 2.71828183 to good approximation.
Exampiesl
EXP(l)
EXP (3)
EXP(t-a*.2)
EXP(LOGCx
CHR$
equals 2.71828183 (- e)
equals e cubed = 20.0855369
the result depends on t and a.
equals x
STR$ - SPC$
CHR$
is a string function which equals the character which
corresponds to the ASCII code o~ the argument.
The
opposite operation is performed with the function ORD.
See Appendix A for Commodore ASCII codes.
Exalllpl.s.
CHR$(6:5)
CHR$(147)
equals the code for cl.ar scr n
the result depends on value
CHR$valu.
CHR$(QRDC"B") ) equals the character B
Chapt.r 4 -
-134-
COI'IAL OVERVIEW
STR$
is a string function which converts a numerical
expression to a string. The reverse operation is
performed by the function VAL.
STR$C1.34)
equals the string
STR$C2-:n
equals the string
STR$CVALC"7" equals the string
"1.34"
"-3"
"7"
SPC$
is a string function which returns the specified number
of spaces ("blanks").
PRINT "1",SPC.(10),"2"
text$.- "a"+SPC$(8)+"Jk"
blank.$.-SPC$(LENCnam.$
blank is a string
with the same number of
spaces as ~here are
letters in nameS.
equals 199
equals 69
the result depends on by$
equals 8
VAL
is a function which transforms a legal string argument
to its corresponding numerical value. To be legal the
string must be composed of the digits 0, ,9, the signs
+-, decimal point
or e used to specify
exponential notation. The reverse operation is carried
out with the function STR$.
Hexadecimal and binart notation is permitted.
~
..
-135-
Chapt.,. 4 -
COMAL. OVERVIEW
Exampl
VALC"123")
VAL C"2"+"3")
VALC"4.12")
VAL. ("ab.")
VAL.(BTR.(2
VAL. (".f.")
equals the
equals the
equals the
illegal
equals the
equals the
number
number
number
123
23
4e+12
number
number
2
254
L.EN
is a function, whose value is the length of the string
argument.
Exallpl
LEN ("abcd")
L.EN(nam )
LEN ( .... )
L.ENC"a ki")
TRUE
equals the
the result
equals the
equals the
number
depends
number
number
4
'an nameS
0
FAL.SE
TRUE
is a system constant which always equals 1.
FALSE
is a system constant which always equals O.
sec:ond
day
60 jiffies.
jiffies
(The clack is reset to zero.)
= 5184000
TIME 3600
c.-INTCTlME/60)
-136-
Ch.pt.... 4 -
COMAL OVERVIEW
RANDOMIZE - RND
RANDOMIZE
is a command and statement which is used to place the
random number generator at an arbitrary point in the
random number series. The random numbers are created
with the function RND.
Ex.mpl
RANDOMIZE
RANDOMIZE 6
I f RANDOMIZE is followed by a
number, this number will indicate the
starting position in the random sequence
each time random numbers are generated.
This will cause the same sequence to be
generated when RND is used.
RND
is a function which selects a random real number ~rom a
random number sequence of evenly distributed 'random'
numbers.
RANDOMIZE is used to position the random number
generator at an arbitrary position (based on the clock)
in this series.
Exampl
numb ..... -RND
no.-RND(-10,30)
PRINT RND(min,m.x)
-137-
COI'IAL OVERVIEW
TRAP ESC<STOP) i . pr d
dummy.-ESC
TRAP ESC+
OPERATORS
See Appendix C for a more detailed treatment of
operators.
DIV
MOD
DIV
is an operator which yields the value of the whole
number part of the quotient after division. x DIV y
is the same as INT(x/yl.
Exampl
:5 DIV 2
74 DIV 10
hc+3lDIV y
equals 2
equals 7
the result depends on x and y.
MOD
is an operator which computes the r.maind.r after
division.
x MOD y is the same as x-INT(x/yl*y.
Exampl
:5 MOD 2
74 MOD 10
8.25 MOD 2.1
(4-xl MOD z
equals 1
equals 4
equals 1.95
the result depends on x and z.
-138-
Ch.pt.... 4 -
CoMAL OVERVIEW
LOGICAL OPERATORS
NOT - AND - AND THEN - DR - DR ELSE
NOT
is a logical operator which changes the truth value of
an expression.
T... uth t.bl
a
NOT a
TRUE
FALSE
FALSE
TRUE
Ex.mpl
WHILE NOT EoF(2) DO
READ FILE 2. numb ....
PRINT numb .....
ENDWHILE
The loop continues until there is no more data in the
file with stream number 2.
IF NOT ok THEN .....d.t.tu.(ok)
The procedure .....d.t.tu. is executed until the
variable ok becomes TRUE >0).
AND
is a logical operator which determines the truth value
of a combined expression, a AND b. The combined
expression is only TRUE, if both. and b are true.
T... uth t.bl
a
TRUE
TRUE
FALSE
FALSE
TRUE
FALSE
TRUE
FALSE
a AND b
TRUE
FALSE
FALSE
FALSE
EIC.mpl
gives the value
FALSE
WHILE .lCp ......ionl AND .lCp ...... ion2 DO mak.d .... win;
If both ~p ...... ionl and .lCp ...... ion2 are TRUE, then
the procedure mak.d ....win; is execut.d. Otherwise it
is not.
-139-
COMAL OVERVIEW
AND THEN
is.a logical operator which is an extension of the
operator AND: a AND THEN b.
The same rules apply to
AND THEN as for AND; but if the first expression a is
false, the expression b is not computed, for it is
certain that the entire expression will be FALSE.
a$.-"t t",i.-l
length.-LENCa.)
WHILE i<-l.ngth AND THEN .$Ci)<>"." DO i.+l
For i:=S an error will occur in the logical expression
.$CO<>".", if this case is not eliminated by the
first condition.
OR
is a logical operator which determines the truth value
of a combined expression, OR b. The combined
expression is true, if just one of the expressions.
or b is TRUE.
Truth table.
a
TRUE
TRUE
FALSE
FALSE
! a OR b
TRUE
FALSE
TRUE
FALSE ,
TRUE
TRUE
TRUE
FALSE
DR ELSE
is a logical operator which is an extension of the
operator OR: a OR ELSE b. The same ru1es apply for
OR ELSE as for OR; but if the first expression a is
true, then the expression b is not calculated, since
the combined expression must be TRUE.
a'
If
equals 0, then the first logical expression is
true.
In this case an evaluation of the last
expression Cinvolving an illegal division) is
superfluous.
-140-
Chapt... 4 -
COI1AL. OVERVIEW
IN
is a operator which returns the position of a search
strlng in a given text: string IN text.
The value is the number in the text of the first
character in the search 'string.
If the search string
is not found, then the value 0 is returned.
IN can therefore be used for example to determine if
a response is contained in a string containing
acceptable answers.
Examples.
x "gram" IN "programing"
If answer. consists
of the letter n or
N, the expression is
TRUE, and the program
stops.
is printed.
length
then
14.
BITAND
a !b 00 01 10 11
00
01
10
11
00
00
00
00
00
01
00
01
00
00
10
10
00
01
10
11
E.g. Yo
1
0
0
AND AND AND
Yo
1
0
1
X
---------0
0
1
-141-
Ch.pt.... 4 -
COMAL OVERVIEW
Ex.mpl
XOOll BITAND X0101
17 BITAND 18
$f. BITAND l5
IF PEEK(urpo... t )
gives %0001
gives 16
gives 4
BIT.~ND
(decimal
1)
!b 00 01
10 11
----------------------00
01
10
11
E.g. %
00 01 10 11
01 01 11 11
10 11 10 11
11 11 11 11
gives
gives
OR
1
0
OR
0
1
OR
0
----------
BITXOR
is a logical (boolean) operator which executes an XOR
<i.e. an "exclusive OR") on each bit in the binary
representation of two numbers: a BITOR b.
Rul :
BITXOR
00
01
10
11
!b 00 01 10 11
00
01
10
11
01
00
11
10
10
11
00
01
E.g. %
11
10
01
00
1
1
0
XOR XOR XOR
0
0
1
---------0
Exampl
X0011 BITXOR X1010
17 BITXOR e
gives
gives
%1001
25
(decimal 9)
OTHER ORDERS
II
Chapt.,. 4 -
-142-
COI'IAL OVERVIEW
a$,-b$(l)+b$(LEN(b$
DIM pOint(-10,20)
DIM .pac.(lO,40,40)
DIM pr-ic.(OllOO,S,lO)
-10,-9,
Ch.pt.... 4 -
-143-
COI1AL OVERVIEW
DIM it.m$(lO) OF 20
Room for up to 10
it.m$-names. Each name may
contain up to 20 characters.
DIM
t.Mt$(0.10.2.~)
OF SO
Not
The first time a string is assigned a value, room is
reserved in memory for 40 characters, if not previously
declared by a DIM statement.
One dimensioned a string is set equal to the empty
string,
PEEK
POKE
PEEK
is a function which fetches the contents of a given
storage address.
The result is an integer between 0
and 255. A "map" with an overview of the use and
availability of Commodore 64 memory addresses can be
seen in Chapter 8 on Machine Language.
Ex.mpl
11 n -PEEK (214)
PRINT PEEK($ddOO)
POKE
is a command and a statement which is used to pl.ce a
number directly into a storage address:
POKE .ddr numb .....
You must be careful when using POKE, since sending
wrong numbers to random addresses can do strange things
-144-
Chapt.r 4 -
COI'IAL OVERVIEW
to your program.
If worst comes to worst, it may be
necessary to power-down and power-up again to continue
programming!
Exampl
POKE 198,0
POKE $dd03,Xl1110000
aya
is a command and statement which directs program
execution to a machinge code subroutine starting at the
address specified.
EKample.
SYS 4000
aya
:50000
END
STOP
is a statement which is used to stop the execution of a
program.
STOP can be placed anywhere in a program, and there
can be several STOP-statements in a program. After the
program has be.n stopped, the values of any variables
can be axamined and lor changed. Using the command
CON the program can be caused to continue at the line
following the STOP statement. However no changes in
program syntax may ba made.
Chapt.r 4 -
-145-
COI1AL OVERVIEW
EKampl.a.
The program stops with the
message: STOP at KKKK
STOP
STOP printout
finiah~N
END
is a statement which completely terminates program
execution and marks the conclusion of a program. END
can be placed anywhere in a program.
In contrast to
STOP, the program can't be continued with the CON
command.
EKampl.a.
END
-146-
-147-
COMAL
PACKAGES
WHAT IS A PACKAGE?
In vour COMAL cartridge there are 11 program packages with
useful procedures.
The packages are written in machlne code
for speed and compactness.
They can help you to take full
advantage of the many resources available in COMAL and the
Commodore 64.
2.
3.
4.
5.
6.
7.
8.
9.
10.
1t.
engl i sh
dansk
graphics
turtle
sprites
sound
system
font
paddles
joysticks
lightpen
,
,
Chapter
-148-
CDMAL PACKAGES
rD.ft)
(3 1 9 0)
-149-
COf'IAL PACKAGES
or
USE turtl .
(f1>
(f~)
While using CONAL graphics you are not limited to the use of
coordinates in the range from (0,0) to (319.199). You can
superimpose your own coordinate system onto the graphics
-150-
COI'1AL PACKAGES
USE graphics
graphicscreen(l)
window(-2,2,-l,l)
moveto(O,O)
drawto(2,-1)
WHILE KEY$-CHR$(O) DO NULL
The order window(-2,2,-l,l) superimposes a
coordinatesystem onto the display screen. The point (-2,-1)
is now at the lower left-hand side of the screen, and (2,1)
is at the upper right-hand corner.
When high resolution graphics is started up using the order
USE graphics, the coordinate system selected corresponds
to the order window(O,319,O,199) , in accord with the
standard screen coordinates. The order USE turtle
performs an automatic window(-160,l~9,-100,99). so that
the origin (0,0) is at the center of the display screen.
If you want to write text on the graphics screen, you can
use the special writing order plottext.
USE graphics
graphicscr n(l)
plott.xt(O,100,"C0I'1AL graphics")
WHILE KEY$-CHR$(O) DO NULL
In Chapter 3 there are further examples of the use of
graphics procedures.
In addition you will find many
examples of the use of graphics on the demonstration
diskette (or cassette tape) which accompanied your COMAL
cartridge.
GRAPHICS OVERVIEW
The packages graphics and turtle contain the following
orders:
D.finition of working area.
vi.wport - window
Choic. of graphics scr n and color graphics stat
graphicscr n
Choic. of graphiCS scre.n.
textscreen - fullscreen
splitscreen
Clearing of graphics acreena.
clearscreen - cl.ar
Color choic
textcolor - t.xtbackground - textbord.r
p.ncolor - background - border
g.tcolor
-151-
COI'IAL PACKA6ES
(X ,V) graphics.
plat
drawto - mov.to
draw - move
s.txy
circl. - arc
xcor - ycor
Int.llig.nt calor fill.
fill - paint
R.lativ. graphics.
showturtl. - hid.turtl.
turtl.siz.
ham.
s.th.ading - h.ading
p.nup - p.ndoNn
l.ft - right
forward - back
arcl - arcr
T.xt an the graphics scre.nl
t.xtstyl. - plott.xt
Information on graphics mod.s.
inq
Storag. and printing of the graphics imag
sav.scr n - loadscre.n
printscr n
In addition it is possible to use the following procedure
names when the turtle package is activated:
bk
bg
cs
fd
ht
It
pc
pd
pu
rt
s.th
st
t.xtbg
back
background
cl.ar.cr n
forward
hid.turtle
left
p.ncolor
p.ndown
p.nup
right
th.ading
showturtl.
t.xtbackground
Chaptar :5 -
-152-
COI'IAL PACKAGES
_-_-- ---d'"TJ i
........ ...
......
...
...... ----
CO....... _.....
~--x
;
!
CJ
-
...... _.n
Examplll:
viewport CO, 1:59,0,99)
windowC(wxmin>,<wxmax>,(wym1n>,<wymax
is a procedure which defines the ~oordinate system in
the given viewport.
The pixel in the lower, left-hand
corner of the viewport is assigned the coordinates
wxmin>,<wymin.
The pixel in the upper, rlght-hand
corner is assigned the coordinates wxmax>,<wymax.
All subsequent graphics orders (except viewport and
the sprite commands) will be refered to this coordinate
system until a new one is defined.
On start-up with USE graphics the viewport is the
entire display screen and the coordinate system is
defined by windowCO,319,0,199)
On start-up with USE turtle the viewport is the
entire display screen and the coordinate system is
defined by windowC-160,1:59,-100,99).
Thus the point
(0,0) is in the middle of the display screen.
Exampla.
w1ndow<-1000,2000,-100,200)
graphicscreenmode
is a procedure which makes the graphics screen appear
on the display screen and makes the the text screen
invisible.
The graphics screen can be made accessible in two
different modes:
graphicscraanCO)
graphic:sc:raanCl)
-153-
COt1AI. PACKAGES
(multi-colo~
di~ection
texttic~een
full.c~e.n
sc~olling
Chapt.r 5 -
-154-
COMAL PACKAGES
p.ncolor({color
is a procedure which defines the color of the pen.
Exampl.sl
p.ncolor (7)
p.ncolor (-1)
background color
is a procedure which defines the graphics screen
background color.
bord.r color
is a procedure which defines the graphics screen border
color.
Chapt.... !5 -
-155-
COl'IAl PACKAGES
g.tcolo... x >,(y
is a function.
Its value equals the color code of the
pixel at location x),(y.
If x),<y is outside the drawing area determined by
the procedure viewport, then getcolo... x>,<y
returns the value -1.
The function getcolor does not change the current pen
position.
Examples.
PRINT getcolo... (1,2)
IF g.tcolo... (O,O)(O THEN move'center
plot xO>,(yO
is a procedure which places a dot at pen position
xO>,(yO
Ex.mpl
plot(4.3,:56)
d ....wto x ),<y>l
is a procedure which draws a line from the current pen
position to the point x>,(y, which becomes the
new pen position.
Examples.
dr.wto(lOO,200)
d .... wto(-20,4000)
moveto x >,(y
moveto(200,-2:5)
d ....wdx >,<dy>l
is a procedure which draws a line from the current pen
position xO>,<yO to the point with coordinates
xO)+<dx>,<yO>+<dy and changes the pen position to
the endpoint.
Ex.mpl.s.
d .... w(O,lOO)
d ....w(-1.!5,O.4)
-156-
COMAL PACKABES
move (-3,20)
meve(-2000,O)
<vymax> - <vymin>
<wymax> - <wymin)
<vxmax> - <vxmin)
vi.wpert(200,300,BO,1BO)
NindeN(-l,l,-l,l)
circl.(O,O,U
yields a round circle on the upper right-hand side of
the screen.
..,
Chaptllr
-157-
COMAL PACKAGES
and
year
are functions.
They equal, respectively, the current x
and y coordinates of the pen.
Example
PRINT xcor;ycer
plottext(xcar,ycar,"Figure 1")
fill x>,<y
is a procedure which uses pencolor to fill a region of
the screen with color. The region to be filled in must
contain the point x),<y)l.
It must be bordered by a
line or area of a different color or by an edge of
the viewport.
fill does not alter the pen posit1on.
See the summary example under the procedure paintx>,<y.
Example.
fill (10,56)
paint x),<y
is a procedure which fills in a region of the screen
with the drawing color.
The reg10n Wh1Ch 1S to be
filled in must contain the point C<x>.<y. and it must
be bordered by a line or area with the same color or
by an edge of the drawing area.
paint does not alter the current pen posit1on.
Example
paintC-10,4)
pencolor (-1)
paint C100,20)
A region is 'erased'.
Chapt.er
-158-
CDI'IAL PACKAGES
USE graphic.
graphic.creen(l)
pencolor(7)
drawt.o(:519,199)
fill (10,100)
pencolor(l)
circle(100,100,70)
fill <100,100)
II
if pa1nt.(10,lOO) , no difference
II
USE
hid.turtle
is a procedure which causes the turtle on the graphics
screen to become invisible.
t.urtl iz..iz.
is a procedure which defines size of the drawing
arrowhead (the turtle).
The parameter <size> must be a number between 0 and 10.
When graphics is started up, this parameter is
automatically set equal to 10.
home
is a procedure which places the turtle at coordinates
(0,0) pointed upwards on the screen
t.h.adingh.ading
is a procedure which sets the direction in which the
turtle points.
If the turtle is viSible, it will turn
to face this direction.
<heading> is given in degrees:
o corresponds to upwards.
90 is towards the right side of the screen.
-90 is towards the left.
Chapter :5 -
-159-
COt1AL PACKAGES
penup
is a procedure which lifts the pen.
pendown
is a procedure which lowers the pen.
turtle to draw as it moves.
It causes the
Ch.pt.... !5 -
-160-
CDI'IAL PACKAGES
Ex.mpl
fa ... "'.... d(20)
.... cl(:50,30)
PRDC softfr.m.(xmin,ymln,,,,idth,h.ight)
IF width)20 AND h.ight>20 THEN
wi dth-",1dth-20
h.ight-h.ight-20
mav.to(xmin+l0,ymin)
th ding(90)
fa ... "'.... d("'idth)
.... cl <10.90)
fa ... "'.... d(h.ight)
.... cl (10,90)
fa ... "'.... d(width)
.... cl <10.90)
far"' .... d(h.ight)
.... cl <10.90)
ENDIF
ENDPROC .aftf .... me
.... cr r >, <d.
is a procedure which draws a curve to the right with
radius of curvature <... > and turning angle <d.>.
The starting point is the current position of the pen,
and the initial heading is the current heading .
rcrr>,<d.
corresponds to .rcl(-<r>,-(d..
Ex.mpl
.... cr(3.4!5.!50)
w....p
is a procedure which allows lines
drawn on the graphics screen to
continue beyond the edge of the
screen, reappearing on the opposite
side.
For example, if the pen
disappears at the top of the screen
with x-coordinate 110 and heading
45, it will reappear at the bottom
with the same x-coordinate and the
same heading.
When USE turtle is engaged, the procedure wr.p is
carried out automatically.
This is however NOT the
case when USE g ....phlc. is started.
na",r.p
is a procedure which terminates wraparound.
be restored with the procedure wrAp.
It can
-161-
Chapter :5 -
COI'IAL. PACKAGES
<mode>
textstyle(2,1,2,0)
textstyle(3,2,-1,-1)
plottextx),(y),(text$
is a procedure which prints out the given text starting at the
point x>,<y>l.
The size of the letters, the orientation and writing mode are
specified by the procedure textstyle.
plottext does not change the position of the pen.
Examples.
plottext (100,1:50, "COt1AL")
text$."Wh_ts my n __ 7"
textstyle(1,3,1,0)
plottext(200,10,text$)
-162-
COI'IAL PACKAGES
inqno
is a function which is used to obtain information concerning th
state of the various graphics variables.
The parameter <no> must be an integer between 0 and 33.
<no> Information
o
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
display
text border
text backgnd.
text color
graf. border
graf. backgnd.
pen color
gr. text width
gr.text height
gr.te>:t dirn.
gr.te:<t state
turtle visible
inside window
txt scrm seen
spIt scrn seen
wraparound
pen down
x - position
y - position
vxmin
o or 1
o - 15
o
15
o
15
o
15
o
15
o
15
1
o
o
254
254
3
or 1
TRUE ,FALSE
TRUE,FALSE
TRUE ,FALSE
TRUE,FALSE
TRUE ,FALSE
TRUE ,FALSE
wymax
integer
integer
integer
integer
integer
integer
real number
real number
real number
real number
COS(headingl
SIN(headingl
turtle size
x-aspect ratio
v-aspect ratio
x-text end
v-text end
-1.0 - 1.0
0.0 - 10.0
real number
real number
integer
integer
vxmax
vymin
vymax
wxmin
wxmax
wymin
-1.0 -
1.0
graphicscreen
setcolors, border
setcolors, textbackground
setcolors, textcolor
border
background
pencolor
textstyle
textstyle
textstyle
textstyle
showturtle, hideturtle
most drawing procedures
screen
screen
wrap, nowrap
penup, pendown
most drawing procedures
most drawing procedures
viewport
viewport
viewport
viewport
window
window
window
window
seth,left,right,home,arcl,arcr
seth,left,right,home,arcl,arcr
turtlesize
=(wxmax-wxminl/(vxmax-vxmin)
=(wymax-wym1n)/(vymax-vymin)
plottext
~
plottext
.,
background color
border color
1000 bytes for colors 0 and 1
8000 bytes for the bit pattern
-163-
Chapter :i -
COI'IAL PACKAGES
background color
border color
1000 bytes for colors 1 and 2
1000 bytes for color 3
8000 bytes for the bitpattern
Example
savescreen("grO.drawing U
ave.creenC"grl.circles")
loadscreenC<filenameS
is a procedure which fetches an image which previously
had been saved on diskette or on tape.
See
savescreen.
Examples:
loadscreenC"grO.drawing")
loadscreen("grl.circles U )
printscr.enfilenameS>,<position
is a procedLlre which saves the contents of the current
viewport to the file named <filename$).
The parameter <position> is an integer from 0 to 479.
It specifies the horizontal placement of the image on
the MPS801 printer.
Six <position) unIts correspond to
one character from the edge of the paper.
Color
background color
all other colors
-164-
COMAL PACKAGES
Multi-color graphics:
Colors are printed according to a grey scale:
Printing
intensity
0/4
1/4
2/4
3/4
4/4
Color
1 : white
3: cyan, 7:
15:
4:
10:
2:
0:
Chapter
-~-
COMAL PACKAGES
SPRITES
With your Commodore 64 it is possible to define a small
graphics image which can be moved about on the graphics
screen.
Such an image is called a sprite.
Up to 8 sprites can be on the screen at one time.
This
makes it possible to create vivid graphics images with
moving figures.
For each sprite can be assigned its own
color and be moved around independently of the others and
the rest of the program.
It is also possible to allow the
sprites to interact with one another.
(graphics drawings)
-166-
COMAL PACKAGES
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
1.00000000,1.00000000,1.00000000
1.00000000,1.00000000,1.00000000
1.00000000,1.00000000,1.00000000
1.00001110,1.00001110,1.00000000
1.00001111,1.00011110,1.00000000
1.00000111,1.00111100,1.00000000
1.00000011,1.00110000,1.00000000
1.00000001,1.11100000,1.00000000
1.00000011,1.11100000,1.00000000
1.00000111,1.11110000,1.00000000
1.00000011,1.11100000,1.00000000
1.00110001,1.11000000,1.00000000
1.00111111,1.11100000,1.00000000
1.00001111,1.11110000,1.00000000
1.00000111,1.11110000,1.00000000
1.00000111,1.11100000,1.00000000
1.00000111,1.11100000,1.00000000
1.00011111,1.11111000,1.00000000
1.00111110,1.01111100,1.00000000
1.00000000,1.00000000,1.00000000
1.00000000,1.00000000,1.00000000
USE graphic.
graphic.cr n(O)
USE sprit
DIM dr.wing$ OF 64
FOR ia-1 TO 63 DO
READ byt.
dr.wing$a+CHR$(byt.)
ENDFOR i
colora-1
dr.wingno:-1
sprit.noa-1
d.fin.(dr.wingno,dr.winQ$+ .. "O .... )
id.ntify(sprit.no,dr.wingno)
sprit.color(sprit.no,color)
sprit.pos(sprit.no,:l0,100)
showsprit.(sprit.no)
WHILE KEV$-CHR$(O) DO NULL
The extra
""OHM is included to specify that the drawing is a representation in high resolution graphics (as opposed to
Ch.pt....
s -
-167-
COt1AL PACKAGES
multi-color graphics).
In line 440 sprite 1 is identified to correspond to drawing
no 1.
In line 450 the color of the sprite with number 1 is
specified (color:=l, i.e. white).
In line 460 sprite no 1 is placed on the screen 50 that the
upper left hand corner of the figure is at (x,y) coordinates
(50,100). Line 470 makes the sprite appear on the screen.
When you have had enough of the rabbit, press any key.
Line 510 causes the sprite to move over to the point with
coordinates (250,150).
The move is made in 200 steps.
We
will get back to the last 0 in the mov.sprit. procedure
call later.
When you again press any key, the program ends.
That was your first program using sprites. Now try giving
the rabbit another color.
Try moving it around to other
points on the screen.
MORE SPRITES
Add the program lines
472
474
476
478
id.ntify(2.drawingno)
sprit.color(2,O)
sprit.pos(2,80,100)
showsprit.(2)
Ch.pt....
s -
-168-
COMAL PACKAGES
This can obviate the need for including all the DATA
statements in programs using the same sprite image.
The following program (Sp ... it. 2) defines the drawing of
the rabbit and saves this drawing on diskette under the name
.pO ..... bbit.
If you run this program, you will e.g. be
able to replace lines 100-310, 360-400 and 430 in other
programs using the drawing with a single line:
430 10.d.h.p.Cdr.winlilno.".pO .....bbit")
First the drawing must be saved using:
0100 til 0300: DATA statements with sprite image
content (See previous program.)
0310
0320 USE .p... it
0330 DIM d .... winlil$ OF 64
0340 FOR il-1 TO 63 DO
0350
READ byt.
0360
d .... Ninlil$I+CHR$(byt.)
0370 ENDFDR i
0380 d .... Ningnol.1
0390 d.fin.(d ....Ningno.d....wing$+ .. "O" .. )
0400 v h.p.(dr.wingno ....pO.r.bbit .. )
SPRITES USED WITH OTHER GRAPHICS
The following program shows how a sprite can be prepared to
detect a collision with a graphics drawing and wait for the
collision to happen.
After the collision, the sprite can
continue in a different direction.
Chapt.r ei -
-169-
COI'IAL PACKAGES
0~80
PROC make'box
0600
p.ncolor (a)
0610
moveto(100,10). draw(~O,O)
0620
drawCO,150), drawC-50,O). drawCO,-150)
0630
fill (10~,1~)
0640 ENDPROC make'box
0~90
This is
-170-
COMAL PACKAGES
0100
0110
0120
0130
0140
01:50
0160
0170
0180
0190
0200
0210
0220
0230
0240
02:50
0260
0270
0280
0290
0300
0310
0320
0330
0340
03:50
0360
0370
0380
0390
0400
0410
0420
0430
0440
0450
0460
0470
0480
0490
0:500
0:510
0:520
0:530
0:540
0:5:50
0:560
0:570
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
7000000000,7000000000,7000000000
7000000000,7000000000,7000000000
7000000000,7000000000,7000000000
7000001110,7000000000,7000000000
7000001111,7000011110,7000000000
7000000111,7000111111,7000000000
7000000011,7000110111,7000000000
7000000001,7011100000,7000000000
7000000011,7011100000,7000000000
7000000111,7011110000,7000000000
7000000011,7011100000,7000000000
7000000001,7011000000,7000000000
7000000011,7011100000,7000000000
7000111111,7011110000,7000000000
1.00111111,7011110000,7000000000
7000000111,7011100000,7000000000
7000000111,7011100000,7000000000
7000011111,7011111000,7000000000
1.00111110,7001111100,7000000000
7000000000,1.00000000,7000000000
1.00000000,7000000000,7000000000
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
1.00000000,7000000000,7000000000
7000000000,1.00000000,7000000000
7000000000,7000000000,7000000000
7000001110,7000001110,7000000000
7000001111,7000011110,1.00000000
7000000111,7.00111100,7000000000
7000000011,7000110000,1.00000000
7000000001,1.11100000,7000000000
7000000011,7011100000,7000000000
7000000111,7011110000,1.00000000
7000000011,7011100000,1.00000000
7000110001,7011000000,7000000000
7000111111,1.11100000,7000000000
1.00001111,7011110000,7000000000
7000000111,7011110000,1.00000000
7000000111,7011100000,7000000000
7000000111,1.11100000,7000000000
7000011111,7011111000,7000000000
7000111110,7001111100,7000000000
7000000000,7000000000,7000000000
7000000000,7000000000,7000000000
USE graphics
Qraphicscr n(l)
USE sprit.s
color.-1
Ch.pter :5 0580
0590
0600
0610
0620
0630
0640
06:50
0660
0670
0680
0690
0700
0710
0720
0730
0740
0750
0760
0770
-171-
COI'IAL. PACKAGES
.prit.nal-l
DIM dr.wing$ OF 64, .ctian$ OF 64
FOR dr.wingnal-l TO 2 DO
dr.wing$I-"H
FOR 1.-1 TO 63 DO
READ byte
dr.wing$I+CHR$Cbyt.)
ENDFOR i
d.fin. Cdr.winljjna,dr.wi nljj$+" "0"" )
ENDFOR dr.winljjna
id.ntifyC.pr1t.na,1)
.prit.colarCsprit.na,calar)
sprit.pasC.prit.na,:50,100)
.hawsprit.Csprit.na)
Actian$I-"Ulllll+""4""+'t"2""+"I'5'1"
.nim.t.Csprit.na,.ctian$)
movprit.C.priteno,3:50,150,300,0)
WHIL.E KEY$-CHR$CO) DO NULL
Transparent
Color 2
Foreground color 1
Color 3
Chapt.... S -
-172-
COI'IAL. PACKAGES
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
YoOOOOOOOO,XOOOOOOOO, XOOOOOOOO
XOOOOI0I0,XOOOOOOOO,XOOOOOOOO
XOOOOI0I0,XOOOOOOOO,XOOOOOOOO
XOOOOOI01,XOI010101,XOI010000
Yo00000101,XOI010101,XOI0I0000
X00000101,XOI010101,XOI010000
XOOOOI010,X10101010,XI0100000
X00001010,XI0101011,XI1100000
X00001000,XOOI01011,XII100000
XOOOOI000,X00101011,XI1100000
X00001000,XOOI01011,XI1100000
XOOOOI000,XOOI0I011,Xll100000
XOOOOI000,X00101001,X11100000
XOOOOI0I0,XI0101011,XI1100000
YoOOOOI010,XI0101011,XIII00000
XOOOOI010,X10101011,XI1100000
X00001010,X10101011,XI1100000
XOOOOI010,XI0101011,XI1100000
Xl1111111,X11111101,XOllll111
Xllllllll,X11111101,XOlll1111
Xll111111,Xl1111101,X01111111
-17303:50
0360
0370
0380
0390
0400
0410
0420
0430
0440
04:50
COI'IAL. PACKAGES
spriteback(2,12)
RANDOMIZE
FOR spritene.-O TO 7 DO
spriteceler(spritene,RND(3,10
spritepesCspritene,spritene*40,:!O)
identifyCspritene,drawingne)
shewspriteCspritene)
spritesizeCspritene,l,l)
ENDFOR spritene
FOR i.-l TO 100 DO pletCRND(0,319),RNDC:50,199
WHIL.E KEY$-CHR$CO) DO NUL.L.
Chapter
-174-
COMAL PACKAGES
Example.
defineC23,house$)
idantifyspriteno),<drawingno
is a procedure which specifies that the sprite with the
number <spritano) is to be displayed using the image
with the number <drawingno). There can be up to 8
different sprites on the screen at once.
The parameter
<spritano) must be an integer from 0 to 7. The same
drawing can form the basis for several sprites.
The sprite with the lowest <spriteno) has the highest
priority and is therefore displayed in front of others
with which it overlaps on the screen.
If the graphics turtle is displayed on the screen, it
always has sprite number 7.
Example.
idantifyCO,23)
spritecolorspriteno),<color
is a procedure which assigns the sprite with the number
<spritano) the color specified. The parameter
<spritano) is an integer from 0 to 7, and <color)
is an integer from 0 to 15.
In high-resolution
graphics the sprite will have this color.
In multicolor graphics it is colorl.
Example.
spritecolorCO,S)
Chaptar
-175-
COI1AL PACKAGES
spritabackcolor2),<color3
is a procedure which specifies the colors in multicolor graphics. A multi-color sprite can have up to
four colors:
transparent
foreground color
additional colors
Exampla.
additional colors are red and yellow.
Special rules for Multi-colored Sprites.
In a multi-color drawing pixels are associated in
horizontal pairs.
Each color (background-, foregroundand additional) is indicated by bit patterns as
follows:
Bit
pair
Color shown
Is set by
00
01
10
11
transparent
color 2
color
color ,;,
graphics orders
spriteback
spritecolor
spriteback
spritasizeC(spriteno>,(xdouble>,<ydouble
is a procedure which determines whether the sprite
numbered <spriteno> will be displayed in double size
format.
Normally a sprite occupies 24 pixels in the
x-direction and 21 pixels in the y-direction.
If
(xdoubla) is set equal to a number not equal to 0
(=TRUE), then the sprite will be shown in double
width. Similarly for <ydouble>.
Examples.
spritesizeC~,O,l)
spritasizaC2,TRUE,TRUE)
spritaposC(spritano),(x),(y
is a procedure which places the upper left-hand corner
of the sprite at the point with screen coordinates
(x, y)
Sprite positions are always specified in the screen
-176-
Chapter S -
COMAL PACKAGES
Sprite
is placed at screen
position (25,50).
The
-177-
COMAL. PACKAGES
FUNC step(spriteno,x)
speed.-10
dist.-ABS(x-spritex(spriteno
RETURN speed*dist
ENDFUNC step
If in particular step equals 0, the sprite will be
moved immediately (next screen update) to the position
x>,<y regardless of the value of <mode>. The
sprite will not move again, but it can be caused to
perform an action by using the procedure animate.
The parameter <mode> affects the moment when the
movement begins, and determines whether or not
collision with other sprites and graphics drawings will
be taken into account. The parameter <mode> is an
integer from
to 7:
<made)
1
2
3
4
5
6
7
effect
Start
Await
Start
Await
Start
Await
Start
Await
now,
start signal,
now,
start Signal,
no.... ,
start signal,
now,
start Signal,
no collision check
no collision check
check sprite/sprite collision
check sprite/sprite collision
check sprite/graphics collision
check sprite/graphics collision
check for any collision
check for any collision
Note.
The procedure mavesprite starts the motion.
The
COMAL system does not wait for the motion to stop but
continues with the next line in the program.
This
makes it possible to start other sprites in motion,
print messages, etc. Many things can be going on at
the same time.
If you do not want program execution to
continue while the motion is carried out, you can add a
"wait" line.
For example:
WHIL.E movingspriteno
DO NULL
or
WHIL.E NOT datacollisionspriteno>,TRUE) DO NUL.L.
Examples.
Move sprite no 2 to the
point (200,130) in 100
screen updates. Start now
with no collision check.
Move sprite no 0 to the
point (250,-10) in 300
steps. Start now, checking for sprite collisions
and collisions with
graphics drawings.
Chapter
-178-
COMAL PACKAGES
startsprites
is a procedure which initiates the motion of those
sprites which are waiting for the start signal.
See
the movesprite procedure.
stopspritespriteno
is a procedure which stops the, moti'on of the sprite
with the number specified.
movinQspriteno
is a function which takes on the value TRUE (=1) if the
sprite specified moves.
Otherwise the value of
function is FALSE (=0).
Examplel
IF NOT mcvinQ(2) THEN
move5prite(2,O,190,~O,O)
and
spriteyspriteno
-179-
COMAL PACKAGES
"h"+CHR$ .prit.no
"y"+CHR$ ydoubl.
Analogous to
"x"+CHR$xdoubl..
"c"+CHR$ coler
.ction$I-"."+CHR$(l)+"p"+CHR$(lO)+"h"+CHR$(l)+"p"+CHR$(10)
is identical to
.ction$.-"."1"p"10"h"1"p"10""
Ex.mpl_
nill.t. (I, ..... l .. p" 10"h"1 "p"lO"")
lIovprit.(l,100,100,O,O)
WHILE KEV$-CHR$(O) DO NULL
anim.t.'l, .... )
Sprite no 1 moves at
once te the screen
position (100,100) and
flashes for 10 time
units, until any key
is pre1ised.
-180-
COI'IAL. PACKAGES
priarity(6,1)
-181-
COt1AL PACKAGES
5
6
7
8
9
10
11
12
13
14
Is s.t with
visible
TRUE/FALSE
0 15
Multi-colo~1
0 15
Multi-colo~3
0 15
double width
TRUE/FALSE
double height
TRUE/FALSE
Multi-colo~
TRUE/FALSE
g~a./sp~ite p~iority
TRUE/FALSE
d~awing numbe~
O 31
time ~emaining
0 215
sp~ite/sp~ite collision TRUE/FALSE
sprite/g~a.
collision
TRUE/FALSE
mode of motion
0 7
numbe~ of actions
0 32
0 . 32
no. of next action
Multi-colo~2
(01)
(10)
( 11)
o.
nume~ical
hide/showsp~ite
spriteback
sp~itecolo~
spriteback
spr-itesize
sp~itesize
define,identify
p~io~ity
identify
movesp~ite
movesp~ite
movesprite
movespr-ite
animate
animate
values 1 and
-182-
Chapter :5 -
Cot1AL PACKAGES
sprite image
file name)
itself must
in length.
Example,
define<2,drawingS)
saveshape<2,l spO.flower")
loadshapedrawingno),(filenama$
is a procedure which fetches a copy of the file named
(filename$) from diskette or cassette tape.
The file
must have been saved previously using the procedure
.aveshap.. The file (filename$) must contain a
string with the definition of a sprite image. This
drawing will be given the number (drawingno).
Example.
I oadshap. <1, "SpO. f lower ")
linkshap.drawingno
is a procedure which associates a copy of the drawing
indicated with the COMAL program.
When the program is
saved using the order SAVE, the drawing will be saved
with it.
It can be read in later together with the
program with the order LOAD.
If desired, the drawing can be disassociated from the
COMAL program by using the order DISCARD.
The drawing must have been fetched earlier using the
procedure loadshape. This drawing is assigned the
number (drawingno).
ChApter
:s -
-183-
COI'IAL PACKAGES
EKAmplel
-184-
Chapt.,.. 5 -
COMAL PACKAGES
II
c4
11
00
rI
G.,--A5- B5
0
-'I
"tJ~
~
n
',.
1_"\.
J
B3 -(4-
jl
I
1/2
1/4
I /8
1/ 16
1/32
Ch.pt.,. 3 -
-185-
COMAL PACKAGES
d.c.y
The
-186-
Chapt.... :5 -
COI"IAL PACKAGES
FOR voic -l TO 3 00
.oundtyp.evoic.,3)
ENDFOR voice
INPUT AT 7,1. "not.-cod ". cod
FOR voic -l TO 3 DO
PRINT AT 10,11 "voic. "Ivoic.
playevoice,cod )
. pl.yevoic.,"z")
ENDFOR voic.
0:
I
21
31
41
silence
triangular wave
sawtooth wave
square wave
white noise
-187-
Chapt.,. :5 -
COMAL PACKAGES
"c4" ,8, liZ If ,2, "c4" ,B, liZ ",2, uc4 11 ,B, ud4 u ,4
".4",e,"z",e,".4",e,"d4",4,".4",8
"f4",4,"g4",16,"z",e,"c:5",4
"c:5",4,"c:5",4,"g4",4,"g4",4
"g4",4,".4",4,".4",4,".4",4
"c4",4,"c4",4,"c4",4,"z",8,"g4",e
"f4",4,".4",e,"d4",4,"c4",e
RESTORE melody
WHILE NOT EOD DO
READ cod.$,sek'32
play(voice,cod.$)
ENDWHILE
ENDPROC play'melody
fJ
t~
Ia!.
.. ......1
..
,..-::--, 5
'T'
--- ........
,.,.---
~~;J- .-~
w"'J
I.
rs
-188-
CDMAL PACKAGES
Chapte,.
-189-
COMAL PACKAGES
sustain
release
II
1!lfl
I
i
Role...
Ilil
\!,..'!I,I
\
{:: :
III I
I'll
1:II1
1IIIil
'!.!
-l~-
COMAL PACKAGES
no.-O
WHILE NOT EOD DO,
no.+1
READ cod ,Um
ton (no).-fr.qu.ncy(cod )
ads'paus (no).-tim*2
r'pause.(no).-tim*2
ENDWHILE
ton (nr+1).-0
s.tscor.(l,ton () ,ads'paus () ,r'pau (
playscor.(1,0,0)
numberz-O
WHILE NOT waitscor.(l,O,O) DO
number. +1
PRINT number;
ENDWHILE
-191-
Chapte,. !5 -
COMAL PACKAGES
Example
volumeU!5)
note voice>,(code.
is a procedure which is used to indicate the tone
(code$> which the voice with the number (voice>
will play.
The parameter <voice> can be 1. 2 Dr 3;
(code$> is a string with possible values: "cO"
.. cO# ... "dO ....... "a7# .. on machines using the European PAL
standard. On machines using the American NTSC standard
tones up to "b7" can be played.
The letters in each
note code indicate the note, and the number indicates
the octave.
The character I indicates half notes
(sharp notes).
EKample.
noteC2,"d!5")
-192-
CDI'IAL PACKAGES
01 silence
11 triangle waveform
21 sawtooth .. ave
31 square wave
41 white noise
adsrvoice>,<attac~>,<d.cay>.<sustain>,<rel.ase
10:
11 :
12:
13:
14:
15:
<attack>
2 msec
8
16
24
38
56
68
80
100
250
500
800
1 sec
3
5
8
<sustain>
can equal
6 msec:
24
48
72
114
168
204
240
300
750
1.5 sec
2.4
3
9
15
24
0, 1 15
-193-
COMAL PACKAGES
Example.
adsr(1,13,13,B,13)
pI ay.c:ore (1,1,0)
stopplayvoice1>,<voic:e2>,<voic:e3
is a procedure which stops the playing of the voices
indicated.
If <voic:eX> is TRUE (=1), then voice X
stops playing.
Example.
stopplay(O,1,1)
waitscorevoice1>,<voice2>,<voice3
is a function which returns the value TRUE (:1) if the
playing of the indicated voice combination has
finished.
E)(ample.
WHILE NOT waitsc:ore(1,1,O) DO NULL do nothing before
voice 1 and 2
have finished
playing.
frequency c:ode.
is a function which returns the integer value which the
SID chip must receive to play the note.
It is mostly
used to compute table values for the procedure
setscore. The integer value lies between -32768 and
32767 inclusive.
It is NOT possble to transform notes
between octaves directly by dividing these numbers by
2. The parameter (c:ode.> must contain a string with
a valid note code (i.e. one of the codes "cO",osv.).
Example.
frequenc:y ("c4")
setfrequenc:yC(voice>,(frequency'value
is a procedure which is used to define the frequency of
each (voice).
The number <frequency'value> must be
in the range 0 - 65535. These numbers do not
c:orrespond directly to the SID chip frequency codes.
-194-
COMAL PACKAGES
.yncvoice'combination>,<ye.'no
is a procedure which takes care of synchronization with
respect to the <voice'combination> indicated if
<y 'no> equals 1. Otherwize the voice combination
is not synchronized.
voice 'combination
number.
corre.pond. to .ync
between voice
1 and 3
1 and 2
2 and 3
1
2
3
Example
ync<l,l>
filterfreqfrequency'value
is a procedure which is used to determine the cutoff
frequency for the filter.
The parameter
<frequency'value> must be in the range 0 to 2047
inclusive, corresponding to frequencies between about
30 and 12000 Hz.
Example.
filtertypelow>,<band>,<high>,<3'interrupt
is a procedure which is used to select the filter type.
If <low> equals I, then a 'low-pass' filter is
used, da'mping tones in the treble range.
All
frequencies above the filter frequency (set by
filterfreq> are damped 12 dB per octave.
If <band> equals 1, then damping occurs on both
sides of the filter frequency; 6 dB per octave.
If <high> equals 1, then the low frequencies are
damped by 12 dB per octave.
If <3'interrupt> equals 1, then voice 3 will not be
audible.
It can be used to code information about
Chapt.r :5 -
-195-
COMAL PACKAGES
puls.voic.>,<puls.'width
is a procedure which is used to indicate the ratio
between the time during which a square wave is high and
the time during which it is low (the "duty-cycle").
The more this ratio deviates ~rom 1:1, the more "nasal"
and "sharp" the sound will be.
The parameter
<puls.'width> is a number from 0 to 4096 inclusive.
When selected as 2048 the ratio is 1:1.
El<ampl
pul.e(l,204S)
ringmodvoice'combination>,<yes'no
is a procedure which is used to determine whether r1ng
modulation is to be in effect.
The parameter
<voic.'combination> selects which voices are affected
(see sync).
If <yes'no> is TRUE (=1) modulation
will occur; it will not if <y 'no> equals FALSE
(=0)
-196-
COMAL PACKAGES
graphicscr n(O)
volum.(10)
soundtyp. (3, 1)
not. (3, "A4 U )
Adsr(3,13,13,S,13)
gat.(3,O)
Chapter
COMAL PACKAGES
PADDLES
The package paddle. is made available by the order:
USE paddle.
A pair of p~ddles should be attached to a control port.
The
paddles will be refered to as paddle a and paddle b.
Each paddle has a knob, which is used to change the
position of a vari~ble resistor, and a push-button, which
shorts a port input to ground when activated.
-198-
COHAL PACKAGES
and
USE p.ddle.
p.ddle(2,.p.ddl.,bpaddle,.'button,bbutton)
PRINT .p.ddl.,bp.ddle,abutton,bbutton
The signal values are fetched from control port 2 and
printed out in th'e next line.
The following program example, Paddle G.me, is available
on the demo diskette (tape):
0010
0020
0030
0040
00:50
0060
0070
0080
0090
0100
0110
USE paddles
DIM form.t$ OF 40
form.t$.",," 4t4t4t
4t4t4t
4t"
PAGE
INPUT AT 2,11 "control port no > ". portno
DIM winn.r$ OF 1
winn.r$I-"c"
PRINT AT 9,21 "Who c.n .dju.t the p.ddl nd pre "
PRINT AT 10.21 "the fire button the f te.t?"
PRINT AT 13.21 "Pre k.y to .t.rt."
RANDOI'1IiE
WHILE KEY$-CHR$(O) DO NULL
number.-RND(0,2:5:5)
PRINT AT 1:5,2. "The numb.r i.1 ".numb.r
0120
0130
0140
01:50
0160
0170
0180
0190 REPEAT
0200 p.ddl.(portno,.p.ddl bp.ddl button,bbutton)
bp.ddl. b'buttan"
0210 PRINT AT :5,11 ".p.ddl button
0220 PRINT AT 6,1. USING form.t$1 .paddl.,abutton.
bpaddl.,bbutton
0230
0240 IF numb.r-.p.ddle AND .button THEN "'inn.r$I- ...
IF numb.r-bp.ddle AND b'button THEN winner$I-"b"
02:50
0260 UNTIL winn.r$ IN ".b"
-199-
Ch.pt.,. :5 -
COI'IAL PACKAGES
0270
0280 PRINT AT 17,2 .... inner$+" ..... f test!"
JOYSTICKS
The package joy.tick. becomes accessible when you use the
order:
USE joystick.
COI'IAL - number
up
up-left
left
up-right
neutral
do... n-left
ri;ht
down-right
down
In addition there is a push-button on the joystick (the fire
button) which sends a signal to the computer when pressed.
The p.ckage contains. single procedure.
joystick portno>,<direction>,<button
which translates the signals from the joystick to
numerical values for use in programs.
O.
Example.
USE joy.ticks
The signal values are fetched
joystick (2,retning.button) from control port 2 and printed
PRINT direction,button
in the next line.
The program example shows how a joystick can be used to draw:
Chapter :5 0100
0110
0120
0130
0140
01:50
0160
0170
01BO
0190
0200
0210
0220
0230
0240
02:50
0260
0270
02BO
0290
0300
0310
0320
0330
0340
0350
0360
0370
03BO
-200-
COMAL PACKAGES
PAGE
PRINT "JOYSTICK FOR DRAWING"
PRINT
PRINT "Th. joy.tick d.termin drawinlil dir.ction.PRINT "Th. fir. button .... itch color ....
PRINT
PRINT "Pr <STOP) to .top the prolilr .... "
PRINT "Pr <f5) to the dr.wing alil.in,PRINT ".nd <fl) to g.t b~ck to the t.xt."
PRINT
INPUT "Joy.tick in port no. (lor 2) .", portno
IF portno<1 OR portno)2 THEN portnol-2
USE turtle
USE joysticks
IjIr.phic.cr n(l)
background (1)
p.ncolor (5)
LOOP
joy.tick(portno,direction,button)
IF direction THEN
.eth dingdirection-l)*45)
forward (1)
ENDIF
IF button THEN I I ch.ng. color
p.ncolorinq(6)+1) MOD 16)
ENDIF
ENDLOOP
LIGHT PEN
In order to understand how a light pen works, you have to
know something about how the picture on your TV or monitor
screen is formed.
The picture is created by an electron
beam which scans back and forth across the face of the
screen at high speed.
As it scans, the intensity of the
beam changes.
Phosphors on the inside surface of the screen
react to the electron beam by emitting light, thus creating
a visible image.
The picture on the screen is updated 50 or
60 times each second, so the eye doesn't notice this
process.
A light pen contains a photodiode in its tip.
It
can detect variations in the light level striking it.
When the electron beam passes the point on the screen where
the light pen is positioned, it can be illuminated.
If it
is illuminated and a signal is sent to the computer, the
instant when the signal arrives corresponds to a particular
position on the screen.
The light pen should always be con~ect.d to control port 1.
Next make the package lililhtpen accessible with the order:
USE lightpen
The light pen works best when the screen border is dark and
the background is light.
If the program segment listed below does not work right
away, then try adjusting the contrast and intenSity
..
~
Chaptltr :5 -
-201-
COI'1AL PACKAGES
PAGE
USE lightpen
USE .y.tem
textcolor.(0,14,6)
off t(O,O)
REPEAT readpen(x,y,ok) UNTIL ok
PRINT XlY
When you run the altered program and move the llght pen In
and out of the corner, It should now reglster the
coordinates (0,0).
If it does not, you have an idea of the
uncertainty with which the light pen can determine screen
coordinates.
Try refining your calIbration.
Now examine the coordinate range which the llght pen can
measure.
It should extend from (0,0) to about (319,199).
After this initial adjustment, we are ready to tackle some
more challenging tasks.
The first example takes advantage of the fact that the
computer automatically sets some important initial parameter
values whenever the order USE lightpen is invoked.
This
is true, for example, of the time for which the pen must be
held at the same spot on the screen before its position will
be registered (the procedure d.lay).
This is also the
case for the time which must pass from the moment when one
set of coordinates has been found to the time when a new
determination will begin (the procedure timeon).
A
program which is to be used to make drawings on the screen
must be able to determine the coordinates of points very
quickly, so delay and tim.on should be set to small
values.
If accuracy is more important than speed, then
larger values should be used.
Chapter S -
-202-
CDI'tAL PACKABES
PAGE
USE li;htpen
USE ;raphic.
;raphic.creenCO)
border CO)
back;round(14)
pencolor(6)
xoff.-S2, yoff.--S1 I I u.e your own value.
off.etCxoff,yoff)
delay (1)
timeon (1)
REPEAT readpen(x,y,ok) UNTIL ok
movetoCx,y)
LOOP
REPEAT readpenCx,y,ok) UNTIL ok
drawtoCx,y)
ENDLDOP
What effect
PAGE
DIM text.(2S,4) OF 10
DIM name. OF 10, all. OF 40
ZONE 10
11-8
USE .y.t_
textcolor.CO,14,6)
arran;e'word.
USE li;htpen
-203-
Ch.pt.r :5 -
0130
0140
01:50
0160
0170
0180
0190
0200
0210
0220
0230
0240
02:50
0260
0270
0280
0290
0300
0310
0320
0330
0340
0350
0360
0370
0380
0390
0400
0410
0420
Q430
0440
0450
b460
0470
0480
0490
0500
0:510
0520
0530
0540
0:550
0:560
0570
0580
0:590
0600
0610
COt1AL PACKAGES
d.l.y(60)
tilll.on(60)
.ccur.cy (10,2)
xoffl-:52, yoffl--51 I I u your own v.lu
off t(xoff,yoff)
choo 'word.
PROC .rr.nQe'word.
CURSOR 1,1
FOR ii-I TO 5 DO
FOR jl-l TO 3 DO
READ t.xt$ (1, j)
PRINT t.xt$(i,j),
ENDFOR J
PRINT
ENDFOR i
text$(6,1)1-".nd"
PRINT t.xt$(6,1)
PRINT AT 6,11 "Point to words with the light pen."
ENDPROC .rr.nge'word.
PROC choo 'word.
REPEAT
REPEAT r dp.n(x,y,ok) UNTIL ok
IF y<199-(1-1)*8 THEN II from lin. I
nam.$I-text$(lin.(y)-l+1 ,col ullin (x) DIV 10+1)
IF n.m.$<>".nd" THEN .11$1+" "+n.m
PRINT AT 2,11 .11$
ENDIF
WHILE penon DO NULL
UNTIL n.m-".nd"
CURSOR 20,1
ENDPROC choo.e'word.
FUNC line (y)
RETURN (200-y) DIV 8+1
ENDFUNC line
FUNC column(x)
RETURN x DIV 8+1
ENDFUNC column
DATA
DATA
DATA
DATA
DATA
-~-
COMAL PACKAGES
off txoff),<yoff
p.non
r.adp.nC<x),<y),<ok
tim.onC<tim.
d.layC<tim.
accuracyC<dx),<dy
off tC<xoff),<yoff
is a procedure which is used to offset the light pen
coordinate pair so that it agrees with the corrdinates
of the Qraphics screen. This offset can vary from
display to display. Try starting with values such as:
<Hoff) ~ 75 and <yoff) ~-45.
Exampl.1
off t(52,-51)
p.non
is a function which has the value TRUE (-1) if the
pen is touching the screen.
Otherwise p.non equals
FALSE (-0).
d.layC<tim.
is a procedure which i. used to specify the tim. for
which the light pen must be held still on the screen
before the light pen reading will be r.corded. The
light pen must be held still within the limits
specified in the procedure accuracy.
The parameter <tim.) is gIven in 1/bO of a second.
Starting value: <tim.)=10 (i.e. 10lbO = l/b second)
Chapte,.
t i mean
:s -
-205-
CDI'IAL PACKAGES
t1 me
Starting value:
accu,.acydM>,<dy
is a procedure which is used to indicate the size of
the region on the screen within which the light pen
must remain to be considered to be 'at rest'.
The
smaller these values, the more precisely the light pen
must be positioned to obtain a reading.
Initial values: (dM)=4
and
(dy>=2
Example.
accuracy (10,S)
Chapter :5 -
-205-
COMAL PACKAGES
setrecorddaiayduration
.etpageinteger
teKtcolorsborder),(background),<teKt
is a procedure which is used to define the color
combination of the border, background and text.
On
start-up teKtcolor. (14,6,14) is executed
automatically on a Commodore 64. On an SX-64
teKtcolors(3,1,6) is the default value.
EKamples.
black border, red background
and subsequent white text
teKtcQlors(12,ll,15)
grey tones
Only the background is
changed (in this case to
green)
keywords'in'upper'ca.e(TRUE or FALSE)
is a procedure which determines whether keywords are to
be written in upper case (TRUE) or lower case (FALSE).
The default is TRUE.
ChApt.,.
-207-
COMAL PACKA6ES
Keywords are
displayed in a
listing with small
letters.
nAm 'in'upp.,.'c& (TRUE or FALSE)
is a procedure which determines whether names are to be
written in upper case (TRUE) or not (FALSE). The
default is FALSE.
EK&mpl.:
n&m 'in'upp.r'c (TRUE)
quot.'mod.(TRUE or FALSE)
is a procedure which determines whether control codes
and other invisible ASCII characters in string
constants are to be displayed in reverse te"t (TRUE) Dr
with their ASCII values enclosed in quotes (FALSE).
After start-up the default is FALSE.
EK&mplellil
PRINT statement afte,- quote 'mode <TRUE):
PRINT 'GHello!"
after quot.'mod.(FALSE):
PRINT .... 2 .. H.llo! ..
ink.y*'
PRINT ink.y*'
ttim.tim.'of'd&y$
is.a procedure which is used to set the clock in the
computer (CIA#l real time clock).
On start-up the
clock is zeroed by ttim.("OO.OO.Oo.o"),
The format of the tim.'of'day$ string is:
-208hh
mm
ss
t
hhlmml t
or
hhlmml
or
hhlmm
or
hh
is
is
is
is
COMAL PACKAGES
(0
24)
the hour
59)
the minute (0
59)
the second
<0
tenths of a second
<0
9)
ttim.("0713011S")
tti me ("10.20")
ttim.("O")
o.
Qettime.
is a function which returns the time'of'day in the
format hh:mm:ss.t
E><&mpl 1
PR I NT Qet ti m
digital clock:
PAGE
USE .yst.m
LOOP
PRINT AT 1,30: Qettime$,
ENDLOOP
QRt.cre.nscreen$
is a procedure which takes a copy of the current te>:t
screen, and saves lt as the string screen$. The
string .cr n$ takes up 1505 characters. This is
reserved by using the order DIM screen$ elF IS0S.
The content of the string screen$(1:150S):
screen$(1)
border color
baggr. color
cursor color
4
cursor: line
cursor: column - 1
5
6: 1505 text and color
information
2
3
-209-
Chapt.r S -
DIM .$ of lS0S, b$ of
USE .y.t.m
lS0~
fiI.t.cr n(b$)
.$1-.$ <1:72S) +b$ (726: lS0S)'
t.cr n(.$)
Not
At two selected times during the execution of the
program, the contents of the text screens are saved in
the strings .$ and b$ respectively.
Later a string
is created by combining the first 725 characters of
.$ (i.e. color and cursor information, and the first
12 lines of the .$ screen image) and of b$'s last
780 characters (i.e. the b$ scree~'s lower 13 lines).
The combined image is finally presented on the screen.
Profilr.m .K.mpl. 2:
PROC h.lp CLOSED
DIM .1$ OF lS0S,B2$ OF lS0S
USE syst.m
fiI.t.cr n(.l$) I I VR .cr n im.fiI.
OPEN FILE 10, ".km.h.lp",READ
READ FILE 10:.2$
CLOSE FILE 10
t.cr n(.2$) I I show. a u r h.lp .cr n
WHILE KEY$-CHR$(O) DO NULL
t.cr n(.l$) I I the old im.g. b.ck .fiI.in
ENDPROC h.lp
h.rdcopyunit$
is a procedure which prints out the contents of the
teKt screen to the unit which 'is given in the string
unitS. The printout begins with a carriage return.
-210currow
and
CCMAL. PACKAGES
curcol
are two functions which return the current row and the
current column respectively.
Examples.
free
is a function which returns the number of free bytes in
working memory. A more complete overview of the use of
working memory is obtainable using the command SIZE.
But because SIZE is a command, it cannot be used from a
running program.
E;:xampla.
PRINT free
defkeyno),(text.
is a procedure which is used to redefine the meaning of
the functIon keys.
The keys are numbered 1, ,8,11, . 18.
The numbers 1 - 8 are normally active for indication of
the usual function keys (fl) - (f8).
But during
programexecution, the function keys will correspond to
numbers 11 - 18.
The string text. may consist of a
maximum of 32 characters.
The procedure showk.y. will print out a list of the
current definitions of the function keys.
-211-
COI'IAL PACKAGES
AUTO
0010
0020 PROC
<f4) will cause:
XXXX ENDPROC
(Interrupt AUTO-numbering.)
SCAN
(Which checks the structure of the
procedure and allows use of the
procedure as a command.)
Program example.
USE .y.tem
defkey (1:5, "COI'IAL for everyone!" 13"")
INPUT "What did you .ay? ": text$
PRINT text$
If the <f5) key is act.ivated in response to the INPUT
statement, the system will react as if the message came
from the keyboard and print it out.
sho",key.
is a procedure which prints out a list of the meanings
of the function keys
erial(TRUE or FALSE)
is a procedure whi ch control s whether communi cati or. 1 s
sent to the serial port or to the IEEE-488 module <if
available)
Example
erial(TRUE)
rial(FALSE)
etprinterattribute.$
is a procedure which is used to select the unit number
and attributes of the peripheral printer.
Printout to
the lineprinter (lpl) will thereafter be performed
according to the rules given by the attributes.
These
are given in a string during procedure calls.
Po ible printer attribute.:
lala+
1111+
Chapt.r :5 -
-212-
COMAL PACKAGES
1.1.0
lsi
la2
la3
la4
laS
la6
Is7
etprinter("lpc/a+/I-")
Ch .. pt....
-213-
COI'IAL PACKAGES
read/write
read/write
read only
read only
2~~
pok. 1024+1,1
-214-
COMAL PACKAGES
USE font
DIM ra.ter$ OF 8
'PAGE
INPUT "Char-.cter .et
": choice.
INPUT "Character no.
": character"
PRINT
getcharacter(choice.,character.,ra.ter$)
FOR il=l TO 8 DO
PRINT TAB(12),bin$(ORD(ra.ter$(i)
ENDFOR i
FUNC bin$(numb.r) CLOSED
DIM binnumber$ OF 8
binnumber$:="OOOOOOOO"
bit:"l
FOR i.I~8 TO 1 STEP -1 DO
IF numbar BITAND bit THEN binnumber$U.):="l"
bit:+bit
ENDFOR i .
RETURN binnumber$
ENDFUNC bin$
-215-
COMAL PACKAGES
0010
0020
0030
0040
0050
0060
0070
0080
0090
0100
0110
0120
0130
0140
0150
0160
0170
0180
0190
0200
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
%00000000
%00000000
%00111110
%01101110
%01111110
%01110110
%01111100
%00000000
use font
1 oadfont ("(fi lenameS)")
-216-
COMAL PACKAGES
linkfant
laadfantfil.nam.S
.av.fantfil.nam.S
k.epfant
Qetcharact.rcharact.r t),<charact.r),<ra.ter$
putcharact.rcharact.r t),<charact.r),<ra.t.rS
Chapter :5 -
-217-
COI'1AL PACKAGES
gatcharactercharacter .et>,<character>,<raster$
is a procedure which fetches a raster image of the
character with the screen code <character> from
<character 'set>.
The image is fetched in the form of
a string variable <raster$) which is 8 characters
long.
Permitted values:
<character .et.>: O. 1, 2 og 3
<characters> : 0, 1 ... , 255
Example.1
getcharacter (3, l,raster$)
The character a is
fetched from character
set 3.
DIM a$ OF B
USE font
getcharacter(2,4,a$)
PRINT a$
Printout: XLFFFLX
putcharact.rcharact.r set),<character),<raster$
is a procedure which prints out the character with the
screen code <character) in <character'.et) with the
raster pattern in the string <ra.ter).
-218-
CDI'IAL PACKAGES
Allowed values:
<chArActer.et>: O. 1
<chArActer>
: O. 1 255
EXAmple
is
-219-
CMAPt._.COMAL.
F:i.l. _ _
WHAT IS A FILE?
As you begin to use your
computer to do more and more
jobs for you, it will be very
convenient to be able to create
files for storing information.
You may wish to save business
transactions, financial records,
address lists, the results of
calculations, measurements or
other data for later use.
Of
course it is possible to
purchase commercially produced
"database" software to help you
do this.
Nevertheless many
computer owners elect to write
their own programs, so that they
can tailor the," precisely to
their particular needs.
A file is a collection of data, organized for storage and
retrieval.
The storage medium can be a Datassette tape
or a diskette.
Because serious file handling usually
requires the use of a disk drive, thiS chapter will
concentrate mostly on file storage with a dlSk drive.
Chapt.,. 6 -
*
*
*
*
-220-
COMAL. FIL.ES
PROGRAM SEGMENTS
LIST(segment)"<file name>"
SAVE "testfile
LIST "testfile"
MERGE(line)"(file name)1I
ENTER "<file name)"
LOAD "testfile"
ENTER "testfile"
MERGE
MERGE
MERGE
ENTER
STORAGE:
fl
RETRIEVAL:
"printout
1100 "printoutll
1100.5 "printout
"printoutll
ll
lJ
-221-
COMAL FILES
*0
Ch ... pt.r 6 -
-222-
COI1AL FILES
-------------------------------------------------*
Chapter 6 -
-223-
COMAL FILES
string variable
John Smith
1200 Wilson Driva
Any town , PA 19380
(212) 123-4567
name$ ()
street$ ()
town$ ()
phone$ ()
*
*
*
*
*
*
-224-
COMAL FILES
PAGE
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
-2250160
0170
0180
0190
0200
0210
0220
0230
COMAL FILES
city"
PRINT "
PRINT"
phonenumb.r"
PRINT
PRINT
PRINT "Pr any k.y to continu "
wait'for'key.trok.
The statement PAGE clears the screen and the following lines
simply print information on the screeen.
Notice the
procedure wait'for'keystroke. This is a procedure which
you might find convenient to use in your own programs:
2240 PROC wait 'for 'keystroke
2250
PRINT
2260
PRINT "< > " I
REPEAT
2270
reply$:zKEY$
2280
2290
UNTIL r.ply$<>CHR$(O)
PRINT AT 0,21 r.ply$
2300
2310 ENDPROC wait'for'keystroke
You mayor may not want < > to be printed on the screen
whenever the computer is awaiting an operator response.
Change it or delete it as you wish.
The REPEAT .. UNTIL loop
will be executed continuously as long as no key is
depressed, since the value of the CDMAL function KEY:f rema.In
equal to CHR:f(O).
When a key is pressed, KEY$ takes on the
value of the character sent from the keyboard, and reply$
will no longer be equal to CHR$(O).
The REPEAT UNTIL loop
will be terminated, and execution proceeds to the next lIne,
The PRINT AT 0,2: reply$ statement causes the character
which was sent from the keyboard to appear Inside the
brackets in the < > symbol,
Now take a look at the main program loop:
0240 LOOP
0230
show'm.nu
0260
flag$I-""
0270
wait'for'k.ystroke
0280
CASE r.ply. OF
0290
WHEN "1"
load 'file
0300
0310
WHEN 11211
cr.ate'record
0320
0330
WHEN 113 11
0340
li.t 'file
0330
WHEN 114 1
.earch'file
0360
0370
WHEN ~
.. ..
-22603S0
0390
0400
0410
0420
0430
0440
04~0
0460
0470
04S0
0490
COMAL FILES
.ort'fil.
WHEN "6"
ch.ng.'r.cord
WHEN "7"
d.l.t.'r.cord
WHEN "s"
v.'fil.
OTHERWISE
PRINT "Ill.g.l r.ply ....
w.it'for'k.y.trok.
ENDCASE
ENDLOOP
0520
0530
0540
O~~O
0560
0570
0~80
0~90
0600
0610
0620
0630
0640
0650
0660
0670
0680
0690
0700
PAGE
PRINT ..
MAIN MENU -=="'..----- ..
PRINT
PRINT
the fil."
<1> LOAD
PRINT "
<2> CREATE r.cord"
PRINT "
<3> LIST
the fil."
PRINT "
<4> SEARCH the fil."
PRINT "
<~> SORT
.lph.b.tic.lly"
PRINT "
PRINT ..
<6> CHANGE r.cord"
PRINT ..
<7> DELETE record"
PRINT ..
r.vi d fil."
<8> SAVE
PRINT
PRINT
PRINT "R.cord.: "Inumb.r
IF numb.r-O THEN fl.g$;"Pl s. lo.d or cr te fil ......
PRINT
PRINT fl.g.
ENDPROC .how'm.nu
------=-==
-227 -
COMAL FILES
0710
0720 PROC load 'file
OPEN FILE 1, .. Addr ....... ,READ
0730
0740
INPUT FILE 11 numb.r
07!50
FOR nOI-l TO numb.r DO
0760
INPUT FILE 11 nam.$(no)
0770
INPUT FILE 11 .tr t$(no)
INPUT FILE 11 city$(no)
0780
0790
INPUT FILE 11 phon.$(no)
0800
ENDFOR no
0810
CLOSE FILE 1
0820 ENDPROC load'fil.
0830
Of course this procedure can only be used after a file has
been created and is available on the diskette in the disk
drive.
Usually this will be first choice a user makes after
starting the program.
It is important to understand the
procedure load'fil., for it shows you how to read a
sequential file from a diskette into program memory.
The
first thing done in this procedure is to OPEN FILE number
1 as a READ file called Addr.s.es. Should you want to
call the file some other name, you can simply alter th1s
file name to one of your choice, here and elsewhere in the
program.
The easiest way to do this is by means of the
CHANGE order (chang . . . Addr.sses ..... your choice").
We have decided to let the first element in the file be
called number corresponding to the number of records 1n
the file.
This variable was the first one to be saved, and
it 1S the first one to be read in now.
Now that the number
of records in the file is known, the file itself can be read
in using a simple FOR . ENDFOR loop.
(In Chapter 3 we used
the logical function EOFfileno to announce End Of File).
Notice the use of the INPUT FILE statement to define the
elements in the arrays name$(), .tr.etS(). townS() and
phon.S().
Finally. notice that file 1 must be CLOSEd
after the data input 1S completed.
The following procedure, activated by user choice 2. can
be used to create new records for the file:
",
",
-228-
COHAL FILES
Ch.pt.,. 6 -
-229-
COMAL FILES
-230-
COMAL FILES
The FOR ENDFOR loop is carried out for each pair of names
in the list. If the names are not in alphabetical order the
names are swapped. The variable no'swap will now be equal
to FALSE, if a swap has occured. The REPEAT UNTIL
no'swap-TRUE loop is repeated until no two names are
swapped on a pass through the list . The bubble sort is not
the most efficient sorting technique, but it is perhaps the
easiest to understand. On the demo diskette you will find a
quick'sort procedure which is much more efficient but
harder to understand.
It will sometimes be necessary to change the contents of a
record in the file. This choice is activated by selecting
6 from the menu.
The procedure chanQe'record is shown
below:
1610 PROC chanQe'record
1620
PAGE
1630
PRINT "1'1 I I I CHANGE A RECORD I I I I I "
1640
PRINT
16:50
PRINT
INPUT "Which record number? "I no
1660
1670
IF no(-number THEN
print 'record (no)
1680
INPUT AT 14,11 "RiQht record? (yIn)? "I reply$
1690
1700
PRINT
1710
PRINT
IF reply$ IN "yV" THEN
1720
INPUT "Name
I "I name$(no)
1730
1740
INPUT "Street
" treet$(no)
INPUT "City
"I city$(no)
17:50
1760
INPUT "Phone
". phone$(no)
1770
ENDIF
ELSE
1780
1790
flaQ$I-"There are only "+STR$(number)+" records"
1800
ENDIF
1810 ENDPROC chanQe'record
1820
The procedure should be easy to follow.
It involves simply
requesting the user to indicate which item is to be changed
then allowing the change to be entered. Notice again the
use of the variable flaQ$ to transmit an error message to
the menu.
Selecting user option 7 from the menu allows a record to
be deleted.
A procedure which can accomplish this
function is as follows:
1830 PROC delete'record
1840
PAGE
PRINT "11111 DELETE A RECORD 11111"
18:50
1860
PRINT
1870
PRINT
1880
INPUT "Which record number? "I record
1890
IF reccrd>number THEN
1900
flaQ$I-"Use a smaller record number!"
-231-
COMAL FILES
1910
ELSE
print'r.cord(r.cord)
1920
1930
PRINT
INPUT "I. thi. the riQht r.cord (yIn)? "I r.ply$
1940
19:50
PRINT
1960
IF r.ply$ IN "yY" THEN
1970
FOR nOI-r.cord TO numb.r-l DO
nam.$(no)l-nam.$(no+l)
1980
1990
.trt$(no):-.tr t$(no+l)
city$(no)l-city$(no+l)
2000
phon.$(no)l-phon.$(no+l)
2010
ENDFOR no
2020
numb.rl-l
2030
2040
ENDIF
20:50
ENDIF
2060 ENDPROC d.l.t.'r.cord
2070
After a file has been entered. sorted or modified. it will
usually be desirable to save it for later use. Choose user
option 8 from the menu to activate the following
procedure:
.av.
2080 PROC
'file
2090
PAGE
PRINT "11111 SAVING FILE TO DISK
2100
OPEN FILE 1 ....Addr ....... ,WRITE
2110
PRINT FILE 11 STR$(numb.r)
2120
2130
PRINT
PRINT
2140
21:50
FOR nOl m 1 TO numb.r DO
2160
PRINT FILE 11 nam.$(no)
2170
PRINT FILE 11 .tr t$(no)
2190
PRINT FILE 11 city$(no)
PRINT FILE 11 phon.$(no)
2190
2200
ENDFOR no
2210
CLOSE FILE 1
2220 ENDPROC .ave'file
2230
Ch.pt.r 6 -
-232-
COHAL FILES
0060
0070
0090
0090
0100
0110
0120
0130
0140
01~0
0160
0170
0190
0190
0200
0210
0220
0230
0240
02~0
PAGE
PRINT "III RANDOH FILE DEHONSTRATION III"
PRINT
PRINT
PRINT "Thi. proQr.m illu.tr.t imply "
PRINT "po ibl. how you c.n .tor nd r.tri.v."
PRINT "inform.tion from. dir.c:t or"
PRINT .. r.ndom-.cc fil "
PRINT
PRINT "Thi x.mpl rv to v nd r.tri.v."
PRINT "inform.tion .bout p.rt. inv.ntory"
PRINT
PRINT "Th. information.n i rr.nQ.dl ..
PRINT
PRINT AT O,SI "cod. numb.r"
PRINT AT O,SI "p.rt n.m."
PRINT AT O,S. "qu.ntity"
PRINT AT O,S. "pric."
PRINT
PRINT "Pr s .ny k.y < > "
w.itfork.y.trok.
0260
After this introduction the program will proceed to the main
program loop as soon as the user presses a key. We have
used the same w.it'for'k.y.trok. procedure as in the
previous program.
0270 REPEAT
.how ...nu
0290
IF r.ply.-"l" THEN cr t.r.cord
0290
0300
IF r.ply$-"2" THEN f.tc:hr.c:ord
0310 UNTIL r.ply.-"3"
0320
The main loop displays a menu then diverts execution to
c:rt.'r.cord or to f.tchr.c:ard in response to a valid
user response to the menu:
-233-
COI1AL FILES
11111"
04~0
-234-
COMAL FILES
-235-
COMAL FILES
PAGE
DIM name$ OF 40
INPUT "Enter file namel ":name$
OPEN FILE 2,name$,READ
DIM numberS OF SOOO
WHILE NOT EOF(2) 00
number$I+GET$(2,1000)
ENDWHIL.E
CLOSE FILE 2
PRINT numberS
PRINT "Switch di.k.ttes and pr any k.y .....
dummy$-KEY$
WHILE KEV$-CHR$(O) 00 NULL
OPEN FILE 3,".01 "+nam.$,WRITE
PRINT FILE 31 numberS
CLOSE FILE 3
-236-
Chapter b -
COMAL FILES
FILE TYPES
You have noticed that when you view the contents of the
diskette using the dir order that different types of files
are stored.
At the right next to the file name you will see
a three-letter abbreviation describing the file type:
prg
seq
reI
usr
program file
sequential file
relative file
user sequential file
txt. letter
1st.
dsp.
obj.
src.
ext.
bas.
txt.
grO.
gr!.
spO.
spl
prc
fnt.
for
for
for
for
for
for
for
for
LISTed files
DISPLAYed files
object code files
source code files
EXTERNAL procedures
Basic programs
text files
graphics screen files
-237 -
CO MAL FILES
lpi
sp,
cs,
u<d.vic.>1
<drive>.
keyboard
display screen
line printer
serial port
cassette recorder
device <such as Printer-Plotter)
disk drive number <default 0)
-238-
COMAL FILES
USING THE
1~20
PRINTER-PLOTTER
REVIEW
In this chapter several important topics
pertaining to the use of CO MAL files have been
covered:
*
*
*
*
*
*
Chapter 6 -
-239-
COI'IAL FILES
file
stora;e _dium
sequenti al f He
random-access (direct) file
record
data element
bubble sort
file types
device specifications
The followin; CO MAL instructions have been discussedl
SAVE - LOAD
LIST - ENTER - MERGE
OPEN FILE - CLOSE FILE
PRINT FILE
INPUT FILE
WRITE FILE - READ FILE
RANDOM
CREATE
GETS
In addition to the examples of the use of files shown in
this chapter, you may find it helpful to study details on
the formal syntax of these orders in Chapter 4.
The following programs have been discussed in this
chapter. They are also to be found on the demo diskette:
Addr List Demo
Random File Demo
Move Sequential
1:520 Plotter Dem
-240-
-241-
PERXPHERAL
DEVXCES
I NTRODUCTI ON
-242-
PERIPHERALS
start
bit
:~o
parity
bit
stop
bit
MSB \
lSB
iT
3T
,
6T
7T
8T
lOT
1 1
9T
o ooo
Ch .. pt .... 7
pin
1
3
4
5
6
7
8
9-17
18
19
20
21-25
-243-
PERIPHERALS
sign .. l
cod.
protective ground
transmitted data
received data
request to send
clear to send
data set ready
signal ground
carrier detect
not used .
ring indicator
not used
data terminal ready
not used
GND
SOUT
SIN
RTS
CTS
DSR
GND
OCD
..
..
..
. ..
RI
..
. ..
DTR
,.
_14
2_
-15 3_
_16
4_
-17 5_
6_
7-
-18
e19
e20
e21
-22
-23
e24
-25
8-
S10_
1,.
1213_
A
B
C
0
E
F
G
K
L
M
RS-232C .ignal
d c ... iption
GNO
SIN
SIN
RTC
OTR
RI
DCO
CTS
DSR
SOUT
GND
Signal
di ....ction
input
input
output
output
input
input
input
input
output
DB-25 .tanda... d
connections
~
.j
3
4
20
18
8
5
6
~
- 244-
Chapter 7 -
23456
PERIPHERALS
10 11 12
:::::::::::::
ABC
H J
K L M N
--------------------------------------------------
@
-~
ayntalC
range
default
baud rate
data bits
stop bits
parity
b<baud>
d<num>
s<num>
p<type>
50-2400
5-8
0-2
n=none
e=even
o=odd
b300
d7
s2
pn
-245 -
PERIPHERALS
"sp:"
300 baud, 7 data bits, 2 stop bits, no parity bit
"sp:b600"
600 baud
"sp:b1200dBslpe" 1200 baud, B data bits, 1 stop bit, even parity
Notice that the serial channel will remain open and the
program continues to execute the REPEAT-UNTIL loop in lines
30-50 until a transmitted character code corresponds to
decimal 255 or any key is pressed.
Data transmission files are opened in the same way, using
WRITE instead of READ.
Notice also that an RS-232C interface file which has been OPENed must of course be CLOSEd
again as soon as possible.
It is not possible to use the
tape recorder or the IEEE serial bus (i.e. the disk drive)
while the RS-232C interface is in operation.
Thus data
which is received must be stored in program memory as it
enters the RS-232C port and then saved to disk later.
Similary, you must prepare a data file in working memory,
OPEN the RS-232C file, send the data, then CLOSE it before
using the disk drive.
IEEE CARTRIDGES
It is possible to purchase a variety of IEEE interface
modules which attach to the Commodore 64 cartridge port.
Such devices are available from your Commodore dealer (ask
for the IEEE 4BB cartridge) as well as from other suppliers.
One of these is called the Bus Card II and is
available from the company Batteries Included.
These
cartridges can be used with your COMAL language cartridge,
for the cartridge bus is accessible in these products.
The
IEEE cartridge is inserted in the cartridge port, then your
COMAL cartridge can then be inserted in a slot in the IEEE
cartridge.
The main advantage of the extra IEEE cartridge is that you
can then use your Commodore to communicate with high
capacity, high speed disk drive units like the Commodore CBM
B050 and B250 devices.
If you have access to other cartridges such as game
cartridges, spreadsheets and the like, you must remove your
COMAL cartridge in order to use them.
In that case be
careful to TURN OFF THE POWER to all units in' your system
before switching cartridges.
-246-
PERIPHERALS
1 (or Al
2
C
0
E
F
H
J
K
.ignal
ground
+5 vdc
port B
port B
-port B
port B
port B
port B
port B
port B
~
~
'~
Chapter 7 -
-~-
PERIPHERALS
56577
56579
heXAdecimAl
$DD01
$DD03
-248-
PERIPHERALS
H.
Line 10 indicates the name of the file.
In line 30 the
screen is cleared by PAGE.
Lines 40-90 print the following
message on the screen:
ELECTRIC TRAIN DEMO
Your tr.in .hould .t.rt . t the station
with the p ; . d.tector just behind
the l t c.r. St.rt the tr.in .nd then
pre.s .ny key to tUrn control over
to your computer
Notice line 100:
0100 WHILE KEY$cCHR$(O) DO NULL
These instructions keep the program in a loop until any key
is pressed.
The system variable KEY$ will then be different
from the null string CHR$(O) , and the program will continue.
The main program starts in line 200.
The procedure
defina'v.riable. in line 220 defines the addresses of port
B and its ddr, and the initial value of the variable
position is set to 1.
Note that COMAL allows the
programmer to indicate the port Band ddr addresses directly
in.hexadecimal form.
Hexadecimal numbers are preceeded by
$. Note also the convenient variable names:
0680 PROC define'variables
0690
port'bl-$ddOl
port'b'ddr:c$dd03
0700
0710
positionl-1
0720 ENDPROC define'v.ri.ble.
0730
The apostrophes ' are necessary to bind the individual
words together, so that CC:IMAL wi 11 interpret them as a
single variable name, just as with procedure names.
The
variable position will be used to control a pointer on the
screen display, indicating the action of the program.
The procedure in line 230 sets port b.
follows:
This is done as
-249-
PERIPHERALS
II ) "
>
train running
train passes light
train waiting at station
Pressing any key will stop the train
next time it stops at the station
REPEAT
check 'light
delay(l.Sl
atop 'train
delay(lOl
start 'train
UNTIL KEY$(>CHR$(Ol
stop 'train
PAGE
END "Au revoir!"
0~40
PERIPHERALS
<>
1 DO NULL
-251-
PERIPHERALS
3
4
5
6
7
8
QAme port A
Qame port B
JOYAO
JOYAl
JOYA2
JOYA3
POTAY
BUTTON A
JOYBO
JOYBl
JOYB2
JOYB3
POTBY
BUTTON B
+ 5V
+ 5V
GROUND
POTAX
GROUND
POTBX
BUTTON
total
the
POTBY.
5
0
Chapter 7 0010
0020
0030
0040
00:50
0060
0070
OOBO
0090
0100
0110
0120
0130
0140
01:50
0160
0170
01BO
0190
0200
0210
0220
0230
0240
0250
0260
0270
02BO
0290
0300
0310
0320
0330
0340
0350
0360
0370
03BO
-252 -
PERIPHERALS
II save ".Thermometer"
USE paddle.
II capacitor: 1000 pF
II thermistor: 100 K at 20 degree.
al-1, bl-O
PAGE
PRINT "DIGITAL THERMOMETER"
PRINT AT :5,11 "Thermistor and capaCitor must be con-"
PRINT "nected to control port 1 "
II Main program
LOOP
check 'paddle (1)
convert (average)
print 'temperature
EXIT WHEN KEV$<>''''
ENDLOOP
END II hovedprogram
PROC check'paddle(port)
totall-O
FOR il-l TO :50 DO
paddle(port,a'paddle,b'paddle,a'button,b'button)
total Imtotal+a 'paddle
END FOR i
average:-total/:50
ENDPROC check'paddle
PROC convert(average)
templ-a*average+b
ENDPROC convert
PROC print'temperature
templ-INT(temp*10)/10
PRINT AT 10,101 "T PRINT AT 10,141 temp
ENDPROC print'temperature
0"
-253-
PERIPHERALS
(7)
+5
VOLT
(9) POTAX
C (CA. 1000 pF)
(8) GND
Figure 7.4: Many different sensor types can be attached to
the control ports.
Vou can make use of up to 4
analogue inputs to the two control ports.
If the program is now run, the measured ADC values will be
shown on the screen.
Draw a graph displaying the
temperature in degrees as a function of the ADC values.
If
the graph is approximately linear in the region of
interest, you can compute the constants a and b as
follows:
Read two coordinate pairs from your graph (Xl.V1) and
(X2,V2).
(Xl and X2 correspond to ADC values, and Vl and V2
correspond to temperatures.)
The constant a can now be
found using the formula:
-254-
PERIPHERALS
REVIEW
In this chapter 'we have considered a
range of possibilities for the use of
the wealth of interfacing facilities
available with your Commodore 64
computer.
You are encouraged to
experiment with the RS-232C interface,
the parallel port and the game ports to
learn more about them.
You will find more information about these ports in the
Commodore 64 Programmer". Reference Guide.
A great deal
additional information is also available from the popular
literature about microcomputers.
- FF-
COMAL
AND
MACHINE
LANGUAGE
language programming.
The material presented here 1S
substantially more difficult than the material in orevious
chapters.
If you want to learn more about 6502 mach1ne
language, a number of excellent books are available.
You
might want to begin with Ian Sinclair s Introducing
Commodore 64 Machine Code iGranada Publishing. London.
1984).
The Programmer'. Reference Guide ava1lable from
your Commodore dealer is also a valuable resource .
Machine language will probably be easier to learn if you can
share the learning experlence with others who have slmilar
interests.
In this connection the many Commodore 64 and
Commodore COMAL users groups can provide useful
opportunities of exchange of information.
Here are some
addresses which may be helpful:
In the USA:
-256-
MACHINE LANGUAGE
-257-
MACHINE LANGUAGE
MODULES
The LINK command fetches a machine language module (object
file) from the library which has been prepared by the
assembler.
This module contains information which specifies
where the machine code is to be located in memory.
CO MAL
can control up to 10 such modules at anyone time.
At least
2 modules, containing the following, are always defined:
(Module 2)
(Module 1 )
english
d .. nsk
system
gr .. phics
turtle
sprite.
font
sound
joysticks
p .. ddl
lightpen
package 1 -
module 2
modui e :. -
- proc 2 -
header 1
body 1
header 2
body 2
module n
package m -
package 2
- proc 1 -
proc 3 -
header 3
body 3
proc p ..
header p
body p
-258-
MACHINE LANGUAGE
SIGNALS
When COMAL carries out an operation which can affect modules
or packages, a signal is issued regarding the operation in
question.
The module or package mayor may not react to the
signal. There are two types of signals:
-259-
Chapter 8 C64
RAM
C64
Ports
C64
ROM
MACHINE LANGUAGE
641(
GRAPHICS
KERNEL
561(
COMAL
1/0
CHAR
521<
COMAL
481(
Free
for usr
401( packages
321(
BASIC'
COMAL
~pagel
COMAL
page2
COMAL
page3
COMAL
page4
Expan- Expansion
sion
page5 page6
Working
memory
01(
RAM is partitioned a. follows.
0- 1KB
1- 2KB
2-32KB
32-48KB
48-52KB
52-56KB
56-64K8
BASIC interpreter
Standard double character set (font)
KERNEL. This is the Commodore 64's operating
system.
It contains among other things routines
for communication with the screen, cassette tape,
disk drives and the R5232 interface.
-260-
Chapter B -
MACHINE LANGUAGE
161<8
32KB
OVRLAY
By
-261-
MACHINE LANGUAGE
RAMCHR
RAMIO
DEFPAG
CBASIC
CART i
641<
RAM
RAM
RAM
KERNEL
KERNEL
KERNEL
RAM
CHAR
110
110
110
110
RAM
RAM
RAM
RAM
RAM
RAM
RAM
RAM
RAM
RAM
56K
52K
48K
BASIC
COMAL
pagei
40K
RAM
321'
RAM
OK .....-
RAM
RAM
RAM
RAM
RAM
........
CREATING MODULES
In order for LINK. USE and DISCARD to work, the placement of
code and the format for packaoe names. procedures and
procedure headers must be specified.
If a module is to be placed in RAM. then it must have the
following format:
and
.lib c64symb
*-<.tart addrass>
.byta <map>
.word and
."ord <.ililnal>
<packalila tabla>
<machina coda>
.and
and
.lib c64aymb
*-<.tart addra >
"ord' cold
.MOrd "arm
.byta 'CBMBOcomal'
.byta >*
.byta <map>+rommad
."ord and
.MOrd <aililnal>
<packaga tabla>
<machina coda>
and
Chapt.,..
e -
-262-
MACHINE LANGUAGE
.lib c64.ymb
procti
initi
ll,'procl'
proehl
12, 'proc2'
proch2
.byt. 0
;End of the
tabl~
-263practi
IJ
'praeJ'
prachJ
is
is
is
is
the
the
the
the
MACHINE LANGUAGE
(paramRterk>
is
is
is
is
the
the
the
the
value+type
value+array+type,dim
ref+type
ref+array+type,dim
type
dim
real
int
str
.byte func+real,(pip,>pip,3
value+real
.byte
.byte
value+int
.byte
ref+array+str,2
.byte endfnc
y*
REF z$ (, )
No more parameters
ChApt.r
a -
-264-
MACHINE LANGUAGE
PARAMETER PASSING
When the COMAL interpreter passes control to an assembler
coded routine, all actual parameters (if any) are computed.
At the same time parameter types are checked for agreement
Nith the procedure header specification. The number of
parameters in the procedure call must also be correct.
It is not possible to know in advance where the parameter
value or the variable (when using REF) are located in
storage.
Therefore it is necessary to call a system routine
FNDPAR (FiND PARameter) to obtain information about the
storage address of a parameter. Then the parameter can be
handled.
FNDPAR:
NB:
In the CO MAL system the following conventions apply:
integers and real numbers are stored in high/low format,
while addresses are saved in low/high format.
This is true
of actual parameters, also for parameters of system
routines.
In the following the format for each parameter type is
described:
VALUE+REAL And REF+REAL
(COPY1) +0:
+1 :
+2:
+3:
+4:
5 bytes
floating
point
Exponent+128
Mantissa(1)
Mantissa(2)
Mantissa(3)
Mantissa(4)
2 bytes
integer
High byte
Low byte
Chapt.,.. B -
-265-
(COPY1) +0:
High byte
Low byte
+2:
High byte
Low byte
+4:
m
bytes
I'tACHINE LANGUAGE
s$ ( 1: 1 )
s$(2:2)
s$(3:3)
s$(4:4)
+4+n-l:
+4+m-l:
s$(m:m)
VALUE+ARRAY+REAL,VALUE+ARRAY+INT, VALUE+ARRAY+STR,
REF+ARRAY+REAL,
REF+ARRAY+INT,
REF+ARRAY+STR
Every array has an information block:
n
Number of indices.
addr: Address of first element in the table.
(COPY1) +0:
+2:
addr
n
Low byte
High byte
Number of indices
+3:
Lower 1 imi t
for 1. index
High byte
Low byte
+5:
Upper 1 imi t
for 1. index
High byte
Low byte
+7:
Lower limit
for 2. index
High byte
Low byte
+9:
High byte
Low byte
-266-
Chapt.,. 8 -
MACHINE LANGUAGE
a<1,6)
a (1,7)
aU,8)
a(2,6)
a(2,7)
a(2,8)
a(3,6)
a(3,7)
a(3,8)
Add,.
INFl
INF2
INF3
S0038
$0039
S003a
$003b-$003c
$003d-S003e
$003f-$0040
$0041-S0042
$0043-$0044
$0045-$0046
$0047-$0048
Q1
Q2
Q3
Q4
Q5
COPYl
COPY2
-267COPY3
ACt
AC2
MOVEAD
TXTLO
TXTHI
RANGES
TXT
$0049-$0050
$0061-$0066
$0069-$006f
$007a-$007b
$007c
$007d
S02eO-$02ff
Sc760-$c7af
MACHINE LANGUAGE
SIGNAL ROUTINES
A signal routine is a subroutine which is terminated by an
RTS instruction.
It is permissible for a signal routine to
do anything which a procedure or a function may do.
If a
signal routine is not required, then a system routine named
DUMMY can be used.
This routine consists of only an RTS
instruction and does nothing.
A USE-si;nal-routine has no parameters.
Each time a USE
<packa;_> statement is encountered 1n a CONAL program, this
routine is called.
If it is not desired that the package be
initialized every time, then a variable should be used to
indicate that a package has previously been activated by
means of USE.
A module-si;nal-routine has one parameter. ~or the .yregister will contain a value when the call 1S e>:ecuted,
indicating which type signal is to be transm1tted.
The
parameter can be one of the follow1ng:
POWERl
POWER2
LINK
DSCRD
NEW
CLRTAB
-268-
MACHINE LANGUAGE
RUN
WARNl
CON
Is
ERROR
STOP!
BASIC
issue~
tHink
slink
4tdscrd
sdscrd
adscrd
,LINK-command?
,Jump if so
,DISCARD?
,Jump if so
;Ionore all other .ion&ls.;
,LINK-handler
,Back to COMAL
,DISCARD-handler
;Back to COMAL
rt.a
ERROR REPORTING
*:5
ldx
jmp runerr
300
o
"illegal parameter value"
-269-
Ch.pt.,- a t.Kt
t.Ktl
MACHINE LANGUAGE
err300 ldx
.tK
rrorp ld
.t.
d.K
bne
ld.
.t.
ld
st.
ld
st.
.t.Ktl
ertl.n
teKt-l,K
ert.Kt-l,K
,Length of t.Kt
,Length of .,-ror mg.
,Mov. the t.Kt to ERTEXT
.rrorp
.$oe:
ql+O
<tr.pve:
ql+l
>tr.pve:
ql+2
ldv 10
IERRFILE - 0
IERR .. 300
IdK 1<300
ld. 1)300
j_r goto
,Ex.e:ut. jmp (tr.pve:) in PAGES
.byt. p.geb,<q1,>ql
PACKAGE EXAMPLE
The following example shows how a e:omplete module e:ontaining
one package named TEST e:an be e:reated.
The purpose of
this example is to illustrate how one e:reates a proe:edure,
a r l fune:tion and a string fune:tion.
The package is
plae:ed from address $a009 in RAM in the memory map DEFPAG
(see the table of useful memory maps shown earlier).
The package is available on the demo diskette.
test. lire:
t.st.obj
Into the
nd . t 0040
-270-
MACHINE LANGUAGE
DISCARD
New printout:
hello!
6S
end . t 0040
Here is the content of the source code of test. arc:
---=== package test===--make all symbols known:
.lib c64symb
opt 1 i st
*=$8009
:start address
.byte defpag
.word end
.word dummy
package tabl e:
.byte
.word
.word
.byte
4, 'test'
testp
dummy
0
procedure table:
estp
.byte
.word
.byte
.word
.byte
.word
.byte
2,'hi'
phi
3, 'add'
padd
6, 'string'
pstrin
0
;the procedure hi
:procedure header for hi
;the function add
;function string
;no more procedures
proc hi
hi
.byte proc,<hi,>hi,O
.byte endprc
;no parameters
;begins in hi
-271-
Chapter B -
MACHINE LANGUAGE
func add(a#,b#)
;
padd
func string$(character$,number#)
pstrin .byte func+str,(string,>string,2 ;two parameters
;begins in string
.byte
value+str ;character$ is string value parameter
.byte
value+int ;number# is integer value parameter
.byte endfnc
proc hi
print "hello'"
endproc hi
text
textl
;
hi
hilp
4.0
text,y
cwrt
#textl
hilp
;text to be printed
; length of text
;begin with 1. character
;fetch character
:print character on screen
; next character
;finished?
;jump if not finished
;return to COMAL
func add(a#.b#)
return a#+b#
endfunc add
add
Ida
jsr
ldx
Ida
stx
sta
#1
fndpar
copy1
copyl+1
copy2
copy2+1
Ida #2
jsr fndpar
#1
(copy2) ,y
(copyl) ,y
; yz =0
(copy2) IY
(copy1) ,y
ovrflw
;high byte of _#
;pIus high byte of b# plus carry
; jump if arithmetic overflow
-272-
Chapt.,. B -
MACHINE LANGUAGE
num =copy2
string Ida
jsr
ldy
lda
bmi
sta
lny
lda
sta
stos
num+l
stos+l
sterr
tay
txa
adc #<2
tax
tya
adc #>2
bcs sterr
cpx sfree
sbc sfree+l
bcs sterr
check character$.
;jump, if overflow
if num+stos+2>=sfree,
then stack-overflow
jump, if stack-overflow
-273Ida
jsr
Idy
Ida
bne
iny
Ida
cmp
bne
MACHINE LANGUAGE
~t1
fndpar
*2
(c:opyU,y
argerr
(c:opy 1) ,y
*1
argerr
fetch character$(l:l)
iny
Ida (c:opyl> ,y
; y: =4
;.a:=c:harac:ter$(l:l)
//
loop varlable
strIp
ldx
cpx
bne
ldx
c:px
beq
strl
sta (stos),y
r$(ql:ql):=c:harac:ter$(l:l)
inc stos
bne str2
1 nc: stos+l
tos:+l
lnc:
bne
1 nc:
jmp
ql:+l
str2
num+l
ql+1
strl
num
ql
strok
;ql:=O
ql
strlp
ql+l
strIp
;endwhlle
Ida
sta
iny
Ida
sta
clc:
lda
adc
sta
Ida
adc
sta
rts
num+l
(stos) ,y
; y: =1
num
(stes) .y
argerr ldx *1
jmp run err
;"argutnent error
sterr
; "out of memory"
ldx *50
jmp run err
II
-274end
.end
;end of
MACHINE LANGUABE
sou~ce
text
-275-
App_ndiM
COMMODORE
A
64-
Ch ...... c:t_..-
ASCII CHARACTERS
mod.
t.Kt
grAphics
CODE
__
SCREEN CHARACTERS
mod.
grAphics
t.lCt
0
I
C~d
3
4
:5
6
7
<STOP)
whi1:.
d
e
9
10
II
12
13
14
1:5
16
17
18
19
20
21
22
23
24
2:5
26
27
28
29
30
31
32
33
34
3:5
36
37
38
39
40
41
42
43
44
415
cursor down
reverse
cursor horne
(DEL)
9
h
"-
IT,
M
N
r.
"
S
t
U
V
w
.<
z
a
..a
red
rillht
green
blue
space
Yo
&
II.
"n
cur~or
E
F
'*
X
6.
'*
X
6.
W
X
Y
Z
f!:
I
A
.
..
$
X
&
"
,
+
46
47
-276-
App.ndilc A -
ASCII CHARACTERS
mod.
graphics
t.lCt
CODE
CHARACTER CODES
SCREEN CHARACTERS
mod.
t.lCt
graphics
2
3
2
3
4
~l
0
1
2
3
~2
3
4
:53
54
55
56
:5
6
7
6
7
6
7
7
Z
~7
8
9
58
59
60
61
62
63
64
.,
.,
III
48
49
~o
6~
66
67
68
..
b
c
d
69
70
71
72
73
74
7:5
76
77
78
79
80
81
82
83
84
f
51
h
i
8~
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
II!
f!
B
C
0
E
F
G
H
I
A
B
C
0
E
F
G
H
I
E;
I
I
I
"',
K
L
"-
K
L
M
N
p
q
S
T
S
T
U
V
U
V
lC
Y
Z
Ii
ID
A
t
W
X
Y
Z
I
m
't
u
..
..
j.
B
C
D
..
.J
.,
r
X
0
+
I
'"
...*
-277ASCII CHARACTERS
CHARACTER CODES
SCREEN 04ARACTERS
lIact.
1Iad.
graphic.
101
102
103
104
10~
1
J
106
107
108
109
110
111
112
113
114
F
G
H
K
P
R
S
116
117
118
V
W
12~
126
127
128
129
130
131
132
133
134
U
X
Y
Z
j(
III
Sf
..
,..
I
~
"-
11~
119
120
121
122
123
124
I
I
L
N
graphic.
.,
L
,
...
...
'T
I
I
I
I
+
I
"
...J
.J
.J
...
orange
Codes
<RUN>
codes 0-127
f 1
f3
13~
f~
136
137
138
139
140
141
142
143
144
14S
146
147
148
148
ISO
lSI
IS2
IS3
f7
f2
f4
f6
f8
(SHIFT-RETURN)
switch to upper
128-2~~
are
reversed images oi
C.HL
black
cursor up
reverse off
as <CLR> (cle.r screen)
as(JNST) (insert>
brown
lillht red
dark lire,.
lire,.
lillht IIreen
AppendiK A
ASCII CHARACTERS
mode
graphics
teKt
CODE
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
-278-
CHARACTER CODES
CODE
206
207
ASCII CHARACTERS
mode
graphic.
teKt
light blue
light grey
purple
208
curs.or
209
yellow
cyan
210
211
space
212
I~ft
1'1
213
214
215
216
217
213
219
220
221
-,
U
'J
~J
X
Y
Z
Ii
01'
..
0
+
I
222
223
224
225
226
227
...
I
228
.J.
.J.
-I
I
I
I
-I
I
I
I
.'
.J
A
B
C
0
E
F
G
H
I
J
K
L.
M
.J
..
I
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
,.
II
I
,
.J.
.J.
-I
I
I
I
-I
I
I
I
248
...
.I
"
249
250
25.
252
253
254
255
.J
.J
.J
"
-279-
App_ndi.K
COLOR
Color
cod.
CODES
Color
black
white
red
cyan
purple
green
blue
yellow
orange
brown
pink
dark grey
grey
light green
light blue
light grey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Gr.y
c.l.
ASCII
4/4
0/4
3/4
114
2/4
2/4
3/4
1/4
2/4
3/4
2/4
3/4
2/4
1/4
2/4
114
144
5
28
159
156
Keyboard
v.lu
(CTRL-1)
(CTRL-2>
(CTRL-3>
<CTRL-4>
(CTRL-5>
<CTRL-6)
(CTRL-7)
<CTRL-8>
(C= 1
(C= 2
<C= '"
<C= 4
(c= 5
(c= 6
(c= 7
<c= 8
30
31
158
129
149
150
151
152
153
154
155
.cr n
color
cod. 0
0
1
+
+
:5
+
+
+
+
7
B
9
10
11
12
13
14
15
+
0
9 10 11 12 13 14 15
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
0
+
0
+
+
+
3
4
+
+
+
+
+
+
+
+
+
0
+
+
+
+
0
0
0
-280-
-281-
CALCULATIONS
WITH
COMAL
PI, a , sum
item#
, no , position
, byte, id
NUMBER RANGES.
2. 93873588e-39
-32768
$00
0
0 = %0
<=
<=
(=
integer
hexadecimal
binary number
<=
<=
<=
<=
1.70141183e+38
32767
$ffff = 65535
Xl111111111111111
65535
PRIORITYI
(in order of highest priority)
1.
()
parenteses
2.
3.
3.
exponentiation
multiplication
*1 division
DIV integer division
3. MOD remainder after division
4. +
addition
4.
subtraction
4.
monadic subtraction
-'.
~
2~'3
equals 8
2*3 equals 6
7/2 equals 3.5
54 DIV 8 equals 6
.23 MOD 7 equals 2
2+3 equals 5
4-3 equals 1
-5+2 equals -3
5. BITAND
5. BITOR
5. BITXOR
bitwise logical
bitwise logical
bitwise logical
'and'
'Dr'
'exclusive Dr
ApPENDIX
C-
-282-
CALCULAT I ONS
R.lational op.rators.
(Comparisons occur in logical expressions, which can be
TRUE (~1), if the comparison is true. Otherwise the
logical expression has the value FALSE (~O.
6. <
6. <~
6.
6. )=
6. >
6. <>
less than
less than or equal to
equal to
greater than or equal to
greater than
not equal to
NOT
AND
AND THEN
OR
OR ELSE
logical negation
logical "and"
as AND
logical 'or'
as OR
STANDARD FUNCTIONS.
INT(x)
ABS(x)
SGN(x)
SIN(x)
COS (x)
TAN (x)
ATN(x)
LOG (x)
EXP(x)
SQR(x)
Integer part of x
Numerical value of x
Sign of x
Sine of x
Cosine of x
Tangent of x
Inverse tangent of x
Natural logarithm
Exponential function
Square root of x
INT(3.2) equals 3
ABS(-2.5) equals 2.5
SGN(-3) equals -1
SIN(PI/6) equals 0.5
COS (PI) equals -1
TAN(PI/4) equals 1
ATN(I) equals Pl/4
LOG(lO) equals 2.3026
EXP(2) equals 7.389
SQR(9) equals 3
-283-
KEVBOARD
AND
SCREEN
EDITOR
<CTRL>
has special meaning when used wIth other keys. See the
following.
<RUN/STOP>
interrupts program execution.
Action is affected by the COMAL statement ESC. See Chapter 4.
<SHIFT/LOCK>
locks <SHIFT) in upper case mode.
Release by preSSIng the key again.
<SHIFT)
As on a typewriter. If this key is held down while another
key is pressed, an upper case character is produced. Letters
appear as upper case. In the semigraphics mode the symbols
on the right front side of the keys are produced. <SHIFT)
pressed together with other special keys has other functions
as described with these keys.
<C- numb.r>
Pressing the C= key with a number 1-8 switches to colors
with color codes 8-15
<C- Qraphics symbol>
Pressing a key with graphics symbols equals the symbol shown
on the front left of the key.
<CLR/HOME>
moves the cursor to the upper left corner of the screen.
<SHIFT-CLR/HOME>
clears the screen.
<INST/DEL>
is the delete key. It deletes the character immediately to
the left of the cursor, and the remainder of the line moves
one space to fill in the gap.
<SHIFT-INST/DEL>
is the insert key.
It pushes the character under the cursor
and the rest of the line one space to the right.
ApPENDIX
D-
-284-
(STOP-RESTORE)
If the (STOP> and <RESTORE) keys are pressed at the same
time, the computer is 'reset'.
The program in working
memory is not lost.
(RETURN)
Indicates that all information on the current line should be
interpreted and processed.
(CRSR)
There are two keys which are used to move the cursor around
the screen.
The arrows indicate directions.
Each key has
two functions. The function changes when the <SHIFT> key is
depressed.
THE FUNCTION KEYS f1) - (f8
The function keys can be programmed by the user to perform
various functions. (See further details in Chapter 5 ln the
section dealing with the procedure defkey in the COMAL
package 5Y5tem.)
When COMAL is started up, these keys have the following
functions:
(f 1>
<f2)
<f3)
<f4)
(f5)
(f6)
(f7)
RENUM + <RETURN>
MOUNT + <RETURN>
USE turtle + <RETURN>
AUTO
EDIT
LIST
RUN + <RETURN> + CHRS(II)
<RETURN>
SCAN + <RETURN)
(f3)
(f:5)
text.cr n
(show the text screen)
split.cr n
(show graphics screen with 4 lines of text)
graphic.cr n (show the graphics screen)
ApPENDrx
D-
-285-
ApPENDIX
D-
-286-
-287-
HANDLXN9
Text
TEXT
WXTH
COMAL
va~iables')
a~e
Examples:
name$, text$,
f~om$,
long'name$.
Examples:
DIM text$ OF 80
DIM name$ OF 20
DIM answe~$ OF 1
positions-"mal" IN slogan$
position
value 3,
"mal" is
slogan.,
is assigned the
since the text
contained in
and the first
cha~acter in "mal" is the 3.
cha~acter in slogan . See
the mo~e detailed description
of the ope~ato~ IN in Chapter
4.
APPENDIX
E-
-288-
HANDlI NG TEXT
last$l-t.Mt$(131)
or
la.t$l-t.xt$(LEN(t.Mt$)-81)
t$l-slogan$(318)
t$I-"program."(~17)
b..
item$(2,2):="train"
item$(3,l):="oil"
item$(3,2):="gas"
1.ct$l-it.m$(2,1) (213) l.ct$ is assigned the string bar".
APPENDIX
E-
-289-
HANDL I NG TEXT
CONCATENATION OF STRINGS.
strings can be linked
together using the
character +.
"+.loQan$(1.~il)
(418)
t$ is assigned the
string "and c".
STRING FUNCTIONS.
The user can define string functions at will to produce string
segments:
0010 FUNC upp.r$(low.r$)
FOR i -1 TO LEN(lower$) DO
0020
0030
a.-ORD(low.r$(i.
0040
IF .>64 AND a<94 THEN
+128
00:50
low.r$(i.).-CHR$(.)
0060
0070
ENDIF
0080
ENDFOR i.
0090
RETURN low.r$
0100 ENDFUNC upp.r$
Examples of the use of the function upper$,
PRINT upper$("m.rry chri.tma.") yields the printout:
MERRV CHRISTMAS
PRINT upp.r$("h.adlin ") (4.8) gives the prIntout:
DLINE
Using COMAL it is easy to define the Basic-function mid$:
0010 FUNC mid$(.$,.tart,number)
0020
RETURN a$(.tart tart+numb.r-1)
0030 ENDFUNC mid$
This function can be used in lieu of mid$, if you wish to use
parts of existing Basic programs.
-290-
-291-
COMAL
ERROR
NUMBERS
AND
MESSAGES
USE dansk
To get back to English, execute:
*
*
*
ApPENDIX
F-
-292-
ERROR flESSAGES
-293-
ApPENDIX F -
o report fejl
report error
argument fejl
argument error
2 overloeb
overflow
3 division med nul
division by zero
4 delstrengsfejl
substring error
5 uden for vaerdiomraade
value out of range
6 step
step
0
0
7 ulovlige graenser
illegal bound
S fejl i print using
error in print using
10 ulovlig indexvaerdi
index out of range
11 ulovligt filnavn
invalid file name
13 verify fejl
verify error
14 program for stort
ERROR MESSAGES
APPEND!X
F-
-294-
ERROR MESSAGES
30 ulovlig farve
illegal color
31 ulovlig graense
illegal boundary
32 ulovlig tegning-nummer
illegal shape number
33 tegningens laengde skal vaere 64
shape length must be 64
34 ulovlig sprite-nummer
illegal sprite number
35 ulovlig stemme
ill egal voice
36 ulovlig node
illegal note
ApPENDIX
F-
-295-
SYNTAX ERROR.
101 syntaksfejl
syntax error
102 forkert type
wrong type
103 saetning for lang eller for kompliceret
statement too long or too complicated
104 kun som saetning, ikke som kommando
statement only, not command
106 linienumre er fra 1 til 9999
line number range: 1 to 9999
108 procedure/funktion findes ikke
procedure/function does not exist
109 struktureret saetning ikke tilladt her
ERROR MESSAGES
ApPENDIX
F-
-2%-
ERROR MESSAGES
APPENDIX
F-
-297-
ERROR MESSAGES
ApPENDIX
F-
-298-
ERROR MESSAGES
260 write file open (An already opened file opened again)
261 file not open (Tried to access an unopened file)
262 file not found
-299-
USER
COMMENTS
AND
CORRECTXONS
-300-
USER CDl9lENTS
-301-
SAMPLE
COMAL
PROGRAMS
0020
0030
0040
0050
0060
0070
0080
0090
0100
0110
0120
0130
0140
0150
0160
0170
0180
0190
0200
0210
0220
0230
0240
0250
0260
0270
0280
0290
0300
0310
0320
DIM code$ OF 3
USE sound
LOOP
PAGE
PRINT "Choose voice (1,2 or 3)"
PRINT "Choose note
(a2,c4,b3, )"
PRINT "The numbers = octave:"
PRINT "'c4' is middle C (4. octave - 440 Hz)"
PRINT '''f5tt' is 'f sharp' in the octave above"
PRINT AT 22,1: "LESSON 1: We playa single note ... "
PRINT AT 20.1: "(Press <RUN/STOP> to end . )"
PRINT
INPUT AT 8,1: "voice: ": voice
INPUT AT 9,1: "note-code: ": code$
play(l,code$)
ENDLOOP
PROC play(voice,code$)
IF code$<>"z" THEN
note(voice,code$)
gate(voice,l) II attack and decay
ENDIF
pause(16l II sustain
gate(voice.Ol II release
ENDPROC play
PROC pause(sec'32)
TIME 0
WHILE TIME<1.875*sec'32 DO NULL
ENDPROC pause
- 302-
AppendiK H 0210
0220
0230
0240
0250
0260
0270
0280
0290
0300
0310
0320
0330
0340
0350
0360
0370
0380
0390
0010
0020
0030
0040
0050
0060
0070
0080
0090
0100
0110
0120
0130
0140
0150
0160
0170
0180
0190
0200
0210
0220
0230
0240
0250
0260
0270
0280
0290
0300
0310
0320
0330
0340
0350
0360
0370
0380
0390
0400
SAMPLE PROGRAMS
play(voice,codeS)
play(voice,"z")
ENDFOR voice
END LOOP
PROC play(voice,codeS)
IF codeS<>"z" THEN
note(voice,codeS)
gate(voice,l) II attach and decay
ENDIF
pause(8) II sustain
gate(voice,O) II release
ENDPROC play
PROC pause(sec'32)
TIME 0
WHILE TIME<1.875*sec'32 DO NULL
ENDPROC pause
"c4",B,"z",2,"c4",B,"
Z ",2,"c4",8,"d4",4
"e41',B,"z",8,"e4",B,"d411,4,"e4I',8
"f4 ,4,"g4",16,"z",B,"c5",4
"c 5",4,"cS",4,"g4",4,"g4",4
"g4",4," e 4",4,"e4 ',4," e 4",4
11
- 303-
SAMPLE PROGRAMS
DATA "C4'.4,"c411,4,"c41I,4,'Z",B,"g4t',8
0410
0420
DATA "f4 u ,4, u e 4 u ,8, ud4" ,4, "c4" ,8
0430
0440
RESTORE melody
0450
WHILE NOT EOD DO
READ code$,tid
0460
play(voice,code$)
0470
ENDWHILE
0480
0490
0500 ENDPROC play'melody
0510
0520 PROC pause(sec'32)
0530
TIME 0
0540
WHILE TIME<I.875*sec'32 DO NULL
0550 ENDPROC pause
0010
0020
0030
0040
0050
0060
0070
0080
0090
0100
0110
0120
0130
0140
0150
0160
0170
0180
0190
0200
0210
0220
0230
0240
0250
0260
0270
0280
0290
0300
0310
0320
0330
0340
0350
0360
0370
0380
0390
0400
.!410
0420
0430
0440
LOOP
PAGE
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
PRINT
."
."
- 3040450
0460
0470
0480
0490
0500
0510
0520
0530
0540
0550
0560
0570
0580
0590
0600
0610
0620
0630
0640
0650
0660
0670
0680
0690
0700
0710
0720
0730
0740
0750
0760
0770
0780
0790
0010
0020
0030
0040
0050
0060
0070
0080
0090
0100
0110
0120
0130
0140
0150
0160
0170
0180
0190
0200
0210
0220
0230
0240
SAMPLE PROGRAMS
play'melody
ENDLOOP
PROC playCvoice,code$)
IF code$<>"z" THEN
noteCvoice,code$)
gate(voice,l) II attack and decay
ENDIF
pauseCtid) II sustain
gateCvoice,O) II release
ENDPROC play
PROC play'melody II Row, Row, Row Your Boat
melody:
DATA 'IC4",8,"Z",2~"C411,8,"Z",2,"C411,8.'ld411,4
DATA "e4",8,"z",8,"e411,8,"d411,4,"e41',8
DATA 1144" .4, "g4"" 16, liZ II ,8, "c5 11 ,4
DATA "CS",4,"c511,4,"g411,4,"g411,4
DATA IIg411 ,4, "e4",4, "e411 .,4, "e411,4
DATA "c411" 4, "c4 11 "4,, "c4" ,,4, liZ 11,8, "g411 ,8
DATA l'f4",4,'e411,8,"d411,4,"c4I',8
RESTORE melody
WHILE NOT EOD DO
READ code$,tid
play(voice,code$)
ENDWHILE
ENDPROC play'melody
PROC pauseCsec'32)
TIME 0
WHILE TIME<1.875*sec'32 DO NULL
ENDPROC pause
II save "@Music 5"
DIM code$ OF 3
DIM tone#(50), ads 'pause# (50) , r'pause#(50)
USE sound
vol ume C15)
soundtype(1,2)
adsrCl,6,6,8,6)
no:=O
WHILE NOT EOD DO
no:+l
READ code$,tim
tone#(no):=frequencyCcode$)
ads'pause#Cno):=tim*2
r'pause#Cno):=tim*2
ENDWHILE
tone#(no+l):=O
setscoreCl,tone#() ,ads'pause#() ,r'pause#(
playscore(l,O,O)
number:=O
WHILE NOT waitscoreCl,O,O) DO
number:+l
- 305 0250
0260
0270
0280
0290
0300
0310
0320
0330
0340
SAMPLE PROGRAMS
PRINT number;
ENDWHILE
END
PROC pause(sec'32)
TIME 0
WHILE TIME<1.875*sec'32 DO NULL
ENDPROC pause
DATA "c4",8,"c4",8,"c4 " ,8,"d4",4
0350 DATA
lI
0360
0370
0380
0390
"f4",4,"g4",16,"c5",4
" c 5",4,"c5",4,"g4",4,"g4",4
"g4 " ,4," e 4",4," e 4",4,"e4",4
"c4" ,4, "c4" ,4, "c4" ,4, "g4" ,8
DATA
DATA
DATA
DATA
MULTICOLOR:
r~~~~D~~
I8
EXPANDY:
BACKGROUND:
COLOR 2:
COLOR 3:
PRESS: H
FOR HELP
- 3(x)-
SAMPLE PROGRAMS
0020
0030
0040
0050
0060
0070
0080
0090
0100
0110
0120
0130
0140
0150
0160
0170
0180
0190
0200
0210
0220
0230
0240
0250
0260
0270
0280
0290
0300
0310
0320
create 'record
0330
WHEN "3"
0340
list'file
0350
WHEN "4"
0360
search'file
0370
WHEN "5"
0380
sort'file
0390
WHEN "6"
0400
change 'record
0410
WHEN "7"
0420
delete 'record
0430
WHEN "8"
0440
save'file
0450
OTHERWISE
0460
PRINT "Illegal reply "
0470
wait 'for 'keystroke
0480
ENDCASE
0490 ENDLOOP
0500
0510 PROC show'menu
0520
PAGE
PRINT .. -----===== MAIN MENU =====----- ..
0530
PRINT
0540
0550
PRINT
- 3070560
0570
0580
0590
0600
0610
0620
0630
0640
0650
0660
0670
06BO
0690
0700
0710
0720
0730
0740
0750
0760
0770
0780
0790
0800
0810
0820
0830
0840
0850
0860
0870
0880
0890
0900
0910
0920
0930
0940
0950
0960
0970
0980
0990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
SAMPLE PROGRAMS
( 1 ) LOAD
PRINT "
the file"
(2) CREATE a record"
PRINT "
(3) LIST
PRINT "
the file"
(4) SEARCH the file"
PRINT "
(5) SORT
PRINT "
alphabetically"
(6) CHANGE a record"
PRINT "
(7) DELETE a record"
PRINT "
<8) SAVE
PRINT "
revised file"
PRINT
PRINT
PRINT "Records: ";number
IF number=O THEN flag$:="Please load or create a file .. "
PRINT
PRINT flag$
ENDPROC show'menu
PROC load'file
OPEN FILE l,"Addresses",READ
INPUT FILE 1: number
FOR no:=l TO number DO
INPUT FILE 1: name$(no)
INPUT FILE 1: street$(no)
INPUT FILE 1: city$(no)
INPUT FILE 1: phone$(no)
ENDFOR no
CLOSE FILE 1
ENDPROC load'file
PROC create'record
PAGE
PRINT "::::: CREATE A NEW RECORD :::::"
PRINT
PRINT
IF number=100 THEN flag$:="No more room for data'"
IF flag$="" THEN
number: +1
INPUT "Name
": name$(number)
INPUT "Street ": street$(number)
INPUT "City": city$(number)
INPUT "Phone ": phone$(number)
ENDIF
ENDPROC create'record
PROC list'file
PAGE
PRINT "::::: LISTING THE FILE :::::"
PRINT
IF number=O THEN
flag$:="No files in memory!"
PRINT
ELSE
FOR no:=l TO number DO print'record(no)
ENDIF
ENDPROC list'file
PROC search'file
PAGE
PRINT "::::: FILE SEARCH :::::"
PRINT
PRINT
flagS:="I am searching ... "
- 308-
SAMPLE PROGRAMS
Appendilc H 1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
- 309-
SAMPLE PROGRAMS
ELSE
flag$:="There are only "+STR$(number)+" records"
ENDIF
ENDPROC change'record
PROC delete'record
PAGE
PRINT "::::: DELETE A RECORD :::::"
PRINT
PRINT
INPUT "Which record number? ": record
IF record>number THEN
flag$:="Use a smaller record number'"
ELSE
print 'record (record)
PRINT
INPUT "Is this the right record (yIn)? ": reply$
PRINT
IF reply$ IN "yY" THEN
FOR no:=record TO number-1 DO
name$(no):=name$(no+l)
street$(no):=street$(no+1)
city$(no):=city$(no+l)
phone$(no):=phone$(no+l)
ENDFOR no
number:-1
ENDIF
ENDIF
ENDPROC delete'record
PROC save'file
PAGE
PRINT "::::: SAVING FILE TO DISK :::::"
OPEN FILE 1,"@Addresses",WRITE
PRINT FILE 1: STR$(number)
PRINT
PRINT
FOR no:=1 TO number DO
PRINT FILE 1: name$(no)
PRINT FILE 1: streetS (no)
PRINT FILE 1: city.(no)
PRINT FILE 1: phone$(no)
ENDFOR no
CLOSE FILE 1
ENDPROC save'file
PROC wait 'for 'keystroke
PRINT
PRINT "< > . ";
REPEAT
reply$:=KEY$
UNTIL reply$<>CHR$(O)
PRINT AT 0,2: reply$
ENDPROC wait'for'keystroke
- 3100105 II
0110
0120
0130
0140
0150
0160
0165
0170
0175
0180
0185
0187
demo'size
demo'c:olor
demo'c:ase
demo'rotation
II
square(100)
blank'line(2)
dotlines(15)
blank'line(8)
C:irc:le(240,240,200)
blank'line(14)
spinsquares(150)
0190 /I
0192 setup'plotter
0195 plotter'off
0197 /I
0200
0297
0497
0500
0510
0515
0520
0525
0530
0545
0547 /I
0550
0555
0560
0565
0570
0575
0595
0597
0600
0605
0610
0615
0620
0625
0645
0647
PROC demo'c:olor
selec:t'size(2)
FOR i:=O TO 3 DO
switc:h'c:olor(i)
print'hello
ENDFOR i
ENDPROC demo'c:olor
II
PROC demo'c:ase
blank'line(l)
selec:t'c:ase(O) II upper c:ase
print'hello
selec:t'c:ase(l) II lower c:ase
print'hello
ENDPROC demo'c:ase
II
PROC demo'rotation
bl ank ' l i ne (2)
rot' c:har (1 )
print'hello
rot'c:har(O)
print 'hello
ENDPROC demo'rotation
0650
0655
0660
0665
0670
0675
0695
0697 /I
0700 PROC dotlines(n)
zero 'pen ("h")
0705
FOR i:=O TO n DO
0710
0715
plot("m",0,-i*20)
dot 'I i ne ( i )
0720
plot ("d" ,400, -i *20)
0725
ENDFOR i
0730
blank'line(4)
0735
dot 'I ine<O)
0740
SAMPLE PROGRAI'IS
Appandilc H -
0745
0747
0750
0752
0754
0756
0758
0760
0762
0764
0766
0768
0785
0795
0797
0800
0805
0810
0820
0830
0840
0845
0847
0850
0852
0854
0856
0858
0860
0862
0864
0865
0867
0870
0875
0880
0885
0890
0895
0897
0900
0910
0915
0920
0925
0930
0945
0947
0950
0960
0970
0990
0995
0997
1990
1995
1997
2000
2010
2045
2047
- 311 -
SAI'IPLE PROGRAMS
ENDPROC dotlines
II
PROC circle(xO,yO,radius)
plotC"m",xO,yO)
zero'pen("i")
plot("r",radius,O)
FOR v:=O TO 360 STEP 5 DO
t:=PI*v/180
x:=radius*COS(t)
y:=radius*SIN(t)
plotCUj",x,y)
ENDFOR v
blank'line(4)
ENDPROC circle
II
PROC squareCside)
blank'line(3)
plotC"j",O,side)
plot("j",side,side)
plot("j",side,O)
plotCUjU,O,O)
ENDPROC square
II
PROC spinsquaresCs)
plot("m",240,240)
zero'pen(Ui")
FOR v:=O TO 360 STEP 20 DO
t:=PI*v/180
draw'boxCs,t)
ENDFOR v
blank'line(4)
ENDPROC spinsquares
II
PROC draw'box(s,t)
plot("j",s*COSCt),s*SINCt
plotC"j",s*SQR(2)*COSCt+PI/4) ,s*SQR(2)*SIN(t+PI/4
plot("j",s*COSCt+PI/2),s*SINCt+PI/Z
plot (U j U,0,0)
ENDPROC draw'box
II
PROC blank'line(bl)
plotter 'on
FOR i:=1 TO bl DO
PRINT FILE 6:
ENDFOR i
plotter'off
ENDPROC blank'line
II
PROC print'hello
plotter'on
PRINT FILE 6: "HELLO!"
plotter'off
ENDPROC print'hello
II
II
I I PLOTTER PROCEDURES
II
PROC plotter'on
OPEN FILE 6,"u6:",WRITE
ENDPROC plotter'on
II
- 3122050
2060
2095
2097
2100
2110
2120
2130
2145
2147
2150
2160
2170
2180
2195
2197
2200
2210
2220
2230
2245
2247
2250
2255
2260
2265
2270
2272
2275
2277
2280
2285
2290
2295
2297
2300
2305
2310
2320
2330
2345
2347
2350
2355
2360
2370
2380
2395
2397
2400
2405
2410
2420
2430
2445
2447
2450
2460
2470
2495
2497
PROC plotter'off
CLOSE FILE 6
ENDPROC plotter'off
II
PROC switch'color(pen)
OPEN FILE 2,"u6:/s2",WRITE
PRINT FILE 2: pen
CLOSE FILE 2
ENDPROC switch'color
II
PROC select'size(size)
OPEN FILE 3,"u6:/s3",WRITE
PRINT FILE 3: size
CLOSE FILE 3
ENDPROC select'size
II
PROC select'ascii
OPEN FILE 4,"u6:/sO",WRITE
PRINT FILE 4:
CLOSE FILE 4
ENDPROC select'ascii
II
PROC plot(sc$,x,y)
OPEN FILE 1,"u6:/sl",WRITE
PRINT FILE 1: sc$;x;y
CLOSE FILE 1
ENDPROC plot
II
PROC zero'pen(zp$)
II zp$ = h/i for abs/relative
OPEN FILE 1,"u6:/s1",WRITE
PRINT FILE 1: zp$
CLOSE FILE 1
ENDPROC zero'pen
II
PROC rot'char(rot)
II rot=0/1 for horlrot 90 deg CW
OPEN FILE 44,"u6:/s4",WRITE
PRINT FILE 44: rot
CLOSE FILE 44
ENDPROC rot'char
II
PROC dot'line(dash)
Ildash=O to 15, 0 = unbroken
OPEN FILE 5,"u6:/s5",WRITE
PRINT FILE 5: dash
CLOSE FILE 5
ENDPROC dot'line
II
PROC select'case(nr)
II nr=0/1 for upper/lower case
OPEN FILE 6, "u6:/s6",WRITE
PRINT FILE 6: nr
CLOSE FILE 6
ENDPROC select'case
II
PROC reset 'plotter
OPEN FILE 7,"u6:/s7",WRITE
PRINT FILE 7:
ENDPROC reset'plotter
II
SAMPLE PROGRAMS
- 313-
SAMPLE PROGRAMS
II
- 3140500
0510
0520
0530
0540
0550
0560
0570
0580
0590
0600
0610
0620
0630
0640
0650
0660
0670
0680
0690
0700
0710
0720
0730
0740
0750
0760
0770
0780
0790
0800
0810
0820
0830
0840
0850
0860
0870
SAMPLE PROGRAMS
advance 'pointer
ENDPROC start'train
PROC check'light
WHILE PEEK(port'b) BITAND 1<>1 DO NULL
advance 'pointer
ENDPROC check'light
PROC delay(sec)
TIME 0
WHILE TIME<sec*60 DO NULL
ENDPROC delay
PROC stop'train
POKE port'b,PEEK(port'b) BITAND 253
advance 'pointer
ENDPROC stop'train
PROC define'variables
port'b:=$ddOl
port'b'ddr~=$dd03
position:=l
ENDPROC define'variables
PROC set'port'b
POKE port'b'ddr,2
POKE port'b,2
ENDPROC set'port'b
PROC advance'pointer
PRINT AT 10+position,2:
IF position<4 THEN
position:=position+l
ELSE
position:=2
ENDIF
PRINT AT 10+position,2:
ENDPROC advance'pointer
II )11
INDEX
INDEX
---------- A ----------
---------- C ----------
221,224,237
AID converters 251
ABS 79, 131
accessories 15
accuracy 205
action blocks 43
actual parameters 73
Addr List Demo 223,306
address list 222
ADSR 185,192
ADSR envelope 186
algorithm 77
AND 138
AND THEN 139
animate 178
apostrophe 38
APPEND, file 113
arc 157
arcl 159
arcr 160
arithmetic operators 281
arrays 67
arrays, one-dimensional 69
arrays, text 70
ASCII characters 275
ASCII-format 113
assembler language 25
assignment operator 47
asynchronous data
transmission 242
at symbol (@) 221,224,237
ATN 133
attack 189
AUTO ~-4,93
calculations 281
call procedure 39
capital letters 21
cartoons 169
cartridge, installation 17
CASE structure 61
CASE-OF-WHEN-OTHERWISEENDCASE 119
CAT 97
CBM 8050 & 8250 disk
drives 245
CHAIN 101,221
CHANGE 95,227
character codes 275
character deletion 28
character replacement 214
character sets (font) 213
character set, replacement 215
character set, user-defined 215
chip, 6510 25
CHR$ 133
CHR$(O) 35
Christensen, B.rge 10
circle 156
circles 50
---------- B ----------
- 315 -
back 159
background 154
backup copy 41,42,46
bank switching 261
Basic 9,10,37
Basic fra COMAL 105
Batteries Included 245
bell 210
Bendict L.fsted 10
binary form 233
BITOR 141
BITXOR 141
Boolean operators 282
border 154
branch blocks 43
branching 59
bubble sort 82,229
Bus Card II 245
byte 25
clear 154
clear/home 29
clearscreen 30
clearscreen 153
CLOSE 116
CLOSE FILE 116
CLOSED 127
closed procedures 82,221
collision, sprite 168
color codes 31,279
color combinations 279
color TV 17
color, foreground 172
colors, screen 27
CO MAL cartridge 258
COMAL files 219
CONAL system 255
comments (II) 141
commercial-a (@) 221,224,237
Commodore Data 10
Commodore key 20,57,283
computer language 32
CON 101
condition 59
conditional Rxecutiion 59
condItionals 117
control key 285
control ports 197
control ports 251
COpy 102
corrections 299
COS 132
INDEX
INDEX
- 316-
crash 88
CREATE 112
cs; 237
cur col 210
currow 210
cursor 110
---------- D ----------
END 145
END TRAP 89
english 148
ENTER 99,220,221
env3 195
EOD 111
EPROM expansion 258
EPROM module 266
equali ty sign 47
equation, solving 77
ERR-ERRFILE-ERRTEXT$ 122
error handling 88,122
error messages 291
error numbers 291
error reporting 268
ESC 136
even parity 242
EXIT WHEN 89
EXP 133
expression 48
EXTERNAL 128
external procedure 84,221
Danish 148
dansk 148
DATA 110,223
data direction register 247
data element 223
data statement 72
data stream 86
datacollision 181
Datassette 15,17,21,40,238
DB-25 connector 246
ddr 247
decay 189
declaration statement 56
define (drawing) 174
defkey 210
DEL 95
delay 204
delete 103
delete character 27
delete key 20
Demo Diskette 23
Demo Program 21
demonstation program 21
digital thermometer 251
DIM 142
dimensioning string
variables 56
DIR 97
direct execution 27
direct files 232
discard 30,106
disk drive 15,22,237
disk drives, CBM 245
disk operating system
errors 297
diskette files 219
display 99,220
DIV 137
DOS error messages 297
draw 155
drawing, save 168
drawto 155
ds: 237
dynamic equals sign 47
FALSE 135
file 86,219
file handling 85
file transfer between
computers 250
file type code 236
fi I e types 236
fi 11 52,157
-filter 194
fi I terfreq 194
-f i l tert ype 194
FIND 94
fine-tuning o-f TV 20
font package 213
font procedures in depth 216
FOR-ENDFOR 36
FOR-TO-STEP-DO-ENDFOR 121
foreground color 172
formal parameters 73
formatting 23
forward 159
free 210
frequency 193
fullscreen 153
FUNC-RETURN-ENDFUNC 77,129
function header 263
function keys 284
function, string 80
functions 9,76,129,257
---------- E ----------
---------- G ----------
Easyscript 220
EDIT 94
empty statements 38
---------- F ----------
- 317-
INDEX
gate 192
GET$ 220
getcharacter 217
getcolor 155
get screen 208
gettime$ 208
global names 74
GOTO 124
graphics 29
graphics cursor 30
graphics overview 150
graphics package 31
graphics screen 30,148
graphics symbols 21
graphics, use 149
graphicscreen 152
greeting message 20
---------- H ---------HANDLER 89
hard copy, text screen 58
hardcopy 209
heading 158
hidesprite 180
hideturtle 158
high-graphics screen 171
high-level language 25
high-resolution
graphics 29,149,153,163
high (voltage level) 242
home 30,158
---------- I
----------
INDEX
interrupt 196
---------- J
----------
jack connections 15
Jensen, Jens Erik 10
joysticks 199
---------- L
----------
I abel 111
Lassen, Helge 10
Laursen, Lars 10
left 159
LEN 135
light pen 199
light pen overview 204
Lindsay, Len 11
line numbers 34
link 105,257
linkfont 214,216
linkshape 182
LIST 98,220,221
LOAD 100,220
loadfont 216
loadscreen 163
loadshape 182
local names 73
LOG 133
logical (bit) operators 281
logical const~nts 59
logical expressions 59
Logo 9,37
LOOP - ENDLOOP 79,89
loop block 35,43,63
loop st~tements 119
LOOP-EXIT-EXIT WHEN-ENDLOOP 12:
loops 63
low <voltage level I 242
lower case 28
lp: 237
INDEX
- 318-
INDEX
---------- M ----------
---------- P ----------
package 30,147,257
package concept 11
package example 269
packages, overview 147
paddle game 198
paddles 197
page 29,49',109
paint 157
PAL TV standard 184
parallel port 245
parameter passing 263
parameter specification 263
parentheses 58
parity bit 242
Pascal 9,10,37
PASS 24,103
PEEK 143
pen 30
pencolor 154
pendown 159
penon 204
penup 159
PI 132
pixel 148,171
playscore 193
plot 155
Plotter Demo 238
plottext 161
POKE 143
power supply 19
prg 236
PRINT 28,107
PRINT AT 108
PRINT FILE 88,113
PRINT USING 108
printer 15
printer attributes 211
Printer-Plotter 238
printscreen 163
priority (operations) 281
~
priority (sprite) 180
~
PROC-ENDPROC 125
procedure 9,25,52,58,73,125,257
procedure call 39
procedure, closed 82
procedure, external 84
procedure header 263
procedure name 39
procedure oriented 26
procedures, recursive 76
procedures, saving 220
program 32
programming 26
programming style 40
programming habits 45
programs, saving 220
programs, samples 301
protocol, RS-232C 242
Music 4 303
Music 5 304
---------- N ----------
names'in'upper'case 207
NEW 93
Niklaus Wirth 10
NOT 138
note 191
nowrap 160
NTSC TV standard 184
NULL 144
INDEX
- 319-
pulse 195
put character 217
---------- Q ----------
quote'mode 207
---------- R ----------
---------- S ----------
INDEX
semicolon 31
seq 236
sequential file 219,222
sequential file, moving 235
serial 211
serial communication 241
setexec 96
setfrequency 193
setheading 158
setpage 212
setprinter 211
setrecorddelay 212
set screen 209
settime 207
setxy 156
SGN 79,131
shift 20
shift lock 20
showkeys 211
showsprite 180
showturtle 158
SID chip 184
slgnal routines 267
signals 257
simulations 27
SIN 132
SIZE 97
sortlng, bubble 82
sound 184
sound default values 190
sound orders in depth 191
sound package 184
sound synthesizer 184
soundtype 185,192
sp: 237
SPC$
split screen 29
splitscreen 153
sprite drawing, save 168
sprite orders 150
sprite overview 173
sprite package 165
sprite, multi-colored 175
spriteback 175
spritecollision 180
spritecolor 174
spriteeditor 305
spriteinq 181
spritepos 175
sprites 165
sprites, enlarged 167
sprites, multi-colored 171
spritesize 175
spritex 178
spritey 178
SQR 132
square, drawing 33
square wave (sound) 196
stampsprite 181
Star Trek 26
startsprites 178
INDEX
- 320-
state 33
statement 54
STATUS 102
STATUS$ 102
STOP 144
stop bit 242
stopplay 193
stopsprite 178
storage diskette 23,41
storage medium 219
STR$ 134
string 55,264
string constant 56
string functions 80,289
string segments 288
string variable 56
strings 234
structure, program 40
structured programming 9
style, programming 40
sustain 189
SX-64 16
symbols 12
sync 194
SYS 144
SYS to COMAL 105
system package 206
INDEX
turtle package orders 151
turtle, use 149
turtlesize 158
TV signal, fine tuning 20
---------- U ----------
u:(device> 237
UHF signal 19
UniComal ApS 10
UNIT 116,237
UNIT$ 116
upper case 28
USE 105
user comments 299
users' groups 11,255
USR 236
Commodore
Made in Denmark
Commodore