Skip to content

Commit

Permalink
part of the panic message in the ROM is getting copied to vram
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed May 26, 2024
1 parent a658400 commit 2c04eec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
38 changes: 21 additions & 17 deletions examples/paddle_ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ use gba::{
asm_runtime::USER_IRQ_HANDLER,
bios::VBlankIntrWait,
gba_cell::GbaCell,
mmio::{DISPCNT, DISPSTAT, IE, IME, KEYINPUT, MODE3_VRAM},
mmio::{
DISPCNT, DISPSTAT, DMA3_CONTROL, DMA3_DESTINATION, DMA3_SOURCE,
DMA3_TRANSFER_COUNT, IE, IME, KEYINPUT, MODE3_VRAM,
},
video::{Color, DisplayControl, DisplayStatus},
IrqBits,
IrqBits, KeyInput,
};

const SCREEN_WIDTH: u16 = 240;
Expand All @@ -39,8 +42,7 @@ impl Paddle {
Self { x, y }
}

fn update(&mut self) {
let keys = KEYINPUT.read();
fn update(&mut self, keys: KeyInput) {
if keys.up() && self.y > 1 {
self.y -= 1;
}
Expand Down Expand Up @@ -106,10 +108,7 @@ static SPRITE_POSITIONS: [GbaCell<u16>; 6] = [
GbaCell::new(0),
];

#[panic_handler]
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
loop {}
}
gba::panic_handler!(empty_loop);

#[no_mangle]
fn main() -> ! {
Expand All @@ -129,8 +128,9 @@ fn main() -> ! {
let mut ball = Ball::new(SCREEN_WIDTH as u16 / 2, SCREEN_HEIGHT as u16 / 2);

loop {
left_paddle.update();
right_paddle.update();
let keys = KEYINPUT.read();
left_paddle.update(keys);
right_paddle.update(keys);
ball.update(&left_paddle, &right_paddle);

SPRITE_POSITIONS[0].write(left_paddle.x);
Expand All @@ -145,31 +145,35 @@ fn main() -> ! {
}

extern "C" fn draw_sprites(_bits: IrqBits) {
MODE3_VRAM
.into_block::<{ 240 * 160 }>()
.iter()
.for_each(|a| a.write(Color::BLACK));
unsafe {
// Clear VRAM using DMA3
let x = &0_u32;
DMA3_SOURCE.write((x as *const u32).cast());
DMA3_DESTINATION.write(MODE3_VRAM.as_usize() as *mut _);
DMA3_TRANSFER_COUNT.write(240 * 160 / 2);
DMA3_CONTROL.write(1 << 15 | 1 << 10 | 2 << 7);
}

draw_rect(
SPRITE_POSITIONS[0].read(),
SPRITE_POSITIONS[1].read(),
PADDLE_WIDTH,
PADDLE_HEIGHT,
Color::WHITE,
Color::RED,
);
draw_rect(
SPRITE_POSITIONS[2].read(),
SPRITE_POSITIONS[3].read(),
PADDLE_WIDTH,
PADDLE_HEIGHT,
Color::WHITE,
Color::GREEN,
);
draw_rect(
SPRITE_POSITIONS[4].read(),
SPRITE_POSITIONS[5].read(),
BALL_SIZE,
BALL_SIZE,
Color::WHITE,
Color::CYAN,
);
}

Expand Down
15 changes: 14 additions & 1 deletion src/mmio.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Definitions for Memory-mapped IO (hardware control).

use core::ffi::c_void;

use bitfrob::u8x2;
#[allow(unused_imports)]
use voladdress::VolAddress;
use voladdress::{VolBlock, VolGrid2d, VolGrid2dStrided};
use voladdress::{Unsafe, VolBlock, VolGrid2d, VolGrid2dStrided};

use crate::{
video::{Color, DisplayControl, DisplayStatus, Tile4bpp},
Expand All @@ -21,6 +23,8 @@ type SOGBA = voladdress::Unsafe;
type PlainAddr<T> = VolAddress<T, SOGBA, SOGBA>;
/// Read-only addr
type RoAddr<T> = VolAddress<T, SOGBA, ()>;
/// Write-only addr
type WoAddr<T> = VolAddress<T, (), SOGBA>;

/// Display Control setting.
///
Expand All @@ -43,6 +47,15 @@ pub const DISPSTAT: PlainAddr<DisplayStatus> =
/// Values of 160 to 227 indicate that a vertical blank line is happening.
pub const VCOUNT: RoAddr<u8> = unsafe { VolAddress::new(0x0400_0006) };

pub const DMA3_SOURCE: WoAddr<*const c_void> =
unsafe { VolAddress::new(0x0400_00D4) };
pub const DMA3_DESTINATION: WoAddr<*mut c_void> =
unsafe { VolAddress::new(0x0400_00D8) };
pub const DMA3_TRANSFER_COUNT: WoAddr<u16> =
unsafe { VolAddress::new(0x0400_00DC) };
pub const DMA3_CONTROL: VolAddress<u16, SOGBA, Unsafe> =
unsafe { VolAddress::new(0x0400_00DE) };

/// Key Input (read-only).
///
/// Gives the low-active button state of all system buttons.
Expand Down

0 comments on commit 2c04eec

Please sign in to comment.