ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/vt102/vt102
(Generate patch)

Comparing vt102/vt102 (file contents):
Revision 1.1 by root, Mon Dec 1 14:58:52 2014 UTC vs.
Revision 1.25 by root, Thu Dec 4 04:36:11 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines