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

Subversion Repositories System09

[/] [System09/] [trunk/] [Tools/] [epedit/] [new/] [epedit.c] - Blame information for rev 218

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, "r" )) == 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
        fclose( 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
        fclose( 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
        fclose( 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
                if (addr > end_addr-1)
786
                        putc( ',', fp_out );
787
                putc( '\n', fp_out );
788
        }
789
        fclose( fp_out );
790
        printf( "\nwritten successfully" );
791
}
792
 
793
 
794
/*
795
 * save VHDL hexadecimal file
796
 */
797
 
798
void save_vhdl_byte( char *fname_out, int start_addr, int end_addr )
799
{
800
        FILE *fp_out;
801
        int addr;
802
        int i,j;
803
        int byte;
804
 
805
        if( (fp_out = fopen( fname_out, "w" )) == NULL )
806
        {
807
                printf( "\nCan't open '%s' for write ", fname_out );
808
                return;
809
        }
810
        j=0;
811
        for( addr=start_addr; addr<=end_addr; addr+=32 )
812
        {
813
                fprintf( fp_out, "    INIT_%02x => x\"", j );
814
                for(i=31; i>=0; i-- )
815
                {
816
                        byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
817
                        putc( hex_str[(byte >>4) & 0xf], fp_out );
818
                        putc( hex_str[byte & 0xf], fp_out );
819
                }
820
                if ( addr > (end_addr - 32) )
821
                        fprintf( fp_out, "\"\n" );
822
                else
823
                        fprintf( fp_out, "\",\n" );
824
                j++;
825
        }
826
        fclose( fp_out );
827
        printf( "\nwritten successfully" );
828
}
829
 
830
 
831
/*
832
 * save VHDL hexadecimal Word file
833
 */
834
 
835
void save_vhdl_word( char *fname_out, int start_addr, int end_addr )
836
{
837
        FILE *fp_out;
838
        int addr;
839
        int i,j;
840
        int byte;
841
 
842
        if( (fp_out = fopen( fname_out, "w" )) == NULL )
843
        {
844
                printf( "\nCan't open '%s' for write ", fname_out );
845
                return;
846
        }
847
        j=0;
848
        for( addr=start_addr; addr<=end_addr; addr+=32 )
849
        {
850
                fprintf( fp_out, "  INIT_%02x => x\"" );
851
                for(i=30; i>=0; i-=2 )
852
                {
853
                        /* MSByte first */
854
                        byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
855
                        putc( hex_str[(byte >>4) & 0xf], fp_out );
856
                        putc( hex_str[byte & 0xf], fp_out );
857
                        /* LSByte second */
858
                        byte = (int)eprom_buff[(addr - offset + i + 1) % EPROM_MAX];
859
                        putc( hex_str[(byte >>4) & 0xf], fp_out );
860
                        putc( hex_str[byte & 0xf], fp_out );
861
                }
862
                if ( addr > (end_addr - 32) )
863
                        fprintf( fp_out, "\"\n" );
864
                else
865
                        fprintf( fp_out, "\",\n" );
866
                j++;
867
        }
868
        fclose( fp_out );
869
        printf( "\nwritten successfully" );
870
}
871
 
872
 
873
/*
874
 * save raw binary file
875
 */
876
 
877
void save_binary( char *fname_out, int start_addr, int end_addr )
878
{
879
        FILE *fp_out;
880
        int addr;
881
 
882
        if( (fp_out = fopen( fname_out, "w" )) == NULL )
883
        {
884
                printf( "\nCan't open '%s' for write ", fname_out );
885
                return;
886
        }
887
        for( addr=start_addr; addr<=end_addr; addr++ )
888
        {
889
                putc( eprom_buff[(addr - offset) % EPROM_MAX ], fp_out );
890
        }
891
        fclose( fp_out );
892
        printf( "\nwritten successfully" );
893
}
894
 
895
 
896
/*
897
 * Save Motorola S1 file
898
 */
899
 
900
void save_mot( char *fname_out, int start_addr, int end_addr )
901
{
902
        FILE *fp_out;
903
        int addr;
904
        int i;
905
        int E_total;
906
        int data_byte;
907
 
908
        fp_out = fopen( fname_out, "w" );
909
        if( !fp_out )
910
        {
911
            printf( "\nCan't open %s for output", fname_out );
912
            return;
913
        }
914
 
915
        addr = start_addr;
916
        while( addr <= end_addr)
917
        {
918
                if( (end_addr - addr + 1) > 0 )
919
                        E_total = 16;
920
                else
921
                        E_total = end_addr - addr + 1;
922
                fputc( 'S', fp_out);    /* record header preamble */
923
                fputc( '1', fp_out);    /* record header preamble */
924
                checksum = 0;
925
                put2hex( fp_out, E_total+3);    /* byte count + 3 */
926
                put4hex( fp_out, addr);         /* Program Counter */
927
                for(i=0; i<E_total; i++)
928
                {
929
                        data_byte = (int)eprom_buff[(addr - offset + i) % EPROM_MAX];
930
                        put2hex( fp_out, data_byte);
931
                }
932
                checksum = ~checksum;   /* one's complement */
933
                put2hex( fp_out, (checksum & 0xff));
934
                fputc( '\n', fp_out);   /* record header preamble */
935
                addr += E_total;
936
        }
937
        checksum = 0;
938
        fputc( 'S', fp_out);            /* record header preamble */
939
        fputc( '9', fp_out);            /* record header preamble */
940
        put2hex( fp_out, 3);            /* byte count + 3 */
941
        put4hex( fp_out, 0);             /* Program Counter */
942
        put2hex( fp_out, 0xfc );        /* byte count + 3 */
943
        fclose( fp_out );
944
}
945
 
946
/*
947
 * save Intelhex file
948
 */
949
 
950
void save_intel( char *fname_out, int start_addr, int end_addr )
951
{
952
        FILE *fp_out;
953
        int byte, addr, start, check, i;
954
 
955
        fp_out = fopen( fname_out, "w" );
956
        if( fp_out == NULL )
957
        {
958
                printf( "\nCan't open %s for output", fname_out );
959
                return;
960
        }
961
 
962
        addr = start_addr;
963
        start = addr;
964
        while( addr <= end_addr )
965
        {
966
                check = 0;
967
                count = 16;
968
                fputc( ':', fp_out );
969
                put2hex( fp_out, count );
970
                check += count;
971
                put4hex( fp_out, addr );
972
                check += (addr & 0xff);
973
                check += ((addr>>8) & 0xff);
974
                put2hex( fp_out, 0 );
975
                check += 0;
976
                for( i=0; i<count; i++ )
977
                {
978
                    byte = (int)eprom_buff[ (addr - offset) % EPROM_MAX ];
979
                    if( addr == 0xfffe )
980
                        start = byte * 256 ;
981
                    if( addr == 0xffff )
982
                        start += byte;
983
                    put2hex( fp_out, byte );
984
                    check += byte;
985
                    addr ++;
986
                }
987
                check = (0-check) & 0xff;
988
                put2hex( fp_out, check);
989
                fputc( '\n', fp_out );
990
        }
991
        fputc( ':', fp_out );
992
        put2hex( fp_out, 0 );
993
        put4hex( fp_out, start );
994
        put2hex( fp_out, 1 );
995
        fclose( fp_out );
996
}
997
 
998
 
999
/*
1000
 * Compare Raw binary file
1001
 */
1002
 
1003
void compare_binary( char *fname_in, int load_addr, int start_addr, int end_addr)
1004
{
1005
        FILE *fp_in;
1006
        int addr;
1007
        int data_byte;
1008
        int diff_count;
1009
 
1010
        if( (fp_in = fopen( fname_in, "r" )) == NULL )
1011
        {
1012
                printf( "\ncant open %s for read", fname_in );
1013
                return;
1014
        } else
1015
        {
1016
                diff_count = 0;
1017
                addr = load_addr;
1018
                while( (data_byte = fgetc( fp_in )) != EOF )
1019
                {
1020
                        if( (addr >= start_addr) && (addr <= end_addr) )
1021
                        {
1022
                                if( (unsigned char)data_byte != eprom_buff[(addr -offset) % EPROM_MAX] )
1023
                                {
1024
                                        printf( "\n%04x %02x %02x", addr, eprom_buff[(addr-offset) % EPROM_MAX ], data_byte );
1025
                                        diff_count++;
1026
                                }
1027
                        }
1028
                        addr++;
1029
                }
1030
                printf( "\nLast compared address %x ", addr-1 );
1031
                printf( "\nThere were %d differences ", diff_count );
1032
                fclose( fp_in );
1033
        }
1034
}
1035
 
1036
 
1037
/*
1038
 * Compare motorola formatted file
1039
 */
1040
 
1041
void compare_mot( char *fname_in, int start_addr, int end_addr )
1042
{
1043
        FILE *fp_in;
1044
        int byte, addr,  i;
1045
        int diff_count;
1046
 
1047
        fp_in = fopen( fname_in, "r" );
1048
        if( !fp_in )
1049
        {
1050
            printf( "\nCan't open %s", fname_in );
1051
            return;
1052
        }
1053
 
1054
        byte = 0;
1055
        diff_count = 0;
1056
 
1057
        while( byte != -1 )
1058
        {
1059
            do {
1060
                byte = fgetc( fp_in);
1061
            } while( (byte != 'S') && (byte != -1) );
1062
 
1063
            byte = fgetc( fp_in );
1064
            checksum = 0;
1065
            if( (byte == '1') || (byte == '2') )
1066
            {
1067
                count = get2hex( fp_in );
1068
                if( byte == '1' )
1069
                    addr = get4hex( fp_in );
1070
                else
1071
                    addr = get6hex( fp_in );
1072
                count -= 3;
1073
                for( i=0; i<count; i++ )
1074
                {
1075
                        byte = get2hex( fp_in );
1076
                        if( (addr >= start_addr) && (addr <= end_addr ))
1077
                        {
1078
                                if( (unsigned char)byte != eprom_buff[(addr - offset) % EPROM_MAX ] )
1079
                                {
1080
                                        printf( "\n%04x %02x %02x", addr, eprom_buff[(addr-offset) % EPROM_MAX ], byte );
1081
                                        diff_count++;
1082
                                }
1083
                        }
1084
                        addr++;
1085
                }
1086
                byte = get2hex( fp_in);
1087
                if( addr >= EPROM_MAX )
1088
                   byte = -1;
1089
             }
1090
        }
1091
        fclose( fp_in );
1092
        printf( "\nLast compared address %x ", addr - 1 );
1093
        printf( "\nThere were %d differences ", diff_count );
1094
}
1095
 
1096
/*
1097
 * Load Intel hex file
1098
 */
1099
 
1100
void compare_intel( char *fname_in, int start_addr, int end_addr )
1101
{
1102
        FILE *fp_in;
1103
        int byte, addr, i;
1104
        int diff_count;
1105
 
1106
        fp_in = fopen( fname_in, "r" );
1107
        if( !fp_in )
1108
        {
1109
            printf( "\nCan't open %s for input", fname_in );
1110
            return;
1111
        }
1112
 
1113
        byte = 0;
1114
        diff_count = 0;
1115
        while( byte != -1)
1116
        {
1117
                do {
1118
                        byte = fgetc( fp_in);
1119
                } while( (byte != ':') && (byte != -1) );
1120
 
1121
                checksum = 0;
1122
                count = get2hex( fp_in );
1123
                addr = get4hex( fp_in );
1124
                i = get2hex( fp_in );           /* read 00 to load a record */
1125
                if( i == 0 )                     /* read 01 to end record */
1126
                {
1127
                        for( i=0; i<count; i++ )
1128
                        {
1129
                                byte = get2hex( fp_in );
1130
                                if( (addr >= start_addr) && (addr <= end_addr ))
1131
                                {
1132
                                        if( (unsigned char)byte != eprom_buff[(addr - offset) % EPROM_MAX] )
1133
                                        {
1134
                                                printf( "\n%04x %02x %02x", addr, eprom_buff[(addr-offset) % EPROM_MAX], byte );
1135
                                                diff_count++;
1136
                                        }
1137
                                }
1138
                                addr++;
1139
                        }
1140
                        byte = get2hex( fp_in);
1141
                        if( checksum != 0 )
1142
                        {
1143
                                printf( "\nchecksum error detected, read %02x, expected %02x", byte, ((checksum - byte) & 0xff) );
1144
                                byte = -1;
1145
                        } else if( addr >= EPROM_MAX )
1146
                           byte = -1;
1147
                } else if( i == 1 )             /* test for end of record */
1148
                        byte = -1;
1149
        }
1150
        fclose( fp_in );
1151
        printf( "\nLast compared address %x ", addr - 1 );
1152
        printf( "\nThere were %d differences ", diff_count );
1153
}
1154
 
1155
 
1156
/*
1157
 * help menu
1158
 */
1159
void display_help()
1160
{
1161
        printf( "\n*** Help Menu ***\n\n");
1162
        printf( "All addresses are in hexadecimal\n" );
1163
        printf( "H/?/help                             - This help menu\n" );
1164
        printf( "T/type - Set Load & Save format\n" );
1165
        printf( "     <I/intel>\n");
1166
        printf( "     <M/motorola>\n");
1167
        printf( "     <B/binary>\n" );
1168
        printf( "     <O/Smal32>\n" );
1169
        printf( "     <V/vhdl> VHDL Binary String \n" );
1170
        printf( "     <H/hex> VHDL Hexadecimal Byte String\n" );
1171
        printf( "     <W/word> VHDL Hexadecimal Word String\n" );
1172
        printf( "O/offset <address>                   - Set Eprom buffer offset\n" );
1173
        printf( "L/load <filename> [<load_addr>]      - Loads file into buffer\n" );
1174
        printf( "S/save <filename> <start-address> <end-address> - Saves buffer to file\n" );
1175
        printf( "C/compare <filename> [<load_addr>]   - compare file to buffer\n" );
1176
/*
1177
        printf( "R/read <address>                     - reads buffer data\n" );
1178
*/
1179
        printf( "W/write <address> <data> .... <data> - Write data to buffer\n" );
1180
        printf( "F/fill <start-address> <end-address> <data> - fill buffer with data\n");
1181
        printf( "M/move <start-address> <end-address> <dest-address> - block move\n" );
1182
        printf( "D/dump <start_address> <end-address> - Hex buffer dump\n");
1183
        printf( "E/echo <string>                      - echo string to console\n" );
1184
        printf( "X/excecute <command-file>            - execute a command script\n");
1185
        printf( "Q/quit                               - quit program\n" );
1186
}
1187
 
1188
/*
1189
 * Parse command
1190
 * Return FALSE to exit
1191
 */
1192
int parse_command( char *cmdline )
1193
{
1194
        int lineptr;
1195
        char *cmdptr;
1196
        int arglen;
1197
        char filename[64];
1198
        int start_addr;
1199
        int end_addr;
1200
        int dest_addr;
1201
        int load_addr;
1202
        int byte_count;
1203
        int data_byte;
1204
        int ch;
1205
        int len;
1206
 
1207
        lineptr = 0;
1208
        start_addr = 0;
1209
        end_addr = 0;
1210
 
1211
        /* skip leading white spaces */
1212
        while( isspace( cmdline[lineptr] ))
1213
                 lineptr++;
1214
        /* point to the start of the command argument & extract command */
1215
        cmdptr = &cmdline[lineptr];
1216
        len = 0;
1217
        while( isalpha( cmdline[lineptr] ) ||
1218
              (cmdline[lineptr] == '?'))
1219
        {
1220
                lineptr++;
1221
                len++;
1222
        }
1223
        /* skip trailing white spaces */
1224
        while( isspace( cmdline[lineptr] ) )
1225
                lineptr++;
1226
    if( len > 0 )
1227
    {
1228
 
1229
        if( str_equal( cmdptr, "T", len ) ||
1230
            str_equal( cmdptr, "type", len ) )
1231
        {
1232
                /***********************************************************
1233
                 *
1234
                 * Specify file I/O format type
1235
                 */
1236
                cmdptr = &cmdline[lineptr];
1237
                len = 0;
1238
                while( isalpha( cmdline[lineptr] ))
1239
                {
1240
                        lineptr++;
1241
                        len++;
1242
                }
1243
                if( len != 0 )
1244
                {
1245
                        if( str_equal( cmdptr, "B", len ) )
1246
                                format_type = BINARY;
1247
                        else if( str_equal( cmdptr, "binary", len ) )
1248
                                format_type = BINARY;
1249
                        else if( str_equal( cmdptr, "M", len ) )
1250
                                format_type = MOTOROLA;
1251
                        else if( str_equal( cmdptr, "motorola", len ) )
1252
                                format_type = MOTOROLA;
1253
                        else if( str_equal( cmdptr, "I", len ) )
1254
                                format_type = INTEL;
1255
                        else if( str_equal( cmdptr, "intel", len ) )
1256
                                format_type = INTEL;
1257
                        else if( str_equal( cmdptr, "O", len ) )
1258
                                format_type = SMAL32;
1259
                        else if( str_equal( cmdptr, "smal", len ) )
1260
                                format_type = SMAL32;
1261
                        else if( str_equal( cmdptr, "V", len ) )
1262
                                format_type = VHDL_BIN;
1263
                        else if( str_equal( cmdptr, "vhdl", len ) )
1264
                                format_type = VHDL_BIN;
1265
                        else if( str_equal( cmdptr, "H", len ) )
1266
                                format_type = VHDL_BYTE;
1267
                        else if( str_equal( cmdptr, "hex", len ) )
1268
                                format_type = VHDL_BYTE;
1269
                        else if( str_equal( cmdptr, "W", len ) )
1270
                                format_type = VHDL_WORD;
1271
                        else if( str_equal( cmdptr, "word", len ) )
1272
                                format_type = VHDL_WORD;
1273
                        else printf( "\nUnrecognised file format" );
1274
                }
1275
                printf( "\nFile format type is : " );
1276
                switch( format_type ) {
1277
                case BINARY:
1278
                        printf( "Raw Binary" );
1279
                        break;
1280
                case MOTOROLA:
1281
                        printf( "Motorola S1" );
1282
                        break;
1283
                case INTEL:
1284
                        printf( "Intel Hex" );
1285
                        break;
1286
                case SMAL32:
1287
                        printf( "Smal32 .o" );
1288
                        break;
1289
                case VHDL_BIN:
1290
                        printf( "vhdl binary" );
1291
                        break;
1292
                case VHDL_BYTE:
1293
                        printf( "vhdl hexadecimal byte" );
1294
                        break;
1295
                case VHDL_WORD:
1296
                        printf( "vhdl hexadecimal word" );
1297
                        break;
1298
                default:
1299
                        printf( "Bad format - should not get here" );
1300
                        break;
1301
                }
1302
                return TRUE;
1303
        }
1304
        else if( str_equal( cmdptr, "L", len ) ||
1305
                 str_equal( cmdptr, "load", len ) )
1306
        {
1307
                /***********************************************************
1308
                 *
1309
                 * Load file into buffer
1310
                 */
1311
                arglen = get_filename( &cmdline[lineptr], &filename[0] );
1312
                if( arglen == 0 )
1313
                {
1314
                        printf( "\nFile name expected " );
1315
                        return TRUE;
1316
                }
1317
                lineptr += arglen;
1318
                arglen = get_address( &cmdline[lineptr], &load_addr );
1319
                if( arglen == 0 )
1320
                {
1321
                        /* default binary load address = start of eprom */
1322
                        load_addr =  offset;
1323
 
1324
                }
1325
                if( (load_addr < offset) || (load_addr >= (offset + EPROM_MAX)))
1326
                {
1327
                        printf( "\nLoad address out of range" );
1328
                        return TRUE;
1329
                }
1330
                switch( format_type ) {
1331
                case BINARY:
1332
                        printf( "\nLoading binary file" );
1333
                        load_binary( filename, load_addr );
1334
                        break;
1335
                case MOTOROLA:
1336
                        printf( "\nLoading Motorola S1 file" );
1337
                        load_mot( filename );
1338
                        break;
1339
                case INTEL:
1340
                        printf( "\nLoading intel hex file" );
1341
                        load_intel( filename );
1342
                        break;
1343
                case SMAL32:
1344
                        printf( "\nLoading Smal32 file" );
1345
                        load_smal32( filename );
1346
                        break;
1347
                case VHDL_BIN:
1348
                        printf( "\nLoading VHDL binary format" );
1349
                        load_vhdl_bin( filename, load_addr );
1350
                        break;
1351
                case VHDL_BYTE:
1352
                        printf( "\nLoading VHDL hexadecimal byte format" );
1353
                        load_vhdl_byte( filename, load_addr );
1354
                        break;
1355
                case VHDL_WORD:
1356
                        printf( "\nLoading VHDL hexadecimal word format" );
1357
                        load_vhdl_word( filename, load_addr );
1358
                        break;
1359
                default:
1360
                        printf( "\nBad format - no load" );
1361
                        break;
1362
                }
1363
                mod_flag = TRUE;
1364
                return TRUE;
1365
        } else if( str_equal( cmdptr, "S", len ) ||
1366
                   str_equal( cmdptr, "save", len ))
1367
        {
1368
                /***********************************************************
1369
                 *
1370
                 * Save buffer to file
1371
                 *
1372
                 ***********************************************************/
1373
                arglen = get_filename( &cmdline[lineptr], &filename[0] );
1374
                if( arglen == 0 )
1375
                {
1376
                        printf( "\nFile name expected " );
1377
                        return TRUE;
1378
                }
1379
                lineptr += arglen;
1380
                arglen = get_address( &cmdline[lineptr], &start_addr );
1381
                if( arglen == 0 )
1382
                {
1383
                        /* this could default to 0 */
1384
                        printf( "\nStart address expected " );
1385
                        return TRUE;
1386
                }
1387
                lineptr += arglen;
1388
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0)
1389
                {
1390
                        /* this could default to eprom_top */
1391
                        printf( "\nEnd address expected " );
1392
                        return TRUE;
1393
                }
1394
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1395
                {
1396
                        printf( "\nStart address out of range" );
1397
                        return TRUE;
1398
                }
1399
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1400
                {
1401
                        printf( "\nEnd address out of range" );
1402
                        return TRUE;
1403
                }
1404
                printf( "\nSaving buffer %x to %x to file %s ", start_addr, end_addr, filename );
1405
                switch( format_type ) {
1406
                case BINARY:
1407
                        printf( "\nBinary format" );
1408
                        save_binary( filename, start_addr, end_addr );
1409
                        break;
1410
                case MOTOROLA:
1411
                        printf( "\nMotorola S1 format" );
1412
                        save_mot( filename, start_addr, end_addr );
1413
                        break;
1414
                case INTEL:
1415
                        printf( "\nIntel hex format" );
1416
                        save_intel( filename, start_addr, end_addr );
1417
                        break;
1418
                case VHDL_BIN:
1419
                        printf( "\nVHDL binary format" );
1420
                        save_vhdl_bin( filename, start_addr, end_addr );
1421
                        break;
1422
                case VHDL_BYTE:
1423
                        printf( "\nVHDL hexadecimal byte format" );
1424
                        save_vhdl_byte( filename, start_addr, end_addr );
1425
                        break;
1426
                case VHDL_WORD:
1427
                        printf( "\nVHDL hexadecimal word format" );
1428
                        save_vhdl_word( filename, start_addr, end_addr );
1429
                        break;
1430
                default:
1431
                        printf( "\nBad format - no save performed" );
1432
                        break;
1433
                }
1434
                return TRUE;
1435
        } else if( str_equal( cmdptr, "D", len ) ||
1436
                   str_equal( cmdptr, "dump", len ))
1437
        {
1438
                /***********************************************************
1439
                 *
1440
                 * dump buffer to the display
1441
                 */
1442
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1443
                {
1444
                        printf( "\nStart address expected " );
1445
                        return TRUE;
1446
                }
1447
                lineptr += arglen;
1448
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0)
1449
                {
1450
                        printf( "\nEnd address expected " );
1451
                        return TRUE;
1452
                }
1453
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1454
                {
1455
                        printf( "\nStart address out of range" );
1456
                        return TRUE;
1457
                }
1458
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1459
                {
1460
                        printf( "\nEnd address out of range" );
1461
                        return TRUE;
1462
                }
1463
                load_addr = 0;
1464
                printf( "\n Memory Dump form %x to %x ", start_addr, end_addr );
1465
                while( (start_addr + load_addr) <= end_addr )
1466
                {
1467
                        printf( "\n  %04x - ", start_addr + load_addr );
1468
                        for( dest_addr = 0; dest_addr < 16; dest_addr++ )
1469
                        {
1470
                                if( (start_addr + load_addr + dest_addr) <= end_addr )
1471
                                        printf( "%02x ", eprom_buff[(start_addr+load_addr+dest_addr) % EPROM_MAX] );
1472
                                else
1473
                                        printf( "   " );
1474
                        }
1475
                        printf( "  " );
1476
                        for( dest_addr = 0; dest_addr < 16; dest_addr++ )
1477
                        {
1478
                                if( (start_addr + load_addr + dest_addr) <= end_addr )
1479
                                {
1480
                                        ch = eprom_buff[(start_addr+load_addr+dest_addr) % EPROM_MAX];
1481
                                        if( (ch > 0x20) && (ch < 0x7f) )
1482
                                                printf( "%c", ch );
1483
                                        else
1484
                                                printf( "." );
1485
                                } else
1486
                                        printf( " " );
1487
                        }
1488
                        load_addr += dest_addr;
1489
                }
1490
                return TRUE;
1491
        } else if( str_equal( cmdptr, "C", len ) ||
1492
                   str_equal( cmdptr, "compare", len ))
1493
        {
1494
                /***********************************************************
1495
                 *
1496
                 * compare file with buffer
1497
                 *
1498
                 ***********************************************************/
1499
                if( (arglen = get_filename( &cmdline[lineptr], &filename[0] )) == 0 )
1500
                {
1501
                        printf( "\nFile name expected " );
1502
                        return TRUE;
1503
                }
1504
                lineptr += arglen;
1505
                /* start/load address is optional - default to start of eprom */
1506
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1507
                {
1508
                        start_addr = offset;
1509
                }
1510
                lineptr += arglen;
1511
 
1512
                /* the end/start address is optional */
1513
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0 )
1514
                {
1515
                        end_addr = EPROM_MAX + offset - 1;
1516
                }
1517
                lineptr += arglen;
1518
 
1519
                /* the end/start address is optional */
1520
                if( (arglen = get_address( &cmdline[lineptr], &load_addr )) == 0 )
1521
                {
1522
                        load_addr = EPROM_MAX + offset - 1;
1523
                }
1524
 
1525
                /* check for valid address range */
1526
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1527
                {
1528
                        printf( "\nStart address out of range" );
1529
                        return TRUE;
1530
                }
1531
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1532
                {
1533
                        printf( "\nEnd address out of range" );
1534
                        return TRUE;
1535
                }
1536
                if( (load_addr < offset) || (load_addr >= (offset + EPROM_MAX)))
1537
                {
1538
                        printf( "\nLoad address out of range" );
1539
                        return TRUE;
1540
                }
1541
                printf( "\nComparing buffer to file %s", filename );
1542
                switch( format_type ) {
1543
                case BINARY:
1544
                        printf( "\nBinary format" );
1545
                        dest_addr = load_addr;
1546
                        load_addr = start_addr;
1547
                        start_addr = end_addr;
1548
                        end_addr = dest_addr;
1549
                        if( start_addr == (EPROM_MAX + offset - 1) )
1550
                                start_addr = offset;
1551
                        compare_binary( filename, load_addr, start_addr, end_addr );
1552
                        break;
1553
                case MOTOROLA:
1554
                        printf( "\nMotorola S1 format" );
1555
                        compare_mot( filename, start_addr, end_addr );
1556
                        break;
1557
                case INTEL:
1558
                        printf( "\nIntel hex format" );
1559
                        compare_intel( filename, start_addr, end_addr );
1560
                        break;
1561
                default:
1562
                        printf( "\nBad format - no save performed" );
1563
                        break;
1564
                }
1565
                return TRUE;
1566
        } else if( str_equal( cmdptr, "M", len ) ||
1567
                   str_equal( cmdptr, "move", len ))
