git @ Cat's Eye Technologies JaC64 / master com / dreamfabric / jac64 / Keyboard.java
master

Tree @master (Download .tar.gz)

Keyboard.java @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
 * encoding: UTF-8
 * This file is a part of JaC64 - a Java C64 Emulator
 * Main Developer: Joakim Eriksson (Dreamfabric.com)
 * Contact: joakime@sics.se
 * Web: http://www.dreamfabric.com/c64
 * ---------------------------------------------------
 *
 *
 */

package com.dreamfabric.jac64;
import com.dreamfabric.c64utils.*;
import java.awt.event.*;
import java.util.*;

/**
 * The keyboard and joystick emulation
 *
 *
 * Created: Wed Jun 14 17:39:15 2006
 *
 * @author Joakim Eriksson, joakime@sics.se
 * @version 1.0
 */
public class Keyboard {

  public static final int IO_OFFSET = CPU.IO_OFFSET;

  public static final int AUTO_SHIFT = 1024;
  public static final int AUTO_CTRL = 2048;
  public static final int AUTO_COMMODORE = 4096;
  public static final int MIN_AUTO = AUTO_SHIFT;

  public static final int STICK_UPDOWN = 1 | 2;
  public static final int STICK_LEFTRIGHT = 4 | 8;

  public static final int STICK_UP = 1;
  public static final int STICK_DOWN = 2;
  public static final int STICK_LEFT = 4;
  public static final int STICK_RIGHT = 8;
  public static final int STICK_FIRE = 16;

  public static final char[][] KEYMAPS = new char[][] {
    { 's','v',  // Keymap for language "sv"
      ';', 'ö',
      '\'','ä',
      '[','å',
      '`', '§',
      '\\', (char) 222,
      '/', '-',
      ']', (char) 135,
      '-', (char) KeyEvent.VK_PLUS,
      '=', (char) 129
    },
    { 'd','e',  // Keymap for language "de"
      ';', 'ö',
      '\'','ä',
      '[','ü',
      '`', '^',
      '\\', (char) 520,
      '/', '-',
      ']', (char) 521,
      '-', (char) 223,
      '=', (char) 129
    }
  };


  boolean extendedKeyboardEmulation = false;
  boolean stickExits = false;

  int joystick1 = 255;
  int bval = 255;
  boolean lastUp = false;
  boolean lastLeft = false;

  private int joy1 = 0xff;
  private int joy2 = 0xff;

  int stick = IO_OFFSET + 0xdc00; // stick 0;
  private int keyPressed = 0;
  private int lastKey;

  private C64Script c64script;
  private ArrayList hotkeyScript;

  int keyrow[] = new int[8];
  int keycol[] = new int[8];

  // Some keys have high value (+ => 521 for example)
  int keytable[][] = new int[1024][3];

  // For handling autoshifts...
  int keyShift = 0;

