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

Comparing vt102/vt102 (file contents):
Revision 1.13 by root, Wed Dec 3 02:13:26 2014 UTC vs.
Revision 1.32 by root, Wed Dec 17 03:06:21 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
20use 5.010;
20use strict; 21use strict;
22use integer;
21#use common::sense; 23#use common::sense;
22 24
23my $VT102 = 1; 25my $VT102 = 1;
24my $VT131 = 0; 26my $VT131 = 0;
25my $AVO = 1; 27my $AVO = 1;
26my $KBD = 1;
27 28
28shift, ($VT102 = 0), ($AVO = 0) if $ARGV[0] =~ /^-?-vt100$/; 29shift, ($VT102 = 0), ($AVO = 0) if $ARGV[0] =~ /^-?-vt100$/;
29shift, ($VT102 = 0) if $ARGV[0] =~ /^-?-vt100\+avo$/; 30shift, ($VT102 = 0) if $ARGV[0] =~ /^-?-vt100\+avo$/;
30shift if $ARGV[0] =~ /^-?-vt102$/; 31shift if $ARGV[0] =~ /^-?-vt102$/;
31shift, ($VT131 = 1) if $ARGV[0] =~ /^-?-vt131$/; 32shift, ($VT131 = 1) if $ARGV[0] =~ /^-?-vt131$/;
32 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
33if ($ARGV[0] =~ /^-/) { 41if ($ARGV[0] =~ /^-/) {
34 die <<EOF; 42 die <<EOF;
35 43
36VT102, A VT100/101/102/131 SIMULATOR 44VT102, A VT100/102/131 SIMULATOR
37 45
38Usage: 46Usage:
39 47
40 $0 [option] [program [args]] 48 $0 [option] [program [args]]
41 49
58 SET UP Home 66 SET UP Home
59 BACKSPACE Rubout 67 BACKSPACE Rubout
60 CAPS LOCK Prior/PgUp 68 CAPS LOCK Prior/PgUp
61 NO SCROLL Next/PgDown 69 NO SCROLL Next/PgDown
62 BREAK End 70 BREAK End
71 CTRL-C Insert
63 72
64Set-Up Guide: 73Set-Up Guide:
65 74
66 http://vt100.net/docs/vt102-ug/chapter3.html#S3.6 75 http://vt100.net/docs/vt102-ug/chapter3.html#S3.6
67 76
72EOF 81EOF
73} 82}
74 83
75############################################################################# 84#############################################################################
76# ROM/hardware init 85# ROM/hardware init
86
87my $PTY; # the pty we allocated, if any
88my $KBD = 1;
77 89
78my $ROMS = do { 90my $ROMS = do {
79 binmode DATA; 91 binmode DATA;
80 local $/; 92 local $/;
81 <DATA> 93 <DATA>
82}; 94};
83 95
840x6801 == length $ROMS or die "corrupted rom image"; 960x6801 == length $ROMS or die "corrupted rom image";
85
86binmode STDOUT;
87 97
88my @M = (0xff) x 65536; # main memory, = (0xff) x 65536; 98my @M = (0xff) x 65536; # main memory, = (0xff) x 65536;
89 99
90# populate mem with rom contents 100# populate mem with rom contents
91if ($VT102) { 101if ($VT102) {
97} 107}
98 108
99############################################################################# 109#############################################################################
100# 8085 CPU registers and I/O support 110# 8085 CPU registers and I/O support
101 111
102my $PTY; # the pty we allocated, if any
103
104# 8080/8085 registers 112# 8080/8085 registers
105# b, c, d, e, h, l, a 113my ($A, $B, $C, $D, $E, $H, $L); # 8 bit general purpose
106my ($A, $B, $C, $D, $E, $H, $L, $A); 114my ($PC, $SP, $IFF); # program counter, stack pointer, interrupt flag
107my ($PC, $SP, $IFF, $FA, $FZ, $FS, $FP, $FC); 115my ($FA, $FZ, $FS, $FP, $FC); # condition codes (psw)
108 116
109my $RST = 0; # 8080 pending interrupts 117my $RST = 0; # pending interrupts (external interrupt logic)
110my $INTMASK = 7; # 8085 half interrupts 118my $INTMASK = 7; # 8085 half interrupt mask
111my $INTPEND = 0; # 8085 half interrupts 119my $INTPEND = 0; # 8085 half interrupts pending
112 120
113my $x; # dummy temp for instructions 121my $CLK; # rather inexact clock, counts extended basic blocks
114
115my $CLK; # rather inexact clock
116 122
117############################################################################# 123#############################################################################
118# the dreaded NVR1400 chip. not needed to get it going, but provided anyway 124# the dreaded NVR1400 chip. not needed to get it going, but provided anyway
119 125
120# nvram 126# nvram
132 sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase 138 sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase
133 sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read 139 sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read
134 sub { }, # 7 standby 140 sub { }, # 7 standby
135); 141);
136 142
137my @bitidx; 143my @NVR_BITIDX; $NVR_BITIDX[1 << $_] = 9 - $_ for 0..9;
138$bitidx[1 << $_] = 9 - $_ for 0..9;
139 144
140# the nvr1400 state machine. what a monster 145# the nvr1400 state machine. what a monster
141sub nvr() { 146sub nvr() {
142 my $a1 = $bitidx[(~$NVRADDR ) & 0x3ff]; 147 my $a1 = $NVR_BITIDX[(~$NVRADDR ) & 0x3ff];
143 my $a0 = $bitidx[(~$NVRADDR >> 10) & 0x3ff]; 148 my $a0 = $NVR_BITIDX[(~$NVRADDR >> 10) & 0x3ff];
144
145# printf "NVR %02x A %020b %d %d D %02x\n", $NVRLATCH, $NVRADDR & 0xfffff, $a1, $a0, $NVRDATA;
146 149
147 $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) 150 $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1)
148} 151}
149 152
150############################################################################# 153#############################################################################
151# I/O ports - output 154# I/O ports - output
152 155
153my $DC11_REVERSE = 0; 156my $DC11_REVERSE = 0; # light background?
154 157
155my $XON = 1; # false if terminal wants us to pause 158my $XON = 1; # false if terminal wants us to pause
156my $PUSARTCMD; 159my $PUSARTCMD;
157 160
161my $KSTATUS; # keyboard status (click + scan flag + leds)
158my @KXMIT; # current scan queue 162my @KXMIT; # current scan queue
159my %KXMIT; # currently pressed keys 163my %KXMIT; # currently pressed keys
160my @KQUEUE; # key event queue 164my @KQUEUE; # key event queue
161my $KXCNT; # count for debouncew 165my $KXCNT; # count for debouncew
162my @PUSARTRECV; 166
163my $KSTATUS; 167my @PUSARTRECV; # serial input (to terminal) queue
164 168
165sub out_00 { # pusartdata 169sub out_00 { # pusartdata
166 # handle xon/xoff, but also pass it through 170 # handle xon/xoff, but also pass it through
167 if ($_[0] == 0x13) { 171 if ($_[0] == 0x13) {
168 $XON = 0; 172 $XON = 0;
175 syswrite $PTY, chr $_[0]; 179 syswrite $PTY, chr $_[0];
176 180
177 $INTPEND |= 1; 181 $INTPEND |= 1;
178} 182}
179 183
180sub out_01 { 184sub out_01 { # pusartcmd
181 $PUSARTCMD = shift; 185 $PUSARTCMD = shift;
182 186
183 $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy 187 $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy
184 $INTPEND |= 2 if $PUSARTCMD & 0x04 && !@PUSARTRECV; # VT102, 6.5 rxrdy, needed for some reason 188 $INTPEND |= 2 if $PUSARTCMD & 0x04 && !@PUSARTRECV; # VT102, 6.5 rxrdy, needed for some reason
185} 189}
186 190
187sub out_02 { } # baudrate generator 191sub out_02 { } # baudrate generator
188 192
189sub out_23 { } # unknown 193sub out_23 { } # vt102 unknown
190sub out_27 { } # unknown 194sub out_27 { } # vt102 unknown
191sub out_2f { } # unknown, connected to in 0f 195sub out_2f { } # vt102 unknown, connected to in 0f
192 196
193sub out_42 { } # brightness 197sub out_42 { } # brightness
194 198
195sub out_62 { 199sub out_62 { # nvr latch register (4 bits)
196 $NVRLATCH = shift; 200 $NVRLATCH = shift;
197} 201}
198 202
199sub out_a2 { 203sub out_a2 { # device control 011
200 my $dc11 = 0x0f & shift; 204 my $dc11 = 0x0f & shift;
201 205
202 $DC11_REVERSE = 1 if $dc11 == 0b1010; 206 $DC11_REVERSE = 1 if $dc11 == 0b1010;
203 $DC11_REVERSE = 0 if $dc11 == 0b1011; 207 $DC11_REVERSE = 0 if $dc11 == 0b1011;
204} 208}
205 209
206sub out_c2 { } # unknown 210sub out_c2 { } # unknown
207sub out_d2 { } # 0..3 == 80c/132c/60hz/50hz 211sub out_d2 { } # device control 012, 0..3 == 80c/132c/60hz/50hz
208 212
209sub out_82 { 213sub out_82 { # keyboard txmit
210 # keyboard 214 # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4 (vt100)
211
212 # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 215 # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 (vt102)
213 # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4
214 $KSTATUS = $_[0]; 216 $KSTATUS = $_[0];
215 217
216 # start new scan unless scan in progress 218 # start new scan unless scan is in progress
217 if (($_[0] & 0x40) && !@KXMIT) { 219 if (($_[0] & 0x40) && !@KXMIT) {
218 # do not reply with keys in locked mode 220 # do not reply with keys in locked mode
219 # or during post (0xff), 221 # or during post (0xff),
220 # mostly to skip init and not fail POST, 222 # mostly to skip init and not fail POST,
221 # and to send startup keys only when terminal is ready 223 # and to send startup keys only when terminal is ready
243} 245}
244 246
245############################################################################# 247#############################################################################
246# I/O ports - input 248# I/O ports - input
247 249
248my $NVRBIT; 250my $NVRBIT; # the current nvr data bit
249my $LBA6; # twice the frequenxy of LBA7 251my $LBA6; # twice the frequenxy of LBA7
250 252
251sub in_00 { # pusart data 253sub in_00 { # pusart data
252 # interrupt not generated here, because infinite 254 # interrupt not generated here, because infinite
253 # speed does not go well with the vt102. 255 # speed does not go well with the vt102.
254 256
258sub in_01 { # pusart status 260sub in_01 { # pusart status
259 # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY 261 # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY
260 0x85 + (@PUSARTRECV && 0x02) 262 0x85 + (@PUSARTRECV && 0x02)
261} 263}
262 264
263sub in_22 { # modem buffer(?) 265sub in_22 { # modem buffer
264 # wild guess: -CTS -SPDI -RI -CD 0 0 0 0 266 # wild guess: -CTS -SPDI -RI -CD 0 0 0 0
265 0x20 267 0x20
266} 268}
267 269
268sub in_0f { 0xff } # vt102 unknown, connected to out 2f 270sub in_0f { 0xff } # vt102 unknown, connected to out 2f
295sub in_1b { 0xff } # vt102 unknown 297sub in_1b { 0xff } # vt102 unknown
296 298
297############################################################################# 299#############################################################################
298# 8085 cpu opcodes and flag handling 300# 8085 cpu opcodes and flag handling
299 301
302my $x; # dummy scratchpad for opcodes
303
300sub sf { # set flags (ZSC - AP not implemented) 304sub sf { # set flags, full version (ZSC - AP not implemented)
301 $FS = $_[0] & 0x080; 305 $FS = $_[0] & 0x080;
302 $FZ = !($_[0] & 0x0ff); 306 $FZ = !($_[0] & 0x0ff);
303 $FC = $_[0] & 0x100; 307 $FC = $_[0] & 0x100;
304 308
305 $_[0] &= 0xff; 309 $_[0] &= 0xff;
306} 310}
307 311
308sub sf8 { # set flags (ZSC - AP not implemented) 312sub sf8 { # set flags, for 8-bit results (ZSC - AP not implemented)
309 $FS = $_[0] & 0x080; 313 $FS = $_[0] & 0x080;
310 $FZ = !($_[0] & 0x0ff); 314 $FZ = !($_[0] & 0x0ff);
311 $FC = 0; 315 $FC = 0;
312} 316}
313 317
314sub sf_nc { # set flags except carry 318sub sf_nc { # set flags, except carry
315 $FS = $_[0] & 0x080; 319 $FS = $_[0] & 0x080;
316 $FZ = ($_[0] & 0x0ff) == 0; 320 $FZ = ($_[0] & 0x0ff) == 0;
317 321
318 $_[0] &= 0xff; 322 $_[0] &= 0xff;
319} 323}
320 324
325# opcode table
321my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff; 326my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff;
322 327
323my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); 328my @reg = qw($B $C $D $E $H $L $M[$H*256+$L] $A); # r/m encoding
324my @cc = ('if !$FZ', 'if $FZ', 'if !$FC', 'if $FC', ';die', ';die', 'if !$FS', 'if $FS'); # die == unimplemented $FP parity 329my @cc = ('!$FZ', '$FZ', '!$FC', '$FC', 'die;', 'die;', '!$FS', '$FS'); # cc encoding. die == unimplemented $FP parity
325 330
326$op[0x00] = ''; 331$op[0x00] = ''; # nop
327 332
328# mov r,r / r,M / M,r 333# mov r,r / r,M / M,r
329for my $s (0..7) { 334for my $s (0..7) {
330 for my $d (0..7) { 335 for my $d (0..7) {
331 $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov 336 $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov
371$op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr 376$op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr
372$op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr 377$op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr
373 378
374$op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc 379$op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc
375$op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral 380$op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral
376
377$op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc 381$op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc
378$op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar 382$op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar
379
380$op[0x2f] = '$A ^= 0xff'; # cma
381 383
382# getting this insn wrong (its the only 16 bit insn to modify flags) 384# getting this insn wrong (its the only 16 bit insn to modify flags)
383# wasted three of my best days with mindless vt102 rom reverse engineering 385# wasted three of my best days with mindless vt102 rom reverse engineering
384sub dad { 386sub dad {
385 $x = $H * 256 + $L + $_[0]; 387 $x = $H * 256 + $L + $_[0];
390 392
391$op[0x09] = 'dad $B * 256 + $C'; # dad 393$op[0x09] = 'dad $B * 256 + $C'; # dad
392$op[0x19] = 'dad $D * 256 + $E'; # dad 394$op[0x19] = 'dad $D * 256 + $E'; # dad
393$op[0x29] = 'dad $H * 256 + $L'; # dad 395$op[0x29] = 'dad $H * 256 + $L'; # dad
394$op[0x39] = 'dad $SP '; # dad 396$op[0x39] = 'dad $SP '; # dad
397
398$op[0x2f] = '$A ^= 0xff'; # cma
395 399
396$op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add 400$op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add
397$op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc 401$op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc
398$op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub 402$op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub
399$op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb 403$op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb
400$op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana 404$op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana
401$op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra 405$op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra
402$op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora 406$op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora
403$op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp 407$op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp
404# possible todo: optimize ora a, maybe xra a 408# possible todo: optimize ora a, maybe xra a, possibly ana
405 409
406$op[0xc6] = 'sf $A += IMM8'; # adi 410$op[0xc6] = 'sf $A += IMM8'; # adi
407# ce ADI NYI
408$op[0xd6] = 'sf $A -= IMM8'; # sui 411$op[0xd6] = 'sf $A -= IMM8'; # sui
409# de SBI NYI
410$op[0xe6] = 'sf8 $A &= IMM8'; # ani 412$op[0xe6] = 'sf8 $A &= IMM8'; # ani
411$op[0xee] = 'sf8 $A ^= IMM8'; # xri 413$op[0xee] = 'sf8 $A ^= IMM8'; # xri
412$op[0xf6] = 'sf8 $A |= IMM8'; # ori 414$op[0xf6] = 'sf8 $A |= IMM8'; # ori
413$op[0xfe] = 'sf $A - IMM8'; # cpi 415$op[0xfe] = 'sf $A - IMM8'; # cpi
416# ce ACI NYI, apparently unused
417# de SBI NYI, apparently unused
414 418
415$op[0xc5] = 'PUSH $B; PUSH $C'; 419$op[0xc5] = 'PUSH $B; PUSH $C';
416$op[0xd5] = 'PUSH $D; PUSH $E'; 420$op[0xd5] = 'PUSH $D; PUSH $E';
417$op[0xe5] = 'PUSH $H; PUSH $L'; 421$op[0xe5] = 'PUSH $H; PUSH $L';
418$op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw 422$op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw
420$op[0xc1] = '($C, $B) = (POP, POP)'; # pop 424$op[0xc1] = '($C, $B) = (POP, POP)'; # pop
421$op[0xd1] = '($E, $D) = (POP, POP)'; # pop 425$op[0xd1] = '($E, $D) = (POP, POP)'; # pop
422$op[0xe1] = '($L, $H) = (POP, POP)'; # pop 426$op[0xe1] = '($L, $H) = (POP, POP)'; # pop
423$op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw 427$op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw
424 428
425$op[0xc2 + $_ * 8] = 'BRA IMM16 ' . $cc[$_] for 0..7; # jcc 429$op[0xc2 + $_ * 8] = 'BRA IMM16 if ' . $cc[$_] for 0..7; # jcc
426$op[0xc3] = 'JMP IMM16'; # jmp 430$op[0xc3] = 'JMP IMM16'; # jmp
427 431
428$op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) ' . $cc[$_] for 0..7; # ccc 432$op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) if ' . $cc[$_] for 0..7; # ccc
429$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call 433$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call
430 434
431$op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 ' . $cc[$_] for 0..7; # rcc 435$op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 if ' . $cc[$_] for 0..7; # rcc
432$op[0xc9] = 'JMP POP + POP * 256'; # ret 436$op[0xc9] = 'JMP POP + POP * 256'; # ret
433 437
434$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst 438$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst
435 439
436$op[0xe9] = 'JMP $H * 256 + $L'; # pchl 440$op[0xe9] = 'JMP $H * 256 + $L'; # pchl
437# f9 SPHL NYI 441# f9 SPHL NYI, apparently unused
438 442
439$op[0x37] = '$FC = 1 '; # stc 443$op[0x37] = '$FC = 1 '; # stc
440$op[0x3f] = '$FC = !$FC'; # cmc 444$op[0x3f] = '$FC = !$FC'; # cmc
441 445
442$op[0xd3] = 'OUT'; # out 446$op[0xd3] = 'OUT'; # out
443$op[0xdb] = 'IN'; # in 447$op[0xdb] = 'IN'; # in
444 448
445$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg 449$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg
446 450
447# e3 xthl NYI # @ 917b, hl <-> (sp) 451# e3 xthl NYI # @ 917b in e69, hl <-> (sp)
448 452
449$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (incomplete) 453$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (8085, incomplete)
450$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (incomplete) 454$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (8085, incomplete)
451 455
452$op[0xf3] = '$IFF = 0'; # DI 456$op[0xf3] = '$IFF = 0'; # di
453$op[0xfb] = '$IFF = 1'; # EI 457$op[0xfb] = '$IFF = 1'; # ei
454 458
455# yeah, the fucking setup screens actually use daa... 459# yeah, the fucking setup screen actually uses daa...
456$op[0x27] = ' 460$op[0x27] = '
457 my ($h, $l); 461 my ($h, $l);
458 462
459 ($h, $l) = ($A >> 4, $A & 15); 463 ($h, $l) = ($A >> 4, $A & 15);
460 464
468 $A = ($h * 16 + $l) & 0xff; 472 $A = ($h * 16 + $l) & 0xff;
469 } 473 }
470'; # daa, almost certainly borked, also, acarry not set by sf 474'; # daa, almost certainly borked, also, acarry not set by sf
471 475
472############################################################################# 476#############################################################################
473# print cpu status for debugging purposes 477# debug
474 478
475# print cpu status, for debugging 479# print cpu status, for debugging
476sub status { 480sub status {
477 my $PC = shift || $PC; 481 my $PC = shift || $PC;
478 482
488} 492}
489 493
490############################################################################# 494#############################################################################
491# video emulation 495# video emulation
492 496
493my @CHARMAP = ( 497binmode STDOUT;
498
499my @CHARMAP = ( # acschars / chars 0..31
494 " " , "\x{29eb}", "\x{2592}", "\x{2409}", 500 " " , "\x{29eb}", "\x{2592}", "\x{2409}",
495 "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}", 501 "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}",
496 "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}", 502 "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}",
497 "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}", 503 "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}",
498 "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}", 504 "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}",
524 530
525my @LED = $VT102 531my @LED = $VT102
526 ? qw(L1 INSERT DSR CTS LOCKED LOCAL SCAN BEEP) 532 ? qw(L1 INSERT DSR CTS LOCKED LOCAL SCAN BEEP)
527 : qw(L4 L3 L2 L1 LOCKED LOCAL SCAN BEEP); 533 : qw(L4 L3 L2 L1 LOCKED LOCAL SCAN BEEP);
528 534
535my $CURSOR_IS_ON;
536
529# display screen 537# display screen
530sub display { 538sub 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
531 my $i = 0x2000; 543 my $i = 0x2000;
532 544
533 my $leds = join " ", map $KSTATUS & 2**$_ ? "\e[7m$LED[$_]\e[m" : "$LED[$_]", reverse 0 .. $#LED; 545 my $leds = join " ", map $KSTATUS & 2**$_ ? "\e[7m$LED[$_]\e[m" : "$LED[$_]", reverse 0 .. $#LED;
534 546
535 my $scr = sprintf "\e[H--- LED [ %s ] CLK %d\e[K\n", $leds, $CLK; 547 my $scr = sprintf "\e[H--- LED [ %s ] CLK %d\e[K\n", $leds, $CLK;
536 548
537 $scr .= "\e[?5" . ($DC11_REVERSE ? "h" : "l"); 549 $scr .= "\e[?5" . ($DC11_REVERSE ? "h" : "l");
538 550
539 line: 551 line:
540 for my $y (0 .. 25) { # ntsc, two vblank delay lines, up to 24 text lines 552 for my $y (0 .. 25) { # ntsc, two vblank delay lines, up to 24 text lines
541 my $prev_sgr; 553 my $prev_attr;
554 my ($c, $attr); # declare here for speedup
542 555
543 $scr .= sprintf "%2d \xe2\x94\x82", $y; 556 $scr .= sprintf "%2d \xe2\x94\x82", $y;
544 557
545 for (0..139) { 558 for (0..139) {
546 my $c = $M[$i]; 559 $c = $M[$i];
547 560
548 if ($c == 0x7f) { # also 0xff, but the firmware avoids that 561 if ($c == 0x7f) { # also 0xff, but the firmware avoids that
549 $scr .= "\e[m\xe2\x94\x82\e[K\n"; 562 $scr .= "\e[m\xe2\x94\x82\e[K\n";
550 563
551 my $a1 = $M[$i + 1]; 564 my $a1 = $M[$i + 1];
554 $i = 0x2000 + (($a1 * 256 + $a0) & 0xfff); 567 $i = 0x2000 + (($a1 * 256 + $a0) & 0xfff);
555 568
556 next line; 569 next line;
557 } 570 }
558 571
572 $scr .= $SGR[$prev_attr = $attr]
559 my $sgr = $SGR[ ($M[$i++ + 0x1000] & 15) | ($c & 0x80)]; 573 if $prev_attr != ($attr = ($M[$i++ + 0x1000] & 15) | ($c & 0x80));
560
561 $scr .= $prev_sgr = $sgr if $sgr ne $prev_sgr;
562 574
563 $scr .= $CHARMAP[$c & 0x7f]; 575 $scr .= $CHARMAP[$c & 0x7f];
564 } 576 }
565 577
566 $scr .= "\e[K\nvideo overflow\e[K\n"; 578 $scr .= "\e[K\nvideo overflow\e[K\n";
587 "\x1e" => 0x24 | 0x100, # CTRL-~ 599 "\x1e" => 0x24 | 0x100, # CTRL-~
588 "\x1f" => 0x75 | 0x100, # CTRL-? 600 "\x1f" => 0x75 | 0x100, # CTRL-?
589 601
590 # hardcoded rxvt keys 602 # hardcoded rxvt keys
591 "\e" => 0x2a, # ESC 603 "\e" => 0x2a, # ESC
604 "\e[2~" => 0x79 | 0x100, # CTRL-C (insert)
592 "\e[3~" => 0x03, # DC 605 "\e[3~" => 0x03, # DC
593 "\e[5~" => 0x7e, # CAPS LOCK (prior) 606 "\e[5~" => 0x7e, # CAPS LOCK (prior)
594 "\e[6~" => 0x6a, # NO SCROLL (next) 607 "\e[6~" => 0x6a, # NO SCROLL (next)
595 "\e[A" => 0x30, # UP 608 "\e[A" => 0x30, # UP
596 "\e[B" => 0x22, # DOWN 609 "\e[B" => 0x22, # DOWN
603 "\e[7~" => 0x7b, # SETUP (home) 616 "\e[7~" => 0x7b, # SETUP (home)
604 "\e[8~" => 0x23, # BREAK (end) 617 "\e[8~" => 0x23, # BREAK (end)
605 "\e[8\$" => 0x23 | 0x080, # SHIFT BREAK / DISCONNECT (shift-end) 618 "\e[8\$" => 0x23 | 0x080, # SHIFT BREAK / DISCONNECT (shift-end)
606 "\x7f" => 0x33, # BACKSPACE 619 "\x7f" => 0x33, # BACKSPACE
607 620
608 "\e[11~" => 0x32, # F1 621 "\e[11~" => 0x32, # PF1
609 "\e[11~" => 0x42, # F2 622 "\e[12~" => 0x42, # PF2
610 "\e[11~" => 0x31, # F3 623 "\e[13~" => 0x31, # PF3
611 "\e[11~" => 0x41, # F4 624 "\e[14~" => 0x41, # PF4
612); 625);
613 626
614@KEYMAP{map chr, 0x20 .. 0x40, 0x5b .. 0x7e} = unpack "C*", pack "H*", 627@KEYMAP{map chr, 0x20 .. 0x40, 0x5b .. 0x7e} = unpack "C*", pack "H*",
615 "779ad5a9a8b8a755a6b5b6b466256575" . "351a3929283837273626d656e634e5f5" . "b9" # 20..40 628 "779ad5a9a8b8a755a6b5b6b466256575" . "351a3929283837273626d656e634e5f5" . "b9" # 20..40
616 . "154514b7a5" . "244a6879591949485816574746766706" . "050a185a0817780969077a95c594a4"; # 5b..7e 629 . "154514b7a5" . "244a6879591949485816574746766706" . "050a185a0817780969077a95c594a4"; # 5b..7e
619$KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift 632$KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift
620 633
621my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; 634my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP;
622$KEYMATCH = qr{^($KEYMATCH)}s; 635$KEYMATCH = qr{^($KEYMATCH)}s;
623 636
624my %KMOD; 637my %KMOD; # currently pressed modifier keys
625 638
626sub key { 639sub key {
627 my ($key) = @_; 640 my ($key) = @_;
628 641
629 push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up 642 push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up
645 # skip input we can't decipher 658 # skip input we can't decipher
646 substr $STDIN_BUF, 0, 1, ""; 659 substr $STDIN_BUF, 0, 1, "";
647} 660}
648 661
649if ($KBD) { 662if ($KBD) {
650 system "stty -icanon -icrnl -inlcr -echo min 1 time 0"; 663 system "stty -icanon -icrnl -inlcr -echo min 1 time 0"; # -isig
651 eval q{ sub END { system "stty sane" } }; 664 eval q{ sub END { system "stty sane" } };
652 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { exit 1 }; 665 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = sub { exit 1 };
653} 666}
654 667
655############################################################################# 668#############################################################################
676); 689);
677 690
678############################################################################# 691#############################################################################
679# process/pty management 692# process/pty management
680 693
694if (1) {
681require IO::Pty; 695 require IO::Pty;
682$PTY = IO::Pty->new; 696 $PTY = IO::Pty->new;
683 697
684my $slave = $PTY->slave; 698 my $slave = $PTY->slave;
685 699
686$PTY->set_winsize (24, 80); 700 $PTY->set_winsize (24, 80);
687 701
688unless (fork) { 702 unless (fork) {
703 $ENV{LC_ALL} = "C";
689 $ENV{TERM} = $VT102 ? "vt102" : "vt100"; 704 $ENV{TERM} = $VT102 ? "vt102" : "vt100";
690 705
691 close $PTY; 706 close $PTY;
692 707
693 open STDIN , "<&", $slave; 708 open STDIN , "<&", $slave;
694 open STDOUT, ">&", $slave; 709 open STDOUT, ">&", $slave;
695 open STDERR, ">&", $slave; 710 open STDERR, ">&", $slave;
696 711
697 system "stty ixoff erase ^H"; 712 system "stty ixoff erase ^H";
698 713
699 $PTY->make_slave_controlling_terminal; 714 $PTY->make_slave_controlling_terminal;
715 $PTY->close_slave;
716
717 @ARGV = "sh" unless @ARGV;
718 exec @ARGV;
719 }
720
700 $PTY->close_slave; 721 $PTY->close_slave;
701 722} else {
702 @ARGV = "sh" unless @ARGV; 723 open $PTY, "+</dev/null"
703 exec @ARGV; 724 or die "/dev/null: $!";
704} 725}
705
706$PTY->close_slave;
707 726
708############################################################################# 727#############################################################################
709# the actual hardware simulator 728# the actual hardware simulator
710 729
711my @ICACHE; # compiled instruction cache 730my @ICACHE; # compiled instruction/basic block cache
712 731
732my $POWERSAVE; # powersave counter
733
734my $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.
713while () { 740while () {
714 # execute extended basic blocks 741 # execute an extended basic block
715 $PC = ($ICACHE[$PC] ||= do { 742 $PC = ($ICACHE[$PC] ||= do {
716 my $pc = $PC; 743 my $pc = $PC;
717 744
718 my $insn = ""; 745 my $insn = "";
719 746
731 758
732 s/\bPC\b/$pc/ge; # PC at end of insn 759 s/\bPC\b/$pc/ge; # PC at end of insn
733 s/\bBRA\b/return/g; # conditional jump 760 s/\bBRA\b/return/g; # conditional jump
734 s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump 761 s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump
735 762
736 s/\bIN\b/ sprintf "\$A = in_%02x", $M[$pc++]/xge; 763 s/\bIN\b/ sprintf "\$A = in_%02x", $M[$pc++]/xge; # in insns call in_HEX
737 s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; 764 s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; # out likewise
738 } 765 }
739 766
740 $insn .= "$op;\n"; 767 $insn .= "$op;\n";
741 } 768 }
742 769
743 770
744 $insn .= $pc; 771 $insn .= $pc;
745 $insn =~ s/\x00.*$//s; 772 $insn =~ s/\x00.*$//s;
746 773
747 eval "use integer; sub { $insn }" or die "$insn: $@" 774 eval "sub { $insn }" or die "$insn: $@"
748 })->(); 775 })->();
749 776
750 ++$CLK; 777 ++$CLK;
751 778
752 # things we do from time too time only 779 # things we do from time to time only
753 unless ($CLK & 0xf) { 780 unless ($CLK & 0xf) {
754 # do I/O 781 # do I/O
755 782
756 unless ($CLK & 0xfff) { 783 unless ($CLK & 0xfff) {
784 if (select $x = $RIN, undef, undef, $POWERSAVE < 10 ? 0 : $CURSOR_IS_ON && 3600) {
757 785
758 # pty/serial I/O 786 # pty/serial I/O
759 unless ((@PUSARTRECV >= 128) || @KQUEUE || !$PTY) { 787 if ($PTY && (vec $x, fileno $PTY, 1) && (@PUSARTRECV < 128) && !@KQUEUE) {
760 my $rin = ""; (vec $rin, fileno $PTY, 1) = 1;
761
762 if (select $rin, undef, undef, 0) {
763 sysread $PTY, my $buf, 256; 788 sysread $PTY, my $buf, 256;
789
790 # linux don't do cs7 and/or parity anymore, so we need to filter
791 # out xoff characters to avoid freezes.
764 push @PUSARTRECV, unpack "C*", $buf; 792 push @PUSARTRECV, grep { ($_ & 0x7f) != 0x13 } unpack "C*", $buf;
765 } 793 }
766 }
767 794
768 # keyboard input 795 # keyboard input
769 if ($KBD) { 796 if ($KBD && (vec $x, 0, 1)) {
797 # to avoid non-blocking mode on stdin (and stty min 0), we
798 # just read byte-by-byte after a select says there is data.
770 while (select my $rin = "\x01", undef, undef, 0) { 799 while (select my $rin = "\x01", undef, undef, 0) {
771 sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF 800 sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF
772 or last; 801 or last;
802 }
803
804 stdin_parse if length $STDIN_BUF;
773 } 805 }
774 806
775 stdin_parse if length $STDIN_BUF; 807 $POWERSAVE = 0; # activity
808 } elsif (@PUSARTRECV || @KQUEUE) {
809 $POWERSAVE = 0;
810 } else {
811 ++$POWERSAVE;
776 } 812 }
777 } 813 }
778 814
779 # kick off various interrupts 815 # kick off serial input interrupt quite often
780
781 $RST |= 2 if @PUSARTRECV && $XON; # VT100, but works on vt102, too (probably not used on real hardware though) 816 $RST |= 2 if @PUSARTRECV && $XON; # VT100, but works on vt102, too (probably not used on real hardware though)
782 #$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy 817 #$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy
783 818
784 # kick off vertical retrace form time to time 819 # kick off vertical retrace interrupt from time to time
785 unless ($CLK & 0x1ff) { 820 unless ($CLK & 0x1ff) {
786 $RST |= 4; # vertical retrace 821 $RST |= 4; # vertical retrace
787 } 822 }
788 823
789 # handle video hardware 824 # handle video hardware
790 unless ($CLK & 0x3fff) { 825 unless ($CLK & 0x3fff) {
791 display; 826 display;
792 } 827 }
793 } 828 }
794 829
795 # the interrupt logic 830 # the interrupt logic - we only interrupt after basic blocks
831 # which, as a side effect, ensures that we don't interrupt
832 # "ei; ret" sequences and thus reduce the risk of stack overflows.
796 if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) { 833 if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) {
797 # rst 1 kbd data available 834 # rst 1 kbd data available
798 # rst 2 pusart xmit+recv flag 835 # rst 2 pusart xmit+recv flag
799 # rst 4 vertical retrace 836 # rst 4 vertical retrace
800 # 5.5 vt125 mb7 trans ready (serial send?) 837 # 5.5 vt125 mb7 trans ready (serial send?)
801 # 6.5 vt125 mb7 read ready (something modem?) 838 # 6.5 vt125 mb7 read ready (something modem?)
802 # 7.5 vt125 mb7 vblank h(?) 839 # 7.5 vt125 mb7 vblank h(?)
803 # trap vt125 mbi init h(?) 840 # trap vt125 mbi init h(?)
804 my $vec; 841 my $vec;
805 842
806 $x = $INTPEND & ~$INTMASK; 843 my $pend = $INTPEND & ~$INTMASK;
807 844
808 if ($x & 1) { $vec = 0x2c; $INTPEND &= ~1; 845 if ($pend & 1) { $vec = 0x2c; $INTPEND &= ~1;
809 } elsif ($x & 2) { $vec = 0x34; $INTPEND &= ~2; 846 } elsif ($pend & 2) { $vec = 0x34; $INTPEND &= ~2;
810 } elsif ($x & 4) { $vec = 0x3c; $INTPEND &= ~4; 847 } elsif ($pend & 4) { $vec = 0x3c; $INTPEND &= ~4;
811# } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # the vt102 firmware doesn't like combined interrupts 848# } elsif ($RST ) { $vec = $RST * 8; $RST = 0; # the vt102 firmware doesn't like combined interrupts
812 } elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102 849 } elsif ($RST & 1) { $vec = 0x08; $RST &= ~1; # separate is better for vt102
813 } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2; 850 } elsif ($RST & 2) { $vec = 0x10; $RST &= ~2;
814 } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; 851 } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4;
815 } else { 852 } else {
816 die; 853 die;
817 } 854 }
818 855
856 # jump to the interrupt vector
819 $M[--$SP] = $PC >> 8; 857 $M[--$SP] = $PC >> 8;
820 $M[--$SP] = $PC & 0xff; 858 $M[--$SP] = $PC & 0xff;
821 $PC = $vec; 859 $PC = $vec;
822 860
823 $IFF = 0; 861 $IFF = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines