Permalink
Browse files

clean up

  • Loading branch information...
BonsaiDen committed May 21, 2011
1 parent 759b73e commit aa8409c524780d2866e3f9f50591aee85ca33b17
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,53 +30,80 @@ 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);
}
}
-/* 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
Oops, something went wrong.

0 comments on commit aa8409c

Please sign in to comment.