1568
        {
1569
                /***********************************************************
1570
                 *
1571
                 * memory block move
1572
                 */
1573
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1574
                {
1575
                        printf( "\nStart address expected " );
1576
                        return TRUE;
1577
                }
1578
                lineptr += arglen;
1579
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0)
1580
                {
1581
                        printf( "\nEnd address expected " );
1582
                        return TRUE;
1583
                }
1584
                lineptr += arglen;
1585
                if( (arglen = get_address( &cmdline[lineptr], &dest_addr )) == 0)
1586
                {
1587
                        printf( "\nDestination address expected " );
1588
                        return TRUE;
1589
                }
1590
                /* check that the range is around the right way */
1591
                if( end_addr < start_addr )
1592
                {
1593
                        load_addr = start_addr;
1594
                        start_addr = end_addr;
1595
                        end_addr = load_addr;
1596
                }
1597
                /* check for valid address range */
1598
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1599
                {
1600
                        printf( "\nStart address out of range" );
1601
                        return TRUE;
1602
                }
1603
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1604
                {
1605
                        printf( "\nEnd address out of range" );
1606
                        return TRUE;
1607
                }
1608
                if( (dest_addr < offset) || (dest_addr >= (offset + EPROM_MAX)))
1609
 
1610
                {
1611
                        printf( "\nDestination address out of range" );
1612
                        return TRUE;
1613
                }
