Ver código fonte

Add petcube: a see-through rotating cube in PETSCII quarter blocks

Wireframe cube on an 80x50 grid built from the 16 quarter-block PETSCII
characters, with an assembly Bresenham plotter that merges pixels into
shared cells. 256 perspective frames are precomputed by tools/gen_cube.py.
Two screens in VIC bank 2 are flipped in the border, paced by a frame-
counting raster IRQ, at a steady 25 fps (PAL). The self-test checks the
plotter, the per-image cycle budget and that no image is held too long.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
refactor/agent-workflow
Daniel Covington 1 semana atrás
pai
commit
fae73342d2
6 arquivos alterados com 821 adições e 0 exclusões
  1. +12
    -0
      projects/petcube/c64project.json
  2. +263
    -0
      projects/petcube/src/cubedata.h
  3. +266
    -0
      projects/petcube/src/draw.s
  4. +35
    -0
      projects/petcube/src/frame.s
  5. +169
    -0
      projects/petcube/src/main.c
  6. +76
    -0
      projects/petcube/tools/gen_cube.py

+ 12
- 0
projects/petcube/c64project.json Ver arquivo

@@ -0,0 +1,12 @@
{
"name": "petcube",
"machine": "c64",
"language": "c",
"sources": ["src/*.c", "src/*.s"],
"flags": ["-Oirs"],
"disk": true,
"test": {
"expect": ["PETSCII CUBE"],
"timeoutSeconds": 60
}
}

+ 263
- 0
projects/petcube/src/cubedata.h Ver arquivo

