From 245940209d5b8458f5d998783f7992573e2951bf Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Mon, 2 Dec 2019 18:02:37 -0500 Subject: Started implementing some pthreads support. --- Makefile | 2 +- opcode.c | 21 ++----- sux.c | 189 +++++++++++++++++++++++++++++++++------------------------------ 3 files changed, 103 insertions(+), 109 deletions(-) diff --git a/Makefile b/Makefile index df46e69..e4d801a 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ CFLAGS = $(PCC_CFLAGS) $(CFLAGS_EXTRA) OBJS = asmmon.o opcode.o sux.o OBJ_NAME = cisc-0.2 all : $(OBJS) - $(CC) $(OBJS) $(CFLAGS) -o $(OBJ_NAME) + $(CC) $(OBJS) $(CFLAGS) -lpthread -o $(OBJ_NAME) sux.o : $(CC) sux.c -c $(CFLAGS) -o sux.o opcode.o : diff --git a/opcode.c b/opcode.c index 4484466..e4bd510 100644 --- a/opcode.c +++ b/opcode.c @@ -372,7 +372,7 @@ void bfc(struct sux *cpu, uint8_t flag, uint64_t adr, uint8_t thread) { uint64_t immaddr(struct sux *cpu, uint8_t thread, uint8_t size) { uint64_t adr = cpu->pc[thread]; cpu->pc[thread]+=size; - clk+=size; + clk++; return adr; } @@ -386,7 +386,6 @@ uint64_t absaddr(struct sux *cpu, uint8_t thread) { | (uint64_t)addr[cpu->pc[thread]+6] << 48 | (uint64_t)addr[cpu->pc[thread]+7] << 56; cpu->pc[thread]+=8; - ibcount+=8; clk++; return adr; } @@ -397,11 +396,7 @@ uint32_t zeromtx(struct sux *cpu, uint8_t thread) { | (uint32_t)addr[cpu->pc[thread]+2] << 16 | (uint32_t)addr[cpu->pc[thread]+3] << 24; cpu->pc[thread]+=4; - ibcount+=4; - if (ibcount > 8) { - clk++; - ibcount = 0; - } + clk++; return adr; } @@ -412,11 +407,7 @@ uint32_t zeromx(struct sux *cpu, uint8_t thread) { | (uint32_t)addr[cpu->pc[thread]+3] << 24; adr += cpu->x[thread]; cpu->pc[thread]+=4; - ibcount+=4; - if (ibcount > 8) { - clk++; - ibcount = 0; - } + clk++; return adr; } @@ -427,10 +418,6 @@ uint32_t zeromy(struct sux *cpu, uint8_t thread) { | (uint32_t)addr[cpu->pc[thread]+3] << 24; adr += cpu->y[thread]; cpu->pc[thread]+=4; - ibcount+=4; - if (ibcount > 8) { - clk++; - ibcount = 0; - } + clk++; return adr; } diff --git a/sux.c b/sux.c index 7bb8661..a38b52a 100644 --- a/sux.c +++ b/sux.c @@ -1,27 +1,28 @@ #include "opcode.h" +#include +#include #define bench 0 #if bench #include #endif +#define THREADS 0 +uint64_t inst; +uint8_t lines[THREADS+1]; +struct sux suxx; +#if bench +double ipc; +time_t str, en; +#endif + int run(struct sux *cpu, uint8_t prefix, uint8_t opcode, uint8_t thread) { uint64_t address; uint8_t rs = (prefix & 0x30) >> 4; uint8_t regsize = (1 << rs); uint8_t tmp; address = cpu->pc[thread]; -#if !bench - printf("pc: 0x%llx, a: 0x%016llx, x: 0x%016llx, y: 0x%016llx" - ", sp: 0x%04lx, ps: 0x%016llx, opcode: 0x%02x, inst: %s\n" - , cpu->pc[thread], cpu->a[thread], cpu->x[thread], cpu->y[thread] - , cpu->sp, cpu->ps, opcode, opname[opcode]); - fflush(stdout); -#endif cpu->pc[thread]++; - if (ibcount > 8) { - clk++; - ibcount = 0; - } + clk++; switch(opcode) { case CPS: /* Clear Processor Status. */ for (uint8_t i = 0; i < 8; i++) { @@ -362,109 +363,115 @@ int run(struct sux *cpu, uint8_t prefix, uint8_t opcode, uint8_t thread) { return 1; } -int main(int argc, char **argv) { - - struct sux cpu; - cpu.a[0] = 0; - cpu.x[0] = 0; - cpu.y[0] = 0; - cpu.sp = 0xFFFF; - ibcount = 0; - addr = malloc(8*0x04000000); - int v = 0; - if (asmmon() == 2) - return 0; - -#if 0 - /* Reset Vector. */ - addr[0xFFC0] = 0x00; - addr[0xFFC1] = 0x80; - addr[0xFFC2] = 0x00; - addr[0xFFC3] = 0x00; - addr[0xFFC4] = 0x00; - addr[0xFFC5] = 0x00; - addr[0xFFC6] = 0x00; - addr[0xFFC7] = 0x00; -#endif -#if 0 - /* BRK Vector. */ - addr[0xFFE0] = 0x00; - addr[0xFFE1] = 0x80; - addr[0xFFE2] = 0x02; - addr[0xFFE3] = 0x00; - addr[0xFFE4] = 0x00; - addr[0xFFE5] = 0x00; - addr[0xFFE6] = 0x00; - addr[0xFFE7] = 0x00; -#endif - cpu.pc[0] = (uint64_t)addr[0xFFC0] - | (uint64_t)addr[0xFFC1] << 8 - | (uint64_t)addr[0xFFC2] << 16 - | (uint64_t)addr[0xFFC3] << 24 - | (uint64_t)addr[0xFFC4] << 32 - | (uint64_t)addr[0xFFC5] << 40 - | (uint64_t)addr[0xFFC6] << 48 - | (uint64_t)addr[0xFFC7] << 56; - uint64_t inst = 0; - uint8_t lines = 2; +void *threads(void *args) { + uint8_t thread = *((uint8_t *) args); uint8_t prefix = 0; uint8_t opcode = 0; - uint8_t end = 0; -#if bench - double ipc; - clock_t st, en; - double t0; -#endif - printf("\033[2J"); + #if bench - st = clock(); -#endif + uint8_t end = 0; while (!end) { - prefix = addr[cpu.pc[0]]; +#endif + prefix = addr[suxx.pc[thread]]; + if ((prefix & 0x07) == 0x07) { - cpu.pc[0]++; - ibcount++; + suxx.pc[thread]++; } - opcode = addr[cpu.pc[0]]; - ibcount++; + + opcode = addr[suxx.pc[thread]]; #if !bench - printf("\033[%uH\033[2K", lines); - lines++; + printf("\033[%uH\033[2K" + "pc: 0x%08llx, a: 0x%016llx, x: 0x%016llx, y: 0x%016llx" + ", sp: 0x%04lx, ps: 0x%016llx, opcode: 0x%02x, thread: %u, inst: %s\n" + , lines[thread] + , suxx.pc[thread], suxx.a[thread], suxx.x[thread], suxx.y[thread] + , suxx.sp, suxx.ps, opcode, thread, opname[opcode]); + fflush(stdout); + lines[thread]++; + if (lines[thread] > 6*(thread+1)) + lines[thread] = (6*thread)+2; #endif if ((prefix & 0x07) == 0x07) - run(&cpu, prefix-7, opcode, 0); + run(&suxx, prefix-7, opcode, thread); else - run(&cpu, 0, opcode, 0); -#if !bench - if (lines > 24) - lines = 2; + run(&suxx, 0, opcode, thread); + + inst++; +#if bench + if (inst >= 10000000) { + end = 1; + } + } #endif +} + + + +int main(int argc, char **argv) { + suxx.a[0] = 0; + suxx.x[0] = 0; + suxx.y[0] = 0; + suxx.sp = 0xFFFF; + ibcount = 0; + addr = malloc(8*0x04000000); + int v = 0; + + if (asmmon() == 2) + return 0; + + for (int i = 0; i < THREADS+1; i++) { + suxx.a[i] = 0; + suxx.x[i] = 0; + suxx.y[i] = 0; + suxx.pc[i] = (uint64_t)addr[0xFFC0] + | (uint64_t)addr[0xFFC1] << 8 + | (uint64_t)addr[0xFFC2] << 16 + | (uint64_t)addr[0xFFC3] << 24 + | (uint64_t)addr[0xFFC4] << 32 + | (uint64_t)addr[0xFFC5] << 40 + | (uint64_t)addr[0xFFC6] << 48 + | (uint64_t)addr[0xFFC7] << 56; + } + for (int i = 0; i < THREADS+1; i++) + lines[i] = (6*i)+2; + inst = 0; + pthread_t therads[THREADS+1]; + int result; + uint8_t throds[THREADS+1]; + printf("\033[2J"); #if bench - t0 = ((double)clock() - (double)st)/(double)CLOCKS_PER_SEC; + str=clock(); +#else + uint8_t end = 0; + while (!end) { #endif - inst++; + for (int i = 0; i < THREADS+1; i++) { + throds[i] = i; + result = pthread_create(&therads[i], NULL, threads, &throds[i]); + assert(!result); + } + + for (int i = 0; i < THREADS+1; i++) { + result = pthread_join(therads[i], NULL); + assert(!result); + } #if !bench - printf("\033[HInstruction bytes: %x, Instructions executed: %llu, Clock cycles: %llu\n", ibcount, inst, clk); + printf("\033[HInstructions executed: %llu, Clock cycles: %llu\n", inst, clk); fflush(stdout); #endif -#if bench - if (t0 >= 1.00) { - end = 1; - } -#endif - if (ibcount >= 7) - ibcount = 0; +#if !bench } +#endif #if bench - en = (double)clock(); - double tm = (double)(en - st); - double clkspd = (t0*1000000)/clk; + en=clock(); + double tm = (en-str)/1+THREADS; + double clkspd = ((tm/CLOCKS_PER_SEC)*1000000)/clk; double mhz = 1000000.0/clkspd/1000000; double ips = (double)inst/(double)tm; ipc=(double)inst/(double)clk; printf("\033[2J"); - printf("Instructions executed: %llu, Instructions per Second in MIPS: %f, Clock cycles: %llu, Clock Speed in MHz: %f\n", inst, ips, clk, mhz); + printf("Instructions executed: %llu, Instructions per Second in MIPS: %f, Clock cycles: %llu, Clock Speed in MHz: %f, tm: %f\n", inst, ips, clk, mhz, tm/CLOCKS_PER_SEC); #endif free(addr); return 0; -- cgit v1.2.3-13-gbd6f