summaryrefslogblamecommitdiffstats
path: root/progP16.c
blob: be9f5a02a37c5348f63df15ba9da8f837481fca7 (plain) (tree)
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
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155

                                                                                             
                                           

































































































































































































































































































































































































































                                                                                   
                            






                                                                                                           
                                    



                                                                                                      
                                    

                 

                                                                






















































































































































































































































































                                                                                                                                          
                                                 










































































































































































































































































































































                                                                                                                                      
                                                 
































































































                                                                                                                                           
                           
































































































































































































































































                                                                                                                                         
                                                 




































































































































                                                                                                                                                                    
                                     






































































































































































































































































































































































































                                                                                                                                                                                                  
                                     










































































































































































































































































































































































                                                                                                                                                                                                  
                                     













































































































































































































































































































































                                                                                                                                                                                                  
                                     





















































































































































































































































































































                                                                                                                                                                                                  
                                     






























































































































































































































































































































































































                                                                                                                                                                                                  
                                     




















































































































































































































































































































































                                                                                                                                                                                                  
                                     

















































































































































































































































































































































































                                                                                                                                                                                                  
                                     


























































































































































































































































































                                                                                                                                                                                                  
                                     





























































































































































































































































































































































                                                                                                                                                                                                  
                                     






































































































































































































































































                                                                                                                                                                                                  
                                     









































































































































































































































































                                                                                                                                                                                                  
                                     

































































































































































































































































































































                                                                                                                                                                                                  
                                     

















































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                  
                                     





























































































































                                                                                                                                                                               
                  






































































































































































































































































                                                                                                                                                                                                  
/**
 * \file progP16.c - algorithms to program the PIC16 (14 bit word) family of microcontrollers
 * Copyright (C) 2009-2023 Alberto Maccioni
 *
 * 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 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 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 USA
 * or see <http://www.gnu.org/licenses/>
 */

#include "common.h"

struct ID16{
	int id;
	char *device;
	int revmask;
} PIC16LIST[]={
	{0x00A>>1,"16F72 rev%d\r\n",0x1F},			//00 0000 101x xxxx
	{0x046>>1,"12F683 rev%d\r\n",0x1F},			//00 0100 011x xxxx
	{0x04A>>1,"16F685 rev%d\r\n",0x1F},			//00 0100 101x xxxx
	{0x04C>>1,"16F818 rev%d\r\n",0x1F},			//00 0100 110x xxxx
	{0x04E>>1,"16F819 rev%d\r\n",0x1F},			//00 0100 111x xxxx
	{0x056>>1,"16F84A rev%d\r\n",0x1F}, 		//00 0101 011x xxxx
	{0x060>>1,"16F73 rev%d\r\n",0x1F},  		//00 0110 000x xxxx
	{0x062>>1,"16F74 rev%d\r\n",0x1F}, 	 		//00 0110 001x xxxx
	{0x064>>1,"16F76 rev%d\r\n",0x1F},			//00 0110 010x xxxx
	{0x066>>1,"16F77 rev%d\r\n",0x1F},			//00 0110 011x xxxx
	{0x072>>1,"16F87 rev%d\r\n",0x1F},			//00 0111 001x xxxx
	{0x076>>1,"16F88 rev%d\r\n",0x1F},			//00 0111 011x xxxx
	{0x07A>>1,"16F627 rev%d\r\n",0x1F},			//00 0111 101x xxxx
	{0x07C>>1,"16F628 rev%d\r\n",0x1F},			//00 0111 110x xxxx
	{0x08E>>1,"16F872 rev%d\r\n",0x1F},			//00 1000 111x xxxx
	{0x092>>1,"16F874 rev%d\r\n",0x1F},			//00 1001 001x xxxx
	{0x096>>1,"16F873 rev%d\r\n",0x1F},			//00 1001 011x xxxx
	{0x09A>>1,"16F877 rev%d\r\n",0x1F},			//00 1001 101x xxxx
	{0x09E>>1,"16F876 rev%d\r\n",0x1F},			//00 1001 111x xxxx
	{0x0BA>>1,"16F737 rev%d\r\n",0x1F},			//00 1011 101x xxxx
	{0x0BE>>1,"16F747 rev%d\r\n",0x1F},			//00 1011 111x xxxx
	{0x0D0>>1,"16F870 rev%d\r\n",0x1F},			//00 1101 000x xxxx
	{0x0D2>>1,"16F871 rev%d\r\n",0x1F},			//00 1101 001x xxxx
	{0x0DE>>1,"16F777 rev%d\r\n",0x1F},			//00 1101 111x xxxx
	{0x0E0>>1,"16F876A rev%d\r\n",0xF},			//00 1110 0000 xxxx
	{0x0E2>>1,"16F877A rev%d\r\n",0xF},			//00 1110 0010 xxxx
	{0x0E4>>1,"16F873A rev%d\r\n",0xF},			//00 1110 0100 xxxx
	{0x0E6>>1,"16F874A rev%d\r\n",0xF},			//00 1110 0110 xxxx
	{0x0EA>>1,"16F767 rev%d\r\n",0x1F},			//00 1110 101x xxxx
	{0x0F8>>1,"12F629 rev%d\r\n",0x1F},			//00 1111 100x xxxx
	{0x0FA>>1,"12F635 rev%d\r\n",0x1F},			//00 1111 101x xxxx
	{0x0FC>>1,"12F675 rev%d\r\n",0x1F},			//00 1111 110x xxxx
	{0x104>>1,"16F627A rev%d\r\n",0x1F},		//01 0000 010x xxxx
	{0x106>>1,"16F628A rev%d\r\n",0x1F},		//01 0000 011x xxxx
	{0x108>>1,"16F684 rev%d\r\n",0x1F},			//01 0000 100x xxxx
	{0x110>>1,"16F648A rev%d\r\n",0x1F},		//01 0001 000x xxxx
	{0x10A>>1,"16F636-639 rev%d\r\n",0x1F},		//01 0000 101x xxxx
	{0x10C>>1,"16F630 rev%d\r\n",0x1F},			//01 0000 110x xxxx
	{0x10E>>1,"16F676 rev%d\r\n",0x1F},			//01 0000 111x xxxx
	{0x114>>1,"16F716 rev%d\r\n",0x1F},			//01 0001 010x xxxx
	{0x118>>1,"16F688 rev%d\r\n",0x1F},			//01 0001 100x xxxx
	{0x120>>1,"16F785 rev%d\r\n",0x1F},			//01 0010 000x xxxx
	{0x122>>1,"16HV785 rev%d\r\n",0x1F},		//01 0010 001x xxxx
	{0x124>>1,"16F616 rev%d\r\n",0x1F},			//01 0010 010x xxxx
	{0x126>>1,"16HV616 rev%d\r\n",0x1F},		//01 0010 011x xxxx
	{0x132>>1,"16F687 rev%d\r\n",0x1F},			//01 0011 001x xxxx
	{0x134>>1,"16F689 rev%d\r\n",0x1F},			//01 0011 010x xxxx
	{0x138>>1,"16F917 rev%d\r\n",0xF},			//01 0011 1000 xxxx
	{0x13A>>1,"16F916 rev%d\r\n",0xF},			//01 0011 1010 xxxx
	{0x13C>>1,"16F914 rev%d\r\n",0xF},			//01 0011 1100 xxxx
	{0x13E>>1,"16F913 rev%d\r\n",0xF},			//01 0011 1110 xxxx
	{0x140>>1,"16F690 rev%d\r\n",0x1F},			//01 0100 000x xxxx
	{0x142>>1,"16F631 rev%d\r\n",0x1F},			//01 0100 001x xxxx
	{0x144>>1,"16F677 rev%d\r\n",0x1F},			//01 0100 010x xxxx
	{0x146>>1,"16F946 rev%d\r\n",0xF},			//01 0100 0110 xxxx
	{0x148>>1,"16F1847 rev%d\r\n",0x1F},		//01 0100 100x xxxx
	{0x14A>>1,"16LF1847 rev%d\r\n",0x1F},		//01 0100 101x xxxx
	{0x150>>1,"12F752 rev%d\r\n",0x1F},			//01 0101 000x xxxx
	{0x152>>1,"12HV752 rev%d\r\n",0x1F},		//01 0101 001x xxxx
	{0x158>>1,"16F1526 rev%d\r\n",0x1F},		//01 0101 100x xxxx
	{0x15A>>1,"16F1527 rev%d\r\n",0x1F},		//01 0101 101x xxxx
	{0x15C>>1,"16LF1526 rev%d\r\n",0x1F},		//01 0101 110x xxxx
	{0x15E>>1,"16LF1527 rev%d\r\n",0x1F},		//01 0101 111x xxxx
	{0x164>>1,"16F1513 rev%d\r\n",0x1F},		//01 0110 010x xxxx
	{0x168>>1,"16F1516 rev%d\r\n",0x1F},		//01 0110 100x xxxx
	{0x16A>>1,"16F1517 rev%d\r\n",0x1F},		//01 0110 101x xxxx
	{0x16C>>1,"16F1518 rev%d\r\n",0x1F},		//01 0110 110x xxxx
	{0x16E>>1,"16F1519 rev%d\r\n",0x1F},		//01 0110 111x xxxx
	{0x170>>1,"16F1512 rev%d\r\n",0x1F},		//01 0111 000x xxxx
	{0x172>>1,"16LF1512 rev%d\r\n",0x1F},		//01 0111 001x xxxx
	{0x174>>1,"16LF1513 rev%d\r\n",0x1F},		//01 0111 010x xxxx
	{0x178>>1,"16LF1516 rev%d\r\n",0x1F},		//01 0111 100x xxxx
	{0x17A>>1,"16LF1517 rev%d\r\n",0x1F},		//01 0111 101x xxxx
	{0x17C>>1,"16LF1518 rev%d\r\n",0x1F},		//01 0111 110x xxxx
	{0x17E>>1,"16LF1519 rev%d\r\n",0x1F},		//01 0111 111x xxxx
	{0x180>>1,"16F727 rev%d\r\n",0x1F},			//01 1000 000x xxxx
	{0x182>>1,"16F726 rev%d\r\n",0x1F},			//01 1000 001x xxxx
	{0x184>>1,"16F724 rev%d\r\n",0x1F},			//01 1000 010x xxxx
	{0x186>>1,"16F723 rev%d\r\n",0x1F},			//01 1000 011x xxxx
	{0x188>>1,"16F722 rev%d\r\n",0x1F},			//01 1000 100x xxxx
	{0x190>>1,"16LF727 rev%d\r\n",0x1F},		//01 1001 000x xxxx
	{0x192>>1,"16LF726 rev%d\r\n",0x1F},		//01 1001 001x xxxx
	{0x194>>1,"16LF724 rev%d\r\n",0x1F},		//01 1001 010x xxxx
	{0x196>>1,"16LF723 rev%d\r\n",0x1F},		//01 1001 011x xxxx
	{0x198>>1,"16LF722 rev%d\r\n",0x1F},		//01 1001 100x xxxx
	{0x1AC>>1,"16F707 rev%d\r\n",0x1F},			//01 1010 110x xxxx
	{0x1AE>>1,"16LF707 rev%d\r\n",0x1F},		//01 1010 111x xxxx
	{0x1B0>>1,"16F723A rev%d\r\n",0x1F},		//01 1011 000x xxxx
	{0x1B2>>1,"16F722A rev%d\r\n",0x1F},		//01 1011 001x xxxx
	{0x1B4>>1,"16LF723A rev%d\r\n",0x1F},		//01 1011 010x xxxx
	{0x1B6>>1,"16LF722A rev%d\r\n",0x1F},		//01 1011 011x xxxx
	{0x1B8>>1,"12F1840 rev%d\r\n",0x1F},		//01 1011 100x xxxx
	{0x1BA>>1,"12LF1840 rev%d\r\n",0x1F},		//01 1011 101x xxxx
	{0x1C0>>1,"16F720 rev%d\r\n",0x1F},			//01 1100 000x xxxx
	{0x1C2>>1,"16F721 rev%d\r\n",0x1F},			//01 1100 001x xxxx
	{0x1C4>>1,"16LF720 rev%d\r\n",0x1F},		//01 1100 010x xxxx
	{0x1C6>>1,"16LF721 rev%d\r\n",0x1F},		//01 1100 011x xxxx
	{0x200>>1,"16F882 rev%d\r\n",0x1F},			//10 0000 000x xxxx
	{0x202>>1,"16F883 rev%d\r\n",0x1F},			//10 0000 001x xxxx
	{0x204>>1,"16F884 rev%d\r\n",0x1F},			//10 0000 010x xxxx
	{0x206>>1,"16F886 rev%d\r\n",0x1F},			//10 0000 011x xxxx
	{0x208>>1,"16F887 rev%d\r\n",0x1F},			//10 0000 100x xxxx
	{0x218>>1,"12F615 rev%d\r\n",0x1F},			//10 0001 100x xxxx
	{0x21A>>1,"12HV615 rev%d\r\n",0x1F},		//10 0001 101x xxxx
	{0x224>>1,"12F609 rev%d\r\n",0x1F},			//10 0010 010x xxxx
	{0x226>>1,"16F610 rev%d\r\n",0x1F},			//10 0010 011x xxxx
	{0x228>>1,"12HV609 rev%d\r\n",0x1F},		//10 0010 100x xxxx
	{0x22A>>1,"16HV610 rev%d\r\n",0x1F},		//10 0010 101x xxxx
	{0x232>>1,"16F1933 rev%d\r\n",0x1F},		//10 0011 001x xxxx
	{0x234>>1,"16F1934 rev%d\r\n",0x1F},		//10 0011 010x xxxx
	{0x236>>1,"16F1936 rev%d\r\n",0x1F},		//10 0011 011x xxxx
	{0x238>>1,"16F1937 rev%d\r\n",0x1F},		//10 0011 100x xxxx
	{0x23A>>1,"16F1938 rev%d\r\n",0x1F},		//10 0011 101x xxxx
	{0x23C>>1,"16F1939 rev%d\r\n",0x1F},		//10 0011 110x xxxx
	{0x242>>1,"16LF1933 rev%d\r\n",0x1F},		//10 0100 001x xxxx
	{0x244>>1,"16LF1934 rev%d\r\n",0x1F},		//10 0100 010x xxxx
	{0x246>>1,"16LF1936 rev%d\r\n",0x1F},		//10 0100 011x xxxx
	{0x248>>1,"16LF1937 rev%d\r\n",0x1F},		//10 0100 100x xxxx
	{0x24A>>1,"16LF1938 rev%d\r\n",0x1F},		//10 0100 101x xxxx
	{0x24C>>1,"16LF1939 rev%d\r\n",0x1F},		//10 0100 110x xxxx
	{0x250>>1,"16F1946 rev%d\r\n",0x1F},		//10 0101 000x xxxx
	{0x252>>1,"16F1947 rev%d\r\n",0x1F},		//10 0101 001x xxxx
	{0x258>>1,"16LF1946 rev%d\r\n",0x1F},		//10 0101 100x xxxx
	{0x25A>>1,"16LF1947 rev%d\r\n",0x1F},		//10 0101 101x xxxx
	{0x270>>1,"12F1822 rev%d\r\n",0x1F},		//10 0111 000x xxxx
	{0x272>>1,"16F1823 rev%d\r\n",0x1F},		//10 0111 001x xxxx
	{0x274>>1,"16F1824 rev%d\r\n",0x1F},		//10 0111 010x xxxx
	{0x276>>1,"16F1825 rev%d\r\n",0x1F},		//10 0111 011x xxxx
	{0x278>>1,"16F1826 rev%d\r\n",0x1F},		//10 0111 100x xxxx
	{0x27A>>1,"16F1827 rev%d\r\n",0x1F},		//10 0111 101x xxxx
	{0x280>>1,"12LF1822 rev%d\r\n",0x1F},		//10 1000 000x xxxx
	{0x282>>1,"16LF1823 rev%d\r\n",0x1F},		//10 1000 001x xxxx
	{0x284>>1,"16LF1824 rev%d\r\n",0x1F},		//10 1000 010x xxxx
	{0x286>>1,"16LF1825 rev%d\r\n",0x1F},		//10 1000 011x xxxx
	{0x288>>1,"16LF1826 rev%d\r\n",0x1F},		//10 1000 100x xxxx
	{0x28A>>1,"16LF1827 rev%d\r\n",0x1F},		//10 1000 101x xxxx
	{0x298>>1,"10F322 rev%d\r\n",0x1F},			//10 1001 100x xxxx
	{0x29A>>1,"10F320 rev%d\r\n",0x1F},			//10 1001 101x xxxx
	{0x29C>>1,"10LF322 rev%d\r\n",0x1F},		//10 1001 110x xxxx
	{0x29E>>1,"10LF320 rev%d\r\n",0x1F},		//10 1001 111x xxxx
	{0x2A0>>1,"16F1782 rev%d\r\n",0x1F},		//10 1010 000x xxxx
	{0x2A2>>1,"16F1783 rev%d\r\n",0x1F},		//10 1010 001x xxxx
	{0x2A4>>1,"16F1784 rev%d\r\n",0x1F},		//10 1010 010x xxxx
	{0x2A6>>1,"16F1786 rev%d\r\n",0x1F},		//10 1010 011x xxxx
	{0x2A8>>1,"16F1787 rev%d\r\n",0x1F},		//10 1010 100x xxxx
	{0x2AA>>1,"16LF1782 rev%d\r\n",0x1F},		//10 1010 101x xxxx
	{0x2AC>>1,"16LF1783 rev%d\r\n",0x1F},		//10 1010 110x xxxx
	{0x2AE>>1,"16LF1784 rev%d\r\n",0x1F},		//10 1010 111x xxxx
	{0x2B0>>1,"16LF1786 rev%d\r\n",0x1F},		//10 1011 000x xxxx
	{0x2B2>>1,"16LF1787 rev%d\r\n",0x1F},		//10 1011 001x xxxx
	{0x2C0>>1,"16LF1903 rev%d\r\n",0x1F},		//10 1100 000x xxxx
	{0x2C2>>1,"16LF1902 rev%d\r\n",0x1F},		//10 1100 001x xxxx
	{0x2C4>>1,"16LF1907 rev%d\r\n",0x1F},		//10 1100 010x xxxx
	{0x2C6>>1,"16LF1906 rev%d\r\n",0x1F},		//10 1100 011x xxxx
	{0x2C8>>1,"16LF1904 rev%d\r\n",0x1F},		//10 1100 100x xxxx
	{0x2CC>>1,"12F1501 rev%d\r\n",0x1F},		//10 1100 110x xxxx
	{0x2CE>>1,"16F1503 rev%d\r\n",0x1F},		//10 1100 111x xxxx
	{0x2D0>>1,"16F1507 rev%d\r\n",0x1F},		//10 1101 000x xxxx
	{0x2D2>>1,"16F1508 rev%d\r\n",0x1F},		//10 1101 001x xxxx
	{0x2D4>>1,"16F1509 rev%d\r\n",0x1F},		//10 1101 010x xxxx
	{0x2D8>>1,"12LF1501 rev%d\r\n",0x1F},		//10 1101 100x xxxx
	{0x2DA>>1,"16LF1503 rev%d\r\n",0x1F},		//10 1101 101x xxxx
	{0x2DC>>1,"16LF1507 rev%d\r\n",0x1F},		//10 1101 110x xxxx
	{0x2DE>>1,"16LF1508 rev%d\r\n",0x1F},		//10 1101 111x xxxx
	{0x2E0>>1,"16LF1509 rev%d\r\n",0x1F},		//10 1110 000x xxxx
	{0x2F0>>1,"16LF1554 rev%d\r\n",0x1F},		//10 1111 000x xxxx
	{0x2F2>>1,"16LF1559 rev%d\r\n",0x1F},		//10 1111 001x xxxx
// New devices with DevID & DevRev in different locations:
	{0x3000,"16F1574\r\n",0},
	{0x3001,"16F1575\r\n",0},
	{0x3002,"16F1578\r\n",0},
	{0x3003,"16F1579\r\n",0},
	{0x3004,"16LF1574\r\n",0},
	{0x3005,"16LF1575\r\n",0},
	{0x3006,"16LF1578\r\n",0},
	{0x3007,"16LF1579\r\n",0},
	{0x3020,"16F1454\r\n",0},
	{0x3021,"16F1455\r\n",0},
	{0x3023,"16F1459\r\n",0},
	{0x3024,"16LF1454\r\n",0},
	{0x3025,"16LF1455\r\n",0},
	{0x3027,"16LF1459\r\n",0},
	{0x302A,"16F1789\r\n",0},
	{0x302B,"16F1788\r\n",0},
	{0x302C,"16LF1789\r\n",0},
	{0x302D,"16LF1788\r\n",0},
	{0x3030,"16F753\r\n",0},
	{0x3031,"16HV753\r\n",0},
	{0x303A,"16F18324\r\n",0},
	{0x303B,"16F18344\r\n",0},
	{0x303C,"16LF18324\r\n",0},
	{0x303D,"16LF18344\r\n",0},
	{0x303E,"16F18325\r\n",0},
	{0x303F,"16F18345\r\n",0},
	{0x3040,"16LF18325\r\n",0},
	{0x3041,"16LF18345\r\n",0},
	{0x3042,"16F1708\r\n",0},
	{0x3043,"16F1704\r\n",0},
	{0x3044,"16LF1708\r\n",0},
	{0x3045,"16LF1704\r\n",0},
	{0x3048,"16F1716\r\n",0},
	{0x3049,"16F1713\r\n",0},
	{0x304A,"16LF1716\r\n",0},
	{0x304B,"16LF1713\r\n",0},
	{0x304C,"16F1613\r\n",0},
	{0x304D,"16LF1613\r\n",0},
	{0x3050,"12F1572\r\n",0},
	{0x3051,"12F1571\r\n",0},
	{0x3052,"12LF1572\r\n",0},
	{0x3053,"12LF1571\r\n",0},
	{0x3054,"16F1709\r\n",0},
	{0x3055,"16F1705\r\n",0},
	{0x3056,"16LF1709\r\n",0},
	{0x3057,"16LF1705\r\n",0},
	{0x3058,"12F1612\r\n",0},
	{0x3059,"12LF1612\r\n",0},
	{0x305A,"16F1719\r\n",0},
	{0x305B,"16F1718\r\n",0},
	{0x305C,"16F1717\r\n",0},
	{0x305D,"16LF1719\r\n",0},
	{0x305E,"16LF1718\r\n",0},
	{0x305F,"16LF1717\r\n",0},
	{0x3060,"16F1707\r\n",0},
	{0x3061,"16F1703\r\n",0},
	{0x3062,"16LF1707\r\n",0},
	{0x3063,"16LF1703\r\n",0},
	{0x3066,"16F18313\r\n",0},
	{0x3067,"16F18323\r\n",0},
	{0x3068,"16LF18313\r\n",0},
	{0x3069,"16LF18323\r\n",0},
	{0x306A,"16F18854\r\n",0},
	{0x306B,"16LF18854\r\n",0},
	{0x306C,"16F18855\r\n",0},
	{0x306D,"16F18875\r\n",0},
	{0x306E,"16LF18855\r\n",0},
	{0x306F,"16LF18875\r\n",0},
	{0x3070,"16F18856\r\n",0},
	{0x3071,"16F18876\r\n",0},
	{0x3072,"16LF18856\r\n",0},
	{0x3073,"16LF18876\r\n",0},
	{0x3074,"16F18857\r\n",0},
	{0x3075,"16F18877\r\n",0},
	{0x3076,"16LF18857\r\n",0},
	{0x3077,"16LF18877\r\n",0},
	{0x3078,"16F1614\r\n",0},
	{0x3079,"16F1618\r\n",0},
	{0x307A,"16LF1614\r\n",0},
	{0x307B,"16LF1618\r\n",0},
	{0x307C,"16F1615\r\n",0},
	{0x307D,"16F1619\r\n",0},
	{0x307E,"16LF1615\r\n",0},
	{0x307F,"16LF1619\r\n",0},
	{0x3080,"16F1764\r\n",0},
	{0x3081,"16F1765\r\n",0},
	{0x3082,"16LF1764\r\n",0},
	{0x3083,"16LF1765\r\n",0},
	{0x3084,"16F1768\r\n",0},
	{0x3085,"16F1769\r\n",0},
	{0x3086,"16LF1768\r\n",0},
	{0x3087,"16LF1769\r\n",0},
	{0x308A,"16F1773\r\n",0},
	{0x308B,"16F1776\r\n",0},
	{0x308C,"16LF1773\r\n",0},
	{0x308D,"16LF1776\r\n",0},
	{0x308E,"16F1777\r\n",0},
	{0x308F,"16F1778\r\n",0},
	{0x3090,"16F1779\r\n",0},
	{0x3091,"16LF1777\r\n",0},
	{0x3092,"16LF1778\r\n",0},
	{0x3093,"16LF1779\r\n",0},
	{0x3096,"16F19155\r\n",0},
	{0x3097,"16LF19155\r\n",0},
	{0x3098,"16F19156\r\n",0},
	{0x3099,"16LF19156\r\n",0},
	{0x309A,"16F19175\r\n",0},
	{0x309B,"16LF19175\r\n",0},
	{0x309C,"16F19176\r\n",0},
	{0x309D,"16LF19176\r\n",0},
	{0x309E,"16F19195\r\n",0},
	{0x309F,"16LF19195\r\n",0},
	{0x30A0,"16F19196\r\n",0},
	{0x30A1,"16LF19196\r\n",0},
	{0x30A2,"16F19197\r\n",0},
	{0x30A3,"16LF19197\r\n",0},
	{0x30A4,"16F18326\r\n",0},
	{0x30A5,"16F18346\r\n",0},
	{0x30A6,"16LF18326\r\n",0},
	{0x30A7,"16LF18346\r\n",0},
	{0x30AC,"16F15354\r\n",0},
	{0x30AD,"16LF15354\r\n",0},
	{0x30AE,"16F15355\r\n",0},
	{0x30AF,"16LF15355\r\n",0},
	{0x30B0,"16F15356\r\n",0},
	{0x30B1,"16LF15356\r\n",0},
	{0x30B2,"16F15375\r\n",0},
	{0x30B3,"16LF15375\r\n",0},
	{0x30B4,"16F15376\r\n",0},
	{0x30B5,"16LF15376\r\n",0},
	{0x30B6,"16F15385\r\n",0},
	{0x30B7,"16LF15385\r\n",0},
	{0x30B8,"16F15386\r\n",0},
	{0x30B9,"16LF15386\r\n",0},
	{0x30BE,"16F15313\r\n",0},
	{0x30BF,"16LF15313\r\n",0},
	{0x30BA,"16F19185\r\n",0},
	{0x30BB,"16LF19185\r\n",0},
	{0x30BC,"16F19186\r\n",0},
	{0x30BD,"16LF19186\r\n",0},
	{0x30C0,"16F15323\r\n",0},
	{0x30C1,"16LF15323\r\n",0},
	{0x30C2,"16F15324\r\n",0},
	{0x30C3,"16LF15324\r\n",0},
	{0x30C4,"16F15344\r\n",0},
	{0x30C5,"16LF15344\r\n",0},
	{0x30C6,"16F15325\r\n",0},
	{0x30C7,"16LF15325\r\n",0},
	{0x30C8,"16F15345\r\n",0},
	{0x30C9,"16LF15345\r\n",0},
	{0x30CA,"16F18424\r\n",0},
	{0x30CB,"16LF18424\r\n",0},
	{0x30CC,"16F18425\r\n",0},
	{0x30CD,"16LF18425\r\n",0},
	{0x30CE,"16F18444\r\n",0},
	{0x30CF,"16LF18444\r\n",0},
	{0x30D0,"16F18445\r\n",0},
	{0x30D1,"16LF18445\r\n",0},
	{0x30D2,"16F18426\r\n",0},
	{0x30D3,"16LF18426\r\n",0},
	{0x30D4,"16F18446\r\n",0},
	{0x30D5,"16LF18446\r\n",0},
	{0x30D7,"16F18455\r\n",0},
	{0x30D8,"16LF18455\r\n",0},
	{0x30D9,"16F18456\r\n",0},
	{0x30DA,"16LF18456\r\n",0},
	{0x30DB,"16F17114\r\n",0},
	{0x30DC,"16F17124\r\n",0},
	{0x30DD,"16F17144\r\n",0},
	{0x30DE,"16F17125\r\n",0},
	{0x30DF,"16F17145\r\n",0},
	{0x30E0,"16F17126\r\n",0},
	{0x30E1,"16F17146\r\n",0},
	{0x30E2,"16F17115\r\n",0},
	{0x30E3,"16F15213\r\n",0},
	{0x30E4,"16F15223\r\n",0},
	{0x30E5,"16F15243\r\n",0},
	{0x30E6,"16F15214\r\n",0},
	{0x30E7,"16F15224\r\n",0},
	{0x30E8,"16F15244\r\n",0},
	{0x30E9,"16F15225\r\n",0},
	{0x30EA,"16F15245\r\n",0},
	{0x30EB,"16F15256\r\n",0},
	{0x30EC,"16F15276\r\n",0},
	{0x30ED,"16F15275\r\n",0},
	{0x30EE,"16F15274\r\n",0},
	{0x30EF,"16F15255\r\n",0},
	{0x30F0,"16F15254\r\n",0},
	{0x30F1,"16F18013\r\n",0},
	{0x30F2,"16F18014\r\n",0},
	{0x30F3,"16F18023\r\n",0},
	{0x30F4,"16F18024\r\n",0},
	{0x30F5,"16F18015\r\n",0},
	{0x30F6,"16F18025\r\n",0},
	{0x30F7,"16F18044\r\n",0},
	{0x30F8,"16F18045\r\n",0},
	{0x30F9,"16F18026\r\n",0},
	{0x30FA,"16F18046\r\n",0},
	{0x30FB,"16F18054\r\n",0},
	{0x30FC,"16F18055\r\n",0},
	{0x30FD,"16F18074\r\n",0},
	{0x30FE,"16F18075\r\n",0},
	{0x30FF,"16F18056\r\n",0},
	{0x3100,"16F18076\r\n",0},
	{0x3107,"16F18114\r\n",0},
	{0x3108,"16F18124\r\n",0},
	{0x3109,"16F18144\r\n",0},
	{0x310A,"16F18154\r\n",0},
	{0x310B,"16F18174\r\n",0},
	{0x310C,"16F18115\r\n",0},
	{0x310D,"16F18125\r\n",0},
	{0x310E,"16F18145\r\n",0},
	{0x310F,"16F18155\r\n",0},
	{0x3110,"16F18175\r\n",0},
	{0x3111,"16F18126\r\n",0},
	{0x3112,"16F18146\r\n",0},
	{0x3113,"16F18156\r\n",0},
	{0x3114,"16F18176\r\n",0},
	{0x3101,"16F17154\r\n",0},
	{0x3102,"16F17174\r\n",0},
	{0x3103,"16F17155\r\n",0},
	{0x3104,"16F17175\r\n",0},
	{0x3105,"16F17156\r\n",0},
	{0x3106,"16F17176\r\n",0},
};

