Skip to content

Commit 1daffb4

Browse files
committed
Print - basic functionality implemented
1 parent 208014a commit 1daffb4

6 files changed

Lines changed: 54 additions & 30 deletions

File tree

p8rs-tests/carts/print/escape.p8

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ p8rs.test_scr("Escape nrbt")
1212
cls()
1313
print("\^wTest\^-wTest\^w", 0, 0)
1414
print("normal")
15-
print("\^=Test\^-=Test")
16-
print("\^=\^t\^wTest\^-=Test")
17-
print("\^=\^wTest\^-=Test")
18-
print("\^pTest\^-pTest")
15+
16+
-- TODO: implement pinball
17+
--print("\^=Test\^-=Test")
18+
--print("\^=\^t\^wTest\^-=Test")
19+
--print("\^=\^wTest\^-=Test")
20+
--print("\^pTest\^-pTest")
21+
1922
p8rs.test_scr("Escape ^")

p8rs-tests/carts/print/scrolling.p8

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ poke(0x5f58, 1 | (1<<2) | (1<<3))
2727

2828
for offset=0,7 do
2929
cls()
30-
print('==', 0, 20 + offset)
31-
for i=0,8 do
30+
print('==', 0, 90 + offset)
31+
for i=0,4 do
3232
print("line" .. i .. "\n--2--\n--3--")
3333
end
3434
p8rs.test_scr("Newlines, offset="..offset)

p8rs-tests/carts/print/scrolling_bottom.p8

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version 43
33

44
__lua__
55

6-
for offset=0,15 do
6+
for offset=0,10 do
77
cls()
88
local y = 115 + offset
99
print("xx", 0, y)
@@ -18,7 +18,7 @@ end
1818
-- large text
1919
poke(0x5f58, 1 | (1<<2) | (1<<3))
2020

21-
for offset=0,15 do
21+
for offset=0,10 do
2222
cls()
2323
local y = 115 + offset
2424
print("xx", 0, y)

p8rs/src/vm/api/print.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use core::alloc::Allocator;
22
use core::pin::Pin;
33
use std::ops::Not;
4+
use bitflags::bitflags;
45
use gc_arena::Collect;
6+
use p8rs_macros::TransparentRef;
57
use p8rs_piccolo::{BoxSequence, Callback, CallbackReturn, Context, Error, Execution, RuntimeError, RuntimeRef, Sequence, SequencePoll, Stack, String, Value};
68
use p8rs_types::p8num::P8Num;
7-
use p8rs_types::p8scii::Display;
89
use crate::vm::font::Font;
910
use crate::vm::memory::machine_state::{MiscChipsetFeatureFlags, PrintDefaultsFlags};
1011
use crate::vm::memory::painter::CallbackResult;
@@ -55,12 +56,7 @@ pub fn install_pico8_print<A: Allocator + 'static>(ctx: Context) {
5556
let flags = *rt.memory.machine_state().print_defaults().flags();
5657

5758
if y.is_none() {
58-
let (line_height, font_height) = get_line_height(rt, flags);
59-
println!("print({}) - cursor: {:?}", text, rt.get_cursor_position());
60-
println!("print: scrolling in case line would not fit, line_height={}, font_height={}", line_height, font_height);
6159
handle_newline(rt, NewlineRequest::MakeSpaceBeforePrint, flags, y.is_some());
62-
}else{
63-
println!("print({}, {}, {}) - cursor: {:?}", text, x.unwrap(), y.unwrap(), rt.get_cursor_position());
6460
}
6561

