From d87a552669455576344e335e2fcb5c65c346945a Mon Sep 17 00:00:00 2001 From: Daniel Covington Date: Thu, 26 Nov 2026 15:39:15 -0500 Subject: [PATCH] M2.3: scrolling without tearing The view is drawn in two halves (rows 0-11, rows 12-22), still column by column at 14 cycles per tile. The frame starts at raster line 156: the top half is drawn while the beam is in the lower half, the bottom half after the beam has left row 22, each followed by its dwarves and cursor. Input now runs after the draw; a scroll is set up (view_setup) and drawn at the next line 156. The old cursor cell is repaired only when no redraw follows. Static locals in main.c. Measured: no tear on PAL or NTSC; realistic scroll frames fit PAL (18.8K of 19.7K cycles), NTSC scroll steps take two frames. VIEW_TIMING build now only redraws; MT1.3 updated. Co-Authored-By: Claude Opus 5.5 --- docs/manual-tests.md | 9 +-- docs/verification-status.md | 5 +- project.json | 9 +-- src/frame.h | 2 + src/frame.s | 13 +++- src/main.c | 114 ++++++++++++++++++++++++------------ src/view.h | 7 +++ src/view.s | 53 ++++++++++++++--- 8 files changed, 155 insertions(+), 57 deletions(-) diff --git a/docs/manual-tests.md b/docs/manual-tests.md index 0d59a71..b05c4c4 100644 --- a/docs/manual-tests.md +++ b/docs/manual-tests.md @@ -72,13 +72,14 @@ Setup: VICE x64sc, once with PAL and once with NTSC. Start: `pwsh -File tools/run.ps1 -Target c64 -Define VIEW_TIMING`, then `pwsh -File tools/run.ps1 -Target c64 -Define VIEW_TIMING -Video ntsc` Steps: -1. Wait for the map to appear and watch the border. +1. Wait for the map to appear and watch the side borders. 2. Close VICE and run `pwsh -File tools/build.ps1` so the normal build is back in build/. Expected: -- The border shows a red band. It starts in the bottom border, continues through the top border and ends partway down the screen (PAL: about the middle of the screen; NTSC: just above the status line). Below it the border is black down to the bottom border. -- The border is never red all the way round (that would mean a redraw takes more than one frame) -- In this timing build the dwarves may flicker, because the view is redrawn over them every frame. The normal build does not do that. +- The timing build redraws the whole view every frame and does nothing else (keys and mouse do nothing in it) +- The side borders are red while the view is drawn: the red starts level with the middle of the map, runs down through the bottom border, round through the top border, and stops before it gets back to the middle +- PAL: a black gap of several map rows above the middle. NTSC: only a thin black line just above the middle +- The border is never red all the way round, and the picture is steady (no flicker between frames) - Automated equivalent: `pwsh -File tools/smoke.ps1 -Video ntsc` passes the 'Full view redraw fits in one frame' check Verifies: R3.3 diff --git a/docs/verification-status.md b/docs/verification-status.md index acf2b92..847c422 100644 --- a/docs/verification-status.md +++ b/docs/verification-status.md @@ -15,7 +15,7 @@ manual = `pending`, `confirmed by user ()`, `problem reported (): )`, `problem reported (): #include @@ -27,6 +28,14 @@ #include "cycles.h" #include "mouse1351.h" +/* The frame loop is not recursive; static locals make cc65 code much faster (M2.3). */ +#pragma static-locals (on) +#ifdef VIEW_TIMING +/* The timing build compiles out the input handling, so these are expected there. */ +#pragma warn (unused-func, off) +#pragma warn (unused-var, off) +#endif + #define SCREEN ((unsigned char *)0x0400) #define COLRAM ((unsigned char *)0xD800) #define JIFFY_LO (*(volatile unsigned char *)0xA2) @@ -67,6 +76,12 @@ static unsigned jiffies(void) /* Cursor blink: shown for CURSOR_ON_FRAMES of every 32 frames, the tile shows the rest. */ #define CURSOR_ON_FRAMES 20u +/* Tear-free redraw (M2.3): the frame starts when the raster reaches DRAW_LINE, just below +** view row 11 (screen row 12 ends at line 154). The top half (view rows 0-11) is drawn while +** the beam is in the lower half; by the time it is done the beam has left row 22 (line 242), +** and the bottom half is drawn before the beam comes back to row 12 (line 155). */ +#define DRAW_LINE 156u + /* Joystick in port 2 (R4.3): move at once, then repeat after JOY_REPEAT_DELAY frames ** every JOY_REPEAT_RATE frames while held. Fire marks/unmarks once per press. */ #define JOY_REPEAT_DELAY 15u @@ -91,9 +106,10 @@ static unsigned char mouse_timer; /* frames until the next edge scroll * static int mouse_last_x, mouse_last_y; /* pointer position last frame */ /* Set by the input handlers each frame. */ -static unsigned char moved; /* cursor moved (old cell already restored) */ -static unsigned char need_redraw; /* view scrolled: full redraw */ +static unsigned char moved; /* cursor moved this frame */ +static unsigned char need_redraw; /* view scrolled this frame */ static unsigned char toggled; /* a mark was set or cleared */ +static unsigned char redraw_pending; /* draw the view at the next DRAW_LINE */ /* Read by tools/smoke.ps1 / test runs: worst cycles spent in one frame's work (input + redraw). */ unsigned frame_max_cycles; @@ -126,36 +142,42 @@ static unsigned char dwarf_at(unsigned char x, unsigned char y) return i; } -static void draw_dwarves(void) +/* What view cell of map tile (x, y) shows without the cursor: a dwarf or the tile. */ +static unsigned char cell_code(unsigned char x, unsigned char y) +{ + if (dwarf_at(x, y) < DWARF_COUNT) { + return GLYPH_DWARF; + } + return *MAP_PTR(x, y); +} + +static unsigned char cursor_shown(void) +{ + return (unsigned char)((blink & 31u) < CURSOR_ON_FRAMES); +} + +/* Dwarves and the cursor over view rows r0 .. r1-1 (after view_draw_top/bottom). */ +static void draw_overlays(unsigned char r0, unsigned char r1) { unsigned char i, sx, sy; for (i = 0; i < DWARF_COUNT; ++i) { sx = (unsigned char)(map_dwarf_x[i] - view_x); sy = (unsigned char)(map_dwarf_y[i] - view_y); - if (sx < VIEW_W && sy < VIEW_H) { + if (sx < VIEW_W && sy >= r0 && sy < r1) { put_cell(sx, sy, GLYPH_DWARF); } } -} - -/* Draw the cursor cell: the cursor glyph, or what lies under it (dwarf or tile). */ -static void draw_cursor_cell(unsigned char show) -{ - unsigned char sx = (unsigned char)(cur_x - view_x); - unsigned char sy = (unsigned char)(cur_y - view_y); - if (show) { - put_cell(sx, sy, GLYPH_CURSOR); - } else if (dwarf_at(cur_x, cur_y) < DWARF_COUNT) { - put_cell(sx, sy, GLYPH_DWARF); - } else { - put_cell(sx, sy, *MAP_PTR(cur_x, cur_y)); + sy = (unsigned char)(cur_y - view_y); + if (sy >= r0 && sy < r1 && cursor_shown()) { + put_cell((unsigned char)(cur_x - view_x), sy, GLYPH_CURSOR); } } -static void redraw_all(void) +/* Draw the cursor cell for the current blink phase (view unchanged this frame). */ +static void draw_cursor_cell(void) { - draw_view(view_x, view_y); - draw_dwarves(); + put_cell((unsigned char)(cur_x - view_x), (unsigned char)(cur_y - view_y), + cursor_shown() ? GLYPH_CURSOR : cell_code(cur_x, cur_y)); } /* Top-left of a view centred on (x, y), kept inside the map. */ @@ -205,13 +227,11 @@ static void toggle_mark(void) toggled = 1; } -/* Restore the old cursor cell once per frame, before the cursor or the view changes. */ +/* The cursor or the view is about to change. The old cursor cell is repaired at the end of +** the frame (or by the redraw), not here, so a scroll frame does not pay for it. */ static void begin_move(void) { - if (!moved) { - moved = 1; - draw_cursor_cell(0); - } + moved = 1; } /* Move the cursor one step from keyboard or joystick. */ @@ -279,8 +299,8 @@ static void pointer_to_cursor(void) if (!mouse_ok) { return; } - mouse_last_x = (int)((unsigned char)(cur_x - view_x)) * 8 + 2; - mouse_last_y = (int)((unsigned char)(cur_y - view_y) + VIEW_TOP) * 8 + 2; + mouse_last_x = (int)(((unsigned)(unsigned char)(cur_x - view_x) << 3) | 2u); + mouse_last_y = (int)(((unsigned)(unsigned char)(cur_y - view_y + VIEW_TOP) << 3) | 2u); ms_move(mouse_last_x, mouse_last_y); } @@ -346,7 +366,7 @@ static void handle_mouse(void) int main(void) { unsigned seed, t0, c; - unsigned char moved_by_mouse; + unsigned char moved_by_mouse, old_x, old_y; bordercolor(COLOR_BLACK); bgcolor(COLOR_BLACK); @@ -390,7 +410,7 @@ int main(void) draw_view(view_x, view_y); view_cycles = cycles_read(); view_fast = view_cycles < (get_tv() == TV_NTSC ? FRAME_CYCLES_NTSC : FRAME_CYCLES_PAL); - draw_dwarves(); + draw_overlays(0, VIEW_H); gotoxy(0, 24); cprintf("STONE 000 SEED %05u RUN", map_seed); @@ -409,13 +429,24 @@ int main(void) smoke_ready(); for (;;) { - wait_frame(); + wait_line(DRAW_LINE); cycles_start(); #ifdef VIEW_TIMING + redraw_pending = 1; /* timing build: redraw every frame */ bordercolor(COLOR_RED); - redraw_all(); - bordercolor(COLOR_BLACK); #endif + if (redraw_pending) { + view_draw_top(); + draw_overlays(0, VIEW_SPLIT); + view_draw_bottom(); + draw_overlays(VIEW_SPLIT, VIEW_H); + redraw_pending = 0; + } +#ifdef VIEW_TIMING + bordercolor(COLOR_BLACK); /* timing build: redraw only, no input */ +#else + old_x = cur_x; + old_y = cur_y; moved = need_redraw = toggled = 0; handle_mouse(); moved_by_mouse = moved; @@ -424,16 +455,23 @@ int main(void) if (moved && !moved_by_mouse) { pointer_to_cursor(); /* keyboard/joystick moved: pointer follows */ } - if (need_redraw) { - redraw_all(); - } if (toggled) { blink = CURSOR_ON_FRAMES; /* show the changed tile for a moment */ } else if (moved) { blink = 0; /* show the cursor at once after a move */ } ++blink; - draw_cursor_cell((blink & 31u) < CURSOR_ON_FRAMES); + if (need_redraw) { + view_setup(view_x, view_y); /* drawn, cursor included, at the next DRAW_LINE */ + redraw_pending = 1; + } else { + if (moved) { /* repair the old cursor cell */ + put_cell((unsigned char)(old_x - view_x), (unsigned char)(old_y - view_y), + cell_code(old_x, old_y)); + } + draw_cursor_cell(); + } +#endif c = cycles_read(); if (c > frame_max_cycles) { frame_max_cycles = c; diff --git a/src/view.h b/src/view.h index f4874a0..6692fcf 100644 --- a/src/view.h +++ b/src/view.h @@ -11,4 +11,11 @@ ** IRQs are held off while it runs. */ void __fastcall__ draw_view(unsigned char vx, unsigned char vy); +/* The same in steps, so the caller can keep each half away from the raster beam (M2.3): +** view_setup() once, then view_draw_top() (rows 0..VIEW_SPLIT-1) and view_draw_bottom(). */ +#define VIEW_SPLIT 12u +void __fastcall__ view_setup(unsigned char vx, unsigned char vy); +void view_draw_top(void); +void view_draw_bottom(void); + #endif diff --git a/src/view.s b/src/view.s index 6e56ab4..e2d4885 100644 --- a/src/view.s +++ b/src/view.s @@ -8,10 +8,19 @@ ; into the code (self-modifying) once per call. vx <= 88 keeps map row reads inside one page, ; so no indexed read crosses a page. ; -; C prototype (view.h): void __fastcall__ draw_view(unsigned char vx, unsigned char vy); +; Tearing (M2.3): the view is drawn in two halves, rows 0-11 and rows 12-22, each column by +; column. The caller starts the top half while the raster beam is below row 11 and the bottom +; half once the beam has left row 22 (see main.c), so the beam never shows a half drawn +; row. view_setup() patches the source addresses; view_draw_top()/view_draw_bottom() copy. +; +; C prototypes (view.h): +; void __fastcall__ draw_view(unsigned char vx, unsigned char vy); setup + both halves +; void __fastcall__ view_setup(unsigned char vx, unsigned char vy); +; void view_draw_top(void); +; void view_draw_bottom(void); ; vy arrives in A, vx on the C stack. - .export _draw_view + .export _draw_view, _view_setup, _view_draw_top, _view_draw_bottom .importzp c_sp .import incsp1 @@ -20,6 +29,7 @@ COLRAM = $D800 VIEW_TOP = 1 VIEW_H = 23 VIEW_W = 40 +SPLIT = 12 ; rows 0..SPLIT-1 are the top half MAP_HI = $40 ; map at $4000 .segment "BSS" @@ -29,6 +39,11 @@ src_hi: .res 1 .segment "CODE" _draw_view: + jsr _view_setup + jsr _view_draw_top + jmp _view_draw_bottom + +_view_setup: ; Row 0 source address = $4000 + vy*128 + vx -> hi = $40 + vy/2, lo = (vy odd ? $80 : 0) + vx lsr a ; A = vy/2, C = vy & 1 tax @@ -56,20 +71,42 @@ _draw_view: inc src_hi : .endrep + rts +; Rows 0 .. SPLIT-1. +_view_draw_top: php sei ; keep timing steady; a pending IRQ runs after plp ldy #VIEW_W - 1 -col_loop: -.repeat VIEW_H, R +top_loop: +.repeat SPLIT, R .ident(.sprintf("src%d", R)): - lda $4000,y ; operand patched above + lda $4000,y ; operand patched by view_setup sta SCREEN + (VIEW_TOP + R) * 40,y sta COLRAM + (VIEW_TOP + R) * 40,y .endrep dey - bmi done - jmp col_loop -done: + bmi top_done + jmp top_loop +top_done: + plp + rts + +; Rows SPLIT .. VIEW_H-1. +_view_draw_bottom: + php + sei + ldy #VIEW_W - 1 +bottom_loop: +.repeat VIEW_H - SPLIT, K +.ident(.sprintf("src%d", K + SPLIT)): + lda $4000,y ; operand patched by view_setup + sta SCREEN + (VIEW_TOP + SPLIT + K) * 40,y + sta COLRAM + (VIEW_TOP + SPLIT + K) * 40,y +.endrep + dey + bmi bottom_done + jmp bottom_loop +bottom_done: plp rts