  // Needs to add some "special keys" - that are handled slightly
  // different => key up / key right = key dwn + shift & key left + shift
  // + some ordinary keys that is hard to get on a normal non c64 keyboard
  // needs to be mapped (+-/*?, etc)
  // The C64 key-table - key to map, then row + col + auto "shift" / flag
  int keytableDef[][] = new int[][] {
      {'A', 1, 2, 0 },
      {'B', 3, 4, 0 },
      {'C', 2, 4, 0 },
      {'D', 2, 2, 0 },
      {'E', 1, 6, 0 },
      {'F', 2, 5, 0 },
      {'G', 3, 2, 0 },
      {'H', 3, 5, 0 },
      {'I', 4, 1, 0 },
      {'J', 4, 2, 0 },
      {'K', 4, 5, 0 },
      {'L', 5, 2, 0 },
      {'M', 4, 4, 0 },
      {'N', 4, 7, 0 },
      {'O', 4, 6, 0 },
      {'P', 5, 1, 0 },
      {'Q', 7, 6, 0 },
      {'R', 2, 1, 0 },
      {'S', 1, 5, 0 },
      {'T', 2, 6, 0 },
      {'U', 3, 6, 0 },
      {'V', 3, 7, 0 },
      {'W', 1, 1, 0 },
      {'X', 2, 7, 0 },
      {'Y', 3, 1, 0 },
      {'Z', 1, 4, 0 },
      {'0', 4, 3, 0 },
      {'1', 7, 0, 0 },
      {'2', 7, 3, 0 },
      {'3', 1, 0, 0 },
      {'4', 1, 3, 0 },
      {'5', 2, 0, 0 },
      {'6', 2, 3, 0 },
      {'7', 3, 0, 0 },
      {'8', 3, 3, 0 },
      {'9', 4, 0, 0 },
      {'\n', 0, 1, 0 },
      {KeyEvent.VK_ENTER, 0, 1, 0 },
      {' ', 7, 4, 0 },
      {',', 5, 7, 0 },
      {'.', 5, 4, 0 },
      // = on the \ pos.
      {'\\', 6, 5, 0},
      // This is actually colon on the C64 ;-)
      {';', 5, 5, 0 },
      {'-', 5, 0, 0 },     // Actually + on the C64... (- => +)
      //    {'-', 5, 3, 0 },
      {'=', 5, 3, 0 },     // Actually - on the C64... (= => -)
      {'`', 7, 1, 0 },
      // This is semi-colon on the C64
      {'\'', 6, 2, 0 },
      {KeyEvent.VK_SUBTRACT, 5, 3, 0 },
      {'[', 5, 6, 0 },
      {']', 6, 1, 0 },
      {KeyEvent.VK_ESCAPE, 7, 1, 0},
      {'/', 6, 7, 0 },
      {KeyEvent.VK_DIVIDE, 6, 7, 0},
      // will be mapped to an auto shift /
      {'?', 6, 7, AUTO_SHIFT},
      {KeyEvent.VK_DELETE, 0, 0, 0 },
      {KeyEvent.VK_BACK_SPACE, 0, 0, 0 },
      // LEFT SHIFT
      {KeyEvent.VK_SHIFT, 1, 7, 0 },
      // RIGHT SHIFT
      {KeyEvent.VK_CAPS_LOCK, 6, 4, 0 },

      // Break
      {19, 7, 7, 0 },
      {KeyEvent.VK_ESCAPE, 7, 7, 0 },
      // Enter + CTRL
      {'\r', 0, 1, 0 },
      // Commodore key on the control keys
      {KeyEvent.VK_CONTROL, 7, 5, KeyEvent.KEY_LOCATION_LEFT},
      {KeyEvent.VK_ENTER, 0, 1, 0 },
      // DOWN     &  RIGHT
      {KeyEvent.VK_DOWN, 0, 7, 0 },
      {KeyEvent.VK_UP, 0, 7, AUTO_SHIFT },
      {KeyEvent.VK_RIGHT, 0, 2, 0 },
      {KeyEvent.VK_LEFT, 0, 2, AUTO_SHIFT },
      // Function keys
      {KeyEvent.VK_F1, 0, 4, 0 },
      {KeyEvent.VK_F3, 0, 5, 0 },
      {KeyEvent.VK_F5, 0, 6, 0 },
      {KeyEvent.VK_F7, 0, 3, 0 },
      {KeyEvent.VK_HOME, 6, 3, 0 },
      {KeyEvent.VK_END, 6, 6, 0},
      {KeyEvent.VK_INSERT, 6, 0, 0},
      {KeyEvent.VK_TAB, 7, 2, 0}
  };

  private int restoreKey = KeyEvent.VK_PAGE_UP;

  public static final int USER_UP = 0;
  public static final int USER_DOWN = 1;
  public static final int USER_LEFT = 2;
  public static final int USER_RIGHT = 3;
  public static final int USER_FIRE = 4;

  private static final int[] ARROWS = new int[] {
    KeyEvent.VK_UP, 0, KeyEvent.VK_DOWN, 0,
    KeyEvent.VK_LEFT, 0, KeyEvent.VK_RIGHT, 0,
    KeyEvent.VK_CONTROL, KeyEvent.KEY_LOCATION_RIGHT};

