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

Subversion Repositories System09

[/] [System09/] [trunk/] [Tools/] [epedit/] [old/] [epedit.c] - Blame information for rev 94

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 dilbert57
/*
2
 * epedit
3
 *
4
 * binary file editer program
5
 */
6
#include <stdio.h>
7
#include <string.h>
8
#include <ctype.h>
9
/*
10
 * equates
11
 */
12
#define EPROM_MAX (1<<16)
13
#define CMD_LINE_MAX 80
14
#define FALSE 0
15
#define TRUE !FALSE
16
#define BINARY 0
17
#define MOTOROLA 1
18
#define INTEL 2
19
#define SMAL32 3
20
#define VHDL_BIN 4
21
#define VHDL_BYTE 5
22
#define VHDL_WORD 6
23
 
24
/*
25
 * global variables
26
 */
27
FILE *cmdfp;                            /* command input pointer */
28
char cmdbuff[CMD_LINE_MAX];
29
unsigned char eprom_buff[EPROM_MAX];    /* eprom buffer */
30
int eprom_top;                          /* top of EPROM buffer */
31
int mod_flag;                           /* buffer has been modified */
32
int auxflag;                            /* Auxillary input file specified */
33
int count;
34
int checksum;
35
int offset;                             /* Eprom Buffer memory offset */
36
int format_type;                        /* load / save format type */
37
char *hex_str = "0123456789ABCDEF";
38
 
39
/*
40
 * Read a command line into the command buffer
41
 */
42
void read_command( char *cb )
43
{
44
        int ch;                 /* character temp */
45
        int ci;                 /* command index */
46
 
47
        ci = 0;
48
        printf( "\n>>> " );
49
 
50
        fflush( stdout );
51
        while( (ch = getc( cmdfp )) != EOF )
52
        {
53
                if( (ch == '\n') || (ch == 0x0d) )
54
                {
55
                        cb[ci] = '\0';
56
                        return;
57
                } else if( ch == 0x8 )
58
                {
59
                        if( ci > 0 )
60
                                ci--;
61
                } else
62
                {
63
                        cb[ci] = ch;
64
                        if( ci < CMD_LINE_MAX )
65
                                ci++;
66
                }
67
                fflush( stdout );
68
        }
69
        if(( ch == EOF) && auxflag )
70
        {
71
                /* revert back to stdin if using an auxillay file
72
                 * We can assume that the auxillary command file
73
                 * has not been terminated with "quit" command
74
                 */
75
                fclose( cmdfp );
76
                cmdfp = stdin;
77
                auxflag = FALSE;
78
        }
79
}
80
 
81
/*
82
 * compare a string of specified length
83
 * return TRUE if a match
84
 * return FALSE if no match
85
 * ignore case
86
 */
87
int str_equal( char *s1, char *s2, int len )
88
{
89
        int i;
90
 
91
        i = 0;
92
        while( i<len )
93
        {
94
                if( toupper( s1[i] ) == toupper( s2[i] ) )
95
                        i++;
96
                else return FALSE;
97
        }
98
        return TRUE;
99
}
100
 
101
/*
102
 * extract a file name from the command line
103
 */
104
int get_filename( char *cb, char *fn )
105
{
106
        int i;
107
 
108
        i = 0;
109
        while( !isspace( cb[i] ) && (cb[i] !='\n') && (cb[i] != '\0'))
110
        {
111
                fn[i] = cb[i];
112
                i++;
113
        }
114
        fn[i] = '\0';
115
        if( i == 0 )
116
                return i;
117
        while( isspace( cb[i] ))
118
                i++;
119
        return i;
120
}
121
 
122
int to_hexadecimal( char c )
123
{
124
        int k;
125
 
126
        for( k=0; k<16; k++ )
127
        {
128
                if( toupper(c) == hex_str[k] )
129
                        return k;
130
        }
131
        return -1;
132
}
133
 
134
/*
135
 * extract an address from the command line
136
 * returns an offset to the end of the argument.
137
 */
138
int get_address( char *cb, int *addr )
139
{
140
        int i, j, k;
141
 
142
        j = 0;
143
        i = 0;
144
 
145
        while((k = to_hexadecimal(cb[i])) != -1)
146
        {
147
                i++;
148
                j = j *16 + k;
149
        }
150
        *addr = j;
151
        if( i == 0 )
152
                return i;
153
        while( isspace( cb[i]) )
154
                i++;
155
        return i;
156
}
157
 
158
 
159
/*
160
 * Motorola S1 format to Intel hex format
161
 * Usage
162
 * mot2hex <file_name>
163
 */
164
 
165
int gethex( FILE *fp_in )
166
{
167
    int hex;
168
 
169
    hex = fgetc( fp_in );
170
    return( to_hexadecimal( hex ) );
171
}
172
 
173
int get2hex( FILE *fp_in )
174
{
175
    int hexhi, hexlo, byte;
176
 
177
    hexhi = gethex( fp_in );
178
    if( hexhi != -1 )
179
    {
180
        hexlo = gethex( fp_in );
181
        if( hexlo != -1 )
182
        {
183
            byte = hexhi * 16 + hexlo;
184
            checksum = (checksum + byte) & 0xff;
185
            return byte;
186
        }
187
    }
188
    return -1;
189
}
190
 
191
int get4hex( FILE *fp_in )
192
{
193
    int bytehi, bytelo, addr;
194
 
195
    bytehi = get2hex( fp_in );
196
    if( bytehi != -1 )
197
    {
198
        bytelo = get2hex( fp_in );
199
        if( bytelo != -1 )
200
        {
201
            addr = (bytehi * 256) + bytelo;
202
            return addr;
203
        }
204
    }
205
    return -1;
206
}
207
 
208
int get6hex( FILE *fp_in )
209
{
210
    int bytehi, bytemid, bytelow, addr;
211
 
212
    bytehi = get2hex( fp_in );
213
    if( bytehi != -1 )
214
    {
215
        bytemid = get2hex( fp_in );
216
        if( bytemid != -1 )
217
        {
218
            bytelow = get2hex( fp_in );
219
            if( bytelow != -1 )
220
            {
221
                addr = (bytehi << 16) + (bytemid << 8) + bytelow;
222
                return addr;
223
            }
224
        }
225
    }
226
    return -1;
227
}
228
 
229
long get8hex( FILE *fp_in )
230
{
231
    int wordhi, wordlow;
232
    long addr;
233
 
234
    wordhi = get4hex( fp_in );
235
    if( wordhi != -1 )
236
    {
237
            wordlow = get4hex( fp_in );
238
            if( wordlow != -1 )
239
            {
240
                addr = ((long)wordhi << 16) + (long)wordlow;
241
                return addr;
242
            }
243
    }
244
    return -1;
245
}
246
 
247
 
248
/*
249
 * Load Raw binary file
250
 */
251
 
252
void load_binary( char *fname_in, int load_addr )
253
{
254
        FILE *fp_in;
255
        int byte_count, addr;
256
        int data_byte;
257
 
258
        if( (fp_in = fopen( fname_in, "rb" )) == NULL )
259
        {
260
                printf( "\ncan't open %s for read", fname_in );
261
                return;
262
        } else
263
        {
264
                byte_count = 0;
265
                addr = load_addr - offset;
266
                printf( "\nReading file '%s' offset by  %x", fname_in, offset );
267
                while( (data_byte = fgetc( fp_in )) != EOF )
268
                {
269
                        eprom_buff[(addr + byte_count) % EPROM_MAX ] = (unsigned char) data_byte;
270
                        byte_count++;
271
                }
272
 
273
                fclose( fp_in );
274
                printf( "\nbuffer loaded from %x to %x", addr, addr+byte_count-1 );
275
                if( (addr + byte_count) > eprom_top )
276
                        eprom_top = addr + byte_count;
277
        }
278
}
279
 