static bool PIC16_ID(int id)
{
	char s[64];
	int i;
	for(i=0;i<sizeof(PIC16LIST)/sizeof(PIC16LIST[0]);i++){
		if(PIC16LIST[i].revmask&&(id>>5)==PIC16LIST[i].id){		//id + rev in same location
			sprintf(s,PIC16LIST[i].device,id&PIC16LIST[i].revmask);
			PrintMessage(s);
			return true;
		}
		else if(!PIC16LIST[i].revmask&&id==PIC16LIST[i].id){		//id separate from rev
			sprintf(s,PIC16LIST[i].device);
			PrintMessage(s);
			return true;
		}
	}
	PrintMessage(strings[S_nodev]); //"Unknown device\r\n");
	return false;
}

void DisplayCODE16F(int size){
// display 14 bit PIC CODE memory
	char s[256]="",t[256]="";
	char* aux=(char*)malloc((size/COL+1)*(16+COL*5));
	aux[0]=0;
	int valid=0,empty=1,i,j,lines=0;
	for(i=0;i<size&&i<sizeW;i+=COL){
		valid=0;
		for(j=i;j<i+COL&&j<sizeW&&i<size;j++){
			sprintf(t,"%04X ",memCODE_W[j]);
			strcat(s,t);
			if((memCODE_W[j]&0x3fff)<0x3fff) valid=1;
		}
		if(valid){
			sprintf(t,"%04X: %s\r\n",i,s);
			empty=0;
			strcat(aux,t);
			lines++;
			if(lines>500){	//limit number of lines printed
				strcat(aux,"(...)\r\n");
				i=(sizeW<size?sizeW:size)-COL*2;
				lines=490;
			}
		}
		s[0]=0;
	}
	if(empty) PrintMessage(strings[S_Empty]);	//empty
	else PrintMessage(aux);
	free(aux);
}

void DisplayEE16F(int size){
// display 14 bit PIC EEPROM memory
	int valid=0,empty=1,i,j;
	char s[256]="",t[256]="",v[256]="";
	char* aux=(char*)malloc((size/COL+1)*(16+COL*5));
	aux[0]=0;
#ifdef __GTK_H__
	char *g;
#endif
	empty=1;
	v[0]=0;
	for(i=0x2100;i<0x2100+size&&i<sizeW;i+=COL){
		valid=0;
		for(j=i;j<i+COL&&j<0x2100+size&&j<sizeW;j++){
			sprintf(t,"%02X ",memCODE_W[j]&0xff);
			strcat(s,t);
			sprintf(t,"%c",isprint(memCODE_W[j]&0xFF)&&(memCODE_W[j]&0xFF)<0xFF?memCODE_W[j]&0xFF:'.');
#ifdef __GTK_H__
			g=g_locale_to_utf8(t,-1,NULL,NULL,NULL);
			if(g) strcat(v,g);
			g_free(g);
#else
			strcat(v,t);
#endif
			if((memCODE_W[j]&0xFF)<0xff) valid=1;/**/
		}
		if(valid){
			sprintf(t,"%04X: %s %s\r\n",i,s,v);
			empty=0;
			strcat(aux,t);
		}
		s[0]=0;
		v[0]=0;
	}
	if(empty) PrintMessage(strings[S_Empty]);	//empty
	else PrintMessage(aux);
	free(aux);
}

void Read16Fxxx(int dim,int dim2,int dim3,int vdd)
// read 14 bit PIC
// dim=program size 	dim2=eeprom size   dim3=config size
// dim2<0 -> eeprom @ 0x2200
// vdd=0 -> vpp before vdd
// vdd=1 -> vdd (+50ms) before vpp
// vdd=2 -> vdd before vpp
// DevREV@0x2005
// DevID@0x2006
// Config@0x2007
// Calib1/Config2@0x2008
// Calib2/Calib1@0x2009
// eeprom@0x2100
{
	int k=0,k2=0,z=0,i,j,ee2200=0;
	char s[512],t[256],*aux;
	if(dim2<0){
		dim2=-dim2;
		ee2200=1;
	}
	if(dim>0x2000||dim<0){
		PrintMessage(strings[S_CodeLim]);	//"Code size exceeds limits\r\n"
		return;
	}
	if(dim2>0x400||dim2<0){		//Max 1K
		PrintMessage(strings[S_EELim]);	//"EEPROM size exceeds limits\r\n"
		return;
	}
	if(dim3>0x100||dim3<0){
		PrintMessage(strings[S_ConfigLim]);	//"Config area size exceeds limits\r\n"
		return;
	}
	if(dim3<8)dim3=8;
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Read16Fxxx(%d,%d,%d,%d)\n",dim,dim2,dim3,vdd);
	}
	sizeW=0x2100+dim2;
	if(memCODE_W) free(memCODE_W);
	memCODE_W=(WORD*)malloc(sizeof(WORD)*sizeW);
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	if(vdd==0){						//VPP before VDD
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=4;				//VPP
		bufferU[j++]=NOP;
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x5;			//VDD+VPP
	}
	else if(vdd==1){				//VDD before VPP with delay 50ms
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=1;				//VDD
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=25000>>8;
		bufferU[j++]=25000&0xff;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=2000>>8;
		bufferU[j++]=2000&0xff;
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x5;			//VDD+VPP
	}
	else if(vdd==2){				//VDD before VPP without delay
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=1;				//VDD
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x5;			//VDD+VPP
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(vdd?55:5);
//****************** read code ********************
	PrintMessage(strings[S_CodeReading1]);		//read code ...
	if(saveLog) fprintf(logfile,"%s\n",strings[S_CodeReading1]);		//read code ...
	PrintStatusSetup();
	for(i=0,j=0;i<dim;i++){
		bufferU[j++]=READ_DATA_PROG;
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF*2/4-3||i==dim-1){		//2B cmd -> 4B data
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(5);
			for(z=0;z<DIMBUF-2;z++){
				if(bufferI[z]==READ_DATA_PROG){
					memCODE_W[k++]=(bufferI[z+1]<<8)+bufferI[z+2];
					z+=2;
				}
			}
			PrintStatus(strings[S_CodeReading],i*100/(dim+dim2+dim3),i);	//"Read: %d%%, addr. %03X"
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log7],i,i,k,k);	//"i=%d(0x%X), k=%d(0x%X)\n"
			}
		}
	}
	PrintStatusEnd();
	if(k!=dim){
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ReadCodeErr],dim,k);	//"Error reading code area, requested %d words, read %d\r\n"
	}
	else PrintMessage(strings[S_Compl]);
	for(i=k;i<0x2000;i++) memCODE_W[i]=0x3fff;
//****************** read config area ********************
	PrintMessage(strings[S_Read_CONFIG_A]);		//read config ...
	if(saveLog) fprintf(logfile,"%s\n",strings[S_Read_CONFIG_A]);		//read config ...
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	for(i=0x2000;i<0x2000+dim3;i++){		//Config
		bufferU[j++]=READ_DATA_PROG;
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF*2/4-3||i==0x2000+dim3-1){		//2B cmd -> 4B data
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(5);
			for(z=0;z<DIMBUF-2;z++){
				if(bufferI[z]==READ_DATA_PROG){
					memCODE_W[0x2000+k2++]=(bufferI[z+1]<<8)+bufferI[z+2];
					z+=2;
				}
			}
			PrintStatus(strings[S_CodeReading],(i-0x2000+dim)*100/(dim+dim2+dim3),i);	//"Read: %d%%, addr. %03X"
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log7],i,i,k2,k2);	//"i=%d(0x%X), k=%d(0x%X)\n"
			}
		}
	}
	if(k2!=dim3){
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigErr],dim3,k2);	//"Error reading config area, requested %d words, read %d\r\n"
	}
	else PrintMessage(strings[S_Compl]);
	for(i=0x2000+k2;i<0x2000+dim3;i++) memCODE_W[i]=0x3fff;
//****************** read eeprom ********************
	if(dim2){
		PrintMessage(strings[S_ReadEE]);		//Read EEPROM ...
		if(saveLog) fprintf(logfile,"%s\n",strings[S_ReadEE]);		//Read EEPROM ...
		PrintStatusSetup();
		if(ee2200){		//eeprom a 0x2200
			bufferU[j++]=INC_ADDR_N;
			bufferU[j++]=0xFF;
			bufferU[j++]=INC_ADDR;
		}
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=0x100-dim3;
		for(k2=0,i=0x2100;i<0x2100+dim2;i++){
			bufferU[j++]=READ_DATA_DATA;
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF*2/3-3||i==0x2100+dim2-1){		//2B cmd -> 3B data
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(8);
				for(z=0;z<DIMBUF-1;z++){
					if(bufferI[z]==READ_DATA_DATA){
						memCODE_W[0x2100+k2++]=bufferI[z+1];
						z++;
					}
				}
				PrintStatus(strings[S_CodeReading],(i-0x2100+dim)*100/(dim+dim2+dim3),i);	//"Read: %d%%, addr. %03X"
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log7],i,i,k2,k2);	//"i=%d(0x%X), k=%d(0x%X)\n"
				}
			}
		}
		PrintStatusEnd();
		if(k2!=dim2){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ReadEEErr],dim2,k2);	//"Error reading EE area, ..."
			for(i=0x2100+k2;i<0x2100+dim2;i++) memCODE_W[i]=0x3fff;
		}
		else PrintMessage(strings[S_Compl]);
	}
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=1;					//VDD
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	PrintStatusClear();			//clear status report
//****************** visualize ********************
	for(i=0;i<4;i+=2){
		PrintMessage4("ID%d: 0x%04X\tID%d: 0x%04X\r\n",i,memCODE_W[0x2000+i],i+1,memCODE_W[0x2000+i+1]);
	}
	PrintMessage1(strings[S_DevID],memCODE_W[0x2006]);	//"DevID: 0x%04X\r\n"
	if(memCODE_W[0x2005]<0x3FFF) PrintMessage1(strings[S_DevREV],memCODE_W[0x2005]);	//"DevREV: 0x%04X\r\n"
	if (!PIC16_ID(memCODE_W[0x2006])) return;
	PrintMessage1(strings[S_ConfigWord],memCODE_W[0x2007]);	//"Configuration word: 0x%04X\r\n"
	if(dim3>8){
		PrintMessage1(strings[S_Config2Cal1],memCODE_W[0x2008]);	//"Config2 or Cal1: 0x%04X\r\n"
	}
	if(dim3>9){
		PrintMessage1(strings[S_Calib1_2],memCODE_W[0x2009]);	//"Calibration word 1 or 2: 0x%04X\r\n"
	}
	PrintMessage(strings[S_CodeMem2]);	//"\r\nCode memory\r\n"
	DisplayCODE16F(dim);
	s[0]=0;
	int valid=0,empty=1;
	if(dim3>8){
		aux=(char*)malloc((dim3/COL+1)*(16+COL*5));
		aux[0]=0;
		empty=1;
		PrintMessage(strings[S_ConfigResMem]);	//"\r\nConfig and reserved memory:\r\n"
		for(i=0x2000;i<0x2000+dim3;i+=COL){
			valid=0;
			for(j=i;j<i+COL&&j<0x2000+dim3;j++){
				sprintf(t,"%04X ",memCODE_W[j]);
				strcat(s,t);
				if(memCODE_W[j]<0x3fff) valid=1;
			}
			if(valid){
				snprintf(t,sizeof(t),"%04X: %s\r\n",i,s);
				empty=0;
				strcat(aux,t);
			}
			s[0]=0;
		}
		if(empty) PrintMessage(strings[S_Empty]);	//empty
		else PrintMessage(aux);
		free(aux);
	}
	if(dim2){
		PrintMessage(strings[S_EEMem]);	//"\r\nEEPROM memory:\r\n"
		DisplayEE16F(dim2);
	}
	sprintf(str,strings[S_End],(stop-start)/1000.0);	//"\r\nEnd (%.2f s)\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Read16F1xxx(int dim,int dim2,int dim3,int options)
