aboutsummaryrefslogtreecommitdiff
path: root/tsalis/src/printer.c
blob: 88da4a8c7509fa6bdbbe6871c86963d7f6593290 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#include <stdarg.h>
#include <string.h>
#include <curses.h>
#include <salis.h>
#include "printer.h"
#include "tsalis.h"

enum {
	PAIR_NORMAL          =  1,
	PAIR_HEADER          =  2,
	PAIR_SELECTED_PROC   =  3,
	PAIR_FREE_CELL       =  4,
	PAIR_ALLOC_CELL      =  5,
	PAIR_MEM_BLOCK_START =  6,
	PAIR_MEM_BLOCK_2     =  7,
	PAIR_MEM_BLOCK_1     =  8,
	PAIR_SELECTED_SP     =  9,
	PAIR_SELECTED_IP     = 10
};

enum {
	PAGE_MEMORY,
	PAGE_EVOLVER,
	PAGE_PROCESS,
	PAGE_WORLD,
	PAGE_COUNT
};

const char *g_instNames[] = {
#define SINST(name, symbol) #name,
#define SILST(name, symbol) #name
	SINST_LIST
#undef SINST
#undef SILST
};

const char g_instSymbols[] = {
#define SINST(name, symbol) symbol,
#define SILST(name, symbol) symbol
	SINST_LIST
#undef SINST
#undef SILST
};

const char *g_procElems[] = {
	"mb1a",
	"mb1s",
	"mb2a",
	"mb2s",
	"ip",
	"sp",
	"reg[0]",
	"reg[1]",
	"reg[2]",
	"reg[3]",
	"stack[0]",
	"stack[1]",
	"stack[2]",
	"stack[3]",
	"stack[4]",
	"stack[5]",
	"stack[6]",
	"stack[7]"
};

const int PROC_ELEMENT_COUNT = (sizeof(g_procElems) / sizeof(*g_procElems));
const int DATA_WIDTH         = 25;

int   g_currentPage;
sword g_selectedProcess;
sbool g_processShowGenes;
sword g_processVertScroll;
sword g_processDataScroll;
sword g_processGeneScroll;
sword g_worldPos;
sword g_worldZoom;
sword g_worldLineWidth;
sword g_worldLineCoverage;
sword g_worldArea;

static void
adjustWorld(void)
{
	g_worldLineWidth    = COLS - DATA_WIDTH;
	g_worldLineCoverage = g_worldLineWidth * g_worldZoom;
	g_worldArea         = LINES * g_worldLineCoverage;

	while ((g_worldArea > (sm_getSize() * 2)) && (g_worldZoom > 1)) {
		g_worldZoom /= 2;
	}
}

sbool
tsp_check(void)
{
	initscr();

	if (!has_colors()) {
		endwin();
		return SFALSE;
	}

	return STRUE;
}

void
tsp_init(void)
{
	cbreak();
	noecho();
	curs_set(0);
	keypad(stdscr, TRUE);
	start_color();

	if (can_change_color()) {
		init_color(COLOR_BLACK,     0,   0,   0);
		init_color(COLOR_RED,     700,   0,   0);
		init_color(COLOR_GREEN,     0, 700,   0);
		init_color(COLOR_YELLOW,  700, 700,   0);
		init_color(COLOR_BLUE,      0,   0, 700);
		init_color(COLOR_MAGENTA, 700,   0, 700);
		init_color(COLOR_CYAN,      0, 700, 700);
		init_color(COLOR_WHITE,   700, 700, 700);
	}

	init_pair(PAIR_NORMAL,          COLOR_WHITE,  COLOR_BLACK);
	init_pair(PAIR_HEADER,          COLOR_CYAN,   COLOR_BLACK);
	init_pair(PAIR_SELECTED_PROC,   COLOR_YELLOW, COLOR_BLACK);
	init_pair(PAIR_FREE_CELL,       COLOR_CYAN,   COLOR_BLUE);
	init_pair(PAIR_ALLOC_CELL,      COLOR_BLUE,   COLOR_CYAN);
	init_pair(PAIR_MEM_BLOCK_START, COLOR_BLUE,   COLOR_WHITE);
	init_pair(PAIR_MEM_BLOCK_1,     COLOR_BLACK,  COLOR_YELLOW);
	init_pair(PAIR_MEM_BLOCK_2,     COLOR_BLACK,  COLOR_GREEN);
	init_pair(PAIR_SELECTED_SP,     COLOR_BLACK,  COLOR_MAGENTA);
	init_pair(PAIR_SELECTED_IP,     COLOR_BLACK,  COLOR_RED);
	g_worldZoom = 1;
	adjustWorld();
}

