summaryrefslogtreecommitdiff
path: root/io.c
blob: cef5eb6df6b1d2e9e76d6f9fd3f4ab5c5e1a6577 (plain)
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
#include "sux.h"

#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
#define DEFAULT(a, b) (a) = (a) ? (a) : (b)

char *key = NULL;
uint8_t key_idx = 0;

uint8_t esc = 0;
uint8_t iscsi = 0;

typedef struct escape escape;

struct escape {
	char buf[128];		/* Buffer. */
	size_t len;		/* Buffer length. */
	int arg[16];		/* Arguments. */
	int narg;		/* Number of arguments. */
	char mode[2];		/* Mode type. */
};

escape escseq;

static inline char *get_keyseq(int keycode) {
	switch (keycode) {
		case KEY_BACKSPACE: return       "\b";
		case KEY_UP       : return   "\x1B[A";
		case KEY_DOWN     : return   "\x1B[B";
		case KEY_RIGHT    : return   "\x1B[C";
		case KEY_LEFT     : return   "\x1B[D";
		case KEY_F( 1)    : return   "\x1BOP";
		case KEY_F( 2)    : return   "\x1BOQ";
		case KEY_F( 3)    : return   "\x1BOR";
		case KEY_F( 4)    : return   "\x1BOS";
		case KEY_F( 5)    : return "\x1B[15~";
		case KEY_F( 6)    : return "\x1B[17~";
		case KEY_F( 7)    : return "\x1B[18~";
		case KEY_F( 8)    : return "\x1B[19~";
		case KEY_F( 9)    : return "\x1B[20~";
		case KEY_F(10)    : return "\x1B[21~";
		case KEY_F(11)    : return "\x1B[23~";
		case KEY_F(12)    : return "\x1B[24~";
		default           : return       NULL;
	}
}

int get_key(WINDOW *scr) {
	int x, y;
	int c;
	int keycode = 0;
	curs_set(1);
	if ((key == NULL) || (key && key[key_idx] == '\0') || !kbd_rdy) {
		c = wgetch(scr);
		if (c == 19) {
			if (kbd_rdy) {
				c = wgetch(scr);
			}
			step = 1;
		} else if (c == 0x11) {
			end = 1;
		}
		if (kbd_rdy) {
			key_idx = 0;
			key = get_keyseq(c);
		}
		keycode = c;
	}
	if (kbd_rdy) {
		c = (key != NULL) ? key[key_idx++] : c;
	}

	if (step) {
		if (keycode != 19 && keycode != 18 && keycode != 0x11 && !isalnum(keycode)) {
			/*WINDOW *w;
			int maxcol = getmaxx(scr)/2;
			int maxrow = getmaxy(scr)/2;*/
			switch (keycode) {
				case KEY_F(1):
					/*w = newwin(maxrow, maxcol, maxrow, maxcol);
					emumon(w);
					delwin(w);*/
					endwin();
					puts("Starting asmmon()");
					asmmon("stdin");
					puts("Reinitializing screen.");
					init_scr();
					wrefresh(scr);
					break;
			}
			#if debug && !bench
			getyx(scr, y, x);
			wmove(scr, getmaxy(scr)-1, 0);
			wclrtoeol(scr);
			wprintw(scr, "keycode: %i", keycode);
			wmove(scr, y, x);
			#endif
		}
	}

	if (kbd_rdy) {
		switch (c) {
			case ERR:
				addr[CTRL_ADDR] = 0;
				break;
			case '\0': break;
			default:
				if (kbd_rdy && c < 0x100) {
					addr[CTRL_ADDR] = 1;
					#if debug && !bench
					wmove(scr, getmaxy(scr)-1, 0);
					wclrtoeol(scr);
					wprintw(scr, "c: %i ", c);
					wprintw(scr, "key: ");
					for (int i = 0; key && key[i] != '\0'; i++) {
						wprintw(scr, "$%02X%s", key[i], (key[i+1] != '\0') ? ", " : "");
					}
					wmove(scr, y, x);
					#endif
				}
				break;
		}
	} else {
		if (step) {
			step = !(c == 18);
		}
	}
	addr[STEP_ADDR] = step;
	curs_set(0);
	return c;
}

void parse_esc() {
	char *str = escseq.buf, *nstr;
	long int value;
	escseq.narg = 0;

	escseq.buf[escseq.len] = '\0';
	for (;str < escseq.buf + escseq.len; str++) {
		nstr = NULL;
		value = strtol(str, &nstr, 10);
		if (nstr == str) {
			value = 0;
		}
		if (value == LONG_MAX || value == LONG_MIN) {
			value = -1;
		}
		escseq.arg[escseq.narg++] = value;
		str = nstr;
		if (*str != ';' || escseq.narg == 16) {
			break;
		}
	}
	escseq.mode[0] = *str++;
	escseq.mode[1] = (str < (escseq.buf + escseq.len)) ? *str : '\0';
}

