Bladeren bron

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 <noreply@anthropic.com>
main
Daniel Covington 1 week geleden
bovenliggende
commit
b28dde6ae0
10 gewijzigde bestanden met toevoegingen van 457 en 18 verwijderingen
  1. +13
    -7
      docs/manual-tests.md
  2. +2
    -0
      docs/memory-map.md
  3. +1
    -1
      docs/milestones.md
  4. +2
    -1
      docs/spec.md
  5. +11
    -1
      docs/toolchain.md
  6. +3
    -1
      docs/verification-status.md
  7. +18
    -4
      project.json
  8. +101
    -3
      src/main.c
  9. +17
    -0
      src/mouse1351.h
  10. +289
    -0
      src/mouse1351.s

+ 13
- 7
docs/manual-tests.md Bestand weergeven

@@ -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



+ 2
- 0
docs/memory-map.md Bestand weergeven

@@ -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 |


+ 1
- 1
docs/milestones.md Bestand weergeven

@@ -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.



+ 2
- 1
docs/spec.md Bestand weergeven

@@ -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 |

+ 11
- 1
docs/toolchain.md Bestand weergeven

@@ -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/<file>.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.


+ 3
- 1
docs/verification-status.md Bestand weergeven

@@ -14,7 +14,7 @@ manual = `pending`, `confirmed by user (<date>)`, `problem reported (<date>): <n
| M1.3 Assembly view redraw (draw_view(x, y), C prototype) copying a 40x23 window of the map to screen and colour RAM. Each map byte is its own screen code with its colour in the low nibble (changed from a tile-to-glyph/colour table with user approval on 2026-11-26: the table version measured 22,754 PAL / 23,177 NTSC cycles, over one frame). | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0; `pwsh -File tools/smoke.ps1 -Video pal` exit 0 and `-Video ntsc` exit 0, [PASS] c64 6/6 each, including view_fast = 01. Measured full redraw: 14,962 cycles PAL (frame 19,656), 15,131 NTSC (frame 17,095). VIEW_TIMING build screenshots in VICE: red bar ends partway down the screen on PAL and just above row 24 on NTSC. Earlier attempt (table version) failed: 22,754 PAL / 23,177 NTSC. | confirmed by user (2026-11-26) |
| M1.4 Keyboard cursor (cursor keys and W/A/S/D) with edge scrolling and map-border clamping. Status line: 'STONE 000 SEED nnnnn RUN'. | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0, no warnings; `pwsh -File tools/smoke.ps1 -Video pal` and `-Video ntsc` exit 0, [PASS] c64 7/7 each (adds 'RUN' on row 24). Scripted VICE runs that hold one cursor key (a key put into the KERNAL buffer every frame) reached every edge on PAL: view (88,y)/cursor x=127 right, (0,y)/x=0 left, (x,105)/y=127 down, (x,0)/y=0 up; right and down repeated on NTSC. Worst frame while scrolling: 19,581 cycles PAL (frame 19,656), 19,354 NTSC (frame 17,095): a scroll frame on NTSC runs one frame over, see Notes. | confirmed by user (2026-11-26) |
| 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. | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0, no warnings; `pwsh -File tools/smoke.ps1 -Video pal` and `-Video ntsc` exit 0, [PASS] c64 8/8 each (adds joy_ok = 01). Scripted VICE runs (FIXED_SEED=4242, keys put into the KERNAL buffer) compared with an untouched run: RETURN on the floor start tile changed nothing (R4.2); 6x down + RETURN turned soft rock (62,67) $99 into marked $97 and changed nothing else (R4.1); RETURN twice left the map byte-identical (R4.1). With no joystick input there were no phantom moves or marks. Joystick movement and fire cannot be scripted: manual only (MT2.1). | pending |
| 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/. | not run | pending |
| 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/. | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0, no warnings; `pwsh -File tools/smoke.ps1 -Video pal` and `-Video ntsc` exit 0, [PASS] c64 9/9 each (adds sprite 0 on, $D015 = 01). Scripted VICE runs (`-controlport1device 3`, no host mouse): with no input the pointer stayed at (162,98) on the cursor tile at 9M, 13M and 17M cycles; after keyboard moves the pointer was on the cursor tile (23,14); with the pointer placed on the rightmost column the view scrolled to x = 88 and the cursor reached tile x = 127 (R5.4). Real mouse movement, buttons and keyboard blinding cannot be scripted: manual only (MT2.2). Worst frame while scrolling is now 21,853 PAL / 21,961 NTSC cycles (over one frame on both), see Notes. | pending |
| M3.1 Dwarf job system: idle dwarves claim the nearest reachable marked tile via a windowed breadth-first search (one search per frame at most), walk there, mine for a time that depends on the rock type, turn it into floor. | not run | pending |
| M3.2 Stone counter increments per mined tile; SPACE pauses and resumes; marking still works while paused. | not run | pending |
| M3.3 Frame-based timers scaled for PAL and NTSC using get_tv(). | not run | pending |
@@ -26,4 +26,6 @@ manual = `pending`, `confirmed by user (<date>)`, `problem reported (<date>): <n
- 2026-11-26, M1.2: cc65 V2.19 (Git cc3c40c) with -O miscompiled `scr[col] = tile_glyph[t]` in src/view.c: it indexed the table with the low byte of the screen address instead of t. Worked around with pointer increments (checked in the generated assembly). M1.3 replaces this loop with assembly. Inspect the generated code (`cc65 -O -t c64 -I src src/file.c`) when a C table lookup misbehaves.
- 2026-11-26: requirement change approved by the user: RETURN marks/unmarks instead of D (R4.1-R4.3, Controls, M2.1, MT2.1). D stays 'move right'. Not yet implemented; marking arrives in M2.1.
- 2026-11-26, M1.4: a frame in which the view scrolls costs about 19,400-19,600 cycles: draw_view (~15,000) plus key handling, cursor/dwarf drawing and the KERNAL IRQ that draw_view holds off. That fits a PAL frame and runs over an NTSC frame (17,095), so on NTSC the next frame is skipped after each scroll. With KERNAL key repeat a scroll happens at most every few frames, so the speed should look the same; review in M3, when dwarves and pathfinding share the frame.
- 2026-11-26, M2.2 / assumption A1 FAILED in VICE 3.10: with the cc65 c64-1351 driver installed (static, mouse_def_callbacks), the pointer random-walks with no mouse input. Reproduced with a minimal cc65 program (mouse_install + mouse_show only): 228 different positions in 237 frames. A monitor trace after the driver's `lda $D419` in its IRQ showed garbage values (255 in 51 of 175 reads, the rest scattered), which its delta maths turns into jumps. A prototype that selects port 1 ($DC00 = $7F) and waits 10 raster lines with IRQs off before reading POTX/POTY read only 64/65, and the pointer did not move. The user chose the game's own reader (src/mouse1351.s), implemented in M2.2.
- 2026-11-26, M2.2: the mouse read (about 800 cycles with IRQs off) and the mouse handling push the worst scroll frame to about 21,900 cycles, over one frame on PAL and NTSC. The next frame is skipped after a scroll, with no wrong drawing. Recommended before M3: a frame-budget task (cheaper cursor/dwarf drawing, smaller C overhead in the frame loop).
- Generated 2026-11-26. Starter verification at generation time is reported by the Foundry; re-run `pwsh -File tools/smoke.ps1` on your machine and record the result in row M0.1.

