Skip to content

Commit

Permalink
Parse GameBoy to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmann committed Jan 2, 2024
1 parent 21dcf77 commit aed0865
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 145 deletions.
79 changes: 38 additions & 41 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions gameboy-lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion gameboy-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
serialize = ["serde", "serde_derive"]
wasm = ["wasm-bindgen"]

[dependencies]
wasm-bindgen = { version = "0.2.84", optional = true}
wasm-bindgen = { version = "0.2.84", optional = true}
serde = { version = "1.0.194", optional = true }
serde_derive = { version = "1.0.194", optional = true }
4 changes: 3 additions & 1 deletion gameboy-lib/src/cpu/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use serde_derive::Serialize;
use crate::cpu::instructions::{JumpCondition, Target16Bit};
use crate::cpu::registers::{Register8BitName, Registers};
use crate::memory::memory::Memory;
use crate::memory::Memory;

use super::instructions::Target8Bit;
use super::instructions::{Instruction, Source16Bit};
use super::registers::Register16BitName;

#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct CPU {
pub register: Registers,
pub pc: u16,
Expand Down
3 changes: 3 additions & 0 deletions gameboy-lib/src/cpu/registers/flag_register.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use serde_derive::Serialize;

#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct FlagRegister {
pub zero: bool,
pub subtract: bool,
Expand Down
2 changes: 2 additions & 0 deletions gameboy-lib/src/cpu/registers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde_derive::Serialize;
use super::registers::flag_register::FlagRegister;
use super::registers::register::Register;
use super::registers::register_8bit::Register8Bit;
Expand All @@ -9,6 +10,7 @@ mod register_8bit;
mod stack_pointer;

#[derive(Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct Registers {
a: Register8Bit,
b: Register8Bit,
Expand Down
2 changes: 2 additions & 0 deletions gameboy-lib/src/cpu/registers/register_8bit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde_derive::Serialize;
use super::register::Register;

#[derive(Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct Register8Bit {
name: String,
value: u8,
Expand Down
2 changes: 2 additions & 0 deletions gameboy-lib/src/cpu/registers/stack_pointer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde_derive::Serialize;
use super::Register;

#[derive(Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct StackPointer {
name: String,
value: u16
Expand Down
4 changes: 1 addition & 3 deletions gameboy-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::cpu::cpu::CPU;
use crate::cpu::registers::Registers;
use crate::memory::memory::Memory;

pub mod cpu;
pub mod memory;
Expand All @@ -25,4 +23,4 @@ impl GameBoy {
self.cpu.step();
}
}
}
}
23 changes: 4 additions & 19 deletions gameboy-lib/src/memory/memory.rs → gameboy-lib/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use crate::memory::observer::{Event, Publisher, Subject};
use serde_derive::Serialize;

pub const BOOT_ROM_BEGIN: usize = 0x00;
pub const BOOT_ROM_END: usize = 0xFF;
pub const BOOT_ROM_SIZE: usize = BOOT_ROM_END - BOOT_ROM_BEGIN + 1;


#[derive(Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct Memory {
publisher: Publisher,
pub memory: [u8; 0xFFFF],
pub memory: Vec<u8>,
}


impl Default for Memory {
fn default() -> Self {
Memory {
publisher: Publisher::default(),
memory: [0; 0xFFFF],
memory: vec![0; 0xFFFF],
}
}
}
Expand All @@ -28,32 +27,18 @@ impl Memory {
}
}

pub fn events(&mut self) -> &mut Publisher {
return &mut self.publisher;
}

pub fn write_byte(&mut self, address: u16, value: u8) {
// println!("[MEM] Writing memory: address: 0x{:x} value: 0x{:x}", address, value);
self.memory[address as usize] = value;
self.publisher.notify(Event::Write, self.create_subject(value, address));
}

pub fn read_byte(&self, address: u16) -> u8 {
let mem_value = self.memory[address as usize];
// println!("[MEM] Reading memory: address: 0x{:x} value: 0x{:x}", address, mem_value);
self.publisher.notify(Event::Read, self.create_subject(mem_value, address));
return mem_value;
}

pub fn read_next_word(&self, pc: u16) -> u16 {
((self.read_byte(pc + 2) as u16) << 8) | (self.read_byte(pc + 1) as u16)
}

fn create_subject(&self, value: u8, address: u16) -> Subject {
return Subject {
value,
address,
memory: self.memory.clone(),
};
}
}
2 changes: 0 additions & 2 deletions gameboy-lib/src/memory/mod.rs

This file was deleted.

41 changes: 0 additions & 41 deletions gameboy-lib/src/memory/observer.rs

This file was deleted.

12 changes: 5 additions & 7 deletions gameboy-web/gameboy-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ crate-type = ["cdylib", "rlib"]
default = ["console_error_panic_hook"]

[dependencies]
gameboy = { path = "../../gameboy-lib", features = ["wasm"] }
wasm-bindgen = "0.2.84"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
gameboy = { path = "../../gameboy-lib", features = ["wasm", "serialize"] }
wasm-bindgen = { version = "0.2.84", features = ["serde-serialize"] }
console_error_panic_hook = { version = "0.1.7", optional = true }
log = "0.4.20"
serde = "1.0.194"
serde_derive = "1.0.194"
serde-wasm-bindgen = "0.6.3"

[dev-dependencies]
wasm-bindgen-test = "0.3.34"
Expand Down
Loading

0 comments on commit aed0865

Please sign in to comment.