  private int[] userDefinedStick = ARROWS;

  private CIA cia;
  private C64Screen screen;

  private void remap(char key, char newkey) {
    for (int i = 0, n = keytableDef.length; i < n; i++) {
      if (keytableDef[i][0] == key) {
        keytableDef[i][0] = newkey;
        System.out.println("Remapped: " + (char) key + " => " + (char) newkey +
            "  key " + (int) key + " => " + (int) newkey);
        return;
      }
    }
  }


  private boolean doMap(String ltarget) {
    for (int i = 0, n = KEYMAPS.length; i < n; i++) {
      char[] map = KEYMAPS[i];
      String lang = "" + map[0] + map[1];
      System.out.println("Checking map for: " + lang);
      if (ltarget.equals(lang)) {
        System.out.println("Found map - mapping...");
        for (int j = 2, m = map.length; j < m; j += 2) {
          remap(map[j], map[j + 1]);
        }
        // Finished!
        return true;
      }
    }
    return false;
  }

  /**
   * Creates a new <code>Keyboard</code> instance.
   *
   */
  public Keyboard(C64Screen screen, CIA cia, int[] memory) {

    Locale locale = Locale.getDefault();
    System.out.println("Locale: " + locale);

    String lang = locale.getLanguage();
    if (!doMap(lang)) {
      System.out.println("Could not find map for keyboard: " + lang);
    }

    this.cia = cia;
    this.screen = screen;

    for (int i = 0; i < keytable.length; i++) {
      keytable[i][0] = -1;
    }

    for (int i = 0; i < keytableDef.length; i++) {
      int key = keytableDef[i][0];
      keytable[key][0] = keytableDef[i][1];
      keytable[key][1] = keytableDef[i][2];
      keytable[key][2] = keytableDef[i][3];
    }

    reset();
  }

  public void registerHotKey(int key, int modflag, String script, Object o) {
    if (hotkeyScript == null) {
      c64script = new C64Script();
      hotkeyScript = new ArrayList();
    }
    hotkeyScript.add(new Object[] {script, o, new int[] {key, modflag}});
  }

  public void setStick(boolean one) {
    if (one)
      stick = IO_OFFSET + 0xdc01;
    else
      stick = IO_OFFSET + 0xdc00;
  }

  private int getUserStick(int key, int location) {
    if (userDefinedStick != null) {
      for (int i = 0, n = userDefinedStick.length; i < n; i += 2) {
        if (key == userDefinedStick[i]) {
          int cmpLoc = userDefinedStick[i + 1];
          if (cmpLoc == 0 || location == userDefinedStick[i + 1]) {
//          System.out.println("Joystick emulation trigger");
            return i / 2;
          }
        }
      }
    }
    return -1;
  }

