Browse Source

M1.3: assembly view redraw within one frame

draw_view(vx, vy) in src/view.s: column loop with the 23 rows unrolled and
self-modified map source addresses. Map bytes are now their own screen codes
with the colour in the low nibble (approved change to M1.3: the lookup-table
version measured 22,754 PAL / 23,177 NTSC cycles, over one frame); this one
measures 14,962 / 15,131. Tile codes and helper macros in map.h, glyphs placed
per code with compile-time colour checks in charset.c.
Timing: CIA2 cycle counter (cycles.s), view_fast smoke check, VIEW_TIMING
border-bar build, smoke.ps1 -Video ntsc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main
Daniel Covington 1 week ago
parent
commit
426a3398da
16 changed files with 285 additions and 78 deletions
  1. +17
    -0
      docs/manual-tests.md
  2. +33
    -3
      docs/memory-map.md
  3. +1
    -1
      docs/milestones.md
  4. +3
    -2
      docs/toolchain.md
  5. +1
    -1
      docs/verification-status.md
  6. +21
    -1
      project.json
  7. +27
    -2
      src/charset.c
  8. +9
    -6
      src/charset.h
  9. +12
    -0
      src/cycles.h
  10. +36
    -0
      src/cycles.s
  11. +16
    -1
      src/main.c
  12. +28
    -7
      src/map.h
  13. +0
    -48
      src/view.c
  14. +3
    -6
      src/view.h
  15. +75
    -0
      src/view.s
  16. +3
    -0
      tools/smoke.ps1

+ 17
- 0
docs/manual-tests.md View File

@@ -62,6 +62,23 @@ Expected:


Verifies: R3 Verifies: R3


### MT1.3: View redraw within one frame (c64)

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.
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.
- Automated equivalent: `pwsh -File tools/smoke.ps1 -Video ntsc` passes the 'Full view redraw fits in one frame' check

Verifies: R3.3

## M2 - Designation by keyboard, joystick and mouse ## M2 - Designation by keyboard, joystick and mouse


### MT2.1: Mark by keyboard and joystick (c64) ### MT2.1: Mark by keyboard and joystick (c64)


+ 33
- 3
docs/memory-map.md View File

