run formatter

This commit is contained in:
Nicholas Orlowsky 2024-01-31 21:04:52 -06:00
parent e679a3ce68
commit f715709942
9 changed files with 550 additions and 469 deletions

View file

@ -1,6 +1,6 @@
module beeper ( module beeper (
input wire [7:0] sound_timer input wire [7:0] sound_timer
); );
import "DPI-C" function void set_beep(bit beep); import "DPI-C" function void set_beep(bit beep);

134
chip8.sv
View file

@ -1,11 +1,11 @@
module chip8 ( module chip8 (
input wire clk_in input wire clk_in
); );
bit [31:0] vram [0:2047]; bit [31:0] vram[0:2047];
bit [7:0] memory [0:4095]; bit [7:0] memory[0:4095];
bit keyboard [15:0]; bit keyboard[15:0];
bit [7:0] sound_timer; bit [7:0] sound_timer;
bit [15:0] program_counter; bit [15:0] program_counter;
@ -15,39 +15,121 @@ module chip8 (
beeper beeper (sound_timer); beeper beeper (sound_timer);
cpu cpu (memory, clk_in, keyboard, random_number, cycle_counter, program_counter, vram, sound_timer); cpu cpu (
memory,
clk_in,
keyboard,
random_number,
cycle_counter,
program_counter,
vram,
sound_timer
);
gpu gpu (vram); gpu gpu (vram);
keyboard kb (clk_in, keyboard); keyboard kb (
rng randy (clk_in, program_counter, keyboard, cycle_counter, random_number); clk_in,
keyboard
);
rng randy (
clk_in,
program_counter,
keyboard,
cycle_counter,
random_number
);
rom_loader loader (memory); rom_loader loader (memory);
initial begin initial begin
bit [7:0] fontset [79:0] = { bit [7:0] fontset[79:0] = {
8'hF0, 8'h90, 8'h90, 8'h90, 8'hF0, // 0 8'hF0,
8'h20, 8'h60, 8'h20, 8'h20, 8'h70, // 1 8'h90,
8'hF0, 8'h10, 8'hF0, 8'h80, 8'hF0, // 2 8'h90,
8'hF0, 8'h10, 8'hF0, 8'h10, 8'hF0, // 3 8'h90,
8'h90, 8'h90, 8'hF0, 8'h10, 8'h10, // 4 8'hF0, // 0
8'hF0, 8'h80, 8'hF0, 8'h10, 8'hF0, // 5 8'h20,
8'hF0, 8'h80, 8'hF0, 8'h90, 8'hF0, // 6 8'h60,
8'hF0, 8'h10, 8'h20, 8'h40, 8'h40, // 7 8'h20,
8'hF0, 8'h90, 8'hF0, 8'h90, 8'hF0, // 8 8'h20,
8'hF0, 8'h90, 8'hF0, 8'h10, 8'hF0, // 9 8'h70, // 1
8'hF0, 8'h90, 8'hF0, 8'h90, 8'h90, // A 8'hF0,
8'hE0, 8'h90, 8'hE0, 8'h90, 8'hE0, // B 8'h10,
8'hF0, 8'h80, 8'h80, 8'h80, 8'hF0, // C 8'hF0,
8'hE0, 8'h90, 8'h90, 8'h90, 8'hE0, // D 8'h80,
8'hF0, 8'h80, 8'hF0, 8'h80, 8'hF0, // E 8'hF0, // 2
8'hF0, 8'h80, 8'hF0, 8'h80, 8'h80 // F 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 // Load fontset into memory
for(int i = 0; i < 80; i++) begin for (int i = 0; i < 80; i++) begin
memory[i] = fontset[79-i]; memory[i] = fontset[79-i];
end end
// Initialize keyboard bits // Initialize keyboard bits
for(int i = 0; i < 15; i++) begin for (int i = 0; i < 15; i++) begin
keyboard[i] = 0; keyboard[i] = 0;
end end
end end

114
cpu.sv
View file

@ -1,21 +1,21 @@
module cpu( module cpu (
output bit [7:0] memory [0:4095], output bit [7:0] memory[0:4095],
input wire clk_in, input wire clk_in,
input wire keyboard [15:0], input wire keyboard[15:0],
input wire [7:0] random_number, input wire [7:0] random_number,
output int cycle_counter, output int cycle_counter,
output wire [15:0] program_counter, output wire [15:0] program_counter,
output wire [31:0] vram [0:2047], output wire [31:0] vram[0:2047],
output wire [7:0] sound_timer output wire [7:0] sound_timer
); );
bit halt; bit halt;
int watch_key; int watch_key;
logic [15:0] stack [0:15]; logic [15:0] stack[0:15];
logic [15:0] index_reg; logic [15:0] index_reg;
logic [3:0] stack_pointer; logic [3:0] stack_pointer;
logic [7:0] registers [0:15]; logic [7:0] registers[0:15];
logic [15:0] opcode; logic [15:0] opcode;
logic [7:0] delay_timer; logic [7:0] delay_timer;
@ -48,16 +48,14 @@ module cpu(
$display("HW : PC %0d 0x%h", program_counter, program_counter); $display("HW : PC %0d 0x%h", program_counter, program_counter);
if (cycle_counter % 20 == 0) begin if (cycle_counter % 20 == 0) begin
if (delay_timer > 0) if (delay_timer > 0) delay_timer--;
delay_timer--; if (sound_timer > 0) sound_timer--;
if (sound_timer > 0)
sound_timer--;
end end
casez(opcode) casez (opcode)
'h00E0: begin 'h00E0: begin
$display("HW : INSTR CLS"); $display("HW : INSTR CLS");
for(int i = 0; i < 2048; i++) begin for (int i = 0; i < 2048; i++) begin
vram[i] = 0; vram[i] = 0;
end end
end end
@ -80,87 +78,87 @@ module cpu(
'h3???: begin 'h3???: begin
$display("HW : INSTR SE Vx, byte"); $display("HW : INSTR SE Vx, byte");
scratch = (opcode & 'h00FF); scratch = (opcode & 'h00FF);
if (scratch[7:0] == registers[(opcode & 'h0F00) >> 8]) begin if (scratch[7:0] == registers[(opcode&'h0F00)>>8]) begin
program_counter += 2; program_counter += 2;
end end
end end
'h4???: begin 'h4???: begin
$display("HW : INSTR SNE Vx, byte"); $display("HW : INSTR SNE Vx, byte");
scratch = (opcode & 'h00FF); scratch = (opcode & 'h00FF);
if (scratch[7:0] != registers[(opcode & 'h0F00) >> 8]) begin if (scratch[7:0] != registers[(opcode&'h0F00)>>8]) begin
program_counter += 2; program_counter += 2;
end end
end end
'h5??0: begin 'h5??0: begin
$display("HW : INSTR SE Vx, Vy"); $display("HW : INSTR SE Vx, Vy");
if (registers[(opcode & 'h00F0) >> 4] == registers[(opcode & 'h0F00) >> 8]) begin if (registers[(opcode&'h00F0)>>4] == registers[(opcode&'h0F00)>>8]) begin
program_counter += 2; program_counter += 2;
end end
end end
'h6???: begin 'h6???: begin
$display("HW : INSTR LD Vx, byte"); $display("HW : INSTR LD Vx, byte");
scratch = (opcode & 'h00FF); scratch = (opcode & 'h00FF);
registers[(opcode & 'h0F00) >> 8] = scratch[7:0]; registers[(opcode&'h0F00)>>8] = scratch[7:0];
end end
'h7???: begin 'h7???: begin
$display("HW : INSTR ADD Vx, byte"); $display("HW : INSTR ADD Vx, byte");
scratch = (opcode & 'h00FF); scratch = (opcode & 'h00FF);
registers[(opcode & 'h0F00) >> 8] += scratch[7:0]; registers[(opcode&'h0F00)>>8] += scratch[7:0];
end end
'h8??0: begin 'h8??0: begin
$display("HW : INSTR LD Vx, Vy"); $display("HW : INSTR LD Vx, Vy");
registers[(opcode & 'h0F00) >> 8] = registers[(opcode & 'h00F0) >> 4]; registers[(opcode&'h0F00)>>8] = registers[(opcode&'h00F0)>>4];
end end
'h8??1: begin 'h8??1: begin
$display("HW : INSTR OR Vx, Vy"); $display("HW : INSTR OR Vx, Vy");
registers[(opcode & 'h0F00) >> 8] |= registers[(opcode & 'h00F0) >> 4]; registers[(opcode&'h0F00)>>8] |= registers[(opcode&'h00F0)>>4];
registers[15] = 0; registers[15] = 0;
end end
'h8??2: begin 'h8??2: begin
$display("HW : INSTR AND Vx, Vy"); $display("HW : INSTR AND Vx, Vy");
registers[(opcode & 'h0F00) >> 8] &= registers[(opcode & 'h00F0) >> 4]; registers[(opcode&'h0F00)>>8] &= registers[(opcode&'h00F0)>>4];
registers[15] = 0; registers[15] = 0;
end end
'h8??3: begin 'h8??3: begin
$display("HW : INSTR XOR Vx, Vy"); $display("HW : INSTR XOR Vx, Vy");
registers[(opcode & 'h0F00) >> 8] ^= registers[(opcode & 'h00F0) >> 4]; registers[(opcode&'h0F00)>>8] ^= registers[(opcode&'h00F0)>>4];
registers[15] = 0; registers[15] = 0;
end end
'h8??4: begin 'h8??4: begin
$display("HW : INSTR ADD Vx, Vy"); $display("HW : INSTR ADD Vx, Vy");
scratch_8 = registers[(opcode & 'h0F00) >> 8]; scratch_8 = registers[(opcode&'h0F00)>>8];
registers[(opcode & 'h0F00) >> 8] += registers[(opcode & 'h00F0) >> 4]; registers[(opcode&'h0F00)>>8] += registers[(opcode&'h00F0)>>4];
registers[15] = {7'b0000000, scratch_8 > registers[(opcode & 'h0F00) >> 8]}; registers[15] = {7'b0000000, scratch_8 > registers[(opcode&'h0F00)>>8]};
end end
'h8??5: begin 'h8??5: begin
$display("HW : INSTR SUB Vx, Vy"); $display("HW : INSTR SUB Vx, Vy");
scratch_8 = registers[(opcode & 'h0F00) >> 8]; scratch_8 = registers[(opcode&'h0F00)>>8];
registers[(opcode & 'h0F00) >> 8] -= registers[(opcode & 'h00F0) >> 4]; registers[(opcode&'h0F00)>>8] -= registers[(opcode&'h00F0)>>4];
registers[15] = {7'b0000000, scratch_8 >= registers[(opcode & 'h0F00) >> 8]}; registers[15] = {7'b0000000, scratch_8 >= registers[(opcode&'h0F00)>>8]};
end end
'h8??6: begin 'h8??6: begin
$display("HW : INSTR SHR Vx {, Vy}"); $display("HW : INSTR SHR Vx {, Vy}");
scratch_8 = registers[(opcode & 'h0F00) >> 8]; scratch_8 = registers[(opcode&'h0F00)>>8];
registers[(opcode & 'h0F00) >> 8] = registers[(opcode & 'h00F0) >> 4]>> 1; registers[(opcode&'h0F00)>>8] = registers[(opcode&'h00F0)>>4] >> 1;
registers[15] = {7'b0000000, ((scratch_8 & 8'h01) == 8'h01)}; registers[15] = {7'b0000000, ((scratch_8 & 8'h01) == 8'h01)};
end end
'h8??7: begin 'h8??7: begin
$display("HW : INSTR SUBN Vx, Vy"); $display("HW : INSTR SUBN Vx, Vy");
scratch_8 = registers[(opcode & 'h00F0) >> 4]; scratch_8 = registers[(opcode&'h00F0)>>4];
scratch_82 = registers[(opcode & 'h0F00) >> 8]; scratch_82 = registers[(opcode&'h0F00)>>8];
registers[(opcode & 'h0F00) >> 8] = registers[(opcode & 'h00F0) >> 4] - registers[(opcode & 'h0F00) >> 8]; registers[(opcode & 'h0F00) >> 8] = registers[(opcode & 'h00F0) >> 4] - registers[(opcode & 'h0F00) >> 8];
registers[15] = {7'b0000000, (scratch_8 >= scratch_82)}; registers[15] = {7'b0000000, (scratch_8 >= scratch_82)};
end end
'h8??E: begin 'h8??E: begin
$display("HW : INSTR SHL Vx {, Vy}"); $display("HW : INSTR SHL Vx {, Vy}");
scratch_8 = registers[(opcode & 'h0F00) >> 8]; scratch_8 = registers[(opcode&'h0F00)>>8];
registers[(opcode & 'h0F00) >> 8] = registers[(opcode & 'h00F0) >> 4]<< 1; registers[(opcode&'h0F00)>>8] = registers[(opcode&'h00F0)>>4] << 1;
registers[15] = {7'b0000000, (scratch_8[7]) }; registers[15] = {7'b0000000, (scratch_8[7])};
end end
'h9??0: begin 'h9??0: begin
$display("HW : INSTR SNE Vx, Vy"); $display("HW : INSTR SNE Vx, Vy");
if (registers[(opcode & 'h00F0) >> 4] != registers[(opcode & 'h0F00) >> 8]) begin if (registers[(opcode&'h00F0)>>4] != registers[(opcode&'h0F00)>>8]) begin
program_counter += 2; program_counter += 2;
end end
end end
@ -177,7 +175,7 @@ module cpu(
// TODO: use a real RNG module, this is not synthesizeable // TODO: use a real RNG module, this is not synthesizeable
scratch = {8'h00, random_number} % 16'h0100; scratch = {8'h00, random_number} % 16'h0100;
scratch2 = (opcode & 'h00FF); scratch2 = (opcode & 'h00FF);
registers[(opcode & 'h0F00) >> 8] = scratch[7:0] & scratch2[7:0]; registers[(opcode&'h0F00)>>8] = scratch[7:0] & scratch2[7:0];
end end
'hD???: begin 'hD???: begin
$display("HW : INSTR DRW Vx, Vy, nibble"); $display("HW : INSTR DRW Vx, Vy, nibble");
@ -185,8 +183,8 @@ module cpu(
halt = 1; halt = 1;
end else begin end else begin
halt = 0; halt = 0;
x_cord = {24'h000000, registers[(opcode & 'h0F00) >> 8]}; x_cord = {24'h000000, registers[(opcode&'h0F00)>>8]};
y_cord = {24'h000000, registers[(opcode & 'h00F0) >> 4]}; y_cord = {24'h000000, registers[(opcode&'h00F0)>>4]};
x_cord %= 64; x_cord %= 64;
y_cord %= 32; y_cord %= 32;
@ -197,16 +195,15 @@ module cpu(
for (int r = 0; r < size; r++) begin for (int r = 0; r < size; r++) begin
for (int c = 0; c < 8; c++) begin for (int c = 0; c < 8; c++) begin
if (r + y_cord >= 32 || x_cord + c >= 64) if (r + y_cord >= 32 || x_cord + c >= 64) continue;
continue; screen_pixel = vram[((r+y_cord)*64)+(x_cord+c)];
screen_pixel = vram[((r + y_cord) * 64) + (x_cord + c)]; sprite_pixel = memory[{16'h0000, index_reg}+r] & ('h80 >> c);
sprite_pixel = memory[{16'h0000, index_reg} + r] & ('h80 >> c);
if (|sprite_pixel) begin if (|sprite_pixel) begin
if (screen_pixel == 32'hFFFFFFFF) begin if (screen_pixel == 32'hFFFFFFFF) begin
registers[15] = 1; registers[15] = 1;
end end
vram[((r + y_cord) * 64) + (x_cord + c)] ^= 32'hFFFFFFFF; vram[((r+y_cord)*64)+(x_cord+c)] ^= 32'hFFFFFFFF;
end end
end end
end end
@ -214,24 +211,26 @@ module cpu(
end end
'hE?9E: begin 'hE?9E: begin
$display("HW : INSTR SKP Vx"); $display("HW : INSTR SKP Vx");
if (keyboard[{registers[(opcode & 'h0F00) >> 8]}[3:0]] == 1) begin scratch_8 = registers[(opcode&'h0F00)>>8];
if (keyboard[scratch_8[3:0]] == 1) begin
program_counter += 2; program_counter += 2;
end end
end end
'hE?A1: begin 'hE?A1: begin
$display("HW : INSTR SNE Vx"); $display("HW : INSTR SNE Vx");
if (keyboard[{registers[(opcode & 'h0F00) >> 8]}[3:0]] != 1) begin scratch_8 = registers[(opcode&'h0F00)>>8];
if (keyboard[scratch_8[3:0]] != 1) begin
program_counter += 2; program_counter += 2;
end end
end end
'hF?07: begin 'hF?07: begin
$display("HW : INSTR LD Vx, DT"); $display("HW : INSTR LD Vx, DT");
registers[(opcode & 'h0F00) >> 8] = delay_timer; registers[(opcode&'h0F00)>>8] = delay_timer;
end end
'hF?0A: begin 'hF?0A: begin
$display("HW : INSTR LD Vx, K"); $display("HW : INSTR LD Vx, K");
halt = 1; halt = 1;
for(int i = 0; i < 16; i++) begin for (int i = 0; i < 16; i++) begin
if (watch_key == 255) begin if (watch_key == 255) begin
if (keyboard[i]) begin if (keyboard[i]) begin
watch_key = i; watch_key = i;
@ -246,31 +245,31 @@ module cpu(
end end
'hF?15: begin 'hF?15: begin
$display("HW : INSTR LD DT, Vx"); $display("HW : INSTR LD DT, Vx");
delay_timer = registers[(opcode & 'h0F00) >> 8]; delay_timer = registers[(opcode&'h0F00)>>8];
end end
'hF?18: begin 'hF?18: begin
$display("HW : INSTR LD ST, Vx"); $display("HW : INSTR LD ST, Vx");
sound_timer = registers[(opcode & 'h0F00) >> 8]; sound_timer = registers[(opcode&'h0F00)>>8];
end end
'hF?1E: begin 'hF?1E: begin
$display("HW : INSTR ADD I, Vx"); $display("HW : INSTR ADD I, Vx");
index_reg = index_reg + {8'h00, registers[(opcode & 'h0F00) >> 8]}; index_reg = index_reg + {8'h00, registers[(opcode&'h0F00)>>8]};
end end
'hF?29: begin 'hF?29: begin
$display("HW : INSTR LDL F, Vx"); $display("HW : INSTR LDL F, Vx");
index_reg = registers[(opcode & 'h0F00) >> 8] * 5; index_reg = registers[(opcode&'h0F00)>>8] * 5;
end end
'hF?33: begin 'hF?33: begin
$display("HW : INSTR LD B, Vx"); $display("HW : INSTR LD B, Vx");
scratch = {8'h00, registers[(opcode & 'h0F00) >> 8]}; scratch = {8'h00, registers[(opcode&'h0F00)>>8]};
scratch2 = scratch % 10; scratch2 = scratch % 10;
memory[index_reg + 2] = scratch2[7:0]; memory[index_reg+2] = scratch2[7:0];
scratch /= 10; scratch /= 10;
scratch2 = scratch % 10; scratch2 = scratch % 10;
memory[index_reg + 1] = scratch2[7:0]; memory[index_reg+1] = scratch2[7:0];
scratch /= 10; scratch /= 10;
scratch2 = scratch % 10; scratch2 = scratch % 10;
memory[index_reg + 0] = scratch2[7:0]; memory[index_reg+0] = scratch2[7:0];
end end
'hF?55: begin 'hF?55: begin
$display("HW : INSTR LD [I], Vx"); $display("HW : INSTR LD [I], Vx");
@ -293,8 +292,7 @@ module cpu(
default: $display("HW : ILLEGAL INSTRUCTION"); default: $display("HW : ILLEGAL INSTRUCTION");
endcase endcase
if (!halt) if (!halt) program_counter += 2;
program_counter += 2;
cycle_counter++; cycle_counter++;
end end

6
gpu.sv
View file

@ -1,9 +1,9 @@
module gpu ( module gpu (
input wire [31:0] vram [0:2047] input wire [31:0] vram[0:2047]
); );
import "DPI-C" function void init_screen(); import "DPI-C" function void init_screen();
import "DPI-C" function void draw_screen(logic [31:0] vram [0:2047]); import "DPI-C" function void draw_screen(logic [31:0] vram[0:2047]);
initial begin initial begin
init_screen(); init_screen();

View file

@ -1,12 +1,12 @@
module keyboard ( module keyboard (
input wire clk_in, input wire clk_in,
output bit keyboard [15:0] output bit keyboard[15:0]
); );
import "DPI-C" function bit [7:0] get_key(); import "DPI-C" function bit [7:0] get_key();
always_ff @(posedge clk_in) begin always_ff @(posedge clk_in) begin
bit[7:0] keyval = get_key(); bit [7:0] keyval = get_key();
if (&keyval != 1) begin if (&keyval != 1) begin
keyboard[keyval[3:0]] = keyval[7]; keyboard[keyval[3:0]] = keyval[7];
end end

View file

@ -1,3 +1,5 @@
.PHONY: run clean format
SDL_CFLAGS = `sdl2-config --cflags` SDL_CFLAGS = `sdl2-config --cflags`
SDL_LDFLAGS = `sdl2-config --libs` SDL_LDFLAGS = `sdl2-config --libs`
@ -13,4 +15,8 @@ run: build
clean: clean:
rm -rf obj_dir rm -rf obj_dir
format:
verible-verilog-format *.sv --inplace && clang-format *.cpp -i

4
rng.sv
View file

@ -1,10 +1,10 @@
module rng ( module rng (
input wire clk_in, input wire clk_in,
input wire [15:0] pc, input wire [15:0] pc,
input bit keyboard [15:0], input bit keyboard[15:0],
input int cycle_counter, input int cycle_counter,
output bit [7:0] rand_bit output bit [7:0] rand_bit
); );
bit [7:0] last; bit [7:0] last;

View file

@ -1,6 +1,6 @@
module rom_loader ( module rom_loader (
output bit [7:0] memory [0:4095] output bit [7:0] memory[0:4095]
); );
import "DPI-C" function int load_rom(); import "DPI-C" function int load_rom();
import "DPI-C" function bit [7:0] get_next_instr(); import "DPI-C" function bit [7:0] get_next_instr();
@ -8,9 +8,10 @@ module rom_loader (
initial begin initial begin
int rom_size = load_rom(); int rom_size = load_rom();
$display("HW : ROM size is %0d bytes (%0d bits) (%0d instructions)", rom_size, rom_size * 8, rom_size / 2); $display("HW : ROM size is %0d bytes (%0d bits) (%0d instructions)", rom_size,
rom_size * 8, rom_size / 2);
for (int i = 0; i < rom_size; i++) begin for (int i = 0; i < rom_size; i++) begin
memory['h200 + i] = get_next_instr(); memory['h200+i] = get_next_instr();
end end
close_rom(); close_rom();
$display("HW : ROM loaded successfully"); $display("HW : ROM loaded successfully");

View file

@ -1,14 +1,14 @@
#include "svdpi.h"
#include "Vchip8__Dpi.h"
#include <iostream>
#include <memory.h>
#include "Vchip8.h" #include "Vchip8.h"
#include "Vchip8__Dpi.h"
#include "svdpi.h"
#include "verilated.h" #include "verilated.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <SDL2/SDL_quit.h> #include <SDL2/SDL_quit.h>
#include <SDL2/SDL_timer.h> #include <SDL2/SDL_timer.h>
#include <inttypes.h> #include <inttypes.h>
#include <iostream>
#include <memory.h>
#define SCREEN_WIDTH 64 #define SCREEN_WIDTH 64
#define SCREEN_HEIGHT 32 #define SCREEN_HEIGHT 32
@ -18,20 +18,19 @@ FILE *rom_file;
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
SDL_Texture *texture; SDL_Texture *texture;
char* rom_name; char *rom_name;
void init_screen() { void init_screen() {
SDL_Init(SDL_INIT_EVERYTHING); SDL_Init(SDL_INIT_EVERYTHING);
window = window = SDL_CreateWindow(
SDL_CreateWindow("Yet Another Yet Another Chip-8 Emulator", // creates a window "Yet Another Yet Another Chip-8 Emulator", // creates a window
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH * 10,
SCREEN_WIDTH * 10, SCREEN_HEIGHT * 10, 0); SCREEN_HEIGHT * 10, 0);
renderer = renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH,
SCREEN_WIDTH, SCREEN_HEIGHT); SCREEN_HEIGHT);
std::cout << "INF_EMU: Screen initialized" << '\n'; std::cout << "INF_EMU: Screen initialized" << '\n';
} }
@ -42,9 +41,9 @@ void set_beep(const svBit beep) {
SDL_SetTextureColorMod(texture, 255, 255, 255); SDL_SetTextureColorMod(texture, 255, 255, 255);
} }
void draw_screen(const svLogicVecVal* vram) { void draw_screen(const svLogicVecVal *vram) {
uint32_t *screen = (uint32_t*) malloc(SCREEN_WIDTH*SCREEN_HEIGHT*32); uint32_t *screen = (uint32_t *)malloc(SCREEN_WIDTH * SCREEN_HEIGHT * 32);
for(int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) { for (int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) {
screen[i] = vram[i].aval; screen[i] = vram[i].aval;
} }
SDL_UpdateTexture(texture, NULL, screen, sizeof(screen[0]) * SCREEN_WIDTH); SDL_UpdateTexture(texture, NULL, screen, sizeof(screen[0]) * SCREEN_WIDTH);
@ -72,61 +71,56 @@ int load_rom() {
return rom_size; return rom_size;
} }
svBitVecVal get_next_instr() { svBitVecVal get_next_instr() { return (uint8_t)fgetc(rom_file); }
return (uint8_t) fgetc(rom_file);
}
void close_rom() { void close_rom() { fclose(rom_file); }
fclose(rom_file);
}
svBitVecVal get_key() { svBitVecVal get_key() {
SDL_Event event; SDL_Event event;
uint8_t down = 0; uint8_t down = 0;
while(SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch(event.type) { switch (event.type) {
case SDL_KEYDOWN: case SDL_KEYDOWN:
down = 128; down = 128;
case SDL_KEYUP: case SDL_KEYUP:
switch(event.key.keysym.sym) { switch (event.key.keysym.sym) {
case SDLK_0: case SDLK_0:
return down | (uint8_t) 0; return down | (uint8_t)0;
case SDLK_1: case SDLK_1:
return down | (uint8_t) 1; return down | (uint8_t)1;
case SDLK_2: case SDLK_2:
return down | (uint8_t) 2; return down | (uint8_t)2;
case SDLK_3: case SDLK_3:
return down | (uint8_t) 3; return down | (uint8_t)3;
case SDLK_4: case SDLK_4:
return down | (uint8_t) 4; return down | (uint8_t)4;
case SDLK_5: case SDLK_5:
return down | (uint8_t) 5; return down | (uint8_t)5;
case SDLK_6: case SDLK_6:
return down | (uint8_t) 6; return down | (uint8_t)6;
case SDLK_7: case SDLK_7:
return down | (uint8_t) 7; return down | (uint8_t)7;
case SDLK_8: case SDLK_8:
return down | (uint8_t) 8; return down | (uint8_t)8;
case SDLK_9: case SDLK_9:
return down | (uint8_t) 9; return down | (uint8_t)9;
case SDLK_a: case SDLK_a:
return down | (uint8_t) 10; return down | (uint8_t)10;
case SDLK_b: case SDLK_b:
return down | (uint8_t) 11; return down | (uint8_t)11;
case SDLK_c: case SDLK_c:
return down | (uint8_t) 12; return down | (uint8_t)12;
case SDLK_d: case SDLK_d:
return down | (uint8_t) 13; return down | (uint8_t)13;
case SDLK_e: case SDLK_e:
return down | (uint8_t) 14; return down | (uint8_t)14;
case SDLK_f: case SDLK_f:
return down | (uint8_t) 15; return down | (uint8_t)15;
default: default:
return 255; return 255;
} }
default: default:
return 255; return 255;
} }
@ -134,7 +128,7 @@ svBitVecVal get_key() {
return 255; return 255;
} }
int main(int argc, char** argv) { int main(int argc, char **argv) {
if (argc < 2) { if (argc < 2) {
std::cout << "Use: yayacemu [ROM_NAME]" << '\n'; std::cout << "Use: yayacemu [ROM_NAME]" << '\n';
exit(1); exit(1);
@ -142,15 +136,15 @@ int main(int argc, char** argv) {
rom_name = argv[1]; rom_name = argv[1];
VerilatedContext* contextp = new VerilatedContext; VerilatedContext *contextp = new VerilatedContext;
contextp->commandArgs(argc, argv); contextp->commandArgs(argc, argv);
Vchip8* dut = new Vchip8{contextp}; Vchip8 *dut = new Vchip8{contextp};
while (true) { while (true) {
dut->clk_in ^= 1; dut->clk_in ^= 1;
dut->eval(); dut->eval();
usleep(1000000/EMULATION_HZ); usleep(1000000 / EMULATION_HZ);
if (SDL_QuitRequested()) { if (SDL_QuitRequested()) {
std::cout << "INF_EMU: Received Quit from SDL. Goodbye!" << '\n'; std::cout << "INF_EMU: Received Quit from SDL. Goodbye!" << '\n';
break; break;