280
 
281
/*
282
 * load motorola formatted file
283
 */
284
 
285
void load_mot( char *fname_in )
286
{
287
        FILE *fp_in;
288
        int byte, addr, i;
289
 
290
        fp_in = fopen( fname_in, "r" );
291
        if( !fp_in )
292
        {
293
            printf( "\nCan't open %s", fname_in );
294
            return;
295
        }
296
 
297
        byte = 0;
298
        addr = 0;
299
 
300
        while( byte != -1 )
301
        {
302
            do {
303
                byte = fgetc( fp_in);
304
            } while( (byte != 'S') && (byte != -1) );
305
 
306
            byte = fgetc( fp_in );
307
            checksum = 0;
308
            if( (byte == '1') || (byte == '2') )
309
            {
310
                count = get2hex( fp_in );
311
                if( byte == '1' )
312
                {
313
                    addr = get4hex( fp_in );
314
                    count -= 3;
315
                }
316
                else
317
                {
318
                    addr = get6hex( fp_in );
319
                    count -= 4;
320
                }
321
                for( i=0; i<count; i++ )
322
                {
323
                    byte = get2hex( fp_in );
324
                    eprom_buff[( addr - offset) % EPROM_MAX ] = (unsigned char)byte;
325
                    addr++;
326
                }
327
                byte = get2hex( fp_in);
328
                checksum = (~checksum) & 0xff;
329
                if( checksum != 0 )
330
                   printf( "\nchecksum error - read check = %02x", byte );
331
             }
332
        }
333
        close( fp_in );
334
}
335
 
336
 
337
/*
338
 * load smal32 .o formatted file
339
 */
340
 
341
void load_smal32( char *fname_in )
342
{
343
        FILE *fp_in;
344
        int byte;
345
        long addr;
346
        int state;
347
 
348
        fp_in = fopen( fname_in, "r" );
349
        if( !fp_in )
350
        {
351
            printf( "\nCan't open %s", fname_in );
352
            return;
353
        }
354
 
355
        byte = 0;
356
        addr = 0;
357
        state = 0;
358
 
359
        while( byte != -1 )
360
        {
361
                byte = fgetc( fp_in);
362
                switch( state )
363
                {
364
                case 0:
365
                  switch( byte )
366
                  {
367
                   case '.':
368
                     state = 1;
369
                     break;
370
                   case 'B':
371
                     state = 4;
372
                     break;
373
                   case 'H':
374
                     state = 5;
375
                     break;
376
                   case 'W':
377
                     state = 6;
378
                     break;
379
                   default:
380
                     state = 0;
381
                  }
382
                  break;
383
                case 1:
384
                  if( byte == '=' )
385
                     state = 2;
386
                  else
387
                     state = 0;
388
                  break;
389
                case 2:
390
                  if( byte == '#' )
391
                  {
392
                     addr = get8hex( fp_in );
393
                     printf( "\nload address = %08x", addr );
394
                  }
395
                  state = 0;
396
                  break;
397
                case 3:
398
                  break;
399
                case 4:
400
                  if( byte == '#' )
401
                  {
402
                    byte = get2hex( fp_in );
403
                    eprom_buff[ (addr - offset) % EPROM_MAX ] = (unsigned char)byte;
404
                    addr++;
405
                  }
406
                  state = 0;
407
                  break;
408
                case 5:
409
                  if( byte == '#' )
410
                  {
411
                    byte = get2hex( fp_in );
412
                    eprom_buff[ (addr - offset) % EPROM_MAX ] = (unsigned char)byte;
413
                    addr++;
414
                    byte = get2hex( fp_in );
415
                    eprom_buff[ (addr - offset) % EPROM_MAX ] = (unsigned char)byte;
416
                    addr++;
417
                  }
418
                  state = 0;
419
                  break;
420
                case 6:
421
                  if( byte == '#' )
422
                  {
423
                    byte = get2hex( fp_in );
424
                    eprom_buff[ (addr - offset) % EPROM_MAX ] = (unsigned char)byte;
425
                    addr++;
426
                    byte = get2hex( fp_in );
427
                    eprom_buff[ (addr - offset) % EPROM_MAX ] = (unsigned char)byte;
428
                    addr++;
429
                    byte = get2hex( fp_in );
430
                    eprom_buff[ (addr - offset) % EPROM_MAX ] = (unsigned char)byte;
431
                    addr++;
432
                    byte = get2hex( fp_in );
433
                    eprom_buff[ (addr - offset) % EPROM_MAX ] = (unsigned char)byte;
434
                    addr++;
435
                  }
436
                  state = 0;
437
                  break;
438
               default :
439
                 state = 0;
440
                 break;
441
 
442
               }
443
 
444
            if( addr >= EPROM_MAX  )
445
               byte = -1;
446
        }
447
        printf( "\nlast address = %08x", addr );
448
        close( fp_in );
449
}
450
 
451
/*
452
 * Load Intel hex file
453
 */
454
 
455
void load_intel( char *fname_in )
456
{
457
        FILE *fp_in;
458
        int byte, addr, i;
459
 
460
        fp_in = fopen( fname_in, "r" );
461
        if( !fp_in )
462
        {
463
            printf( "\nCan't open %s for input", fname_in );
464
            return;
465
        }
466
 
467
        byte = 0;
468
        while( byte != -1)
469
        {
470
                do {
471
                        byte = fgetc( fp_in);
472
                } while( (byte != ':') && (byte != -1) );
473
 
474
                checksum = 0;
475
                count = get2hex( fp_in );       /* read byte count */
476
                addr = get4hex( fp_in );        /* read address */
477
                i = get2hex( fp_in );           /* read 00 to load a record */
478
                if( i == 0 )                     /* read 01 to end record */
479
                {
480
                        for( i=0; i<count; i++ )
481
                        {
482
                            byte = get2hex( fp_in );
483
                            eprom_buff[ (addr - offset) % EPROM_MAX ] = (unsigned char)byte;
484
                            addr++;
485
                        }
486
                        byte = get2hex( fp_in);
487
                        if( checksum != 0 )
488
                        {
489
                                printf( "\nchecksum read %02x, expected %02x", byte, (checksum - byte)&0xff );
490
                                byte = -1;
491
                        } else if( addr >= EPROM_MAX )
492
                           byte = -1;
493
                } else if( i == 1 )
494
                        byte = -1;
495
        }
496
        close( fp_in );
497
}
498
 
499
 
500
/*
501
 * load VHDL Binary file
502
 */
503
 