@@ -28,12 +28,42 @@ these areas. `tools/build.ps1` passes it to cl65 (`resolved.targets[0].build.con
`#pragma bss-name (pop)`. `src/map.c` and `src/charset.c` show how. `#pragma bss-name (pop)`. `src/map.c` and `src/charset.c` show how.
- At run time `main()` checks that `charset` is at `CHARSET_ADDR` (charset.h), that `map` is at - At run time `main()` checks that `charset` is at `CHARSET_ADDR` (charset.h), that `map` is at
`MAP_ADDR` (map.h) and that BSS ends below the charset. If any check fails it shows `MAP_ADDR` (map.h) and that BSS ends below the charset. If any check fails it shows
`MEMORY CLASH` instead of `CHARSET OK`. This catches a linker config that has drifted from the
`MEMORY CLASH` on row 1 instead of the map, and the smoke test's `layout_ok` check fails. This catches a linker config that has drifted from the
headers. headers.
- cc65's startup puts the top of the C stack at the end of MAIN ($3800), so the stack and any heap - cc65's startup puts the top of the C stack at the end of MAIN ($3800), so the stack and any heap
stay below the charset. The game does not use `malloc`. stay below the charset. The game does not use `malloc`.
- Size at M1.1: the program ends at $1702 (about 3.8 KB). That leaves about 6 KB of room below the
stack.
- Size at M1.3: the program ends at $1FC6 (about 6 KB, see build/stonehold-64.map). That leaves
about 4 KB of room below the stack.

## Tile byte encoding

Each map byte is also the screen code it is drawn with, and its low nibble is its colour.
Colour RAM keeps only the low 4 bits, so `draw_view` (src/view.s) copies the byte to both
screen RAM and colour RAM. That costs 14 cycles per tile, about 14,900 cycles per full
redraw. Changed in M1.3 with user approval: a glyph/colour lookup table measured 22,754 (PAL) /
23,177 (NTSC) cycles, over one frame.

| Bits | Meaning |
|---|---|
| 7 | always 1: game glyphs live at codes 128-255 |
| 6 | claimed by a dwarf |
| 5-4 | type: 00 floor, 01 soft rock, 10 hard rock |
| 3-0 | colour: the type's colour, or 7 (yellow) when marked for digging |

| Code | Tile | Colour |
|---|---|---|
| $8B | floor | 11 dark grey |
| $99 | soft rock | 9 brown |
| $AC | hard rock | 12 grey |
| $97 / $A7 | marked soft / hard rock | 7 yellow |
| $D7 / $E7 | marked and claimed soft / hard rock | 7 yellow |
| $88 | dwarf (drawn over the map, not stored in it) | 8 orange |
| $81 | cursor (drawn over the map, not stored in it) | 1 white |

Use the macros in src/map.h (`TILE_IS_ROCK`, `TILE_IS_MARKED`, `TILE_MARK`, `TILE_UNMARK`)
instead of testing bits by hand. A new tile state needs a code whose low nibble is its colour,
and a glyph at that code in src/charset.c. charset.c has compile-time checks that keep the
codes and the spec's colours in step.


## If MAIN fills up ## If MAIN fills up




+ 1
- 1
docs/milestones.md View File

@@ -25,7 +25,7 @@ Requirements: R1, R2, R3, R7
|---|---|---|---|---| |---|---|---|---|---|
| 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. | R1, R2 | Documented memory map in docs/ and a PRG that still passes the smoke test | build/stonehold-64.map shows no overlap; smoke test passes | | 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. | R1, R2 | Documented memory map in docs/ and a PRG that still passes the smoke test | build/stonehold-64.map shows no overlap; smoke test passes |
| 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. | R2 | PRG that generates a map and shows 'SEED nnnnn' | Manual test MT1.1 | | 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. | R2 | PRG that generates a map and shows 'SEED nnnnn' | Manual test MT1.1 |
| M1.3 | Assembly view redraw (draw_view(x, y), C prototype) copying a 40x23 window of the map to screen and colour RAM via a tile-to-glyph/colour table. | R3 | PRG with a full-view redraw inside one frame | Border-colour timing bar ends before the bottom of the screen on PAL and NTSC |
| 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). | R3 | PRG with a full-view redraw inside one frame | Border-colour timing bar ends before the bottom of the screen on PAL and NTSC (manual test MT1.3); smoke check view_fast on PAL and NTSC |
| 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'. | R3, R7 | PRG in which the cursor moves and the view scrolls | Manual test MT1.2 | | 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'. | R3, R7 | PRG in which the cursor moves and the view scrolls | Manual test MT1.2 |


Manual verification: see docs/manual-tests.md section M1. Manual verification: see docs/manual-tests.md section M1.


+ 3
- 2
docs/toolchain.md View File

@@ -43,7 +43,8 @@ Check them with `pwsh -File tools/check-prereqs.ps1`. Lookup order for each tool
| Build one target | `pwsh -File tools/build.ps1 -Target <id>` | | Build one target | `pwsh -File tools/build.ps1 -Target <id>` |
| Build with a fixed map seed | `pwsh -File tools/build.ps1 -Define FIXED_SEED=12345` (`tools/run.ps1` takes `-Define` too) | | Build with a fixed map seed | `pwsh -File tools/build.ps1 -Define FIXED_SEED=12345` (`tools/run.ps1` takes `-Define` too) |
| Run in VICE | `pwsh -File tools/run.ps1 -Target <id>` (add `-Video ntsc` to try NTSC) | | Run in VICE | `pwsh -File tools/run.ps1 -Target <id>` (add `-Video ntsc` to try NTSC) |
| Automated smoke test | `pwsh -File tools/smoke.ps1` (exit 0 pass, 1 fail, 2 missing tool, 3 manual only) |
| Automated smoke test | `pwsh -File tools/smoke.ps1` (exit 0 pass, 1 fail, 2 missing tool, 3 manual only; add `-Video ntsc` to run the checks on NTSC) |
| View redraw timing bar | `pwsh -File tools/run.ps1 -Target c64 -Define VIEW_TIMING` (add `-Video ntsc`) |