void
tsp_quit(void)
{
	endwin();
}

void
tsp_onResize(void)
{
	clear();
	adjustWorld();
}

void
tsp_prevPage(void)
{
	clear();
	g_currentPage += (PAGE_COUNT - 1);
	g_currentPage %= PAGE_COUNT;
}

void
tsp_nextPage(void)
{
	clear();
	g_currentPage++;
	g_currentPage %= PAGE_COUNT;
}

void
tsp_scrollUp(void)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
		if (g_processVertScroll) {
			g_processVertScroll--;
		}

		break;

	case PAGE_WORLD:
		if (g_worldPos >= g_worldLineCoverage) {
			g_worldPos -= g_worldLineCoverage;
		} else {
			g_worldPos = 0;
		}

		break;
	}

	refresh();
}

void
tsp_scrollDown(void)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
		if (g_processVertScroll < (sp_getCap() - 1)) {
			g_processVertScroll++;
		}

		break;

	case PAGE_WORLD:
		if ((g_worldPos + g_worldLineCoverage) < sm_getSize()) {
			g_worldPos += g_worldLineCoverage;
		}

		break;
	}

	refresh();
}

void
tsp_scrollLeft(void)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
		if (g_processShowGenes) {
			if (g_processGeneScroll) {
				g_processGeneScroll--;
			}
		} else {
			if (g_processDataScroll) {
				g_processDataScroll--;
			}
		}

		break;

	case PAGE_WORLD:
		if (g_worldPos >= g_worldZoom) {
			g_worldPos -= g_worldZoom;
		} else {
			g_worldPos = 0;
		}

		break;
	}

	refresh();
}

void
tsp_scrollRight(void)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
		if (g_processShowGenes) {
			g_processGeneScroll++;
		} else {
			if (g_processDataScroll < (sword)(PROC_ELEMENT_COUNT - 1)) {
				g_processDataScroll++;
			}
		}

		break;

	case PAGE_WORLD:
		if ((g_worldPos + g_worldZoom) < sm_getSize()) {
			g_worldPos += g_worldZoom;
		}

		break;
	}

	refresh();
}

void
tsp_scrollToTop(void)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
		g_processVertScroll = 0;
		break;

	case PAGE_WORLD:
		g_worldPos = 0;
		break;
	}
}

void
tsp_scrollToLeft(void)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
		if (g_processShowGenes) {
			g_processGeneScroll = 0;
		} else {
			g_processDataScroll = 0;
		}

		break;
	}
}

void
tsp_zoomIn(void)
{
	if (g_currentPage == PAGE_WORLD) {
		if (g_worldZoom > 1) {
			g_worldZoom /= 2;
		}

		adjustWorld();
	}
}

void
tsp_zoomOut(void)
{
	if (g_currentPage == PAGE_WORLD) {
		if (g_worldArea < sm_getSize()) {
			g_worldZoom *= 2;
			refresh();
		}

		adjustWorld();
	}
}

void
tsp_prevOrganism(void)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
	case PAGE_WORLD:
		if (g_selectedProcess < sp_getCap()) {
			g_selectedProcess += (sp_getCap() - 1);
			g_selectedProcess %= sp_getCap();
		}

		break;
	}

	refresh();
}

void
tsp_nextOrganism(void)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
	case PAGE_WORLD:
		if (g_selectedProcess < sp_getCap()) {
			g_selectedProcess++;
			g_selectedProcess %= sp_getCap();
		}

		break;
	}

	refresh();
}

