-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathremote.html
More file actions
520 lines (482 loc) · 19.3 KB
/
Copy pathremote.html
File metadata and controls
520 lines (482 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MTMC-16 Remote</title>
<!-- Reuse all existing emulator styling -->
<link rel="stylesheet" href="css/emulator.css">
<style>
/* Layout overrides for remote mode */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
#app {
display: flex;
flex-direction: column;
height: 100vh;
}
/* Content: side-by-side columns */
#content {
flex: 1 1 0;
display: flex;
min-height: 0;
overflow: hidden;
background: #c3c3c3;
gap: 4px;
padding: 4px;
}
#col-left {
flex: 0 0 470px;
display: flex;
flex-direction: column;
min-height: 0;
overflow: hidden;
gap: 4px;
}
#col-right {
flex: 1 1 0;
display: flex;
flex-direction: column;
min-height: 0;
overflow: hidden;
gap: 4px;
}
/* 1-col: stack everything vertically, allow scroll */
#content.one-col {
flex-direction: column;
overflow-y: auto;
overflow-x: hidden;
}
#content.one-col #col-left {
flex: 0 0 auto;
overflow: visible;
}
#content.one-col #col-right {
flex: 0 0 auto;
overflow: visible;
}
#content.one-col #memory-panel {
max-height: 80vh;
min-height: 400px;
}
/* Registers: natural height */
#register-panel { flex: 0 0 auto; }
/* Memory: fills remaining space in left column */
#memory-panel {
flex: 1 1 0;
min-height: 100px;
display: flex;
flex-direction: column;
overflow: hidden;
}
#memory-panel > div {
display: flex;
flex-direction: column;
flex: 1 1 0;
min-height: 0;
overflow: hidden;
}
.memory {
flex: 1 1 0;
min-height: 0;
overflow-y: auto;
overflow-x: hidden;
}
/* Display: natural height */
#display { flex: 0 0 auto; }
#display-wrapper { margin: 0; }
/* Console: fill remaining space in right column */
#console-panel {
flex: 1 1 0;
min-height: 100px;
display: flex;
flex-direction: column;
overflow: hidden;
}
#console-panel > div {
display: flex;
flex-direction: column;
flex: 1 1 0;
min-height: 0;
overflow: hidden;
}
#console {
flex: 1 1 0;
min-height: 0;
overflow-y: auto;
}
/* Hide elements not needed in remote mode */
#control-buttons { display: none; }
#control-secondary { display: none; }
/* Larger LEDs for embedded view */
.blinken { height: 10px; width: 10px; padding: 5px; margin-right: 1px; }
.blinken.space { margin-right: 8px; }
</style>
</head>
<body>
<div id="app">
<!-- Header (reuses emulator.css #controls styling) -->
<div class="panel" id="controls" style="flex:0 0 auto; border-radius:0; margin:0;">
<div>
<div id="control-header">
<span>m</span><span>s</span><span>u</span> <i>MTMC-16</i>
<span style="float:right;font-size:11px">
<select id="speed-select" style="font-family:monospace;background:#000;color:#ffcc00;border:1px solid #555;font-size:11px">
<option value="1">1 Hz</option>
<option value="10">10 Hz</option>
<option value="100">100 Hz</option>
<option value="1000" selected>1 KHz</option>
<option value="10000">10 KHz</option>
<option value="100000">100 KHz</option>
</select>
<select id="memory-size-select" style="font-family:monospace;background:#000;color:#ffcc00;border:1px solid #555;font-size:11px">
<option value="1024">1K</option>
<option value="2048">2K</option>
<option value="4096">4K</option>
<option value="8192">8K</option>
<option value="16384">16K</option>
</select>
</span>
</div>
</div>
</div>
<!-- Content -->
<div id="content" class="one-col">
<!-- Left: Registers + Memory -->
<div id="col-left">
<div class="panel" id="register-panel">
<div>
<table id="register-table" style="table-layout: fixed;">
<colgroup>
<col style="width:18px"><col style="width:220px">
<col style="width:40px"><col style="width:35px"><col style="width:25px">
</colgroup>
<tr><th></th><th></th>
<th style="text-align:center;font-size:10px">HEX</th>
<th style="text-align:center;font-size:10px">DEC</th>
<th style="text-align:center;font-size:10px">STR</th></tr>
<tr><td>AX</td><td id="ax-leds"></td><td class="reg-value" id="ax-value">0x0000</td><td class="reg-value" id="ax-dec">0</td><td class="reg-value" id="ax-str"></td></tr>
<tr><td>BX</td><td id="bx-leds"></td><td class="reg-value" id="bx-value">0x0000</td><td class="reg-value" id="bx-dec">0</td><td class="reg-value" id="bx-str"></td></tr>
<tr><td>CX</td><td id="cx-leds"></td><td class="reg-value" id="cx-value">0x0000</td><td class="reg-value" id="cx-dec">0</td><td class="reg-value" id="cx-str"></td></tr>
<tr><td>DX</td><td id="dx-leds"></td><td class="reg-value" id="dx-value">0x0000</td><td class="reg-value" id="dx-dec">0</td><td class="reg-value" id="dx-str"></td></tr>
<tr><td>SI</td><td id="si-leds"></td><td class="reg-value" id="si-value">0x0000</td><td class="reg-value" id="si-dec">0</td><td class="reg-value" id="si-str"></td></tr>
<tr><td>DI</td><td id="di-leds"></td><td class="reg-value" id="di-value">0x0000</td><td class="reg-value" id="di-dec">0</td><td class="reg-value" id="di-str"></td></tr>
<tr><td>SP</td><td id="sp-leds"></td><td class="reg-value" id="sp-value">0x0000</td><td class="reg-value" id="sp-dec">0</td><td class="reg-value" id="sp-str"></td></tr>
<tr><td>BP</td><td id="bp-leds"></td><td class="reg-value" id="bp-value">0x0000</td><td class="reg-value" id="bp-dec">0</td><td class="reg-value" id="bp-str"></td></tr>
<tr><td colspan="5"><hr/></td></tr>
<tr><td>HP</td><td id="hp-leds"></td><td class="reg-value" id="hp-value">0x0000</td><td class="reg-value" id="hp-dec">0</td><td class="reg-value" id="hp-str"></td></tr>
<tr><td>IP</td><td id="ip-leds"></td><td class="reg-value" id="ip-value">0x0020</td><td class="reg-value" id="ip-dec">32</td><td class="reg-value" id="ip-str"></td></tr>
<tr><td>CB</td><td id="cb-leds"></td><td class="reg-value" id="cb-value">0x0000</td><td class="reg-value" id="cb-dec">0</td><td class="reg-value" id="cb-str"></td></tr>
<tr><td>IR</td><td id="ir-leds"></td><td class="reg-value" id="ir-value" colspan="3" style="text-align:center">0x0000</td></tr>
<tr><td>DR</td><td id="dr-leds"></td><td class="reg-value" id="dr-value" colspan="3" style="text-align:center">0x0000</td></tr>
<tr><td colspan="5"><hr/></td></tr>
<tr><td colspan="5" style="text-align:center">
FLAGS:
<span id="flag-zf" class="blinken off"></span> Z
<span id="flag-sf" class="blinken off"></span> S
<span id="flag-cf" class="blinken off"></span> C
<span id="flag-of" class="blinken off"></span> O
</td></tr>
</table>
</div>
</div>
<div class="panel" id="memory-panel">
<div>
<div id="memory-header">Memory
<button id="memory-mode-btn" style="float:right;font-family:monospace;font-size:10px;padding:2px 6px">DYN</button>
</div>
<div class="memory" id="memory-view"></div>
</div>
</div>
</div>
<!-- Right: Display + Gamepad + Console -->
<div id="col-right">
<div class="panel" id="display">
<div id="display-wrapper">
<canvas id="display-canvas"></canvas>
</div>
<div id="gameboy-controls">
<div id="dpad">
<button class="dpad-btn dpad-up" id="btn-up"></button>
<button class="dpad-btn dpad-left" id="btn-left"></button>
<button class="dpad-btn dpad-right" id="btn-right"></button>
<button class="dpad-btn dpad-down" id="btn-down"></button>
</div>
<div id="start-select">
<button class="small-btn" id="btn-select">select</button>
<button class="small-btn" id="btn-start">start</button>
</div>
<div id="ab-buttons">
<button class="action-btn" id="btn-b">B</button>
<button class="action-btn" id="btn-a">A</button>
</div>
</div>
</div>
<div class="panel" id="console-panel">
<div>
<div id="console-header">Terminal</div>
<div id="console">
<div id="console-history"></div>
<div id="console-input-wrapper">
<span style="color:var(--console-primary)">></span>
<input type="text" id="console-input" autocomplete="off" spellcheck="false" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="module">
import { CPU, Memory, Opcode } from './js/emulator.js'
import { Display } from './js/display.js'
const display = new Display('display-canvas')
const memory = new Memory(1024, display)
const cpu = new CPU(memory, null)
// --- Register rendering (same as ui.js) ---
function createLEDs(value) {
let html = ''
for (let bit = 15; bit >= 0; bit--) {
const on = ((value >> bit) & 1) === 1
html += `<span class="blinken ${on ? 'on' : 'off'}"></span>`
if (bit > 0 && bit % 4 === 0) html += `<span class="blinken space"></span>`
}
return html
}
function updateRegister(name, value) {
const lc = name.toLowerCase()
const el = document.getElementById(lc + '-leds')
if (el) el.innerHTML = createLEDs(value)
const hex = document.getElementById(lc + '-value')
if (hex) hex.textContent = '0x' + (value & 0xFFFF).toString(16).toUpperCase().padStart(4, '0')
const dec = document.getElementById(lc + '-dec')
if (dec) dec.textContent = (value << 16 >> 16).toString()
const str = document.getElementById(lc + '-str')
if (str) {
const ch = value & 0xFF
str.textContent = (ch >= 32 && ch < 127) ? '\u00B7' + String.fromCharCode(ch) : ''
}
}
function updateRegisters() {
for (const r of ['AX','BX','CX','DX','SI','DI','SP','BP','HP','IP','CB'])
updateRegister(r, cpu.registers[r])
const ip = cpu.registers.IP
if (ip >= 0 && ip + 1 < memory.data.length) {
updateRegister('IR', (memory.data[ip] << 8) | memory.data[ip + 1])
if (ip + 3 < memory.data.length)
updateRegister('DR', (memory.data[ip + 2] << 8) | memory.data[ip + 3])
}
for (const [id, val] of [['flag-zf', cpu.registers.ZF], ['flag-sf', cpu.registers.SF],
['flag-cf', cpu.registers.CF], ['flag-of', cpu.registers.OF]]) {
const el = document.getElementById(id)
if (el) el.className = 'blinken ' + (val ? 'on' : 'off')
}
}
// --- Memory rendering ---
let memMode = 'dyn'
const OPCODE_NAMES = {
0x00:'NOP',0x01:'HLT',0x10:'MOV',0x11:'MOVI',0x12:'LOAD',0x13:'STORE',
0x14:'LOADR',0x15:'STORER',0x16:'LOADB',0x17:'LEA',0x18:'STOREB',
0x19:'LOADBR',0x1A:'STOREBR',0x1B:'STOREI',0x1C:'STOREI',
0x20:'ADD',0x21:'ADDI',0x22:'SUB',0x23:'SUBI',0x24:'INC',0x25:'DEC',
0x26:'MUL',0x27:'DIV',0x28:'ADDM',0x29:'ADDMR',0x2A:'SUBM',0x2B:'SUBMR',
0x30:'AND',0x31:'ANDI',0x32:'OR',0x33:'ORI',0x34:'XOR',0x35:'XORI',
0x36:'NOT',0x37:'SHL',0x38:'SHR',0x39:'TEST',0x3A:'TESTI',0x3B:'NEG',
0x40:'CMP',0x41:'CMPI',0x42:'CMPM',0x43:'CMPMR',
0x44:'SETE',0x45:'SETNE',0x46:'SETL',0x47:'SETG',0x48:'SETLE',0x49:'SETGE',
0x50:'JMP',0x51:'JE',0x52:'JNE',0x53:'JL',0x54:'JG',0x55:'JLE',0x56:'JGE',0x57:'LOOP',
0x60:'PUSH',0x61:'POP',0x70:'CALL',0x71:'RET',0x90:'SYSCALL',0x91:'DEBUG'
}
const TWO_BYTE = new Set([0x00,0x01,0x24,0x25,0x26,0x27,0x36,0x3B,0x44,0x45,0x46,0x47,0x48,0x49,0x60,0x61,0x71,0x90,0x91])
const REG_NAMES = ['AX','BX','CX','DX','SI','DI','SP','BP','HP']
// Build instruction boundary map for the code segment
let instrMap = null // addr -> {start, size, text}
function buildInstrMap() {
instrMap = {}
const cs = cpu.registers.CB || 0
let addr = 0x20
while (addr < cs && addr < memory.data.length) {
const op = memory.data[addr] & 0xFF
const size = TWO_BYTE.has(op) ? 2 : 4
const name = OPCODE_NAMES[op] || `0x${op.toString(16).toUpperCase()}`
let text = name
if (size === 4 && addr + 3 < memory.data.length) {
const b1 = memory.data[addr+1] & 0xFF
const b2 = memory.data[addr+2] & 0xFF
const b3 = memory.data[addr+3] & 0xFF
// Basic operand decoding
const reg = b1 < REG_NAMES.length ? REG_NAMES[b1] : b1.toString()
if (op >= 0x50 && op <= 0x57 || op === 0x70) {
// Jump/Call: [op][0x00][addr_hi][addr_lo] (big-endian)
text = `${name} 0x${((b2<<8)|b3).toString(16).toUpperCase()}`
} else if (op === 0x11 || op === 0x21 || op === 0x23 || op === 0x41) {
// REG_IMM: [op][reg][imm_hi][imm_lo] (big-endian)
const imm = (b2 << 8) | b3
text = `${name} ${reg},${imm > 32767 ? imm - 65536 : imm}`
} else if (op === 0x10 || op === 0x20 || op === 0x22 || op === 0x40) {
// REG_REG
const src = b2 < REG_NAMES.length ? REG_NAMES[b2] : b2.toString()
text = `${name} ${reg},${src}`
}
} else if (size === 2 && op === 0x90) {
const num = memory.data[addr+1] & 0xFF
const syscalls = ['EXIT','PR_CH','PR_ST','PR_IN','RD_CH','RD_IN','RD_ST','ATOI','SBRK']
text = `SYSCALL ${syscalls[num] || num}`
}
for (let i = 0; i < size && addr + i < memory.data.length; i++) {
instrMap[addr + i] = { start: addr, size, text, offset: i }
}
addr += size
}
}
function classify(addr) {
const ip = cpu.registers.IP, sp = cpu.registers.SP
const cs = cpu.registers.CB || 0, hp = cpu.registers.HP
if (addr === ip) return 'current'
if (addr >= sp) return 'stack'
if (addr >= 0x20 && addr < cs) return 'code'
if (addr >= cs && addr < hp) return 'data'
if (addr >= hp && addr < sp) return 'heap'
return ''
}
function formatChar(b) {
if (b === 0) return 'NUL'
if (b === 10) return '\\n'
if (b >= 32 && b < 127) return String.fromCharCode(b)
return '\u00B7'
}
function formatCell(addr) {
const b = memory.data[addr] & 0xFF
const region = classify(addr)
if (memMode === 'hex') return b.toString(16).toUpperCase().padStart(2, '0')
if (memMode === 'dec') return b.toString()
if (memMode === 'str') return formatChar(b)
// DYN mode
if (region === 'data' || region === 'heap') return formatChar(b)
if (region === 'stack') return b.toString()
if ((region === 'code' || region === 'current') && instrMap) {
const info = instrMap[addr]
if (info) {
if (info.offset === 0) return info.text
return '\u00B7' // continuation byte
}
}
return b.toString(16).toUpperCase().padStart(2, '0')
}
function updateMemoryView() {
const view = document.getElementById('memory-view')
if (!view || !memory.data) return
buildInstrMap()
let html = ''
const rows = Math.ceil(memory.data.length / 16)
for (let row = 0; row < rows; row++) {
const base = row * 16
let line = ''
for (let col = 0; col < 16; col++) {
const a = base + col
if (a >= memory.data.length) break
const region = classify(a)
const cls = region ? ` class="mem-${region}"` : ''
const text = formatCell(a)
// Span multiple cells for instruction names
const info = instrMap && instrMap[a]
if (info && info.offset === 0 && memMode === 'dyn') {
const span = Math.min(info.size, 16 - col)
line += `<span${cls} style="display:inline-block;width:${span*28}px;text-align:center;font-size:11px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="0x${a.toString(16).toUpperCase().padStart(4,'0')}">${text}</span>`
col += span - 1
} else if (info && info.offset > 0 && memMode === 'dyn') {
// Skip continuation bytes (already covered by the spanning cell)
continue
} else {
line += `<span${cls} style="display:inline-block;width:28px;text-align:center;font-size:11px;overflow:hidden;text-overflow:ellipsis" title="0x${a.toString(16).toUpperCase().padStart(4,'0')}">${text}</span>`
}
}
html += `<div style="white-space:nowrap">${line}</div>`
}
view.innerHTML = html
}
document.getElementById('memory-mode-btn').addEventListener('click', () => {
const modes = ['dyn','hex','dec','str']
memMode = modes[(modes.indexOf(memMode) + 1) % modes.length]
document.getElementById('memory-mode-btn').textContent = memMode.toUpperCase()
updateMemoryView()
})
// --- Console ---
function consolePrint(text) {
const el = document.getElementById('console-history')
if (!el) return
const div = document.createElement('div')
div.textContent = text
el.appendChild(div)
el.parentElement.scrollTop = el.parentElement.scrollHeight
}
function updateUI() { updateRegisters(); updateMemoryView() }
// --- SSE ---
const eventSource = new EventSource('/api/events')
eventSource.onmessage = (event) => {
try {
const state = JSON.parse(event.data)
if (state.registers) {
const r = state.registers
cpu.registers.AX = r.AX; cpu.registers.BX = r.BX
cpu.registers.CX = r.CX; cpu.registers.DX = r.DX
cpu.registers.SI = r.SI; cpu.registers.DI = r.DI
cpu.registers.SP = r.SP; cpu.registers.BP = r.BP
cpu.registers.HP = r.HP; cpu.registers.IP = r.IP
if (r.CS !== undefined) cpu.registers.CB = r.CS
cpu.registers.setFlags(r.ZF, r.SF, r.CF, r.OF)
}
if (state.memory) {
const bytes = Uint8Array.from(atob(state.memory), c => c.charCodeAt(0))
if (bytes.length !== memory.data.length) {
memory.resize(bytes.length)
const sel = document.getElementById('memory-size-select')
if (sel) sel.value = String(bytes.length)
}
memory.data.set(bytes)
if (state.registers) memory.cs = state.registers.CS || 0
}
if (state.vram) {
display.getVRAM().set(Uint8Array.from(atob(state.vram), c => c.charCodeAt(0)))
display.refresh()
}
if (state.console) consolePrint(state.console)
updateUI()
} catch (err) { console.error('Parse error:', err) }
}
eventSource.onerror = () => consolePrint('[Connection lost]')
// --- Console input ---
document.getElementById('console-input').addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
const text = e.target.value; e.target.value = ''
consolePrint('> ' + text)
fetch('/api/command', { method: 'POST', headers: {'Content-Type':'application/json'},
body: JSON.stringify({action:'input', text}) }).catch(() => {})
}
})
// --- Gamepad ---
for (const [id, bit] of Object.entries({
'btn-right':0x01,'btn-left':0x02,'btn-up':0x04,'btn-down':0x08,
'btn-a':0x10,'btn-b':0x20,'btn-select':0x40,'btn-start':0x80})) {
const el = document.getElementById(id)
if (!el) continue
el.addEventListener('mousedown', () =>
fetch('/api/command',{method:'POST',body:JSON.stringify({action:'button',type:'press',bit})}).catch(()=>{}))
el.addEventListener('mouseup', () =>
fetch('/api/command',{method:'POST',body:JSON.stringify({action:'button',type:'release',bit})}).catch(()=>{}))
}
// --- Speed control ---
document.getElementById('speed-select').addEventListener('change', (e) => {
const value = parseInt(e.target.value)
fetch('/api/command', { method: 'POST', headers: {'Content-Type':'application/json'},
body: JSON.stringify({action:'speed', value}) }).catch(() => {})
})
// --- Memory size control ---
document.getElementById('memory-size-select').addEventListener('change', (e) => {
const value = parseInt(e.target.value)
fetch('/api/command', { method: 'POST', headers: {'Content-Type':'application/json'},
body: JSON.stringify({action:'memory-size', value}) }).catch(() => {})
})
updateUI()
</script>
</body>
</html>