diff --git a/chip8.sv b/chip8.sv index d0017ab..62870bf 100644 --- a/chip8.sv +++ b/chip8.sv @@ -1,5 +1,6 @@ module chip8 ( - input wire clk_in + input wire clk_in, + input wire rst_in ); bit [31:0] vram[0:2047]; @@ -16,6 +17,7 @@ module chip8 ( beeper beeper (sound_timer); cpu cpu ( + rst_in, memory, clk_in, keyboard, @@ -39,98 +41,100 @@ module chip8 ( ); rom_loader loader (memory); - initial begin - bit [7:0] fontset[79:0] = { - 8'hF0, - 8'h90, - 8'h90, - 8'h90, - 8'hF0, // 0 - 8'h20, - 8'h60, - 8'h20, - 8'h20, - 8'h70, // 1 - 8'hF0, - 8'h10, - 8'hF0, - 8'h80, - 8'hF0, // 2 - 8'hF0, - 8'h10, - 8'hF0, - 8'h10, - 8'hF0, // 3 - 8'h90, - 8'h90, - 8'hF0, - 8'h10, - 8'h10, // 4 - 8'hF0, - 8'h80, - 8'hF0, - 8'h10, - 8'hF0, // 5 - 8'hF0, - 8'h80, - 8'hF0, - 8'h90, - 8'hF0, // 6 - 8'hF0, - 8'h10, - 8'h20, - 8'h40, - 8'h40, // 7 - 8'hF0, - 8'h90, - 8'hF0, - 8'h90, - 8'hF0, // 8 - 8'hF0, - 8'h90, - 8'hF0, - 8'h10, - 8'hF0, // 9 - 8'hF0, - 8'h90, - 8'hF0, - 8'h90, - 8'h90, // A - 8'hE0, - 8'h90, - 8'hE0, - 8'h90, - 8'hE0, // B - 8'hF0, - 8'h80, - 8'h80, - 8'h80, - 8'hF0, // C - 8'hE0, - 8'h90, - 8'h90, - 8'h90, - 8'hE0, // D - 8'hF0, - 8'h80, - 8'hF0, - 8'h80, - 8'hF0, // E - 8'hF0, - 8'h80, - 8'hF0, - 8'h80, - 8'h80 // F - }; - - // Load fontset into memory - for (int i = 0; i < 80; i++) begin - memory[i] = fontset[79-i]; - end - - // Initialize keyboard bits - for (int i = 0; i < 15; i++) begin - keyboard[i] = 0; + always_ff @(posedge clk_in) begin + if (rst_in) begin + bit [7:0] fontset[79:0] = { + 8'hF0, + 8'h90, + 8'h90, + 8'h90, + 8'hF0, // 0 + 8'h20, + 8'h60, + 8'h20, + 8'h20, + 8'h70, // 1 + 8'hF0, + 8'h10, + 8'hF0, + 8'h80, + 8'hF0, // 2 + 8'hF0, + 8'h10, + 8'hF0, + 8'h10, + 8'hF0, // 3 + 8'h90, + 8'h90, + 8'hF0, + 8'h10, + 8'h10, // 4 + 8'hF0, + 8'h80, + 8'hF0, + 8'h10, + 8'hF0, // 5 + 8'hF0, + 8'h80, + 8'hF0, + 8'h90, + 8'hF0, // 6 + 8'hF0, + 8'h10, + 8'h20, + 8'h40, + 8'h40, // 7 + 8'hF0, + 8'h90, + 8'hF0, + 8'h90, + 8'hF0, // 8 + 8'hF0, + 8'h90, + 8'hF0, + 8'h10, + 8'hF0, // 9 + 8'hF0, + 8'h90, + 8'hF0, + 8'h90, + 8'h90, // A + 8'hE0, + 8'h90, + 8'hE0, + 8'h90, + 8'hE0, // B + 8'hF0, + 8'h80, + 8'h80, + 8'h80, + 8'hF0, // C + 8'hE0, + 8'h90, + 8'h90, + 8'h90, + 8'hE0, // D + 8'hF0, + 8'h80, + 8'hF0, + 8'h80, + 8'hF0, // E + 8'hF0, + 8'h80, + 8'hF0, + 8'h80, + 8'h80 // F + }; + + // Load fontset into memory + for (int i = 0; i < 80; i++) begin + memory[i] = fontset[79-i]; + end + + // Initialize keyboard bits + for (int i = 0; i < 15; i++) begin + keyboard[i] = 0; + end end end endmodule diff --git a/cpu.sv b/cpu.sv index ae6db50..7fb3a2c 100644 --- a/cpu.sv +++ b/cpu.sv @@ -1,4 +1,5 @@ module cpu ( + input wire rst_in, output bit [7:0] memory[0:4095], input wire clk_in, input wire keyboard[15:0], @@ -31,18 +32,16 @@ module cpu ( logic [31:0] screen_pixel; logic [7:0] sprite_pixel; - // Initialize CPU - initial begin - halt = 0; - watch_key = 255; - sound_timer = 0; - delay_timer = 0; - cycle_counter = 0; - program_counter = 'h200; - stack_pointer = 4'b0000; - end - always_ff @(posedge clk_in) begin + if (rst_in) begin + halt = 0; + watch_key = 255; + sound_timer = 0; + delay_timer = 0; + cycle_counter = 0; + program_counter = 'h200; + stack_pointer = 4'b0000; + end opcode = {memory[program_counter+0], memory[program_counter+1]}; $display("HW : opcode is 0x%h (%b)", opcode, opcode); $display("HW : PC %0d 0x%h", program_counter, program_counter); diff --git a/yayacemu.cpp b/yayacemu.cpp index e695987..3021574 100644 --- a/yayacemu.cpp +++ b/yayacemu.cpp @@ -141,9 +141,11 @@ int main(int argc, char **argv) { Vchip8 *dut = new Vchip8{contextp}; + dut->rst_in = 1; while (true) { dut->clk_in ^= 1; dut->eval(); + dut->rst_in = 0; usleep(1000000 / EMULATION_HZ); if (SDL_QuitRequested()) { std::cout << "INF_EMU: Received Quit from SDL. Goodbye!" << '\n';