1614
                byte_count = end_addr - start_addr;
1615
                printf( "\nTransfering memory block %04x thru %04x to %04x", start_addr, end_addr, dest_addr );
1616
                if( start_addr > dest_addr  )
1617
                {
1618
                        for( load_addr=0; load_addr<=byte_count; load_addr++ )
1619
                                eprom_buff[(dest_addr-offset+load_addr) % EPROM_MAX] = eprom_buff[(start_addr-offset+load_addr) % EPROM_MAX];
1620
                } else
1621
                {
1622
                        for( load_addr=byte_count; load_addr>=0; load_addr-- )
1623
                                eprom_buff[(dest_addr-offset+load_addr) % EPROM_MAX] = eprom_buff[(start_addr-offset+load_addr) % EPROM_MAX];
1624
                }
1625
                printf( "\nDone" );
1626
                mod_flag = TRUE;
1627
                return TRUE;
1628
        } else if( str_equal( cmdptr, "F", len ) ||
1629
                   str_equal( cmdptr, "fill", len ))
1630
        {
1631
                /***********************************************************
1632
                 *
1633
                 * fill memory with data
1634
                 */
1635
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1636
                {
1637
                        printf( "\nStart address expected " );
1638
                        return TRUE;
1639
                }
1640
                lineptr += arglen;
1641
                if( (arglen = get_address( &cmdline[lineptr], &end_addr )) == 0)
1642
                {
1643
                        printf( "\nEnd address expected " );
1644
                        return TRUE;
1645
                }
1646
                lineptr += arglen;
1647
                if( (arglen = get_address( &cmdline[lineptr], &data_byte )) == 0)
1648
                {
1649
                        printf( "\nFill data byte expected " );
1650
                        return TRUE;
1651
                }
1652
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1653
                {
1654
                        printf( "\nStart address out of range" );
1655
                        return TRUE;
1656
                }
1657
                if( (end_addr < offset) || (end_addr >= (offset + EPROM_MAX)))
1658
                {
1659
                        printf( "\nEnd address out of range" );
1660
                        return TRUE;
1661
                }
1662
                printf( "\nFilling address %x thru %x with %x ", start_addr, end_addr, data_byte );
1663
                for( dest_addr=start_addr; dest_addr<=end_addr; dest_addr++ )
1664
                        eprom_buff[(dest_addr-offset) % EPROM_MAX] = (unsigned char)data_byte;
1665
                printf( "\nDone" );
1666
                mod_flag = TRUE;
1667
                return TRUE;
1668
        } else if( str_equal( cmdptr, "W", len ) ||
1669
                   str_equal( cmdptr, "write", len ))
