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