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

Subversion Repositories jtag_stapl_player

[/] [jtag_stapl_player/] [trunk/] [jamarray.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sukhanov
/****************************************************************************/
2
/*                                                                                                                                                      */
3
/*      Module:                 jamarray.c                                                                                              */
4
/*                                                                                                                                                      */
5
/*                                      Copyright (C) Altera Corporation 1997                                   */
6
/*                                                                                                                                                      */
7
/*      Description:    Contains array management functions, including                  */
8
/*                                      functions for reading array initialization data in              */
9
/*                                      compressed formats.                                                                             */
10
/*                                                                                                                                                      */
11
/*      Revisions:              1.1     added support for dynamic memory allocation                     */
12
/*                                                                                                                                                      */
13
/****************************************************************************/
14
 
15
/****************************************************************************/
16
/*                                                                                                                                                      */
17
/*      Actel version 1.1             May 2003                                                                  */
18
/*                                                                                                                                                      */
19
/****************************************************************************/
20
 
21
#include "jamexprt.h"
22
#include "jamdefs.h"
23
#include "jamexec.h"
24
#include "jamexp.h"
25
#include "jamsym.h"
26
#include "jamstack.h"
27
#include "jamheap.h"
28
#include "jamutil.h"
29
#include "jamcomp.h"
30
#include "jamarray.h"
31
 
32
/*
33
*       Table of names of Boolean data representation schemes
34
*/
35
struct JAMS_BOOL_REP_MAP
36
{
37
        JAME_BOOLEAN_REP rep;
38
        char string[4];
39
} jam_bool_rep_table[] =
40
{
41
        { JAM_BOOL_BINARY,      "BIN" },
42
        { JAM_BOOL_HEX,         "HEX" },
43
        { JAM_BOOL_RUN_LENGTH,  "RLC" },
44
        { JAM_BOOL_COMPRESSED,  "ACA" },
45
};
46
 
47
#define JAMC_BOOL_REP_COUNT \
48
  ((int) (sizeof(jam_bool_rep_table) / sizeof(jam_bool_rep_table[0])))
49
 
50
#define JAMC_DICTIONARY_SIZE 4096
51
 
52
typedef enum
53
{
54
        JAM_CONSTANT_ZEROS,
55
        JAM_CONSTANT_ONES,
56
        JAM_RANDOM
57
 
58
} JAME_RLC_BLOCK_TYPE;
59
 
60
JAM_RETURN_TYPE jam_reverse_boolean_array_bin
61
(
62
        JAMS_HEAP_RECORD *heap_record
63
)
64
{
65
        long *heap_data = &heap_record->data[0];
66
        long dimension = heap_record->dimension;
67
        int a, b;
68
        long i, j;
69
 
70
        for (i = 0; i < dimension / 2; ++i)
71
        {
72
                j = (dimension - 1) - i;
73
                a = (heap_data[i >> 5] & (1L << (i & 0x1f))) ? 1 : 0;
74
                b = (heap_data[j >> 5] & (1L << (j & 0x1f))) ? 1 : 0;
75
                if (a)
76
                {
77
                        heap_data[j >> 5] |= (1L << (j & 0x1f));
78
                }
79
                else
80
                {
81
                        heap_data[j >> 5] &= ~(1L << (j & 0x1f));
82
                }
83
                if (b)
84
                {
85
                        heap_data[i >> 5] |= (1L << (i & 0x1f));
86
                }
87
                else
88
                {
89
                        heap_data[i >> 5] &= ~(1L << (i & 0x1f));
90
                }
91
        }
92
 
93
        return (JAMC_SUCCESS);
94
}
95
 
96
JAM_RETURN_TYPE jam_reverse_boolean_array_hex
97
(
98
        JAMS_HEAP_RECORD *heap_record
99
)
100
{
101
        long *heap_data = &heap_record->data[0];
102
        long nibbles = (heap_record->dimension + 3) / 4;
103
        long a, b, i, j;
104
 
105
        for (i = 0; i < nibbles / 2; ++i)
106
        {
107
                j = (nibbles - 1) - i;
108
                a = (heap_data[i >> 3] >> ((i & 7) << 2)) & 0x0f;
109
                b = (heap_data[j >> 3] >> ((j & 7) << 2)) & 0x0f;
110
                heap_data[j >> 3] &= ~(0x0fL << ((j & 7) << 2));
111
                heap_data[j >> 3] |= (a << ((j & 7) << 2));
112
                heap_data[i >> 3] &= ~(0x0fL << ((i & 7) << 2));
113
                heap_data[i >> 3] |= (b << ((i & 7) << 2));
114
        }
115
 
116
        return (JAMC_SUCCESS);
117
}
118
 
119
/****************************************************************************/
120
/*                                                                                                                                                      */
121
 
122
JAM_RETURN_TYPE jam_extract_bool_comma_sep
123
(
124
        JAMS_HEAP_RECORD *heap_record,
125
        char *statement_buffer
126
)
127
 
128
/*                                                                                                                                                      */
129
/*      Description:    Extracts Boolean array data from statement buffer.              */
130
/*                                      Works on data in comma separated representation.                */
131
/*                                                                                                                                                      */
132
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
133
/*                                                                                                                                                      */
134
/****************************************************************************/
135
{
136
        int index = 0;
137
        int expr_begin = 0;
138
        int expr_end = 0;
139
        char save_ch = 0;
140
        long address = 0L;
141
        long value = 0L;
142
        long dimension = heap_record->dimension;
143
        JAME_EXPRESSION_TYPE expr_type = JAM_ILLEGAL_EXPR_TYPE;
144
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
145
        long *heap_data = &heap_record->data[0];
146
 
147
        for (address = 0L; (status == JAMC_SUCCESS) && (address < dimension);
148
                ++address)
149
        {
150
                status = JAMC_SYNTAX_ERROR;
151
 
152
                while ((jam_isspace(statement_buffer[index])) &&
153
                        (index < JAMC_MAX_STATEMENT_LENGTH))
154
                {
155
                        ++index;        /* skip over white space */
156
                }
157
 
158
                expr_begin = index;
159
                expr_end = 0;
160
 
161
                while ((statement_buffer[index] != JAMC_COMMA_CHAR) &&
162
                        (statement_buffer[index] != JAMC_SEMICOLON_CHAR) &&
163
                        (index < JAMC_MAX_STATEMENT_LENGTH))
164
                {
165
                        ++index;        /* skip over the expression */
166
                }
167
 
168
                if ((statement_buffer[index] == JAMC_COMMA_CHAR) ||
169
                        (statement_buffer[index] == JAMC_SEMICOLON_CHAR))
170
                {
171
                        expr_end = index;
172
                }
173
 
174
                if (expr_end > expr_begin)
175
                {
176
                        save_ch = statement_buffer[expr_end];
177
                        statement_buffer[expr_end] = JAMC_NULL_CHAR;
178
                        status = jam_evaluate_expression(
179
                                &statement_buffer[expr_begin], &value, &expr_type);
180
                        statement_buffer[expr_end] = save_ch;
181
                }
182
 
183
                if ((status == JAMC_SUCCESS) &&
184
                        ((expr_type != JAM_BOOLEAN_EXPR) &&
185
                        (expr_type != JAM_INT_OR_BOOL_EXPR)))
186
                {
187
                        status = JAMC_TYPE_MISMATCH;
188
                }
189
 
190
                if (status == JAMC_SUCCESS)
191
                {
192
                        if (value == 0L)
193
                        {
194
                                /* clear a single bit */
195
                                heap_data[address >> 5] &=
196
                                        (~(unsigned long)(1L << (address & 0x1f)));
197
                        }
198
                        else if (value == 1L)
199
                        {
200
                                /* set a single bit */
201
                                heap_data[address >> 5] |= (1L << (address & 0x1f));
202
                        }
203
                        else
204
                        {
205
                                status = JAMC_SYNTAX_ERROR;
206
                        }
207
 
208
                        if ((address < dimension) &&
209
                                (statement_buffer[index] == JAMC_COMMA_CHAR))
210
                        {
211
                                ++index;
212
                        }
213
                }
214
        }
215
 
216
        if (status == JAMC_SUCCESS)
217
        {
218
                while ((jam_isspace(statement_buffer[index])) &&
219
                        (index < JAMC_MAX_STATEMENT_LENGTH))
220
                {
221
                        ++index;        /* skip over white space */
222
                }
223
 
224
                if (statement_buffer[index] != JAMC_SEMICOLON_CHAR)
225
                {
226
                        status = JAMC_SYNTAX_ERROR;
227
                }
228
        }
229
 
230
        return (status);
231
}
232
 
233
/****************************************************************************/
234
/*                                                                                                                                                      */
235
 
236
JAM_RETURN_TYPE jam_extract_bool_binary
237
(
238
        JAMS_HEAP_RECORD *heap_record,
239
        char *statement_buffer
240
)
241
 
242
/*                                                                                                                                                      */
243
/*      Description:    Extracts Boolean array data from statement buffer.              */
244
/*                                      Works on data in binary (001100100101) representation.  */
245
/*                                                                                                                                                      */
246
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
247
/*                                                                                                                                                      */
248
/****************************************************************************/
249
{
250
        int index = 0;
251
        long address = 0L;
252
        long dimension = heap_record->dimension;
253
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
254
        long *heap_data = &heap_record->data[0];
255
 
256
        for (address = 0L; (status == JAMC_SUCCESS) && (address < dimension);
257
                ++address)
258
        {
259
                while ((jam_isspace(statement_buffer[index])) &&
260
                        (index < JAMC_MAX_STATEMENT_LENGTH))
261
                {
262
                        ++index;        /* skip over white space */
263
                }
264
 
265
                if (statement_buffer[index] == '0')
266
                {
267
                        /* clear a single bit */
268
                        heap_data[address >> 5] &=
269
                                (~(unsigned long)(1L << (address & 0x1f)));
270
                }
271
                else if (statement_buffer[index] == '1')
272
                {
273
                        /* set a single bit */
274
                        heap_data[address >> 5] |= (1L << (address & 0x1f));
275
                }
276
                else
277
                {
278
                        status = JAMC_SYNTAX_ERROR;
279
                }
280
 
281
                ++index;
282
        }
283
 
284
        if (status == JAMC_SUCCESS)
285
        {
286
                while ((jam_isspace(statement_buffer[index])) &&
287
                        (index < JAMC_MAX_STATEMENT_LENGTH))
288
                {
289
                        ++index;        /* skip over white space */
290
                }
291
 
292
                if (statement_buffer[index] != JAMC_SEMICOLON_CHAR)
293
                {
294
                        status = JAMC_SYNTAX_ERROR;
295
                }
296
        }
297
 
298
        return (status);
299
}
300
 
301
/****************************************************************************/
302
/*                                                                                                                                                      */
303
 
304
JAM_RETURN_TYPE jam_extract_bool_hex
305
(
306
        JAMS_HEAP_RECORD *heap_record,
307
        char *statement_buffer
308
)
309
 
310
/*                                                                                                                                                      */
311
/*      Description:    Extracts Boolean array data from statement buffer.              */
312
/*                                      Works on data in hexadecimal (3BA97C0F) representation. */
313
/*                                                                                                                                                      */
314
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
315
/*                                                                                                                                                      */
316
/****************************************************************************/
317
{
318
        int index = 0;
319
        int ch = 0;
320
        long data = 0L;
321
        long nibble = 0L;
322
        long nibbles = 0L;
323
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
324
        long *heap_data = &heap_record->data[0];
325
 
326
        /* compute number of hex digits expected */
327
        nibbles = (heap_record->dimension >> 2) +
328
                ((heap_record->dimension & 3) ? 1 : 0);
329
 
330
        for (nibble = 0L; (status == JAMC_SUCCESS) && (nibble < nibbles); ++nibble)
331
        {
332
                while ((jam_isspace(statement_buffer[index])) &&
333
                        (index < JAMC_MAX_STATEMENT_LENGTH))
334
                {
335
                        ++index;        /* skip over white space */
336
                }
337
 
338
                ch = (int) statement_buffer[index];
339
 
340
                if ((ch >= 'A') && (ch <= 'F'))
341
                {
342
                        data = (long) (ch + 10 - 'A');
343
                }
344
                else if ((ch >= 'a') && (ch <= 'f'))
345
                {
346
                        data = (long) (ch + 10 - 'a');
347
                }
348
                else if ((ch >= '0') && (ch <= '9'))
349
                {
350
                        data = (long) (ch - '0');
351
                }
352
                else
353
                {
354
                        status = JAMC_SYNTAX_ERROR;
355
                }
356
 
357
                if (status == JAMC_SUCCESS)
358
                {
359
                        /* modify four bits of data in the array */
360
                        heap_data[nibble >> 3] = (heap_data[nibble >> 3] &
361
                                (~(unsigned long) (15L << ((nibble & 7) << 2)))) |
362
                                (data << ((nibble & 7) << 2));
363
                }
364
 
365
                ++index;
366
        }
367
 
368
        if (status == JAMC_SUCCESS)
369
        {
370
                while ((jam_isspace(statement_buffer[index])) &&
371
                        (index < JAMC_MAX_STATEMENT_LENGTH))
372
                {
373
                        ++index;        /* skip over white space */
374
                }
375
 
376
                if (statement_buffer[index] != JAMC_SEMICOLON_CHAR)
377
                {
378
                        status = JAMC_SYNTAX_ERROR;
379
                }
380
        }
381
 
382
        return (status);
383
}
384
 
385
/****************************************************************************/
386
/*                                                                                                                                                      */
387
 
388
int jam_6bit_char(int ch)
389
 
390
/*                                                                                                                                                      */
391
/*      Description:    Extracts numeric value from ASCII character code,               */
392
/*                                      based on character mapping defined in JAM language              */
393
/*                                      specification.  Numeric value is in range 0 to 63.              */
394
/*                                      Used for RLC and ACA data representations.                              */
395
/*                                                                                                                                                      */
396
/*      Returns:                Integer value in range 0 to 63, or -1 for error.                */
397
/*                                                                                                                                                      */
398
/****************************************************************************/
399
{
400
        int result = 0;
401
 
402
        if ((ch >= '0') && (ch <= '9')) result = (ch - '0');
403
        else if ((ch >= 'A') && (ch <= 'Z')) result = (ch + 10 - 'A');
404
        else if ((ch >= 'a') && (ch <= 'z')) result = (ch + 36 - 'a');
405
        else if (ch == '_') result = 62;
406
        else if (ch == '@') result = 63;
407
        else result = -1;       /* illegal character */
408
 
409
        return (result);
410
}
411
 
412
/****************************************************************************/
413
/*                                                                                                                                                      */
414
 
415
BOOL jam_rlc_key_char
416
(
417
        int ch,
418
        JAME_RLC_BLOCK_TYPE *block_type,
419
        int *count_size
420
)
421
 
422
/*                                                                                                                                                      */
423
/*      Description:    Decodes RLC block ID character.  Returns block type             */
424
/*                                      and count size (number of count characters in the               */
425
/*                                      block) by reference.                                                                    */
426
/*                                                                                                                                                      */
427
/*      Returns:                TRUE for success, FALSE if illegal block ID character   */
428
/*                                                                                                                                                      */
429
/****************************************************************************/
430
{
431
        BOOL status = TRUE;
432
 
433
        if ((ch >= 'A') && (ch <= 'E'))
434
        {
435
                *block_type = JAM_CONSTANT_ZEROS;
436
                *count_size = (ch + 1 - 'A');
437
        }
438
        else if ((ch >= 'I') && (ch <= 'M'))
439
        {
440
                *block_type = JAM_CONSTANT_ONES;
441
                *count_size = (ch + 1 - 'I');
442
        }
443
        else if ((ch >= 'Q') && (ch <= 'U'))
444
        {
445
                *block_type = JAM_RANDOM;
446
                *count_size = (ch + 1 - 'Q');
447
        }
448
        else
449
        {
450
                status = FALSE;
451
        }
452
 
453
        return (status);
454
}
455
 
456
/****************************************************************************/
457
/*                                                                                                                                                      */
458
 
459
JAM_RETURN_TYPE jam_extract_bool_run_length
460
(
461
        JAMS_HEAP_RECORD *heap_record,
462
        char *statement_buffer
463
)
464
 
465
/*                                                                                                                                                      */
466
/*      Description:    Extracts Boolean array data from statement buffer.              */
467
/*                                      Works on data encoded using RLC (run-length compressed) */
468
/*                                      representation.                                                                                 */
469
/*                                                                                                                                                      */
470
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
471
/*                                                                                                                                                      */
472
/****************************************************************************/
473
{
474
        int index = 0;
475
        int index2 = 0;
476
        int count_index = 0;
477
        int count_size = 0;
478
        int value = 0;
479
        long bit = 0L;
480
        long count = 0L;
481
        long address = 0L;
482
        long dimension = heap_record->dimension;
483
        JAME_RLC_BLOCK_TYPE block_type = JAM_CONSTANT_ZEROS;
484
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
485
        long *heap_data = &heap_record->data[0];
486
 
487
        /* remove all white space */
488
        while (statement_buffer[index] != JAMC_NULL_CHAR)
489
        {
490
                if (!jam_isspace(statement_buffer[index]))
491
                {
492
                        statement_buffer[index2] = statement_buffer[index];
493
                        ++index2;
494
                }
495
                ++index;
496
        }
497
        statement_buffer[index2] = JAMC_NULL_CHAR;
498
 
499
        index = 0;
500
        while ((status == JAMC_SUCCESS) && (address < dimension))
501
        {
502
                if (jam_rlc_key_char(statement_buffer[index], &block_type, &count_size))
503
                {
504
                        ++index;
505
 
506
                        count = 0L;
507
                        for (count_index = 0; count_index < count_size; ++count_index)
508
                        {
509
                                count <<= 6;
510
                                value = jam_6bit_char(statement_buffer[index]);
511
                                if (value == -1)
512
                                {
513
                                        status = JAMC_SYNTAX_ERROR;
514
                                }
515
                                else
516
                                {
517
                                        count |= value;
518
                                }
519
                                ++index;
520
                        }
521
 
522
                        if (status == JAMC_SUCCESS)
523
                        {
524
                                switch (block_type)
525
                                {
526
                                case JAM_CONSTANT_ZEROS:
527
                                        for (bit = 0; bit < count; bit++)
528
                                        {
529
                                                /* add zeros to array */
530
                                                heap_data[address >> 5] &=
531
                                                        ~(unsigned long) (1L << (address & 0x1f));
532
                                                ++address;
533
                                        }
534
                                        break;
535
 
536
                                case JAM_CONSTANT_ONES:
537
                                        for (bit = 0; bit < count; bit++)
538
                                        {
539
                                                /* add ones to array */
540
                                                heap_data[address >> 5] |= (1L << (address & 0x1f));
541
                                                ++address;
542
                                        }
543
                                        break;
544
 
545
                                case JAM_RANDOM:
546
                                        for (bit = 0; bit < count; bit++)
547
                                        {
548
                                                /* add random data to array */
549
                                                value = jam_6bit_char(statement_buffer[index + (bit / 6)]);
550
                                                if (value == -1)
551
                                                {
552
                                                        status = JAMC_SYNTAX_ERROR;
553
                                                }
554
                                                else if (value & (1 << (bit % 6)))
555
                                                {
556
                                                        heap_data[address >> 5] |= (1L << (address & 0x1f));
557
                                                }
558
                                                else
559
                                                {
560
                                                        heap_data[address >> 5] &=
561
                                                                ~(unsigned long) (1L << (address & 0x1f));
562
                                                }
563
                                                ++address;
564
                                        }
565
                                        index = index + (int)((count / 6) + ((count % 6) ? 1 : 0));
566
                                        break;
567
 
568
                                default:
569
                                        status = JAMC_SYNTAX_ERROR;
570
                                        break;
571
                                }
572
                        }
573
                }
574
                else
575
                {
576
                        /* unrecognized key character */
577
                        status = JAMC_SYNTAX_ERROR;
578
                }
579
        }
580
 
581
        if ((status == JAMC_SUCCESS) &&
582
                (statement_buffer[index] != JAMC_SEMICOLON_CHAR))
583
        {
584
                status = JAMC_SYNTAX_ERROR;
585
        }
586
 
587
        if ((status == JAMC_SUCCESS) && (address != dimension))
588
        {
589
                status = JAMC_SYNTAX_ERROR;
590
        }
591
 
592
        return (status);
593
}
594
 
595
/****************************************************************************/
596
/*                                                                                                                                                      */
597
 
598
JAM_RETURN_TYPE jam_extract_bool_compressed
599
(
600
        JAMS_HEAP_RECORD *heap_record,
601
        char *statement_buffer
602
)
603
 
604
/*                                                                                                                                                      */
605
/*      Description:    Extracts Boolean array data from statement buffer.              */
606
/*                                      Works on data encoded using ACA representation.                 */
607
/*                                                                                                                                                      */
608
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
609
/*                                                                                                                                                      */
610
/****************************************************************************/
611
{
612
        int bit = 0;
613
        int word = 0;
614
        int value = 0;
615
        int index = 0;
616
        int index2 = 0;
617
        long uncompressed_length = 0L;
618
        char *ch_data = NULL;
619
        long out_size = 0L;
620
        long address = 0L;
621
        long *heap_data = &heap_record->data[0];
622
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
623
 
624
        /* remove all white space */
625
        while (statement_buffer[index] != JAMC_NULL_CHAR)
626
        {
627
                if (!jam_isspace(statement_buffer[index]))
628
                {
629
                        statement_buffer[index2] = statement_buffer[index];
630
                        ++index2;
631
                }
632
                ++index;
633
        }
634
        statement_buffer[index2] = JAMC_NULL_CHAR;
635
 
636
        /* convert 6-bit encoded characters to binary -- in the same buffer */
637
        index = 0;
638
        while ((status == JAMC_SUCCESS) &&
639
                (statement_buffer[index] != JAMC_NULL_CHAR) &&
640
                (statement_buffer[index] != JAMC_SEMICOLON_CHAR))
641
        {
642
                value = jam_6bit_char(statement_buffer[index]);
643
                statement_buffer[index] = 0;
644
 
645
                if (value == -1)
646
                {
647
                        status = JAMC_SYNTAX_ERROR;
648
                }
649
                else
650
                {
651
                        for (bit = 0; bit < 6; ++bit)
652
                        {
653
                                if (value & (1 << (bit % 6)))
654
                                {
655
                                        statement_buffer[address >> 3] |= (1L << (address & 7));
656
                                }
657
                                else
658
                                {
659
                                        statement_buffer[address >> 3] &=
660
                                                ~(unsigned int) (1 << (address & 7));
661
                                }
662
                                ++address;
663
                        }
664
                }
665
 
666
                ++index;
667
        }
668
 
669
        if ((status == JAMC_SUCCESS) &&
670
                (statement_buffer[index] != JAMC_SEMICOLON_CHAR))
671
        {
672
                status = JAMC_SYNTAX_ERROR;
673
        }
674
 
675
        /*
676
        *       We need two memory buffers:
677
        *
678
        *       in   (length of compressed bitstream)
679
        *       out  (length of uncompressed bitstream)
680
        *
681
        *       The statement buffer is re-used for the "in" buffer.  The "out"
682
        *       buffer is inside the heap record.
683
        */
684
 
685
        if (status == JAMC_SUCCESS)
686
        {
687
                /*
688
                *       Uncompress the data
689
                */
690
                out_size = (heap_record->dimension >> 3) +
691
                        ((heap_record->dimension & 7) ? 1 : 0);
692
 
693
                uncompressed_length = jam_uncompress(
694
                        statement_buffer,
695
                        (address >> 3) + ((address & 7) ? 1 : 0),
696
                        (char *)heap_data,
697
                        out_size,
698
                        jam_version);
699
 
700
                if (uncompressed_length != out_size)
701
                {
702
                        status = JAMC_SYNTAX_ERROR;
703
                }
704
                else
705
                {
706
                        /* convert data from bytes into 32-bit words */
707
                        out_size = (heap_record->dimension >> 5) +
708
                                ((heap_record->dimension & 0x1f) ? 1 : 0);
709
                        ch_data = (char *)heap_data;
710
 
711
                        for (word = 0; word < out_size; ++word)
712
                        {
713
                                heap_data[word] =
714
                                        ((((long) ch_data[(word * 4) + 3]) & 0xff) << 24L) |
715
                                        ((((long) ch_data[(word * 4) + 2]) & 0xff) << 16L) |
716
                                        ((((long) ch_data[(word * 4) + 1]) & 0xff) << 8L) |
717
                                        (((long) ch_data[word * 4]) & 0xff);
718
                        }
719
                }
720
        }
721
 
722
        return (status);
723
}
724
 
725
/****************************************************************************/
726
/*                                                                                                                                                      */
727
 
728
int jam_get_real_char(void)
729
 
730
/*                                                                                                                                                      */
731
/*      Description:    Gets next character from input stream, eliminating              */
732
/*                                      white space and comments.                                                               */
733
/*                                                                                                                                                      */
734
/*      Returns:                Character code, or EOF if no characters available               */
735
/*                                                                                                                                                      */
736
/****************************************************************************/
737
{
738
        int ch = 0;
739
        BOOL comment = FALSE;
740
        BOOL found = FALSE;
741
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
742
 
743
        while ((status == JAMC_SUCCESS) && (!found))
744
        {
745
                ch = jam_getc();
746
 
747
                if ((!comment) && (ch == JAMC_COMMENT_CHAR))
748
                {
749
                        /* beginning of comment */
750
                        comment = TRUE;
751
                }
752
 
753
                if (!comment)
754
                {
755
                        if (!jam_isspace((char) ch))
756
                        {
757
                                found = TRUE;
758
                        }
759
                }
760
 
761
                if (ch == EOF)
762
                {
763
                        /* end of file */
764
                        status = JAMC_UNEXPECTED_END;
765
                }
766
 
767
                if (comment &&
768
                        ((ch == JAMC_NEWLINE_CHAR) || (ch == JAMC_RETURN_CHAR)))
769
                {
770
                        /* end of comment */
771
                        comment = FALSE;
772
                }
773
        }
774
 
775
        return (ch);
776
}
777
 
778
/****************************************************************************/
779
/*                                                                                                                                                      */
780
 
781
JAM_RETURN_TYPE jam_read_bool_comma_sep
782
(
783
        JAMS_HEAP_RECORD *heap_record
784
)
785
 
786
/*                                                                                                                                                      */
787
/*      Description:    Reads Boolean array data directly from input stream.    */
788
/*                                      Works on data in comma separated representation.                */
789
/*                                                                                                                                                      */
790
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
791
/*                                                                                                                                                      */
792
/****************************************************************************/
793
{
794
        int index = 0;
795
        int ch = 0;
796
        long address = 0L;
797
        long value = 0L;
798
        long dimension = heap_record->dimension;
799
        char expr_buffer[JAMC_MAX_STATEMENT_LENGTH + 1];
800
        JAME_EXPRESSION_TYPE expr_type = JAM_ILLEGAL_EXPR_TYPE;
801
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
802
        long *heap_data = &heap_record->data[0];
803
 
804
        if (jam_seek(heap_record->position) != 0)
805
        {
806
                status = JAMC_IO_ERROR;
807
        }
808
 
809
        while ((status == JAMC_SUCCESS) && (address < dimension))
810
        {
811
                ch = jam_get_real_char();
812
 
813
                if (((ch == JAMC_COMMA_CHAR) && (address < (dimension - 1))) ||
814
                        ((ch == JAMC_SEMICOLON_CHAR) && (address == (dimension - 1))))
815
                {
816
                        expr_buffer[index] = JAMC_NULL_CHAR;
817
                        index = 0;
818
 
819
                        status = jam_evaluate_expression(
820
                                expr_buffer, &value, &expr_type);
821
 
822
                        if ((status == JAMC_SUCCESS) &&
823
                                ((expr_type != JAM_BOOLEAN_EXPR) &&
824
                                (expr_type != JAM_INT_OR_BOOL_EXPR)))
825
                        {
826
                                status = JAMC_TYPE_MISMATCH;
827
                        }
828
 
829
                        if (status == JAMC_SUCCESS)
830
                        {
831
                                if (value == 0L)
832
                                {
833
                                        /* clear a single bit */
834
                                        heap_data[address >> 5] &=
835
                                                (~(unsigned long)(1L << (address & 0x1f)));
836
                                        ++address;
837
                                }
838
                                else if (value == 1L)
839
                                {
840
                                        /* set a single bit */
841
                                        heap_data[address >> 5] |= (1L << (address & 0x1f));
842
                                        ++address;
843
                                }
844
                                else
845
                                {
846
                                        status = JAMC_TYPE_MISMATCH;
847
                                }
848
                        }
849
                }
850
                else
851
                {
852
                        expr_buffer[index] = (char) ch;
853
 
854
                        if (index < JAMC_MAX_STATEMENT_LENGTH)
855
                        {
856
                                ++index;
857
                        }
858
                        else
859
                        {
860
                                /* expression was too long */
861
                                status = JAMC_SYNTAX_ERROR;
862
                        }
863
                }
864
 
865
                if (ch == EOF)
866
                {
867
                        /* end of file */
868
                        status = JAMC_UNEXPECTED_END;
869
                }
870
        }
871
 
872
        return (status);
873
}
874
 
875
/****************************************************************************/
876
/*                                                                                                                                                      */
877
 
878
JAM_RETURN_TYPE jam_read_bool_binary
879
(
880
        JAMS_HEAP_RECORD *heap_record
881
)
882
 
883
/*                                                                                                                                                      */
884
/*      Description:    Reads Boolean array data directly from input stream.    */
885
/*                                      Works on data in binary (001100100101) representation.  */
886
/*                                                                                                                                                      */
887
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
888
/*                                                                                                                                                      */
889
/****************************************************************************/
890
{
891
        int ch = 0;
892
        long address = 0L;
893
        long dimension = heap_record->dimension;
894
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
895
        long *heap_data = &heap_record->data[0];
896
 
897
        if (jam_seek(heap_record->position) != 0)
898
        {
899
                status = JAMC_IO_ERROR;
900
        }
901
 
902
        while ((status == JAMC_SUCCESS) && (address < dimension))
903
        {
904
                ch = jam_get_real_char();
905
 
906
                if (ch == '0')
907
                {
908
                        /* clear a single bit */
909
                        heap_data[address >> 5] &=
910
                                (~(unsigned long)(1L << (address & 0x1f)));
911
                        ++address;
912
                }
913
                else if (ch == '1')
914
                {
915
                        /* set a single bit */
916
                        heap_data[address >> 5] |= (1L << (address & 0x1f));
917
                        ++address;
918
                }
919
                else
920
                {
921
                        status = JAMC_SYNTAX_ERROR;
922
                }
923
 
924
                if (ch == EOF)
925
                {
926
                        /* end of file */
927
                        status = JAMC_UNEXPECTED_END;
928
                }
929
        }
930
 
931
        if (status == JAMC_SUCCESS)
932
        {
933
                ch = jam_get_real_char();
934
 
935
                if (ch != JAMC_SEMICOLON_CHAR)
936
                {
937
                        status = JAMC_SYNTAX_ERROR;
938
                }
939
        }
940
 
941
        return (status);
942
}
943
 
944
/****************************************************************************/
945
/*                                                                                                                                                      */
946
 
947
JAM_RETURN_TYPE jam_read_bool_hex
948
(
949
        JAMS_HEAP_RECORD *heap_record
950
)
951
 
952
/*                                                                                                                                                      */
953
/*      Description:    Reads Boolean array data directly from input stream.    */
954
/*                                      Works on data in hexadecimal (3BA97C0F) representation. */
955
/*                                                                                                                                                      */
956
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
957
/*                                                                                                                                                      */
958
/****************************************************************************/
959
{
960
        int ch = 0;
961
        long data = 0L;
962
        long nibble = 0L;
963
        long nibbles = 0L;
964
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
965
        long *heap_data = &heap_record->data[0];
966
 
967
        /* compute number of hex digits expected */
968
        nibbles = (heap_record->dimension >> 2) +
969
                ((heap_record->dimension & 3) ? 1 : 0);
970
 
971
        if (jam_seek(heap_record->position) != 0)
972
        {
973
                status = JAMC_IO_ERROR;
974
        }
975
 
976
        while ((status == JAMC_SUCCESS) && (nibble < nibbles))
977
        {
978
                ch = jam_get_real_char();
979
 
980
                if ((ch >= 'A') && (ch <= 'F'))
981
                {
982
                        data = (long) (ch + 10 - 'A');
983
                }
984
                else if ((ch >= 'a') && (ch <= 'f'))
985
                {
986
                        data = (long) (ch + 10 - 'a');
987
                }
988
                else if ((ch >= '0') && (ch <= '9'))
989
                {
990
                        data = (long) (ch - '0');
991
                }
992
                else
993
                {
994
                        status = JAMC_SYNTAX_ERROR;
995
                }
996
 
997
                if (status == JAMC_SUCCESS)
998
                {
999
                        /* modify four bits of data in the array */
1000
                        heap_data[nibble >> 3] = (heap_data[nibble >> 3] &
1001
                                (~(unsigned long) (15L << ((nibble & 7) << 2)))) |
1002
                                (data << ((nibble & 7) << 2));
1003
                        ++nibble;
1004
                }
1005
 
1006
                if (ch == EOF)
1007
                {
1008
                        /* end of file */
1009
                        status = JAMC_UNEXPECTED_END;
1010
                }
1011
        }
1012
 
1013
        return (status);
1014
}
1015
 
1016
/****************************************************************************/
1017
/*                                                                                                                                                      */
1018
 
1019
JAM_RETURN_TYPE jam_read_bool_run_length
1020
(
1021
        JAMS_HEAP_RECORD *heap_record
1022
)
1023
 
1024
/*                                                                                                                                                      */
1025
/*      Description:    Reads Boolean array data directly from input stream.    */
1026
/*                                      Works on data encoded using RLC (run-length compressed) */
1027
/*                                      representation.                                                                                 */
1028
/*                                                                                                                                                      */
1029
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1030
/*                                                                                                                                                      */
1031
/****************************************************************************/
1032
{
1033
        int ch = 0;
1034
        int count_index = 0;
1035
        int count_size = 0;
1036
        int value = 0;
1037
        long bit = 0L;
1038
        long count = 0L;
1039
        long address = 0L;
1040
        long dimension = heap_record->dimension;
1041
        JAME_RLC_BLOCK_TYPE block_type = JAM_CONSTANT_ZEROS;
1042
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1043
        long *heap_data = &heap_record->data[0];
1044
 
1045
        if (jam_seek(heap_record->position) != 0)
1046
        {
1047
                status = JAMC_IO_ERROR;
1048
        }
1049
 
1050
        while ((status == JAMC_SUCCESS) && (address < dimension))
1051
        {
1052
                if (jam_rlc_key_char(jam_get_real_char(), &block_type, &count_size))
1053
                {
1054
                        count = 0L;
1055
                        for (count_index = 0; count_index < count_size; ++count_index)
1056
                        {
1057
                                count <<= 6;
1058
                                value = jam_6bit_char(jam_get_real_char());
1059
                                if (value == -1)
1060
                                {
1061
                                        status = JAMC_SYNTAX_ERROR;
1062
                                }
1063
                                else
1064
                                {
1065
                                        count += (long) value;
1066
                                }
1067
                        }
1068
 
1069
                        switch (block_type)
1070
                        {
1071
                        case JAM_CONSTANT_ZEROS:
1072
                                for (bit = 0; bit < count; bit++)
1073
                                {
1074
                                        /* add zeros to array */
1075
                                        heap_data[address >> 5] &=
1076
                                                ~(unsigned long) (1L << (address & 0x1f));
1077
                                        ++address;
1078
                                }
1079
                                break;
1080
 
1081
                        case JAM_CONSTANT_ONES:
1082
                                for (bit = 0; bit < count; bit++)
1083
                                {
1084
                                        /* add ones to array */
1085
                                        heap_data[address >> 5] |= (1L << (address & 0x1f));
1086
                                        ++address;
1087
                                }
1088
                                break;
1089
 
1090
                        case JAM_RANDOM:
1091
                                for (bit = 0; bit < count; bit++)
1092
                                {
1093
                                        /* add random data to array */
1094
                                        if ((bit % 6) == 0)
1095
                                        {
1096
                                                value = jam_6bit_char(jam_get_real_char());
1097
 
1098
                                                if (value == -1)
1099
                                                {
1100
                                                        status = JAMC_SYNTAX_ERROR;
1101
                                                }
1102
                                        }
1103
 
1104
                                        if (value & (1 << ((int)(bit % 6))))
1105
                                        {
1106
                                                heap_data[address >> 5] |= (1L << (address & 0x1f));
1107
                                        }
1108
                                        else
1109
                                        {
1110
                                                heap_data[address >> 5] &=
1111
                                                        ~(unsigned long) (1L << (address & 0x1f));
1112
                                        }
1113
                                        ++address;
1114
                                }
1115
                                break;
1116
 
1117
                        default:
1118
                                status = JAMC_SYNTAX_ERROR;
1119
                                break;
1120
                        }
1121
                }
1122
                else
1123
                {
1124
                        /* unrecognized key character */
1125
                        status = JAMC_SYNTAX_ERROR;
1126
                }
1127
        }
1128
 
1129
        ch = jam_get_real_char();
1130
 
1131
        if (ch == EOF)
1132
        {
1133
                status = JAMC_UNEXPECTED_END;
1134
        }
1135
 
1136
        if ((status == JAMC_SUCCESS) &&
1137
                ((ch != JAMC_SEMICOLON_CHAR) || (address != dimension)))
1138
        {
1139
                status = JAMC_SYNTAX_ERROR;
1140
        }
1141
 
1142
        return (status);
1143
}
1144
 
1145
/****************************************************************************/
1146
/*                                                                                                                                                      */
1147
 
1148
JAM_RETURN_TYPE jam_read_bool_compressed
1149
(
1150
        JAMS_HEAP_RECORD *heap_record
1151
)
1152
 
1153
/*                                                                                                                                                      */
1154
/*      Description:    Reads Boolean array data directly from input stream.    */
1155
/*                                      Works on data encoded using ACA representation.                 */
1156
/*                                                                                                                                                      */
1157
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1158
/*                                                                                                                                                      */
1159
/****************************************************************************/
1160
{
1161
        int ch = 0;
1162
        int bit = 0;
1163
        int word = 0;
1164
        int value = 0;
1165
        long uncompressed_length = 0L;
1166
        char *in = NULL;
1167
        char *ch_data = NULL;
1168
        long in_size = 0L;
1169
        long out_size = 0L;
1170
        long address = 0L;
1171
        BOOL done = FALSE;
1172
        long *heap_data = &heap_record->data[0];
1173
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1174
 
1175
        if (jam_seek(heap_record->position) != 0)
1176
        {
1177
                status = JAMC_IO_ERROR;
1178
        }
1179
 
1180
        /*
1181
        *       We need two memory buffers:
1182
        *
1183
        *       in   (length of compressed bitstream)
1184
        *       out  (length of uncompressed bitstream)
1185
        *
1186
        *       The "out" buffer is inside the heap record.  The "in" buffer
1187
        *       resides in temporary storage above the last heap record.
1188
        */
1189
 
1190
        out_size = (heap_record->dimension >> 3) +
1191
                ((heap_record->dimension & 7) ? 1 : 0);
1192
        in = jam_get_temp_workspace(out_size + (out_size / 10) + 100);
1193
        if (in == NULL)
1194
        {
1195
                status = JAMC_OUT_OF_MEMORY;
1196
        }
1197
 
1198
        while ((status == JAMC_SUCCESS) && (!done))
1199
        {
1200
                ch = jam_get_real_char();
1201
 
1202
                if (ch == JAMC_SEMICOLON_CHAR)
1203
                {
1204
                        done = TRUE;
1205
                }
1206
                else
1207
                {
1208
                        value = jam_6bit_char(ch);
1209
 
1210
                        if (value == -1)
1211
                        {
1212
                                status = JAMC_SYNTAX_ERROR;
1213
                        }
1214
                        else
1215
                        {
1216
                                for (bit = 0; bit < 6; ++bit)
1217
                                {
1218
                                        if (value & (1 << (bit % 6)))
1219
                                        {
1220
                                                in[address >> 3] |= (1L << (address & 7));
1221
                                        }
1222
                                        else
1223
                                        {
1224
                                                in[address >> 3] &=
1225
                                                        ~(unsigned int) (1 << (address & 7));
1226
                                        }
1227
                                        ++address;
1228
                                }
1229
                        }
1230
                }
1231
        }
1232
 
1233
        if (done && (status == JAMC_SUCCESS))
1234
        {
1235
                /*
1236
                *       Uncompress the data
1237
                */
1238
                in_size = (address >> 3) + ((address & 7) ? 1 : 0);
1239
                uncompressed_length = jam_uncompress(
1240
                        in, in_size, (char *)heap_data, out_size, jam_version);
1241
 
1242
                if (uncompressed_length != out_size)
1243
                {
1244
                        status = JAMC_SYNTAX_ERROR;
1245
                }
1246
                else
1247
                {
1248
                        /* convert data from bytes into 32-bit words */
1249
                        out_size = (heap_record->dimension >> 5) +
1250
                                ((heap_record->dimension & 0x1f) ? 1 : 0);
1251
                        ch_data = (char *)heap_data;
1252
 
1253
                        for (word = 0; word < out_size; ++word)
1254
                        {
1255
                                heap_data[word] =
1256
                                        ((((long) ch_data[(word * 4) + 3]) & 0xff) << 24L) |
1257
                                        ((((long) ch_data[(word * 4) + 2]) & 0xff) << 16L) |
1258
                                        ((((long) ch_data[(word * 4) + 1]) & 0xff) << 8L) |
1259
                                        (((long) ch_data[word * 4]) & 0xff);
1260
                        }
1261
                }
1262
        }
1263
 
1264
        if (in != NULL) jam_free_temp_workspace(in);
1265
 
1266
        return (status);
1267
}
1268
 
1269
/****************************************************************************/
1270
/*                                                                                                                                                      */
1271
 
1272
JAM_RETURN_TYPE jam_read_boolean_array_data
1273
(
1274
        JAMS_HEAP_RECORD *heap_record,
1275
        char *statement_buffer
1276
)
1277
 
1278
/*                                                                                                                                                      */
1279
/*      Description:    Reads Boolean array initialization data.  If it is all  */
1280
/*                                      present in the statement buffer, then it is extracted   */
1281
/*                                      from the buffer.  If the array initialization data did  */
1282
/*                                      not fit into the statement buffer, it is read directly  */
1283
/*                                      from the input stream.  Five formats of Boolean array   */
1284
/*                                      initialization data are supported:  comma-separated             */
1285
/*                                      values (the default), and BIN, HEX, RLC, and ACA.               */
1286
/*                                                                                                                                                      */
1287
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1288
/*                                                                                                                                                      */
1289
/****************************************************************************/
1290
{
1291
        int index = 0;
1292
        int ch = 0;
1293
        int rep = 0;
1294
        int length = 0;
1295
        int data_offset = 0;
1296
        long position = 0L;
1297
        long data_position = 0L;
1298
        BOOL done = FALSE;
1299
        BOOL comment = FALSE;
1300
        BOOL found_equal = FALSE;
1301
        BOOL found_space = FALSE;
1302
        BOOL found_keyword = FALSE;
1303
        BOOL data_complete = FALSE;
1304
        JAME_BOOLEAN_REP representation = JAM_ILLEGAL_REP;
1305
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1306
 
1307
        while ((jam_isspace(statement_buffer[index])) &&
1308
                (index < JAMC_MAX_STATEMENT_LENGTH))
1309
        {
1310
                ++index;        /* skip over white space */
1311
        }
1312
 
1313
        /*
1314
        *       Figure out which data representation scheme is used
1315
        */
1316
        if (jam_version == 2)
1317
        {
1318
                if (statement_buffer[index] == JAMC_POUND_CHAR)
1319
                {
1320
                        representation = JAM_BOOL_BINARY;
1321
                        data_offset = index + 1;
1322
                }
1323
                else if (statement_buffer[index] == JAMC_DOLLAR_CHAR)
1324
                {
1325
                        representation = JAM_BOOL_HEX;
1326
                        data_offset = index + 1;
1327
                }
1328
                else if (statement_buffer[index] == JAMC_AT_CHAR)
1329
                {
1330
                        representation = JAM_BOOL_COMPRESSED;
1331
                        data_offset = index + 1;
1332
                }
1333
        }
1334
        else if (jam_isdigit(statement_buffer[index]))
1335
        {
1336
                /*
1337
                *       First character is digit -- assume comma separated list
1338
                */
1339
                representation = JAM_BOOL_COMMA_SEP;
1340
                data_offset = index;
1341
        }
1342
        else if (jam_isalpha(statement_buffer[index]))
1343
        {
1344
                /*
1345
                *       Get keyword to indicate representation scheme
1346
                */
1347
                for (rep = 0; (rep < JAMC_BOOL_REP_COUNT) &&
1348
                        (representation == JAM_ILLEGAL_REP); ++rep)
1349
                {
1350
                        length = jam_strlen(jam_bool_rep_table[rep].string);
1351
 
1352
                        if ((jam_strnicmp(&statement_buffer[index],
1353
                                jam_bool_rep_table[rep].string, length) == 0) &&
1354
                                jam_isspace(statement_buffer[index + length]))
1355
                        {
1356
                                representation = jam_bool_rep_table[rep].rep;
1357
                        }
1358
                }
1359
 
1360
                data_offset = index + length;
1361
        }
1362
 
1363
        if (representation == JAM_ILLEGAL_REP)
1364
        {
1365
                status = JAMC_SYNTAX_ERROR;
1366
        }
1367
        else
1368
        {
1369
                heap_record->rep = representation;
1370
        }
1371
 
1372
        if ((status == JAMC_SUCCESS) && (jam_version == 2))
1373
        {
1374
                if ((representation != JAM_BOOL_BINARY) &&
1375
                        (representation != JAM_BOOL_HEX) &&
1376
                        (representation != JAM_BOOL_COMPRESSED))
1377
                {
1378
                        /* only these three formats are supported in Jam 2.0 */
1379
                        status = JAMC_SYNTAX_ERROR;
1380
                }
1381
        }
1382
 
1383
        /*
1384
        *       See if all the initialization data is present in the statement buffer
1385
        */
1386
        if ((status == JAMC_SUCCESS) && !heap_record->cached)
1387
        {
1388
                while ((statement_buffer[index] != JAMC_NULL_CHAR) &&
1389
                        (statement_buffer[index] != JAMC_SEMICOLON_CHAR) &&
1390
                        (index < JAMC_MAX_STATEMENT_LENGTH))
1391
                {
1392
                        ++index;        /* look for semicolon */
1393
                }
1394
 
1395
                if (statement_buffer[index] == JAMC_SEMICOLON_CHAR)
1396
                {
1397
                        data_complete = TRUE;
1398
                }
1399
        }
1400
 
1401
        /*
1402
        *       If data is not all present in the statement buffer, or if data
1403
        *       will be cached, find the position of the data in the input file
1404
        */
1405
        if ((status == JAMC_SUCCESS) && ((!data_complete) || heap_record->cached))
1406
        {
1407
                /*
1408
                *       Get position offset of initialization data
1409
                */
1410
                if (jam_seek(jam_current_statement_position) == 0)
1411
                {
1412
                        position = jam_current_statement_position;
1413
                }
1414
                else status = JAMC_IO_ERROR;
1415
 
1416
                while ((status == JAMC_SUCCESS) && !done)
1417
                {
1418
                        ch = jam_getc();
1419
 
1420
                        if ((!comment) && (ch == JAMC_COMMENT_CHAR))
1421
                        {
1422
                                /* beginning of comment */
1423
                                comment = TRUE;
1424
                        }
1425
 
1426
                        if ((!comment) && (!found_equal) && (ch == JAMC_EQUAL_CHAR))
1427
                        {
1428
                                /* found the equal sign */
1429
                                found_equal = TRUE;
1430
                        }
1431
 
1432
                        if ((!comment) && found_equal && (!found_space) &&
1433
                                jam_isspace((char)ch))
1434
                        {
1435
                                /* found the space after the equal sign */
1436
                                found_space = TRUE;
1437
                        }
1438
 
1439
                        if ((!comment) && found_equal && found_space)
1440
                        {
1441
                                if (representation == JAM_BOOL_COMMA_SEP)
1442
                                {
1443
                                        if (jam_isdigit((char)ch))
1444
                                        {
1445
                                                /* found the first character of the data area */
1446
                                                done = TRUE;
1447
                                                data_position = position;
1448
                                        }
1449
                                }
1450
                                else    /* other representations */
1451
                                {
1452
                                        if ((jam_version == 2) && (!found_keyword) &&
1453
                                                ((ch == JAMC_POUND_CHAR) ||
1454
                                                (ch == JAMC_DOLLAR_CHAR) ||
1455
                                                (ch == JAMC_AT_CHAR)))
1456
                                        {
1457
                                                found_keyword = TRUE;
1458
                                                done = TRUE;
1459
                                                data_position = position + 1;
1460
                                        }
1461
 
1462
                                        if ((jam_version != 2) && (!found_keyword) &&
1463
                                                (jam_isalpha((char)ch)))
1464
                                        {
1465
                                                /* found the first char of the representation keyword */
1466
                                                found_keyword = TRUE;
1467
                                        }
1468
 
1469
                                        if ((jam_version != 2) && found_keyword &&
1470
                                                (jam_isspace((char)ch)))
1471
                                        {
1472
                                                /* found the first character of the data area */
1473
                                                done = TRUE;
1474
                                                data_position = position;
1475
                                        }
1476
                                }
1477
 
1478
                        }
1479
 
1480
                        if ((!comment) && (ch == JAMC_SEMICOLON_CHAR))
1481
                        {
1482
                                /* end of statement */
1483
                                done = TRUE;
1484
                        }
1485
 
1486
                        if (ch == EOF)
1487
                        {
1488
                                /* end of file */
1489
                                done = TRUE;
1490
                                status = JAMC_UNEXPECTED_END;
1491
                        }
1492
 
1493
                        if (comment &&
1494
                                ((ch == JAMC_NEWLINE_CHAR) || (ch == JAMC_RETURN_CHAR)))
1495
                        {
1496
                                /* end of comment */
1497
                                comment = FALSE;
1498
                        }
1499
 
1500
                        ++position;     /* position of next character to be read */
1501
                }
1502
 
1503
                if (status == JAMC_SUCCESS)
1504
                {
1505
                        heap_record->position = data_position;
1506
                }
1507
 
1508
                /*
1509
                *       If data will not be cached, read it in from the file now.
1510
                */
1511
                if ((status == JAMC_SUCCESS) && !heap_record->cached)
1512
                {
1513
                        /*
1514
                        *       Data is present, and will not be cached.  Read it in.
1515
                        */
1516
                        switch (representation)
1517
                        {
1518
                        case JAM_BOOL_COMMA_SEP:
1519
                                status = jam_read_bool_comma_sep(heap_record);
1520
                                break;
1521
 
1522
                        case JAM_BOOL_BINARY:
1523
                                status = jam_read_bool_binary(heap_record);
1524
                                break;
1525
 
1526
                        case JAM_BOOL_HEX:
1527
                                status = jam_read_bool_hex(heap_record);
1528
                                break;
1529
 
1530
                        case JAM_BOOL_RUN_LENGTH:
1531
                                status = jam_read_bool_run_length(heap_record);
1532
                                break;
1533
 
1534
                        case JAM_BOOL_COMPRESSED:
1535
                                status = jam_read_bool_compressed(heap_record);
1536
                                break;
1537
 
1538
                        default:
1539
                                status = JAMC_INTERNAL_ERROR;
1540
                        }
1541
                }
1542
 
1543
                /*
1544
                *       Restore file pointer to position of next statement
1545
                */
1546
                if (status == JAMC_SUCCESS)
1547
                {
1548
                        if (jam_seek(jam_next_statement_position) == 0)
1549
                        {
1550
                                jam_current_file_position = jam_next_statement_position;
1551
                        }
1552
                        else status = JAMC_IO_ERROR;
1553
                }
1554
        }
1555
 
1556
        if ((status == JAMC_SUCCESS) && data_complete && !heap_record->cached)
1557
        {
1558
                /*
1559
                *       Data is present, and will not be cached.  Extract it from buffer.
1560
                */
1561
                switch (representation)
1562
                {
1563
                case JAM_BOOL_COMMA_SEP:
1564
                        status = jam_extract_bool_comma_sep(
1565
                                heap_record, &statement_buffer[data_offset]);
1566
                        break;
1567
 
1568
                case JAM_BOOL_BINARY:
1569
                        status = jam_extract_bool_binary(
1570
                                heap_record, &statement_buffer[data_offset]);
1571
                        break;
1572
 
1573
                case JAM_BOOL_HEX:
1574
                        status = jam_extract_bool_hex(
1575
                                heap_record, &statement_buffer[data_offset]);
1576
                        break;
1577
 
1578
                case JAM_BOOL_RUN_LENGTH:
1579
                        status = jam_extract_bool_run_length(
1580
                                heap_record, &statement_buffer[data_offset]);
1581
                        break;
1582
 
1583
                case JAM_BOOL_COMPRESSED:
1584
                        status = jam_extract_bool_compressed(
1585
                                heap_record, &statement_buffer[data_offset]);
1586
                        break;
1587
 
1588
                default:
1589
                        status = JAMC_INTERNAL_ERROR;
1590
                }
1591
        }
1592
 
1593
        /* in Jam 2.0, Boolean arrays in BIN and HEX format are reversed */
1594
        if ((status == JAMC_SUCCESS) && (jam_version == 2) &&
1595
                (representation == JAM_BOOL_BINARY))
1596
        {
1597
                status = jam_reverse_boolean_array_bin(heap_record);
1598
        }
1599
 
1600
        if ((status == JAMC_SUCCESS) && (jam_version == 2) &&
1601
                (representation == JAM_BOOL_HEX))
1602
        {
1603
                status = jam_reverse_boolean_array_hex(heap_record);
1604
        }
1605
 
1606
        return (status);
1607
}
1608
 
1609
/****************************************************************************/
1610
/*                                                                                                                                                      */
1611
 
1612
JAM_RETURN_TYPE jam_extract_int_comma_sep
1613
(
1614
        JAMS_HEAP_RECORD *heap_record,
1615
        char *statement_buffer
1616
)
1617
 
1618
/*                                                                                                                                                      */
1619
/*      Description:    Extracts integer array data from statement buffer.              */
1620
/*                                      Works on data in comma separated representation.                */
1621
/*                                                                                                                                                      */
1622
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1623
/*                                                                                                                                                      */
1624
/****************************************************************************/
1625
{
1626
        int index = 0;
1627
        int expr_begin = 0;
1628
        int expr_end = 0;
1629
        char save_ch = 0;
1630
        long address = 0L;
1631
        long value = 0L;
1632
        long dimension = heap_record->dimension;
1633
        JAME_EXPRESSION_TYPE expr_type = JAM_ILLEGAL_EXPR_TYPE;
1634
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1635
        long *heap_data = &heap_record->data[0];
1636
 
1637
        for (address = 0L; (status == JAMC_SUCCESS) && (address < dimension);
1638
                ++address)
1639
        {
1640
                status = JAMC_SYNTAX_ERROR;
1641
 
1642
                while ((jam_isspace(statement_buffer[index])) &&
1643
                        (index < JAMC_MAX_STATEMENT_LENGTH))
1644
                {
1645
                        ++index;        /* skip over white space */
1646
                }
1647
 
1648
                expr_begin = index;
1649
                expr_end = 0;
1650
 
1651
                while ((statement_buffer[index] != JAMC_COMMA_CHAR) &&
1652
                        (statement_buffer[index] != JAMC_SEMICOLON_CHAR) &&
1653
                        (index < JAMC_MAX_STATEMENT_LENGTH))
1654
                {
1655
                        ++index;        /* skip over the expression */
1656
                }
1657
 
1658
                if ((statement_buffer[index] == JAMC_COMMA_CHAR) ||
1659
                        (statement_buffer[index] == JAMC_SEMICOLON_CHAR))
1660
                {
1661
                        expr_end = index;
1662
                }
1663
 
1664
                if (expr_end > expr_begin)
1665
                {
1666
                        save_ch = statement_buffer[expr_end];
1667
                        statement_buffer[expr_end] = JAMC_NULL_CHAR;
1668
                        status = jam_evaluate_expression(
1669
                                &statement_buffer[expr_begin], &value, &expr_type);
1670
                        statement_buffer[expr_end] = save_ch;
1671
                }
1672
 
1673
                if ((status == JAMC_SUCCESS) &&
1674
                        ((expr_type != JAM_INTEGER_EXPR) &&
1675
                        (expr_type != JAM_INT_OR_BOOL_EXPR)))
1676
                {
1677
                        status = JAMC_TYPE_MISMATCH;
1678
                }
1679
 
1680
                if (status == JAMC_SUCCESS)
1681
                {
1682
                        heap_data[address] = value;
1683
 
1684
                        if ((address < dimension) &&
1685
                                (statement_buffer[index] == JAMC_COMMA_CHAR))
1686
                        {
1687
                                ++index;
1688
                        }
1689
                }
1690
        }
1691
 
1692
        if (status == JAMC_SUCCESS)
1693
        {
1694
                while ((jam_isspace(statement_buffer[index])) &&
1695
                        (index < JAMC_MAX_STATEMENT_LENGTH))
1696
                {
1697
                        ++index;        /* skip over white space */
1698
                }
1699
 
1700
                if (statement_buffer[index] != JAMC_SEMICOLON_CHAR)
1701
                {
1702
                        status = JAMC_SYNTAX_ERROR;
1703
                }
1704
        }
1705
 
1706
        return (status);
1707
}
1708
 
1709
/****************************************************************************/
1710
/*                                                                                                                                                      */
1711
 
1712
JAM_RETURN_TYPE jam_read_int_comma_sep
1713
(
1714
        JAMS_HEAP_RECORD *heap_record
1715
)
1716
 
1717
/*                                                                                                                                                      */
1718
/*      Description:    Reads integer array data directly from input stream.    */
1719
/*                                      Works on data in comma separated representation.                */
1720
/*                                                                                                                                                      */
1721
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1722
/*                                                                                                                                                      */
1723
/****************************************************************************/
1724
{
1725
        int index = 0;
1726
        int ch = 0;
1727
        long address = 0L;
1728
        long value = 0L;
1729
        long dimension = heap_record->dimension;
1730
        char expr_buffer[JAMC_MAX_STATEMENT_LENGTH + 1];
1731
        JAME_EXPRESSION_TYPE expr_type = JAM_ILLEGAL_EXPR_TYPE;
1732
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1733
        long *heap_data = &heap_record->data[0];
1734
 
1735
        if (jam_seek(heap_record->position) != 0)
1736
        {
1737
                status = JAMC_IO_ERROR;
1738
        }
1739
 
1740
        while ((status == JAMC_SUCCESS) && (address < dimension))
1741
        {
1742
                ch = jam_get_real_char();
1743
 
1744
                if (((ch == JAMC_COMMA_CHAR) && (address < (dimension - 1))) ||
1745
                        ((ch == JAMC_SEMICOLON_CHAR) && (address == (dimension - 1))))
1746
                {
1747
                        expr_buffer[index] = JAMC_NULL_CHAR;
1748
                        index = 0;
1749
 
1750
                        status = jam_evaluate_expression(
1751
                                expr_buffer, &value, &expr_type);
1752
 
1753
                        if ((status == JAMC_SUCCESS) &&
1754
                                ((expr_type != JAM_INTEGER_EXPR) &&
1755
                                (expr_type != JAM_INT_OR_BOOL_EXPR)))
1756
                        {
1757
                                status = JAMC_TYPE_MISMATCH;
1758
                        }
1759
 
1760
                        if (status == JAMC_SUCCESS)
1761
                        {
1762
                                heap_data[address] = value;
1763
                                ++address;
1764
                        }
1765
                }
1766
                else if ((ch == JAMC_COMMA_CHAR) && (address >= (dimension - 1)))
1767
                {
1768
                        status = JAMC_BOUNDS_ERROR;
1769
                }
1770
                else
1771
                {
1772
                        expr_buffer[index] = (char) ch;
1773
 
1774
                        if (index < JAMC_MAX_STATEMENT_LENGTH)
1775
                        {
1776
                                ++index;
1777
                        }
1778
                        else
1779
                        {
1780
                                /* expression was too long */
1781
                                status = JAMC_SYNTAX_ERROR;
1782
                        }
1783
                }
1784
 
1785
                if (ch == EOF)
1786
                {
1787
                        /* end of file */
1788
                        status = JAMC_UNEXPECTED_END;
1789
                }
1790
        }
1791
 
1792
        return (status);
1793
}
1794
 
1795
/****************************************************************************/
1796
/*                                                                                                                                                      */
1797
 
1798
JAM_RETURN_TYPE jam_read_integer_array_data
1799
(
1800
        JAMS_HEAP_RECORD *heap_record,
1801
        char *statement_buffer
1802
)
1803
 
1804
/*                                                                                                                                                      */
1805
/*      Description:    Reads integer array initialization data.  If it is all  */
1806
/*                                      present in the statement buffer, then it is extracted   */
1807
/*                                      from the buffer.  If the array initialization data did  */
1808
/*                                      not fit into the statement buffer, it is read directly  */
1809
/*                                      from the input stream.  The only data representation    */
1810
/*                                      supported for integer arrays is a comma-separated list  */
1811
/*                                      of integer expressions.                                                                 */
1812
/*                                                                                                                                                      */
1813
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1814
/*                                                                                                                                                      */
1815
/****************************************************************************/
1816
{
1817
        int index = 0;
1818
        int ch = 0;
1819
        long position = 0L;
1820
        long data_position = 0L;
1821
        BOOL done = FALSE;
1822
        BOOL comment = FALSE;
1823
        BOOL found_equal = FALSE;
1824
        BOOL found_space = FALSE;
1825
        BOOL data_complete = FALSE;
1826
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1827
 
1828
        /*
1829
        *       See if all the initialization data is present in the statement buffer
1830
        */
1831
        if ((status == JAMC_SUCCESS) && !heap_record->cached)
1832
        {
1833
                while ((statement_buffer[index] != JAMC_NULL_CHAR) &&
1834
                        (statement_buffer[index] != JAMC_SEMICOLON_CHAR) &&
1835
                        (index < JAMC_MAX_STATEMENT_LENGTH))
1836
                {
1837
                        ++index;        /* look for semicolon */
1838
                }
1839
 
1840
                if (statement_buffer[index] == JAMC_SEMICOLON_CHAR)
1841
                {
1842
                        data_complete = TRUE;
1843
                }
1844
        }
1845
 
1846
        /*
1847
        *       If data is not all present in the statement buffer, or if data
1848
        *       will be cached, find the position of the data in the input file
1849
        */
1850
        if ((status == JAMC_SUCCESS) && ((!data_complete) || heap_record->cached))
1851
        {
1852
                /*
1853
                *       Get position offset of initialization data
1854
                */
1855
                if (jam_seek(jam_current_statement_position) == 0)
1856
                {
1857
                        position = jam_current_statement_position;
1858
                }
1859
                else status = JAMC_IO_ERROR;
1860
 
1861
                while ((status == JAMC_SUCCESS) && !done)
1862
                {
1863
                        ch = jam_getc();
1864
 
1865
                        if ((!comment) && (ch == JAMC_COMMENT_CHAR))
1866
                        {
1867
                                /* beginning of comment */
1868
                                comment = TRUE;
1869
                        }
1870
 
1871
                        if ((!comment) && (!found_equal) && (ch == JAMC_EQUAL_CHAR))
1872
                        {
1873
                                /* found the equal sign */
1874
                                found_equal = TRUE;
1875
                        }
1876
 
1877
                        if ((!comment) && found_equal && (!found_space) &&
1878
                                jam_isspace((char)ch))
1879
                        {
1880
                                /* found the space after the equal sign */
1881
                                found_space = TRUE;
1882
                        }
1883
 
1884
                        if ((!comment) && found_equal && found_space &&
1885
                                jam_isdigit((char)ch))
1886
                        {
1887
                                /* found the first character of the data area */
1888
                                done = TRUE;
1889
                                data_position = position;
1890
                        }
1891
 
1892
                        if ((!comment) && (ch == JAMC_SEMICOLON_CHAR))
1893
                        {
1894
                                /* end of statement */
1895
                                done = TRUE;
1896
                        }
1897
 
1898
                        if (ch == EOF)
1899
                        {
1900
                                /* end of file */
1901
                                done = TRUE;
1902
                                status = JAMC_UNEXPECTED_END;
1903
                        }
1904
 
1905
                        if (comment &&
1906
                                ((ch == JAMC_NEWLINE_CHAR) || (ch == JAMC_RETURN_CHAR)))
1907
                        {
1908
                                /* end of comment */
1909
                                comment = FALSE;
1910
                        }
1911
 
1912
                        ++position;     /* position of next character to be read */
1913
                }
1914
 
1915
                if (status == JAMC_SUCCESS)
1916
                {
1917
                        heap_record->position = data_position;
1918
                }
1919
 
1920
                /*
1921
                *       If data will not be cached, read it in from the file now.
1922
                */
1923
                if ((status == JAMC_SUCCESS) && !heap_record->cached)
1924
                {
1925
                        /*
1926
                        *       Data is present, and will not be cached.  Read it in.
1927
                        */
1928
                        status = jam_read_int_comma_sep(heap_record);
1929
                }
1930
 
1931
                /*
1932
                *       Restore file pointer to position of next statement
1933
                */
1934
                if (status == JAMC_SUCCESS)
1935
                {
1936
                        if (jam_seek(jam_next_statement_position) == 0)
1937
                        {
1938
                                jam_current_file_position = jam_next_statement_position;
1939
                        }
1940
                        else status = JAMC_IO_ERROR;
1941
                }
1942
        }
1943
 
1944
        if ((status == JAMC_SUCCESS) && data_complete && !heap_record->cached)
1945
        {
1946
                /*
1947
                *       Data is present, and will not be cached.  Extract it from buffer.
1948
                */
1949
                status = jam_extract_int_comma_sep(heap_record, statement_buffer);
1950
        }
1951
 
1952
        /*
1953
        *       For Jam 2.0, reverse the order of the data values
1954
        */
1955
        if ((status == JAMC_SUCCESS) && (jam_version == 2))
1956
        {
1957
                long *heap_data = &heap_record->data[0];
1958
                long dimension = heap_record->dimension;
1959
                long a, b, i, j;
1960
 
1961
                for (i = 0; i < dimension / 2; ++i)
1962
                {
1963
                        j = (dimension - 1) - i;
1964
                        a = heap_data[i];
1965
                        b = heap_data[j];
1966
                        heap_data[j] = a;
1967
                        heap_data[i] = b;
1968
                }
1969
        }
1970
 
1971
        return (status);
1972
}
1973
 
1974
/****************************************************************************/
1975
/*                                                                                                                                                      */
1976
 
1977
JAM_RETURN_TYPE jam_get_array_value
1978
(
1979
        JAMS_SYMBOL_RECORD *symbol_record,
1980
        long index,
1981
        long *value
1982
)
1983
 
1984
/*                                                                                                                                                      */
1985
/*      Description:    Gets the value of an array element.      The value is           */
1986
/*                                      passed back by reference.                                                               */
1987
/*                                                                                                                                                      */
1988
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1989
/*                                                                                                                                                      */
1990
/****************************************************************************/
1991
{
1992
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1993
        JAMS_HEAP_RECORD *heap_record = NULL;
1994
        long *heap_data = NULL;
1995
 
1996
        if ((symbol_record == NULL) ||
1997
                ((symbol_record->type != JAM_INTEGER_ARRAY_WRITABLE) &&
1998
                (symbol_record->type != JAM_BOOLEAN_ARRAY_WRITABLE) &&
1999
                (symbol_record->type != JAM_INTEGER_ARRAY_INITIALIZED) &&
2000
                (symbol_record->type != JAM_BOOLEAN_ARRAY_INITIALIZED)))
2001
        {
2002
                status = JAMC_INTERNAL_ERROR;
2003
        }
2004
        else
2005
        {
2006
                heap_record = (JAMS_HEAP_RECORD *) symbol_record->value;
2007
 
2008
                if (heap_record == NULL)
2009
                {
2010
                        status = JAMC_INTERNAL_ERROR;
2011
                }
2012
 
2013
                if ((status == JAMC_SUCCESS) &&
2014
                        ((index < 0) || (index >= heap_record->dimension)))
2015
                {
2016
                        status = JAMC_BOUNDS_ERROR;
2017
                }
2018
 
2019
                if (status == JAMC_SUCCESS)
2020
                {
2021
                        heap_data = &heap_record->data[0];
2022
 
2023
                        if ((symbol_record->type == JAM_INTEGER_ARRAY_WRITABLE) ||
2024
                                (symbol_record->type == JAM_INTEGER_ARRAY_INITIALIZED))
2025
                        {
2026
                                if (!heap_record->cached)
2027
                                {
2028
                                        if (value != NULL) *value = heap_data[index];
2029
                                }
2030
                                else
2031
                                {
2032
                                        /* get data from cache */
2033
 
2034
                                        /* cache not implemented yet! */
2035
                                        status = JAMC_INTERNAL_ERROR;
2036
                                }
2037
                        }
2038
                        else if ((symbol_record->type == JAM_BOOLEAN_ARRAY_WRITABLE) ||
2039
                                (symbol_record->type == JAM_BOOLEAN_ARRAY_INITIALIZED))
2040
                        {
2041
                                if (!heap_record->cached)
2042
                                {
2043
                                        *value = (heap_data[index >> 5] & (1L << (index & 0x1f)))
2044
                                                ? 1 : 0;
2045
                                }
2046
                                else
2047
                                {
2048
                                        /* get data from cache */
2049
 
2050
                                        /* cache not implemented yet! */
2051
                                        status = JAMC_INTERNAL_ERROR;
2052
                                }
2053
                        }
2054
                        else
2055
                        {
2056
                                status = JAMC_INTERNAL_ERROR;
2057
                        }
2058
                }
2059
        }
2060
 
2061
        return (status);
2062
}

powered by: WebSVN 2.1.0

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