1670
 
1671
        {
1672
                /***********************************************************
1673
                 *
1674
                 * Write to buffer location
1675
                 */
1676
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1677
                {
1678
                        printf( "\nStart address expected " );
1679
                        return TRUE;
1680
                }
1681
                lineptr += arglen;
1682
                /* check for valid address range */
1683
                if( (start_addr < offset) || (start_addr >= (offset + EPROM_MAX)))
1684
                {
1685
                        printf( "\nStart address out of range" );
1686
                        return TRUE;
1687
                }
1688
                printf( "\nSetting buffer loaction %x", start_addr );
1689
                while( (arglen = get_address( &cmdline[lineptr], &data_byte )) != 0)
1690
                {
1691
                        eprom_buff[(start_addr-offset) % EPROM_MAX] = (unsigned char)data_byte;
1692
                        printf( "\n %04x = %02x ", start_addr, data_byte );
1693
                        start_addr++;
1694
                        lineptr += arglen;
1695
                }
1696
                mod_flag = TRUE;
1697
                return TRUE;
1698
        } else if( str_equal( cmdptr, "O", len ) ||
1699
                   str_equal( cmdptr, "offset", len ))
1700
        {
1701
                /***********************************************************
1702
                 *
1703
                 * Set Eprom base address offset
1704
                 */
1705
                if( (arglen = get_address( &cmdline[lineptr], &start_addr )) == 0 )
1706
                {
1707
                        /* No argument, display offset */
1708
                        printf( "\nBase address offset = %04x", offset );
1709
                        return TRUE;
1710
                }
1711
                offset = start_addr;
1712
                mod_flag = TRUE;
1713
                return TRUE;
1714
        } else if( str_equal( cmdptr, "H", len ) ||
1715
                   str_equal( cmdptr, "?", len ) ||
1716
                   str_equal( cmdptr, "help", len ))
