Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CHIP-8.h"
- void CHIP8_LOADINTERP(CHIP8_CPU *cpu, UINT16 offset, void *data, UINT16 size)
- {
- if (offset + size > 0x1FF) return;
- memcpy(cpu->mem, data, size);
- }
- void CHIP8_LOADPROG(CHIP8_CPU *cpu, void *data, UINT16 size)
- {
- memcpy(cpu->mem + 0x200, data, size);
- cpu->PROGSIZE = size;
- cpu->PC = 0x200;
- cpu->SP = 0;
- }
- void CHIP8_STEP(CHIP8_CPU *cpu)
- {
- if (cpu->PC >= cpu->PROGSIZE+0x200) { printf("***END OF PROGRAM***"); return; } /*END OF PROGRAM*/
- UINT16 OPCODE = (cpu->mem[cpu->PC]<<8) + cpu->mem[cpu->PC + 1];
- printf("INSTRUCTION = %04X\n", OPCODE);
- printf("PC = %03X\nSP = %2X\n", cpu->PC, cpu->SP);
- BYTE OPCODE1 = OPCODE & 0xF000;
- BYTE OPCODE2 = OPCODE & 0x0F00;
- BYTE OPCODE3 = OPCODE & 0x00F0;
- BYTE OPCODE4 = OPCODE & 0x000F;
- printf("OPCODE: %X %X %X %X\n", OPCODE1, OPCODE2, OPCODE3, OPCODE4);
- INT8 OPCODE34 = OPCODE & 0x00FF;
- INT16 OPCODE234 = OPCODE & 0x0FFF;
- if (OPCODE1 == 0x0) { //CHIP8_0***
- if (OPCODE34 == 0xE0) CHIP8_00E0(cpu);
- else if (OPCODE34 == 0xEE) CHIP8_00EE(cpu);
- } else if (OPCODE1 == 0x1) { //CHIP8_1***
- CHIP8_1NNN(cpu, OPCODE234);
- } else if (OPCODE1 == 0x2) { //CHIP8_5***
- CHIP8_2NNN(cpu, OPCODE234);
- } else if (OPCODE1 == 0x3) { //CHIP8_3***
- CHIP8_3XNN(cpu, OPCODE2, OPCODE34);
- } else if (OPCODE1 == 0x4) { //CHIP8_4***
- CHIP8_4XNN(cpu, OPCODE2, OPCODE34);
- } else if (OPCODE1 == 0x5) { //CHIP8_5***
- if(OPCODE4 == 0x0) CHIP8_5XY0(cpu, OPCODE2, OPCODE3);
- } else if (OPCODE1 == 0x6) { //CHIP8_6***
- CHIP8_6XNN(cpu, OPCODE2, OPCODE34);
- } else if (OPCODE1 == 0x7) { //CHIP8_7***
- CHIP8_7XNN(cpu, OPCODE2, OPCODE34);
- } else if (OPCODE1 == 0x8) { //CHIP8_8***
- if (OPCODE4 == 0x0) CHIP8_8XY0(cpu, OPCODE2, OPCODE3);
- else if (OPCODE4 == 0x1) CHIP8_8XY1(cpu, OPCODE2, OPCODE3);
- else if (OPCODE4 == 0x2) CHIP8_8XY2(cpu, OPCODE2, OPCODE3);
- else if (OPCODE4 == 0x3) CHIP8_8XY3(cpu, OPCODE2, OPCODE3);
- else if (OPCODE4 == 0x4) CHIP8_8XY4(cpu, OPCODE2, OPCODE3);
- else if (OPCODE4 == 0x5) CHIP8_8XY5(cpu, OPCODE2, OPCODE3);
- else if (OPCODE4 == 0x6) CHIP8_8XY6(cpu, OPCODE2, OPCODE3);
- else if (OPCODE4 == 0x7) CHIP8_8XY7(cpu, OPCODE2, OPCODE3);
- else if (OPCODE4 == 0xE) CHIP8_8XYE(cpu, OPCODE2, OPCODE3);
- } else if (OPCODE1 == 0x9) { //CHIP8_9***
- if (OPCODE4 == 0x0) CHIP8_9XY0(cpu, OPCODE2, OPCODE3);
- } else if (OPCODE1 == 0xA) { //CHIP8_A***
- CHIP8_ANNN(cpu, OPCODE234);
- } else if (OPCODE1 == 0xB) { //CHIP8_B***
- CHIP8_BNNN(cpu, OPCODE234);
- } else if (OPCODE1 == 0xC) { //CHIP8_C***
- CHIP8_CXNN(cpu, OPCODE2, OPCODE34);
- } else if (OPCODE1 == 0xD) { //CHIP8_D***
- CHIP8_DXYN(cpu, OPCODE2, OPCODE3, OPCODE4);
- } else if (OPCODE1 == 0xE) { //CHIP8_E***
- if (OPCODE34 == 0x9E) CHIP8_EX9E(cpu, OPCODE2);
- else if (OPCODE34 == 0xA1) CHIP8_EXA1(cpu, OPCODE2);
- } else if (OPCODE1 == 0xF) { //CHIP8_F***
- if (OPCODE34 == 0x07) CHIP8_FX07(cpu, OPCODE2);
- else if (OPCODE34 == 0x0A) CHIP8_FX0A(cpu, OPCODE2);
- else if (OPCODE34 == 0x15) CHIP8_FX15(cpu, OPCODE2);
- else if (OPCODE34 == 0x18) CHIP8_FX18(cpu, OPCODE2);
- else if (OPCODE34 == 0x1E) CHIP8_FX1E(cpu, OPCODE2);
- else if (OPCODE34 == 0x29) CHIP8_FX29(cpu, OPCODE2);
- else if (OPCODE34 == 0x33) CHIP8_FX33(cpu, OPCODE2);
- else if (OPCODE34 == 0x55) CHIP8_FX55(cpu, OPCODE2);
- else if (OPCODE34 == 0x65) CHIP8_FX65(cpu, OPCODE2);
- } else { /*unknown opcode*/ }
- cpu->PC += 2;
- }
- void CHIP8_0NNN(C8, VNNN) { return; }
- void CHIP8_00E0(C8) { memset(cpu->display, 0, 64*32); }
- void CHIP8_00EE(C8) { cpu->PC = _CHIP8_POP(cpu); }
- void CHIP8_1NNN(C8, VNNN) { cpu->PC = NNN; }
- void CHIP8_2NNN(C8, VNNN) { _CHIP8_PUSH(cpu, cpu->PC+2); cpu->PC = NNN; }
- void CHIP8_3XNN(C8, VXNN) { if (cpu->V[X] == NN) cpu->PC += 2; }
- void CHIP8_4XNN(C8, VXNN) { if (cpu->V[X] != NN) cpu->PC += 2; }
- void CHIP8_5XY0(C8, VXY) { if (cpu->V[X] == cpu->V[Y]) cpu->PC += 2; }
- void CHIP8_6XNN(C8, VXNN) { cpu->V[X] = NN; }
- void CHIP8_7XNN(C8, VXNN) { cpu->V[X] += NN; }
- void CHIP8_8XY0(C8, VXY) { cpu->V[X] = cpu->V[Y]; }
- void CHIP8_8XY1(C8, VXY) { cpu->V[X] = cpu->V[X] | cpu->V[Y]; }
- void CHIP8_8XY2(C8, VXY) { cpu->V[X] = cpu->V[X] & cpu->V[Y]; }
- void CHIP8_8XY3(C8, VXY) { cpu->V[X] = cpu->V[X] ^ cpu->V[Y]; }
- void CHIP8_8XY4(C8, VXY) { cpu->V[X] += cpu->V[Y]; cpu->V[0xF] = (cpu->V[X] < cpu->V[Y]); }
- void CHIP8_8XY5(C8, VXY) { cpu->V[X] -= cpu->V[Y]; cpu->V[0xF] = /*borrow*/0; }
- void CHIP8_8XY6(C8, VXY) { cpu->V[0xF] = /*LSB*/0; cpu->V[X] = cpu->V[X] >> 1; }
- void CHIP8_8XY7(C8, VXY) { cpu->V[X] = cpu->V[Y] - cpu->V[X]; cpu->V[0xF] = /*borrow*/0; }
- void CHIP8_8XYE(C8, VXY) { cpu->V[0xF] = /*MSB*/0; cpu->V[X] = cpu->V[X] << 1; }
- void CHIP8_9XY0(C8, VXY) { if (cpu->V[X] != cpu->V[Y]) cpu->PC += 2; }
- void CHIP8_ANNN(C8, VNNN) { cpu->I = NNN; }
- void CHIP8_BNNN(C8, VNNN) { cpu->PC = NNN + cpu->V[0x0]; }
- void CHIP8_CXNN(C8, VXNN) { cpu->V[X] = (/*rand*/0 & NN); }
- void CHIP8_DXYN(C8, VXYN) { cpu->V[0xF] = CHIP8_DRAWSPRITE(X, Y, cpu->mem + cpu->I, N); }
- void CHIP8_EX9E(C8, VX) { if (cpu->input[X] == 0xFF) cpu->PC += 2; }
- void CHIP8_EXA1(C8, VX) { if (cpu->input[X] == 0x00) cpu->PC += 2; }
- void CHIP8_FX07(C8, VX) { cpu->V[X] = cpu->delaytimer; }
- void CHIP8_FX0A(C8, VX) { BYTE i = 0; for (i = 0; i < 0xF; i++) if (cpu->input[i] == 0xFF) cpu->V[X] = i; }
- void CHIP8_FX15(C8, VX) { cpu->delaytimer = cpu->V[X]; }
- void CHIP8_FX18(C8, VX) { cpu->soundtimer = cpu->V[X]; }
- void CHIP8_FX1E(C8, VX) { cpu->I += cpu->V[X]; }
- void CHIP8_FX29(C8, VX) { cpu->I = 0x000 + (cpu->V[X] * 5); }
- void CHIP8_FX33(C8, VX) { /*not implemented*/return; }
- void CHIP8_FX55(C8, VX) { BYTE i = 0; for (i = 0; i < 0xF; i++) *(cpu->mem + cpu->I + i) = cpu->V[i]; }
- void CHIP8_FX65(C8, VX) { BYTE i = 0; for (i = 0; i < 0xF; i++) cpu->V[i] = *(cpu->mem + cpu->I + i); }
- void _CHIP8_PUSH(CHIP8_CPU *cpu, UINT16 val)
- {
- if (cpu->SP == 16) return; //stack full
- cpu->stack[cpu->SP++] = val;
- }
- INT16 _CHIP8_POP(CHIP8_CPU *cpu)
- {
- if (cpu->SP == 0) return 0x0000; //stack is empty
- return cpu->stack[--cpu->SP];
- }
Add Comment
Please, Sign In to add comment