c64-ready 0.6.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,210 @@
1
+ /**
2
+ * crt-info.ts
3
+ *
4
+ * Lightweight parser for the CRT cartridge file format.
5
+ * Returns a human-readable summary line and structured metadata.
6
+ *
7
+ * Reference: https://vice-emu.sourceforge.io/vice_17.html#SEC380
8
+ *
9
+ * CRT header layout (all multi-byte fields are big-endian):
10
+ * 0x00 16 bytes Signature: "C64 CARTRIDGE "
11
+ * 0x10 4 bytes Header length (usually 0x40)
12
+ * 0x14 2 bytes CRT version
13
+ * 0x16 2 bytes Hardware type (cart type ID)
14
+ * 0x18 1 byte EXROM line status
15
+ * 0x19 1 byte GAME line status
16
+ * 0x1A 6 bytes Reserved
17
+ * 0x20 32 bytes Cart name (null-terminated ASCII)
18
+ * <headerLen>+ CHIP packets
19
+ *
20
+ * CHIP packet layout:
21
+ * 0x00 4 bytes Signature: "CHIP"
22
+ * 0x04 4 bytes Total packet length (including this header)
23
+ * 0x08 2 bytes Chip type (0=ROM, 1=RAM, 2=Flash)
24
+ * 0x0A 2 bytes Bank number
25
+ * 0x0C 2 bytes Load address
26
+ * 0x0E 2 bytes Data size (bytes)
27
+ * 0x10 N bytes Data
28
+ */
29
+
30
+ /** Subset of well-known hardware type names (matches VICE source). */
31
+ export const CRT_HW_TYPES: Record<number, string> = {
32
+ 0: 'Normal cartridge',
33
+ 1: 'Action Replay',
34
+ 2: 'KCS Power Cartridge',
35
+ 3: 'Final Cartridge III',
36
+ 4: 'Simons BASIC',
37
+ 5: 'Ocean type 1',
38
+ 6: 'Expert Cartridge',
39
+ 7: 'Fun Play, Power Play',
40
+ 8: 'Super Games',
41
+ 9: 'Atomic Power',
42
+ 10: 'Epyx Fastload',
43
+ 11: 'Westermann Learning',
44
+ 12: 'Rex Utility',
45
+ 13: 'Final Cartridge I',
46
+ 14: 'Magic Formel',
47
+ 15: 'C64 Game System (SYSTEM 3)',
48
+ 16: 'Warp Speed',
49
+ 17: 'Dinamic',
50
+ 18: 'Zaxxon / Super Zaxxon (SEGA)',
51
+ 19: 'Magic Desk / Domark / HES Australia',
52
+ 20: 'Super Snapshot V5',
53
+ 21: 'Comal-80',
54
+ 22: 'Structured BASIC',
55
+ 23: 'Ross',
56
+ 24: 'Dela EP64',
57
+ 25: 'Dela EP7x8',
58
+ 26: 'Dela EP256',
59
+ 27: 'Rex EP256',
60
+ 28: 'Mikro Assembler',
61
+ 29: 'Final Cartridge Plus',
62
+ 30: 'Action Replay 4',
63
+ 31: 'Stardos',
64
+ 32: 'EasyFlash',
65
+ 33: 'EasyFlash Xbank',
66
+ 34: 'Capture',
67
+ 35: 'Action Replay 3',
68
+ 36: 'Retro Replay',
69
+ 37: 'MMC64',
70
+ 38: 'MMC Replay',
71
+ 39: 'IDE64',
72
+ 40: 'Super Snapshot V4',
73
+ 41: 'IEEE-488',
74
+ 42: 'Game Killer',
75
+ 43: 'Prophet64',
76
+ 44: 'EXOS',
77
+ 45: 'Freeze Frame',
78
+ 46: 'Freeze Machine',
79
+ 47: 'Snapshot64',
80
+ 48: 'Super Explode V5.0',
81
+ 49: 'Magic Voice',
82
+ 50: 'Action Replay 2',
83
+ 51: 'MACH 5',
84
+ 52: 'Diashow-Maker',
85
+ 53: 'Pagefox',
86
+ 54: 'Kingsoft',
87
+ 55: 'Silverrock 128K Longshot',
88
+ 56: 'Formel 64',
89
+ 57: 'RGCD',
90
+ 58: 'RR-Net MK3',
91
+ 59: 'Easy Calc',
92
+ 60: 'GMod2',
93
+ 61: 'MAX Basic',
94
+ 62: 'GMod3',
95
+ 63: 'ZIPP-CODE 48',
96
+ 64: 'Blackbox V8',
97
+ 65: 'Blackbox V3',
98
+ 66: 'Blackbox V4',
99
+ 67: 'REX RAM-Floppy',
100
+ 68: 'BIS-Plus',
101
+ 69: 'SD-BOX',
102
+ 70: 'MultiMAX',
103
+ 71: 'Blackbox V9',
104
+ 72: 'Lt. Kernal Host Adaptor',
105
+ 73: 'RAMLink',
106
+ 74: 'H.E.R.O.',
107
+ 75: 'IEEE Flash! 64',
108
+ 76: 'Turtle Graphics II',
109
+ 77: 'Freeze Frame MK2',
110
+ 78: 'Partner 64',
111
+ };
112
+
113
+ export interface CrtInfo {
114
+ /** Full human-readable log line, ready to pass to console.log/error. */
115
+ line: string;
116
+ /** Numeric hardware type ID from the CRT header. */
117
+ hwType: number;
118
+ /** Human-readable hardware type name. */
119
+ hwName: string;
120
+ /** EXROM line value (0 or 1). */
121
+ exrom: number;
122
+ /** GAME line value (0 or 1). */
123
+ game: number;
124
+ /** Bank configuration derived from EXROM/GAME bits. */
125
+ bankConfig: string;
126
+ /** Cart name string from the header (may be empty). */
127
+ cartName: string;
128
+ /** Number of CHIP packets found. */
129
+ chipCount: number;
130
+ /** Total ROM data bytes across all CHIP packets. */
131
+ totalRomBytes: number;
132
+ }
133
+
134
+ function readU32BE(buf: Uint8Array, offset: number): number {
135
+ return (((buf[offset] << 24) | (buf[offset + 1] << 16) | (buf[offset + 2] << 8) | buf[offset + 3]) >>> 0);
136
+ }
137
+
138
+ function readU16BE(buf: Uint8Array, offset: number): number {
139
+ return (((buf[offset] << 8) | buf[offset + 1]) >>> 0);
140
+ }
141
+
142
+ /**
143
+ * Parse a CRT file and return a human-readable summary plus structured metadata.
144
+ *
145
+ * @param data Raw CRT bytes.
146
+ * @param filename Optional filename shown in the log prefix (e.g. "game.crt").
147
+ * @returns `null` if the data is too short or the magic bytes are absent.
148
+ */
149
+ export function parseCrtInfo(data: Uint8Array, filename?: string): CrtInfo | null {
150
+ // Need at least 0x40 bytes for the full CRT header
151
+ if (!data || data.length < 0x40) return null;
152
+
153
+ // Validate magic "C64 CARTRIDGE"
154
+ const magic = String.fromCharCode(...Array.from(data.slice(0, 16)));
155
+ if (!magic.startsWith('C64 CARTRIDGE')) return null;
156
+
157
+ const headerLen = readU32BE(data, 0x10); // typically 0x40
158
+ const hwType = readU16BE(data, 0x16);
159
+ const exrom = data[0x18];
160
+ const game = data[0x19];
161
+
162
+ // Cart name: null-terminated ASCII at 0x20, up to 32 bytes
163
+ let cartName = '';
164
+ for (let i = 0x20; i < 0x40 && data[i] !== 0; i++) {
165
+ cartName += String.fromCharCode(data[i]);
166
+ }
167
+ cartName = cartName.trim();
168
+
169
+ // Bank config from EXROM/GAME bits (mirrors VICE behaviour)
170
+ // exrom=0 game=0 → 16K (ROML+ROMH)
171
+ // exrom=0 game=1 → 8K (ROML only)
172
+ // exrom=1 game=0 → Ultimax
173
+ // exrom=1 game=1 → inactive (pass-through)
174
+ let bankConfig: string;
175
+ if (exrom === 0 && game === 0) bankConfig = '16K (ROML+ROMH)';
176
+ else if (exrom === 0 && game === 1) bankConfig = '8K (ROML only)';
177
+ else if (exrom === 1 && game === 0) bankConfig = 'Ultimax';
178
+ else bankConfig = 'inactive (pass-through)';
179
+
180
+ // Walk CHIP packets to count them and sum up actual ROM data bytes
181
+ let chipCount = 0;
182
+ let totalRomBytes = 0;
183
+ let offset = Math.max(headerLen, 0x40);
184
+
185
+ while (offset + 16 <= data.length) {
186
+ const chipMagic = String.fromCharCode(...Array.from(data.slice(offset, offset + 4)));
187
+ if (chipMagic !== 'CHIP') break;
188
+
189
+ const packetLen = readU32BE(data, offset + 4);
190
+ if (packetLen < 16) break; // malformed packet
191
+
192
+ const dataSize = readU16BE(data, offset + 0x0E);
193
+ chipCount++;
194
+ totalRomBytes += dataSize;
195
+ offset += packetLen;
196
+ }
197
+
198
+ const hwName = CRT_HW_TYPES[hwType] ?? `Unknown(${hwType})`;
199
+ const fileLabel = filename ? ` "${filename}"` : '';
200
+ const namePart = cartName ? ` name="${cartName}"` : '';
201
+ const kbActual = (totalRomBytes / 1024).toFixed(0);
202
+
203
+ const line =
204
+ `[C64 cart]${fileLabel} loading: hwType=${hwType}(${hwName})` +
205
+ ` | ${bankConfig} flags, ${kbActual}K actual` +
206
+ ` | ${chipCount} CHIP(s) | ${data.length} bytes${namePart}`;
207
+
208
+ return { line, hwType, hwName, exrom, game, bankConfig, cartName, chipCount, totalRomBytes };
209
+ }
210
+