504
void load_vhdl_bin( char *fname_in, int start_addr )
505
{
506
        FILE *fp_in;
507
        int addr;
508
        int i;
509
        int byte, data;
510
        int state;
511
 
512
        if( (fp_in = fopen( fname_in, "r" )) == NULL )
513
        {
514
                printf( "\nCan't open '%s' for read ", fname_in );
515
                return;
516
        }
517
        byte = 0;
518
        state = 0;
519
        addr = start_addr;
520
        while( byte != -1)
521
        {
522
                byte = fgetc( fp_in);
523
                switch( state )
524
                {
525
                case 0:
526
                        if( byte == '"' )
527
                        {
528
                                data = 0;
529
                                state = 1;
530
                                i = 0;
531
                        }
532
                        break;
533
                case 1:
534
                        data <<= 1;
535
                        if( byte == '1' )
536
                                data += 1;
537
                        else
538
                        {
539
                                if( byte != '0' )
540
                                {
541
                                        state = 0;
542
                                        break;
543
                                }
544
                        }
545
                        i++;
546
                        if( i == 8 )
547
                                state = 2;
548
                        break;
549
                case 2:
550
                        if( byte == '"' )
551
                        {
552
                                eprom_buff[ addr % EPROM_MAX ] = (unsigned char)data;
553
                                addr++;
554
                        }
555
                        state = 0;
556
                        break;
557
                default:
558
                        break;
559
                }
560
        }
561
        fclose( fp_in );
562
        printf( "\nLoaded " );
563
}
564
 
565
/*
566
 * load VHDL Hex file
567
 */
568
 
569
void load_vhdl_byte( char *fname_in, int start_addr )
570
{
571
        FILE *fp_in;
572
        int addr, i;
573
        int byte, data;
574
        int state;
575
 
576
        if( (fp_in = fopen( fname_in, "r" )) == NULL )
577
        {
578
                printf( "\nCan't open '%s' for read ", fname_in );
579
                return;
580
        }
581
        byte = 0;
582
        state = 0;
583
        addr = start_addr;
584
        while( byte != -1)
585
        {
586
                byte = fgetc( fp_in);
587
                switch( state )
588
                {
589
                case 0:
590
                        if( byte == 'x' )
591
                        {
592
                                state = 1;
593
                        }
594
                        break;
595
                case 1:
596
                        if( byte == '"' )
597
                        {
598
                                data = 0;
599
                                state = 2;
600
                                i = 31;
601
                        }
602
                        break;
603
                case 2:
604
                        data = to_hexadecimal( (char)byte );
605
                        if( data != -1 )
606
                        {
607
                                data <<= 4;
608
                                state = 3;
609
                        } else
610
                                state = 0;
611
                        break;
612
                case 3:
613
                        if( to_hexadecimal( byte ) != -1 )
614
                        {
615
                                data += to_hexadecimal( (char)byte );
616
                                eprom_buff[ (addr + i ) % EPROM_MAX ] = (unsigned char)data;
617
                                if( i == 0 )
618
                                {
619
                                        addr += 32;
620
                                        state = 0;
621
                                } else
622
                                {
623
                                        i--;
624
                                        state = 2;
625
                                }
626
                        } else
627
                                state = 0;
628
 
629
                        break;
630
                default:
631
                        break;
632
                }
633
        }
634
        fclose( fp_in );
635
        printf( "\nLoaded " );
636
}
637
 
638
 
639
/*
640
 * load VHDL Word file
641
 */
642
 
643
void load_vhdl_word( char *fname_in, int start_addr )
644
{
645
        FILE *fp_in;
646
        int addr;
647
        int i;
648
        int byte, data;
649
        int state;
650
 
651
        if( (fp_in = fopen( fname_in, "r" )) == NULL )
652
        {
653
                printf( "\nCan't open '%s' for read ", fname_in );
654
                return;
655
        }
656
        byte = 0;
657
        state = 0;
658
        addr = start_addr;
659
        while( byte != -1)
660
        {
661
                byte = fgetc( fp_in);
662
                switch( state )
663
                {
664
                case 0:
665
                        if( byte == 'x' )
666
                        {
667
                                state = 1;
668
                        }
669
                        break;
670
                case 1:
671
                        if( byte == '"' )
672
                        {
673
                                data = 0;
674
                                state = 2;
675
                                i = 30;
676
                        }
677
                        break;
678
                case 2:
679
                        data = to_hexadecimal( (char)byte );
680
                        if( data != -1 )
681
                        {
682
                                data <<= 4;
683
                                state = 3;
684
                        } else
685
                                state = 0;
686
                        break;
687
                case 3:
688
                        if( to_hexadecimal( (char)byte ) != -1 )
689
                        {
690
                                data += to_hexadecimal( (char)byte );
691
                                eprom_buff[ (addr + i) % EPROM_MAX ] = (unsigned char)data;
692
                                state = 4;
693
                        } else
694
                                state = 0;
695
 
696
                        break;
697
                case 4:
698
                        data = to_hexadecimal( (char)byte );
699
                        if( data != -1 )
700
                        {
701
                                data <<= 4;
702
                                state = 5;
703
                        } else
704
                                state = 0;
705
                        break;
706
                case 5:
707
                        if( to_hexadecimal( (char)byte ) != -1 )
708
                        {
709
                                data += to_hexadecimal( (char)byte );
710
                                eprom_buff[ (addr + i + 1) % EPROM_MAX ] = (unsigned char)data;
711
                                if( i == 0 )
712
                                {
713
                                        addr += 32;
714
                                        state = 0;
715
                                } else
716
                                {
717
                                        i -= 2;
718
                                        state = 2;
719
                                }
720
                        } else
721
                                state = 0;
722
 
723
                        break;
724
                default:
725
                        break;
726
                }
727
        }
728
        fclose( fp_in );
729
        printf( "\nLoaded " );
730
}
731
 
732
 
733
 
734
int put2hex( FILE *fp, int h )
735
{
736
        int i, hex;
737
 
738
        hex = (h & 0xf0)>>4;
739
        i = fputc( (int)hex_str[hex], fp );
740
        hex = (h & 0xf);
741
        i = fputc( (int)hex_str[hex], fp );
742
        checksum = (checksum + h) & 0xff;
743
        return i;
744
}
745
 
746
int put4hex( FILE * fp, int h )
747
{
748
        int i;
749
 
750
        i = put2hex( fp, (h & 0xff00 )>>8 );
751
        i = put2hex( fp, (h & 0xff) );
752
        return i;
753
}
754
 
755
 
756
 
757
/*
758
 * save VHDL Binary file
759
 */
760
 
761
void save_vhdl_bin( char *fname_out, int start_addr, int end_addr )
762
{
763
        FILE *fp_out;
764
        int addr;
765
        int i;
766
        int byte;
767
 
768
        if( (fp_out = fopen( fname_out, "w" )) == NULL )
769
        {
770
                printf( "\nCan't open '%s' for write ", fname_out );
771
                return;
772
        }
773
        for( addr=start_addr; addr<=end_addr; addr++ )
774
        {
775
                putc( '"', fp_out );
776
                for(i=7; i>=0; i-- )
777
                {
778
                        byte = (int)eprom_buff[(addr - offset) % EPROM_MAX];
779
                        if(( byte & (1<<i)) == 0 )
780
                                putc( '0', fp_out );
781
                        else
782
                                putc( '1', fp_out );
783
                }
784
                putc( '"', fp_out );
785
                putc( ',', fp_out );
786
                putc( '\n', fp_out );
787
        }
788
        fclose( fp_out );
789
        printf( "\nwritten successfully" );
790
}
791
 
792
 
793
/*
794
 * save VHDL hexadecimal file
795
 */
796
 