1717
        {
1718
                /***********************************************************
1719
                 *
1720
                 * Display help menu
1721
                 */
1722
                display_help();
1723
                return TRUE;
1724
        } else if( str_equal( cmdptr, "Q", len ) ||
1725
                   str_equal( cmdptr, "quit", len ))
1726
        {
1727
                /***********************************************************
1728
                 *
1729
                 * Quit program - return FALSE
1730
                 */
1731
                printf( "\nQuitting program" );
1732
                return FALSE;
1733
        } else if( str_equal( cmdptr, "E", len ) ||
1734
                   str_equal( cmdptr, "echo", len ))
1735
        {
1736
                /***********************************************************
1737
                 *
1738
                 * Echo string to the console
1739
                 */
1740
                printf( "\n" );
1741
                while( (cmdline[lineptr] != '\0') && (cmdline[lineptr] != '\n'))
1742
                        printf( "%c", cmdline[lineptr++] );
1743
                printf( "\n" );
1744
                return TRUE;
1745
 
1746
        } else if( str_equal( cmdptr, "X", len ) ||
1747
                   str_equal( cmdptr, "execute", len ))
1748
        {
1749
                /***********************************************************
1750
                 *
1751
                 * Execute command script
1752
                 * We can onle do this if we are using stdin
1753
                 */
1754
                if( cmdfp != stdin )
1755
                {
1756
                        printf( "\nWe cannot nest redirected input " );
1757
                        return TRUE;
1758
                }
1759
 
1760
                arglen = get_filename( &cmdline[lineptr], &filename[0] );
1761
                if( arglen == 0 )
1762
                {
1763
                        printf( "\nFile name expected " );
1764
                        return TRUE;
1765
                }
1766
 
1767
                if( (cmdfp = fopen( &filename[0], "r" )) == NULL )
1768
                {
1769
                        printf( "\ncan't open auxillary input file '%s'", &filename[0] );
1770
                        /* we had better revert to the original command file */
1771
                        cmdfp = stdin;
1772
                        return TRUE;
1773
                }
1774
                /* every thing is ok, input is re-directed */
1775
                auxflag = TRUE;
1776
                return TRUE;
1777
        }