void
tsp_gotoSelectedProc(void)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
		g_processVertScroll = g_selectedProcess;
		break;

	case PAGE_WORLD:
		g_worldPos = sp_getProc(g_selectedProcess).mb1a;
		break;
	}
}

void
tsp_selectProcess(sword proc)
{
	if (proc < sp_getCap()) {
		g_selectedProcess = proc;
	}
}

void
tsp_moveTo(sword loc)
{
	switch (g_currentPage) {
	case PAGE_PROCESS:
		if (loc < sp_getCap()) {
			g_processVertScroll = loc;
		}

		break;

	case PAGE_WORLD:
		if (loc < sm_getSize()) {
			g_worldPos = loc;
		}

		break;
	}
}

static void
printWidget(int line, const char *format, ...)
{
	if (line < LINES) {
		va_list args;
		char    dataLine[24];
		va_start(args, format);
		vsprintf(dataLine, format, args);
		mvprintw(line, 1, "%.*s", COLS - 1, dataLine);
		va_end(args);
	}
}

static void
printHeader(int line, const char *string)
{
	attron(COLOR_PAIR(PAIR_HEADER));
	printWidget(line, string);
	attron(COLOR_PAIR(PAIR_NORMAL));
}

#define PHEADER(label)       printHeader((*line)++, label)
#define PWIDGET(label, data) printWidget((*line)++, "%-10s : %10u", label, data)
#define PSIDGET(label, data) printWidget((*line)++, "%-10s : %10s", label, data)
#define INCREMENT_LINE       (*line)++

static void
printMemoryPage(int *line)
{
	PHEADER("MEMORY");
	PWIDGET("order",  sm_getOrder());
	PWIDGET("size",   sm_getSize());
	PWIDGET("blocks", sm_getMemBlockCount());
	PWIDGET("alloc",  sm_getAllocated());
	PWIDGET("cap",    sm_getCap());
}

static void
printEvolverPage(int *line)
{
	PHEADER("EVOLVER");
	PWIDGET("lastAddr", se_getLastAddress());
	PSIDGET("lastInst", g_instNames[se_getLastInst()] + 1);
	PWIDGET("state0",   se_getState(0));
	PWIDGET("state1",   se_getState(1));
	PWIDGET("state2",   se_getState(2));
	PWIDGET("state3",   se_getState(3));
}

static void
printField(int y, int x, const char *field, sbool lalign)
{
	if ((y < LINES) && (x < COLS)) {
		if (lalign) {
			mvprintw(y, x, "%-.*s", COLS - x, field);
		} else {
			mvprintw(y, x, "%.*s", COLS - x, field);
		}
	}
}

static void
printSingleProcessGenome(int line, sword pidx)
{
	char  sidx[13];
	SProc proc = sp_getProc(pidx);
	sword gidx = g_processGeneScroll;
	int   xpos = 14;

	if (pidx == g_selectedProcess) {
		attron(COLOR_PAIR(PAIR_SELECTED_PROC));
	} else if (!sp_isFree(pidx)) {
		attron(COLOR_PAIR(PAIR_HEADER));
	}

	sprintf(sidx, "%-10u |", pidx);
	printField(line, 1, sidx, STRUE);
	move(line, xpos);

	while ((gidx < proc.mb1s) && (xpos < COLS)) {
		sword gaddr = proc.mb1a + gidx;

		if (gaddr == proc.ip) {
			attron(COLOR_PAIR(PAIR_SELECTED_IP));
		} else if (gaddr == proc.sp) {
			attron(COLOR_PAIR(PAIR_SELECTED_SP));
		} else {
			attron(COLOR_PAIR(PAIR_MEM_BLOCK_1));
		}

		addch(g_instSymbols[sm_getInstAt(gaddr)]);
		gidx++;
		xpos++;
	}

	if (proc.mb1s < g_processGeneScroll) {
		gidx = g_processGeneScroll - proc.mb1s;
	} else {
		gidx = 0;
	}

	while ((gidx < proc.mb2s) && (xpos < COLS)) {
		sword gaddr = proc.mb2a + gidx;

		if (gaddr == proc.ip) {
			attron(COLOR_PAIR(PAIR_SELECTED_IP));
		} else if (gaddr == proc.sp) {
			attron(COLOR_PAIR(PAIR_SELECTED_SP));
		} else {
			attron(COLOR_PAIR(PAIR_MEM_BLOCK_2));
		}

		addch(g_instSymbols[sm_getInstAt(gaddr)]);
		gidx++;
		xpos++;
	}

	attron(COLOR_PAIR(PAIR_NORMAL));
}

