Skip to content
Browse files

clean up

  • Loading branch information...
1 parent 759b73e commit aa8409c524780d2866e3f9f50591aee85ca33b17 @BonsaiDen committed
Showing with 672 additions and 314 deletions.
  1. +0 −208 codes.c
  2. +58 −33 cpu.c
  3. +7 −10 cpu.h
  4. +0 −51 helpers.c
  5. +99 −0 inst_names.h
  6. BIN main
  7. +23 −7 main.c
  8. +41 −0 mmu.c
  9. +13 −0 mmu.h
  10. +197 −0 old/codes.c
  11. 0 { → old}/old.c
  12. +45 −0 ops.c
  13. +12 −5 map.c → ops.h
  14. +83 −0 ops_16bit.c
  15. +94 −0 ops_8bit.c
View
208 codes.c
@@ -1,208 +0,0 @@
-
-#include <stdlib.h>
-#include <stdio.h>
-#include "cpu.h"
-
-/* Op Codes ----------------------------------------------------------------- */
-static void NOP(CPU *c, uint8_t *mem) { }
-
-
-/* Move --------------------------------------------------------------------- */
-#define MVI_8(REG) static void MVI_##REG(CPU *c, uint8_t *mem) { \
- *c->REG = *mem; \
-}
-
-MVI_8(A);
-MVI_8(B);
-MVI_8(C);
-MVI_8(D);
-MVI_8(E);
-MVI_8(H);
-MVI_8(L);
-
-static void MVI_M(CPU *c, uint8_t *mem) {
- *c->HL = *mem;
-}
-
-
-/* Load --------------------------------------------------------------------- */
-#define LXI(REG, RE) static void LXI_##REG(CPU *c, uint8_t *mem) { \
- *c->RE = read16(mem); \
-}
-
-LXI(B, BC);
-LXI(D, DE);
-LXI(H, HL);
-LXI(SP, SP);
-
-static void LDAX_B(CPU *c, uint8_t *mem) {
- *c->A = *c->BC;
-}
-
-static void LDAX_D(CPU *c, uint8_t *mem) {
- *c->A = *c->DE;
-}
-
-
-// Memory
-static void LHLD(CPU *c, uint8_t *mem) {
-
- // use c->mem here bcause *mem is offseted with the PC
- uint16_t p = read16(mem);
- *c->HL = read16(&c->mem[p]);
-
-}
-
-
-/* Store -------------------------------------------------------------------- */
-static void STAX_B(CPU *c, uint8_t *mem) {
- *c->BC = *c->A;
-}
-
-static void STAX_D(CPU *c, uint8_t *mem) {
- *c->DE = *c->A;
-}
-
-
-// Memory
-static void SHLD(CPU *c, uint8_t *mem) {
-
- // use c->mem here bcause *mem is offseted with the PC
- uint16_t p = read16(mem);
- write16(&c->mem[p], c->HL);
-
-}
-
-// just here to make gcc compile
-static void DAA(CPU *c, uint8_t *mem) {
-
-}
-
-static void STA(CPU *c, uint8_t *mem) {
-
-}
-
-static void STC(CPU *c, uint8_t *mem) {
-
-}
-
-static void LDA(CPU *c, uint8_t *mem) {
-
-}
-
-static void CMC(CPU *c, uint8_t *mem) {
-
-}
-static void CMA(CPU *c, uint8_t *mem) {
-
-}
-
-
-/* Increment ---------------------------------------------------------------- */
-#define INX_16(REG, RE) static void INX_##REG(CPU *c, uint8_t *mem) { \
- (*c->RE)++; \
-}
-
-INX_16(B, BC);
-INX_16(D, DE);
-INX_16(H, HL);
-INX_16(SP, SP);
-
-// 8 bit
-#define INR_8(REG) static void INR_##REG(CPU *c, uint8_t *mem) { \
- (*c->REG)++; \
- check_szap_inc(c, (uint16_t*)c->REG); \
-}
-
-INR_8(A);
-INR_8(B);
-INR_8(C);
-INR_8(D);
-INR_8(E);
-INR_8(H);
-INR_8(L);
-
-// 16 bit
-static void INR_M(CPU *c, uint8_t *mem) {
- (*c->HL)++;
- check_szap_inc(c, c->HL);
-}
-
-
-/* Decrement ---------------------------------------------------------------- */
-#define DCX_16(REG, RE) static void DCX_##REG(CPU *c, uint8_t *mem) { \
- (*c->RE)--; \
-}
-
-DCX_16(B, BC);
-DCX_16(D, DE);
-DCX_16(H, HL);
-DCX_16(SP, SP);
-
-// 8 bit
-#define DCR_8(REG) static void DCR_##REG(CPU *c, uint8_t *mem) { \
- (*c->REG)--; \
- check_szap_dec(c, (uint16_t*)c->REG); \
-}
-
-DCR_8(A);
-DCR_8(B);
-DCR_8(C);
-DCR_8(D);
-DCR_8(E);
-DCR_8(H);
-DCR_8(L);
-
-
-// 16 bit
-#define DCR_16(REG, RE) static void DCR_##REG(CPU *c, uint8_t *mem) { \
- (*c->RE)--; \
- check_szap_dec(c, c->RE); \
-}
-
-DCR_16(M, HL);
-
-
-/* Rotate ------------------------------------------------------------------- */
-static void RLC(CPU *c, uint8_t *mem) {
- // reg C left, check carry...
-}
-
-static void RRC(CPU *c, uint8_t *mem) {
- // reg C right
-}
-
-static void RAL(CPU *c, uint8_t *mem) {
- // reg A left
-}
-
-static void RAR(CPU *c, uint8_t *mem) {
- // reg A right
-}
-
-
-/* Double Add --------------------------------------------------------------- */
-static void DAD_B(CPU *c, uint8_t *mem) {
- uint32_t i = *c->HL + *c->BC;
- *c->F = i > 0xffff;
- *c->HL = (i & 0xffff);
-}
-
-static void DAD_D(CPU *c, uint8_t *mem) {
- uint32_t i = *c->HL + *c->DE;
- *c->F = i > 0xffff;
- *c->HL = (i & 0xffff);
-}
-
-static void DAD_H(CPU *c, uint8_t *mem) {
- uint32_t i = *c->HL + *c->HL;
- *c->F = i > 0xffff;
- *c->HL = (i & 0xffff);
-}
-
-static void DAD_SP(CPU *c, uint8_t *mem) {
- uint32_t i = *c->HL + *c->SP;
- *c->F = i > 0xffff;
- *c->HL = (i & 0xffff);
-}
-
View
91 cpu.c
@@ -3,20 +3,18 @@
#include <stdio.h>
#include "cpu.h"
+#include "ops.h"
-#include "helpers.c"
-#include "map.c"
+#include "inst_names.h"
+extern CPU_8080 *CPU;
+extern uint8_t *MEMORY;
-/* Definitions -------------------------------------------------------------- */
-static void run(CPU *cpu);
-static void dump(CPU *cpu, uint8_t inst);
-
-/* Constructor -------------------------------------------------------------- */
-CPU *cpu(uint8_t *mem) {
+/* Create ------------------------------------------------------------------- */
+CPU_8080 *cpu_create() {
- CPU *cpu = calloc(1, sizeof(CPU));
+ CPU_8080 *cpu = calloc(1, sizeof(CPU_8080));
cpu->A = (uint8_t*)&cpu->reg[1]; cpu->F = (uint8_t*)&cpu->reg[0];
cpu->B = (uint8_t*)&cpu->reg[3]; cpu->C = (uint8_t*)&cpu->reg[2];
@@ -32,42 +30,38 @@ CPU *cpu(uint8_t *mem) {
cpu->PC = (uint16_t*)&cpu->reg[10];
cpu->cycles = 0;
- cpu->mem = mem;
- cpu->run = &run;
return cpu;
}
-/* Control ------------------------------------------------------------------ */
-static void run(CPU *c) {
+/* Main --------------------------------------------------------------------- */
+void cpu_run() {
+
+ cpu_dump(0);
while(1) {
// fetch op code and increase PC by 1
- uint8_t *inst = &c->mem[(*c->PC)++];
+ uint8_t *inst = &MEMORY[(*CPU->PC)++];
if (*inst > 255) {
printf("invalid op code: %d\n", *inst);
break;
}
- // resolve op code function pointer...
- opCode *func = (opCode*)&OP_CODES[*inst * 3 + 2];
- (*func)(c, &c->mem[(*c->PC)]);
-
- // increase PC by op size - 1
- (*c->PC) += (OP_CODES[*inst * 3] - 1);
-
- // add min cycle count
- c->cycles += OP_CODES[*inst * 3 + 1];
+ OPC *func = (OPC*)&OP_CODES[*inst * 3 + 2]; // call op func
+ (*func)();
+
+ (*CPU->PC) += (OP_CODES[*inst * 3] - 1); // increase PC by op size - 1
+ CPU->cycles += OP_CODES[*inst * 3 + 1]; // add min cycle count
// exit on NOP
if (*inst == 0) {
break;
}
- dump(c, *inst);
+ cpu_dump(*inst);
usleep(1000000);
}
@@ -75,10 +69,41 @@ static void run(CPU *c) {
}
-/* Debugging ---------------------------------------------------------------- */
-static void dump(CPU *cpu, uint8_t inst) {
+/* Flags -------------------------------------------------------------------- */
+static const unsigned int ParityTable256[256] = {
+# define P2(n) n, n^1, n^1, n
+# define P4(n) P2(n), P2(n^1), P2(n^1), P2(n)
+# define P6(n) P4(n), P4(n^1), P4(n^1), P4(n)
+ P6(0), P6(1), P6(1), P6(0)
+};
+
+inline cpu_flag_szap_inc(uint16_t *r) {
+
+ (*CPU->F) = 0; // clear flags
+
+ if (!(*r & 128)) (*CPU->F) |= 128; // set SIGN flag
+ if (*r == 0) (*CPU->F) |= 64; // set ZERO flag
+ if (!(*r & 15 && *r & 31)) (*CPU->F) |= 16; // set AUXILLARY CARRY flag
+ if (ParityTable256[*r]) (*CPU->F) |= 4; // set PARITY flag
+
+}
+
+inline void cpu_flag_szap_dec(uint16_t *r) {
+
+ (*CPU->F) = 0; // clear flags
+
+ if (!(*r & 128)) (*CPU->F) |= 128; // set SIGN flag
+ if (*r == 0) (*CPU->F) |= 64; // set ZERO flag
+ if (!(*r & 7 && *r & 15)) (*CPU->F) |= 16; // set AUXILLARY CARRY flag
+ if (ParityTable256[*r]) (*CPU->F) |= 4; // set PARITY flag
+
+}
+
+
+/* Debug -------------------------------------------------------------------- */
+void cpu_dump(uint8_t inst) {
- uint8_t flags = *cpu->F;
+ uint8_t flags = *CPU->F;
printf("\n (F) %c %c %c %c %c %c %c %c\n",
(flags & 128) ? 'S' : '-',
@@ -90,12 +115,12 @@ static void dump(CPU *cpu, uint8_t inst) {
(flags & 2) ? '1' : ' ',
(flags & 1) ? 'C' : '-' );
- printf(" (A) %03d (PSW) %05d (OP) %02x\n", *cpu->A, *cpu->PSW, inst);
- printf(" (B) %03d (C) %03d (BC) %05d\n", *cpu->B, *cpu->C, *cpu->BC);
- printf(" (D) %03d (E) %03d (DE) %05d\n", *cpu->D, *cpu->E, *cpu->DE);
- printf(" (H) %03d (L) %03d (HL) %05d\n", *cpu->H, *cpu->L, *cpu->HL);
- printf(" (SP) %05d (PC) %05d\n", *cpu->SP, *cpu->PC);
- printf("(CYC) %17lu\n", cpu->cycles);
+ printf(" (A) %03d (PSW) %05d (OP) %-5s(%02x)\n", *CPU->A, *CPU->PSW, INST_NAMES[inst], inst);
+ printf(" (B) %03d (C) %03d (BC) %05d\n", *CPU->B, *CPU->C, *CPU->BC);
+ printf(" (D) %03d (E) %03d (DE) %05d\n", *CPU->D, *CPU->E, *CPU->DE);
+ printf(" (H) %03d (L) %03d (HL) %05d\n", *CPU->H, *CPU->L, *CPU->HL);
+ printf(" (SP) %05d (PC) %05d\n", *CPU->SP, *CPU->PC);
+ printf("(CYC) %17lu\n", CPU->cycles);
// fputs("\x1b[2J", stdout);
//
View
17 cpu.h
@@ -4,9 +4,7 @@
#include <stdint.h>
-
-/* Structs ------------------------------------------------------------------ */
-typedef struct CPU {
+typedef struct CPU_8080 {
uint8_t reg[12];
@@ -29,16 +27,15 @@ typedef struct CPU {
uint16_t *SP;
uint16_t *PC;
- uint8_t *mem;
unsigned long cycles;
- void (*run)(struct CPU *cpu);
-
-} CPU;
-
+} CPU_8080;
-/* Constructor -------------------------------------------------------------- */
-CPU *cpu(uint8_t *mem);
+CPU_8080 *cpu_create();
+inline cpu_flag_szap_inc(uint16_t *r);
+inline void cpu_flag_szap_dec(uint16_t *r);
+void cpu_dump(uint8_t inst);
+void cpu_run();
#endif
View
51 helpers.c
@@ -1,51 +0,0 @@
-
-#include <stdlib.h>
-#include "cpu.h"
-
-
-/* Lookups ------------------------------------------------------------------ */
-static const unsigned int ParityTable256[256] = {
-# define P2(n) n, n^1, n^1, n
-# define P4(n) P2(n), P2(n^1), P2(n^1), P2(n)
-# define P6(n) P4(n), P4(n^1), P4(n^1), P4(n)
- P6(0), P6(1), P6(1), P6(0)
-};
-
-
-/* 16 bit helpers ----------------------------------------------------------- */
-static inline uint16_t read16(uint8_t *mem) {
- return *mem << 8 | *(mem + 1);
- // TODO implement page protection
-
-// return (*(uint16_t*)value << 8) | (*(uint16_t*)value >> (16 - 8));
-}
-
-static inline void write16(uint8_t *mem, uint16_t *r) {
- *mem = *r >> 8;
- *(mem + 1) = *r & 0xff;
-}
-
-
-/* Flag helpers ------------------------------------------------------------- */
-static inline void check_szap_inc(CPU *c, uint16_t *r) {
-
- (*c->F) = 0; // clear flags
-
- if (!(*r & 128)) (*c->F) |= 128; // set SIGN flag
- if (*r == 0) (*c->F) |= 64; // set ZERO flag
- if (!(*r & 15 && *r & 31)) (*c->F) |= 16; // set AUXILLARY CARRY flag
- if (ParityTable256[*r]) (*c->F) |= 4; // set PARITY flag
-
-}
-
-static inline void check_szap_dec(CPU *c, uint16_t *r) {
-
- (*c->F) = 0; // clear flags
-
- if (!(*r & 128)) (*c->F) |= 128; // set SIGN flag
- if (*r == 0) (*c->F) |= 64; // set ZERO flag
- if (!(*r & 7 && *r & 15)) (*c->F) |= 16; // set AUXILLARY CARRY flag
- if (ParityTable256[*r]) (*c->F) |= 4; // set PARITY flag
-
-}
-
View
99 inst_names.h
@@ -0,0 +1,99 @@
+const char *INST_NAMES[256] = {
+
+ // x0 - x7
+ "NOP",
+ "LXI_B",
+ "STAX_B",
+ "INX_B",
+
+ "INR_B",
+ "DCR_B",
+ "MVI_B",
+ "RLC",
+
+ // x8 - xF
+ "NOP_8",
+ "DAD_B",
+ "LDAX_B",
+ "DCX_B",
+
+ "INR_C",
+ "DCR_C",
+ "MVI_C",
+ "RRC",
+
+ // -----------------------
+
+ // 10 - 17
+ "NOP_10",
+ "LXI_D",
+ "STAX_D",
+ "INX_D",
+
+ "INR_D",
+ "DCR_D",
+ "MVI_D",
+ "RAL",
+
+ // 18 - 1F
+ "NOP_18",
+ "DAD_D",
+ "LDAX_D",
+ "DCX_D",
+
+ "INR_E",
+ "DCR_E",
+ "MVI_E",
+ "RAR",
+
+ // -----------------------
+
+ // 20 - 27
+ "NOP_20",
+ "LXI_H",
+ "SHLD",
+ "INX_H",
+
+ "INR_H",
+ "DCR_H",
+ "MVI_H",
+ "DAA",
+
+ // 28 - 2f
+ "NOP_28",
+ "DAD_H",
+ "LHLD",
+ "DCX_H",
+
+ "INR_L",
+ "DCR_L",
+ "MVI_L",
+ "CMA",
+
+ // -----------------------
+
+ // 30 - 37
+ "NOP_30",
+ "LXI_SP",
+ "STA",
+ "INX_SP",
+
+ "INR_M",
+ "DCR_M",
+ "MVI_M",
+ "STC",
+
+ // 38 - 3f
+ "NOP_38",
+ "DAD_SP",
+ "LDA",
+ "DCX_SP",
+
+ "INR_A",
+ "DCR_A",
+ "MVI_A",
+ "CMC"
+};
+
+
+
View
BIN main
Binary file not shown.
View
30 main.c
@@ -3,15 +3,28 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
+#include <string.h>
+
+uint8_t PAGE;
+
+struct CPU_8080 *CPU;
#include "cpu.h"
+#include "mmu.h"
#include "inst.h"
+uint8_t *MEMORY;
+
int main(int argc, const char *argv[]) {
+
+ MEMORY = calloc(65536, sizeof(uint8_t));
- // load from mem, and do some stuff
- uint8_t mem[65536] = {
+ // load from mem, and do some stuf
+ //
+ //
+ //
+ uint8_t TEST[65536] = {
LHLD, 0, 100,
LXI_B, 0, 42,
@@ -27,12 +40,15 @@ int main(int argc, const char *argv[]) {
NOP
};
- mem[100] = 128;
- mem[101] = 255;
+ TEST[100] = 128;
+ TEST[101] = 255;
+
+ memcpy(MEMORY, TEST, sizeof(TEST));
+
+ CPU = cpu_create();
+ cpu_run();
- CPU *core = cpu(mem);
- core->run(core);
- free(core);
+ free(CPU);
return 0;
}
View
41 mmu.c
@@ -0,0 +1,41 @@
+
+#include "mmu.h"
+#include "cpu.h"
+
+#include <stdio.h>
+
+extern uint8_t *MEMORY;
+extern uint8_t PAGE;
+extern CPU_8080 *CPU;
+
+inline uint8_t read8(uint16_t addr) {
+ uint16_t val = MEMORY[addr];
+ #ifdef DEBUG_MEM
+ printf("MEM (read8) @ %d > %d\n", addr, val);
+ #endif
+ return val;
+}
+
+inline uint16_t read16(uint16_t addr) {
+ uint16_t val = MEMORY[addr] << 8 | MEMORY[addr + 1];
+ #ifdef DEBUG_MEM
+ printf("MEM (read16) @ %d > %d\n", addr, val);
+ #endif
+ return val;
+}
+
+inline void write8(uint16_t addr, uint8_t *val) {
+ #ifdef DEBUG_MEM
+ printf("MEM (write8) @ %d > %d\n", addr, *val);
+ #endif
+ MEMORY[addr] = *val;
+}
+
+inline void write16(uint16_t addr, uint16_t *val) {
+ #ifdef DEBUG_MEM
+ printf("MEM (write16) @ %d > %d\n", addr, *val);
+ #endif
+ MEMORY[addr] = *val >> 8;
+ MEMORY[addr + 1] = *val & 0xff;
+}
+
View
13 mmu.h
@@ -0,0 +1,13 @@
+
+#ifndef MEMORY_H
+#define MEMORY_H
+
+#include <stdint.h>
+
+inline uint8_t read8(uint16_t addr);
+inline uint16_t read16(uint16_t addr);
+inline void write8(uint16_t addr, uint8_t *val);
+inline void write16(uint16_t addr, uint16_t *val);
+
+#endif
+
View
197 old/codes.c
@@ -0,0 +1,197 @@
+
+#include <stdio.h>
+#include "cpu.h"
+
+extern uint8_t *MEMORY;
+extern CPU_8080 *CPU;
+
+/* Others ------------------------------------------------------------------- */
+static void NOP(uint8_t *mem) { };
+
+
+/* Move --------------------------------------------------------------------- */
+#define MVI_8(REG) static void MVI_##REG(uint8_t *mem) { \
+ *CPU->REG = *mem; \
+}
+
+MVI_8(A); MVI_8(B); MVI_8(C); MVI_8(D);
+MVI_8(E); MVI_8(H); MVI_8(L);
+
+static void MVI_M(uint8_t *mem) {
+ *CPU->HL = *mem;
+}
+
+
+/* Load --------------------------------------------------------------------- */
+#define LXI(REG, RE) static void LXI_##REG(uint8_t *mem) { \
+ *CPU->RE = read16(mem); \
+}
+
+LXI(B, BC);
+LXI(D, DE);
+LXI(H, HL);
+LXI(SP, SP);
+
+static void LDAX_B(uint8_t *mem) {
+ *CPU->A = *CPU->BC;
+}
+
+static void LDAX_D(uint8_t *mem) {
+ *CPU->A = *CPU->DE;
+}
+
+
+// Memory
+static void LHLD(uint8_t *mem) {
+ *CPU->HL = read16(read16(*mem));
+}
+
+
+/* Store -------------------------------------------------------------------- */
+static void STAX_B(uint8_t *mem) {
+ *CPU->BC = *CPU->A;
+}
+
+static void STAX_D(uint8_t *mem) {
+ *CPU->DE = *CPU->A;
+}
+
+
+// Memory
+static void SHLD(uint8_t *mem) {
+ write16(read16(*mem), CPU->HL);
+}
+
+// just here to make gcc compile
+static void DAA(uint8_t *mem) {
+
+}
+
+static void STA(uint8_t *mem) {
+
+}
+
+static void STC(uint8_t *mem) {
+
+}
+
+static void LDA(uint8_t *mem) {
+
+}
+
+static void CMC(uint8_t *mem) {
+
+}
+static void CMA(uint8_t *mem) {
+
+}
+
+
+/* Increment ---------------------------------------------------------------- */
+#define INX_16(REG, RE) static void INX_##REG(uint8_t *mem) { \
+ (*CPU->RE)++; \
+}
+
+INX_16(B, BC);
+INX_16(D, DE);
+INX_16(H, HL);
+INX_16(SP, SP);
+
+// 8 bit
+#define INR_8(REG) static void INR_##REG(uint8_t *mem) { \
+ (*CPU->REG)++; \
+ cpu_flag_szap_inc((uint16_t*)CPU->REG); \
+}
+
+INR_8(A);
+INR_8(B);
+INR_8(C);
+INR_8(D);
+INR_8(E);
+INR_8(H);
+INR_8(L);
+
+// 16 bit
+static void INR_M(uint8_t *mem) {
+ (*CPU->HL)++;
+ cpu_flag_szap_inc(CPU->HL);
+}
+
+
+/* Decrement ---------------------------------------------------------------- */
+#define DCX_16(REG, RE) static void DCX_##REG(uint8_t *mem) { \
+ (*CPU->RE)--; \
+}
+
+DCX_16(B, BC);
+DCX_16(D, DE);
+DCX_16(H, HL);
+DCX_16(SP, SP);
+
+// 8 bit
+#define DCR_8(REG) static void DCR_##REG(uint8_t *mem) { \
+ (*CPU->REG)--; \
+ cpu_flag_szap_dec((uint16_t*)CPU->REG); \
+}
+
+DCR_8(A);
+DCR_8(B);
+DCR_8(C);
+DCR_8(D);
+DCR_8(E);
+DCR_8(H);
+DCR_8(L);
+
+
+// 16 bit
+#define DCR_16(REG, RE) static void DCR_##REG(uint8_t *mem) { \
+ (*CPU->RE)--; \
+ cpu_flag_szap_dec(CPU->RE); \
+}
+
+DCR_16(M, HL);
+
+
+/* Rotate ------------------------------------------------------------------- */
+static void RLC(uint8_t *mem) {
+ // reg C left, check carry...
+}
+
+static void RRC(uint8_t *mem) {
+ // reg C right
+}
+
+static void RAL(uint8_t *mem) {
+ // reg A left
+}
+
+static void RAR(uint8_t *mem) {
+ // reg A right
+}
+
+
+/* Double Add --------------------------------------------------------------- */
+static void DAD_B(uint8_t *mem) {
+ uint32_t i = *CPU->HL + *CPU->BC;
+ *CPU->F = i > 0xffff;
+ *CPU->HL = (i & 0xffff);
+}
+
+static void DAD_D(uint8_t *mem) {
+ uint32_t i = *CPU->HL + *CPU->DE;
+ *CPU->F = i > 0xffff;
+ *CPU->HL = (i & 0xffff);
+}
+
+static void DAD_H(uint8_t *mem) {
+ uint32_t i = *CPU->HL + *CPU->HL;
+ *CPU->F = i > 0xffff;
+ *CPU->HL = (i & 0xffff);
+}
+
+static void DAD_SP(uint8_t *mem) {
+ uint32_t i = *CPU->HL + *CPU->SP;
+ *CPU->F = i > 0xffff;
+ *CPU->HL = (i & 0xffff);
+}
+
View
0 old.c → old/old.c
File renamed without changes.
View
45 ops.c
@@ -0,0 +1,45 @@
+
+#include <stdio.h>
+#include "cpu.h"
+
+extern uint8_t *MEMORY;
+extern CPU_8080 *CPU;
+
+/* Others ------------------------------------------------------------------- */
+static void NOP() { }
+
+static void HLT() {
+ (*CPU->PC)--;
+}
+
+static void DAA(uint8_t *mem) {
+
+}
+
+static void STC(uint8_t *mem) {
+
+}
+
+static void CMC(uint8_t *mem) {
+
+}
+static void CMA(uint8_t *mem) {
+
+}
+
+static void RLC(uint8_t *mem) {
+ // reg C left, check carry...
+}
+
+static void RRC(uint8_t *mem) {
+ // reg C right
+}
+
+static void RAL(uint8_t *mem) {
+ // reg A left
+}
+
+static void RAR(uint8_t *mem) {
+ // reg A right
+}
+
View
17 map.c → ops.h
@@ -1,14 +1,19 @@
-/* Mappings ----------------------------------------------------------------- */
+#ifndef OP_CODES_H
+#define OP_CODES_H
-#include "codes.c"
+#include <stdint.h>
+#include "ops.c"
+#include "ops_16bit.c"
+#include "ops_8bit.c"
+
+typedef void (*OPC)();
#define op (unsigned int)&
-typedef void (*opCode)(CPU *c, uint8_t *mem);
-// byte size and min cycle length for op codes
-static unsigned int OP_CODES[768] = {
+// byte size /min cycle length / function
+unsigned int OP_CODES[768] = {
// x0 - x7
1, 4, op NOP,
@@ -107,4 +112,6 @@ static unsigned int OP_CODES[768] = {
};
+#undef op
+#endif
View
83 ops_16bit.c
@@ -0,0 +1,83 @@
+
+#include "cpu.h"
+
+extern uint8_t *MEMORY;
+extern CPU_8080 *CPU;
+
+
+// Transfer Instructions ------------------------------------------------------
+// ----------------------------------------------------------------------------
+
+
+// Read 16 REG ----------------------------------------------------------------
+#define LXI(REG, RE) static void LXI_##REG() { \
+ *CPU->RE = read16(*CPU->PC); \
+}
+
+LXI(B, BC);
+LXI(D, DE);
+LXI(H, HL);
+LXI(SP, SP);
+
+
+// Read / write HL ------------------------------------------------------------
+static void LHLD() {
+ *CPU->HL = read16(read16(*CPU->PC));
+}
+
+static void SHLD() {
+ write16(read16(*CPU->PC), CPU->HL);
+}
+
+
+// Double ADD -----------------------------------------------------------------
+static void DAD_B() {
+ uint32_t i = *CPU->HL + *CPU->BC;
+ *CPU->F = i > 0xffff;
+ *CPU->HL = (i & 0xffff);
+}
+
+static void DAD_D() {
+ uint32_t i = *CPU->HL + *CPU->DE;
+ *CPU->F = i > 0xffff;
+ *CPU->HL = (i & 0xffff);
+}
+
+static void DAD_H() {
+ uint32_t i = *CPU->HL + *CPU->HL;
+ *CPU->F = i > 0xffff;
+ *CPU->HL = (i & 0xffff);
+}
+
+static void DAD_SP() {
+ uint32_t i = *CPU->HL + *CPU->SP;
+ *CPU->F = i > 0xffff;
+ *CPU->HL = (i & 0xffff);
+}
+
+
+// Register Instructions ------------------------------------------------------
+// ----------------------------------------------------------------------------
+
+
+// Increment ------------------------------------------------------------------
+#define INX(REG, RE) static void INX_##REG(uint8_t *mem) { \
+ (*CPU->RE)++; \
+}
+
+INX(B, BC);
+INX(D, DE);
+INX(H, HL);
+INX(SP, SP);
+
+
+// Decrement ------------------------------------------------------------------
+#define DCX(REG, RE) static void DCX_##REG(uint8_t *mem) { \
+ (*CPU->RE)--; \
+}
+
+DCX(B, BC);
+DCX(D, DE);
+DCX(H, HL);
+DCX(SP, SP);
+
View
94 ops_8bit.c
@@ -0,0 +1,94 @@
+
+#include "cpu.h"
+
+extern uint8_t *MEMORY;
+extern CPU_8080 *CPU;
+
+
+// Transfer Instructions ------------------------------------------------------
+// ----------------------------------------------------------------------------
+
+
+// Load into A ----------------------------------------------------------------
+static void LDA() {
+ // TODO impelement
+}
+
+static void LDAX_B() {
+ *CPU->A = *CPU->BC;
+}
+
+static void LDAX_D() {
+ *CPU->A = *CPU->DE;
+}
+
+
+// Store into A ---------------------------------------------------------------
+static void STA(uint8_t *mem) {
+ // TODO impelement
+}
+
+static void STAX_B(uint8_t *mem) {
+ *CPU->BC = *CPU->A;
+}
+
+static void STAX_D(uint8_t *mem) {
+ *CPU->DE = *CPU->A;
+}
+
+
+// Move 8 REG -----------------------------------------------------------------
+#define MVI_8(REG) static void MVI_##REG() { \
+ *CPU->REG = read8(*CPU->PC); \
+}
+
+MVI_8(A); MVI_8(B); MVI_8(C); MVI_8(D);
+MVI_8(E); MVI_8(H); MVI_8(L);
+
+static void MVI_M() {
+ *CPU->HL = read8(*CPU->PC);
+}
+
+
+// Register Instructions ------------------------------------------------------
+// ----------------------------------------------------------------------------
+
+// Increment ------------------------------------------------------------------
+#define INR(REG) static void INR_##REG() { \
+ (*CPU->REG)++; \
+ cpu_flag_szap_inc((uint16_t*)CPU->REG); \
+}
+
+INR(A);
+INR(B);
+INR(C);
+INR(D);
+INR(E);
+INR(H);
+INR(L);
+
+static void INR_M(uint8_t *mem) {
+ (*CPU->HL)++;
+ cpu_flag_szap_inc(CPU->HL);
+}
+
+
+// Decrement ------------------------------------------------------------------
+#define DCR(REG) static void DCR_##REG() { \
+ (*CPU->REG)--; \
+ cpu_flag_szap_dec((uint16_t*)CPU->REG); \
+}
+
+DCR(A);
+DCR(B);
+DCR(C);
+DCR(D);
+DCR(E);
+DCR(H);
+DCR(L);
+
+static void DCR_M() {
+ (*CPU->HL)--;
+ cpu_flag_szap_dec(CPU->HL);
+}
+

0 comments on commit aa8409c

Please sign in to comment.
Something went wrong with that request. Please try again.