1 |
#!/opt/bin/perl |
2 |
|
3 |
# |
4 |
# Copyright(C) 2014 Marc Alexander Lehmann <vt102@schmorp.de> |
5 |
# |
6 |
# vt102 is free software; you can redistribute it and/or modify it under |
7 |
# the terms of the GNU General Public License as published by the Free |
8 |
# Software Foundation; either version 3, or (at your option) any later |
9 |
# version. |
10 |
# |
11 |
# vt102 is distributed in the hope that it will be useful, but WITHOUT |
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 |
# for more details. |
15 |
# |
16 |
|
17 |
# If this file contains embedded ROMs, the above copyright notice does |
18 |
# not apply to them. |
19 |
|
20 |
use 5.010; |
21 |
use strict; |
22 |
use integer; |
23 |
#use common::sense; |
24 |
|
25 |
my $VT102 = 1; |
26 |
my $VT131 = 0; |
27 |
my $AVO = 1; |
28 |
|
29 |
shift, ($VT102 = 0), ($AVO = 0) if $ARGV[0] =~ /^-?-vt100$/; |
30 |
shift, ($VT102 = 0) if $ARGV[0] =~ /^-?-vt100\+avo$/; |
31 |
shift if $ARGV[0] =~ /^-?-vt102$/; |
32 |
shift, ($VT131 = 1) if $ARGV[0] =~ /^-?-vt131$/; |
33 |
|
34 |
# vt100 wps = word processing roms |
35 |
# vt101 = vt102 - avo, but custom rom? really? |
36 |
# vt103 = vt100 + tu58 tape drive |
37 |
# vt125 = vt100 + gpo graphics processor |
38 |
# vt132 = vt100 + avo, stp |
39 |
# vt180 = vt100 + z80 cp/m |
40 |
|
41 |
if ($ARGV[0] =~ /^-/) { |
42 |
die <<EOF; |
43 |
|
44 |
VT102, A VT100/102/131 SIMULATOR |
45 |
|
46 |
Usage: |
47 |
|
48 |
$0 [option] [program [args]] |
49 |
|
50 |
Examples: |
51 |
|
52 |
$0 bash |
53 |
$0 telnet towel.blinkenlights.nl |
54 |
$0 curl http://artscene.textfiles.com/vt100/trekvid.vt |
55 |
$0 curl http://artscene.textfiles.com/vt100/surf.vt # in 3d! |
56 |
|
57 |
Option can be one of: |
58 |
|
59 |
--vt100 |
60 |
--vt100+avo |
61 |
--vt102 |
62 |
--vt131 |
63 |
|
64 |
Non-obvious special keys are: |
65 |
|
66 |
SET UP Home |
67 |
BACKSPACE Rubout |
68 |
CAPS LOCK Prior/PgUp |
69 |
NO SCROLL Next/PgDown |
70 |
BREAK End |
71 |
CTRL-C Insert |
72 |
|
73 |
Set-Up Guide: |
74 |
|
75 |
http://vt100.net/docs/vt102-ug/chapter3.html#S3.6 |
76 |
|
77 |
Author: |
78 |
|
79 |
Marc Lehmann <vt102\@schmorp.de> |
80 |
|
81 |
EOF |
82 |
} |
83 |
|
84 |
############################################################################# |
85 |
# ROM/hardware init |
86 |
|
87 |
my $PTY; # the pty we allocated, if any |
88 |
my $KBD = 1; |
89 |
|
90 |
my $ROMS = do { |
91 |
binmode DATA; |
92 |
local $/; |
93 |
<DATA> |
94 |
}; |
95 |
|
96 |
0x6801 == length $ROMS or die "corrupted rom image"; |
97 |
|
98 |
my @M = (0xff) x 65536; # main memory |
99 |
|
100 |
# populate mem with rom contents |
101 |
if ($VT102) { |
102 |
@M[0x0000 .. 0x1fff] = unpack "C*", substr $ROMS, 0x2000, 0x2000; |
103 |
@M[0x8000 .. 0x9fff] = unpack "C*", substr $ROMS, 0x4000, 0x2000; |
104 |
@M[0xa000 .. 0xa7ff] = unpack "C*", substr $ROMS, 0x6000, 0x0800 if $VT131; |
105 |
} else { |
106 |
@M[0x0000 .. 0x1fff] = unpack "C*", substr $ROMS, 0x0000, 0x2000; |
107 |
} |
108 |
|
109 |
############################################################################# |
110 |
# 8085 CPU registers and I/O support |
111 |
|
112 |
# 8080/8085 registers |
113 |
my ($A, $B, $C, $D, $E, $H, $L); # 8 bit general purpose |
114 |
my ($PC, $SP, $IFF); # program counter, stack pointer, interrupt flag |
115 |
my ($FA, $FZ, $FS, $FP, $FC); # condition codes (psw) |
116 |
|
117 |
my $RST = 0; # pending interrupts (external interrupt logic) |
118 |
my $INTMASK = 7; # 8085 half interrupt mask |
119 |
my $INTPEND = 0; # 8085 half interrupts pending |
120 |
|
121 |
my $CLK; # rather inexact clock, counts extended basic blocks |
122 |
|
123 |
############################################################################# |
124 |
# the dreaded NVR1400 chip. not needed to get it going, but provided anyway |
125 |
|
126 |
# nvram |
127 |
my @NVR = (0x3fff) x 100; # vt102: 214e accum, 214f only lower 8 bit used, first 44 bytes |
128 |
my $NVRADDR; |
129 |
my $NVRDATA; |
130 |
my $NVRLATCH; |
131 |
|
132 |
my @NVRCMD = ( |
133 |
sub { $NVRDATA = ($NVRDATA << 1) + $_[1]; }, # 0 accept data |
134 |
sub { $NVRADDR = ($NVRADDR << 1) + $_[1]; }, # 1 accept addr |
135 |
sub { ($NVRDATA <<= 1) & 0x4000 }, # 2 shift out |
136 |
undef, # 3 not used, will barf |
137 |
sub { $NVR[$_[0]] = $NVRDATA & 0x3fff; }, # 4 write |
138 |
sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase |
139 |
sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read |
140 |
sub { }, # 7 standby |
141 |
); |
142 |
|
143 |
my @NVR_BITIDX; $NVR_BITIDX[1 << $_] = 9 - $_ for 0..9; |
144 |
|
145 |
# the nvr1400 state machine. what a monster |
146 |
sub nvr() { |
147 |
my $a1 = $NVR_BITIDX[(~$NVRADDR ) & 0x3ff]; |
148 |
my $a0 = $NVR_BITIDX[(~$NVRADDR >> 10) & 0x3ff]; |
149 |
|
150 |
$NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) |
151 |
} |
152 |
|
153 |
############################################################################# |
154 |
# I/O ports - output |
155 |
|
156 |
my $DC11_REVERSE = 0; # light background? |
157 |
|
158 |
my $XON = 1; # false if terminal wants us to pause |
159 |
my $PUSARTCMD; |
160 |
|
161 |
my $KSTATUS; # keyboard status (click + scan flag + leds) |
162 |
my @KXMIT; # current scan queue |
163 |
my %KXMIT; # currently pressed keys |
164 |
my @KQUEUE; # key event queue |
165 |
my $KXCNT; # count for debouncew |
166 |
|
167 |
my @PUSARTRECV; # serial input (to terminal) queue |
168 |
|
169 |
sub out_00 { # pusartdata |
170 |
# handle xon/xoff, but also pass it through |
171 |
if ($_[0] == 0x13) { |
172 |
$XON = 0; |
173 |
return;#d# |
174 |
} elsif ($_[0] == 0x11) { |
175 |
$XON = 1; |
176 |
return;#d# |
177 |
} |
178 |
|
179 |
syswrite $PTY, chr $_[0]; |
180 |
|
181 |
$INTPEND |= 1; |
182 |
} |
183 |
|
184 |
sub out_01 { # pusartcmd |
185 |
$PUSARTCMD = shift; |
186 |
|
187 |
$INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy |
188 |
$INTPEND |= 2 if $PUSARTCMD & 0x04 && !@PUSARTRECV; # VT102, 6.5 rxrdy, needed for some reason |
189 |
} |
190 |
|
191 |
sub out_02 { } # baudrate generator |
192 |
|
193 |
sub out_23 { } # vt102 unknown |
194 |
sub out_27 { } # vt102 unknown |
195 |
sub out_2f { } # vt102 unknown, connected to in 0f |
196 |
|
197 |
sub out_42 { } # brightness |
198 |
|
199 |
sub out_62 { # nvr latch register (4 bits) |
200 |
$NVRLATCH = shift; |
201 |
} |
202 |
|
203 |
sub out_a2 { # device control 011 |
204 |
my $dc11 = 0x0f & shift; |
205 |
|
206 |
$DC11_REVERSE = 1 if $dc11 == 0b1010; |
207 |
$DC11_REVERSE = 0 if $dc11 == 0b1011; |
208 |
} |
209 |
|
210 |
sub out_c2 { } # unknown |
211 |
sub out_d2 { } # device control 012, 0..3 == 80c/132c/60hz/50hz |
212 |
|
213 |
sub out_82 { # keyboard txmit |
214 |
# CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4 (vt100) |
215 |
# CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 (vt102) |
216 |
$KSTATUS = $_[0]; |
217 |
|
218 |
# start new scan unless scan is in progress |
219 |
if (($_[0] & 0x40) && !@KXMIT) { |
220 |
# do not reply with keys in locked mode |
221 |
# or during post (0xff), |
222 |
# mostly to skip init and not fail POST, |
223 |
# and to send startup keys only when terminal is ready |
224 |
unless (($_[0] & 0x10) || ($_[0] == 0xff) || ($VT102 && $INTMASK == 0x07)) { |
225 |
if ($KXCNT <= 0 && @KQUEUE) { |
226 |
my $c = shift @KQUEUE; |
227 |
|
228 |
if ($c < 0) { # key up |
229 |
delete $KXMIT{-$c}; |
230 |
$KXCNT = 10; |
231 |
} elsif ($c > 0) { # key down |
232 |
undef $KXMIT{$c}; |
233 |
$KXCNT = 10; |
234 |
} else { # delay |
235 |
$KXCNT = 100; |
236 |
} |
237 |
} |
238 |
|
239 |
--$KXCNT; |
240 |
@KXMIT = sort keys %KXMIT; |
241 |
} |
242 |
|
243 |
$RST |= 1; |
244 |
} |
245 |
} |
246 |
|
247 |
############################################################################# |
248 |
# I/O ports - input |
249 |
|
250 |
my $NVRBIT; # the current nvr data bit |
251 |
my $LBA6; # twice the frequenxy of LBA7 |
252 |
|
253 |
sub in_00 { # pusart data |
254 |
# interrupt not generated here, because infinite |
255 |
# speed does not go well with the vt102. |
256 |
|
257 |
shift @PUSARTRECV |
258 |
} |
259 |
|
260 |
sub in_01 { # pusart status |
261 |
# DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY |
262 |
0x85 + (@PUSARTRECV && 0x02) |
263 |
} |
264 |
|
265 |
sub in_22 { # modem buffer |
266 |
# wild guess: -CTS -SPDI -RI -CD 0 0 0 0 |
267 |
0x20 |
268 |
} |
269 |
|
270 |
sub in_0f { 0xff } # vt102 unknown, connected to out 2f |
271 |
|
272 |
sub in_42 { # flag buffer |
273 |
++$LBA6; |
274 |
|
275 |
$NVRBIT = nvr ? 0x20 : 0x00 if ($LBA6 & 0x3) == 0x2; |
276 |
|
277 |
# KBD_XMITEMPTY LBA7 NVRDATA ODDFIELD - OPTION !GFX !AVO PUSART_TXRDY |
278 |
|
279 |
my $f = 0x85 | $NVRBIT; |
280 |
|
281 |
$f |= 0x02 unless $AVO; |
282 |
$f |= 0x40 if $LBA6 & 0x2; |
283 |
|
284 |
$f |
285 |
} |
286 |
|
287 |
sub in_82 { # tbmt keyboard uart |
288 |
return 0x7f unless @KXMIT; |
289 |
|
290 |
$RST |= 1; |
291 |
shift @KXMIT |
292 |
} |
293 |
|
294 |
sub in_03 { 0xff } # vt102 unknown, printer uart input? |
295 |
sub in_0b { 0xff } # vt102 unknown |
296 |
sub in_17 { 0xff } # vt102 unknown, printer status clear by reading? |
297 |
sub in_1b { 0xff } # vt102 unknown |
298 |
|
299 |
############################################################################# |
300 |
# 8085 cpu opcodes and flag handling |
301 |
|
302 |
my $x; # dummy scratchpad for opcodes |
303 |
|
304 |
sub sf { # set flags, full version (ZSC - AP not implemented) |
305 |
$FS = $_[0] & 0x080; |
306 |
$FZ = !($_[0] & 0x0ff); |
307 |
$FC = $_[0] & 0x100; |
308 |
|
309 |
$_[0] &= 0xff; |
310 |
} |
311 |
|
312 |
sub sf8 { # set flags, for 8-bit results (ZSC - AP not implemented) |
313 |
$FS = $_[0] & 0x080; |
314 |
$FZ = !($_[0] & 0x0ff); |
315 |
$FC = 0; |
316 |
} |
317 |
|
318 |
sub sf_nc { # set flags, except carry |
319 |
$FS = $_[0] & 0x080; |
320 |
$FZ = ($_[0] & 0x0ff) == 0; |
321 |
|
322 |
$_[0] &= 0xff; |
323 |
} |
324 |
|
325 |
# opcode table |
326 |
my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff; |
327 |
|
328 |
my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); # r/m encoding |
329 |
my @cc = ('!$FZ', '$FZ', '!$FC', '$FC', 'die;', 'die;', '!$FS', '$FS'); # cc encoding. die == unimplemented $FP parity |
330 |
|
331 |
$op[0x00] = ''; # nop |
332 |
|
333 |
# mov r,r / r,M / M,r |
334 |
for my $s (0..7) { |
335 |
for my $d (0..7) { |
336 |
$op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov |
337 |
} |
338 |
} |
339 |
|
340 |
$op[0x76] = 'die "HLT"'; # hlt (mov m,m) |
341 |
|
342 |
# mvi r / M |
343 |
$op[0x06 + $_ * 8] = "$reg[$_] = IMM8" for 0..7; |
344 |
|
345 |
$op[0x01] = '($B, $C) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
346 |
$op[0x11] = '($D, $E) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
347 |
$op[0x21] = '($H, $L) = (IMM16 >> 8, IMM16 & 0xff)'; # lxi |
348 |
$op[0x31] = '$SP = IMM16' ; # lxi |
349 |
|
350 |
$op[0x02] = '$M[$B * 256 + $C] = $A'; # stax |
351 |
$op[0x12] = '$M[$D * 256 + $E] = $A'; # stax |
352 |
$op[0x32] = '$M[IMM16 ] = $A'; # sta |
353 |
|
354 |
$op[0x0a] = '$A = $M[$B * 256 + $C]'; # ldax b |
355 |
$op[0x1a] = '$A = $M[$D * 256 + $E]'; # ldax d |
356 |
$op[0x3a] = '$A = $M[IMM16]'; # lda |
357 |
|
358 |
$op[0x22] = '($M[IMM16], $M[IMM16 + 1]) = ($L, $H)'; # shld |
359 |
$op[0x2a] = '($L, $H) = ($M[IMM16], $M[IMM16 + 1])'; # lhld |
360 |
|
361 |
sub inxdcx($$$) { |
362 |
$x = $_[0] * 256 + $_[1] + $_[2]; |
363 |
($_[0], $_[1]) = (($x >> 8) & 0xff, $x & 0xff); |
364 |
} |
365 |
|
366 |
$op[0x03] = 'inxdcx $B, $C, 1'; # inx |
367 |
$op[0x13] = 'inxdcx $D, $E, 1'; # inx |
368 |
$op[0x23] = 'inxdcx $H, $L, 1'; # inx |
369 |
$op[0x33] = '$SP++' ; # inx |
370 |
$op[0x0b] = 'inxdcx $B, $C, -1'; # dcx |
371 |
$op[0x1b] = 'inxdcx $D, $E, -1'; # dcx |
372 |
$op[0x2b] = 'inxdcx $H, $L, -1'; # dcx |
373 |
$op[0x3b] = '--$SP' ; # dcx |
374 |
|
375 |
# "no carry" doesn't seem to be needed for vt100 - optimize? |
376 |
$op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr |
377 |
$op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr |
378 |
|
379 |
$op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc |
380 |
$op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral |
381 |
$op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc |
382 |
$op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar |
383 |
|
384 |
# getting this insn wrong (its the only 16 bit insn to modify flags) |
385 |
# wasted three of my best days with mindless vt102 rom reverse engineering |
386 |
sub dad { |
387 |
$x = $H * 256 + $L + $_[0]; |
388 |
|
389 |
($H, $L) = (($x >> 8) & 0xff, $x & 0xff); |
390 |
$FC = $x > 0xffff; |
391 |
} |
392 |
|
393 |
$op[0x09] = 'dad $B * 256 + $C'; # dad |
394 |
$op[0x19] = 'dad $D * 256 + $E'; # dad |
395 |
$op[0x29] = 'dad $H * 256 + $L'; # dad |
396 |
$op[0x39] = 'dad $SP '; # dad |
397 |
|
398 |
$op[0x2f] = '$A ^= 0xff'; # cma |
399 |
|
400 |
$op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add |
401 |
$op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc |
402 |
$op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub |
403 |
$op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb |
404 |
$op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana |
405 |
$op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra |
406 |
$op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora |
407 |
$op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp |
408 |
# possible todo: optimize ora a, maybe xra a, possibly ana |
409 |
|
410 |
$op[0xc6] = 'sf $A += IMM8'; # adi |
411 |
$op[0xd6] = 'sf $A -= IMM8'; # sui |
412 |
$op[0xe6] = 'sf8 $A &= IMM8'; # ani |
413 |
$op[0xee] = 'sf8 $A ^= IMM8'; # xri |
414 |
$op[0xf6] = 'sf8 $A |= IMM8'; # ori |
415 |
$op[0xfe] = 'sf $A - IMM8'; # cpi |
416 |
# ce ACI NYI, apparently unused |
417 |
# de SBI NYI, apparently unused |
418 |
|
419 |
$op[0xc5] = 'PUSH $B; PUSH $C'; |
420 |
$op[0xd5] = 'PUSH $D; PUSH $E'; |
421 |
$op[0xe5] = 'PUSH $H; PUSH $L'; |
422 |
$op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw |
423 |
|
424 |
$op[0xc1] = '($C, $B) = (POP, POP)'; # pop |
425 |
$op[0xd1] = '($E, $D) = (POP, POP)'; # pop |
426 |
$op[0xe1] = '($L, $H) = (POP, POP)'; # pop |
427 |
$op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw |
428 |
|
429 |
$op[0xc2 + $_ * 8] = 'BRA IMM16 if ' . $cc[$_] for 0..7; # jcc |
430 |
$op[0xc3] = 'JMP IMM16'; # jmp |
431 |
|
432 |
$op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) if ' . $cc[$_] for 0..7; # ccc |
433 |
$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call |
434 |
|
435 |
$op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 if ' . $cc[$_] for 0..7; # rcc |
436 |
$op[0xc9] = 'JMP POP + POP * 256'; # ret |
437 |
|
438 |
$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst |
439 |
|
440 |
$op[0xe9] = 'JMP $H * 256 + $L'; # pchl |
441 |
# f9 SPHL NYI, apparently unused |
442 |
|
443 |
$op[0x37] = '$FC = 1 '; # stc |
444 |
$op[0x3f] = '$FC = !$FC'; # cmc |
445 |
|
446 |
$op[0xd3] = 'OUT'; # out |
447 |
$op[0xdb] = 'IN'; # in |
448 |
|
449 |
$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg |
450 |
|
451 |
# e3 xthl NYI # @ 917b in e69, hl <-> (sp) |
452 |
|
453 |
$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (8085, incomplete) |
454 |
$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (8085, incomplete) |
455 |
|
456 |
$op[0xf3] = '$IFF = 0'; # di |
457 |
$op[0xfb] = '$IFF = 1'; # ei |
458 |
|
459 |
# yeah, the fucking setup screen actually uses daa... |
460 |
$op[0x27] = ' |
461 |
my ($h, $l); |
462 |
|
463 |
($h, $l) = ($A >> 4, $A & 15); |
464 |
|
465 |
if ($l > 9 || $FA) { |
466 |
sf $A += 6; |
467 |
($h, $l) = ($A >> 4, $A & 15); |
468 |
} |
469 |
|
470 |
if ($h > 9 || $FC) { |
471 |
$h += 6; |
472 |
$A = ($h * 16 + $l) & 0xff; |
473 |
} |
474 |
'; # daa, almost certainly borked, also, acarry not set by sf |
475 |
|
476 |
############################################################################# |
477 |
# debug |
478 |
|
479 |
# print cpu status, for debugging |
480 |
sub status { |
481 |
my $PC = shift || $PC; |
482 |
|
483 |
printf "%04x/%04x A=%02x BC=%02x:%02x DE=%02x:%02x HL=%02x:%02x ZSCAP=%s: %02x %s\n", |
484 |
$PC, $SP, |
485 |
$A, $B, $C, $D, $E, $H, $L, |
486 |
($FZ ? "1" : "0") |
487 |
. ($FS ? "1" : "0") |
488 |
. ($FC ? "1" : "0") |
489 |
. ($FA ? "1" : "0") |
490 |
. ($FP ? "1" : "0"), |
491 |
$M[$PC], $op[$M[$PC]]; |
492 |
} |
493 |
|
494 |
############################################################################# |
495 |
# video emulation |
496 |
|
497 |
binmode STDOUT; |
498 |
|
499 |
my @CHARMAP = ( # acschars / chars 0..31 |
500 |
" " , "\x{29eb}", "\x{2592}", "\x{2409}", |
501 |
"\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}", |
502 |
"\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}", |
503 |
"\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}", |
504 |
"\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}", |
505 |
"\x{23bd}", "\x{251c}", "\x{2524}", "\x{2534}", |
506 |
"\x{252c}", "\x{2502}", "\x{2264}", "\x{2265}", |
507 |
"\x{03c0}", "\x{2260}", "\x{00a3}", "\x{00b7}", |
508 |
(map chr, 0x020 .. 0x7e), |
509 |
); |
510 |
|
511 |
utf8::encode $_ for @CHARMAP; |
512 |
|
513 |
my @SGR; # sgr sequences for attributes |
514 |
|
515 |
for (0x00 .. 0xff) { |
516 |
my $sgr = ""; |
517 |
|
518 |
# ~1 sgr 5 blink |
519 |
# ~2 sgr 4 underline |
520 |
# ~4 sgr 1 bold |
521 |
# 0x80 in attr, sgr 7, reversed |
522 |
|
523 |
$sgr .= ";5" unless $_ & 0x01; |
524 |
$sgr .= ";4" unless $_ & 0x02; |
525 |
$sgr .= ";1" unless $_ & 0x04; |
526 |
$sgr .= ";7" if $_ & 0x80; |
527 |
|
528 |
$SGR[$_] = "\e[${sgr}m"; |
529 |
} |
530 |
|
531 |
my @LED = $VT102 |
532 |
? qw(L1 INSERT DSR CTS LOCKED LOCAL SCAN BEEP) |
533 |
: qw(L4 L3 L2 L1 LOCKED LOCAL SCAN BEEP); |
534 |
|
535 |
my $CURSOR_IS_ON; |
536 |
|
537 |
# display screen |
538 |
sub display { |
539 |
# this is for the powersave mode - check whether the cursor is on here, |
540 |
# and only allow powersave later when it was on the last display time |
541 |
$CURSOR_IS_ON = $M[$VT102 ? 0x207b : 0x21ba]; |
542 |
|
543 |
my $leds = join " ", map $KSTATUS & 2**$_ ? "\e[7m$LED[$_]\e[m" : "$LED[$_]", reverse 0 .. $#LED; |
544 |
|
545 |
my $scr = sprintf "\e[H--- LED [ %s ] CLK %d\e[K\n", $leds, $CLK; |
546 |
|
547 |
$scr .= "\e[?5" . ($DC11_REVERSE ? "h" : "l"); |
548 |
|
549 |
my $i = 0x2000; |
550 |
|
551 |
line: |
552 |
for my $y (0 .. 25) { # ntsc, two vblank delay lines, up to 24 text lines |
553 |
my $prev_attr; |
554 |
my ($c, $attr); # declare here for speedup |
555 |
|
556 |
$scr .= sprintf "%2d \xe2\x94\x82", $y; |
557 |
|
558 |
for (0..139) { |
559 |
$c = $M[$i]; |
560 |
|
561 |
if ($c == 0x7f) { # also 0xff, but the firmware avoids that |
562 |
$scr .= "\e[m\xe2\x94\x82\e[K\n"; |
563 |
|
564 |
my $a1 = $M[$i + 1]; |
565 |
my $a0 = $M[$i + 2]; |
566 |
|
567 |
$i = 0x2000 + (($a1 * 256 + $a0) & 0xfff); |
568 |
|
569 |
next line; |
570 |
} |
571 |
|
572 |
$scr .= $SGR[$prev_attr = $attr] |
573 |
if $prev_attr != ($attr = ($M[$i++ + 0x1000] & 15) | ($c & 0x80)); |
574 |
|
575 |
$scr .= $CHARMAP[$c & 0x7f]; |
576 |
} |
577 |
|
578 |
$scr .= "\e[K\nvideo overflow\e[K\n"; |
579 |
last; |
580 |
} |
581 |
|
582 |
$scr .= "\e[m\e[J"; |
583 |
|
584 |
syswrite STDOUT, $scr; |
585 |
} |
586 |
|
587 |
############################################################################# |
588 |
# keyboard handling |
589 |
|
590 |
# 0x080 shift, 0x100 ctrl |
591 |
my %KEYMAP = ( |
592 |
"\t" => 0x3a, |
593 |
"\r" => 0x64, |
594 |
"\n" => 0x44, |
595 |
|
596 |
"\x00" => 0x77 | 0x100, # CTRL-SPACE |
597 |
"\x1c" => 0x45 | 0x100, # CTRL-\ |
598 |
"\x1d" => 0x14 | 0x100, # CTRL-] |
599 |
"\x1e" => 0x24 | 0x100, # CTRL-~ |
600 |
"\x1f" => 0x75 | 0x100, # CTRL-? |
601 |
|
602 |
# hardcoded rxvt keys |
603 |
"\e" => 0x2a, # ESC |
604 |
"\e[2~" => 0x79 | 0x100, # CTRL-C (insert) |
605 |
"\e[3~" => 0x03, # DC |
606 |
"\e[5~" => 0x7e, # CAPS LOCK (prior) |
607 |
"\e[6~" => 0x6a, # NO SCROLL (next) |
608 |
"\e[A" => 0x30, # UP |
609 |
"\e[B" => 0x22, # DOWN |
610 |
"\e[C" => 0x10, # RIGHT |
611 |
"\e[D" => 0x20, # LEFT |
612 |
"\e[a" => 0x30 | 0x080, # UP |
613 |
"\e[b" => 0x22 | 0x080, # DOWN |
614 |
"\e[c" => 0x10 | 0x080, # RIGHT |
615 |
"\e[d" => 0x20 | 0x080, # LEFT |
616 |
"\e[7~" => 0x7b, # SETUP (home) |
617 |
"\e[8~" => 0x23, # BREAK (end) |
618 |
"\e[8\$" => 0x23 | 0x080, # SHIFT BREAK / DISCONNECT (shift-end) |
619 |
"\x7f" => 0x33, # BACKSPACE |
620 |
|
621 |
"\e[11~" => 0x32, # PF1 |
622 |
"\e[12~" => 0x42, # PF2 |
623 |
"\e[13~" => 0x31, # PF3 |
624 |
"\e[14~" => 0x41, # PF4 |
625 |
); |
626 |
|
627 |
@KEYMAP{map chr, 0x20 .. 0x40, 0x5b .. 0x7e} = unpack "C*", pack "H*", |
628 |
"779ad5a9a8b8a755a6b5b6b466256575" . "351a3929283837273626d656e634e5f5" . "b9" # 20..40 |
629 |
. "154514b7a5" . "244a6879591949485816574746766706" . "050a185a0817780969077a95c594a4"; # 5b..7e |
630 |
|
631 |
$KEYMAP{"\x1f" & $_} ||= $KEYMAP{$_} | 0x100 for "a" .. "z"; # ctrl |
632 |
$KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift |
633 |
|
634 |
my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; |
635 |
$KEYMATCH = qr{^($KEYMATCH)}s; |
636 |
|
637 |
my %KMOD; # currently pressed modifier keys |
638 |
|
639 |
sub key { |
640 |
my ($key) = @_; |
641 |
|
642 |
push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up |
643 |
push @KQUEUE, -0x7d if !($key & 0x080) && delete $KMOD{0x7d}; # shift-up |
644 |
|
645 |
push @KQUEUE, 0x7c if $key & 0x100 && !$KMOD{0x7c}++; # ctrl-down |
646 |
push @KQUEUE, 0x7d if $key & 0x080 && !$KMOD{0x7d}++; # shift-down |
647 |
|
648 |
$key &= 0x7f; |
649 |
push @KQUEUE, $key, -$key; |
650 |
} |
651 |
|
652 |
my $STDIN_BUF; |
653 |
|
654 |
sub stdin_parse { |
655 |
key $KEYMAP{$1} |
656 |
while $STDIN_BUF =~ s/$KEYMATCH//; |
657 |
|
658 |
# skip input we can't decipher |
659 |
substr $STDIN_BUF, 0, 1, ""; |
660 |
} |
661 |
|
662 |
if ($KBD) { |
663 |
system "stty -icanon -icrnl -inlcr -echo min 1 time 0"; # -isig |
664 |
eval q{ sub END { system "stty sane" } }; |
665 |
$SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { exit 1 }; |
666 |
} |
667 |
|
668 |
############################################################################# |
669 |
# initial key input, to set up online mode etc. |
670 |
# could be done via nvram defaults |
671 |
|
672 |
@KQUEUE = ( |
673 |
0x7b, -0x7b, # setup |
674 |
0, # delay |
675 |
0x28, -0x28, # 4, toggle local/online |
676 |
0x38, -0x38, # 5, setup b |
677 |
0, # delay |
678 |
(0x10, -0x10) x 2, # cursor right |
679 |
0x37, -0x37, # 6 toggle soft scroll |
680 |
(0x10, -0x10) x 1, # cursor right |
681 |
0x37, -0x37, # 6 toggle autorepeat off |
682 |
(0x10, -0x10) x 8, # cursor right |
683 |
0x37, -0x37, # 6 toggle keyclick |
684 |
(0x10, -0x10) x 1, # cursor right |
685 |
$VT102 ? () : (0x37, -0x37), # 6 toggle ansi/vt52 |
686 |
(0x10, -0x10) x 7, # cursor right |
687 |
0x37, -0x37, # 6 toggle wrap around |
688 |
0x7b, -0x7b, # leave setup |
689 |
); |
690 |
|
691 |
############################################################################# |
692 |
# process/pty management |
693 |
|
694 |
if (1) { |
695 |
require IO::Pty; |
696 |
$PTY = IO::Pty->new; |
697 |
|
698 |
my $slave = $PTY->slave; |
699 |
|
700 |
$PTY->set_winsize (24, 80); |
701 |
|
702 |
unless (fork) { |
703 |
$ENV{LC_ALL} = "C"; |
704 |
$ENV{TERM} = $VT102 ? "vt102" : "vt100"; |
705 |
|
706 |
close $PTY; |
707 |
|
708 |
open STDIN , "<&", $slave; |
709 |
open STDOUT, ">&", $slave; |
710 |
open STDERR, ">&", $slave; |
711 |
|
712 |
system "stty ixoff erase ^H"; |
713 |
|
714 |
$PTY->make_slave_controlling_terminal; |
715 |
$PTY->close_slave; |
716 |
|
717 |
@ARGV = "sh" unless @ARGV; |
718 |
exec @ARGV; |
719 |
} |
720 |
|
721 |
$PTY->close_slave; |
722 |
} else { |
723 |
open $PTY, "+</dev/null" |
724 |
or die "/dev/null: $!"; |
725 |
} |
726 |
|
727 |
############################################################################# |
728 |
# the actual hardware simulator |
729 |
|
730 |
my @ICACHE; # compiled instruction/basic block cache |
731 |
|
732 |
my $POWERSAVE; # powersave counter |
733 |
|
734 |
my $RIN; # libev for the less well-off |
735 |
|
736 |
(vec $RIN, 0, 1) = 1 if $KBD; |
737 |
(vec $RIN, fileno $PTY, 1) = 1 if $PTY; |
738 |
|
739 |
# the cpu. |
740 |
while () { |
741 |
# execute an extended basic block |
742 |
$PC = ($ICACHE[$PC] ||= do { |
743 |
my $pc = $PC; |
744 |
|
745 |
my $insn = ""; |
746 |
|
747 |
# the jit compiler |
748 |
for (0..31) { |
749 |
my $imm; |
750 |
my $op = $op[$M[$pc++]]; |
751 |
|
752 |
for ($op) { |
753 |
s/\bPUSH\b/\$M[--\$SP] =/g; # push byte to stack |
754 |
s/\bPOP\b/\$M[\$SP++]/g; # pop byte from stack |
755 |
|
756 |
s/\bIMM16\b/$imm \/\/= $M[$pc++] + $M[$pc++] * 256/xge; # 16 bit insn immediate |
757 |
s/\bIMM8\b /$imm \/\/= $M[$pc++] /xge; # 8 bit insn immediate |
758 |
|
759 |
s/\bPC\b/$pc/ge; # PC at end of insn |
760 |
s/\bBRA\b/return/g; # conditional jump |
761 |
s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump |
762 |
|
763 |
s/\bIN\b/ sprintf "\$A = in_%02x", $M[$pc++]/xge; # in insns call in_HEX |
764 |
s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; # out likewise |
765 |
} |
766 |
|
767 |
$insn .= "$op;\n"; |
768 |
} |
769 |
|
770 |
$insn .= $pc; |
771 |
$insn =~ s/\x00.*$//s; |
772 |
|
773 |
eval "sub { $insn }" or die "$insn: $@" |
774 |
})->(); |
775 |
|
776 |
++$CLK; |
777 |
|
778 |
# things we do from time to time only |
779 |
unless ($CLK & 0xf) { |
780 |
# do I/O |
781 |
|
782 |
unless ($CLK & 0xfff) { |
783 |
if (select $x = $RIN, undef, undef, $POWERSAVE < 10 ? 0 : $CURSOR_IS_ON && 3600) { |
784 |
|
785 |
# pty/serial I/O |
786 |
if ($PTY && (vec $x, fileno $PTY, 1) && (@PUSARTRECV < 128) && !@KQUEUE) { |
787 |
sysread $PTY, my $buf, 256; |
788 |
|
789 |
# linux don't do cs7 and/or parity anymore, so we need to filter |
790 |
# out xoff characters to avoid freezes. |
791 |
push @PUSARTRECV, grep { ($_ & 0x7f) != 0x13 } unpack "C*", $buf; |
792 |
} |
793 |
|
794 |
# keyboard input |
795 |
if ($KBD && (vec $x, 0, 1)) { |
796 |
# to avoid non-blocking mode on stdin (and stty min 0), we |
797 |
# just read byte-by-byte after a select says there is data. |
798 |
while (select my $rin = "\x01", undef, undef, 0) { |
799 |
sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF |
800 |
or last; |
801 |
} |
802 |
|
803 |
stdin_parse if length $STDIN_BUF; |
804 |
} |
805 |
|
806 |
$POWERSAVE = 0; # activity |
807 |
} elsif (@PUSARTRECV || @KQUEUE) { |
808 |
$POWERSAVE = 0; |
809 |
} else { |
810 |
++$POWERSAVE; |
811 |
} |
812 |
} |
813 |
|
814 |
# kick off serial input interrupt quite often |
815 |
$RST |= 2 if @PUSARTRECV && $XON; # VT100, but works on vt102, too (probably not used on real hardware though) |
816 |
#$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy |
817 |
|
818 |
# kick off vertical retrace interrupt from time to time |
819 |
unless ($CLK & 0x1ff) { |
820 |
$RST |= 4; # vertical retrace |
821 |
} |
822 |
|
823 |
# handle video hardware |
824 |
unless ($CLK & 0x3fff) { |
825 |
display; |
826 |
} |
827 |
} |
828 |
|
829 |
# the interrupt logic - we only interrupt after basic blocks |
830 |
# which, as a side effect, ensures that we don't interrupt |
831 |
# "ei; ret" sequences and thus reduce the risk of stack overflows. |
832 |
if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) { |
833 |
# rst 1 kbd data available |
834 |
# rst 2 pusart xmit+recv flag |
835 |
# rst 4 vertical retrace |
836 |
# 5.5 vt125 mb7 trans ready (serial send?) |
837 |
# 6.5 vt125 mb7 read ready (something modem?) |
838 |
# 7.5 vt125 mb7 vblank h(?) |
839 |
# trap vt125 mbi init h(?) |
840 |
my $vec; |
841 |
|
842 |
my $pend = $INTPEND & ~$INTMASK; |
843 |
|
844 |
if ($pend & 1) { $vec = 0x2c; $INTPEND &= ~1; |
845 |
} elsif ($pend & 2) { $vec = 0x34; $INTPEND &= ~2; |
846 |
} elsif ($pend & 4) { $vec = 0x3c; $INTPEND &= ~4; |
847 |
# } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # the vt102 firmware doesn't like combined interrupts |
848 |
} elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102 |
849 |
} elsif ($RST & 2) { $vec = 0x10; $RST &= ~2; |
850 |
} elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; |
851 |
} else { |
852 |
die; |
853 |
} |
854 |
|
855 |
# jump to the interrupt vector |
856 |
$M[--$SP] = $PC >> 8; |
857 |
$M[--$SP] = $PC & 0xff; |
858 |
$PC = $vec; |
859 |
|
860 |
$IFF = 0; |
861 |
} |
862 |
} |
863 |
|
864 |
############################################################################# |
865 |
# roms in the data section + one newline |
866 |
# |
867 |
# vt100 @ 0x0000+0x0800 23-032E2 |
868 |
# vt100 @ 0x0800+0x0800 23-061E2 |
869 |
# vt100 @ 0x1000+0x0800 23-033E2 |
870 |
# vt100 @ 0x1800+0x0800 23-034E2 |
871 |
# |
872 |
# vt102 @ 0x0000+0x2000 23-226E4 |
873 |
# vt102 @ 0x8000+0x2000 23-225E4 |
874 |
# |
875 |
# vt131 @ 0xa000+0x0800 23-280E2 |
876 |
# |
877 |
|
878 |
__DATA__ |
879 |
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 |
880 |
2!0* w4ʘ> 2! ~î!N ̓/2!! "R !"" |
881 |
! ͋! 0 Ãppppp!"-!>52,!>2[ 2v!!"I!>2s >2 B>2y >2!2!&l" >@:X!:!͔>2!b:!K>G:| !p ^!p" :!x:! u> |
882 |
Ӣ:[!Ӣ:!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 Ѱ |
883 |
\lkgfa ';jhds
.,nbx /m vczN> Ӣ:e !Q >>2e 6 >2[ :V!>2[ :U!=/2z [ |
884 |
!Z 'wWӢzӢz>2e |
885 |
:V!8:U!=2z !x ~O52F!!,!~=i65>Ӣ!!~jӢw>@2H!! 4:!B:X!:!b:!!{ uG:{ x©:y xک ڲ*@!! |
886 |
2>2y |
887 |
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 ~[ |
888 |
f#}rQ |
889 |
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ʞ _{ |
890 |
>!_ ^#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!! |
891 |
0ҽ !} O~ʻ q2~ ! |
892 |
:} :! !P |
893 |
! |
894 |
G:! x( 2)1#!{ |
895 |
!:~ O#
|
896 |
## |
897 |
~#fo!"@!A |
898 |
B |
899 |
C |
900 |
D |
901 |
F |
902 |
GH |
903 |
IJ |
904 |
K |
905 |
YZ=>1W<]`
c ER M1W[ |
906 |
H
DU 7h8q=>ZNFOF 345J67`
8
!"@!>H2~ ! "0!! |
907 |
:} ! |
908 |
:!!0!:K!_ |
909 |
~! |
910 |
G :!# |
911 |
DBCHSArfSxyd cqn
JAKlmhg
!"@!G ڲ!Q!.! "@!~B6x 2R!!"@!6 x PO2 Ϳ:R!]2 |
912 |
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 ~ |
913 |
6! |
914 |
!! 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͎> |