void handle_esc() {
	int x, y;
	getyx(scr, y, x);
	switch (escseq.mode[0]) {
		case 'A':
			DEFAULT(escseq.arg[0], 1);
			if (y-escseq.arg[0] >= 0) {
				y -= escseq.arg[0];
				#if !debug
				wmove(scr, y, x);
				#endif
			}
			break;
		case 'B':
		case 'e':
			DEFAULT(escseq.arg[0], 1);
			if (y+escseq.arg[0] <= getmaxy(scr)) {
				y += escseq.arg[0];
				#if !debug
				wmove(scr, y, x);
				#endif
			}
			break;
		case 'C':
		case 'a':
			DEFAULT(escseq.arg[0], 1);
			if (x+escseq.arg[0] <= getmaxx(scr)) {
				x += escseq.arg[0];
				#if !debug
				wmove(scr, y, x);
				#endif
			}
			break;
		case 'D':
			DEFAULT(escseq.arg[0], 1);
			if (x-escseq.arg[0] >= 0) {
				x -= escseq.arg[0];
				#if !debug
				wmove(scr, y, x);
				#endif
			}
			break;
		case 'H':
		case 'f':
			DEFAULT(escseq.arg[0], 1);
			DEFAULT(escseq.arg[1], 1);
			#if !debug
			wmove(scr, escseq.arg[0]-1, escseq.arg[1]-1);
			#else
			wmove(scr, 31, 0);
			wclrtoeol(scr);
			wprintw(scr, "escseq.arg[0]: %i, escseq.arg[1]: %i", escseq.arg[0], escseq.arg[1]);
			#endif
			break;
		case 'S':
			DEFAULT(escseq.arg[0], 1);
			#if !debug
			wscrl(scr, -escseq.arg[0]);
			#else
			#endif
			break;
		case 'T':
			DEFAULT(escseq.arg[0], 1);
			#if !debug
			wscrl(scr, escseq.arg[0]);
			#else
			#endif
			break;
	}
}

void reset_esc() {
	memset(&escseq, 0, sizeof(escseq));
}

void handle_ctrlcode(int c) {
	int x, y;
	uint16_t scr_col = 0;
	#if debug
	if (!subdbg) {
		scr_col = (addr[TX_ADDR] != 0x0C && addr[TX_ADDR] != '\n' && scr_col < 160) ? (addr[1] << 1)-2 : 0;
		wmove(scr, 28, scr_col);
	}
	#endif
	getyx(scr, y, x);
	switch (c) {
		case 0xC:
			x=0,y=0;
			#if !debug
			wclear(scr);
			wmove(scr, y, x);
			#endif
			break;
		case CURSES_BACKSPACE:
		case '\b':
			if (x > 0) {
				x--;
				#if !debug
				wmove(scr, y, x);
				#endif
			}
			#if !debug
			wdelch(scr);
			#else
			if (!subdbg) {
				scr_col++;
				wmove(scr, 28, scr_col--);
				wdelch(scr);
				wmove(scr, 28, scr_col);
				wdelch(scr);
			}
			#endif
			break;
		case '\033': reset_esc(); esc = 1; break;
		case '\n':
			x = 0;
			y+=1;
			#if !debug
			wmove(scr, y, x);
			#endif
			break;
		default:
			#if !debug
			/*wmove(scr, y, x);*/
			waddch(scr, c);
			#else
			if (!subdbg && scr_col < 160) {
				if (c != ' ') {
					wprintw(scr, "%02X", c);
				} else {
					wprintw(scr, "  ");
				}
			}
			#endif
			x+=1;
			break;
	}
}


void io(uint64_t address, uint8_t rw) {
	/*step |= (!rw) ? (cpu->pc[thread] == CTRL_ADDR) : step;*/
	switch (address) {
		case STEP_ADDR: step = (!rw) ? addr[STEP_ADDR] : step; break;
		case CTRL_ADDR:
			if (!rw) {
				break;
			}
			kbd_rdy = 1;
			wrefresh(scr);
			addr[RX_ADDR] = get_key(scr);
			kbd_rdy = 0;
			break;
		case TX_ADDR:
			if (rw) {
				break;
			}
			if (esc) {
				#if debug
				if (!subdbg && addr[RX_ADDR] == '\n') {
					wclrtoeol(scr);
				}
				#endif
				int c = addr[TX_ADDR];
				if (iscsi) {
					escseq.buf[escseq.len++] = c;
					if (BETWEEN(c, 0x40, 0x7E) || escseq.len >= sizeof(escseq.buf)-1) {
						esc = 0;
						iscsi = 0;
						parse_esc();
						handle_esc();
					}
				}
				if (c == '[') {
					iscsi = 1;
				}
			} else {
				handle_ctrlcode(addr[TX_ADDR]);
			}
			//wrefresh(scr);
			break;
	}
}