]> git.sur5r.net Git - cc65/blob - testcode/lib/em-test.c
Switched to Markdown in order to allow to add links.
[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 #if defined(__C64__)
10 #define DRIVERNAME      "c64-ram.emd"
11 #elif defined(__C128__)
12 #define DRIVERNAME      "c128-ram.emd"
13 #elif defined(__C16__)
14 #define DRIVERNAME      "c16-ram.emd"
15 #elif defined(__CBM510__)
16 #define DRIVERNAME      "cbm510-ram.emd"
17 #elif defined(__CBM610__)
18 #define DRIVERNAME      "cbm610-ram.emd"
19 #elif defined(__APPLE2ENH__)
20 #define DRIVERNAME      "a2e.auxmem.emd"
21 #elif defined(__APPLE2__)
22 #define DRIVERNAME      "a2.auxmem.emd"
23 #elif defined(__ATARIXL__)
24 #define DRIVERNAME      "atrx130.emd"
25 #elif defined(__ATARI__)
26 #define DRIVERNAME      "atr130.emd"
27 #else
28 #define DRIVERNAME      "unknown"
29 #error "Unknown target system"
30 #endif
31
32
33 #define FORCE_ERROR1 0
34 #define FORCE_ERROR2 0
35
36
37 #define PAGE_SIZE       128                     /* Size in words */
38 #define BUF_SIZE        (PAGE_SIZE + PAGE_SIZE/2)
39 static unsigned buf[BUF_SIZE];
40
41
42
43 static void cleanup (void)
44 /* Remove the driver on exit */
45 {
46     em_unload ();
47 }
48
49
50
51 static void fill (register unsigned* page, register unsigned char count, register unsigned num)
52 {
53     register unsigned char i;
54     for (i = 0; i < count; ++i, ++page) {
55         *page = num;
56     }
57 }
58
59
60
61 static void cmp (unsigned page, register const unsigned* buf,
62                  register unsigned char count, register unsigned num)
63 {
64     register unsigned char i;
65     for (i = 0; i < count; ++i, ++buf) {
66         if (*buf != num) {
67             cprintf ("\r\nData mismatch in page $%04X at $%04X\r\n"
68                      "Data is $%04X (should be $%04X)\r\n",
69                      page, buf, *buf, num);
70 #ifdef __ATARI__
71             cgetc ();
72 #endif
73             exit (EXIT_FAILURE);
74         }
75     }
76 }
77
78
79
80 int main (void)
81 {
82     unsigned char Res;
83     unsigned I;
84     unsigned Offs;
85     unsigned PageCount;
86     unsigned char X, Y;
87     struct em_copy c;
88
89     clrscr ();
90     Res = em_load_driver (DRIVERNAME);
91     if (Res != EM_ERR_OK) {
92         cprintf ("Error in em_load_driver: %u\r\n", Res);
93         cprintf ("os: %u, %s\r\n", _oserror, _stroserror (_oserror));
94 #ifdef __ATARI__
95         cgetc ();
96 #endif
97         exit (EXIT_FAILURE);
98     }
99     atexit (cleanup);
100
101     /* Get the number of available pages */
102     PageCount = em_pagecount ();
103     cprintf ("Loaded ok, page count = $%04X\r\n", PageCount);
104
105     /* TEST #1: em_map/em_use/em_commit */
106     cputs ("Testing em_map/em_use/em_commit");
107
108     /* Fill all pages */
109     cputs ("\r\n  Filling   ");
110     X = wherex ();
111     Y = wherey ();
112     for (I = 0; I < PageCount; ++I) {
113
114         /* Fill the buffer and copy it to em */
115         fill (em_use (I), PAGE_SIZE, I);
116         em_commit ();
117
118         /* Keep the user happy */
119         gotoxy (X, Y);
120         cputhex16 (I);
121     }
122
123 #if FORCE_ERROR1
124     ((unsigned*) em_map (0x03))[0x73] = 0xFFFF;
125     em_commit ();
126 #endif
127
128     /* Check all pages */
129     cputs ("\r\n  Comparing ");
130     X = wherex ();
131     Y = wherey ();
132     for (I = 0; I < PageCount; ++I) {
133
134         /* Get the buffer and compare it */
135         cmp (I, em_map (I), PAGE_SIZE, I);
136
137         /* Keep the user happy */
138         gotoxy (X, Y);
139         cputhex16 (I);
140     }
141
142     /* TEST #2: em_copyfrom/em_copyto. */
143     cputs ("\r\nTesting em_copyfrom/em_copyto");
144
145     /* We're filling now 384 bytes per run to test the copy routines with
146      * other sizes.
147      */
148     PageCount = (PageCount * 2) / 3;
149
150     /* Setup the copy structure */
151     c.buf   = buf;
152     c.count = sizeof (buf);
153
154     /* Fill again all pages */
155     cputs ("\r\n  Filling   ");
156     X = wherex ();
157     Y = wherey ();
158     c.page = 0;
159     c.offs = 0;
160     for (I = 0; I < PageCount; ++I) {
161
162         /* Fill the buffer and copy it to em */
163         fill (buf, BUF_SIZE, I ^ 0xFFFF);
164         em_copyto (&c);
165
166         /* Adjust the em offset */
167         Offs = c.offs + sizeof (buf);
168         c.offs = (unsigned char) Offs;
169         c.page += (Offs >> 8);
170
171         /* Keep the user happy */
172         gotoxy (X, Y);
173         cputhex16 (I);
174     }
175
176 #if FORCE_ERROR2
177     c.page = 0x03;
178     em_copyfrom (&c);
179     buf[0x73] = 0xFFFF;
180     em_copyto (&c);
181 #endif
182
183     /* Check all pages */
184     cputs ("\r\n  Comparing ");
185     X = wherex ();
186     Y = wherey ();
187     c.page = 0;
188     c.offs = 0;
189     for (I = 0; I < PageCount; ++I) {
190
191         /* Get the buffer and compare it */
192         em_copyfrom (&c);
193         cmp (I, buf, BUF_SIZE, I ^ 0xFFFF);
194
195         /* Adjust the em offset */
196         Offs = c.offs + sizeof (buf);
197         c.offs = (unsigned char) Offs;
198         c.page += (Offs >> 8);
199
200         /* Keep the user happy */
201         gotoxy (X, Y);
202         cputhex16 (I);
203     }
204
205     /* Success */
206     cprintf ("\r\nPassed!\r\n");
207
208 #ifdef __ATARI__
209     cgetc ();
210 #endif
211
212     return 0;
213
214 }