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