@@ -0,0 +1,263 @@
/* Generated by tools/gen_cube.py: do not edit. */
#define CUBE_FRAMES 256
static const unsigned char edge_a[12] = { 0, 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6 };
static const unsigned char edge_b[12] = { 1, 2, 4, 3, 5, 3, 6, 7, 5, 6, 7, 7 };
/* per frame: x0, y0, ... x7, y7 on the 80x46 grid */
static const unsigned char cube_xy[4096] = {
24,7,31,14,24,38,31,31,55,7,48,14,55,38,48,31,
24,7,31,13,23,38,31,31,55,7,49,13,54,39,49,31,
23,7,32,13,23,37,31,30,55,7,50,13,53,40,49,32,
23,7,33,13,22,37,32,30,55,8,51,13,52,41,49,32,
23,7,33,12,22,36,32,29,54,8,52,13,51,41,50,32,
23,7,34,12,22,36,33,29,54,8,52,13,50,42,50,32,
23,8,35,12,21,35,33,29,53,9,53,13,48,43,50,32,
23,8,35,12,21,35,34,28,52,9,54,13,47,43,51,32,
23,8,36,11,22,34,35,28,51,9,54,13,46,43,51,32,
23,8,37,11,22,33,35,28,50,10,55,13,45,44,51,33,
23,8,38,11,22,33,36,28,49,10,56,13,43,44,52,33,
23,8,38,11,22,33,36,27,48,10,56,13,42,44,52,33,
23,8,39,11,22,32,37,27,46,10,57,13,41,44,52,33,
23,8,39,11,23,32,38,27,45,10,57,13,40,44,53,34,
23,9,40,11,23,31,38,27,43,10,58,13,38,44,53,34,
23,9,41,11,23,31,39,27,41,10,58,13,37,44,53,34,
24,9,41,11,24,31,39,27,40,10,58,13,36,44,53,35,
24,9,42,11,24,31,40,27,38,10,59,13,35,44,54,35,
24,9,42,11,25,30,41,27,36,10,59,12,34,44,54,35,
25,9,43,11,25,30,41,27,35,10,59,12,33,44,54,36,
25,10,43,11,26,30,42,27,33,10,59,12,32,43,54,36,
26,10,44,11,26,30,43,28,32,9,59,12,31,43,55,36,
26,10,44,11,27,30,43,28,30,9,59,11,31,43,55,37,
26,10,45,12,27,30,44,28,29,9,59,11,30,42,55,37,
27,11,45,12,27,30,44,28,28,9,59,11,29,42,55,37,
27,11,46,12,28,30,45,29,27,8,58,10,28,42,55,38,
28,11,46,12,28,30,45,29,26,8,58,10,27,41,55,38,
28,12,47,12,29,30,46,29,26,8,58,9,27,41,55,38,
29,12,47,12,29,30,46,30,25,7,57,9,26,40,55,38,
29,12,47,13,30,31,47,30,25,7,57,8,25,40,55,38,
30,13,48,13,30,31,47,31,24,7,56,8,25,39,55,38,
30,13,48,13,30,31,48,31,24,7,56,7,24,39,55,38,
31,14,48,14,31,31,48,31,24,7,55,7,24,38,55,38,
31,14,49,14,31,32,49,32,24,7,55,6,23,38,55,38,
31,15,49,14,31,32,49,32,24,7,54,6,23,37,55,38,
32,15,49,15,31,32,50,33,24,7,54,5,22,37,55,37,
32,16,50,15,32,33,50,33,24,7,53,5,22,36,55,37,
33,16,50,15,32,33,50,34,24,7,53,4,21,35,54,36,
33,17,50,16,32,34,51,34,24,7,52,4,21,35,54,36,
33,17,50,16,32,34,51,35,24,7,52,4,20,34,54,35,
34,18,50,17,32,35,51,35,25,7,51,3,20,33,53,34,
34,18,50,17,32,35,52,36,25,7,51,3,20,32,53,33,
34,19,51,18,32,35,52,36,25,7,51,3,19,31,53,32,
34,19,51,18,32,36,52,36,26,7,50,3,19,31,53,30,
35,20,51,19,32,36,52,37,26,8,50,3,19,30,52,29,
35,21,51,19,32,37,53,37,26,8,50,3,18,29,52,27,
35,21,51,20,32,37,53,38,27,8,50,3,18,28,52,26,
35,22,51,20,31,38,53,38,27,8,49,3,18,27,52,24,
35,23,51,21,31,38,53,38,27,9,49,4,18,26,52,22,
35,23,51,21,31,39,53,39,28,9,49,4,18,25,52,21,
35,24,51,22,31,39,53,39,28,9,49,4,18,24,52,19,
35,24,51,23,30,39,54,39,28,9,49,5,18,22,52,17,
35,25,51,23,30,40,54,39,29,10,49,5,18,21,52,16,
35,26,51,24,29,40,54,39,29,10,49,6,18,20,52,14,
34,26,51,24,29,40,54,39,29,10,49,6,18,19,52,13,
34,27,51,25,29,40,54,39,29,11,49,7,18,17,52,12,
34,27,51,26,28,40,54,39,30,11,49,8,19,16,53,11,
34,28,50,27,27,41,54,39,30,11,49,8,19,15,53,10,
33,29,50,27,27,41,54,39,30,12,49,9,19,14,53,9,
33,29,50,28,26,40,55,39,30,12,49,10,20,12,54,8,
33,30,50,29,26,40,55,39,30,12,49,10,21,11,54,8,
32,30,49,29,25,40,55,39,30,13,49,11,21,10,54,7,
32,31,49,30,25,39,55,39,30,13,49,12,22,9,55,7,
31,31,49,31,24,39,55,38,31,13,49,13,23,8,55,7,
31,31,48,31,24,38,55,38,31,14,48,14,24,7,55,7,
30,32,48,32,23,37,55,38,31,14,48,14,25,6,56,7,
29,32,48,33,23,37,56,38,31,14,48,15,26,5,56,7,
29,32,47,33,23,36,56,38,31,14,48,16,27,4,56,7,
28,32,47,34,22,34,56,37,31,15,48,17,28,3,56,8,
27,33,46,35,22,33,56,37,31,15,47,18,29,3,57,8,
27,33,46,36,22,32,56,37,31,15,47,18,30,2,57,9,
26,33,45,36,23,30,56,37,31,15,47,19,31,2,57,10,
25,33,44,37,23,28,56,36,31,15,46,20,32,1,57,10,
24,32,44,37,23,27,57,36,30,15,46,21,34,1,57,11,
23,32,43,38,24,25,57,36,30,15,46,22,35,1,57,12,
23,32,42,39,25,23,57,36,30,15,45,22,36,1,57,13,
22,31,41,39,26,21,57,36,30,16,45,23,37,1,57,14,
21,31,41,40,27,19,57,36,30,16,44,24,38,1,57,14,
20,30,40,40,28,18,57,36,30,16,44,24,39,1,57,15,
20,30,39,40,29,16,57,36,30,16,43,25,40,1,57,16,
19,29,38,41,31,14,57,36,30,16,43,26,41,2,56,17,
19,28,37,41,32,12,57,36,30,15,42,26,42,2,56,18,
18,27,36,42,34,11,57,36,30,15,42,27,43,3,56,19,
18,26,35,42,36,10,57,36,30,15,41,27,44,3,56,20,
17,25,34,42,38,8,57,36,30,15,40,28,44,4,55,21,
17,23,34,42,39,7,57,36,30,15,40,29,45,5,55,22,
17,22,33,42,41,6,57,36,30,15,39,29,46,5,54,23,
17,20,32,42,43,6,57,36,30,15,38,29,46,6,54,24,
17,19,31,42,45,5,57,36,30,15,37,30,47,7,53,25,
18,17,30,42,46,5,57,37,30,15,37,30,47,8,53,26,
18,16,29,42,48,5,56,37,30,14,36,30,47,8,52,27,
19,14,28,41,49,5,56,37,30,14,35,31,48,9,52,27,
19,13,27,41,51,5,56,37,30,14,34,31,48,10,51,28,
20,11,26,40,52,5,56,38,30,14,33,31,48,11,51,29,
21,10,25,40,53,6,56,38,30,14,32,31,48,12,50,30,
22,8,24,39,54,6,55,38,30,14,31,31,48,13,49,31,
24,7,24,38,55,7,55,38,31,14,31,31,48,14,48,31,
25,5,23,37,56,7,55,38,31,13,30,31,48,14,48,32,
27,4,23,36,57,8,55,39,31,13,29,31,48,15,47,33,
28,3,22,35,57,9,55,39,31,13,28,31,48,16,46,34,
30,2,22,34,58,10,54,39,31,13,27,31,48,17,45,34,
31,2,22,32,58,11,54,39,31,13,26,31,48,18,44,35,
33,1,22,31,59,12,54,39,31,13,25,30,47,19,44,35,
34,1,22,29,59,13,54,40,32,13,25,30,47,20,43,36,
36,0,22,28,59,14,53,40,32,13,24,30,47,20,42,36,
37,0,23,26,59,15,53,40,32,13,23,29,46,21,41,37,
39,0,23,24,59,16,53,40,32,13,22,29,46,22,40,37,
40,0,24,22,59,17,53,40,32,13,22,28,46,23,39,38,
42,0,25,21,59,17,53,40,32,13,21,27,45,23,38,38,
43,1,27,19,59,18,53,40,32,13,20,27,44,24,37,39,
44,1,28,17,59,19,53,40,32,13,20,26,44,25,36,39,
45,2,29,15,58,20,53,40,32,13,19,25,43,25,35,39,
46,2,31,14,58,21,53,40,33,13,19,24,43,26,34,39,
47,3,33,12,57,22,53,40,33,13,18,23,42,26,33,40,
47,3,35,11,57,23,53,40,33,13,18,22,41,27,32,40,
48,4,36,10,57,24,53,40,33,13,18,21,41,27,31,40,
48,5,38,9,56,24,53,40,33,13,18,20,40,28,31,40,
49,6,40,8,56,25,53,40,32,13,18,19,39,28,30,40,
49,6,42,7,55,26,53,40,32,13,18,18,39,29,29,40,
49,7,44,6,54,27,53,40,32,13,18,17,38,29,28,40,
50,8,45,6,54,27,53,39,32,14,18,15,37,29,27,40,
50,9,47,6,53,28,54,39,32,14,19,14,36,30,27,40,
50,10,49,5,53,29,54,39,32,14,19,13,35,30,26,40,
50,10,50,5,52,29,54,39,32,14,20,12,35,30,25,40,
49,11,51,5,51,30,54,39,32,14,20,11,34,31,25,39,
49,12,53,6,50,30,55,39,31,14,21,10,33,31,24,39,
49,12,54,6,50,31,55,39,31,14,22,9,32,31,24,39,
49,13,54,6,49,31,55,38,31,14,23,8,31,31,24,39,
48,14,55,7,48,31,55,38,31,14,24,7,31,31,24,38,
48,14,56,7,48,32,55,38,30,14,25,6,30,32,24,38,
48,15,56,8,47,32,56,38,30,13,26,5,29,32,24,38,
47,15,57,8,46,32,56,38,30,13,27,4,28,32,24,37,
47,16,57,9,46,33,56,38,29,13,28,4,27,32,25,37,
46,16,57,9,45,33,56,38,29,13,29,3,27,32,25,37,
46,16,58,10,44,33,56,37,29,13,31,2,26,32,26,36,
45,17,58,10,44,33,56,37,28,13,32,2,25,32,27,36,
44,17,57,11,43,34,56,37,28,13,33,2,25,32,28,36,
44,17,57,12,42,34,56,37,28,12,34,1,24,32,29,35,
43,17,57,12,41,34,56,37,27,12,36,1,23,32,30,35,
43,18,57,12,41,34,56,37,27,12,37,1,23,32,31,35,
42,18,57,13,40,34,56,37,27,12,38,1,22,32,33,35,
41,18,56,13,40,34,56,37,26,11,39,1,22,32,34,35,
41,18,56,14,39,34,56,36,26,11,41,1,21,32,36,35,
40,18,56,14,38,34,56,36,26,11,42,1,21,32,38,35,
40,18,55,14,38,34,55,36,26,10,43,1,21,32,39,35,
39,18,55,14,37,34,55,36,25,10,44,1,20,32,41,35,
38,18,54,15,37,34,55,36,25,10,45,1,20,33,43,35,
38,18,54,15,36,34,54,36,25,9,46,1,20,33,44,35,
37,18,53,15,36,34,54,35,25,9,47,2,20,33,46,35,
36,17,53,15,35,34,53,35,24,9,48,2,20,33,47,36,
36,17,52,15,35,34,53,35,24,8,48,2,20,34,49,36,
35,17,52,15,34,33,53,35,24,8,49,3,20,34,50,36,
35,17,52,15,34,33,52,34,24,8,50,3,20,34,51,36,
34,16,51,15,33,33,52,34,24,7,51,3,21,35,52,37,
34,16,51,15,33,33,51,34,24,7,52,4,21,35,53,37,
33,16,50,15,32,33,51,33,24,7,52,4,21,36,53,37,
33,15,50,15,32,33,50,33,24,7,53,5,22,36,54,38,
32,15,49,14,32,32,50,33,24,7,54,5,22,37,54,38,
32,14,49,14,31,32,49,32,24,7,54,6,23,37,55,38,
31,14,49,14,31,32,49,32,24,7,55,6,23,38,55,38,
31,14,48,14,31,31,48,31,24,7,55,7,24,38,55,38,
30,13,48,13,30,31,48,31,24,7,56,7,24,39,55,38,
30,13,48,13,30,31,48,30,24,7,56,8,25,39,55,38,
29,12,48,13,30,30,47,30,24,8,57,8,25,40,55,38,
29,12,47,12,29,30,47,29,24,8,57,9,26,40,55,38,
29,11,47,12,29,30,46,29,25,9,58,10,26,41,55,38,
28,11,47,11,29,29,46,28,25,9,58,10,27,41,55,38,
28,10,47,11,29,29,46,28,25,10,59,11,27,41,55,38,
28,10,47,10,29,28,45,27,26,11,59,12,28,42,54,38,
27,9,47,10,29,28,45,27,26,12,59,13,28,42,54,38,
27,9,47,10,28,27,45,26,26,13,60,14,28,42,54,38,
27,9,47,9,28,27,45,26,26,15,60,14,29,42,53,38,
27,8,47,9,28,26,44,25,27,16,60,15,29,42,53,37,
26,8,47,8,28,26,44,24,27,18,61,16,29,42,53,37,
26,7,47,8,28,25,44,24,27,19,61,17,29,42,52,37,
26,7,48,7,28,25,44,23,27,21,61,18,30,42,52,37,
26,7,48,7,28,24,44,22,27,23,61,19,30,41,52,36,
26,6,48,6,28,24,44,22,27,24,61,20,30,41,51,36,
26,6,48,6,28,23,44,21,27,26,61,21,30,41,51,36,
25,6,49,6,28,22,44,21,27,28,61,23,30,40,51,36,
25,6,49,5,28,22,44,20,27,29,61,24,30,40,50,35,
25,6,50,5,28,21,44,19,27,31,61,25,30,39,50,35,
25,6,50,5,28,21,45,19,27,32,61,26,30,39,50,35,
25,6,50,5,28,20,45,18,27,33,61,28,30,38,50,34,
25,6,51,5,28,19,45,18,26,34,60,29,30,37,49,34,
25,6,52,4,29,18,45,17,26,35,60,30,30,37,49,34,
25,6,52,4,29,18,46,16,26,36,60,31,30,36,49,33,
24,6,53,5,29,17,46,16,25,37,59,33,30,35,49,33,
24,6,53,5,29,16,46,15,25,37,58,34,30,35,49,33,
24,6,54,5,30,16,47,15,25,38,58,35,30,34,49,32,
24,6,54,6,30,15,47,14,24,38,57,36,30,33,49,32,
24,7,55,6,30,14,48,14,24,38,56,37,30,32,48,32,
24,7,55,7,31,14,48,14,24,38,55,38,31,31,48,31,
24,7,56,8,31,13,49,13,23,38,54,39,31,31,48,31,
23,7,56,8,31,12,50,13,23,38,53,40,31,30,48,31,
23,7,56,9,32,12,50,13,23,38,52,41,31,29,48,31,
23,8,57,11,32,11,51,13,23,37,51,42,31,28,48,30,
23,8,57,12,33,10,52,12,22,37,50,42,32,27,48,30,
23,8,57,13,33,9,52,12,22,36,49,43,32,27,48,30,
23,8,56,15,34,9,53,12,22,35,48,43,32,26,48,30,
23,9,56,17,35,8,54,12,22,35,47,44,33,25,48,30,
22,9,56,18,35,8,55,13,22,34,45,44,33,24,49,30,
22,9,55,20,36,7,56,13,22,33,44,44,33,23,49,30,
22,9,54,22,37,6,56,13,22,32,43,44,34,23,49,30,
22,9,53,24,38,6,57,14,22,31,42,44,34,22,49,29,
22,9,52,26,38,5,58,14,22,31,41,44,35,21,49,29,
22,9,51,27,39,5,59,15,22,30,40,44,35,21,49,29,
22,9,50,29,40,5,59,15,22,29,39,44,36,20,49,29,
22,9,48,31,41,4,60,16,23,28,38,43,36,19,49,29,
22,9,47,33,42,4,60,17,23,27,37,43,37,19,49,30,
22,9,45,34,43,3,61,18,23,26,36,42,37,18,49,30,
22,9,43,35,44,3,61,19,23,25,35,42,38,18,49,30,
22,9,41,37,45,3,62,20,24,24,35,41,39,17,49,30,
22,9,40,38,45,3,62,22,24,23,34,40,39,16,49,30,
22,9,38,39,46,3,62,23,25,22,33,40,40,16,49,30,
22,9,36,39,47,3,62,25,25,21,33,39,41,16,49,30,
22,9,34,40,48,3,62,26,26,20,32,38,42,15,49,30,
22,8,33,40,49,3,61,28,26,19,32,37,42,15,49,30,
23,8,31,40,50,3,61,29,27,18,32,37,43,15,49,31,
23,8,30,40,51,4,60,31,27,18,31,36,44,14,49,31,
23,8,28,40,52,4,60,32,28,17,31,35,45,14,49,31,
23,7,27,40,53,5,59,34,28,16,31,34,46,14,49,31,
23,7,26,39,54,5,58,35,29,15,31,33,47,14,49,31,
24,7,25,39,55,6,57,37,30,14,31,32,48,14,49,31,
24,7,24,38,55,7,55,38,31,14,31,31,48,14,48,31,
24,7,23,38,56,8,54,40,31,13,31,31,49,14,48,32,
24,6,22,37,56,9,52,41,32,12,31,30,50,14,48,32,
24,6,22,36,57,10,51,42,33,11,31,29,51,14,48,32,
25,6,21,35,57,11,49,43,34,11,31,28,52,14,48,32,
25,6,21,34,57,13,48,43,35,10,31,27,53,14,48,32,
25,6,20,33,57,14,46,44,35,10,32,26,54,15,48,32,
25,5,20,32,57,16,45,44,36,9,32,25,54,15,47,32,
26,5,20,31,57,17,43,45,37,9,32,25,55,15,47,32,
26,5,20,30,56,19,42,45,38,8,33,24,56,16,47,32,
26,5,20,29,56,21,40,45,39,8,33,23,57,16,47,32,
26,5,20,28,55,23,39,45,40,7,33,22,57,17,47,32,
26,5,20,28,54,24,37,45,41,7,34,22,58,18,47,32,
26,5,20,27,52,26,36,44,42,6,35,21,59,18,47,32,
26,5,20,26,51,28,35,44,43,6,35,20,59,19,47,32,
26,5,21,25,50,30,34,43,44,6,36,20,60,20,47,32,
26,5,21,24,48,31,33,43,45,6,36,19,60,21,46,32,
26,5,22,23,46,33,32,42,46,5,37,19,61,22,46,32,
26,5,22,22,44,34,32,42,47,5,38,18,61,23,46,32,
26,5,22,21,43,35,31,41,48,5,38,18,61,24,46,32,
26,5,23,21,41,36,31,40,48,5,39,17,61,25,46,32,
26,5,23,20,39,37,30,39,49,5,40,17,61,26,47,32,
26,5,24,19,37,38,30,39,50,5,40,16,61,27,47,32,
26,5,25,18,35,39,30,38,51,5,41,16,61,28,47,32,
26,6,25,18,34,39,29,37,52,5,42,16,61,30,47,31,
25,6,26,17,32,39,29,36,52,5,43,15,60,31,47,31,
25,6,26,16,30,40,29,35,53,5,44,15,60,32,47,31,
25,6,27,16,29,40,29,35,54,5,44,15,59,33,47,31,
25,6,28,15,28,40,30,34,54,6,45,14,59,34,47,31,
24,6,29,15,26,39,30,33,55,6,46,14,58,35,48,31,
24,6,29,14,25,39,30,33,55,6,47,14,57,36,48,31,
24,7,30,14,25,39,30,32,55,6,48,14,56,37,48,31,
};

