/
bl08.c
1569 lines (1322 loc) · 37.9 KB
/
bl08.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
/*
File: bl08.c
Version: 1.0.0.1
Copyright (c) 2004,2008 Kustaa Nyholm
Copyright (c) 2008 Robert Larice (SWI return to MON, QY2 chip)
Copyright (c) 2010 Tormod Volden
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2.0 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// This code compiles cleanly with i686-apple-darwin8-gcc-4.0.1 with command:
// gcc -Wall -std=c99 -pedantic -o bl08 bl08.c
// and uses only POSIX standard headers so this should be pretty
// easy to port anywhere: Mac OS X, Linux , MinGW, Cygwin
//
// Having said that I'm pretty sure there is implicit assumptions that
// int is 32 bits, char is signed 8 bits
// I also expect that extending this code to handle S-records in the +2G range
// will uncover more implicit assumptions.
//
// cheers Kusti
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#if defined(__linux__)
# include <sys/wait.h>
# include <sys/ioctl.h>
#else
// Terrible hack here because POSIX says 'ioctl()' is in <stropts.h> but
// for example Mac OS X Tiger does not have this header, OTOH I think
// that <sys/ioctl.h> is not POSIX either so how do you write
// actual POSIX compliant code that compiles cleanly on POSIX...
extern int ioctl (int filedes, int command, ...);
// End of hack
#endif
#define PIDFILENAME "bl08PIDfile.temp"
//char* COM = "/dev/tty.usbserial-FTOXM3NX";
char* COM = "/dev/ttyS0";
typedef struct {
char* str;
int val;
} pair;
pair portPins[]= {
{"LE",TIOCM_LE},
{"DTR",TIOCM_DTR},
{"RTS",TIOCM_RTS},
{"ST",TIOCM_ST},
{"SR",TIOCM_SR},
{"CTS",TIOCM_CTS},
{"CAR",TIOCM_CAR},
{"CD",TIOCM_CD},
{"RNG",TIOCM_RNG},
{"RI",TIOCM_RI},
{"DSR",TIOCM_DSR}
};
int forcePins[sizeof(portPins)/sizeof(portPins[0])] = {0};
pair baudrates[]= {
// Some common, but non POSIX baudrates here
#ifdef B14400
{"14400",B14400},
#endif
#ifdef B57600
{"57600",B57600},
#endif
#ifdef B115200
{"115200",B115200},
#endif
#ifdef B230400
{"230400",B230400},
#endif
{"0",B0},
{"50",B50},
{"75",B75},
{"110",B110},
{"134",B134},
{"150",B150},
{"200",B200},
{"300",B300},
{"600",B600},
{"1200",B1200},
{"1800",B1800},
{"2400",B2400},
{"4800",B4800},
{"9600",B9600},
{"19200",B19200},
{"38400",B38400},
};
int baudRate=B9600;
int com;
char version[] = "1.0.0.1";
unsigned char image[ 0x10000 ]; // HC908 memory image
unsigned char scode[8]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
// EARRNG, CTRLBYT parameter
#define ERARRNG_PAGE_ERASE 0x00
#define ERARRNG_MASS_ERASE 0x40
// These will be initialized by setCPUtype()
int CPUSPEED; // 2 x Fbus freq, e.g. ext osc 16 MHz -> Fbus == 4 Mh => CPUSPEED==2
int RAM;
int FLASH; // Flash start address
int PUTBYTE; // Receive byte routine address
int GETBYTE; // Receive byte routine address
int RDVRRNG; // Read/verify flash routine address
int ERARRNG; // Erase flash routine address
int PRGRNGE; // Flash programming routine address
int FLBPR; // Flash block proctection register address
int MONRTN; // Monitor mode return jump address
int EADDR; // For FLBPR in Flash the mass erase must use FLBPR as the erase address
// These will be calculated
int MONDATA; // Flashing routines parameter block address (==RAM+8)
int CTRLBYT; // Address of flashing routine control variable (==MONDATA+0)
int CPUSPD; // Address of flashing routine cpu speed variable (==MONDATA+1)
int LADDR; // Address of flashing routine last address variable (==MONDATA+2)
int DATABUF; // Flashing routines data buffer address (==MONDATA+4)
int PAGESIZE; // Databuffer size
int WORKRAM; // Work storage needed for calling flashing routines
int WORKTOP; // Topmost work storage address
// HC908GZ16 Memory usage (note for some HC908 variants the ROM routines use memory starting from 0x80):
// 0x40 - 0x47 reserved for future ROM routine expansion needs
// 0x48 - 0x4B ROM routine parameters
// 0x4C - 0x6C ROM routine data buffer (64 bytes as used in this code)
// 0xAC - 0xFF Working storage for calling the ROM routines (about 17 bytes used)
int tickP1=15;
int tickP2=1023;
int verbose = 1;
int size = sizeof(image);
int useStdin=0;
int dumpStart=0;
int dumpSize=0;
char* dumpFormat="hex";
int eraseFlash=0;
int verify=0;
char* executeCode=NULL;
int pageErase=0;
int uploadOnly=0;
int terminalMode=0;
int connected=0;
int useFastProg=0;
int resetPulse=0;
int killPrevious=0;
int loadOnly=0;
void comErr(char *fmt, ...) {
char buf[ 500 ];
va_list va;
va_start(va, fmt);
vsnprintf(buf, sizeof(buf), fmt, va);
fprintf(stderr,"%s", buf);
perror(COM);
va_end(va);
abort();
}
void flsprintf(FILE* f, char *fmt, ...) {
char buf[ 500 ];
va_list va;
va_start(va, fmt);
vsnprintf(buf, sizeof(buf), fmt, va);
fprintf(f,"%s", buf);
fflush(f);
va_end(va);
}
void ioctlErrCheck(int e) {
if (e) {
flsprintf(stdout,"ioctl returned %d\n",e);
abort();
}
}
void setHandshake() {
int i,s;
ioctlErrCheck(ioctl(com, TIOCMGET, &s));
for (i=0; i<sizeof(forcePins)/sizeof(forcePins[0]); i++) {
int v=forcePins[i]-1;
if (v==1)
s &= ~portPins[i].val;
if (v==0)
s |= portPins[i].val;
}
ioctlErrCheck(ioctl(com, TIOCMSET, &s));
}
void initSerialPort() {
com = open(COM, O_RDWR | O_NOCTTY | O_NDELAY);
if (com <0)
comErr("Failed to open serial port\n");
fcntl(com, F_SETFL, 0);
struct termios opts;
tcgetattr(com, &opts);
opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
opts.c_cflag |= (CLOCAL | CREAD);
opts.c_cflag &= ~PARENB;
opts.c_cflag |= CSTOPB; // two stop bits
opts.c_cflag &= ~CSIZE;
opts.c_cflag |= CS8;
opts.c_oflag &= ~OPOST;
opts.c_iflag &= ~INPCK;
opts.c_iflag &= ~(IXON | IXOFF | IXANY);
opts.c_cc[ VMIN ] = 0;
opts.c_cc[ VTIME ] = 10;//0.1 sec
cfsetispeed(&opts, baudRate);
cfsetospeed(&opts, baudRate);
setHandshake();
if (tcsetattr(com, TCSANOW, &opts) != 0) {
perror(COM);
abort();
}
tcflush(com,TCIOFLUSH); // just in case some crap is the buffers
if (!terminalMode) {
char buf = -2;
while (read(com, &buf, 1)>0) {
if (verbose)
flsprintf(stderr,"Unexpected data from serial port: %02X\n",buf & 0xFF);
}
}
}
void putByte(int byte) {
char buf = byte;
if (verbose>3)
flsprintf(stdout,"TX: 0x%02X\n", byte);
int n = write(com, &buf, 1);
if (n != 1)
comErr("Serial port failed to send a byte, write returned %d\n", n);
}
int getByte() {
char buf;
int n = read(com, &buf, 1);
if (verbose>3)
flsprintf(stdout,n<1?"RX: fail\n":"RX: 0x%02X\n", buf & 0xFF);
if (n == 1)
return buf & 0xFF;
comErr("Serial port failed to receive a byte, read returned %d\n", n);
return -1; // never reached
}
// This reads away break 'character' from the serial line
void flushBreak() {
int i;
for (i=0; i<2; ++i) {
char buf;
int n = read(com, &buf, 1);
if (verbose>3)
flsprintf(stdout,n<1?"FL: nothing\n":"FL: 0x%02X\n", buf & 0xFF);
}
}
void sendByte(int byte) {
byte &= 0xFF;
putByte(byte);
char buf;
if (read(com, &buf, 1)!=1)
comErr("Loopback failed, nothing was received\n");
int rx=buf &0xFF;
if (byte != rx)
comErr("Loopback failed, sent 0x%02X, got 0x%02X\n", byte, rx);
rx = getByte();
if (byte != rx)
comErr("Target echo failed, sent 0x%02X, got 0x%02X\n", byte, rx);
}
void readMemory(int addr, int n,int tick) {
if (verbose>2)
flsprintf(stdout,"Read memory address %04X size %04X\n",addr,n);
unsigned char* p = &image[ addr ];
sendByte(0x4A); // Monitor mode READ command
sendByte(addr >> 8);
sendByte(addr & 0xFF);
*(p++) = getByte();
n--;
int tc=0;
while (n>0) {
sendByte(0x1A); // Monitor mode IREAD command
int b1 = getByte();
int b2 = getByte();
*(p++) = b1;
n--;
if (n > 0)
*(p++) = b2;
n--;
if (tick) {
tc++;
if ((tc & tickP1)==0)
flsprintf(stdout,".");
//if ((tc & tickP2)==0)
// flsprintf(stdout,"\n");
}
}
}
void writeMemory(int addr, int n, int tick) {
struct timespec tspec;
tspec.tv_sec=0;
tspec.tv_nsec=1000000; /* wait at least 1 ms (datasheet specifies 11 bit times) */
if (verbose>2)
flsprintf(stdout,"Write memory address %04X size %04X\n",addr,n);
unsigned char* p = &image[ addr ];
sendByte(0x49); // Monitor mode WRITE command
sendByte(addr >> 8);
sendByte(addr & 0xFF);
sendByte(*(p++));
if (verbose>3)
flsprintf(stdout,"Command delay\n");
nanosleep(&tspec,0);
int tc=1;
while (n>1) {
sendByte(0x19); // Monitor mode IWRITE command
sendByte(*(p++));
if (verbose>3)
flsprintf(stdout,"Command delay\n");
nanosleep(&tspec,0);
n -= 1;
if (tick) {
tc++;
if ((tc & tickP1)==0)
flsprintf(stdout,".");
//if ((tc & tickP2)==0)
// flsprintf(stdout,"\n");
}
}
}
void connectTarget() {
int j;
if (connected)
return;
flsprintf(stdout, "Security code: ");
for (j = 0; j<8; ++j) {
sendByte(scode[j]);
flsprintf(stdout, ".");
}
flsprintf(stdout, " ");
flushBreak();
readMemory(RAM, 1 , 0);
connected=1;
if ((image[ RAM ] & 0x40) == 0)
flsprintf(stdout,"failed\n");
else
flsprintf(stdout,"passed\n");
// in case FLBPR is RAM based we clear it first by just writing it
image[FLBPR]=0xFF;
writeMemory(FLBPR,1,0);
}
void dumpMemory(int addr, int n) {
unsigned char* p = &image[ addr ];
int i;
for (i = 0; i<n; ++i) {
if ((i&0xF) == 0)
flsprintf(stdout,"%04X ", addr+i);
flsprintf(stdout,"%02X ", *(p++) & 0xFF);
if ((i&0xF) == 7)
flsprintf(stdout," ");
if ((i&0xF) == 15)
flsprintf(stdout,"\n");
}
if ((i&0xF) != 0)
flsprintf(stdout,"\n");
}
void dumpMemorySrec(int addr, int size) {
unsigned char* p = &image[ addr ];
while (size>0) {
int n = size>16 ? 16 : size;
int bc=2+n+1;
flsprintf(stdout,"S1%02X%04X",bc,addr);
int s=(addr >> 8) + (addr & 0xFF) + bc;
int i;
for (i=0; i<n; ++i) {
int bty=*p & 0xFF;
s += bty;
flsprintf(stdout,"%02X",bty);
++p;
++addr;
}
size -= n;
flsprintf(stdout,"%02X\n",~s & 0xFF);
}
}
int readSP() {
if (verbose>2)
flsprintf(stdout,"Read Stack Pointer\n");
sendByte(0x0C); // Monitor mode READSP command
return (((getByte() << 8) | (getByte() & 0xFF)) - 1) & 0xFFFF;
}
int runFrom(int PC, int A, int CC, int HX) {
int SP=readSP();
if (verbose>2)
flsprintf(stdout,"Execute code PC=%04X A=%02X CC=%02X H:X=%04X SP=%04X\n",PC,A,CC,HX,SP);
image[ SP + 1 ] = HX >> 8;
image[ SP + 2 ] = CC;
image[ SP + 3 ] = A;
image[ SP + 4 ] = HX & 0xFF;
image[ SP + 5 ] = PC >> 8;
image[ SP + 6 ] = PC & 0xFF;
writeMemory(SP + 1 , 6 , 0);
sendByte(0x28); // Monitor mode RUN command
//struct timespec tspec;
//tspec.tv_sec=0;
//tspec.tv_nsec=5000000; /* wait at least 1 ms (datasheet specifies 11 bit times) */
//nanosleep(&tspec,0);
return SP;
}
int lastMon=-1;
int callMonitor(int mon, int ctrlbyt, int accu, int faddr, int laddr) {
int SP;
image[ CTRLBYT ] = ctrlbyt; // CTRLBYT BIT 6 = 1 = > mass erase
image[ CPUSPD ] = CPUSPEED; // CPUSPD = 16 MHz ext clock = > 4 MHz Fbus speed = > 8
if (verbose>3)
flsprintf(stdout,"CPUSPEED=%02X\n",CPUSPEED);
image[ LADDR ] = laddr>>8;
image[ LADDR+1 ] = laddr&0xFF;
writeMemory(MONDATA, 4, 0);
if (WORKRAM>0xFF) {
flsprintf(stderr,"Work RAM must be on zero page\n");
abort();
}
if (lastMon!=mon) {
// construct small HC908 code fragment to call the monitor function and return results
int i = WORKRAM;
image[ i++ ] = 0xCD; // JSR mon ; calls the monitor routine
image[ i++ ] = mon>>8;
image[ i++ ] = mon&0xFF;
image[ i++ ] = 0x83; // SWI ; return to monitor
if (WORKRAM>=WORKTOP) { // leave some stack space for monitor routines
flsprintf(stderr,"Not enough WORKRAM on target\n");
abort();
}
writeMemory(WORKRAM , i-WORKRAM , 0);
lastMon=mon;
}
// now execute the fragment
runFrom(WORKRAM, accu, 0x00, faddr);
// SWI drops into MON, which will send a BREAK
{ char buf;
if (read(com, &buf, 1) != 1)
comErr("ERROR: waiting for MON, nothing was received\n");
if (verbose>3)
flsprintf(stdout,"MON returned %02X\n", buf);
if(buf != 0)
comErr("ERROR: unexpected swi answer read %x\n", buf);
}
// BREAK seems to introduce a frame error on the FT230X, read once more to clear that
{ char buf;
if (read(com, &buf, 1) != 1)
comErr("ERROR: waiting for MON, nothing was received\n");
if (verbose>3)
flsprintf(stdout,"MON returned %02X\n", buf);
}
SP = readSP();
readMemory(SP + 1 , 6 , 0);
if (verbose>2)
flsprintf(stdout, "SP=%02x SP+1..6: hi(X)=%02x CC=%02x A=%02x lo(X)=%02x hi(PC)=%02x lo(PC)=%02x\n",
SP,
image[SP+1], // hi(X)
image[SP+2], // CC
image[SP+3], // A
image[SP+4], // lo(X)
image[SP+5], // hi(PC)
image[SP+6]); // lo(PC)
return ((image[SP+2] & 0xff) << 8) | (image[SP+3] & 0xff); // return condition codes and accu
}
int fastProg(int faddr,int n) {
static int n_addr;
static int last_n;
if (WORKRAM>0xFF) {
flsprintf(stderr,"Work RAM must be on zero page\n");
abort();
}
if (lastMon!=-1) {
int SP = readSP();
image[ CTRLBYT ] = 0; // CTRLBYT =0
image[ CPUSPD ] = CPUSPEED; // CPUSPD = 16 MHz ext clock = > 4 MHz Fbus speed = > 8
// construct small HC908 code fragment to call the monitor function and return results
int i = WORKRAM;
image[ i++ ] = 0x9D; // NOP / reserve space
image[ i++ ] = 0x9D; // NOP / reserve space
image[ i++ ] = 0x35; // STHX WORKRAM (dir)
image[ i++ ] = WORKRAM;
image[ i++ ] = 0x45; // LDHX #DATABUF
image[ i++ ] = DATABUF>>8;
image[ i++ ] = DATABUF&0xFF;
image[ i++ ] = 0xCD; // JSR GETBYTE ; calls the monitor routine
image[ i++ ] = GETBYTE>>8;
image[ i++ ] = GETBYTE&0xFF;
// image[ i++ ] = 0xFE9C>>8; ; // 0xFE9C = GET WITH echo
// image[ i++ ] = 0xFE9C&0xFF;
image[ i++ ] = 0xF7; // STA ,X
image[ i++ ] = 0x5C; // INCX
image[ i++ ] = 0xA3; // cpx #DATABUF+n
n_addr=i;
image[ i++ ] = 0; // place holder
image[ i++ ] = 0x25; // BLO *-10
image[ i++ ] = (-9)&0xFF;
image[ i++ ] = 0x55; // LDHX WORKRAM (dir)
image[ i++ ] = WORKRAM;
image[ i++ ] = 0xCD; // JSR PRGRNGE ; calls the monitor routine
image[ i++ ] = PRGRNGE>>8;
image[ i++ ] = PRGRNGE&0xFF;
image[ i++ ] = 0x45; // LDHX #SP+1 ; restore stack pointer
image[ i++ ] = (SP+1) >> 8;
image[ i++ ] = (SP+1) & 0xFF;
image[ i++ ] = 0x94; // TXS
image[ i++ ] = 0xCC; // JMP back to MON (this is the only way)
image[ i++ ] = MONRTN>>8;
image[ i++ ] = MONRTN&0xFF;
if (WORKRAM>=WORKTOP) { // leave some stack space for monitor routines
flsprintf(stderr,"Not enough WORKRAM on target\n");
abort();
}
writeMemory(WORKRAM , i-WORKRAM , 0);
//dumpMemorySrec(WORKRAM , i-WORKRAM);
lastMon=-1;
}
if (last_n!=n) {
image[ n_addr ] = DATABUF+n;
writeMemory(n_addr,1,0);
last_n=n;
}
int laddr = faddr+n-1;
image[ LADDR ] = laddr>>8;
image[ LADDR+1 ] = laddr&0xFF;
writeMemory(LADDR, 2, 0);
// now execute the fragment
runFrom(WORKRAM+2, 0x00, 0x00, faddr); // NOTE! to override flash security and erase FLBPR at be used as the erase address for mass erase
int i;
for (i=0; i<n; ++i)
putByte(image[faddr+i]);
for (i=0; i<n; ++i)
getByte();
readSP();
return 0;
}
int fastProg2(int faddr,int progn) {
if (WORKRAM>0xFF) {
flsprintf(stderr,"Work RAM must be on zero page\n");
abort();
}
int SP = readSP();
image[ CTRLBYT ] = 0; // CTRLBYT =0
image[ CPUSPD ] = CPUSPEED; // CPUSPD= 16 MHz ext => 4 MHz Fbus = > 8
// construct small HC908 code fragment to call the monitor function
int i = WORKRAM;
int j;
int FADDR=i;
image[ i++ ] = faddr>>8; // FADDR initial contents
image[ i++ ] = faddr&0xFF;
int PROGN=i;
image[ i++ ] = progn>>8; // PROGN initial contents
image[ i++ ] = progn&0xFF;
int start=i;
if (lastMon!=-1) {
image[ i++ ] = 0x55 ; // LDHX PROGN get bytes left to program
image[ i++ ] = PROGN;
image[ i++ ] = 0x27 ; // BEQ DONE branch if all done
int patchDone=i;
image[ i++ ] = 0 ;
image[ i++ ] = 0x65 ; // CPHX #PAGESIZE only page full at a time
image[ i++ ] = 0 ;
image[ i++ ] = PAGESIZE;
image[ i++ ] = 0x25 ; // BLO DOIT
image[ i++ ] = 2 ;
image[ i++ ] = 0xAE ; // LDX #PAGESIZE
image[ i++ ] = PAGESIZE;
image[ i++ ] = 0x9F ; // TXA
image[ i++ ] = 0x87 ; // PSHA
image[ i++ ] = 0xB6 ; // LDA PROGN+1
image[ i++ ] = PROGN+1;
image[ i++ ] = 0x9E; // SUB 1,SP
image[ i++ ] = 0xE0;
image[ i++ ] = 1;
image[ i++ ] = 0xB7 ; // STA PROGN+1
image[ i++ ] = PROGN+1;
image[ i++ ] = 0xB6 ; // LDA PROGN
image[ i++ ] = PROGN;
image[ i++ ] = 0xA2 ; // SBC #0
image[ i++ ] = 0 ;
image[ i++ ] = 0xB7 ; // STA PROGN
image[ i++ ] = PROGN;
image[ i++ ] = 0x9F ; // TXA
image[ i++ ] = 0x4A ; // DECA
image[ i++ ] = 0xBB ; // ADD FADDR+1
image[ i++ ] = FADDR+1;
image[ i++ ] = 0xB7 ; // STA LADDR+1
image[ i++ ] = LADDR+1;
image[ i++ ] = 0xB6 ; // LDA FADDR
image[ i++ ] = FADDR;
image[ i++ ] = 0xA9 ; // ADC #0
image[ i++ ] = 0 ;
image[ i++ ] = 0xB7 ; // STA LADDR
image[ i++ ] = LADDR;
image[ i++ ] = 0x45; // LDHX #DATABUF
image[ i++ ] = DATABUF>>8;
image[ i++ ] = DATABUF&0xFF;
image[ i++ ] = 0x86 ;// PULA
int getMore=i;
image[ i++ ] = 0x87 ; // PSHA
image[ i++ ] = 0xCD; // JSR GETBYTE
image[ i++ ] = GETBYTE>>8;
image[ i++ ] = GETBYTE&0xFF;
image[ i++ ] = 0xF7; // STA ,X
image[ i++ ] = 0x5C; // INCX
image[ i++ ] = 0x86 ; // PULA
image[ i++ ] = 0x4A ; // DECA
image[ i++ ] = 0x26 ; // BNE GETMORE
j=i;
image[ i++ ] = (getMore-(j+1))&0xFF ;
image[ i++ ] = 0x55 ; // LDHX FADDR
image[ i++ ] = FADDR;
image[ i++ ] = 0xCD; // JSR PRGRNGE
image[ i++ ] = PRGRNGE>>8;
image[ i++ ] = PRGRNGE&0xFF;
image[ i++ ] = 0x8B; // PSHH Annoyingly JB8 RDVRNG leaves H:X off by one
image[ i++ ] = 0x89; // PSHX compared to GZ16 necessiating this pus/pul sequence
image[ i++ ] = 0x55 ;// LDHX FADDR
image[ i++ ] = FADDR;
image[ i++ ] = 0xA6 ; // LDAA #1
image[ i++ ] = 1 ;
image[ i++ ] = 0xCD; // JSR RDVRRNG
image[ i++ ] = RDVRRNG>>8;
image[ i++ ] = RDVRRNG&0xFF;
image[ i++ ] = 0x88; // PULX
image[ i++ ] = 0x8A; // PULH
image[ i++ ] = 0x35 ; // STHX FADDR
image[ i++ ] = FADDR;
image[ i++ ] = 0xCD; // JSR PUTBYTE
image[ i++ ] = PUTBYTE>>8;
image[ i++ ] = PUTBYTE&0xFF;
image[ i++ ] = 0x20 ; // BRA start
j=i;
image[ i++ ] = (start-(j+1))&0xFF;
image[patchDone] = (i-(patchDone+1))&0xFF;
image[ i++ ] = 0x45; // LDHX #SP+1 ; restore stack pointer
image[ i++ ] = (SP+1) >> 8;
image[ i++ ] = (SP+1) & 0xFF;
image[ i++ ] = 0x94; // TXS
image[ i++ ] = 0xCC; // JMP back to MON (this is the only way)
image[ i++ ] = MONRTN>>8;
image[ i++ ] = MONRTN&0xFF;
if (i>=WORKTOP) { // leave some stack space for monitor routines
flsprintf(stderr,"Not enough WORKRAM on target\n");
abort();
}
writeMemory(WORKRAM , i-WORKRAM , 0);
lastMon=-1;
}
else
writeMemory(WORKRAM , 4 , 0);
runFrom(start, 0x00, 0x00, 0);
while (progn) {
int n = progn < PAGESIZE ? progn : PAGESIZE;
int sum=0;
for (i=0; i<n; ++i) {
putByte(image[faddr+i]);
}
for (i=0; i<n; ++i) {
int b=getByte();
//flsprintf(stderr,"%d %02X\n",i,b);
sum = (sum+b)&0xFF;
if (b != image[faddr+i])
flsprintf(stderr,"Program data transfer error, at %04X sent %02X got %02X\n",faddr+i,image[faddr+i],b);
}
int back=getByte();
//flsprintf(stderr,"%02X\n",back);
if (back != sum) {
flsprintf(stderr,"Program checksum failure, at %04X size %04X checksum calculated %02X received %02X\n",faddr,n,sum,back);
abort();
}
progn -= n;
faddr += n;
if (verbose)
flsprintf(stdout,".");
}
return 0;
}
void massErase() {
// NOTE! to override flash security and erase FLBPR must be used as the erase address for mass erase
if (verbose)
flsprintf(stdout,"Mass erase\n");
callMonitor(ERARRNG , ERARRNG_MASS_ERASE, 0, EADDR, 0);
}
void flashProgram(int addr,int size,int verify) {
if (addr < FLASH) {
flsprintf(stdout,"Programming address %04X below flash start address %04X\n",addr,FLASH);
abort();
}
if (useFastProg) {
if (verbose)
flsprintf(stdout,"Program %04X - %04X ",addr,addr+size-1);
fastProg2(addr,size);
if (verbose)
flsprintf(stdout,"\n");
}
else {
while (size>0) {
int n=size <= PAGESIZE ? size : PAGESIZE;
if (verbose)
flsprintf(stdout,"Program %04X - %04X ",addr,addr+n-1);
memcpy(&image[ DATABUF ] , &image[addr] , n);
writeMemory(DATABUF , n , verbose);
callMonitor(PRGRNGE, 0 , 0, addr , addr+n-1);
if (verify) {
int cca=callMonitor(RDVRRNG, 0 , 1 , addr , addr+n-1);
int sum=0;
int i;
for (i=0; i<n; ++i)
sum = (sum + image[addr+i]) & 0xFF;
int back= cca & 0xFF; // get Accu from target
if (sum != back) {
flsprintf(stderr,"Program checksum failure, at %04X size %04X checksum calculated %02X received %02X\n",addr,n,sum,back);
abort();
}
if (!(cca & 0x0100)) { // check Carry bit from target
flsprintf(stderr,"Verify failed\n");
abort();
}
if (verbose)
flsprintf(stdout,"OK");
}
if (verbose)
flsprintf(stdout,"\n");
addr += n;
size -= n;
}
}
}
int readSrec(int verbose,FILE* sf,unsigned char* image, int size, int base, int* ranges, int rn) {
if (verbose)
flsprintf(stdout,"Reading S-records\n");
memset(image,0xff,size);
char line[2+2+255*2+2+1]; // Sx + count + 255 bytes for data address & checksum + CR/LF +nul (in windows)
int amax=0;
int rc=0;
while (fgets(line,sizeof(line),sf)!=NULL) {
int o=0;
if (line[0]=='S') {
unsigned int n,a;
sscanf(line+2,"%2x",&n);
n--;
if (line[1]=='1') {
sscanf(line+4,"%4x",&a);
n=n-2;
o=8;
}
if (line[1]=='2') {
sscanf(line+4,"%6x",&a);
n=n-4;
o=10;
}
if (line[1]=='3') {
sscanf(line+4,"%8x",&a);
n=n-6;
o=12;
}
if (o!=0) {
int i,j;
if (ranges) {
for (i=0; i<rc; i+=2) {
int rlo=ranges[i];
int rhi=rlo+ranges[i+1];
if (!((a+n<=rlo) || (rhi<=a))) {
flsprintf(stderr,"Overlapping S-record ranges %04X,%04X and %0x4 %04X\n",rlo,rhi-rlo,a,n);
abort();
}
}
if (rc + 2 >= rn)
return -1;
ranges[rc]=a;
ranges[rc+1]=n;
rc += 2;
int cf=0;
do compact: {
for (i=0; i<rc; i+=2) {
for (j=i+2; j<rc; j+=2) {
cf=1;
if (ranges[i]+ranges[i+1]==ranges[j])
ranges[i+1] += ranges[j+1];
else if (ranges[i]==ranges[j]+ranges[j+1])
ranges[i]-=ranges[j+1];
else
cf=0;
if (cf) {
for (i=j+2; i<rc; i++)
ranges[i]=ranges[i+2];
rc-=2;
cf=0;
goto compact;
}
}
}
} while (cf);
}
for (i=0; i<n; ++i) {
unsigned int d;
sscanf(line+o+i*2,"%2x",&d);
if ( (a >= base) && (a < base+size)) {
image[ a - base ] = d;
a++;
amax = a>amax ? a : amax;
}
}
}
}
if (verbose>1)
flsprintf(stdout,">>> %s",line);
if (verbose && o==0)
flsprintf(stdout,"Line ignored: %s\n",line);
}
if (verbose) {
if (ranges) {
int i;
for (i=0; i<rc; i+=2)
flsprintf(stdout,"S-record data address %06X size %06X\n",ranges[i],ranges[i+1]);
}
flsprintf(stdout,"\n");
}
return rc;
}
void printHelp() {