-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinput.c
123 lines (97 loc) · 2.47 KB
/
input.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
#include <conio.h>
#include <graph.h>
#include "binary.h"
#include "screen.h"
void
erase_pointer(int shift)
{
#define INSTR_X 330
#define INSTR_Y 140
#define SIZE 32
_setcolor(0); /* black */
_rectangle(_GFILLINTERIOR, INSTR_X + (7-shift)*SIZE, INSTR_Y,
INSTR_X + (7-shift)*SIZE + SIZE, INSTR_Y + 2);
}
void
show_pointer(int shift)
{
_setcolor(15); /* br white */
_rectangle(_GFILLINTERIOR, INSTR_X + (7-shift)*SIZE, INSTR_Y,
INSTR_X + (7-shift)*SIZE + SIZE, INSTR_Y + 2);
}
int
edit_instruction(int instr)
{
int key;
int value, shift = 7;
/* read an instruction value from the user, by simulating switches
and lights */
draw_status(STATUS_EDT); /* edit mode */
value = instr;
show_pointer(shift);
do {
key = getch();
switch (key) {
case 0: /* extended key .. call getch again */
switch ( getch() ) {
case 75: /* left .. increment shift */
if (shift < 7) {
erase_pointer(shift);
show_pointer(++shift);
}
break;
case 77: /* right .. decrement shift */
if (shift > 0) {
erase_pointer(shift);
show_pointer(--shift);
}
}
break;
case 32: /* space .. flip the bit value */
value = value ^ (1<<shift);
draw_instr(value);
}
} while (key != 13); /* 13 = Enter */
erase_pointer(shift);
return value;
}
int
input_program(int *mem, int start)
{
int counter;
int key;
/* allow the user to step through the program memory, and select
a program instruction to edit */
draw_status(STATUS_INP); /* input mode */
counter = start;
draw_count(counter);
draw_instr(mem[counter]);
/* loop: select the instruction to edit */
do {
key = getch();
switch (key) {
case 0:
/* extended key .. call getch again */
switch ( getch() ) {
case 72: /* up .. decrement counter */
if (counter > 0) {
draw_count(--counter);
draw_instr(mem[counter]);
}
break;
case 80: /* down .. increment counter */
if (counter < 255) {
draw_count(++counter);
draw_instr(mem[counter]);
}
}
break;
case 13: /* enter .. edit this instruction */
mem[counter] = edit_instruction(mem[counter]);
draw_status(STATUS_INP); /* input mode */
}
} while ((key != 'r') && (key != 'R')
&& (key != 'q') && (key != 'Q'));
/* done when the user presses R (run) or Q (quit) */
return key;
}