]> git.sur5r.net Git - cc65/blob - testcode/lib/em-test.c
Move zpsave into its own segment to decrease the executable size.
[cc65] / testcode / lib / em-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <conio.h>
6 #include <em.h>
7
8
9 #define FORCE_ERROR1 0
10 #define FORCE_ERROR2 0
11
12
13 #define PAGE_SIZE       128                     /* Size in words */
14 #define BUF_SIZE        (PAGE_SIZE + PAGE_SIZE/2)
15 static unsigned buf[BUF_SIZE];
16
17
18
19 static void cleanup (void)
20 /* Remove the driver on exit */
21 {
22     em_unload ();
23 }
24
25
26
27 static void fill (register unsigned* page, register unsigned char count, register unsigned num)
28 {
29     register unsigned char i;
30     for (i = 0; i < count; ++i, ++page) {
31         *page = num;
32     }
33 }
34
35
36
37 static void cmp (unsigned page, register const unsigned* buf,
38                  register unsigned char count, register unsigned num)
39 {
40     register unsigned char i;
41     for (i = 0; i < count; ++i, ++buf) {
42         if (*buf != num) {
43             cprintf ("\r\nData mismatch in page $%04X at $%04X\r\n"
44                      "Data is $%04X (should be $%04X)\r\n",
45                      page, buf, *buf, num);
46             exit (EXIT_FAILURE);
47         }
48     }
49 }
50
51
52
53 int main (void)
54 {
55     unsigned char Res;
56     unsigned I;
57     unsigned Offs;
58     unsigned PageCount;
59     unsigned char X, Y;
60     struct em_copy c;
61
62     clrscr ();
63     Res = em_load_driver ("c64-ram.emd");
64     if (Res != EM_ERR_OK) {
65         cprintf ("Error in em_load_driver: %u\r\n", Res);
66         cprintf ("os: %u, %s\r\n", _oserror, _stroserror (_oserror));
67         exit (EXIT_FAILURE);
68     }
69     atexit (cleanup);
70
71     /* Get the number of available pages */
72     PageCount = em_pagecount ();
73     cprintf ("Loaded ok, page count = $%04X\r\n", PageCount);
74
75     /* TEST #1: em_map/em_use/em_commit */
76     cputs ("Testing em_map/em_use/em_commit");
77
78     /* Fill all pages */
79     cputs ("\r\n  Filling   ");
80     X = wherex ();
81     Y = wherey ();
82     for (I = 0; I < PageCount; ++I) {
83
84         /* Fill the buffer and copy it to em */
85         fill (em_use (I), PAGE_SIZE, I);
86         em_commit ();
87
88         /* Keep the user happy */
89         gotoxy (X, Y);
90         cputhex16 (I);
91     }
92
93 #if FORCE_ERROR1
94     ((unsigned*) em_map (0x03))[0x73] = 0xFFFF;
95     em_commit ();
96 #endif
97
98     /* Check all pages */
99     cputs ("\r\n  Comparing ");
100     X = wherex ();
101     Y = wherey ();
102     for (I = 0; I < PageCount; ++I) {
103
104         /* Get the buffer and compare it */
105         cmp (I, em_map (I), PAGE_SIZE, I);
106
107         /* Keep the user happy */
108         gotoxy (X, Y);
109         cputhex16 (I);
110     }
111
112     /* TEST #2: em_copyfrom/em_copyto. */
113     cputs ("\r\nTesting em_copyfrom/em_copyto");
114
115     /* We're filling now 384 bytes per run to test the copy routines with
116      * other sizes.
117      */
118     PageCount = (PageCount * 2) / 3;
119
120     /* Setup the copy structure */
121     c.buf   = buf;
122     c.count = sizeof (buf);
123
124     /* Fill again all pages */
125     cputs ("\r\n  Filling   ");
126     X = wherex ();
127     Y = wherey ();
128     c.page = 0;
129     c.offs = 0;
130     for (I = 0; I < PageCount; ++I) {
131
132         /* Fill the buffer and copy it to em */
133         fill (buf, BUF_SIZE, I ^ 0xFFFF);
134         em_copyto (&c);
135
136         /* Adjust the em offset */
137         Offs = c.offs + sizeof (buf);
138         c.offs = (unsigned char) Offs;
139         c.page += (Offs >> 8);
140
141         /* Keep the user happy */
142         gotoxy (X, Y);
143         cputhex16 (I);
144     }
145
146 #if FORCE_ERROR2
147     c.page = 0x03;
148     em_copyfrom (&c);
149     buf[0x73] = 0xFFFF;
150     em_copyto (&c);
151 #endif
152
153     /* Check all pages */
154     cputs ("\r\n  Comparing ");
155     X = wherex ();
156     Y = wherey ();
157     c.page = 0;
158     c.offs = 0;
159     for (I = 0; I < PageCount; ++I) {
160
161         /* Get the buffer and compare it */
162         em_copyfrom (&c);
163         cmp (I, buf, BUF_SIZE, I ^ 0xFFFF);
164
165         /* Adjust the em offset */
166         Offs = c.offs + sizeof (buf);
167         c.offs = (unsigned char) Offs;
168         c.page += (Offs >> 8);
169
170         /* Keep the user happy */
171         gotoxy (X, Y);
172         cputhex16 (I);
173     }
174
175     /* Success */
176     cprintf ("\r\nPassed!\r\n");
177
178     return 0;
179
180 }