[ucsim-z80] Fix #3828: uCsim SM83 flags simulation
[sdcc.git] / sdcc / support / regression / fwk / lib / testfwk.c
blob3aace1a8c49e417105e3035456063ff55f6cea9a
1 /** Test framework support functions.
2 */
3 #include <testfwk.h>
4 #ifndef NO_VARARGS
5 #include <stdarg.h>
6 #endif
8 #ifdef __SDCC_ds390
9 #include <tinibios.h> /* main() must see the ISR declarations */
10 #endif
12 #ifdef __SDCC_mcs51
13 /* until changed, isr's must have a prototype in the module containing main */
14 void T2_isr (void) __interrupt (5);
15 #define MEMSPACE_BUF __idata
16 #else
17 #define MEMSPACE_BUF
18 #endif
20 /** Define this if the port's div or mod functions are broken.
21 A slow loop based method will be substituded.
23 //#define BROKEN_DIV_MOD 1
25 extern void _putchar(char c);
26 extern void _initEmu(void);
27 extern void _exitEmu(void);
29 int __numTests = 0;
30 static int __numFailures = 0;
32 #if BROKEN_DIV_MOD && !defined(TARGET_VERY_LOW_MEMORY)
33 static int
34 __div(int num, int denom)
36 int q = 0;
37 while (num >= denom)
39 q++;
40 num -= denom;
42 return q;
45 static int
46 __mod (int num, int denom)
48 while (num >= denom)
50 num -= denom;
52 return num;
54 #else
55 #define __div(num, denom) ((num) / (denom))
56 #define __mod(num, denom) ((num) % (denom))
57 #endif
59 void
60 __prints (const char *s)
62 char c;
64 while ('\0' != (c = *s))
66 _putchar(c);
67 ++s;
71 #ifdef TARGET_VERY_LOW_MEMORY
72 static void
73 __printNibble (unsigned char c)
75 c &= 0x0F;
76 if (c <= 9 )
77 c += '0';
78 else
79 c += 'A' - 10;
80 _putchar(c);
82 void
83 __printd (int n)
85 unsigned char chr;
86 #define SWAP_BYTE(x) ((x) >> 4 | (x) << 4)
88 if (0 > n)
90 n = -n;
91 _putchar('-');
93 _putchar('x');
95 // This seems to be the most efficient way to do it for PDK (both in RAM & ROM)
96 chr = n >> 8;
97 chr = SWAP_BYTE(chr);
98 __printNibble(chr);
99 chr = SWAP_BYTE(chr);
100 __printNibble(chr);
102 chr = n & 0xFF;
103 chr = SWAP_BYTE(chr);
104 __printNibble(chr);
105 chr = SWAP_BYTE(chr);
106 __printNibble(chr);
108 #else
109 void
110 __printd (int n)
112 if (0 == n)
114 _putchar('0');
116 else
118 static char MEMSPACE_BUF buf[6];
119 char MEMSPACE_BUF *p = &buf[sizeof (buf) - 1];
120 char neg = 0;
122 buf[sizeof(buf) - 1] = '\0';
124 if (0 > n)
126 n = -n;
127 neg = 1;
130 while (0 != n)
132 *--p = '0' + __mod (n, 10);
133 n = __div (n, 10);
136 if (neg)
137 _putchar('-');
139 __prints(p);
143 void
144 __printu (unsigned int n)
146 if (0 == n)
148 _putchar('0');
150 else
152 static char MEMSPACE_BUF buf[6];
153 char MEMSPACE_BUF *p = &buf[sizeof (buf) - 1];
155 buf[sizeof(buf) - 1] = '\0';
157 while (0 != n)
159 *--p = '0' + __mod (n, 10);
160 n = __div (n, 10);
163 __prints(p);
166 #endif
168 #ifndef NO_VARARGS
169 void
170 __printf (const char *szFormat, ...)
172 va_list ap;
173 va_start (ap, szFormat);
175 while (*szFormat)
177 if (*szFormat == '%')
179 switch (*++szFormat)
181 case 's':
183 char *sz = va_arg (ap, char *);
184 __prints(sz);
185 break;
188 case 'd':
190 int i = va_arg (ap, int);
191 __printd (i);
192 break;
195 case 'u':
197 unsigned int i = va_arg (ap, unsigned int);
198 __printu (i);
199 break;
202 case '%':
203 _putchar ('%');
204 break;
206 default:
207 break;
210 else
212 _putchar (*szFormat);
214 szFormat++;
216 va_end (ap);
219 void
220 __fail (__code const char *szMsg, __code const char *szCond, __code const char *szFile, int line)
222 __printf("--- FAIL: \"%s\" on %s at %s:%u\n", szMsg, szCond, szFile, line);
223 __numFailures++;
227 main (void)
229 _initEmu();
231 __printf("--- Running: %s\n", __getSuiteName());
233 __runSuite();
235 __printf("--- Summary: %u/%u/%u: %u failed of %u tests in %u cases.\n",
236 __numFailures, __numTests, __numCases,
237 __numFailures, __numTests, __numCases
240 _exitEmu();
242 return 0;
244 #else
245 void
246 __fail (__code const char *szMsg, __code const char *szCond, __code const char *szFile, int line)
248 __prints("--- FAIL: \"");
249 __prints(szMsg);
250 __prints("\" on ");
251 __prints(szCond);
252 __prints(" at ");
253 __prints(szFile);
254 _putchar(':');
255 __printd(line);
256 _putchar('\n');
258 __numFailures++;
262 main (void)
264 _initEmu();
266 __prints("--- Running: ");
267 __prints(__getSuiteName());
268 _putchar('\n');
270 __runSuite();
272 __prints("--- Summary: ");
273 __printd(__numFailures);
274 _putchar('/');
275 __printd(__numTests);
276 _putchar('/');
277 __printd(__numCases);
279 #ifndef TARGET_VERY_LOW_MEMORY
280 __prints(": ");
281 __printd(__numFailures);
282 __prints(" failed of ");
283 __printd(__numTests);
284 __prints(" tests in ");
285 __printd(__numCases);
286 __prints(" cases.\n");
287 #else
288 _putchar('\n');
289 #endif
291 _exitEmu();
293 return 0;
295 #endif