OpenCores
URL https://opencores.org/ocsvn/ao68000/ao68000/trunk

Subversion Repositories ao68000

[/] [ao68000/] [trunk/] [tests/] [nbcd_abcd_sbcd/] [nbcd_abcd_sbcd.c] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 alfik
/*
2
 * Copyright 2010, Aleksander Osman, alfik@poczta.fm. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without modification, are
5
 * permitted provided that the following conditions are met:
6
 *
7
 *  1. Redistributions of source code must retain the above copyright notice, this list of
8
 *     conditions and the following disclaimer.
9
 *
10
 *  2. Redistributions in binary form must reproduce the above copyright notice, this list
11
 *     of conditions and the following disclaimer in the documentation and/or other materials
12
 *     provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 */
24
 
25
#include <stdio.h>
26
 
27
/*
28
struct from_68knotes_t {
29
        unsigned char operand;
30
        unsigned int x;
31
        unsigned char unadjusted;
32
        unsigned char adjusted;
33
        unsigned int v;
34
};
35
struct from_68knotes_t from_68knotes[] = {
36
        { 0x00, 0, 0x00, 0x00, 0 },
37
        { 0x00, 1, 0xFF, 0x99, 0 },
38
        { 0xFF, 0, 0x01, 0x9B, 0 },
39
        { 0xFF, 1, 0x00, 0x9A, 0 },
40
        { 0x01, 0, 0xFF, 0x99, 0 },
41
        { 0x01, 1, 0xFE, 0x98, 0 },
42
        { 0x0F, 0, 0xF1, 0x8B, 0 },
43
        { 0x0F, 1, 0xF0, 0x8A, 0 },
44
        { 0xF0, 0, 0x10, 0xB0, 0 },
45
        { 0xF0, 1, 0x0F, 0xA9, 0 },
46
        { 0x9A, 0, 0x66, 0x00, 0 },
47
        { 0x9A, 1, 0x65, 0xFF, 0 },
48
        { 0x99, 0, 0x67, 0x01, 0 },
49
        { 0x99, 1, 0x66, 0x00, 0 },
50
        { 0x10, 0, 0xF0, 0x90, 0 },
51
        { 0x10, 1, 0xEF, 0x89, 0 },
52
        { 0x7F, 0, 0x81, 0x1B, 1 },
53
        { 0x7F, 1, 0x80, 0x1A, 1 },
54
        { 0x80, 0, 0x80, 0x20, 1 },
55
        { 0x80, 1, 0x7F, 0x19, 0 },
56
        { 0x81, 0, 0x7F, 0x19, 0 },
57
        { 0x81, 1, 0x7E, 0x18, 0 }
58
};
59
*/
60
 
61
struct input_t {
62
        unsigned char src;
63
        unsigned char dst;
64
 
65
        unsigned int x;
66
        unsigned int z;
67
        unsigned int v;
68
};
69
 
70
struct output_t {
71
        unsigned char result;
72
 
73
        unsigned int c;
74
        unsigned int v;
75
        unsigned int z;
76
        unsigned int n;
77
        unsigned int x;
78
};
79
 
80
struct output_t uae_nbcd(struct input_t in) {
81
        signed char src = in.dst;
82
 
83
        unsigned short newv_lo = - (src & 0xF) - in.x;
84
        unsigned short newv_hi = - (src & 0xF0);
85
        unsigned short newv;
86
        int cflg;
87
        if (newv_lo > 9) { newv_lo -= 6; }
88
        newv = newv_hi + newv_lo;       cflg = (newv & 0x1F0) > 0x90;
89
        if (cflg) newv -= 0x60;
90
 
91
        struct output_t out;
92
        out.c = cflg ? 1 : 0;
93
        out.x = out.c;
94
        out.z = in.z & ((((signed char)(newv)) == 0) ? 1 : 0);
95
        out.n = (((signed char)(newv)) < 0) ? 1 : 0;
96
        out.v = in.v;
97
 
98
        out.result = (newv) & 0xff;
99
        return out;
100
}
101
struct output_t verilog_nbcd(struct input_t in) {
102
        struct output_t out;
103
 
104
        unsigned char l = 25 - ((in.dst) & 0x0F);
105
        unsigned char h = 25 - (((in.dst) & 0xF0) >> 4);
106
 
107
        if( ((in.dst) & 0x0F) > 9 ) h -= 1;
108
 
109
        l &= 0x0F;
110
        h &= 0x0F;
111
 
112
        if(in.x == 0) {
113
                if(l == 9) {
114
                        l = 0;
115
                        h = (h==9) ? 0 : h+1;
116
                }
117
                else if(l == 0xF) {
118
                        l = 0;
119
                        h += 1;
120
                }
121
                else {
122
                        l += 1;
123
                }
124
        }
125
 
126
        l &= 0x0F;
127
        h &= 0x0F;
128
 
129
        out.result = (h << 4) + l;
130
 
131
        out.v = in.v;
132
        out.z = in.z & ((out.result == 0) ? 1 : 0);
133
        out.c = out.x = (in.dst == 0 && in.x == 0) ? 0 : 1;
134
        out.n = (((out.result) & 0x80) == 0) ? 0 : 1;
135
 
136
        return out;
137
}
138
 
