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

Comparing vt102/vt102 (file contents):
Revision 1.4 by root, Mon Dec 1 18:31:02 2014 UTC vs.
Revision 1.24 by root, Wed Dec 3 18:27:12 2014 UTC

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