-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPINEWOOD.CPP
904 lines (753 loc) · 23.1 KB
/
PINEWOOD.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <mem.h>
#include <ctype.h>
#include <stdlib.h>
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long ulong;
// ****************************************************************************
// Local defines to make life easier
#define LPT1_PORT 0x3BC
#define LPT2_PORT 0x378
#define LPT3_PORT 0x278
#define DATA_PORT (LPT_BASE + 0)
#define STATUS_PORT (LPT_BASE + 1)
#define CONTROL_PORT (LPT_BASE + 2)
#define ECP_CONTROL_PORT (LPT_BASE + 0x402)
// in the following comments two pin numbers are listed. The actual pin
// that will be read depends on the setting of the INPUT_SOURCE parameter.
// If it is set to 1, then pins 2-9 are used, if 0 then pins 15, 13, etc. are
// used. This setting is based on two things: 1) if the port address for
// the printer is 0x3BC then we always use 15-11. 2) If the
// command line swith 'T' is specified as 0 we always use 15-11, if it is 1
// then we use pins 2-9.
// Pins 15-11 are the safest set and work on all ports, pins 2-9 only work
// under certain circumstances, but it gives you 3 more bits.
#define START_BIT 0x01 // pin 2 or 15
#define LANE1_FINISH 0x02 // pin 3 or 13
#define LANE2_FINISH 0x04 // pin 4 or 12
#define LANE3_FINISH 0x08 // pin 5 or 10
#define LANE4_FINISH 0x10 // pin 6 or 11
#define LANE5_FINISH 0x20 // pin 7
#define LANE6_FINISH 0x40 // pin 8
#define LANE7_FINISH 0x80 // pin 9
// defines for the printer output of race results
#define FORM_FEED "\x0C"
#define MAX_LANES 7 // max number of lanes on a track
#if MAX_LANES < 1 || MAX_LANES > 7
#error This software supports a maximum of 7 lanes
#endif
#define MAXCARS 100 // maximum number of cars in the race
// used by the Race() function to track each car
typedef struct
{
int ID;
ulong laneTime[MAX_LANES], // in clicks
totalTime; // in clicks
int scaleSpeed[MAX_LANES], // in scaleMPH
place[MAX_LANES]; // 1 = first, 2 = second, etc.
} CarData;
// used by Heat() function to return data on each lane
typedef struct
{
int carID; // input to Heat() function, only used for display
ulong laneTime; // in clicks
int scaleSpeed, // in scaleMPH
place; // 1 = first, 2 = second, etc.
} LaneInfo;
// ****************************************************************************
// prototypes
void Diagnostics(void);
void Race(void);
void OpenRace(void);
void UpdateLaneInformation(int cars, CarData *car);
void ConfigurePort(uint port, int inputSource);
void ResetPort(void);
void SetTimerMode2(void);
ulong HiResolutionTime(void);
uint GetTrackBitsFromStatus(void);
uint GetTrackBitsFromData(void);
// #define GetTrackBits() ((inportb(STATUS_PORT) >> 4) ^ 0x08)
// #define GetTrackBits() inportb(DATA_PORT)
// ****************************************************************************
// global variables
uint LPT_BASE,
LANES = 3, // number of lanes on this track (default)
TIMEOUT = 15; // number of seconds to timeout a race (default)
int INPUT_SOURCE; // default input source is status bits
uint (*GetTrackBits)(void);
// ****************************************************************************
// routines
void main(int argc, char *argv[])
{
int done;
uint lptPort;
int inputSource = 0; // default input source is status bits
clrscr();
// find a default LPT port address
lptPort = *((uint far *)MK_FP(0x40, 0x08));
if (lptPort == 0)
lptPort = LPT1_PORT;
if (argc > 1)
{
int i;
for (i = 1; i < argc; i++)
{
// P for port
if (toupper(argv[i][0]) == 'P')
{
lptPort = (int) strtoul(&argv[i][1], NULL, 16);
// did they put a port (1-3) number instead of a port address (0x378)
if (lptPort >=1 && lptPort <=3)
{
lptPort = ((uint far *)MK_FP(0x40, 0x08))[lptPort - 1];
if (lptPort == 0)
{
// if specified port not available, try first LPT
lptPort = ((uint far *)MK_FP(0x40, 0x08))[0];
// if that's 0 too, default to our LPT1 address
if (lptPort == 0)
lptPort = LPT1_PORT;
}
}
}
// T for port type (read from data or status port)
// T0 (default) is read from status port
// T1 is read from data port
if (toupper(argv[i][0]) == 'T')
{
inputSource = atoi(&argv[i][1]);
}
// L for lanes
if (toupper(argv[i][0]) == 'L')
{
int lanes;
lanes = atoi(&argv[i][1]);
// set lanes if it is valid
if (lanes >= 1 && lanes <= MAX_LANES)
LANES = lanes;
}
// O for timeOut
if (toupper(argv[i][0]) == 'O')
{
int val;
val = atoi(&argv[i][1]);
// set timeout if it is valid
if (val >= 1 && val <= 3500) // 0xFFFFFFFF / 1193182 = 3599
TIMEOUT = val;
printf("Timeout set to %u\n", TIMEOUT);
}
}
}
SetTimerMode2(); // set 8253 timer chip to mode 2
done = 0;
do
{
// configure port at start of each loop, we may have reset
// the port at the end of the last race to print results to the
// printer
ConfigurePort(lptPort, inputSource);
printf("Pinewood Derby Race timing V1.1 (" __DATE__ ")\n"
"Copyright (C) 1997-1999 by James H. Brown, Orem, UT (jbrown@burgoyne.com)\n"
"All Rights Reserved Worldwide\n"
"\n"
"License is hereby granted by the author for non-commercial use by any and all\n"
"charitable organizations and / or community groups.\n"
"\n"
"\n");
// present main menu
printf(" -- Main Menu --\n"
"\n"
" 1 - Race\n"
" 2 - Open track racing\n"
" 3 - Diagnostics\n"
"ESC - Exit\n"
"\n"
"Your choice: ");
switch(getch())
{
case '1':
Race();
break;
case '2':
OpenRace();
break;
case '3':
Diagnostics();
break;
case 'x':
case 'X':
case 0x1b: // escape
done = 1;
break;
default:
printf("\n\aInvalid entry\n");
break;
}
printf("\n\n");
} while (!done);
ResetPort();
}
// ****************************************************************************
// port is the base port address
// inputSource is 1 to read from the data port and 0 to read from status port
void ConfigurePort(uint port, int inputSource)
{
LPT_BASE = port; // set port number for all operations
outportb(DATA_PORT, 0x5A); // flag to see if input mode is working
// ECP mode is not available on 0x3BC
if (LPT_BASE != 0x3BC && inputSource)
{
// need to reconfigure ECP to SPP bidirectional mode
outportb(ECP_CONTROL_PORT, 0x20); // set to byte mode
outportb(CONTROL_PORT, 0x24); // set direction bit to input
GetTrackBits = GetTrackBitsFromData;
INPUT_SOURCE = inputSource;
}
else
{
GetTrackBits = GetTrackBitsFromStatus;
INPUT_SOURCE = 0;
}
}
// ****************************************************************************
void ResetPort(void)
{
// ECP mode is not available on 0x3BC
if (LPT_BASE != 0x3BC && INPUT_SOURCE)
{
outportb(CONTROL_PORT, 0x04); // set direction bit to output
// need to reconfigure port back to ECP mode
// outportb(ECP_CONTROL_PORT, 0x60); // set to ECP FIFO mode
}
}
// ****************************************************************************
// this function returns pins 2-9 as bits 0-7
uint GetTrackBitsFromData(void)
{
return inportb(DATA_PORT);
}
// ****************************************************************************
// this function returns
// bit 0 - pin 15 /ERROR
// bit 1 - pin 13 SELECT
// bit 2 - pin 12 PAPER OUT
// bit 3 - pin 10 /ACK
// bit 4 - pin 11 /BUSY (See note below)
// bit 5-7 always 0
//
// pin 11 (bit 4 after the shift) is inverted by the hardware so we invert it
// in software so that it reads correctly.
uint GetTrackBitsFromStatus(void)
{
return ((inportb(STATUS_PORT) >> 3) ^ 0x10);
}
// ****************************************************************************
// Heat() returns 0 on success NZ on error or abort (1 = key hit, -1 = timeout)
int Heat(LaneInfo *laneInfo)
{
ulong startClicks, // when the race started
stopClicks[MAX_LANES], // when each car crossed the line
speedClicks[MAX_LANES]; // when each car cleared the sensor
uchar bits;
enum {INITIAL, SPEED, END}
state[MAX_LANES];
int i,
done,
placeCnt;
// set lane states to their initial condition
for (i = 0; i < LANES; i++)
state[i] = INITIAL;
// check for proper sensor setup
printf("Waiting for proper sensor state (any key exits)\n");
do
{
done = 1; // assume all is ready
printf("Not ready: ");
// all sensor bits should be zero right now
if (GetTrackBits() & START_BIT)
{
printf("Start sensor");
done = 0;
}
for (i = 0; i < LANES; i++)
{
if ((GetTrackBits() & (LANE1_FINISH << i)))
{
if (!done)
printf(", ");
printf("Lane %d", i + 1);
done = 0;
}
}
clreol();
printf("\r");
// if not ready now, wait for a quarter second before checking again
if (!done)
delay(250);
if (kbhit())
{
getch();
return 1;
}
} while (!done);
clreol();
printf("All sensors ready -- waiting for start of race\n");
// wait for start of race
while ((GetTrackBits() & START_BIT) == 0)
;
startClicks = HiResolutionTime();
printf("The race has started! ... waiting for all cars to cross the finish line\n");
placeCnt = 1;
do
{
ulong clicks;
done = 1; // assume we are done
// get current bits and time
bits = GetTrackBits();
clicks = HiResolutionTime();
// now process each lane
for (i = 0; i < LANES; i++)
{
switch(state[i])
{
// the race has started looking for this car to cross the finish line
case INITIAL:
// look for first 0 to 1 transition (leading edge of car)
if (bits & (LANE1_FINISH << i))
{
laneInfo[i].place = placeCnt++;
stopClicks[i] = clicks;
state[i] = SPEED;
}
done = 0; // not done w/ some in initial state
break;
// the nose has crossed measure the car's speed by measuring how long
// it takes for the entire car to cross
case SPEED:
// look for last 1 to 0 transition (trailing edge of car)
if ((bits & (LANE1_FINISH << i)) == 0)
{
speedClicks[i] = clicks;
state[i] = END;
}
done = 0; // never done if we are in the speed state
break;
case END:
// if we go back to a 1 (sensor covered) then revert to speed state
if (bits & (LANE1_FINISH << i))
{
state[i] = SPEED;
}
else
// check if it has been long enough to assume we have the last one
// a time of 700,000 would allow enough time for a 7 inch car to
// pass the sensor while traveling at only 1 foot per second
if (clicks - speedClicks[i] < 700000L)
{
done = 0; // not done until finished with END state
}
break;
}
}
// check to see if race is running too long
if (clicks - startClicks > TIMEOUT * 1193182L) // over timeout value
{
printf("\a\n\nRace time limit exceeded! Race aborted!\n\n");
return -1; // return failure!
}
} while (!done);
printf("The race is over, all lanes are clear\n"
"\n"
"Lane Car Time(.0001) Place Speed(scale MPH)\n");
for (i = 0; i < LANES; i++)
{
// assume the car traveled 15 scale feet (the size of a real car) in the
// time it took to cross the sensor. Generate scale MPH from that.
// equation used:
// (feet / feetPerMile) = miles
// (clicks / clicksPerHour) = hours
// so ...
// (feet / feetPerMile) / (clicks / clicksPerHour) = miles per hour
// this is the same as inverting the second group and multiplying so ...
// (feet / feetPerMile) * (clicksPerHour / clicks) = miles per hour
// substituting real numbers gives you
// (15 / 5280) * ((1193182*60*60) / clicks) = scale MPH
// 15 = scale feet
// 5280 = feet per mile
// 1193182*60*60 = clicks per second * seconds per minute * minutes per hour
// rearranging gets:
// ((15 * 1193182*60*60) / 5280) / clicks = scale MPH
// reducing the constants gets:
// 12202997.73 / clicks = scale MPH
// I rounded the ...7.73 up to ...8
laneInfo[i].laneTime = stopClicks[i] - startClicks;
laneInfo[i].scaleSpeed = (int) (12202998L / (speedClicks[i] - stopClicks[i]));
printf(" %-3d %3d %9ld %3d %d\n",
i + 1, // lane
laneInfo[i].carID, // car #
laneInfo[i].laneTime / 119, // time
laneInfo[i].place, // place
laneInfo[i].scaleSpeed); // speed
}
return 0; // successful
}
// ****************************************************************************
// compare function for qsort() to call
int _USERENTRY CarTimeCompare(const void *elem1, const void *elem2)
{
const CarData
*car1, *car2;
car1 = (const CarData *) elem1;
car2 = (const CarData *) elem2;
if (car1->totalTime == car2->totalTime)
return 0;
else
return (car1->totalTime < car2->totalTime) ? -1 : 1;
}
// ****************************************************************************
#define CarIndex(x) ((x) >= cars ? (x) - cars : (x))
void Race(void)
{
LaneInfo laneInfo[MAX_LANES]; // storage for each race's results
CarData car[MAXCARS];
int cars,
i,
done;
// clear the car memory to all zeros
memset(car, 0, sizeof(CarData)*MAXCARS);
// now set the ID field of each car entry
for (i = 0; i < MAXCARS; i++)
car[i].ID = i + 1;
clrscr();
printf("Race mode\n\n");
do
{
cars = 0;
printf("How many cars [%d-%d]: ", LANES, MAXCARS);
scanf("%d", &cars);
if (cars > MAXCARS || cars < LANES)
{
cars = 0;
printf("\aERROR: You must enter a number between %d and %d\n", LANES, MAXCARS);
}
} while (cars < LANES);
// run each heat (there will be as many heats as there are cars)
for (i = 0; i < cars; i++)
{
int j;
// identify the cars in this heat & set LaneInfo.carID for each lane
printf("\nThe cars in this heat are:\n"
"Lane Car\n");
for (j = 0; j < LANES; j++)
printf(" %-3d %d\n", j + 1, (laneInfo[j].carID = car[CarIndex(i + j)].ID));
// run the heat
if (Heat(laneInfo) == 0)
{
char ch;
do
{
// possibly record the car's time
printf("Record this race [Y/N]: ");
ch = getch();
ch = toupper(ch);
} while (ch != 'Y' && ch != 'N');
printf("%c\n", ch);
if (ch == 'Y')
{
// set CarData fields from LaneInfo information
for (j = 0; j < LANES; j++)
{
CarData *c;
c = &car[CarIndex(i + j)]; // for efficiency
c->totalTime += (c->laneTime[j] = laneInfo[j].laneTime);
c->scaleSpeed[j] = laneInfo[j].scaleSpeed;
c->place[j] = laneInfo[j].place;
}
}
else
goto InvalidRace;
}
else
{
InvalidRace:
// invalid race
char ch;
// clear keyboard buffer first
while (kbhit())
getch();
do
{
printf("Continue race [Y/N]: ");
ch = getch();
ch = toupper(ch);
} while (ch != 'Y' && ch != 'N');
printf("%c\n", ch);
if (ch == 'Y')
{
i--; // decrement I to rerun the race
continue;
}
else
{
printf("\n\nPartial race results\n");
break;
}
}
}
// sort the results by time
qsort(car, cars, sizeof(CarData), CarTimeCompare);
// only update lane info if not a partial race
if (i >= cars)
UpdateLaneInformation(cars, car);
ResetPort(); // reconfigure the LPT port so the printer will work
done = 0;
do {
// the printer is the STDPRN device
FILE *printer = stdprn;
// present race menu
printf(" -- Race Results Menu --\n"
"\n"
" 1 - Display race results\n"
" 2 - Print race results\n"
" 3 - Print individual race sheets\n"
"ESC - Return to main menu\n"
"\n"
"Your choice: ");
switch(getch())
{
case '1':
// display race results
printf("\nCar Avg. time Total clicks\n");
for (i = 0; i < cars; i++)
{
printf("%3d %9lu %12lu\n",
car[i].ID,
(car[i].totalTime / 119) / LANES,
car[i].totalTime);
// pause the display every 20 lines
if (i && i % 20 == 0)
{
printf("Press any key to continue ... ");
// flush keyboard
while (kbhit())
getch();
// wait for a key
getch();
printf("\n");
}
}
break;
case '2': // print race results
fprintf(printer, "Car Avg. time Total clicks\r\n");
fprintf(printer, " # (seconds) 1193182/sec \r\n");
fprintf(printer, "--- --------- ------------\r\n");
for (i = 0; i < cars; i++)
{
fprintf(printer, "%3d %4lu.%04lu %12lu\r\n",
car[i].ID,
((car[i].totalTime / 119) / LANES) / 10000L,
((car[i].totalTime / 119) / LANES) % 10000L,
car[i].totalTime);
}
fprintf(printer, FORM_FEED); // go to top of form
break;
case '3': // print individual race sheets
for (i = 0; i < cars; i++)
{
int j,
totalSpeed;
fprintf(printer,
"\r\n"
"\r\n"
"%33sPinewood Derby\r\n"
"\r\n"
"Car #: %3d Driver Name: ____________________ Car Name: ____________________\r\n"
"\r\n",
"", car[i].ID);
fprintf(printer, "Lane |");
for (j = 0; j < LANES; j++)
fprintf(printer, " %2d |", j + 1);
fprintf(printer, " Avg. |\r\n");
fprintf(printer, "Time (secs) |");
for (j = 0; j < LANES; j++)
fprintf(printer, " %1ld.%04ld |",
(car[i].laneTime[j] / 119) / 10000L,
(car[i].laneTime[j] / 119) % 10000L);
fprintf(printer, " %1ld.%04ld |\r\n",
((car[i].totalTime / 119) / LANES) / 10000L,
((car[i].totalTime / 119) / LANES) % 10000L);
fprintf(printer, "Scale speed |");
for (totalSpeed = 0, j = 0; j < LANES;
totalSpeed += car[i].scaleSpeed[j], j++)
fprintf(printer, " %6d |", car[i].scaleSpeed[j]);
fprintf(printer, " %6d |\r\n", totalSpeed / LANES);
fprintf(printer, "Place |");
for (j = 0; j < LANES; j++)
fprintf(printer, " %2d |", car[i].place[j]);
fprintf(printer, "\r\n");
fprintf(printer, "\r\n");
}
fprintf(printer, FORM_FEED); // go to top of form
break;
case 'x':
case 'X':
case 0x1b: // escape
done = 1;
break;
default:
printf("\n\aInvalid entry\n");
break;
}
printf("\n\n");
} while (!done);
}
// ****************************************************************************
void OpenRace(void)
{
int i;
LaneInfo laneInfo[MAX_LANES]; // storage for each race's results
clrscr();
printf("Open racing mode\n\n");
for (i = 0; i < LANES; i++)
laneInfo[i].carID = 0;
// loop on timeout (returns -1) or success (0), exit on key hit (1)
while (Heat(laneInfo) <= 0)
printf("To exit open racing leave the start lever down and press a key\n");
}
// ****************************************************************************
// Update the disk file w/ the per-lane data from this race
// Format of per-lane data file:
// timestamp, # cars, lane 1 avg. time, lane 1 std. deviation, ...
void UpdateLaneInformation(int cars, CarData *car)
{
FILE *laneInfo;
int i,
j;
laneInfo = fopen("LANEINFO.DAT", "at");
if (laneInfo == NULL)
{
printf("Could not open/create LaneInfo.dat file\n");
return;
}
// print the header for this line (timestamp, # cars)
fprintf(laneInfo, "%lu, %d", time(NULL), cars);
for (j = 0; j < LANES; j++)
{
ulong avg,
stdDev;
// get avg time for the lane
for (i = 0, avg = 0; i < cars; i++)
avg += car[i].laneTime[j] / 119;
avg /= cars; // calculate average
// now calculate standard deviation
for (i = 0, stdDev = 0; i < cars; i++)
{
// use the absolute value of the deviation
if (avg < (car[i].laneTime[j] / 119))
stdDev += (car[i].laneTime[j] / 119) - avg;
else
stdDev += avg - (car[i].laneTime[j] / 119);
}
stdDev /= cars;
fprintf(laneInfo, ", %lu, %lu", avg, stdDev);
}
fprintf(laneInfo, "\n");
fclose(laneInfo);
}
// ****************************************************************************
char *ToBinary(uchar bits)
{
static char text[9];
int i;
for (i = 0; i < 8; i++)
{
text[i] = (bits & (1 << (7 - i))) ? '1' : '0';
}
text[8] = '\0';
return text;
}
// ****************************************************************************
void Diagnostics(void)
{
uchar bits;
ulong startClicks,
clicks,
maxClicks = 0;
clrscr();
printf("Port Diagnostics\n\n");
printf("Configuration:\nLanes: %d\nLPT Port: 0x%03X\nInput: %s\nTimeout: %u (secs)\n\n",
LANES, LPT_BASE,
INPUT_SOURCE ? "Data (pins 2-9)" : "Status (pins 15, 13, 12, 10, 11)",
TIMEOUT);
if (LANES > 4 && !INPUT_SOURCE)
printf("* * * WARNING: Too many lanes for input source\n\n");
printf("Bits Clicks\n");
while (!kbhit())
{
startClicks = HiResolutionTime();
bits = GetTrackBits();
clicks = HiResolutionTime() - startClicks;
if (maxClicks < clicks)
maxClicks = clicks;
printf("%s %-10lu\r", ToBinary(bits), clicks);
}
getch();
printf("\n\nMax clicks = %lu\n", maxClicks);
}
void SetTimerMode2(void)
{
disable(); // no one else must touch timer chip
outportb(0x43, 0x34); // set channel 0, mode 2
outportb(0x40, 0); // reset divisor to 65536 (0,0 = 65536)
outportb(0x40, 0);
enable();
}
// The PC timer runs at 1,193,182 / 65536 = 18.20651245 ticks per second.
// The click counter runs at 1,193,182 per second and wraps at 16 bits.
// The total clicks can be obtained by TICKS * 65536 + CLICKS
// Milliseconds can be caluclated by total clicks / 1193
/****************************************************************************/
#define BIOS_TIMER 0x46C
/****************************************************************************/
// return total clicks value
ulong HiResolutionTime(void)
{
uint clicks;
ulong ticks,
retVal;
ulong far *
biosTicks = (ulong far *) MK_FP(0, BIOS_TIMER);
static ulong
lastTime = 0; // memory for our last returned value
retry:
ticks = *biosTicks; // read bios tick count
disable(); // no one must touch 8253
outportb(0x43, 0); // latch counter value for counter 0
clicks = inportb(0x40); // read lsb counter 0
clicks += inportb(0x40) << 8; // read msb counter 0
enable(); // ok to touch 8253 now
clicks = 65535U - clicks; // convert from count down to count up
if (ticks != *biosTicks) // did tick elapse while we processed?
goto retry; // if so, try again
// return total clicks by ticks * 65536 + clicks
retVal = (ticks << 16) + clicks;
// check for a missed tick value
if (retVal < lastTime &&
(retVal & 0xFFFF0000L) == (lastTime & 0xFFFF0000L))
{
#if 0
fprintf(stderr, "\n\nHiResolutionTime: Missed a tick!\n");
fprintf(stderr, "\tlastTime = %lX, retVal = %lX, ticks = %lX, clicks = %X\n",
lastTime, retVal, ticks, clicks);
#endif
(*biosTicks)++; // add a tick!
goto retry;
}
lastTime = retVal;
return retVal;
}
// ****************************************************************************
// ****************************************************************************