139
struct output_t uae_abcd(struct input_t in) {
140
        signed char src = in.src;
141
        signed char dst = in.dst;
142
 
143
        unsigned short newv_lo = (src & 0xF) + (dst & 0xF) + (in.x ? 1 : 0);
144
        unsigned short newv_hi = (src & 0xF0) + (dst & 0xF0);
145
        unsigned short newv, tmp_newv;
146
        int cflg;
147
        newv = tmp_newv = newv_hi + newv_lo;    if (newv_lo > 9) { newv += 6; }
148
        cflg = (newv & 0x3F0) > 0x90;
149
        if (cflg) newv += 0x60;
150
 
151
        struct output_t out;
152
        out.c = cflg;
153
        out.x = out.c;
154
        out.z = in.z & (((signed char)(newv)) == 0);
155
        out.n = ((signed char)(newv)) < 0;
156
        out.v = (tmp_newv & 0x80) == 0 && (newv & 0x80) != 0;
157
        out.result = (newv) & 0xff;
158
 
159
        return out;
160
}
161
struct output_t verilog_abcd(struct input_t in) {
162
 
163
        unsigned char l = (in.src & 0x0F) + (in.dst & 0x0F) + in.x;
164
        unsigned char h = ((in.src & 0xF0) >> 4) + ((in.dst & 0xF0) >> 4);
165
 
166
        int tmp = (in.src + in.dst + in.x) & 0x80;
167
 
168
        l = (l > 0x09) ? (l+6) : l;
169
        h = (l > 0x1F) ? (h+2) :
170
                (l > 0x0F) ? (h+1) : h;
171
        h = (h > 0x09) ? (h+6) : h;
172
 
173
        struct output_t out;
174
        out.c = (h > 0x09) ? 1 : 0;
175
        out.x = out.c;
176
 
177
        l &= 0x0F;
178
        h &= 0x0F;
179
 
180
        out.result = (h << 4) + l;
181
 
182
        out.z = in.z & (out.result == 0);
183
        out.n = ((out.result & 0x80) == 0x80) ? 1 : 0;
184
        out.v = (tmp == 0) && ((out.result & 0x80) != 0);
185
 
186
        return out;
187
}
188
 
189
struct output_t uae_sbcd(struct input_t in) {
190
        signed char src = in.src;
191
        signed char dst = in.dst;
192
 
193
        unsigned short newv_lo = (dst & 0xF) - (src & 0xF) - (in.x ? 1 : 0);
194
        unsigned short newv_hi = (dst & 0xF0) - (src & 0xF0);
195
        unsigned short newv, tmp_newv;
196
        int bcd = 0;
197
        newv = tmp_newv = newv_hi + newv_lo;
198
        if (newv_lo & 0xF0) { newv -= 6; bcd = 6; };
199
        if ((((dst & 0xFF) - (src & 0xFF) - (in.x ? 1 : 0)) & 0x100) > 0xFF) { newv -= 0x60; }
200
 
201
        struct output_t out;
202
        out.c = (((dst & 0xFF) - (src & 0xFF) - bcd - (in.x ? 1 : 0)) & 0x300) > 0xFF;
203
        out.x = out.c;
204
        out.z = in.z & (((signed char)(newv)) == 0);
205
        out.n = ((signed char)(newv)) < 0;
206
        out.v = (tmp_newv & 0x80) != 0 && (newv & 0x80) == 0;
207
        out.result = (newv) & 0xff;
208
 
209
        return out;
210
}
211
 
