forked from Flogistoni/raspbiec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raspbiec.cpp
419 lines (390 loc) · 9.7 KB
/
raspbiec.cpp
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
/*
* Raspbiec - Commodore 64 & 1541 serial bus handler for Raspberry Pi
* Copyright (C) 2013 Antti Paarlahti <[email protected]>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "raspbiec.h"
#include "raspbiec_utils.h"
#include <sys/stat.h>
#include <unistd.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
enum raspiec_mode {
MODE_NONE,
MODE_SERVE,
MODE_LOAD,
MODE_SAVE,
MODE_COMMAND,
MODE_ERROR_CHANNEL
};
int main(int argc, char** argv)
{
int mode = MODE_NONE;
int argno = 2;
const char *path = NULL;
int devicenum;
if (argc < 2)
{
char *basec = strdup(argv[0]);
char *bname = basename(basec);
printf("As drive: %s [serve] <directory> [<device #>]\n", bname);
printf("As computer: %s load <filename> [<device #>]\n", bname);
printf(" %s save <filename> [<device #>]\n", bname);
printf(" %s cmd <command> [<device #>]\n", bname);
printf(" %s errch [<device #>]\n", bname);
free(basec);
return EXIT_SUCCESS;
}
else if (strcmp("load",argv[1]) == 0)
{
mode = MODE_LOAD;
path = (2 < argc) ? argv[2] : NULL;
devicenum = (3 < argc) ? strtol(argv[3], NULL, 10) : 8;
}
else if (strcmp("save",argv[1]) == 0)
{
mode = MODE_SAVE;
path = (2 < argc) ? argv[2] : NULL;
devicenum = (3 < argc) ? strtol(argv[3], NULL, 10) : 8;
}
else if (strcmp("cmd",argv[1]) == 0)
{
mode = MODE_COMMAND;
path = (2 < argc) ? argv[2] : NULL;
devicenum = (3 < argc) ? strtol(argv[3], NULL, 10) : 8;
}
else if (strcmp("errch",argv[1]) == 0)
{
mode = MODE_ERROR_CHANNEL;
path = "";
devicenum = (2 < argc) ? strtol(argv[2], NULL, 10) : 8;
}
else if (strcmp("serve",argv[1]) == 0)
{
mode = MODE_SERVE;
path = (2 < argc) ? argv[2] : ".";
devicenum = (3 < argc) ? strtol(argv[3], NULL, 10) : 8;
}
else
{
mode = MODE_SERVE;
path = (1 < argc) ? argv[1] : ".";
devicenum = (2 < argc) ? strtol(argv[2], NULL, 10) : 8;
}
if (!path)
{
fprintf(stderr,"Missing filename\n");
return EXIT_FAILURE;
}
try
{
device iecbus;
switch(mode)
{
case MODE_SERVE:
{
drive c1541(iecbus, devicenum);
c1541.serve(path);
break;
}
case MODE_LOAD:
{
computer c64(iecbus);
c64.load(path, devicenum);
break;
}
case MODE_SAVE:
{
computer c64(iecbus);
c64.save(path, devicenum);
break;
}
case MODE_COMMAND:
{
computer c64(iecbus);
c64.command(path, devicenum);
break;
}
case MODE_ERROR_CHANNEL:
{
computer c64(iecbus);
c64.read_error_channel(devicenum);
break;
}
default:
throw raspbiec_error(IEC_UNKNOWN_MODE);
break;
}
}
catch (raspbiec_error &e)
{
printf("%s\n",e.what());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*********************************************************************/
drive::drive(device &dev, int device_number) :
m_dev(dev),
m_device_number(device_number)
{
m_dev.set_identity(device_number);
}
drive::~drive()
{
}
void drive::serve(const char *path)
{
struct stat stb;
if(stat(path, &stb)==0)
{
if (S_ISREG(stb.st_mode))
{
fprintf(stderr,"'%s' is a regular file, expected a directory.\n"
"(Disk images not yet supported)\n", path);
throw raspbiec_error(IEC_GENERAL_ERROR);
}
if (!S_ISDIR(stb.st_mode))
{
fprintf(stderr, "'%s' is not a directory.\n", path);
throw raspbiec_error(IEC_FILE_NOT_FOUND);
}
}
else
{
fprintf(stderr, "Cannot access '%s'\n", path);
throw raspbiec_error(IEC_FILE_NOT_FOUND);
}
if (chdir(path)==-1)
{
fprintf(stderr, "Cannot change to directory '%s'\n", path);
throw raspbiec_error(IEC_FILE_NOT_FOUND);
}
for (int i=0; i<16; ++i)
{
channels[i].petscii_name.clear();
channels[i].ascii_name.clear();
channels[i].open = false;
}
printf("Entering disk drive service loop\n"
"Exit with Ctrl-C or SIGINT\n");
/* Service loop */
raspbiec_sighandler::setup();
device::Command cmd = device::Unknown;
do
{
try {
int sa = -1;
cmd = m_dev.receive_command(m_device_number, sa);
switch(cmd)
{
case device::Open:
{
printf("Open %d\n",sa);
channel &ch = channels[sa];
if (ch.open)
{
printf("Channel %d aready open!\n", sa);
throw raspbiec_error(IEC_ILLEGAL_STATE);
}
receive_filename(ch);
ch.open = true;
break;
}
case device::Close:
{
printf("Close %d\n",sa);
channel &ch = channels[sa];
if (!ch.open)
{
printf("Channel %d aready closed!\n", sa);
throw raspbiec_error(IEC_ILLEGAL_STATE);
}
ch.petscii_name.clear();
ch.ascii_name.clear();
ch.open = false;
break;
}
case device::Save:
{
channel &ch = channels[sa];
printf("Save %d:\"%s\"\n",sa,ch.ascii_name.c_str());
if (!ch.open)
{
printf("Channel %d not open!\n", sa);
throw raspbiec_error(IEC_ILLEGAL_STATE);
}
save(ch);
break;
}
case device::Load:
{
channel &ch = channels[sa];
printf("Load %d:\"%s\"\n",sa,ch.ascii_name.c_str());
if (!ch.open)
{
printf("Channel %d not open!\n", sa);
throw raspbiec_error(IEC_ILLEGAL_STATE);
}
load(ch);
break;
}
case device::Unlisten:
printf("Unlisten\n");
break;
case device::Untalk:
printf("Untalk\n");
break;
case device::Exit:
printf("\nExiting disk drive service loop\n");
break;
case device::OpenOtherDevice:
printf("Open other device\n");
break;
case device::CloseOtherDevice:
printf("Close other device\n");
break;
case device::SaveOtherDevice:
printf("Save other device\n");
break;
case device::LoadOtherDevice:
printf("Load other device\n");
break;
case device::Unknown:
// Ignore, usually results from spurious ATN asserts
// e.g.during power cycling
break;
default:
printf("Unknown command %d\n", cmd);
break;
}
}
catch( raspbiec_error &e )
{
if (e.status() == IEC_ILLEGAL_STATE)
throw;
// Continue serving in other cases
printf("\n%s\n",e.what());
m_dev.clear_error();
for (int i=0; i<16; ++i)
{
channels[i].petscii_name.clear();
channels[i].ascii_name.clear();
channels[i].open = false;
}
}
}
while( cmd != device::Exit );
}
void drive::receive_filename(channel &ch)
{
ch.petscii_name.clear();
m_dev.receive_from_bus(ch.petscii_name, 0);
petscii2ascii( ch.petscii_name, ch.ascii_name );
printf("filename \"%s\"\n",ch.ascii_name.c_str());
}
// TODO: Instead of reading and writing to a memory buffer
// some kind of a file stream object could also be used
// instead if interelaved I/O is desired
void drive::load(channel &ch)
{
std::vector<unsigned char> buf;
if (ch.ascii_name == "$")
{
read_local_dir(buf, ".");
}
else /* ordinary PRG file */
{
read_local_file(buf, ch.ascii_name.c_str());
}
m_dev.send_to_bus_verbose(buf);
}
void drive::save(channel &ch)
{
std::vector<unsigned char> buf;
m_dev.receive_from_bus_verbose(buf);
write_local_file(buf, ch.ascii_name.c_str());
}
/*********************************************************************/
computer::computer(device &dev) :
m_dev(dev)
{
m_dev.set_identity(device::computer);
}
computer::~computer()
{
}
void computer::load(const char* filename, int device_number)
{
bool is_directory( strcmp(filename,"$")==0 );
if (!is_directory && local_file_exists(filename))
{
printf("Not overwriting '%s'\n", filename);
throw raspbiec_error(IEC_FILE_EXISTS);
}
std::vector<unsigned char> ram;
size_t read = 0;
try
{
read = m_dev.load(ram, filename, device_number, 1);
printf("%d bytes\n",read);
}
catch (raspbiec_error &e)
{
printf("%s\n",e.what());
read_error_channel(device_number);
return;
}
if (is_directory)
{
basic_listing(ram);
}
else
{
write_local_file(ram, filename);
}
}
void computer::save(const char *filename, int device_number)
{
std::vector<unsigned char> prg;
size_t prgsize = read_local_file(prg, filename);
try
{
size_t written = m_dev.save(prg, filename, device_number, 0);
printf("%d bytes\n",written);
}
catch (raspbiec_error &e)
{
printf("%s\n",e.what());
read_error_channel(device_number);
}
}
void computer::command(const char *command, int device_number)
{
std::string asccmd(command);
std::vector<unsigned char> cmd;
ascii2petscii( asccmd, cmd );
m_dev.send_data( cmd, device_number, 15 );
read_error_channel(device_number);
}
void computer::read_error_channel(int device_number)
{
std::vector<unsigned char> msg;
m_dev.receive_data(msg, device_number, 15);
std::string ascmsg;
petscii2ascii( msg, ascmsg );
printf("%s\n", ascmsg.c_str());
}