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

#if getclk
uint64_t clk[THREADS];	/* Per Thread Clock cycles. */
uint64_t tclk;		/* Total Clock cycles. */
#endif

const uint16_t tv = 0xFF50; /* Starting address of the Thread Vectors. */

#if !IO
uint64_t inst[THREADS];
#endif

#if bench
uint64_t inss;
#endif

#if debug
uint8_t subdbg;
#endif

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t main_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t main_cond = PTHREAD_COND_INITIALIZER;

uint8_t threads_done = 0;
uint8_t step = 0;
uint8_t esc = 0;

uint8_t *addr;

uint8_t kbd_rdy;

WINDOW *scr;

struct suxthr {
	struct sux sx;
	uint8_t th;
};

#if bench
double ipc;
struct timeval str[THREADS], en[THREADS];
#endif

inline uint8_t get_addrsize(uint8_t prefix, uint8_t addrmode) {
	uint8_t id = (prefix & 0x0C) >> 2;
	switch (addrmode) {
		case ZM:
			switch (id) {
				case 2: return 5;
				case 3: return 3;
				case 1: return 2;
				case 0: return 0;
			}
			break;
		case ABS:
			switch (id) {
				case 3: return 7;
				case 2: return 6;
				case 1: return 4;
				case 0: return 1;
			}
			break;
	}
	return 0;
}

