-
Notifications
You must be signed in to change notification settings - Fork 0
/
kbase-main.c
240 lines (205 loc) · 4.82 KB
/
kbase-main.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
#include <apple2enh.h>
#include <conio.h>
#include <stdlib.h>
#include <tuilibap.h>
#define STATE_DEFAULT 0
#define STATE_INPUT 1
#define STATE_TERMINATE 2
void cursorOn(void);
void newline(char address);
void blankLine(void);
void displayMemory(char address, int value);
void displayPc(char value);
void displayAccumulator(int value);
int getInput(char address);
int shift(char left, char right, int accumulator);
void displayOutput(int value);
char cardiacState = STATE_DEFAULT;
void main(void) {
int col80prev = videomode(VIDEOMODE_80x24);
char inc = 0, cIndex;
int memory[100], programCounter = 0, accumulator = 0;
tgui_Window *tgw3;
clrscr();
tgw3=newwindow(0,0,79,23,COLOR_BLACK, 1,"A2 CARDIAC");
for (cIndex = 0; cIndex < 100; cIndex++) {
if (cIndex == 0) {
memory[cIndex] = 001;
} else {
memory[cIndex] = 000;
}
}
// clrscr();
// cputsxy(35, 0, "A2 CARDIAC\r\n");
// chlinexy(0, 1, 80);
// chlinexy(0, 22, 80);
// cvlinexy(50, 23, 1);
for (cIndex = 0; cIndex < 100; cIndex++) {
displayMemory(cIndex, memory[cIndex]);
}
displayPc(programCounter);
displayAccumulator(accumulator);
blankLine();
while (1) {
if (kbhit()) {
inc = cgetc();
if (inc == 'Q' || inc == 'q') {
break;
}
};
if (cardiacState == STATE_DEFAULT) {
int instruction = memory[programCounter];
char op = instruction/100;
char arg = instruction - (op * 100);
int value, left, right;
displayPc(programCounter++);
switch (op) {
case 0: // input
value = getInput(arg);
memory[arg] = value;
displayMemory(arg, value);
break;
case 1: // clear and add
accumulator = memory[arg];
displayAccumulator(accumulator);
break;
case 2: // add
accumulator += memory[arg];
displayAccumulator(accumulator);
break;
case 3: // test accumulator
if (accumulator < 0) {
programCounter = arg;
}
break;
case 4: // shift
left = arg / 10;
right = arg - (left * 10);
accumulator = shift(left, right, accumulator);
displayAccumulator(accumulator);
break;
case 5: // output
displayOutput(memory[arg]);
break;
case 6: // store accumulator
memory[arg] = accumulator;
displayMemory(arg, accumulator);
break;
case 7: // subtract
accumulator -= memory[arg];
displayAccumulator(accumulator);
break;
case 8: // jump
programCounter = arg;
break;
case 9: // halt and reset
programCounter = arg;
break;
}
}
if (cardiacState == STATE_TERMINATE) {
break;
}
}
if (col80prev == VIDEOMODE_40x24) {
clrscr();
videomode(VIDEOMODE_40x24);
}
}
void displayMemory(char address, int value) {
int y = (address % 20) + 2, x = ((int)(address/20) * 10) + 2;
gotoxy(x, y);
cprintf("%02d:%03d", address, abs(value));
if (value < 0) {
cputc('-');
} else {
cputc(' ');
}
}
void displayPc(char value) {
gotoxy(58, 5);
cputs("PROGRAM COUNTER");
chlinexy(56, 6, 19);
chlinexy(56, 10, 19);
cvlinexy(55, 7, 3);
cvlinexy(75, 7, 3);
gotoxy(64, 8);
cprintf("%02d", value);
}
void displayOutput(int value) {
gotoxy(64, 23);
if (value < 0) {
cputc('-');
} else {
cputc(' ');
}
cprintf("%03d", abs(value));
}
void displayAccumulator(int value) {
gotoxy(60, 13);
cputs("ACCUMULATOR");
chlinexy(56, 14, 19);
chlinexy(56, 18, 19);
cvlinexy(55, 15, 3);
cvlinexy(75, 15, 3);
gotoxy(63, 16);
if (value < 0) {
cputc('-');
} else {
cputc(' ');
}
cprintf("%03d", abs(value));
}
void cursorOn(void) {
cputc('_');
gotox(wherex() - 1);
}
void newline(char address) {
gotoxy(0, 23);
cclear(40);
gotoxy(0, 23);
cprintf("%02d:", address);
cursorOn();
}
void blankLine(void) {
gotoxy(0, 23);
cclear(40);
gotoxy(0, 23);
}
int getInput(char address) {
int result = 0;
char num[4], numIndex = 0;
num[3]= '\0';
newline(address);
while (1) {
if (kbhit()) {
char c = cgetc();
if (c == 'Q' || c == 'q') {
cardiacState = STATE_TERMINATE;
return 0;
} else if (c >= '0' && c <= '9' && numIndex < 3) {
num[numIndex++] = c;
cputc(c);
} else if (c == '\r' || c == '\n') {
result = atoi(num);
break;
}
}
}
blankLine();
return result;
}
int shift(char left, char right, int accumulator) {
char index = left;
int result = accumulator;
for (index = 0; index < left; index++) {
result *= 10;
}
if (result > 999) {
result %= 1000;
}
for (index = 0; index < right; index++) {
result /= 10;
}
return result;
}