summaryrefslogtreecommitdiff
path: root/sux.c
blob: c202d6ce4484a2e9f7151f414a6dacd819e15cdc (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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
#include "opcode.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define C (1 << 0)
#define Z (1 << 1)
#define I (1 << 2)
#define S (1 << 3)
#define V (1 << 6)
#define N (1 << 7)

#define STK_STADDR 0x010000 /* Starting address of the stack. */
uint8_t *addr; /* Address Space. */
uint8_t update_pc = 1; /* Update the program counter. */
uint16_t sp = 0xFFFF; /* Stack pointer. */
uint64_t a[8]; /* Accumulator. */
uint64_t y[8]; /* Y index. */
uint64_t x[8]; /* X index. */
uint8_t crt; /* Current Running Threads. */
uint64_t pc[8]; /* Program counter. */
uint64_t ps; /* Processor status. */

int optype(uint8_t opcode) {
	switch (opcode) {
		/* ALU instructions. */
		case ADC:
		case SBC:
		case MUL:
		case DIV:
		case AND:
		case AAY:
		case AAX:
		case ANY:
		case ANX:
		case ORA:
		case OAY:
		case OAX:
		case ORY:
		case ORX:
		case XOR:
		case XAY:
		case XAX:
		case XRY:
		case XRX:
		case SLA:
		case SRA:
		case ROL:
		case ROR:
		case INC:
		case IAY:
		case IAX:
		case INY:
		case INX:
		case DEC:
		case DAY:
		case DAX:
		case DEY:
		case DEX:
			return ALU;
		/* Thread instructions. */
		case STT:
		case ENT:
			return THREAD;
		/* Branching instructions. */
		case JMP:
		case JSR:
		case RTS:
		case BPO:
		case BNG:
		case BEQ:
		case BNE:
		case BCS:
		case BCC:
		case BVS:
		case BVC:
		case CMP:
		case CAY:
		case CAX:
		case CPY:
		case CPX:
			return BRANCH;
		/* Processor status instructions. */
		case CPS:
		case SEI:
		case CLI:
		case SEC:
		case CLC:
		case SSP:
		case CSP:
		case SEV:
		case CLV:
			return FLAG;
		/* Memory based instructions. */
		case LDA:  /* LDA, imm */
		case LDY:  /* LDY, imm */
		case LAY:  /* LAY, imm */
		case LDX:  /* LDX, imm */
		case LAX:  /* LAX, imm */
		case 0x59: /* LDA, abs */
		case 0x52: /* LDY, abs */
		case 0x53: /* LAY, abs */
		case 0x54: /* LDX, abs */
		case 0x55: /* LAX, abs */
		case STA:
		case SAY:
		case SAX:
		case STY:
		case STX:
		case PHP:
		case PHA:
		case PAY:
		case PAX:
		case PHY:
		case PHX:
		case PLP:
		case PLA:
		case PYA:
		case PXA:
		case PLY:
		case PLX:
			return MEMORY;
		/* Misc intructions. */
		case NOP:
		case BRK:
			return MISC;
		default:
			return -1;
	}
}

uint64_t rol(uint64_t a, uint64_t value) {
	const uint64_t mask = 8 * sizeof(a) - 1;
	value &= mask;
	return (a << value) | (a >> (-value & mask));
}

uint64_t ror(uint64_t a, uint64_t value) {
	const uint64_t mask = 8 * sizeof(a) - 1;
	value &= mask;
	return (a >> value) | (a << (-value & mask));
}

/*int push(uint64_t value, uint8_t size) {
	for (uint8_t i = 0; i < size; i++) {
		addr[STK_STADDR+sp] = value >> 8*i;
		sp--;
	}
	return 1;
}*/

/*uint64_t pull(uint8_t size) {
	uint64_t value;
	for (uint8_t i = 0; i < size; i++) {
		sp++;
		if (i == 0)
			value = (addr[STK_STADDR+sp]);
		else
			value |= (addr[STK_STADDR+sp] << (8*i));
	}
	return value;
}*/



void push(uint8_t value) {
	addr[STK_STADDR+sp] = value;
	sp--;
}

uint8_t pull() {
	sp++;
	return addr[STK_STADDR+sp];
}

int alu(uint8_t opcode, uint64_t value, uint8_t thread) {
	uint64_t sum;
	uint8_t c = 0; /* Carry flag. */
	uint8_t v = 0; /* Overflow flag. */
	uint8_t z = 0; /* Zero flag. */
	uint8_t n = 0; /* Negative flag. */
	switch(opcode) {
		/* Add with carry. */
		case ADC:
			c = (ps & (1 << thread)) >> thread;
			sum = a[thread]+value+c;
			v = !((a[thread]^value) & 0x8000000000000000) && ((a[thread]^sum) & 0x8000000000000000);
			c = (sum < value);
			a[thread] = sum;
			break;
		/* Subtract with carry. */
		case SBC:
			c = !(ps & (1 << thread));
			sum = a[thread]-value-c;
			v = ((a[thread]^value) & 0x8000000000000000) && ((a[thread]^sum) & 0x8000000000000000);
			c = (sum > value);
			a[thread] = sum;
			break;
		/* Multiply with accumulator. */
		case MUL:
			c = (ps & (1 << thread)) >> thread;
			sum = a[thread]*value+c;
			v = !((a[thread]^value) & 0x8000000000000000) && ((a[thread]^sum) & 0x8000000000000000);
			c = (!((a[thread]^sum) && (a[thread]^value)) || (a[thread] >= ((uint64_t)1 << 32) && value >= ((uint64_t)1 << 32)));
			a[thread] = sum;
			break;
		/* Divide with accumulator. */
		case DIV:
			sum = a[thread]/value;
			v = ((a[thread]^value) & 0x8000000000000000) && ((a[thread]^sum) & 0x8000000000000000);
			a[thread] = sum;
			break;
		/* Bitwise AND. */
		case AND:
		case AAY:
		case AAX:
			a[thread] &= value;
			if (opcode == AAY)
				y[thread] &= value;
			if (opcode == AAX)
				x[thread] &= value;
			break;
		case ANY:
			y[thread] &= value;
			break;
		case ANX:
			x[thread] &= value;
			break;
		/* Bitwise OR. */
		case ORA:
		case OAY:
		case OAX:
			a[thread] |= value;
			if (opcode == OAY)
				y[thread] |= value;
			if (opcode == OAX)
				x[thread] |= value;
			break;
		case ORY:
			y[thread] |= value;
			break;
		case ORX:
			x[thread] |= value;
			break;
		/* Bitwise Exclusive OR. */
		case XOR:
		case XAY:
		case XAX:
			a[thread] ^= value;
			if (opcode == XAY)
				y[thread] ^= value;
			if (opcode == XAX)
				x[thread] ^= value;
			break;
		case XRY:
			y[thread] ^= value;
			break;
		case XRX:
			x[thread] ^= value;
			break;
		/* Shift accumulator left. */
		case SLA:
			sum = (value < 64) ? a[thread] << value : 0;
			c = a[thread] >> 64-value;
			a[thread] = sum;
			break;
		/* Shift accumulator right. */
		case SRA:
			sum = (value < 64) ? a[thread] >> value : 0;
			c = a[thread] & 1;
			a[thread] = sum;
			break;
		/* Rotate accumulator left. */
		case ROL:
			c = (ps & (1 << thread)) >> thread;
			sum = a[thread] << value;
			sum |= c;
			c = a[thread] >> (uint64_t)64-value;
			a[thread] = sum;
			break;
		/* Rotate accumulator right. */
		case ROR:
			c = (ps & (1 << thread)) >> thread;
			sum = a[thread] >> value;
			sum |= (uint64_t)c << (uint64_t)64-value;
			c = a[thread] & 1;
			a[thread] = sum;
			break;
		/* Increment accumulator. */
		case INC:
		case IAY:
		case IAX:
			a[thread]++;
			if (opcode == IAY)
				y[thread]++;
			if (opcode == IAX)
				x[thread]++;
			break;
		/* Increment y register. */
		case INY:
			y[thread]++;
			break;
		/* Increment x register. */
		case INX:
			x[thread]++;
			break;
		/* Decrement accumulator. */
		case DEC:
		case DAY:
		case DAX:
			a[thread]--;
			if (opcode == DAY)
				y[thread]--;
			if (opcode == DAX)
				x[thread]--;
			break;
		/* Decrement y register. */
		case DEY:
			y[thread]--;
			break;
		/* Decrement y register. */
		case DEX:
			x[thread]--;
			break;
		default:
			/*printf("Cool, you inputed a non existent opcode, which means\n"
				"that you have now wasted clock cycles.\n"
				"Good job! *clap*\n");*/
			break;
	}
	z = (a[thread] == 0);
	n = (a[thread] >> 63);
	ps = (n) ? (ps | (uint64_t)N << 8*thread) : (ps & ~((uint64_t)N << 8*thread));
	ps = (v) ? (ps | (uint64_t)V << 8*thread) : (ps & ~((uint64_t)V << 8*thread));
	ps = (z) ? (ps | (uint64_t)Z << 8*thread) : (ps & ~((uint64_t)Z << 8*thread));
	ps = (c) ? (ps | (uint64_t)C << 8*thread) : (ps & ~((uint64_t)C << 8*thread));
	return 1;
}

int threads(uint8_t opcode, uint64_t value) {
	uint8_t t = value & 0xFF; /* This tells the CPU, which threads to start, or end. */
	switch(opcode) {
		/* Start Thread. */
		case STT:
			crt |= t;
			for (uint8_t i = 0; i < 7; i++)
				if ((t >> i) & 1)
					pc[i] = value >> 8;
			break;
		/* End Thread. */
		case ENT:
			crt &= ~t;
			for (uint8_t i = 0; i < 7; i++)
				if ((t >> i) & 1)
					pc[i] = 0;
			break;
	}
	return 1;
}

int branch(uint8_t opcode, uint64_t value, uint8_t thread) {
	uint64_t dif;
	switch (opcode) {
		/* Jump. */
		case JMP:
			pc[thread] = value;
			break;
		/* Jump to subroutine. */
		case JSR:
			/*push(pc[thread], 8);*/
			pc[thread] = value;
			break;
		/* Return from subroutine. */
		case RTS:
			/*pc[thread] = pull(8);*/
			break;
		/* Branch if positive. */
		case BPO:
			if (!(ps & ((uint64_t)N << 8*thread)))
				pc[thread] = value;
			else
				pc[thread]+=0;
			break;
		/* Branch if negative. */
		case BNG:
			if (ps & ((uint64_t)N << 8*thread))
				pc[thread] = value;
			else
				pc[thread]+=0;
			break;
		/* Branch if equal. */
		case BEQ:
			if (ps & ((uint64_t)Z << 8*thread))
				pc[thread] = value;
			else
				pc[thread]+=0;
			break;
		/* Branch if not equal. */
		case BNE:
			if (!(ps & ((uint64_t)Z << 8*thread)))
				pc[thread] = value;
			else
				pc[thread]+=0;
			break;
		/* Branch if carry set. */
		case BCS:
			if (ps & ((uint64_t)C << 8*thread))
				pc[thread] = value;
			else
				pc[thread]+=0;
			break;
		/* Branch if carry clear. */
		case BCC:
			if (!(ps & ((uint64_t)C << 8*thread)))
				pc[thread] = value;
			else
				pc[thread]+=0;
			break;
		/* Branch if overflow set. */
		case BVS:
			if (ps & ((uint64_t)V << 8*thread))
				pc[thread] = value;
			else
				pc[thread]+=0;
			break;
		/* Branch if overflow clear. */
		case BVC:
			if (!(ps & ((uint64_t)V << 8*thread)))
				pc[thread] = value;
			else
				pc[thread]+=0;
			break;
		/* Compare accumulator. */
		case CMP:
		case CAY:
		case CAX:
			if (opcode == CAY || opcode == CAX) {
				dif = (opcode == CAY) ? a[thread]-y[thread] : a[thread]-x[thread];
				value = a[thread];
			} else {
				dif = value-a[thread];
			}
			ps = (dif & 0x8000000000000000) ? (ps | (uint64_t)N << 8*thread) : (ps & ~((uint64_t)N << 8*thread));
			ps = (dif == 0) ? (ps | (uint64_t)Z << 8*thread) : (ps & ~((uint64_t)Z << 8*thread));
			ps = (dif > value) ? (ps | (uint64_t)C << 8*thread) : (ps & ~((uint64_t)C << 8*thread));
			break;
		/* Compare y register. */
		case CPY:
			dif = value-y[thread];
			ps = (dif & 0x8000000000000000) ? (ps | (uint64_t)N << 8*thread) : (ps & ~((uint64_t)N << 8*thread));
			ps = (dif == 0) ? (ps | (uint64_t)Z << 8*thread) : (ps & ~((uint64_t)Z << 8*thread));
			ps = (dif > value) ? (ps | (uint64_t)C << 8*thread) : (ps & ~((uint64_t)C << 8*thread));
			break;
		/* Compare x register. */
		case CPX:
			dif = value-x[thread];
			ps = (dif & 0x8000000000000000) ? (ps | (uint64_t)N << 8*thread) : (ps & ~((uint64_t)N << 8*thread));
			ps = (dif == 0) ? (ps | (uint64_t)Z << 8*thread) : (ps & ~((uint64_t)Z << 8*thread));
			ps = (dif > value) ? (ps | (uint64_t)C << 8*thread) : (ps & ~((uint64_t)C << 8*thread));
			break;
	}
	return 1;
}

int setflags(uint8_t opcode, uint8_t thread) {
	switch (opcode) {
		/* Clear processor status. */
		case CPS:
			ps &= 0;
			break;
		/* Set interupt flag. */
		case SEI:
			ps |= ((uint64_t)I << 8*thread);
			break;
		/* Clear interupt flag. */
		case CLI:
			ps &= ~((uint64_t)I << 8*thread);
			break;
		/* Set carry flag. */
		case SEC:
			ps |= ((uint64_t)C << 8*thread);
			break;
		/* Clear carry flag. */
		case CLC:
			ps &= ~((uint64_t)C << 8*thread);
			break;
		/* Set stack protection flag. */
		case SSP:
			ps |= ((uint64_t)S << 8*thread);
			break;
		/* Clear stack protection flag. */
		case CSP:
			ps &= ~((uint64_t)S << 8*thread);
			break;
		/* Set overflow flag. */
		case SEV:
			ps |= ((uint64_t)V << 8*thread);
			break;
		/* Clear overflow flag. */
		case CLV:
			ps &= ~((uint64_t)V << 8*thread);
			break;
	}
	return 1;
}

int memory(uint8_t opcode, uint64_t value, uint8_t thread) {
	switch (opcode) {
		/* Load value. */
		case LDA:  /* LDA, imm */
		case LDY:  /* LDY, imm */
		case LAY:  /* LAY, imm */
		case LDX:  /* LDX, imm */
		case LAX:  /* LAX, imm */
		case 0x95: /* LDA, abs */
		case 0x25: /* LDY, abs */
		case 0x35: /* LAY, abs */
		case 0x45: /* LDX, abs */
		case 0x55: /* LAX, abs */
			if (opcode == LDA || opcode == LAY || opcode == LAX)
				a[thread] = value;
			else if (opcode == 0x95 || opcode == 0x35 || opcode == 0x55)
				a[thread] = addr[value];
			if (opcode == LAY || opcode == LDY)
				y[thread] = value;
			else if (opcode == 0x35)
				y[thread] = addr[value];
			if (opcode == LAX)
				x[thread] = value;
			else if (opcode == 0x55)
				x[thread] = addr[value];
			break;
		/* Store value. */
		case STA:
		case SAY:
		case SAX:
			addr[value] = a[thread];
			if (opcode == SAY)
				addr[value] = y[thread];
			if (opcode == SAX)
				addr[value] = x[thread];
			break;
		case STY:
			addr[value] = y[thread];
			break;
		case STX:
			addr[value] = x[thread];
			break;
		/* Push processor status. */
		case PHP:
			/*push(ps, value);*/
			break;
		/* Push accumulator. */
		case PHA:
		case PAY:
		case PAX:
			if (value > 7)
				value = 7;
			for (int8_t i = 8*value; i > 0; i-=8) {
				push(a[thread] >> i);
			}
			push(a[thread] & 0xFF);
			if (opcode == PAY) {
				if (value > 7)
					value = 7;
				for (int8_t i = 8*value; i > 0; i-=8) {
					push(y[thread] >> i);
				}
				push(y[thread] & 0xFF);
			}
			if (opcode == PAX) {
				if (value > 7)
					value = 7;
				for (int8_t i = 8*value; i > 0; i-=8) {
					push(x[thread] >> i);
				}
				push(x[thread] & 0xFF);
			}
			break;
		case PHY:
			if (value > 7)
				value = 7;
			for (int8_t i = 8*value; i > 0; i-=8) {
				push(y[thread] >> i);
			}
			push(y[thread] & 0xFF);
			/*for (uint8_t i = value-1; i > 0; i--)
				push(y[thread] >> 8*i);*/
			break;
		case PHX:
			if (value > 7)
				value = 7;
			for (int8_t i = 8*value; i > 0; i-=8) {
				push(x[thread] >> i);
			}
			push(x[thread] & 0xFF);
			/*for (uint8_t i = value-1; i > 0; i--)
				push(x[thread] >> 8*i);*/
			break;
		/* Pull processor status. */
		case PLP:
			/*ps = pull(value);*/
			break;
		/* Pull accumulator. */
		case PLA:
		case PYA:
		case PXA:
			if (value > 7)
				value = 7;
			for (uint8_t i = 0; i <= 8*value; i+=8) {
				if (!i)
					a[thread] = (uint64_t)pull();
				else
					a[thread] += (uint64_t)pull() << i;
			}
			if (opcode == PAY) {
				if (value > 7)
					value = 7;
				for (uint8_t i = 0; i <= 8*value; i+=8) {
					if (!i)
						y[thread] = (uint64_t)pull();
					else
						y[thread] += (uint64_t)pull() << i;
				}
			}
			if (opcode == PAX) {
				if (value > 7)
					value = 7;
				for (uint8_t i = 0; i <= 8*value; i+=8) {
					if (!i)
						x[thread] = (uint64_t)pull();
					else
						x[thread] += (uint64_t)pull() << i;
				}
			}
			break;
		/* Pull y register. */
		case PLY:
			if (value > 7)
				value = 7;
			for (uint8_t i = 0; i <= 8*value; i+=8) {
				if (!i)
					y[thread] = (uint64_t)pull();
				else
					y[thread] += (uint64_t)pull() << i;
			}
			break;
		/* Pull x register. */
		case PLX:
			if (value > 7)
				value = 7;
			for (uint8_t i = 0; i <= 8*value; i+=8) {
				if (!i)
					x[thread] = (uint64_t)pull();
				else
					x[thread] += (uint64_t)pull() << i;
			}
			break;
	}
	return 1;
}

int misc(uint8_t opcode, uint8_t thread) {
	switch (opcode) {
		/* No operation. */
		case NOP:
			break;
		/* TODO: Implement interupts. */
		case BRK:
			break;
	}
	return 1;
}

int main(int argc, char **argv) {
	a[0] = 0;
	x[0] = 0;
	y[0] = 0;
	addr = malloc(8*0x04000000);
	int v = 0;

	/*addr[0x8000] = 0x00;	/* CPS */
	/*addr[0x8001] = 0xE1;	/* LDA #$01 */
	/*addr[0x8002] = 0x01;
	addr[0x8003] = 0x51;	/* SLA #$04 */
	/*addr[0x8004] = 0x04;
	addr[0x8005] = 0x50;	/* BCS $800B */
	/*addr[0x8006] = 0x0B;
	addr[0x8007] = 0x80;
	addr[0x8008] = 0x10;	/* JMP $8003 */
	/*addr[0x8009] = 0x03;
	addr[0x800A] = 0x80;
	addr[0x800B] = 0xE1;	/* LDA #$01 */
	/*addr[0x800C] = 0x01;
	addr[0x800D] = 0x51;	/* SLA #$38 */
	/*addr[0x800E] = 0x38;
	addr[0x800F] = 0x61;	/* SRA #$04 */
	/*addr[0x8010] = 0x04;
	addr[0x8011] = 0x50;	/* BCS $8017 */
	/*addr[0x8012] = 0x17;
	addr[0x8013] = 0x80;
	addr[0x8014] = 0x10;	/* JMP $800F */
	/*addr[0x8015] = 0x0F;
	addr[0x8016] = 0x80;
	addr[0x8017] = 0x10;	/* JMP $8001 */
	/*addr[0x8018] = 0x01;
	addr[0x8019] = 0x80;
	addr[0xFFFC] = 0x00;
	addr[0xFFFD] = 0x80;

	/*addr[0x8000] = 0x00; CPS */
        /*addr[0x8001] = 0xE1; /* LDA #$01 */
	/*addr[0x8002] = 0xFF;
        addr[0x8003] = 0x51; /* SLA #$3C */
	/*addr[0x8004] = 0x38;
	addr[0x8005] = 0x01; /* ADC #$02 */
	/*addr[0x8006] = 0x02;
        addr[0x8007] = 0x81; /* ROR #$04 */
	/*addr[0x8008] = 0x04;
        addr[0x8009] = 0x10; /* JMP $8007 */
        /*addr[0x800A] = 0x07;
        addr[0x800B] = 0x80;
        addr[0x800C] = 0x10; /* JMP $8001 */
        /*addr[0x800D] = 0x01;
	addr[0x800E] = 0x80;
        /*addr[0x8008] = 0x70;  BEQ $800E */
        /*addr[0x8009] = 0x0E;
        addr[0x800A] = 0x80;
        addr[0x800B] = 0x10;  JMP $8001
        addr[0x800C] = 0x01;
        addr[0x800D] = 0x80;
        addr[0x800E] = 0xC1;  INC
        addr[0x800F] = 0xE8;
        addr[0x8010] = 0x04;
        addr[0x8011] = 0x50;
        addr[0x8012] = 0x17;
        addr[0x8013] = 0x80;
        addr[0x8014] = 0x10;
        addr[0x8015] = 0x0F;
        addr[0x8016] = 0x80;
        addr[0x8017] = 0x10;
        addr[0x8018] = 0x01;
        addr[0x8019] = 0x80;*/


	addr[0x8000] = 0x00; /* CPS */
	addr[0x8001] = 0xC1; /* INC */
	addr[0x8002] = 0x09; /* PHA #$08 */
	addr[0x8003] = 0x08;
	addr[0x8004] = 0x1C; /* PLX #$08 */
	addr[0x8005] = 0x08;
	addr[0x8006] = 0x10; /* JMP $8001 */
	addr[0x8007] = 0x01;
	addr[0x8008] = 0x80;
	addr[0x8009] = 0x00;
	addr[0x800A] = 0x00;
	addr[0x800B] = 0x00;

        addr[0xFFFC] = 0x00;
        addr[0xFFFD] = 0x80;

	pc[0] = addr[0xFFFC] | (addr[0xFFFD] << 8);
	uint64_t value = 0;
	uint64_t inst = 0;
	uint8_t lines = 2;
	uint8_t end = 0;
	uint8_t opcode = 0;
	uint8_t size;
	printf("\033[2J");
	while (!end) {
		/*printf("pc: 0x%04llx, a: 0x%016llx, carry: %u, inst: %s, value: 0x%04llx, $%04llx: %02x\n", pc[0], a[0], ps & 1, opname[opcode], value, value, addr[value]);*/
		opcode = addr[pc[0]];
		if (opcode == STA || opcode == JMP || opcode == BCS)
			size = 2;
		else if (opcode == CPS || opcode == NOP || opcode == INC || opcode == IAX || opcode == CAX)
			size = 0;
		else if (opcode == STP)
			end = 1, size = 0;
		else
			size = 1;
		pc[0]++;
		if (size) {
			int i = 0;
			while (i < size) {
				if (!i)
					value = (addr[pc[0]]);
				else
					value |= (addr[pc[0]] << (8*i));
				/*printf("pc: 0x%04llx, a: 0x%016llx, carry: %u, inst: %s, value: 0x%04llx, $%04llx: %02x\n", pc[0], a[0], ps & 1, opname[opcode], value, value, addr[value]);*/
				pc[0]++;
				i++;
			}
		}
		if (optype(opcode) == ALU)
			alu(opcode, value, 0);
		if (optype(opcode) == THREAD)
			threads(opcode, value);
		if (optype(opcode) == BRANCH)
			branch(opcode, value, 0);
		if (optype(opcode) == MEMORY)
			memory(opcode, value, 0);
		if (optype(opcode) == FLAG)
			setflags(opcode, 0);
		if (optype(opcode) == MISC)
			misc(opcode, 0);

		if (lines > 50)
			lines = 2;
		printf("\033[%uHpc: 0x%04llx, a: 0x%016llx, x: 0x%016llx, y: 0x%016llx, sp: 0x%04lx, carry: %u, inst: %s, value: 0x%04llx, $%04llx: %02x\n", lines, pc[0], a[0], x[0], y[0], sp, ps & 1, opname[opcode], value, value, addr[value]);
		lines++;
		printf("\033[1HInstructions executed: %llu\n", inst++);
	}
	free(addr);
	return 0;

}