+ 18
- 4
project.json Bestand weergeven

@@ -263,7 +263,7 @@
"assumptions": [
{
"id": "A1",
"text": "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.",
"text": "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.",
"status": "unverified"
},
{
@@ -331,7 +331,7 @@
},
"starter": {
"screenTitle": "STONEHOLD 64",
"expectedScreen": "Row 0: 'STONEHOLD 64'. Rows 1-23: the part of a randomly generated 128x128 map around the starting cave, drawn with the custom glyphs: brown soft rock, grey hard-rock veins, dark-grey floor in caverns and in the starting cave, three orange dwarves standing on floor in the starting cave, and a blinking white cursor bracket in the cave. Row 24: 'STONE 000 SEED nnnnn RUN'. Cursor keys or W/A/S/D move the cursor; the view scrolls when the cursor is within 2 tiles of an edge. If the memory map is wrong, row 1 shows 'MEMORY CLASH' instead of the map.",
"expectedScreen": "Row 0: 'STONEHOLD 64'. Rows 1-23: the part of a randomly generated 128x128 map around the starting cave, drawn with the custom glyphs: brown soft rock, grey hard-rock veins, dark-grey floor in caverns and in the starting cave, three orange dwarves standing on floor in the starting cave, and a blinking white cursor bracket in the cave with the white arrow pointer (sprite 0) on it. Row 24: 'STONE 000 SEED nnnnn RUN'. Cursor keys or W/A/S/D move the cursor; the view scrolls when the cursor is within 2 tiles of an edge. If the memory map is wrong, row 1 shows 'MEMORY CLASH' instead of the map.",
"smokeChecks": [
{
"row": 0,
@@ -463,7 +463,7 @@
},
{
"id": "M2.2",
"text": "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/.",
"text": "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/.",
"requirements": [
"R5"
],
@@ -806,6 +806,12 @@
"symbol": "_joy_ok",
"address": null,
"length": 1
},
{
"name": "sprites",
"symbol": null,
"address": "D015",
"length": 1
}
],
"checks": [
@@ -865,6 +871,14 @@
"bytes": "01",
"description": "Joystick driver installed (joy_ok = 1, R4.3)"
},
{
"dump": "sprites",
"offset": 0,
"text": null,
"encoding": null,
"bytes": "01",
"description": "Mouse pointer: sprite 0 on, no other sprites (R5)"
},
{
"dump": "vector",
"offset": 0,
@@ -874,7 +888,7 @@
"description": "C64 KERNAL reset vector at $FFFC (machine is in C64 mode)"
}
],
"expectedScreen": "Row 0: 'STONEHOLD 64'. Rows 1-23: the part of a randomly generated 128x128 map around the starting cave, drawn with the custom glyphs: brown soft rock, grey hard-rock veins, dark-grey floor in caverns and in the starting cave, three orange dwarves standing on floor in the starting cave, and a blinking white cursor bracket in the cave. Row 24: 'STONE 000 SEED nnnnn RUN'. Cursor keys or W/A/S/D move the cursor; the view scrolls when the cursor is within 2 tiles of an edge. If the memory map is wrong, row 1 shows 'MEMORY CLASH' instead of the map."
"expectedScreen": "Row 0: 'STONEHOLD 64'. Rows 1-23: the part of a randomly generated 128x128 map around the starting cave, drawn with the custom glyphs: brown soft rock, grey hard-rock veins, dark-grey floor in caverns and in the starting cave, three orange dwarves standing on floor in the starting cave, and a blinking white cursor bracket in the cave with the white arrow pointer (sprite 0) on it. Row 24: 'STONE 000 SEED nnnnn RUN'. Cursor keys or W/A/S/D move the cursor; the view scrolls when the cursor is within 2 tiles of an edge. If the memory map is wrong, row 1 shows 'MEMORY CLASH' instead of the map."
},
"recipeStatus": "verified",
"validationPlan": []


