1663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng#include <stdint.h>
2663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng#include <inttypes.h>
3663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng#include <stdlib.h>
4663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng#include <string.h>
5663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng#include <stdio.h>
6663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng#include "opcodes.h"
7663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
8663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng#ifndef M3
9663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng#define M3 0
10663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng#endif
11663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
12663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* The abstracted result of an CU21 insn */
13663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengtypedef struct {
14663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   uint64_t addr1;  // target
15663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   uint64_t len1;
16663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   uint64_t addr2;  // source
17663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   uint64_t len2;
18663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   uint32_t cc;
19663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng} cu21_t;
20663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
21663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* Define various input buffers. */
22663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
23663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* U+0000 to U+007f:  Result is 1 byte for each uint16_t */
24663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chenguint16_t pattern1[] = {
25663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0x0000, 0x007f,    /* corner cases */
26663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0x0047, 0x0056, 0x0045, 0x0021, 0x007b, 0x003a /* misc */
27663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng};
28663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
29663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* U+0080 to U+07ff:  Result is 2 bytes for each uint16_t */
30663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chenguint16_t pattern2[] = {
31663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0x0080, 0x07ff,    /* corner cases */
32663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0x07df, 0x008f, 0x0100, 0x017f, 0x052f, 0x0600, 0x06ff /* misc */
33663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng};
34663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
35663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* U+0800 to U+d7ff:  Result is 3 bytes for each uint16_t
36663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   U+dc00 to U+ffff:  Result is 3 bytes for each uint16_t */
37663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chenguint16_t pattern3[] = {
38663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0x0800, 0xd7ff,    /* corner cases */
39663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0xdc00, 0xffff,    /* corner cases */
40663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0x083f, 0x1a21, 0x1b10, 0x2200, 0x225e, 0x22c9, 0xe001  /* misc */
41663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng};
42663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
43663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* U+d800 to U+dbff:  Result is 4 bytes for each uint16_t pair */
44663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chenguint16_t pattern4[] = {
45663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0xd800, 0xdc00,    /* left  corner case */
46663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0xdbff, 0xdfff,    /* right corner case */
47663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0xdada, 0xdddd, 0xdeaf, 0xdcdc  /* misc */
48663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng};
49663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
50663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* Invalid low surrogate */
51663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chenguint16_t invalid[] = { 0xd801, 0x0098 };
52663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
53663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* Mixed bytes */
54663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chenguint16_t mixed[] = {
55663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0x0078 /* 1 byte */,
56663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0x0200 /* 2 bytes */,
57663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0xffff /* 3 bytes */,
58663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   0xd800, 0xdc01 /* 4 bytes */
59663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng};
60663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
61663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng/* This is the buffer for the converted bytes. */
62663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chenguint8_t buff[1000];  /* Large so we con'don't have to worry about it */
63663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
64663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengvoid write_and_check(uint16_t *, unsigned, unsigned);
65663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
66663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
67663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengstatic cu21_t
68663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengdo_cu21(uint8_t *dst, uint64_t dst_len, uint16_t *src, uint64_t src_len)
69663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng{
70663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   int cc = 42;
71663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   cu21_t regs;
72663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
73663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* build up the register pairs */
74663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   register uint16_t *source     asm("4") = src;
75663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   register uint64_t  source_len asm("5") = src_len;
76663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   register uint8_t  *dest       asm("2") = dst;
77663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   register uint64_t  dest_len   asm("3") = dst_len;
78663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
79663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   asm volatile(
80663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                CU21(M3,2,4)
81663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                "ipm %2\n\t"
82663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                "srl %2,28\n\t"
83663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                : "+d"(dest), "+d"(source), "=d"(cc),
84663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                  "+d"(source_len), "+d"(dest_len)
85663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                :
86663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                : "memory", "cc");
87663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
88663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Capture register contents at end of cu21 */
89663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   regs.addr1 = (uint64_t)dest;
90663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   regs.len1  = dest_len;
91663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   regs.addr2 = (uint64_t)source;
92663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   regs.len2  = source_len;
93663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   regs.cc = cc;
94663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
95663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   return regs;
96663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng}
97663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
98663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengvoid
99663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengrun_test(uint8_t *dst, uint64_t dst_len, uint16_t *src, uint64_t src_len)
100663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng{
101663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   int i;
102663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   cu21_t result;
103663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
104663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   result = do_cu21(dst, dst_len, src, src_len);
105663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
106663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   // Write out the converted bytes, if any
107663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("UTF8: ");
108663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   if (dst_len - result.len1 == 0)
109663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      printf(" <none>");
110663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   else
111663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      for (i = 0; i < dst_len - result.len1; i++) {
112663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng         printf(" %02x", dst[i]);
113663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      }
114663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n");
115663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
116663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("  cc = %d\n", result.cc);
117663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   if (dst != NULL)
118663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      printf("  dst address difference: %"PRId64, result.addr1 - (uint64_t)dst);
119663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("  dst len: %"PRId64"\n", result.len1);
120663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
121663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   if (src != NULL)
122663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      printf("  src address difference: %"PRId64, result.addr2 - (uint64_t)src);
123663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("  src len: %"PRId64"\n", result.len2);
124663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng}
125663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
126663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengint main()
127663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng{
128663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Length == 0, no memory should be read or written */
129663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test1 ----------------\n");
130663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 0, NULL, 0);
131663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
132663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Test exhaustion of source length (source bytes are valid) */
133663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test2.1 ----------------\n");
134663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
135663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* No character will be written to BUFF, i.e. loop in jitted code
136663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      is not iterated */
137663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, NULL,     1);
138663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern1, 1);
139663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern2, 1);
140663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern3, 1);
141663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern4, 1);
142663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern4, 2);
143663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern4, 3);
144663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
145663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test2.2 ----------------\n");
146663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* At least one character will be written to BUFF, i.e. loop in jitted
147663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      code is iterated */
148663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern1, 3);
149663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern2, 5);
150663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern3, 7);
151663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern4, 9);
152663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern4, 10);
153663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern4, 11);
154663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
155663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Test exhaustion of destination length (source bytes are valid) */
156663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test3.1 ----------------\n");
157663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
158663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* No character will be written to BUFF, i.e. loop in jitted code
159663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      is not iterated */
160663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
161663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Want to write a single byte */
162663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 0, pattern1, sizeof pattern1);
163663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
164663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Want to write two bytes */
165663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 0, pattern2, sizeof pattern2);
166663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 1, pattern2, sizeof pattern2);
167663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
168663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Want to write three bytes */
169663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 0, pattern3, sizeof pattern3);
170663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 1, pattern3, sizeof pattern3);
171663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 2, pattern3, sizeof pattern3);
172663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
173663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Want to write four bytes */
174663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 0, pattern4, sizeof pattern4);
175663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 1, pattern4, sizeof pattern4);
176663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 2, pattern4, sizeof pattern4);
177663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(NULL, 3, pattern4, sizeof pattern4);
178663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
179663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test3.2 ----------------\n");
180663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* At least one character will be written to BUFF, i.e. loop in jitted
181663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      code is iterated */
182663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, 3, pattern1, sizeof pattern1);
183663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
184663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, 5, pattern2, sizeof pattern2);
185663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
186663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, 7, pattern3, sizeof pattern3);
187663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, 8, pattern3, sizeof pattern3);
188663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
189663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff,  9, pattern4, sizeof pattern4);
190663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, 10, pattern4, sizeof pattern4);
191663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, 11, pattern4, sizeof pattern4);
192663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
193663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* When both operands are exhausted, cc=0 takes precedence.
194663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      (test1 tests this for len == 0) */
195663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test4 ----------------\n");
196663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, 6, pattern1, 6);
197663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
198663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Input has invalid low surrogate. */
199663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test5 ----------------\n");
200663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, invalid, sizeof invalid);
201663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, 0, invalid, sizeof invalid);
202663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
203663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Convert all pattern buffers */
204663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test6 ----------------\n");
205663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern1, sizeof pattern1);
206663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern2, sizeof pattern2);
207663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern3, sizeof pattern3);
208663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, pattern4, sizeof pattern4);
209663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, mixed,    sizeof mixed);
210663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
211663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Make sure we only write the exact number of bytes (and not more) */
212663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   uint16_t pat[2];
213663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
214663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Write 1 byte */
215663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test7.1 ----------------\n");
216663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   pat[0] = 0x10;
217663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   write_and_check(pat, 2, 1);
218663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
219663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Write 2 bytes */
220663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test7.2 ----------------\n");
221663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   pat[0] = 0x8f;
222663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   write_and_check(pat, 2, 2);
223663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
224663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Write 3 bytes */
225663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test7.3 ----------------\n");
226663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   pat[0] = 0x842;
227663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   write_and_check(pat, 2, 3);
228663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
229663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Write 4 bytes */
230663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   printf("\n------------- test7.4 ----------------\n");
231663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   pat[0] = 0xd842;
232663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   pat[1] = 0xdc42;
233663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   write_and_check(pat, 2, 4);
234663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
235663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   return 0;
236663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng}
237663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
238663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
239663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengvoid
240663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengwrite_and_check_aux(uint16_t *input, unsigned num_input_bytes,
241663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                    unsigned num_expected_output_bytes,
242663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                    unsigned fill_byte)
243663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng{
244663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   int num_errors, i;
245663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
246663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Fill output buffer with FILL_BYTE */
247663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   memset(buff, fill_byte, sizeof buff);
248663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
249663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Execute cu21 */
250663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   run_test(buff, sizeof buff, input, num_input_bytes);
251663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
252663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Make sure the rest of the buffer is unmodified.  */
253663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   num_errors = 0;
254663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   for (i = num_expected_output_bytes; i < sizeof buff; ++i)
255663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      if (buff[i] != fill_byte) ++num_errors;
256663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   if (num_errors)
257663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      fprintf(stderr, "*** wrote more than %u bytes\n",
258663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng              num_expected_output_bytes);
259663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng}
260663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
261663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengvoid
262663860b1408516d02ebfcb3a9999a134e6cfb223Ben Chengwrite_and_check(uint16_t *input, unsigned num_input_bytes,
263663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng                unsigned num_expected_output_bytes)
264663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng{
265663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   write_and_check_aux(input, num_input_bytes, num_expected_output_bytes, 0x0);
266663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng
267663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   /* Run again with different fill pattern to make sure we did not write
268663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng      an extra 0x0 byte */
269663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng   write_and_check_aux(input, num_input_bytes, num_expected_output_bytes, 0xFF);
270663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng}
271