From b28dde6ae0de1187f6a24f2cb3e9c3f6fe69a897 Mon Sep 17 00:00:00 2001 From: Daniel Covington Date: Thu, 26 Nov 2026 15:14:59 -0500 Subject: [PATCH] M2.2: 1351 mouse with the game's own POT reader cc65's c64_1351_mou read garbage POT values in its IRQ under VICE 3.10, so the pointer wandered with no input (A1 failed; reproduced with a minimal program). Replaced, with user approval, by src/mouse1351.s: once per frame, port 1 selected and IRQs off for 12 raster lines before POTX/POTY are read; cc65-style delta maths; sprite 0 arrow pointer; left/right buttons with keyboard blinding while held. Dig cursor = tile under the pointer tip; pointer on an edge scrolls every 4 frames; keyboard/joystick moves carry the pointer along. Spec A1, M2.2, MT2.2, memory map, toolchain notes and smoke (sprite 0 on) updated together. Co-Authored-By: Claude Opus 5.5 --- docs/manual-tests.md | 20 ++- docs/memory-map.md | 2 + docs/milestones.md | 2 +- docs/spec.md | 3 +- docs/toolchain.md | 12 +- docs/verification-status.md | 4 +- project.json | 22 ++- src/main.c | 104 ++++++++++++- src/mouse1351.h | 17 +++ src/mouse1351.s | 289 ++++++++++++++++++++++++++++++++++++ 10 files changed, 457 insertions(+), 18 deletions(-) create mode 100644 src/mouse1351.h create mode 100644 src/mouse1351.s diff --git a/docs/manual-tests.md b/docs/manual-tests.md index 1f432d6..f759316 100644 --- a/docs/manual-tests.md +++ b/docs/manual-tests.md @@ -108,18 +108,24 @@ Verifies: R4 ### MT2.2: Mark by mouse (c64) -Setup: VICE x64sc, PAL, control port 1 set to Mouse (1351), mouse grab enabled. +Setup: VICE x64sc, PAL. tools/run.ps1 already sets control port 1 to Mouse (1351) (`-controlport1device 3`). After the game starts, click into the VICE window and press Alt+M to grab the host mouse (Alt+M again releases it). A joystick or keyset on port 2 is needed for step 6. Start: `pwsh -File tools/run.ps1 -Target c64` Steps: -1. Move the mouse slowly across the map. -2. Left-click a rock tile, then click it again. -3. Hold the pointer at the right edge of the view. +1. Before grabbing the mouse, wait 10 seconds without touching anything. +2. Grab the mouse (Alt+M). Move it slowly across the map, then quickly. +3. Left-click a rock tile, then click it again. Left-click a floor tile. +4. Hold the pointer on the rightmost column, then on the leftmost, the top view row and the bottom view row. +5. Use the cursor keys while the pointer rests in the middle of the map. +6. Move with the joystick, then with the mouse, then press RETURN, all in one session. Expected: -- The pointer moves smoothly and the highlighted cursor tile is always under the pointer tip -- The first click marks the tile, the second unmarks it -- The map scrolls right while the pointer stays at the edge +- Step 1: the white arrow pointer sits on the cursor in the starting cave and does not move. The map does not scroll on its own. +- Step 2: the pointer follows the mouse smoothly, pixel by pixel, in the right directions (mouse up = pointer up). The blinking cursor is always the tile under the arrow tip. +- Step 3: the first click marks the rock (yellow), the second unmarks it, a click on floor does nothing. Holding the button down does not toggle again, and the game does not react as if a key had been pressed. +- Step 4: the map scrolls steadily in that direction while the pointer rests on the edge, and stops at the map border. +- Step 5: the pointer jumps onto the cursor tile and follows each keyboard move. +- Step 6: all three devices work together; none of them stops working or produces stray moves. Verifies: R5 diff --git a/docs/memory-map.md b/docs/memory-map.md index eb42b48..d957eb8 100644 --- a/docs/memory-map.md +++ b/docs/memory-map.md @@ -7,7 +7,9 @@ these areas. `tools/build.ps1` passes it to cl65 (`resolved.targets[0].build.con | Range | Size | ld65 memory area / segment | Contents | |---|---|---|---| | $0002-$001B | 26 B | ZP / ZEROPAGE | cc65 runtime zero page | +| $0340-$037E | 63 B | (not linked) | Mouse pointer sprite shape (cassette buffer), written by `ms_init` (src/mouse1351.s) | | $0400-$07E7 | 1000 B | (not linked) | VIC-II screen RAM | +| $07F8 | 1 B | (not linked) | Sprite 0 pointer = 13 ($0340 / 64) | | $0801-$37FF | 12 KB | MAIN / STARTUP, CODE, RODATA, DATA, INIT, ONCE, BSS | Program. BSS may use up to $2FFF | | $3000-$37FF | 2 KB | (top of MAIN) | C stack (`__STACKSIZE__` = $0800), grows down from $3800 | | $3800-$3FFF | 2 KB | CHARMEM / CHARSET | Custom character set (`charset[]`), VIC bank 0 | diff --git a/docs/milestones.md b/docs/milestones.md index fc9d542..e064d28 100644 --- a/docs/milestones.md +++ b/docs/milestones.md @@ -39,7 +39,7 @@ Requirements: R4, R5 | Task | Description | Requirements | Buildable outcome | Completion check | |---|---|---|---|---| | M2.1 | Mark/unmark with RETURN on rock tiles only; marked-rock glyph. Joystick port 2 cursor movement with key repeat and fire to mark. | R4 | PRG in which RETURN and fire toggle marks | Manual test MT2.1 | -| M2.2 | 1351 mouse via cc65 c64_1351_mou: sprite 0 pointer, dig cursor = tile under the pointer tip, left button marks/unmarks, pointer at a view edge scrolls. Verify A1/A2 and record the exact VICE options in docs/. | R5 | PRG controllable by mouse | Manual test MT2.2 | +| M2.2 | 1351 mouse via the game's own POT reader in assembly (src/mouse1351.s; changed from cc65 c64_1351_mou with user approval on 2026-11-26 because that driver read garbage POT values in VICE 3.10): sprite 0 pointer, dig cursor = tile under the pointer tip, left button marks/unmarks, pointer at a view edge scrolls. Verify A1/A2 and record the exact VICE options in docs/. | R5 | PRG controllable by mouse | Manual test MT2.2 | Manual verification: see docs/manual-tests.md section M2. diff --git a/docs/spec.md b/docs/spec.md index f229a8d..8fcb239 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -200,7 +200,7 @@ Capabilities: text-ui | ID | Assumption | Status | |---|---|---| -| A1 | cc65's statically linked 1351 driver (c64_1351_mou, mouse_install with mouse_def_callbacks) drives sprite 0 as the pointer and works alongside joystick reads on port 2. The Foundry verified (test brief mouse-1351-c) that the driver links and installs in VICE 3.10 with a 1351 on control port 1; pointer movement together with joystick and keyboard input is still to be checked by hand. | unverified | +| A1 | The 1351 mouse is read by the game's own assembly routine (src/mouse1351.s), once per frame, with control port 1 selected and IRQs off for 12 raster lines before POTX/POTY are read. It drives sprite 0 as the pointer and works alongside joystick reads on port 2 and the keyboard. Originally cc65's c64_1351_mou driver; in VICE 3.10 that driver read garbage POT values in its IRQ and the pointer wandered with no input (M2.2, 2026-11-26), so it was replaced with user approval. Automated runs show a still pointer with no input; movement with a grabbed host mouse, the buttons and joystick/keyboard use at the same time are still to be checked by hand. | unverified | | A2 | VICE emulates the 1351 on control port 1 with -controlport1device 3 (added to tools/run.ps1 and tools/smoke.ps1 automatically by the mouse-1351 capability). The host mouse must be grabbed with Alt+M (or -mouse) for the pointer to move. | confirmed | | A3 | Memory map: screen $0400, custom charset $3800-$3FFF in VIC bank 0. Once the program (code, data, BSS including the 16 KB map) grows past $3800, the map or the charset must move (for example map at a fixed address above the program, or a custom ld65 config that reserves $3800-$3FFF and updating tools/build.ps1). The starter checks at run time that its BSS ends below $3800 and shows 'MEMORY CLASH' if not. | unverified | | A4 | A breadth-first search over a 65x65 window, at most one search per frame, is fast enough that dwarves start walking within a second of a tile being marked. | unverified | @@ -217,3 +217,4 @@ Capabilities: text-ui |---|---|---| | 2026-11-26 | Charset layout: codes 0-127 from the lower/upper-case ROM set, game glyphs at 128+ (cc65 conio prints capitals at screen codes 65-90). | user | | 2026-11-26 | R4 and Controls: RETURN marks/unmarks instead of D, because D also moves the cursor right (W/A/S/D). | user | +| 2026-11-26 | A1 / M2.2: the 1351 mouse is read by the game's own assembly routine instead of cc65's c64_1351_mou driver, which read garbage POT values in VICE 3.10. | user | diff --git a/docs/toolchain.md b/docs/toolchain.md index 85adb1a..660ccf9 100644 --- a/docs/toolchain.md +++ b/docs/toolchain.md @@ -76,9 +76,19 @@ Smoke hooks in the source: cl65 V2.19 Git cc3c40c with `-O` miscompiled an indexed table lookup in a loop (`scr[col] = tile_glyph[t]`, src/view.c, found in M1.2). It used the low byte of the destination address as the table index. If a C lookup table gives wrong results, look at the generated assembly (`cc65 -t c64 -O -I src -o out.s src/.c`) before you suspect the logic. +## Known VICE / driver issue: cc65 1351 driver + +In VICE 3.10 x64sc (`-controlport1device 3`), cc65's statically linked c64_1351_mou driver +read garbage SID POT values in its IRQ (255 in about 30% of reads; other values scattered). +The pointer then random-walked with no input, even in a minimal program that only calls +`mouse_install` and `mouse_show`. The game reads the 1351 itself instead (src/mouse1351.s): +once per frame it selects control port 1 (`$DC00 = $7F`) and waits 12 raster lines with +IRQs off before reading `$D419/$D41A`. Do not switch back to the cc65 mouse driver without +repeating the drift test (docs/verification-status.md, M2.2). + ## Assumptions still to verify -- [ ] A1: the pointer follows the grabbed host mouse and the left button is detected while the joystick in port 2 and the keyboard still work (before M2 is complete). +- [ ] A1: the pointer follows the grabbed host mouse and the left button is detected while the joystick in port 2 and the keyboard still work (MT2.2). The cc65 c64_1351_mou driver failed in VICE 3.10 (pointer drifts with no input; see below). The game uses its own reader, src/mouse1351.s, which kept the pointer still in automated runs. - [x] A3: memory map settled in M1.1 (2026-11-26): custom ld65 config `cfg/stonehold-64.cfg`, map at $4000-$7FFF, see docs/memory-map.md. Still check build/stonehold-64.map at every milestone. - [ ] A4: measure worst-case pathfinding time with a border-colour timing bar in M3. - [x] Frame budget: the view redraw finishes within one frame on PAL and NTSC. M1.3 (2026-11-26) measured 14,962 / 15,131 cycles, and the smoke check `view_fast` verifies it on every run. diff --git a/docs/verification-status.md b/docs/verification-status.md index 96d40db..aa02f73 100644 --- a/docs/verification-status.md +++ b/docs/verification-status.md @@ -14,7 +14,7 @@ manual = `pending`, `confirmed by user ()`, `problem reported (): )`, `problem reported (): > 3); /* 0..39 */ + row = (unsigned char)((unsigned)py >> 3); /* 0..24 */ + + /* Edge scrolling while the pointer rests on an edge. */ + if (col == 0) dx = -1; else if (col == VIEW_W - 1) dx = 1; + if (row <= VIEW_TOP) dy = -1; else if (row >= VIEW_TOP + VIEW_H - 1) dy = 1; + if ((dx || dy) && --mouse_timer == 0) { + mouse_timer = MOUSE_EDGE_RATE; + nx = (unsigned char)(view_x + dx); + ny = (unsigned char)(view_y + dy); + if (dx && nx <= VIEW_MAX_X && nx != view_x) { + begin_move(); + view_x = nx; + need_redraw = 1; + } + if (dy && ny <= VIEW_MAX_Y && ny != view_y) { + begin_move(); + view_y = ny; + need_redraw = 1; + } + } else if (!dx && !dy) { + mouse_timer = MOUSE_EDGE_RATE; + } + + /* The cursor follows the pointer when the pointer moved or the view scrolled. */ + if (px != mouse_last_x || py != mouse_last_y || need_redraw) { + mouse_last_x = px; + mouse_last_y = py; + if (row < VIEW_TOP) row = VIEW_TOP; + if (row > VIEW_TOP + VIEW_H - 1) row = VIEW_TOP + VIEW_H - 1; + nx = (unsigned char)(view_x + col); + ny = (unsigned char)(view_y + row - VIEW_TOP); + if (nx != cur_x || ny != cur_y) { + begin_move(); + cur_x = nx; + cur_y = ny; + } + } + + btn = (unsigned char)(ms_buttons & MS_LEFT); + if (btn && !mouse_btn_prev) { + toggle_mark(); + } + mouse_btn_prev = btn; +} + int main(void) { unsigned seed, t0, c; + unsigned char moved_by_mouse; bordercolor(COLOR_BLACK); bgcolor(COLOR_BLACK); @@ -308,6 +397,10 @@ int main(void) KEY_REPEAT_MODE = 0x80; joy_ok = joy_install(joy_static_stddrv) == JOY_ERR_OK; + ms_init(); + mouse_ok = 1; + mouse_timer = MOUSE_EDGE_RATE; + pointer_to_cursor(); while (kbhit()) { /* drop keys pressed while the map was generated */ cgetc(); } @@ -324,8 +417,13 @@ int main(void) bordercolor(COLOR_BLACK); #endif moved = need_redraw = toggled = 0; + handle_mouse(); + moved_by_mouse = moved; handle_keys(); handle_joystick(); + if (moved && !moved_by_mouse) { + pointer_to_cursor(); /* keyboard/joystick moved: pointer follows */ + } if (need_redraw) { redraw_all(); } diff --git a/src/mouse1351.h b/src/mouse1351.h new file mode 100644 index 0000000..9b7bb6e --- /dev/null +++ b/src/mouse1351.h @@ -0,0 +1,17 @@ +#ifndef STONEHOLD_MOUSE1351_H +#define STONEHOLD_MOUSE1351_H + +/* Commodore 1351 mouse in control port 1, read once per frame (mouse1351.s, R5). +** Pointer = sprite 0, tip at its top-left pixel. */ + +#define MS_LEFT 0x10 +#define MS_RIGHT 0x01 + +extern int ms_x, ms_y; /* pointer tip in screen pixels: 0..319, 0..199 */ +extern unsigned char ms_buttons; /* MS_LEFT | MS_RIGHT while held */ + +void ms_init(void); /* sprite set-up, first sample, pointer at (160, 100) */ +void ms_poll(void); /* call once per frame: IRQs off for about 12 raster lines */ +void __fastcall__ ms_move(int x, int y); + +#endif diff --git a/src/mouse1351.s b/src/mouse1351.s new file mode 100644 index 0000000..721bd17 --- /dev/null +++ b/src/mouse1351.s @@ -0,0 +1,289 @@ +; Commodore 1351 mouse in control port 1, read once per frame (R5). +; +; Why not cc65's c64-1351 driver: in VICE 3.10 its IRQ read the SID POT registers while +; they held stale or cross-port samples (255 in ~30% of reads), so the pointer wandered +; with no input (M2.2, assumption A1, docs/verification-status.md). This reader keeps +; control port 1 selected with IRQs off for SETTLE_LINES raster lines, which is longer than +; one 512-cycle SID POT sample, and only then reads POTX/POTY. +; +; Movement maths as in cc65's driver: 7-bit wrapped difference, halved, +-1 ignored, and the +; old value kept until the pointer moves, so slow movement still adds up. +; +; Buttons are read from CIA1 port B with no keyboard column selected (left = bit 4, +; right = bit 0). While a button is held, port B is switched to output with all bits low, +; so the KERNAL keyboard scan sees every key held and decodes nothing. Otherwise a click +; would look like SPACE and similar keys. +; +; The pointer is sprite 0: shape at SPR_MEM ($0340, cassette buffer, VIC bank 0), +; sprite pointer at $07F8 (screen $0400), tip = top-left pixel. +; +; C interface (mouse1351.h): +; void ms_init(void); sprite set-up, first sample, pointer at (160,100) +; void ms_poll(void); once per frame: move pointer, read buttons +; void __fastcall__ ms_move(int x, int y); place the pointer +; extern int ms_x, ms_y; pointer tip, 0..319 / 0..199 +; extern unsigned char ms_buttons; MS_LEFT ($10) | MS_RIGHT ($01) + + .export _ms_init, _ms_poll, _ms_move + .export _ms_x, _ms_y, _ms_buttons + .import popax + +SPR_MEM = $0340 +SPR_PTR0 = $07F8 +VIC_SPR0_X = $D000 +VIC_SPR0_Y = $D001 +VIC_SPR_HI_X = $D010 +VIC_RASTER = $D012 +VIC_SPR_ENA = $D015 +VIC_SPR_EXPY = $D017 +VIC_SPR_BG = $D01B +VIC_SPR_MCOL = $D01C +VIC_SPR_EXPX = $D01D +VIC_SPR0_COL = $D027 +SID_POTX = $D419 +SID_POTY = $D41A +CIA1_PRA = $DC00 +CIA1_PRB = $DC01 +CIA1_DDRA = $DC02 +CIA1_DDRB = $DC03 + +MAX_X = 319 +MAX_Y = 199 +SETTLE_LINES = 12 ; 12 lines = 756 cycles PAL, 780 NTSC (> 512-cycle sample) +POINTER_COL = 1 ; white + + .segment "BSS" +_ms_x: .res 2 +_ms_y: .res 2 +_ms_buttons: .res 1 +pot_x: .res 1 +pot_y: .res 1 +old_x: .res 1 +old_y: .res 1 +tmp: .res 1 + + .segment "RODATA" +; Arrow pointer, tip at the top-left pixel. 21 rows x 3 bytes. +arrow: + .byte %11000000, 0, 0 + .byte %11100000, 0, 0 + .byte %11110000, 0, 0 + .byte %11111000, 0, 0 + .byte %11111100, 0, 0 + .byte %11111110, 0, 0 + .byte %11111111, 0, 0 + .byte %11111000, 0, 0 + .byte %11011000, 0, 0 + .byte %10001100, 0, 0 + .byte %00001100, 0, 0 + .byte %00000110, 0, 0 + .res 9 * 3, 0 + + .segment "CODE" + +_ms_init: + ldx #62 +@copy: lda arrow,x + sta SPR_MEM,x + dex + bpl @copy + lda #<(SPR_MEM / 64) + sta SPR_PTR0 + lda #POINTER_COL + sta VIC_SPR0_COL + lda #$FE ; sprite 0: no expansion, hi-res, in front of text + and VIC_SPR_EXPY + sta VIC_SPR_EXPY + lda #$FE + and VIC_SPR_EXPX + sta VIC_SPR_EXPX + lda #$FE + and VIC_SPR_MCOL + sta VIC_SPR_MCOL + lda #$FE + and VIC_SPR_BG + sta VIC_SPR_BG + jsr sample + lda pot_x + sta old_x + lda pot_y + sta old_y + lda #<160 + sta _ms_x + lda #>160 + sta _ms_x+1 + lda #100 + sta _ms_y + lda #0 + sta _ms_y+1 + jsr place + lda VIC_SPR_ENA + ora #$01 + sta VIC_SPR_ENA + rts + +; ms_move(x, y): y in A/X, x on the C stack. +_ms_move: + sta _ms_y + stx _ms_y+1 + jsr popax + sta _ms_x + stx _ms_x+1 + jmp place + +_ms_poll: + jsr sample + + lda pot_x ; X: right = POTX up + ldy old_x + jsr delta + beq @no_x + jsr add_x + lda pot_x + sta old_x +@no_x: + lda pot_y ; Y: the 1351 counts up when moved away (up) + ldy old_y + jsr delta + beq @no_y + eor #$FF ; negate: screen y grows downwards + clc + adc #1 + jsr add_y + lda pot_y + sta old_y +@no_y: + jmp place + +; Read POTX/POTY with port 1 selected long enough, then the buttons. +sample: + php + sei + lda #$FF + sta CIA1_DDRA ; port A output (KERNAL default) + lda #$7F ; bit 6 = 1: POT lines see control port 1 + sta CIA1_PRA + ldx #SETTLE_LINES +@line: lda VIC_RASTER +@same: cmp VIC_RASTER + beq @same + dex + bne @line + lda SID_POTX + sta pot_x + lda SID_POTY + sta pot_y + + lda #$00 ; port B input, no keyboard column selected: + sta CIA1_DDRB ; only control port 1 can pull port B low + lda #$FF + sta CIA1_PRA + lda CIA1_PRB + eor #$FF + and #$11 ; bit 4 left button, bit 0 right button + sta _ms_buttons + lda #$7F + sta CIA1_PRA + lda _ms_buttons + beq @done + lda #$FF ; button held: blind the keyboard scan + sta CIA1_DDRB + lda #$00 + sta CIA1_PRB +@done: plp + rts + +; A = new POT value, Y = old. Returns the signed movement in A (Z set if none). +delta: + sty tmp + sec + sbc tmp + and #%01111111 + cmp #%01000000 + bcs @neg + lsr a ; 0..31 + rts +@neg: ora #%11000000 ; -64..-1 + sec + ror a ; -32..-1 + cmp #$FF + bne @ret + lda #0 ; -1 counts as noise +@ret: rts + +; Add signed A to ms_x and clamp to 0..MAX_X. +add_x: + ldx #0 + cmp #$80 + bcc :+ + dex +: clc + adc _ms_x + sta _ms_x + txa + adc _ms_x+1 + sta _ms_x+1 + bmi @zero + cmp #>MAX_X + bcc @ok + bne @max + lda _ms_x + cmp #MAX_X + sta _ms_x+1 +@ok: rts +@zero: lda #0 + sta _ms_x + sta _ms_x+1 + rts + +; Add signed A to ms_y and clamp to 0..MAX_Y. +add_y: + ldx #0 + cmp #$80 + bcc :+ + dex +: clc + adc _ms_y + sta _ms_y + txa + adc _ms_y+1 + sta _ms_y+1 + bmi @zero + bne @max + lda _ms_y + cmp #MAX_Y + 1 + bcc @ok +@max: lda #MAX_Y + sta _ms_y + lda #0 + sta _ms_y+1 +@ok: rts +@zero: lda #0 + sta _ms_y + sta _ms_y+1 + rts + +; Put sprite 0 at the pointer position (tip = sprite top-left). +place: + lda _ms_x + clc + adc #24 + sta VIC_SPR0_X + lda _ms_x+1 + adc #0 + beq @lo + lda VIC_SPR_HI_X + ora #$01 + bne @hi +@lo: lda VIC_SPR_HI_X + and #$FE +@hi: sta VIC_SPR_HI_X + lda _ms_y + clc + adc #50 + sta VIC_SPR0_Y + rts