797
void save_vhdl_byte( char *fname_out, int start_addr, int end_addr )
798
{
799
        FILE *fp_out;
800
        int addr;
801
        int i,j;
802
        int byte;
803
 
804
        if( (fp_out = fopen( fname_out, "w" )) == NULL )
805
        {
806
                printf( "\nCan't open '%s' for write ", fname_out );
807
                return;
808
        }
809
        j=0;
810
        for( addr=start_addr; addr<=end_addr; addr+=32 )
811
        {
812
                fprintf( fp_out, "    INIT_%02x => x\"", j );
813
                for(i=31; i>=0; i-- )
814
                {
815
                        byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
816
                        putc( hex_str[(byte >>4) & 0xf], fp_out );
817
                        putc( hex_str[byte & 0xf], fp_out );
818
                }
819
                fprintf( fp_out, "\",\n" );
820
                j++;
821
        }
822
        fclose( fp_out );
823
        printf( "\nwritten successfully" );
824
}
825
 
826
 
827
/*
828
 * save VHDL hexadecimal Word file
829
 */
830
 
831
void save_vhdl_word( char *fname_out, int start_addr, int end_addr )
832
{
833
        FILE *fp_out;
834
        int addr;
835
        int i,j;
836
        int byte;
837
 
838
        if( (fp_out = fopen( fname_out, "w" )) == NULL )
839
        {
840
                printf( "\nCan't open '%s' for write ", fname_out );
841
                return;
842
        }
843
        j=0;
844
        for( addr=start_addr; addr<=end_addr; addr+=32 )
845
        {
846
                fprintf( fp_out, "  INIT_%02x => x\"" );
847
                for(i=30; i>=0; i-=2 )
848
                {
849
                        /* MSByte first */
850
                        byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
851
                        putc( hex_str[(byte >>4) & 0xf], fp_out );
852
                        putc( hex_str[byte & 0xf], fp_out );
853
                        /* LSByte second */
854
                        byte = (int)eprom_buff[(addr - offset + i + 1) % EPROM_MAX];
855
                        putc( hex_str[(byte >>4) & 0xf], fp_out );
856
                        putc( hex_str[byte & 0xf], fp_out );
857
                }
858
                fprintf( fp_out, "\",\n" );
859
                j++;
860
        }
861
        fclose( fp_out );
862
        printf( "\nwritten successfully" );
863
}
864
 
865
 
866
/*
867
 * save raw binary file
868
 */
869
 
870
void save_binary( char *fname_out, int start_addr, int end_addr )
871
{
872
        FILE *fp_out;
873
        int addr;
874
 
875
        if( (fp_out = fopen( fname_out, "wb" )) == NULL )
876
        {
877
                printf( "\nCan't open '%s' for write ", fname_out );
878
                return;
879
        }
880
        for( addr=start_addr; addr<=end_addr; addr++ )
881
        {
882
                putc( eprom_buff[(addr - offset) % EPROM_MAX ], fp_out );
883
        }
884
        fclose( fp_out );
885
        printf( "\nwritten successfully" );
886
}
887
 
888
 
889
/*
890
 * Save Motorola S1 file
891
 */
892
 
893
void save_mot( char *fname_out, int start_addr, int end_addr )
894
{
895
        FILE *fp_out;
896
        int addr, start;
897
        int i;
898
        int E_total;
899
        int data_byte;
900
 
901
        fp_out = fopen( fname_out, "w" );
902
        if( !fp_out )
903
        {
904
            printf( "\nCan't open %s for output", fname_out );
905
            return;
906
        }
907
 
908
        addr = start_addr;
909
        while( addr <= end_addr)
910
        {
911
                if( (end_addr - addr + 1) > 0 )
912
                        E_total = 16;
913
                else
914
                        E_total = end_addr - addr + 1;
915
                fputc( 'S', fp_out);    /* record header preamble */
916
                fputc( '1', fp_out);    /* record header preamble */
917
                checksum = 0;
918
                put2hex( fp_out, E_total+3);    /* byte count + 3 */
919
                put4hex( fp_out, addr);         /* Program Counter */
920
                for(i=0; i<E_total; i++)
921
                {
922
                        data_byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
923
                        put2hex( fp_out, data_byte);
924
                }
925
                checksum = ~checksum;   /* one's complement */
926
                put2hex( fp_out, (checksum & 0xff));
927
                fputc( '\n', fp_out);   /* record header preamble */
928
                addr += E_total;
929
        }
930
        checksum = 0;
931
        fputc( 'S', fp_out);            /* record header preamble */
932
        fputc( '9', fp_out);            /* record header preamble */
933
        put2hex( fp_out, 3);            /* byte count + 3 */
934
        put4hex( fp_out, 0);             /* Program Counter */
935
        put2hex( fp_out, 0xfc );        /* byte count + 3 */
936
        fclose( fp_out );
937
}
938
 
939
/*
940
 * save Intelhex file
941
 */
942
 
943
void save_intel( char *fname_out, int start_addr, int end_addr )
944
{
945
        FILE *fp_out;
946
        int byte, addr, start, check, i;
947
 
948
        fp_out = fopen( fname_out, "w" );
949
        if( fp_out == NULL )
950
        {
951
                printf( "\nCan't open %s for output", fname_out );
952
                return;
953
        }
954
 
955
        addr = start_addr;
956
        start = addr;
957
        while( addr <= end_addr )
958
        {
959
                check = 0;
960
                count = 16;
961
                fputc( ':', fp_out );
962
                put2hex( fp_out, count );
963
                check += count;
964
                put4hex( fp_out, addr );
965
                check += (addr & 0xff);
966
                check += ((addr>>8) & 0xff);
967
                put2hex( fp_out, 0 );
968
                check += 0;
969
                for( i=0; i<count; i++ )
970
                {
971
                    byte = (int)eprom_buff[ (addr - offset) % EPROM_MAX ];
972
                    if( addr == 0xfffe )
973
                        start = byte * 256 ;
974
                    if( addr == 0xffff )
975
                        start += byte;
976
                    put2hex( fp_out, byte );
977
                    check += byte;
978
                    addr ++;
979
                }
980
                check = (0-check) & 0xff;
981
                put2hex( fp_out, check);
982
                fputc( '\n', fp_out );
983
        }
984
        fputc( ':', fp_out );
985
        put2hex( fp_out, 0 );
986
        put4hex( fp_out, start );
987
        put2hex( fp_out, 1 );
988
        close( fp_out );
989
}
990
 
991
 
992
/*
993
 * Compare Raw binary file
994
 */
995
 
996
void compare_binary( char *fname_in, int load_addr, int start_addr, int end_addr)
997
{
998
        FILE *fp_in;
999
        int addr;
1000
        int data_byte;
1001
        int diff_count;
1002
 
1003
        if( (fp_in = fopen( fname_in, "r" )) == NULL )
1004
        {
1005
                printf( "\ncant open %s for read", fname_in );
1006
                return;
1007
        } else
1008
        {
1009
                diff_count = 0;
1010
                addr = load_addr;
1011
                while( (data_byte = fgetc( fp_in )) != EOF )
1012
                {
1013
                        if( (addr >= start_addr) && (addr <= end_addr) )
1014
                        {
1015
                                if( (unsigned char)data_byte != eprom_buff[(addr -offset) % EPROM_MAX] )
1016
                                {
1017
                                        printf( "\n%04x %02x %02x", addr, eprom_buff[(addr-offset) % EPROM_MAX ], data_byte );
1018
                                        diff_count++;
1019
                                }
1020
                        }
1021
                        addr++;
1022
                }
1023
                printf( "\nLast compared address %x ", addr-1 );
1024
                printf( "\nThere were %d differences ", diff_count );
1025
                fclose( fp_in );
1026
        }
1027
}
1028
 