// read 14 bit enhanced PIC
// dim=program size (up to 0x8000)
// dim2=eeprom size (up to 0x400)
// dim3=config area size @0x8000
// options:
//		bit0=0 -> vpp before vdd
//		bit0=1 -> vdd before vpp
//		bit1=1 -> LVP programming
//		bit2=1 -> Config3@0x8009
//		bit3=1 -> Config4@0x800A
//		bit4=1 -> PIC16F18xxx (calib words @0xE000 + new commands)
// DevREV@0x8005     DevID@0x8006
// Config1@0x8007    Config2@0x8008
// Config3@0x8009    Config4@0x800A
// Calib1@0x8009/A   Calib2@0x800A/B    Calib3@0x800B/C
// eeprom@0x0 or 0xF000
{
	int k=0,k2=0,z=0,i,j;
	int F18x=options&16;
	if(F18x&&FWVersion<0xA00){		//only for 16F18xxx
		PrintMessage1(strings[S_FWver2old],"0.10.0");	//"This firmware is too old. Version %s is required\r\n"
		return;
	}
	if(!CheckV33Regulator()){
		PrintMessage(strings[S_noV33reg]);	//Can't find 3.3V expansion board
		return;
	}
	char s[256],t[256],*aux;
	if(dim>0x8000||dim<0){
		PrintMessage(strings[S_CodeLim]);	//"Code size exceeds limits\r\n"
		return;
	}
	if(dim2>0x400||dim2<0){		//Max 1K
		PrintMessage(strings[S_EELim]);	//"EEPROM size exceeds limits\r\n"
		return;
	}
	if(dim3>0x200||dim3<0){
		PrintMessage(strings[S_ConfigLim]);	//"Config area size exceeds limits\r\n"
		return;
	}
	if(dim3<11)dim3=11;		//at least config1-2 + calib1-2
	if(saveLog){
		OpenLogFile();	//"Log.txt"
		fprintf(logfile,"Read16F1xxx(%d,%d,%d,%d)\n",dim,dim2,dim3,options);
	}
	if((options&2)==0){				//HV entry
		if(!StartHVReg(8.5)){
			PrintMessage(strings[S_HVregErr]); //"HV regulator error\r\n"
			return;
		}
		else hvreg=8.5;
	}
	else StartHVReg(-1);			//LVP mode, turn off HV
	sizeW=0x8200;
	if(F18x) sizeW=0xE004;
	sizeEE=dim2;
	if(memCODE_W) free(memCODE_W);
	memCODE_W=(WORD*)malloc(sizeof(WORD)*sizeW);
	for(i=0;i<sizeW;i++) memCODE_W[i]=0x3fff;
	if(memEE) free(memEE);
	memEE=(unsigned char*)malloc(sizeEE);			//EEPROM
	for(i=0;i<sizeEE;i++) memEE[i]=0xFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	if((options&2)==0){				//HV entry
		if((options&1)==0){				//VPP before VDD
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=4;				//VPP
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x5;			//VDD+VPP
	}
		else{							//VDD before VPP without delay
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=1;				//VDD
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x5;			//VDD+VPP
	}
	}
	else{			//Low voltage programming
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=4;				//VPP
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=TX16;			//0000 1010 0001 0010 1100 0010 1011 0010 = 0A12C2B2
		bufferU[j++]=2;
		bufferU[j++]=0x0A;
		bufferU[j++]=0x12;
		bufferU[j++]=0xC2;
		bufferU[j++]=0xB2;
		bufferU[j++]=SET_CK_D;		//Clock pulse
		bufferU[j++]=0x4;
		bufferU[j++]=SET_CK_D;
		bufferU[j++]=0x0;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
//****************** read code ********************
	PrintMessage(strings[S_CodeReading1]);		//read code ...
	if(saveLog) fprintf(logfile,"%s\n",strings[S_CodeReading1]);		//read code ...
	PrintStatusSetup();
	for(i=0,j=0;i<dim;i++){
		bufferU[j++]=READ_DATA_PROG;
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF*2/4-3||i==dim-1){		//2B cmd -> 4B data
			bufferU[j++]=FLUSH;				//remember: FLUSH generates a response even empty!
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(5);
			for(z=0;z<DIMBUF-2;z++){
				if(bufferI[z]==READ_DATA_PROG){
					memCODE_W[k++]=(bufferI[z+1]<<8)+bufferI[z+2];
					z+=2;
				}
			}
			PrintStatus(strings[S_CodeReading],i*100/(dim+dim2+dim3),i);	//"Read: %d%%, addr. %03X"
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log7],i,i,k,k);	//"i=%d(0x%X), k=%d(0x%X)\n"
			}
		}
	}
	PrintStatusEnd();
	if(k!=dim){
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ReadCodeErr],dim,k);	//"Error reading code area, requested %d words, read %d\r\n"
	}
	else PrintMessage(strings[S_Compl]);
//****************** read config area ********************
	PrintMessage(strings[S_Read_CONFIG_A]);		//read config ...
	if(saveLog) fprintf(logfile,"%s\n",strings[S_Read_CONFIG_A]);		//read config ...
	bufferU[j++]=LOAD_CONF;			//counter at 0x8000
	bufferU[j++]=0xFF;
	bufferU[j++]=0xFF;
	for(i=0x8000;i<0x8000+dim3;i++){		//Config
		bufferU[j++]=READ_DATA_PROG;
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF*2/4-3||i==0x8000+dim3-1){		//2B cmd -> 4B data
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(5);
			for(z=0;z<DIMBUF-2;z++){
				if(bufferI[z]==READ_DATA_PROG){
					memCODE_W[0x8000+k2++]=(bufferI[z+1]<<8)+bufferI[z+2];
					z+=2;
				}
			}
			PrintStatus(strings[S_CodeReading],(i-0x8000+dim)*100/(dim+dim2+dim3),i);	//"Read: %d%%, addr %03X"
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log7],i,i,k2,k2);	//"i=%d(0x%X), k=%d(0x%X)\n"
			}
		}
	}
	if(k2!=dim3){
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigErr],dim3,k2);	//"Error reading config area, requested %d words, read %d\r\n"
	}
	else PrintMessage(strings[S_Compl]);
//	for(i=0x8000+k2;i<0x8000+dim3;i++) memCODE_W[i]=0x3fff;
//****************** read calibration area ********************
	if(F18x){
		k2=0;
		PrintMessage(strings[S_Read_CONFIG_A]);		//read config ...
		bufferU[j++]=LOAD_PC;			//counter at 0xE000
		bufferU[j++]=0xE0;
		bufferU[j++]=0x00;
		for(i=0;i<4;i++) bufferU[j++]=READ_DATA_INC;
		bufferU[j++]=FLUSH;
		for(;j<DIMBUF;j++) bufferU[j]=0x0;
		PacketIO(5);
		for(z=0;z<DIMBUF-2;z++){
			if(bufferI[z]==READ_DATA_INC){
				memCODE_W[0xE000+k2++]=(bufferI[z+1]<<8)+bufferI[z+2];
				z+=2;
			}
		}
		j=0;
		if(saveLog){
			fprintf(logfile,"Calibration area @0xE000\n");
		}
		if(k2!=4){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ConfigErr],dim3,k2);	//"Error reading config area, requested %d words, read %d\r\n"
		}
		else PrintMessage(strings[S_Compl]);
	}
//****************** read eeprom ********************
	if(dim2&&!F18x){					//16F1xxx
		PrintMessage(strings[S_ReadEE]);		//Read EEPROM ...
		if(saveLog) fprintf(logfile,"%s\n",strings[S_ReadEE]);		//Read EEPROM ...
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x16;		//Reset address
		for(i=k=0;i<dim2;i++){
			bufferU[j++]=READ_DATA_DATA;
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF*2/3-3||i==dim2-1){		//2B cmd -> 3B data
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(5);
				for(z=0;z<DIMBUF-1;z++){
					if(bufferI[z]==READ_DATA_DATA){
						memEE[k++]=bufferI[z+1];
						z++;
					}
				}
				PrintStatus(strings[S_CodeReading],i*100/(dim+dim2+dim3),i);	//"Read: %d%%, addr %03X"
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log7],i,i,k,k);	//"i=%d(0x%X), k=%d(0x%X)\n"
				}
			}
		}
		PrintStatusEnd();
		if(i!=dim2){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ReadEEErr],dim2,i);	//"Error reading EE area, ..."
			for(;i<dim2;i++) memEE[i]=0xff;
		}
		else PrintMessage(strings[S_Compl]);
	}
	else if(dim2&&F18x){					//16F18xxx
		PrintMessage(strings[S_ReadEE]);		//Read EEPROM ...
		if(saveLog) fprintf(logfile,"%s\n",strings[S_ReadEE]);		//Read EEPROM ...
		bufferU[j++]=LOAD_PC;				//counter at 0xF000
		bufferU[j++]=0xF0;
		bufferU[j++]=0x00;
		for(i=k=0;i<dim2;i++){
			bufferU[j++]=READ_DATA_INC;
			if(j>DIMBUF/3-3||i==dim2-1){		//1B cmd -> 3B answer
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(5);
				for(z=0;z<DIMBUF-1;z++){
					if(bufferI[z]==READ_DATA_INC){
						memEE[k++]=bufferI[z+2];	//LSB
						z+=2;
					}
				}
				PrintStatus(strings[S_CodeReading],i*100/(dim+dim2+dim3),i);	//"Read: %d%%, addr %03X"
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log7],i,i,k,k);	//"i=%d(0x%X), k=%d(0x%X)\n"
				}
			}
		}
		PrintStatusEnd();
		if(i!=dim2){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ReadEEErr],dim2,i);	//"Error reading EE area, ..."
			for(;i<dim2;i++) memEE[i]=0xff;
		}
		else PrintMessage(strings[S_Compl]);
	}
/***********************************/
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=1;					//VDD
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	PrintStatusClear();			//clear status report
//****************** visualize ********************
	for(i=0;i<4;i+=2){
		PrintMessage4("ID%d: 0x%04X\tID%d: 0x%04X\r\n",i,memCODE_W[0x8000+i],i+1,memCODE_W[0x8000+i+1]);
	}
	PrintMessage1(strings[S_DevID],memCODE_W[0x8006]);	//"DevID: 0x%04X\r\n"
	if(memCODE_W[0x8005]<0x3FFF) PrintMessage1(strings[S_DevREV],memCODE_W[0x8005]);	//"DevREV: 0x%04X\r\n"
	if (!PIC16_ID(memCODE_W[0x8006])) return;
	PrintMessage2(strings[S_ConfigWordX],1,memCODE_W[0x8007]);	//"Configuration word %d: 0x%04X\r\n"
	PrintMessage2(strings[S_ConfigWordX],2,memCODE_W[0x8008]);	//"Configuration word %d: 0x%04X\r\n"
	if((options&0xC)==0&&F18x==0){	//2 config + 2/3 calib words
		PrintMessage2(strings[S_CalibWordX],1,memCODE_W[0x8009]);	//"Calibration word %d: 0x%04X\r\n"
		PrintMessage2(strings[S_CalibWordX],2,memCODE_W[0x800A]);	//"Calibration word %d: 0x%04X\r\n"
		if(memCODE_W[0x800B]<0x3FFF) PrintMessage2(strings[S_CalibWordX],3,memCODE_W[0x800B]);	//"Calibration word %d: 0x%04X\r\n"
	}
	else if((options&0xC)==4&&F18x==0){	//3 config + 3 calib words
		PrintMessage2(strings[S_ConfigWordX],3,memCODE_W[0x8009]);	//"Configuration word %d: 0x%04X\r\n"
		PrintMessage2(strings[S_CalibWordX],1,memCODE_W[0x800A]);	//"Calibration word %d: 0x%04X\r\n"
		PrintMessage2(strings[S_CalibWordX],2,memCODE_W[0x800B]);	//"Calibration word %d: 0x%04X\r\n"
		PrintMessage2(strings[S_CalibWordX],3,memCODE_W[0x800C]);	//"Calibration word %d: 0x%04X\r\n"
	}
	else if((options&0xC)==8){		//4 config
		PrintMessage2(strings[S_ConfigWordX],3,memCODE_W[0x8009]);	//"Configuration word %d: 0x%04X\r\n"
		PrintMessage2(strings[S_ConfigWordX],4,memCODE_W[0x800A]);	//"Configuration word %d: 0x%04X\r\n"
	}
	if(F18x){			//4 calib words @0xE000
		PrintMessage2(strings[S_CalibWordX],1,memCODE_W[0xE000]);	//"Calibration word %d: 0x%04X\r\n"
		PrintMessage2(strings[S_CalibWordX],2,memCODE_W[0xE001]);	//"Calibration word %d: 0x%04X\r\n"
		PrintMessage2(strings[S_CalibWordX],3,memCODE_W[0xE002]);	//"Calibration word %d: 0x%04X\r\n"
		PrintMessage2(strings[S_CalibWordX],4,memCODE_W[0xE003]);	//"Calibration word %d: 0x%04X\r\n"
	}
	PrintMessage(strings[S_CodeMem2]);	//"\r\nCode memory:\r\n"
	DisplayCODE16F(dim);
	if(dim3>15){
		int valid=0,empty=1;
		s[0]=0;
		aux=(char*)malloc(dim3/COL*(16+COL*5));
		aux[0]=0;
		empty=1;
		PrintMessage(strings[S_ConfigResMem]);	//"\r\nConfig and reserved memory:\r\n"
		for(i=0x8000;i<0x8000+dim3;i+=COL){
			valid=0;
			for(j=i;j<i+COL&&j<0x8000+dim3;j++){
				sprintf(t,"%04X ",memCODE_W[j]);
				strcat(s,t);
				if(memCODE_W[j]<0x3fff) valid=1;
			}
			if(valid){
				sprintf(t,"%04X: %s\r\n",i,s);
				empty=0;
				strcat(aux,t);
			}
			s[0]=0;
		}
		if(empty) PrintMessage(strings[S_Empty]);	//empty
		else PrintMessage(aux);
		free(aux);
	}
	if(dim2) DisplayEE();	//visualize
	sprintf(str,strings[S_End],(stop-start)/1000.0);	//"\r\nEnd (%.2f s)\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

#define LOAD_PC_ADDR 0x80
#define BULK_ERASE_PROGRAM_MEM 0x18
#define ROW_ERASE_PROGRAM_MEM 0xF0
#define LOAD_NVM 0x00
#define LOAD_NVM_INC 0x02
#define READ_NVM 0xFC
#define READ_NVM_INC 0xFE
#define INC_ADDR8 0xF8
#define BEGIN_INT_PROG 0xE0
#define BEGIN_EXT_PROG 0xC0
#define END_EXT_PROG 0x82

void Read16F18xxx(int dim,int dim2,int dim3,int options)
// read 14 bit enhanced PIC with new 8b commands
// dim=program size (up to 0x8000)
// dim2=if>0 use eeprom, size is automatic when reading DCI
// dim3=not used
// options:
//		bit0=0 -> vpp before vdd
//		bit0=1 -> vdd before vpp
//		bit1=1 -> LVP programming
//		bit4=1 -> do not use DIA&DCI
// DevREV@0x8005     DevID@0x8006
// Config1@0x8007    Config2@0x8008 ...
// Device info area @0x8100
// Device configuration info area @0x8200
{
	int k=0,k2=0,z=0,i,j;
	int useDCI=(options&0x10)==0?1:0;
	if(FWVersion<0xB00){		//only for 16F18xxx
		PrintMessage1(strings[S_FWver2old],"0.11.0");	//"This firmware is too old. Version %s is required\r\n"
		return;
	}
	if(!CheckV33Regulator()){
		PrintMessage(strings[S_noV33reg]);	//Can't find 3.3V expansion board
		return;
	}
	char s[256],t[256];
	if(dim>0x8000||dim<0){
		PrintMessage(strings[S_CodeLim]);	//"Code size exceeds limits\r\n"
		return;
	}
	if(saveLog){
		OpenLogFile();	//"Log.txt"
		fprintf(logfile,"Read16F18xxx(%d,%d,%d,%d)\n",dim,dim2,dim3,options);
	}
	if((options&2)==0){				//HV entry
		if(!StartHVReg(8.5)){
			PrintMessage(strings[S_HVregErr]); //"HV regulator error\r\n"
			return;
		}
		else hvreg=8.5;
	}
	else StartHVReg(-1);			//LVP mode, turn off HV
	sizeW=0x8220;
	if(memCODE_W) free(memCODE_W);
	memCODE_W=(WORD*)malloc(sizeof(WORD)*sizeW);
	for(i=0;i<sizeW;i++) memCODE_W[i]=0x3fff;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	if((options&2)==0){				//HV entry
		if((options&1)==0){				//VPP before VDD
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=4;				//VPP
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=0x5;			//VDD+VPP
		}
		else{							//VDD before VPP without delay
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=1;				//VDD
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=0x5;			//VDD+VPP
		}
	}
	else{			//Low voltage programming
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=4;				//VPP
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=TX16;			//0000 1010 0001 0010 1100 0010 1011 0010 = 0A12C2B2
		bufferU[j++]=2;
		bufferU[j++]=0x0A;
		bufferU[j++]=0x12;
		bufferU[j++]=0xC2;
		bufferU[j++]=0xB2;
		bufferU[j++]=SET_CK_D;		//Clock pulse
		bufferU[j++]=0x4;
		bufferU[j++]=SET_CK_D;
		bufferU[j++]=0x0;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
//****************** read code ********************
	PrintMessage(strings[S_CodeReading1]);		//read code ...
	if(saveLog) fprintf(logfile,"%s\n",strings[S_CodeReading1]);		//read code ...
	PrintStatusSetup();
	for(i=0,j=0;i<dim;i++){
		bufferU[j++]=ICSP8_READ;
		bufferU[j++]=READ_NVM_INC;
		if((j+1)/2*3+3>DIMBUF||i==dim-1){		//2B cmd -> 3B data
			bufferU[j++]=FLUSH;				//remember: FLUSH generates a response (even empty)!
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(5);
			for(z=0;z<DIMBUF-2;z++){
				if(bufferI[z]==ICSP8_READ){
					memCODE_W[k++]=(bufferI[z+1]<<8)+bufferI[z+2];
					z+=2;
				}
			}
			PrintStatus(strings[S_CodeReading],i*100/(dim+dim2+0x60),i);	//"Read: %d%%, addr. %03X"
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log7],i,i,k,k);	//"i=%d(0x%X), k=%d(0x%X)\n"
			}
		}
	}
	PrintStatusEnd();
	if(k!=dim){
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ReadCodeErr],dim,k);	//"Error reading code area, requested %d words, read %d\r\n"
	}
	else PrintMessage(strings[S_Compl]);
//****************** read config area ********************
	PrintMessage(strings[S_Read_CONFIG_A]);		//read config ...
	if(saveLog) fprintf(logfile,"%s\n",strings[S_Read_CONFIG_A]);		//read config ...
	bufferU[j++]=ICSP8_LOAD;			//counter at 0x8000
	bufferU[j++]=LOAD_PC_ADDR;
	bufferU[j++]=0x80;
	bufferU[j++]=0x00;
	k2=0;
	for(i=0x8000;i<0x8010;i++){		//Config
		bufferU[j++]=ICSP8_READ;
		bufferU[j++]=READ_NVM_INC;
		if((j+1)/2*3+3>DIMBUF||i==0x800F){		//2B cmd -> 3B data
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(5);
			for(z=0;z<DIMBUF-2;z++){
				if(bufferI[z]==ICSP8_READ){
					memCODE_W[0x8000+k2++]=(bufferI[z+1]<<8)+bufferI[z+2];
					z+=2;
				}
			}
			PrintStatus(strings[S_CodeReading],(i-0x8000+dim)*100/(dim+dim2+0x60),i);	//"Read: %d%%, addr %03X"
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log7],i,i,k2,k2);	//"i=%d(0x%X), k=%d(0x%X)\n"
			}
		}
	}
	if(k2!=0x10){
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigErr],0x10,k2);	//"Error reading config area, requested %d words, read %d\r\n"
	}
	else PrintMessage(strings[S_Compl]);
//****************** read device info area ********************
	if(useDCI){	//if not disabled
		if(saveLog) fprintf(logfile,"Read device info area @0x8100\n");
		bufferU[j++]=ICSP8_LOAD;			//counter at 0x8000
		bufferU[j++]=LOAD_PC_ADDR;
		bufferU[j++]=0x81;
		bufferU[j++]=0x00;
		k2=0;
		for(i=0x8100;i<0x8120;i++){		//DIA
			bufferU[j++]=ICSP8_READ;
			bufferU[j++]=READ_NVM_INC;
			if((j+1)/2*3+3>DIMBUF||i==0x811F){		//2B cmd -> 3B data
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(5);
				for(z=0;z<DIMBUF-2;z++){
					if(bufferI[z]==ICSP8_READ){
						memCODE_W[0x8100+k2++]=(bufferI[z+1]<<8)+bufferI[z+2];
						z+=2;
					}
				}
				PrintStatus(strings[S_CodeReading],(i-0x8100+dim+0x20)*100/(dim+dim2+0x60),i);	//"Read: %d%%, addr %03X"
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log7],i,i,k2,k2);	//"i=%d(0x%X), k=%d(0x%X)\n"
				}
			}
		}
		if(k2!=0x20){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ConfigErr],0x20,k2);	//"Error reading config area, requested %d words, read %d\r\n"
		}
	//****************** read calibration area (Device Configuration Information) ********************
		if(saveLog) fprintf(logfile,"Read Device Configuration Information @0x8200\n");
		bufferU[j++]=ICSP8_LOAD;			//counter at 0x8200
		bufferU[j++]=LOAD_PC_ADDR;
		bufferU[j++]=0x82;
		bufferU[j++]=0x00;
		k2=0;
		for(i=0x8200;i<0x8220;i++){		//DCI
			bufferU[j++]=ICSP8_READ;
			bufferU[j++]=READ_NVM_INC;
			if((j+1)/2*3+3>DIMBUF||i==0x821F){		//2B cmd -> 3B data
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(5);
				for(z=0;z<DIMBUF-2;z++){
					if(bufferI[z]==ICSP8_READ){
						memCODE_W[0x8200+k2++]=(bufferI[z+1]<<8)+bufferI[z+2];
						z+=2;
					}
				}
				PrintStatus(strings[S_CodeReading],(i-0x8200+dim+0x40)*100/(dim+dim2+0x60),i);	//"Read: %d%%, addr %03X"
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log7],i,i,k2,k2);	//"i=%d(0x%X), k=%d(0x%X)\n"
				}
			}
		}
		if(k2!=0x20){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ConfigErr],0x20,k2);	//"Error reading config area, requested %d words, read %d\r\n"
		}
	}
