Pixel block fixed

This commit is contained in:
Vinicius Silva 2024-02-21 12:31:22 -03:00
parent e671c8c71b
commit 0bf36e06f1
2 changed files with 24 additions and 91 deletions

View File

@ -61,8 +61,9 @@ struct Screen{
const mem_size = 4096 const mem_size = 4096
const num_of_registers = 16 const num_of_registers = 16
const f = 15 const f = 15
struct Chip8{ struct Chip8{
timer_clock u8 = 0x9
pub mut: pub mut:
ram [mem_size]u8 ram [mem_size]u8
v [num_of_registers]u8 v [num_of_registers]u8
@ -74,6 +75,7 @@ struct Chip8{
sound_timer u8 sound_timer u8
is_draw bool is_draw bool
key u8 key u8
cycles u8
} }
fn (mut chip Chip8) start_cpu(){ fn (mut chip Chip8) start_cpu(){
@ -102,6 +104,11 @@ fn (mut chip Chip8) set_ram(instructions []u8, index u16) {
} }
} }
fn (mut chip Chip8) update_timers(){
if chip.delay_timer > 0 { chip.delay_timer-- }
if chip.sound_timer > 0 { chip.sound_timer-- }
}
fn (mut chip Chip8) fetch() u16{ fn (mut chip Chip8) fetch() u16{
mut instruction := u16(0x00) mut instruction := u16(0x00)
@ -126,20 +133,17 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
mut is_jump := false mut is_jump := false
chip.is_draw = false chip.is_draw = false
if chip.delay_timer > 0 { chip.delay_timer-- }
if chip.sound_timer > 0 { chip.sound_timer-- }
match opcode_msb{ match opcode_msb{
0x0000 { 0x0000 {
match opcode_lsb { match opcode_lsb {
0xEE { 0x00EE {
chip.pc = chip.stack.pop() or { panic(err) } chip.pc = chip.stack.pop() or { panic(err) }
// Returns from a subroutine // Returns from a subroutine
} }
0xE0 { 0x00E0 {
chip.is_draw = true chip.is_draw = true
for i := 0; i < chip.screen.display_height; i++{ for i := 0; i < chip.screen.display_height; i++{
for j := 0; j < chip.screen.display_width; j++ { for j := 0; j < chip.screen.display_width; j++ {
@ -154,8 +158,8 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
//} //}
else{ else{
nnn = instruction & 0x0FFF //nnn = instruction & 0x0FFF
//panic('Invalid instruction! 0x${instruction.hex()}') panic('Invalid instruction! 0x${instruction.hex()}')
} }
} }
} }
@ -341,8 +345,8 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
y = (instruction & 0x00F0) >> 4 y = (instruction & 0x00F0) >> 4
n = (instruction & 0x000F) n = (instruction & 0x000F)
mut regvy := u16(chip.v[y] % chip.screen.display_height) mut regvy := u16(chip.v[y])
mut regvx := u16(chip.v[x] % chip.screen.display_width) mut regvx := u16(chip.v[x])
chip.v[f] = 0 chip.v[f] = 0
@ -413,13 +417,13 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
} }
0x29{ 0x29{
chip.i = chip.v[x] * 5 chip.i = chip.v[x] * 0x5
} }
0x33{ 0x33{
chip.ram[chip.i] = chip.v[x] / 100; chip.ram[chip.i] = chip.v[x] / 100;
chip.ram[chip.i + 1] = (chip.v[x] / 10) % 10 chip.ram[chip.i + 1] = (chip.v[x] / 10) % 10
chip.ram[chip.i + 2] = chip.v[x] % 10; chip.ram[chip.i + 2] = (chip.v[x] % 100) % 10;
} }
0x55{ 0x55{
@ -427,14 +431,14 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
for i := 0; i <= x; i++ { for i := 0; i <= x; i++ {
chip.ram[chip.i + i] = chip.v[i] chip.ram[chip.i + i] = chip.v[i]
} }
chip.i = u16(chip.i + x + 1) chip.i = u16(x + 1)
} }
0x65{ 0x65{
for i := 0; i <= x; i++ { for i := 0; i <= x; i++ {
chip.v[x] = chip.ram[chip.i + x] chip.v[x] = chip.ram[chip.i + i]
} }
chip.i = u16(chip.i + x + 1) chip.i = u16(x + 1)
} }
else { else {

View File

@ -13,7 +13,7 @@ struct Emulator{
} }
fn (mut emulator Emulator) draw_block(i f32, j f32) { fn (mut emulator Emulator) draw_block(i f32, j f32) {
emulator.graphic.draw_rect_filled(j,i, f32(20-1), f32(20-1), gx.rgb(0,255,0)) emulator.graphic.draw_rect_filled(j,i, f32(20), f32(20), gx.rgb(0,255,0))
} }
fn (mut emulator Emulator) load_rom() !{ fn (mut emulator Emulator) load_rom() !{
@ -44,6 +44,8 @@ fn frame(mut emulator Emulator){
emulator.graphic.begin() emulator.graphic.begin()
emulator.chip8.run() emulator.chip8.run()
emulator.chip8.cycles++;
emulator.chip8.update_timers()
display_height := emulator.chip8.screen.display_height display_height := emulator.chip8.screen.display_height
display_width := emulator.chip8.screen.display_width display_width := emulator.chip8.screen.display_width
@ -54,10 +56,11 @@ fn frame(mut emulator Emulator){
pixel := emulator.chip8.screen.display[y][x] pixel := emulator.chip8.screen.display[y][x]
if pixel == 1 { if pixel == 1 {
emulator.draw_block(f32((y-1)*20), f32((x-1)*20)) emulator.draw_block(f32((y)*20), f32((x)*20))
} }
} }
} }
emulator.graphic.end() emulator.graphic.end()
} }
@ -65,80 +68,6 @@ fn (mut emulator Emulator) show_display(){
emulator.graphic.run() emulator.graphic.run()
} }
//fn (emulator Emulator) keyboard(input string) !string{
//
// match input {
// '1' {
// return 0x0001
// },
//
// '2' {
//
// },
//
// '3' {
//
// },
//
// '4' {
//
// },
//
// 'Q', 'q' {
//
// },
//
// 'W', 'w' {
//
// },
//
// 'E', 'e' {
//
// },
//
// 'R', 'r' {
//
// },
//
// 'A', 'a' {
//
// },
//
// 'S', 's' {
//
// },
//
// 'D', 'd' {
//
// },
//
// 'F', 'f' {
//
// },
//
// 'Z', 'z' {
//
// },
//
// 'X', 'x' {
//
// },
//
// 'C', 'c' {
//
// },
//
// 'V', 'v' {
//
// },
//
// else {
// panic('Invalid key!')
// }
// }
//
//}
fn is_graphic() bool{ fn is_graphic() bool{
return os.environ()['DISPLAY'] != '' return os.environ()['DISPLAY'] != ''
} }