summaryrefslogtreecommitdiff
path: root/sux.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2019-12-11 11:28:30 -0500
committermrb0nk500 <b0nk@b0nk.xyz>2019-12-11 11:28:30 -0500
commit9526483f93b5950ecfa81a93f30b617bec22dfe1 (patch)
tree34ee0e468f3d9ea239870967df1931cef2206dcf /sux.c
parent3dfde833082fc66cededd0206ae5fc76162867b6 (diff)
We can print to the Screen!!!!
I also added the ASR instruction, for doing arithmetic shifts, and have added a hello world program.
Diffstat (limited to 'sux.c')
-rw-r--r--sux.c84
1 files changed, 52 insertions, 32 deletions
diff --git a/sux.c b/sux.c
index 97a695d..19263c3 100644
--- a/sux.c
+++ b/sux.c
@@ -3,12 +3,15 @@
#include <string.h>
#include <pthread.h>
#define bench 0
+#define debug 0
+#define IO 1
#if bench
#include <sys/time.h>
#endif
#define THREADS 1
#define BENCH_INST 100000000*THREADS
+#define DATA_ADDR 0xC001
uint64_t clk[THREADS]; /* Per Thread Clock cycles. */
uint64_t tclk; /* Total Clock cycles. */
uint64_t inst[THREADS];
@@ -37,6 +40,7 @@ void *run(void *args) {
uint64_t value = 0;
uint64_t iclk = 0;
uint64_t ins = 0;
+ uint64_t sign = 0;
char *s = malloc(2048);
uint8_t lines = (6*thread)+2;
uint16_t tv = 0xFF50; /* Starting address of the Thread Vectors. */
@@ -51,10 +55,10 @@ void *run(void *args) {
prefix = 0;
opcode = addr[cpu->pc[thread]];
- #if !bench
+ #if debug && !bench
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"
+ ", 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[thread], cpu->ps, prefix, opcode, thread, opname[opcode]);
@@ -87,7 +91,7 @@ void *run(void *args) {
case 0x05: /* ADC Zero Matrix. */
if (opcode == ADC) {
address = cpu->pc[thread];
- cpu->pc[thread]++;
+ cpu->pc[thread]+=regsize;
}
if (opcode == 0x03) {
address = (uint64_t)addr[cpu->pc[thread]]
@@ -247,7 +251,7 @@ void *run(void *args) {
case 0x15: /* SBC Zero Matrix. */
if (opcode == SBC) {
address = cpu->pc[thread];
- cpu->pc[thread]++;
+ cpu->pc[thread]+=regsize;
}
if (opcode == 0x13) {
address = (uint64_t)addr[cpu->pc[thread]]
@@ -706,14 +710,22 @@ void *run(void *args) {
cpu->pc[thread]+=4;
iclk++;
}
- if (opcode == STA || opcode == 0x7B)
+ if (opcode == STA || opcode == 0x7B || opcode == 0x8B || opcode == 0x9B)
value = cpu->a[thread];
- if (opcode == STY || opcode == 0x7D)
+ if (opcode == STY || opcode == 0x7D || opcode == 0x8D)
value = cpu->y[thread];
- if (opcode == STX || opcode == 0x7E)
+ if (opcode == STX || opcode == 0x7E || opcode == 0x9E)
value = cpu->x[thread];
addr[address] = value & 0xFF;
+ #if IO
+ if (address == DATA_ADDR) {
+ if (addr[address] == '\b')
+ putchar('\b');
+ else
+ putchar(addr[address]);
+ }
+ #endif
if (regsize >= 2)
addr[address+1] = value >> 8;
if (regsize >= 4) {
@@ -744,11 +756,14 @@ void *run(void *args) {
case LSR: /* LSR Immediate. */
case 0x63: /* LSR Absolute. */
case 0x65: /* LSR Zero Matrix. */
- if (opcode == LSR) {
+ case ASR: /* ASR Immediate. */
+ case 0xAB: /* ASR Absolute. */
+ case 0xAD: /* ASR Zero Matrix. */
+ if (opcode == LSR || opcode == ASR) {
address = cpu->pc[thread];
cpu->pc[thread]++;
}
- if (opcode == 0x63) {
+ if (opcode == 0x63 || opcode == 0xAB) {
address = (uint64_t)addr[cpu->pc[thread]]
| (uint64_t)addr[cpu->pc[thread]+1] << 8
| (uint64_t)addr[cpu->pc[thread]+2] << 16
@@ -760,7 +775,7 @@ void *run(void *args) {
cpu->pc[thread]+=8;
iclk++;
}
- if (opcode == 0x65) {
+ if (opcode == 0x65 || opcode == 0xAD) {
address = addr[cpu->pc[thread]]
| addr[cpu->pc[thread]+1] << 8
| addr[cpu->pc[thread]+2] << 16
@@ -769,7 +784,13 @@ void *run(void *args) {
iclk++;
}
value = addr[address];
- sum = (value < 64) ? cpu->a[thread] >> value : 0;
+ if (opcode == ASR || opcode == 0xAB || opcode == 0xAD) {
+ sign = cpu->a[thread] & 0x8000000000000000;
+ sum = (value < 64) ? (cpu->a[thread] >> value) | sign : 0;
+ }
+ if (opcode == LSR || opcode == 0x63 || opcode == 0x65) {
+ sum = (value < 64) ? cpu->a[thread] >> value : 0;
+ }
cpu->z[thread] = (sum == 0);
cpu->n[thread] = (sum >> 63);
cpu->c[thread] = cpu->a[thread] & 1;
@@ -836,11 +857,11 @@ void *run(void *args) {
value += (uint64_t)addr[address+6] << 48;
value += (uint64_t)addr[address+7] << 56;
}
- if (opcode == LDA || opcode == 0x59 || opcode == 0x79)
+ if (opcode == LDA || opcode == 0x59 || opcode == 0x79 || opcode == 0x89 || opcode == 0x99)
cpu->a[thread] = value;
- if (opcode == LDY || opcode == 0x5A || opcode == 0x7A)
+ if (opcode == LDY || opcode == 0x5A || opcode == 0x7A || opcode == 0x8A)
cpu->y[thread] = value;
- if (opcode == LDX || opcode == 0x5C || opcode == 0x7C)
+ if (opcode == LDX || opcode == 0x5C || opcode == 0x7C || opcode == 0x9C)
cpu->x[thread] = value;
cpu->z[thread] = (value == 0);
cpu->n[thread] = (value >> 63);
@@ -978,7 +999,7 @@ void *run(void *args) {
case 0x95: /* MUL Zero Matrix. */
if (opcode == MUL) {
address = cpu->pc[thread];
- cpu->pc[thread]++;
+ cpu->pc[thread]+=regsize;
}
if (opcode == 0x93) {
address = (uint64_t)addr[cpu->pc[thread]]
@@ -1048,7 +1069,7 @@ void *run(void *args) {
case 0xA5: /* DIV Zero Matrix. */
if (opcode == DIV) {
address = cpu->pc[thread];
- cpu->pc[thread]++;
+ cpu->pc[thread]+=regsize;
}
if (opcode == 0xA3) {
address = (uint64_t)addr[cpu->pc[thread]]
@@ -1117,7 +1138,7 @@ void *run(void *args) {
case 0xF5: /* CMP Zero Matrix. */
if (opcode == CMP || opcode == CPY || opcode == CPX) {
address = cpu->pc[thread];
- cpu->pc[thread]++;
+ cpu->pc[thread]+=regsize;
}
if (opcode == 0xE5 || opcode == 0xE2 || opcode == 0xE4) {
address = (uint64_t)addr[cpu->pc[thread]]
@@ -1360,18 +1381,19 @@ void *run(void *args) {
break;
default:
if(opcode != BRK) {
- printf("Cool, you inputed a non existent opcode, which means\n"
+ /*printf("Cool, you inputed a non existent opcode, which means\n"
"that you have now wasted clock cycles.\n"
- "Good job! *clap*\n");
+ "Good job! *clap*\n");*/
}
break;
}
ins++;
- #if !bench
+ #if debug && !bench
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 bench
if (ins >= BENCH_INST) {
end = 1;
pthread_mutex_lock(&mutex);
@@ -1380,10 +1402,9 @@ void *run(void *args) {
clk[thread] = iclk;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
-#if bench
- gettimeofday(&en[thread], 0);
-#endif
+ gettimeofday(&en[thread], 0);
}
+ #endif
}
free(s);
}
@@ -1405,14 +1426,14 @@ int main(int argc, char **argv) {
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+(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;
+ thr[i].sx.pc[i] = (uint64_t)addr[0xFF50+(8*(i-1))]
+ | (uint64_t)addr[0xFF51+(8*(i-1))] << 8
+ | (uint64_t)addr[0xFF52+(8*(i-1))] << 16
+ | (uint64_t)addr[0xFF53+(8*(i-1))] << 24
+ | (uint64_t)addr[0xFF54+(8*(i-1))] << 32
+ | (uint64_t)addr[0xFF55+(8*(i-1))] << 40
+ | (uint64_t)addr[0xFF56+(8*(i-1))] << 48
+ | (uint64_t)addr[0xFF57+(8*(i-1))] << 56;
} else {
thr[i].sx.a[i] = 0;
thr[i].sx.x[i] = 0;
@@ -1472,7 +1493,6 @@ int main(int argc, char **argv) {
mhz = 1000000.0/clkspd/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;