summaryrefslogtreecommitdiff
path: root/sux.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2020-09-18 14:49:07 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2020-09-18 14:49:07 -0400
commiteea04e3327972f052dcfb8ae0854b77f87d3d52f (patch)
treee6c65aad43cb65eb152dc55fba1eb13efffdf7f2 /sux.c
parent385a621b9487456c3167f204b02cb0ea0752191d (diff)
- Added support for structs, and unions to the
emulator's assembler. - Make the symbol table a doublely linked list, in both ways. - Optimized the memcopy() function. - Changed the benchmark timing, to now use a timer, and stops once the timer reaches zero. When the timer hits zero, it sends SIGALRM to the main function, which tells the emulator that the benchmark is done.
Diffstat (limited to 'sux.c')
-rw-r--r--sux.c118
1 files changed, 73 insertions, 45 deletions
diff --git a/sux.c b/sux.c
index c53e7b8..1131210 100644
--- a/sux.c
+++ b/sux.c
@@ -14,6 +14,7 @@ uint64_t inst[THREADS];
#if bench
uint64_t inss;
+uint8_t time_done = 0;
#endif
#if debug
@@ -43,7 +44,6 @@ struct suxthr {
#if bench
double ipc;
-struct timeval str[THREADS], en[THREADS];
#endif
inline uint8_t get_addrsize(uint8_t prefix, uint8_t addrmode) {
@@ -138,6 +138,28 @@ static inline uint8_t isread(uint8_t opcode) {
}
}
+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);
+ }
+}
+
void *run(void *args) {
struct suxthr *thr = (void *)args;
struct sux *cpu = &thr->sx;
@@ -147,6 +169,7 @@ void *run(void *args) {
union reg address;
union reg value;
cpu->clk = 0;
+ uint64_t *rem = NULL;
#if !IO
uint64_t ins = 0;
#endif
@@ -167,9 +190,9 @@ void *run(void *args) {
#endif
#endif
uint64_t tmpaddr = 0;
-#if bench
- gettimeofday(&str[thread], 0);
-#endif
+ #if bench
+ start_timer(1, 0);
+ #endif
for (;;) {
#if !bench
if (end) {
@@ -221,6 +244,7 @@ void *run(void *args) {
uint8_t rs = (prefix >> 4) & 3;
uint8_t size = (/***/1 << rs) - 1;
uint8_t check_io = (am != IMM);
+ uint64_t sp = (cpu->sp & 0xFFFF) | (cpu->stk_st << 16);
#if debug && !bench
#if keypoll
pthread_mutex_lock(&mutex);
@@ -250,25 +274,23 @@ void *run(void *args) {
case ADC_IMM: /* ADC Immediate. */
case ADC_AB: /* ADC Absolute. */
case ADC_Z: /* ADC Zero Matrix. */
- adc(cpu, value.u64, thread);
+ cpu->a = adc(cpu, cpu->a, 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 TAY_IMP: cpu->y = transfer(cpu, cpu->a, value.u64, opcode, prefix, thread); break; /* Transfer Accumulator to Y. */
+ case TAX_IMP: cpu->x = transfer(cpu, cpu->a, value.u64, opcode, prefix, thread); break; /* Transfer Accumulator to Y. */
+ case TYX_IMP: cpu->x = transfer(cpu, cpu->y, value.u64, opcode, prefix, thread); break; /* Transfer Y to X. */
+ case TYA_IMP: cpu->a = transfer(cpu, cpu->y, value.u64, opcode, prefix, thread); break; /* Transfer Y to Accumulator. */
+ case TXA_IMP: cpu->a = transfer(cpu, cpu->x, value.u64, opcode, prefix, thread); break; /* Transfer X to Accumulator. */
+ case TXY_IMP: cpu->y = transfer(cpu, cpu->x, value.u64, opcode, prefix, thread); break; /* Transfer X to Y. */
+ case TAB_IMP: cpu->b = transfer(cpu, cpu->a, value.u64, opcode, prefix, thread); break; /* Transfer Accumulator to B. */
+ case TSX_IMP: cpu->x = transfer(cpu, sp, value.u64, opcode, prefix, thread); break; /* Transfer Stack pointer to X. */
+ case TBA_IMP: cpu->a = transfer(cpu, cpu->b, value.u64, opcode, prefix, thread); break; /* Transfer B to Accumulator. */
+ case TXS_IMM: cpu->sp = transfer(cpu, cpu->x, value.u64, opcode, prefix, thread); break; /* Transfer X to Stack pointer. */
case BRA_REL: /* BRA Relative. */
case JMP_AB: /* JMP Absolute. */
case JMP_Z: /* JMP Zero Matrix. */
@@ -280,7 +302,7 @@ void *run(void *args) {
case SBC_IMM: /* SBC Immediate. */
case SBC_AB: /* SBC Absolute. */
case SBC_Z: /* SBC Zero Matrix. */
- sbc(cpu, value.u64, thread);
+ cpu->a = sbc(cpu, cpu->a, 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. */
@@ -293,7 +315,7 @@ void *run(void *args) {
case AND_IMM: /* AND Immediate. */
case AND_AB: /* AND Absolute. */
case AND_Z: /* AND Zero Matrix. */
- and(cpu, value.u64, thread);
+ cpu->a = and(cpu, cpu->a, value.u64, thread);
break;
case BPO_REL: /* BPO Relative. */
if (!getflag(N)) {
@@ -305,7 +327,7 @@ void *run(void *args) {
case ORA_IMM: /* ORA Immediate. */
case ORA_AB: /* ORA Absolute. */
case ORA_Z: /* ORA Zero Matrix. */
- or(cpu, value.u64, thread);
+ cpu->a = or(cpu, cpu->a, value.u64, thread);
break;
case SEI_IMP: /* SEt Interrupt. */
setflag(1, I);
@@ -320,7 +342,7 @@ void *run(void *args) {
case XOR_IMM: /* XOR Immediate. */
case XOR_AB: /* XOR Absolute. */
case XOR_Z: /* XOR Zero Matrix. */
- xor(cpu, value.u64, thread);
+ cpu->a = xor(cpu, cpu->a, value.u64, thread);
break;
case CLI_IMP: /* CLear Interrupt. */
setflag(0, I);
@@ -335,7 +357,7 @@ void *run(void *args) {
case LSL_IMM: /* LSL Immediate. */
case LSL_AB: /* LSL Absolute. */
case LSL_Z: /* LSL Zero Matrix. */
- lsl(cpu, value.u64, thread);
+ cpu->a = lsl(cpu, cpu->a, value.u64, thread);
break;
case SEC_IMP: /* SEt Carry flag.*/
setflag(1, C);
@@ -378,14 +400,14 @@ void *run(void *args) {
case LSR_IMM: /* LSR Immediate. */
case LSR_AB: /* LSR Absolute. */
case LSR_Z: /* LSR Zero Matrix. */
- lsr(cpu, value.u64, thread);
+ cpu->a = lsr(cpu, cpu->a, 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);
+ cpu->a = asr(cpu, cpu->a, value.u64, thread);
break;
case CLC_IMP: /* CLear Carry flag. */
setflag(0, C);
@@ -432,7 +454,7 @@ void *run(void *args) {
case ROL_IMM: /* ROL Immediate. */
case ROL_AB: /* ROL Absolute. */
case ROL_Z: /* ROL Zero Matrix. */
- rol(cpu, value.u64, thread);
+ cpu->a = rol(cpu, cpu->a, value.u64, thread);
break;
case BNE_REL: /* BNE Relative. */
if (!getflag(Z)) {
@@ -444,7 +466,7 @@ void *run(void *args) {
case ROR_IMM: /* ROR Immediate. */
case ROR_AB: /* ROR Absolute. */
case ROR_Z: /* ROR Zero Matrix. */
- ror(cpu, value.u64, thread);
+ cpu->a = ror(cpu, cpu->a, value.u64, thread);
break;
case BVS_REL: /* BVS Relative. */
if (getflag(V)) {
@@ -456,18 +478,19 @@ void *run(void *args) {
case MUL_IMM: /* MUL Immediate. */
case MUL_AB: /* MUL Absolute. */
case MUL_Z: /* MUL Zero Matrix. */
- mul(cpu, value.u64, thread);
+ cpu->a = mul(cpu, cpu->a, 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_IMM: /* DIV Immediate. */
case DIV_AB: /* DIV Absolute. */
case DIV_Z: /* DIV Zero Matrix. */
- divd(cpu, value.u64, opcode, thread);
+ rem = (opcode != DIV_B) ? &cpu->b : &cpu->x;
+ cpu->a = divd(cpu, cpu->a, value.u64, rem, thread);
break;
case CLV_IMP: /* CLear oVerflow flag. */
setflag(0, V);
@@ -583,7 +606,7 @@ void *run(void *args) {
pthread_mutex_unlock(&mutex);
#endif
#elif bench
- if (ins >= BENCH_INST) {
+ if (time_done) {
pthread_mutex_lock(&main_mutex);
threads_done++;
inst[thread] = ins;
@@ -592,7 +615,6 @@ void *run(void *args) {
#endif
pthread_cond_signal(&main_cond);
pthread_mutex_unlock(&main_mutex);
- gettimeofday(&en[thread], 0);
break;
}
#endif
@@ -617,15 +639,13 @@ void init_scr() {
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;
@@ -678,6 +698,13 @@ int main(int argc, char **argv) {
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);
@@ -695,52 +722,53 @@ int main(int argc, char **argv) {
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[THREADS], ttm;
+ 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++) {
- 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];
+ ips[i] = inst[i]/tm;
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];
+ 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, tm: %f\n", i, inst[i], i, ips[i], tm[i]/1000000);
+ 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 = (ttm/1000000)*1000000/tclk;
+ 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", ttm/1000000);
+ sprintf(tmp, ", tm: %f\n", tm/1000000);
fwrite(tmp, sizeof(char), strlen(tmp), stdout);
fflush(stdout);
free(tmp);