-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLOGIDOC.CPP
2964 lines (2584 loc) · 98.3 KB
/
LOGIDOC.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
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
// Logidoc.cpp : implementation of the CLogiDoc class
//
// Copyright (C) 1993-1994 George Mills and Softronics, Inc. Corporation
// All rights reserved.
//
#include "stdafx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
//Now AnodeNULL is now document specific. Use
// pLogiObj->m_pDocument->m_pAnodeNULL or
// pView->GetDocument()->m_pAnodeNULL to access.
//Anode AnodeNULL(UNKNOWN,"xyz",FLOAT);
/////////////////////////////////////////////////////////////////////////////
// CLogiDoc
IMPLEMENT_DYNCREATE(CLogiDoc, CDocument)
BEGIN_MESSAGE_MAP(CLogiDoc, CDocument)
//{{AFX_MSG_MAP(CLogiDoc)
ON_COMMAND(ID_VIEW_PAPERCOLOR, OnViewPaperColor)
ON_COMMAND(ID_SIMULATE_RUN, OnSimulateRun)
ON_COMMAND(ID_SIMULATE_SETUP, OnSimulateSetup)
ON_COMMAND(ID_SIMULATE_STOP, OnSimulateStop)
ON_UPDATE_COMMAND_UI(ID_SIMULATE_RUN, OnUpdateSimulateRun)
ON_UPDATE_COMMAND_UI(ID_SIMULATE_SETUP, OnUpdateSimulateSetup)
ON_UPDATE_COMMAND_UI(ID_SIMULATE_STOP, OnUpdateSimulateStop)
ON_COMMAND(ID_VIEW_PALETTE, OnSimulateDraw)
ON_UPDATE_COMMAND_UI(ID_VIEW_PALETTE, OnUpdateSimulateDraw)
ON_COMMAND(ID_SIMULATE_DRAW, OnSimulateDraw)
ON_UPDATE_COMMAND_UI(ID_SIMULATE_DRAW, OnUpdateSimulateDraw)
ON_COMMAND(ID_SIMULATE_PAUSE, OnSimulatePause)
ON_UPDATE_COMMAND_UI(ID_SIMULATE_PAUSE, OnUpdateSimulatePause)
ON_COMMAND(ID_SIMULATE_STEP, OnSimulateStep)
ON_UPDATE_COMMAND_UI(ID_SIMULATE_STEP, OnUpdateSimulateStep)
ON_COMMAND(ID_SIMULATE_RESET, OnSimulateReset)
ON_UPDATE_COMMAND_UI(ID_SIMULATE_RESET, OnUpdateSimulateReset)
ON_COMMAND(ID_SIMULATE_PROBE, OnSimulateProbe)
ON_UPDATE_COMMAND_UI(ID_SIMULATE_PROBE, OnUpdateSimulateProbe)
ON_COMMAND(ID_VIEW_GRID, OnViewGrid)
ON_UPDATE_COMMAND_UI(ID_VIEW_GRID, OnUpdateViewGrid)
ON_COMMAND(ID_SIMULATE_ANALYZE, OnSimulateAnalyze)
ON_UPDATE_COMMAND_UI(ID_SIMULATE_ANALYZE, OnUpdateSimulateAnalyze)
ON_COMMAND(ID_VIEW_ZOOM_IN, OnViewZoomIn)
ON_UPDATE_COMMAND_UI(ID_VIEW_ZOOM_IN, OnUpdateViewZoomIn)
ON_COMMAND(ID_VIEW_ZOOM_NORMAL, OnViewZoomNormal)
ON_UPDATE_COMMAND_UI(ID_VIEW_ZOOM_NORMAL, OnUpdateViewZoomNormal)
ON_COMMAND(ID_VIEW_ZOOM_OUT, OnViewZoomOut)
ON_UPDATE_COMMAND_UI(ID_VIEW_ZOOM_OUT, OnUpdateViewZoomOut)
ON_COMMAND(ID_VIEW_SNAPTOGRID, OnViewSnapToGrid)
ON_UPDATE_COMMAND_UI(ID_VIEW_SNAPTOGRID, OnUpdateViewSnapToGrid)
ON_COMMAND(ID_VIEW_COLORWIRES, OnViewColorWires)
ON_UPDATE_COMMAND_UI(ID_VIEW_COLORWIRES, OnUpdateViewColorWires)
ON_COMMAND(ID_ESCAPE, OnEscape)
//}}AFX_MSG_MAP
ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLogiDoc construction/destruction
CLogiDoc::CLogiDoc()
{
LOGFONT lf;
m_iZoom = 0;
m_bGrid = TRUE;
m_bSnapToGrid = FALSE;
m_uGridWidth = 6;
m_uGridHeight = 6;
m_uCanvasWidth = XDEFAULTDOCSIZE;
m_uCanvasHeight = YDEFAULTDOCSIZE;
m_nMapMode = MM_TEXT;
m_paperColor = RGB(255, 255, 255);
m_bSimulating = FALSE;
//TODO: Color wires should probably be an option retained from one session to another.
m_bColorWires = FALSE;
m_iUnconnected = 0;
m_bKeepGoing = FALSE;
m_bWasUp = FALSE;
m_bPause = 0;
m_uProbe = 0;
m_uAnalyze = 0;
KeyBoardObj = NULL;
ComputePageSize();
::GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &lf);
// if (lf.lfHeight > 0)
// {
// lf.lfHeight = -lf.lfHeight;
// lf.lfWidth = 0;
// lf.lfQuality = 1;
// }
m_cfScreen.CreateFontIndirect(&lf);
m_cpBlack.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
m_cpGreen.CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
m_cpRed.CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
m_cpPurple.CreatePen(PS_SOLID, 1, RGB(255, 0, 255));
m_cbWhite.CreateSolidBrush(RGB(255, 255, 255));
m_cpBlue.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
m_cbBlue.CreateSolidBrush(RGB(0, 0, 255));
m_cpGrey.CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
m_cbGrey.CreateSolidBrush(RGB(192, 192, 192));
m_uCycleCount = 0;
m_iVersionMajor = VERSION_MAJOR;
m_iVersionMinor = VERSION_MINOR;
m_bReadOnly = ((CLogiApp *)AfxGetApp())->m_bReadOnly;
m_bSimRateSpecified = FALSE;
m_iSimRate = DEFAULTSIMRATE;
// pAnodeDummy = new Anode(UNKNOWN,"xyz");
m_pAnodeNULL = new Anode(UNKNOWN,"xyz",FLOAT);
}
CLogiDoc::~CLogiDoc()
{
POSITION pos;
CObject* pObj;
CObList Temp;
// Clear the undo/redo stack
// (Necessary to avoid wires in the undo stack referencing
// objects we are about to delete.)
m_undo.Reset();
// Copy the object list because it gets munged as we delete
pos = m_objects.GetHeadPosition();
while (pos != NULL)
{
pObj = m_objects.GetNext( pos );
if (!pObj->IsKindOf(RUNTIME_CLASS(CLogiWire))) Temp.AddTail(pObj);
else
delete pObj; // delete wires first
}
// Now do the deletes of non-wires
pos = Temp.GetHeadPosition();
while (pos != NULL)
{
pObj = Temp.GetNext( pos );
delete pObj;
}
// Now delete nodes
pos = NodeList.GetHeadPosition();
while (pos != NULL)
{
pObj = NodeList.GetNext( pos );
delete pObj;
}
// Now delete events (due to self inited devices)
EventQueue.RemoveAll();
PostEventQueue.RemoveAll();
while (!KeyboardMessageQueue.IsEmpty()) delete (AKeyboardMessage*) KeyboardMessageQueue.RemoveHead();
while (!MouseMessageQueue.IsEmpty()) delete (AMouseMessage*) MouseMessageQueue.RemoveHead();
while (!NetworkMessageQueue.IsEmpty()) delete (ANetworkMessage*) NetworkMessageQueue.RemoveHead();
UpdateNodeQueue.RemoveAll();
UpdateNodeQueue2.RemoveAll();
CallPostQueue.RemoveAll();
m_cfScreen.DeleteObject();
m_cpBlack.DeleteObject();
m_cpGreen.DeleteObject();
m_cpRed.DeleteObject();
m_cpPurple.DeleteObject();
m_cbWhite.DeleteObject();
m_cpBlue.DeleteObject();
m_cbBlue.DeleteObject();
// Delete the default NULL node
delete m_pAnodeNULL;
}
int CLogiDoc::GetZoom(int i)
{
if (m_iZoom == 0) return i;
else if (m_iZoom > 0) return i<<m_iZoom;
else return i>>(-m_iZoom);
}
BOOL CLogiDoc::CheckForStrays()
{
POSITION pos;
//POSITION vpos;
BOOL bOutSide = FALSE;
CRect rect;
// rect.left = -GetSize().cx / 2;
// rect.top = -GetSize().cy / 2;
rect.left = 0;
rect.top = 0;
rect.right = rect.left + GetSize().cx;
rect.bottom = rect.top + GetSize().cy;
rect.NormalizeRect();
// check each non-wire device to be fully on the page
pos = m_objects.GetHeadPosition();
while (pos != NULL)
{
CLogiObj* pObj = (CLogiObj*)m_objects.GetNext(pos);
// only non-wires
if (pObj->m_Logishape != wire)
{
CRect position = pObj->m_position;
// if any edge of device off any edge of page then fix
if ((position.left < rect.left)
|| (position.bottom < rect.top)
|| (position.right > rect.right)
|| (position.top > rect.bottom))
{
/*
int delta;
// fix appropriate edge
if (position.left < rect.left)
{
delta = position.left - rect.left;
position.right -= delta;
position.left -= delta;
}
if (position.bottom < rect.top)
{
delta = position.bottom - rect.top;
position.bottom -= delta;
position.top -= delta;
}
if (position.right > rect.right)
{
delta = rect.right - position.right;
position.right += delta;
position.left += delta;
}
if (position.top > rect.bottom)
{
delta = rect.bottom - position.top;
position.bottom += delta;
position.top += delta;
}
// move it in each view
vpos = GetFirstViewPosition();
while (vpos != NULL)
{
CLogiView* pView = (CLogiView*)GetNextView(vpos);
pObj->MoveTo(position, pView);
pView->SetPage(pObj->m_iPage);
pView->Select(pObj, TRUE);
}
*/
bOutSide = TRUE;
}
}
}
// let user know we fiddled with his work.
if (bOutSide)
{
UpdateAllViews(NULL);
//::MessageBox(::GetFocus(),"The selected devices were off page and have been moved onto the page", "Warning", MB_OK | MB_ICONEXCLAMATION);
::MessageBox(::GetFocus(),"Some devices are off of the page. They will not appear when printing.", "Warning", MB_OK | MB_ICONEXCLAMATION);
}
return bOutSide;
}
void CLogiDoc::Halt()
{
// This method is called by CLogiGate::Initialize and CLogiGate::Simulate
// methods if an error occurs. Now, it sets a halt flag and just delegates
// to OnSimulateStop().
// When halt is called the simulator might be running or it might
// be resetting (i.e., initializing).
// Set the halt flag (so any pending Reset() fails by returning FALSE)
m_bHalt = TRUE;
// If simulator running or stepping, stop it
if (m_bKeepGoing || m_bPause)
OnSimulateStop();
}
BOOL CLogiDoc::Reset(UINT iMode)
{
// To properly start the simulator, the following logic must
// be observed:
// if (!Reset(ID_SIMULATE_RUN)) // initialize all devices
// Reset(ID_SIMULATE_RESET); // if error, deinitialize all devices
// else
// Simulate(ID_SIMULATE_RUN); // no errors, run the simulator
POSITION pos;
POSITION pos2;
POSITION vpos;
CLogiObj* CurrentObj;
CLogiObj* CurrentObj2;
Anode* CurrentNode;
CLogiView* pView = NULL;
BOOL bWinNT;
m_uCycleCount = 0;
KeyBoardObj = NULL;
if (m_bWasUp)
{
CMainFrame *pMainFrm = (CMainFrame *)AfxGetMainWnd();
pMainFrm->ShowControlBar(&pMainFrm->m_wndPaletteBar, TRUE, FALSE);
m_bWasUp = FALSE;
}
// Erase grid in all views because it will not be updated during simulation
if (GetGridOn())
{
pos = GetFirstViewPosition();
while (pos != NULL)
{
pView = (CLogiView*)GetNextView(pos);
pView->Invalidate(FALSE);
}
}
// Start with everything deselected
// (However, do not deselect if switching to a stopped mode,
// since simulator may have selected components having detected errors.)
if (iMode == ID_SIMULATE_RUN || iMode == ID_SIMULATE_STEP)
{
vpos = GetFirstViewPosition();
while (vpos != NULL)
{
pView = (CLogiView*)GetNextView(vpos);
pView->Select(NULL);
}
}
// Check for lost little souls
//if(CheckForStrays()) return false;
// Init all Nodes to Unknown
for( pos = NodeList.GetHeadPosition(); pos != NULL; )
{
CurrentNode = (Anode*) NodeList.GetNext( pos );
CurrentNode->State = UNKNOWN;
CurrentNode->NextState = UNKNOWN;
CurrentNode->PrevState = UNKNOWN;
CurrentNode->DriveState = DRIVE;
CurrentNode->NextDriveState = DRIVE;
CurrentNode->PrevDriveState = DRIVE;
}
switch (m_iUnconnected)
{
case 0:
{
m_pAnodeNULL->State = UNKNOWN;
m_pAnodeNULL->DriveState = DRIVE;
break;
}
case 1:
{
m_pAnodeNULL->State = LO;
m_pAnodeNULL->DriveState = DRIVE;
break;
}
case 2:
{
m_pAnodeNULL->State = LO;
m_pAnodeNULL->DriveState = FLOAT;
break;
}
case 3:
{
m_pAnodeNULL->State = UNKNOWN;
m_pAnodeNULL->DriveState = FLOAT;
break;
}
}
// Some of the PostSimulate devices, such as the null node
// care about these values.
m_pAnodeNULL->NextState = m_pAnodeNULL->State;
m_pAnodeNULL->NextDriveState = m_pAnodeNULL->DriveState;
m_pAnodeNULL->PrevState = m_pAnodeNULL->State;
m_pAnodeNULL->PrevDriveState = m_pAnodeNULL->DriveState;
// Start with a clean Queue(s)
EventQueue.RemoveAll();
PostEventQueue.RemoveAll();
while (!KeyboardMessageQueue.IsEmpty()) delete (AKeyboardMessage*) KeyboardMessageQueue.RemoveHead();
while (!MouseMessageQueue.IsEmpty()) delete (AMouseMessage*) MouseMessageQueue.RemoveHead();
while (!NetworkMessageQueue.IsEmpty()) delete (ANetworkMessage*) NetworkMessageQueue.RemoveHead();
UpdateNodeQueue.RemoveAll();
UpdateNodeQueue2.RemoveAll();
CallPostQueue.RemoveAll();
// Clear shared memory map
m_cSharedMemory.clear();
// Initialize all devices (switches off, lights off, etc.)
bWinNT = IsWindowsNT();
for( pos = m_objects.GetHeadPosition(); pos != NULL; )
{
CurrentObj = (CLogiObj*) m_objects.GetNext( pos );
if (CurrentObj->IsKindOf(RUNTIME_CLASS(CLogiGate)))
{
// Initialize the device
vpos = GetFirstViewPosition();
while (vpos != NULL)
{
pView = (CLogiView*)GetNextView(vpos);
// m_bHalt is workaround since Initialize does not return a value.
// The Halt method in this class sets this flag.
m_bHalt = FALSE;
((CLogiGate *)CurrentObj)->Initialize(pView, iMode);
if (m_bHalt && (iMode == ID_SIMULATE_RUN || iMode == ID_SIMULATE_STEP))
return FALSE;
}
}
}
PlaySound(NULL, NULL, SND_PURGE);
NetworkShutdown();
// No need to do anything further if RESETting.
if (iMode != ID_SIMULATE_RUN && iMode != ID_SIMULATE_STEP)
return TRUE;
// Queue up all self generating devices (oscillators)
for( pos = m_objects.GetHeadPosition(); pos != NULL; )
{
CurrentObj= (CLogiObj*) m_objects.GetNext( pos );
if (CurrentObj->IsKindOf(RUNTIME_CLASS(CLogiGate)))
{
if (
(CurrentObj->m_Logishape == oscillatorgate) ||
(CurrentObj->m_Logishape == switchgate) ||
(CurrentObj->m_Logishape == selectgate) ||
(CurrentObj->m_Logishape == groundgate) ||
(CurrentObj->m_Logishape == plusgate) ||
(CurrentObj->m_Logishape == asciikeyboardgate) ||
(CurrentObj->m_Logishape == keypadgate) ||
(CurrentObj->m_Logishape == clockgate)
)
{
EventQueue.AddTail(CurrentObj);
}
}
}
// Queue up PostSimulate devices for the first cycle.
// Afterwards these devices enqueue themselves if input changes.
for( pos = m_objects.GetHeadPosition(); pos != NULL; )
{
CurrentObj = (CLogiObj*) m_objects.GetNext( pos );
if (CurrentObj->IsKindOf(RUNTIME_CLASS(CLogiGate)))
{
if (
(CurrentObj->m_Logishape == nullgate) ||
(CurrentObj->m_Logishape == busgate) ||
(CurrentObj->m_Logishape == signalsendergate)
)
{
CallPostQueue.AddTail(CurrentObj);
}
}
}
// Init all Robots and Playgrounds
for( pos = m_objects.GetHeadPosition(); pos != NULL; )
{
CurrentObj = (CLogiObj*) m_objects.GetNext( pos );
if (CurrentObj->m_Logishape == robotgate)
{
((CLogiRobotGate*) CurrentObj)->m_Playground = NULL;
}
else if (CurrentObj->m_Logishape == bitmapgate)
{
if (((CLogiBitmapGate*) CurrentObj)->m_bPlayground)
{
for (int i=0;i<16;i++) ((CLogiBitmapGate*) CurrentObj)->m_Robot[i] = NULL;
}
}
}
// Pair Robots and Playgrounds
for( pos = m_objects.GetHeadPosition(); pos != NULL; )
{
CurrentObj = (CLogiObj*) m_objects.GetNext( pos );
if (CurrentObj->m_Logishape == robotgate)
{
// Try robots on same page first
for( pos2 = m_objects.GetHeadPosition(); pos2 != NULL; )
{
CurrentObj2 = (CLogiObj*) m_objects.GetNext( pos2 );
if (CurrentObj2->m_Logishape == bitmapgate)
{
if ((((CLogiBitmapGate*) CurrentObj2)->m_bPlayground) && (CurrentObj->m_iPage == CurrentObj2->m_iPage ))
{
for (int i=0;i<16;i++)
{
if (((CLogiBitmapGate*) CurrentObj2)->m_Robot[i] == NULL)
{
((CLogiBitmapGate*) CurrentObj2)->m_Robot[i] = (CLogiRobotGate*) CurrentObj;
((CLogiRobotGate*) CurrentObj)->m_Playground = (CLogiBitmapGate*) CurrentObj2;
pos2 = NULL;
break;
}
}
}
}
}
// If still no home, try on any page
if (((CLogiRobotGate*) CurrentObj)->m_Playground == NULL)
{
for( pos2 = m_objects.GetHeadPosition(); pos2 != NULL; )
{
CurrentObj2 = (CLogiObj*) m_objects.GetNext( pos2 );
if (CurrentObj2->m_Logishape == bitmapgate)
{
if (((CLogiBitmapGate*) CurrentObj2)->m_bPlayground)
{
for (int i=0;i<16;i++)
{
if (((CLogiBitmapGate*) CurrentObj2)->m_Robot[i] == NULL)
{
((CLogiBitmapGate*) CurrentObj2)->m_Robot[i] = (CLogiRobotGate*) CurrentObj;
((CLogiRobotGate*) CurrentObj)->m_Playground = (CLogiBitmapGate*) CurrentObj2;
pos2 = NULL;
break;
}
}
}
}
}
}
// if still no home select robot and let user know
if (((CLogiRobotGate*) CurrentObj)->m_Playground == NULL)
{
vpos = GetFirstViewPosition();
while (vpos != NULL)
{
pView = (CLogiView*)GetNextView(vpos);
pView->SetPage(CurrentObj->m_iPage);
pView->Select(CurrentObj, TRUE);
}
::MessageBox(::GetFocus(),"The selected Robot has no Bitmap Playground", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
// Pair Signal Senders and Signal Receivers
CObList receivers;
CObList sendersSatisfied;
// Init pointers and make list of all receivers
for( pos = m_objects.GetHeadPosition(); pos != NULL; )
{
CurrentObj = (CLogiObj*) m_objects.GetNext( pos );
// init sender or receiver mates'
if (CurrentObj->m_Logishape == signalsendergate)
{
((CLogiSignalsenderGate*) CurrentObj)->m_Mates.RemoveAll();
}
else if (CurrentObj->m_Logishape == signalreceivergate)
{
((CLogiSignalreceiverGate*) CurrentObj)->m_Mate = NULL;
receivers.AddTail(CurrentObj);
}
}
// now look for senders
for (pos = m_objects.GetHeadPosition(); pos != NULL; )
{
CurrentObj = (CLogiObj*) m_objects.GetNext(pos);
if (CurrentObj->m_Logishape == signalsendergate)
{
CString signalname = ((CLogiSignalsenderGate*) CurrentObj)->m_csSignalName;
int signalwidth = ((CLogiSignalsenderGate*)CurrentObj)->m_iSignalWidth;
// find receiver(s)
for (pos2 = receivers.GetHeadPosition(); pos2 != NULL; )
{
POSITION thispos = pos2;
CurrentObj2 = (CLogiObj*) receivers.GetNext(pos2);
if (((CLogiSignalreceiverGate*) CurrentObj2)->m_csSignalName == signalname)
{
if (((CLogiSignalreceiverGate*) CurrentObj2)->m_iSignalWidth != signalwidth)
{
vpos = GetFirstViewPosition();
while (vpos != NULL)
{
pView = (CLogiView*)GetNextView(vpos);
pView->SetPage(CurrentObj2->m_iPage);
pView->Select(CurrentObj2, TRUE);
}
::MessageBox(::GetFocus(), "The width of the selected Signal Receiver does not match the width of its Sender", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// add
((CLogiSignalsenderGate*) CurrentObj)->m_Mates.AddTail(CurrentObj2);
receivers.RemoveAt(thispos);
}
}
// sender with no receivers?
if (((CLogiSignalsenderGate*) CurrentObj)->m_Mates.IsEmpty())
{
vpos = GetFirstViewPosition();
while (vpos != NULL)
{
pView = (CLogiView*)GetNextView(vpos);
pView->SetPage(CurrentObj->m_iPage);
pView->Select(CurrentObj, TRUE);
}
// is this because we previously satisfied a duplicate receiver,
// or because there is no such named receiver
for (pos2 = sendersSatisfied.GetHeadPosition(); pos2 != NULL ;)
{
CurrentObj2 = (CLogiObj*) sendersSatisfied.GetNext(pos2);
if (((CLogiSignalsenderGate*) CurrentObj2)->m_csSignalName == signalname)
{
pView->Select(CurrentObj2, TRUE);
::MessageBox(::GetFocus(),"The selected Signal Senders have the same name", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
::MessageBox(::GetFocus(),"The selected Signal Sender has no Receiver", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// done handling this sender
sendersSatisfied.AddTail(CurrentObj);
}
}
// leftover receivers without senders?
if (!receivers.IsEmpty())
{
CurrentObj = (CLogiObj*) receivers.GetHead();
vpos = GetFirstViewPosition();
while (vpos != NULL)
{
pView = (CLogiView*)GetNextView(vpos);
pView->SetPage(CurrentObj->m_iPage);
pView->Select(CurrentObj, TRUE);
}
::MessageBox(::GetFocus(),"The selected Signal Receiver has no Sender", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// Add marker event
EventQueue.AddTail((CObject*) NULL);
return TRUE;
}
void CLogiDoc::UpdateProbe()
{
switch (pAnodeProbe->PrevDriveState)
{
case DRIVE:
{
switch (pAnodeProbe->PrevState)
{
case LO:
{
m_uProbe = IDC_PROBE_OFF;
SetCursor(AfxGetApp()->LoadCursor(m_uProbe));
break;
}
case HI:
{
m_uProbe = IDC_PROBE_ON;
SetCursor(AfxGetApp()->LoadCursor(m_uProbe));
break;
}
case UNKNOWN:
{
m_uProbe = IDC_PROBE_UNKNOWN;
SetCursor(AfxGetApp()->LoadCursor(m_uProbe));
break;
}
}
break;
}
case FLOAT:
{
switch (pAnodeProbe->PrevState)
{
case LO:
{
m_uProbe = IDC_PROBE_MINUS;
SetCursor(AfxGetApp()->LoadCursor(m_uProbe));
break;
}
case HI:
{
m_uProbe = IDC_PROBE_PLUS;
SetCursor(AfxGetApp()->LoadCursor(m_uProbe));
break;
}
case UNKNOWN:
{
m_uProbe = IDC_PROBE_FLOAT;
SetCursor(AfxGetApp()->LoadCursor(m_uProbe));
break;
}
}
break;
}
}
}
void CLogiDoc::Simulate(UINT iMode)
{
// To properly start the simulator, the following logic must
// be observed:
// if (!Reset(ID_SIMULATE_RUN)) // initialize all devices
// Reset(ID_SIMULATE_RESET); // if error, deinitialize all devices
// else
// Simulate(ID_SIMULATE_RUN); // no errors, run the simulator
POSITION pos;
POSITION pos2;
CLogiGate* CurrentEventGate;
AKeyboardMessage* CurrentKeyboardMessage;
AMouseMessage* CurrentMouseMessage;
ANetworkMessage* CurrentNetworkMessage;
Anode* CurrentNode;
MSG msg;
CLogiView* pView;
unsigned int uCurrentTime;
unsigned int uWorkUnitStartTime; // when was unit of work started? (micro seconds)
int iWorkUnitExpectedTimeuS; // how much time is work unit supposed to take?
unsigned int uuSPerCycle = 0; // how many microseconds per cycle to keep desired rate?
// Set all views to select mode
// We call the "internal" version of the command because
// the usual version is already disabled at this point.
AfxGetApp()->m_pMainWnd->SendMessage(WM_COMMAND, ID_DRAW_SELECTOR_INTERNAL, 0L);
m_bKeepGoing = TRUE;
m_bForceStopAndExitApp = FALSE;
m_bForceStopAndCloseDoc = FALSE;
m_bCloseNotExit = FALSE;
m_bForceStopAndChangeDoc = FALSE;
// If palette is up shut it down on 1st cycle
if (m_uCycleCount == 0)
{
m_bWasUp = FALSE;
CMainFrame *pMainFrm = (CMainFrame *)AfxGetMainWnd();
if ((pMainFrm->m_wndPaletteBar.GetStyle() & WS_VISIBLE) != 0)
{
pMainFrm->ShowControlBar(&pMainFrm->m_wndPaletteBar, FALSE, FALSE);
m_bWasUp = TRUE;
}
}
// Compute microseconds to spend per cycle
if (m_bSimRateSpecified)
uuSPerCycle = 1000000/m_iSimRate;
uCurrentTime = GetCurrentTime();
// While User Allows Simulation to Continue, do so
uWorkUnitStartTime = GetTickCount();
iWorkUnitExpectedTimeuS = 0; // no work done yet
while (m_bKeepGoing)
{
// Simulate a Cycle (until a NULL "Marker" is seen)
while (1)
{
// Get and remove the event
CurrentEventGate = (CLogiGate*) EventQueue.RemoveHead();
// if NULL "Marker" then end of Cycle
if (CurrentEventGate == NULL)
{
break;
}
// Perform Simulation for this device
CurrentEventGate->Simulate(this);
// Check for Halt() call by Simulate
// This is a hack because Simulate does not return a value
if (!m_bKeepGoing)
break;
// Now Animate the Device in all Views
pos = GetFirstViewPosition();
while (pos != NULL)
{
pView = (CLogiView*)GetNextView(pos);
if (pView->m_iPage == CurrentEventGate->m_iPage)
{
CurrentEventGate->Animate(pView);
}
}
}
// If any keyboard events then process just one
//TODO: Look here for hotkeys while simulating?
if (!KeyboardMessageQueue.IsEmpty())
{
CurrentKeyboardMessage = (AKeyboardMessage*) KeyboardMessageQueue.RemoveHead();
((CLogiAsciikeyboardGate *) CurrentKeyboardMessage->pLogiGate)->Message(CurrentKeyboardMessage->pView, CurrentKeyboardMessage->nChar, CurrentKeyboardMessage->bDown);
delete CurrentKeyboardMessage;
}
// If any mouse events (actions) then process just one
if (!MouseMessageQueue.IsEmpty())
{
CurrentMouseMessage = (AMouseMessage*) MouseMessageQueue.RemoveHead();
CurrentMouseMessage->pLogiGate->Action(CurrentMouseMessage->pView, CurrentMouseMessage->bDown, CurrentMouseMessage->aPoint);
delete CurrentMouseMessage;
}
// If any network events then process just one (of each device)
if (!NetworkMessageQueue.IsEmpty())
{
for( pos = NetworkMessageQueue.GetHeadPosition(); pos != NULL; )
{
CurrentNetworkMessage = (ANetworkMessage*) NetworkMessageQueue.GetNext( pos );
((CLogiNetworkGate *) CurrentNetworkMessage->pLogiGate)->m_bQueue = FALSE;
}
for( pos = NetworkMessageQueue.GetHeadPosition(); pos != NULL; )
{
pos2 = pos;
CurrentNetworkMessage = (ANetworkMessage*) NetworkMessageQueue.GetNext( pos );
if (!((CLogiNetworkGate *) CurrentNetworkMessage->pLogiGate)->m_bQueue)
{
((CLogiNetworkGate *) CurrentNetworkMessage->pLogiGate)->m_bQueue = TRUE;
((CLogiNetworkGate *) CurrentNetworkMessage->pLogiGate)->Message(CurrentNetworkMessage->pDoc, CurrentNetworkMessage->lParam, CurrentNetworkMessage->wParam);
NetworkMessageQueue.RemoveAt(pos2);
delete CurrentNetworkMessage;
}
}
}
// Process PostEvents
// Copy "NextState" of Signal Senders input to Signal Receivers Output
// Copy "NextState" of Node input to Node Output
BOOL bChanged = TRUE;
while (bChanged)
{
bChanged = FALSE;
// These are objects that PostSimulate is always called on
for( pos = PostEventQueue.GetHeadPosition(); pos != NULL; )
{
CurrentEventGate = (CLogiGate*) PostEventQueue.GetNext( pos );
if (CurrentEventGate->PostSimulate(this)) bChanged = TRUE;
}
}
// These objects have indicated they need PostSimulate by
// enqueueing themselves
while (!CallPostQueue.IsEmpty())
{
CurrentEventGate = (CLogiGate*) CallPostQueue.RemoveHead();
CurrentEventGate->PostSimulate(this);
}
// Add NULL cycle "marker"
EventQueue.AddTail((CLogiGate*) NULL);
// Transfer Nodes from "Current" State to "Previous" State
while (!UpdateNodeQueue2.IsEmpty())
{
CurrentNode = (Anode *) UpdateNodeQueue2.RemoveHead();
CurrentNode->Update();
CurrentNode->LastCycle = m_uCycleCount;
}
// Transfer "Next" Node states to "Current" State
while (!UpdateNodeQueue.IsEmpty())
{
CurrentNode = (Anode *) UpdateNodeQueue.RemoveHead();
// Must only call Update once per cycle
if (CurrentNode->LastCycle != m_uCycleCount)
{
CurrentNode->Update();
CurrentNode->LastCycle = m_uCycleCount;
}
// We store 1 cycle of previous state, so queue
// an additional update
UpdateNodeQueue2.AddTail(CurrentNode);
}
//color wires
if(m_bColorWires)
{
// call update for each view, to draw the new wire colors
POSITION pos3 = GetFirstViewPosition();
while (pos3 != NULL) ((CLogiView*)GetNextView(pos3))->OnUpdate(NULL, HINT_UPDATE_WINDOW, NULL);
}
// Tally Cycle Count