+ 101
- 3
src/main.c Bestand weergeven

@@ -25,6 +25,7 @@
#include "map.h"
#include "view.h"
#include "cycles.h"
#include "mouse1351.h"

#define SCREEN ((unsigned char *)0x0400)
#define COLRAM ((unsigned char *)0xD800)
@@ -72,12 +73,22 @@ static unsigned jiffies(void)
#define JOY_REPEAT_RATE 4u
#define JOY_DIR_MASK (JOY_UP_MASK | JOY_DOWN_MASK | JOY_LEFT_MASK | JOY_RIGHT_MASK)

/* 1351 mouse in port 1 (R5): own reader (mouse1351.s), pointer = sprite 0 (shape at $0340).
** The dig cursor is the tile under the pointer tip. Holding the pointer on the outermost
** view column, or on the top/bottom view row (or the title/status row), scrolls every
** MOUSE_EDGE_RATE frames. */
#define MOUSE_EDGE_RATE 4u

static unsigned char view_x, view_y; /* top-left map tile of the view */
static unsigned char cur_x, cur_y; /* dig cursor, map tile */
static unsigned char blink; /* frame counter for the cursor blink */
static unsigned char joy_ok; /* joystick driver installed */
static unsigned char joy_fire_prev; /* fire was down last frame */
static unsigned char joy_timer; /* frames until the next joystick repeat */
static unsigned char mouse_ok; /* mouse reader set up (read by tools/smoke.ps1) */
static unsigned char mouse_btn_prev; /* left button was down last frame */
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) */
@@ -194,13 +205,19 @@ static void toggle_mark(void)
toggled = 1;
}

/* Move the cursor one step from keyboard or joystick. */
static void step(signed char dx, signed char dy)
/* Restore the old cursor cell once per frame, before the cursor or the view changes. */
static void begin_move(void)
{
if (!moved) {
moved = 1;
draw_cursor_cell(0); /* restore the old cursor cell before moving */
draw_cursor_cell(0);
}
}

/* Move the cursor one step from keyboard or joystick. */
static void step(signed char dx, signed char dy)
{
begin_move();
if (move_cursor(dx, dy)) {
need_redraw = 1;
}
@@ -255,9 +272,81 @@ static void handle_joystick(void)
(signed char)(JOY_UP(dir) ? -1 : (JOY_DOWN(dir) ? 1 : 0)));
}

/* Put the pointer tip inside the cursor tile, so the cursor is still the tile under the
** pointer after a keyboard or joystick move (R5.2). */
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;
ms_move(mouse_last_x, mouse_last_y);
}

/* 1351 mouse: edge scrolling, cursor = tile under the pointer, left button toggles. */
static void handle_mouse(void)
{
int px, py;
unsigned char col, row, btn, nx, ny;
signed char dx = 0, dy = 0;

if (!mouse_ok) {
return;
}
ms_poll();
px = ms_x;
py = ms_y;
col = (unsigned char)((unsigned)px >> 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();
}


+ 17
- 0
src/mouse1351.h Bestand weergeven

@@ -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

+ 289
- 0
src/mouse1351.s Bestand weergeven

@@ -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 + 1
bcc @ok
@max: lda #<MAX_X
sta _ms_x
lda #>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

Laden…
Annuleren
Opslaan

Powered by TurnKey Linux.