summaryrefslogtreecommitdiffstats
path: root/tis-curses.c
blob: 1b7488e3689e429575975a990f90cb25ce38aa70 (plain) (blame)
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
#define NCURSES_WIDECHAR 1

#include "tpu.h"
#include "util.h"

#include <curses.h>

#include <libgen.h>
#include <locale.h>
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define KEY_ESC 0x1b
#define KEY_CTRL(c) ((c) & ~0x60)

#define TCELL_INPUT_ROWS 14
#define TCELL_INPUT_COLS 20
#define TCELL_INFO_W 6
#define TCELL_INFO_H 4
#define TCELL_CNT 6
#define TCELL_W (TCELL_INPUT_COLS + 2 + 6)
#define TCELL_H (TCELL_INPUT_ROWS + 2)

enum {
	COLOR_HEADING,
	COLOR_VAL
};

struct tpu_cell {
	struct tpu tpu;
	enum tpu_status status;
	size_t x, y;
	const char source[TCELL_INPUT_ROWS][TCELL_INPUT_COLS + 1];
	bool enabled;
	char *warn;
	float idle;
};

static size_t tpu_cell_rows = 2;
static size_t tpu_cell_cols = 3;

static size_t screenw = 80;
static size_t screenh = 40;

static struct tpu_cell *tpu_cells = NULL;

const char *progname = "tis-curses";

static const char *
cell_mode_str(struct tpu_cell *cell)
{
	switch (cell->status) {
	case STATUS_READ:
		return "=R=";
	case STATUS_WRITE:
		return "=W=";
	case STATUS_IDLE:
		return "=I=";
	case STATUS_RUN:
		return "=X=";
	}
}

static const char *
cell_last_str(struct tpu_cell *cell)
{
	return "N/A";
}

static void
tui_draw_box(int sx, int sy, int w, int h, const cchar_t *ul, const cchar_t *ur,
	const cchar_t *ll, const cchar_t *lr)
{
	int x, y;

	mvadd_wch(sy, sx, ul);
	mvadd_wch(sy, sx + w - 1, ur);
	mvadd_wch(sy + h - 1, sx, ll);
	mvadd_wch(sy + h - 1, sx + w - 1, lr);
	for (x = sx + 1; x < sx + w - 1; x++)
		mvadd_wch(sy, x, WACS_D_HLINE);
	for (x = sx + 1; x < sx + w - 1; x++)
		mvadd_wch(sy + h - 1, x, WACS_D_HLINE);
	for (y = sy + 1; y < sy + h - 1; y++)
		mvadd_wch(y, sx, WACS_D_VLINE);
	for (y = sy + 1; y < sy + h - 1; y++)
		mvadd_wch(y, sx + w - 1, WACS_D_VLINE);
}

static void
__attribute__((format(printf, 4, 5)))
tui_draw_text(int x, int y, int attr, const char *fmt, ...)
{
	char buf[256];
	va_list ap;

	va_start(ap, fmt);
	vsnprintf(buf, 256, fmt, ap);
	va_end(ap);

	attron(attr);
	mvprintw(y, x, "%s", buf);
	attroff(attr);
}

static void
tui_draw_wch(int x, int y, int attr, const cchar_t *c)
{
	attron(attr);
	mvadd_wch(y, x, c);
	attroff(attr);
}