  // # Chars in char buffer - 0xc6 - need not to be set here...
  public void keyPressed(KeyEvent event) {
//  System.out.println("Key pressed..." + event.getKeyCode());
//  System.out.println("Char pressed..." + event.getKeyChar() + " = " +
//  (int) event.getKeyChar());
    char chr = event.getKeyChar();
    int key = event.getKeyCode();
    int location = event.getKeyLocation();

    if (hotkeyScript != null) {
      int mod = event.getModifiersEx();
//    System.out.println("HotKey: " + key + " mod: " + mod);

      for (int i = 0, n = hotkeyScript.size(); i < n; i++) {
        Object[] hk = (Object[]) hotkeyScript.get(i);
        int[] keys = (int[]) hk[2];
//      System.out.println("Cmp " + keys[0] + " mod: " + keys[1]);
        if (keys[0] == key && ((mod & keys[1]) == keys[1])) {
          c64script.interpretCall((String) hk[0], hk[1]);
        }
      }
    }

    if (key == 0) {
      System.out.println("KeyZero ???");
      key = (int) Character.toLowerCase(chr);
    }

    if (key == restoreKey) {
      screen.restoreKey(true);
    }

    if (key != lastKey) {
      keyPressed++;
    }
    lastKey = key;

    int usr = getUserStick(key, location);
    if (extendedKeyboardEmulation) usr = -1;
    if (usr == -1) {
      usr = getNormalStick(key);
    }

    switch (usr) {
    // CHANGE TO STOCK1LEFT = true; (or similar!)
    case USER_UP:
      joystick1 = joystick1 & (255 - STICK_UP);
      lastUp = true;
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    case USER_DOWN:
      joystick1 = joystick1 & (255 - STICK_DOWN);
      lastUp = false;
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    case USER_LEFT:
      joystick1 = joystick1 & (255 - STICK_LEFT);
      lastLeft = true;
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    case USER_RIGHT:
      joystick1 = joystick1 & (255 - STICK_RIGHT);
      lastLeft = false;
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    case USER_FIRE:
      joystick1 = joystick1 & (255 - STICK_FIRE);
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    }

    // Change the joystick with F10!
    switch (key) {
    case KeyEvent.VK_F10:
      setStick(stick == IO_OFFSET + 0xdc00);
      break;
    case KeyEvent.VK_F9:
      System.out.println("F9");
      extendedKeyboardEmulation = !extendedKeyboardEmulation;
      stickExits = !extendedKeyboardEmulation;
      break;
    case KeyEvent.VK_F11:
      // Toggle colour set?
      break;
    }

    if (extendedKeyboardEmulation) {
      // Auto shift handling!
      if ((keytable[key][2] & AUTO_SHIFT) != 0) {
        keyShift++;
        if (keyShift == 1) {
          // AutoShift on if first shift! - otherwize not?!
          handleKeyPress(KeyEvent.VK_SHIFT, location);
        }
        // Also increase shift is shift is pressed
      } else if (key == KeyEvent.VK_SHIFT) {
        keyShift++;
      }
      handleKeyPress(key, location);

    } else {
      // No autoshift keys!!!
      if (keytable[key][2] < MIN_AUTO) {
        handleKeyPress(key, location);
      }
    }
  }

  public void keyReleased(KeyEvent event)
  {
    int key = event.getKeyCode();
    char chr = event.getKeyChar();
    int location = event.getKeyLocation();

    if (key == 0) {
      System.out.println("KeyZero???");
      key = (int) Character.toLowerCase(chr);
    }

    if (key == restoreKey) {
      screen.restoreKey(false);
    }


    keyPressed--;
    lastKey = 0;
    if (keyPressed < 0)
      keyPressed = 0;

    int usr = getUserStick(key, location);
    if (usr == -1)
      usr = getNormalStick(key);

    switch (usr) {
    case USER_UP:
      joystick1 = joystick1 | STICK_UP;
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    case USER_DOWN:
      joystick1 = joystick1 | STICK_DOWN;
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    case USER_LEFT:
      joystick1 = joystick1 | STICK_LEFT;
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    case USER_RIGHT:
      joystick1 = joystick1 | STICK_RIGHT;
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    case USER_FIRE:
      joystick1 = joystick1 | STICK_FIRE;
      updateJoystick();
      if (stickExits) {
        return;
      }
      break;
    }

    if (extendedKeyboardEmulation) {
      // Auto shift handling!
      if ((keytable[key][2] & AUTO_SHIFT) != 0) {
        keyShift--;
        if (keyShift == 0) {
          // AutoShift on if first shift! - otherwize not?!
          handleKeyRelease(KeyEvent.VK_SHIFT, location);
        }
        // Also decrease shift is shift is released
      } else if (key == KeyEvent.VK_SHIFT) {
        keyShift--;
        // Should not remove shift if autoshift was on?
        if (keyShift > 0)
          return;
      }
      handleKeyRelease(key, location);
    } else {
      if (keytable[key][2] < MIN_AUTO) {
        handleKeyRelease(key, location);
      }
    }
  }

