summaryrefslogtreecommitdiff
path: root/sux.h
diff options
context:
space:
mode:
Diffstat (limited to 'sux.h')
-rw-r--r--sux.h338
1 files changed, 138 insertions, 200 deletions
diff --git a/sux.h b/sux.h
index 4678eb9..e483f09 100644
--- a/sux.h
+++ b/sux.h
@@ -25,9 +25,37 @@ extern uint8_t subdbg;
extern uint8_t step;
extern uint8_t esc;
+static inline uint8_t get_opsize(uint8_t prefix, uint8_t addrmode);
+
+enum sw_type {RS, AM, BYTE};
+
#define setflag(flag, bit) ((flag)) ? (cpu->ps.u8[thread] |= bit) : (cpu->ps.u8[thread] &= ~bit)
#define getflag(bit) (cpu->ps.u8[thread] & bit)
+/* reg_expr, and val_expr are the arithmetic expressions
+ * that will be used for either the value, or register, such as '+', or '-'.
+ */
+#define setreg(reg, reg_expr, reg_idx, val, val_expr, val_idx, size) {\
+ switch (size) {\
+ case 7: reg[(reg_idx) reg_expr 7] = val[(val_idx) val_expr 7];\
+ case 6: reg[(reg_idx) reg_expr 6] = val[(val_idx) val_expr 6];\
+ case 5: reg[(reg_idx) reg_expr 5] = val[(val_idx) val_expr 5];\
+ case 4: reg[(reg_idx) reg_expr 4] = val[(val_idx) val_expr 4];\
+ case 3: reg[(reg_idx) reg_expr 3] = val[(val_idx) val_expr 3];\
+ case 2: reg[(reg_idx) reg_expr 2] = val[(val_idx) val_expr 2];\
+ case 1: reg[(reg_idx) reg_expr 1] = val[(val_idx) val_expr 1];\
+ case 0: reg[(reg_idx) reg_expr 0] = val[(val_idx) val_expr 0];\
+ }\
+}
+
+#define setreg_sw(reg, reg_idx, val, val_idx, prefix, addrmode, type) {\
+ switch (type) {\
+ case RS: setreg(reg, +, reg_idx, val, +, val_idx, (1 << (prefix >> 4))-1); break;\
+ case AM: setreg(reg, +, reg_idx, val, +, val_idx, get_opsize(prefix, addrmode)); break;\
+ }\
+}
+
+
extern pthread_mutex_t mutex;
extern pthread_mutex_t main_mutex;
extern pthread_cond_t cond;
@@ -39,6 +67,29 @@ extern void disasm(struct sux *cpu, uint64_t *operands, uint8_t lines, uint8_t o
extern void io(uint64_t address, uint8_t rw);
+static inline uint8_t get_opsize(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 uint64_t get_addr(struct sux *cpu, uint64_t *tmpaddr, uint8_t opcode, uint8_t prefix, uint8_t thread) {
union reg address;
union reg value;
@@ -49,7 +100,7 @@ static inline uint64_t get_addr(struct sux *cpu, uint64_t *tmpaddr, uint8_t opco
case IMPL:
break;
case IMM:
- address.u64 = cpu->pc[thread];
+ address.u64 = cpu->pc;
switch (opcode) {
case PHB:
case PHP:
@@ -67,8 +118,8 @@ static inline uint64_t get_addr(struct sux *cpu, uint64_t *tmpaddr, uint8_t opco
case ROL:
case ROR:
case ASR:
- case ENT: ++cpu->pc[thread]; break;
- default : cpu->pc[thread]+=(1 << (prefix >> 4));
+ case ENT: ++cpu->pc; break;
+ default : cpu->pc+=(1 << (prefix >> 4)); /* Falls Through. */
case TXS: break;
}
break;
@@ -79,21 +130,11 @@ static inline uint64_t get_addr(struct sux *cpu, uint64_t *tmpaddr, uint8_t opco
case INDX:
case INDY:
tmp = 0;
- address.u8[0] = addr[cpu->pc[thread]];
+ address.u8[0] = addr[cpu->pc];
/* Unroll Loop by implementing Duff's Device. */
- switch ((prefix & 0x0C) >> 2) {
- case 2:
- address.u8[5] = addr[cpu->pc[thread]+5];++tmp;
- address.u8[4] = addr[cpu->pc[thread]+4];++tmp;
- case 3:
- address.u8[3] = addr[cpu->pc[thread]+3];++tmp;
- case 1:
- address.u8[2] = addr[cpu->pc[thread]+2];++tmp;
- address.u8[1] = addr[cpu->pc[thread]+1];++tmp;
- case 0:
- ++tmp;
- }
- cpu->pc[thread]+=tmp;
+ tmp = get_opsize(prefix, ZM)+1;
+ setreg_sw(address.u8, 0, addr, cpu->pc, prefix, ZM, AM);
+ cpu->pc+=tmp;
#if debug && !bench
*tmpaddr = address.u64;
#endif
@@ -103,43 +144,32 @@ static inline uint64_t get_addr(struct sux *cpu, uint64_t *tmpaddr, uint8_t opco
uint64_t reg = 0;
switch (optype[opcode]) {
case ZMX:
- address.u64 += cpu->x[thread];
+ address.u64 += cpu->x;
#if getclk
iclk++;
#endif
break;
case ZMY:
- address.u64 += cpu->y[thread];
+ address.u64 += cpu->y;
#if getclk
iclk++;
#endif
break;
case INDX:
- address.u64 += cpu->x[thread];
- #if getclk
- iclk++;
- #endif
- /* Falls Through. */
case INDY:
- /* Did we fall through? */
if (optype[opcode] == INDX) {
- reg = 0; /* Yes, so set reg back to zero. */
+ address.u64 += cpu->x;
+ #if getclk
+ iclk++;
+ #endif
} else {
- reg = cpu->y[thread]; /* No, so set reg to Y. */
+ reg = cpu->y;
#if getclk
iclk++;
#endif
}
- /* Falls Through. */
case IND:
- value.u8[0] = addr[address.u64 ];
- value.u8[1] = addr[address.u64+1];
- value.u8[2] = addr[address.u64+2];
- value.u8[3] = addr[address.u64+3];
- value.u8[4] = addr[address.u64+4];
- value.u8[5] = addr[address.u64+5];
- value.u8[6] = addr[address.u64+6];
- value.u8[7] = addr[address.u64+7];
+ setreg(value.u8, +, 0, addr, +, address.u64, 7);
#if getclk
iclk++;
#endif
@@ -149,26 +179,9 @@ static inline uint64_t get_addr(struct sux *cpu, uint64_t *tmpaddr, uint8_t opco
}
break;
case ABS:
- tmp = 0;
- address.u8[0] = addr[cpu->pc[thread]];++tmp;
- /* Unroll Loop by implementing Duff's Device. */
- switch ((prefix & 0x0C) >> 2) {
- case 3:
- address.u8[7] = addr[cpu->pc[thread]+7];++tmp;
- case 2:
- address.u8[6] = addr[cpu->pc[thread]+6];++tmp;
- address.u8[5] = addr[cpu->pc[thread]+5];++tmp;
- #if getclk
- iclk++;
- #endif
- case 1:
- address.u8[4] = addr[cpu->pc[thread]+4];++tmp;
- address.u8[3] = addr[cpu->pc[thread]+3];++tmp;
- address.u8[2] = addr[cpu->pc[thread]+2];++tmp;
- case 0:
- address.u8[1] = addr[cpu->pc[thread]+1];++tmp;
- }
- cpu->pc[thread]+=tmp;
+ tmp = get_opsize(prefix, ABS)+1;
+ setreg_sw(address.u8, 0, addr, cpu->pc, prefix, ABS, AM);
+ cpu->pc+=tmp;
#if getclk
iclk++;
#endif
@@ -179,39 +192,39 @@ static inline uint64_t get_addr(struct sux *cpu, uint64_t *tmpaddr, uint8_t opco
}
static inline void adc(struct sux *cpu, uint64_t value, uint8_t thread) {
- uint64_t sum = cpu->a[thread]+value+getflag(C);
+ uint64_t sum = cpu->a+value+getflag(C);
setflag(sum == 0, Z);
setflag((sum >> 63), N);
- setflag(((cpu->a[thread]^value) >> 63) && ((cpu->a[thread]^sum) >> 63), V);
+ setflag(((cpu->a^value) >> 63) && ((cpu->a^sum) >> 63), V);
setflag((sum < value), C);
- cpu->a[thread] = sum;
+ cpu->a = sum;
}
static inline void sbc(struct sux *cpu, uint64_t value, uint8_t thread) {
- uint64_t sum = cpu->a[thread]-value-!getflag(C);
+ uint64_t sum = cpu->a-value-!getflag(C);
setflag(sum == 0, Z);
setflag(sum >> 63, N);
- setflag(((cpu->a[thread]^value) >> 63) && ((cpu->a[thread]^sum) >> 63), V);
+ setflag(((cpu->a^value) >> 63) && ((cpu->a^sum) >> 63), V);
setflag((sum > value), C);
- cpu->a[thread] = sum;
+ cpu->a = sum;
}
static inline void transfer(struct sux *cpu, uint64_t value, uint8_t opcode, uint8_t prefix, uint8_t thread) {
uint64_t reg;
switch (opcode) {
- case TBA: cpu->a[thread] = cpu->b[thread]; reg = cpu->a[thread]; break;
- case TXA: cpu->a[thread] = cpu->x[thread]; reg = cpu->a[thread]; break;
- case TYA: cpu->a[thread] = cpu->y[thread]; reg = cpu->a[thread]; break;
- case TAB: cpu->b[thread] = cpu->a[thread]; reg = cpu->b[thread]; break;
- case TAY: cpu->y[thread] = cpu->a[thread]; reg = cpu->y[thread]; break;
- case TXY: cpu->y[thread] = cpu->x[thread]; reg = cpu->y[thread]; break;
- case TAX: cpu->x[thread] = cpu->a[thread]; reg = cpu->x[thread]; break;
- case TYX: cpu->x[thread] = cpu->y[thread]; reg = cpu->x[thread]; break;
- case TSX: cpu->x[thread] = cpu->sp[thread] & 0xFFFF; cpu->x[thread] = cpu->stk_st[thread] << 16; break;
- case TXS: cpu->sp[thread] = cpu->x[thread];
+ case TBA: cpu->a = cpu->b; reg = cpu->a; break;
+ case TXA: cpu->a = cpu->x; reg = cpu->a; break;
+ case TYA: cpu->a = cpu->y; reg = cpu->a; break;
+ case TAB: cpu->b = cpu->a; reg = cpu->b; break;
+ case TAY: cpu->y = cpu->a; reg = cpu->y; break;
+ case TXY: cpu->y = cpu->x; reg = cpu->y; break;
+ case TAX: cpu->x = cpu->a; reg = cpu->x; break;
+ case TYX: cpu->x = cpu->y; reg = cpu->x; break;
+ case TSX: cpu->x = cpu->sp & 0xFFFF; cpu->x = cpu->stk_st << 16; break;
+ case TXS: cpu->sp = cpu->x;
if (prefix == 0x13 && (value == thread+1 || value > 8)) {
- cpu->stk_st[thread] = value & 0xFF;
- cpu->stk_st[thread] += value << 16;
- cpu->pc[thread]+=2;
+ cpu->stk_st = value & 0xFF;
+ cpu->stk_st += value << 16;
+ cpu->pc+=2;
}
break;
}
@@ -219,125 +232,100 @@ static inline void transfer(struct sux *cpu, uint64_t value, uint8_t opcode, uin
setflag(reg >> 63, N);
}
-static inline void push(struct sux *cpu, uint64_t value, uint64_t reg, uint8_t thread) {
- union {
- uint64_t reg;
- uint8_t byte[8];
- } r;
- r.reg = reg;
- uint8_t size = (value > 0) ? value-1 : 0;
+static inline void push(struct sux *cpu, uint64_t size, uint64_t value, uint8_t thread) {
+ union reg reg;
+ reg.u64 = value;
+ size = (size > 0) ? size-1 : 0;
uint8_t tmp = (size <= 7) ? size : 7;
- /* Unroll Loop by implementing Duff's Device. */
- switch (tmp) {
- case 7: addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = r.byte[7];cpu->sp[thread]--;
- case 6: addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = r.byte[6];cpu->sp[thread]--;
- case 5: addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = r.byte[5];cpu->sp[thread]--;
- case 4: addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = r.byte[4];cpu->sp[thread]--;
- case 3: addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = r.byte[3];cpu->sp[thread]--;
- case 2: addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = r.byte[2];cpu->sp[thread]--;
- case 1: addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = r.byte[1];cpu->sp[thread]--;
- case 0: addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]] = r.byte[0];cpu->sp[thread]--;
- }
+ setreg(addr, -, (cpu->stk_st << 16)+cpu->sp, reg.u8, +, 0, tmp);
+ cpu->sp -= (tmp+1);
}
-static inline uint64_t pull(struct sux *cpu, uint64_t value, uint8_t thread) {
- union {
- uint64_t reg;
- uint8_t byte[8];
- } r;
- r.reg = 0;
- uint8_t size = (value > 0) ? value-1 : 0;
+static inline uint64_t pull(struct sux *cpu, uint64_t size, uint8_t thread) {
+ union reg reg;
+ reg.u64 = 0;
+ size = (size > 0) ? size-1 : 0;
uint8_t tmp = (size <= 7) ? size : 7;
- uint8_t tmp2 = 0;
- /* Unroll Loop by implementing Duff's Device. */
- cpu->sp[thread]++;r.byte[tmp2] = addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
- switch (tmp) {
- case 7: cpu->sp[thread]++;tmp2++;r.byte[tmp2] |= addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
- case 6: cpu->sp[thread]++;tmp2++;r.byte[tmp2] |= addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
- case 5: cpu->sp[thread]++;tmp2++;r.byte[tmp2] |= addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
- case 4: cpu->sp[thread]++;tmp2++;r.byte[tmp2] |= addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
- case 3: cpu->sp[thread]++;tmp2++;r.byte[tmp2] |= addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
- case 2: cpu->sp[thread]++;tmp2++;r.byte[tmp2] |= addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
- case 1: cpu->sp[thread]++;tmp2++;r.byte[tmp2] |= addr[(cpu->stk_st[thread] << 16)+cpu->sp[thread]];
- }
- return r.reg;
+ cpu->sp += (tmp+1);
+ setreg(reg.u8, +, 0, addr, -, (cpu->stk_st << 16)+cpu->sp, tmp);
+ return reg.u64;
}
static inline void and(struct sux *cpu, uint64_t value, uint8_t thread) {
- cpu->a[thread] &= value;
- setflag(cpu->a[thread] == 0, Z);
- setflag(cpu->a[thread] >> 63, N);
+ cpu->a &= value;
+ setflag(cpu->a == 0, Z);
+ setflag(cpu->a >> 63, N);
}
static inline void or(struct sux *cpu, uint64_t value, uint8_t thread) {
- cpu->a[thread] |= value;
- setflag(cpu->a[thread] == 0, Z);
- setflag(cpu->a[thread] >> 63, N);
+ cpu->a |= value;
+ setflag(cpu->a == 0, Z);
+ setflag(cpu->a >> 63, N);
}
static inline void xor(struct sux *cpu, uint64_t value, uint8_t thread) {
- cpu->a[thread] ^= value;
- setflag(cpu->a[thread] == 0, Z);
- setflag(cpu->a[thread] >> 63, N);
+ cpu->a ^= value;
+ setflag(cpu->a == 0, Z);
+ setflag(cpu->a >> 63, N);
}
static inline void lsl(struct sux *cpu, uint64_t value, uint8_t thread) {
- uint64_t sum = (value < 64) ? cpu->a[thread] << value : 0;
+ uint64_t sum = (value < 64) ? cpu->a << value : 0;
setflag(sum == 0, Z);
setflag(sum >> 63, N);
- setflag(cpu->a[thread] >> (64-value), C);
- cpu->a[thread] = sum;
+ setflag(cpu->a >> (64-value), C);
+ cpu->a = sum;
}
static inline void lsr(struct sux *cpu, uint64_t value, uint8_t thread) {
- uint64_t sum = (value < 64) ? cpu->a[thread] >> value : 0;
+ uint64_t sum = (value < 64) ? cpu->a >> value : 0;
setflag(sum == 0, Z);
setflag(sum >> 63, N);
- setflag(cpu->a[thread] & 1, C);
- cpu->a[thread] = sum;
+ setflag(cpu->a & 1, C);
+ cpu->a = sum;
}
static inline void asr(struct sux *cpu, uint64_t value, uint8_t thread) {
- uint8_t sign = cpu->a[thread] >> 63;
- uint64_t sum = (value < 64) ? (cpu->a[thread] >> value) | ((uint64_t)sign << 63) : 0;
+ uint8_t sign = cpu->a >> 63;
+ uint64_t sum = (value < 64) ? (cpu->a >> value) | ((uint64_t)sign << 63) : 0;
setflag(sum == 0, Z);
setflag(sum >> 63, N);
- setflag(cpu->a[thread] & 1, C);
- cpu->a[thread] = sum;
+ setflag(cpu->a & 1, C);
+ cpu->a = sum;
}
static inline void rol(struct sux *cpu, uint64_t value, uint8_t thread) {
- uint64_t sum = cpu->a[thread] << value;
+ uint64_t sum = cpu->a << value;
sum |= getflag(C);
setflag(sum == 0, Z);
setflag(sum >> 63, N);
- setflag(cpu->a[thread] >> (uint64_t)(64-value), C);
- cpu->a[thread] = sum;
+ setflag(cpu->a >> (uint64_t)(64-value), C);
+ cpu->a = sum;
}
static inline void ror(struct sux *cpu, uint64_t value, uint8_t thread) {
- uint64_t sum = cpu->a[thread] >> value;
+ uint64_t sum = cpu->a >> value;
sum |= (uint64_t)getflag(C) << (uint64_t)(64-value);
setflag(sum == 0, Z);
setflag(sum >> 63, N);
- setflag(cpu->a[thread] & 1, C);
- cpu->a[thread] = sum;
+ setflag(cpu->a & 1, C);
+ cpu->a = sum;
}
static inline void mul(struct sux *cpu, uint64_t value, uint8_t thread) {
- uint64_t sum = cpu->a[thread]*value;
- cpu->a[thread] = sum;
+ uint64_t sum = cpu->a*value;
+ cpu->a = sum;
setflag(sum == 0, Z);
setflag(sum >> 63, N);
- setflag(!((cpu->a[thread]^value) >> 63) && ((cpu->a[thread]^sum) >> 63), V);
+ setflag(!((cpu->a^value) >> 63) && ((cpu->a^sum) >> 63), V);
}
static inline void divd(struct sux *cpu, uint64_t value, uint8_t opcode, uint8_t thread) {
- uint64_t sum = cpu->a[thread]/value;
+ uint64_t sum = cpu->a/value;
if (opcode != DAB) {
- cpu->b[thread] = cpu->a[thread] % value;
+ cpu->b = cpu->a % value;
} else {
- value = cpu->b[thread];
- cpu->x[thread] = cpu->a[thread] % value;
+ value = cpu->b;
+ cpu->x = cpu->a % value;
}
- cpu->a[thread] = sum;
+ cpu->a = sum;
setflag(sum == 0, Z);
setflag((sum >> 63), N);
}
@@ -366,19 +354,7 @@ static inline void idm(struct sux *cpu, uint64_t address, uint8_t prefix, uint8_
union reg value;
value.u64 = 0;
/* Unroll Loop by implementing Duff's Device. */
- value.u8[0] = addr[address];
- switch (1 << (prefix >> 4)) {
- case 8:
- value.u8[7] = addr[address+7];
- value.u8[6] = addr[address+6];
- value.u8[5] = addr[address+5];
- value.u8[4] = addr[address+4];
- case 4:
- value.u8[3] = addr[address+3];
- value.u8[2] = addr[address+2];
- case 2:
- value.u8[1] = addr[address+1];
- }
+ setreg_sw(value.u8, 0, addr, address, prefix, 0, RS);
if (inc) {
value.u64++;
} else {
@@ -386,40 +362,15 @@ static inline void idm(struct sux *cpu, uint64_t address, uint8_t prefix, uint8_
}
setflag(value.u64 == 0, Z);
setflag(value.u64 >> 7, N);
- addr[address] = value.u8[0];
+ setreg_sw(addr, address, value.u8, 0, prefix, 0, RS);
io(address, 0);
- switch (1 << (prefix >> 4)) {
- case 8:
- addr[address+7] = value.u8[7];
- addr[address+6] = value.u8[6];
- addr[address+5] = value.u8[5];
- addr[address+4] = value.u8[4];
- case 4:
- addr[address+3] = value.u8[3];
- addr[address+2] = value.u8[2];
- case 2:
- addr[address+1] = value.u8[1];
- }
}
static inline uint64_t load(struct sux *cpu, uint64_t address, uint64_t reg, uint8_t prefix, uint8_t thread) {
io(address, 1);
union reg value;
value.u64 = reg;
- /* Unroll Loop by implementing Duff's Device. */
- value.u8[0] = addr[address];
- switch (1 << (prefix >> 4)) {
- case 8:
- value.u8[7] = addr[address+7];
- value.u8[6] = addr[address+6];
- value.u8[5] = addr[address+5];
- value.u8[4] = addr[address+4];
- case 4:
- value.u8[3] = addr[address+3];
- value.u8[2] = addr[address+2];
- case 2:
- value.u8[1] = addr[address+1];
- }
+ setreg_sw(value.u8, 0, addr, address, prefix, 0, RS);
setflag(value.u64 == 0, Z);
setflag(value.u64 >> 63, N);
return value.u64;
@@ -428,7 +379,7 @@ static inline uint64_t load(struct sux *cpu, uint64_t address, uint64_t reg, uin
static inline void store(struct sux *cpu, uint64_t address, uint64_t reg, uint8_t prefix, uint8_t thread) {
union reg value;
value.u64 = reg;
- addr[address] = value.u8[0];
+ setreg_sw(addr, address, value.u8, 0, prefix, 0, RS);
#if (IO || debug) && !branch
#if keypoll
pthread_mutex_lock(&mutex);
@@ -438,18 +389,5 @@ static inline void store(struct sux *cpu, uint64_t address, uint64_t reg, uint8_
pthread_mutex_unlock(&mutex);
#endif
#endif
- /* Unroll Loop by implementing Duff's Device. */
- switch (1 << (prefix >> 4)) {
- case 8:
- addr[address+7] = value.u8[7];
- addr[address+6] = value.u8[6];
- addr[address+5] = value.u8[5];
- addr[address+4] = value.u8[4];
- case 4:
- addr[address+3] = value.u8[3];
- addr[address+2] = value.u8[2];
- case 2:
- addr[address+1] = value.u8[1];
- }
}