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

uint8_t iscol;
uint8_t idx = 3;
uint8_t bcd[4];

void io(uint64_t address, uint8_t rw) {
	int x, y;
	uint16_t scr_col = 0;
	/*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;
			pthread_mutex_lock(&main_mutex);
			pthread_cond_signal(&main_cond);
			pthread_mutex_unlock(&main_mutex);
			#if !keypoll
			pthread_mutex_lock(&mutex);
			pthread_cond_wait(&cond, &mutex);
			pthread_mutex_unlock(&mutex);
			#endif
			kbd_rdy = 0;
			break;
		case TX_ADDR:
			if (rw) {
				break;
			}
			getyx(scr, y, x);
			#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
			if (esc) {
				#if debug
				if (!subdbg && addr[RX_ADDR] == '\n') {
					wclrtoeol(scr);
				}
				#endif
				switch (addr[TX_ADDR]) {
					case 'A':
						if (y > 0)
							y--;
						#if !debug
						wmove(scr, y, x);
						#endif
						esc = 0;
						break;
					case 'B':
						if (y < getmaxy(scr))
							y++;
						#if !debug
						wmove(scr, y, x);
						#endif
						esc = 0;
						break;
					case 'C':
						if (x < getmaxx(scr))
							x++;
						#if !debug
						wmove(scr, y, x);
						#endif
						esc = 0;
						break;
					case 'D':
						if (x > 0)
							x--;
						#if !debug
						wmove(scr, y, x);
						#endif
						esc = 0;
						break;
					case 'H':
						if (!bcd[2] && !bcd[3]) {
							y = 0;
						} else {
							y = ((bcd[3]*10) + bcd[2]);
						}
						if (!bcd[0] && !bcd[1]) {
							x = 0;
						} else {
							x = ((bcd[1]*10) + bcd[0]);
						}
						#if !debug
						wmove(scr, y, x);
						#else
						/*mvwprintw(scr, 31, 0, "x: %i, y: %i  ", x, y);*/
						mvwprintw(scr, 31, 0, "bcd[3-2]: {%u, %u}, bcd[1-0]: {%u, %u}", bcd[3], bcd[2], bcd[1], bcd[0]);
						#endif
						idx = 3;
						bcd[0] = 0;
						bcd[1] = 0;
						bcd[2] = 0;
						bcd[3] = 0;
						esc = 0;
						break;
					case 'S':
						#if !debug
						wscrl(scr, -1);
						#else
						#endif
						esc = 0;
						break;
					case 'T':
						#if !debug
						wscrl(scr, 1);
						#else
						#endif
						esc = 0;
						break;
					case '0':
					case '1':
					case '2':
					case '3':
					case '4':
					case '5':
					case '6':
					case '7':
					case '8':
					case '9':
						bcd[idx--] = (addr[address] - '0');
						break;
					default:
						iscol = (addr[address] == ';');
						break;
				}
			} else {
				switch (addr[TX_ADDR]) {
					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': 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, addr[address]);
						#else
						if (!subdbg && scr_col < 160) {
							if (addr[address] != ' ') {
								wprintw(scr, "%02X", addr[address]);
							} else {
								wprintw(scr, "  ");
							}
						}
						#endif
						x+=1;
						break;
				}
			}
			break;
	}
}