forked from MEGA65/mega65-user-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodes.tex
313 lines (254 loc) · 13.7 KB
/
modes.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
309
310
\chapter {C64, C65 and MEGA65 Modes}
\label{cha:modes}
The MEGA65, like the C65 and the C128, has multiple operating modes.
However, there are important differences between the MEGA65 and both
of these earlier computers.
By default, the {\tt MEGA65.ROM} file boots to MEGA65-mode
(including BASIC 65), and
provides a method to switch to C64-mode via the \screentext{GO 64} command.
However, it is also possible to use an original C65 ROM (version {\tt 91XXXX.BIN})
renamed to {\tt MEGA65.ROM}, making the MEGA65 start in C65-mode
with BASIC 10. This also provides the same functionality to switch to C64-mode.
Therefore, dependent on your boot ROM choice, you have:
\begin{center}
\begin{tabular}{|l|l|l|l|}
\hline
{\textbf{Boot Mode}} & {\textbf{ROM version}} & {\textbf{BASIC}} & {\textbf{C64-mode}} \\
\hline
MEGA65 & \screentext{92XXXX} & BASIC 65 & \screentext{GO 64} \\
C65 & \screentext{91XXXX} & BASIC 10 & \screentext{GO 64} \\
\hline
\end{tabular}
\end{center}
For readers familiar with the C128,
the most important difference is that all of the MEGA65's new features
can be accessed from every mode, and that you can even switch back and forth
between the different modes. It's also possible to create hybrid modes that combine different
features from the different modes -- all you need is the {\bf MAP} instruction and the KEY register address,
which is {\bf 53295} (\$D02F).
This chapter explains the different modes, the {\bf MAP} instruction, and
the KEY register, which allows you to change the mode of operation of the MEGA65.
This chapter also explains how to use BASIC commands to switch from one mode to another.
\phantomsection
\section{Switching Modes from BASIC}
The MEGA65 is used in either C64-mode (running BASIC 2), C65-mode (running BASIC 10) for ROM versions {\tt 91XXXX},
or MEGA65-mode (running BASIC 65) for ROM versions {\tt 92XXXX}.
However, various MEGA65 features can be accessed from all modes, and all MEGA65 features are available
to programs written in assembly language / machine code.
\ifdefined\printmanual
For more information on how to write such programs, refer to the {\bf MEGA65 Book}.
\else
More information on how to write such programs can be found in the various appendices.
\fi
\subsection{From MEGA65/C65 to C64-mode}
To switch from MEGA65/C65 to C64-mode, use the familiar \screentext{GO 64} command, which is identical to switching to C64
mode on a C128:
\index{BASIC 65 Examples!GO64}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
GO 64
ARE YOU SURE? Y
\end{verbatim}
\end{tcolorbox}
Note that any programs in memory will be lost in the process of switching modes. This is the same as the C128.
\index{Keyboard!MEGA Key}
Alternatively, you can hold \megasymbolkey down while pressing the reset button or switching the MEGA65 on. Again,
this is the same as the C128.
\subsection{From C64 to MEGA65/C65-mode}
To switch from C64 to MEGA65/C65-mode, use the following command. {\bf Note that this command does not ask you for
confirmation}!
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
SYS 58552
\end{verbatim}
\end{tcolorbox}
Alternatively, you can switch back to MEGA65/C65-mode by pressing the reset
button on the left-hand side of the MEGA65, or by switching the
MEGA65 off and on again.
Another option is to press and hold \widekey{RESTORE} for between half to one second, and then press \megakey {F5}
from the Freeze Menu. This simulates pressing the reset button.
Note that any programs in memory will be lost in the process of switching modes. This is the same as the C128.
\subsection{Entering Machine Code Monitor Mode}
The Machine Code Monitor can be entered by typing either the {\bf MONITOR}
command from BASIC 65/10, or by holding \specialkey{RUN STOP}
down, and then pressing the reset button on the left-hand side of the
MEGA65.
\phantomsection
\section{The KEY Register}
The MEGA65 has a VIC-IV video controller chip instead of the C64's VIC-II or
the C65's VIC-III. Just as the VIC-III has extra registers compared to the
VIC-II, the VIC-IV has even more registers. If these were visible all the time,
software that was made for the C64 and VIC-II may inadvertently use these
new registers, resulting in unexpected behaviour. Therefore, the
creators of the C65 created a way to hide the extra VIC-III registers from old
C64 programs. Enabling and disabling (or hiding and un-hiding) the extra registers is done via the KEY register. For more information
about which registers are disabled and enabled in each of the
VIC-II, VIC-III and VIC-IV I/O modes, refer to
\ifdefined\printmanual
the {\bf MEGA65 Book}.
\else
\bookvref{sec:iopersonalities}.
\fi
% iopersonalities results in ?? (??) when building on my machine
The KEY register, located at address {\bf 53295}, is an unused register of the VIC-II, which you can {\bf POKE} to and
{\bf PEEK} from, similar to other registers. But the KEY register has a special function: If
you write two certain values to it in quick succession, you can tell the VIC-IV
to stop hiding the VIC-III or VIC-IV registers from the rest of the MEGA65.
\subsection{Exposing Extra C65 Registers}
For example, to enable the VIC-III's new registers when in C64-mode, you must {\bf POKE} the values 165 and 150
into the KEY register. The easiest way to do this is to switch your MEGA65 off and on again, and type GO 64
and answer Y to enter C64-mode.
\underline{Note}: If you perform these POKEs while in C65-mode, the MEGA65 may not function correctly.
Once you are in C64-mode, try typing the following commands:
\index{BASIC 65 Examples!POKE}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
POKE 53295,165: POKE 53295,150
\end{verbatim}
\end{tcolorbox}
When you enter these commands, the MEGA65 returns a \screentextwide{READY.} prompt, and seemingly nothing else has
happened. This is expected, because the MEGA65 has only enabled the VIC-III's new registers (and some other
C65-mode features). The C64 BASIC and KERNAL will still function as normal, and it may appear
that nothing has changed... But things \textit{have} changed.
For example, you can do something that the C64 and its VIC-II can't do: smoothly change one colour to another.
The VIC-III has registers that allow you to change the red, green and blue components of the colours. Now that the VIC-III
registers are enabled, it's possible to change the colour of the background progressively from blue to purple, by increasing
the red component of the colour that is normally blue on the C64. The red component value registers are at
53504 -- 53759 (\$D100 -- \$D1FF). Blue is colour 6, so a change to register 53510 (53504 + 6, or \$D106) is required.
An example BASIC listing that includes a {\bf FOR} loop to change the colour is:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
FOR I = 0 TO 15 STEP 0.2 : POKE 53510,I : NEXT
\end{verbatim}
\end{tcolorbox}
Once the program has been entered, type {\bf RUN} on a new line. This will make the background of the screen fade from
blue to purple. If you would like to make the effect progress faster, increase the 0.2 to a larger number such as 0.5. To
make it slower, change it to a smaller number such as 0.02. You can also change the red component by {\bf POKE}ing a
different number to 53504 – 53759 (\$D100 – \$D1FF), the green component at 53760 -- 54015 (\$D200 -- \$D2FF), or the
blue component at 54016 -- 54271 (\$D300 -- \$D3FF). For example, to have
the border and text (since they are both normally ``light blue'') fade from blue to green, you can try:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
POKE 53518,0 : FOR I = 0 TO 15 STEP 0.1 : POKE 53774,I : POKE 54030,15-I : NEXT
\end{verbatim}
\end{tcolorbox}
\subsection{Disabling the C65/MEGA65 Extra Registers}
You can also disable the VIC-III registers again by {\bf POKE}ing any number into the KEY register, e.g.:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
POKE 53295,0
\end{verbatim}
\end{tcolorbox}
If you {\bf RUN} the examples above again, the colours won't change because
the registers are disabled. Instead, writing to those addresses changes some of the VIC-II's registers,
as on a C64 they appear several times over. Fortunately for the above example, the registers used have no obvious
side-effects. This is because the modified registers in the examples above on a standard VIC-II are used to change the
sprite positions. Since there are no sprites on the screen, you won't see anything change.
\subsection{Enabling MEGA65 Extra Registers}
The MEGA65 has \textit{even more} registers than the C65. To enable these in C64-mode, it's required to {\bf POKE} another
two values into the KEY register:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
POKE 53295,71: POKE 53295,83
\end{verbatim}
\end{tcolorbox}
Again, you won't see any immediate difference, which is similar to when enabling the VIC-III registers. However, now the
MEGA65 can access not only the VIC-II and VIC-III registers, but also the VIC-IV registers. If you like,
you can try the examples from earlier in this chapter to see that the VIC-III registers are accessible again.
But now you can also do MEGA65 specific things. For example, if you wanted to move the start of the top border higher
on the screen, you can try something such as:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
POKE 53320,60
\end{verbatim}
\end{tcolorbox}
Alternatively, you can have some fun and animate the screen borders, by having them move closer and further apart:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
FOR I = 255 TO 0 STEP -1 : POKE 53320,I : POKE 53322, 255 - I : NEXT
\end{verbatim}
\end{tcolorbox}
The above example has the loop count backwards (from 255 to 0), so that your don't end up with only a
tiny sliver of the text visible. You can make it go forwards if you like. If you do get stuck with only a sliver
of the screen, you can press \specialkey{RUN STOP} and \widekey{RESTORE}. You might be wondering: Why does \specialkey{RUN STOP}
and \widekey{RESTORE} work when these are VIC-IV registers that the C64-mode BASIC and KERNAL
don't know about? The reason is the VIC-IV has a feature called ``hot registers'',
where certain C64 and C65 registers cause some MEGA65 registers to be reset to the C64 or
C65-mode defaults. In this particular case, it is the KERNAL resetting the VIC-II screen using
53265 (\$D011), which adjusts the vertical border size in C64/C65-mode, and is thus a ``hot register''
for the MEGA65's vertical border position registers.
See if you can make the screen shake around instead by changing the TEXTXPOS and TEXTYPOS registers of
the VIC-IV. You can find out the {\bf POKE} codes for those, and lots of other interesting new registers
by looking through
\ifdefined\printmanual
the {\bf MEGA65 Book}.
\else
\bookvref{cha:viciv}.
\fi
\subsection{Traps to Look Out For}
In all modes, the DOS for the internal 3.5'' disk drive (including when you use D81 disk images from
an SD card) resets the KEY register to VIC-II mode whenever it is accessed. This means if you perform actions
such as check the drive status, or {\bf LOAD} or {\bf SAVE} a file, the KEY register will be reset, and only the VIC-II registers
will be enabled. You can of course enable the C65 or MEGA65 registers by {\bf POKE}ing the correct values
to the KEY register again.
\phantomsection
\section{Accessing Memory from BASIC 65}
BASIC 65 contains powerful memory banking and Direct Memory Access (DMA) commands that can be used to read,
fill, copy, and write areas of memory beyond the C65's 128KB of RAM. The MEGA65 has 384KB of main memory, split into 6
banks of 64KB each. They are:
\begin{itemize}
\item BANK 0 and BANK 1 - acts as the C65's normal 128KB RAM.
\item BANK 2 and BANK 3 - normally write-protected, and contains the C65's ROM image.
\item BANK 4 and BANK 5 - used for all graphic routines in BASIC 65 for high resolution bitplane graphics.
BASIC 10 doesn't use banks 4 and 5.
\end{itemize}
\index{BASIC 65 Examples!BANK}
\index{BASIC 65 Examples!PEEK}
\index{BASIC 65 Examples!POKE}
Using the {\bf BANK}, {\bf PEEK} and {\bf POKE} commands, this region of memory can be easily accessed, for example:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
BANK 4: POKE0,123: REM PUT 123 IN LOCATION $40000
BANK 4: PRINT PEEK(0): REM SHOW CONTENTS OF LOCATION $40000
\end{verbatim}
\end{tcolorbox}
Or, by using the {\bf DMA} command, you can copy the current contents of the screen and colour RAM into BANK 4 with:
\index{BASIC 65 Examples!DMA}
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
DMA 0, 2000, 2048, 0, 0, 4 : REM SCREEN TEXT TO BANK 4
DMA 0, 2000, DEC("F800"), 1, 2000, 4 : REM COPY COLOUR RAM TO BANK 4
\end{verbatim}
\end{tcolorbox}
You can then put something else on the screen, and copy it back with:
\begin{tcolorbox}[colback=black,coltext=white]
\verbatimfont{\codefont}
\begin{verbatim}
DMA 0, 2000, 0, 4, 2048, 0 : REM SCREEN TEXT FROM BANK 4
DMA 0, 2000, 2000, 4, DEC("F800"), 1 : REM COPY COLOUR RAM FROM BANK 4
\end{verbatim}
\end{tcolorbox}
\index{BASIC 65 Examples!MAP}
\phantomsection
\section{The MAP Instruction}
The above methods can be used from BASIC. In contrast, the {\bf MAP} instruction is an assembly
language instruction that can be used to rearrange the memory that the MEGA65 uses.
It is used by the C65 ROM and BASIC 65 to manage what memory it can use at any particular
point in time. For further explanation of the {\bf MAP} instruction,
refer to the relevant section of
\ifdefined\printmanual
the {\bf MEGA65 Book}.
\else
\bookvref{sec:map-instruction}.
\fi