+ 266
- 0
projects/petcube/src/draw.s Ver arquivo

@@ -0,0 +1,266 @@
; petcube: quarter-block line drawing.
;
; The cube is drawn on an 80x46 grid of "pixels", four per character cell:
; bit 0 = upper left, 1 = upper right, 2 = lower left, 3 = lower right.
; ORTAB holds, for each of the four pixels, a 256-byte page that maps a
; cell's current screen code to the code with that pixel added. A plot is
; then one read, one table lookup and one write, and lines can cross and
; share cells freely.
;
; Two screens in VIC bank 2 ($8000, $8400); draw_page ($80 or $84) is the
; hidden back buffer. Only rows 0-22 are drawn and cleared; rows 23-24 hold
; the caption on both screens.
;
; Uses cc65's ptr1 and tmp1-tmp4: call only from the main program (no IRQ
; uses them; the program runs with interrupts off).

.export _line, _clear_back, _draw_init, _draw_edges, _frame_xy, _frame_vis
.export _lx0, _ly0, _lx1, _ly1, _draw_page, _mask2code, _code2mask
.importzp ptr1, ptr2, tmp1, tmp2, tmp3, tmp4

ROWS = 23 ; character rows used by the cube
ORTAB = $7C00 ; 4 pages, built by draw_init (free RAM below $8000)

