]> git.sur5r.net Git - cc65/blob - samples/ascii.c
Added info about the new OPTIONAL segment attribute.
[cc65] / samples / ascii.c
1 /* ascii.c
2 **
3 ** Shows the ASCII (or ATASCII, PETSCII) codes of typed characters.
4 **
5 ** 2002-12-25, Greg King <gngking@erols.com>
6 */
7
8 /* Define CONIO when you want to use the conio functions.
9 ** Undefine it when you want to use the stdio functions.
10 ** NOTE: Undefining CONIO will currently not work on the CBMs!!!
11 */
12 #define CONIO
13
14 #include <conio.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdbool.h>
18
19 #define QUIT    'Q'
20
21 static unsigned char height, width, r, t;
22 static int c;
23
24 #ifdef CONIO
25 # define PRINT  cprintf
26 # define PUT(c) cputc((char)(c))
27
28 /* conio doesn't echo typed characters.
29 ** So, this function does it.
30 */
31 static int GET(void) {
32         PUT(c = (int)cgetc());
33         return c;
34         }
35 #else
36 # define PRINT  printf
37 # define GET    getchar
38 #endif
39
40 int main(void) {
41
42 #       ifdef CONIO
43         /* conio doesn't scroll!  Avoid trouble by starting at the top
44         ** of the screen, and never going "below" the bottom of the screen.
45         */
46         clrscr();
47         r = 7;          /* allow for prompt */
48 #       endif
49
50         /* This prompt fits on the VIC-20's narrow screen.
51         */
52         PRINT("Type characters to see\r\ntheir hexadecimal code\r\nnumbers:\r\n\n");
53         screensize(&width, &height);    /* get the screen's dimensions */
54         width /= 6;                     /* get number of codes on a line */
55         cursor(true);
56         t = 0;
57         while ((c = GET()) != EOF) {
58
59 #               ifdef CONIO
60                 if (r == height) {
61                         clrscr();
62                         PUT(c); /* echo char. again because screen was erased */
63                         r = 1;
64                         }
65 #               endif
66
67                 PRINT("=$%02x ", c);
68                 if (c == QUIT)
69                     break;
70                 if (++t == width) {
71                         PRINT("\r\n");
72                         ++r;
73                         t = 0;
74                         }
75                 }
76         PRINT("\r\n");
77         return EXIT_SUCCESS;
78         }