Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void opcodes(int opcode) {
- // these opcodes are the ones that don't need manipulation
- switch (opcode) {
- // clear the screen by filling the array with false
- case 0x00E0:
- for (int y = 0; y <= display[y].length; y++) {
- Arrays.fill(display[y], false); // using the fill to fill each array of the 2d array with false
- }
- PC += 2;
- break;
- // This returns the program counter from a subroutine
- case 0x00EE:
- // Returns from a subroutine then increments the program counter
- PC = stack[SP--];
- PC += 2;
- break;
- }
- // This switch performs a bitwise operation to separate the goto address from
- // the action wanted to be performed
- switch (opcode & 0xF000) {
- // this opcode jumps to address NNN
- case 0x1000:
- PC = opcode & 0x0FFF;
- break;
- // this one calls a subroutine and stores the return in the stack
- case 0x2000:
- stack[++SP] = PC;
- PC = opcode & 0x0FFF;
- break;
- // If VX == NN then skip the next instruction, the opcode it 0x3XNN
- case 0x3000:
- if (V[(opcode & 0x0F00) >>> 8] == (opcode & 0x00FF)) {
- PC += 4;
- } else {
- PC += 2;
- }
- break;
- // If VX != NN then skip the next instruction, the opcode is 0x3XNN
- case 0x4000:
- if (V[(opcode & 0x0F00) >>> 0x0008] != (opcode & 0x00FF)) {
- PC += 4;
- } else {
- PC += 2;
- }
- break;
- // we are comparing VX and VY, if they match then skip, the opcode is 5XY0
- case 0x5000:
- if (V[(opcode & 0x0F00) >>> 0x0008] == V[(opcode & 0x00F0) >>> 0x0004]) {
- PC += 4;
- } else {
- PC += 2;
- }
- break;
- // Set register VX to NN, opcode is 6XNN
- case 0x6000:
- V[(opcode & 0x0F00) >>> 0x0008] = (opcode & 0x00FF);
- PC += 2;
- break;
- // add value NN to register VX, opcode is 7XNN
- case 0x7000:
- V[(opcode & 0x0F00) >>> 8] += (opcode & 0x00FF);
- if ((V[(opcode & 0x0F00) >>> 8] >= 256)) {
- V[(opcode & 0x0F00) >>> 8] -= 256;
- }
- PC += 2;
- }
- // Where the F is in the bitwise is used to determine which opcode is meant to
- // be used
- switch (opcode & 0xF00F) {
- // sets VX to VY
- case 0x8000:
- V[(opcode & 0x0F00) >>> 0x0008] = V[(opcode & 0x00F0) >>> 4];
- PC += 2;
- break;
- // bitwise OR operation setting VX to VX|VY
- case 0x8001:
- // the bitwise OR means any bit that is 1 in the byte will be 1 if either are 1
- V[(opcode & 0x0F00) >>> 0x0008] = (V[(opcode & 0x0F00) >>> 8] | V[(opcode & 0x00F0) >>> 4]);
- PC += 2;
- break;
- // Bitwise AND operation setting VX to VX&VY
- case 0x8002:
- // the bitwise AND means any bit that is 1 in both will be 1
- V[(opcode & 0x0F00) >>> 0x0008] = (V[(opcode & 0x0F00) >>> 8] & V[(opcode & 0x00F0) >>> 4]);
- PC += 2;
- break;
- // bitwise XOR operation will only have a 1 if only one of the values has a 1
- // there
- case 0x8003:
- // the bitwise AND means any bit that is 1 in both will be 1
- V[(opcode & 0x0F00) >>> 0x0008] = (V[(opcode & 0x0F00) >>> 8] ^ V[(opcode & 0x00F0) >>> 4]);
- PC += 2;
- break;
- // Adds VY to VX VF is set to 1 when there's a carry and to 0 when there isn't
- case 0x8004:
- if (V[(opcode & 0x0F00) >>> 0x0008] + V[(opcode & 0x00F0) >>> 4] > 0x00FF) {
- V[0x000F] = 1;
- } else {
- V[0x000F] = 0;
- }
- V[(opcode & 0x0F00) >>> 0x0008] = ((V[(opcode & 0x0F00) >>> 0x0008] + V[(opcode & 0x00F0) >>> 4]) & 0x00FF);
- PC += 2;
- break;
- // subtract VY from VX and sets a register to 1 if borrow
- case 0x8005:
- if (V[(opcode & 0x0F00) >>> 0x0008] - V[(opcode & 0x00F0) >>> 4] < 0x0000) {
- V[0x000F] = 1;
- } else {
- V[0x000F] = 0;
- }
- PC += 2;
- break;
- // Stores the least significant bit of VX in VF and then shifts VX to the right
- // by 1
- case 0x8006:
- if ((V[(opcode & 0x0F00) >>> 8] & 0x0001) == 1) {
- V[0x000F] = 1;
- } else {
- V[0x000F] = 0;
- }
- V[(opcode & 0x0F00) >>> 8] = V[(opcode & 0x0F00) >>> 8] >>> 1;
- PC += 2;
- break;
- // set VX to VY minus VX and sets a register to 1 if borrow
- case 0x8007:
- V[(opcode & 0x0F00) >>> 0x0008] = V[(opcode & 0x00F0) >>> 4] - V[(opcode & 0x0F00) >>> 0x0008];
- // have to check and account for a carry
- if (V[opcode & 0x0F00 >>> 0x0008] < 0x0000) {
- V[opcode & 0x0F00 >>> 0x0008] += 0x00FF;
- V[0x000F] = 1;
- } else {
- V[0x000F] = 0;
- }
- PC += 2;
- break;
- // Stores the most significant bit of VX in VF and then shifts VX to the left by
- // 1
- case 0x800E:
- if ((V[(opcode & 0x0F00) >>> 8] >>> 7 & 0x0001) == 1) {
- V[0x000F] = 1;
- } else {
- V[0x000F] = 0;
- }
- V[(opcode & 0x0F00) >>> 8] = V[(opcode & 0x0F00) >>> 8] << 1 & 0x00FF;
- PC += 2;
- break;
- // skip if the two registers are not equal, VX and VY
- case 0x9000:
- if (V[(opcode & 0x0F00) >>> 8] != V[(opcode & 0x0F00) >>> 8]) {
- PC += 4;
- } else {
- PC += 2;
- }
- break;
- }
- switch (opcode & 0xF000) {
- // set register I to address NNN, opcode ANNN
- case 0xA000:
- I = (opcode & 0x0FFF);
- PC += 2;
- break;
- // Jump to address NNN plus V0
- case 0xB000:
- PC = V[0] + (opcode & 0x0FFF);
- break;
- // set VX to random value from random number with a bitwise & with NN, opcode is
- // CXNN
- case 0xC000:
- V[(opcode & 0x0F00) >>> 8] = ((int) (Math.random() * 0x0FF)) & (opcode & 0x0FF);
- PC += 2;
- break;
- // Draws a sprite at coordinate (VX, VY) that has a width of 8 pixels and a
- // height of N pixels. Each row of 8 pixels is read as bit-coded starting from
- // memory location I; I value doesn’t change after the execution of this
- // instruction. As described above, VF is set to 1 if any screen pixels are
- // flipped from set to unset when the sprite is drawn, and to 0 if that doesn’t
- // happen opcode DXYN
- //using someone elses code
- //TODO Redo this code as original code
- case 0xD000:
- // N - Display n-byte sprite starting at memory location I at (Vx, Vy), set VF = collision.
- V[0xF] = 0;
- for (int yLine = 0; yLine < (opcode & 0x000F); yLine++) {
- int pixel = memory[I + yLine];
- for (int xLine = 0; xLine < 8; xLine++) {
- // check each bit (pixel) in the 8 bit row
- if ((pixel & (0x80 >> xLine)) != 0) {
- // wrap pixels if they're drawn off screen
- int xCoord = V[(opcode & 0x0F00) >> 8]+xLine;
- int yCoord = V[(opcode & 0x00F0) >> 4]+yLine;
- if (xCoord < 64 && yCoord < 32) {
- // if pixel already exists, set carry (collision)
- if (display[xCoord] [yCoord] == true) {
- V[0xF] = 1;
- }
- // draw via xor
- display[xCoord][yCoord]^= true;
- }
- }
- }
- }
- PC += 2;
- break;
- }
- switch (opcode & 0xF0FF) {
- case 0xE09E:
- // EX9E - Skip next instruction if key with the value of Vx is pressed.
- // TODO Implement
- PC += 2;
- break;
- case 0xE0A1:
- // EXA1 - Skip next instruction if key with the value of Vx is not pressed.
- PC += 4;
- break;
- case 0xF007:
- V[((opcode & 0x0F00) >>> 8)] = (delayTimer);
- PC += 2;
- break;
- case 0xF00A:
- // FX0A - Wait for a key press, store the value of the key in Vx.
- // TODO Implement.
- break;
- case 0xF015:
- // Set delay timer to Vx.
- delayTimer = V[(opcode & 0x0F00) >>> 8];
- PC += 2;
- break;
- case 0xF018:
- // set sound timer to Vx
- soundTimer = V[(opcode & 0x0F00) >>> 8];
- PC += 2;
- break;
- case 0xF01E:
- // Set I to I + Vx.
- I += V[(opcode & 0x0F00) >>> 8] * 5;
- PC += 2;
- break;
- case 0xF029:
- // set I to location of sprite
- I = V[(opcode & 0x0F00) >>> 8];
- PC += 2;
- break;
- case 0xF033:
- // Stores the binary-coded decimal representation of VX, with the most
- // significant of three digits at the address in I, the middle digit at I plus
- // 1, and the least significant digit at I plus 2. (In other words, take the
- // decimal representation of VX, place the hundreds digit in memory at location
- // in I, the tens digit at location I+1, and the ones digit at location I+2.).
- // get first digit
- // get most significant digit
- //using someone elses code
- //TODO Redo this code as original code
- memory[I] = (V[(opcode & 0x0F00) >>> 8] / 100);
- memory[I + 1] = ((V[(opcode & 0x0F00) >>> 8] % 100) / 10);
- memory[I + 2] = ((V[(opcode & 0x0F00) >>> 8] % 100) % 10);
- PC += 2;
- break;
- case 0xF055:
- // Store registers V0 through Vx in memory at I
- for (int x = 0x000; x < (opcode & 0x0F00 >>> 8); x++) {
- memory[I + x] = V[x];
- }
- PC += 2;
- break;
- case 0xF065:
- // Read registers V0 through Vx
- for (int x = 0x000; x < (opcode & 0x0F00) >>> 8; x++) {
- V[x] = memory[I + x] & 0x00ff;
- }
- PC += 2;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement