-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemu68k.c
4308 lines (4048 loc) · 86.6 KB
/
emu68k.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*Custom Corewars for Motorola 68000*/
/* v1.0 */
/* Based on emu68k 68000 simulator*/
/* by James L. Antonakos */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//#include <malloc.h> replaced with stdlib.h
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include "68defs.h"
#include "srecord.h"
#include <time.h>
void loadX(int exp);
void loadN(int exp);
void loadZ(int exp);
void loadV(int exp);
void loadC(int exp);
void save_context(int cont_num); //save cpu context
void load_context(int cont_num); //load cpu context
void execute_single(); //Single instruction execution
struct ucode icode;
char isize, ops[32], tops[32];
int mw;
static int SRecord_Address_Lengths[] = {
4, // S0
4, // S1
6, // S2
8, // S3
8, // S4
4, // S5
6, // S6
8, // S7
6, // S8
4, // S9
};
/**
* Structure to hold warrior cpu context
*/
struct Cpucontext
{
unsigned long int data_reg[8]; //= {0,0,0,0,0,0,0,0};
unsigned long int address_reg[8]; // = {0,0,0,0,0,0,0,0xff0};
long int pcounter;
int sreg;
unsigned char wXflag, wNflag, wZflag, wVflag, wCflag;
};
struct Cpucontext contexts[100];
int main(int argc, char *argv[])
{
char com;
char close;
FILE *fp;
char fname2[20];
fname2[0] = '\0';
const char * fileread;
SRecord srec;
uint32_t staddress[100][100];
int Numwarr=0;
int wNum=0;
char exit_code;
int cycles;
printf("Custom Corewars V1.0, 26/4/2013\n");
init_data();
printf("Enter '?' for help\n");
// printf("Use 0x%04X for all DOS segment addresses.\n",FP_SEG(pbuf));
printf("Enter Number of warriors: \n");
scanf ("%d",&Numwarr);
printf("Enter Number of of execution cycles: \n");
scanf ("%d",&cycles);
while(wNum < Numwarr)
{
printf("Enter warrior name: \n");
// fname2 = getchar();
scanf ("%s",fname2);
printf("Warrior name is: \n",fname2);
int record_count = 0;
fp = fopen(fname2,"r");
while (Read_SRecord(&srec, fp) == SRECORD_OK)
{
//recordCount++;
//dataByteSum += srec.dataLen;
Print_SRecord(&srec);
printf("\n");
for (unsigned long int mData = 0; mData < srec.dataLen; mData++)
{
unsigned long int effA, myaddr;
myaddr = srec.address + mData;
// staddress[mData] = srec.address;
staddress[wNum][record_count] = srec.address;
// printf("starting pc is: %x \n",staddress[mData]);
// effA = myaddr & 0xffff;
effA = myaddr;
if (mData+1 < srec.dataLen)
// printf("0x%02X, ", srec->data[i]);
(pbuf)[effA] = srec.data[mData];
// printf("starting pc is: %x",srec.address);
}
// (pbuf)[ea] = srcbyte;
// (pbuf)[srec.address] = srcbyte;
// for (int mData = 0; mData < srec.dataLen; mData++)
// {
// if (mData+1 < srec.dataLen)
// (pbuf)[srec.address] = srec.data[mData];
// printf("0x%02X, ", srec.data[mData]);
// else
// printf("0x%02X", srec.data[mData]);
record_count++;
}
printf("starting pc is: %x \n",staddress[wNum][0]);
for(int loader = 0; loader < Numwarr; loader++)
{
contexts[loader].pcounter = staddress[loader][0] ;
}
wNum++;
}
// Cpucontext* contexts = malloc(sizeof (contexts) * Numwarr);
// Cpucontext contexts[Numwarr];
do
{
for (int running = 0; running < Numwarr; running++)
{
// Cpucontext contexts = {d_reg[reg]d_reg[reg]};
load_context(running);
execute_single();
save_context(running);
}
cycles--;
/* gets(cmd);
cptr = 0;
skipblank();
com = toupper(cmd[cptr]);
cptr++;
skipblank();
switch(com)
{
case 'A' : printf("Based on Motorola 68000 emulator by James L. Antonakos CC\n");
printf("email: antonakos_j@sunybroome.edu for questions/comments\n");
printf("http://www.sunybroome.edu/~antonakos_j for new version downloads\n");
break;
case 'B' : bkpt(); break;
case 'D' : disp_mem(); break;
case 'E' : enter(); break;
case 'F' : fill(); break;
case 'G' : go(); break;
case 'H' : hex_math(); break;
case '?' : show_help(); break;
case 'L' : load_hexfile(); break;
case 'Q' : break;
case 'R' : modify_reg(); break;
case 'T' : trace(); break;
case 'U' : unassemble(); break;
case '\n': break;
case '\0': break;
default : printf("Unknown command %s\n",cmd);
}
*/
} while((cycles > 0) && !stopem);
scanf ("%s",exit_code);
if(exit_code == 'Q')
return 0;
}
void int21h()
{
unsigned long int inregs;
srand ( time(NULL) );
inregs = rand();
d_reg[0] = (d_reg[0] & 0xffff0000) | inregs;
}
void save_context(int cont_num)
{
int reg;
for(reg = 0; reg <= 7; reg++)
{
contexts[cont_num].data_reg[reg] = d_reg[reg];
contexts[cont_num].address_reg[reg] = a_reg[reg];
}
contexts[cont_num].pcounter = pc;
contexts[cont_num].sreg = sr;
contexts[cont_num].wXflag = XF;
contexts[cont_num].wNflag = NF;
contexts[cont_num].wNflag = ZF;
contexts[cont_num].wZflag = VF;
contexts[cont_num].wCflag = CF;
}
void load_context(int cont_num)
{
int reg;
for(reg = 0; reg <= 7; reg++)
{
d_reg[reg] = contexts[cont_num].data_reg[reg];
a_reg[reg] = contexts[cont_num].address_reg[reg];
}
pc = contexts[cont_num].pcounter;
sr = contexts[cont_num].sreg;
Xflag = contexts[cont_num].wXflag;
Nflag = contexts[cont_num].wNflag;
Zflag = contexts[cont_num].wNflag;
Vflag = contexts[cont_num].wZflag;
Cflag = contexts[cont_num].wCflag;
}
void execute_single()
{
int num;
//pc = tohex(&cmd[cptr]) & 0xffffff;
// if ('\0' != cmd[cptr])
num = 1;
stopem = FALSE;
while((num > 0) && !stopem)
{
fetch(EX);
disp_reg();
num--;
/*
if (kbhit()) //2.27.98
{
kyb = getch();
if (kyb == 0x15)
break;
}
*/
}
}
void show_help()
{
printf("Author A\n");
printf("Breakpoint B number (1-4) address (0 to disable)\n");
printf("Dump D [address] [lines]\n");
printf("Fill F start-address stop-address pattern-byte\n");
printf("Enter E [address] (use <cr> to skip over a byte\n");
printf(" and any illegal key to exit)\n");
printf("Go G [address] , [break address 1] , [break address 2]\n");
printf(" Ex: G 8200 (begin execution at $8200)\n");
printf(" Ex: G 8200, 8212 (begin execution at $8200 with\n");
printf(" temporary breakpoint at $8212)\n");
printf(" Ex: G 8200, 8212, 8348 (begin execution at $8200 with\n");
printf(" temporary breakpoints at $8212 and $8348)\n");
printf(" Ex: G , 8212, 8348 (begin execution at current PC with)\n");
printf(" temporary breakpoints at $8212 and $8348)\n");
printf("Help ?\n");
printf("Hex H number1 number2\n");
printf("Load L filename\n");
printf("Quit Q\n");
printf("Register R [register]\n");
printf("Trace T [address] , [lines]\n");
printf(" Ex: T 400 (begin trace at $400)\n");
printf(" Ex: T 400,5 (trace 5 inst. starting at $400)\n");
printf(" Ex: T ,5 (trace next 5 inst. at current pc)\n");
printf("Unassemble U [address] , [lines]\n");
printf(" Ex: Same syntax as Trace command\n");
}
void skipblank()
{
while (' ' == cmd[cptr])
cptr++;
}
unsigned long int tohex(char hstr[])
{
unsigned long int number;
char digit;
int k;
number = 0;
k = 0;
while (('\0' != hstr[k])
&& (' ' != hstr[k]) //9.17.96
&& (',' != hstr[k])) //2.27.98
{
digit = toupper(hstr[k]);
if (isalpha(digit) != 0)
digit -= 7;
number <<= 4;
number |= (digit & 0xf);
k++;
cptr++; //9.17.96
}
return number;
}
int inrange(unsigned long int addr)
{
// if (((addr >= 0x1000) && (addr < 0x8000)) || (addr >= 0x9000))
// {
// printf("No memory supported at address %06lX.\n",dump_ptr);
// return FALSE;
//}
// else
return TRUE;
}
void hex_math()
{
unsigned long int h1,h2;
h1 = h2 = 0;
h1 = tohex(&cmd[cptr]);
skipblank();
h2 = tohex(&cmd[cptr]);
printf("Sum %08lX, Diff %08lX\n",h1+h2,h1-h2);
}
void disp_mem()
{
int k,endk,j;
if ('\0' != cmd[cptr])
dump_ptr = tohex(&cmd[cptr]) & 0xfffff0;
skipblank();
endk = 16;
if ('\0' != cmd[cptr])
endk = tohex(&cmd[cptr]) & 0xffff;
endk <<= 3;
k = 0;
printf("%06lX ",dump_ptr);
while ((k < endk) && inrange(dump_ptr))
{
readmem(WORD,dump_ptr);
printf("%04X ",memword);
dump_ptr += 2;
k++;
if (0 == dump_ptr % 16)
{
printf(" ");
dump_ptr -= 16;
for(j = 0; j < 16; j++)
{
readmem(BYTE,dump_ptr++);
if ((membyte >= 32) && (membyte <= 127))
printf("%c",membyte);
else
printf(".");
}
if (k != endk)
printf("\n%06lX ",dump_ptr);
}
}
printf("\n");
}
void disp_reg()
{
disp_reg_list('D');
disp_reg_list('A');
printf("PC: %06lX SR: %04X\n",pc,sr);
printf("Flags: X = %1d, N = %1d, Z = %1d, V = %1d, C = %1d\n",
XF,NF,ZF,VF,CF);
}
void disp_reg_list(char reg_type)
{
int reg;
for(reg = 0; reg <= 7; reg++)
{
printf("%c%1d: ",reg_type,reg);
(reg_type == 'D') ? printf("%08lX",d_reg[reg])
: printf("%08lX",a_reg[reg]);
printf(" ");
if(reg == 3)
printf("\n");
}
printf("\n");
}
void modify_reg(void)
{
unsigned char reg,num, huh;
char val[12];
if ('\0' == cmd[cptr])
disp_reg();
else
{
huh = FALSE;
reg = toupper(cmd[cptr]);
num = (cmd[cptr+1] - 0x30) & 0xff;
if ((reg != 'A') && (reg != 'D') && (reg != 'P')) huh = TRUE;
if (reg == 'P')
{
num = toupper(cmd[cptr+1]);
if (num != 'C') huh = TRUE;
else num = 0;
}
if (num > 7) huh = TRUE;
if (huh)
printf("Huh?\n");
else
{
switch(reg)
{
case 'A': printf("A%d: %08lX ? ",
num,a_reg[num]); break;
case 'D': printf("D%d: %08lX ? ",
num,d_reg[num]); break;
case 'P': printf("PC: %08lX ? ",
pc); break;
}
gets(val);
if (0 != strlen(val))
{
switch(reg)
{
case 'A': a_reg[num] = tohex(val); break;
case 'D': d_reg[num] = tohex(val); break;
case 'P': pc = tohex(val); break;
}
}
}
}
}
void fill()
{
unsigned long int startadd, endadd;
startadd = 0;
endadd = 0;
startadd = tohex(&cmd[cptr]) & 0xffffff;
skipblank();
endadd = tohex(&cmd[cptr]) & 0xffffff;
skipblank();
srcbyte = tohex(&cmd[cptr]) & 0xff;
while (startadd <= endadd)
writemem(BYTE,startadd++);
}
void enter()
{
unsigned char val;
char hexin[8];
unsigned long int enter_ptr;
int illeg;
enter_ptr = 0x400;
illeg = FALSE;
if ('\0' != cmd[cptr])
enter_ptr = tohex(&cmd[cptr]) & 0xffffff;
do
{
if (inrange(enter_ptr))
{
readmem(BYTE,enter_ptr);
val = membyte;
printf("%06lX %02X? ",enter_ptr,val);
gets(hexin);
if (0 != strlen(hexin))
{
illeg = isdigit(hexin[0]);
illeg |= ((toupper(hexin[0]) >= 'A') &&
(toupper(hexin[0]) <= 'F'));
illeg = !illeg;
if (!illeg)
{
srcbyte = tohex(hexin) & 0xff;
writemem(BYTE,enter_ptr);
}
}
enter_ptr++;
}
} while (inrange && !illeg);
}
void init_data()
{
unsigned count = 0xffff;
// struct SREGS segregs;
pc = 0;
loadX(1);
loadZ(0);
loadC(1);
loadN(0);
loadV(0);
ingo = FALSE;
stopem = FALSE;
dump_ptr = 0;
if( (pbuf = (unsigned char *)malloc( (size_t)count )) == NULL )
{
// count = _memmax();
count = 0xfff7;
// count = coreleft();
if( (pbuf = (unsigned char *)malloc( (size_t)count )) == NULL )
{
printf("Fatal: Cannot allocate ANY memory.\n");
exit(-1);
}
}
printf("%u (0x%04X) bytes allocated for emulator memory.\n",count,count);
// "at %04X:%04X.\n",count,FP_SEG(pbuf),FP_OFF(pbuf));
// segread(&segregs);
// printf("CS: %04X, DS: %04X, ES: %04X, SS: %04X\n",
// segregs.cs,segregs.ds,segregs.es,segregs.ss);
}
int daa(unsigned char *num1, unsigned char num2)
{
int CYout;
unsigned int bcd1,bcd2; //rewritten for ABCD fix 9.30.96
bcd1 = ((*num1 >> 4) & 0xf) * 10;
bcd1 += (*num1 & 0xf);
bcd2 = ((num2 >> 4) & 0xf) * 10;
bcd2 += (num2 & 0xf);
bcd1 += bcd2;
if (bcd1 > 99)
{
CYout = 1;
bcd1 -= 100;
}
else
CYout = 0;
*num1 = tobcd((unsigned char)(bcd1 & 0xff));
return CYout;
}
unsigned char tobcd(unsigned char num)
{
unsigned char val;
val = (num / 10) << 4;
val += num % 10;
return val;
}
unsigned char frombcd(unsigned char num)
{
unsigned char val;
val = (num >> 4) * 10;
val += num & 0xf;
return val;
}
void go()
{
int k,nobp,firstloop,fetched;
stopem = FALSE;
ingo = TRUE;
firstloop = TRUE;
tbp1 = 1;
tbp2 = 1;
fetched = FALSE;
if (',' == cmd[cptr])
cptr++;
else
if ('\0' != cmd[cptr])
pc = tohex(&cmd[cptr]) & 0xffffff;
skipblank();
if (',' == cmd[cptr])
cptr++;
skipblank();
if ('\0' != cmd[cptr])
tbp1 = tohex(&cmd[cptr]) & 0xffffff;
skipblank();
if (',' == cmd[cptr])
cptr++;
skipblank();
if ('\0' != cmd[cptr])
tbp2 = tohex(&cmd[cptr]) & 0xffffff;
while (inrange(pc) && !stopem)
{
if (!bpstat[0] && !bpstat[1] && !bpstat[2] && !bpstat[3]
&& (tbp1 == 1) && (tbp2 == 1))
{
fetch(EX);
fetched = TRUE;
}
else
{
nobp = -1;
for(k = 0; k < 4; k++)
if (bpstat[k] && (pc == bpadr[k]))
{
if (!firstloop)
nobp = k;
}
if (nobp == -1)
{
if (((tbp1 == pc) || (tbp2 == pc)) && fetched)
{
printf("\nTemporary breakpoint encountered at address $%08lX\n",pc);
tbp1 = 1;
tbp2 = 1;
stopem = TRUE;
}
else
{
fetch(EX);
fetched = TRUE;
}
}
else
{
printf("\nBreakpoint %d encountered at "
"address %08lX.\n",1+nobp,pc);
stopem = TRUE;
}
}
firstloop = FALSE;
}
ingo = FALSE;
}
void bkpt()
{
char bnum;
int k;
bnum = cmd[cptr] - '1';
if ((0 <= bnum) && (bnum <= 3))
{
cptr++;
skipblank();
if ('\0' != cmd[cptr])
bpadr[bnum] = tohex(&cmd[cptr]) & 0xffffff;
if (0 == bpadr[bnum])
{
bpstat[bnum] = 0;
printf("Breakpoint %d cleared.\n",1+bnum);
}
else
{
bpstat[bnum] = 1;
printf("Breakpoint %d active.\n",1+bnum);
}
}
else
if (cmd[cptr] == '\0')
{
for(k = 0; k < 4; k++)
{
printf("Breakpoint %d: ",1+k);
if (bpstat[k])
printf("%08lX\n",bpadr[k]);
else
printf("Not active\n");
}
}
else
printf("Error! Breakpoint number must be from 1 to 4.\n");
}
unsigned char getbyte(void)
{
char fbyte[3];
unsigned char dbyte;
fbyte[0] = getc(fptr);
printf("%c",fbyte[0]);
fbyte[1] = getc(fptr);
printf("%c",fbyte[1]);
fbyte[2] = '\0';
dbyte = tohex(fbyte) & 0xff;
chksum += dbyte;
}
/* Utility function to read an S-Record from a file */
int Read_SRecord(SRecord *srec, FILE *in) {
char recordBuff[SRECORD_RECORD_BUFF_SIZE];
/* A temporary buffer to hold ASCII hex encoded data, set to the maximum length we would ever need */
char hexBuff[SRECORD_MAX_ADDRESS_LEN+1];
int asciiAddressLen, asciiDataLen, dataOffset, fieldDataCount, i;
/* Check our record pointer and file pointer */
if (srec == NULL || in == NULL)
return SRECORD_ERROR_INVALID_ARGUMENTS;
if (fgets(recordBuff, SRECORD_RECORD_BUFF_SIZE, in) == NULL) {
/* In case we hit EOF, don't report a file error */
if (feof(in) != 0)
return SRECORD_ERROR_EOF;
else
return SRECORD_ERROR_FILE;
}
/* Null-terminate the string at the first sign of a \r or \n */
for (i = 0; i < (int)strlen(recordBuff); i++) {
if (recordBuff[i] == '\r' || recordBuff[i] == '\n') {
recordBuff[i] = 0;
break;
}
}
/* Check if we hit a newline */
if (strlen(recordBuff) == 0)
return SRECORD_ERROR_NEWLINE;
/* Size check for type and count fields */
if (strlen(recordBuff) < SRECORD_TYPE_LEN + SRECORD_COUNT_LEN)
return SRECORD_ERROR_INVALID_RECORD;
/* Check for the S-Record start code at the beginning of every record */
if (recordBuff[SRECORD_START_CODE_OFFSET] != SRECORD_START_CODE)
return SRECORD_ERROR_INVALID_RECORD;
/* Copy the ASCII hex encoding of the type field into hexBuff, convert it into a usable integer */
strncpy(hexBuff, recordBuff+SRECORD_TYPE_OFFSET, SRECORD_TYPE_LEN);
hexBuff[SRECORD_TYPE_LEN] = 0;
srec->type = strtol(hexBuff, (char **)NULL, 16);
/* Copy the ASCII hex encoding of the count field into hexBuff, convert it to a usable integer */
strncpy(hexBuff, recordBuff+SRECORD_COUNT_OFFSET, SRECORD_COUNT_LEN);
hexBuff[SRECORD_COUNT_LEN] = 0;
fieldDataCount = strtol(hexBuff, (char **)NULL, 16);
/* Check that our S-Record type is valid */
if (srec->type < SRECORD_TYPE_S0 || srec->type > SRECORD_TYPE_S9)
return SRECORD_ERROR_INVALID_RECORD;
/* Get the ASCII hex address length of this particular S-Record type */
asciiAddressLen = SRecord_Address_Lengths[srec->type];
/* Size check for address field */
if (strlen(recordBuff) < (unsigned int)(SRECORD_ADDRESS_OFFSET+asciiAddressLen))
return SRECORD_ERROR_INVALID_RECORD;
/* Copy the ASCII hex encoding of the count field into hexBuff, convert it to a usable integer */
strncpy(hexBuff, recordBuff+SRECORD_ADDRESS_OFFSET, asciiAddressLen);
hexBuff[asciiAddressLen] = 0;
srec->address = strtol(hexBuff, (char **)NULL, 16);
/* Compute the ASCII hex data length by subtracting the remaining field lengths from the S-Record
* count field (times 2 to account for the number of characters used in ASCII hex encoding) */
asciiDataLen = (fieldDataCount*2) - asciiAddressLen - SRECORD_CHECKSUM_LEN;
/* Bailout if we get an invalid data length */
if (asciiDataLen < 0 || asciiDataLen > SRECORD_MAX_DATA_LEN)
return SRECORD_ERROR_INVALID_RECORD;
/* Size check for final data field and checksum field */
if (strlen(recordBuff) < (unsigned int)(SRECORD_ADDRESS_OFFSET+asciiAddressLen+asciiDataLen+SRECORD_CHECKSUM_LEN))
return SRECORD_ERROR_INVALID_RECORD;
dataOffset = SRECORD_ADDRESS_OFFSET+asciiAddressLen;
/* Loop through each ASCII hex byte of the data field, pull it out into hexBuff,
* convert it and store the result in the data buffer of the S-Record */
for (i = 0; i < asciiDataLen/2; i++) {
/* Times two i because every byte is represented by two ASCII hex characters */
strncpy(hexBuff, recordBuff+dataOffset+2*i, SRECORD_ASCII_HEX_BYTE_LEN);
hexBuff[SRECORD_ASCII_HEX_BYTE_LEN] = 0;
srec->data[i] = strtol(hexBuff, (char **)NULL, 16);
}
/* Real data len is divided by two because every byte is represented by two ASCII hex characters */
srec->dataLen = asciiDataLen/2;
/* Copy out the checksum ASCII hex encoded byte, and convert it back to a usable integer */
strncpy(hexBuff, recordBuff+dataOffset+asciiDataLen, SRECORD_CHECKSUM_LEN);
hexBuff[SRECORD_CHECKSUM_LEN] = 0;
srec->checksum = strtol(hexBuff, (char **)NULL, 16);
if (srec->checksum != Checksum_SRecord(srec))
return SRECORD_ERROR_INVALID_RECORD;
return SRECORD_OK;
}
/* Utility function to print the information stored in an S-Record */
void Print_SRecord(const SRecord *srec) {
int i;
printf("S-Record Type: \t\tS%d\n", srec->type);
printf("S-Record Address: \t0x%2.8X\n", srec->address);
printf("S-Record Data: \t\t{");
for (i = 0; i < srec->dataLen; i++) {
if (i+1 < srec->dataLen)
printf("0x%02X, ", srec->data[i]);
else
printf("0x%02X", srec->data[i]);
}
printf("}\n");
printf("S-Record Checksum: \t0x%2.2X\n", srec->checksum);
}
uint8_t Checksum_SRecord(const SRecord *srec) {
uint8_t checksum;
int fieldDataCount, i;
/* Compute the record count, address and checksum lengths are halved because record count
* is the number of bytes left in the record, not the length of the ASCII hex representation */
fieldDataCount = SRecord_Address_Lengths[srec->type]/2 + srec->dataLen + SRECORD_CHECKSUM_LEN/2;
/* Add the count, address, and data fields together */
checksum = fieldDataCount;
/* Add each byte of the address individually */
checksum += (uint8_t)(srec->address & 0x000000FF);
checksum += (uint8_t)((srec->address & 0x0000FF00) >> 8);
checksum += (uint8_t)((srec->address & 0x00FF0000) >> 16);
checksum += (uint8_t)((srec->address & 0xFF000000) >> 24);
for (i = 0; i < srec->dataLen; i++)
checksum += srec->data[i];
/* One's complement the checksum */
checksum = ~checksum;
return checksum;
}
void load_hexfile()
{
int k;
unsigned char data, length, chkerr;
unsigned long int adr;
// FILE* fp;
// char fname2[20];
//make fptr NULL
fptr = NULL;
// fp = NULL;
chkerr = FALSE;
fname[0] = '\0';
// fname2[0] = '\0';
strcat(fname,&cmd[cptr]);
// strcat(fname2,&cmd[cptr]);
strcat(fname,".hex");
fptr = fopen(fname,"r");
// fp = fopen(fname2,"r");
// SRecord srec;
// uint32_t staddress[100];
if (fptr == NULL)
{
printf("Error opening %s\n",fname);
chkerr = TRUE;
}
else
{
fchr = getc(fptr);
while (fchr != EOF)
{
printf("%c",fchr);
if (fchr == 'S')
{
fchr = getc(fptr);
printf("%c",fchr);
switch(fchr)
{
case '0' : fchr = getc(fptr); // 3.1.98
break;
case '1' :
{
chksum = 0;
length = getbyte();
length -= 3;
// printf("sourcebyte is %d\n",length); //check length
adr = getbyte();
adr <<= 8;
data = getbyte();
adr = adr | data;
while (length) //was a do-loop
{ //0-length Srecord 8.26.96
srcbyte = getbyte();
// printf("sourcebyte is %d",adr); //testing if anything is written
writemem(BYTE,adr);
adr++;
length--;
};
getbyte(); /* read checksum */
if (chksum != 0xff)
{
printf("\nChecksum error!\n");
chkerr = TRUE;
}
fchr = getc(fptr);
/*
for (int wip = 0; wip < srec.dataLen; wip++)
{
if (wip+1 < srec.dataLen)
srcbyte = srec.data[wip];
adr = staddress[wip];
adr <<= 8;
printf("addr is 0x%02X,\n ", adr);
writemem(BYTE,adr);
}
*/
break;
}
case '9' :
{
length = getbyte();
length -= 3;
adr = getbyte();
adr <<= 8;
data = getbyte();
pc = adr | data;
unaspc = pc; // 3.1.98
dump_ptr = pc; // 3.3.98
fchr = getc(fptr);
while (fchr != EOF)
{
printf("%c",fchr);
fchr = getc(fptr);
}
printf("Starting address: %04X\n", pc & 0xffff);
// long int loc = (pc & 0xffff);
long int loc = (pc);
printf("PC Location: %04X\n", loc);
break;
}
}
}
else
fchr = getc(fptr);
}
fclose(fptr);
}
if (!chkerr)
printf("%s loaded into emulator memory.\n",fname);
}
void pushAn()
{
int k;
for(k = 0; k < 8; k++)
tempAn[k] = a_reg[k];
}