-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathibm1130_gui.c
More file actions
1834 lines (1505 loc) · 66.6 KB
/
ibm1130_gui.c
File metadata and controls
1834 lines (1505 loc) · 66.6 KB
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
/* ibm1130_gui.c: IBM 1130 CPU simulator Console Display
*
* Based on the SIMH package written by Robert M Supnik
*
* (C) Copyright 2002, Brian Knittel.
* You may freely use this program, but: it offered strictly on an AS-IS, AT YOUR OWN
* RISK basis, there is no warranty of fitness for any purpose, and the rest of the
* usual yada-yada. Please keep this notice and the copyright in any distributions
* or modifications.
*
* This is not a supported product, but I welcome bug reports and fixes.
* Mail to simh@ibm1130.org
*
* 30-Dec-05 BLK Fixed mask for IAR and SAR register display and added display
* of Arithmetic Factor, per Carl Claunch.
*
* 09-Apr-04 BLK Changed code to use stock windows cursor IDC_HAND if available
*
* 02-Dec-02 BLK Changed display, added printer and card reader icons
* Added drag and drop support for scripts and card decks
* Added support for physical card reader and printer (hides icons)
*
* 17-May-02 BLK Pulled out of ibm1130_cpu.c
*/
/* ------------------------------------------------------------------------
* Definitions
* ------------------------------------------------------------------------ */
#include <stdarg.h>
#include <ctype.h>
#include <math.h>
#include "ibm1130_defs.h"
#include "ibm1130res.h"
#include "sim_tmxr.h"
#define UPDATE_BY_TIMER
#ifdef UPDATE_BY_TIMER
# define UPDATE_INTERVAL 20 /* set to desired number of updates/second */
#else
# define UPDATE_INTERVAL 5000 /* GUI: set to 100000/f where f = desired updates/second of 1130 time */
#endif
#define UNIT_V_CR_EMPTY (UNIT_V_UF + 5) /* NOTE: THESE MUST MATCH THE DEFINITION IN ibm1130_cr.c */
#define UNIT_CR_EMPTY (1u << UNIT_V_CR_EMPTY)
#define UNIT_V_PHYSICAL (UNIT_V_UF + 9)
#define UNIT_PHYSICAL (1u << UNIT_V_PHYSICAL)
#define UNIT_V_PHYSICAL_PTR (UNIT_V_UF + 10) /* NOTE: THESE MUST MATCH THE DEFINITION IN ibm1130_prt.c */
#define UNIT_PHYSICAL_PTR (1u << UNIT_V_PHYSICAL_PTR)
/* I think I had it wrong; Program Load actually does start the processor after
* reading in the card?
*/
#define PROGRAM_LOAD_STARTS_CPU
/* ------------------------------------------------------------------------
* Function declarations
* ------------------------------------------------------------------------ */
t_stat console_reset (DEVICE *dptr);
/* ------------------------------------------------------------------------
* Console display - on Windows builds (only) this code displays the 1130 console
* and toggle switches. It really enhances the experience.
*
* Currently, when the IPS throttle is nonzero, I update the display after every
* UPDATE_INTERVAL instructions, plus or minus a random amount to avoid aliased
* sampling in loops. When UPDATE_INTERVAL is defined as zero, we update every
* instruction no matter what the throttle. This makes the simulator too slow
* but it's cool and helpful during development.
* ------------------------------------------------------------------------ */
#define UNIT_V_DISPLAY (UNIT_V_UF + 0)
#define UNIT_DISPLAY (1u << UNIT_V_DISPLAY)
MTAB console_mod[] = {
{ UNIT_DISPLAY, 0, "off", "OFF", NULL },
{ UNIT_DISPLAY, UNIT_DISPLAY, "on", "ON", NULL },
{ 0 }
};
UNIT console_unit = {UDATA (NULL, UNIT_DISABLE|UNIT_DISPLAY, 0) };
DEVICE console_dev = {
"GUI", &console_unit, NULL, console_mod,
1, 16, 16, 1, 16, 16,
NULL, NULL, console_reset,
NULL, NULL, NULL
};
/* reset for the "console" display device */
extern UNIT cr_unit; /* pointers to 1442 and 1132 (1403) printers */
extern UNIT prt_unit;
extern UNIT dsk_unit[];
extern int boot_drive;
extern t_bool program_is_loaded;
#ifndef GUI_SUPPORT
void update_gui (t_bool force) {} /* stubs for non-GUI builds */
void forms_check (int set) {}
void print_check (int set) {}
void keyboard_select (int select) {}
void keyboard_selected (int select) {}
void disk_ready (int ready) {}
void disk_unlocked (int unlocked) {}
void gui_run (int running) {}
t_stat console_reset (DEVICE *dptr) {return SCPE_OK;}
long stuff_cmd (char *cmd) {return 0;}
t_bool stuff_and_wait (char *cmd, int timeout, int delay) {return FALSE;}
char *read_cmdline (char *ptr, int size, FILE *stream) {return read_line(ptr, size, stream);}
void remark_cmd (char *remark) {sim_printf("%s\n", remark);}
#else
static HWND hConsoleWindow = NULL;
t_stat console_reset (DEVICE *dptr)
{
if (! sim_gui) {
SETBIT(console_unit.flags, UNIT_DIS); /* disable the GUI */
CLRBIT(console_unit.flags, UNIT_DISPLAY); /* turn the GUI off */
} else {
if (!hConsoleWindow)
hConsoleWindow = GetConsoleWindow();
}
update_gui(FALSE);
return SCPE_OK;
}
/* scp_panic - report fatal internal programming error */
void scp_panic (const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
#ifdef _WIN32
/* only _WIN32 is defined right now */
#include <windows.h>
#define BUTTON_WIDTH 90
#define BUTTON_HEIGHT 50
#define IDC_UNUSED 0
#define IDC_DISK_UNLOCK 1
#define IDC_RUN 2
#define IDC_KEYBOARD_SELECT 3
#define IDC_POWER_ON 4
#define IDC_FILE_READY 5
#define IDC_PARITY_CHECK 6
#define IDC_FORMS_CHECK 7
#define IDC_POWER 8
#define IDC_PROGRAM_START 9
#define IDC_PROGRAM_STOP 10
#define IDC_LOAD_IAR 11
#define IDC_KEYBOARD 12
#define IDC_IMM_STOP 13
#define IDC_RESET 14
#define IDC_PROGRAM_LOAD 15
#define IDC_TEAR 16 /* standard button */
#define IDC_1442 17 /* device images */
#define IDC_1132 18
#define LAMPTIME 500 /* 500 msec delay on updating */
#define FLASH_TIMER_ID 1
#define UPDATE_TIMER_ID 2
#define RUNSWITCH_X 689 /* center of the run mode switch dial */
#define RUNSWITCH_Y 107
#define TOGGLES_X 122 /* left edge of series of toggle switches */
#define TXTBOX_X 200 /* text labels showing attached devices */
#define TXTBOX_Y 300
#define TXTBOX_WIDTH 195
#define TXTBOX_HEIGHT 12
static BOOL class_defined = FALSE;
static HWND hConsoleWnd = NULL;
static HBITMAP hBitmap = NULL;
static HFONT hFont = NULL;
static HFONT hBtnFont = NULL;
static HFONT hTinyFont = NULL;
static HBRUSH hbLampOut = NULL;
static HBRUSH hbWhite = NULL;
static HBRUSH hbBlack = NULL;
static HBRUSH hbGray = NULL;
static HPEN hSwitchPen = NULL;
static HPEN hWhitePen = NULL;
static HPEN hBlackPen = NULL;
static HPEN hLtGreyPen = NULL;
static HPEN hGreyPen = NULL;
static HPEN hDkGreyPen = NULL;
static int hUpdateTimer = 0;
static int hFlashTimer = 0;
static HCURSOR hcArrow = NULL;
static HCURSOR hcHand = NULL;
static HINSTANCE hInstance;
static HDC hCDC = NULL;
static char szConsoleClassName[] = "1130CONSOLE";
static DWORD PumpID = 0;
static HANDLE hPump = INVALID_HANDLE_VALUE;
static int bmwid, bmht;
static HANDLE hbm1442_full, hbm1442_empty, hbm1442_eof, hbm1442_middle;
static HANDLE hbm1132_full, hbm1132_empty;
static struct tag_btn {
int x, y, wx, wy;
char *txt;
BOOL pushable, state;
COLORREF clr;
HBRUSH hbrLit, hbrDark;
HWND hBtn;
BOOL subclassed;
} btn[] = {
0, 0, BUTTON_WIDTH, BUTTON_HEIGHT, "", FALSE, FALSE, RGB(255,255,180), NULL, NULL, NULL, TRUE,
0, 1, BUTTON_WIDTH, BUTTON_HEIGHT, "DISK\nUNLOCK", FALSE, TRUE, RGB(255,255,180), NULL, NULL, NULL, TRUE,
0, 2, BUTTON_WIDTH, BUTTON_HEIGHT, "RUN", FALSE, FALSE, RGB(0,255,0), NULL, NULL, NULL, TRUE,
0, 3, BUTTON_WIDTH, BUTTON_HEIGHT, "K B\nSELECT", FALSE, FALSE, RGB(255,255,180), NULL, NULL, NULL, TRUE,
1, 0, BUTTON_WIDTH, BUTTON_HEIGHT, "POWER\nON", FALSE, TRUE, RGB(255,255,180), NULL, NULL, NULL, TRUE,
1, 1, BUTTON_WIDTH, BUTTON_HEIGHT, "FILE\nREADY", FALSE, FALSE, RGB(0,255,0), NULL, NULL, NULL, TRUE,
1, 2, BUTTON_WIDTH, BUTTON_HEIGHT, "PARITY\nCHECK", FALSE, FALSE, RGB(255,0,0), NULL, NULL, NULL, TRUE,
1, 3, BUTTON_WIDTH, BUTTON_HEIGHT, "FORMS\nCHECK", FALSE, FALSE, RGB(255,255,0), NULL, NULL, NULL, TRUE,
2, 0, BUTTON_WIDTH, BUTTON_HEIGHT, "POWER", TRUE, FALSE, RGB(255,255,180), NULL, NULL, NULL, TRUE,
2, 1, BUTTON_WIDTH, BUTTON_HEIGHT, "PROGRAM\nSTART", TRUE, FALSE, RGB(0,255,0), NULL, NULL, NULL, TRUE,
2, 2, BUTTON_WIDTH, BUTTON_HEIGHT, "PROGRAM\nSTOP", TRUE, FALSE, RGB(255,0,0), NULL, NULL, NULL, TRUE,
2, 3, BUTTON_WIDTH, BUTTON_HEIGHT, "LOAD\nIAR", TRUE, FALSE, RGB(0,0,255), NULL, NULL, NULL, TRUE,
3, 0, BUTTON_WIDTH, BUTTON_HEIGHT, "KEYBOARD", TRUE, FALSE, RGB(255,255,180), NULL, NULL, NULL, TRUE,
3, 1, BUTTON_WIDTH, BUTTON_HEIGHT, "IMM\nSTOP", TRUE, FALSE, RGB(255,0,0), NULL, NULL, NULL, TRUE,
3, 2, BUTTON_WIDTH, BUTTON_HEIGHT, "RESET", TRUE, FALSE, RGB(0,0,255), NULL, NULL, NULL, TRUE,
3, 3, BUTTON_WIDTH, BUTTON_HEIGHT, "PROGRAM\nLOAD", TRUE, FALSE, RGB(0,0,255), NULL, NULL, NULL, TRUE,
TXTBOX_X+40, TXTBOX_Y+25, 35, 12, "Tear", TRUE, FALSE, 0, NULL, NULL, NULL, FALSE,
635, 238, 110, 110, "EMPTY_1442", TRUE, FALSE, 0, NULL, NULL, NULL, FALSE,
635, 366, 110, 110, "EMPTY_1132", TRUE, FALSE, 0, NULL, NULL, NULL, FALSE,
};
#define NBUTTONS (sizeof(btn) / sizeof(btn[0]))
#define STATE_1442_EMPTY 0 /* no cards (no file attached) */
#define STATE_1442_FULL 1 /* cards in hopper (file attached at BOF) */
#define STATE_1442_MIDDLE 2 /* cards in hopper and stacker (file attached, neither EOF nor BOF) */
#define STATE_1442_EOF 3 /* cards in stacker (file attached, at EOF) */
#define STATE_1442_HIDDEN 4 /* simulator is attached to physical card reader */
#define STATE_1132_EMPTY 0 /* no paper hanging out of printer */
#define STATE_1132_FULL 1 /* paper hanging out of printer */
#define STATE_1132_HIDDEN 2 /* printer is attached to physical printer */
static struct tag_txtbox {
int x, y;
char *txt;
char *unitname;
int idctrl;
} txtbox[] = {
TXTBOX_X, TXTBOX_Y, "Card Reader", "CR", -1,
TXTBOX_X, TXTBOX_Y+ 25, "Printer", "PRT", IDC_1132,
TXTBOX_X, TXTBOX_Y+ 50, "Disk 0", "DSK0", -1,
TXTBOX_X, TXTBOX_Y+ 75, "Disk 1", "DSK1", -1,
TXTBOX_X, TXTBOX_Y+100, "Disk 2", "DSK2", -1,
TXTBOX_X, TXTBOX_Y+125, "Disk 3", "DSK3", -1,
TXTBOX_X, TXTBOX_Y+150, "Disk 4", "DSK4", -1,
};
#define NTXTBOXES (sizeof(txtbox) / sizeof(txtbox[0]))
#define TXTBOX_BOTTOM (TXTBOX_Y+150)
static void init_console_window (void);
static void destroy_console_window (void);
LRESULT CALLBACK ConsoleWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static DWORD WINAPI Pump (LPVOID arg);
static void accept_dropped_file (HANDLE hDrop);
static void tear_printer (void);
#define NIXOBJECT(hObj) if (hObj != NULL) {DeleteObject(hObj); hObj = NULL;}
/* ------------------------------------------------------------------------
* init_console_window - display the 1130 console. Actually just creates a thread
* to run the Pump routine which does the actual work.
* ------------------------------------------------------------------------ */
static void init_console_window (void)
{
static BOOL did_atexit = FALSE;
if (hConsoleWnd != NULL)
return;
if (PumpID == 0)
hPump = CreateThread(NULL, 0, Pump, 0, 0, &PumpID);
if (! did_atexit) {
atexit(destroy_console_window);
did_atexit = TRUE;
}
}
/* ------------------------------------------------------------------------
* destroy_console_window - delete GDI objects.
* ------------------------------------------------------------------------ */
static void destroy_console_window (void)
{
int i;
if (hConsoleWnd != NULL)
SendMessage(hConsoleWnd, WM_CLOSE, 0, 0); /* cross thread call is OK */
if (hPump != INVALID_HANDLE_VALUE) { /* this is not the most graceful way to do it */
TerminateThread(hPump, 0);
hPump = INVALID_HANDLE_VALUE;
PumpID = 0;
hConsoleWnd = NULL;
}
if (hCDC != NULL) {
DeleteDC(hCDC);
hCDC = NULL;
}
NIXOBJECT(hBitmap)
NIXOBJECT(hbLampOut)
NIXOBJECT(hFont)
NIXOBJECT(hBtnFont);
NIXOBJECT(hTinyFont);
NIXOBJECT(hcHand)
NIXOBJECT(hSwitchPen)
NIXOBJECT(hLtGreyPen)
NIXOBJECT(hGreyPen)
NIXOBJECT(hDkGreyPen)
for (i = 0; i < NBUTTONS; i++) {
NIXOBJECT(btn[i].hbrLit);
NIXOBJECT(btn[i].hbrDark);
}
/* if (class_defined) {
UnregisterClass(hInstance, szConsoleClassName);
class_defined = FALSE;
}
*/
}
/* ------------------------------------------------------------------------
* these variables hold the displayed versions of the system registers
* ------------------------------------------------------------------------ */
static int shown_iar = 0, shown_sar = 0, shown_sbr = 0, shown_afr = 0, shown_acc = 0, shown_ext = 0;
static int shown_op = 0, shown_tag = 0, shown_irq = 0, shown_ccc = 0, shown_cnd = 0, shown_wait = 0;
static int shown_ces = 0, shown_arf = 0, shown_runmode = MODE_RUN;
static int CND;
/* ------------------------------------------------------------------------
* RedrawRegion - mark a region for redrawing without background erase
* ------------------------------------------------------------------------ */
static void RedrawRegion (HWND hWnd, int left, int top, int right, int bottom)
{
RECT r;
r.left = left;
r.top = top;
r.right = right;
r.bottom = bottom;
InvalidateRect(hWnd, &r, FALSE);
}
/* ------------------------------------------------------------------------
* RepaintRegion - mark a region for redrawing with background erase
* ------------------------------------------------------------------------ */
static void RepaintRegion (HWND hWnd, int left, int top, int right, int bottom)
{
RECT r;
r.left = left;
r.top = top;
r.right = right;
r.bottom = bottom;
InvalidateRect(hWnd, &r, TRUE);
}
/* ------------------------------------------------------------------------
* update_gui - sees if anything on the console display has changed, and invalidates
* the changed regions. Then it calls UpdateWindow to force an immediate repaint. This
* function (update_gui) should probably not be called every time through the main
* instruction loop but it should be called at least whenever wait_state or int_req change, and then
* every so many instructions. It's also called after every simh command so manual changes are
* reflected instantly.
* ------------------------------------------------------------------------ */
void update_gui (t_bool force)
{
int i;
BOOL state;
static int in_here = FALSE;
static int32 displayed = 0;
RECT xin;
if ((int32)(console_unit.flags & UNIT_DISPLAY) != displayed) { /* setting has changed */
displayed = console_unit.flags & UNIT_DISPLAY;
if (displayed)
init_console_window();
else
destroy_console_window();
}
if (hConsoleWnd == NULL)
return;
GUI_BEGIN_CRITICAL_SECTION /* only one thread at a time, please */
if (in_here) {
GUI_END_CRITICAL_SECTION
return;
}
in_here = TRUE;
GUI_END_CRITICAL_SECTION
CND = 0; /* combine carry and V as two bits */
if (C)
CND |= 2;
if (V)
CND |= 1;
if ((boot_drive<0) || (!program_is_loaded)) {
boot_drive = CES & 7;
if (boot_drive > 4)
boot_drive = -1;
}
if ((boot_drive>=0) && (dsk_unit[boot_drive].flags&UNIT_ATT)) {
disk_ready(TRUE);
disk_unlocked(FALSE);
}
else {
disk_ready(FALSE);
disk_unlocked(TRUE);
}
int_lamps |= int_req;
if (ipl >= 0)
int_lamps |= (0x20 >> ipl);
if (RUNMODE == MODE_LOAD)
SBR = CES; /* in load mode, SBR follows the console switches */
if (IAR != shown_iar)
{shown_iar = IAR; RedrawRegion(hConsoleWnd, 75, 8, 364, 32);} /* lamps: don't bother erasing bkgnd */
if (SAR != shown_sar)
{shown_sar = SAR; RedrawRegion(hConsoleWnd, 75, 42, 364, 65);}
if (ARF != shown_arf)
{shown_arf = ARF; RedrawRegion(hConsoleWnd, 75, 114, 364, 136);}
if (ACC != shown_acc)
{shown_acc = ACC; RedrawRegion(hConsoleWnd, 75, 141, 364, 164);}
if (EXT != shown_ext)
{shown_ext = EXT; RedrawRegion(hConsoleWnd, 75, 174, 364, 197);}
if (SBR != shown_sbr)
{shown_sbr = SBR; RedrawRegion(hConsoleWnd, 75, 77, 364, 97);}
if (OP != shown_op)
{shown_op = OP; RedrawRegion(hConsoleWnd, 501, 8, 595, 32);}
if (TAG != shown_tag)
{shown_tag = TAG; RedrawRegion(hConsoleWnd, 501, 77, 595, 97);}
if (int_lamps != shown_irq)
{shown_irq = int_lamps; RedrawRegion(hConsoleWnd, 501, 108, 595, 130);}
if (CCC != shown_ccc)
{shown_ccc = CCC; RedrawRegion(hConsoleWnd, 501, 141, 595, 164);}
if (CND != shown_cnd)
{shown_cnd = CND; RedrawRegion(hConsoleWnd, 501, 174, 595, 197);}
if ((wait_state|wait_lamp) != shown_wait)
{shown_wait= (wait_state|wait_lamp); RedrawRegion(hConsoleWnd, 380, 77, 414, 97);}
if (CES != shown_ces)
{shown_ces = CES; RepaintRegion(hConsoleWnd, TOGGLES_X-7, 230, TOGGLES_X+360, 275);} /* console entry sw: do erase bkgnd */
if (RUNMODE != shown_runmode)
{shown_runmode = RUNMODE;RepaintRegion(hConsoleWnd, RUNSWITCH_X-50, RUNSWITCH_Y-50, RUNSWITCH_X+50, RUNSWITCH_Y+50);}
int_lamps = 0;
/* this loop works with lamp buttons that are calculated on-the-fly only */
for (i = 0; i < NBUTTONS; i++) {
if (btn[i].pushable)
continue;
switch (i) {
case IDC_RUN:
state = hFlashTimer || (running && ! wait_state);
break;
/* this button is always off
case IDC_PARITY_CHECK
*/
/* these buttons are enabled/disabled directly
case IDC_POWER_ON:
case IDC_FILE_READY:
case IDC_FORMS_CHECK:
case IDC_KEYBOARD_SELECT:
case IDC_DISK_UNLOCK:
*/
default:
continue;
}
if (state != btn[i].state) { /* state has changed */
EnableWindow(btn[i].hBtn, state);
btn[i].state = state;
}
}
if (force) { /* if force flag is set, update text region */
SetRect(&xin, TXTBOX_X, TXTBOX_Y, TXTBOX_X+TXTBOX_WIDTH, TXTBOX_BOTTOM+2*TXTBOX_HEIGHT);
InvalidateRect(hConsoleWnd, &xin, TRUE);
}
state = ((cr_unit.flags & UNIT_ATT) == 0) ? STATE_1442_EMPTY :
(cr_unit.flags & UNIT_PHYSICAL) ? STATE_1442_HIDDEN :
(cr_unit.flags & UNIT_CR_EMPTY) ? STATE_1442_EOF :
cr_unit.pos ? STATE_1442_MIDDLE :
STATE_1442_FULL;
if (state != btn[IDC_1442].state) {
if (state == STATE_1442_HIDDEN)
ShowWindow(btn[IDC_1442].hBtn, SW_HIDE);
else {
if (btn[IDC_1442].state == STATE_1442_HIDDEN)
ShowWindow(btn[IDC_1442].hBtn, SW_SHOWNA);
SendMessage(btn[IDC_1442].hBtn, STM_SETIMAGE, IMAGE_BITMAP,
(LPARAM) (
(state == STATE_1442_FULL) ? hbm1442_full :
(state == STATE_1442_MIDDLE) ? hbm1442_middle :
(state == STATE_1442_EOF) ? hbm1442_eof :
hbm1442_empty));
}
btn[IDC_1442].state = state;
}
state = ((prt_unit.flags & UNIT_ATT) == 0) ? STATE_1132_EMPTY :
(prt_unit.flags & UNIT_PHYSICAL_PTR) ? STATE_1132_HIDDEN :
prt_unit.pos ? STATE_1132_FULL :
STATE_1132_EMPTY;
if (state != btn[IDC_1132].state) {
if (state == STATE_1132_HIDDEN)
ShowWindow(btn[IDC_1132].hBtn, SW_HIDE);
else {
if (btn[IDC_1132].state == STATE_1132_HIDDEN)
ShowWindow(btn[IDC_1132].hBtn, SW_SHOWNA);
SendMessage(btn[IDC_1132].hBtn, STM_SETIMAGE, IMAGE_BITMAP,
(LPARAM) (
(state == STATE_1132_FULL) ? hbm1132_full : hbm1132_empty));
}
btn[IDC_1132].state = state;
}
in_here = FALSE;
}
WNDPROC oldButtonProc = NULL;
/* ------------------------------------------------------------------------
* ------------------------------------------------------------------------ */
LRESULT CALLBACK ButtonProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int i;
i = GetWindowLongPtr(hWnd, GWLP_ID);
if (! btn[i].pushable) {
if (uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONUP || uMsg == WM_LBUTTONDBLCLK)
return 0;
if (uMsg == WM_CHAR)
if ((TCHAR) wParam == ' ')
return 0;
}
return CallWindowProc(oldButtonProc, hWnd, uMsg, wParam, lParam);
}
/* ------------------------------------------------------------------------
* ------------------------------------------------------------------------ */
static int occurs (char *txt, char ch)
{
int count = 0;
while (*txt)
if (*txt++ == ch)
count++;
return count;
}
/* ------------------------------------------------------------------------
* turns out to get properly colored buttons you have to paint them yourself. Sheesh.
* On the plus side, this lets do a better job of aligning the button text than
* the button would by itself.
* ------------------------------------------------------------------------ */
void PaintButton (LPDRAWITEMSTRUCT dis)
{
int i = dis->CtlID, nc, nlines, x, y, dy;
BOOL down = dis->itemState & ODS_SELECTED;
HPEN hOldPen;
HFONT hOldFont;
UINT oldAlign;
COLORREF oldBk;
char *txt, *tstart;
if (! BETWEEN(i, 0, NBUTTONS-1))
return;
if (! btn[i].subclassed)
return;
FillRect(dis->hDC, &dis->rcItem, ((btn[i].pushable || power) && IsWindowEnabled(btn[i].hBtn)) ? btn[i].hbrLit : btn[i].hbrDark);
if (! btn[i].pushable) {
hOldPen = SelectObject(dis->hDC, hBlackPen);
MoveToEx(dis->hDC, dis->rcItem.left, dis->rcItem.top, NULL);
LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top);
LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.bottom-1);
LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.bottom-1);
LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.top);
}
else if (down) {
/* do the three-D thing */
hOldPen = SelectObject(dis->hDC, hDkGreyPen);
MoveToEx(dis->hDC, dis->rcItem.left, dis->rcItem.bottom-2, NULL);
LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.top);
LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top);
SelectObject(dis->hDC, hWhitePen);
MoveToEx(dis->hDC, dis->rcItem.left, dis->rcItem.bottom-1, NULL);
LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.bottom-1);
LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top);
SelectObject(dis->hDC, hGreyPen);
MoveToEx(dis->hDC, dis->rcItem.left+1, dis->rcItem.bottom-3, NULL);
LineTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.top+1);
LineTo(dis->hDC, dis->rcItem.right-3, dis->rcItem.top+1);
}
else {
hOldPen = SelectObject(dis->hDC, hWhitePen);
MoveToEx(dis->hDC, dis->rcItem.left, dis->rcItem.bottom-2, NULL);
LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.top);
LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top);
SelectObject(dis->hDC, hDkGreyPen);
MoveToEx(dis->hDC, dis->rcItem.left, dis->rcItem.bottom-1, NULL);
LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.bottom-1);
LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top);
SelectObject(dis->hDC, hGreyPen);
MoveToEx(dis->hDC, dis->rcItem.left+1, dis->rcItem.bottom-2, NULL);
LineTo(dis->hDC, dis->rcItem.right-2, dis->rcItem.bottom-2);
LineTo(dis->hDC, dis->rcItem.right-2, dis->rcItem.top+1);
}
SelectObject(dis->hDC, hOldPen);
hOldFont = SelectObject(dis->hDC, hBtnFont);
oldAlign = SetTextAlign(dis->hDC, TA_CENTER|TA_TOP);
oldBk = SetBkMode(dis->hDC, TRANSPARENT);
txt = btn[i].txt;
nlines = occurs(txt, '\n')+1;
x = (dis->rcItem.left + dis->rcItem.right) / 2;
y = (dis->rcItem.top + dis->rcItem.bottom) / 2;
dy = 14;
y = y - (nlines*dy)/2;
if (down) {
x += 1;
y += 1;
}
for (;;) {
for (nc = 0, tstart = txt; *txt && *txt != '\n'; txt++, nc++)
;
TextOut(dis->hDC, x, y, tstart, nc);
if (*txt == '\0')
break;
txt++;
y += dy;
}
SetTextAlign(dis->hDC, oldAlign);
SetBkMode(dis->hDC, oldBk);
SelectObject(dis->hDC, hOldFont);
}
/* ------------------------------------------------------------------------
* ------------------------------------------------------------------------ */
HWND CreateSubclassedButton (HWND hwParent, UINT_PTR i)
{
HWND hBtn;
int x, y;
int r, g, b;
y = bmht - (4*BUTTON_HEIGHT) + BUTTON_HEIGHT * btn[i].y;
x = (btn[i].x < 2) ? (btn[i].x*BUTTON_WIDTH) : (598 - (4-btn[i].x)*BUTTON_WIDTH);
if ((hBtn = CreateWindow("BUTTON", btn[i].txt, WS_CHILD|WS_VISIBLE|BS_CENTER|BS_MULTILINE|BS_OWNERDRAW,
x, y, BUTTON_WIDTH, BUTTON_HEIGHT, hwParent, (HMENU) i, hInstance, NULL)) == NULL)
return NULL;
btn[i].hBtn = hBtn;
if (oldButtonProc == NULL)
oldButtonProc = (WNDPROC) GetWindowLongPtr(hBtn, GWLP_WNDPROC);
btn[i].hbrLit = CreateSolidBrush(btn[i].clr);
if (! btn[i].pushable) {
r = GetRValue(btn[i].clr) / 4;
g = GetGValue(btn[i].clr) / 4;
b = GetBValue(btn[i].clr) / 4;
btn[i].hbrDark = CreateSolidBrush(RGB(r,g,b));
EnableWindow(hBtn, FALSE);
}
SetWindowLongPtr(hBtn, GWLP_WNDPROC, (UINT_PTR) ButtonProc);
return hBtn;
}
/* ------------------------------------------------------------------------
* Pump - thread that takes care of the console window. It has to be a separate thread so that it gets
* execution time even when the simulator is compute-bound or IO-blocked. This routine creates the window
* and runs a standard Windows message pump. The window function does the actual display work.
* ------------------------------------------------------------------------ */
static DWORD WINAPI Pump (LPVOID arg)
{
MSG msg;
int wx, wy;
UINT_PTR i;
RECT r, ra;
BITMAP bm;
WNDCLASS cd;
HDC hDC;
HWND hActWnd;
hActWnd = GetForegroundWindow();
if (! class_defined) { /* register Window class */
hInstance = GetModuleHandle(NULL);
memset(&cd, 0, sizeof(cd));
cd.style = CS_NOCLOSE;
cd.lpfnWndProc = ConsoleWndProc;
cd.cbClsExtra = 0;
cd.cbWndExtra = 0;
cd.hInstance = hInstance;
cd.hIcon = NULL;
cd.hCursor = hcArrow;
cd.hbrBackground = NULL;
cd.lpszMenuName = NULL;
cd.lpszClassName = szConsoleClassName;
if (! RegisterClass(&cd)) {
PumpID = 0;
return 0;
}
class_defined = TRUE;
}
hbWhite = GetStockObject(WHITE_BRUSH); /* create or fetch useful GDI objects */
hbBlack = GetStockObject(BLACK_BRUSH); /* create or fetch useful GDI objects */
hbGray = GetStockObject(GRAY_BRUSH);
hSwitchPen = CreatePen(PS_SOLID, 5, RGB(255,255,255));
hWhitePen = GetStockObject(WHITE_PEN);
hBlackPen = GetStockObject(BLACK_PEN);
hLtGreyPen = CreatePen(PS_SOLID, 1, RGB(190,190,190));
hGreyPen = CreatePen(PS_SOLID, 1, RGB(128,128,128));
hDkGreyPen = CreatePen(PS_SOLID, 1, RGB(64,64,64));
hcArrow = LoadCursor(NULL, IDC_ARROW);
#ifdef IDC_HAND
hcHand = LoadCursor(NULL, IDC_HAND); /* use stock object provided by Windows */
if (hcHand == NULL)
hcHand = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_MYHAND));
#else
hcHand = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_MYHAND));
#endif
if (hBitmap == NULL)
hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_CONSOLE));
if (hbLampOut == NULL)
hbLampOut = CreateSolidBrush(RGB(50,50,50));
if (hFont == NULL)
hFont = CreateFont(-10, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, FIXED_PITCH, FF_SWISS, "Arial");
if (hBtnFont == NULL)
hBtnFont = CreateFont(-12, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, FIXED_PITCH, FF_SWISS, "Arial");
if (hTinyFont == NULL)
hTinyFont = CreateFont(-10, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, FIXED_PITCH, FF_SWISS, "Arial");
if (hConsoleWnd == NULL) { /* create window */
if ((hConsoleWnd = CreateWindow(szConsoleClassName, "IBM 1130", WS_OVERLAPPED|WS_CLIPCHILDREN, 0, 0, 200, 200, NULL, NULL, hInstance, NULL)) == NULL) {
PumpID = 0;
return 0;
}
DragAcceptFiles(hConsoleWnd, TRUE); /* let it accept dragged files (scripts) */
}
GetObject(hBitmap, sizeof(bm), &bm); /* get bitmap size */
bmwid = bm.bmWidth;
bmht = bm.bmHeight;
for (i = 0; i < NBUTTONS; i++) {
if (! btn[i].subclassed)
continue;
CreateSubclassedButton(hConsoleWnd, i);
if (! btn[i].pushable)
EnableWindow(btn[i].hBtn, btn[i].state);
}
/* This isn't needed anymore, now that we have the big printer icon -- it acts like a button now
* i = IDC_TEAR;
* btn[i].hBtn = CreateWindow("BUTTON", btn[i].txt, WS_CHILD|WS_VISIBLE|BS_CENTER,
* btn[i].x, btn[i].y, btn[i].wx, btn[i].wy, hConsoleWnd, (HMENU) i, hInstance, NULL);
*
* SendMessage(btn[i].hBtn, WM_SETFONT, (WPARAM) hTinyFont, TRUE);
*/
hbm1442_full = LoadBitmap(hInstance, "FULL_1442");
hbm1442_empty = LoadBitmap(hInstance, "EMPTY_1442");
hbm1442_eof = LoadBitmap(hInstance, "EOF_1442");
hbm1442_middle = LoadBitmap(hInstance, "MIDDLE_1442");
hbm1132_full = LoadBitmap(hInstance, "FULL_1132");
hbm1132_empty = LoadBitmap(hInstance, "EMPTY_1132");
i = IDC_1442;
btn[i].hBtn = CreateWindow("STATIC", btn[i].txt, WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_SUNKEN|WS_BORDER|SS_REALSIZEIMAGE|SS_NOTIFY,
btn[i].x, btn[i].y, btn[i].wx, btn[i].wy, hConsoleWnd, (HMENU) i, hInstance, NULL);
btn[i].state = STATE_1442_EMPTY;
wx = SendMessage(btn[i].hBtn, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hbm1442_empty);
i = IDC_1132;
btn[i].hBtn = CreateWindow("STATIC", btn[i].txt, WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_SUNKEN|WS_BORDER|SS_REALSIZEIMAGE|SS_NOTIFY,
btn[i].x, btn[i].y, btn[i].wx, btn[i].wy, hConsoleWnd, (HMENU) i, hInstance, NULL);
btn[i].state = FALSE;
wx = SendMessage(btn[i].hBtn, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hbm1132_empty);
GetWindowRect(hConsoleWnd, &r); /* get window size as created */
wx = r.right - r.left + 1;
wy = r.bottom - r.top + 1;
if (hCDC == NULL) { /* get a memory DC and select the bitmap into ti */
hDC = GetDC(hConsoleWnd);
hCDC = CreateCompatibleDC(hDC);
SelectObject(hCDC, hBitmap);
ReleaseDC(hConsoleWnd, hDC);
}
GetClientRect(hConsoleWnd, &r);
wx = (wx - r.right - 1) + bmwid; /* compute new desired size based on how client area came out */
wy = (wy - r.bottom - 1) + bmht;
MoveWindow(hConsoleWnd, 0, 0, wx, wy, FALSE); /* resize window */
ShowWindow(hConsoleWnd, SW_SHOWNOACTIVATE); /* display it */
UpdateWindow(hConsoleWnd);
if (hActWnd != NULL) { /* bring console (sim) window back to top */
GetWindowRect(hConsoleWnd, &r);
ShowWindow(hActWnd, SW_NORMAL); /* and move it just below the display window */
SetWindowPos(hActWnd, HWND_TOP, 0, r.bottom, 0, 0, SWP_NOSIZE);
GetWindowRect(hActWnd, &ra);
if (ra.bottom >= GetSystemMetrics(SM_CYSCREEN)) { /* resize if it goes of bottom of screen */
ra.bottom = GetSystemMetrics(SM_CYSCREEN) - 1;
SetWindowPos(hActWnd, 0, 0, 0, ra.right-ra.left+1, ra.bottom-ra.top+1, SWP_NOZORDER|SWP_NOMOVE);
}
}
if (running) /* if simulator is already running, start update timer */
gui_run(TRUE);
while (GetMessage(&msg, hConsoleWnd, 0, 0)) { /* message pump - this basically loops forevermore */
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (hConsoleWnd != NULL) {
DragAcceptFiles(hConsoleWnd, FALSE); /* unregister as drag/drop target */
DestroyWindow(hConsoleWnd); /* but if a quit message got posted, clean up */
hConsoleWnd = NULL;
}
PumpID = 0;
return 0;
}
/* ------------------------------------------------------------------------
* DrawBits - starting at position (x,y), draw lamps for nbits bits of word 'bits', looking only at masked bits
* ------------------------------------------------------------------------ */
static void DrawBits (HDC hDC, int x, int y, int bits, int nbits, int mask, char *syms)
{
int i, b = 0x0001 << (nbits-1);
for (i = 0; i < nbits; i++, b >>= 1) {
if (mask & b) { /* select white or black lettering then write 2 chars */
SetTextColor(hDC, (b & bits && power) ? RGB(255,255,255) : RGB(0,0,0));
TextOut(hDC, x, y, syms, 2);
}
syms += 2; /* go to next symbol pair */
if (i < 10)
x += 15; /* step between lamps */
else
x += 19;
if (x < 500) {
if (b & 0x1110)
x += 10; /* step over nibble divisions on left side */
else if (b & 0x0001)
x += 9;
}
}
}
/* ------------------------------------------------------------------------
* DrawToggles - display the console sense switches
* ------------------------------------------------------------------------ */
static void DrawToggles (HDC hDC, int bits)
{
int b, x;
for (b = 0x8000, x = TOGGLES_X; b != 0; b >>= 1) {
if (shown_ces & b) { /* up */
SelectObject(hDC, hbWhite);
Rectangle(hDC, x, 232, x+9, 240);
SelectObject(hDC, hbGray);
Rectangle(hDC, x, 239, x+9, 255);
}
else { /* down */
SelectObject(hDC, hbWhite);
Rectangle(hDC, x, 263, x+9, 271);
SelectObject(hDC, hbGray);
Rectangle(hDC, x, 248, x+9, 264);
}
x += (b & 0x1111) ? 31 : 21;
}
}
/* ------------------------------------------------------------------------
* DrawRunmode - draw the run mode rotary switch's little tip
* ------------------------------------------------------------------------ */
void DrawRunmode (HDC hDC, int mode)
{
double angle = (mode*45. + 90.) * 3.1415926 / 180.; /* convert mode position to angle in radians */
double ca, sa; /* sine and cosine */
int x0, y0, x1, y1;
HPEN hOldPen;
ca = cos(angle);
sa = sin(angle);
x0 = RUNSWITCH_X + (int) (20.*ca + 0.5); /* inner radius */
y0 = RUNSWITCH_Y - (int) (20.*sa + 0.5);
x1 = RUNSWITCH_X + (int) (25.*ca + 0.5); /* outer radius */
y1 = RUNSWITCH_Y - (int) (25.*sa + 0.5);