chip8 draws in st7920 format now

This commit is contained in:
Nicholas Orlowsky 2024-04-06 21:36:24 -05:00
parent 9804af7796
commit fdd6553f11
Signed by: nickorlow
GPG key ID: 838827D8C4611687
4 changed files with 47 additions and 15 deletions

View file

@ -3,7 +3,7 @@ module chip8 (
input wire rst_in input wire rst_in
); );
bit vram[0:2047]; bit [7:0] vram[0:1023];
bit [7:0] memory[0:4095]; bit [7:0] memory[0:4095];
bit keyboard[15:0]; bit keyboard[15:0];

38
cpu.sv
View file

@ -6,7 +6,7 @@ module cpu (
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 vram[0:2047], output wire [7:0] vram[0:1023],
output wire [7:0] sound_timer output wire [7:0] sound_timer
); );
@ -32,6 +32,36 @@ module cpu (
logic screen_pixel; logic screen_pixel;
logic [7:0] sprite_pixel; logic [7:0] sprite_pixel;
task write_pixels;
input [31:0] x;
input [31:0] y;
int i;
begin
// bottom left
i = (y*128*2) + x*2 +127;
if (vram[i/8][7-(i%8)] == 1) begin
registers[15] = 1;
end
vram[i/8][7-(i%8)] ^= 1;
// bottom right
i = (y*128*2) + x*2 +128;
vram[i/8][7-(i%8)] ^= 1;
// top left
i = (y*128*2) + x*2-1;
vram[i/8][7-(i%8)] ^= 1;
// top right
i = (y*128*2) + x*2;
vram[i/8][7-(i%8)] ^= 1;
end
endtask
always_ff @(negedge clk_in) begin always_ff @(negedge clk_in) begin
if (system_ready) begin if (system_ready) begin
opcode = {memory[program_counter+0], memory[program_counter+1]}; opcode = {memory[program_counter+0], memory[program_counter+1]};
@ -187,14 +217,10 @@ 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) continue; if (r + y_cord >= 32 || x_cord + c >= 64) continue;
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 == 1) begin write_pixels(x_cord + c, r+y_cord);
registers[15] = 1;
end
vram[((r+y_cord)*64)+(x_cord+c)] ^= 1;
end end
end end
end end

6
gpu.sv
View file

@ -1,14 +1,16 @@
module gpu ( module gpu (
input wire vram[0:2047] input wire [7:0] vram[0:1023]
); );
import "DPI-C" function void init_screen(); import "DPI-C" function void init_screen();
import "DPI-C" function void draw_screen(logic vram[0:2047]); import "DPI-C" function void draw_screen(logic [7:0] vram[0:1023]);
initial begin initial begin
init_screen(); init_screen();
end end
always_comb begin always_comb begin
draw_screen(vram); draw_screen(vram);
end end

View file

@ -10,8 +10,8 @@
#include <iostream> #include <iostream>
#include <memory.h> #include <memory.h>
#define SCREEN_WIDTH 64 #define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32 #define SCREEN_HEIGHT 64
#define EMULATION_HZ 2000 #define EMULATION_HZ 2000
FILE *rom_file; FILE *rom_file;
@ -41,10 +41,14 @@ void set_beep(const svBit beep) {
SDL_SetTextureColorMod(texture, 255, 255, 255); SDL_SetTextureColorMod(texture, 255, 255, 255);
} }
void draw_screen(const svLogic *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 < 1024; i++) {
screen[i] = vram[i] == 1 ? 0xFFFFFFFF : 0x00000000; uint8_t line_byte = (uint8_t) vram[i].aval;
for (int j = 0; j < 8; j++) {
uint8_t pixel_val = (line_byte >> (7-j)) & 1;
screen[(i*8) + j] = pixel_val == 1 ? 0xFFFFFFFF : 0x00000000;
}
} }
SDL_UpdateTexture(texture, NULL, screen, sizeof(screen[0]) * SCREEN_WIDTH); SDL_UpdateTexture(texture, NULL, screen, sizeof(screen[0]) * SCREEN_WIDTH);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);