//****************** read eeprom ********************
	if(dim2){					//EEPROM
		if(useDCI) dim2=memCODE_W[0x8203]; 	//EEPROM size from DCI
		if(dim2>0x1000||dim2<0){		//Max 4K
			PrintMessage(strings[S_EELim]);	//"EEPROM size exceeds limits\r\n"
			return;
		}
		sizeEE=dim2;
		if(memEE) free(memEE);
		memEE=(unsigned char*)malloc(sizeEE);			//EEPROM
		for(i=0;i<sizeEE;i++) memEE[i]=0xFF;
		PrintMessage(strings[S_ReadEE]);		//Read EEPROM ...
		if(saveLog) fprintf(logfile,"%s\n",strings[S_ReadEE]);		//Read EEPROM ...
		bufferU[j++]=ICSP8_LOAD;			//counter at 0xF000
		bufferU[j++]=LOAD_PC_ADDR;
		bufferU[j++]=0xF0;
		bufferU[j++]=0x00;
		for(i=k=0;i<dim2;i++){
			bufferU[j++]=ICSP8_READ;
			bufferU[j++]=READ_NVM_INC;
			if((j+1)/2*3+3>DIMBUF||i==dim2-1){		//2B cmd -> 3B answer
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(5);
				for(z=0;z<DIMBUF-2;z++){
					if(bufferI[z]==ICSP8_READ){
						memEE[k++]=bufferI[z+2];	//LSB
						z+=2;
					}
				}
				PrintStatus(strings[S_CodeReading],i*100+dim+0x60/(dim+dim2+0x60),i);	//"Read: %d%%, addr %03X"
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log7],i,i,k,k);	//"i=%d(0x%X), k=%d(0x%X)\n"
				}
			}
		}
		PrintStatusEnd();
		if(i!=dim2){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ReadEEErr],dim2,i);	//"Error reading EE area, ..."
			for(;i<dim2;i++) memEE[i]=0xff;
		}
		else PrintMessage(strings[S_Compl]);
	}
/***********************************/
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=1;					//VDD
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	PrintStatusClear();			//clear status report
//****************** visualize ********************
	for(i=0;i<4;i+=2){
		PrintMessage4("ID%d: 0x%04X\tID%d: 0x%04X\r\n",i,memCODE_W[0x8000+i],i+1,memCODE_W[0x8000+i+1]);
	}
	PrintMessage1(strings[S_DevID],memCODE_W[0x8006]);	//"DevID: 0x%04X\r\n"
	if(memCODE_W[0x8005]<0x3FFF) PrintMessage1(strings[S_DevREV],memCODE_W[0x8005]);	//"DevREV: 0x%04X\r\n"
	if (!PIC16_ID(memCODE_W[0x8006])) return;
	for(i=0;i<5;i++){
		PrintMessage2(strings[S_ConfigWordX],i+1,memCODE_W[0x8007+i]);	//"Configuration word %d: 0x%04X\r\n"
	}
	if(useDCI){	//if not disabled
		PrintMessage("Device Information Area @0x8100\r\n");
		s[0]=0;
		for(i=0;i<0x20;i+=COL){
			sprintf(t,"%04X: ",0x8100+i);
			strcat(s,t);
			for(j=i;j<i+COL&&j<0x20;j++){
				sprintf(t,"%04X ",memCODE_W[0x8100+j]);
				strcat(s,t);
			}
			strcat(s,"\r\n");
		}
		PrintMessage(s);
		PrintMessage("Device Configuration Information @0x8200\r\n");
		s[0]=0;
		for(i=0;i<0x20;i+=COL){
			sprintf(t,"%04X: ",0x8200+i);
			strcat(s,t);
			for(j=i;j<i+COL&&j<0x20;j++){
				sprintf(t,"%04X ",memCODE_W[0x8200+j]);
				strcat(s,t);
			}
			strcat(s,"\r\n");
		}
		PrintMessage(s);
		PrintMessage1("Erase row size: %d words\r\n",memCODE_W[0x8200]);
		PrintMessage1("Write latches: %d\r\n",memCODE_W[0x8201]);
		PrintMessage1("User rows: %d\r\n",memCODE_W[0x8202]);
		PrintMessage1("->%d Flash words\r\n",memCODE_W[0x8200]*memCODE_W[0x8202]);
		PrintMessage1("EE data memory size: %d\r\n",memCODE_W[0x8203]);
		PrintMessage1("Pin count: %d\r\n",memCODE_W[0x8204]);
		if(dim!=memCODE_W[0x8200]*memCODE_W[0x8202]) PrintMessage(strings[S_WarnFlashSize]);	//"Warning, flash size is different from the expected value"
	}
	PrintMessage(strings[S_CodeMem2]);	//"\r\nCode memory:\r\n"
	DisplayCODE16F(dim);
	if(dim2) DisplayEE();	//visualize
	sprintf(str,strings[S_End],(stop-start)/1000.0);	//"\r\nEnd (%.2f s)\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write12F6xx(int dim,int dim2)
// write 14 bit PIC
// dim=program size 	dim2=eeprom size
// vpp before vdd
// DevID@0x2006
// Config@0x2007
// Calib1@0x2008 (save)
// Calib2@0x2009 (save)
// eeprom@0x2100
// erase: BULK_ERASE_PROG (1001) +10ms
// write:LOAD_DATA_PROG (0010) + BEGIN_PROG (1000) + 4ms
// eeprom:	BULK_ERASE_DATA (1011) + 16ms
//			LOAD_DATA_DATA (0011) + BEGIN_PROG (1000) + 8ms
// verify during write
{
	int err=0;
	WORD devID=0x3fff,calib1=0x3fff,calib2=0x3fff;
	int k=0,z=0,i,j,w;
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(load_calibword){
		if(sizeW>0x2009) load_calibword=2;
		else if(sizeW>0x2008) load_calibword=1;
		else{
			PrintMessage(strings[S_NoCalibW]);	//"Can't find calibration data\r\n"
			load_calibword=0;
		}
	}
	if(dim2){
		if(sizeW<0x2100){
			dim2=0;
			PrintMessage(strings[S_NoEEMem]);	//"Can't find EEPROM data\r\n"
		}
		else if(dim2>sizeW-0x2100) dim2=sizeW-0x2100;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write12F6xx(%d,%d)\n",dim,dim2);
	}
	for(i=0;i<0x2009&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Calib1
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Calib2
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=10000>>8;
	bufferU[j++]=10000&0xff;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(15);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	calib1=(bufferI[z+1]<<8)+bufferI[z+2];
	if(calib1<0x3fff){
		PrintMessage1(strings[S_CalibWord1],calib1);	//"Calib1: 0x%04X\r\n"
	}
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	calib2=(bufferI[z+1]<<8)+bufferI[z+2];
	if(calib2<0x3fff){
		PrintMessage1(strings[S_CalibWord2],calib2);	//"Calib2: 0x%04X\r\n"
	}
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=WAIT_T3;		//necessary when erasing fully written 16F62xA
								//not mentioned in the prog spec!
	if(programID||load_calibword||ICDenable){
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		if(load_calibword){
			bufferU[j++]=INC_ADDR_N;
			if(load_calibword==2) bufferU[j++]=0x09;
			else bufferU[j++]=0x08;
		}
	}
	bufferU[j++]=LOAD_DATA_PROG;
	bufferU[j++]=0xFF;
	bufferU[j++]=0xFF;
	bufferU[j++]=BULK_ERASE_PROG;
	bufferU[j++]=WAIT_T3;			// delay T3=10ms
	if(dim2){
		bufferU[j++]=BULK_ERASE_DATA;
		bufferU[j++]=WAIT_T3;			// delay T3=10ms
	}
	bufferU[j++]=EN_VPP_VCC;		//exit program mode
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			// delay T3=10ms
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x5;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=4000>>8;
	bufferU[j++]=4000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(50);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 4ms
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-12||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*5+2);
			w=0;
			for(z=0;z<DIMBUF-5;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+3]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+4]<<8)+bufferI[z+5]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+4]<<8)+bufferI[z+5]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=6;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d,\n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write eeprom ********************
	if(dim2){
		int err_e=0;
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		j=0;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=8000>>8;
		bufferU[j++]=8000&0xff;
		bufferU[j++]=BULK_ERASE_DATA;
		bufferU[j++]=WAIT_T3;			// delay T3=8ms
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=INC_ADDR_N;		//use only INC_ADDR_N so verification does not look at it
		bufferU[j++]=1;
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=0x2100-0x2001;		//EEPROM area: counter at 0x2100
		for(w=2,i=k=0x2100;i<0x2100+dim2;i++){
			if(memCODE_W[i]<0xff){
				bufferU[j++]=LOAD_DATA_DATA;
				bufferU[j++]=memCODE_W[i]&0xff;
				bufferU[j++]=BEGIN_PROG;			//internally timed, T=6ms min
				bufferU[j++]=WAIT_T3;				//Tprogram 8ms
				bufferU[j++]=READ_DATA_DATA;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-12||i==0x2100+dim2-1){
				PrintStatus(strings[S_CodeWriting],(i-0x2100+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*9+2);
				w=0;
				for(z=0;z<DIMBUF-4;z++){
					if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_DATA&&bufferI[z+3]==READ_DATA_DATA){
						if (memCODE_W[k]!=bufferI[z+4]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memCODE_W[k],bufferI[z+4]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							err_e++;
							if(max_err&&err+err_e>max_err){
								PrintMessage1(strings[S_MaxErr],err+err_e);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"Write interrupted"
								i=0x2200;
								z=DIMBUF;
							}
						}
						k++;
						z+=5;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
				}
			}
		}
		err_e+=i-k;
		err+=err_e;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],err_e);	//"completed, %d errors\r\n"
	}
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	int ICDgoto=0x2800+(ICDaddr&0x7FF);		//GOTO ICD routine (0x28xx)
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=4000>>8;
	bufferU[j++]=4000&0xff;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 4ms
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=4;
	}
	if(ICDenable){		//write a GOTO ICD routine (0x28xx)
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=ICDgoto>>8;		//MSB
		bufferU[j++]=ICDgoto&0xFF;			//LSB
		bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
		bufferU[j++]=WAIT_T3;				//Tprogram
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=3;
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
	bufferU[j++]=WAIT_T3;				//Tprogram 4ms
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	if(load_calibword){
		bufferU[j++]=LOAD_DATA_PROG;			//Calib word 1
		bufferU[j++]=memCODE_W[0x2008]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x2008]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
		bufferU[j++]=WAIT_T3;				//Tprogram 4ms
		bufferU[j++]=READ_DATA_PROG;
		bufferU[j++]=INC_ADDR;
		bufferU[j++]=LOAD_DATA_PROG;			//Calib word 2
		bufferU[j++]=memCODE_W[0x2009]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x2009]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
		bufferU[j++]=WAIT_T3;				//Tprogram 4ms
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(35);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	if(ICDenable){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (ICDgoto!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage4(strings[S_ICDErr],0x2004,i,ICDgoto,(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ICD (0x%X): written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	if(load_calibword){
		for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2008]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_Calib1Err],memCODE_W[0x2008],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing Calib1: written %04X, read %04X\r\n"
			err_c++;
		}
		if(load_calibword==2){
			for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
			if (memCODE_W[0x2009]!=(bufferI[z+1]<<8)+bufferI[z+2]){
				PrintMessage("\r\n");
				PrintMessage2(strings[S_Calib2Err],memCODE_W[0x2009],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing Calib2: written %04X, read %04X\r\n"
				err_c++;
			}
		}
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F8x (int dim,int dim2)
// write 14 bit PIC
// dim=program size 	dim2=eeprom size
// vdd + 50ms + vdd&vpp
// DevID@0x2006
// Config@0x2007
// eeprom@0x2100
// erase if protected:
// LOAD_CONF (0)(0x3FFF) + CUST_CMD (0001) + CUST_CMD (0111)
// + BEGIN_PROG (1000) + 10ms
// + CUST_CMD (0001) + CUST_CMD (0111)
// erase if not protected and DevID=16F84A:
// LOAD_DATA_PROG (0010)(0x3FFF) + BULK_ERASE_PROG (1001) +10ms
// LOAD_DATA_DATA (0011)(0xFF) + BULK_ERASE_DATA (1011) + BEGIN_PROG (1000) + 10ms
// erase if not protected and DevID!=16F84A:
// LOAD_DATA_PROG (0010)(0x3FFF) + CUST_CMD (0001) + CUST_CMD (0111)
// + BEGIN_PROG (1000) + 10ms + CUST_CMD (0001) + CUST_CMD (0111)
// LOAD_DATA_DATA (0011)(0xFF) + CUST_CMD (0001) + CUST_CMD (0111)
// + BEGIN_PROG (1000) + 10ms + CUST_CMD (0001) + CUST_CMD (0111)
// write: LOAD_DATA_PROG (0010) + BEGIN_PROG (1000) + 20ms or 8ms(16F84A)
// write eeprom: LOAD_DATA_DATA (0011) + BEGIN_PROG (1000) + 20ms or 8ms(16F84A)
// verify during write
{
	int err=0;
	WORD devID,config;
	int k=0,z=0,i,j,w,r;
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(dim2){
		if(sizeW<0x2100){
			dim2=0;
			PrintMessage(strings[S_NoEEMem]);	//"Can't find EEPROM data\r\n"
		}
		else if(dim2>sizeW-0x2100) dim2=sizeW-0x2100;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write16F8x(%d,%d)\n",dim,dim2);
	}
	for(i=0;i<0x2009&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=25000>>8;
	bufferU[j++]=25000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Config
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	bufferU[j++]=WAIT_T3;			//50ms
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(140);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	config=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_ConfigWord],config);	//"Config word: 0x%04X\r\n"
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=10000>>8;
	bufferU[j++]=10000&0xff;
	if(config<0x3FF0){
		PrintMessage(strings[S_ProtErase]);	//"Override write protection\r\n"
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0x3F;				//fake config
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=0x07;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
		bufferU[j++]=BEGIN_PROG;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
	}
	else if(devID>>5==0x2B){			//16F84A
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=0x3f;				//MSB
		bufferU[j++]=0xff;				//LSB
		bufferU[j++]=BULK_ERASE_PROG;
		bufferU[j++]=BEGIN_PROG;
		bufferU[j++]=WAIT_T3;
		if(dim2){
			bufferU[j++]=LOAD_DATA_DATA;
			bufferU[j++]=0xff;				//LSB
			bufferU[j++]=BULK_ERASE_DATA;
			bufferU[j++]=BEGIN_PROG;
			bufferU[j++]=WAIT_T3;
		}
	}
	else{								//altri
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=0x3f;				//MSB
		bufferU[j++]=0xff;				//LSB
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
		bufferU[j++]=BEGIN_PROG;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
		if(dim2){
			bufferU[j++]=LOAD_DATA_DATA;	//EEPROM:  spec error?
			bufferU[j++]=0xff;				//LSB
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x01;
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x07;
			bufferU[j++]=BEGIN_PROG;
			bufferU[j++]=WAIT_T3;
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x01;
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x07;
		}
	}
	if(!programID){					//back to program memory
		bufferU[j++]=NOP;				//exit program mode
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x1;
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x0;
		bufferU[j++]=SET_CK_D;
		bufferU[j++]=0x0;
		bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
		bufferU[j++]=EN_VPP_VCC;		//VDD
		bufferU[j++]=0x1;
		bufferU[j++]=WAIT_T3;			//50ms
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
		bufferU[j++]=0x5;
	}
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	r=(devID>>5==0x2B)?8000:20000;
	bufferU[j++]=r>>8;
	bufferU[j++]=r&0xff;
	bufferU[j++]=FLUSH;
	r/=1000;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(!programID?140:60);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-10||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*r+4);
			w=0;
			for(z=0;z<DIMBUF-5;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+3]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+4]<<8)+bufferI[z+5]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+4]<<8)+bufferI[z+5]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=6;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write eeprom ********************
	if(dim2){
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		j=0;
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=INC_ADDR_N;		//use only INC_ADDR_N so verification does not look at it
		bufferU[j++]=0xFF;
		bufferU[j++]=INC_ADDR_N;	//EEPROM: counter at 0x2100
		bufferU[j++]=1;
		for(w=0,i=k=0x2100;i<0x2100+dim2;i++){
			if(memCODE_W[i]<0xff){
				bufferU[j++]=LOAD_DATA_DATA;
				bufferU[j++]=memCODE_W[i]&0xff;
				bufferU[j++]=BEGIN_PROG;			//internally timed
				bufferU[j++]=WAIT_T3;				//Tprogram
				bufferU[j++]=READ_DATA_DATA;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-10||i==0x2100+dim2-1){
				PrintStatus(strings[S_CodeWriting],(i-0x2100+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*r+5);
				w=0;
				for(z=0;z<DIMBUF-4;z++){
					if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_DATA&&bufferI[z+3]==READ_DATA_DATA){
						if (memCODE_W[k]!=bufferI[z+4]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memCODE_W[k],bufferI[z+4]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							err++;
							if(max_err&&err>max_err){
								PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"Write interrupted"
								i=0x2200;
								z=DIMBUF;
							}
						}
						k++;
						z+=5;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
				}
			}
		}
		err+=i-k;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],i-k);	//"completed, %d errors\r\n"
	}
//****************** write ID, CONFIG ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=3;
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=7;
	}
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG;			//internally timed
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(programID?125:35);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F62x (int dim,int dim2)
// write 14 bit PIC
// dim=program size 	dim2=eeprom size
// vpp before vdd
// DevID@0x2006
// Config@0x2007
// eeprom@0x2200
// erase if protected:
// LOAD_CONF (0000)(0) + CUST_CMD (0001) + CUST_CMD (0111)
// + BEGIN_PROG (1000) + 15ms + CUST_CMD (0001) + CUST_CMD (0111)
// erase if not protected:
// LOAD_DATA_PROG (0010)(0x3FFF) + BULK_ERASE_PROG (1001) +5ms
// LOAD_DATA_DATA (0011)(0xFF) + BULK_ERASE_DATA (1011) + BEGIN_PROG (1000) + 5ms
// write: LOAD_DATA_PROG (0010) + BEGIN_PROG2 (11000) + 8ms
// write ID: LOAD_DATA_PROG (0010) + BEGIN_PROG (1000) + 16ms
// write CONFIG: LOAD_DATA_PROG (0010) + BEGIN_PROG2 (11000) + 8ms
// eeprom: LOAD_DATA_DATA (0011) + BEGIN_PROG2 (11000) + 8ms
// verify during write
{
	int err=0;
	WORD devID,config;
	int k=0,z=0,i,j,w;
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(dim2){
		if(sizeW<0x2100){
			dim2=0;
			PrintMessage(strings[S_NoEEMem]);	//"Can't find EEPROM data\r\n"
		}
		else if(dim2>sizeW-0x2100) dim2=sizeW-0x2100;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write16F62x(%d,%d)\n",dim,dim2);
	}
	for(i=0;i<0x2009&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=10000>>8;
	bufferU[j++]=10000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Config
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=EN_VPP_VCC;		//0
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay exit-enter prog mode
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(12);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	config=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_ConfigWord],config);	//"Config word: 0x%04X\r\n"
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=5000>>8;
	bufferU[j++]=5000&0xff;
	if(config<0x3C00){
		PrintMessage(strings[S_ProtErase]);	//"Override write protection\r\n"
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0x3F;				//fake config	spec ERROR!!! is written data=0!!
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=0x07;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
		bufferU[j++]=BEGIN_PROG;		//Tera+Tprog=5+8 ms
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
	}
	else{
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=0x3f;				//MSB
		bufferU[j++]=0xff;				//LSB
		bufferU[j++]=BULK_ERASE_PROG;
		bufferU[j++]=BEGIN_PROG;		//Tera=5ms
		bufferU[j++]=WAIT_T3;
		if(dim2){
			bufferU[j++]=LOAD_DATA_DATA;
			bufferU[j++]=0xff;				//LSB
			bufferU[j++]=BULK_ERASE_DATA;
			bufferU[j++]=BEGIN_PROG;		//Tera=5ms
			bufferU[j++]=WAIT_T3;
		}
	}
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x4;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=8000>>8;
	bufferU[j++]=8000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(60);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//program only, internally timed
			bufferU[j++]=WAIT_T3;				//Tprogram=8ms
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-10||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*9+2);
			w=0;
			for(z=0;z<DIMBUF-5;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+3]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+4]<<8)+bufferI[z+5]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+4]<<8)+bufferI[z+5]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=6;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write eeprom ********************
	if(dim2){
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		j=0;
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=INC_ADDR_N;		//use only INC_ADDR_N so verification does not look at it
		bufferU[j++]=0xFF;		//20FF
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=0xFF;		//21FE
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=0x2;		//EEPROM: counter at 0x2200
		for(w=0,i=k=0x2100;i<0x2100+dim2;i++){
			if(memCODE_W[i]<0xff){
				bufferU[j++]=LOAD_DATA_DATA;
				bufferU[j++]=memCODE_W[i]&0xff;
				bufferU[j++]=BEGIN_PROG2;			//internally timed
				bufferU[j++]=WAIT_T3;				//Tprogram=8ms
				bufferU[j++]=READ_DATA_DATA;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-10||i==0x2100+dim2-1){
				PrintStatus(strings[S_CodeWriting],(i-0x2100+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*14+1);
				w=0;
				for(z=0;z<DIMBUF-4;z++){
					if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_DATA&&bufferI[z+3]==READ_DATA_DATA){
						if (memCODE_W[k]!=bufferI[z+4]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memCODE_W[k],bufferI[z+4]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							err++;
							if(max_err&&err>max_err){
								PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"Write interrupted"
								i=0x2200;
								z=DIMBUF;
							}
						}
						k++;
						z+=5;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
				}
			}
		}
		err+=i-k;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],i-k);	//"completed, %d errors\r\n"
	}
