captmicro

CHIP8 .CPP

Sep 2nd, 2010
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.92 KB | None | 0 0
  1. #include "CHIP-8.h"
  2.  
  3. void CHIP8_LOADINTERP(CHIP8_CPU *cpu, UINT16 offset, void *data, UINT16 size)
  4. {
  5.     if (offset + size > 0x1FF) return;
  6.     memcpy(cpu->mem, data, size);
  7. }
  8.  
  9. void CHIP8_LOADPROG(CHIP8_CPU *cpu, void *data, UINT16 size)
  10. {
  11.     memcpy(cpu->mem + 0x200, data, size);
  12.     cpu->PROGSIZE = size;
  13.     cpu->PC = 0x200;
  14.     cpu->SP = 0;
  15. }
  16.  
  17. void CHIP8_STEP(CHIP8_CPU *cpu)
  18. {
  19.     if (cpu->PC >= cpu->PROGSIZE+0x200) { printf("***END OF PROGRAM***"); return; } /*END OF PROGRAM*/
  20.     UINT16 OPCODE = (cpu->mem[cpu->PC]<<8) + cpu->mem[cpu->PC + 1];
  21.     printf("INSTRUCTION = %04X\n", OPCODE);
  22.     printf("PC          =  %03X\nSP          =   %2X\n", cpu->PC, cpu->SP);
  23.  
  24.     BYTE OPCODE1 = OPCODE & 0xF000;
  25.     BYTE OPCODE2 = OPCODE & 0x0F00;
  26.     BYTE OPCODE3 = OPCODE & 0x00F0;
  27.     BYTE OPCODE4 = OPCODE & 0x000F;
  28.     printf("OPCODE: %X %X %X %X\n", OPCODE1, OPCODE2, OPCODE3, OPCODE4);
  29.     INT8 OPCODE34 = OPCODE & 0x00FF;
  30.     INT16 OPCODE234 = OPCODE & 0x0FFF;
  31.     if (OPCODE1 == 0x0) { //CHIP8_0***
  32.         if (OPCODE34 == 0xE0) CHIP8_00E0(cpu);
  33.         else if (OPCODE34 == 0xEE) CHIP8_00EE(cpu);
  34.     } else if (OPCODE1 == 0x1) { //CHIP8_1***
  35.         CHIP8_1NNN(cpu, OPCODE234);
  36.     } else if (OPCODE1 == 0x2) { //CHIP8_5***
  37.         CHIP8_2NNN(cpu, OPCODE234);
  38.     } else if (OPCODE1 == 0x3) { //CHIP8_3***
  39.         CHIP8_3XNN(cpu, OPCODE2, OPCODE34);
  40.     } else if (OPCODE1 == 0x4) { //CHIP8_4***
  41.         CHIP8_4XNN(cpu, OPCODE2, OPCODE34);
  42.     } else if (OPCODE1 == 0x5) { //CHIP8_5***
  43.         if(OPCODE4 == 0x0) CHIP8_5XY0(cpu, OPCODE2, OPCODE3);
  44.     } else if (OPCODE1 == 0x6) { //CHIP8_6***
  45.         CHIP8_6XNN(cpu, OPCODE2, OPCODE34);
  46.     } else if (OPCODE1 == 0x7) { //CHIP8_7***
  47.         CHIP8_7XNN(cpu, OPCODE2, OPCODE34);
  48.     } else if (OPCODE1 == 0x8) { //CHIP8_8***
  49.         if (OPCODE4 == 0x0) CHIP8_8XY0(cpu, OPCODE2, OPCODE3);
  50.         else if (OPCODE4 == 0x1) CHIP8_8XY1(cpu, OPCODE2, OPCODE3);
  51.         else if (OPCODE4 == 0x2) CHIP8_8XY2(cpu, OPCODE2, OPCODE3);
  52.         else if (OPCODE4 == 0x3) CHIP8_8XY3(cpu, OPCODE2, OPCODE3);
  53.         else if (OPCODE4 == 0x4) CHIP8_8XY4(cpu, OPCODE2, OPCODE3);
  54.         else if (OPCODE4 == 0x5) CHIP8_8XY5(cpu, OPCODE2, OPCODE3);
  55.         else if (OPCODE4 == 0x6) CHIP8_8XY6(cpu, OPCODE2, OPCODE3);
  56.         else if (OPCODE4 == 0x7) CHIP8_8XY7(cpu, OPCODE2, OPCODE3);
  57.         else if (OPCODE4 == 0xE) CHIP8_8XYE(cpu, OPCODE2, OPCODE3);
  58.     } else if (OPCODE1 == 0x9) { //CHIP8_9***
  59.         if (OPCODE4 == 0x0) CHIP8_9XY0(cpu, OPCODE2, OPCODE3);
  60.     } else if (OPCODE1 == 0xA) { //CHIP8_A***
  61.         CHIP8_ANNN(cpu, OPCODE234);
  62.     } else if (OPCODE1 == 0xB) { //CHIP8_B***
  63.         CHIP8_BNNN(cpu, OPCODE234);
  64.     } else if (OPCODE1 == 0xC) { //CHIP8_C***
  65.         CHIP8_CXNN(cpu, OPCODE2, OPCODE34);
  66.     } else if (OPCODE1 == 0xD) { //CHIP8_D***
  67.         CHIP8_DXYN(cpu, OPCODE2, OPCODE3, OPCODE4);
  68.     } else if (OPCODE1 == 0xE) { //CHIP8_E***
  69.         if (OPCODE34 == 0x9E) CHIP8_EX9E(cpu, OPCODE2);
  70.         else if (OPCODE34 == 0xA1) CHIP8_EXA1(cpu, OPCODE2);
  71.     } else if (OPCODE1 == 0xF) { //CHIP8_F***
  72.         if (OPCODE34 == 0x07) CHIP8_FX07(cpu, OPCODE2);
  73.         else if (OPCODE34 == 0x0A) CHIP8_FX0A(cpu, OPCODE2);
  74.         else if (OPCODE34 == 0x15) CHIP8_FX15(cpu, OPCODE2);
  75.         else if (OPCODE34 == 0x18) CHIP8_FX18(cpu, OPCODE2);
  76.         else if (OPCODE34 == 0x1E) CHIP8_FX1E(cpu, OPCODE2);
  77.         else if (OPCODE34 == 0x29) CHIP8_FX29(cpu, OPCODE2);
  78.         else if (OPCODE34 == 0x33) CHIP8_FX33(cpu, OPCODE2);
  79.         else if (OPCODE34 == 0x55) CHIP8_FX55(cpu, OPCODE2);
  80.         else if (OPCODE34 == 0x65) CHIP8_FX65(cpu, OPCODE2);
  81.     } else { /*unknown opcode*/ }
  82.  
  83.     cpu->PC += 2;
  84. }
  85.  
  86. void CHIP8_0NNN(C8, VNNN) { return; }
  87. void CHIP8_00E0(C8) { memset(cpu->display, 0, 64*32); }
  88. void CHIP8_00EE(C8) { cpu->PC = _CHIP8_POP(cpu); }
  89. void CHIP8_1NNN(C8, VNNN) { cpu->PC = NNN; }
  90. void CHIP8_2NNN(C8, VNNN) { _CHIP8_PUSH(cpu, cpu->PC+2); cpu->PC = NNN; }
  91. void CHIP8_3XNN(C8, VXNN) { if (cpu->V[X] == NN) cpu->PC += 2; }
  92. void CHIP8_4XNN(C8, VXNN) { if (cpu->V[X] != NN) cpu->PC += 2; }
  93. void CHIP8_5XY0(C8, VXY) { if (cpu->V[X] == cpu->V[Y]) cpu->PC += 2; }
  94. void CHIP8_6XNN(C8, VXNN) { cpu->V[X] = NN; }
  95. void CHIP8_7XNN(C8, VXNN) { cpu->V[X] += NN; }
  96. void CHIP8_8XY0(C8, VXY) { cpu->V[X] = cpu->V[Y]; }
  97. void CHIP8_8XY1(C8, VXY) { cpu->V[X] = cpu->V[X] | cpu->V[Y]; }
  98. void CHIP8_8XY2(C8, VXY) { cpu->V[X] = cpu->V[X] & cpu->V[Y]; }
  99. void CHIP8_8XY3(C8, VXY) { cpu->V[X] = cpu->V[X] ^ cpu->V[Y]; }
  100. void CHIP8_8XY4(C8, VXY) { cpu->V[X] += cpu->V[Y]; cpu->V[0xF] = (cpu->V[X] < cpu->V[Y]); }
  101. void CHIP8_8XY5(C8, VXY) { cpu->V[X] -= cpu->V[Y]; cpu->V[0xF] = /*borrow*/0; }
  102. void CHIP8_8XY6(C8, VXY) { cpu->V[0xF] = /*LSB*/0; cpu->V[X] = cpu->V[X] >> 1; }
  103. void CHIP8_8XY7(C8, VXY) { cpu->V[X] = cpu->V[Y] - cpu->V[X]; cpu->V[0xF] = /*borrow*/0; }
  104. void CHIP8_8XYE(C8, VXY) { cpu->V[0xF] = /*MSB*/0; cpu->V[X] = cpu->V[X] << 1; }
  105. void CHIP8_9XY0(C8, VXY) { if (cpu->V[X] != cpu->V[Y]) cpu->PC += 2; }
  106. void CHIP8_ANNN(C8, VNNN) { cpu->I = NNN; }
  107. void CHIP8_BNNN(C8, VNNN) { cpu->PC = NNN + cpu->V[0x0]; }
  108. void CHIP8_CXNN(C8, VXNN) { cpu->V[X] = (/*rand*/0 & NN); }
  109. void CHIP8_DXYN(C8, VXYN) { cpu->V[0xF] = CHIP8_DRAWSPRITE(X, Y, cpu->mem + cpu->I, N); }
  110. void CHIP8_EX9E(C8, VX) { if (cpu->input[X] == 0xFF) cpu->PC += 2; }
  111. void CHIP8_EXA1(C8, VX) { if (cpu->input[X] == 0x00) cpu->PC += 2; }
  112. void CHIP8_FX07(C8, VX) { cpu->V[X] = cpu->delaytimer; }
  113. void CHIP8_FX0A(C8, VX) { BYTE i = 0; for (i = 0; i < 0xF; i++) if (cpu->input[i] == 0xFF) cpu->V[X] = i; }
  114. void CHIP8_FX15(C8, VX) { cpu->delaytimer = cpu->V[X]; }
  115. void CHIP8_FX18(C8, VX) { cpu->soundtimer = cpu->V[X]; }
  116. void CHIP8_FX1E(C8, VX) { cpu->I += cpu->V[X]; }
  117. void CHIP8_FX29(C8, VX) { cpu->I = 0x000 + (cpu->V[X] * 5); }
  118. void CHIP8_FX33(C8, VX) { /*not implemented*/return; }
  119. void CHIP8_FX55(C8, VX) { BYTE i = 0; for (i = 0; i < 0xF; i++) *(cpu->mem + cpu->I + i) = cpu->V[i]; }
  120. void CHIP8_FX65(C8, VX) { BYTE i = 0; for (i = 0; i < 0xF; i++) cpu->V[i] = *(cpu->mem + cpu->I + i); }
  121.  
  122. void _CHIP8_PUSH(CHIP8_CPU *cpu, UINT16 val)
  123. {
  124.     if (cpu->SP == 16) return; //stack full
  125.     cpu->stack[cpu->SP++] = val;
  126. }
  127.  
  128. INT16 _CHIP8_POP(CHIP8_CPU *cpu)
  129. {
  130.     if (cpu->SP == 0) return 0x0000; //stack is empty
  131.     return cpu->stack[--cpu->SP];
  132. }
Add Comment
Please, Sign In to add comment