-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises_en.tex
308 lines (220 loc) · 13.8 KB
/
exercises_en.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{eurosym}
\title{Exercises for workshop Commodore 64}
\author{Folkert van Verseveld}
\newcounter{problem}
\newcounter{solution}
\newcommand\problem{%
\stepcounter{problem}%
\textbf{\theproblem.}~%
\setcounter{solution}{0}%
}
\parindent 0in
\parskip 1em
\begin{document}
\maketitle
\section{Introduction}
This bundle contains a set of exercises with increasing difficulty.
The difficult exercises may be skipped.
\emph{Please note that this document has embedded clickable links.
If the text is not surrounded by a blue or red rectangle you can also try to open the pdf in your browser.}
\subsection{VICE setup}
This workshop uses the Versatile Commodore Emulator (VICE).
You can download the emulator \href{http://vice-emu.sourceforge.net/}{from this site}.
Depending on your operating system, some additional setup may be required as will be discussed in the next section.
\subsubsection{Linux}
Setting up the emulator depends on your Linux distribution.
If your Linux distro is derived from Debian (e.g. Ubuntu or Xubuntu) you can setup the emulator by running the script as root:
\verb:sudo sh vice.sh:.
If this does not work, but you have a different package manager (e.g. \verb:yum: rather than \verb:apt:), you can modify the variables in the script and (re)run it.
If this does not work either, but you have Wine installed, you can also use the Windows recipe.
The die-hards may also try to compile the \href{http://sourceforge.net/projects/vice-emu/files/releases/vice-3.1.tar.gz/download}{source code}.
\subsubsection{Windows}
Download the \href{http://sourceforge.net/projects/vice-emu/files/releases/binaries/windows/WinVICE-3.1-x64.7z/download}{windows version}.
Other versions can be downloaded from the \href{http://vice-emu.sourceforge.net/windows.html}{download section}.
You need \href{http://www.7-zip.org/}{7Zip} in order to extract the archive.
Extract all files and run \verb:x64.exe:.
\subsubsection{Mac}
Download the \href{http://sourceforge.net/projects/vice-emu/files/releases/binaries/macosx/vice-macosx-cocoa-x86_64-10.12-3.1.dmg/download}{mac version}.
You need to extract the .dmg in order to use the emulator.
Right click (or using CTRL+left click) on x64sc and then on \verb:Open:.
\subsection{Tips and Tricks}
Please don't hesitate to try some things before giving up.
Some exercises are designed with this in mind.
If the emulator is haning you can try to reset it using \verb:File->Reset->Soft: to restart the C64.
If this does not work, you can try to hard reset it using \verb:File->Reset->Hard: or restart the emulator if that does not work either.
\emph{If you reset the emulator all BASIC programs are lost. Make sure that you save your BASIC program to disk first!
The floppydisk has to be reattached if you reset the emulator!}
You can speed up the emulator using \verb:ALT+W: and reduce program load time.
The slow loading times must be emulated because many software depends on this!
Use \verb:ALT+W: to start/stop speed up mode.
\include{load_save_en}
\section{Introduction BASIC}
The Commodore 64 understands machine code and the high-level programming language BASIC.
If you run the VICE emulator you will see a similar window to figure \ref{fig:vice}.
\begin{figure}
\centering
\includegraphics[width=0.5\linewidth]{images/boot.png}
\caption{VICE emulator}
\label{fig:vice}
\end{figure}
The default screens tells us that we are using BASIC version 2 and it has 64KiB of RAM leaving 38911 bytes for BASIC.
This means that our BASIC program cannot exceed 38.9KB!
You may have noticed that the default screen only uses upper case letters.
This is because many computer designers at that time deemed it unnessecary to support both upper and lower case letters at the same time!
It is possible to use both at the same time, but we are not going to use this.
\subsection{Typing}
You can use the spacebar to place a space character on the screen or to `erase' the current character.
\problem Try to change different portions of text on the screen and try to remove some text by overwriting them with spaces.
\problem What happens if we place the cursor to the far right and press the right arrow key?
What happens if we place the cursor to the far left and press the left arrow key?
And what happens if we place the cursor at the bottom and press the down arrow key?
\subsection{Keyboardconfiguration}
You may have noticed that the C64 keyboard layout is very different compared to a QWERTY keyboard.
If you would type \verb:SHIFT+2: and \verb:]: you will probably see \verb:@]: on the screen.
In case you see \verb:"@:, this means that the emulator uses the real C64 keyboard layout.
You can change the layout using \verb:Settings->Keyboard settings->Keyboard mapping type:.
A real C64 uses \verb:Positional mapping: and the emulator uses \verb:Symbolic mapping: by default.
See also figure \ref{fig:changekbd} and \ref{fig:vicekbd}.
\begin{figure}
\centering
\includegraphics[width=0.75\linewidth]{images/changekbd.png}
\caption{Changing keyboard layout}
\label{fig:changekbd}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=\linewidth]{images/vicekbd.png}
\caption{C64 Keyboard cheatsheet}
\label{fig:vicekbd}
\end{figure}
If you need to type special characters from the BASIC examples that are not available on a QWERTY keyboard than you have to change the layout to \verb:Positional mapping:.
Many exercises can be typed in using \verb:Symbolic mapping:.
\subsection{Printing}
Now that we have learned how to write text and remove it, we can look at some BASIC programming.
Lets see how we can write a ``Hello World'' program.
You can print text using the \verb:PRINT: command.
Each BASIC command is executed after you press enter.
All text to be printed must be enclosed with double quotes, that is \verb:":.
One example is \verb:PRINT "HELLO":.
\problem Change the example to print \verb:HELLO WORLD:.
\verb:PRINT: can also join multiple portions of text using either \verb:,: or \verb:;:.
You can use these multiple times.
\problem What is the difference between \verb:PRINT"HELLO","OLLEH": and \verb:PRINT"HELLO";"OLLEH":?
\subsection{Calculations}
Using BASIC we can compute some expressions.
The basic math operators $+$,$-$,$*$, $/$, and $\uparrow$ (power) can be used in expressions.
Please note that all numbers are stored as floating point numbers.
For example, \verb:PRINT 2/3: prints $0.666666667$, rather than $0$ as some programmers would have expected.
If we want to perform integer division, this means `ignoring' the fractional part of the result,
we can use the function \verb:INT():.
For example, $\verb:PRINT:\ \verb:INT:(2/3)$ prints $0$.
As you may have noticed is that the operator precedence is the same as in mathematics.
Note that some calculators and computers from the same era did not have operator precedence at all!
You can also use braces to change the order of computation.
\subsection{Variables}
It would be nice if we could store the result of a computation in a variable so we don't have to recalculate each result.
Using variables we can also write interactive programs.
BASIC has numeric variables and a couple of other types (think about text or a list of numbers).
For now, we will only look at the numeric variables.
Back then, many programmers first wrote their program on tape before typing them in.
This is one of the many reasons why variables had very short and cryptic names and why the C64 only supports short variable names.
BASIC commands are illegal variable names. For example, \verb:PRINT: is an illegal variable name.
A variable may \emph{not be longer than two characters}.
If it is longer than two characters, all characters after the second are ignored.
For example, \verb:BANANA:, \verb:BA: and \verb:BAROMETER: are all considered to refer to the same variable!
Lets say we want to keep track of the number of apples we have.
We could do this using \verb:A=3: and we can print the number of apples with \verb:PRINT"APPELS",A:
If we want to add one apple we just have to add \verb:A=A+1:.
If you would print the number of apples you should see that we now have 4 apples.
You can navigate the cursor to the previous typed in \verb:PRINT: statement and press enter to re-execute the statement without having to type it all over again.
Now we know some fundamental theory about BASIC and lets take a look at some more serious programs.
\section{Programming using BASIC}
Until now we had to manually type each BASIC command.
While this works quite nicely for small experiments, it is inpractical for real programs.
Lets see how we can write our first BASIC program that can run by itself once we RUN it.
Each line of BASIC codoe has to be preceeded by a unique number.
It is similar to a linenumber.
The BASIC interpreter uses these linenumbers to determine in what order the program has to be executed.
Lets write our first BASIC program:
\begin{lstlisting}
10 PRINT"COMMODORE 64"
\end{lstlisting}
If you type RUN (or if you have a real C64, press the RUN/STOP key) it will start RUNning the program.
Congratulations! You have written your first real BASIC program!
This program is a little boring.
Lets change the program so it will keep printing the text over and over.
In BASIC you can use the \verb:GOTO: command to jump to a line.
Using \verb:GOTO: we can make a loop.
Lets add this line and RUN the program:
\begin{lstlisting}
20 GOTO 10
\end{lstlisting}
You can stop the program by pressing ESCAPE (\verb:STOP: on a real C=64).
\problem Add \verb:;: to line 10 and press enter. What happens if we RUN the program?
\problem Type \verb:LIST: and then \verb:LIST10:. What does this command do?
Whenever you want to write a new program it is a good idea to erase all previous code.
We can reset the emulator to accomplish this, but this is unpractical.
It is much easier to just type \verb:NEW: to start a NEW program.
Be careful with this command, because it does not ask for confirmation!
\subsection{Peeks 'n Pokes}
RUN the program \verb:BORDERKLEUR: located on the slides disk (\verb:roms/workshop/slides.d64:).
\problem What does the \verb:PEEK: commando do on line 20?
Because we have only 16 colors we ignore the upper 4 bits using \verb:AND15:.
\problem What does the \verb:POKE: commando do on line 50?
\problem What information does the magic address \verb:VIC+32: store?
\subsection{Input/Output}
You can read user input using the \verb:INPUT: command.
For example, enter the following program:
\begin{lstlisting}
10 INPUT A$
20 PRINT "YOU TYPED: "; A$
\end{lstlisting}
Typ and RUN the program.
You can enter some text that will be printed to the screen.
\problem Write a BASIC program that asks a username and prints it to the screen.
Now is a good moment to figure out how to save your program to floppydisk so you can load it when you need it.
See section 2 for a cheatsheet for save and load commands.
\subsection{Programmer's Reference Guide and Literature}
This workshop provides a glimpse of BASIC programming and shows how easy it is.
If you would like to write more programs you can start reading at chapter 3 of the \verb:User's Guide:.
If you would like more challenging stuff or want to learn more details about the Commodore 64 you can also look at the \verb:Programmer's Reference Guide:.
Both documents are included if you have downloaded the release version from the project's github page.
\section{Appendix}
\subsection{Commodore 64 programs, games and demo's}
There is a lot of software for the Commodore 64.
In fact, many software is still being written to this day!
The most famous and complete website for roms is \href{http://csdb.dk}{csdb}.
Another real well known, also for programming contests, is \href{https://www.pouet.net}{pouet}.
Lots of Commodore 64 software can be found on that website.
For example, the world's biggest and most recent programming contest for the C64 is X.
\href{https://www.pouet.net/party.php?which=50&when=2016}{Here} you can find an overview of all software that has been released at this party.
\subsection{easy6502}
If you thought the BASIC exercises were boring and are interested in programming in assembly, you can try out \href{https://skilldrick.github.io/easy6502/}{this introduction}.
\subsection{KickAssembler}
If you have completed all exercises and need more challenging problems you can try to start coding in assembly using \href{http://theweb.dk/KickAssembler/Main.html#frontpage}{this assembler}.
A couple of examples and very good documentation is included as well.
These sources are also very important for coding in assembly:
\begin{tabular}{l|l|l}
Link & Domain & Notes \\
\href{http://codebase64.org}{codebase64} & The Programmer's wiki & Tips and examples \\
\href{http://sta.c64.org/cbm64mem.html}{cbm64mem.html} & C64 Memory map & Every memory address with notes \\
\href{https://www.c64-wiki.com}{c64-wiki} & Wikipedia for C64 & Everything about Commodore \\
\end{tabular}
\subsection{Demoscene}
This workshop shows a glimpse about the demoscene that originated primarily by computer clubs of the first Personal Computers.
If you are interested in the demoscene or just want to learn what it really is, then we recommend to take a look at the following:
\begin{tabular}{l|l|l}
Link & Domain & Notes \\
\href{https://www.youtube.com/watch?v=AdTANxS-LHg}{YT: Polderpioniers - J. Tel} & First game music composers & Dutch only! \\
\href{https://www.youtube.com/watch?v=5MexnBunH_g}{YT: The Art of Algorithms} & History of Demoscene & Documentary \\
\href{https://www.youtube.com/watch?v=O-1zEo7DD8w}{YT: Rev2017 Shader Finals} & Programming Contest & Stream \\
\href{https://en.wikipedia.org/wiki/Demoscene}{Wikipedia: Demoscene} & Demoscene crash course & Wikipedia's view \\
\href{https://id.scene.org/}{Scene portal} & Demoscene portal & Account for demoscene sites \\
\href{https://www.youtube.com/watch?v=rFv7mHTf0nA}{Future Crew - Second Reality} & Winner Assembly 1993 & Very famous '90s demo \\
\end{tabular}
\end{document}