Bläddra i källkod

M1.4: keyboard cursor with edge scrolling and RUN status

Cursor keys or W/A/S/D move a blinking dig cursor one tile (KERNAL key
repeat on for all keys); the view scrolls when the cursor is within 2
tiles of an edge and stops at the map borders. Status line reads
'STONE 000  SEED nnnnn  RUN'. Smoke adds a RUN check and records the
worst frame time. Scripted VICE runs reached all four edges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main
Daniel Covington 1 vecka sedan
förälder
incheckning
40b6c93231
4 ändrade filer med 189 tillägg och 31 borttagningar
  1. +8
    -4
      docs/manual-tests.md
  2. +2
    -1
      docs/verification-status.md
  3. +22
    -2
      project.json
  4. +157
    -24
      src/main.c

+ 8
- 4
docs/manual-tests.md Visa fil

@@ -53,12 +53,16 @@ Setup: VICE x64sc, PAL.
Start: `pwsh -File tools/run.ps1 -Target c64`

Steps:
1. Hold the right cursor key until the view stops moving.
2. Repeat for down, left and up (W/A/S/D as well).
1. Note row 24 and the blinking white cursor bracket in the starting cave.
2. Tap the right cursor key a few times. Then hold it until the view stops moving, and keep holding.
3. Repeat for down, left and up. Then do the same with W (up), A (left), S (down) and D (right).

Expected:
- The view scrolls one tile at a time without garbage characters
- Scrolling stops at each map edge and the cursor cannot leave the map
- Row 24 reads 'STONE 000 SEED nnnnn RUN'
- Each tap moves the cursor one tile. The cursor blinks, showing the tile or dwarf under it in between.
- The view starts scrolling when the cursor is 2 tiles from an edge, and scrolls steadily one tile at a time while the key is held, without garbage characters. A brief shear or tear in the map while it scrolls is a known limitation, not a failure.
- At each map edge the view stops scrolling and the cursor goes on to the outermost tile (2 tiles from the view edge), then stops. It cannot leave the map.
- W/A/S/D move exactly like the cursor keys

Verifies: R3



+ 2
- 1
docs/verification-status.md Visa fil

@@ -12,7 +12,7 @@ manual = `pending`, `confirmed by user (<date>)`, `problem reported (<date>): <n
| M1.1 Settle the memory map (A3): where the 16 KB map, the charset and the program live. Add a check that fails the build or shows an error if they overlap. | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0, build/stonehold-64.map shows program $0801-$1702, CHARSET $3800-$3FFF, MAP $4000-$7FFF, no overlap; deliberate overflow links (1 extra byte in MAP, 10 KB extra BSS) failed with ld65 'overflows memory area', cl65 exit 1; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64 4/4. | pending |
| M1.2 Map generator in C: seeded PRNG, soft rock, hard-rock veins, caverns, starting cave near the centre. Show the seed on row 24. | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64 5/5 (title, layout_ok=01, STONE 000, 'SEED ' on row 24, KERNAL vector). Extra VICE runs with memory dumps: two runs of the normal build gave seeds 21910 and 30091 with different maps (R2.2); two runs of a FIXED_SEED=12345 build gave byte-identical 16 KB maps (R2.3); dwarves on floor tiles in the starting cave (R2.4). Generation time 133 jiffies (about 2.2 s), measured by the program. | pending |
| 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. | pending |
| 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'. | not run | pending |
| 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. | pending |
| 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. | not run | 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 |
| 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 |
@@ -25,4 +25,5 @@ manual = `pending`, `confirmed by user (<date>)`, `problem reported (<date>): <n
- 2026-11-26, open question (spec vs code): cc65 conio writes capital letters as screen codes 65-90. The spec's Visuals decision (codes 0-63 from the uppercase ROM, game glyphs at 64+) would put glyphs on the letters A-E and blank F-Z. The uncommitted working-tree edit to src/charset.c/.h copies ROM codes 0-127 from the lower/upper-case set and puts the game glyphs at 128-133. The M0 smoke run and screenshot used that edit. The user approved the change on 2026-11-26; docs/spec.md (Visuals), project.json and the src/main.c header now describe codes 0-127 = ROM lower/upper-case set, 128+ = game glyphs. Build and smoke re-run after the doc update (see M0.1).
- 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.
- 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.

+ 22
- 2
project.json Visa fil