1029
 
1030
/*
1031
 * Compare motorola formatted file
1032
 */
1033
 
1034
void compare_mot( char *fname_in, int start_addr, int end_addr )
1035
{
1036
        FILE *fp_in;
1037
        int byte, addr, start, check, i;
1038
        int diff_count;
1039
 
1040
        fp_in = fopen( fname_in, "r" );
1041
        if( !fp_in )
1042
        {
1043
            printf( "\nCan't open %s", fname_in );
1044
            return;
1045
        }
1046
 
1047
        byte = 0;
1048
        addr = start;
1049
        diff_count = 0;
1050
 
1051
        while( byte != -1 )
1052
        {
1053
            do {
1054
                byte = fgetc( fp_in);
1055
            } while( (byte != 'S') && (byte != -1) );
1056
 
1057
            byte = fgetc( fp_in );
1058
            checksum = 0;
1059
            if( (byte == '1') || (byte == '2') )
1060
            {
1061
                count = get2hex( fp_in );
1062
                if( byte == '1' )
1063
                    addr = get4hex( fp_in );
1064
                else
1065
                    addr = get6hex( fp_in );
1066
                count -= 3;
1067
                for( i=0; i<count; i++ )
1068
                {
1069
                        byte = get2hex( fp_in );
1070
                        if( (addr >= start_addr) && (addr <= end_addr ))
1071
                        {
1072
                                if( (unsigned char)byte != eprom_buff[(addr - offset) % EPROM_MAX ] )
1073
                                {
1074
                                        printf( "\n%04x %02x %02x", addr, eprom_buff[(addr-offset) % EPROM_MAX ], byte );
1075
                                        diff_count++;
1076
                                }
1077
                        }
1078
                        addr++;
1079
                }
1080
                byte = get2hex( fp_in);
1081
                if( addr >= EPROM_MAX )
1082
                   byte = -1;
1083
             }
1084
        }
1085
        close( fp_in );
1086
        printf( "\nLast compared address %x ", addr - 1 );
1087
        printf( "\nThere were %d differences ", diff_count );
1088
}
1089
 
1090
/*
1091
 * Load Intel hex file
1092
 */
1093
 
1094
void compare_intel( char *fname_in, int start_addr, int end_addr )
1095
{
1096
        FILE *fp_in;
1097
        int byte, addr, start, i;
1098
        int diff_count;
1099
 
1100
        fp_in = fopen( fname_in, "r" );
1101
        if( !fp_in )
1102
        {
1103
            printf( "\nCan't open %s for input", fname_in );
1104
            return;
1105
        }
1106
 
1107
        byte = 0;
1108
        diff_count = 0;
1109
        while( byte != -1)
1110
        {
1111
                do {
1112
                        byte = fgetc( fp_in);
1113
                } while( (byte != ':') && (byte != -1) );
1114
 
1115
                checksum = 0;
1116
                count = get2hex( fp_in );
1117
                addr = get4hex( fp_in );
1118
                i = get2hex( fp_in );           /* read 00 to load a record */
1119
                if( i == 0 )                     /* read 01 to end record */
1120
                {
1121
                        for( i=0; i<count; i++ )
1122
                        {
1123
                                byte = get2hex( fp_in );
1124
                                if( (addr >= start_addr) && (addr <= end_addr ))
1125
                                {
1126
                                        if( (unsigned char)byte != eprom_buff[(addr - offset) % EPROM_MAX] )
1127
                                        {
1128
                                                printf( "\n%04x %02x %02x", addr, eprom_buff[(addr-offset) % EPROM_MAX], byte );
1129
                                                diff_count++;
1130
                                        }
1131
                                }
1132
                                addr++;
1133
                        }
1134
                        byte = get2hex( fp_in);
1135
                        if( checksum != 0 )
1136
                        {
1137
                                printf( "\nchecksum error detected, read %02x, expected %02x", byte, ((checksum - byte) & 0xff) );
1138
                                byte = -1;
1139
                        } else if( addr >= EPROM_MAX )
1140
                           byte = -1;
1141
                } else if( i == 1 )             /* test for end of record */
1142
                        byte = -1;
1143
        }
1144
        close( fp_in );
1145
        printf( "\nLast compared address %x ", addr - 1 );
1146
        printf( "\nThere were %d differences ", diff_count );
1147
}
1148
 
1149
 
1150
/*
1151
 * help menu
1152
 */
1153
void display_help()
1154
{
1155
        printf( "\n*** Help Menu ***\n\n");
1156
        printf( "All addresses are in hexadecimal\n" );
1157
        printf( "H/?/help                             - This help menu\n" );
1158
        printf( "T/type - Set Load & Save format\n" );
1159
        printf( "     <I/intel>\n");
1160
        printf( "     <M/motorola>\n");
1161
        printf( "     <B/binary>\n" );
1162
        printf( "     <O/Smal32>\n" );
1163
        printf( "     <V/vhdl> VHDL Binary String \n" );
1164
        printf( "     <H/hex> VHDL Hexadecimal Byte String\n" );
1165
        printf( "     <W/word> VHDL Hexadecimal Word String\n" );
1166
        printf( "O/offset <address>                   - Set Eprom buffer offset\n" );
1167
        printf( "L/load <filename> [<load_addr>]      - Loads file into buffer\n" );
1168
        printf( "S/save <filename> <start-address> <end-address> - Saves buffer to file\n" );
1169
        printf( "C/compare <filename> [<load_addr>]   - compare file to buffer\n" );
1170
/*
1171
        printf( "R/read <address>                     - reads buffer data\n" );
1172
*/
1173
        printf( "W/write <address> <data> .... <data> - Write data to buffer\n" );
1174
        printf( "F/fill <start-address> <end-address> <data> - fill buffer with data\n");
1175
        printf( "M/move <start-address> <end-address> <dest-address> - block move\n" );
1176
        printf( "D/dump <start_address> <end-address> - Hex buffer dump\n");
1177
        printf( "E/echo <string>                      - echo string to console\n" );
1178
        printf( "X/excecute <command-file>            - execute a command script\n");
1179
        printf( "Q/quit                               - quit program\n" );
1180
}
1181
 
1182
/*
1183
 * Parse command
1184
 * Return FALSE to exit
1185
 */
