Finalize chip8 and init fix graphics

This commit is contained in:
Vinicius Silva 2024-02-19 22:21:14 -03:00
parent b674df5a59
commit 835f969963
5 changed files with 51 additions and 45 deletions

0
gl
View File

0
sgl
View File

View File

@ -39,35 +39,32 @@ struct Chip8{
v [num_of_registers]u8
screen Screen
pc u16 = 0x200
i int
i u16
stack Stack
delay_timer u8
is_draw bool
}
fn (mut chip Chip8) start_cpu(){
chip.screen = Screen{}
mut index := 0
// load font in the memory
for sprite in font {
chip.set_ram(sprite, index)
chip.set_ram(sprite, chip.i)
}
}
fn (mut chip Chip8) run(){
for{
mut instruction := chip.fetch()
println(instruction)
chip.decode_and_run(instruction)
}
}
fn (mut chip Chip8) set_ram(instructions []u8, index int) {
fn (mut chip Chip8) set_ram(instructions []u8, index u16) {
mut j := index
for i := 0; i < instructions.len; i++ {
chip.ram[j] = instructions[i]
j++
@ -96,6 +93,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
mut opcode_msb := instruction & 0xF000
mut opcode_lsb := instruction & 0x00FF
mut is_jump := false
chip.is_draw = false
match opcode_msb{
@ -108,7 +106,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
}
0x00E0 {
chip.is_draw = true
for i := 0; i < chip.screen.display_height; i++{
for j := 0; j < chip.screen.display_width; j++ {
chip.screen.display[i][j] = 0
@ -125,7 +123,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
0x1000 {
nnn = instruction & 0x0FFF
chip.pc = u8(nnn)
chip.pc = u16(nnn)
is_jump = true
// Jumps to address NNN
}
@ -278,7 +276,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
0xA000 {
nnn = instruction & 0x0FFF
chip.i = nnn
chip.i = u16(nnn)
}
0xB000 {
@ -298,37 +296,32 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
}
0xD000 {
chip.is_draw = true
x = (instruction & 0x0F00) >> 8
y = (instruction & 0x00F0) >> 4
n = (instruction & 0x000F)
mut regvy := u8(chip.v[y] % chip.screen.display_height)
mut regvx := u8(chip.v[x] % chip.screen.display_width)
mut regvy := u16(chip.v[y])
mut regvx := u16(chip.v[x])
chip.v[f] = 0
for y_coord := 0; y_coord < n; y_coord++ {
regn := chip.ram[chip.i + y_coord]
pixel := chip.ram[chip.i + y_coord]
for x_coord := 0; x_coord < 8; x_coord++ {
if (regvx + x_coord) < chip.screen.display_width && (regvy + y_coord) < chip.screen.display_height {
if regn & (0x80 >> x_coord) == 1 && chip.screen.display[regvy + y_coord][regvx + x_coord] == 1 {
if pixel & (0x80 >> x_coord) == 1 {
if chip.screen.display[regvy + y_coord][regvx + x_coord] == 1 {
chip.v[f] = 1
chip.screen.display[regvy + y_coord][regvx + x_coord] = 0
//chip.screen.display[regvy + y_coord][regvx + x_coord] = 0
}
chip.screen.display[regvy + y_coord][regvx + x_coord] = chip.ram[chip.i + y_coord]
regvx = u8(chip.v[x + x_coord] % chip.screen.display_width)
chip.screen.display[regvy + y_coord][regvx + x_coord] ^= 1
}
}
regvy = u8(chip.v[y + y_coord] % chip.screen.display_height)
}
}

View File

@ -13,7 +13,7 @@ struct Emulator{
}
fn (mut emulator Emulator) draw_block(i f32, j f32) {
emulator.graphic.draw_rect_filled(f32((j - 1) * 20) + 14, f32((i - 1) * 20), f32(20 - 1), f32(20 - 1), gx.rgb(255,255,255))
emulator.graphic.draw_rect_filled(j,i, f32(20-1), f32(20-1), gx.rgb(0,255,0))
}
fn (mut emulator Emulator) load_rom() !{
@ -28,8 +28,8 @@ fn (mut emulator Emulator) load_rom() !{
println(' Loading ROM in the memory...\n')
load_animate()
mut instructions := file.read_bytes_at(1024, 0)
mut index := 0x200
mut instructions := file.read_bytes(1024)
mut index := u16(0x200)
emulator.chip8.set_ram(instructions, index)
println('ROM successfully loaded into memory!')
@ -50,7 +50,7 @@ fn draw_screen(mut emulator Emulator){
pixel := emulator.chip8.screen.display[y][x]
if pixel == 1 {
emulator.draw_block(f32(y*10), f32(x*10))
emulator.draw_block(f32((y-1)*20), f32((x-1)*20))
}
}
}
@ -152,8 +152,7 @@ fn main() {
emulator.load_rom()!
emulator.chip8.start_cpu()
emulator.chip8.run()
print('oi')
emulator.graphic = gg.new_context(
bg_color: gx.rgb(0, 0, 0)
width: 1280
@ -162,7 +161,21 @@ fn main() {
frame_fn : draw_screen
user_data: emulator
)
//emulator.show_display()
for{
emulator.chip8.run()
emulator.graphic.quit()
if emulator.chip8.is_draw {
emulator.graphic.begin()
emulator.show_display()
emulator.graphic.end()
}
}
}else{
panic('System is not graphic!')
}

View File

@ -33,7 +33,7 @@ fn load_animate() {
mut bars := ['|','/','-','\\']
for i := 0; i < 2000; i++ {
for i := 0; i < 4000; i++ {
print('[${bars[i%4]}]\r ')
time.sleep(400000)
}