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
|
#include "sux.h"
#include <assert.h>
#if getclk
uint64_t clk[THREADS];
uint64_t tclk;
#endif
const uint16_t tv = 0xFF50;
#if !IO
uint64_t inst[THREADS];
#endif
#if bench
uint64_t inss;
uint8_t time_done = 0;
#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 *addr;
uint8_t kbd_rdy;
uint8_t end = 0;
WINDOW *scr;
struct suxthr {
struct sux sx;
uint8_t th;
};
#if bench
double ipc;
#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;
}
static inline uint8_t isrw(uint8_t opcode) {
switch (opcode) {
case STA_AB:
case STA_Z:
case STA_ZX:
case STA_ZY:
case STA_IN:
case STA_IX:
case STA_IY:
case STY_AB:
case STY_Z:
case STY_IN:
case STX_AB:
case STX_Z:
case STX_IN:
case STB_AB:
case STB_Z:
case STB_ZX:
case STB_ZY:
case STB_IN:
case STB_IX:
case STB_IY:
case INC_AB:
case INC_Z:
case DEC_AB:
case DEC_Z:
return 0;
default:
return 1;
}
}
static inline uint8_t isread(uint8_t opcode) {
switch (opcode) {
case LDA_IMM:
case LDA_AB:
case LDA_Z:
case LDA_ZX:
case LDA_ZY:
case LDA_IN:
case LDA_IX:
case LDA_IY:
case LDB_IMM:
case LDB_AB:
case LDB_Z:
case LDB_ZX:
case LDB_ZY:
case LDB_IN:
case LDB_IX:
case LDB_IY:
case LDY_IMM:
case LDY_AB:
case LDY_Z:
case LDY_IN:
case LDX_IMM:
case LDX_AB:
case LDX_Z:
case LDX_IN:
case JMP_AB:
case JMP_Z:
case JMP_IN:
case JSR_IN:
case JSR_AB:
case JSR_Z:
return 0;
default:
return 1;
}
}
#if bench
void stop_timer() {
time_done = 1;
}
void start_timer(int sec, int usec) {
struct itimerval it_val;
for (; usec > 1000000; sec++, usec -= 1000000);
it_val.it_value.tv_sec = sec;
it_val.it_value.tv_usec = usec;
it_val.it_interval.tv_sec = 0;
it_val.it_interval.tv_usec = 0;
if (signal(SIGALRM, stop_timer) == SIG_ERR) {
perror("Unable to catch SIGALRM.");
exit(1);
}
if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) {
perror("Error calling setitimer().");
exit(1);
}
}
#endif
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;
cpu->clk = 0;
uint64_t *rem = NULL;
#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
start_timer(1, 0);
#endif
for (;;) {
#if !bench
if (end) {
pthread_mutex_lock(&main_mutex);
pthread_cond_signal(&main_cond);
pthread_mutex_unlock(&main_mutex);
return NULL;
}
#endif
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
print_regs(cpu, lines, thread);
#if keypoll
pthread_mutex_unlock(&mutex);
#endif
#endif
uint16_t instr = read_value(cpu, 0, cpu->pc, 1, 1, 0);
uint8_t *tmp_inst = (uint8_t *)&instr;
prefix = ((instr & 3) == 3) ? *tmp_inst++ : 0;
opcode = *tmp_inst;
cpu->pc += ((instr & 3) == 3)+1;
address.u64 = cpu->pc;
uint8_t am = optype[opcode];
uint8_t rs = (prefix >> 4) & 3;
uint8_t size = (1 << rs) - 1;
uint8_t check_io = (am != IMM);
#if debug && !bench
#if keypoll
pthread_mutex_lock(&mutex);
#endif
disasm(cpu, lines, opcode, prefix, thread);
lines+=1;
#if keypoll
pthread_mutex_unlock(&mutex);
#endif
#endif
if (am != IMPL && am != BREG) {
address.u64 = get_addr(cpu, opcode, prefix, 1, 1, thread);
if (isrw(opcode) && am != REL && isread(opcode)) {
value.u64 = read_value(cpu, 0, address.u64, size, 1, check_io);
}
}
switch (opcode) {
case CPS_IMP:
cpu->ps.u64 = 0;
break;
case ADC_B:
value.u64 = cpu->b;
case ADC_IMM:
case ADC_AB:
case ADC_Z:
cpu->a = adc(cpu, cpu->a, value.u64, thread);
break;
case PHP_IMP: push(cpu, cpu->ps.u8[thread], 0, thread); break;
case PHA_IMP: push(cpu, cpu->a , size, thread); break;
case PHB_IMP: push(cpu, cpu->b , size, thread); break;
case PHY_IMP: push(cpu, cpu->y , size, thread); break;
case PHX_IMP: push(cpu, cpu->x , size, thread); break;
case TAY_IMP: cpu->y = transfer(cpu, cpu->a , value.u64, thread); break;
case TAX_IMP: cpu->x = transfer(cpu, cpu->a , value.u64, thread); break;
case TYX_IMP: cpu->x = transfer(cpu, cpu->y , value.u64, thread); break;
case TYA_IMP: cpu->a = transfer(cpu, cpu->y , value.u64, thread); break;
case TXA_IMP: cpu->a = transfer(cpu, cpu->x , value.u64, thread); break;
case TXY_IMP: cpu->y = transfer(cpu, cpu->x , value.u64, thread); break;
case TAB_IMP: cpu->b = transfer(cpu, cpu->a , value.u64, thread); break;
case TSX_IMP: cpu->x = transfer(cpu, cpu->sp, value.u64, thread); break;
case TBA_IMP: cpu->a = transfer(cpu, cpu->b , value.u64, thread); break;
case TXS_IMM: cpu->sp = transfer(cpu, cpu->x , value.u64, thread); break;
case BRA_REL:
case JMP_AB:
case JMP_Z:
case JMP_IN:
cpu->pc = address.u64;
break;
case SBC_B:
value.u64 = cpu->b;
case SBC_IMM:
case SBC_AB:
case SBC_Z:
cpu->a = adc(cpu, cpu->a, ~value.u64, thread);
break;
case PLP_IMP: cpu->ps.u8[thread] = pull(cpu, 0, thread); break;
case PLA_IMP: cpu->a = pull(cpu, size, thread); break;
case PLB_IMP: cpu->b = pull(cpu, size, thread); break;
case PLY_IMP: cpu->y = pull(cpu, size, thread); break;
case PLX_IMP: cpu->x = pull(cpu, size, thread); break;
break;
case AND_B:
value.u64 = cpu->b;
case AND_IMM:
case AND_AB:
case AND_Z:
cpu->a = and(cpu, cpu->a, value.u64, thread);
break;
case BPO_REL:
if (!getflag(N)) {
cpu->pc = address.u64;
}
break;
case ORA_B:
value.u64 = cpu->b;
case ORA_IMM:
case ORA_AB:
case ORA_Z:
cpu->a = or(cpu, cpu->a, value.u64, thread);
break;
case SEI_IMP:
setflag(1, I);
break;
case BNG_REL:
if (getflag(N)) {
cpu->pc = address.u64;
}
break;
case XOR_B:
value.u64 = cpu->b;
case XOR_IMM:
case XOR_AB:
case XOR_Z:
cpu->a = xor(cpu, cpu->a, value.u64, thread);
break;
case CLI_IMP:
setflag(0, I);
break;
case BCS_REL:
if (getflag(C)) {
cpu->pc = address.u64;
}
break;
case LSL_B:
value.u64 = cpu->b;
case LSL_IMM:
case LSL_AB:
case LSL_Z:
cpu->a = lsl(cpu, cpu->a, value.u64, thread);
break;
case SEC_IMP:
setflag(1, C);
break;
case STA_AB:
case STA_Z:
case STA_ZX:
case STA_ZY:
case STA_IN:
case STA_IX:
case STA_IY:
store(cpu, address.u64, cpu->a, prefix, thread);
break;
case STY_AB:
case STY_Z:
case STY_IN:
store(cpu, address.u64, cpu->y, prefix, thread);
break;
case STX_AB:
case STX_Z:
case STX_IN:
store(cpu, address.u64, cpu->x, prefix, thread);
break;
case STB_AB:
case STB_Z:
case STB_ZX:
case STB_ZY:
case STB_IN:
case STB_IX:
case STB_IY:
store(cpu, address.u64, cpu->b, prefix, thread);
break;
case BCC_REL:
if (!getflag(C)) {
cpu->pc = address.u64;
}
break;
case LSR_B:
value.u64 = cpu->b;
case LSR_IMM:
case LSR_AB:
case LSR_Z:
cpu->a = lsr(cpu, cpu->a, value.u64, thread);
break;
case ASR_B:
value.u64 = cpu->b;
case ASR_IMM:
case ASR_AB:
case ASR_Z:
cpu->a = asr(cpu, cpu->a, value.u64, thread);
break;
case CLC_IMP:
setflag(0, C);
break;
case LDB_IMM:
case LDB_AB:
case LDB_Z:
case LDB_ZX:
case LDB_ZY:
case LDB_IN:
case LDB_IX:
case LDB_IY:
cpu->b = load(cpu, cpu->b, address.u64, size, thread);
break;
case LDA_IMM:
case LDA_AB:
case LDA_Z:
case LDA_ZX:
case LDA_ZY:
case LDA_IN:
case LDA_IX:
case LDA_IY:
cpu->a = load(cpu, cpu->a, address.u64, size, thread);
break;
case LDY_IMM:
case LDY_AB:
case LDY_Z:
case LDY_IN:
cpu->y = load(cpu, cpu->y, address.u64, size, thread);
break;
case LDX_IMM:
case LDX_AB:
case LDX_Z:
case LDX_IN:
cpu->x = load(cpu, cpu->x, address.u64, size, thread);
break;
case BEQ_REL:
if (getflag(Z)) {
cpu->pc = address.u64;
}
break;
case ROL_B:
value.u64 = cpu->b;
case ROL_IMM:
case ROL_AB:
case ROL_Z:
cpu->a = rol(cpu, cpu->a, value.u64, thread);
break;
case BNE_REL:
if (!getflag(Z)) {
cpu->pc = address.u64;
}
break;
case ROR_B:
value.u64 = cpu->b;
case ROR_IMM:
case ROR_AB:
case ROR_Z:
cpu->a = ror(cpu, cpu->a, value.u64, thread);
break;
case BVS_REL:
if (getflag(V)) {
cpu->pc = address.u64;
}
break;
case MUL_B:
value.u64 = cpu->b;
case MUL_IMM:
case MUL_AB:
case MUL_Z:
cpu->a = mul(cpu, cpu->a, value.u64, thread);
break;
case BVC_REL:
if (!getflag(V)) {
cpu->pc = address.u64;
}
break;
case DIV_B:
case DIV_IMM:
case DIV_AB:
case DIV_Z:
rem = (opcode != DIV_B) ? &cpu->b : &cpu->x;
cpu->a = divd(cpu, cpu->a, value.u64, rem, thread);
break;
case CLV_IMP:
setflag(0, V);
break;
case CPB_IMM:
case CPB_AB:
case CPB_Z:
case CPB_IN:
case CPB_IX:
case CPB_IY:
cmp(cpu, value.u64, cpu->b, thread);
break;
case CMP_B:
value.u64 = cpu->b;
case CMP_IMM:
case CMP_AB:
case CMP_Z:
case CMP_IN:
case CMP_IX:
case CMP_IY:
cmp(cpu, value.u64, cpu->a, thread);
break;
case CPY_IMM:
case CPY_AB:
case CPY_Z:
cmp(cpu, value.u64, cpu->y, thread);
break;
case CPX_IMM:
case CPX_AB:
case CPX_Z:
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:
case JSR_AB:
case JSR_Z:
push(cpu, cpu->pc, (size) ? size : 7, thread);
cpu->pc = address.u64;
break;
case INC_AB:
case INC_Z:
idm(cpu, address.u64, prefix, 1, thread);
break;
case NOP_IMP:
break;
case RTI_IMP:
cpu->ps.u8[thread] = pull(cpu, 0, thread);
size = 0;
case RTS_IMP:
cpu->pc = pull(cpu, (size) ? size : 7, thread);
break;
case DEC_AB:
case DEC_Z:
idm(cpu, address.u64, prefix, 0, thread);
break;
case BRK_IMP:
case WAI_IMP:
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) {
int c = 0;
#if debug
wrefresh(scr);
#endif
for (; step && c != 19 && !end; c = get_key(scr));
#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, cpu->clk);
#endif
if (step || !subdbg) {
wrefresh(scr);
}
#if keypoll
pthread_mutex_unlock(&mutex);
#endif
#elif bench
if (time_done) {
pthread_mutex_lock(&main_mutex);
threads_done++;
inst[thread] = ins;
#if getclk
clk[thread] = cpu->clk;
#endif
pthread_cond_signal(&main_cond);
pthread_mutex_unlock(&main_mutex);
break;
}
#endif
}
return NULL;
}
void init_scr() {
if (!scr) {
scr = initscr();
}
nodelay(scr, 0);
keypad(scr, 1);
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);
#if bench
inss = 0;
struct timeval str, en;
#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 = (i << 16) | 0xFFFF;
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);
}
werase(scr);
#if bench
endwin();
gettimeofday(&str, 0);
double t = 0;
double dt = 0;
double t2 = 0;
#endif
while (threads_done < THREADS && !end) {
#if !bench
pthread_mutex_lock(&main_mutex);
pthread_cond_wait(&main_cond, &main_mutex);
pthread_mutex_unlock(&main_mutex);
#else
pthread_mutex_lock(&main_mutex);
pthread_cond_wait(&main_cond, &main_mutex);
pthread_mutex_unlock(&main_mutex);
#endif
}
#if !bench
endwin();
#endif
#if bench
gettimeofday(&en, 0);
if (threads_done == THREADS) {
double tm_sec, tm_usec, tm;
#if getclk
double clkspd;
double mhz;
#endif
double ips[THREADS];
double ipst;
tm_sec = (en.tv_sec - str.tv_sec);
tm_usec = (en.tv_usec-str.tv_usec);
tm = (tm_sec*1000000)+(tm_usec);
for (int i = 0; i < THREADS; i++) {
ips[i] = inst[i]/tm;
if (i) {
inss += inst[i];
ipst += ips[i];
#if getclk
tclk += clk[i];
#endif
} else {
inss = inst[i];
ipst = ips[i];
#if getclk
tclk = clk[i];
#endif
}
#if getclk
clkspd = (tm/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\n", i, inst[i], i, ips[i]);
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 = (tm/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", tm/1000000);
fwrite(tmp, sizeof(char), strlen(tmp), stdout);
fflush(stdout);
free(tmp);
}
#endif
free(addr);
return 0;
}
|