1186
int parse_command( char *cmdline )
1187
{
1188
        int lineptr;
1189
        char *cmdptr;
1190
        int arglen;
1191
        FILE *fp;
1192
        char filename[64];
1193
        int start_addr;
1194
        int end_addr;
1195
        int dest_addr;
1196
        int load_addr;
1197
        int byte_count;
1198
        int data_byte;
1199
        int ch;
1200
        int len;
1201
 
1202
        lineptr = 0;
1203
        start_addr = 0;
1204
        end_addr = 0;
1205
 
1206
        /* skip leading white spaces */
1207
        while( isspace( cmdline[lineptr] ))
1208
                 lineptr++;
1209
        /* point to the start of the command argument & extract command */
1210
        cmdptr = &cmdline[lineptr];
1211
        len = 0;
1212
        while( isalpha( cmdline[lineptr] ) ||
1213
              (cmdline[lineptr] == '?'))
1214
        {
1215
                lineptr++;
1216
                len++;
1217
        }
1218
        /* skip trailing white spaces */
1219
        while( isspace( cmdline[lineptr] ) )
1220
                lineptr++;
1221
    if( len > 0 )
1222
    {
1223
 
1224
        if( str_equal( cmdptr, "T", len ) ||
1225
            str_equal( cmdptr, "type", len ) )
1226
        {
1227
                /***********************************************************
1228
                 *
1229
                 * Specify file I/O format type
1230
                 */
1231
                cmdptr = &cmdline[lineptr];
1232
                len = 0;
1233
                while( isalpha( cmdline[lineptr] ))
1234
                {
1235
                        lineptr++;
1236
                        len++;
1237
                }
1238
                if( len != 0 )
1239
                {
1240
                        if( str_equal( cmdptr, "B", len ) )
1241
                                format_type = BINARY;
1242
                        else if( str_equal( cmdptr, "binary", len ) )
1243
                                format_type = BINARY;
1244
                        else if( str_equal( cmdptr, "M", len ) )
1245
                                format_type = MOTOROLA;
1246
                        else if( str_equal( cmdptr, "motorola", len ) )
1247
                                format_type = MOTOROLA;
1248
                        else if( str_equal( cmdptr, "I", len ) )
1249
                                format_type = INTEL;
1250
                        else if( str_equal( cmdptr, "intel", len ) )
1251
                                format_type = INTEL;
1252
                        else if( str_equal( cmdptr, "O", len ) )
1253
                                format_type = SMAL32;
1254
                        else if( str_equal( cmdptr, "smal", len ) )
1255
                                format_type = SMAL32;
1256
                        else if( str_equal( cmdptr, "V", len ) )
1257
                                format_type = VHDL_BIN;
1258
                        else if( str_equal( cmdptr, "vhdl", len ) )
1259
                                format_type = VHDL_BIN;
1260
                        else if( str_equal( cmdptr, "H", len ) )
1261
                                format_type = VHDL_BYTE;
1262
                        else if( str_equal( cmdptr, "hex", len ) )
1263
                                format_type = VHDL_BYTE;
1264
                        else if( str_equal( cmdptr, "W", len ) )
1265
                                format_type = VHDL_WORD;
1266
                        else if( str_equal( cmdptr, "word", len ) )
1267
                                format_type = VHDL_WORD;
1268
                        else printf( "\nUnrecognised file format" );
1269
                }
1270
                printf( "\nFile format type is : " );
1271
                switch( format_type ) {
1272
                case BINARY:
1273
                        printf( "Raw Binary" );
1274
                        break;
1275
                case MOTOROLA:
1276
                        printf( "Motorola S1" );
1277
                        break;
1278
                case INTEL:
1279
                        printf( "Intel Hex" );
1280
                        break;
1281
                case SMAL32:
1282
                        printf( "Smal32 .o" );
1283
                        break;
1284
                case VHDL_BIN:
1285
                        printf( "vhdl binary" );
1286
                        break;
1287
                case VHDL_BYTE:
1288
                        printf( "vhdl hexadecimal byte" );
1289
                        break;
1290
                case VHDL_WORD:
1291
                        printf( "vhdl hexadecimal word" );
1292
                        break;
1293
                default:
1294
                        printf( "Bad format - should not get here" );
1295
                        break;
1296
                }
1297
                return TRUE;
1298
        }
1299
        else if( str_equal( cmdptr, "L", len ) ||
1300
                 str_equal( cmdptr, "load", len ) )
1301
        {
1302
                /***********************************************************
1303
                 *
1304
                 * Load file into buffer
1305
                 */
1306
                arglen = get_filename( &cmdline[lineptr], &filename[0] );
1307
                if( arglen == 0 )
1308
                {
1309
                        printf( "\nFile name expected " );
1310
                        return TRUE;
1311
                }
1312
                lineptr += arglen;
1313
                arglen = get_address( &cmdline[lineptr], &load_addr );
1314
                if( arglen == 0 )
1315
                {
1316
                        /* default binary load address = start of eprom */
1317
                        load_addr =  offset;
1318
 
1319
                }
1320
                if( (load_addr < offset) || (load_addr >= (offset + EPROM_MAX)))
1321
                {
1322
                        printf( "\nLoad address out of range" );
1323
                        return TRUE;
1324
                }
1325
                switch( format_type ) {
1326
                case BINARY:
1327
                        printf( "\nLoading binary file" );
1328
                        load_binary( filename, load_addr );
1329
                        break;
1330
                case MOTOROLA:
1331
                        printf( "\nLoading Motorola S1 file" );
1332
                        load_mot( filename );
1333
                        break;
1334
                case INTEL:
1335
                        printf( "\nLoading intel hex file" );
1336
                        load_intel( filename );
1337
                        break;
1338
                case SMAL32:
1339
                        printf( "\nLoading Smal32 file" );
1340
                        load_smal32( filename );
1341
                        break;
1342
                case VHDL_BIN:
1343
                        printf( "\nLoading VHDL binary format" );
1344
                        load_vhdl_bin( filename, load_addr );
1345
                        break;
1346
                case VHDL_BYTE:
1347
                        printf( "\nLoading VHDL hexadecimal byte format" );
1348
                        load_vhdl_byte( filename, load_addr );
1349
                        break;
1350
                case VHDL_WORD:
1351
                        printf( "\nLoading VHDL hexadecimal word format" );
1352
                        load_vhdl_word( filename, load_addr );
1353
                        break;
1354
                default:
1355
                        printf( "\nBad format - no load" );
1356
                        break;
1357
                }
1358
                mod_flag = TRUE;
1359
                return TRUE;
1360
        } else if( str_equal( cmdptr, "S", len ) ||
1361
                   str_equal( cmdptr, "save", len ))
1362
        {
1363
                /***********************************************************
1364
                 *
1365
                 * Save buffer to file
1366
                 *
1367
                 ***********************************************************/
1368
                arglen = get_filename( &cmdline[lineptr], &filename[0] );
1369
                if( arglen == 0 )
1370
                {
1371
                        printf( "\nFile name expected " );
1372
                        return TRUE;
1373
                }
1374
                lineptr += arglen;
1375
                arglen = get_address( &cmdline[lineptr], &start_addr );
1376
                if( arglen == 0 )
1377
                {
1378
                        /* this could default to 0 */
1379
                        printf( "\nStart address expected " );
1380
                        return TRUE;
1381
                }
1382
                lineptr += arglen;
1383
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0)
1384
                {
1385
                        /* this could default to eprom_top */
1386
                        printf( "\nEnd address expected " );
1387
                        return TRUE;
1388
                }
1389
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1390
                {
1391
                        printf( "\nStart address out of range" );
1392
                        return TRUE;
1393
                }
1394
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1395
                {
1396
                        printf( "\nEnd address out of range" );
1397
                        return TRUE;
1398
                }
1399
                printf( "\nSaving buffer %x to %x to file %s ", start_addr, end_addr, filename );
1400
                switch( format_type ) {
1401
                case BINARY:
1402
                        printf( "\nBinary format" );
1403
                        save_binary( filename, start_addr, end_addr );
1404
                        break;
1405
                case MOTOROLA:
1406
                        printf( "\nMotorola S1 format" );
1407
                        save_mot( filename, start_addr, end_addr );
1408
                        break;
1409
                case INTEL:
1410
                        printf( "\nIntel hex format" );
1411
                        save_intel( filename, start_addr, end_addr );
1412
                        break;
1413
                case VHDL_BIN:
1414
                        printf( "\nVHDL binary format" );
1415
                        save_vhdl_bin( filename, start_addr, end_addr );
1416
                        break;
1417
                case VHDL_BYTE:
1418
                        printf( "\nVHDL hexadecimal byte format" );
1419
                        save_vhdl_byte( filename, start_addr, end_addr );
1420
                        break;
1421
                case VHDL_WORD:
1422
                        printf( "\nVHDL hexadecimal word format" );
1423
                        save_vhdl_word( filename, start_addr, end_addr );
1424
                        break;
1425
                default:
1426
                        printf( "\nBad format - no save performed" );
1427
                        break;
1428
                }
1429
                return TRUE;
1430
        } else if( str_equal( cmdptr, "D", len ) ||
1431
                   str_equal( cmdptr, "dump", len ))
1432
        {
1433
                /***********************************************************
1434
                 *
1435
                 * dump buffer to the display
1436
                 */
1437
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1438
                {
1439
                        printf( "\nStart address expected " );
1440
                        return TRUE;
1441
                }
1442
                lineptr += arglen;
1443
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0)
1444
                {
1445
                        printf( "\nEnd address expected " );
1446
                        return TRUE;
1447
                }
1448
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1449
                {
1450
                        printf( "\nStart address out of range" );
1451
                        return TRUE;
1452
                }
1453
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1454
                {
1455
                        printf( "\nEnd address out of range" );
1456
                        return TRUE;
1457
                }
1458
                load_addr = 0;
1459
                printf( "\n Memory Dump form %x to %x ", start_addr, end_addr );
1460
                while( (start_addr + load_addr) <= end_addr )
1461
                {
1462
                        printf( "\n  %04x - ", start_addr + load_addr );
1463
                        for( dest_addr = 0; dest_addr < 16; dest_addr++ )
1464
                        {
1465
                                if( (start_addr + load_addr + dest_addr) <= end_addr )
1466
                                        printf( "%02x ", eprom_buff[(start_addr+load_addr+dest_addr) % EPROM_MAX] );
1467
                                else
1468
                                        printf( "   " );
1469
                        }
1470
                        printf( "  " );
1471
                        for( dest_addr = 0; dest_addr < 16; dest_addr++ )
1472
                        {
1473
                                if( (start_addr + load_addr + dest_addr) <= end_addr )
1474
                                {
1475
                                        ch = eprom_buff[(start_addr+load_addr+dest_addr) % EPROM_MAX];
1476
                                        if( (ch > 0x20) && (ch < 0x7f) )
1477
                                                printf( "%c", ch );
1478
                                        else
1479
                                                printf( "." );
1480
                                } else
1481
                                        printf( " " );
1482
                        }
1483
                        load_addr += dest_addr;
1484
                }
1485
                return TRUE;
1486
        } else if( str_equal( cmdptr, "C", len ) ||
1487
                   str_equal( cmdptr, "compare", len ))
