-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAD.C
1842 lines (1743 loc) · 58.6 KB
/
AD.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************
**
** AD - Program to acquire analog data from a DT2821 a/d board
** and process it for spike waveforms on multiple 4 channel electrodes.
** Handles up to 8 channels using interrupt driven, or polled dual-DMA,
** triggered scan a/d conversion up to 250 KHz. Continuous storage
** to disk of up to 8 channels at an aggregate rate of 100KHz can
** be accomplished in the interrupt driven mode. SVGA display of
** acquired data and processed spike waveforms. Has been tested on
** Pentium P90, PCI, ATI Mach64; I486DX266, Cirrus 5826 VLB.
** Runs in 640K (minimum memory ???). Uses system timer to achieve
** an absolute temporal resolution of 1 msec. Optional use of a CTM05/10 can
** improve resolution.
** Support will be included for Dragon Tracker video tracking hardware.
**
** Current support summary:
** - 8 channels
** - 250 KHz max sampling rate
** - continuous acquisition, display, and disk storage up to 100 KHz
** - 4 channel spike detection
**
** Work in progress:
** - channel gain, threshold specification
** - extracted spike display and storage
** - spike parameter scatter plots
**
** Known problems:
** - rapid keystroke entry (e.g. hold down a key) causes a/d errors
** due to kb interrupts
** - graphics library has glitches which cause text display to occur
** at incorrect coordinates particularly during rapid update
**
** Developed with Borland C++ 4.0 under MSDOS6.2
**
** Created 10/17/94
** Written by Matthew A. Wilson, Ph.D.
** Assistant Professor
** Center for Learning and Memory
** Departments of Brain and Cognitive Sciences, and Biology
** Massachusetts Institute of Technology
** Cambridge, MA 02139
**
** and
** Loren Frank
** Graduate Student
** Department of Brain and Cognitive Sciences
** Massachusetts Institute of Technology
** Cambridge, MA 02139
**
**
** Copyright (c) 1994,
** Matthew A. Wilson and the Massachusetts Institute of Technology
** All Rights Reserved
** Duplication or use of this software without the expressed written
** consent of the author is strictly forbidden
**
*************************************************************************
*/
#include "adext.h"
/* Global variables */
#ifdef DAS-1800
WORD boardstatus;
#endif
extern int got_command; // indicates command on command bus
extern int got_command2; // indicates command on command bus 2
int count1 = 0, count2 = 0, count3 = 0, istart, location;
int spikesaves = 0;
ADInfo adinfo;
SystemInfo sysinfo;
WORD supcsr,adcsr; /* DT2821 register variables */
#ifdef BORLAND
void _interrupt (*oldadirq)();
void _interrupt (*oldnetworkvec)();
#endif
#ifdef MSVC
void _interrupt far *oldadirq;
void _interrupt far *oldnetworkvec;
#endif
char far tmpstring[200];
char far tmpstring2[200];
char far tmpstring3[200];
char far tmpstring4[200];
char far tmpstring5[200];
Button buttonlist[MAXBUTTONS];
MessageArea message_area[MAXBUTTONS];
#ifdef BORGRAPH
char *Fonts[NFONTS] = {
"DefaultFont", "TriplexFont", "SmallFont",
"SansSerifFont", "GothicFont", "ScriptFont", "SimplexFont", "TriplexScriptFont",
"ComplexFont", "EuropeanFont", "BoldFont"
};
char *LineStyles[] = {
"SolidLn", "DottedLn", "CenterLn", "DashedLn", "UserBitLn"
};
char *FillStyles[] = {
"EmptyFill", "SolidFill", "LineFill", "LtSlashFill",
"SlashFill", "BkSlashFill", "LtBkSlashFill", "HatchFill",
"XHatchFill", "InterleaveFill", "WideDotFill", "CloseDotFill"
};
char *TextDirect[] = {
"HorizDir", "VertDir"
};
char *HorizJust[] = {
"LeftText", "CenterText", "RightText"
};
char *VertJust[] = {
"BottomText", "CenterText", "TopText"
};
int GraphDriver; /* The Graphics device driver */
int GraphMode; /* The Graphics mode value */
double AspectRatio; /* Aspect ratio of a pixel on the screen*/
int MaxX, MaxY; /* The maximum resolution of the screen */
int MaxColors; /* The maximum # of colors available */
int ErrorCode; /* Reports any graphics errors */
struct palettetype palette; /* Used to read palette info */
#endif
/*
*****************************************************************
**
** Program structure:
** Miscellaneous Routines
** DT2821 Routines
** Data Processing/ISR Routines
** DMA Routines
** Display Routines
** IO Routines
** Main Program
**
*****************************************************************
*/
/*
************************************************************************
**
** Miscellaneous Routines
**
************************************************************************
*/
void SystemRestore(void)
{
int i;
BoardRestore();
#ifndef BORGRAPH
// restore the text mode
restext();
#endif
// free the dma buffers
for (i = 0; i < DMABUFFERS; i++) {
if(adinfo.dataptr[i]){
free(adinfo.dataptr[i]);
adinfo.dataptr[i] = NULL;
}
}
/* restore the interrupt service routine vector for the ad */
setvect(sysinfo.adirqvec, oldadirq);
/* and for the network packet driver */
setvect(sysinfo.networkvec, oldnetworkvec);
if(sysinfo.command_mode == SLAVE){
ClkRestoreSystem();
}
if(sysinfo.command_mode2 == SLAVE){
Clk2RestoreSystem();
}
if(sysinfo.tracker_enabled){
RestoreTracker();
}
}
void SystemExit(int errorlevel,char *string)
{
/*
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,"Press any key to exit\n");
_getch();
*/
ErrorMessage("Exiting...");
if (adinfo.fp){
DisableADIRQ;
fclose(adinfo.fp);
adinfo.fp = NULL;
EnableADIRQ;
}
ErrorMessage("Restoring...");
SystemRestore();
fprintf(stderr,"Errorcode %d: %s\n",errorlevel,string);
exit(errorlevel);
}
long pow2(WORD power)
{
if(power == 0) return(1);
return(1<<(power));
}
void AllocateBuffers(void)
{
int i;
/* allocate the DMA buffers */
adinfo.dataptr = (int **) malloc(sizeof(int *) * DMABUFFERS);
for (i = 0; i < DMABUFFERS; i++) {
allocbuff(adinfo.dataptr+i, adinfo.dma_bufsize);
/* get the DMA page and offset value for the first buffer */
GetDMAInfo(adinfo.dma_bufsize,adinfo.dataptr[i],
&adinfo.dmapage[i],&adinfo.dmabase[i]);
/* fprintf(stderr,"buffer %d maxsize:%u page:%u offset:%u ",
i,getDMAlength(adinfo.dataptr[i]),adinfo.dmapage[i],
adinfo.dmabase[i]);
StatusMessage(tmpstring); */
}
/* allocate space for the continuous display mode buffer */
if((adinfo.prevptr = (int *)calloc(adinfo.dma_bufsize,sizeof(int))) == NULL) {
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,
"MEMORY ERROR: unable to allocate prev buffer\n", i);
}
for(i=0;i<adinfo.nelectrodes;i++){
/* allocate a display buffer for old points to allow erasure */
if((adinfo.electrode[i].prevbufptr = (int *)calloc(
adinfo.dma_bufsize / NELECTRODES, sizeof(int))) == NULL){
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,
"MEMORY ERROR: unable to allocate prev buffer E%d\n",
i);
SystemExit(7,"prev buffer MEMORY ERROR");
}
/* allocate the spike buffers */
if((adinfo.electrode[i].spikebuf = (Spike *)calloc(
MAXSPIKES, sizeof(Spike))) == NULL){
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,
"MEMORY ERROR: unable to allocate spike buffer E%d\n", i);
SystemExit(7,"spike buffer MEMORY ERROR");
}
/* allocate space for the subthreshold buffer */
if ((adinfo.electrode[i].subthresh = (char *) calloc(
adinfo.nelect_chan, sizeof(char))) == NULL) {
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,
"MEMORY ERROR: unable to allocate subthreshold buffer E%d\n",
i);
SystemExit(7,"spike buffer MEMORY ERROR");
}
}
}
void ADSetup(void)
{
int i,j;
BoardSetup();
for(i=0;i<adinfo.nchannels;i++){
adinfo.channel[i].adgain = 0;
adinfo.channel[i].color = 15-i;
adinfo.channel[i].triggerable = 1;
adinfo.channel[i].thresh = 200;
adinfo.channel[i].subthresh = 1;
}
for(i=0;i<adinfo.nelectrodes;i++){
for(j=0;j<adinfo.nelect_chan;j++){
adinfo.electrode[i].channel[j] = j+i*adinfo.nelect_chan;
adinfo.electrode[i].thresh[j] = 200;
adinfo.electrode[i].subthresh[j] = 1;
}
adinfo.electrode[i].ntotal_spikes = 0;
adinfo.electrode[i].displaythresh = 31;
}
}
short nextbuf(short buf)
/* returns the index of the next buffer */
{
return (buf + 1) % DMABUFFERS;
}
short prevbuf(short buf)
/* returns the index of the previous buffer */
{
return buf > 0 ? buf - 1 : DMABUFFERS - 1;
}
unsigned long compute_timestamp(short bufindex, unsigned long offset)
{
return *(adinfo.timestamp + bufindex) + (unsigned long) (((float) offset /
(float) adinfo.dma_bufsize) * adinfo.conversion_duration);
}
int getnelectrodes(void)
{
return adinfo.nelectrodes;
}
long DiskFree()
{
struct diskfree_t free;
long avail;
if (_dos_getdiskfree(0, &free) != 0) {
printf("Error in _dos_getdiskfree() call\n");
exit(1);
}
return (long) free.avail_clusters
* (long) free.bytes_per_sector
* (long) free.sectors_per_cluster;
}
/*
************************************************************************
**
** DT2821 A/D Routines
**
************************************************************************
*/
void ADResetClock(void)
{
#ifdef OLD
/* note that this will reset the PIO port as well */
outpw(SUPCSR,BIT0); /* initialize the board */
#endif
#ifdef DT2821
outpw(TMRCTR,adinfo.clockreg);
#endif
}
void InitAcq(void)
{
int i;
int boardstat;
/* initialize the DT2821 board */
if(boardstat = InitBoard()) {
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,"ERROR initializing board.\n");
SystemExit(boardstat,"Init board Error");
}
/* the DT2821 is now acquiring into buffer A */
DmaStatus; /* get supcsr variable */
/*
*/
adinfo.count = 0;
adinfo.nbuffers = adinfo.computed_used = 0;
adinfo.timestamp[0] = ReadTS();
adinfo.computed_timestamp = (double) adinfo.timestamp[0];
adinfo.dmadone = 0;
adinfo.error = 0;
adinfo.next_buf = 1;
for (i = 0; i < adinfo.nelectrodes; i++) {
adinfo.electrode[i].nspikes = 0;
adinfo.electrode[i].prevstart = adinfo.buflen;
}
for (i = 0; i < DMABUFFERS; i++)
adinfo.buffer_valid[i] = 0;
}
/*
************************************************************************
**
** Data Processing Routines
**
************************************************************************
*/
void interrupt GetCompletedBuffer(void)
{
int i;
long bufsize;
unsigned long time;
/*
fprintf(stderr, "in GetCompletedBuffer\n");
fprintf(stderr, "%2x %2x %2x %2x %2x\n",
inp(DATASELECT), inp(CONTROLA),
inp(CONTROLB), inp(CONTROLC), inp(STATUS));
*/
/* disable interrupts while processing */
disable();
#ifdef DT2821
if(sysinfo.debug){
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,"ISR: Buffer f%d F%d : ",
adinfo.next_buf,FilledBuffer);
}
#endif
/* check for a/d error status */
if(ErrorSet){
#ifdef DT2821
adinfo.error = adcsr;
#endif
#ifdef DAS-1800
adinfo.error = boardstatus;
#endif
goto aderror;
}
/*
** get the time of occurence of the completion of the dma transfer.
** Use the system clock for now, but later go to the CTIO board.
** Note that the time of buffer completion is the time of buffer
** onset for the subsequent buffer.
** Empirical testing has shown that about 50-100 msec of jitter
** can be expected in the actual time of interrupt generation.
** Therefore the computed timestamp is a more accurate measure
** as long as its overall consistency with the system clock can
** be maintained.
*/
time = ReadTS();
adinfo.nbuffers++;
adinfo.computed_timestamp += adinfo.conversion_duration;
if (time <= (unsigned long) adinfo.computed_timestamp + 2) {
/*
** jitter in the interrupt time can only push the read timestamp forward,
** so if the read timestamp is within 0.2 ms of the computed timestamp,
** use the read timestamp as the correct current time
*/
adinfo.timestamp[adinfo.next_buf] = time;
adinfo.computed_timestamp = (double) time;
}
else {
/* the computed timestamp is correct */
adinfo.timestamp[adinfo.next_buf] = (unsigned long)
adinfo.computed_timestamp;
adinfo.computed_used++;
}
/*
** In the ISR
** FilledBuffer should return the buffer index of the buffer
** currently being filled because the ISR is called after
** the dmadone bit is set and the buffer bit has been automatically
** switched to the next buffer. The adinfo.filled_buffer must
** be set after dmadone has been detected (call to ISR)
*/
adinfo.buffer_valid[adinfo.next_buf] = 1;
/*
** increment the next buffer index.
*/
/* adinfo.next_buf is the index of the buffer that is to be filled
next, so prevbuf(adinfo.next_buf) is the index of the buffer that was just filled */
adinfo.process_buf = prevbuf(adinfo.next_buf);
adinfo.next_buf = nextbuf(adinfo.next_buf);
adinfo.dmadone = 1;
adinfo.error = 0;
if(sysinfo.debug){
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,": buf %d valid :",
adinfo.next_buf);
}
/*
** check for the acq stop flag
*/
#ifdef DT2821
if(sysinfo.acq){
/* interrupt on error, clear dmadone, dual dma, a/d clocked dma */
outpw(SUPCSR,BIT14 | BIT13 | BIT12 | BIT10);
} else {
/* this is the last buffer so turn off dual-dma. Acq will
** stop on the next dma done
*/
/* interrupt on error, clear dmadone */
outpw(SUPCSR,BIT14 | BIT10);
#endif
#ifdef DAS-1800
if(sysinfo.acq){
// clear the status register
outp(STATUS, 0x80);
}
else {
// stop acquisition
outp(STATUS, 0x0);
outp(STATUS, 0x0);
// disable fifo and counters
outp(CONTROLA, 0x0);
#endif
if(sysinfo.debug){
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,
" *** terminate *** ");
}
// need to make sure A/D has actually stopped. This requires
// GetCompleteBuffer to be called twice. This condition is
// checked in StartAcq
sysinfo.stopped++;
}
/* fprintf(stderr, "GetCompletedBuffer section 2\n");
fprintf(stderr, "%2x %2x %2x %2x %2x\n",
inp(DATASELECT), inp(CONTROLA),
inp(CONTROLB), inp(CONTROLC), inp(STATUS));
*/
/*
** reprogram the DMA controller. Note that this changes the filled
** buffer index adinfo.filled_buffer to be consistent with FilledBuffer
*/
ProgramDMAController();
/* check for error */
if (ErrorSet)
#ifdef DT2821
adinfo.error = adcsr;
#endif
#ifdef DAS-1800
adinfo.error = boardstatus;
#endif
aderror:
if(adinfo.error != 0){
sprintf(tmpstring,"ISR: Buffer %d %ld : Error %X ",
prevbuf(adinfo.next_buf),
adinfo.timestamp[adinfo.process_buf],
adinfo.error);
ErrorMessage(tmpstring);
} else {
if(sysinfo.showstatus){
sprintf(tmpstring,"ISR: NBuffer %d PBuffer %d %7ld %7ld %7ld %7ld ",
adinfo.next_buf, adinfo.process_buf,
adinfo.timestamp[adinfo.next_buf],
adinfo.timestamp[adinfo.next_buf] -
adinfo.timestamp[adinfo.process_buf],
(unsigned long)(adinfo.computed_timestamp/1e3),
(unsigned long)(adinfo.computed_timestamp/1e3) -
adinfo.timestamp[adinfo.next_buf]);
StatusMessage(tmpstring);
}
}
#ifdef DT2821
if(sysinfo.debug){
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,"ISR: Buffer f%d F%d\n",
adinfo.process_buf,FilledBuffer);
}
#endif
abort_isr:
/* signal the end of the ISR to the controller */
outp(IRQMASTER,IRQEOI);
outp(IRQSLAVE,IRQEOI);
/* reenable interrupts */
enable();
//sprintf(tmpstring,"count=%ld proc=%d next=%d",
// adinfo.count,adinfo.process_buf,adinfo.next_buf);
//ErrorMessage(tmpstring);
}
char temp[100];
void SpikeModeProcessBuffer(void)
{
/*
** Multiple buffer code
*/
register int i, j, k, l;
int e_num;
register int *dptr;
register int *spikedataptr;
register Spike *spikeptr;
register int *tptr; /* pointer to threshold buffer */
register char *subptr; /* pointer to subthreshold status array */
int *previousdptr;
register int *pdptr;
int *dataptr; /* pointer to the data in the buffer */
int nspikes;
int offset;
ElectrodeInfo *electrode;
int peak;
int previousbuf;
int start;
int bufoffset;
register int previouslength;
register int currentlength;
int spikefound;
long sum;
int totspikes=0;
DisableADIRQ;
dataptr = adinfo.dataptr[adinfo.process_buf];
previousbuf = prevbuf(adinfo.process_buf);
previousdptr = adinfo.dataptr[previousbuf];
offset = ADOFFSET;
if(sysinfo.debug){
fprintf(stderr,"testing buffer validity: buf %d=%d : buf %d=%d :",
adinfo.process_buf,
adinfo.buffer_valid[adinfo.process_buf],
(adinfo.process_buf),
adinfo.buffer_valid[prevbuf(adinfo.process_buf)]);
}
if(sysinfo.detect_spikes){
/*
** loop through once for each electrode in the buffer
*/
for(e_num = 0; e_num < adinfo.nelectrodes; e_num++) {
electrode = adinfo.electrode + e_num;
spikeptr = electrode->spikebuf;
spikedataptr = spikeptr->dataptr;
nspikes = 0;
start = 0;
/*
** the buffer start offset is 0 or 4 if there are two electrodes
*/
bufoffset = e_num * adinfo.nelect_chan;
dptr = dataptr + bufoffset;
pdptr = previousdptr + electrode->prevstart + bufoffset;
/*
** Check the last elements of the previous buffer
** for an above threshold event
*/
for(i = electrode->prevstart; i < adinfo.dma_bufsize; i+=
adinfo.nchannels) {
spikefound = 0;
tptr = electrode->thresh;
subptr = electrode->subthresh;
for(j = 0; j < adinfo.nelect_chan; j++) {
/*
** scan the buffer for threshold crossings
*/
if (*(pdptr)-offset > *(tptr)) {
if (*subptr) {
/* if it started out subthreshold */
spikefound = 1;
/*
** detected a threshold crossing.
*/
/* set the electrode number and the time */
spikeptr->type = 's';
spikeptr->type2 = 's';
spikeptr->electrode = e_num;
spikeptr->timestamp = compute_timestamp(previousbuf,
i);
/* move the data pointer back to the beginning of
the period to be saved */
pdptr -= (j + adinfo.prespike_points);
/*
** previouslength is the length of the section of
** the previous buffer to be saved
** currentlength is the length of the section of the
** current buffer to be saved
*/
previouslength = adinfo.dma_bufsize - i +
adinfo.prespike_points;
currentlength = adinfo.spikelen - previouslength;
for(k=0; k<previouslength; k+=adinfo.nchannels) {
/*
** copy each word from the previous data buffer
** to the appropriate electrode data buffer.
** Note that the data pointer will be
** pointing to the data just past the spike
** when the memory copy is completed
*/
memcpy(spikedataptr, pdptr, adinfo.nelect_size);
pdptr += adinfo.nchannels;
spikedataptr += adinfo.nelect_chan;
}
for(k=0; k < currentlength; k+= adinfo.nchannels) {
/*
** copy each word from the current data buffer
** to the appropriate electrode data buffer.
*/
memcpy(spikedataptr, dptr, adinfo.nelect_size);
dptr += adinfo.nchannels;
spikedataptr += adinfo.nelect_chan;
}
nspikes++;
/* there can only be one spike in this buffer */
i = adinfo.dma_bufsize;
/*
** the next threshold crossing event will start in
** the current data buffer. Increment start so
** that detection will begin after the current spike
*/
start = currentlength;
/*
** move to the next spike
*/
spikeptr++;
spikedataptr = spikeptr->dataptr;
/*
** hop to the next event. no need to look at the
** other channels on the same electrode
*/
/* set all of subthreshold varibles to indicate
** suprathreshold status
*/
subptr = electrode->subthresh;
for (l = 0; l < adinfo.nelect_chan; l++) {
*subptr = 0;
subptr++;
}
break;
}
}
else {
// the input is currently subthreshold
*subptr = 1;
}
pdptr++;
tptr++;
subptr++;
}
if (!spikefound){
pdptr += adinfo.elect_inc;
}
}
/*
** if the start point for the next spike is close to the
** beginning of the current buffer, scan the first points of
** the current buffer for a threshold crossing
*/
for(i = start; i < adinfo.prespike_points; i +=
adinfo.nchannels) {
spikefound = 0;
tptr = electrode->thresh;
subptr = electrode->subthresh;
for(j = 0; j < adinfo.nelect_chan; j++) {
/*
** scan the buffer for threshold crossings
*/
if(*(dptr)-offset > *(tptr)) {
if (*subptr) {
/*
** detected a threshold crossing.
*/
spikefound = 1;
/* set the electrode number and the time */
spikeptr->type = 's';
spikeptr->type2 = 's';
spikeptr->electrode = e_num;
spikeptr->timestamp =
compute_timestamp(adinfo.process_buf, i);
/*
** move the data pointer back to the beginning of
** the buffer
*/
dptr = dataptr + bufoffset;
/*
** previouslength is the length of the section of
** the previous buffer to be saved
** currentlength is the length of the section of the
** current buffer to be saved
*/
previouslength = adinfo.prespike_points - i;
currentlength = adinfo.spikelen - previouslength;
/*
** move the previous data pointer to the right
** place
*/
pdptr = previousdptr + adinfo.dma_bufsize -
previouslength;
for(k=0; k<previouslength; k += adinfo.nchannels) {
/*
** copy each word from the previous data buffer
** to the appropriate electrode data buffer.
*/
memcpy(spikedataptr, pdptr, adinfo.nelect_size);
pdptr += adinfo.nchannels;
spikedataptr += adinfo.nelect_chan;
}
for(k=0; k < currentlength; k += adinfo.nchannels) {
/*
** copy each word from the current data buffer
** to the appropriate electrode data buffer.
*/
memcpy(spikedataptr, dptr, adinfo.nelect_size);
dptr += adinfo.nchannels;
spikedataptr += adinfo.nelect_chan;
}
nspikes++;
i += currentlength;
/* spike detection should start immediately after
this spike */
start = i;
/* move past the spike */
spikeptr++;
spikedataptr = spikeptr->dataptr;
/*
** set all of subthreshold varibles to indicate
** suprathreshold status
*/
subptr = electrode->subthresh;
for (l = 0; l < adinfo.nelect_chan; l++) {
*subptr = 0;
subptr++;
}
break;
}
}
else {
// the input is currently subthreshold
*subptr = 1;
}
dptr++;
tptr++;
subptr++;
}
if (!spikefound){
dptr += adinfo.elect_inc;
}
}
if (start == 0){
/*
** no spike was found in the first few points of the buffer,
** so start after adinfo.prespike_points
*/
start = adinfo.prespike_points;
}
/* find the spikes up to the point of potential overlap with the
** next buffer and copy them to spikeptr
*/
currentlength = adinfo.spikelen;
for(i = start; i < adinfo.buflen; i += adinfo.nchannels) {
spikefound = 0;
tptr = electrode->thresh;
subptr = electrode->subthresh;
for(j = 0; j < adinfo.nelect_chan; j++) {
/*
** scan the buffer for threshold crossings
*/
if(*(dptr)-offset > *(tptr)) {
if (*subptr) {
/*
** detected a threshold crossing.
*/
spikefound = 1;
if(nspikes >= MAXSPIKES){
ErrorMessage("WARNING: spike buffer overrun");
break;
}
/* set the electrode number and the time */
spikeptr->type = 's';
spikeptr->type2 = 's';
spikeptr->electrode = e_num;
spikeptr->timestamp =
compute_timestamp(adinfo.process_buf, i);
/* move the data pointer back to the beginning of
** the period to be saved
*/
dptr -= (j + adinfo.prespike_points);
for(k = 0; k < currentlength; k+=adinfo.nchannels) {
/* copy each word from the data buffer to the
** appropriate electrode data buffer.
** Note that the data pointer will be
** pointing to the data just past the spike
** when the memory copy is completed
*/
memcpy(spikedataptr, dptr, adinfo.nelect_size);
dptr += adinfo.nchannels;
spikedataptr += adinfo.nelect_chan;
}
/* move to the next spike */
spikeptr++;
spikedataptr = spikeptr->dataptr;
nspikes++;
/*
** adinfo.nchannels will be added to i at the top
** of the for loop, so subtract one extra
** adinfo.nchannels
*/
i += adinfo.spikesep - adinfo.nchannels;
/*
** set all of subthreshold varibles to indicate
** suprathreshold status
*/
subptr = electrode->subthresh;
for (l = 0; l < adinfo.nelect_chan; l++) {
*subptr = 0;
subptr++;
}
/*
** hop to the next event. no need to look at the
** other channels on the same electrode
*/
break;
}
}
else {
// the input is currently subthreshold
*subptr = 1;
}
dptr++;
tptr++;
subptr++;
}
if (!spikefound) {
dptr += adinfo.elect_inc;
}
}
if(sysinfo.debug){
gprintf(&sysinfo.debugwinx, &sysinfo.debugwiny,
"Buf %d : E %d : Spk %ld ", adinfo.filled_buffer,
j, electrode->nspikes);
}
/*
** processing might have found a spike that overlaps the section
** of the previous buffer that will be processed next time around.
** Set the start point for the next processing run to be the point
** at which processing ended for this run
*/
electrode->prevstart = i;
electrode->nspikes = nspikes;
totspikes += nspikes;
}
if(totspikes > adinfo.peak_rate){
adinfo.peak_rate = totspikes;
}
}
EnableADIRQ;
}
void ContinuousModeProcessBuffer(void)
{
/*
** Multiple buffer code
*/
register int i, j, k, l;
int e_num;
register int *dptr;
register ChannelInfo *cptr;
int *dataptr; /* pointer to the data in the buffer */
int nspikes;
int offset;
ElectrodeInfo *electrode;
int peak;
int previousbuf;
int start;
int bufoffset;
int spikefound;
long sum;
int totspikes=0;
int cnum;
DisableADIRQ;
dataptr = adinfo.dataptr[adinfo.process_buf];
sysinfo.trigger=0;
nspikes = 0;
spikefound = 0;
offset = ADOFFSET;
if(sysinfo.debug){
fprintf(stderr,"testing buffer validity: buf %d=%d : buf %d=%d :",
adinfo.process_buf,
adinfo.buffer_valid[adinfo.process_buf],
(adinfo.process_buf),
adinfo.buffer_valid[prevbuf(adinfo.process_buf)]);
}
if(sysinfo.triggered_continuous){
/*
** loop through once for each electrode in the buffer
*/
/*
** scan each channel
*/
for(cnum = 0; cnum < adinfo.nchannels; cnum++) {
cptr = adinfo.channel+cnum;
/*
** is it a triggerable channel?
*/
if(!cptr->triggerable) continue;
dptr = dataptr+cnum;
for(i = 0; i < adinfo.buflen; i += adinfo.nchannels) {
if(*(dptr)-offset > cptr->thresh) {
if (cptr->subthresh) {
/*
** detected a threshold crossing.
*/
spikefound = 1;
if(nspikes >= MAXSPIKES){
ErrorMessage("WARNING: spike buffer overrun");
break;
}
sysinfo.trigger = i;
nspikes++;
/*
** hop to the next event. no need to look at the
** other channels on the same electrode
*/
break;
}
} else {
/* the input is currently subthreshold */
cptr->subthresh = 1;
}
dptr += adinfo.nchannels;
}
if (spikefound) {