From 9f560af8615acbcbbb71c146bad8474e7bbee9c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20H=C3=BCbner?= Date: Sun, 30 Sep 2018 19:58:46 +0200 Subject: [PATCH] hm --- source/app.d | 56 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/source/app.d b/source/app.d index 1253f48..739576f 100644 --- a/source/app.d +++ b/source/app.d @@ -301,7 +301,7 @@ struct PixelBuffer { out vec4 out_col; void main() { - float v = texture(tex, frag_uv.st).r; + float v = texture(tex, frag_uv.st).r * 255; out_col = vec4(v, v, v, 1.0); } @@ -624,6 +624,13 @@ struct Chip8 { void reset() { cpu = cpu.init; + stack = stack.init; + ram = ram.init; + sp = sp.init; + + run_flag = run_flag.init; + draw_flag = draw_flag.init; + screen_buf = screen_buf.init; screen_data = screen_data.init; @@ -666,8 +673,7 @@ struct Chip8 { case 0x2000: // 0x2NNN Calls subroutine at NNN. stack[sp++] = cpu.pc; - pc_target = cpu.opcode.capture!(0, 12); - + pc_target = cpu.opcode & 0x0FFF; break; case 0x3000: // 0x3XNN Skips the next instruction if VX equals NN. @@ -726,11 +732,25 @@ struct Chip8 { break; case 0x0004: // 0x8XY4 Adds VY to VX. VF is set to 1 when there's a carry, and to 0 when there isn't. - cpu.v[x] += cpu.v[y]; //TODO carry flag + auto vx = cpu.v[x]; + auto vy = cpu.v[y]; + if (cast(ushort)vx + cast(ushort)vy > 255) { + cpu.v[0xF] = 1; + } else { + cpu.v[0xF] = 0; + } + cpu.v[x] += cpu.v[y]; break; case 0x0005: // 0x8XY5 VY is subtracted from VX. VF is set to 0 when there's a borrow, and 1 when there isn't. - cpu.v[x] -= cpu.v[y]; //TODO borrow flag + auto vx = cpu.v[x]; + auto vy = cpu.v[y]; + if (vx > vy) { + cpu.v[0xF] = 1; + } else { + cpu.v[0xF] = 0; + } + cpu.v[x] -= cpu.v[y]; break; case 0x0006: // 0x8XY6 Shifts VX right by one. VF is set to the value of the least significant bit of VX before the shift. @@ -738,10 +758,18 @@ struct Chip8 { auto vx = cpu.v[x]; cpu.v[0xF] = (vx & 0b10000000) >> 7; cpu.v[x] >>= 1; + cpu.v[x] /= 2; break; case 0x0007: // 0x8XY7 Sets VX to VY minus VX. VF is set to 0 when there's a borrow, and 1 when there isn't. - cpu.v[x] = cast(Register)(cpu.v[y] - cpu.v[x]); // TODO borrow flag + auto vx = cpu.v[x]; + auto vy = cpu.v[y]; + if (vy > vx) { + cpu.v[0xF] = 1; + } else { + cpu.v[0xF] = 0; + } + cpu.v[x] = cast(Register)(vy - vx); // TODO borrow flag break; case 0x000E: // 0x8XYE Shifts VX left by one. VF is set to the value of the most significant bit of VX before the shift. @@ -749,6 +777,7 @@ struct Chip8 { auto vx = cpu.v[x]; cpu.v[0xF] = (vx & 0b10000000) >> 7; cpu.v[x] <<= 1; + cpu.v[x] *= 2; break; default: // unhandled for some reason @@ -762,7 +791,7 @@ struct Chip8 { case 0x9000: // 0x9XYO Skips the next instruction if VX doesn't equal VY. if (cpu.v[cpu.opcode.capture!(8, 12)] != cpu.v[cpu.opcode.capture!(4, 8)]) { - pc_target += 2; //do skip yes + pc_target += 2; // do skip yes } break; @@ -897,7 +926,7 @@ struct Chip8 { break; - default: //unhandled for some reason + default: // unhandled for some reason writefln("unknown opcode: 0x%x", cpu.opcode); break; @@ -1012,10 +1041,10 @@ struct Emulator { status.initialize(&this, &emu); // debug data - emu.screen_buf[0] = 255; - emu.screen_buf[64 - 1] = 255; - emu.screen_buf[64*32 - 64] = 255; - emu.screen_buf[64*32 - 1] = 255; + emu.screen_buf[0] = 1; + emu.screen_buf[64 - 1] = 1; + emu.screen_buf[64*32 - 64] = 1; + emu.screen_buf[64*32 - 1] = 1; // set up pixel buffer to poke a buf.create(emu.screen_buf.ptr, 64, 32); @@ -1069,12 +1098,11 @@ struct Emulator { int w, h; window.windowSize(w, h); - if (emu.draw_flag) { buf.update(emu.screen_buf); } - buf.draw(w, h); + imgui.newFrame(window); status.draw();