1488
        {
1489
                /***********************************************************
1490
                 *
1491
                 * compare file with buffer
1492
                 *
1493
                 ***********************************************************/
1494
                if( (arglen = get_filename( &cmdline[lineptr], &filename[0] )) == 0 )
1495
                {
1496
                        printf( "\nFile name expected " );
1497
                        return TRUE;
1498
                }
1499
                lineptr += arglen;
1500
                /* start/load address is optional - default to start of eprom */
1501
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1502
                {
1503
                        start_addr = offset;
1504
                }
1505
                lineptr += arglen;
1506
 
1507
                /* the end/start address is optional */
1508
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0 )
1509
                {
1510
                        end_addr = EPROM_MAX + offset - 1;
1511
                }
1512
                lineptr += arglen;
1513
 
1514
                /* the end/start address is optional */
1515
                if( (arglen = get_address( &cmdline[lineptr], &load_addr )) == 0 )
1516
                {
1517
                        load_addr = EPROM_MAX + offset - 1;
1518
                }
1519
 
1520
                /* check for valid address range */
1521
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1522
                {
1523
                        printf( "\nStart address out of range" );
1524
                        return TRUE;
1525
                }
1526
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1527
                {
1528
                        printf( "\nEnd address out of range" );
1529
                        return TRUE;
1530
                }
1531
                if( (load_addr < offset) || (load_addr >= (offset + EPROM_MAX)))
1532
                {
1533
                        printf( "\nLoad address out of range" );
1534
                        return TRUE;
1535
                }
1536
                printf( "\nComparing buffer to file %s", filename );
1537
                switch( format_type ) {
1538
                case BINARY:
1539
                        printf( "\nBinary format" );
1540
                        dest_addr = load_addr;
1541
                        load_addr = start_addr;
1542
                        start_addr = end_addr;
1543
                        end_addr = dest_addr;
1544
                        if( start_addr == (EPROM_MAX + offset - 1) )
1545
                                start_addr = offset;
1546
                        compare_binary( filename, load_addr, start_addr, end_addr );
1547
                        break;
1548
                case MOTOROLA:
1549
                        printf( "\nMotorola S1 format" );
1550
                        compare_mot( filename, start_addr, end_addr );
1551
                        break;
1552
                case INTEL:
1553
                        printf( "\nIntel hex format" );
1554
                        compare_intel( filename, start_addr, end_addr );
1555
                        break;
1556
                default:
1557
                        printf( "\nBad format - no save performed" );
1558
                        break;
1559
                }
1560
                return TRUE;
1561
        } else if( str_equal( cmdptr, "M", len ) ||
1562
                   str_equal( cmdptr, "move", len ))
1563
        {
1564
                /***********************************************************
1565
                 *
1566
                 * memory block move
1567
                 */
1568
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1569
                {
1570
                        printf( "\nStart address expected " );
1571
                        return TRUE;
1572
                }
1573
                lineptr += arglen;
1574
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0)
1575
                {
1576
                        printf( "\nEnd address expected " );
1577
                        return TRUE;
1578
                }
1579
                lineptr += arglen;
1580
                if( (arglen = get_address( &cmdline[lineptr], &dest_addr )) == 0)
1581
                {
1582
                        printf( "\nDestination address expected " );
1583
                        return TRUE;
1584
                }
1585
                /* check that the range is around the right way */
1586
                if( end_addr < start_addr )
1587
                {
1588
                        load_addr = start_addr;
1589
                        start_addr = end_addr;
1590
                        end_addr = load_addr;
1591
                }
1592
                /* check for valid address range */
1593
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1594
                {
1595
                        printf( "\nStart address out of range" );
1596
                        return TRUE;
1597
                }
1598
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1599
                {
1600
                        printf( "\nEnd address out of range" );
1601
                        return TRUE;
1602
                }
1603
                if( (dest_addr < offset) || (dest_addr >= (offset + EPROM_MAX)))
1604
 
1605
                {
1606
                        printf( "\nDestination address out of range" );
1607
                        return TRUE;
1608
                }
1609
                byte_count = end_addr - start_addr;
1610
                printf( "\nTransfering memory block %04x thru %04x to %04x", start_addr, end_addr, dest_addr );
1611
                if( start_addr > dest_addr  )