cx = tmp1
cy = tmp2
count = tmp3
err = tmp4

.bss
_lx0: .res 1
_ly0: .res 1
_lx1: .res 1
_ly1: .res 1
_draw_page: .res 1
_frame_vis: .res 2 ; edges to draw, bit e = edge e
_frame_xy = ptr2 ; this frame's 8 (x, y) vertex pairs
ecur: .res 1
major: .res 1
minor: .res 1
ypage: .res 1 ; ORTAB page for this pixel row (left column)
_code2mask: .res 256

.rodata
; screen codes for masks 0-15 (uppercase/graphics character set)
_mask2code:
.byte 32, 126, 124, 226, 123, 97, 255, 236
.byte 108, 127, 225, 251, 98, 252, 254, 160

; per pixel row y (0-45): offset of character row y/2, and its ORTAB page
rowlo:
.repeat ROWS*2, I
.byte <((I/2)*40)
.endrepeat
rowhi:
.repeat ROWS*2, I
.byte >((I/2)*40)
.endrepeat
ypg:
.repeat ROWS*2, I
.byte >ORTAB + (I .mod 2)*2
.endrepeat

; cube edges as vertex-pair offsets into frame_xy (vertex * 2)
edge_a2: .byte 0, 0, 0, 2, 2, 4, 4, 6, 8, 8, 10, 12
edge_b2: .byte 2, 4, 8, 6, 10, 6, 12, 14, 10, 12, 14, 14