@@ -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. Row 24: 'STONE 000 SEED nnnnn ANY KEY EXITS'. If the memory map is wrong, row 1 shows 'MEMORY CLASH' instead of the map. Any key restores the normal character set and returns to BASIC.",
"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.",
"smokeChecks": [
{
"row": 0,
@@ -350,6 +350,12 @@
"col": 11,
"text": "SEED ",
"description": "Seed on the status line"
},
{
"row": 24,
"col": 23,
"text": "RUN",
"description": "Run state on the status line"
}
],
"filesDir": "stonehold-64-files",
@@ -788,6 +794,12 @@
"symbol": "_view_fast",
"address": null,
"length": 1
},
{
"name": "framemax",
"symbol": "_frame_max_cycles",
"address": null,
"length": 2
}
],
"checks": [
@@ -823,6 +835,14 @@
"bytes": null,
"description": "Seed on the status line"
},
{
"dump": "screen",
"offset": 983,
"text": "RUN",
"encoding": "screen-mixed",
"bytes": null,
"description": "Run state on the status line"
},
{
"dump": "viewfast",
"offset": 0,
@@ -840,7 +860,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. Row 24: 'STONE 000 SEED nnnnn ANY KEY EXITS'. If the memory map is wrong, row 1 shows 'MEMORY CLASH' instead of the map. Any key restores the normal character set and returns to BASIC."
"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."
},
"recipeStatus": "verified",
"validationPlan": []


+ 157
- 24
src/main.c Visa fil

@@ -54,21 +54,84 @@ static unsigned jiffies(void)
return ((unsigned)mid << 8) | lo;
}

static void draw_dwarves(unsigned char vx, unsigned char vy)
/* Keyboard (R3, R4). KERNAL buffer with repeat on for every key, so holding a key moves steadily. */
#define KEY_REPEAT_MODE (*(unsigned char *)0x028A) /* $80 = repeat all keys */

/* Edge scrolling: the view follows when the cursor comes within SCROLL_MARGIN tiles of an edge. */
#define SCROLL_MARGIN 2u
#define VIEW_MAX_X (MAP_W - VIEW_W) /* 88 */
#define VIEW_MAX_Y (MAP_H - VIEW_H) /* 105 */

/* Cursor blink: shown for CURSOR_ON_FRAMES of every 32 frames, the tile shows the rest. */
#define CURSOR_ON_FRAMES 20u

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 */

/* Read by tools/smoke.ps1 / test runs: worst cycles spent in one frame's work (input + redraw). */
unsigned frame_max_cycles;

/* Offset of the first cell of each view row in screen/colour RAM (avoids a multiply). */
#define ROW_OFF(r) ((VIEW_TOP + (r)) * 40u)
static const unsigned view_row_off[VIEW_H] = {
ROW_OFF(0), ROW_OFF(1), ROW_OFF(2), ROW_OFF(3), ROW_OFF(4), ROW_OFF(5),
ROW_OFF(6), ROW_OFF(7), ROW_OFF(8), ROW_OFF(9), ROW_OFF(10), ROW_OFF(11),
ROW_OFF(12), ROW_OFF(13), ROW_OFF(14), ROW_OFF(15), ROW_OFF(16), ROW_OFF(17),
ROW_OFF(18), ROW_OFF(19), ROW_OFF(20), ROW_OFF(21), ROW_OFF(22)
};

static void put_cell(unsigned char sx, unsigned char sy, unsigned char code)
{
unsigned off = view_row_off[sy] + sx;
SCREEN[off] = code;
COLRAM[off] = code; /* colour = low nibble of the code (map.h) */
}

/* Dwarf index at map tile (x, y), or DWARF_COUNT if none. */
static unsigned char dwarf_at(unsigned char x, unsigned char y)
{
unsigned char i;
for (i = 0; i < DWARF_COUNT; ++i) {
if (map_dwarf_x[i] == x && map_dwarf_y[i] == y) {
break;
}
}
return i;
}

static void draw_dwarves(void)
{
unsigned char i, sx, sy;
unsigned off;
for (i = 0; i < DWARF_COUNT; ++i) {
sx = (unsigned char)(map_dwarf_x[i] - vx);
sy = (unsigned char)(map_dwarf_y[i] - vy);
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) {
off = (unsigned)(sy + VIEW_TOP) * 40u + sx;
SCREEN[off] = GLYPH_DWARF;
COLRAM[off] = COLOR_ORANGE;
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));
}
}

static void redraw_all(void)
{
draw_view(view_x, view_y);
draw_dwarves();
}