1612
                {
1613
                        for( load_addr=0; load_addr<=byte_count; load_addr++ )
1614
                                eprom_buff[(dest_addr-offset+load_addr) % EPROM_MAX] = eprom_buff[(start_addr-offset+load_addr) % EPROM_MAX];
1615
                } else
1616
                {
1617
                        for( load_addr=byte_count; load_addr>=0; load_addr-- )
1618
                                eprom_buff[(dest_addr-offset+load_addr) % EPROM_MAX] = eprom_buff[(start_addr-offset+load_addr) % EPROM_MAX];
1619
                }
1620
                printf( "\nDone" );
1621
                mod_flag = TRUE;
1622
                return TRUE;
1623
        } else if( str_equal( cmdptr, "F", len ) ||
1624
                   str_equal( cmdptr, "fill", len ))
1625
        {
1626
                /***********************************************************
1627
                 *
1628
                 * fill memory with data
1629
                 */
1630
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1631
                {
1632
                        printf( "\nStart address expected " );
1633
                        return TRUE;
1634
                }
1635
                lineptr += arglen;
1636
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0)
1637
                {
1638
                        printf( "\nEnd address expected " );
1639
                        return TRUE;
1640
                }
1641
                lineptr += arglen;
1642
                if( (arglen = get_address( &cmdline[lineptr], &data_byte )) == 0)
1643
                {
1644
                        printf( "\nFill data byte expected " );
1645
                        return TRUE;
1646
                }
1647
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1648
                {
1649
                        printf( "\nStart address out of range" );
1650
                        return TRUE;
1651
                }
1652
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1653
                {
1654
                        printf( "\nEnd address out of range" );
1655
                        return TRUE;
1656
                }
1657
                printf( "\nFilling address %x thru %x with %x ", start_addr, end_addr, data_byte );
1658
                for( dest_addr=start_addr; dest_addr<=end_addr; dest_addr++ )
1659
                        eprom_buff[(dest_addr-offset) % EPROM_MAX] = (unsigned char)data_byte;
1660
                printf( "\nDone" );
1661
                mod_flag = TRUE;
1662
                return TRUE;
1663
        } else if( str_equal( cmdptr, "W", len ) ||
1664
                   str_equal( cmdptr, "write", len ))
1665
 
1666
        {
1667
                /***********************************************************
1668
                 *
1669
                 * Write to buffer location
1670
                 */
1671
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1672
                {
1673
                        printf( "\nStart address expected " );
1674
                        return TRUE;
1675
                }
1676
                lineptr += arglen;
1677
                /* check for valid address range */
1678
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1679
                {
1680
                        printf( "\nStart address out of range" );
1681
                        return TRUE;
1682
                }
1683
                printf( "\nSetting buffer loaction %x", start_addr );
1684
                while( (arglen = get_address( &cmdline[lineptr], &data_byte )) != 0)
1685
                {
1686
                        eprom_buff[(start_addr-offset) % EPROM_MAX] = (unsigned char)data_byte;
1687
                        printf( "\n %04x = %02x ", start_addr, data_byte );
1688
                        start_addr++;
1689
                        lineptr += arglen;
1690
                }
1691
                mod_flag = TRUE;
1692
                return TRUE;
1693
        } else if( str_equal( cmdptr, "O", len ) ||
1694
                   str_equal( cmdptr, "offset", len ))
1695
        {
1696
                /***********************************************************
1697
                 *
1698
                 * Set Eprom base address offset
1699
                 */
1700
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1701
                {
1702
                        /* No argument, display offset */
1703
                        printf( "\nBase address offset = %04x", offset );
1704
                        return TRUE;
1705
                }
1706
                offset = start_addr;
1707
                mod_flag = TRUE;
1708
                return TRUE;
1709
        } else if( str_equal( cmdptr, "H", len ) ||
1710
                   str_equal( cmdptr, "?", len ) ||
1711
                   str_equal( cmdptr, "help", len ))
1712
        {
1713
                /***********************************************************
1714
                 *
1715
                 * Display help menu
1716
                 */
1717
                display_help();
1718
                return TRUE;
1719
        } else if( str_equal( cmdptr, "Q", len ) ||
1720
                   str_equal( cmdptr, "quit", len ))
1721
        {
1722
                /***********************************************************
1723
                 *
1724
                 * Quit program - return FALSE
1725
                 */
1726
                printf( "\nQuitting program" );
1727
                return FALSE;
1728
        } else if( str_equal( cmdptr, "E", len ) ||
1729
                   str_equal( cmdptr, "echo", len ))
1730
        {
1731
                /***********************************************************
1732
                 *
1733
                 * Echo string to the console
1734
                 */
1735
                printf( "\n" );
1736
                while( (cmdline[lineptr] != '\0') && (cmdline[lineptr] != '\n'))
1737
                        printf( "%c", cmdline[lineptr++] );
1738
                printf( "\n" );
1739
                return TRUE;
1740
 
1741
        } else if( str_equal( cmdptr, "X", len ) ||
1742
                   str_equal( cmdptr, "execute", len ))
1743
        {
1744
                /***********************************************************
1745
                 *
1746
                 * Execute command script
1747
                 * We can onle do this if we are using stdin
1748
                 */
1749
                if( cmdfp != stdin )
1750
                {
1751
                        printf( "\nWe cannot nest redirected input " );
1752
                        return TRUE;
1753
                }
1754
 
1755
                arglen = get_filename( &cmdline[lineptr], &filename[0] );
1756
                if( arglen == 0 )
1757
                {
1758
                        printf( "\nFile name expected " );
1759
                        return TRUE;
1760
                }
1761
 
1762
                if( (cmdfp = fopen( &filename[0], "r" )) == NULL )
1763
                {
1764
                        printf( "\ncan't open auxillary input file '%s'", &filename[0] );
1765
                        /* we had better revert to the original command file */
1766
                        cmdfp = stdin;
1767
                        return TRUE;
1768
                }
1769
                /* every thing is ok, input is re-directed */
1770
                auxflag = TRUE;
1771
                return TRUE;
1772
        }
1773
    }
1774
        /***********************************************************
1775
         *
1776
         * Command not understood
1777
         */
1778
        printf( "\nUnrecognised command" );
1779
        return TRUE;
1780
}
1781
 
1782
/*
1783
 * epedit main program
1784
 */
1785
main(int argc, char **argv)
1786
{
1787
 
1788
        auxflag = FALSE;
1789
        offset = 0;
1790
        format_type = BINARY;
1791
 
1792
        if(argc >= 2)
1793
        {
1794
                /* check for auxillary command file */
1795
                if((cmdfp = fopen( argv[1], "r" )) == NULL)
1796
                {
1797
                        printf(" can't open input on %s\n", argv[1] );
1798
                        printf("Usage: %s [command-file]\n",argv[0]);
1799
                        exit(1);
1800
                }
1801
                auxflag = TRUE;
1802
        } else
1803
        {
1804
                /* no auxillary command file specified, use stdin */
1805
                cmdfp = stdin;
1806
        }
1807
 
1808
        eprom_top = 0;
1809
        printf( "*** EPROM Editor ***\n" );
1810
        printf( "type H/?/help for commands\n" );
1811
        do {
1812
                read_command( cmdbuff );
1813
        } while( parse_command( cmdbuff ) );
1814
        printf( "\n*** exit epedit ***\n" );
1815
        exit( 0 );
1816
}
1817
 

powered by: WebSVN 2.1.0

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