static void
printProcessGenes(int *line)
{
	sword pidx = g_processVertScroll;
	attron(COLOR_PAIR(PAIR_HEADER));
	printField(*line, 1, "pidx", STRUE);

	attron(COLOR_PAIR(PAIR_NORMAL));
	INCREMENT_LINE;

	while ((*line < LINES) && (pidx < sp_getCap())) {
		printSingleProcessGenome(*line, pidx);
		INCREMENT_LINE;
		pidx++;
	}

	attron(COLOR_PAIR(PAIR_NORMAL));
}

static void
printSingleProcessData(int line, sword pidx)
{
	char   sidx[11];
	int    eidx = g_processDataScroll;
	int    xpos = 12;
	SProc  proc = sp_getProc(pidx);
	sword *data = (sword *)&proc;

	if (pidx == g_selectedProcess) {
		attron(COLOR_PAIR(PAIR_SELECTED_PROC));
	} else if (!sp_isFree(pidx)) {
		attron(COLOR_PAIR(PAIR_HEADER));
	}

	sprintf(sidx, "%u", pidx);
	printField(line, 1, sidx, STRUE);

	while (eidx < PROC_ELEMENT_COUNT) {
		char element[13];
		sprintf(element, "| %10u", data[eidx]);
		printField(line, xpos, element, SFALSE);
		eidx++;
		xpos += 13;
	}

	attron(COLOR_PAIR(PAIR_NORMAL));
}

static void
printProcessData(int *line)
{
	sword pidx = g_processVertScroll;
	int   eidx = g_processDataScroll;
	int   xpos = 12;
	attron(COLOR_PAIR(PAIR_HEADER));
	printField(*line, 1, "pidx", STRUE);

	while (eidx < PROC_ELEMENT_COUNT) {
		char element[13];
		sprintf(element, "| %10s", g_procElems[eidx]);
		printField(*line, xpos, element, SFALSE);
		eidx++;
		xpos += 13;
	}

	attron(COLOR_PAIR(PAIR_NORMAL));
	INCREMENT_LINE;

	while ((*line < LINES) && (pidx < sp_getCap())) {
		printSingleProcessData(*line, pidx);
		INCREMENT_LINE;
		pidx++;
	}

	attron(COLOR_PAIR(PAIR_NORMAL));
}

static void
printProcessPage(int *line)
{
	int   cline;
	sbool fnull = (sp_getFirst() == (sword) - 1);
	sbool lnull = (sp_getLast()  == (sword) - 1);
	PHEADER("PROCESS");
	PWIDGET("count", sp_getCount());
	PWIDGET("cap",   sp_getCap());
	fnull ? PSIDGET("first", "---") : PWIDGET("first", sp_getFirst());
	lnull ? PSIDGET("last",  "---") : PWIDGET("last",  sp_getLast());
	PWIDGET("selected", g_selectedProcess);
	INCREMENT_LINE;

	for (cline = *line; cline < LINES; cline++) {
		move(cline, 0);
		clrtoeol();
	}

	if (g_processShowGenes) {
		printProcessGenes(line);
	} else {
		printProcessData(line);
	}
}