6662
Ok(CallbackReturn::Sequence(BoxSequence::new(ctx.mutation(), PrintSeq {
@@ -98,10 +94,33 @@ enum EscapeSequenceAction {
9894
ModifyFlags(PrintDefaultsFlags),
9995
}
10096

97+
#[derive(Copy, Clone, Debug, PartialEq, Eq, TransparentRef)]
98+
#[repr(transparent)]
99+
pub struct PrintStateFlags(u16);
100+
101+
bitflags! {
102+
impl PrintStateFlags: u16 {
103+
// const ENABLE = 1 << 0;
104+
const PADDING = 1 << 1;
105+
const WIDE = 1 << 2;
106+
const TALL = 1 << 3;
107+
const SOLID_BG = 1 << 4;
108+
const INVERT = 1 << 5;
109+
const DOTTY = 1 << 6;
110+
const CUSTOM_FONT = 1 << 7;
111+
112+
const PINBALL_DOTTY = 1 << 8;
113+
const WRAP = 1 << 9;
114+
const SCROLL = 1 << 10;
115+
const END_NEWLINE = 1 << 11;
116+
// TODO: finish internal print state
117+
118+
const _ = !0;
119+
}
120+
}
121+
101122
fn screen_shift_up_exact(rt: &mut Runtime, shift: u8) {
102-
println!("screen_shift_up_exact: scrolling, shift={}", shift);
103123
if rt.memory.machine_state().misc_chipset_flags().contains(MiscChipsetFeatureFlags::NO_PRINT_SCROLL) {
104-
println!("screen_shift_up_exact: jk actually NO_PRINT_SCROLL");
105124
return;
106125
}
107126

@@ -118,8 +137,6 @@ enum NewlineRequest {
118137
}
119138

120139
fn handle_newline(rt: &mut Runtime, request: NewlineRequest, flags: PrintDefaultsFlags, y_passed: bool) {
121-
println!("handle_newline({:?})", request);
122-
123140
let (line_height, font_height) = get_line_height(rt, flags);
124141

125142
let (reset_x, align_shift, advance_y, considered_height) = match request {
@@ -146,7 +163,6 @@ fn handle_newline(rt: &mut Runtime, request: NewlineRequest, flags: PrintDefault
146163
if align_shift && shift < font_height {
147164
shift = font_height;
148165
}
149-
println!("handle_newline - shifting - new_y: {}, max_y: {}, shift: {}", new_y, max_y, shift);
150166
screen_shift_up_exact(rt, shift);
151167
}
152168
}
@@ -185,17 +201,14 @@ fn execute_escape_sequence<'gc>(_ctx: Context<'gc>, rt: &mut Runtime, flags: Pri
185201
}
186202
b'-' => {
187203
if let Some(flag) = escape_to_print_flags(bytes[2]) {
188-
println!("Escape: ^- {}", Display(bytes));
189204
EscapeSequenceAction::ModifyFlags(flags & flag.not())
190205
}else{
191-
println!("Escape UNPARSED: {}", Display(bytes));
192206
debug!("unparsed sequence {:?}", bytes);
193207
EscapeSequenceAction::Nop
194208
}
195209
},
196210
_ => {
197211
if let Some(flag) = escape_to_print_flags(arg) {
198-
println!("Escape parsed: ^ {}", Display(bytes));
199212
return Ok(EscapeSequenceAction::ModifyFlags(flags | flag | PrintDefaultsFlags::ENABLE))
200213
}
201214
debug!("Unimplemented escape sequence! {:?}", bytes);

p8rs/src/vm/memory/machine_state.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ impl MachineState<'_> {
1616

1717
*self.pen_color() = 6;
1818
*self.clip_rect() = [0, 0, 128, 128];
19-
*self._cursor_position() = [0, 6];
19+
self._set_cursor_y(6);
20+
2021
*self.sprite_addr_map() = SpriteScreenMemoryMap::SPRITE_SHEET;
2122
*self.screen_addr_map() = SpriteScreenMemoryMap::SCREEN;
2223
*self.map_addr_map() = 0x20;
@@ -50,16 +51,23 @@ impl MachineState<'_> {
5051
self.const_slice(0x20)
5152
}
5253

53-
pub fn _cursor_home_x(&mut self) -> &mut u8 {
54-
&mut self[0x24]
54+
/// Cursor must be controlled via methods in `Runtime` instead of changing memory directly!
55+
pub(crate) fn _set_cursor_home_x(&mut self, value: u8) {
56+
self.write(0x24, value);
5557
}
5658

5759
pub fn pen_color(&mut self) -> &mut u8 {
5860
&mut self[0x25]
5961
}
6062

61-
pub fn _cursor_position(&mut self) -> &mut [u8; 2] {
62-
self.const_slice(0x26)
63+
/// Cursor must be controlled via methods in `Runtime` instead of changing memory directly!
64+
pub(crate) fn _set_cursor_x(&mut self, val: u8) {
65+
self.write(0x26, val);
66+
}
67+
68+
/// Cursor must be controlled via methods in `Runtime` instead of changing memory directly!
69+
pub(crate) fn _set_cursor_y(&mut self, val: u8) {
70+
self.write(0x27, val);
6371
}
6472

6573
pub fn get_camera_position(&self) -> [i16; 2] {

p8rs/src/vm/runtime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ where A: Allocator
5959

6060
pub fn set_cursor_x(&mut self, val: i16) {
6161
self.cursor[0] = val;
62-
self.memory.machine_state()._cursor_position()[0] = val as u8;
62+
self.memory.machine_state()._set_cursor_x(val as u8);
6363
}
6464

6565
pub fn set_cursor_y(&mut self, val: i16) {
6666
self.cursor[1] = val;
67-
self.memory.machine_state()._cursor_position()[1] = val as u8;
67+
self.memory.machine_state()._set_cursor_y(val as u8);
6868
}
6969

7070

@@ -74,7 +74,7 @@ where A: Allocator
7474

7575
pub fn set_cursor_home(&mut self, x: i16) {
7676
self.cursor_home = x;
77-
*self.memory.machine_state()._cursor_home_x() = x as u8;
77+
self.memory.machine_state()._set_cursor_home_x(x as u8);
7878
}
7979
}
8080

0 commit comments

Comments
 (0)