Skip to content

Commit

Permalink
psx: Make video mode configurable in Framebuffer and DispEnv init
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrtonm committed Dec 20, 2023
1 parent b1a1664 commit 8b09e31
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion examples/hello_world/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![no_main]

use psx::gpu::VideoMode;
use psx::{dprintln, Framebuffer};

#[no_mangle]
Expand All @@ -9,7 +10,7 @@ fn main() {
let buf1 = (0, 240);
let res = (320, 240);
let txt_offset = (0, 8);
let mut fb = Framebuffer::new(buf0, buf1, res, None).unwrap();
let mut fb = Framebuffer::new(buf0, buf1, res, VideoMode::NTSC, None).unwrap();
let font = fb.load_default_font();
let mut txt = font.new_text_box(txt_offset, res);
loop {
Expand Down
12 changes: 8 additions & 4 deletions psx/src/framebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct Framebuffer {
impl Default for Framebuffer {
fn default() -> Self {
// SAFETY: The framebuffer parameters are valid.
unsafe { Self::new((0, 0), (0, 240), (320, 240), None).unwrap_unchecked() }
unsafe { Self::new((0, 0), (0, 240), (320, 240), VideoMode::NTSC, None).unwrap_unchecked() }
}
}

Expand All @@ -53,7 +53,8 @@ impl Framebuffer {
/// `None`). Also resets the GPU, enables DMA to GP0 on the GPU-side and
/// enables the display.
pub fn new(
buf0: (i16, i16), buf1: (i16, i16), res: (i16, i16), bg_color: Option<Color>,
buf0: (i16, i16), buf1: (i16, i16), res: (i16, i16), video_mode: VideoMode,
bg_color: Option<Color>,
) -> Result<Self, VertexError> {
let mut fb = Framebuffer {
// These registers are read-only
Expand All @@ -63,7 +64,10 @@ impl Framebuffer {
// wait_vblank will reload this anyway
irq_status: irq::Status::skip_load(),
irq_mask: irq::Mask::new(),
disp_envs: [DispEnv::new(buf0, res)?, DispEnv::new(buf1, res)?],
disp_envs: [
DispEnv::new(buf0, res, video_mode)?,
DispEnv::new(buf1, res, video_mode)?,
],
draw_envs: [
Packet::new(DrawEnv::new(buf1, res, bg_color)?),
Packet::new(DrawEnv::new(buf0, res, bg_color)?),
Expand All @@ -73,7 +77,7 @@ impl Framebuffer {
GP1::skip_load()
.reset_gpu()
.dma_mode(Some(DMAMode::GP0))
.display_mode(res, VideoMode::NTSC, Depth::Bits15, false)?
.display_mode(res, video_mode, Depth::Bits15, false)?
.enable_display(true);
fb.irq_mask.enable_irq(IRQ::Vblank).store();
//fb.wait_vblank();
Expand Down
11 changes: 9 additions & 2 deletions psx/src/gpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,18 @@ pub struct DispEnv {
impl DispEnv {
/// Creates a new display buffer at `offset` in VRAM with the specified
/// `size`.
pub fn new(offset: (i16, i16), size: (i16, i16)) -> Result<Self, VertexError> {
pub fn new(
offset: (i16, i16), size: (i16, i16), video_mode: VideoMode,
) -> Result<Self, VertexError> {
let offset = Vertex::new(offset);
let size = Vertex::new(size);
let offset = PackedVertex::try_from(offset)?;
let ntsc_vrange = Vertex(0x88 - (240 / 2), 0x88 + (240 / 2));
let (center, range) = if video_mode == VideoMode::NTSC {
(0x88, 0x240)
} else {
(0xA3, 0x256)
};
let ntsc_vrange = Vertex(center - (range / 2), center + (range / 2));
let hrange = Vertex(0x260, 0x260 + (size.0 * 8));

let horizontal_range = PackedVertex::try_from(hrange)?;
Expand Down

0 comments on commit 8b09e31

Please sign in to comment.