The Keyboard. Firstly, it is important to once again recognise that the VIC 20 is very similar to the C64. Both machines have very similar keyboards. They don't function entirely the same though. The difference is described later on. Almost all my VIC 20 keyboard knowledge is based on the information and code provided by VICE. THE VIC 20 KEYBOARD AND FEATURES The keyboard is treated as a set of switches organized into a matrix of 8 columns by 8 rows. The keyboard matrix is scanned for key switch- closures by the KERNAL using the VIA #2 chip (MOS 6522 Versatile Interface Adapter). Two VIA registers are used to perform the scan: register #0 at location 37152 ($9120) for keyboard columns and register #1 at location 37153 ($9121) for keyboard rows. Bits 0-7 of memory location 37152 correspond to the columns 0-7. Bits 0-7 of memory location 37153 correspond to rows 0-7. By writing column values in sequence, then reading row values, the KERNAL decodes the switch closures into the CHR$ (N) value of the key pressed. Eight columns by eight rows yields 64 possible values. Below is shown the keyboard layout of a real VIC-20 and C64 computers. They both use the same keyboard mechanism, only wiring on the motherboeard differs. The keyboard mechanism is interchangeable between VIC-20 and the C64. However, on the main circuit board (motherboard), the VIC-20 has all the lines connected to the VIA chip in order, whereas C64 has Column3 and Column7 swapped. That's why the C64 gives different keyboard matrix. It also affects to the keycodes returned. VIC-20 / C64 Standard: | <- 1! 2" 3# 4$ 5% 6& 7' 8( 9) 0 + - GBP HOME DEL | | F1 | | CTRL q w e r t y u i o p @ * ^~ RESTORE | | F3 | | RUN LOCK a s d f g h j k l :[ ;] = RETURN | | F5 | | C= SHIFT z x c v b n m ,< .> /? SHIFT DWN RGT | | F7 | -------------|_______________SPACE_______________|------------------ '~' is 'pi' VIC-20 Keyboard Matrix 9121 9120: (Write column value to this address) 7f bf df ef f7 fb fd fe fe RunStop q CBM Space 2 Control Lft_arr 1 fd Shift_L e s z 4 a w 3 fb x t f c 6 d r 5 f7 v u h b 8 g y 7 ef n o k m 0 j i 9 df , @ : . - l p + bf / Up_arr = Shift_R Home ; * GBP 7f Down F5 F3 F1 F7 Right Return Del Note: the values above are the complements of the values 1, 2, 4, 8, 16 32, 64, 128. The VIC 20 uses one of its VIA chip timers to generate regular interrupts to check on such things as its keyboard. This procedure used to check the keyboard is described below. The KERNEL begins by deciding if any key has been pressed. It does this by writing $00 to $9120 which is the code to scan all keyboard columns. If it receives back the value $FF, then it knows that no key is being pushed. If it receives a different value then a key is currently being pushed down. When this happens the KERNEL scans each row separately to determine which keys are being pushed down. For example, if the KERNEL sends the value $FE to the address $9120 and it receives the value $7f back at address $9121, then it would know that the Del key is being pushed down. EMULATING THE KEYBOARD Every time your emulator drops out of executing 6502 code to check on interrupts, update timers and draw a new scanline, you must also check if a key has been pushed on the PC keyboard. If it has, then the emulator should translate this key into a VIC key press and store it in some sort of buffer structured as an 8x8 matrix representing the state of the VIC keyboard. Ideally this matrix will only retain this value until the key is released. (In practice this is hard to achieve because of the difficulty in detect