static int
getColorOf(sword addr)
{
	if (!sp_isFree(g_selectedProcess)) {
		SProc proc = sp_getProc(g_selectedProcess);

		if (addr == proc.ip) {
			return PAIR_SELECTED_IP;
		} else if (addr == proc.sp) {
			return PAIR_SELECTED_SP;
		} else if ((addr >= proc.mb1a) && (addr < (proc.mb1a + proc.mb1s))) {
			return PAIR_MEM_BLOCK_1;
		} else if ((addr >= proc.mb2a) && (addr < (proc.mb2a + proc.mb2s))) {
			return PAIR_MEM_BLOCK_2;
		}
	}

	if (sm_isMemBlockStartAt(addr)) {
		return PAIR_MEM_BLOCK_START;
	} else if (sm_isAllocatedAt(addr)) {
		return PAIR_ALLOC_CELL;
	} else {
		return PAIR_FREE_CELL;
	}
}

static void
printWorld(void)
{
	sword y;

	for (y = 0; y < (sword)LINES; y++) {
		sword x;

		for (x = 0; x < g_worldLineWidth; x++) {
			sword addr  = g_worldPos + (((y * g_worldLineWidth) + x) * g_worldZoom);
			sbool atEnd = !sm_isValidAt(addr);
			int   xpos  = DATA_WIDTH + x;

			if (atEnd) {
				mvaddch(y, xpos, ' ');
				continue;
			}

			if (g_worldZoom == 1) {
				char symbol = g_instSymbols[sm_getInstAt(addr)];
				attron(COLOR_PAIR(getColorOf(addr)));
				mvaddch(y, xpos, symbol);
			} else {
				sword offset;
				char  symbol;
				sword instSum = 0;
				int   color   = PAIR_FREE_CELL;

				for (offset = 0; offset < g_worldZoom; offset++) {
					int   testColor;
					sword offsetAddr = addr + offset;

					if (!sm_isValidAt(offsetAddr)) {
						break;
					}

					instSum  += sm_getInstAt(offsetAddr);
					testColor = getColorOf(offsetAddr);

					if (testColor > color) {
						color = testColor;
					}
				}

				instSum /= g_worldZoom;

				if (!instSum) {
					symbol = ' ';
				} else if (instSum < 32) {
					symbol = '-';
				} else {
					symbol = '=';
				}

				attron(COLOR_PAIR(color));
				mvaddch(y, xpos, symbol);
			}

			attron(COLOR_PAIR(PAIR_NORMAL));
		}
	}
}

static void
printWorldPage(int *line)
{
	int    eidx;
	SProc  proc = sp_getProc(g_selectedProcess);
	sword *data = (sword *)&proc;
	PHEADER("WORLD");
	PWIDGET("pos",      g_worldPos);
	PWIDGET("zoom",     g_worldZoom);
	PWIDGET("selected", g_selectedProcess);
	INCREMENT_LINE;
	PHEADER("SELECTED");

	if (!sp_isFree(g_selectedProcess)) {
		attron(COLOR_PAIR(PAIR_SELECTED_PROC));
	}

	for (eidx = 0; eidx < PROC_ELEMENT_COUNT; eidx++) {
		PWIDGET(g_procElems[eidx], data[eidx]);
	}

	attron(COLOR_PAIR(PAIR_NORMAL));
	printWorld();
}

void
tsp_printData(void)
{
	int  linev = 1;
	int *line  = &linev;
	PHEADER("SALIS");

	if (strlen(g_simName) < 24) {
		PHEADER(g_simName);
	} else {
		attron(COLOR_PAIR(PAIR_HEADER));
		printWidget((*line)++, "%.20s...", g_simName);
		attron(COLOR_PAIR(PAIR_NORMAL));
	}

	PSIDGET("state", g_running ? "running" : "paused");

	if (g_autoSaveInterval) {
		PWIDGET("autosave", g_autoSaveInterval);
	} else {
		PSIDGET("autosave", "---");
	}

	PWIDGET("cycle", s_getCycle());
	PWIDGET("epoch", s_getEpoch());
	INCREMENT_LINE;

	switch (g_currentPage) {
	case PAGE_MEMORY:
		printMemoryPage(line);
		break;

	case PAGE_EVOLVER:
		printEvolverPage(line);
		break;

	case PAGE_PROCESS:
		printProcessPage(line);
		break;

	case PAGE_WORLD:
		printWorldPage(line);
		break;
	}

	refresh();
}