static void
tui_draw_tpu_cell(struct tpu_cell *cell)
{
	struct tpu_port *port;
	int x, y, w, h;

	tui_draw_box((int) cell->x, (int) cell->y, TCELL_W, TCELL_H,
		WACS_D_ULCORNER, WACS_D_URCORNER, WACS_D_LLCORNER, WACS_D_LRCORNER);

	x = (int) cell->x + TCELL_W - TCELL_INFO_W;
	w = TCELL_INFO_W;
	h = TCELL_INFO_H;

	tui_draw_box(x, (y = (int) cell->y), w, h,
		WACS_D_TTEE, WACS_D_URCORNER, WACS_D_LTEE, WACS_D_RTEE);
	tui_draw_text(x + 2, y + 1, A_BOLD, "ACC");
	tui_draw_text(x + 2, y + 2, 0, "%03i", cell->tpu.acc);

	tui_draw_box(x, (y += TCELL_INFO_H - 1), w, h,
		WACS_D_LTEE, WACS_D_RTEE, WACS_D_LTEE, WACS_D_RTEE);
	tui_draw_text(x + 2, y + 1, A_BOLD, "BAK");
	tui_draw_text(x + 2, y + 2, 0, "%03i", cell->tpu.bak);

	tui_draw_box(x, (y += TCELL_INFO_H - 1), w, h,
		WACS_D_LTEE, WACS_D_RTEE, WACS_D_LTEE, WACS_D_RTEE);
	tui_draw_text(x + 2, y + 1, A_BOLD, "LST");
	tui_draw_text(x + 2, y + 2, 0, "%s", cell_last_str(cell));

	tui_draw_box(x, (y += TCELL_INFO_H - 1), w, h,
		WACS_D_LTEE, WACS_D_RTEE, WACS_D_LTEE, WACS_D_RTEE);
	tui_draw_text(x + 2, y + 1, A_BOLD, "MOD");
	tui_draw_text(x + 2, y + 2, 0, "%s", cell_mode_str(cell));

	tui_draw_box(x, (y += TCELL_INFO_H - 1), w, h,
		WACS_D_LTEE, WACS_D_RTEE, WACS_D_BTEE, WACS_D_LRCORNER);
	tui_draw_text(x + 2, y + 1, A_BOLD, "IDL");
	tui_draw_text(x + 2, y + 2, 0, "%03i", (int) (cell->idle * 100));

	if (cell->tpu.ports[DIR_LEFT].attached) {
		port = &cell->tpu.ports[DIR_LEFT];
		if (port->type & PORT_IN)
			tui_draw_wch((int) cell->x - 1, (int) cell->y + 8,
				port->read ? A_BOLD : 0, WACS_RARROW);
		if (port->type & PORT_OUT)
			tui_draw_wch((int) cell->x - 1, (int) cell->y + 7,
				port->write ? A_BOLD : 0, WACS_LARROW);
		if (port->write)
			tui_draw_text((int) cell->x - 3, (int) cell->y + 6,
				A_BOLD, "%03i", port->val);
	}

	if (cell->tpu.ports[DIR_RIGHT].attached) {
		port = &cell->tpu.ports[DIR_RIGHT];
		if (port->type & PORT_IN)
			tui_draw_wch((int) cell->x + TCELL_W + 1, (int) cell->y + 7,
				port->read ? A_BOLD : 0, WACS_LARROW);
		if (port->type & PORT_OUT)
			tui_draw_wch((int) cell->x + TCELL_W + 1, (int) cell->y + 8,
				port->write ? A_BOLD : 0, WACS_RARROW);
		if (port->write)
			tui_draw_text((int) cell->x + TCELL_W + 1, (int) cell->y + 9,
				A_BOLD, "%03i", port->val);
	}

	if (cell->tpu.ports[DIR_UP].attached) {
		port = &cell->tpu.ports[DIR_UP];
		if (port->type & PORT_IN)
			tui_draw_wch((int) cell->x + 13, (int) cell->y - 1,
				port->read ? A_BOLD : 0, WACS_DARROW);
		if (port->type & PORT_OUT)
			tui_draw_wch((int) cell->x + 15, (int) cell->y - 1,
				port->write ? A_BOLD : 0, WACS_UARROW);
		if (port->write)
			tui_draw_text((int) cell->x + 16, (int) cell->y - 1,
				A_BOLD, "%03i", port->val);
	}

	if (cell->tpu.ports[DIR_DOWN].attached) {
		port = &cell->tpu.ports[DIR_DOWN];
		if (port->type & PORT_IN)
			tui_draw_wch((int) cell->x + 13, (int) cell->y + TCELL_H,
				port->write ? A_BOLD : 0, WACS_DARROW);
		if (port->type & PORT_OUT)
			tui_draw_wch((int) cell->x + 15, (int) cell->y + TCELL_H,
				port->read ? A_BOLD : 0, WACS_UARROW);
		if (port->write)
			tui_draw_text((int) cell->x + 10, (int) cell->y + TCELL_H,
				A_BOLD, "%03i", port->val);
	}
}

static void
tui_draw(void)
{
	int i;

	for (i = 0; i < TCELL_CNT; i++)
		tui_draw_tpu_cell(&tpu_cells[i]);

	refresh();
}

static void
tui_resize(void)
{
	size_t x, y, i;

	screenw = (size_t) getmaxx(stdscr);
	screenh = (size_t) getmaxy(stdscr);

	for (y = 0; y < tpu_cell_rows; y++) {
		for (x = 0; x < tpu_cell_cols; x++) {
			i = y * tpu_cell_cols + x;
			tpu_cells[i].x = 2 + x * (TCELL_W + 4);
			tpu_cells[i].y = 2 + y * (TCELL_H + 2);
		}
	}
}

int
main(int argc, char **argv)
{
	size_t x, y, i;
	bool quit;
	int key;

	setlocale(LC_ALL, "");

	initscr();

	raw();
	noecho();
	keypad(stdscr, TRUE);
	start_color();
	curs_set(0);

	/* TODO load like tis-as */

	tpu_cells = calloc(tpu_cell_rows * tpu_cell_cols, sizeof(struct tpu_cell));
	if (!tpu_cells) die("malloc:");

	for (y = 0; y < tpu_cell_rows; y++) {
		for (x = 0; x < tpu_cell_cols; x++) {
			i = y * tpu_cell_cols + x;
			tpu_cells[i].enabled = true;
			tpu_init(&tpu_cells[i].tpu);
			memset((void *) tpu_cells[i].source, 0,
				TCELL_INPUT_ROWS * (TCELL_INPUT_COLS + 1));
		}
	}

	tui_resize();
	quit = false;
	while (!quit) {
		tui_draw();
		key = getch();
		switch (key) {
		case KEY_RESIZE:
			tui_resize();
			break;
		case KEY_CTRL('c'):
			quit = true;
			break;
		}
	}

	for (i = 0; i < TCELL_CNT; i++)
		tpu_deinit(&tpu_cells[i].tpu);
	free(tpu_cells);

	endwin();
}