-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.cpp
604 lines (490 loc) · 11.5 KB
/
io.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// awBASIC interpreter (c) 2002 Anthony J. Wood
// www.awsoftware.org
//
// This file is part of awbasic.
// awbasic 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.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "basic.h"
/*
* io.cpp
*
* Console I/O routines.
*
* These functions take a pointer to a struct IoConsole, which must be created via a call
* to a device dependent console creation call.
*
* Two types of consoles are supported: TTY (serial, stdin/out) and Windowed.
* The basic TTY functions (e.g. putc) will work on TTY or Windowed consoles.
* The Windowed functions (eg. setreset, set_io->cursor) only work on windowed consoles.
*
* io->io_width and io->io_height are set to zero for TTY devices (stream devices)
*
* struct IoConsole contains pointers to device dependent driver functions, as well
* as variables used by this code.
*
* For Example:
* struct IoConsole* io = IoWinConOpen();
* io_putstr(io, "hello world\n");
*
*/
//*************************************************
void _erase_line(struct IoConsole* io)
{
for (int i=io->cursor; i < (io->cursor/io->io_width)*io->io_width+io->io_width; i++)
io_set_c(io, i, ' ');
}
void io_putc(struct IoConsole* io, char cp)
{
unsigned char c = *((unsigned char*)&cp);
if (io->io_putc)
{
/*
* If a stream device (should always be if here), then maintain the cursor
*/
if (io->io_width==0 || io->io_height==0) // Stream
{
if (c==13 || c==10) // return
{
io->cursor = 0;
}
else if (c==8) // backspace
{
if (io->cursor > 0)
io->cursor--;
}
else if (c >=192) // 192 to 255 are shortcuts for 0 to 63 spaces, respectively
{
for (int i=0; i < (c-192); i++)
io_putc(io, ' ');
return;
}
else if (c==24) /* backspace cursor*/
{
if (io->cursor > 0)
io->cursor--;
}
else if (c==25) /* advance cursor*/
{
io->cursor++;
}
else
{
io->cursor++;
}
}
(*io->io_putc)(io, c);
return;
}
if (c==13 || c==10) // return
{
if (io->cursor_visable)
io_set_c(io, io->cursor,' ');
io->cursor = io->cursor/io->io_width;
io->cursor = io->cursor*io->io_width;
io->cursor = io->cursor + io->io_width;
/* if return put us on a new line that isn't a scroll, erase the line */
if (io->cursor < io->io_width*io->io_height)
_erase_line(io);
}
else if (c==8) // backspace
{
if (io->cursor > 0)
{
if (io->cursor_visable)
io_set_c(io, io->cursor,' ');
io->cursor--;
io_set_c(io, io->cursor,' ');
}
}
else if (c==0xE) // Turn on cursor
{
io->cursor_visable = TRUE;
}
else if (c==0xF) // Turn off cursor
{
io_set_c(io, io->cursor,' ');
io->cursor_visable = FALSE;
}
else if (c >=192) // 192 to 255 are shortcuts for 0 to 63 spaces, respectively
{
for (int i=0; i < (c-192); i++)
io_putc(io, ' ');
}
else if (c==24) /* backspace cursor*/
{
if (io->cursor > 0)
io->cursor--;
}
else if (c==25) /* advance cursor*/
{
if (io->cursor < (io->io_width*io->io_height-1))
io->cursor++;
}
else if (c==26) /* cursor down one line */
{
if (io->cursor < io->io_width*(io->io_height-1))
io->cursor+=io->io_width;
}
else if (c==27) /* cursor up one line */
{
if (io->cursor >= io->io_width)
io->cursor-=io->io_width;
}
else if (c==28) /* home */
{
io->cursor=0;
}
else if (c==29) /* beginning of line */
{
io->cursor=(io->cursor/io->io_width)*io->io_width;
}
else if (c==30) /* erase to end of line */
{
_erase_line(io);
}
else if (c==31) /* erase to end of frame */
{
for (int i=io->cursor; i < io->io_width*io->io_height; i++)
io_set_c(io, i, ' ');
}
else
{
io_set_c(io, io->cursor, c);
io->cursor++;
}
// Do we need to scroll?
if (io->cursor == io->io_width*io->io_height)
{
io->cursor = io->io_width*(io->io_height-1);
for (int i=0; i < (io->io_width*(io->io_height-1)); i++)
io->video_mem[i] = io->video_mem[i+io->io_width];
for (i=0; i < io->io_width; i++)
io->video_mem[io->io_width*io->io_height-i-1] = ' ';
io_draw_range(io, 0, io->io_width*io->io_height-1);
}
if (io->cursor_visable)
io_set_c(io, io->cursor, '_');
if (io->cursor >=io->io_width*io->io_height)
{
io->cursor=0; // SHOULD NEVER HAPPEN
io_putstr(io, "INTERR");
}
}
//*************************************************
char io_inkey(struct IoConsole* io)
{
return (*io->io_inkey)(io);
}
//*************************************************
char io_getc(struct IoConsole* io)
{
return (*io->io_getc)(io);
}
//*************************************************
int io_chk_brk(struct IoConsole* io)
{
return (*io->io_chk_brk)(io);
}
//*************************************************
void io_close(struct IoConsole* io)
{
if (io->io_close)
io->io_close(io);
}
//*************************************************
void io_cls(struct IoConsole* io)
{
if (io->io_width==0 || io->io_height==0)
return;
for (int i=0; i < io->io_width*io->io_height; i++)
io->video_mem[i]=' ';
io->cursor = 0;
io->cursor_visable = FALSE;
io_draw_range(io, 0, io->io_width*io->io_height-1);
}
//*************************************************
void io_set_c(struct IoConsole* io, unsigned short loc, unsigned char c)
{
if (io->io_width==0 || io->io_height==0)
return;
io->video_mem[loc]=c;
io->io_draw_range(io, io->video_mem, loc, loc);
}
//*************************************************
unsigned char io_peek(struct IoConsole* io, short loc)
{
if (io->io_width==0 || io->io_height==0)
return 0;
/** Video Ram? **/
if (loc>=0x3c00 && loc<=0x3fff)
return io->video_mem[loc-0x3c00];
else if (loc>=0x3801 && loc<=0x3880)
return io->io_peek_keyboard(io, loc);
else
return 0;
}
//*************************************************
bool io_setreset(struct IoConsole* io, short x, short y, bool isset)
{
unsigned short loc = x/2+io->io_width*(y/3);
unsigned char cellx = x%2;
unsigned char celly = y%3;
if (loc>=io->io_width*io->io_height)
return FALSE;
if (!(io->video_mem[loc]&128))
io->video_mem[loc]= 128; /* set to a cleared graphics value */
if (isset)
io->video_mem[loc]|= (1<<(celly*2+cellx));
else
io->video_mem[loc]&= ~(1<<(celly*2+cellx));
io_draw_range(io, loc, loc);
return TRUE;
}
//*************************************************
short io_point(struct IoConsole* io, short x, short y)
{
unsigned short loc = x/2+io->io_width*(y/3);
unsigned char cellx = x%2;
unsigned char celly = y%3;
if (loc>=io->io_width*io->io_height)
return 0; /* -1 = TRUE, 0 = FALSE */
if (!(io->video_mem[loc]&128))
return 0; /* not a graphic cell, so false */
if (io->video_mem[loc]&(1<<(celly*2+cellx)))
return -1; /* -1 - TRS-80 TRUE */
else
return 0;
}
//*************************************************
void io_set_cursor(struct IoConsole* io, short i)
{
if (io->io_width==0 || io->io_height==0)
return;
if (i <0) i =0;
else if (i >= io->io_width*io->io_height)
i=io->io_width*io->io_height-1;
io->cursor = i;
}
//*************************************************
short io_get_cursor(struct IoConsole* io)
{
return io->cursor;
}
//*************************************************
void io_putstr(struct IoConsole* io, char* str)
{
for (int i=0; str[i]; i++) // Otherwise, implement using putc
io_putc(io, str[i]);
}
//*************************************************
void io_draw_range(struct IoConsole* io, int start, int end)
{
if (io->io_width==0 || io->io_height==0)
return;
io->io_draw_range(io, io->video_mem, start, end);
}
//*************************************************
char *io_gets(struct IoConsole* io, char *buffer )
{
short i=0;
int c;
io_putc(io, 0xE); /* io->cursor on */
while (TRUE)
{
c=io_getc(io);
if (c== 0x1b) // ESC key
return NULL;
if (!(c==0x08 && i == 0))
io_putc(io, c);
if (c=='\n' || c=='\r')
{
buffer[i]=0;
io_putc(io, 0xF); /* io->cursor off */
return buffer;
}
else if (c==0x08 && i > 0)
{
i--;
}
else if (isgraph(c) || c==' ')
{
buffer[i++] = c;
}
}
}
//*************************************************
int io_printf1i(struct IoConsole *io, const char *format, int arg1)
{
char buffer[256];
int retval;
retval = _snprintf(buffer, sizeof(buffer), format, arg1);
buffer[sizeof(buffer)-1]=0;
io_putstr(io, buffer);
return retval;
}
//*************************************************
int io_printf2i(struct IoConsole *io, const char *format, int arg1, int arg2)
{
char buffer[256];
int retval;
retval = _snprintf(buffer, sizeof(buffer), format, arg1, arg2);
buffer[sizeof(buffer)-1]=0;
io_putstr(io, buffer);
return retval;
}
//*************************************************
/*
* PRINT USING format
*
* supports all format specifiers except ^ (E&D)
*
*/
void io_printusingnum(struct IoConsole *io, char* string, double value)
{
int i=0,adp,ads,numcommas;
bool dollar=FALSE, astrix=FALSE, postplus=FALSE,postneg=FALSE, preplus=FALSE, comma=FALSE;
int predec=0, postdec=0, pad;
char * ad;
/*
* scan up until the first #
*/
while (TRUE)
{
if (string[i]=='*' && string[i+1]=='*')
{
astrix = TRUE;
predec+=2;
i=i+2;
if (string[i]=='$')
{
i++;
dollar=TRUE;
}
break;
}
else if (string[i]=='$' && string[i+1]=='$')
{
dollar = TRUE;
i=i+2;
break;
}
else if (string[i]=='+' && string[i+1]=='#')
{
preplus=TRUE;
i++;
break;
}
else if (string[i]=='.' && string[i+1]=='#')
break;
else if (string[i]=='#')
break;
else
io_putc(io, string[i++]);
}
/*
* scan the # field
*/
while (string[i]==',' || string[i]=='#')
{
predec++;
if (string[i]==',')
comma = TRUE;
i++;
}
if (string[i]=='.')
{
i++;
while (string[i]=='#')
{
postdec++;
i++;
}
}
/*
* any post + o - ?
*/
if (string[i]=='-')
postneg=TRUE;
else if (string[i]=='+')
postplus=TRUE;
/*
* convert the number to ASCII
*/
ad=_fcvt(value, postdec, &adp, &ads);
/*
* take all the preceding requirments and generate the output formated string
*/
if (adp < 0) adp = 0;
if (comma)
numcommas=adp/3;
else
numcommas=0;
pad = predec - adp - numcommas;
if (preplus || ads!=0)
pad--; /* the sign will take one space */
if (pad < 0)
io_putc(io, '%'); /* error */
while (pad-- > 0)
{
if (astrix)
io_putc(io, '*');
else
io_putc(io, ' ');
}
if (predec > 0)
{
if (dollar)
io_putc(io, '$');
if (ads!=0)
io_putc(io, '-');
if (ads==0 && preplus)
io_putc(io, '+');
if (numcommas == 0)
for (i=0; i < adp; i++)
io_putc(io, ad[i]);
else
{
int commacount=adp%3;
for (i=0; i < adp; i++)
{
io_putc(io, ad[i]);
commacount--;
if (commacount%3==0 && i!=(adp-1))
io_putc(io, ',');
}
}
}
if (postdec > 0)
{
io_putc(io, '.');
io_putstr(io, ad+adp);
}
if (postneg && ads!=0)
io_putc(io, '-');
if (postplus)
{
if (ads==0) io_putc(io, '+');
else io_putc(io, '-');
}
}
//*************************************************
#if 0 // CANT GET THIS TO WORK. arglist is a struct, not a representation of a variable arg list
int __cdecl trs80_printf(const char *format, ... )
{
va_list arglist;
char buffer[256];
int retval;
va_start(arglist, format);
retval = _snprintf(buffer, sizeof(buffer), format, (char*)arglist);
// retval = sprintf(buffer, format, arglist);
buffer[sizeof(buffer)-1]=0;
for (int i=0; buffer[i]; i++)
trs80_putchar(buffer[i]);
return retval=0;
}
#endif