-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfdisk_hal_mega65.c
435 lines (359 loc) · 10.1 KB
/
fdisk_hal_mega65.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
431
432
433
434
435
#include <stdio.h>
#include <stdlib.h>
#include "fdisk_hal.h"
#include "fdisk_memory.h"
#include "fdisk_screen.h"
#include "ascii.h"
#define POKE(X, Y) (*(unsigned char *)(X)) = Y
#define PEEK(X) (*(unsigned char *)(X))
const long sd_sectorbuffer = 0xffd6e00L;
const uint16_t sd_ctl = 0xd680L;
const uint16_t sd_addr = 0xd681L;
const uint16_t sd_errorcode = 0xd6daL;
unsigned char sdhc_card = 0;
uint8_t hal_border_flicker = 0;
// Tell utilpacker what our display name is
const char *prop_m65u_name = "PROP.M65U.NAME=SDCARD FDISK+FORMAT UTILITY";
void usleep(uint32_t micros)
{
// Sleep for desired number of micro-seconds.
// Each VIC-II raster line is ~64 microseconds
// this is not totally accurate, but is a reasonable approach
while (micros > 64) {
uint8_t b = PEEK(0xD012);
while (PEEK(0xD012) == b)
continue;
micros -= 64;
}
return;
}
/*
* sdcard_visual_feedback(do_flicker)
*
* do_flicker 0 == no border change
* 1 == only success/fail display
* 2 == full progress
*/
void sdcard_visual_feedback(const uint8_t do_flicker)
{
hal_border_flicker = do_flicker < 3 ? do_flicker : 2;
}
void sdcard_reset(void)
{
// Reset and release reset
// write_line("Resetting SD card...",0);
POKE(sd_ctl, 0);
POKE(sd_ctl, 1);
// Now wait for SD card reset to complete
while (PEEK(sd_ctl) & 3)
if (hal_border_flicker > 1)
POKE(0xd020, (PEEK(0xd020) + 1) & 0xf);
if (sdhc_card) {
// Set SDHC flag (else writing doesnt work for some reason)
// write_line("Setting SDHC mode",0);
POKE(sd_ctl, 0x41);
}
}
void mega65_fast(void)
{
// Fast CPU
POKE(0, 65);
// MEGA65 IO registers
POKE(0xD02FU, 0x47);
POKE(0xD02FU, 0x53);
}
void sdcard_open(void)
{
sdcard_reset();
}
uint32_t write_count = 0;
void sdcard_map_sector_buffer(void)
{
m65_io_enable();
POKE(sd_ctl, 0x81);
}
void sdcard_unmap_sector_buffer(void)
{
m65_io_enable();
POKE(sd_ctl, 0x82);
}
unsigned short timeout;
void sdcard_readsector(const uint32_t sector_number)
{
char tries = 0;
uint32_t sector_address = sector_number * 512;
if (sdhc_card)
sector_address = sector_number;
else {
if (sector_number >= 0x7fffff) {
// write_line("ERROR: Asking for sector @ >= 4GB on SDSC card.",0);
while (1)
continue;
}
}
POKE(sd_addr + 0, (sector_address >> 0) & 0xff);
POKE(sd_addr + 1, (sector_address >> 8) & 0xff);
POKE(sd_addr + 2, ((uint32_t)sector_address >> 16) & 0xff);
POKE(sd_addr + 3, ((uint32_t)sector_address >> 24) & 0xff);
// write_line("Reading sector @ $",0);
// screen_hex(screen_line_address-80+18,sector_address);
while (tries < 10) {
// Wait for SD card to be ready
timeout = 50000U;
while (PEEK(sd_ctl) & 0x3) {
timeout--;
if (!timeout) {
// Time out -- so reset SD card
POKE(sd_ctl, 0);
POKE(sd_ctl, 1);
timeout = 50000U;
}
if (PEEK(sd_ctl) & 0x40) {
return;
}
// Sometimes we see this result, i.e., sdcard.vhdl thinks it is done,
// but sdcardio.vhdl thinks not. This means a read error
if (PEEK(sd_ctl) == 0x01)
return;
}
// Command read
POKE(sd_ctl, 2);
// Wait for read to complete
timeout = 50000U;
while (PEEK(sd_ctl) & 0x3) {
timeout--;
if (!timeout)
return;
// write_line("Waiting for read to complete",0);
if (PEEK(sd_ctl) & 0x40) {
return;
}
// Sometimes we see this result, i.e., sdcard.vhdl thinks it is done,
// but sdcardio.vhdl thinks not. This means a read error
if (PEEK(sd_ctl) == 0x01)
return;
}
// Note result
// result=PEEK(sd_ctl);
if (!(PEEK(sd_ctl) & 0x67)) {
// Copy data from hardware sector buffer via DMA
lcopy(sd_sectorbuffer, (long)sector_buffer, 512);
return;
}
if (hal_border_flicker > 1)
POKE(0xd020, (PEEK(0xd020) + 1) & 0xf);
// Reset SD card
sdcard_open();
tries++;
}
}
uint8_t verify_buffer[512];
void sdcard_writesector(const uint32_t sector_number, uint8_t is_multi)
{
// Copy buffer into the SD card buffer, and then execute the write job
uint32_t sector_address;
int i;
char tries = 0, result;
uint16_t counter = 0;
// Set address to read/write
POKE(sd_ctl, 1); // end reset
if (!sdhc_card)
sector_address = sector_number * 512;
else
sector_address = sector_number;
POKE(sd_addr + 0, (sector_address >> 0) & 0xff);
POKE(sd_addr + 1, (sector_address >> 8) & 0xff);
POKE(sd_addr + 2, (sector_address >> 16) & 0xff);
POKE(sd_addr + 3, (sector_address >> 24) & 0xff);
// Read the sector and see if it already has the correct contents.
// If so, nothing to write
POKE(sd_ctl, 2); // read the sector we just wrote
counter = 0;
while (PEEK(sd_ctl) & 3) {
if (hal_border_flicker > 1)
POKE(0xD020, (PEEK(0xd020) + 1) & 0xf);
counter++;
if (!counter) {
// SD card not becoming ready: try reset
POKE(sd_ctl, 0); // begin reset
usleep(500000);
POKE(sd_ctl, 1); // end reset
POKE(sd_ctl, 2);
}
}
// Copy the read data to a buffer for verification
lcopy(sd_sectorbuffer, (long)verify_buffer, 512);
// VErify that it matches the data we wrote
for (i = 0; i < 512; i++) {
if (sector_buffer[i] != verify_buffer[i])
break;
}
if (i == 512) {
return;
}
while (tries < 10) {
// Copy data to hardware sector buffer via DMA
lcopy((long)sector_buffer, sd_sectorbuffer, 512);
// Wait for SD card to be ready
counter = 0;
while (PEEK(sd_ctl) & 3) {
counter++;
if (!counter) {
// SD card not becoming ready: try reset
POKE(sd_ctl, 0); // begin reset
usleep(500000);
POKE(sd_ctl, 1); // end reset
POKE(sd_ctl, 0x57); // Open SD card write gate
if (is_multi)
POKE(sd_ctl, 4);
else
POKE(sd_ctl, 3); // retry write
}
// Show we are doing something
// POKE(0x804f,1+(PEEK(0x804f)&0x7f));
}
// Command write
POKE(sd_ctl, 0x57); // Open SD card write gate
if (is_multi)
POKE(sd_ctl, 4);
else
POKE(sd_ctl, 3);
// Wait for write to complete
counter = 0;
while (PEEK(sd_ctl) & 3) {
counter++;
if (!counter) {
// SD card not becoming ready: try reset
POKE(sd_ctl, 0); // begin reset
usleep(500000);
POKE(sd_ctl, 1); // end reset
// Retry write
POKE(sd_ctl, 0x57); // Open SD card write gate
if (is_multi)
POKE(sd_ctl, 4);
else
POKE(sd_ctl, 3);
}
// Show we are doing something
// POKE(0x809f,1+(PEEK(0x809f)&0x7f));
}
write_count++;
if (hal_border_flicker > 1)
POKE(0xD020, write_count & 0x0f);
// Note result
result = PEEK(sd_ctl);
if (!(PEEK(sd_ctl) & 0x67)) {
write_count++;
if (hal_border_flicker > 1)
POKE(0xD020, write_count & 0x0f);
// There is a bug in the SD controller: You have to read between writes, or it
// gets really upset.
// But sometimes even that doesn't work, and we have to reset it.
// Does it just need some time between accesses?
POKE(sd_ctl, 2); // read the sector we just wrote
while (PEEK(sd_ctl) & 3) {
continue;
}
// Copy the read data to a buffer for verification
lcopy(sd_sectorbuffer, (long)verify_buffer, 512);
// VErify that it matches the data we wrote
for (i = 0; i < 512; i++) {
if (sector_buffer[i] != verify_buffer[i])
break;
}
if (i != 512) {
// VErify error has occurred
// write_line("Verify error for sector $$$$$$$$",0);
screen_hex(screen_line_address - 80 + 24, sector_number);
}
else {
// write_line("Wrote sector $$$$$$$$, result=$$",2);
// screen_hex(screen_line_address-80+2+14,sector_number);
// screen_hex(screen_line_address-80+2+30,result);
return;
}
}
if (hal_border_flicker > 1)
POKE(0xd020, (PEEK(0xd020) + 1) & 0xf);
}
// write_line("Write error @ $$$$$$$$$",2);
// screen_hex(screen_line_address-80+2+16,sector_number);
}
void sdcard_writenextsector(void)
{
// Copy data to hardware sector buffer via DMA
lcopy((long)sector_buffer, sd_sectorbuffer, 512);
// Command write of follow-on block in multi-block write job
while (PEEK(sd_ctl) & 3) {
continue;
}
POKE(sd_ctl, 0x57); // Open SD card write gate
POKE(sd_ctl, 5);
while (!(PEEK(sd_ctl) & 3)) {
continue;
}
while (PEEK(sd_ctl) & 3) {
continue;
}
}
void sdcard_writemultidone(void)
{
while (PEEK(sd_ctl) & 3) {
continue;
}
POKE(sd_ctl, 0x57);
POKE(sd_ctl, 6);
while (!(PEEK(sd_ctl) & 3)) {
continue;
}
while (PEEK(sd_ctl) & 3) {
continue;
}
}
void sdcard_erase(const uint32_t first_sector, const uint32_t last_sector)
{
uint32_t n;
clear_sector_buffer();
// fprintf(stderr,"ERASING SECTORS %d..%d\r\n",first_sector,last_sector);
#ifndef NOFAST_ERASE
POKE(sd_addr + 0, (first_sector >> 0) & 0xff);
POKE(sd_addr + 1, (first_sector >> 8) & 0xff);
POKE(sd_addr + 2, (first_sector >> 16) & 0xff);
POKE(sd_addr + 3, (first_sector >> 24) & 0xff);
#endif
for (n = first_sector; n <= last_sector; n++) {
#ifndef NOFAST_ERASE
// Wait for SD card to go ready
while (PEEK(sd_ctl) & 3)
continue;
if (n == first_sector) {
// First sector of multi-sector write
POKE(sd_ctl, 0x04);
}
else
// All other sectors
POKE(sd_ctl, 0x05);
// Wait for SD card to go busy
while (!(PEEK(sd_ctl) & 3))
continue;
// Wait for SD card to go ready
while (PEEK(sd_ctl) & 3)
continue;
#else
sdcard_writesector(n);
#endif
// Show count-down
screen_decimal(screen_line_address, last_sector - n);
// fprintf(stderr,"."); fflush(stderr);
}
#ifndef NOFAST_ERASE
// Then say when we are done
POKE(sd_ctl, 0x06);
// Wait for SD card to go busy
while (!(PEEK(sd_ctl) & 3))
continue;
// Wait for SD card to go ready
while (PEEK(sd_ctl) & 3)
continue;
#endif
}