void *run(void *args) {
	struct suxthr *thr = (void *)args;
	struct sux *cpu = &thr->sx;
	uint8_t thread = thr->th;
	uint8_t prefix = 0;
	uint8_t opcode = 0;
	union reg address;
	union reg value;
	#if getclk
	uint64_t iclk = 0;
	#endif
	#if !IO
	uint64_t ins = 0;
	#endif
#if !bench
	uint8_t lines = (6*thread)+2;
#endif
#if debug && !bench
	if (!subdbg) {
		addr[STEP_ADDR] = 1;
		step = 1;
	}
	#if keypoll
	pthread_mutex_lock(&mutex);
	#endif
	werase(scr);
	#if keypoll
	pthread_mutex_unlock(&mutex);
	#endif
#endif
	uint64_t tmpaddr = 0;
#if bench
	gettimeofday(&str[thread], 0);
#endif
	for (;;) {
		address.u64 = 0;
		value.u64 = 0;
		#if debug && !bench
		if (lines > 24*(thread+1)) {
			lines = (24*thread)+2;
		}
		#if keypoll
		pthread_mutex_lock(&mutex);
		#endif
		for (uint8_t i = (24*thread)+2; i <= 24*(thread+1); i++) {
			wmove(scr, i, 0);
			waddch(scr, (i == lines) ? '>' : ' ');
		}
		wmove(scr, lines, 1);
		wclrtoeol(scr);
		wprintw(scr, "pc: $%04"PRIX64  , cpu->pc);
		wprintw(scr, ", a: $%016"PRIX64, cpu->a);
		wprintw(scr, ", b: $%016"PRIX64, cpu->b);
		wprintw(scr, ", x: $%016"PRIX64, cpu->x);
		wprintw(scr, ", y: $%016"PRIX64, cpu->y);
		wprintw(scr, ", sp: $%04X", cpu->sp);
		wprintw(scr, ", ps: $%02X", cpu->ps.u8[thread]);
		wprintw(scr, ", inst: ");
		#if keypoll
		pthread_mutex_unlock(&mutex);
		#endif
		#endif
		prefix = addr[cpu->pc];
		if ((prefix & 0x03) != 0x03) {
			prefix = 0;
		}
		cpu->pc += ((prefix & 0x03) == 0x03);
		opcode = addr[cpu->pc];
		address.u64 = cpu->pc;
		++cpu->pc;
		#if getclk
		++iclk;
		#endif
		if (optype[opcode] != IMPL) {
			address.u64 = get_addr(cpu, &tmpaddr, opcode, prefix, thread);

			if (address.u64 > mem_size-1) {
				addr[STEP_ADDR] = 1;
				step = 1;
			}
			setreg_sw(value.u8, 0, addr, address.u64, prefix, 0, RS);
			if (optype[opcode] == REL) {
				switch ((prefix >> 4) & 3) {
					default: address.u64 = cpu->pc + (int8_t )value.u8[0] ; break;
					case 1 : address.u64 = cpu->pc + (int16_t)value.u16[0]; break;
					case 2 : address.u64 = cpu->pc + (int32_t)value.u32[0]; break;
					case 3 : address.u64 = cpu->pc + (int64_t)value.u64   ; break;
				}
			}
			#if getclk
			++iclk;
			#endif
		}
	#if debug && !bench
		#if keypoll
		pthread_mutex_lock(&mutex);
		#endif
		uint64_t operands[3];
		operands[0] = value.u64;
		operands[1] = address.u64;
		operands[2] = tmpaddr;
		disasm(cpu, operands, lines, opcode, prefix, thread);
		lines+=1;
		#if keypoll
		pthread_mutex_unlock(&mutex);
		#endif
	#endif
		uint8_t size = (1 << ((prefix >> 4) & 3))-1;
		switch(opcode) {
			case CPS_IMP: /* Clear Processor Status. */
				cpu->ps.u64 = 0;
				break;
			case ADC_B: /* ADC B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case ADC_IMM:  /* ADC Immediate. */
			case ADC_AB: /* ADC Absolute. */
			case ADC_Z: /* ADC Zero Matrix. */
				adc(cpu, value.u64, thread);
				break;
			case PHP_IMP: push(cpu, cpu->ps.u8[thread],    0, thread); break; /* PusH Processor status to stack. */
			case PHA_IMP: push(cpu, cpu->a            , size, thread); break; /* PusH Accumulator to stack. */
			case PHB_IMP: push(cpu, cpu->b            , size, thread); break; /* PusH B register to stack. */
			case PHY_IMP: push(cpu, cpu->y            , size, thread); break; /* PusH Y register to stack. */
			case PHX_IMP: push(cpu, cpu->x            , size, thread); break; /* PusH X register to stack. */
			case TAY_IMP: /* Transfer Accumulator to Y. */
			case TAX_IMP: /* Transfer Accumulator to Y. */
			case TYX_IMP: /* Transfer Y to X. */
			case TYA_IMP: /* Transfer Y to Accumulator. */
			case TXA_IMP: /* Transfer X to Accumulator. */
			case TXY_IMP: /* Transfer X to Y. */
			case TAB_IMP: /* Transfer Accumulator to B. */
			case TSX_IMP: /* Transfer Stack pointer to X. */
			case TBA_IMP: /* Transfer B to Accumulator. */
			case TXS_IMM: /* Transfer X to Stack pointer. */
				transfer(cpu, value.u64, opcode, prefix, thread);
				break;
			case BRA_REL: /* BRA Relative. */
			case JMP_AB: /* JMP Absolute. */
			case JMP_Z: /* JMP Zero Matrix. */
			case JMP_IN: /* JMP Indirect. */
				cpu->pc = address.u64;
				break;
			case SBC_B: /* SBC B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case SBC_IMM: /* SBC Immediate. */
			case SBC_AB: /* SBC Absolute. */
			case SBC_Z: /* SBC Zero Matrix. */
				sbc(cpu, value.u64, thread);
				break;
			case PLP_IMP: cpu->ps.u8[thread] = pull(cpu,    0, thread); break; /* PuLl Processor status from stack. */
			case PLA_IMP: cpu->a             = pull(cpu, size, thread); break; /* PuLl Accumulator from stack. */
			case PLB_IMP: cpu->b             = pull(cpu, size, thread); break; /* PuLl B register from stack. */
			case PLY_IMP: cpu->y             = pull(cpu, size, thread); break; /* PuLl Y register from stack. */
			case PLX_IMP: cpu->x             = pull(cpu, size, thread); break; /* PuLl X register from stack. */
				break;
			case AND_B: /* AND B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case AND_IMM: /* AND Immediate. */
			case AND_AB: /* AND Absolute. */
			case AND_Z: /* AND Zero Matrix. */
				and(cpu, value.u64, thread);
				break;
			case BPO_REL: /* BPO Relative. */
				if (!getflag(N)) {
					cpu->pc = address.u64;
				}
				break;
			case ORA_B: /* ORA B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case ORA_IMM: /* ORA Immediate. */
			case ORA_AB: /* ORA Absolute. */
			case ORA_Z: /* ORA Zero Matrix. */
				or(cpu, value.u64, thread);
				break;
			case SEI_IMP: /* SEt Interrupt. */
				setflag(1, I);
				break;
			case BNG_REL: /* BNG Relative. */
				if (getflag(N)) {
					cpu->pc = address.u64;
				}
				break;
			case XOR_B: /* XOR B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case XOR_IMM: /* XOR Immediate. */
			case XOR_AB: /* XOR Absolute. */
			case XOR_Z: /* XOR Zero Matrix. */
				xor(cpu, value.u64, thread);
				break;
			case CLI_IMP: /* CLear Interrupt. */
				setflag(0, I);
				break;
			case BCS_REL: /* BCS Relative. */
				if (getflag(C)) {
					cpu->pc = address.u64;
				}
				break;
			case LSL_B: /* LSL B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case LSL_IMM: /* LSL Immediate. */
			case LSL_AB: /* LSL Absolute. */
			case LSL_Z: /* LSL Zero Matrix. */
				lsl(cpu, value.u64, thread);
				break;
			case SEC_IMP: /* SEt Carry flag.*/
				setflag(1, C);
				break;
			case STA_AB: /* STA Absolute. */
			case STA_Z: /* STA Zero Matrix. */
			case STA_ZX: /* STA Zero Matrix, Indexed with X. */
			case STA_ZY: /* STA Zero Matrix, Indexed with Y. */
			case STA_IN: /* STA Indirect. */
			case STA_IX: /* STA Indexed Indirect. */
			case STA_IY: /* STA Indirect Indexed. */
				store(cpu, address.u64, cpu->a, prefix, thread);
				break;
			case STY_AB: /* STY Absolute. */
			case STY_Z: /* STY Zero Matrix. */
			case STY_IN: /* STY Indirect. */
				store(cpu, address.u64, cpu->y, prefix, thread);
				break;
			case STX_AB: /* STX Absolute. */
			case STX_Z: /* STX Zero Matrix. */
			case STX_IN: /* STX Indirect. */
				store(cpu, address.u64, cpu->x, prefix, thread);
				break;
			case STB_AB: /* STB Absolute. */
			case STB_Z: /* STB Zero Matrix. */
			case STB_ZX: /* STB Zero Matrix, Indexed with X. */
			case STB_ZY: /* STB Zero Matrix, Indexed with Y. */
			case STB_IN: /* STB Indirect. */
			case STB_IX: /* STB Indexed Indirect. */
			case STB_IY: /* STB Indirect Indexed. */
				store(cpu, address.u64, cpu->b, prefix, thread);
				break;
			case BCC_REL: /* BCC Relative. */
				if (!getflag(C)) {
					cpu->pc = address.u64;
				}
				break;
			case LSR_B: /* LSR B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case LSR_IMM: /* LSR Immediate. */
			case LSR_AB: /* LSR Absolute. */
			case LSR_Z: /* LSR Zero Matrix. */
				lsr(cpu, value.u64, thread);
				break;
			case ASR_B: /* ASR B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case ASR_IMM: /* ASR Immediate. */
			case ASR_AB: /* ASR Absolute. */
			case ASR_Z: /* ASR Zero Matrix. */
				asr(cpu, value.u64, thread);
				break;
			case CLC_IMP: /* CLear Carry flag. */
				setflag(0, C);
				break;
			case LDB_IMM: /* LDB Immediate. */
			case LDB_AB: /* LDB Absolute. */
			case LDB_Z: /* LDB Zero Matrix. */
			case LDB_ZX: /* LDB Zero Matrix, Indexed with X. */
			case LDB_ZY: /* LDB Zero Matrix, Indexed with Y. */
			case LDB_IN: /* LDB Indirect. */
			case LDB_IX: /* LDB Indexed Indirect. */
			case LDB_IY: /* LDB Indirect Indexed. */
				cpu->b = load(cpu, address.u64, cpu->b, prefix, thread);
				break;
			case LDA_IMM: /* LDA Immediate. */
			case LDA_AB: /* LDA Absolute. */
			case LDA_Z: /* LDA Zero Matrix. */
			case LDA_ZX: /* LDA Zero Matrix, Indexed with X. */
			case LDA_ZY: /* LDA Zero Matrix, Indexed with Y. */
			case LDA_IN: /* LDA Indirect. */
			case LDA_IX: /* LDA Indexed Indirect. */
			case LDA_IY: /* LDA Indirect Indexed. */
				cpu->a = load(cpu, address.u64, cpu->a, prefix, thread);
				break;
			case LDY_IMM: /* LDY Immediate. */
			case LDY_AB: /* LDY Absolute. */
			case LDY_Z: /* LDY Zero Matrix. */
			case LDY_IN: /* LDY Indirect. */
				cpu->y = load(cpu, address.u64, cpu->y, prefix, thread);
				break;
			case LDX_IMM: /* LDX Immediate. */
			case LDX_AB: /* LDX Absolute. */
			case LDX_Z: /* LDX Zero Matrix. */
			case LDX_IN: /* LDX Indirect. */
				cpu->x = load(cpu, address.u64, cpu->x, prefix, thread);
				break;
			case BEQ_REL: /* BEQ Relative. */
				if (getflag(Z)) {
					cpu->pc = address.u64;
				}
				break;
			case ROL_B: /* ROL B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case ROL_IMM: /* ROL Immediate. */
			case ROL_AB: /* ROL Absolute. */
			case ROL_Z: /* ROL Zero Matrix. */
				rol(cpu, value.u64, thread);
				break;
			case BNE_REL: /* BNE Relative. */
				if (!getflag(Z)) {
					cpu->pc = address.u64;
				}
				break;
			case ROR_B: /* ROR B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case ROR_IMM: /* ROR Immediate. */
			case ROR_AB: /* ROR Absolute. */
			case ROR_Z: /* ROR Zero Matrix. */
				ror(cpu, value.u64, thread);
				break;
			case BVS_REL: /* BVS Relative. */
				if (getflag(V)) {
					cpu->pc = address.u64;
				}
				break;
			case MUL_B: /* MUL B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case MUL_IMM: /* MUL Immediate. */
			case MUL_AB: /* MUL Absolute. */
			case MUL_Z: /* MUL Zero Matrix. */
				mul(cpu, value.u64, thread);
				break;
			case BVC_REL: /* BVC Relative. */
				if (!getflag(V)) {
					cpu->pc = address.u64;
				}
				break;
			case DIV_IMM: /* DIV Immediate. */
			case DIV_B: /* DIV B register. */
			case DIV_AB: /* DIV Absolute. */
			case DIV_Z: /* DIV Zero Matrix. */
				divd(cpu, value.u64, opcode, thread);
				break;
			case CLV_IMP: /* CLear oVerflow flag. */
				setflag(0, V);
				break;
			case CPB_IMM: /* CPB Immediate. */
			case CPB_AB: /* CPB Absolute. */
			case CPB_Z: /* CPB Zero Matrix. */
			case CPB_IN: /* CPB Indirect. */
			case CPB_IX: /* CPB Indexed Indirect. */
			case CPB_IY: /* CPB Indirect Indexed. */
				cmp(cpu, value.u64, cpu->b, thread);
				break;
			case CMP_B: /* CMP B register. */
				value.u64 = cpu->b; /* Falls Through. */
			case CMP_IMM: /* CMP Immediate. */
			case CMP_AB: /* CMP Absolute. */
			case CMP_Z: /* CMP Zero Matrix. */
			case CMP_IN: /* CMP Indirect. */
			case CMP_IX: /* CMP Indexed Indirect. */
			case CMP_IY: /* CMP Indirect Indexed. */
				cmp(cpu, value.u64, cpu->a, thread);
				break;
			case CPY_IMM: /* CPY Immediate. */
			case CPY_AB: /* CPY Absolute. */
			case CPY_Z: /* CPY Zero Matrix. */
				cmp(cpu, value.u64, cpu->y, thread);
				break;
			case CPX_IMM: /* CPX Immediate. */
			case CPX_AB: /* CPX Absolute. */
			case CPX_Z: /* CPX Zero Matrix. */
				cmp(cpu, value.u64, cpu->x, thread);
				break;
			case INC_IMP: cpu->a = idr(cpu, cpu->a, 1, thread); break;
			case INB_IMP: cpu->b = idr(cpu, cpu->b, 1, thread); break;
			case INY_IMP: cpu->y = idr(cpu, cpu->y, 1, thread); break;
			case INX_IMP: cpu->x = idr(cpu, cpu->x, 1, thread); break;
			case DEC_IMP: cpu->a = idr(cpu, cpu->a, 0, thread); break;
			case DEB_IMP: cpu->b = idr(cpu, cpu->b, 0, thread); break;
			case DEY_IMP: cpu->y = idr(cpu, cpu->y, 0, thread); break;
			case DEX_IMP: cpu->x = idr(cpu, cpu->x, 0, thread); break;
			case JSR_IN: /* JSR Indirect. */
			case JSR_AB: /* Jump to SubRoutine. */
			case JSR_Z: /* JSR Zero Matrix. */
				push(cpu, cpu->pc, 7, thread);
				cpu->pc = address.u64;
				break;
			case INC_AB: /* INC Absolute. */
			case INC_Z: /* INC Zero Matrix. */
				idm(cpu, address.u64, prefix, 1, thread);
				break;
			case NOP_IMP: /* No OPeration. */
				break;
			case RTI_IMP: /* ReTurn from Interrupt routine. */
				cpu->ps.u64 = pull(cpu, 0, thread);
			case RTS_IMP: /* ReTurn from Subroutine. */
				cpu->pc = pull(cpu, 7, thread);
				break;
			case DEC_AB: /* DEC Absolute. */
			case DEC_Z: /* DEC Zero Matrix. */
				idm(cpu, address.u64, prefix, 0, thread);
				break;
			case BRK_IMP: /* BReaK. */
			case WAI_IMP: /* WAit for Interrupt. */
				if (opcode == WAI_IMP) {
					pthread_mutex_lock(&main_mutex);
					pthread_cond_signal(&main_cond);
					pthread_mutex_unlock(&main_mutex);
					pthread_mutex_lock(&mutex);
					pthread_cond_wait(&cond, &mutex);
					pthread_mutex_unlock(&mutex);
				}
				push(cpu, cpu->pc, 7, thread);
				push(cpu, cpu->ps.u8[thread], 0, thread);
				setflag(1, I);
				setreg(value.u8, +, 0, addr, +, (opcode == BRK) ? 0xFFE0 : 0xFFA0, 7);
				if (opcode == WAI_IMP) {
					kbd_rdy &= (uint8_t)~(1 << thread);
				}
				cpu->pc = value.u64;
			default:
				break;
		}
		#if !IO
		ins++;
		#endif
		#if !bench
		if (step) {
			pthread_mutex_lock(&main_mutex);
			pthread_cond_signal(&main_cond);
			pthread_mutex_unlock(&main_mutex);
			pthread_mutex_lock(&mutex);
			pthread_cond_wait(&cond, &mutex);
			pthread_mutex_unlock(&mutex);
			#if debug
			wrefresh(scr);
			#endif
		}
		#endif
		#if debug && !bench
		#if keypoll
		pthread_mutex_lock(&mutex);
		#endif
		wmove(scr, (6*thread)+1, 0);
		wprintw(scr, "Instructions executed: %"PRIu64, ins);
		#if getclk
		wprintw(scr, ", Clock cycles: %"PRIu64, iclk);
		#endif
		if (step && !subdbg) {
			wrefresh(scr);
		}
		#if keypoll
		pthread_mutex_unlock(&mutex);
		#endif
		#elif bench
		if (ins >= BENCH_INST) {
			pthread_mutex_lock(&main_mutex);
			threads_done++;
			inst[thread] = ins;
			#if getclk
			clk[thread] = iclk;
			#endif
			pthread_cond_signal(&main_cond);
			pthread_mutex_unlock(&main_mutex);
			gettimeofday(&en[thread], 0);
			break;
		}
		#endif
	}
	return NULL;
}