.code

; void draw_edges(void): draw each edge in frame_vis, from frame_xy
_draw_edges:
ldx #0
@edge: lsr _frame_vis+1
ror _frame_vis
bcc @skip
stx ecur
ldy edge_a2,x
lda (_frame_xy),y
sta _lx0
iny
lda (_frame_xy),y
sta _ly0
ldy edge_b2,x
lda (_frame_xy),y
sta _lx1
iny
lda (_frame_xy),y
sta _ly1
jsr _line
ldx ecur
@skip: inx
cpx #12
bne @edge
rts

; void draw_init(void): build code2mask and ORTAB
_draw_init:
ldx #15 ; code2mask = inverse of mask2code (others 0)
: lda _mask2code,x
tay
txa
sta _code2mask,y
dex
bpl :-
ldx #0 ; ORTAB[b][code] = mask2code[code2mask[code] | 1<<b]
@fill: ldy _code2mask,x
tya
ora #1
tay
lda _mask2code,y
sta ORTAB,x
ldy _code2mask,x
tya
ora #2
tay
lda _mask2code,y
sta ORTAB+$100,x
ldy _code2mask,x
tya
ora #4
tay
lda _mask2code,y
sta ORTAB+$200,x
ldy _code2mask,x
tya
ora #8
tay
lda _mask2code,y
sta ORTAB+$300,x
inx
bne @fill
rts

