Skip to content

Instantly share code, notes, and snippets.

@niconii
Created February 16, 2017 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niconii/8e00f97906d97c23bff381d4e8dd8d2f to your computer and use it in GitHub Desktop.
Save niconii/8e00f97906d97c23bff381d4e8dd8d2f to your computer and use it in GitHub Desktop.
Testing text buffer cursors for CrayonForth
pub struct SingleCursor {
cursor: u16,
mask: u16
}
impl SingleCursor {
pub fn new(offset: u16, mask: u16) -> SingleCursor {
SingleCursor {
cursor: offset,
mask: mask
}
}
pub fn curr(&self) -> u16 {
self.cursor
}
pub fn advance(&mut self) {
// let a = self.mask + 1;
// if (a & self.cursor) != 0 {
// self.cursor = (self.cursor + 2) & self.mask;
// } else {
// self.cursor |= a;
// }
let (mut a, z);
// preserve a in y // tay ; 1b 2c
a = self.mask; // lda Mask ; 4b 7c
a += 1; // inc a ; 5b 9c
z = (a & self.cursor) == 0; // bit Cursor ; 8b 14c
if !z { // beq :+ ; 10b 16c
a = self.cursor; // lda Cursor ; 13b 21c
a += 1; // inc a ; 14b 23c
a += 1; // inc a ; 15b 25c
a &= self.mask; // and Mask ; 18b 30c
self.cursor = a; // sta Cursor ; 21b 35c
// restore a from y // tya ; 22b 37c
return // rts ; 23b 43c
} else { // : ; 17c
a |= self.cursor; // ora Cursor ; 26b 22c
self.cursor = a; // sta Cursor ; 29b 27c
// restore a from y // tya ; 30b 29c
return // rts ; 31b 35c
}
// 31 bytes; 43 cycles on odd, 35 cycles on even
}
}
pub struct DoubleCursor {
cursor_a: u16,
cursor_b: u16,
mask: u16
}
impl DoubleCursor {
pub fn new(offset: u16, mask: u16) -> DoubleCursor {
DoubleCursor {
cursor_a: offset,
cursor_b: offset + mask + 1,
mask: mask
}
}
pub fn curr(&self) -> u16 {
self.cursor_a
}
pub fn advance(&mut self) {
// std::mem::swap(&mut self.cursor_a, &mut self.cursor_b);
// let top = self.cursor_b & (self.mask + 1);
// let bottom = (self.cursor_b + 2) & self.mask;
// self.cursor_b = top | bottom;
let (mut a, y, s);
// preserve a on stack // pha ; 1b 4c
a = self.cursor_a; // lda CursorA ; 4b 9c
y = self.cursor_b; // ldy CursorB ; 7b 14c
self.cursor_a = y; // sty CursorA ; 10b 19c
self.cursor_b = a; // sta CursorB ; 13b 24c
a += 1; // inc a ; 14b 26c
a += 1; // inc a ; 15b 28c
a &= self.mask; // and Mask ; 18b 33c
s = a; // pha ; 19b 37c
a = self.mask; // lda Mask ; 22b 42c
a += 1; // inc a ; 23b 44c
a &= self.cursor_b; // and CursorB ; 26b 49c
a |= s; // ora 1,s ; 28b 54c
self.cursor_b = a; // sta CursorB ; 31b 59c
// drop s // ply ; 32b 64c
// restore a from stack // pla ; 33b 69c
return // rts ; 34b 75c
// 34 bytes; 75 cycles
}
}
fn main() {
let mut single = SingleCursor::new(0x07f0, 0x07ff);
let mut double = DoubleCursor::new(0x07f0, 0x07ff);
for _ in 0..32 {
println!("{:04x} {:04x}", single.curr(), double.curr());
single.advance(); double.advance();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment