summaryrefslogtreecommitdiff
path: root/sux.c
diff options
context:
space:
mode:
Diffstat (limited to 'sux.c')
-rw-r--r--sux.c204
1 files changed, 99 insertions, 105 deletions
diff --git a/sux.c b/sux.c
index 08f5ebe..97a695d 100644
--- a/sux.c
+++ b/sux.c
@@ -14,7 +14,6 @@ uint64_t tclk; /* Total Clock cycles. */
uint64_t inst[THREADS];
uint64_t inss;
uint8_t threads_done = 0;
-uint8_t lines[THREADS];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct suxthr {
@@ -38,7 +37,9 @@ void *run(void *args) {
uint64_t value = 0;
uint64_t iclk = 0;
uint64_t ins = 0;
- uint16_t tv = 0xFF50; /* Thread Vector. */
+ char *s = malloc(2048);
+ uint8_t lines = (6*thread)+2;
+ uint16_t tv = 0xFF50; /* Starting address of the Thread Vectors. */
#if bench
gettimeofday(&str[thread], 0);
#endif
@@ -51,15 +52,16 @@ void *run(void *args) {
opcode = addr[cpu->pc[thread]];
#if !bench
- printf("\033[%uH", lines[thread]);
- printf("pc: 0x%08llx, a: 0x%016llx, x: 0x%016llx, y: 0x%016llx"
+ sprintf(s, "\033[%uH"
+ "pc: 0x%08llx, a: 0x%016llx, x: 0x%016llx, y: 0x%016llx"
", sp: 0x%04lx, ps: 0x%016llx, prefix: 0x%02x, opcode: 0x%02x, thread: %u, inst: %s \r"
+ , lines
, cpu->pc[thread], cpu->a[thread], cpu->x[thread], cpu->y[thread]
- , cpu->sp, cpu->ps, prefix, opcode, thread, opname[opcode]);
- fflush(stdout);
- lines[thread]++;
- if (lines[thread] > 6*(thread+1))
- lines[thread] = (6*thread)+2;
+ , cpu->sp[thread], cpu->ps, prefix, opcode, thread, opname[opcode]);
+ fwrite(s, sizeof(char), strlen(s), stdout);
+ lines++;
+ if (lines > 6*(thread+1))
+ lines = (6*thread)+2;
#endif
uint8_t rs = (prefix & 0x30) >> 4;
@@ -138,12 +140,10 @@ void *run(void *args) {
tmp = 7;
for (int8_t i = tmp*8; i >= 0; i-=8) {
if (i)
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->ps >> i;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->ps >> i;
else
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->ps & 0xFF;
- cpu->sp--;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->ps & 0xFF;
+ cpu->sp[thread]--;
}
break;
case PHA: /* PusH Accumulator to stack. */
@@ -152,12 +152,10 @@ void *run(void *args) {
tmp = 7;
for (int8_t i = tmp*8; i >= 0; i-=8) {
if (i)
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->a[thread] >> i;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->a[thread] >> i;
else
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->a[thread] & 0xFF;
- cpu->sp--;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->a[thread] & 0xFF;
+ cpu->sp[thread]--;
}
break;
case PHY: /* PusH Y register to stack. */
@@ -166,12 +164,10 @@ void *run(void *args) {
tmp = 7;
for (int8_t i = tmp*8; i >= 0; i-=8) {
if (i)
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->y[thread] >> i;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->y[thread] >> i;
else
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->y[thread] & 0xFF;
- cpu->sp--;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->y[thread] & 0xFF;
+ cpu->sp[thread]--;
}
break;
case TAY: /* Transfer Accumulator to Y. */
@@ -180,6 +176,8 @@ void *run(void *args) {
case TYA: /* Transfer Y to Accumulator. */
case TXA: /* Transfer X to Accumulator. */
case TXY: /* Transfer X to Y. */
+ case TSX: /* Transfer Stack pointer to X. */
+ case TXS: /* Transfer X to Stack pointer. */
if (opcode == TAY)
cpu->y[thread] = cpu->a[thread];
if (opcode == TAX)
@@ -192,6 +190,18 @@ void *run(void *args) {
cpu->a[thread] = cpu->x[thread];
if (opcode == TXY)
cpu->y[thread] = cpu->x[thread];
+ if (opcode == TSX) {
+ cpu->x[thread] = cpu->sp[thread] & 0xFFFF;
+ cpu->x[thread] = cpu->stk_st[thread] << 16;
+ }
+ if (opcode == TXS) {
+ cpu->sp[thread] = cpu->x[thread];
+ if (prefix == 0x17 && (value == thread+1 || value > 8)) {
+ cpu->stk_st[thread] = value & 0xFF;
+ cpu->stk_st[thread] += value << 16;
+ cpu->pc[thread]+=2;
+ }
+ }
if (opcode == TYA || opcode == TXA) {
cpu->z[thread] = (cpu->a[thread] == 0);
cpu->n[thread] = (cpu->a[thread] >> 63);
@@ -213,12 +223,10 @@ void *run(void *args) {
tmp = 7;
for (int8_t i = tmp*8; i >= 0; i-=8) {
if (i)
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->x[thread] >> i;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->x[thread] >> i;
else
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->x[thread] & 0xFF;
- cpu->sp--;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->x[thread] & 0xFF;
+ cpu->sp[thread]--;
}
break;
case JMP: /* JMP Absolute. */
@@ -291,13 +299,11 @@ void *run(void *args) {
if (tmp > 7)
tmp = 7;
for (uint8_t i = 0; i < (tmp+1)*8; i+=8) {
- cpu->sp++;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ cpu->sp[thread]++;
if (i)
- cpu->ps += (uint64_t)addr[STK_STADDR+cpu->sp] << i;
+ cpu->ps += (uint64_t)addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i;
else
- cpu->ps = (uint64_t)addr[STK_STADDR+cpu->sp] & 0xFF;
+ cpu->ps = (uint64_t)addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] & 0xFF;
}
break;
case PLA: /* PuLl Accumulator from stack. */
@@ -305,13 +311,11 @@ void *run(void *args) {
if (tmp > 7)
tmp = 7;
for (uint8_t i = 0; i < (tmp+1)*8; i+=8) {
- cpu->sp++;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ cpu->sp[thread]++;
if (i)
- cpu->a[thread] += (uint64_t)addr[STK_STADDR+cpu->sp] << i;
+ cpu->a[thread] += (uint64_t)addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i;
else
- cpu->a[thread] = (uint64_t)addr[STK_STADDR+cpu->sp] & 0xFF;
+ cpu->a[thread] = (uint64_t)addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] & 0xFF;
}
break;
case PLY: /* PuLl Y register from stack. */
@@ -319,13 +323,11 @@ void *run(void *args) {
if (tmp > 7)
tmp = 7;
for (uint8_t i = 0; i < (tmp+1)*8; i+=8) {
- cpu->sp++;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ cpu->sp[thread]++;
if (i)
- cpu->y[thread] += (uint64_t)addr[STK_STADDR+cpu->sp] << i;
+ cpu->y[thread] += (uint64_t)addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i;
else
- cpu->y[thread] = (uint64_t)addr[STK_STADDR+cpu->sp] & 0xFF;
+ cpu->y[thread] = (uint64_t)addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] & 0xFF;
}
break;
case PLX: /* PuLl X register from stack. */
@@ -333,13 +335,11 @@ void *run(void *args) {
if (tmp > 7)
tmp = 7;
for (uint8_t i = 0; i < (tmp+1)*8; i+=8) {
- cpu->sp++;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ cpu->sp[thread]++;
if (i)
- cpu->x[thread] += (uint64_t)addr[STK_STADDR+cpu->sp] << i;
+ cpu->x[thread] += (uint64_t)addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i;
else
- cpu->x[thread] = (uint64_t)addr[STK_STADDR+cpu->sp] & 0xFF;
+ cpu->x[thread] = (uint64_t)addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] & 0xFF;
}
break;
case JSR: /* Jump to SubRoutine. */
@@ -350,12 +350,10 @@ void *run(void *args) {
cpu->pc[thread]+=4;
for (int8_t i = 24; i >= 0; i-=8) {
if (i)
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->pc[thread] >> i;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->pc[thread] >> i;
else
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->pc[thread] & 0xFF;
- cpu->sp--;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->pc[thread] & 0xFF;
+ cpu->sp[thread]--;
}
cpu->pc[thread] = address;
break;
@@ -1099,15 +1097,13 @@ void *run(void *args) {
break;
case RTS: /* ReTurn from Subroutine. */
for (uint8_t i = 0; i < 32; i+=8) {
- cpu->sp++;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ cpu->sp[thread]++;
if (i < 24)
- cpu->pc[thread] += addr[STK_STADDR+cpu->sp] << i;
+ cpu->pc[thread] += addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i;
else if (i == 24)
- cpu->pc[thread] += addr[STK_STADDR+cpu->sp] << i +1;
+ cpu->pc[thread] += addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i +1;
else
- cpu->pc[thread] = addr[STK_STADDR+cpu->sp];
+ cpu->pc[thread] = addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
}
break;
case CMP: /* CMP Immediate. */
@@ -1193,20 +1189,16 @@ void *run(void *args) {
cpu->pc[i+1] = cpu->pc[0]+(i+1);
break;
case RTI: /* ReTurn from Interupt routine. */
- cpu->sp++;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
- cpu->ps = addr[STK_STADDR+cpu->sp] << 8*thread;
+ cpu->sp[thread]++;
+ cpu->ps = addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << 8*thread;
for (uint8_t i = 0; i < 64; i+=8) {
- cpu->sp++;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ cpu->sp[thread]++;
if (i < 56)
- cpu->pc[thread] += addr[STK_STADDR+cpu->sp] << i;
+ cpu->pc[thread] += addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i;
else if (i == 56)
- cpu->pc[thread] += addr[STK_STADDR+cpu->sp] << i +1;
+ cpu->pc[thread] += addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i +1;
else
- cpu->pc[thread] = addr[STK_STADDR+cpu->sp];
+ cpu->pc[thread] = addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
}
break;
case INC: /* INC Accumulator. */
@@ -1277,12 +1269,10 @@ void *run(void *args) {
cpu->pc[thread]+=8;
for (int8_t i = 56; i >= 0; i-=8) {
if (i)
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->pc[thread] >> i;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->pc[thread] >> i;
else
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->pc[thread] & 0xFF;
- cpu->sp--;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->pc[thread] & 0xFF;
+ cpu->sp[thread]--;
}
cpu->pc[thread] = address;
break;
@@ -1314,15 +1304,13 @@ void *run(void *args) {
break;
case RTL: /* ReTurn from subroutine Long. */
for (uint8_t i = 0; i < 64; i+=8) {
- cpu->sp++;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ cpu->sp[thread]++;
if (i < 56)
- cpu->pc[thread] = addr[STK_STADDR+cpu->sp] << i;
+ cpu->pc[thread] = addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i;
else if (i == 56)
- cpu->pc[thread] = addr[STK_STADDR+cpu->sp] << i + 1;
+ cpu->pc[thread] = addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] << i + 1;
else
- cpu->pc[thread] = addr[STK_STADDR+cpu->sp];
+ cpu->pc[thread] = addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
}
break;
case 0xF1: /* DEC Absolute. */
@@ -1352,17 +1340,13 @@ void *run(void *args) {
case BRK: /* BReaK. */
for (uint8_t i = 56; i >= 0; i-=8) {
if (i)
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->pc[thread]-1 >> i;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->pc[thread]-1 >> i;
else
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->pc[thread]-1 & 0xFF;
- cpu->sp--;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
- }
- addr[STK_STADDR+cpu->sp] = (uint64_t)cpu->ps >> 8*thread;
- cpu->sp--;
- addr[0xFF90] = cpu->sp & 0xFF;
- addr[0xFF91] = cpu->sp >> 8;
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->pc[thread]-1 & 0xFF;
+ cpu->sp[thread]--;
+ }
+ addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = (uint64_t)cpu->ps >> 8*thread;
+ cpu->sp[thread]--;
cpu->i[thread] = 1;
setps(cpu, thread);
cpu->pc[thread] = (uint64_t)addr[0xFFE0]
@@ -1384,7 +1368,8 @@ void *run(void *args) {
}
ins++;
#if !bench
- printf("\033[%uHInstructions executed: %llu, Clock cycles: %llu\n", (6*thread)+1, ins, iclk);
+ sprintf(s, "\033[%uHInstructions executed: %llu, Clock cycles: %llu\n", (6*thread)+1, ins, iclk);
+ fwrite(s, sizeof(char), strlen(s), stdout);
fflush(stdout);
#endif
if (ins >= BENCH_INST) {
@@ -1400,10 +1385,12 @@ void *run(void *args) {
#endif
}
}
+ free(s);
}
int main(int argc, char **argv) {
struct suxthr thr[THREADS];
+ char *tmp = malloc(2048);
ibcount = 0;
addr = malloc(0x04000000);
inss = 0;
@@ -1412,19 +1399,20 @@ int main(int argc, char **argv) {
if (asmmon() == 2)
return 0;
for (int i = 0; i < THREADS; i++) {
- thr[i].sx.sp = 0xFFFF;
+ thr[i].sx.sp[i] = 0xFFFF;
+ thr[i].sx.stk_st[i] = i+1;
if (i) {
thr[i].sx.a[i] = 0;
thr[i].sx.x[i] = 0;
thr[i].sx.y[i] = 0;
- thr[i].sx.pc[i] = (uint64_t)addr[0xFF50]
- | (uint64_t)addr[0xFF51] << 8
- | (uint64_t)addr[0xFF52] << 16
- | (uint64_t)addr[0xFF53] << 24
- | (uint64_t)addr[0xFF54] << 32
- | (uint64_t)addr[0xFF55] << 40
- | (uint64_t)addr[0xFF56] << 48
- | (uint64_t)addr[0xFF57] << 56;
+ thr[i].sx.pc[i] = (uint64_t)addr[0xFF50+(8*i)]
+ | (uint64_t)addr[0xFF51+(8*i)] << 8
+ | (uint64_t)addr[0xFF52+(8*i)] << 16
+ | (uint64_t)addr[0xFF53+(8*i)] << 24
+ | (uint64_t)addr[0xFF54+(8*i)] << 32
+ | (uint64_t)addr[0xFF55+(8*i)] << 40
+ | (uint64_t)addr[0xFF56+(8*i)] << 48
+ | (uint64_t)addr[0xFF57+(8*i)] << 56;
} else {
thr[i].sx.a[i] = 0;
thr[i].sx.x[i] = 0;
@@ -1441,12 +1429,13 @@ int main(int argc, char **argv) {
thr[i].th = i;
}
for (int i = 0; i < THREADS; i++) {
- lines[i] = (6*i)+2;
inst[i] = 0;
}
pthread_t therads[THREADS];
int result;
- puts("\033[2J\033[H");
+ sprintf(tmp, "\033[2J\033[H");
+ fwrite(tmp, sizeof(char), strlen(tmp), stdout);
+ fflush(stdout);
for (int i = 0; i < THREADS; i++) {
result = pthread_create(&therads[i], NULL, run, &thr[i]);
assert(!result);
@@ -1481,11 +1470,16 @@ int main(int argc, char **argv) {
}
clkspd = (tm[i]/1000000)*1000000/clk[i];
mhz = 1000000.0/clkspd/1000000;
- printf("Instructions executed for thread %i: %llu, Instructions per Second for thread %i in MIPS: %f, tm: %f\n", i, inst[i], i, ips[i], tm[i]/1000000);
+ sprintf(tmp, "Instructions executed for thread %i: %llu, 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);
+ fflush(stdout);
}
clkspd = (ttm/1000000)*1000000/tclk;
mhz = 1000000.0/clkspd/1000000;
- printf("Total Instructions executed: %llu, Total Instructions per Second in MIPS: %f, Clock cycles: %llu, Clock Speed in MHz: %f, tm: %f\n", inss, ipst, tclk, mhz, ttm/1000000);
+ sprintf(tmp, "Total Instructions executed: %llu, Total Instructions per Second in MIPS: %f, Clock cycles: %llu, Clock Speed in MHz: %f, tm: %f\n", inss, ipst, tclk, mhz, ttm/1000000);
+ fwrite(tmp, sizeof(char), strlen(tmp), stdout);
+ fflush(stdout);
+ free(tmp);
}
#endif
free(addr);