-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmakedisk.c
430 lines (363 loc) · 12.3 KB
/
makedisk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
Based on mega65-fdisk program as a starting point.
*/
#include <stdio.h>
#include <string.h>
#include "freezer.h"
#include "freezer_common.h"
#include "fdisk_hal.h"
#include "fdisk_memory.h"
#include "fdisk_screen.h"
#include "fdisk_fat32.h"
void setup_menu_screen(void)
{
POKE(0xD018U, 0x15); // upper case
// NTSC 60Hz mode for monitor compatibility?
// POKE(0xD06FU, 0x80);
// Reset border widths
POKE(0xD05CU, 80);
POKE(0xD05DU, 0xC0);
// No sprites
POKE(0xD015U, 0x00);
// Move screen to SCREEN_ADDRESS
POKE(0xD018U, (((CHARSET_ADDRESS - 0x8000U) >> 11) << 1) + (((SCREEN_ADDRESS - 0x8000U) >> 10) << 4));
POKE(0xDD00U, (PEEK(0xDD00U) & 0xfc) | 0x01);
// 16-bit text mode with full colour for chars >$FF
// (which we will use for showing the thumbnail)
POKE(0xD054U, (PEEK(0xD054) & 0xa8) | 0x05);
POKE(0xD058U, 80);
POKE(0xD059U, 0); // 80 bytes per row
// Fill colour RAM with a value that won't cause problems in Super-Extended Attribute Mode
// lfill(0xff80000U, 1, 2000);
}
void draw_box(
unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char colour, unsigned char erase)
{
unsigned char x, y;
// Clear colour RAM
for (x = x1; x <= x2; x++) {
for (y = y1; y <= y2; y++) {
lpoke(COLOUR_RAM_ADDRESS + y * 80 + x * 2 + 1, colour);
lpoke(COLOUR_RAM_ADDRESS + y * 80 + x * 2 + 0, 0);
}
}
if (erase) {
for (x = x1 + 1; x < x2; x++) {
for (y = y1 + 1; y < y2; y++) {
lpoke(SCREEN_ADDRESS + y * 80 + x * 2 + 0, 0x20);
lpoke(SCREEN_ADDRESS + y * 80 + x * 2 + 1, 0);
}
}
}
for (x = x1; x < x2; x++) {
lpoke(SCREEN_ADDRESS + y1 * 80 + x * 2, 0x40); // horizontal line, centred
lpoke(SCREEN_ADDRESS + y1 * 80 + x * 2 + 1, 0);
lpoke(SCREEN_ADDRESS + y2 * 80 + x * 2, 0x40); // horizontal line, centred
lpoke(SCREEN_ADDRESS + y2 * 80 + x * 2 + 1, 0);
}
for (y = y1; y < y2; y++) {
lpoke(SCREEN_ADDRESS + y * 80 + x1 * 2, 0x42); // vertical line, centred
lpoke(SCREEN_ADDRESS + y * 80 + x1 * 2 + 1, 0);
lpoke(SCREEN_ADDRESS + y * 80 + x2 * 2, 0x42); // vertical line, centred
lpoke(SCREEN_ADDRESS + y * 80 + x2 * 2 + 1, 0);
}
lpoke(SCREEN_ADDRESS + y1 * 80 + x1 * 2, 0x55); // top left corner
lpoke(SCREEN_ADDRESS + y1 * 80 + x2 * 2, 73); // top right corner
lpoke(SCREEN_ADDRESS + y2 * 80 + x1 * 2, 74); // bottom left corner
lpoke(SCREEN_ADDRESS + y2 * 80 + x2 * 2, 75); // bottom right corner
lpoke(SCREEN_ADDRESS + y1 * 80 + x1 * 2 + 1, 0);
lpoke(SCREEN_ADDRESS + y1 * 80 + x2 * 2 + 1, 0);
lpoke(SCREEN_ADDRESS + y2 * 80 + x1 * 2 + 1, 0);
lpoke(SCREEN_ADDRESS + y2 * 80 + x2 * 2 + 1, 0);
}
void write_text(unsigned char x1, unsigned char y1, unsigned char colour, char *t)
{
unsigned char ofs = 0, x, c;
for (x = x1; t[x - x1]; x++) {
c = t[x - x1];
if (c > 0x60)
c -= 0x60;
if (c > 0x40)
c -= 0x40;
lpoke(SCREEN_ADDRESS + y1 * 80 + x * 2 + 0, c);
lpoke(SCREEN_ADDRESS + y1 * 80 + x * 2 + 1, 0);
lpoke(COLOUR_RAM_ADDRESS + y1 * 80 + x * 2 + 0, 0x00);
lpoke(COLOUR_RAM_ADDRESS + y1 * 80 + x * 2 + 1, colour);
}
}
void input_text(unsigned char x1, unsigned char y1, unsigned char len, unsigned char colour, char *out)
{
unsigned char ofs = 0, x, c;
for (x = x1; x < (x1 + len); x++) {
lpoke(SCREEN_ADDRESS + y1 * 80 + x1 * 2 + 0, ' ');
lpoke(SCREEN_ADDRESS + y1 * 80 + x1 * 2 + 1, 0);
lpoke(COLOUR_RAM_ADDRESS + y1 * 80 + x1 * 2 + 0, 0x00);
lpoke(COLOUR_RAM_ADDRESS + y1 * 80 + x1 * 2 + 1, colour);
}
out[0] = 0;
while (1) {
// Enable cursor on current char
lpoke(COLOUR_RAM_ADDRESS + y1 * 80 + (x1 + ofs) * 2 + 1, colour | 0x30);
c = PEEK(0xD610);
if (c >= 0x41 && c <= 0x5a || c >= 0x61 && c <= 0x7a || c >= 0x30 && c <= 0x39) {
if (ofs < len) {
out[ofs] = c;
// ASCII to screen code conversion
if (c > 0x60) {
c -= 0x60;
}
lpoke(SCREEN_ADDRESS + y1 * 80 + (x1 + ofs) * 2 + 0, c);
lpoke(COLOUR_RAM_ADDRESS + y1 * 80 + (x1 + ofs) * 2 + 1, colour);
ofs++;
}
}
else {
switch (c) {
case 0x14: // delete
// XXX actually copy chars down, instead of just erasing from
// end of line, and allow cursor left and right
lpoke(SCREEN_ADDRESS + y1 * 80 + (x1 + ofs) * 2 + 0, ' ');
lpoke(COLOUR_RAM_ADDRESS + y1 * 80 + (x1 + ofs) * 2 + 1, colour);
if (ofs)
ofs--;
lpoke(SCREEN_ADDRESS + y1 * 80 + (x1 + ofs) * 2 + 0, ' ');
lpoke(COLOUR_RAM_ADDRESS + y1 * 80 + (x1 + ofs) * 2 + 1, colour);
break;
case 0x03:
out[0] = 0;
POKE(0xD610, 0);
return;
case 0x0d:
out[ofs] = 0;
POKE(0xD610, 0);
return;
}
}
if (c)
POKE(0xD610, 0);
}
}
char hexchar(unsigned char v)
{
v = v & 0xf;
if (v < 10)
return '0' + v;
return 0x41 + v - 10;
}
void hexout(char *m, unsigned long v, int n)
{
if (!n)
return;
do {
m[n - 1] = hexchar(v);
v = v >> 4L;
} while (--n);
}
char msg[80];
// clang-format off
unsigned char bam_sector1[0x100] = {
0x28, 0x02, 0x44, 0xbb, 0x39, 0x38, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff,
0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff,
0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff,
0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff,
0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff,
0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff,
0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff,
0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff,
0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff,
0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff,
0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff,
0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff,
0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff,
0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x28, 0xff,
0xff, 0xff, 0xff, 0xff, 0x28, 0xff, 0xff, 0xff, 0xff, 0xff, 0x24, 0xf0, 0xff, 0xff, 0xff, 0xff
};
// clang-format on
unsigned char to_hex(unsigned char i)
{
if (i < 10)
return '0' + i;
return 0x41 + i - 10;
}
void format_disk_image(unsigned long file_sector, char *diskname, unsigned char isD65)
{
unsigned char i;
unsigned short s;
unsigned short sect_count = 80 * 20;
if (isD65)
sect_count = 85 * 64;
// Make sure entire image is empty
clear_sector_buffer();
for (s = 0; s < sect_count; s++) {
// XXX - Using multi-sector writes here would be much faster
sdcard_writesector(file_sector + s, 0);
}
// Link to first directory sector
sector_buffer[0] = 0x28;
sector_buffer[1] = 0x03;
// DOS Version
sector_buffer[2] = 0x44;
sector_buffer[3] = 0x00;
// Diskname
lcopy((long)diskname, (long)§or_buffer[4], 16);
if (strlen(diskname) < 16) {
for (i = strlen(diskname); i < 16; i++)
sector_buffer[4 + i] = 0xa0;
}
sector_buffer[0x14] = 0xa0;
sector_buffer[0x15] = 0xa0;
// Random disk ID
i = PEEK(0xD012);
sector_buffer[0x16] = to_hex(i & 0xf);
sector_buffer[0x17] = to_hex(i >> 4);
sector_buffer[0x18] = 0xa0;
// DOS type
sector_buffer[0x19] = 0x31;
sector_buffer[0x1A] = 0x44;
sector_buffer[0x1B] = 0xa0;
sector_buffer[0x1C] = 0xa0;
lcopy((long)bam_sector1, (long)§or_buffer[0x100], 0x100);
// Disk ID in BAM
sector_buffer[0x104] = to_hex(i & 0xf);
sector_buffer[0x105] = to_hex(i >> 4);
if (!isD65)
sdcard_writesector(file_sector + (39 * 10 * 2 + 0), 0);
else
sdcard_writesector(file_sector + (39 * 64 * 2 + 0), 0);
clear_sector_buffer();
lcopy((long)bam_sector1, (long)sector_buffer, 0x100);
// Disk ID in BAM
sector_buffer[0x004] = to_hex(i & 0xf);
sector_buffer[0x005] = to_hex(i >> 4);
sector_buffer[0x101] = 0xff;
// Link to first sector of dir
sector_buffer[0x000] = 0x00;
sector_buffer[0x001] = 0xFF;
// Mark all sectors free in 2nd half of disk
sector_buffer[0x0FA] = 40;
sector_buffer[0x0FB] = 0xff;
if (!isD65)
sdcard_writesector(file_sector + (39 * 10 * 2 + 1), 0);
else
sdcard_writesector(file_sector + (39 * 64 * 2 + 1), 0);
}
void do_make_disk_image(unsigned char isD65, unsigned char drive_id)
{
char diskname[16 + 1];
char filename[16 + 1];
unsigned char filename_len;
unsigned short slot_number = 0;
unsigned long file_sector;
fat32_open_file_system();
if (!fat1_sector) {
draw_box(10, 8, 30, 13, 2, 1);
write_text(11, 9, 7, "COULD NOT FIND SD CARD");
while (!PEEK(0xD610))
continue;
POKE(0xD610, 0);
return;
}
draw_box(10, 8, 30, 14, 14, 1);
write_text(11, 9, 14, "ENTER NAME FOR");
if (isD65)
write_text(11, 10, 14, "HD (D65) IMAGE:");
else
write_text(11, 10, 14, "DD (D81) IMAGE:");
input_text(11, 12, 8, 1, filename);
for (filename_len = 0; filename[filename_len]; filename_len++) {
// Convert to upper case and work out length of string
if (filename[filename_len] >= 0x61 && filename[filename_len] <= 0x7a)
filename[filename_len] -= 0x20;
}
if (!filename_len)
return;
// Copy filename into diskname before it gets extended by the filename extension
strcpy(diskname, filename);
filename[filename_len++] = '.';
filename[filename_len++] = 0x44;
if (isD65) {
filename[filename_len++] = 0x36;
filename[filename_len++] = 0x35;
}
else {
filename[filename_len++] = 0x38;
filename[filename_len++] = 0x31;
}
filename[filename_len] = 0;
lcopy((long)filename, 0x0400, 16);
draw_box(10, 8, 30, 14, 7, 1);
write_text(11, 9, 7, "CREATING IMAGE...");
// Actually create the file
// while(!PEEK(0xD610)) POKE(0xD020,PEEK(0xD020)+1); POKE(0xD610,0);
file_sector = fat32_create_contiguous_file(
filename, isD65 ? (85 * 64 * 2 * 512L) : (80 * 10 * 2 * 512L), root_dir_sector, fat1_sector, fat2_sector);
if (!file_sector) {
// Error making file
draw_box(10, 8, 30, 14, 2, 1);
write_text(11, 9, 2, "Error creating file");
write_text(11, 12, 1, "Press almost any key...");
while (!PEEK(0xD610))
continue;
POKE(0xD610, 0);
}
else {
// File creation succeeded
// Write header, BAM and zero out directory track
write_text(11, 10, 14, "FORMATTING IMAGE...");
format_disk_image(file_sector, diskname, isD65);
draw_box(8, 8, 32, 14, 13, 1);
write_text(9, 9, 13, "Created disk image");
// now mount the new image on the drive_id we got
mega65_dos_attach(filename, drive_id);
// store mount to freeze slot
copy_imageproc_to_freezeregion(drive_id, 0);
write_text(9, 12, 1, "Press almost any key...");
while (!PEEK(0xD610))
continue;
POKE(0xD610, 0);
}
}
#ifdef __CC65__
void main(void)
#else
int main(int argc, char **argv)
#endif
{
#ifdef __CC65__
mega65_fast();
#endif
// Disable interrupts and interrupt sources
__asm__("sei");
POKE(0xDC0DU, 0x7F);
POKE(0xDD0DU, 0x7F);
POKE(0xD01AU, 0x00);
// XXX add missing C65 AND M65 peripherals
// C65 UART, ethernet etc
// Bank out BASIC ROM, leave KERNAL and IO in
POKE(0x00, 0x3F);
POKE(0x01, 0x36);
// No decimal mode!
__asm__("cld");
// Enable extended attributes so we can use reverse
POKE(0xD031U, PEEK(0xD031U) | 0x20);
// Correct horizontal scaling
POKE(0xD05AU, 0x78);
// Silence SIDs
POKE(0xD418U, 0);
POKE(0xD438U, 0);
set_palette();
// Now find the start sector of the slot, and make a copy for safe keeping
slot_number = 0;
find_freeze_slot_start_sector(slot_number);
freeze_slot_start_sector = *(uint32_t *)0xD681U;
// SD or SDHC card?
if (PEEK(0xD680U) & 0x10)
sdhc_card = 1;
else
sdhc_card = 0;
setup_menu_screen();
request_freeze_region_list();
do_make_disk_image(PEEK(0x33C) ? 1 : 0, PEEK(0x3C0) ? 1 : 0); // 0=DD, 1=HD
mega65_dos_exechelper("FREEZER.M65");
}