//****************** write ID, CONFIG ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//erase + prog internally timed, T=8+5 ms
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=3;
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=7;
	}
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG2;				//prog only, internally timed, T=8 ms
	bufferU[j++]=WAIT_T3;					//Tprogram 8ms
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(programID?125:35);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d\n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write12F62x(int dim,int dim2)
// write 14 bit PIC
// dim=program size 	dim2=eeprom size
// vpp before vdd
// save OSCCAL @ dim-1
// CONFIG@0x2007 includes 2  calibration bits
// DevID@0x2006
// eeprom@0x2100
// erase: BULK_ERASE_PROG (1001) +10ms
// write: LOAD_DATA_PROG (0010) + BEGIN_PROG (1000) + 3ms
// eeprom: BULK_ERASE_DATA (1011) + 9ms
// LOAD_DATA_DATA (0011) + BEGIN_PROG (1000) + 6ms
// verify during write
{
	int err=0;
	WORD devID,config,osccal;
	int k=0,z=0,i,j,w;
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(dim2){
		if(sizeW<0x2100){
			dim2=0;
			PrintMessage(strings[S_NoEEMem]);	//"Can't find EEPROM data\r\n"
		}
		else if(dim2>sizeW-0x2100) dim2=sizeW-0x2100;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write12F62x(%d,%d)\n",dim,dim2);
	}
	for(i=0;i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	for(i=0;i<dim-0xff;i+=0xff){
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=0xff;
	}
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=dim-1-i;
	bufferU[j++]=READ_DATA_PROG;	// OSCCAL
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Config
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=10000>>8;
	bufferU[j++]=10000&0xff;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(15);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	osccal=(bufferI[z+1]<<8)+bufferI[z+2];
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	config=(bufferI[z+1]<<8)+bufferI[z+1];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	if(programID){
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
	}
	bufferU[j++]=BULK_ERASE_PROG;
	bufferU[j++]=WAIT_T3;			// delay T3=10ms
	bufferU[j++]=EN_VPP_VCC;		//exit program mode
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			// delay T3=10ms after exiting program mode
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x5;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=3000>>8;
	bufferU[j++]=3000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(40);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	if(!load_osccal) memCODE_W[dim-1]=osccal;	//backup osccal
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms
			bufferU[j++]=WAIT_T3;				//Tprogram 3ms
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-10||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*6.5+j*0.1);
			w=0;
			for(z=0;z<DIMBUF-5;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+3]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+4]<<8)+bufferI[z+5]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+4]<<8)+bufferI[z+5]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=6;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
			}
		}
	}
	PrintStatusEnd();
	err+=i-k;
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write eeprom ********************
	if(dim2){
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		j=0;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=6000>>8;
		bufferU[j++]=6000&0xff;
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=INC_ADDR_N;		//use only INC_ADDR_N so verification does not look at it
		bufferU[j++]=0xFF;
		bufferU[j++]=INC_ADDR_N;	//EEPROM: counter at 0x2100
		bufferU[j++]=1;
		bufferU[j++]=BULK_ERASE_DATA;
		bufferU[j++]=WAIT_T3;			// delay=12ms
		bufferU[j++]=WAIT_T3;
		for(w=3,i=k=0x2100;i<0x2100+dim2;i++){
			if(memCODE_W[i]<0xff){
				bufferU[j++]=LOAD_DATA_DATA;
				bufferU[j++]=memCODE_W[i]&0xff;
				bufferU[j++]=BEGIN_PROG;			//internally timed, T=6ms
				bufferU[j++]=WAIT_T3;				//Tprogram 6ms
				bufferU[j++]=READ_DATA_DATA;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-10||i==0x2100+dim2-1){
				PrintStatus(strings[S_CodeWriting],(i-0x2100+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*7+2);
				w=0;
				for(z=0;z<DIMBUF-4;z++){
					if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_DATA&&bufferI[z+3]==READ_DATA_DATA){
						if (memCODE_W[k]!=bufferI[z+4]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memCODE_W[k],bufferI[z+4]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							err++;
							if(max_err&&err>max_err){
								PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"Write interrupted"
								i=0x2200;
								z=DIMBUF;
							}
						}
						k++;
						z+=5;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
				}
			}
		}
		err+=i-k;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],i-k);	//"completed, %d errors\r\n"
	}
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms
			bufferU[j++]=WAIT_T3;				//Tprogram 3ms
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=3;
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=7;
	}
	if(!load_calibword)	memCODE_W[0x2007]=(memCODE_W[0x2007]&0xfff)+(config&0x3000);
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms
	bufferU[j++]=WAIT_T3;				//Tprogram 3ms
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(35);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//exit program mode
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F87x (int dim,int dim2)
// write 14 bit PIC
// dim=program size 	dim2=eeprom size
// dim2<0 -> eeprom @ 0x2200
// vdd + (50ms?) + vdd&vpp
// DevID@0x2006
// Config@0x2007
// eeprom@0x2100
// erase if protected:
// LOAD_CONF (0000)(0x3FFF) + CUST_CMD (0001) + CUST_CMD (0111)
// + BEGIN_PROG (1000) + 8ms
// + CUST_CMD (0001) + CUST_CMD (0111)
// erase if not protected:
// LOAD_DATA_PROG (0010)(0x3FFF) + CUST_CMD (0001) + CUST_CMD (0111)
// + BEGIN_PROG (1000) + 8ms + CUST_CMD (0001) + CUST_CMD (0111)
// LOAD_DATA_DATA (0011)(0xFF) + CUST_CMD (0001) + CUST_CMD (0111)
// + BEGIN_PROG (1000) + 8ms + CUST_CMD (0001) + CUST_CMD (0111)
// write: LOAD_DATA_PROG (0010) + BEGIN_PROG2 (11000) + 4ms
// write eeprom: LOAD_DATA_DATA (0011) + BEGIN_PROG (1000) + 8ms
// verify during write
{
	int err=0;
	WORD devID,config;
	int k=0,z=0,i,j,w,ee2200=0;
	if(dim2<0){
		dim2=-dim2;
		ee2200=1;
	}
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(dim2){
		if(sizeW<0x2100){
			dim2=0;
			PrintMessage(strings[S_NoEEMem]);	//"Can't find EEPROM data\r\n"
		}
		else if(dim2>sizeW-0x2100) dim2=sizeW-0x2100;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write16F87x(%d,%d)\n",dim,dim2);
	}
	for(i=0;i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Config
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(60);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	config=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_ConfigWord],config);	//"Config word: 0x%04X\r\n"
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=8000>>8;
	bufferU[j++]=8000&0xff;
	if((config&0x3130)!=0x3130){
		PrintMessage(strings[S_ProtErase]);	//"override write protection\r\n"
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0x3F;				//fake config
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=0x07;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
		bufferU[j++]=BEGIN_PROG;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
		bufferU[j++]=NOP;				//exit program mode
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x1;
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x0;
		bufferU[j++]=SET_CK_D;
		bufferU[j++]=0x0;
		bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
		bufferU[j++]=EN_VPP_VCC;		//VDD
		bufferU[j++]=0x1;
		bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
		bufferU[j++]=0x5;
	}
	else{
		if(ICDenable||programID){			//erase 0x2000-2004 also
			bufferU[j++]=LOAD_CONF;			//counter at 0x2000
			bufferU[j++]=0x3F;				//fake config
			bufferU[j++]=0xFF;				//fake config
		}
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=0x3f;				//MSB
		bufferU[j++]=0xff;				//LSB
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
		bufferU[j++]=BEGIN_PROG;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x01;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x07;
		if(dim2){
			bufferU[j++]=LOAD_DATA_DATA;	//EEPROM:  spec error?
			bufferU[j++]=0xff;				//LSB
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x01;
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x07;
			bufferU[j++]=BEGIN_PROG;
			bufferU[j++]=WAIT_T3;
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x01;
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x07;
		}
		if(ICDenable||programID){			//back to addr 0
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=0x1;
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=0x0;
			bufferU[j++]=SET_CK_D;
			bufferU[j++]=0x0;
			bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
			bufferU[j++]=EN_VPP_VCC;		//VDD
			bufferU[j++]=0x1;
			bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
			bufferU[j++]=0x5;
		}
	}
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=4000>>8;
	bufferU[j++]=4000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(60);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//internally timed
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-10||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*5+2);
			w=0;
			for(z=0;z<DIMBUF-5;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+3]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+4]<<8)+bufferI[z+5]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+4]<<8)+bufferI[z+5]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=6;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write eeprom ********************
	if(dim2){
		int err_e=0;
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		j=0;
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=INC_ADDR_N;		//use only INC_ADDR_N so verification does not look at it
		bufferU[j++]=0xFF;
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=1;					//EEPROM: counter at 0x2100
		if(ee2200){		//eeprom at 0x2200
			bufferU[j++]=INC_ADDR_N;
			bufferU[j++]=0xFF;
			bufferU[j++]=INC_ADDR_N;
			bufferU[j++]=1;
		}
		for(w=0,i=k=0x2100;i<0x2100+dim2;i++){
			if(memCODE_W[i]<0xff){
				bufferU[j++]=LOAD_DATA_DATA;
				bufferU[j++]=memCODE_W[i]&0xff;
				bufferU[j++]=BEGIN_PROG2;			//internally timed ?????
				bufferU[j++]=WAIT_T3;				//Tprogram         ?????
				bufferU[j++]=READ_DATA_DATA;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-10||i==0x2100+dim2-1){
				PrintStatus(strings[S_CodeWriting],(i-0x2100+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*8+5);
				w=0;
				for(z=0;z<DIMBUF;z++){
					if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_DATA&&bufferI[z+3]==READ_DATA_DATA){
						if (memCODE_W[k]!=bufferI[z+4]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memCODE_W[k],bufferI[z+4]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							err_e++;
							if(max_err&&err+err_e>max_err){
								PrintMessage1(strings[S_MaxErr],err+err_e);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"Write interrupted"
								i=0x2200;
								z=DIMBUF;
							}
						}
						k++;
						z+=5;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
				}
			}
		}
		err_e+=i-k;
		err+=err_e;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],err_e);	//"completed, %d errors\r\n"
	}
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	int ICDgoto=0x2800+(ICDaddr&0x7FF);		//GOTO ICD routine (0x28xx)
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=8000>>8;
	bufferU[j++]=8000&0xff;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//internally timed
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=4;
	}
	if(ICDenable){		//write a GOTO ICD routine (0x28xx)
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=ICDgoto>>8;		//MSB
		bufferU[j++]=ICDgoto&0xFF;			//LSB
		bufferU[j++]=BEGIN_PROG2;			//internally timed
		bufferU[j++]=WAIT_T3;				//Tprogram
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=3;
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG2;			//internally timed
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(programID?125:35);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	if(ICDenable){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (ICDgoto!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage4(strings[S_ICDErr],0x2004,i,ICDgoto,(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ICD (0x%X): written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F87xA (int dim,int dim2,int seq)
// write 14 bit PIC
// dim=program size 	dim2=eeprom size
// seq=0: vdd + (50ms) + vdd&vpp
// seq=1: vdd + (50us) + vdd&vpp
// DevID@0x2006
// Config@0x2007
// write CONFIG2@0x2008 if different from 3FFF
// eeprom@0x2100
// erase:
// CHIP ERASE (11111) + 15ms
// write: LOAD_DATA_PROG (0010) + BEGIN_PROG2 (11000) + 1.2ms + END_PROGX (10111)
// write eeprom: LOAD_DATA_DATA (0011) + BEGIN_PROG (1000) + 12ms
// verify during write
{
	int err=0;
	WORD devID,config;
	int k=0,z=0,i,j,w;
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(dim2){
		if(sizeW<0x2100){
			dim2=0;
			PrintMessage(strings[S_NoEEMem]);	//"Can't find EEPROM data\r\n"
		}
		else if(dim2>sizeW-0x2100) dim2=sizeW-0x2100;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write16F87xA(%d,%d,%d)\n",dim,dim2,seq);
	}
	for(i=0;i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	bufferU[0]=0;
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=25000>>8;
	bufferU[j++]=25000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	if(seq==0){
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
	}
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Config
	bufferU[j++]=EN_VPP_VCC;		//exit program mode
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(seq==0?90:40);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	config=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_ConfigWord],config);	//"Config word: 0x%04X\r\n"
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=15000>>8;
	bufferU[j++]=15000&0xff;
	if(ICDenable||programID){			//erase 0x2000-2004 also
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0x3F;				//fake config
		bufferU[j++]=0xFF;				//fake config
	}
	bufferU[j++]=CUST_CMD;
	bufferU[j++]=0x1F;					// CHIP_ERASE (11111)
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=EN_VPP_VCC;		//exit program mode
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=1200>>8;
	bufferU[j++]=1200&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(60);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//externally timed
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x17;					//END_PROGX (10111)
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-11||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*1.5+(6-w)*0.2+2);
			w=0;
			for(z=0;z<DIMBUF-6;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+4]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+5]<<8)+bufferI[z+6]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+5]<<8)+bufferI[z+6]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=7;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write eeprom ********************
	if(dim2){
		int err_e=0;
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		j=0;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=12000>>8;
		bufferU[j++]=12000&0xff;
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=INC_ADDR_N;		//use only INC_ADDR_N so verification does not look at it
		bufferU[j++]=0xFF;
		bufferU[j++]=INC_ADDR_N;		//EEPROM: counter at 0x2100
		bufferU[j++]=1;
		for(w=0,i=k=0x2100;i<0x2100+dim2;i++){
			if(memCODE_W[i]<0xff){
				bufferU[j++]=LOAD_DATA_DATA;
				bufferU[j++]=memCODE_W[i]&0xff;
				bufferU[j++]=BEGIN_PROG;			//internally timed
				bufferU[j++]=WAIT_T3;				//Tprogram 8ms
				bufferU[j++]=READ_DATA_DATA;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-10||i==0x2100+dim2-1){
				PrintStatus(strings[S_CodeWriting],(i-0x2100+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*12.5+5);
				w=0;
				for(z=0;z<DIMBUF-4;z++){
					if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_DATA&&bufferI[z+3]==READ_DATA_DATA){
						if (memCODE_W[k]!=bufferI[z+4]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memCODE_W[k],bufferI[z+4]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							err_e++;
							if(max_err&&err>max_err){
								PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"Write interrupted"
								i=0x2200;
								z=DIMBUF;
							}
						}
						k++;
						z+=5;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
				}
			}
		}
		err_e+=i-k;
		err+=err_e;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],err_e);	//"completed, %d errors\r\n"
	}
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	int ICDgoto=0x2800+(ICDaddr&0x7FF);		//GOTO ICD routine (0x28xx)
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=12000>>8;
	bufferU[j++]=12000&0xff;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=4;
	}
	if(ICDenable){		//write a GOTO ICD routine (0x28xx)
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=ICDgoto>>8;		//MSB
		bufferU[j++]=ICDgoto&0xFF;			//LSB
		bufferU[j++]=BEGIN_PROG;			//internally timed
		bufferU[j++]=WAIT_T3;				//Tprogram
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=3;
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG;				//internally timed
	bufferU[j++]=WAIT_T3;					//Tprogram
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	if(sizeW>0x2008&&memCODE_W[0x2008]!=0x3fff){
		bufferU[j++]=LOAD_DATA_PROG;			//Config word2 0x2008
		bufferU[j++]=memCODE_W[0x2008]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x2008]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG;				//internally timed
		bufferU[j++]=WAIT_T3;					//Tprogram
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	i=28;
	if(programID) i+=50;
	if(ICDenable) i+=13;
	PacketIO(i);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	if(ICDenable){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (ICDgoto!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage4(strings[S_ICDErr],0x2004,i,ICDgoto,(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ICD (0x%X): written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	if(sizeW>0x2008&&memCODE_W[0x2008]!=0x3fff){
		for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if(~memCODE_W[0x2008]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
			PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2008],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
			err_c++;
		}
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F81x (int dim,int dim2)
// write 14 bit PIC
// dim=program size 	dim2=eeprom size
// seq=0: vdd + (50ms) + vdd&vpp
// seq=1: vdd + (50us) + vdd&vpp
// DevID@0x2006
// Config@0x2007
// write CONFIG2@0x2008 if different from 3FFF
// erase if protected: CHIP ERASE (11111) + 8ms
// erase if not protected:
// BULK_ERASE_PROG (1001) + BEGIN_PROG (1001) + 2ms + END_PROGX (10111)
// BULK_ERASE_DATA (1011) + BEGIN_PROG (1001) + 2ms + END_PROGX (10111)
// write: LOAD_DATA_PROG (0010) + BEGIN_PROG2 (11000) + 1.5ms + END_PROGX (10111)
// write eeprom: LOAD_DATA_DATA (0011) + BEGIN_PROG2 (11000) + 1.5ms + END_PROGX (10111)
// verify during write
{
	int err=0;
	WORD devID,config;
	int k=0,z=0,i,j,w;
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(dim2){
		if(sizeW<0x2100){
			dim2=0;
			PrintMessage(strings[S_NoEEMem]);	//"Can't find EEPROM data\r\n"
		}
		else if(dim2>sizeW-0x2100) dim2=sizeW-0x2100;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write16F81x(%d,%d)\n",dim,dim2);
	}
	for(i=0;i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=25000>>8;
	bufferU[j++]=25000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Config
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(40);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	config=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_ConfigWord],config);	//"Config word: 0x%04X\r\n"
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	if(programID||ICDenable){
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0x3F;				//fake config
		bufferU[j++]=0xFF;				//fake config
	}
	if((config&0x2100)!=0x2100){
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x1F;					// CHIP_ERASE (11111)
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=WAIT_T3;
	}
	else{
		bufferU[j++]=BULK_ERASE_PROG;
		bufferU[j++]=BEGIN_PROG;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x17;					//END_PROGX (10111)
	}
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(15);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//externally timed
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x17;					//END_PROGX (10111)
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-11||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*2.5+3);
			w=0;
			for(z=0;z<DIMBUF-6;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+4]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+5]<<8)+bufferI[z+6]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+5]<<8)+bufferI[z+6]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=7;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write eeprom ********************
	if(dim2){
		int err_e=0;
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		j=0;
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x1;
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=0x0;
		bufferU[j++]=SET_CK_D;
		bufferU[j++]=0x0;
		bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
		bufferU[j++]=EN_VPP_VCC;		//VDD
		bufferU[j++]=0x1;
		bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
		bufferU[j++]=0x5;
		bufferU[j++]=LOAD_DATA_DATA;
		bufferU[j++]=0x01;
		bufferU[j++]=BULK_ERASE_DATA;
		bufferU[j++]=BEGIN_PROG;
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x17;					//END_PROGX (10111)
		bufferU[j++]=FLUSH;
		for(;j<DIMBUF;j++) bufferU[j]=0x0;
		PacketIO(5);
		j=0;
		for(w=0,i=k=0x2100;i<0x2100+dim2;i++){
			if(memCODE_W[i]<0xff){
				bufferU[j++]=LOAD_DATA_DATA;
				bufferU[j++]=memCODE_W[i]&0xff;
				bufferU[j++]=BEGIN_PROG2;			//externally timed
				bufferU[j++]=WAIT_T3;				//Tprogram
				bufferU[j++]=CUST_CMD;
				bufferU[j++]=0x17;					//END_PROGX (10111)
				bufferU[j++]=READ_DATA_DATA;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-10||i==0x2100+dim2-1){
				PrintStatus(strings[S_CodeWriting],(i-0x2100+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*2.5+5);
				w=0;
				for(z=0;z<DIMBUF-5;z++){
					if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_DATA&&bufferI[z+4]==READ_DATA_DATA){
						if (memCODE_W[k]!=bufferI[z+5]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memCODE_W[k],bufferI[z+5]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							err_e++;
							if(max_err&&err>max_err){
								PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"Write interrupted"
								i=0x2200;
								z=DIMBUF;
							}
						}
						k++;
						z+=6;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
				}
			}
		}
		err_e+=i-k;
		err+=err_e;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],err_e);	//"completed, %d errors\r\n"
	}
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	int ICDgoto=0x2800+(ICDaddr&0x7FF);		//GOTO ICD routine (0x28xx)
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//externally timed
			bufferU[j++]=WAIT_T3;				//Tprogram
			bufferU[j++]=CUST_CMD;
			bufferU[j++]=0x17;					//END_PROGX (10111)
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=4;
	}
	if(ICDenable){		//write a GOTO ICD routine (0x28xx)
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=ICDgoto>>8;		//MSB
		bufferU[j++]=ICDgoto&0xFF;			//LSB
		bufferU[j++]=BEGIN_PROG2;				//externally timed
		bufferU[j++]=WAIT_T3;					//Tprogram
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x17;						//END_PROGX (10111)
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=3;
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG2;				//externally timed
	bufferU[j++]=WAIT_T3;					//Tprogram
	bufferU[j++]=CUST_CMD;
	bufferU[j++]=0x17;						//END_PROGX (10111)
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	if(sizeW>0x2008&&memCODE_W[0x2008]!=0x3fff){
		bufferU[j++]=LOAD_DATA_PROG;			//Config word2 0x2008
		bufferU[j++]=memCODE_W[0x2008]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x2008]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG2;				//externally timed
		bufferU[j++]=WAIT_T3;					//Tprogram
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x17;						//END_PROGX (10111)
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(15);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=7;
	}
	if(ICDenable){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (ICDgoto!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage4(strings[S_ICDErr],0x2004,i,ICDgoto,(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ICD (0x%X): written %04X, read %04X\r\n"
			err_c++;
		}
		z+=7;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	if(sizeW>0x2008&&memCODE_W[0x2008]!=0x3fff){
		for(z+=7;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if(~memCODE_W[0x2008]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
			PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2008],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
			err_c++;
		}
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write12F61x(int dim, int d, int d2)
// write 14 bit PIC
// dim=program size
// d not used
// vpp before vdd
// DevREV@0x2005
// DevID@0x2006
// Config@0x2007
// Calib1@0x2008 (save)
// Calib2@0x2009
// erase: BULK_ERASE_PROG (1001) +10ms
// write: LOAD_DATA_PROG (0010) + BEGIN_PROG2 (11000) + 4ms + END_PROG (1010)
// verify during write
{
	int err=0;
	WORD devID=0x3fff,devREV=0x3fff,calib1=0x3fff,calib2=0x3fff;
	int k=0,z=0,i,j,w;
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(load_calibword){
		if(sizeW>0x2008) load_calibword=1;
		else{
			PrintMessage(strings[S_NoCalibW]);	//"Can't find calibration data\r\n"
			load_calibword=0;
		}
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write12F61x(%d)\n",dim);
	}
	for(i=0;i<0x2009&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x05;
	bufferU[j++]=READ_DATA_PROG;	//DevREV
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Calib1
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Calib2
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=10000>>8;
	bufferU[j++]=10000&0xff;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(15);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devREV=(bufferI[z+1]<<8)+bufferI[z+2];
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if(devREV<0x3FFF) PrintMessage1(strings[S_DevREV],devREV);	//"DevREV: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	calib1=(bufferI[z+1]<<8)+bufferI[z+2];
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	calib2=(bufferI[z+1]<<8)+bufferI[z+2];
	if(calib1<0x3fff) PrintMessage1(strings[S_CalibWord1],calib1);	//"Calib1: 0x%04X\r\n"
	if(calib2<0x3fff) PrintMessage1(strings[S_CalibWord2],calib2);	//"Calib2: 0x%04X\r\n"
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	if(programID||load_calibword){
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		if(load_calibword){
			bufferU[j++]=INC_ADDR_N;
			bufferU[j++]=0x08;
		}
	}
	bufferU[j++]=BULK_ERASE_PROG;
	bufferU[j++]=WAIT_T3;			// delay T3=10ms
	bufferU[j++]=EN_VPP_VCC;		//exit program mode
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			// delay T3=10ms after exiting program mode
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x5;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=4000>>8;
	bufferU[j++]=4000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(40);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//externally timed, T=3ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 4ms
			bufferU[j++]=END_PROG;
			bufferU[j++]=WAIT_T2;				//Tdischarge 100us
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-12||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*5+2.5);
			w=0;
			for(z=0;z<DIMBUF-7;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+5]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+6]<<8)+bufferI[z+7]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+4]<<8)+bufferI[z+5]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=8;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d,\n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//externally timed, T=3ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 4ms
			bufferU[j++]=END_PROG;
			bufferU[j++]=WAIT_T2;				//Tdischarge 100us
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=3;
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=7;
	}
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG2;			//externally timed, T=3ms min
	bufferU[j++]=WAIT_T3;				//Tprogram 4ms
	bufferU[j++]=END_PROG;
	bufferU[j++]=WAIT_T2;				//Tdischarge 100us
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	if(load_calibword){
		bufferU[j++]=LOAD_DATA_PROG;			//Calib word 1
		bufferU[j++]=memCODE_W[0x2008]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x2008]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG2;			//externally timed, T=3ms min
		bufferU[j++]=WAIT_T3;				//Tprogram 4ms
		bufferU[j++]=END_PROG;
		bufferU[j++]=WAIT_T2;				//Tdischarge 100us
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(35);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=8;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	if(load_calibword){
		for(z+=8;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2008]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_Calib1Err],memCODE_W[0x2008],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing Calib1: written %04X, read %04X\r\n"
			err_c++;
		}
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F88x(int dim,int dim2)
// write 14 bit PIC
// dim=program size 	dim2=eeprom size
// vpp before vdd
// DevID@0x2006
// Config@0x2007
// Config2@0x2008
// Calib1@0x2009 (salva)
// eeprom@0x2100
// erase: BULK_ERASE_PROG (1001) +6ms
// write:LOAD_DATA_PROG (0010) + BEGIN_PROG (1000) + 3ms
// eeprom:	BULK_ERASE_DATA (1011) + 6ms
//			LOAD_DATA_DATA (0011) + BEGIN_PROG (1000) + 6ms
// verify during write
{
	int err=0;
	WORD devID=0x3fff,calib1=0x3fff;
	int k=0,z=0,i,j,w;
	if(sizeW<0x2009){
		PrintMessage(strings[S_NoConfigW4]);	//"Can't find CONFIG (0x2008)\r\nEnd\r\n"
		return;
	}
	if(load_calibword){
		if(sizeW>0x200A) load_calibword=1;
		else{
			PrintMessage(strings[S_NoCalibW]);	//"Can't find calibration data\r\n"
			load_calibword=0;
		}
	}
	if(dim2){
		if(sizeW<0x2100){
			dim2=0;
			PrintMessage(strings[S_NoEEMem]);	//"Can't find EEPROM data\r\n"
		}
		else if(dim2>sizeW-0x2100) dim2=sizeW-0x2100;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write16F88x(%d,%d)\n",dim,dim2);
	}
	for(i=0;i<0x200A&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Calib1
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=6000>>8;
	bufferU[j++]=6000&0xff;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(15);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	calib1=(bufferI[z+1]<<8)+bufferI[z+2];
	if(calib1<0x3fff){
		PrintMessage1(strings[S_CalibWord1],calib1);	//"Calib1: 0x%04X\r\n"
	}
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	if(programID||load_calibword||ICDenable){
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		if(load_calibword){
			bufferU[j++]=INC_ADDR_N;
			bufferU[j++]=9;
		}
	}
	bufferU[j++]=BULK_ERASE_PROG;
	bufferU[j++]=WAIT_T3;			// delay T3=6ms
	bufferU[j++]=EN_VPP_VCC;		//exit program mode
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			// delay T3=6ms after exiting program mode
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x4;
	bufferU[j++]=NOP;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x5;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=3000>>8;
	bufferU[j++]=3000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(40);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 3ms
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-12||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*3+2);
			w=0;
			for(z=0;z<DIMBUF-5;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+3]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+4]<<8)+bufferI[z+5]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+4]<<8)+bufferI[z+5]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=6;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d,\n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write eeprom ********************
	if(dim2){
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		j=0;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=6000>>8;
		bufferU[j++]=6000&0xff;
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=BULK_ERASE_DATA;
		bufferU[j++]=WAIT_T3;			// delay T3=6ms
		bufferU[j++]=INC_ADDR_N;		//use only INC_ADDR_N so verification does not look at it
		bufferU[j++]=0xFF;
		bufferU[j++]=INC_ADDR_N;		//EEPROM: counter at 0x2100
		bufferU[j++]=1;
		for(w=2,i=k=0x2100;i<0x2100+dim2;i++){
			if(memCODE_W[i]<0xff){
				bufferU[j++]=LOAD_DATA_DATA;
				bufferU[j++]=memCODE_W[i]&0xff;
				bufferU[j++]=BEGIN_PROG;			//internally timed, T=6ms min
				bufferU[j++]=WAIT_T3;				//Tprogram 6ms
				bufferU[j++]=READ_DATA_DATA;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-12||i==0x2100+dim2-1){
				PrintStatus(strings[S_CodeWriting],(i-0x2100+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*6.5+2);
				w=0;
				for(z=0;z<DIMBUF-4;z++){
					if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_DATA&&bufferI[z+3]==READ_DATA_DATA){
						if (memCODE_W[k]!=bufferI[z+4]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memCODE_W[k],bufferI[z+4]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							err++;
							if(max_err&&err>max_err){
								PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"Write interrupted"
								i=0x2200;
								z=DIMBUF;
							}
						}
						k++;
						z+=5;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d \n"
				}
			}
		}
		err+=i-k;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],i-k);	//"completed, %d errors\r\n"
	}
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	int ICDgoto=0x2800+(ICDaddr&0x7FF);		//GOTO ICD routine (0x28xx)
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=3000>>8;
	bufferU[j++]=3000&0xff;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 3ms
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=4;
	}
	if(ICDenable){		//write a GOTO ICD routine (0x28xx)
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=ICDgoto>>8;			//MSB
		bufferU[j++]=ICDgoto&0xFF;			//LSB
		bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
		bufferU[j++]=WAIT_T3;				//Tprogram 3ms
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=3;
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG;				//internally timed, T=3ms min
	bufferU[j++]=WAIT_T3;					//Tprogram 3ms
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=LOAD_DATA_PROG;			//Config word2 0x2008
	bufferU[j++]=memCODE_W[0x2008]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2008]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG;				//internally timed, T=3ms min
	bufferU[j++]=WAIT_T3;					//Tprogram 3ms
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	if(load_calibword){
		bufferU[j++]=LOAD_DATA_PROG;		//Calib word 1
		bufferU[j++]=memCODE_W[0x2009]>>8;	//MSB
		bufferU[j++]=memCODE_W[0x2009]&0xff;	//LSB
		bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
		bufferU[j++]=WAIT_T3;				//Tprogram 3ms
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(35);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	if(ICDenable){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (ICDgoto!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage4(strings[S_ICDErr],0x2004,i,ICDgoto,(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ICD (0x%X): written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2008]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2008],(bufferI[z+1]<<8)+bufferI[z+2]);	//"\r\n"
		err_c++;
	}
	if(load_calibword){
		for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2009]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_Calib1Err],memCODE_W[0x2009],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing Calib1: written %04X, read %04X\r\n"
			err_c++;
		}
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F7x(int dim,int vdd)
// dim=program size
// write 14 bit PIC
// vdd=0  vdd +50ms before vpp
// vdd=1  vdd before vpp
// DevID@0x2006
// Config@0x2007
// Config2@0x2008
// erase: BULK_ERASE_PROG (1001) +30ms
// write:LOAD_DATA_PROG (0010) + BEGIN_PROG (1000) + 1ms + END_PROG2(1110)
// verify during write
{
	int err=0;
	WORD devID=0x3fff;
	int k=0,z=0,i,j,w;
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write16F7x(%d,%d)\n",dim,vdd);
	}
	for(i=0;i<0x2009&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=10000>>8;
	bufferU[j++]=10000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	if(vdd==0){
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
	}
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(vdd?20:70);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	//enter program mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	if(vdd==0){
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
	}
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=BULK_ERASE_PROG;
	bufferU[j++]=WAIT_T3;			// delay T3=10ms
	bufferU[j++]=WAIT_T3;			// delay T3=10ms
	bufferU[j++]=WAIT_T3;			// delay T3=10ms
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(vdd?50:100);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
	j=0;
	//enter program mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	if(vdd==0){
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
	}
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=1000>>8;
	bufferU[j++]=1000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(vdd?2:52);
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//externally timed, T=1ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 1ms min
			bufferU[j++]=END_PROG2;
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-10||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*1.5+2);
			w=0;
			for(z=0;z<DIMBUF-6;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+4]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+5]<<8)+bufferI[z+6]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+5]<<8)+bufferI[z+6]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=7;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d,\n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//externally timed, T=1ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 1ms
			bufferU[j++]=END_PROG2;
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=3;
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=7;
	}
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG;			//externally timed, T=1ms min
	bufferU[j++]=WAIT_T3;				//Tprogram 1ms
	bufferU[j++]=END_PROG2;
	bufferU[j++]=WAIT_T3;				//Tprogram 1ms
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	if(sizeW>0x2008&&memCODE_W[0x2008]<0x3fff){
		bufferU[j++]=LOAD_DATA_PROG;			//Config word 2 0x2008
		bufferU[j++]=memCODE_W[0x2008]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x2008]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG;			//externally timed, T=1ms min
		bufferU[j++]=WAIT_T3;				//Tprogram 1ms
		bufferU[j++]=END_PROG2;
		bufferU[j++]=WAIT_T3;				//Tprogram 1ms
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(15);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	if(sizeW>0x2008&&memCODE_W[0x2008]<0x3fff){
		for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if(~memCODE_W[0x2008]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2008],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
			err_c++;
		}
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F71x(int dim,int vdd)
// write 14 bit PIC
// dim=program size
// vdd=0  vdd +50ms before vpp
// vdd=1  vdd before vpp
// DevID@0x2006
// Config@0x2007
// erase: BULK_ERASE_PROG (1001) +6ms
// write:LOAD_DATA_PROG (0010) + BEGIN_PROG2 (11000) + 2ms + END_PROG2(1110)
// verify during write
{
	int err=0;
	WORD devID=0x3fff;
	int k=0,z=0,i,j,w;
	if(sizeW<0x2007){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write16F71x(%d,%d)\n",dim,vdd);
	}
	for(i=0;i<0x2009&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=10000>>8;
	bufferU[j++]=10000&0xff;
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	if(vdd==0){
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
	}
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(vdd?20:70);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	//enter program mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	if(vdd==0){
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
	}
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=NOP;
	if(programID){
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
	}
	bufferU[j++]=BULK_ERASE_PROG;
	bufferU[j++]=WAIT_T3;			// delay T3=10ms
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=WAIT_T3;			//delay after exiting prog mode
	//enter program mode
	bufferU[j++]=EN_VPP_VCC;		//VDD
	bufferU[j++]=0x1;
	if(vdd==0){
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
		bufferU[j++]=WAIT_T3;			//delay between vdd and vpp
	}
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(vdd?30:130);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//externally timed, T=1ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 1ms min
			bufferU[j++]=END_PROG2;
			bufferU[j++]=WAIT_T2;				//Tdischarge 100us
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-11||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*2.5+2);
			w=0;
			for(z=0;z<DIMBUF-7;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+5]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+6]<<8)+bufferI[z+7]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+6]<<8)+bufferI[z+7]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=8;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d,\n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG2;			//externally timed, T=1ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 1ms
			bufferU[j++]=END_PROG2;
			bufferU[j++]=WAIT_T2;				//Tdischarge 100us
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=3;
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=7;
	}
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG2;			//externally timed, T=1ms min
	bufferU[j++]=WAIT_T3;				//Tprogram 1ms
	bufferU[j++]=END_PROG2;
	bufferU[j++]=WAIT_T2;				//Tdischarge 100us
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	if(sizeW>0x2008&&memCODE_W[0x2008]<0x3fff){
		bufferU[j++]=LOAD_DATA_PROG;			//Config word 2 0x2008
		bufferU[j++]=memCODE_W[0x2008]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x2008]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG2;			//externally timed, T=1ms min
		bufferU[j++]=WAIT_T3;				//Tprogram 1ms
		bufferU[j++]=END_PROG2;
		bufferU[j++]=WAIT_T2;				//Tdischarge 100us
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(15);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	if(sizeW>0x2008&&memCODE_W[0x2008]<0x3fff){
		for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if(~memCODE_W[0x2008]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2008],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
			err_c++;
		}
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F72x(int dim, int d, int d2)
// write 14 bit PIC
// dim=program size
// d not used
// vpp before vdd
// DevID@0x2006
// Config@0x2007
// Config2@0x2008 (not used on LF devices)
// erase: BULK_ERASE_PROG (1001) +6ms
// write:LOAD_DATA_PROG (0010) + BEGIN_PROG (1000) + 2.5ms
// verify during write
{
	int err=0;
	WORD devID=0x3fff;
	int k=0,z=0,i,j,w;
	if(!CheckV33Regulator()){
		PrintMessage(strings[S_noV33reg]);	//Can't find 3.3V expansion board
		return;
	}
	if(!StartHVReg(8.5)){
		PrintMessage(strings[S_HVregErr]); //"HV regulator error\r\n"
		return;
	}
	if(sizeW<0x2008){
		PrintMessage(strings[S_NoConfigW3]);	//"Can't find CONFIG (0x2007)\r\nEnd\r\n"
		return;
	}
	if(saveLog){
		OpenLogFile();	//"log.txt"
		fprintf(logfile,"Write16F72x(%d)\n",dim);
	}
	for(i=0;i<0x2009&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=EN_VPP_VCC;		//VPP
	bufferU[j++]=0x4;
	bufferU[j++]=EN_VPP_VCC;		//VDD+VPP
	bufferU[j++]=0x5;
	bufferU[j++]=WAIT_T2;
	bufferU[j++]=WAIT_T2;
	bufferU[j++]=WAIT_T2;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x06;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=CUST_CMD;
	bufferU[j++]=0x16;		//Reset address
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=6000>>8;
	bufferU[j++]=6000&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(3);
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erase ... "
	j=0;
	if(programID||ICDenable){
		bufferU[j++]=LOAD_CONF;			//counter at 0x2000
		bufferU[j++]=0xFF;				//fake config
		bufferU[j++]=0xFF;				//fake config
	}
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=BULK_ERASE_PROG;
	bufferU[j++]=WAIT_T3;			// delay T3=6ms
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=CUST_CMD;
	bufferU[j++]=0x16;		//Reset address
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2500>>8;
	bufferU[j++]=2500&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(18);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	for(w=i=k=0,j=0;i<dim;i++){
		if(memCODE_W[i]<0x3fff){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 3ms
			bufferU[j++]=READ_DATA_PROG;
			w++;
		}
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF-12||i==dim-1){
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, add. %03X"
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(w*2.5+2);
			w=0;
			for(z=0;z<DIMBUF-5;z++){
				if(bufferI[z]==INC_ADDR&&memCODE_W[k]>=0x3fff) k++;
				else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+3]==READ_DATA_PROG){
					if (memCODE_W[k]!=(bufferI[z+4]<<8)+bufferI[z+5]){
						PrintMessage("\r\n");
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+4]<<8)+bufferI[z+5]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
						if(max_err&&err>max_err){
							PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
							PrintMessage(strings[S_IntW]);	//"Write interrupted"
							i=dim;
							z=DIMBUF;
						}
					}
					k++;
					z+=6;
				}
			}
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d,\n"
			}
		}
	}
	err+=i-k;
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
//****************** write ID, CONFIG, CALIB ********************
	PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
	int err_c=0;
	int ICDgoto=0x2800+(ICDaddr&0x7FF);		//GOTO ICD routine (0x28xx)
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=6000>>8;
	bufferU[j++]=6000&0xff;
	bufferU[j++]=LOAD_CONF;			//counter at 0x2000
	bufferU[j++]=0xFF;				//fake config
	bufferU[j++]=0xFF;				//fake config
	if(programID){
		for(i=0x2000;i<0x2004;i++){
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
			bufferU[j++]=WAIT_T3;				//Tprogram 3ms
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
	}
	else{
		bufferU[j++]=INC_ADDR_N;
		bufferU[j++]=4;
	}
	if(ICDenable){		//write a GOTO ICD routine (0x28xx)
		bufferU[j++]=LOAD_DATA_PROG;
		bufferU[j++]=ICDgoto>>8;		//MSB
		bufferU[j++]=ICDgoto&0xFF;			//LSB
		bufferU[j++]=BEGIN_PROG;			//internally timed, T=3ms min
		bufferU[j++]=WAIT_T3;				//Tprogram 3ms
		bufferU[j++]=READ_DATA_PROG;
	}
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=3;
	bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x2007
	bufferU[j++]=memCODE_W[0x2007]>>8;		//MSB
	bufferU[j++]=memCODE_W[0x2007]&0xff;		//LSB
	bufferU[j++]=BEGIN_PROG;				//internally timed, T=5ms min
	bufferU[j++]=WAIT_T3;					//Tprogram 3ms
	bufferU[j++]=READ_DATA_PROG;
	bufferU[j++]=INC_ADDR;
	if(sizeW>0x2008){						//only if Config2 is present
		bufferU[j++]=LOAD_DATA_PROG;			//Config word 2 0x2008
		bufferU[j++]=memCODE_W[0x2008]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x2008]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG;				//internally timed, T=5ms min
		bufferU[j++]=WAIT_T3;					//Tprogram 3ms
		bufferU[j++]=READ_DATA_PROG;
		bufferU[j++]=FLUSH;
	}
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(45);
	for(i=0,z=0;programID&&i<4;i++){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (memCODE_W[0x2000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage("\r\n");
			PrintMessage3(strings[S_IDErr],i,memCODE_W[0x2000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	if(ICDenable){
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if (ICDgoto!=(bufferI[z+1]<<8)+bufferI[z+2]){
			PrintMessage4(strings[S_ICDErr],0x2004,i,ICDgoto,(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ICD (0x%X): written %04X, read %04X\r\n"
			err_c++;
		}
		z+=6;
	}
	for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	if(~memCODE_W[0x2007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
		PrintMessage("\r\n");
		PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
		err_c++;
	}
	if(sizeW>0x2008){						//only if Config2 is present
		for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if(~memCODE_W[0x2008]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x2008],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
			err_c++;
		}
	}
	err+=err_c;
	PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
	if(saveLog){
		fprintf(logfile,strings[S_Log9],err);	//"Area config. 	errors=%d \n"
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}

void Write16F1xxx(int dim,int dim2,int options)
// write 14 bit enhanced PIC
// dim=program size
// dim2=eeprom size
// options:
//		bit0=0 -> vpp before vdd
//		bit0=1 -> vdd before vpp
//		bit1=1 -> LVP programming
//		bit2=1 -> Config3@0x8009
//		bit3=1 -> Config4@0x800A
//		bit4=1 -> PIC16F18xxx (calib words @0xE000 + new commands)
// DevREV@0x8005     DevID@0x8006
// Config1@0x8007    Config2@0x8008
// Config3@0x8009    Config4@0x800A
// Calib1@0x8009/A   Calib2@0x800A/B    Calib3@0x800B/C
// eeprom@0x0 or 0xF000
// erase: BULK_ERASE_PROG (1001) +5ms
// write:LOAD_DATA_PROG (0010) + BEGIN_PROG (1000) + 2.5ms (8 word algorithm)
// config write time 5ms
// eeprom:	BULK_ERASE_DATA (1011) + 5ms
//			LOAD_DATA_DATA (0011) + BEGIN_PROG (1000) + 2.5ms
// verify after write
// write (16F18xxx): LOAD_DATA_INC (100010) + BEGIN_PROG (1000) + 2.5ms (32 word algorithm)
{
	int err=0;
	WORD devID=0x3fff,devREV=0x3fff,calib1=0x3fff,calib2=0x3fff,calib3=0x3fff;
	int k=0,k2=0,z=0,i,j,w;
	int F18x=options&16;
	if(F18x&&FWVersion<0xA00){		//only for 16F18xxx
		PrintMessage1(strings[S_FWver2old],"0.10.0");	//"This firmware is too old. Version %s is required\r\n"
		return;
	}
	if(!CheckV33Regulator()){
		PrintMessage(strings[S_noV33reg]);	//Can't find 3.3V expansion board
		return;
	}
	if(sizeW<0x8009){
		PrintMessage(strings[S_NoConfigW5]);	//"Can't find CONFIG (0x8007-0x8008)\r\n"
		PrintMessage(strings[S_End]);
		return;
	}
	if((options&4)&&sizeW<0x800A){		//Config3 defaults to 0x3FFF
		sizeW=0x800A;
		memCODE_W=(WORD*)realloc(memCODE_W,sizeof(WORD)*sizeW);
		memCODE_W[0x8009]=0x3FFF;
	}
	if((options&8)&&sizeW<0x800B){		//Config4 defaults to 0x3FFF
		sizeW=0x800B;
		memCODE_W=(WORD*)realloc(memCODE_W,sizeof(WORD)*sizeW);
		memCODE_W[0x800A]=0x3FFF;
	}
	if(saveLog){
		OpenLogFile();	//"Log.txt"
		fprintf(logfile,"Write16F1xxx(%d,%d,%d)\n",dim,dim2,options);
	}
	if(dim2>sizeEE) dim2=sizeEE;
	if((options&2)==0){				//HV entry
			if(!StartHVReg(8.5)){
			PrintMessage(strings[S_HVregErr]); //"HV regulator error\r\n"
			return;
		}
	}
	else StartHVReg(-1);			//LVP mode, turn off HV
	for(i=0;i<0x800C&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
//	bufferU[0]=0;
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	if((options&2)==0){				//HV entry
		if((options&1)==0){				//VPP before VDD
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=4;				//VPP
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=0x5;			//VDD+VPP
		}
		else{							//VDD before VPP without delay
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=1;				//VDD
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=0x5;			//VDD+VPP
		}
	}
	else{			//Low voltage programming
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=4;				//VPP
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=TX16;			//0000 1010 0001 0010 1100 0010 1011 0010 = 0A12C2B2
		bufferU[j++]=2;
		bufferU[j++]=0x0A;
		bufferU[j++]=0x12;
		bufferU[j++]=0xC2;
		bufferU[j++]=0xB2;
		bufferU[j++]=SET_CK_D;		//Clock pulse
		bufferU[j++]=0x4;
		bufferU[j++]=SET_CK_D;
		bufferU[j++]=0x0;
	}
	bufferU[j++]=WAIT_T2;
	bufferU[j++]=WAIT_T2;
	bufferU[j++]=WAIT_T2;
	bufferU[j++]=LOAD_CONF;			//counter at 0x8000
	bufferU[j++]=0xFF;
	bufferU[j++]=0xFF;
	bufferU[j++]=INC_ADDR_N;
	bufferU[j++]=0x05;
	bufferU[j++]=READ_DATA_PROG;	//DevREV
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//DevID
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=INC_ADDR;
	if(options&4) bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Calib1
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Calib2
	bufferU[j++]=INC_ADDR;
	bufferU[j++]=READ_DATA_PROG;	//Calib3
//	bufferU[j++]=CUST_CMD;
//	bufferU[j++]=0x16;		//Reset address
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2500>>8;
	bufferU[j++]=2500&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(5);
	j=0;
	for(z=0;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devREV=(bufferI[z+1]<<8)+bufferI[z+2];
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if(devREV<0x3FFF) PrintMessage1(strings[S_DevREV],devREV);	//"DevREV: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	if(memCODE_W[0x8006]<0x3FFF&&devID!=memCODE_W[0x8006]) PrintMessage(strings[S_DevMismatch]);	//"Warning: the device is different from what specified in source data"
	if(!F18x){		//16F1xxx
		for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		calib1=(bufferI[z+1]<<8)+bufferI[z+2];
		if(calib1<0x3fff) PrintMessage2(strings[S_CalibWordX],1,calib1);	//"Calibration word %d: 0x%04X\r\n"
		for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		calib2=(bufferI[z+1]<<8)+bufferI[z+2];
		if(calib2<0x3fff) PrintMessage2(strings[S_CalibWordX],2,calib2);	//"Calibration word %d: 0x%04X\r\n"
		for(z+=3;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		calib3=(bufferI[z+1]<<8)+bufferI[z+2];
		if(calib3<0x3fff) PrintMessage2(strings[S_CalibWordX],3,calib3);	//"Calibration word %d: 0x%04X\r\n"
	}
	else{		//16F18xxx
		WORD calib[4]={0x3FFF,0x3FFF,0x3FFF,0x3FFF};
		bufferU[j++]=LOAD_PC;			//counter at 0xE000
		bufferU[j++]=0xE0;
		bufferU[j++]=0x00;
		for(i=0;i<4;i++) bufferU[j++]=READ_DATA_INC;
		bufferU[j++]=FLUSH;
		for(;j<DIMBUF;j++) bufferU[j]=0x0;
		PacketIO(5);
		j=0;
		if(saveLog){
			fprintf(logfile,"Calibration area @0xE000\n");
		}
		for(z=0,i=0;z<DIMBUF-2&&i<4;z++){
			if(bufferI[z]==READ_DATA_INC){
				calib[i++]=(bufferI[z+1]<<8)+bufferI[z+2];
				z+=2;
			}
		}
		if(i!=4){
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ConfigErr],4,i);	//"Error reading config area, requested %d words, read %d\r\n"
		}
		else{
			for(i=0;i<4;i++) PrintMessage2(strings[S_CalibWordX],i+1,calib[i]);	//"Calibration word %d: 0x%04X\r\n"
		}
	}
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erasing ... "
	if(saveLog)	fprintf(logfile,"%s\n",strings[S_StartErase]);
	int dly=8;
	j=0;
	if(programID){
		bufferU[j++]=LOAD_CONF;			//PC @ 0x8000
		bufferU[j++]=0xFF;
		bufferU[j++]=0xFF;
	}
	else if(!F18x){
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x16;		//Reset address
	}
	else{	//18F18xxx
		bufferU[j++]=LOAD_PC;	//counter at 0
		bufferU[j++]=0x00;
		bufferU[j++]=0x00;
	}
	bufferU[j++]=BULK_ERASE_PROG;
	bufferU[j++]=WAIT_T3;			// wait 5ms
	bufferU[j++]=WAIT_T3;
	if(F18x){		//18F18xxx: EEPROM@0xF000
		bufferU[j++]=LOAD_PC;			//counter at 0xF000
		bufferU[j++]=0xF0;
		bufferU[j++]=0x00;
		bufferU[j++]=BULK_ERASE_PROG;
		bufferU[j++]=WAIT_T3;			// wait 5ms
		bufferU[j++]=WAIT_T3;
		dly+=6;
	}
	if(!F18x){
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x16;		//Reset address
	}
	else{	//18F18xxx
		bufferU[j++]=LOAD_PC;	//counter at 0
		bufferU[j++]=0x00;
		bufferU[j++]=0x00;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(dly);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	if(saveLog)	fprintf(logfile,"%s\n",strings[S_StartCodeProg]);
	fflush(logfile);
	int valid,inc;
	for(;dim>0&&memCODE_W[dim]>=0x3fff;dim--); //skip empty space at end
	if(!F18x){					//16F1xxx
	if(dim%8) dim+=8-dim%8;		//grow to 8 word multiple
	for(i=k=0,j=0;i<dim;i+=8){
		valid=inc=0;
		for(;i<dim&&!valid;){	//skip empty locations (8 words)
			valid=0;
			for(k=0;k<8;k++) if(memCODE_W[i+k]<0x3fff) valid=1;
			if(!valid){
				inc+=8;
				i+=8;
			}
			if(inc&&(valid||inc==248)){	//increase address to skip empty words
				bufferU[j++]=INC_ADDR_N;
				bufferU[j++]=k=inc;
				inc=0;
			}
			if(j>DIMBUF-4||(valid&&j>1)){
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(3+k/2);
				j=0;
			}
		}
		if(valid){
			k=0;
			bufferU[j++]=LOAD_DATA_PROG;
			bufferU[j++]=memCODE_W[i+k]>>8;  		//MSB
			bufferU[j++]=memCODE_W[i+k]&0xff;		//LSB
			for(k=1;k<8;k++){
				bufferU[j++]=INC_ADDR;
				bufferU[j++]=LOAD_DATA_PROG;
				bufferU[j++]=memCODE_W[i+k]>>8;  		//MSB
				bufferU[j++]=memCODE_W[i+k]&0xff;		//LSB
			}
			bufferU[j++]=BEGIN_PROG;			//internally timed, T=2.5ms
			bufferU[j++]=WAIT_T3;
			bufferU[j++]=INC_ADDR;
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(3);
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log7],i,i,0,0);	//"i=%d, k=%d 0=%d\n"
				}
				PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, addr. %03X"
			}
		}
	}
	else{					//16F18xxx 32 word algorithm
		if(dim%32) dim+=32-dim%32;		//grow to 32 word multiple
		for(i=k=0,j=0;i<dim;i+=32){
			for(valid=0;i<dim&&!valid;i+=valid?0:32){	//skip empty locations (32 words)
				valid=0;
				for(k=0;k<32;k++) if(memCODE_W[i+k]<0x3fff) valid=1;
			}
			bufferU[j++]=LOAD_PC;	//update counter
			bufferU[j++]=i>>8;
			bufferU[j++]=i&0xFF;
			for(k=0;k<32&&i<dim;k++){
				bufferU[j++]=k<31?LOAD_DATA_INC:LOAD_DATA_PROG;
				bufferU[j++]=memCODE_W[i+k]>>8;  		//MSB
				bufferU[j++]=memCODE_W[i+k]&0xff;		//LSB
				if(j>DIMBUF-4){
					bufferU[j++]=FLUSH;
					for(;j<DIMBUF;j++) bufferU[j]=0x0;
					PacketIO(3);
					j=0;
				}
			}
			if(i<dim){
				bufferU[j++]=BEGIN_PROG;			//internally timed, T=2.5ms
				bufferU[j++]=WAIT_T3;
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(3);
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log7],i,i,0,0);	//"i=%d, k=%d 0=%d\n"
			}
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, addr. %03X"
		}
	}
	}
	PrintStatusEnd();
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** verify code ********************
	PrintMessage(strings[S_CodeV]);	//"Verifying code ... "
	PrintStatusSetup();
	if(saveLog)	fprintf(logfile,"%s\n",strings[S_CodeV]);
	j=0;
	if(!F18x){
	bufferU[j++]=CUST_CMD;
	bufferU[j++]=0x16;		//Reset address
	}
	else{	//18F18xxx
		bufferU[j++]=LOAD_PC;	//counter at 0
		bufferU[j++]=0x00;
		bufferU[j++]=0x00;
	}
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	j=0;
	for(i=k=0;i<dim;i++){
		if(j==0){				//skip empty locations (only after a write)
			for(valid=0;i<dim&&!valid;){
				for(k2=0;k2<255&&!valid;k2++) if(memCODE_W[i+k2]<0x3fff) valid=1;
				if(k2>16){			//increase address to skip empty words, if enough are found
					bufferU[j++]=INC_ADDR_N;
					bufferU[j++]=k2;
					i+=k2;
					k+=k2;
				}
				if(j>DIMBUF-4||(valid&&j>1)){		//if buffer is full or last skip
					bufferU[j++]=FLUSH;
					for(;j<DIMBUF;j++) bufferU[j]=0x0;
					PacketIO(3+j/2*0.1);
					j=0;
				}
			}
		}
		if(memCODE_W[i]<0x3FFF) bufferU[j++]=READ_DATA_PROG;
		bufferU[j++]=INC_ADDR;
		if(j>DIMBUF*2/4-3||i==dim-1){		//2B cmd -> 4B data
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(5);
			for(z=0;z<DIMBUF-2;z++){
				if(bufferI[z]==INC_ADDR) k++;
				else if(bufferI[z]==READ_DATA_PROG){
					if(memCODE_W[k]<0x3FFF&&(memCODE_W[k]!=(bufferI[z+1]<<8)+bufferI[z+2])){
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
					}
					z+=2;
				}
			}
			PrintStatus(strings[S_CodeV2],i*100/(dim+dim2),i);	//"Verify: %d%%, addr. %04X"
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d\n"
			}
			if(err>=max_err) i=dim;
		}
	}
	PrintStatusEnd();
	if(k<dim){
		PrintMessage2(strings[S_CodeVError3],dim,k);	//"Error verifying code area, requested %d words, read %d\r\n"
	}
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
	if(err>=max_err){
		PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
	}
//****************** write eeprom ********************
	if(dim2&&err<max_err&&!F18x){
		int errEE=0;
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		if(saveLog)	fprintf(logfile,"%s\n",strings[S_EEAreaW]);
		j=0;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=5000>>8;
		bufferU[j++]=5000&0xff;
		bufferU[j++]=BULK_ERASE_DATA;
		bufferU[j++]=WAIT_T3;			// wait 5ms
		bufferU[j++]=CUST_CMD;
		bufferU[j++]=0x16;		//Reset address
		bufferU[j++]=FLUSH;
		for(;j<DIMBUF;j++) bufferU[j]=0x0;
		PacketIO(7);
		j=0;
		for(w=i=k=0;i<dim2;i++){
			if(memEE[i]<0xff){
				bufferU[j++]=LOAD_DATA_DATA;
				bufferU[j++]=memEE[i];
				bufferU[j++]=BEGIN_PROG;			//internally timed, T=5ms max
				bufferU[j++]=WAIT_T3;				//Tprogram
				bufferU[j++]=READ_DATA_DATA;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-12||i==dim2-1){
				PrintStatus(strings[S_CodeWriting],(i+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*5+2);
				w=0;
				for(z=0;z<DIMBUF-4;z++){
					if(bufferI[z]==INC_ADDR&&memEE[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_DATA&&bufferI[z+3]==READ_DATA_DATA){
						if (memEE[k]!=bufferI[z+4]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memEE[k],bufferI[z+4]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							errEE++;
							if(max_err&&err+errEE>max_err){
								PrintMessage1(strings[S_MaxErr],err+errEE);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"write interrupted"
								i=dim2;
								z=DIMBUF;
							}
						}
						k++;
						z+=5;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,errEE);	//"i=%d, k=%d, errors=%d\n"
				}
			}
		}
		errEE+=i-k;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],errEE);	//"completed, %d errors\r\n"
		err+=errEE;
	}
	else if(dim2&&err<max_err&&F18x){	//16F18xxx
		int errEE=0;
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		if(saveLog)	fprintf(logfile,"%s\n",strings[S_EEAreaW]);
		j=0;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=5000>>8;
		bufferU[j++]=5000&0xff;
		bufferU[j++]=LOAD_PC;	//update counter
		bufferU[j++]=0xF0;
		bufferU[j++]=0x00;
		bufferU[j++]=FLUSH;
		for(;j<DIMBUF;j++) bufferU[j]=0x0;
		PacketIO(7);
		j=0;
		for(w=i=k=0;i<dim2;i++){
			if(memEE[i]<0xff){
				bufferU[j++]=LOAD_DATA_PROG;
				bufferU[j++]=0;
				bufferU[j++]=memEE[i];
				bufferU[j++]=BEGIN_PROG;			//internally timed, T=2.5ms max
				bufferU[j++]=WAIT_T3;				//Tprogram
				bufferU[j++]=READ_DATA_PROG;
				w++;
			}
			bufferU[j++]=INC_ADDR;
			if(j>DIMBUF-12||i==dim2-1){
				PrintStatus(strings[S_CodeWriting],(i+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*5+2);
				w=0;
				for(z=0;z<DIMBUF-4;z++){
					if(bufferI[z]==INC_ADDR&&memEE[k]>=0xff) k++;
					else if(bufferI[z]==LOAD_DATA_PROG&&bufferI[z+3]==READ_DATA_PROG){
						if (memEE[k]!=bufferI[z+5]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memEE[k],bufferI[z+5]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							errEE++;
							if(max_err&&err+errEE>max_err){
								PrintMessage1(strings[S_MaxErr],err+errEE);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"write interrupted"
								i=dim2;
								z=DIMBUF;
							}
						}
						k++;
						z+=6;
					}
				}
				j=0;
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,errEE);	//"i=%d, k=%d, errors=%d\n"
				}
			}
		}
		errEE+=i-k;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],errEE);	//"completed, %d errors\r\n"
		err+=errEE;

	}
	if(err>=max_err){
		PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
	}
