-
Notifications
You must be signed in to change notification settings - Fork 2
/
a2stream.c
432 lines (364 loc) · 9.65 KB
/
a2stream.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
/******************************************************************************
Copyright (c) 2022, Oliver Schmidt
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL OLIVER SCHMIDT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#include <fcntl.h>
#include <ctype.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <device.h>
#include <apple2_filetype.h>
#include <ip65.h>
#include "w5100.h"
#include "w5100_http.h"
#include "player.h"
#include "linenoise.h"
#include "a2stream.h"
#define write_main() (*(uint8_t *)0xC004 = 0)
#define write_aux() (*(uint8_t *)0xC005 = 0)
#define text_off() (*(uint8_t *)0xC050 = 0)
#define text_on() (*(uint8_t *)0xC051 = 0)
#define mix_off() (*(uint8_t *)0xC052 = 0)
#define mix_on() (*(uint8_t *)0xC053 = 0)
#define page_1() (*(uint8_t *)0xC054 = 0)
#define page_2() (*(uint8_t *)0xC055 = 0)
#define hires_off() (*(uint8_t *)0xC056 = 0)
#define hires_on() (*(uint8_t *)0xC057 = 0)
#define dhires() (*(uint8_t *)0xC05E = 0)
#define shires() (*(uint8_t *)0xC05F = 0)
static bool match(const char *filter, const char *string)
{
while (*filter)
{
if (!*string)
{
return false;
}
if (toupper(*filter++) != toupper(*string++))
{
return false;
}
}
return true;
}
static void completion(const char *line, linenoiseCompletions *lc)
{
if (match(line, "http://"))
{
linenoiseAddCompletion(lc, "http://");
}
if (match(line, "http://www."))
{
linenoiseAddCompletion(lc, "http://www.");
}
if (isalnum(line[strlen(line) - 1]))
{
char *buffer = malloc(0x200);
linenoiseAddCompletion(lc, strcat(strcpy(buffer, line), ".a2s"));
linenoiseAddCompletion(lc, strcat(strcpy(buffer, line), ".a2stream"));
free(buffer);
}
}
static void error_exit(void)
{
printf("- %s\n", ip65_strerror(ip65_error));
exit(EXIT_FAILURE);
}
static void confirm_exit(void)
{
printf("\nPress any key");
cgetc();
}
bool load(register uint8_t *ptr, uint16_t len, bool aux)
{
register uint16_t i = len;
register volatile uint8_t *data = w5100_data;
while (w5100_receive_request() < len)
{
if (!w5100_connected() || input_check_for_abort_key())
{
return false;
}
}
if (aux)
{
write_aux();
}
// Only - actual - register (aka zero page) variables here !!!
do
{
*ptr++ = *data;
}
while (--i);
if (aux)
{
write_main();
}
w5100_receive_commit(len);
return true;
}
bool load_hires(bool page2)
{
uint8_t *ptr;
bool ok;
uint8_t x = wherex();
for (ptr = (uint8_t *)0x2000; ptr < (uint8_t *)0x4000; ptr += 0x0800)
{
if (page2)
{
page_2();
}
ok = load(ptr, 0x0800, false);
if (page2)
{
page_1();
}
if (!ok)
{
return false;
}
}
return true;
}
void main(int argc, char *argv[])
{
uint8_t eth_init = ETH_INIT_DEFAULT;
bool do_again = false;
bool tape_out = false;
char *url = NULL;
bool Offload_DNS;
_filetype = PRODOS_T_TXT;
_heapadd((void *)0x0800, 0x1800);
videomode(VIDEOMODE_80COL);
atexit(confirm_exit);
cputsxy(19, 0, "\xDA\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC"
"\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC"
"\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC"
"\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xDF");
cputsxy(19, 1, "\xDA A2Stream 1.4 - Oliver Schmidt - 2022 \xDF");
cputsxy(19, 2, "\xDA\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F"
"\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F"
"\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F"
"\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\xDF");
{
char cwd[FILENAME_MAX];
if (!*getcwd(cwd, sizeof(cwd)))
{
chdir(getdevicedir(getcurrentdevice(), cwd, sizeof(cwd)));
printf("\n\nSetting prefix - %s", cwd);
}
}
{
DIR *dir;
struct dirent *dirent;
dir = opendir("/RAM");
if (!dir)
{
printf("\n\n/RAM not present\n");
exit(EXIT_FAILURE);
}
dirent = readdir(dir);
if (dirent)
{
printf("\n\n/RAM not empty\n");
exit(EXIT_FAILURE);
}
closedir(dir);
}
{
int file;
printf("\n\nSetting slot ");
file = open("ethernet.slot", O_RDONLY);
if (file != -1)
{
read(file, ð_init, 1);
close(file);
eth_init &= 7;
}
}
printf("- %u\n\nInitializing %s ", eth_init, eth_name);
if (ip65_init(eth_init))
{
error_exit();
}
Offload_DNS = w5100_init(eth_init);
if (!Offload_DNS)
{
printf("- Ok\n\nObtaining IP address ");
if (dhcp_init())
{
error_exit();
}
}
printf("- Ok\n\n");
linenoiseHistoryLoad("stream.urls");
linenoiseSetCompletionCallback(completion);
// Repeat playing a stream
while (true)
{
if (do_again)
{
// Reinit IP65 for DNS lookup,
// IP configuration still valid
ip65_init(eth_init);
}
// Repeat parsing a URL
while (true)
{
// Access command line arg only once
if (argc > 1 && !url)
{
url = argv[1];
printf("URL: %s", url);
}
else
{
url = linenoise("URL? ");
if (!url || !*url)
{
putchar('\n');
exit(EXIT_FAILURE);
}
}
linenoiseHistoryAdd(url);
printf("\n\nProcessing URL ");
if (!url_parse(url, !Offload_DNS))
{
break;
}
printf("- %s\n\n", ip65_strerror(ip65_error));
}
printf("- Ok\n\nSaving URL ");
printf("- %s\n\n", linenoiseHistorySave("stream.urls") ? "No" : "Ok");
if (!do_again)
{
int file;
do_again = true;
printf("Setting output ");
file = open("output.tape", O_RDONLY);
if (file != -1)
{
close(file);
tape_out = true;
}
printf("- %s\n\n", tape_out ? "Tape" : "Speaker");
gen_player(eth_init, tape_out);
printf("\n\n");
}
// Copy IP config from IP65 to W5100
w5100_config();
{
bool ok;
char* buffer = malloc(0x800);
if (!buffer)
{
printf("Connecting - Out of memory\n");
exit(EXIT_FAILURE);
}
if (Offload_DNS)
{
ok = w5100_http_open_name(url_host, strlen(url_host) - 4, url_port,
url_selector, buffer, 0x800);
}
else
{
ok = w5100_http_open_addr(url_ip, url_port,
url_selector, buffer, 0x800);
}
free(buffer);
if (!ok)
{
putchar('\n');
continue;
}
}
hires_on();
printf("- Ok\n\nLoading cover art ");
{
uint8_t type[2];
char *error = NULL;
if (!load(type, sizeof(type), false))
{
error = "Failed";
}
else if (type[0] != 0xA2 || type[1] != 0x01)
{
error = "Unknown stream type";
}
else if (!load_hires(true) || !load_hires(false))
{
error = "Failed";
}
if (error)
{
printf("- %s\n\n", error);
w5100_disconnect();
continue;
}
}
printf("- Ok\n\n");
mix_on();
dhires();
text_off();
{
char text_1[4][40];
char text_2[4][40];
uint8_t y;
uint8_t cursor_x = wherex();
uint8_t cursor_y = wherey();
// Save and clear text lines 20 to 24
for (y = 0; y < 4; ++y)
{
gotoy(20 + y);
memcpy(text_1[y], *(void **)0x28, 40);
memset(*(void **)0x28, ' ' | 0x80, 40);
page_2();
memcpy(text_2[y], *(void **)0x28, 40);
memset(*(void **)0x28, ' ' | 0x80, 40);
page_1();
}
cputsxy(31, 21, "\xDA\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC"
"\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xDF");
cputsxy(31, 22, "\xDA Loading... \xDF");
cputsxy(31, 23, "\xDA\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F"
"\x5F\x5F\x5F\x5F\x5F\x5F\x5F\x5F\xDF");
play();
// Restore text lines 20 to 24
for (y = 0; y < 4; ++y)
{
gotoy(20 + y);
memcpy(*(void **)0x28, text_1[y], 40);
page_2();
memcpy(*(void **)0x28, text_2[y], 40);
page_1();
}
gotoxy(cursor_x, cursor_y);
}
text_on();
shires();
}
}