forked from whizzosoftware/SIO2Arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSIO2Arduino.ino
359 lines (314 loc) · 9.32 KB
/
SIO2Arduino.ino
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
/*
* sio2arduino.ino - An Atari 8-bit device emulator for Arduino.
*
* Copyright (c) 2012 Whizzo Software LLC (Daniel Noguerol)
*
* This file is part of the SIO2Arduino project which emulates
* Atari 8-bit SIO devices on Arduino hardware.
*
* SIO2Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* SIO2Arduino is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SIO2Arduino; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <SdFat.h>
#include "atari.h"
#include "sio_channel.h"
#include "disk_drive.h"
#ifdef LCD_DISPLAY
#include <LiquidCrystal.h>
#endif
/**
* Function declarations
*/
DriveStatus* getDeviceStatus(int deviceId);
SectorDataInfo* readSector(int deviceId, unsigned long sector, byte *data);
boolean writeSector(int deviceId, unsigned long sector, byte* data, unsigned long length);
boolean format(int deviceId, int density);
int getFileList(int startIndex, int count, FileEntry *entries);
void mountFileIndex(int deviceId, int ix);
void changeDirectory(int ix);
/**
* Global variables
*/
DriveAccess driveAccess(getDeviceStatus, readSector, writeSector, format);
DriveControl driveControl(getFileList, mountFileIndex, changeDirectory);
SIOChannel sioChannel(PIN_ATARI_CMD, &SIO_UART, &driveAccess, &driveControl);
SdFat32 card;
SdFile currDir;
SdFile file; // TODO: make this unnecessary
DiskDrive drive1;
#ifdef SELECTOR_BUTTON
boolean isSwitchPressed = false;
unsigned long lastSelectionPress;
boolean isFileOpened=false;
#endif
#ifdef RESET_BUTTON
unsigned long lastResetPress;
#endif
#ifdef LCD_DISPLAY
LiquidCrystal lcd(PIN_LCD_RD,PIN_LCD_ENABLE,PIN_LCD_DB4,PIN_LCD_DB5,PIN_LCD_DB6,PIN_LCD_DB7);
#endif
void setup() {
#ifdef DEBUG
// set up logging serial port
LOGGING_UART.begin(115200);
while (!LOGGING_UART) {
yield();
}
LOG_MSG_CR(F("Debug logging enabled"));
#ifdef ARDUINO_UNO
LOG_MSG_CR(F("WARNING: using debug logging with Uno, which only has 1 UART - SIO will conflict"));
LOG_MSG_FLUSH(); // make sure this log displays before making the SIO connection
#endif
#endif
// initialize serial port to Atari
SIO_UART.begin(19200);
// set pin modes
#ifdef SELECTOR_BUTTON
pinMode(PIN_SELECTOR, INPUT_PULLUP);
#endif
#ifdef RESET_BUTTON
pinMode(PIN_RESET, INPUT_PULLUP);
#endif
#ifdef LCD_DISPLAY
// set up LCD if appropriate
lcd.begin(16, 2);
lcd.print(F("SIO2Arduino"));
lcd.setCursor(0,1);
#endif
// initialize SD card
LOG_MSG(F("Initializing SD card..."));
pinMode(PIN_SD_CS, OUTPUT);
if (!card.begin(PIN_SD_CS, SD_SCK_MHZ(50))) {
LOG_MSG_CR(F(" failed."));
#ifdef LCD_DISPLAY
lcd.print(F("SD Init Error"));
#endif
return;
}
if (!currDir.open("/")) {
LOG_MSG_CR(F(" failed."));
#ifdef LCD_DISPLAY
lcd.print(F("SD Root Error"));
#endif
return;
}
LOG_MSG_CR(F(" done."));
#ifdef LCD_DISPLAY
lcd.print(F("READY"));
delay(3000);
#endif
mountFilename(0, "AUTORUN.ATR");
}
void loop() {
// let the SIO channel do its thing
sioChannel.runCycle();
#ifdef SELECTOR_BUTTON
// watch the selector button (accounting for debounce)
if (digitalRead(PIN_SELECTOR) == LOW && millis() - lastSelectionPress > 250 && isSwitchPressed==false) {
lastSelectionPress = millis();
changeDisk(0);
} else isSwitchPressed=(digitalRead(PIN_SELECTOR) == LOW);
#endif
#ifdef RESET_BUTTON
// watch the reset button
if (digitalRead(PIN_RESET) == LOW && millis() - lastResetPress > 250) {
lastResetPress = millis();
mountFilename(0, "AUTORUN.ATR");
}
#endif
#ifdef ARDUINO_TEENSY
if (SIO_UART.available())
SIO_CALLBACK();
#endif
}
void SIO_CALLBACK() {
// inform the SIO channel that an incoming byte is available
sioChannel.processIncomingByte();
}
DriveStatus* getDeviceStatus(int deviceId) {
return drive1.getStatus();
}
SectorDataInfo* readSector(int deviceId, unsigned long sector, byte *data) {
if (drive1.hasImage()) {
return drive1.getSectorData(sector, data);
} else {
return NULL;
}
}
boolean writeSector(int deviceId, unsigned long sector, byte* data, unsigned long length) {
return (drive1.writeSectorData(sector, data, length) == length);
}
boolean format(int deviceId, int density) {
char name[13];
// get current filename
file.getName(name, 13);
// close and delete the current file
file.close();
file.remove();
LOG_MSG(F("Remove old file: "));
LOG_MSG_CR(name);
// open new file for writing
file.open(&currDir, name, O_RDWR | O_SYNC | O_CREAT);
LOG_MSG(F("Created new file: "));
LOG_MSG_CR(name);
// allow the virtual drive to format the image (and possibly alter its size)
if (drive1.formatImage(&file, density)) {
// set the new image file for the drive
drive1.setImageFile(&file);
return true;
} else {
return false;
}
}
void changeDisk(int deviceId) {
DirFat_t dir;
char name[13];
boolean imageChanged = false;
while (!imageChanged) {
// get next dir entry
int8_t result = currDir.readDir((DirFat_t*)&dir);
// if we got back a 0, rewind the directory and get the first dir entry
if (!result) {
currDir.rewind();
result = currDir.readDir((DirFat_t*)&dir);
}
// if we have a valid file response code, open it
if (result > 0 && isValidFilename((char*)&dir.name)) {
createFilename(name, (char*)dir.name);
imageChanged = mountFilename(deviceId, name);
}
}
}
boolean isValidFilename(char *s) {
return ( s[0] != '.' && // ignore hidden files
s[0] != '_' && ( // ignore bogus files created by OS X
(s[8] == 'A' && s[9] == 'T' && s[10] == 'R')
|| (s[8] == 'X' && s[9] == 'F' && s[10] == 'D')
#ifdef PRO_IMAGES
|| (s[8] == 'P' && s[9] == 'R' && s[10] == 'O')
#endif
#ifdef ATX_IMAGES
|| (s[8] == 'A' && s[9] == 'T' && s[10] == 'X')
#endif
#ifdef XEX_IMAGES
|| (s[8] == 'X' && s[9] == 'E' && s[10] == 'X')
#endif
)
);
}
void createFilename(char* filename, char* name) {
for (int i=0; i < 8; i++) {
if (name[i] != ' ') {
*(filename++) = name[i];
}
}
if (name[8] != ' ') {
*(filename++) = '.';
*(filename++) = name[8];
*(filename++) = name[9];
*(filename++) = name[10];
}
*(filename++) = '\0';
}
/**
* Returns a list of files in the current directory.
*
* startIndex = the first valid file in the directory to start from
* count = how many files to return
* entries = a pointer to the a FileEntry array to hold the returned data
*/
int getFileList(int startIndex, int count, FileEntry *entries) {
DirFat_t dir;
int currentEntry = 0;
currDir.rewind();
int ix = 0;
while (ix < count) {
if (currDir.readDir((DirFat_t*)&dir) < 1) {
break;
}
if (isValidFilename((char*)&dir.name) || (isSubdir(&dir) && dir.name[0] != '.')) {
if (currentEntry >= startIndex) {
memcpy(entries[ix].name, dir.name, 11);
if (isSubdir(&dir)) {
entries[ix].isDirectory = true;
} else {
entries[ix].isDirectory = false;
}
ix++;
}
currentEntry++;
}
}
return ix;
}
/**
* Changes the SD card directory.
*
* ix = index number (or -1 to go to parent directory)
*/
void changeDirectory(int ix) {
FileEntry entries[1];
char name[13];
SdFile subDir;
if (ix > -1) {
getFileList(ix, 1, entries);
createFilename(name, entries[0].name);
if (subDir.open(&currDir, name, O_READ)) {
currDir = subDir;
}
} else {
if (subDir.open("/")) {
currDir = subDir;
}
}
}
/**
* Mount a file with the given index number.
*
* deviceId = the drive ID
* ix = the index of the file to mount
*/
void mountFileIndex(int deviceId, int ix) {
FileEntry entries[1];
char name[13];
// figure out what filename is associated with the index
getFileList(ix, 1, entries);
// build a full 8.3 filename
createFilename(name, entries[0].name);
// mount the image
mountFilename(deviceId, name);
}
/**
* Mount a file with the given name.
*
* deviceId = the drive ID
* name = the name of the file to mount
*/
boolean mountFilename(int deviceId, char *name) {
// close previously open file
if (file.isOpen()) {
file.close();
}
if (file.open(&currDir, name, O_RDWR | O_SYNC) && drive1.setImageFile(&file)) {
LOG_MSG_CR(name);
#ifdef LCD_DISPLAY
lcd.clear();
lcd.print(name);
lcd.setCursor(0,1);
#endif
return true;
}
return false;
}