//****************** write ID, CONFIG, CALIB ********************
	if(max_err&&err<max_err){
		PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
		if(saveLog)	fprintf(logfile,"%s\n",strings[S_ConfigAreaW]);
		int err_c=0;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=5000>>8;
		bufferU[j++]=5000&0xff;
		bufferU[j++]=LOAD_CONF;			//PC @ 0x8000
		bufferU[j++]=0xFF;
		bufferU[j++]=0xFF;
		if(programID){
			for(i=0x8000;i<0x8004;i++){
				bufferU[j++]=LOAD_DATA_PROG;
				bufferU[j++]=memCODE_W[i]>>8;		//MSB
				bufferU[j++]=memCODE_W[i]&0xff;		//LSB
				bufferU[j++]=BEGIN_PROG;			//internally timed
				bufferU[j++]=WAIT_T3;				//Tprogram 5ms
				bufferU[j++]=READ_DATA_PROG;
				bufferU[j++]=INC_ADDR;
			}
			bufferU[j++]=INC_ADDR_N;
			bufferU[j++]=3;
		}
		else{
			bufferU[j++]=INC_ADDR_N;
			bufferU[j++]=7;
		}
		bufferU[j++]=LOAD_DATA_PROG;			//Config word 0x8007
		bufferU[j++]=memCODE_W[0x8007]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x8007]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG;			//internally timed
		bufferU[j++]=WAIT_T3;				//Tprogram 5ms
		bufferU[j++]=READ_DATA_PROG;
		bufferU[j++]=INC_ADDR;
		bufferU[j++]=LOAD_DATA_PROG;			//Config word 2
		bufferU[j++]=memCODE_W[0x8008]>>8;		//MSB
		bufferU[j++]=memCODE_W[0x8008]&0xff;		//LSB
		bufferU[j++]=BEGIN_PROG;			//internally timed
		bufferU[j++]=WAIT_T3;				//Tprogram 5ms
		bufferU[j++]=READ_DATA_PROG;
		bufferU[j++]=INC_ADDR;
		if(options&0xC){
			bufferU[j++]=LOAD_DATA_PROG;			//Config word 3
			bufferU[j++]=memCODE_W[0x8009]>>8;		//MSB
			bufferU[j++]=memCODE_W[0x8009]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed
			bufferU[j++]=WAIT_T3;				//Tprogram 5ms
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
		if(options&8){
			bufferU[j++]=LOAD_DATA_PROG;			//Config word 4
			bufferU[j++]=memCODE_W[0x800A]>>8;		//MSB
			bufferU[j++]=memCODE_W[0x800A]&0xff;		//LSB
			bufferU[j++]=BEGIN_PROG;			//internally timed
			bufferU[j++]=WAIT_T3;				//Tprogram 5ms
			bufferU[j++]=READ_DATA_PROG;
			bufferU[j++]=INC_ADDR;
		}
		bufferU[j++]=FLUSH;
		for(;j<DIMBUF;j++) bufferU[j]=0x0;
		j=(programID?22:0)+(options&0xC?10:0);
		PacketIO(18+j);
		for(i=0,z=0;programID&&i<4;i++){
			for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
				if (memCODE_W[0x8000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
				PrintMessage("\r\n");
				PrintMessage3(strings[S_IDErr],i,memCODE_W[0x8000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
				err_c++;
			}
			z+=6;
		}
		for(;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if(~memCODE_W[0x8007]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x8007],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
			err_c++;
		}
		for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
		if(~memCODE_W[0x8008]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
			PrintMessage("\r\n");
			PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x8008],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
			err_c++;
		}
		if(options&0xC){
			for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
			if(~memCODE_W[0x8009]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
				PrintMessage("\r\n");
				PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x8009],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
				err_c++;
			}
		}
		if(options&8){
			for(z+=6;z<DIMBUF-2&&bufferI[z]!=READ_DATA_PROG;z++);
			if(~memCODE_W[0x800A]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
				PrintMessage("\r\n");
				PrintMessage2(strings[S_ConfigWErr3],memCODE_W[0x800A],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing config area: written %04X, read %04X\r\n"
				err_c++;
			}
		}
		err+=err_c;
		PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
		if(saveLog){
			fprintf(logfile,strings[S_Log9],err);	//"Config area 	errors=%d \n"
		}
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}



void Write16F18xxx(int dim,int dim2,int options)
// write 14 bit enhanced PIC
// dim=program size (words)
// dim2=eeprom size (bytes)
// options:
//		bit0=0 -> vpp before vdd
//		bit0=1 -> vdd before vpp
//		bit1=1 -> LVP programming
//		bit4=1 -> do not use DIA&DCI
// DevREV@0x8005     DevID@0x8006
// Config@0x8007-800B
// Device info area @0x8100
// Device configuration info area @0x8200
// erase: BULK_ERASE_PROGRAM_MEM (0x18) +14ms
// EEPROM erase: idem
// write flash: LOAD_NVM (0x0/2) + BEGIN_INT_PROG (0xE0) + 2.8ms (32 word algorithm)
// EEPROM write: one word, 6ms
// config write: one word, 10ms
// verify after write
{
	int err=0;
	WORD devID=0x3fff,devREV=0x3fff;
	int k=0,k2=0,z=0,i,j,w;
	int useDCI=(options&0x10)==0?1:0;
	int rowN=32;		//32 word algorithm
	if(FWVersion<0xB00){
		PrintMessage1(strings[S_FWver2old],"0.11.0");	//"This firmware is too old. Version %s is required\r\n"
		return;
	}
	if(!CheckV33Regulator()){
		PrintMessage(strings[S_noV33reg]);	//Can't find 3.3V expansion board
		return;
	}
	if(sizeW<0x800C){
		PrintMessage(strings[S_NoConfigW6]);	//"Can't find CONFIG (0x8007-0x800B)\r\n"
		PrintMessage1(strings[S_End],0);
		return;
	}
	if(saveLog){
		OpenLogFile();	//"Log.txt"
		fprintf(logfile,"Write16F18xxx(%d,%d,%d)\n",dim,dim2,options);
	}
	if(dim2>0) dim2=sizeEE;
	if(dim2>0x1000) dim2=0x100;
	if((options&2)==0){				//HV entry
			if(!StartHVReg(8.5)){
			PrintMessage(strings[S_HVregErr]); //"HV regulator error\r\n"
			return;
		}
	}
	else StartHVReg(-1);			//LVP mode, turn off HV
	for(i=0;i<0x800C&&i<sizeW;i++) memCODE_W[i]&=0x3FFF;
	unsigned int start=GetTickCount();
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T1T2;
	bufferU[j++]=1;						//T1=1u
	bufferU[j++]=100;					//T2=100u
	bufferU[j++]=EN_VPP_VCC;		//enter program mode
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	if((options&2)==0){				//HV entry
		if((options&1)==0){				//VPP before VDD
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=4;				//VPP
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=0x5;			//VDD+VPP
		}
		else{							//VDD before VPP without delay
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=1;				//VDD
			bufferU[j++]=EN_VPP_VCC;
			bufferU[j++]=0x5;			//VDD+VPP
		}
	}
	else{			//Low voltage programming
		bufferU[j++]=EN_VPP_VCC;
		bufferU[j++]=4;				//VPP
		bufferU[j++]=WAIT_T3;
		bufferU[j++]=TX16;			//0000 1010 0001 0010 1100 0010 1011 0010 = 0A12C2B2
		bufferU[j++]=2;
		bufferU[j++]=0x0A;
		bufferU[j++]=0x12;
		bufferU[j++]=0xC2;
		bufferU[j++]=0xB2;
		bufferU[j++]=SET_CK_D;		//Clock pulse
		bufferU[j++]=0x4;
		bufferU[j++]=SET_CK_D;
		bufferU[j++]=0x0;
	}
	bufferU[j++]=WAIT_T2;
	bufferU[j++]=WAIT_T2;
	bufferU[j++]=WAIT_T2;
	bufferU[j++]=ICSP8_LOAD;
	bufferU[j++]=LOAD_PC_ADDR;
	bufferU[j++]=0x80;
	bufferU[j++]=0x05;
	bufferU[j++]=ICSP8_READ;
	bufferU[j++]=READ_NVM_INC;	//DevREV
	bufferU[j++]=ICSP8_READ;
	bufferU[j++]=READ_NVM_INC;	//DevID
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2800>>8;
	bufferU[j++]=2800&0xff;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(5);
	j=0;
	for(z=0;z<DIMBUF-2&&bufferI[z]!=ICSP8_READ;z++);
	devREV=(bufferI[z+1]<<8)+bufferI[z+2];
	for(z+=3;z<DIMBUF-2&&bufferI[z]!=ICSP8_READ;z++);
	devID=(bufferI[z+1]<<8)+bufferI[z+2];
	PrintMessage1(strings[S_DevID],devID);	//"DevID: 0x%04X\r\n"
	if(devREV<0x3FFF) PrintMessage1(strings[S_DevREV],devREV);	//"DevREV: 0x%04X\r\n"
	if (!PIC16_ID(devID)) return;
	if(memCODE_W[0x8006]<0x3FFF&&devID!=memCODE_W[0x8006]) PrintMessage(strings[S_DevMismatch]);	//"Warning: the device is different from what specified in source data"
//****************** DIA-DCI ********************
	if(useDCI){	//if not disabled
		if(saveLog) fprintf(logfile,"Device Information Area @0x8100\n");
		WORD DIA[0x20];
		bufferU[j++]=ICSP8_LOAD;			//counter at 0x8100
		bufferU[j++]=LOAD_PC_ADDR;
		bufferU[j++]=0x81;
		bufferU[j++]=0x00;
		k=0;
		for(i=0;i<0x20;i++){		//DIA
			bufferU[j++]=ICSP8_READ;
			bufferU[j++]=READ_NVM_INC;
			if((j+1)/2*3+3>DIMBUF||i==0x1F){		//2B cmd -> 3B data
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(5);
				for(z=0;z<DIMBUF-2;z++){
					if(bufferI[z]==ICSP8_READ){
						DIA[k++]=(bufferI[z+1]<<8)+bufferI[z+2];
						z+=2;
					}
				}
				j=0;
			}
		}
		PrintMessage("Device Information Area @0x8100\r\n");
		char s[256],t[256];
		s[0]=0;
		for(i=0;i<0x20;i+=COL){
			sprintf(t,"%04X: ",0x8100+i);
			strcat(s,t);
			for(j=i;j<i+COL&&j<0x20;j++){
				sprintf(t,"%04X ",DIA[j]);
				strcat(s,t);
			}
			strcat(s,"\r\n");
		}
		PrintMessage(s);
		if(saveLog) fprintf(logfile,"Device Configuration Information @0x8200\n");
		WORD DCI[0x20];
		j=0;
		k=0;
		bufferU[j++]=ICSP8_LOAD;
		bufferU[j++]=LOAD_PC_ADDR;
		bufferU[j++]=0x82;
		bufferU[j++]=0x00;
		for(i=0;i<0x20;i++){		//DCI
			bufferU[j++]=ICSP8_READ;
			bufferU[j++]=READ_NVM_INC;
			if((j+1)/2*3+3>DIMBUF||i==0x1F){		//2B cmd -> 3B data
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(5);
				for(z=0;z<DIMBUF-2;z++){
					if(bufferI[z]==ICSP8_READ){
						DCI[k++]=(bufferI[z+1]<<8)+bufferI[z+2];
						z+=2;
					}
				}
				j=0;
			}
		}
		PrintMessage("Device Configuration Information @0x8200\r\n");
		s[0]=0;
		for(i=0;i<0x20;i+=COL){
			sprintf(t,"%04X: ",0x8200+i);
			strcat(s,t);
			for(j=i;j<i+COL&&j<0x20;j++){
				sprintf(t,"%04X ",DCI[j]);
				strcat(s,t);
			}
			strcat(s,"\r\n");
		}
		PrintMessage(s);
		PrintMessage1("Erase row size: %d words\r\n",DCI[0]);
		PrintMessage1("Write latches: %d\r\n",DCI[1]);
		PrintMessage1("User rows: %d\r\n",DCI[2]);
		PrintMessage1("->%d Flash words\r\n",DCI[0]*DCI[2]);
		PrintMessage1("EE data memory size: %d\r\n",DCI[3]);
		PrintMessage1("Pin count: %d\r\n",DCI[4]);
		if(DCI[0]*DCI[2]!=dim) PrintMessage(strings[S_WarnFlashSize]);	//"Warning, flash size is different from the expected value"
		if(dim2>DCI[3]) dim2=DCI[3];	//limit EE to the real one
	}
//****************** erase memory ********************
	PrintMessage(strings[S_StartErase]);	//"Erasing ... "
	if(saveLog)	fprintf(logfile,"%s\n",strings[S_StartErase]);
	j=0;
	bufferU[j++]=ICSP8_LOAD;
	bufferU[j++]=LOAD_PC_ADDR;
	if(programID){	//0x8000 to erase UserID
		bufferU[j++]=0x80;
		bufferU[j++]=0x00;
	}
	else{
		bufferU[j++]=0x00;
		bufferU[j++]=0x00;
	}
	bufferU[j++]=ICSP8_SHORT;
	bufferU[j++]=BULK_ERASE_PROGRAM_MEM;
	bufferU[j++]=WAIT_T3;			// wait ~14ms
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=WAIT_T3;
	//separate EEPROM erase (undocumented on 16F184xx devices)
	bufferU[j++]=ICSP8_LOAD;
	bufferU[j++]=LOAD_PC_ADDR;
	bufferU[j++]=0xF0;
	bufferU[j++]=0x00;
	bufferU[j++]=ICSP8_SHORT;
	bufferU[j++]=BULK_ERASE_PROGRAM_MEM;
	bufferU[j++]=WAIT_T3;			// wait ~14ms
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=WAIT_T3;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(30);
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** write code ********************
	PrintMessage(strings[S_StartCodeProg]);	//"Write code ... "
	PrintStatusSetup();
	if(saveLog)	fprintf(logfile,"%s\n",strings[S_StartCodeProg]);
	fflush(logfile);
	int valid;
	for(;dim>0&&memCODE_W[dim-1]>=0x3fff;dim--); //skip empty space at end
	if(dim%rowN) dim+=rowN-dim%rowN;		//grow to 32 word multiple
	for(i=k=0,j=0;i<dim;i+=rowN){
		for(valid=0;i<dim&&!valid;i+=valid?0:rowN){	//skip empty locations (32 words)
			valid=0;
			for(k=0;k<rowN;k++) if(memCODE_W[i+k]<0x3fff) valid=1;
		}
		bufferU[j++]=ICSP8_LOAD;
		bufferU[j++]=LOAD_PC_ADDR;	//update counter
		bufferU[j++]=i>>8;
		bufferU[j++]=i&0xFF;
		for(k=0;k<rowN&&i<dim;k++){	//load all latches
			bufferU[j++]=ICSP8_LOAD;
			bufferU[j++]=k<(rowN-1)?LOAD_NVM_INC:LOAD_NVM;
			bufferU[j++]=memCODE_W[i+k]>>8;  		//MSB
			bufferU[j++]=memCODE_W[i+k]&0xff;		//LSB
			if(j>DIMBUF-5){
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(3);
				j=0;
			}
		}
		if(i<dim){
			bufferU[j++]=ICSP8_SHORT;
			bufferU[j++]=BEGIN_INT_PROG;			//internally timed, T=2.8ms
			bufferU[j++]=WAIT_T3;
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(3);
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log7],i,i,0,0);	//"i=%d, k=%d 0=%d\n"
			}
			PrintStatus(strings[S_CodeWriting],i*100/dim,i);	//"Writing: %d%%, addr. %03X"
		}
	}
	PrintStatusEnd();
	PrintMessage(strings[S_Compl]);	//"completed\r\n"
