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

Comparing vt102/vt102 (file contents):
Revision 1.22 by root, Wed Dec 3 10:35:59 2014 UTC vs.
Revision 1.23 by root, Wed Dec 3 11:09:51 2014 UTC

105 105
106############################################################################# 106#############################################################################
107# 8085 CPU registers and I/O support 107# 8085 CPU registers and I/O support
108 108
109# 8080/8085 registers 109# 8080/8085 registers
110my ($A, $B, $C, $D, $E, $H, $L); 110my ($A, $B, $C, $D, $E, $H, $L); # 8 bit general purpose
111my ($PC, $SP, $IFF, $FA, $FZ, $FS, $FP, $FC); 111my ($PC, $SP, $IFF); # program counter, stack pointer, interrupt flag
112my ($FA, $FZ, $FS, $FP, $FC); # condition codes (psw)
112 113
113my $RST = 0; # 8080 pending interrupts 114my $RST = 0; # pending interrupts (8259 interrupt controller)
114my $INTMASK = 7; # 8085 half interrupts 115my $INTMASK = 7; # 8085 half interrupt mask
115my $INTPEND = 0; # 8085 half interrupts 116my $INTPEND = 0; # 8085 half interrupts pending
116 117
117my $CLK; # rather inexact clock 118my $CLK; # rather inexact clock, counts extended basic blocks
118 119
119############################################################################# 120#############################################################################
120# the dreaded NVR1400 chip. not needed to get it going, but provided anyway 121# the dreaded NVR1400 chip. not needed to get it going, but provided anyway
121 122
122# nvram 123# nvram
134 sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase 135 sub { $NVR[$_[0]] = 0x3fff; }, # 5 erase
135 sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read 136 sub { $NVRDATA = $NVR[$_[0]]; }, # 6 read
136 sub { }, # 7 standby 137 sub { }, # 7 standby
137); 138);
138 139
139my @NVR_BITIDX;
140$NVR_BITIDX[1 << $_] = 9 - $_ for 0..9; 140my @NVR_BITIDX; $NVR_BITIDX[1 << $_] = 9 - $_ for 0..9;
141 141
142# the nvr1400 state machine. what a monster 142# the nvr1400 state machine. what a monster
143sub nvr() { 143sub nvr() {
144 my $a1 = $NVR_BITIDX[(~$NVRADDR ) & 0x3ff]; 144 my $a1 = $NVR_BITIDX[(~$NVRADDR ) & 0x3ff];
145 my $a0 = $NVR_BITIDX[(~$NVRADDR >> 10) & 0x3ff]; 145 my $a0 = $NVR_BITIDX[(~$NVRADDR >> 10) & 0x3ff];
146 146
147# printf "NVR %02x A %020b %d %d D %02x\n", $NVRLATCH, $NVRADDR & 0xfffff, $a1, $a0, $NVRDATA;
148
149 $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1) 147 $NVRCMD[($NVRLATCH >> 1) & 7]($a1 * 10 + $a0, $NVRLATCH & 1)
150} 148}
151 149
152############################################################################# 150#############################################################################
153# I/O ports - output 151# I/O ports - output
154 152
155my $DC11_REVERSE = 0; 153my $DC11_REVERSE = 0; # light background?
156 154
157my $XON = 1; # false if terminal wants us to pause 155my $XON = 1; # false if terminal wants us to pause
158my $PUSARTCMD; 156my $PUSARTCMD;
159 157
158my $KSTATUS; # keyboard status (click + scan flag + leds)
160my @KXMIT; # current scan queue 159my @KXMIT; # current scan queue
161my %KXMIT; # currently pressed keys 160my %KXMIT; # currently pressed keys
162my @KQUEUE; # key event queue 161my @KQUEUE; # key event queue
163my $KXCNT; # count for debouncew 162my $KXCNT; # count for debouncew
164my @PUSARTRECV; 163
165my $KSTATUS; 164my @PUSARTRECV; # serial input (to terminal) queue
166 165
167sub out_00 { # pusartdata 166sub out_00 { # pusartdata
168 # handle xon/xoff, but also pass it through 167 # handle xon/xoff, but also pass it through
169 if ($_[0] == 0x13) { 168 if ($_[0] == 0x13) {
170 $XON = 0; 169 $XON = 0;
177 syswrite $PTY, chr $_[0]; 176 syswrite $PTY, chr $_[0];
178 177
179 $INTPEND |= 1; 178 $INTPEND |= 1;
180} 179}
181 180
182sub out_01 { 181sub out_01 { # pusartcmd
183 $PUSARTCMD = shift; 182 $PUSARTCMD = shift;
184 183
185 $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy 184 $INTPEND |= 1 if $PUSARTCMD & 0x01; # VT102, 5.5 txrdy
186 $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
187} 186}
188 187
189sub out_02 { } # baudrate generator 188sub out_02 { } # baudrate generator
190 189
191sub out_23 { } # unknown 190sub out_23 { } # vt102 unknown
192sub out_27 { } # unknown 191sub out_27 { } # vt102 unknown
193sub out_2f { } # unknown, connected to in 0f 192sub out_2f { } # vt102 unknown, connected to in 0f
194 193
195sub out_42 { } # brightness 194sub out_42 { } # brightness
196 195
197sub out_62 { 196sub out_62 { # nvr latch register (4 bits)
198 $NVRLATCH = shift; 197 $NVRLATCH = shift;
199} 198}
200 199
201sub out_a2 { 200sub out_a2 { # device control 011
202 my $dc11 = 0x0f & shift; 201 my $dc11 = 0x0f & shift;
203 202
204 $DC11_REVERSE = 1 if $dc11 == 0b1010; 203 $DC11_REVERSE = 1 if $dc11 == 0b1010;
205 $DC11_REVERSE = 0 if $dc11 == 0b1011; 204 $DC11_REVERSE = 0 if $dc11 == 0b1011;
206} 205}
207 206
208sub out_c2 { } # unknown 207sub out_c2 { } # unknown
209sub out_d2 { } # 0..3 == 80c/132c/60hz/50hz 208sub out_d2 { } # device control 012, 0..3 == 80c/132c/60hz/50hz
210 209
211sub out_82 { 210sub out_82 { # keyboard txmit
212 # keyboard 211 # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4 (vt100)
213
214 # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 212 # CLICK STARTSCAN ONLINE LOCKED | CTS DSR INS L1 (vt102)
215 # CLICK STARTSCAN ONLINE LOCKED | L1 L2 L3 L4
216 $KSTATUS = $_[0]; 213 $KSTATUS = $_[0];
217 214
218 # start new scan unless scan in progress 215 # start new scan unless scan is in progress
219 if (($_[0] & 0x40) && !@KXMIT) { 216 if (($_[0] & 0x40) && !@KXMIT) {
220 # do not reply with keys in locked mode 217 # do not reply with keys in locked mode
221 # or during post (0xff), 218 # or during post (0xff),
222 # mostly to skip init and not fail POST, 219 # mostly to skip init and not fail POST,
223 # and to send startup keys only when terminal is ready 220 # and to send startup keys only when terminal is ready
245} 242}
246 243
247############################################################################# 244#############################################################################
248# I/O ports - input 245# I/O ports - input
249 246
250my $NVRBIT; 247my $NVRBIT; # the current nvr data bit
251my $LBA6; # twice the frequenxy of LBA7 248my $LBA6; # twice the frequenxy of LBA7
252 249
253sub in_00 { # pusart data 250sub in_00 { # pusart data
254 # interrupt not generated here, because infinite 251 # interrupt not generated here, because infinite
255 # speed does not go well with the vt102. 252 # speed does not go well with the vt102.
256 253
260sub in_01 { # pusart status 257sub in_01 { # pusart status
261 # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY 258 # DSR SYNDET FE OE | PE TXEMPTY RXRDY TXRDY
262 0x85 + (@PUSARTRECV && 0x02) 259 0x85 + (@PUSARTRECV && 0x02)
263} 260}
264 261
265sub in_22 { # modem buffer(?) 262sub in_22 { # modem buffer
266 # wild guess: -CTS -SPDI -RI -CD 0 0 0 0 263 # wild guess: -CTS -SPDI -RI -CD 0 0 0 0
267 0x20 264 0x20
268} 265}
269 266
270sub in_0f { 0xff } # vt102 unknown, connected to out 2f 267sub in_0f { 0xff } # vt102 unknown, connected to out 2f
299############################################################################# 296#############################################################################
300# 8085 cpu opcodes and flag handling 297# 8085 cpu opcodes and flag handling
301 298
302my $x; # dummy scratchpad for opcodes 299my $x; # dummy scratchpad for opcodes
303 300
304sub sf { # set flags (ZSC - AP not implemented) 301sub sf { # set flags, full version (ZSC - AP not implemented)
305 $FS = $_[0] & 0x080; 302 $FS = $_[0] & 0x080;
306 $FZ = !($_[0] & 0x0ff); 303 $FZ = !($_[0] & 0x0ff);
307 $FC = $_[0] & 0x100; 304 $FC = $_[0] & 0x100;
308 305
309 $_[0] &= 0xff; 306 $_[0] &= 0xff;
310} 307}
311 308
312sub sf8 { # set flags (ZSC - AP not implemented) 309sub sf8 { # set flags, for 8-bit results (ZSC - AP not implemented)
313 $FS = $_[0] & 0x080; 310 $FS = $_[0] & 0x080;
314 $FZ = !($_[0] & 0x0ff); 311 $FZ = !($_[0] & 0x0ff);
315 $FC = 0; 312 $FC = 0;
316} 313}
317 314
318sub sf_nc { # set flags except carry 315sub sf_nc { # set flags, except carry
319 $FS = $_[0] & 0x080; 316 $FS = $_[0] & 0x080;
320 $FZ = ($_[0] & 0x0ff) == 0; 317 $FZ = ($_[0] & 0x0ff) == 0;
321 318
322 $_[0] &= 0xff; 319 $_[0] &= 0xff;
323} 320}
324 321
322# opcode table
325my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff; 323my @op = map { sprintf "status(); die 'unknown op %02x'", $_ } 0x00 .. 0xff;
326 324
327my @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
328my @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
329 327
330$op[0x00] = ''; 328$op[0x00] = ''; # nop
331 329
332# mov r,r / r,M / M,r 330# mov r,r / r,M / M,r
333for my $s (0..7) { 331for my $s (0..7) {
334 for my $d (0..7) { 332 for my $d (0..7) {
335 $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov 333 $op[0x40 + $d * 8 + $s] = "$reg[$d] = $reg[$s]"; # mov
375$op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr 373$op[0x04 + $_ * 8] = "sf_nc ++$reg[$_]" for 0..7; # inr
376$op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr 374$op[0x05 + $_ * 8] = "sf_nc --$reg[$_]" for 0..7; # dcr
377 375
378$op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc 376$op[0x07] = ' $FC = $A & 0x80; $A = (($A << 1) + ($FC && 0x01)) & 0xff '; # rlc
379$op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral 377$op[0x17] = ' ($FC, $A) = ($A & 0x80, (($A << 1) + ($FC && 0x01)) & 0xff)'; # ral
380
381$op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc 378$op[0x0f] = ' $FC = $A & 0x01; $A = ($A >> 1) | ($FC && 0x80) '; # rrc
382$op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar 379$op[0x1f] = ' ($FC, $A) = ($A & 0x01, ($A >> 1) | ($FC && 0x80))'; # rar
383
384$op[0x2f] = '$A ^= 0xff'; # cma
385 380
386# 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)
387# 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
388sub dad { 383sub dad {
389 $x = $H * 256 + $L + $_[0]; 384 $x = $H * 256 + $L + $_[0];
394 389
395$op[0x09] = 'dad $B * 256 + $C'; # dad 390$op[0x09] = 'dad $B * 256 + $C'; # dad
396$op[0x19] = 'dad $D * 256 + $E'; # dad 391$op[0x19] = 'dad $D * 256 + $E'; # dad
397$op[0x29] = 'dad $H * 256 + $L'; # dad 392$op[0x29] = 'dad $H * 256 + $L'; # dad
398$op[0x39] = 'dad $SP '; # dad 393$op[0x39] = 'dad $SP '; # dad
394
395$op[0x2f] = '$A ^= 0xff'; # cma
399 396
400$op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add 397$op[0x80 + $_] = 'sf $A += + ' . $reg[$_] for 0..7; # add
401$op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc 398$op[0x88 + $_] = 'sf $A += ($FC && 1) + ' . $reg[$_] for 0..7; # adc
402$op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub 399$op[0x90 + $_] = 'sf $A -= + ' . $reg[$_] for 0..7; # sub
403$op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb 400$op[0x98 + $_] = 'sf $A -= ($FC && 1) + ' . $reg[$_] for 0..7; # sbb
404$op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana 401$op[0xa0 + $_] = 'sf8 $A &= ' . $reg[$_] for 0..7; # ana
405$op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra 402$op[0xa8 + $_] = 'sf8 $A ^= ' . $reg[$_] for 0..7; # xra
406$op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora 403$op[0xb0 + $_] = 'sf8 $A |= ' . $reg[$_] for 0..7; # ora
407$op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp 404$op[0xb8 + $_] = 'sf $x = $A - ' . $reg[$_] for 0..7; # cmp
408# possible todo: optimize ora a, maybe xra a 405# possible todo: optimize ora a, maybe xra a, possibly ana
409 406
410$op[0xc6] = 'sf $A += IMM8'; # adi 407$op[0xc6] = 'sf $A += IMM8'; # adi
411# ce ACI NYI, apparently unused
412$op[0xd6] = 'sf $A -= IMM8'; # sui 408$op[0xd6] = 'sf $A -= IMM8'; # sui
413# de SBI NYI, apparently unused
414$op[0xe6] = 'sf8 $A &= IMM8'; # ani 409$op[0xe6] = 'sf8 $A &= IMM8'; # ani
415$op[0xee] = 'sf8 $A ^= IMM8'; # xri 410$op[0xee] = 'sf8 $A ^= IMM8'; # xri
416$op[0xf6] = 'sf8 $A |= IMM8'; # ori 411$op[0xf6] = 'sf8 $A |= IMM8'; # ori
417$op[0xfe] = 'sf $A - IMM8'; # cpi 412$op[0xfe] = 'sf $A - IMM8'; # cpi
413# ce ACI NYI, apparently unused
414# de SBI NYI, apparently unused
418 415
419$op[0xc5] = 'PUSH $B; PUSH $C'; 416$op[0xc5] = 'PUSH $B; PUSH $C';
420$op[0xd5] = 'PUSH $D; PUSH $E'; 417$op[0xd5] = 'PUSH $D; PUSH $E';
421$op[0xe5] = 'PUSH $H; PUSH $L'; 418$op[0xe5] = 'PUSH $H; PUSH $L';
422$op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw 419$op[0xf5] = 'PUSH $A; PUSH +($FS && 0x80) | ($FZ && 0x40) | ($FA && 0x10) | ($FP && 0x04) | ($FC && 0x01)'; # psw
424$op[0xc1] = '($C, $B) = (POP, POP)'; # pop 421$op[0xc1] = '($C, $B) = (POP, POP)'; # pop
425$op[0xd1] = '($E, $D) = (POP, POP)'; # pop 422$op[0xd1] = '($E, $D) = (POP, POP)'; # pop
426$op[0xe1] = '($L, $H) = (POP, POP)'; # pop 423$op[0xe1] = '($L, $H) = (POP, POP)'; # pop
427$op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw 424$op[0xf1] = '($x, $A) = (POP, POP); ($FS, $FZ, $FA, $FP, $FC) = ($x & 0x80, $x & 0x40, $x & 0x10, $x & 0x04, $x & 0x01)'; # pop psw
428 425
429$op[0xc2 + $_ * 8] = 'BRA IMM16 ' . $cc[$_] for 0..7; # jcc 426$op[0xc2 + $_ * 8] = 'BRA IMM16 if ' . $cc[$_] for 0..7; # jcc
430$op[0xc3] = 'JMP IMM16'; # jmp 427$op[0xc3] = 'JMP IMM16'; # jmp
431 428
432$op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) ' . $cc[$_] for 0..7; # ccc 429$op[0xc4 + $_ * 8] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16) if ' . $cc[$_] for 0..7; # ccc
433$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call 430$op[0xcd] = '(PUSH PC >> 8), (PUSH PC & 0xff), (BRA IMM16)'; # call
434 431
435$op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 ' . $cc[$_] for 0..7; # rcc 432$op[0xc0 + $_ * 8] = 'BRA POP + POP * 256 if ' . $cc[$_] for 0..7; # rcc
436$op[0xc9] = 'JMP POP + POP * 256'; # ret 433$op[0xc9] = 'JMP POP + POP * 256'; # ret
437 434
438$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst 435$op[0xc7 + $_ * 8] = "JMP $_ * 8" for 0..7; # rst
439 436
440$op[0xe9] = 'JMP $H * 256 + $L'; # pchl 437$op[0xe9] = 'JMP $H * 256 + $L'; # pchl
442 439
443$op[0x37] = '$FC = 1 '; # stc 440$op[0x37] = '$FC = 1 '; # stc
444$op[0x3f] = '$FC = !$FC'; # cmc 441$op[0x3f] = '$FC = !$FC'; # cmc
445 442
446$op[0xd3] = 'OUT'; # out 443$op[0xd3] = 'OUT'; # out
447$op[0xdb] = 'IN'; # in 444$op[0xdb] = 'IN'; # in
448 445
449$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg 446$op[0xeb] = '($D, $E, $H, $L) = ($H, $L, $D, $E)'; # xchg
450 447
451# e3 xthl NYI # @ 917b in e69, hl <-> (sp) 448# e3 xthl NYI # @ 917b in e69, hl <-> (sp)
452 449
453$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (incomplete) 450$op[0x20] = '$A = $INTPEND * 16 + $INTMASK + ($IFF && 8)'; # rim (8085, incomplete)
454$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (incomplete) 451$op[0x30] = '$INTMASK = $A & 7 if $A & 8'; # sim (8085, incomplete)
455 452
456$op[0xf3] = '$IFF = 0'; # DI 453$op[0xf3] = '$IFF = 0'; # di
457$op[0xfb] = '$IFF = 1'; # EI 454$op[0xfb] = '$IFF = 1'; # ei
458 455
459# yeah, the fucking setup screens actually use daa... 456# yeah, the fucking setup screen actually uses daa...
460$op[0x27] = ' 457$op[0x27] = '
461 my ($h, $l); 458 my ($h, $l);
462 459
463 ($h, $l) = ($A >> 4, $A & 15); 460 ($h, $l) = ($A >> 4, $A & 15);
464 461
472 $A = ($h * 16 + $l) & 0xff; 469 $A = ($h * 16 + $l) & 0xff;
473 } 470 }
474'; # daa, almost certainly borked, also, acarry not set by sf 471'; # daa, almost certainly borked, also, acarry not set by sf
475 472
476############################################################################# 473#############################################################################
477# print cpu status for debugging purposes 474# debug
478 475
479# print cpu status, for debugging 476# print cpu status, for debugging
480sub status { 477sub status {
481 my $PC = shift || $PC; 478 my $PC = shift || $PC;
482 479
494############################################################################# 491#############################################################################
495# video emulation 492# video emulation
496 493
497binmode STDOUT; 494binmode STDOUT;
498 495
499my @CHARMAP = ( 496my @CHARMAP = ( # acschars / chars 0..31
500 " " , "\x{29eb}", "\x{2592}", "\x{2409}", 497 " " , "\x{29eb}", "\x{2592}", "\x{2409}",
501 "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}", 498 "\x{240c}", "\x{240d}", "\x{240a}", "\x{00b0}",
502 "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}", 499 "\x{00b1}", "\x{2424}", "\x{240b}", "\x{2518}",
503 "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}", 500 "\x{2510}", "\x{250c}", "\x{2514}", "\x{253c}",
504 "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}", 501 "\x{23ba}", "\x{23bb}", "\x{2500}", "\x{23bc}",
625$KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift 622$KEYMAP{"\x20" ^ $_} ||= $KEYMAP{$_} | 0x080 for "a" .. "z"; # shift
626 623
627my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP; 624my $KEYMATCH = join "|", map quotemeta, reverse sort keys %KEYMAP;
628$KEYMATCH = qr{^($KEYMATCH)}s; 625$KEYMATCH = qr{^($KEYMATCH)}s;
629 626
630my %KMOD; 627my %KMOD; # currently pressed modifier keys
631 628
632sub key { 629sub key {
633 my ($key) = @_; 630 my ($key) = @_;
634 631
635 push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up 632 push @KQUEUE, -0x7c if !($key & 0x100) && delete $KMOD{0x7c}; # ctrl-up
713$PTY->close_slave; 710$PTY->close_slave;
714 711
715############################################################################# 712#############################################################################
716# the actual hardware simulator 713# the actual hardware simulator
717 714
718my @ICACHE; # compiled instruction cache 715my @ICACHE; # compiled instruction/basic block cache
719 716
720my $POWERSAVE; # powersave counter 717my $POWERSAVE; # powersave counter
721 718
722my $RIN; # libev for the less well-off 719my $RIN; # libev for the less well-off
723 720
724(vec $RIN, 0, 1) = 1 if $KBD; 721(vec $RIN, 0, 1) = 1 if $KBD;
725(vec $RIN, fileno $PTY, 1) = 1 if $PTY; 722(vec $RIN, fileno $PTY, 1) = 1 if $PTY;
726 723
724# the cpu.
727while () { 725while () {
728 # execute extended basic blocks 726 # execute extended basic blocks
729 $PC = ($ICACHE[$PC] ||= do { 727 $PC = ($ICACHE[$PC] ||= do {
730 my $pc = $PC; 728 my $pc = $PC;
731 729
745 743
746 s/\bPC\b/$pc/ge; # PC at end of insn 744 s/\bPC\b/$pc/ge; # PC at end of insn
747 s/\bBRA\b/return/g; # conditional jump 745 s/\bBRA\b/return/g; # conditional jump
748 s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump 746 s/\bJMP\b(.*)/$1\x00/sg; # unconditional jump
749 747
750 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
751 s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; 749 s/\bOUT\b/sprintf "out_%02x \$A ", $M[$pc++]/xge; # out likewise
752 } 750 }
753 751
754 $insn .= "$op;\n"; 752 $insn .= "$op;\n";
755 } 753 }
756 754
769 767
770 unless ($CLK & 0xfff) { 768 unless ($CLK & 0xfff) {
771 if (select $x = $RIN, undef, undef, $POWERSAVE < 100 ? 0 : 0.2) { 769 if (select $x = $RIN, undef, undef, $POWERSAVE < 100 ? 0 : 0.2) {
772 # pty/serial I/O 770 # pty/serial I/O
773 if ($PTY && (vec $x, fileno $PTY, 1) && (@PUSARTRECV < 128) && !@KQUEUE) { 771 if ($PTY && (vec $x, fileno $PTY, 1) && (@PUSARTRECV < 128) && !@KQUEUE) {
774 my $rin = ""; (vec $rin, fileno $PTY, 1) = 1;
775
776 if (select $rin, undef, undef, 0) {
777 sysread $PTY, my $buf, 256; 772 sysread $PTY, my $buf, 256;
778 773
779 # linux don't do cs7 and/or parity anymore, so we need to filter 774 # linux don't do cs7 and/or parity anymore, so we need to filter
780 # out xoff characters to avoid freezes. 775 # out xoff characters to avoid freezes.
781 push @PUSARTRECV, grep { ($_ & 0x7f) != 0x13 } unpack "C*", $buf; 776 push @PUSARTRECV, grep { ($_ & 0x7f) != 0x13 } unpack "C*", $buf;
782 }
783 } 777 }
784 778
785 # keyboard input 779 # keyboard input
786 if ($KBD && (vec $x, 0, 1)) { 780 if ($KBD && (vec $x, 0, 1)) {
781 # to avoid non-blocking mode on stdin (and stty min 0), we
782 # just read byte-by-byte after a select says there is data.
787 while (select my $rin = "\x01", undef, undef, 0) { 783 while (select my $rin = "\x01", undef, undef, 0) {
788 sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF 784 sysread STDIN, $STDIN_BUF, 1, length $STDIN_BUF
789 or last; 785 or last;
790 } 786 }
791 787
792 stdin_parse if length $STDIN_BUF; 788 stdin_parse if length $STDIN_BUF;
793 $POWERSAVE = 0;
794 } 789 }
795 790
796 $POWERSAVE = 0; 791 $POWERSAVE = 0; # activity
797 } else { 792 } else {
798 ++$POWERSAVE unless @PUSARTRECV || @KQUEUE; 793 ++$POWERSAVE unless @PUSARTRECV || @KQUEUE;
799 } 794 }
800 } 795 }
801 796
802 # kick off various interrupts 797 # kick off serial input interrupt quite often
803
804 $RST |= 2 if @PUSARTRECV && $XON; # VT100, but works on vt102, too (probably not used on real hardware though) 798 $RST |= 2 if @PUSARTRECV && $XON; # VT100, but works on vt102, too (probably not used on real hardware though)
805 #$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy 799 #$INTPEND |= 2 if @PUSARTRECV && $XON; # VT102, 6.5 rxrdy
806 800
807 # kick off vertical retrace form time to time 801 # kick off vertical retrace interrupt from time to time
808 unless ($CLK & 0x1ff) { 802 unless ($CLK & 0x1ff) {
809 $RST |= 4; # vertical retrace 803 $RST |= 4; # vertical retrace
810 } 804 }
811 805
812 # handle video hardware 806 # handle video hardware
813 unless ($CLK & 0x3fff) { 807 unless ($CLK & 0x3fff) {
814 display; 808 display;
815 } 809 }
816 } 810 }
817 811
818 # the interrupt logic 812 # the interrupt logic - we only interrupt after basic blocks
813 # which, as a side effect, ensures that we don't interrupt
814 # "ei; ret" sequences and thus reduce the risk of stack overflows.
819 if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) { 815 if (($RST || ($INTPEND & ~$INTMASK)) && $IFF) {
820 # rst 1 kbd data available 816 # rst 1 kbd data available
821 # rst 2 pusart xmit+recv flag 817 # rst 2 pusart xmit+recv flag
822 # rst 4 vertical retrace 818 # rst 4 vertical retrace
823 # 5.5 vt125 mb7 trans ready (serial send?) 819 # 5.5 vt125 mb7 trans ready (serial send?)
837 } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4; 833 } elsif ($RST & 4) { $vec = 0x20; $RST &= ~4;
838 } else { 834 } else {
839 die; 835 die;
840 } 836 }
841 837
838 # jump to the interrupt vector
842 $M[--$SP] = $PC >> 8; 839 $M[--$SP] = $PC >> 8;
843 $M[--$SP] = $PC & 0xff; 840 $M[--$SP] = $PC & 0xff;
844 $PC = $vec; 841 $PC = $vec;
845 842
846 $IFF = 0; 843 $IFF = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines