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