; point ptr1 at the start of pixel row cy's character row in the back buffer
.macro SETROW
ldx cy
lda rowlo,x
sta ptr1
lda rowhi,x
ora _draw_page
sta ptr1+1
lda ypg,x
sta ypage
.endmacro

; set pixel (cx, cy); ptr1 and ypage must match cy. \1 = unique label
.macro PLOT smc
lda cx
lsr a ; column; carry = right half of the cell
tay
lda ypage
adc #0
sta smc+2
lda (ptr1),y
tax
smc: lda ORTAB,x
sta (ptr1),y
.endmacro

; void line(void): draw from (lx0, ly0) to (lx1, ly1), both ends included
_line:
lda _lx0
sta cx
lda _ly0
sta cy
lda #$E6 ; inc zp
sta xstep1
sta xstep2
lda _lx1
sec
sbc _lx0
bcs :+
eor #$FF
adc #1
ldx #$C6 ; dec zp
stx xstep1
stx xstep2
: sta major ; dx for now
lda #$E6
sta ystep1
sta ystep2
lda _ly1
sec
sbc _ly0
bcs :+
eor #$FF
adc #1
ldx #$C6
stx ystep1
stx ystep2
: sta minor ; dy for now
SETROW
lda minor
cmp major
bcc xmajor
ldx major ; |dy| >= |dx|: swap so major = dy
sta major
stx minor
jmp ymajor

; |dx| > |dy|: one pixel per x step; the row changes only on y steps
xmajor:
lda major
lsr a
sta err
ldx major
inx
stx count
xloop: PLOT xsmc
dec count
beq xdone
xstep1: inc cx ; inc or dec, patched above
lda err
sec
sbc minor
bcs xnost
adc major ; carry clear
sta err
ystep1: inc cy
SETROW
jmp xloop
xnost: sta err
jmp xloop
xdone: rts

; |dy| >= |dx|: one pixel per y step
ymajor:
lda major
lsr a
sta err
ldx major
inx
stx count
yloop: PLOT ysmc
dec count
beq ydone
ystep2: inc cy
SETROW
lda err
sec
sbc minor
bcs ynost
adc major
sta err
xstep2: inc cx
jmp yloop
ynost: sta err
jmp yloop
ydone: rts

; void clear_back(void): blank rows 0-22 of the back buffer, fully unrolled
_clear_back:
lda #32
ldx _draw_page
cpx #$84
bne :+
jmp @hi
:
.repeat ROWS*40, I
sta $8000+I
.endrepeat
rts
@hi:
.repeat ROWS*40, I
sta $8400+I
.endrepeat
rts

+ 35
- 0
projects/petcube/src/frame.s Ver arquivo

@@ -0,0 +1,35 @@
; petcube: a raster IRQ at line 251 that only counts frames, so the main
; loop knows a new frame has begun before it flips screens. Runs through the
; Kernal's $0314 vector with the CIA 1 timer IRQ off (no keyboard scan, no
; jiffy clock: the program needs neither). Uses no cc65 zero page.

.export _frame_irq_init, _irq_frames

.bss
_irq_frames: .res 1

.code
_frame_irq_init:
sei
lda #<irq
sta $0314
lda #>irq
sta $0315
lda #$7F ; CIA 1: no timer IRQ
sta $DC0D
lda $DC0D ; drop any pending one
lda #251
sta $D012
lda $D011
and #$7F ; raster compare bit 8 = 0
sta $D011
lda #$01
sta $D01A ; raster IRQ on
sta $D019 ; clear a stale request
cli
rts

irq: inc _irq_frames
lda #$01
sta $D019 ; acknowledge
jmp $EA81 ; Kernal: restore Y, X, A and RTI

+ 169
- 0
projects/petcube/src/main.c Ver arquivo