1778
    }
1779
        /***********************************************************
1780
         *
1781
         * Command not understood
1782
         */
1783
        printf( "\nUnrecognised command" );
1784
        return TRUE;
1785
}
1786
 
1787
/*
1788
 * epedit main program
1789
 */
1790
int main(int argc, char **argv)
1791
{
1792
 
1793
        auxflag = FALSE;
1794
        offset = 0;
1795
        format_type = BINARY;
1796
 
1797
        if(argc >= 2)
1798
        {
1799
                /* check for auxillary command file */
1800
                if((cmdfp = fopen( argv[1], "r" )) == NULL)
1801
                {
1802
                        printf(" can't open input on %s\n", argv[1] );
1803
                        printf("Usage: %s [command-file]\n",argv[0]);
1804
                        return 1;
1805
                }
1806
                auxflag = TRUE;
1807
        } else
1808
        {
1809
                /* no auxillary command file specified, use stdin */
1810
                cmdfp = stdin;
1811
        }
1812
 
1813
        eprom_top = 0;
1814
        printf( "*** EPROM Editor ***\n" );
1815
        printf( "type H/?/help for commands\n" );
1816
        do {
1817
                read_command( cmdbuff );
1818
        } while( parse_command( cmdbuff ) );
1819
        printf( "\n*** exit epedit ***\n" );
1820
        return 0;
1821
}
1822
 

powered by: WebSVN 2.1.0

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