This commit is contained in:
Robin Hübner 2018-09-30 21:12:37 +02:00
parent 9f560af861
commit f0d309f925
3 changed files with 19 additions and 24 deletions

BIN
programs/chip8_picture.ch8 Normal file

Binary file not shown.

BIN
programs/sierpinski.ch8 Normal file

Binary file not shown.

View File

@ -397,7 +397,7 @@ struct Chip8Status {
import std.file : read;
auto buf = read("programs/sqrt.ch8");
auto buf = read("programs/chip8_picture.ch8");
emu_.load(0x200, buf); // do ze load yes, will copy all the data in
} // loadShortcut
@ -523,15 +523,15 @@ struct Chip8Status {
case SDL_SCANCODE_G: redrawShortcut(); break;
case SDL_SCANCODE_T: toggleRunShortcut(); break;
case SDL_SCANCODE_S: stepShortcut(); break;
case SDL_SCANCODE_Q: quitShortcut();
case SDL_SCANCODE_Q: quitShortcut(); break;
default: break;
}
} else {
if (ev.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
quitShortcut();
}
}
break;
}
}
default:
break;
}
@ -625,7 +625,8 @@ struct Chip8 {
cpu = cpu.init;
stack = stack.init;
ram = ram.init;
ram[0x200 .. $ - 1] = 0;
cpu.pc = 0x200;
sp = sp.init;
run_flag = run_flag.init;
@ -758,7 +759,6 @@ 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.
@ -777,7 +777,6 @@ 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
@ -801,13 +800,13 @@ struct Chip8 {
break;
case 0xB000: // 0xBNNN Jumps to the address NNN plus V0.
pc_target = cast(ubyte)cpu.opcode.capture!(0, 12) + cpu.v[0x0];
pc_target = cast(ProgramCounter)(cpu.opcode.capture!(0, 12) + cpu.v[0x0]);
break;
case 0xC000: // 0xCXNN Sets VX to the result of a bitwise and operation on a random number and NN.
import std.random : uniform;
auto x = cpu.opcode.capture!(8, 12);
auto x = cpu.opcode.capture!(0, 8);
cpu.v[x] = uniform(Register.min, Register.max) & cpu.opcode.capture!(0, 8);
break;
@ -829,12 +828,19 @@ struct Chip8 {
ushort pixel = ram[spr_addr + row];
foreach (int col; 0 .. 8) {
if ((pixel & (0x80 >> col)) != 0) {
if (screen_buf[(x + col + ((y + row) * 64))] == 1) {
if ((pixel & 0x80) > 0) {
auto x_off = (x + col) % 64;
auto y_off = (y + row) % 32;
auto offset = x_off + (y_off * 64);
if (screen_buf[offset] == 1) {
cpu.v[0xF] = 1;
} else {
cpu.v[0xF] = 0;
}
screen_buf[x + row + ((y + col) * 64)] ^= 1;
screen_buf[offset] ^= 1;
writefln("write to offset: %d", offset);
}
pixel <<= 1;
}
}
@ -971,18 +977,6 @@ struct Chip8 {
} // tick
void draw() {
if (draw_flag) {
// update buffer with new pixel data
draw_flag = false;
}
// glPixelZoom(rt.width / 64, rt.height / 32);
// glDrawPixels(64, 32, GL_RGB, GL_UNSIGNED_BYTE, screen_data.ptr);
} // draw
} // Emulator
void loadLibs() {
@ -1046,7 +1040,7 @@ struct Emulator {
emu.screen_buf[64*32 - 64] = 1;
emu.screen_buf[64*32 - 1] = 1;
// set up pixel buffer to poke a
// set up pixel buffer to poke at
buf.create(emu.screen_buf.ptr, 64, 32);
}
@ -1100,6 +1094,7 @@ struct Emulator {
window.windowSize(w, h);
if (emu.draw_flag) {
buf.update(emu.screen_buf);
emu.draw_flag = false;
}
buf.draw(w, h);