@@ -0,0 +1,169 @@
/*
* petcube - a rotating cube drawn in PETSCII on the Commodore 64 (PAL).
*
* c64 build projects/petcube c64 run projects/petcube c64 test projects/petcube
*
* Techniques:
* - Quarter-block graphics: PETSCII has a character for each of the 16 ways
* to fill a cell's four quarters, so 40x25 text becomes an 80x50 grid of
* "pixels". The plotter (draw.s) reads a cell back, ORs in a pixel and
* writes the combined character, so edges can cross in one cell.
* - Precomputed animation: tools/gen_cube.py rotates and perspective-
* projects the cube for 256 frames. The cube is see-through, so each frame
* here is a screen clear and all 12 edges as Bresenham lines in assembly.
* - Double buffering: two screens in VIC bank 2 ($8000/$8400, uppercase
* character ROM seen at $9000). The hidden one is drawn, then $D018 flips
* them in the border, so no half-drawn frame is ever shown. A raster IRQ
* counts frames and a new image goes up every second frame: a steady
* 25 fps (an image takes up to ~29k cycles; two PAL frames are 39312).
* - Color RAM is not double-buffered, so it stays fixed: a vertical
* gradient the cube's edges pass through as it turns.
*/
#include <c64.h>
#include <string.h>
#include "c64test.h"
#include "cubedata.h"

extern unsigned char lx0, ly0, lx1, ly1, draw_page;
extern const unsigned char mask2code[16];
extern unsigned char code2mask[256];
void line(void);
void clear_back(void);
void draw_init(void);
void draw_edges(void);
extern const unsigned char *frame_xy;
#pragma zpsym ("frame_xy")
extern unsigned frame_vis;
extern volatile unsigned char irq_frames;
void frame_irq_init(void);

#define SCR0 ((unsigned char *)0x8000)
#define SCR1 ((unsigned char *)0x8400)
#define COLRAM ((unsigned char *)0xD800)
#define D018_SCR0 0x04 /* screen $8000, characters $9000 (ROM) */
#define D018_SCR1 0x14 /* screen $8400 */
#define CUBE_ROWS 23

static unsigned max_cycles; /* slowest frame, read by the self-test */

static const unsigned char gradient[CUBE_ROWS] = {
6, 6, 14, 14, 3, 3, 13, 1, 1, 13, 7, 7, 15, 15, 10, 10, 8, 8, 4, 4, 14, 6, 6
};

/* Draw animation frame t into the back buffer. */
static void draw_frame(unsigned char t)
{
clear_back();
frame_xy = cube_xy + (unsigned)t * 16;
frame_vis = 0x0FFF; /* see-through: all 12 edges */
draw_edges();
}

/* Show the back buffer and swap roles. The flip waits until two frames have
begun since the last one (the IRQ counts frames at line 251), then for the
border: lines 252-311 or 0-47, where the VIC reads no characters. */
static unsigned char last_flip;
static unsigned missed; /* images held for 3+ frames */

static void show_back(void)
{
unsigned char lo;

while ((unsigned char)(irq_frames - last_flip) < 2) ;
for (;;) {
lo = VIC.rasterline;
if ((VIC.ctrl1 & 0x80) || lo >= 252 || lo < 48) break;
}
VIC.addr = draw_page == 0x80 ? D018_SCR0 : D018_SCR1;
draw_page ^= 0x04;
lo = irq_frames;
if ((unsigned char)(lo - last_flip) > 2) ++missed;
last_flip = lo;
}

static void put_text(unsigned char *row, const char *s)
{
unsigned char c;
while ((c = *s++)) *row++ = (c >= 0x40 && c < 0x60) ? c - 0x40 : c; /* PETSCII -> screen code */
}

static unsigned cia_cycles(void) /* CIA 2 timer A counts down every cycle */
{
unsigned char hi = CIA2.ta_hi, lo = CIA2.ta_lo;
if (hi != CIA2.ta_hi) { hi = CIA2.ta_hi; lo = CIA2.ta_lo; }
return ((unsigned)hi << 8) | lo;
}

static void self_test(void)
{
unsigned char i, t;
unsigned before, used;

/* plotting into the back buffer at $8000 */
draw_page = 0x80;
draw_init();
clear_back();
for (i = 0; i < 16; ++i) c64test_check(code2mask[mask2code[i]] == i, 1);
c64test_check(SCR0[0] == 32 && SCR0[CUBE_ROWS * 40 - 1] == 32, 2);

lx0 = 0; ly0 = 0; lx1 = 3; ly1 = 0; line(); /* top halves of cells 0, 1 */
c64test_check(SCR0[0] == 226 && SCR0[1] == 226 && SCR0[2] == 32, 3);
lx0 = 0; ly0 = 3; lx1 = 0; ly1 = 0; line(); /* left halves of cells 0, 40 */
c64test_check(SCR0[0] == 236 && SCR0[40] == 97, 4); /* 236 = all but lower right */
lx0 = 79; ly0 = 45; lx1 = 76; ly1 = 42; line(); /* diagonal, last cells */
c64test_check(SCR0[22 * 40 + 39] == 127 && SCR0[21 * 40 + 38] == 127, 5);
clear_back();
c64test_check(SCR0[0] == 32 && SCR0[22 * 40 + 39] == 32, 6);

/* every image must fit in two PAL frames (39312 cycles) with room to spare */
CIA2.ta_lo = 0xFF; CIA2.ta_hi = 0xFF;
CIA2.cra = 0x11; /* load latch, run continuously */
t = 0;
do {
before = cia_cycles();
draw_frame(t);
used = before - cia_cycles();
if (used > max_cycles) max_cycles = used;
} while (++t);
c64test_check(max_cycles < 36000, 7);
}

int main(void)
{
unsigned char t = 0, i;
unsigned frames = 0;

VIC.bordercolor = COLOR_BLACK;
VIC.bgcolor0 = COLOR_BLACK;

/* both screens: empty cube area, caption below */
memset(SCR0, 32, 1000);
memset(SCR0 + CUBE_ROWS * 40, 64, 40); /* a horizontal rule */
put_text(SCR0 + 24 * 40, " petscii cube - 80x50 quarter blocks ");
memcpy(SCR1, SCR0, 1000);
for (i = 0; i < CUBE_ROWS; ++i) memset(COLRAM + i * 40, gradient[i], 40);
memset(COLRAM + CUBE_ROWS * 40, COLOR_GRAY1, 40);
memset(COLRAM + 24 * 40, COLOR_GRAY3, 40);

/* VIC bank 2 ($8000-$BFFF), showing screen 0 */
CIA2.pra = (CIA2.pra & 0xFC) | 0x01;
VIC.addr = D018_SCR0;

self_test(); /* draws into screen 0 while it shows: fine */

frame_irq_init();
draw_page = 0x84;
last_flip = irq_frames;
for (;;) {
draw_frame(t++);
show_back();
if (++frames == 4) missed = 0; /* settled */
if (frames == 260) { /* a full turn: about 10 s at 25 fps */
*(unsigned *)0x02F0 = max_cycles; /* for the test log */
*(unsigned *)0x02F2 = missed;
c64test_check(missed == 0, 8);
c64test_pass(); /* harmless on real hardware */
}
}
return 0;
}

+ 76
- 0
projects/petcube/tools/gen_cube.py Ver arquivo

@@ -0,0 +1,76 @@
"""Precompute a rotating cube for petcube: src/cubedata.h.

256 frames of one seamless rotation (X turns once, Y twice, Z once per loop),
perspective-projected onto the 80x46 quarter-block grid (2x2 "pixels" per
character, rows 0-22). Per frame: 8 vertices as (x, y) bytes. The cube is
see-through, so every edge is drawn and no visibility data is needed.

python projects/petcube/tools/gen_cube.py
"""
import math
import os

FRAMES = 256
W, H = 80, 46 # pixel grid used by the cube
CX, CY = 39.5, 22.5 # centre of that grid
DIST = 3.6 # camera distance (cube spans -1..1)

V = [(x, y, z) for x in (-1, 1) for y in (-1, 1) for z in (-1, 1)]
# faces as vertex loops (used only to list the 12 edges)
FACES = [
(0, 1, 3, 2), # x = -1
(4, 6, 7, 5), # x = +1
(0, 4, 5, 1), # y = -1
(2, 3, 7, 6), # y = +1
(0, 2, 6, 4), # z = -1
(1, 5, 7, 3), # z = +1
]
EDGES = sorted({tuple(sorted((f[i], f[(i + 1) % 4]))) for f in FACES for i in range(4)})
assert len(EDGES) == 12


def rotate(p, ax, ay, az):
x, y, z = p
y, z = y * math.cos(ax) - z * math.sin(ax), y * math.sin(ax) + z * math.cos(ax)
x, z = x * math.cos(ay) + z * math.sin(ay), -x * math.sin(ay) + z * math.cos(ay)
x, y = x * math.cos(az) - y * math.sin(az), x * math.sin(az) + y * math.cos(az)
return x, y, z


def project(frames_scale):
out = []
for t in range(FRAMES):
a = 2 * math.pi * t / FRAMES
pts = []
for p in V:
x, y, z = rotate(p, a, 2 * a, a)
k = frames_scale / (z + DIST)
pts.append((CX + x * k, CY + y * k))
out.append(pts)
return out


# scale so the largest extent just fits the grid
raw = project(1.0)
ext = max(max(abs(x - CX) / (W / 2 - 0.6), abs(y - CY) / (H / 2 - 0.6)) for f in raw for x, y in f)
frames = project(1.0 / ext)

xy = []
for pts in frames:
for x, y in pts:
xi, yi = int(round(x)), int(round(y))
assert 0 <= xi < W and 0 <= yi < H, (xi, yi)
xy += [xi, yi]

src = os.path.join(os.path.dirname(__file__), '..', 'src', 'cubedata.h')
with open(src, 'w', newline='\n') as fh:
fh.write('/* Generated by tools/gen_cube.py: do not edit. */\n')
fh.write('#define CUBE_FRAMES %d\n' % FRAMES)
fh.write('static const unsigned char edge_a[12] = { %s };\n' % ', '.join(str(a) for a, b in EDGES))
fh.write('static const unsigned char edge_b[12] = { %s };\n' % ', '.join(str(b) for a, b in EDGES))
fh.write('/* per frame: x0, y0, ... x7, y7 on the 80x46 grid */\n')
fh.write('static const unsigned char cube_xy[%d] = {\n' % len(xy))
for i in range(0, len(xy), 16):
fh.write(' ' + ','.join(str(v) for v in xy[i:i + 16]) + ',\n')
fh.write('};\n')
print('wrote', os.path.normpath(src))

Carregando…
Cancelar
Salvar

Powered by TurnKey Linux.