/* Top-left of a view centred on (x, y), kept inside the map. */
static unsigned char view_origin(unsigned char centre, unsigned char size, unsigned char limit)
{
@@ -80,10 +143,64 @@ static unsigned char view_origin(unsigned char centre, unsigned char size, unsig
return centre > (unsigned char)(limit - size) ? (unsigned char)(limit - size) : centre;
}

/* Move the cursor by (dx, dy), clamp it to the map, and scroll the view if the cursor is
** within SCROLL_MARGIN tiles of a view edge. Returns 1 if the view scrolled. */
static unsigned char move_cursor(signed char dx, signed char dy)
{
unsigned char scrolled = 0;

if (dx < 0 && cur_x > 0) --cur_x;
if (dx > 0 && cur_x < MAP_W - 1) ++cur_x;
if (dy < 0 && cur_y > 0) --cur_y;
if (dy > 0 && cur_y < MAP_H - 1) ++cur_y;

if (cur_x < view_x + SCROLL_MARGIN && view_x > 0) {
--view_x; scrolled = 1;
} else if (cur_x > view_x + (VIEW_W - 1 - SCROLL_MARGIN) && view_x < VIEW_MAX_X) {
++view_x; scrolled = 1;
}
if (cur_y < view_y + SCROLL_MARGIN && view_y > 0) {
--view_y; scrolled = 1;
} else if (cur_y > view_y + (VIEW_H - 1 - SCROLL_MARGIN) && view_y < VIEW_MAX_Y) {
++view_y; scrolled = 1;
}
return scrolled;
}

/* Handle every key waiting in the buffer. Returns 1 if the view must be redrawn,
** 2 if only the cursor moved, 0 if nothing changed. */
static unsigned char handle_keys(void)
{
unsigned char result = 0;
signed char dx, dy;
char k;

while (kbhit()) {
k = cgetc();
dx = 0;
dy = 0;
switch (k) {
case CH_CURS_UP: case 'w': case 'W': dy = -1; break;
case CH_CURS_DOWN: case 's': case 'S': dy = 1; break;
case CH_CURS_LEFT: case 'a': case 'A': dx = -1; break;
case CH_CURS_RIGHT: case 'd': case 'D': dx = 1; break;
default: continue;
}
if (result == 0) {
result = 2;
draw_cursor_cell(0); /* restore the old cursor cell before moving */
}
if (move_cursor(dx, dy)) {
result = 1;
}
}
return result;
}

int main(void)
{
unsigned seed, t0;
unsigned char vx, vy;
unsigned seed, t0, c;
unsigned char changed;

bordercolor(COLOR_BLACK);
bgcolor(COLOR_BLACK);
@@ -101,11 +218,10 @@ int main(void)
textcolor(COLOR_LIGHTRED);
cputsxy(0, 1, "MEMORY CLASH");
textcolor(COLOR_WHITE);
cputsxy(0, 24, "STONE 000 ANY KEY EXITS");
cputsxy(0, 24, "STONE 000");
smoke_capture();
smoke_ready();
cgetc();
return 1;
for (;;) { }
}

charset_install();
@@ -120,31 +236,48 @@ int main(void)
map_generate(seed);
gen_jiffies = jiffies() - t0;

vx = view_origin(map_start_x, VIEW_W, MAP_W);
vy = view_origin(map_start_y, VIEW_H, MAP_H);
cur_x = map_start_x;
cur_y = map_start_y;
view_x = view_origin(cur_x, VIEW_W, MAP_W);
view_y = view_origin(cur_y, VIEW_H, MAP_H);
cycles_start();
draw_view(vx, vy);
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(vx, vy);
draw_dwarves();

gotoxy(0, 24);
cprintf("STONE 000 SEED %05u ANY KEY EXITS", map_seed);
cprintf("STONE 000 SEED %05u RUN", map_seed);

KEY_REPEAT_MODE = 0x80;
while (kbhit()) { /* drop keys pressed while the map was generated */
cgetc();
}

smoke_capture();
smoke_ready();

while (!kbhit()) {
for (;;) {
wait_frame();
cycles_start();
#ifdef VIEW_TIMING
bordercolor(COLOR_RED);
draw_view(vx, vy);
redraw_all();
bordercolor(COLOR_BLACK);
draw_dwarves(vx, vy);
#endif
changed = handle_keys();
if (changed == 1) {
redraw_all();
}
if (changed) {
blink = 0; /* show the cursor at once after a move */
}
++blink;
draw_cursor_cell((blink & 31u) < CURSOR_ON_FRAMES);
c = cycles_read();
if (c > frame_max_cycles) {
frame_max_cycles = c;
}
}
cgetc();
charset_remove();
clrscr();
return 0;
/* not reached: the game runs until reset */
}

Laddar…
Avbryt
Spara

Powered by TurnKey Linux.