void init_scr() {
	if (!scr) {
		scr = initscr();
	}
	nodelay(scr, 0);
	wtimeout(scr, 8);
	crmode();
	noecho();
	nl();
	curs_set(1);
	scrollok(scr, 1);
	start_color();
	use_default_colors();
	init_pair(1, COLOR_WHITE, -1);
	attron(COLOR_PAIR(1) | A_BOLD);
}

int main(int argc, char **argv) {
	struct suxthr thr[THREADS];
	char *tmp = malloc(2048);
	addr = malloc(mem_size);
	memset(key, 0, sizeof(key));
	#if bench
	inss = 0;
	#endif
	int v = 0;

	if (argc != 2) {
		if (asmmon("stdin") == 2) {
			return 0;
		}
	} else {
		#if debug
		subdbg = !strcmp(argv[1], "programs/sub-suite/subsuite.s");
		#endif
		if (asmmon(argv[1]) == 2) {
			return 0;
		}
	}
	sprintf(tmp, "\033[2J\033[H");
	fwrite(tmp, sizeof(char), strlen(tmp), stdout);
	fflush(stdout);
	init_scr();
	werase(scr);
	wmove(scr, 0, 0);
	wrefresh(scr);
	pthread_t therads[THREADS];
	int result;
	uint16_t vec = 0xFFC0;
	uint8_t offset;
	for (int i = 0; i < THREADS; i++) {
		thr[i].sx.sp = 0xFFFF;
		thr[i].sx.stk_st = i+1;
		offset = (i) ? ((i-1) << 3) : 0;
		vec = (i) ? 0xFF50 : 0xFFC0;
		thr[i].sx.a = 0;
		thr[i].sx.b = 0;
		thr[i].sx.x = 0;
		thr[i].sx.y = 0;
		thr[i].sx.pc = (uint64_t)addr[vec+0+offset]
				| (uint64_t)addr[vec+1+offset] << 8
				| (uint64_t)addr[vec+2+offset] << 16
				| (uint64_t)addr[vec+3+offset] << 24
				| (uint64_t)addr[vec+4+offset] << 32
				| (uint64_t)addr[vec+5+offset] << 40
				| (uint64_t)addr[vec+6+offset] << 48
				| (uint64_t)addr[vec+7+offset] << 56;

		thr[i].th = i;
		#if !IO
		inst[i] = 0;
		#endif
		result = pthread_create(&therads[i], NULL, run, &thr[i]);
		assert(!result);
	}
	int c = 0;
	uint8_t step_key = 0;
	uint8_t end = 0;
	werase(scr);
	while (threads_done < THREADS && !end) {
		#if !bench
		int x, y;
		if ((step_key && step && !kbd_rdy) || !step || kbd_rdy) {
			if ((c != EOF && c !=-1)) {
				#if !keypoll
				pthread_mutex_lock(&mutex);
				pthread_cond_signal(&cond);
				pthread_mutex_unlock(&mutex);
				#endif
				pthread_mutex_lock(&main_mutex);
				curs_set(0);
				pthread_cond_wait(&main_cond, &main_mutex);
				pthread_mutex_unlock(&main_mutex);
				curs_set(1);
				c = 0;
				step_key = 0;
				addr[CTRL_ADDR] = 0;
				wrefresh(scr);
			}
		}
		#if keypoll
		pthread_mutex_lock(&mutex);
		#endif
		c = get_key(scr);
		if (c == 19) {
			if (kbd_rdy) {

				c = get_key(scr);
			}
			step = 1;
		} else if (c == 0x11) {
			end = 1;
			continue;
		}
		if (step) {
			if (c != 19 && c != 18 && c != 0x11 && !isalnum(c)) {
				/*WINDOW *w;
				int maxcol = getmaxx(scr)/2;
				int maxrow = getmaxy(scr)/2;*/
				int keycode = get_keycode(key);
				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
				wmove(scr, getmaxy(scr)-1, 0);
				wclrtoeol(scr);
				wprintw(scr, "c: %i", c);
				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[RX_ADDR] = (uint8_t)c;
						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[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);
				step_key = (c == 19);
			}
		}
		addr[STEP_ADDR] = step;
		#if keypoll
		pthread_mutex_unlock(&mutex);
		#endif
		#else
		pthread_mutex_lock(&main_mutex);
		pthread_cond_wait(&main_cond, &main_mutex);
		pthread_mutex_unlock(&main_mutex);
		#endif
	}
	endwin();
#if bench
	if (threads_done == THREADS) {
		double tm_sec, tm_usec, tm[THREADS], ttm;
		#if getclk
		double clkspd;
		double mhz;
		#endif
		double ips[THREADS];
		double ipst;
		for (int i = 0; i < THREADS; i++) {
			tm_sec = (en[i].tv_sec - str[i].tv_sec);
			tm_usec = (en[i].tv_usec-str[i].tv_usec);
			tm[i] = (tm_sec*1000000)+(tm_usec);
			ips[i] = inst[i]/tm[i];
			if (i) {
				inss += inst[i];
				ttm += tm[i];
				ipst += ips[i];
				#if getclk
				tclk += clk[i];
				#endif
			} else {
				inss = inst[i];
				ttm = tm[i];
				ipst = ips[i];
				#if getclk
				tclk = clk[i];
				#endif
			}
			#if getclk
			clkspd = (tm[i]/1000000)*1000000/clk[i];
			mhz = 1000000.0/clkspd/1000000;
			#endif
			sprintf(tmp, "Instructions executed for thread %i: %"PRIu64", Instructions per Second for thread %i in MIPS: %f, tm: %f\n", i, inst[i], i, ips[i], tm[i]/1000000);
			fwrite(tmp, sizeof(char), strlen(tmp), stdout);
		}
		sprintf(tmp, "Total Instructions executed: %"PRIu64", Total Instructions per Second in MIPS: %f", inss, ipst);
		fwrite(tmp, sizeof(char), strlen(tmp), stdout);
		#if getclk
		clkspd = (ttm/1000000)*1000000/tclk;
		mhz = 1000000.0/clkspd/1000000;
		sprintf(tmp, ", Clock cycles: %"PRIu64", Clock Speed in MHz: %f", tclk, mhz);
		fwrite(tmp, sizeof(char), strlen(tmp), stdout);
		#endif
		sprintf(tmp, ", tm: %f\n", ttm/1000000);
		fwrite(tmp, sizeof(char), strlen(tmp), stdout);
		fflush(stdout);
		free(tmp);
	}
#endif
	free(addr);
	return 0;
}