1 |
#!/opt/bin/perl |
2 |
|
3 |
# this hack is not considered release ready in and way, shape, or form |
4 |
# ./vt102 bash |
5 |
# ./vt102 telnet towel.blinkenlights.nl |
6 |
# ./vt102 curl http://artscene.textfiles.com/vt100/trekvid.vt |
7 |
# ./vt102 curl http://artscene.textfiles.com/vt100/surf.vt # in 3d! |
8 |
|
9 |
use common::sense; |
10 |
|
11 |
$| = 1; |
12 |
|
13 |
my $VT102 = 1; |
14 |
my $AVO = $VT102 || 1; |
15 |
my $KBD = 1; |
16 |
|
17 |
############################################################################# |
18 |
|
19 |
my $PTY; # the pty we allocated, if any |
20 |
|
21 |
my $ROM = do { |
22 |
binmode DATA; |
23 |
local $/; |
24 |
<DATA> |
25 |
}; |
26 |
|
27 |
0x6001 == length $ROM or die "corrupted rom image"; |
28 |
|
29 |
binmode STDOUT; |
30 |
|
31 |
my @M = (0xff) x 65536; # main memory, = (0xff) x 65536; |
32 |
|
33 |
# populate mem with rom contents |
34 |
if ($VT102) { |
35 |
@M[0x0000 .. 0x1fff] = unpack "C*", substr $ROM, 0x2000, 0x2000; |
36 |
@M[0x8000 .. 0x9fff] = unpack "C*", substr $ROM, 0x4000, 0x2000; |
37 |
} else { |
38 |
@M[0x0000 .. 0x1fff] = unpack "C*", substr $ROM, 0x0000, 0x2000; |
39 |
} |
40 |
|
41 |
############################################################################# |
42 |
|
43 |
my $PRSTATUS = 0; |
44 |
|
45 |
# 8080/8085 registers |
46 |
# b, c, d, e, h, l, a |
47 |
my ($A, $B, $C, $D, $E, $H, $L, $A); |
48 |
my ($PC, $SP, $IFF, $FA, $FZ, $FS, $FP, $FC); |
49 |
|
50 |
my $RST = 0; # 8080 pending interrupts |
51 |
my $INTMASK = 7; # 8085 half interrupts |
52 |
my $INTPEND = 0; # 8085 half interrupts |
53 |
|
54 |
my $x; # dummy temp for instructions |
55 |
|
56 |
my $CLK; # rather inexact clock |
57 |
|
58 |
############################################################################# |
59 |
|
60 |
# nvram |
61 |
my @NVR = (0x3fff) x 100; # vt102: 214e accum, 214f only lower 8 bit used, first 44 bytes |
62 |
my $NVRADDR; |
63 |
my $NVRDATA; |
64 |
my $NVRLATCH; |
65 |
|
66 |
#$NVR[$_] = $_ for 0..99;#d# |
67 |
|
68 |
my @NVRCMD = ( |
69 |
sub { # accept data |
70 |
$NVRDATA = ($NVRDATA << 1) + $_[1]; |
71 |
}, |
72 |
sub { # accept addr |
73 |
$NVRADDR = ($NVRADDR << 1)+ $_[1]; |
74 |
}, |
75 |
sub { # shift out |
76 |
my $bit = $NVRDATA & 0x02000; |
77 |
$NVRDATA *= 2; |
78 |
$bit |
79 |
}, |
80 |
undef, |
81 |
sub { # write |
82 |
print "NVR WRITE $_[0]\n";#d# |
83 |
$NVR[$_[0]] = $NVRDATA & 0x3fff; |
84 |
}, |
85 |
sub { # erase |
86 |
print "NVR ERASE $_[0]\n";#d# |
87 |
$NVR[$_[0]] = 0x3fff; |
88 |
}, |
89 |
sub { # read |
90 |
# print "NVR READ $_[0] = $NVR[$_[0]]\n";#d# |
91 |
$NVRDATA = $NVR[$_[0]]; |
92 |
}, |
93 |
sub { # standby |
94 |
}, |
95 |
); |
96 |
|
97 |
my @bitidx; |
98 |
$bitidx[1 << $_] = 9 - $_ for 0..9; |
99 |
|
100 |
# the nvr1400 state machine. what a monster |
101 |
sub nvr() { |
102 |
my $a1 = $bitidx[(~$NVRADDR ) & 0x3ff]; |
103 |
my $a0 = $bitidx[(~$NVRADDR >> 10) & 0x3ff]; |
104 |
|
105 |
# printf "NVR %02x A %020b %d %d D %02x\n", $NVRLATCH, $NVRADDR & 0xfffff, $a1, $a0, $NVRDATA; |
106 |
|
107 |
$NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) |
108 |
} |
109 |
|
110 |
############################################################################# |
111 |
|
112 |
my $DC11 = 0; # 4 bit commands |
113 |
my $DC12 = 0; |
114 |
|
115 |
my $XON = 1; # false if terminal wants us to pause |
116 |
my $PUSARTCMD; |
117 |
|
118 |
my @KXMIT; # current scan queue |
119 |
my %KXMIT; # currently pressed keys |
120 |
my @KQUEUE; # key event queue |
121 |
my $KXCNT; # count for debouncew |
122 |
my @PUSARTRECV; |
123 |
my $KSTATUS; |
124 |
|
125 |
sub out_00 { # pusartdata |
126 |
# handle xon/xoff, but also pass it through |
127 |
if ($_[0] == 0x13) { |
128 |
$XON = 0; |
129 |
} elsif ($_[0] == 0x11) { |
130 |
$XON = 1; |
131 |
} |
132 |
|
133 |
syswrite $PTY, chr $_[0]; |
134 |
|
135 |
$INTPEND |= 1; # 5.5 txrdy |
136 |
} |
137 |
|
138 |
sub out_01 { |
139 |
$PUSARTCMD = shift; |
140 |
|
141 |
$INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy |
142 |
$INTPEND |= 2 if $PUSARTCMD & 0x04; # VT102, 6.5 rxrdy |
143 |
} |
144 |
|
145 |
sub out_02 { } # baudrate generator |
146 |
|
147 |
sub out_23 { } # unknown |
148 |
sub out_27 { } # unknown |
149 |
sub out_2f { } # unknown, connected to in 0f |
150 |
|
151 |
sub out_42 { } # brightness |
152 |
|
153 |
sub out_62 { |
154 |
$NVRLATCH = shift; |
155 |
} |
156 |
|
157 |
sub out_a2 { $DC11 = shift } |
158 |
sub out_c2 { } # unknown |
159 |
sub out_d2 { $DC12 = shift } |
160 |
|
161 |
sub out_82 { |
162 |
# keyboard |
163 |
|
164 |
# CLICK STARTSCAN ONLINE LOCKED | CTS DSR INSERT L1(?) |
165 |
# CLICK STARTSCAN ONLINE LOCKED | LED1 LED2 LED3 LED4 |
166 |
$KSTATUS = $_[0]; |
167 |
|
168 |
# start new scan unless scan in progress |
169 |
if (($_[0] & 0x40) && !@KXMIT) { |
170 |
# do not reply with keys in locked mode |
171 |
# or during post (0xff), |
172 |
# mostly to skip init and not fail POST, |
173 |
# and to send startup keys only when terminal is ready |
174 |
unless (($_[0] & 0x10) || ($_[0] == 0xff) || ($VT102 && $INTMASK == 0x07)) { |
175 |
if ($KXCNT <= 0 && @KQUEUE) { |
176 |
my $c = shift @KQUEUE; |
177 |
|
178 |
if ($c < 0) { # key up |
179 |
delete $KXMIT{-$c}; |
180 |
$KXCNT = 10; |
181 |
} elsif ($c > 0) { # key down |
182 |
undef $KXMIT{$c}; |
183 |
$KXCNT = 10; |
184 |
} else { # delay |
185 |
$KXCNT = 100; |
186 |
} |
187 |
} |
188 |
|
189 |
--$KXCNT; |
190 |
@KXMIT = sort keys %KXMIT; |
191 |
} |
192 |
|
193 |
$RST |= 1; |
194 |
} |
195 |
} |
196 |
|
197 |
############################################################################# |
198 |
|
199 |
my $NVRBIT; |
200 |
my $LBA; |
201 |
|
202 |
sub in_00 { # pusart data |
203 |
# print "READ PUSARTDATA (@PUSARTRECV)\n"; |
204 |
|
205 |
# $RST |= 2 if $#PUSARTRECV && $XON; |
206 |
# $INTPEND |= 2 if $#PUSARTRECV && $XON; |
207 |
|
208 |
shift @PUSARTRECV |
209 |
} |
210 |
|
211 |
sub in_01 { # pusart status |
212 |
# DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY |
213 |
0x85 + (@PUSARTRECV && 0x02) |
214 |
} |
215 |
|
216 |
sub in_22 { # modem buffer(?) |
217 |
# wild guess: -CTS -SPDI -RI -CD 0 0 0 0 |
218 |
0x20 |
219 |
} |
220 |
|
221 |
sub in_0f { } # unknown, connected to out 2f |
222 |
|
223 |
sub in_42 { # flag buffer |
224 |
++$LBA; |
225 |
# ++$LBA; |
226 |
# printf "%04x lba %04x, %04x\n", $PC, $LBA, $CLK; |
227 |
|
228 |
$NVRBIT = nvr ? 0x20 : 0x00 if ($LBA & 0x3) == 0x2; |
229 |
|
230 |
# KBD_XMITEMPTY LBA7 NVRDATA ODDFIELD - OPTION !GFX !AVO PUSART_TXRDY |
231 |
|
232 |
my $f = 0x85 | $NVRBIT; |
233 |
|
234 |
$f |= 0x02 unless $AVO; |
235 |
$f |= 0x40 if $LBA & 0x2; |
236 |
|
237 |
$f |
238 |
} |
239 |
|
240 |
sub in_82 { # tbmt keyboard uart |
241 |
return 0x7f unless @KXMIT; |
242 |
|
243 |
$RST |= 1; |
244 |
shift @KXMIT |
245 |
} |
246 |
|
247 |
sub in_03 { 0xff } # unknown, printer uart input? |
248 |
sub in_0b { 0xff } # unknown |
249 |
sub in_17 { 0xff } # unknown, printer status clear by reading? |
250 |
sub in_1b { 0xff } # unknown |
251 |
|
252 |
############################################################################# |
253 |
|
254 |
sub sf { |
255 |
$FS = $_[0] & 0x080; |
256 |
$FZ = ($_[0] & 0x0ff) == 0; |
257 |
$FC = $_[0] & 0x100; |
258 |
|
259 |
$_[0] & 0xff |
260 |
} |
261 |
|
262 |
sub sf_nc { |
263 |
$FS = $_[0] & 0x080; |
264 |
$FZ = ($_[0] & 0x0ff) == 0; |
265 |
|
266 |
$_[0] & 0xff |
267 |
} |
268 |
|
269 |
my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0 .. 255; |
270 |
my @ops; |
271 |
|
272 |
my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); |
273 |
my @cc = ('if !$FZ', 'if $FZ', 'if !$FC', 'if $FC', ';die', ';die', 'if !$FS', 'if $FS'); # die == unimplemented $FP parity |
274 |
|
275 |
# mov r,r / r,M / M,r |
276 |
for my $s (0..7) { |
277 |
for my $d (0..7) { |
278 |
$op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; |
279 |
} |
280 |
} |
281 |
|
282 |
$op[0x00] = ''; |
283 |
|
284 |
$op[0x01] = '($B, $C) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
285 |
$op[0x11] = '($D, $E) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
286 |
$op[0x21] = '($H, $L) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
287 |
$op[0x31] = '$SP = IMM16' ; # lxi #d# 0xf000 because of limited stack |
288 |
|
289 |
$op[0x02] = '$M[$B * 256 + $C] = $A'; # stax |
290 |
$op[0x12] = '$M[$D * 256 + $E] = $A'; # stax |
291 |
$op[0x32] = '$M[IMM16 ] = $A'; # sta |
292 |
|
293 |
sub inxdcx($$$) { |
294 |
$x = ($_[0] * 256 + $_[1] + $_[2]) & 0xffff; |
295 |
$_[0] = $x >> 8; |
296 |
$_[1] = $x & 0xff; |
297 |
} |
298 |
|
299 |
$op[0x03] = 'inxdcx $B, $C, 1'; # inx |
300 |
$op[0x13] = 'inxdcx $D, $E, 1'; # inx |
301 |
$op[0x23] = 'inxdcx $H, $L, 1'; # inx |
302 |
$op[0x33] = '$SP++' ; # inx |
303 |
$op[0x0b] = 'inxdcx $B, $C, -1'; # dcx |
304 |
$op[0x1b] = 'inxdcx $D, $E, -1'; # dcx |
305 |
$op[0x2b] = 'inxdcx $H, $L, -1'; # dcx |
306 |
$op[0x3b] = '--$SP' ; # dcx |
307 |
|
308 |
# "no carry" doesn't seem to be needed for vt100 - optimize? |
309 |
$op[0x04 + $_ * 8] = "$reg[$_] = sf_nc $reg[$_] + 1" for 0..7; # inr |
310 |
$op[0x05 + $_ * 8] = "$reg[$_] = sf_nc $reg[$_] - 1" for 0..7; # dcr |
311 |
|
312 |
# mvi r / M |
313 |
$op[0x06 + $_ * 8] = "$reg[$_] = IMM8" for 0..7; |
314 |
|
315 |
# getting this insn wrong (its the only 16 bit insn to modify flags) |
316 |
# wasted three of my best days with mindless vt102 rom reverse engineering |
317 |
sub dad { |
318 |
$x = $H * 256 + $L + $_[0]; |
319 |
|
320 |
($H, $L) = (($x >> 8) & 0xff, $x & 0xff); |
321 |
$FC = $x > 0xffff; |
322 |
} |
323 |
|
324 |
$op[0x09] = 'dad $B * 256 + $C'; # dad |
325 |
$op[0x19] = 'dad $D * 256 + $E'; # dad |
326 |
$op[0x29] = 'dad $H * 256 + $L'; # dad |
327 |
$op[0x39] = 'dad $SP '; # dad |
328 |
|
329 |
$op[0x07] = ' $FC = $A >> 7; $A = ($A * 2 + $FC) & 0xff '; # rlc |
330 |
$op[0x17] = ' ($FC, $A) = ($A >> 7, ($A * 2 + $FC) & 0xff)'; # ral |
331 |
|
332 |
$op[0x0f] = ' $FC = $A & 1; $A = ($A >> 1) | ($FC && 0x80) '; # rrc |
333 |
$op[0x1f] = ' ($FC, $A) = ($A & 1, ($A >> 1) | ($FC && 0x80))'; # rar |
334 |
|
335 |
$op[0x0a] = '$A = $M[$B * 256 + $C]'; # ldax b |
336 |
$op[0x1a] = '$A = $M[$D * 256 + $E]'; # ldax d |
337 |
$op[0x3a] = '$A = $M[IMM16]'; # lda |
338 |
|
339 |
$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (incomplete) |
340 |
$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (incomplete) |
341 |
|
342 |
$op[0x22] = '($M[IMM16], $M[IMM16 + 1]) = ($L, $H)'; # shld |
343 |
$op[0x2a] = '($L, $H) = ($M[IMM16], $M[IMM16 + 1])'; # lhld |
344 |
|
345 |
# yeah, the fucking setup screens actually use daa... |
346 |
$op[0x27] = ' |
347 |
my ($h, $l); |
348 |
|
349 |
($h, $l) = ($A >> 4, $A & 15); |
350 |
|
351 |
if ($l > 9 || $FA) { |
352 |
$A = sf $A + 6; |
353 |
($h, $l) = ($A >> 4, $A & 15); |
354 |
} |
355 |
|
356 |
if ($h > 9 || $FC) { |
357 |
$h += 6; |
358 |
$A = ($h * 16 + $l) & 0xff; |
359 |
} |
360 |
'; # daa, almost certainly borked, also, acarry not set by sf |
361 |
|
362 |
$op[0x2f] = '$A ^= 0xff'; # cma |
363 |
|
364 |
$op[0x37] = '$FC = 1 '; # stc |
365 |
$op[0x3f] = '$FC = !$FC'; # cmc |
366 |
|
367 |
$op[0x76] = 'die "HLT"'; # hlt |
368 |
|
369 |
$op[0x80 + $_] = '$A = sf $A + ' . $reg[$_] for 0..7; # add |
370 |
$op[0x88 + $_] = '$A = sf $A + $FC + ' . $reg[$_] for 0..7; # adc |
371 |
$op[0x90 + $_] = '$A = sf $A - ' . $reg[$_] for 0..7; # sub |
372 |
$op[0x98 + $_] = '$A = sf $A - $FC - ' . $reg[$_] for 0..7; # sbb |
373 |
$op[0xa0 + $_] = '$A = sf $A & ' . $reg[$_] for 0..7; # ana |
374 |
$op[0xa8 + $_] = '$A = sf $A ^ ' . $reg[$_] for 0..7; # xra |
375 |
$op[0xb0 + $_] = '$A = sf $A | ' . $reg[$_] for 0..7; # ora |
376 |
$op[0xb8 + $_] = ' sf $A - ' . $reg[$_] for 0..7; # cmp |
377 |
# possible todo: optimize ora a, maybe xra a |
378 |
|
379 |
$op[0xc6 + $_] = '$A = sf $A + IMM8'; # adi |
380 |
$op[0xd6 + $_] = '$A = sf $A - IMM8'; # sui |
381 |
$op[0xe6 + $_] = '$A = sf $A & IMM8'; # ani |
382 |
$op[0xee + $_] = '$A = sf $A ^ IMM8'; # xri |
383 |
$op[0xf6 + $_] = '$A = sf $A | IMM8'; # ori |
384 |
$op[0xfe + $_] = ' sf $A - IMM8'; # cpi |
385 |
|
386 |
$op[0xc1] = '($C, $B) = (POP, POP)'; # pop |
387 |
$op[0xd1] = '($E, $D) = (POP, POP)'; # pop |
388 |
$op[0xe1] = '($L, $H) = (POP, POP)'; # pop |
389 |
$op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = (!!($x & 0x80), !!($x & 0x40), !!($x & 0x10), !!($x & 0x04), !!($x & 0x01))'; # pop psw |
390 |
|
391 |
$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg |
392 |
|
393 |
$op[0xc2 + $_ * 8] = 'BRA IMM16 ' . $cc[$_] for 0..7; # jcc |
394 |
$op[0xc3] = 'JMP IMM16'; # jmp |
395 |
|
396 |
$op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) ' . $cc[$_] for 0..7; # ccc |
397 |
$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call |
398 |
|
399 |
$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst |
400 |
|
401 |
$op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 ' . $cc[$_] for 0..7; # rcc |
402 |
$op[0xc9] = 'JMP POP + POP * 256'; # ret |
403 |
|
404 |
$op[0xc5] = 'PUSH $B; PUSH $C'; |
405 |
$op[0xd5] = 'PUSH $D; PUSH $E'; |
406 |
$op[0xe5] = 'PUSH $H; PUSH $L'; |
407 |
$op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw |
408 |
|
409 |
$op[0xd3] = 'OUT'; # out |
410 |
$op[0xdb] = 'IN'; # in |
411 |
|
412 |
# e3 xthl @ 917b, hl <-> (sp) |
413 |
|
414 |
$op[0xe9] = 'JMP $H * 256 + $L'; # pchl |
415 |
|
416 |
$op[0xf3] = '$IFF = 0'; # DI |
417 |
$op[0xfb] = '$IFF = 1'; # EI |
418 |
|
419 |
@ops = @op; # for debugging #d# |
420 |
|
421 |
############################################################################# |
422 |
|
423 |
# print cpu status, for debugging |
424 |
sub status { |
425 |
my $PC = shift || $PC; |
426 |
|
427 |
printf "%04x/%04x A=%02x BC=%02x:%02x DE=%02x:%02x HL=%02x:%02x ZSCAP=%s: %02x %s\n", |
428 |
$PC, $SP, |
429 |
$A, $B, $C, $D, $E, $H, $L, |
430 |
($FZ ? "1" : "0") |
431 |
. ($FS ? "1" : "0") |
432 |
. ($FC ? "1" : "0") |
433 |
. ($FA ? "1" : "0") |
434 |
. ($FP ? "1" : "0"), |
435 |
$M[$PC], $ops[$M[$PC]]; |
436 |
} |
437 |
|
438 |
############################################################################# |
439 |
|
440 |
my @chr = ( |
441 |
" ", |
442 |
"\x{29eb}", |
443 |
"\x{2592}", |
444 |
"\x{2409}", |
445 |
"\x{240c}", |
446 |
"\x{240d}", |
447 |
"\x{240a}", |
448 |
"\x{00b0}", |
449 |
"\x{00b1}", |
450 |
"\x{2424}", |
451 |
"\x{240b}", |
452 |
"\x{2518}", |
453 |
"\x{2510}", |
454 |
"\x{250c}", |
455 |
"\x{2514}", |
456 |
"\x{253c}", |
457 |
"\x{23ba}", |
458 |
"\x{23bb}", |
459 |
"\x{2500}", |
460 |
"\x{23bc}", |
461 |
"\x{23bd}", |
462 |
"\x{251c}", |
463 |
"\x{2524}", |
464 |
"\x{2534}", |
465 |
"\x{252c}", |
466 |
"\x{2502}", |
467 |
"\x{2264}", |
468 |
"\x{2265}", |
469 |
"\x{03c0}", |
470 |
"\x{2260}", |
471 |
"\x{0142}", |
472 |
"\x{00b7}", |
473 |
(map chr, 0x020 .. 0x7e), |
474 |
"?", |
475 |
); |
476 |
|
477 |
utf8::encode $_ for @chr; |
478 |
|
479 |
sub prscr { |
480 |
my $i = 0x2000; |
481 |
|
482 |
my $scr = sprintf "\x1b[H--- KBD %08b CLK %d PC %04x RST %03b IFF %01b PUS %02x IM %03b\x1b[K\n", $KSTATUS, $CLK, $PC, $RST, $IFF, $PUSARTCMD, $INTMASK; |
483 |
|
484 |
line: |
485 |
for my $y (0 .. 25) { |
486 |
$scr .= sprintf "%2d |", ++$y; |
487 |
|
488 |
for (0..140) { |
489 |
my $c = $M[$i++]; |
490 |
|
491 |
# printf "%04x %02x\n", $i-1,$c; |
492 |
|
493 |
if ($c == 0x7f) { # also 0xff, but the firmware avoids that |
494 |
$scr .= "|\x1b[K\n"; |
495 |
|
496 |
my $a1 = $M[$i++]; |
497 |
my $a0 = $M[$i++]; |
498 |
|
499 |
$i = 0x2000 + (($a1 * 256 + $a0) & 0xfff); |
500 |
|
501 |
next line; |
502 |
} |
503 |
|
504 |
$scr .= "\x1b[7m" if $c & 0x80; |
505 |
$scr .= $chr[$c & 0x7f] // sprintf "[%02x]", $c & 0x7f; |
506 |
$scr .= "\x1b[m" if $c & 0x80; |
507 |
} |
508 |
|
509 |
$scr .= "\x1b[K\noverflow\x1b[K\n"; |
510 |
last; |
511 |
} |
512 |
|
513 |
if (0) { |
514 |
$scr .= "\x1b[K\n"; |
515 |
for my $o (0x200 .. 0x232) { |
516 |
$scr .= sprintf "%04x:", $o * 16; |
517 |
for (0..15) { |
518 |
$scr .= sprintf " %02x", $M[$o * 16 + $_]; |
519 |
} |
520 |
$scr .= "\x1b[K\n"; |
521 |
} |
522 |
} |
523 |
|
524 |
$scr .= "\x1b[J"; |
525 |
|
526 |
syswrite STDOUT, $scr; |
527 |
} |
528 |
|
529 |
############################################################################# |
530 |
|
531 |
if (@ARGV) { |
532 |
require IO::Pty; |
533 |
$PTY = IO::Pty->new; |
534 |
|
535 |
my $slave = $PTY->slave; |
536 |
|
537 |
unless (fork) { |
538 |
$ENV{TERM} = $VT102 ? "vt102" : "vt100"; |
539 |
|
540 |
close $PTY; |
541 |
|
542 |
open STDIN , "<&", $slave; |
543 |
open STDOUT, ">&", $slave; |
544 |
open STDERR, ">&", $slave; |
545 |
|
546 |
close $slave; |
547 |
|
548 |
exec @ARGV; |
549 |
} |
550 |
|
551 |
close $slave; |
552 |
} else { |
553 |
open $PTY, "</dev/null" or die;#d |
554 |
} |
555 |
|
556 |
############################################################################# |
557 |
|
558 |
# initial key input, to set up online mode etc. |
559 |
@KQUEUE = ( |
560 |
0x7b, -0x7b, # setup |
561 |
0, # delay |
562 |
0x28, -0x28, # 4, toggle local/online |
563 |
0x38, -0x38, # 5, setup b |
564 |
0, # delay |
565 |
(0x10, -0x10) x 2, # cursor right |
566 |
0x37, -0x37, # 6 toggle soft scroll |
567 |
(0x10, -0x10) x 1, # cursor right |
568 |
0x37, -0x37, # 6 toggle autorepeat off |
569 |
(0x10, -0x10) x 8, # cursor right |
570 |
0x37, -0x37, # 6 toggle keyclick |
571 |
(0x10, -0x10) x 1, # cursor right |
572 |
$VT102 ? () : (0x37, -0x37), # 6 toggle ansi/vt52 |
573 |
(0x10, -0x10) x 7, # cursor right |
574 |
0x37, -0x37, # 6 toggle wrap around |
575 |
0x7b, -0x7b, # leave setup |
576 |
); |
577 |
|
578 |
############################################################################# |
579 |
|
580 |
# 0x80 shift, 0x100 ctrl, 0x200 toggle |
581 |
my %KEYMAP = ( |
582 |
"\t" => 0x3a, |
583 |
"\r" => 0x64, |
584 |
"\n" => 0x44, |
585 |
|
586 |
# hardcoded rxvt keys |
587 |
"\e" => 0x2a, # ESC |
588 |
"\e[3~" => 0x03, # DC |
589 |
"\e[5~" => 0x7e, # CAPS LOCK (prior) |
590 |
"\e[6~" => 0x6a, # NO SCROLL (next) |
591 |
"\e[A" => 0x30, # UP |
592 |
"\e[B" => 0x22, # DOWN |
593 |
"\e[C" => 0x10, # RIGHT |
594 |
"\e[D" => 0x20, # LEFT |
595 |
"\e[a" => 0x30 | 0x080, # UP |
596 |
"\e[b" => 0x22 | 0x080, # DOWN |
597 |
"\e[c" => 0x10 | 0x080, # RIGHT |
598 |
"\e[d" => 0x20 | 0x080, # LEFT |
599 |
"\e[7~" => 0x7b, # SETUP (home) |
600 |
"\e[8~" => 0x23, # BREAK (end) |
601 |
"\e[8\$" => 0x23 | 0x080, # SHIFT BREAK / DISCONNECT (shift-end) |
602 |
"\x7f" => 0x33, # BACKSPACE |
603 |
|
604 |
"\e[11~" => 0x32, # F1 |
605 |
"\e[11~" => 0x42, # F2 |
606 |
"\e[11~" => 0x31, # F3 |
607 |
"\e[11~" => 0x41, # F4 |
608 |
); |
609 |
|
610 |
@KEYMAP{map chr, 0x20..0x40} = unpack "C*", pack "H*", |
611 |
"779ad5a9a8b8a755a6b5b6b466256575" . "351a3929283837273626d656e634e5f5" . "b9"; |
612 |
|
613 |
@KEYMAP{map chr, 0x5b .. 0x7e} = unpack "C*", pack "H*", |
614 |
"154514b7a5" . "244a6879591949485816574746766706" . "050a185a0817780969077a95c594a4"; |
615 |
|
616 |
$KEYMAP{"\x3f" & $_} ||= $KEYMAP{$_} | 0x100 for "a" .. "z"; # ctrl |
617 |
$KEYMAP{uc $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift |
618 |
|
619 |
my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; |
620 |
$KEYMATCH = qr{^($KEYMATCH)}s; |
621 |
|
622 |
sub key { |
623 |
my ($key) = @_; |
624 |
|
625 |
state %MOD; |
626 |
|
627 |
push @KQUEUE, -0x7c if !($key & 0x100) && delete $MOD{0x7c}; # ctrl-up |
628 |
push @KQUEUE, -0x7d if !($key & 0x080) && delete $MOD{0x7d}; # shift-up |
629 |
|
630 |
push @KQUEUE, 0x7c if $key & 0x100 && !$MOD{0x7c}++; # ctrl-down |
631 |
push @KQUEUE, 0x7d if $key & 0x080 && !$MOD{0x7d}++; # shift-down |
632 |
|
633 |
$key &= 0x7f; |
634 |
push @KQUEUE, $key, -$key; |
635 |
} |
636 |
|
637 |
my $STDIN_BUF; |
638 |
|
639 |
sub stdin_parse { |
640 |
key $KEYMAP{$1} |
641 |
while $STDIN_BUF =~ s/$KEYMATCH//; |
642 |
|
643 |
# skip input we can't decipher |
644 |
substr $STDIN_BUF, 0, 1, ""; |
645 |
} |
646 |
|
647 |
if ($KBD) { |
648 |
system "stty -icanon -icrnl -inlcr -echo min 1 time 0"; |
649 |
eval q{ sub END { system "stty sane" } }; |
650 |
$SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { exit 1 }; |
651 |
} |
652 |
|
653 |
############################################################################# |
654 |
|
655 |
my @ICACHE; # compiled instruction cache |
656 |
|
657 |
# the cpu |
658 |
while () { |
659 |
|
660 |
# execute extended basic blocks |
661 |
$PC = ($ICACHE[$PC] ||= do { |
662 |
my $pc = $PC; |
663 |
|
664 |
my $insn = ""; |
665 |
|
666 |
# the jit compiler |
667 |
for (0..15) { |
668 |
|
669 |
# optional tracing support |
670 |
if (0) { |
671 |
$insn .= qq< |
672 |
if (\$PRSTATUS) { |
673 |
status $pc; |
674 |
die unless --\$PRSTATUS; |
675 |
} |
676 |
>; |
677 |
} |
678 |
|
679 |
my $imm; |
680 |
my $op = $op[$M[$pc++]]; |
681 |
|
682 |
for ($op) { |
683 |
s/\bPUSH\b/\$M[--\$SP] =/g; # push byte to stack |
684 |
s/\bPOP\b/\$M[\$SP++]/g; # pop byte from stack |
685 |
|
686 |
s/\bIMM16\b/$imm \/\/= $M[$pc++] + $M[$pc++] * 256/xge; # 16 bit insn immediate |
687 |
s/\bIMM8\b /$imm \/\/= $M[$pc++] /xge; # 8 bit insn immediate |
688 |
|
689 |
s/\bPC\b/$pc/ge; # PC at end of insn |
690 |
s/\bBRA\b/return/g; # conditional jump |
691 |
s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump |
692 |
|
693 |
s/\bIN\b/ sprintf "\$A = in_%02x", $M[$pc++]/xge; |
694 |
s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; |
695 |
} |
696 |
|
697 |
$insn .= "$op;\n"; |
698 |
} |
699 |
|
700 |
|
701 |
$insn .= "$pc"; |
702 |
$insn =~ s/\x00.*$//s; |
703 |
|
704 |
eval "use integer; sub { $insn }" or die "$insn: $@" |
705 |
})->(); |
706 |
|
707 |
++$CLK; |
708 |
|
709 |
#TODO: just check on ret instructions or so |
710 |
# the interrupt logic |
711 |
$x = $INTPEND & ~$INTMASK; |
712 |
|
713 |
if (($RST || $x) && $IFF) { |
714 |
# rst 1 kbd data available |
715 |
# rst 2 pusart xmit+recv flag |
716 |
# rst 4 vertical retrace |
717 |
# 5.5 vt125 mb7 trans ready (serial send?) |
718 |
# 6.5 vt125 mb7 read ready (something modem?) |
719 |
# 7.5 vt125 mb7 vblank h(?) |
720 |
# trap vt125 mbi init h(?) |
721 |
my $vec; |
722 |
|
723 |
if ($x & 1) { $vec = 0x2c; $INTPEND &= ~1; |
724 |
} elsif ($x & 2) { $vec = 0x34; $INTPEND &= ~2; |
725 |
} elsif ($x & 4) { $vec = 0x3c; $INTPEND &= ~4; |
726 |
# } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # for some reason, this breaks vt102 |
727 |
} elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102 |
728 |
} elsif ($RST & 2) { $vec = 0x10; $RST &= ~2; |
729 |
} elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; |
730 |
} else { |
731 |
die; |
732 |
} |
733 |
|
734 |
$M[--$SP] = $PC >> 8; |
735 |
$M[--$SP] = $PC & 0xff; |
736 |
$PC = $vec; |
737 |
|
738 |
$IFF = 0; |
739 |
} |
740 |
|
741 |
# things we do from time too time only |
742 |
unless ($CLK & 0xf) { |
743 |
# do I/O |
744 |
|
745 |
unless ($CLK & 0x7ff) { |
746 |
|
747 |
# pty/serial I/O |
748 |
unless (@PUSARTRECV || @KQUEUE || !$PTY) { |
749 |
my $rin = ""; (vec $rin, fileno $PTY, 1) = 1; |
750 |
|
751 |
if (select $rin, undef, undef, 0) { |
752 |
sysread $PTY, my $buf, 256; |
753 |
push @PUSARTRECV, unpack "C*", $buf; |
754 |
} |
755 |
} |
756 |
|
757 |
# keyboard input |
758 |
if ($KBD) { |
759 |
while (select my $rin = "\x01", undef, undef, 0) { |
760 |
sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF |
761 |
or last; |
762 |
} |
763 |
|
764 |
stdin_parse if length $STDIN_BUF; |
765 |
} |
766 |
} |
767 |
|
768 |
# kick off various interrupts |
769 |
|
770 |
$RST |= 2 if @PUSARTRECV && $XON;# vt100, also works on vt102, probably by accident |
771 |
#$INTPEND |= 2 if @PUSARTRECV && $XON;# real vt102 probably does it this way |
772 |
|
773 |
unless ($CLK & 0x3ff) { |
774 |
$RST |= 4; # vertical retrace |
775 |
} |
776 |
|
777 |
# handle video hardware |
778 |
|
779 |
unless ($CLK & 0x1fff) { |
780 |
prscr; |
781 |
} |
782 |
} |
783 |
} |
784 |
|
785 |
__DATA__ |
786 |
1N ; 0 >b/BWog<Gӂ,O $
O [ xI <ӂ,Bt @`+6 +|v #~ʐ |¡ 0ڡ q~ʨ |¡ 0Ҩ ,ڥ # yOt ͤ[ zW>/2!b>> >ӂx ӂ:h zW͢uۂG|g$>%
!h w-!h >-4!j pO:{ y:! u:x!_yA[>y
>yA[>?y@
:x!yA[P>>O[>>[Î:!ʵyA>>OlyAPÇ!:!S!h ~ ~ >%: O͓: Ô!20!2!!!yAG~"&=w< w:!/!!A:!Ey2!~1N ! ~eBi<2!͢:P =2S!~6ʘ!!6 |
787 |
2!0* w4ʘ> 2! ~î!N ̓/2!! "R !"" |
788 |
! ͋! 0 Ãppppp!"-!>52,!>2[ 2v!!"I!>2s >2 B>2y >2!2!&l" >@:X!:!͔>2!b:!K>G:| !p ^!p" :!x:! u> |
789 |
Ӣ:[!Ӣ:!cww#:!>>{́kB͈͇!w!~6 :!ʮ2D!î :O:!:8>'y7:!y !!>D! NDy<wG: 'qx<o61,@ 7~G>G>w70)1!2@3#4$5%6^7&8*9(-_=+`~[{]};:/?'",<.>\| poytwq ][iure1 `-9743=08652 Ѱ |
790 |
\lkgfa ';jhds
.,nbx /m vczN> Ӣ:e !Q >>2e 6 >2[ :V!>2[ :U!=/2z [ |
791 |
!Z 'wWӢzӢz>2e |
792 |
:V!8:U!=2z !x ~O52F!!,!~=i65>Ӣ!!~jӢw>@2H!! 4:!B:X!:!b:!!{ uG:{ x©:y xک ڲ*@!! |
793 |
2>2y |
794 |
G>xbO͈ByFsO! V#zozz2 V: Gzy_ Oz@y$#$: O:B!A! :S!A6 U * q|gpx2 ! q! :S!hy2 :!@m462B!:!
! ~#n& V! ~<w+@0§~z:h _!A{2g W !n ~!j #6
#{>2r !G:r < 2r :!@{:P!!_#!s 56:r y!n ~C#8vyj !n ~[ |
795 |
f#}rQ |
796 |
lNG:g x2g 2P!{j,C_:D!A{WW{!v_ Ny1 xpy{aOx0!K~###NxyA[? {AAO:!2T!S:P!!n A#!n ~8#+A:P!w>2r !h Vw#r#w#L:!@>2G!{*j:d:0!W:1!_>= zħ;>>ܧ>b>ܧz >oz2 QW>>Wʞ _{ |
797 |
>!_ ^#V 8 A
U U U K <2 :!*{!|!r!>w!r!~w!{!F#\!~0 #
$ !x ~w! ~562 6K :! K ! V:V!s Ϳxr6:!ʘ ͎>2!4:! 2 =2 2} 2!! |
798 |
0ҽ !} O~ʻ q2~ ! |
799 |
:} :! !P |
800 |
! |
801 |
G:! x( 2)1#!{ |
802 |
!:~ O#
|
803 |
## |
804 |
~#fo!"@!A |
805 |
B |
806 |
C |
807 |
D |
808 |
F |
809 |
GH |
810 |
IJ |
811 |
K |
812 |
YZ=>1W<]`
c ER M1W[ |
813 |
H
DU 7h8q=>ZNFOF 345J67`
8
!"@!>H2~ ! "0!! |
814 |
:} ! |
815 |
:!!0!:K!_ |
816 |
~! |
817 |
G :!# |
818 |
DBCHSArfSxyd cqn
JAKlmhg
!"@!G ڲ!Q!.! "@!~B6x 2R!!"@!6 x PO2 Ϳ:R!]2 |
819 |
62!ͭPLPÉ>2!ͭL2U!Ϳx2V!y2P BH>ê! :U!G~ʸ! 56:!͎>2!5:!2 -!x!6!x!6 !!~ w:!&&Hl" " 2 &&l" !E!x#~wG7)w:~ G!J##~ȸ9#~! 44AHB01 2 !y ~ |
820 |
6! |
821 |
!! w! !͋6!xʗ 2 ! ~w>õ:!»>õ>! w>2 :0!!r!~w>Ϳ
:! 6+6/6?#>1
BG/Oxy0w#6:!x!r!2
~w>Ϳ
:!x)
0w#6~w>Ϳ
: G:!J
:U!Ox<z
ͻ
: <z
6B͎> |