## Target settings and verification status ## Target settings and verification status


@@ -80,7 +81,7 @@ cl65 V2.19 Git cc3c40c with `-O` miscompiled an indexed table lookup in a loop (
- [ ] 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 (before M2 is complete).
- [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. - [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. - [ ] A4: measure worst-case pathfinding time with a border-colour timing bar in M3.
- [ ] Frame budget: the view redraw finishes within one frame on PAL and NTSC (timing bar) in M1.
- [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.








+ 1
- 1
docs/verification-status.md View File

@@ -11,7 +11,7 @@ manual = `pending`, `confirmed by user (<date>)`, `problem reported (<date>): <n
| M0.1 Run tools/check-prereqs.ps1, then tools/smoke.ps1 for every target. | passed (2026-11-26, `pwsh -File tools/check-prereqs.ps1` exit 0; `pwsh -File tools/build.ps1` exit 0; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64, 4/4 checks). Run on the working tree including the uncommitted src/charset.c/.h edit (glyphs at codes 128+), see Notes. | confirmed by user (2026-11-26) | | M0.1 Run tools/check-prereqs.ps1, then tools/smoke.ps1 for every target. | passed (2026-11-26, `pwsh -File tools/check-prereqs.ps1` exit 0; `pwsh -File tools/build.ps1` exit 0; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64, 4/4 checks). Run on the working tree including the uncommitted src/charset.c/.h edit (glyphs at codes 128+), see Notes. | confirmed by user (2026-11-26) |
| 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.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.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 via a tile-to-glyph/colour table. | not run | 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'. | not run | pending |
| M2.1 Mark/unmark with D on rock tiles only; marked-rock glyph. Joystick port 2 cursor movement with key repeat and fire to mark. | not run | pending | | M2.1 Mark/unmark with D 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 | | 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 |


+ 21
- 1
project.json View File

@@ -384,7 +384,7 @@
}, },
{ {
"id": "M1.3", "id": "M1.3",
"text": "Assembly view redraw (draw_view(x, y), C prototype) copying a 40x23 window of the map to screen and colour RAM via a tile-to-glyph/colour table.",
"text": "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).",
"requirements": [ "requirements": [
"R3" "R3"
], ],
@@ -776,6 +776,18 @@
"symbol": "_gen_jiffies", "symbol": "_gen_jiffies",
"address": null, "address": null,
"length": 2 "length": 2
},
{
"name": "viewtime",
"symbol": "_view_cycles",
"address": null,
"length": 2
},
{
"name": "viewfast",
"symbol": "_view_fast",
"address": null,
"length": 1
} }
], ],
"checks": [ "checks": [
@@ -811,6 +823,14 @@
"bytes": null, "bytes": null,
"description": "Seed on the status line" "description": "Seed on the status line"
}, },
{
"dump": "viewfast",
"offset": 0,
"text": null,
"encoding": null,
"bytes": "01",
"description": "Full view redraw fits in one frame (view_fast = 1, R3.3)"
},
{ {
"dump": "vector", "dump": "vector",
"offset": 0, "offset": 0,


+ 27
- 2
src/charset.c View File

@@ -2,11 +2,23 @@
#include <string.h> #include <string.h>
#include <c64.h> #include <c64.h>
#include "charset.h" #include "charset.h"
#include "map.h"


#define CPU_PORT (*(unsigned char *)0x01) #define CPU_PORT (*(unsigned char *)0x01)
#define CHARROM_MIX ((const unsigned char *)0xD800) /* lower/upper-case ROM set */ #define CHARROM_MIX ((const unsigned char *)0xD800) /* lower/upper-case ROM set */


/* Original game glyphs, 8 bytes each, in screen-code order starting at GLYPH_ROCK_SOFT. */
/* Each game code carries its colour in the low nibble (see map.h). Fail the build if a
** code and the spec's colour (docs/spec.md, Visuals) ever drift apart. */
typedef char check_floor [(GLYPH_FLOOR & 15) == COLOR_GRAY1 ? 1 : -1];
typedef char check_soft [(GLYPH_ROCK_SOFT & 15) == COLOR_BROWN ? 1 : -1];
typedef char check_hard [(GLYPH_ROCK_HARD & 15) == COLOR_GRAY2 ? 1 : -1];
typedef char check_marked[(GLYPH_MARKED & 15) == COLOR_YELLOW ? 1 : -1];
typedef char check_dwarf [(GLYPH_DWARF & 15) == COLOR_ORANGE ? 1 : -1];
typedef char check_cursor[(GLYPH_CURSOR & 15) == COLOR_WHITE ? 1 : -1];
typedef char check_tiles [(TILE_FLOOR == GLYPH_FLOOR && TILE_SOFT == GLYPH_ROCK_SOFT &&
TILE_HARD == GLYPH_ROCK_HARD && TILE_SOFT_MARKED == GLYPH_MARKED) ? 1 : -1];

/* Original game glyphs, 8 bytes each, in the order of glyph_codes[] below. */
static const unsigned char glyphs[] = { static const unsigned char glyphs[] = {
/* soft rock: rough solid block */ /* soft rock: rough solid block */
0xFF, 0xDD, 0xFF, 0x77, 0xFF, 0xDD, 0xFF, 0x77, 0xFF, 0xDD, 0xFF, 0x77, 0xFF, 0xDD, 0xFF, 0x77,
@@ -22,6 +34,14 @@ static const unsigned char glyphs[] = {
0xE7, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0xE7 0xE7, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0xE7
}; };


/* Where each glyph goes; marked rock is copied to every marked tile code. */
static const unsigned char glyph_codes[] = {
GLYPH_ROCK_SOFT, GLYPH_ROCK_HARD, GLYPH_FLOOR, GLYPH_MARKED, GLYPH_DWARF, GLYPH_CURSOR
};
static const unsigned char marked_copies[] = {
TILE_HARD_MARKED, TILE_SOFT_MARKED | TILE_CLAIMED, TILE_HARD_MARKED | TILE_CLAIMED
};

/* The 2 KB charset itself, placed at CHARSET_ADDR by the linker (segment CHARSET). */ /* The 2 KB charset itself, placed at CHARSET_ADDR by the linker (segment CHARSET). */
#pragma bss-name (push, "CHARSET") #pragma bss-name (push, "CHARSET")
unsigned char charset[2048]; unsigned char charset[2048];
@@ -48,7 +68,12 @@ void charset_install(void)
CPU_PORT = old_port; CPU_PORT = old_port;
__asm__("cli"); __asm__("cli");


memcpy(dst + GLYPH_ROCK_SOFT * 8u, glyphs, sizeof glyphs);
for (i = 0; i < sizeof glyph_codes; ++i) {
memcpy(dst + glyph_codes[i] * 8u, glyphs + i * 8u, 8);
}
for (i = 0; i < sizeof marked_copies; ++i) {
memcpy(dst + marked_copies[i] * 8u, dst + GLYPH_MARKED * 8u, 8);
}


/* Screen at $0400 (upper nibble 1), charset at $3800 (bits 1-3 = 7). */ /* Screen at $0400 (upper nibble 1), charset at $3800 (bits 1-3 = 7). */
saved_vic_addr = VIC.addr; saved_vic_addr = VIC.addr;


+ 9
- 6
src/charset.h View File

@@ -12,13 +12,16 @@ extern unsigned char charset[2048];
** capital letters at screen codes 65-90. Codes 0-127 therefore keep the ROM ** capital letters at screen codes 65-90. Codes 0-127 therefore keep the ROM
** lower/upper-case shapes, and the game's own glyphs live at 128 and up. ** lower/upper-case shapes, and the game's own glyphs live at 128 and up.
** (Codes 128-255 are the reverse-video copies in ROM; this game does not use revers().) ** (Codes 128-255 are the reverse-video copies in ROM; this game does not use revers().)
**
** Each game glyph code has its colour in the low nibble, so the code can be written to
** screen RAM and colour RAM unchanged (map tile codes: see map.h).
*/ */
#define GLYPH_ROCK_SOFT 128
#define GLYPH_ROCK_HARD 129
#define GLYPH_FLOOR 130
#define GLYPH_MARKED 131
#define GLYPH_DWARF 132
#define GLYPH_CURSOR 133
#define GLYPH_FLOOR 0x8B /* = TILE_FLOOR, dark grey */
#define GLYPH_ROCK_SOFT 0x99 /* = TILE_SOFT, brown */
#define GLYPH_ROCK_HARD 0xAC /* = TILE_HARD, grey */
#define GLYPH_MARKED 0x97 /* = TILE_SOFT_MARKED, yellow (also at 0xA7, 0xD7, 0xE7) */
#define GLYPH_DWARF 0x88 /* orange */
#define GLYPH_CURSOR 0x81 /* white */


/* Build the charset at CHARSET_ADDR and point the VIC-II at it. */ /* Build the charset at CHARSET_ADDR and point the VIC-II at it. */
void charset_install(void); void charset_install(void);


+ 12
- 0
src/cycles.h View File

@@ -0,0 +1,12 @@
#ifndef STONEHOLD_CYCLES_H
#define STONEHOLD_CYCLES_H

/* Cycle counter on CIA2 timer A, for timing checks (cycles.s). */
void cycles_start(void);
unsigned cycles_read(void); /* cycles since cycles_start(), at most 65535 */

/* System clock cycles in one video frame. */
#define FRAME_CYCLES_PAL 19656u /* 312 lines x 63 */
#define FRAME_CYCLES_NTSC 17095u /* 263 lines x 65 */

#endif

+ 36
- 0
src/cycles.s View File

@@ -0,0 +1,36 @@
; Cycle counter for timing checks, on CIA2 timer A (unused by this game; the KERNAL only
; uses it for RS-232). Counts system clock cycles, at most 65535 between start and read.
;
; C prototypes (cycles.h):
; void cycles_start(void);
; unsigned cycles_read(void); /* cycles since cycles_start(), including call overhead */

.export _cycles_start, _cycles_read

CIA2_TA_LO = $DD04
CIA2_TA_HI = $DD05
CIA2_CRA = $DD0E

.segment "CODE"

_cycles_start:
lda #$00
sta CIA2_CRA ; stop timer A
lda #$FF
sta CIA2_TA_LO ; latch = $FFFF
sta CIA2_TA_HI
lda #%00010001 ; force load, start, continuous, count system clock
sta CIA2_CRA
rts

_cycles_read:
lda #$00
sta CIA2_CRA ; stop, so the two bytes belong together
lda CIA2_TA_LO ; elapsed = $FFFF - timer = timer XOR $FFFF
eor #$FF
pha
lda CIA2_TA_HI
eor #$FF
tax
pla
rts

+ 16
- 1
src/main.c View File

@@ -13,6 +13,8 @@
** row 24 status line ** row 24 status line
** **
** Build with -D FIXED_SEED=n (tools/build.ps1 -Define FIXED_SEED=n) to always generate seed n. ** Build with -D FIXED_SEED=n (tools/build.ps1 -Define FIXED_SEED=n) to always generate seed n.
** Build with -D VIEW_TIMING to redraw the view every frame with a red border while it draws
** (timing bar for R3.3: the red band must end before the next frame starts at the bottom border).
*/ */
#include <conio.h> #include <conio.h>
#include <c64.h> #include <c64.h>
@@ -21,6 +23,7 @@
#include "charset.h" #include "charset.h"
#include "map.h" #include "map.h"
#include "view.h" #include "view.h"
#include "cycles.h"


#define SCREEN ((unsigned char *)0x0400) #define SCREEN ((unsigned char *)0x0400)
#define COLRAM ((unsigned char *)0xD800) #define COLRAM ((unsigned char *)0xD800)
@@ -36,6 +39,10 @@ extern unsigned char _BSS_SIZE__[];
unsigned char layout_ok; unsigned char layout_ok;
/* Read by tools/smoke.ps1 for information: map generation time in jiffies (1/60 s). */ /* Read by tools/smoke.ps1 for information: map generation time in jiffies (1/60 s). */
unsigned gen_jiffies; unsigned gen_jiffies;
/* Read by tools/smoke.ps1: cycles for one full draw_view(), and 1 if that fits in one frame
** of the detected video standard (R3.3). */
unsigned view_cycles;
unsigned char view_fast;


static unsigned jiffies(void) static unsigned jiffies(void)
{ {
@@ -102,7 +109,6 @@ int main(void)
} }


charset_install(); charset_install();
view_init();


#ifdef FIXED_SEED #ifdef FIXED_SEED
seed = FIXED_SEED; seed = FIXED_SEED;
@@ -116,7 +122,10 @@ int main(void)


vx = view_origin(map_start_x, VIEW_W, MAP_W); vx = view_origin(map_start_x, VIEW_W, MAP_W);
vy = view_origin(map_start_y, VIEW_H, MAP_H); vy = view_origin(map_start_y, VIEW_H, MAP_H);
cycles_start();
draw_view(vx, vy); draw_view(vx, vy);
view_cycles = cycles_read();
view_fast = view_cycles < (get_tv() == TV_NTSC ? FRAME_CYCLES_NTSC : FRAME_CYCLES_PAL);
draw_dwarves(vx, vy); draw_dwarves(vx, vy);


gotoxy(0, 24); gotoxy(0, 24);
@@ -127,6 +136,12 @@ int main(void)


while (!kbhit()) { while (!kbhit()) {
wait_frame(); wait_frame();
#ifdef VIEW_TIMING
bordercolor(COLOR_RED);
draw_view(vx, vy);
bordercolor(COLOR_BLACK);
draw_dwarves(vx, vy);
#endif
} }
cgetc(); cgetc();
charset_remove(); charset_remove();


+ 28
- 7
src/map.h View File

@@ -8,13 +8,34 @@
#define MAP_H 128u #define MAP_H 128u
#define MAP_ADDR 0x4000u #define MAP_ADDR 0x4000u


/* Tile byte: bits 0-1 tile type, bit 6 claimed by a dwarf, bit 7 marked for digging. */
#define TILE_FLOOR 0x00
#define TILE_SOFT 0x01
#define TILE_HARD 0x02
#define TILE_TYPE 0x03
#define TILE_CLAIMED 0x40
#define TILE_MARKED 0x80
/* Tile byte. Each map byte is also the screen code it is drawn with, and its low nibble is
** its colour (colour RAM keeps only 4 bits), so draw_view copies bytes without a lookup (R3.3).
** bit 7 always 1 (game glyphs live at codes 128-255)
** bit 6 claimed by a dwarf
** bits 5-4 type: 00 floor, 01 soft rock, 10 hard rock
** bits 3-0 colour: the type's colour, or yellow (7) when marked for digging
** charset.c puts a glyph at every code used here; see docs/memory-map.md. */
#define TILE_FLOOR 0x8B /* type 0, dark grey (COLOR_GRAY1) */
#define TILE_SOFT 0x99 /* type 1, brown (COLOR_BROWN) */
#define TILE_HARD 0xAC /* type 2, grey (COLOR_GRAY2) */
#define TILE_CLAIMED 0x40
#define TILE_TYPE_MASK 0x30
#define TILE_TYPE_FLOOR 0x00
#define TILE_TYPE_SOFT 0x10
#define TILE_TYPE_HARD 0x20
#define TILE_COLOR_MASK 0x0F
#define TILE_COLOR_MARKED 0x07 /* COLOR_YELLOW */

#define TILE_IS_ROCK(t) (((t) & TILE_TYPE_MASK) != TILE_TYPE_FLOOR)
#define TILE_IS_HARD(t) (((t) & TILE_TYPE_MASK) == TILE_TYPE_HARD)
#define TILE_IS_MARKED(t) (((t) & TILE_COLOR_MASK) == TILE_COLOR_MARKED)
/* Marked version of rock tile t (claim flag kept). Only for rock tiles. */
#define TILE_MARK(t) ((unsigned char)(((t) & 0xF0) | TILE_COLOR_MARKED))
/* Unmarked, unclaimed version of rock tile t. */
#define TILE_UNMARK(t) ((unsigned char)(TILE_IS_HARD(t) ? TILE_HARD : TILE_SOFT))
/* Marked tiles drawn by charset.c: soft, hard, and both again with the claim bit. */
#define TILE_SOFT_MARKED 0x97
#define TILE_HARD_MARKED 0xA7


/* Address of tile (x, y) = MAP_ADDR + y*128 + x; x and y must be 0-127. /* Address of tile (x, y) = MAP_ADDR + y*128 + x; x and y must be 0-127.
** Built from bytes (high = $40 + y/2, low = (y odd ? $80 : 0) + x) because a 16-bit ** Built from bytes (high = $40 + y/2, low = (y odd ? $80 : 0) + x) because a 16-bit


+ 0
- 48
src/view.c View File

@@ -1,48 +0,0 @@
/* Map view (R3). Tile bytes are translated through two 256-entry tables, so the
** marked/claimed flag bits need no special case when drawing. */
#include <c64.h>
#include "charset.h"
#include "map.h"
#include "view.h"

#define SCREEN ((unsigned char *)0x0400)
#define COLRAM ((unsigned char *)0xD800)

unsigned char tile_glyph[256];
unsigned char tile_color[256];

void view_init(void)
{
unsigned i = 0;
unsigned char t;
do {
t = (unsigned char)i;
if (t & TILE_MARKED) {
tile_glyph[i] = GLYPH_MARKED; tile_color[i] = COLOR_YELLOW;
} else switch (t & TILE_TYPE) {
case TILE_FLOOR: tile_glyph[i] = GLYPH_FLOOR; tile_color[i] = COLOR_GRAY1; break;
case TILE_HARD: tile_glyph[i] = GLYPH_ROCK_HARD; tile_color[i] = COLOR_GRAY2; break;
default: tile_glyph[i] = GLYPH_ROCK_SOFT; tile_color[i] = COLOR_BROWN; break;
}
} while (++i < 256u);
}

void draw_view(unsigned char vx, unsigned char vy)
{
unsigned char row, col, t;
const unsigned char *src;
unsigned char *scr = SCREEN + VIEW_TOP * 40u;
unsigned char *clr = COLRAM + VIEW_TOP * 40u;

for (row = 0; row < VIEW_H; ++row) {
src = MAP_PTR(vx, vy + row);
/* Written with pointer increments: the indexed form (scr[col] = tile_glyph[t])
** is miscompiled by cc65 V2.19 cc3c40c with -O (it indexes the table with the
** low byte of the screen address). M1.3 replaces this loop with assembly. */
for (col = 0; col < VIEW_W; ++col) {
t = *src++;
*scr++ = tile_glyph[t];
*clr++ = tile_color[t];
}
}
}

+ 3
- 6
src/view.h View File

@@ -6,12 +6,9 @@
#define VIEW_H 23u #define VIEW_H 23u
#define VIEW_TOP 1u #define VIEW_TOP 1u


/* Build the tile-to-glyph/colour tables. Call once after charset_install(). */
void view_init(void);

/* Draw the VIEW_W x VIEW_H window of the map whose top-left tile is (vx, vy). /* Draw the VIEW_W x VIEW_H window of the map whose top-left tile is (vx, vy).
** vx must be 0..MAP_W-VIEW_W and vy 0..MAP_H-VIEW_H.
** M1.2: plain C. M1.3 replaces it with an assembly routine with the same prototype. */
void draw_view(unsigned char vx, unsigned char vy);
** vx must be 0..MAP_W-VIEW_W and vy 0..MAP_H-VIEW_H. Assembly (view.s).
** IRQs are held off while it runs. */
void __fastcall__ draw_view(unsigned char vx, unsigned char vy);


#endif #endif

+ 75
- 0
src/view.s View File

@@ -0,0 +1,75 @@
; draw_view(vx, vy): copy the 40x23 map window with top-left tile (vx, vy) to screen rows 1-23
; and colour RAM. Each map byte is its own screen code and carries its colour in the low
; nibble (map.h), so the same byte goes to screen RAM and colour RAM: 14 cycles per tile.
; Measured in M1.3: about 14,900 cycles per full redraw (PAL frame 19,656, NTSC 17,095).
;
; Speed: the column loop is outermost (Y = 39..0) and the 23 rows are unrolled inside it,
; so every screen and colour address is a constant. The 23 map source addresses are patched
; 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);
; vy arrives in A, vx on the C stack.

.export _draw_view
.importzp c_sp
.import incsp1

SCREEN = $0400
COLRAM = $D800
VIEW_TOP = 1
VIEW_H = 23
VIEW_W = 40
MAP_HI = $40 ; map at $4000

.segment "BSS"
src_lo: .res 1
src_hi: .res 1

.segment "CODE"

_draw_view:
; 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
lda #0
ror a ; A = $80 if vy odd
ldy #0
ora (c_sp),y ; + vx (vx < $80)
sta src_lo
txa
clc
adc #MAP_HI
sta src_hi
jsr incsp1 ; drop vx from the C stack

; Patch the 23 row source operands, adding 128 for each row.
.repeat VIEW_H, R
lda src_lo
sta .ident(.sprintf("src%d", R)) + 1
lda src_hi
sta .ident(.sprintf("src%d", R)) + 2
lda src_lo
eor #$80
sta src_lo
bmi :+ ; low byte went $0x -> $8x: no carry
inc src_hi
:
.endrep

php
sei ; keep timing steady; a pending IRQ runs after plp
ldy #VIEW_W - 1
col_loop:
.repeat VIEW_H, R
.ident(.sprintf("src%d", R)):
lda $4000,y ; operand patched above
sta SCREEN + (VIEW_TOP + R) * 40,y
sta COLRAM + (VIEW_TOP + R) * 40,y
.endrep
dey
bmi done
jmp col_loop
done:
plp
rts

+ 3
- 0
tools/smoke.ps1 View File

@@ -18,12 +18,14 @@


.EXAMPLE .EXAMPLE
pwsh -File tools/smoke.ps1 pwsh -File tools/smoke.ps1
pwsh -File tools/smoke.ps1 -Video ntsc # run the same checks on an NTSC machine
pwsh -File tools/smoke.ps1 -Target c64 pwsh -File tools/smoke.ps1 -Target c64
#> #>
[CmdletBinding()] [CmdletBinding()]
param( param(
[string]$Target, [string]$Target,
[switch]$NoBuild, [switch]$NoBuild,
[ValidateSet('pal', 'ntsc')][string]$Video,
[int]$TimeoutSeconds = 180 [int]$TimeoutSeconds = 180
) )
. (Join-Path $PSScriptRoot 'lib.ps1') . (Join-Path $PSScriptRoot 'lib.ps1')
@@ -128,6 +130,7 @@ try {
$done = @{} $done = @{}
$results = @() $results = @()
foreach ($t in $targets) { foreach ($t in $targets) {
if ($Video) { $t.video = $Video }
if (-not $NoBuild) { Invoke-TargetBuild $project $t $done } if (-not $NoBuild) { Invoke-TargetBuild $project $t $done }
if (-not $t.smoke) { if (-not $t.smoke) {
$results += @{ target = $t.id; status = 'MANUAL'; checks = @(); message = 'No automated smoke check for this target; follow docs/manual-tests.md (M0).' } $results += @{ target = $t.id; status = 'MANUAL'; checks = @(); message = 'No automated smoke check for this target; follow docs/manual-tests.md (M0).' }


Loading…
Cancel
Save

Powered by TurnKey Linux.