212
struct output_t verilog_sbcd(struct input_t in) {
213
 
214
        unsigned char l = 32 + (in.dst & 0x0F) - (in.src & 0x0F) - in.x;
215
        unsigned char h = 32 + ((in.dst & 0xF0) >> 4) - ((in.src & 0xF0) >> 4);
216
 
217
        int tmp = in.dst - in.src - in.x;
218
 
219
        l = (l < 32) ? (l-6) : l;
220
        h = (l < 16) ? (h-2) :
221
                (l < 32) ? (h-1) : h;
222
        h = (h < 32 && (tmp & 0x100) > 0xFF) ? (h-6) : h;
223
 
224
        struct output_t out;
225
        out.c = (h < 32) ? 1 : 0;
226
        out.x = out.c;
227
 
228
        l &= 0x0F;
229
        h &= 0x0F;
230
 
231
        out.result = (h << 4) + l;
232
 
233
        out.z = in.z & (out.result == 0);
234
        out.n = ((out.result & 0x80) == 0x80) ? 1 : 0;
235
        out.v = ((tmp & 0x80) != 0) && ((out.result & 0x80) == 0);
236
 
237
        return out;
238
}
239
 
240
int test_failed = 0;
241
void compare(struct input_t in, struct output_t uae, struct output_t verilog) {
242
        if( uae.result == verilog.result &&
243
                uae.c == verilog.c &&
244
                uae.v == verilog.v &&
245
                uae.z == verilog.z &&
246
                uae.n == verilog.n &&
247
                uae.x == verilog.x
248
        ) return;
249
 
250
        //printf("%hhx + %hhx + %x: | ", in.dst, in.src, in.x);
251
        //printf("%hhx - %hhx - %x: | ", in.dst, in.src, in.x);
252
 
253
        printf("[Mismatch: in.dst: %hhx, in.src: %hhx, in.x: %x] ", in.dst, in.src, in.x);
254
 
255
        if( uae.result != verilog.result )              printf("result: %hhx != %hhx | ", uae.result, verilog.result);
256
        if( uae.c != verilog.c )                        printf("c: %x != %x | ", uae.c, verilog.c);
257
        if( uae.v != verilog.v )                        printf("v: %x != %x | ", uae.v, verilog.v);
258
        if( uae.z != verilog.z )                        printf("z: %x != %x | ", uae.z, verilog.z);
259
        if( uae.n != verilog.n )                        printf("n: %x != %x | ", uae.n, verilog.n);
260
        if( uae.x != verilog.x )                        printf("x: %x != %x | ", uae.x, verilog.x);
261
        printf("\n");
262
 
263
        test_failed = 1;
264
}
265
 
266
int main(int argc, char **argv) {
267
        struct input_t in;
268
 
269
        int i,j,k,l,m;
270
        for(i=0; i<256; i++) {
271
                for(j=0; j<256; j++) {
272
                        for(k=0; k<2; k++) {
273
                                for(l=0; l<2; l++) {
274
                                        for(m=0; m<2; m++) {
275
                                                in.src = i;
276
                                                in.dst = j;
277
                                                in.x = k;
278
                                                in.z = l;
279
                                                in.v = m;
280
 
281
                                                struct output_t uae0 = uae_nbcd(in);
282
                                                struct output_t verilog0 = verilog_nbcd(in);
283
 
284
                                                compare(in, uae0, verilog0);
285
 
286
                                                struct output_t uae1 = uae_abcd(in);
287
                                                struct output_t verilog1 = verilog_abcd(in);
288
 
289
                                                compare(in, uae1, verilog1);
290
 
291
                                                struct output_t uae2 = uae_sbcd(in);
292
                                                struct output_t verilog2 = verilog_sbcd(in);
293
 
294
                                                compare(in, uae2, verilog2);
295
                                        }
296
                                }
297
                        }
298
                }
299
        }
300
        if(test_failed) printf("Test FAILED.\n");
301
        else            printf("Test OK.\n");
302
 
303
        return 0;
304
}
305
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.