zx81.js (8297B)
1 // Machine ZX81 : Z80 à 3,25 MHz, ROM 8 Ko, 16 Ko de RAM, clavier matriciel et 2 // une émulation simplifiée de la ULA pour l'affichage. 3 // 4 // Principe de l'affichage (fidèle au mécanisme réel, simplifié dans le détail) : 5 // la ROM fait « exécuter » le fichier d'écran (D_FILE) par le Z80 à l'adresse 6 // écho (A15 = 1). Quand le Z80 lit un opcode dans cette zone et que le bit 6 de 7 // l'octet est à 0, la ULA affiche le caractère et fait exécuter un NOP au CPU ; 8 // le 0x76 (HALT) qui termine chaque ligne, lui, s'exécute pour de vrai et 9 // l'interruption qui suit enchaîne la ligne suivante. Chaque ligne de texte est 10 // parcourue 8 fois (une par ligne de pixels, compteur LNCTR de la ULA). 11 // Ici, on dessine le glyphe complet dès qu'on voit le caractère, on compte les 12 // HALT pour avancer de ligne, et on présente l'image à la fin du VSYNC. 13 14 import { Z80 } from './z80.js'; 15 16 export const CPU_HZ = 3250000; 17 export const FRAME_T = 65000; // T-states par trame (50 Hz) 18 const LINE_T = 207; // T-states par ligne (période du NMI) 19 20 export const COLS = 32, ROWS = 24; 21 22 // Ordre des touches dans les tables clavier de la ROM (K-UNSHIFTED à 0x007E…). 23 // Chaque touche : [rangée du port (bit d'adresse à 0), bit de donnée]. 24 export const KEYS = { 25 SHIFT: [0, 0], Z: [0, 1], X: [0, 2], C: [0, 3], V: [0, 4], 26 A: [1, 0], S: [1, 1], D: [1, 2], F: [1, 3], G: [1, 4], 27 Q: [2, 0], W: [2, 1], E: [2, 2], R: [2, 3], T: [2, 4], 28 '1': [3, 0], '2': [3, 1], '3': [3, 2], '4': [3, 3], '5': [3, 4], 29 '0': [4, 0], '9': [4, 1], '8': [4, 2], '7': [4, 3], '6': [4, 4], 30 P: [5, 0], O: [5, 1], I: [5, 2], U: [5, 3], Y: [5, 4], 31 NEWLINE: [6, 0], L: [6, 1], K: [6, 2], J: [6, 3], H: [6, 4], 32 SPACE: [7, 0], '.': [7, 1], M: [7, 2], N: [7, 3], B: [7, 4], 33 }; 34 35 // Adresses de la ROM utilisées pour les interceptions. 36 const ROM_LOAD = 0x0347; // boucle de lecture d'octets de LOAD 37 const ROM_SAVE = 0x02FC; // boucle d'écriture de SAVE 38 const ROM_SLOW_FAST = 0x0207; // retour après LOAD/SAVE (comme z81) 39 40 export class ZX81 { 41 constructor(rom) { 42 if (!(rom instanceof Uint8Array) || rom.length !== 8192) throw new Error('ROM ZX81 : 8192 octets attendus'); 43 this.rom = rom; 44 this.ram = new Uint8Array(0x4000); 45 this.keys = new Uint8Array(8).fill(0x1f); // bits actifs à 0 46 // Image : 32×24 cellules, chaque cellule = code affiché (0..0x3f | 0x80 inverse), 0xff = vide 47 this.screen = new Uint8Array(COLS * ROWS); 48 this.work = new Uint8Array(COLS * ROWS); 49 this.pixels = new Uint8Array(COLS * 8 * ROWS * 8); // 1 = pixel noir, image présentée 50 this.frames = 0; // trames présentées depuis le reset 51 this.onFrame = null; // rappel à chaque trame présentée 52 this.tape = null; // Uint8Array d'un fichier .P à charger au prochain LOAD 53 this.onSave = null; // rappel (Uint8Array) quand le programme fait SAVE 54 this.onLoad = null; // rappel quand un .P a été chargé 55 this.cpu = new Z80({ 56 mem_read: (a) => this.read(a), 57 mem_write: (a, v) => this.write(a, v), 58 io_read: (p) => this.ioRead(p), 59 io_write: (p, v) => this.ioWrite(p, v), 60 fetch_opcode: (a) => this.fetch(a), 61 }); 62 this.reset(); 63 } 64 65 reset() { 66 this.ram.fill(0); 67 this.cpu.reset(); 68 this.nmiOn = false; 69 this.vsync = false; 70 this.nmiT = 0; 71 this.row = 0; this.col = 0; this.hline = 0; 72 this.pendingInt = false; 73 this.work.fill(0xff); 74 this.screen.fill(0xff); 75 this.pixels.fill(0); 76 this.frames = 0; 77 this.tstates = 0; 78 } 79 80 read(addr) { 81 const a = addr & 0x7fff; 82 return a < 0x4000 ? this.rom[a & 0x1fff] : this.ram[a - 0x4000]; 83 } 84 85 write(addr, v) { 86 const a = addr & 0x7fff; 87 if (a >= 0x4000) this.ram[a - 0x4000] = v; 88 } 89 90 fetch(addr) { 91 if (addr & 0x8000) { 92 const b = this.read(addr); 93 if ((b & 0x40) === 0) { 94 // Caractère affiché par la ULA ; le CPU voit un NOP. 95 if (this.row < ROWS && this.col < COLS) this.work[this.row * COLS + this.col] = b; 96 this.col++; 97 return 0x00; 98 } 99 if (b === 0x76) { 100 // Fin de ligne : on simule l'interruption que le compteur de 101 // rafraîchissement (R) produirait pendant le HALT. 102 this.col = 0; 103 this.hline++; 104 this.row = this.hline >> 3; 105 this.pendingInt = true; 106 return 0x00; 107 } 108 return b; 109 } 110 if (addr === ROM_LOAD && this.tape) { 111 this.loadP(this.tape); 112 this.tape = null; 113 this.cpu.set_pc(ROM_SLOW_FAST); 114 if (this.onLoad) this.onLoad(); 115 return this.read(ROM_SLOW_FAST); 116 } 117 if (addr === ROM_SAVE && this.onSave) { 118 this.onSave(this.dumpP()); 119 this.cpu.set_pc(ROM_SLOW_FAST); 120 return this.read(ROM_SLOW_FAST); 121 } 122 return this.read(addr); 123 } 124 125 ioRead(port) { 126 if ((port & 1) === 0) { 127 // Port 0xFE : clavier ; lance aussi le VSYNC si le générateur de NMI est coupé. 128 if (!this.nmiOn) this.vsync = true; 129 let v = 0x1f; 130 const hi = port >> 8; 131 for (let r = 0; r < 8; r++) if (((hi >> r) & 1) === 0) v &= this.keys[r]; 132 // bit 5 : inutilisé (1) ; bit 6 : 1 = machine 50 Hz ; bit 7 : entrée magnétophone (0) 133 return v | 0x60; 134 } 135 return 0xff; 136 } 137 138 ioWrite(port, _v) { 139 if (this.vsync) { 140 this.vsync = false; 141 this.endFrame(); 142 } 143 if ((port & 0xff) === 0xfd) { this.nmiOn = false; } 144 else if ((port & 0xff) === 0xfe) { this.nmiOn = true; this.nmiT = 0; } 145 } 146 147 endFrame() { 148 // Fin du VSYNC : l'image accumulée depuis le dernier VSYNC devient la trame courante. 149 this.screen.set(this.work); 150 this.work.fill(0xff); 151 this.row = 0; this.col = 0; this.hline = 0; 152 this.frames++; 153 this.render(); 154 if (this.onFrame) this.onFrame(this); 155 } 156 157 // Convertit la trame (codes) en pixels avec le jeu de caractères pointé par I. 158 render() { 159 const base = this.cpu.get_i() << 8; 160 const px = this.pixels, scr = this.screen; 161 const W = COLS * 8; 162 for (let r = 0; r < ROWS; r++) { 163 for (let c = 0; c < COLS; c++) { 164 const code = scr[r * COLS + c]; 165 const inv = code !== 0xff && (code & 0x80) ? 0xff : 0; 166 const g = code === 0xff ? -1 : base + ((code & 0x3f) << 3); 167 for (let y = 0; y < 8; y++) { 168 const bits = g < 0 ? 0 : this.read(g + y) ^ inv; 169 let o = (r * 8 + y) * W + c * 8; 170 for (let x = 7; x >= 0; x--) px[o++] = (bits >> x) & 1; 171 } 172 } 173 } 174 } 175 176 // Exécute au moins `t` T-states. 177 run(t) { 178 const cpu = this.cpu; 179 let done = 0; 180 while (done < t) { 181 const n = cpu.run_instruction(); 182 done += n; 183 if (this.pendingInt) { 184 this.pendingInt = false; 185 cpu.interrupt(false, 0xff); 186 } 187 if (this.nmiOn) { 188 this.nmiT += n; 189 if (this.nmiT >= LINE_T) { 190 this.nmiT -= LINE_T; 191 cpu.interrupt(true, 0); 192 } 193 } 194 } 195 this.tstates += done; 196 return done; 197 } 198 199 keyDown(name) { const k = KEYS[name]; if (k) this.keys[k[0]] &= ~(1 << k[1]); } 200 keyUp(name) { const k = KEYS[name]; if (k) this.keys[k[0]] |= (1 << k[1]); } 201 releaseAll() { this.keys.fill(0x1f); } 202 203 // Charge un fichier .P (image mémoire à partir de 0x4009, variables système comprises). 204 loadP(bytes) { 205 const max = 0x8000 - 0x4009; 206 const n = Math.min(bytes.length, max); 207 this.ram.set(bytes.subarray(0, n), 0x4009 - 0x4000); 208 } 209 210 // Image .P du programme en mémoire : de 0x4009 à E_LINE (exclu), comme SAVE. 211 dumpP() { 212 const eline = this.ram[0x4014 - 0x4000] | (this.ram[0x4015 - 0x4000] << 8); 213 return this.ram.slice(0x4009 - 0x4000, Math.max(0x4009, Math.min(eline, 0x8000)) - 0x4000); 214 } 215 216 // Texte de l'écran (pour les tests) : codes convertis en ASCII approximatif. 217 screenText() { 218 const lines = []; 219 for (let r = 0; r < ROWS; r++) { 220 let s = ''; 221 for (let c = 0; c < COLS; c++) s += charToAscii(this.screen[r * COLS + c]); 222 lines.push(s); 223 } 224 return lines.join('\n'); 225 } 226 } 227 228 const ASCII = ' ##########"£$:?()><=+-*/;,.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 229 export function charToAscii(code) { 230 if (code === 0xff) return ' '; 231 const ch = ASCII[code & 0x3f] ?? '?'; 232 return (code & 0x80) ? ch.toLowerCase() : ch; 233 }