  private int getNormalStick(int key) {
    switch (key) {
    case KeyEvent.VK_NUMPAD8 :
      return USER_UP;
    case KeyEvent.VK_NUMPAD2 :
    case KeyEvent.VK_NUMPAD5 :
      return USER_DOWN;
    case KeyEvent.VK_NUMPAD4 :
      return USER_LEFT;
    case KeyEvent.VK_NUMPAD6 :
      return USER_RIGHT;
    case KeyEvent.VK_NUMPAD0:
      return USER_FIRE;
    }
    return -1;
  }

  private void handleKeyPress(int key, int location) {
    int maprow = keytable[key][0];
    int mapcol = keytable[key][1];

    if (maprow != -1 && mapcol != -1) {
//    System.out.println("Row: " + maprow + " Col: " + mapcol);
      keyrow[maprow] = keyrow[maprow] & (255 - (1 << keytable[key][1]));
      keycol[mapcol] = keycol[mapcol] & (255 - (1 << keytable[key][0]));
    }
  }

  private void handleKeyRelease(int key, int location) {
    int maprow = keytable[key][0];
    int mapcol = keytable[key][1];

    if (maprow != -1 && mapcol != -1) {
      keyrow[maprow] = keyrow[maprow] | (1 << keytable[key][1]);
      keycol[mapcol] = keycol[mapcol] | (1 << keytable[key][0]);
    }
  }

  int readDC00(int pc) {
    // 0xdc00 - why does this not work when it is "correctly" implemented?
    // DC00 a'la VICE which seems to work on StarPost!?
    int val = 0xff;
    int tmp = pc < 0x10000 ? joy1 : 0xff;
    int mask = (cia.prb | ~(cia.ddrb)) & tmp;


    for (int m = 0x1, i = 0; i < 8; m <<= 1, i++) {
      if ((mask & m) == 0)
        val &= keycol[i];
    }

    tmp = pc < 0x10000 ? joy2 : 0xff;
//    System.out.println("Read keyboard via dc00: => " + ((val & (cia.pra | ~(cia.ddra))) & tmp));
    return (val & (cia.pra | ~(cia.ddra))) & tmp;
  }

  void setButtonval(int bval) {
    this.bval = bval;
    updateJoystick();
  }

  int readDC01(int pc) {
    // A'la VICE!
    //    System.out.println("PC: " + Integer.toHexString(pc));

    int val = 0xff;
    int tmp = pc < 0x10000 ? (joy2 & bval) : 0xff;
    int mask = (cia.pra | ~(cia.ddra)) & tmp;

    for (int m = 0x1, i = 0; i < 8; m <<= 1, i++) {
      if ((mask & m) == 0)
        val &= keyrow[i];
    }
    tmp = pc < 0x10000 ? (joy1 & bval) : 0xff;
//    System.out.println("Read keyboard via dc00: " + val + " => " + ((val & (cia.prb | ~(cia.ddrb))) & tmp));

    return (val & (cia.prb | ~(cia.ddrb))) & tmp;
  }
  
  /* Updates $DC00/$DC01 based on recent joystick activity. */
  private void updateJoystick() {

    int jst = joystick1 & bval;
    // both up and down?
    if ((jst & STICK_UPDOWN) == 0) {
      jst = (jst | STICK_UPDOWN) & (0xff - (lastUp ? STICK_UP : STICK_DOWN));
    }

    // both left and rigth?
    if ((jst & STICK_LEFTRIGHT) == 0) {
      jst = (jst | STICK_LEFTRIGHT) & (0xff - (lastLeft ? STICK_LEFT : STICK_RIGHT));
    }

    joy2 = stick == 0xdc00 + IO_OFFSET ? 0xff : jst;
    joy1 = stick == 0xdc00 + IO_OFFSET ? jst : 0xff;
  }

  public void reset() {
    lastKey = 0;
    keyPressed = 0;
    keyShift = 0;
    joystick1 = 255;

    for (int i = 0; i < 8; i++) {
      keyrow[i] = 255;
      keycol[i] = 255;
    }

  }
}