//****************** verify code ********************
	PrintMessage(strings[S_CodeV]);	//"Verifying code ... "
	PrintStatusSetup();
	if(saveLog)	fprintf(logfile,"%s\n",strings[S_CodeV]);
	j=0;
	bufferU[j++]=ICSP8_LOAD;
	bufferU[j++]=LOAD_PC_ADDR;	//update counter
	bufferU[j++]=0x00;
	bufferU[j++]=0x00;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	j=0;
	for(i=k=0;i<dim;i++){
		if(j==0){				//skip empty locations (only at the beginning of a packet)
			for(k2=i;k2<dim&&memCODE_W[k2]>=0x3fff;k2++);
			if(k2>i+10){			//at least 10 skipped
				i=k2;
				k=i;
				bufferU[j++]=ICSP8_LOAD;
				bufferU[j++]=LOAD_PC_ADDR;	//update counter
				bufferU[j++]=(i>>8)&0xFF;
				bufferU[j++]=i&0xFF;
			}
		}
		bufferU[j++]=ICSP8_READ;
		bufferU[j++]=READ_NVM_INC;
		if((j+1)/2*3+3>DIMBUF||i==dim-1){		//2B cmd -> 3B data
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(5);
			for(z=0;z<DIMBUF-2;z++){
				if(bufferI[z]==ICSP8_READ){
					if(memCODE_W[k]!=(bufferI[z+1]<<8)+bufferI[z+2]){
						PrintMessage3(strings[S_CodeWError2],k,memCODE_W[k],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing address %3X: written %04X, read %04X\r\n"
						err++;
					}
					z+=2;
					k++;
				}
			}
			PrintStatus(strings[S_CodeV2],i*100/(dim+dim2),i);	//"Verify: %d%%, addr. %04X"
			j=0;
			if(saveLog){
				fprintf(logfile,strings[S_Log8],i,i,k,k,err);	//"i=%d, k=%d, errors=%d\n"
			}
			if(err>=max_err) i=dim;
		}
	}
	PrintStatusEnd();
	PrintMessage1(strings[S_ComplErr],err);	//"completed, %d errors\r\n"
	if(err>=max_err){
		PrintMessage1(strings[S_MaxErr],err);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
	}
//****************** write eeprom ********************
	else if(dim2&&err<max_err){
		int errEE=0;
		PrintMessage(strings[S_EEAreaW]);	//"Writing EEPROM ... "
		PrintStatusSetup();
		if(saveLog)	fprintf(logfile,"%s\n",strings[S_EEAreaW]);
		j=0;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=5600>>8;		//5.6ms in datasheet?
		bufferU[j++]=5600&0xff;
		bufferU[j++]=ICSP8_LOAD;
		bufferU[j++]=LOAD_PC_ADDR;	//load address
		bufferU[j++]=0xF0;
		bufferU[j++]=0x00;
		for(w=i=k=0;i<dim2;i++){
			if(memEE[i]<0xff){
				bufferU[j++]=ICSP8_LOAD;
				bufferU[j++]=LOAD_NVM;
				bufferU[j++]=0;  		//MSB
				bufferU[j++]=memEE[i]&0xff;		//LSB
				bufferU[j++]=ICSP8_SHORT;
				bufferU[j++]=BEGIN_INT_PROG;		//internally timed
				bufferU[j++]=WAIT_T3;				//Tprogram XXms
				bufferU[j++]=ICSP8_READ;
				bufferU[j++]=READ_NVM_INC;
				w++;
			}
			else{
				bufferU[j++]=ICSP8_SHORT;
				bufferU[j++]=INC_ADDR8;
			}
			if(j>DIMBUF-10||i==dim2-1){
				PrintStatus(strings[S_CodeWriting],(i+dim)*100/(dim+dim2),i);	//"Writing: %d%%, add. %03X"
				bufferU[j++]=FLUSH;
				for(;j<DIMBUF;j++) bufferU[j]=0x0;
				PacketIO(w*6+2);
				j=0;
				w=0;
				for(z=0;z<DIMBUF-5;z++){
					if(bufferI[z]==ICSP8_SHORT&&memEE[k]>=0xff) k++;
					else if(bufferI[z]==ICSP8_LOAD&&bufferI[z+3]==ICSP8_READ){
						if(memEE[k]!=bufferI[z+5]){
							PrintMessage("\r\n");
							PrintMessage3(strings[S_CodeWError3],k,memEE[k],bufferI[z+5]);	//"Error writing address %4X: written %02X, read %02X\r\n"
							errEE++;
							if(max_err&&err+errEE>max_err){
								PrintMessage1(strings[S_MaxErr],err+errEE);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
								PrintMessage(strings[S_IntW]);	//"write interrupted"
								i=dim2;
								z=DIMBUF;
							}
						}
						k++;
						z+=5;
					}
				}
				if(saveLog){
					fprintf(logfile,strings[S_Log8],i,i,k,k,errEE);	//"i=%d, k=%d, errors=%d\n"
				}
			}
		}
		errEE+=i-k;
		PrintStatusEnd();
		PrintMessage1(strings[S_ComplErr],errEE);	//"completed, %d errors\r\n"
		if(max_err&&err+errEE>max_err){
			PrintMessage1(strings[S_MaxErr],err+errEE);	//"Exceeded maximum number of errors (%d), write interrupted\r\n"
		}
		err+=errEE;
	}
//****************** write ID, CONFIG, CALIB ********************
	if(max_err&&err<max_err){
		char t[256];
		PrintMessage(strings[S_ConfigAreaW]);	//"Writing CONFIG area ... "
		PrintMessage("\r\n");
		if(saveLog)	fprintf(logfile,"%s\n",strings[S_ConfigAreaW]);
		int err_c=0;
		bufferU[j++]=SET_PARAMETER;
		bufferU[j++]=SET_T3;
		bufferU[j++]=10000>>8;		//5.6ms in datasheet, but requires more in practice
		bufferU[j++]=10000&0xff;
		if(programID){
			bufferU[j++]=ICSP8_LOAD;
			bufferU[j++]=LOAD_PC_ADDR;	//load address
			bufferU[j++]=0x80;
			bufferU[j++]=0x00;
			for(i=0x8000;i<0x8004;i++){
				bufferU[j++]=ICSP8_LOAD;
				bufferU[j++]=LOAD_NVM;
				bufferU[j++]=memCODE_W[i]>>8;		//MSB
				bufferU[j++]=memCODE_W[i]&0xff;		//LSB
				bufferU[j++]=ICSP8_SHORT;
				bufferU[j++]=BEGIN_INT_PROG;		//internally timed
				bufferU[j++]=WAIT_T3;				//Tprogram 10ms
				bufferU[j++]=ICSP8_READ;
				bufferU[j++]=READ_NVM_INC;
			}
			bufferU[j++]=FLUSH;
			for(;j<DIMBUF;j++) bufferU[j]=0x0;
			PacketIO(45);
			for(i=0,z=0;i<4;i++){
				for(;z<DIMBUF-2&&bufferI[z]!=ICSP8_READ;z++);
				if (memCODE_W[0x8000+i]!=(bufferI[z+1]<<8)+bufferI[z+2]){
					PrintMessage3(strings[S_IDErr],i,memCODE_W[0x8000+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing ID%d: written %04X, read %04X\r\n"
					err_c++;
				}
				z+=6;
			}
			j=0;
		}
		bufferU[j++]=ICSP8_LOAD;
		bufferU[j++]=LOAD_PC_ADDR;	//load address
		bufferU[j++]=0x80;
		bufferU[j++]=0x07;
		for(i=0x8007;i<0x800C;i++){		//5 config words
			bufferU[j++]=ICSP8_LOAD;
			bufferU[j++]=LOAD_NVM;
			bufferU[j++]=memCODE_W[i]>>8;		//MSB
			bufferU[j++]=memCODE_W[i]&0xff;		//LSB
			bufferU[j++]=ICSP8_SHORT;
			bufferU[j++]=BEGIN_INT_PROG;		//internally timed
			bufferU[j++]=WAIT_T3;				//Tprogram 10ms
			bufferU[j++]=ICSP8_READ;
			bufferU[j++]=READ_NVM_INC;
		}
		bufferU[j++]=FLUSH;
		for(;j<DIMBUF;j++) bufferU[j]=0x0;
		PacketIO(55);
		for(i=0,z=0;i<5;i++){
			for(;z<DIMBUF-2&&bufferI[z]!=ICSP8_READ;z++);
			if (~memCODE_W[0x8007+i]&((bufferI[z+1]<<8)+bufferI[z+2])){	//error if written 0 and read 1 (~W&R)
				
				sprintf(t,"config%d",i+1);
				PrintMessage3(strings[S_WErr2],t,memCODE_W[0x8007+i],(bufferI[z+1]<<8)+bufferI[z+2]);	//"Error writing %s: written %04X, read %04X"
				err_c++;
			}
			z+=6;
		}
		err+=err_c;
		PrintMessage1(strings[S_ComplErr],err_c);	//"completed, %d errors\r\n"
		if(saveLog){
			fprintf(logfile,strings[S_Log9],err_c);	//"Config area 	errors=%d \n"
		}
	}
//****************** exit ********************
	j=0;
	bufferU[j++]=SET_PARAMETER;
	bufferU[j++]=SET_T3;
	bufferU[j++]=2000>>8;
	bufferU[j++]=2000&0xff;
	bufferU[j++]=NOP;				//exit program mode
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x1;
	bufferU[j++]=EN_VPP_VCC;
	bufferU[j++]=0x0;
	bufferU[j++]=SET_CK_D;
	bufferU[j++]=0x0;
	bufferU[j++]=FLUSH;
	for(;j<DIMBUF;j++) bufferU[j]=0x0;
	PacketIO(2);
	unsigned int stop=GetTickCount();
	sprintf(str,strings[S_EndErr],(stop-start)/1000.0,err,err!=1?strings[S_ErrPlur]:strings[S_ErrSing]);	//"\r\nEnd (%.2f s) %d %s\r\n\r\n"
	PrintMessage(str);
	if(saveLog){
		fprintf(logfile,str);
		CloseLogFile();
	}
	PrintStatusClear();			//clear status report
}