Finalize chip8 and init fix graphics
This commit is contained in:
parent
b674df5a59
commit
835f969963
43
src/chip8.v
43
src/chip8.v
|
|
@ -39,35 +39,32 @@ struct Chip8{
|
||||||
v [num_of_registers]u8
|
v [num_of_registers]u8
|
||||||
screen Screen
|
screen Screen
|
||||||
pc u16 = 0x200
|
pc u16 = 0x200
|
||||||
i int
|
i u16
|
||||||
stack Stack
|
stack Stack
|
||||||
delay_timer u8
|
delay_timer u8
|
||||||
|
is_draw bool
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut chip Chip8) start_cpu(){
|
fn (mut chip Chip8) start_cpu(){
|
||||||
|
|
||||||
chip.screen = Screen{}
|
chip.screen = Screen{}
|
||||||
|
|
||||||
mut index := 0
|
|
||||||
// load font in the memory
|
// load font in the memory
|
||||||
for sprite in font {
|
for sprite in font {
|
||||||
chip.set_ram(sprite, index)
|
chip.set_ram(sprite, chip.i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut chip Chip8) run(){
|
fn (mut chip Chip8) run(){
|
||||||
|
|
||||||
for{
|
|
||||||
mut instruction := chip.fetch()
|
mut instruction := chip.fetch()
|
||||||
println(instruction)
|
|
||||||
chip.decode_and_run(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
|
mut j := index
|
||||||
|
|
||||||
for i := 0; i < instructions.len; i++ {
|
for i := 0; i < instructions.len; i++ {
|
||||||
chip.ram[j] = instructions[i]
|
chip.ram[j] = instructions[i]
|
||||||
j++
|
j++
|
||||||
|
|
@ -96,6 +93,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
|
||||||
mut opcode_msb := instruction & 0xF000
|
mut opcode_msb := instruction & 0xF000
|
||||||
mut opcode_lsb := instruction & 0x00FF
|
mut opcode_lsb := instruction & 0x00FF
|
||||||
mut is_jump := false
|
mut is_jump := false
|
||||||
|
chip.is_draw = false
|
||||||
|
|
||||||
match opcode_msb{
|
match opcode_msb{
|
||||||
|
|
||||||
|
|
@ -108,7 +106,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
|
||||||
}
|
}
|
||||||
|
|
||||||
0x00E0 {
|
0x00E0 {
|
||||||
|
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++ {
|
||||||
chip.screen.display[i][j] = 0
|
chip.screen.display[i][j] = 0
|
||||||
|
|
@ -125,7 +123,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
|
||||||
|
|
||||||
0x1000 {
|
0x1000 {
|
||||||
nnn = instruction & 0x0FFF
|
nnn = instruction & 0x0FFF
|
||||||
chip.pc = u8(nnn)
|
chip.pc = u16(nnn)
|
||||||
is_jump = true
|
is_jump = true
|
||||||
// Jumps to address NNN
|
// Jumps to address NNN
|
||||||
}
|
}
|
||||||
|
|
@ -278,7 +276,7 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
|
||||||
0xA000 {
|
0xA000 {
|
||||||
nnn = instruction & 0x0FFF
|
nnn = instruction & 0x0FFF
|
||||||
|
|
||||||
chip.i = nnn
|
chip.i = u16(nnn)
|
||||||
}
|
}
|
||||||
|
|
||||||
0xB000 {
|
0xB000 {
|
||||||
|
|
@ -298,37 +296,32 @@ fn (mut chip Chip8) decode_and_run(instruction u16) {
|
||||||
}
|
}
|
||||||
|
|
||||||
0xD000 {
|
0xD000 {
|
||||||
|
|
||||||
|
chip.is_draw = true
|
||||||
x = (instruction & 0x0F00) >> 8
|
x = (instruction & 0x0F00) >> 8
|
||||||
y = (instruction & 0x00F0) >> 4
|
y = (instruction & 0x00F0) >> 4
|
||||||
n = (instruction & 0x000F)
|
n = (instruction & 0x000F)
|
||||||
|
|
||||||
mut regvy := u8(chip.v[y] % chip.screen.display_height)
|
mut regvy := u16(chip.v[y])
|
||||||
mut regvx := u8(chip.v[x] % chip.screen.display_width)
|
mut regvx := u16(chip.v[x])
|
||||||
|
|
||||||
chip.v[f] = 0
|
chip.v[f] = 0
|
||||||
|
|
||||||
for y_coord := 0; y_coord < n; y_coord++ {
|
for y_coord := 0; y_coord < n; y_coord++ {
|
||||||
|
|
||||||
|
pixel := chip.ram[chip.i + y_coord]
|
||||||
regn := chip.ram[chip.i + y_coord]
|
|
||||||
|
|
||||||
for x_coord := 0; x_coord < 8; x_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 pixel & (0x80 >> x_coord) == 1 {
|
||||||
|
if chip.screen.display[regvy + y_coord][regvx + x_coord] == 1 {
|
||||||
if regn & (0x80 >> x_coord) == 1 && chip.screen.display[regvy + y_coord][regvx + x_coord] == 1 {
|
|
||||||
chip.v[f] = 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] ^= 1
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
regvy = u8(chip.v[y + y_coord] % chip.screen.display_height)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
27
src/main.v
27
src/main.v
|
|
@ -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(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() !{
|
fn (mut emulator Emulator) load_rom() !{
|
||||||
|
|
@ -28,8 +28,8 @@ fn (mut emulator Emulator) load_rom() !{
|
||||||
println(' Loading ROM in the memory...\n')
|
println(' Loading ROM in the memory...\n')
|
||||||
load_animate()
|
load_animate()
|
||||||
|
|
||||||
mut instructions := file.read_bytes_at(1024, 0)
|
mut instructions := file.read_bytes(1024)
|
||||||
mut index := 0x200
|
mut index := u16(0x200)
|
||||||
emulator.chip8.set_ram(instructions, index)
|
emulator.chip8.set_ram(instructions, index)
|
||||||
|
|
||||||
println('ROM successfully loaded into memory!')
|
println('ROM successfully loaded into memory!')
|
||||||
|
|
@ -50,7 +50,7 @@ fn draw_screen(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*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.load_rom()!
|
||||||
emulator.chip8.start_cpu()
|
emulator.chip8.start_cpu()
|
||||||
emulator.chip8.run()
|
|
||||||
print('oi')
|
|
||||||
emulator.graphic = gg.new_context(
|
emulator.graphic = gg.new_context(
|
||||||
bg_color: gx.rgb(0, 0, 0)
|
bg_color: gx.rgb(0, 0, 0)
|
||||||
width: 1280
|
width: 1280
|
||||||
|
|
@ -162,7 +161,21 @@ fn main() {
|
||||||
frame_fn : draw_screen
|
frame_fn : draw_screen
|
||||||
user_data: emulator
|
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{
|
}else{
|
||||||
panic('System is not graphic!')
|
panic('System is not graphic!')
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ fn load_animate() {
|
||||||
|
|
||||||
mut bars := ['|','/','-','\\']
|
mut bars := ['|','/','-','\\']
|
||||||
|
|
||||||
for i := 0; i < 2000; i++ {
|
for i := 0; i < 4000; i++ {
|
||||||
print('[${bars[i%4]}]\r ')
|
print('[${bars[i%4]}]\r ')
|
||||||
time.sleep(400000)
|
time.sleep(400000)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue