PPI.java
     1: //========================================================================================
     2: //  PPI.java
     3: //    en:8255 PPI -- It emulates joystick ports.
     4: //    ja:8255 PPI -- ジョイスティックポートをエミュレートします。
     5: //  Copyright (C) 2003-2023 Makoto Kamada
     6: //
     7: //  This file is part of the XEiJ (X68000 Emulator in Java).
     8: //  You can use, modify and redistribute the XEiJ if the conditions are met.
     9: //  Read the XEiJ License for more details.
    10: //  https://stdkmd.net/xeij/
    11: //========================================================================================
    12: 
    13: package xeij;
    14: 
    15: import java.awt.*;
    16: import java.awt.event.*;
    17: import java.io.*;
    18: import java.util.*;
    19: import javax.swing.*;
    20: import javax.swing.event.*;
    21: 
    22: import com.fazecast.jSerialComm.*;
    23: 
    24: public class PPI {
    25: 
    26:   //ポート
    27:   //  0x00e9a000-0x00e9bfffは下位3ビットだけデコードされる
    28:   //  偶数アドレスをバイトサイズでリード/ライトするとバスエラーになる
    29:   //  偶数アドレスをワードサイズでリードすると上位バイトは0xffになる
    30:   //
    31:   //  0x00e9a001  PPIポートA  ジョイスティック1
    32:   //         7        6        5        4        3        2        1        0
    33:   //    +--------+--------+--------+--------+--------+--------+--------+--------+
    34:   //    |   PA7  |   PA6  |   PA5  |   PA4  |   PA3  |   PA2  |   PA1  |   PA0  |
    35:   //    |    1   |   JS7  |   JS6  |    1   |   JS4  |   JS3  |   JS2  |   JS1  |
    36:   //    |        |    B   |    A   |        |   →   |   ←   |   ↓   |   ↑   |
    37:   //    +--------+--------+--------+--------+--------+--------+--------+--------+
    38:   //    bit7    PA7  1
    39:   //    bit6    PA6  JS7入力。0=Bが押されている
    40:   //    bit5    PA5  JS6入力。0=Aが押されている
    41:   //    bit4    PA4  1
    42:   //    bit3    PA3  JS4入力。0=→が押されている
    43:   //    bit2    PA2  JS3入力。0=←が押されている
    44:   //    bit1    PA1  JS2入力。0=↓が押されている
    45:   //    bit0    PA0  JS1入力。0=↑が押されている
    46:   //
    47:   //  0x00e9a003  PPIポートB  ジョイスティック2
    48:   //         7        6        5        4        3        2        1        0
    49:   //    +--------+--------+--------+--------+--------+--------+--------+--------+
    50:   //    |   PB7  |   PB6  |   PB5  |   PB4  |   PB3  |   PB2  |   PB1  |   PB0  |
    51:   //    |    1   |   JT7  |   JT6  |    1   |   JT4  |   JT3  |   JT2  |   JT1  |
    52:   //    |        |    B   |    A   |        |   →   |   ←   |   ↓   |   ↑   |
    53:   //    +--------+--------+--------+--------+--------+--------+--------+--------+
    54:   //    bit7    PB7  1
    55:   //    bit6    PB6  JT7入力。0=Bが押されている
    56:   //    bit5    PB5  JT6入力。0=Aが押されている
    57:   //    bit4    PB4  1
    58:   //    bit3    PB3  JT4入力。0=→が押されている
    59:   //    bit2    PB2  JT3入力。0=←が押されている
    60:   //    bit1    PB1  JT2入力。0=↓が押されている
    61:   //    bit0    PB0  JT1入力。0=↑が押されている
    62:   //
    63:   //  0x00e9a005  PPIポートC  ADPCMコントロール。初期値は0x0b
    64:   //         7        6        5        4        3        2        1        0
    65:   //    +--------+--------+--------+--------+--------+--------+--------+--------+
    66:   //    |   PC7  |   PC6  |   PC5  |   PC4  |   PC3  |   PC2  |   PC1  |   PC0  |
    67:   //    |   JS7  |   JS6  |   JT8  |   JS8  |      RATIO      |  LEFT  |  RIGHT |
    68:   //    +--------+--------+--------+--------+--------+--------+--------+--------+
    69:   //    bit7    PC7    JS7出力(負論理)
    70:   //    bit6    PC6    JS6出力(負論理)
    71:   //    bit5    PC5    JT8出力
    72:   //    bit4    PC4    JS8出力
    73:   //    bit3-2  PC3-2  ADPCM分周比。00=1/1024,01=1/768,10=1/512,11=inhibited
    74:   //    bit1    PC1    ADPCM出力LEFT。0=出力する,1=出力しない
    75:   //    bit0    PC0    ADPCM出力RIGHT。0=出力する,1=出力しない
    76:   //
    77:   //  0x00e9a007  PPIコントロール
    78:   //    bit7=0  ポートCで出力に設定されているビットの操作
    79:   //      bit3-1  ビット番号
    80:   //      bit0    設定値
    81:   //    bit7=1  モードの設定。0x92に固定
    82:   //      bit6-5  グループA(ポートAとポートCの上位)のモード(0=モード0,1=モード1,2/3=モード2)。モード0に固定
    83:   //      bit4    ポートAの方向(0=出力,1=入力)。入力に固定
    84:   //      bit3    ポートCの上位の方向(0=出力,1=入力)。出力に固定
    85:   //      bit2    グループB(ポートBとポートCの下位)のモード(0=モード0,1=モード1)。モード0に固定
    86:   //      bit1    ポートBの方向(0=出力,1=入力)。入力に固定
    87:   //      bit0    ポートCの下位の方向(0=出力,1=入力)。出力に固定
    88:   //
    89:   //  ボタンのマスク
    90:   //    上と下または左と右のキーが同時に押された場合は両方キャンセルする
    91:   //    SELECTボタンは上下同時押し、RUNボタンは左右同時押しに割り当てられる
    92:   //
    93:   //  bit4とbit7は入力できない
    94:   //    8255の足がプルアップされているので実機ではどうすることもできない(外付けの回路でどうにかなるものではない)
    95:   //    エミュレータで入力できるようにするのは簡単だか対応しているソフトは存在しないだろう
    96:   //
    97:   //  参考
    98:   //    電脳倶楽部67号  B/MDPAD/M6PAD_AN.DOC
    99:   //    電脳倶楽部77号  B/6B/T_EXPAD.DOC
   100:   //
   101:   public static final int PPI_PORT_A  = 0x00e9a001;   //PPIポートA
   102:   public static final int PPI_PORT_B  = 0x00e9a003;   //PPIポートB
   103:   public static final int PPI_PORT_C  = 0x00e9a005;   //PPIポートC
   104:   public static final int PPI_CONTROL = 0x00e9a007;   //PPIコントロール
   105: 
   106:   //ジョイスティック
   107:   public static Joystick[] ppiJoysticks;
   108:   public static Joystick ppiJoystick1;
   109:   public static Joystick ppiJoystick2;
   110: 
   111:   //モード
   112:   public static boolean ppiJoyKey;  //true=キーボードの一部をジョイスティックとみなす
   113:   public static boolean ppiJoyAuto;  //true=ポートが繰り返し読み出されている間だけ有効
   114:   public static boolean ppiJoyBlock;  //true=ジョイスティック操作として処理されたキー入力データを取り除く
   115: 
   116:   //じょいぽーとU君
   117:   public static final boolean PPI_UKUN_ON = true;  //じょいぽーとU君。false=使わない,true=使う
   118:   public static final boolean PPI_UKUN_DEBUG = false;  //デバッグメッセージ。false=出力しない,true=出力する
   119:   //  接続
   120:   public static final int PPI_UKUN_VID = 0x04d8;  //ベンダーID
   121:   public static final int PPI_UKUN_PID = 0xe6b3;  //プロダクトID
   122:   public static boolean ppiUkunRequestedConnection;  //要求された接続。false=接続しない,true=接続する
   123:   public static boolean ppiUkunCurrentConnection;  //現在の接続。false=接続していない,true=接続している
   124:   //  シリアルポート
   125:   public static OldSerialPort ppiUkunOldSerialPort;  //(!jSerialComm)シリアルポート
   126:   public static String ppiUkunPortName;  //ポート名。"COM4"など
   127:   public static OldSerialPort.SerialInputStream ppiUkunOldInputStream;  //(!jSerialComm)入力ストリーム
   128:   public static OldSerialPort.SerialOutputStream ppiUkunOldOutputStream;  //(!jSerialComm)出力ストリーム
   129:   //  モード
   130:   public static final int PPI_UKUN_NOTIFY_A   = 0b0000_0000_0011_0001;  //Notify(A)モード
   131:   public static final int PPI_UKUN_NOTIFY_B   = 0b0000_0001_0011_0001;  //Notify(B)モード
   132:   public static final int PPI_UKUN_COMMAND    = 0b0000_0000_0011_0010;  //Commandモード
   133:   public static final int PPI_UKUN_TRANSITION = 0b0000_0000_0000_0000;  //モード遷移中。I/Oポートのリードは前回の値が返り、ライトは無視される
   134:   public static volatile int ppiUkunRequestedMode;  //要求されたモード。PPI_UKUN_NOTIFY_AまたはPPI_UKUN_COMMANDまたはPPI_UKUN_NOTIFY_B
   135:   public static volatile int ppiUkunCurrentMode;  //現在のモード。PPI_UKUN_NOTIFY_AまたはPPI_UKUN_COMMANDまたはPPI_UKUN_NOTIFY_BまたはPPI_UKUN_TRANSITION
   136:   //  データ
   137:   public static volatile int ppiUkunPortA;  //ポートAのデータ。0b1..1_....
   138:   public static volatile int ppiUkunPortB;  //ポートBのデータ。0b1..1_....
   139:   public static volatile int ppiUkunPortC;  //ポートCのデータ。0b...._0000
   140:   //  受信スレッド
   141:   public static volatile boolean ppiUkunSending;  //false=送信中ではない,true=送信中
   142:   public static volatile Thread ppiUkunThread;  //(!jSerialComm)受信スレッド
   143:   //  ナノタイマー
   144:   public static final boolean PPI_UKUN_NANO = false;  //I/Oポートのアクセス時間を計測する
   145:   public static boolean ppiUkunNanoMeasuring;  //true=計測中
   146:   public static long[] ppiUkunNanoBestArray;  //最短アクセス時間(ns)
   147:   public static long[] ppiUkunNanoWorstArray;  //最長アクセス時間(ns)
   148:   public static long[] ppiUkunNanoTotalArray;  //合計アクセス時間(ns)
   149:   public static long[] ppiUkunNanoCountArray;  //アクセス回数
   150:   //  ウェイト
   151:   public static final long PPI_UKUN_WAIT = XEiJ.TMR_FREQ / (1000000 / 100);  //I/Oポートのアクセスに追加するウェイト(XEiJ.TMR_FREQ単位)。100us
   152:   //  出力間隔調整
   153:   public static final boolean PPI_UKUN_INTERVAL = true;
   154:   public static final long PPI_UKUN_INTERVAL_LIMIT = XEiJ.TMR_FREQ / (1000000L / 500L);  //調整する間隔のずれの上限。500us
   155:   public static boolean ppiUkunIntervalOn;  //true=出力間隔調整を行う
   156:   public static long ppiUkunLastVirtualTime;  //前回のXEiJ.mpuClockTime
   157:   public static long ppiUkunLastRealTime;  //前回のSystem.nanoTime()
   158:   //  メニュー
   159:   public static JCheckBox ppiUkunConnectCheckbox;  //接続チェックボックス
   160:   public static JRadioButton ppiUkunNotifyAModeRadioButton;  //Notify(A)モードラジオボタン
   161:   public static JRadioButton ppiUkunNotifyBModeRadioButton;  //Notify(B)モードラジオボタン
   162:   public static JRadioButton ppiUkunCommandModeRadioButton;  //Commandモードラジオボタン
   163:   //  jSerialComm
   164:   public static final boolean PPI_UKUN_JSERIALCOMM = true;  //false=xeijwin.dllのみ,true=jSerialCommを併用
   165:   public static final String PPI_UKUN_DESCRIPTION = "8255 emulator";  //プロダクト概要
   166:   public static boolean ppiUkunJSerialCommOn;  //じょいぽーとU君でjSerialCommを使う。Windows以外は常にtrue
   167:   public static SerialPort ppiUkunSerialPort;  //シリアルポート
   168:   public static UkunDataListener ppiUkunDataListener;  //シリアルポートデータリスナー
   169:   public static JCheckBox ppiUkunJSerialCommCheckbox;  //jSerialCommチェックボックス。接続していないときだけ有効
   170: 
   171: 
   172:   //ポートの値
   173:   public static int ppiPortCData;
   174: 
   175:   //自動切り替え
   176:   //  PPIのポートが参照されてから一定時間だけJOYKEYを有効にする
   177:   public static final long PPI_CONTINUOUS_ACCESS_SPAN = XEiJ.TMR_FREQ / 10;  //自動切り替えの有効期間(TMR_FREQ単位)。1/10秒
   178:   public static long ppiLastAccessTime;  //最後にPPIのポートがアクセスされた時刻
   179: 
   180:   //XInput
   181:   public static final boolean PPI_XINPUT_ON = true;  //false=使わない,true=使う
   182:   public static boolean ppiXInputOn;  //false=使わない,true=使う
   183:   public static XInput ppiXInput;  //XInput。nullでなければ動作中
   184:   public static int ppiXInputLastButtons;
   185: 
   186:   //ウインドウ
   187:   public static JFrame ppiFrame;
   188: 
   189:   public static JScrollPane ppiConfigurationScrollPane;
   190: 
   191:   //ppiInit ()
   192:   //  PPIを初期化する
   193:   public static void ppiInit () {
   194:     ppiJoyKey = Settings.sgsGetOnOff ("joykey");
   195:     ppiJoyAuto = Settings.sgsGetOnOff ("joyauto");
   196:     ppiJoyBlock = Settings.sgsGetOnOff ("joyblock");
   197: 
   198:     ppiJoysticks = new Joystick[] {
   199:       new DummyPad (),
   200:       new Normal2ButtonPad (1),
   201:       new Normal2ButtonPad (2),
   202:       new MegaDrive3ButtonPad (1),
   203:       new MegaDrive3ButtonPad (2),
   204:       new MegaDrive6ButtonPad (1),
   205:       new MegaDrive6ButtonPad (2),
   206:       new CyberStickAnalog (1),
   207:       new CyberStickAnalog (2),
   208:       new CyberStickDigital (1),
   209:       new CyberStickDigital (2),
   210:       new Shiromadokun (1),
   211:       new Shiromadokun (2),
   212:     };
   213: 
   214:     String id1 = Settings.sgsGetString ("joystick1");  //ジョイスティックポート1に接続するデバイス
   215:     ppiJoystick1 = ppiJoysticks[0];  //DummyPad
   216:     for (Joystick joystick : ppiJoysticks) {
   217:       if (joystick.getId ().equalsIgnoreCase (id1)) {
   218:         ppiJoystick1 = joystick;
   219:       }
   220:     }
   221: 
   222:     String id2 = Settings.sgsGetString ("joystick2");  //ジョイスティックポート2に接続するデバイス
   223:     ppiJoystick2 = ppiJoysticks[0];  //DummyPad
   224:     for (Joystick joystick : ppiJoysticks) {
   225:       if (joystick.getId ().equalsIgnoreCase (id2)) {
   226:         ppiJoystick2 = joystick;
   227:       }
   228:     }
   229: 
   230:     if (PPI_UKUN_ON) {
   231:       ppiUkunRequestedConnection = (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded) && Settings.sgsGetOnOff ("joyportukun");
   232:       ppiUkunCurrentConnection = false;
   233:       ppiUkunPortName = null;
   234:       ppiUkunOldSerialPort = null;
   235:       ppiUkunOldInputStream = null;
   236:       ppiUkunOldOutputStream = null;
   237:       String paramUkunmode = Settings.sgsGetString ("ukunmode");
   238:       ppiUkunRequestedMode = (paramUkunmode.equalsIgnoreCase ("notifya") ? PPI_UKUN_NOTIFY_A :
   239:                               paramUkunmode.equalsIgnoreCase ("notifyb") ? PPI_UKUN_NOTIFY_B :
   240:                               paramUkunmode.equalsIgnoreCase ("command") ? PPI_UKUN_COMMAND :
   241:                               PPI_UKUN_NOTIFY_A);
   242:       ppiUkunCurrentMode = ppiUkunRequestedMode;
   243:       if (PPI_UKUN_DEBUG) {
   244:         System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   245:       }
   246:       ppiUkunPortA = 0b1111_1111;  //0b1..1_....
   247:       ppiUkunPortB = 0b1111_1111;  //0b1..1_....
   248:       ppiUkunPortC = 0b0000_0000;  //0b...._0000
   249:       ppiUkunSending = false;
   250:       ppiUkunThread = null;
   251:       if (PPI_UKUN_NANO) {
   252:         ppiUkunNanoMeasuring = false;
   253:         ppiUkunNanoBestArray = new long[8];
   254:         ppiUkunNanoWorstArray = new long[8];
   255:         ppiUkunNanoTotalArray = new long[8];
   256:         ppiUkunNanoCountArray = new long[8];
   257:       }
   258:       if (PPI_UKUN_INTERVAL) {
   259:         ppiUkunIntervalOn = Settings.sgsGetOnOff ("ukuninterval");
   260:         ppiUkunLastVirtualTime = XEiJ.mpuClockTime;
   261:         ppiUkunLastRealTime = System.nanoTime ();
   262:       }
   263:       if (PPI_UKUN_JSERIALCOMM) {
   264:         ppiUkunJSerialCommOn = !XEiJ.prgIsWindows || Settings.sgsGetOnOff ("ukunjsc");  //Windows以外は常にtrue
   265:         ppiUkunSerialPort = null;
   266:         ppiUkunDataListener = null;
   267:       }  //if PPI_UKUN_JSERIALCOMM
   268:     }  //if PPI_UKUN_ON
   269: 
   270:     if (PPI_XINPUT_ON) {
   271:       ppiXInputOn = XEiJ.prgWindllLoaded && Settings.sgsGetOnOff ("xinput");
   272:       if (ppiXInputOn) {
   273:         ppiXInputStart ();
   274:       }
   275:       ppiXInputLastButtons = 0;
   276:     }
   277: 
   278:     ppiReset ();
   279:   }  //ppiInit
   280: 
   281:   //ppiTini ()
   282:   //  後始末
   283:   public static void ppiTini () {
   284:     Settings.sgsPutOnOff ("joykey", ppiJoyKey);
   285:     Settings.sgsPutOnOff ("joyauto", ppiJoyAuto);
   286:     Settings.sgsPutOnOff ("joyblock", ppiJoyBlock);
   287:     for (Joystick joystick : ppiJoysticks) {
   288:       joystick.tini ();
   289:     }
   290:     Settings.sgsPutString ("joystick1", ppiJoystick1.getId ());
   291:     Settings.sgsPutString ("joystick2", ppiJoystick2.getId ());
   292:     if (PPI_UKUN_ON) {
   293:       Settings.sgsPutOnOff ("joyportukun", ppiUkunRequestedConnection);
   294:       Settings.sgsPutString ("ukunmode",
   295:                              ppiUkunRequestedMode == PPI_UKUN_NOTIFY_A ? "notifya" :
   296:                              ppiUkunRequestedMode == PPI_UKUN_NOTIFY_B ? "notifyb" :
   297:                              ppiUkunRequestedMode == PPI_UKUN_COMMAND ? "command" :
   298:                              "notifya");
   299:       if (PPI_UKUN_INTERVAL) {
   300:         Settings.sgsPutOnOff ("ukuninterval", ppiUkunIntervalOn);
   301:       }
   302:       if (PPI_UKUN_JSERIALCOMM) {
   303:         Settings.sgsPutOnOff ("ukunjsc", !XEiJ.prgIsWindows ? false : ppiUkunJSerialCommOn);  //Windows以外はデフォルトのfalseを保存する。次回再び強制的にtrueになる
   304:       }
   305:       ppiUkunDisconnect ();
   306:     }
   307:     //XInput
   308:     if (PPI_XINPUT_ON) {
   309:       Settings.sgsPutOnOff ("xinput", ppiXInputOn);
   310:       if (ppiXInputOn) {
   311:         ppiXInputOn = false;
   312:         ppiXInputEnd ();
   313:       }
   314:     }
   315:   }  //ppiTini
   316: 
   317:   //ppiReset ()
   318:   //  リセット
   319:   public static void ppiReset () {
   320:     ppiPortCData = 0;
   321:     ppiLastAccessTime = 0L;
   322:   }
   323: 
   324: 
   325:   public static void ppiXInputStart () {
   326:     if (ppiXInput == null) {
   327:       System.out.println (Multilingual.mlnJapanese ?
   328:                           "XInput のポーリングを開始します" :
   329:                           "Starts polling XInput");
   330:       ppiXInput = new XInput ();
   331:     }
   332:   }
   333: 
   334:   public static void ppiXInputEnd () {
   335:     if (ppiXInput != null) {
   336:       System.out.println (Multilingual.mlnJapanese ?
   337:                           "XInput のポーリングを終了します" :
   338:                           "Ends polling XInput");
   339:       ppiXInput.end ();
   340:       ppiXInput = null;
   341:     }
   342:   }
   343: 
   344: 
   345:   //ppiUkunConnect ()
   346:   //  接続する
   347:   //  MPU動作中はI/Oポートのアクセスと衝突させないためコアのスレッドで呼び出すこと
   348:   //  接続はここで完結するので遷移中の状態は考慮しない
   349:   public static void ppiUkunConnect () {
   350:     if (PPI_UKUN_ON && (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded)) {
   351:       if (PPI_UKUN_DEBUG) {
   352:         System.out.println ("ppiUkunConnect start");
   353:       }
   354:       ppiUkunRequestedConnection = true;
   355:       if (ppiUkunCurrentConnection) {  //接続している
   356:         ppiUkunAdjustMenu ();
   357:         return;
   358:       }
   359:       if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
   360:         //ジョイポートU君を探して開く
   361:         if (ppiUkunSerialPort == null) {
   362:           for (SerialPort serialPort : SerialPort.getCommPorts ()) {
   363:             if (serialPort.getPortDescription ().startsWith (PPI_UKUN_DESCRIPTION) &&  //見つかった
   364:                 serialPort.openPort ()) {  //開けた
   365:               ppiUkunSerialPort = serialPort;
   366:               break;
   367:             }
   368:           }
   369:         }
   370:         if (ppiUkunSerialPort == null) {  //開けなかった
   371:           System.out.printf (Multilingual.mlnJapanese ?
   372:                              "じょいぽーと U 君 (%s) のシリアルポートが見つかりません\n" :
   373:                              "Serial port for JoyPortUkun (%s) not found\n",
   374:                              PPI_UKUN_DESCRIPTION);
   375:           ppiUkunRequestedConnection = false;
   376:           //ppiUkunCurrentConnection = false;
   377:           ppiUkunAdjustMenu ();
   378:           return;
   379:         }
   380:         ppiUkunPortName = ppiUkunSerialPort.getSystemPortName ();
   381:         ppiUkunSerialPort.setComPortParameters (480000000, 8, SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY);
   382:         ppiUkunSerialPort.setComPortTimeouts (SerialPort.TIMEOUT_WRITE_BLOCKING | SerialPort.TIMEOUT_READ_BLOCKING, 0, 0);
   383:       } else {  //!jSerialComm
   384:         //ポートを開く
   385:         if (ppiUkunOldSerialPort == null) {
   386:           try {
   387:             ppiUkunOldSerialPort = new OldSerialPort (PPI_UKUN_VID, PPI_UKUN_PID);
   388:           } catch (IOException ioe) {
   389:             System.out.printf (Multilingual.mlnJapanese ?
   390:                                "じょいぽーと U 君 (VID=%04X, PID=%04X) の COM ポートが見つかりません\n" :
   391:                                "COM port for JoyPortUkun (VID=%04X, PID=%04X) not found\n",
   392:                                PPI_UKUN_VID, PPI_UKUN_PID);
   393:             ppiUkunRequestedConnection = false;
   394:             //ppiUkunCurrentConnection = false;
   395:             ppiUkunAdjustMenu ();
   396:             return;
   397:           }
   398:         }
   399:         try {
   400:           ppiUkunPortName = ppiUkunOldSerialPort.getPortName ();
   401:           ppiUkunOldSerialPort.speed ("480000000 b8 pn s1 none");
   402:           ppiUkunOldInputStream = ppiUkunOldSerialPort.getInputStream ();
   403:           ppiUkunOldOutputStream = ppiUkunOldSerialPort.getOutputStream ();
   404:         } catch (IOException ioe) {
   405:           ioe.printStackTrace ();
   406:         }
   407:       }  //if jSerialComm/!
   408:       //接続完了
   409:       ppiUkunCurrentMode = PPI_UKUN_TRANSITION;
   410:       if (PPI_UKUN_DEBUG) {
   411:         System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   412:       }
   413:       ppiUkunCurrentConnection = true;  //接続している
   414:       ppiUkunAdjustMenu ();
   415:       if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
   416:         System.out.printf (Multilingual.mlnJapanese ?
   417:                            "じょいぽーと U 君 (%s, %s) に接続しました\n" :
   418:                            "Connected to JoyPortUkun (%s, %s)\n",
   419:                            ppiUkunPortName, ppiUkunSerialPort.getPortDescription ());
   420:         //シリアルポートデータリスナーを登録する
   421:         if (ppiUkunDataListener == null) {
   422:           ppiUkunSending = false;
   423:           ppiUkunDataListener = new UkunDataListener ();
   424:           ppiUkunSerialPort.addDataListener (ppiUkunDataListener);
   425:         }
   426:         //要求されたモードを送信する
   427:         ppiUkunSerialPort.writeBytes (new byte[] { (byte) ppiUkunRequestedMode }, 1);
   428:       } else {  //!jSerialComm
   429:         System.out.printf (Multilingual.mlnJapanese ?
   430:                            "じょいぽーと U 君 (%s, VID=%04X, PID=%04X) に接続しました\n" :
   431:                            "Connected to JoyPortUkun (%s, VID=%04X, PID=%04X)\n",
   432:                            ppiUkunPortName, PPI_UKUN_VID, PPI_UKUN_PID);
   433:         //受信スレッドを開始する
   434:         if (ppiUkunThread == null) {
   435:           ppiUkunSending = false;
   436:           ppiUkunThread = new UkunThread ();
   437:           ppiUkunThread.start ();
   438:         }
   439:         //要求されたモードを送信する
   440:         try {
   441:           ppiUkunOldOutputStream.write (ppiUkunRequestedMode);
   442:         } catch (IOException ioe) {
   443:           ioe.printStackTrace ();
   444:         }
   445:       }  //if jSerialComm/!
   446:       if (PPI_UKUN_DEBUG) {
   447:         System.out.println ("ppiUkunConnect end");
   448:       }
   449:     }
   450:   }  //ppiUkunConnect
   451: 
   452:   //ppiUkunDisconnect ()
   453:   //  切断する
   454:   //  MPU動作中はI/Oポートのアクセスと衝突させないためコアのスレッドで呼び出すこと
   455:   //  切断はここで完結するので遷移中の状態は考慮しない
   456:   public static void ppiUkunDisconnect () {
   457:     if (PPI_UKUN_ON) {
   458:       if (PPI_UKUN_DEBUG) {
   459:         System.out.println ("ppiUkunDisconnect start");
   460:       }
   461:       ppiUkunRequestedConnection = false;
   462:       if (!ppiUkunCurrentConnection) {  //接続していない
   463:         ppiUkunAdjustMenu ();
   464:         return;
   465:       }
   466:       //切断開始
   467:       ppiUkunCurrentMode = PPI_UKUN_TRANSITION;
   468:       if (PPI_UKUN_DEBUG) {
   469:         System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   470:       }
   471:       ppiUkunCurrentConnection = false;  //接続していない
   472:       ppiUkunAdjustMenu ();
   473:       if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
   474:         if (ppiUkunSerialPort != null) {
   475:           //シリアルポートデータリスナーを解除する
   476:           ppiUkunSerialPort.removeDataListener ();
   477:           ppiUkunDataListener = null;
   478:           //ポートを閉じる
   479:           ppiUkunSerialPort.closePort ();
   480:           ppiUkunSerialPort = null;
   481:         }
   482:       } else {  //!jSerialComm
   483:         //ポートを閉じる
   484:         if (ppiUkunOldSerialPort != null) {
   485:           try {
   486:             ppiUkunOldSerialPort.close ();
   487:           } catch (IOException ioe) {
   488:             ioe.printStackTrace ();
   489:           }
   490:           ppiUkunOldSerialPort = null;
   491:           ppiUkunOldInputStream = null;
   492:           ppiUkunOldOutputStream = null;
   493:         }
   494:         //受信スレッドが終了するまで待つ
   495:         while (ppiUkunThread != null) {
   496:         }
   497:       }  //if jSerialComm/!
   498:       ppiUkunSending = false;
   499:       //切断完了
   500:       ppiUkunCurrentMode = ppiUkunRequestedMode;
   501:       if (PPI_UKUN_DEBUG) {
   502:         System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   503:       }
   504:       ppiUkunAdjustMenu ();
   505:       System.out.printf (Multilingual.mlnJapanese ?
   506:                          "じょいぽーと U 君 (%s) を切り離しました\n" :
   507:                          "Disconnected JoyPortUkun (%s)\n",
   508:                          ppiUkunPortName);
   509:       ppiUkunPortName = null;
   510:       if (PPI_UKUN_DEBUG) {
   511:         System.out.println ("ppiUkunDisconnect end");
   512:       }
   513:     }
   514:   }  //ppiUkunDisconnect
   515: 
   516:   //ppiUkunNotifyAMode ()
   517:   //  Notify(A)モード
   518:   public static void ppiUkunNotifyAMode () {
   519:     if (PPI_UKUN_ON) {
   520:       if (PPI_UKUN_DEBUG) {
   521:         System.out.println ("ppiUkunNotifyAMode start");
   522:       }
   523:       if (!ppiUkunRequestedConnection &&
   524:           !ppiUkunCurrentConnection) {  //接続していない
   525:         //Notify(A)モード
   526:         ppiUkunRequestedMode = PPI_UKUN_NOTIFY_A;
   527:         ppiUkunCurrentMode = PPI_UKUN_NOTIFY_A;
   528:         if (PPI_UKUN_DEBUG) {
   529:           System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   530:         }
   531:         ppiUkunAdjustMenu ();
   532:       } else if (ppiUkunRequestedConnection &&
   533:                  ppiUkunCurrentConnection &&  //接続している
   534:                  ((ppiUkunRequestedMode == PPI_UKUN_COMMAND &&
   535:                    ppiUkunCurrentMode == PPI_UKUN_COMMAND) ||  //Commandモード
   536:                   (ppiUkunRequestedMode == PPI_UKUN_NOTIFY_B &&
   537:                    ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B))) {  //Notify(B)モード
   538:         //遷移中
   539:         ppiUkunRequestedMode = PPI_UKUN_NOTIFY_A;
   540:         ppiUkunCurrentMode = PPI_UKUN_TRANSITION;
   541:         if (PPI_UKUN_DEBUG) {
   542:           System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   543:         }
   544:         ppiUkunAdjustMenu ();
   545:         if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
   546:           //シリアルポートデータリスナーを登録する
   547:           if (ppiUkunDataListener == null) {
   548:             ppiUkunSending = false;
   549:             ppiUkunDataListener = new UkunDataListener ();
   550:             ppiUkunSerialPort.addDataListener (ppiUkunDataListener);
   551:           }
   552:           //Notify(A)モードを送信する
   553:           ppiUkunSerialPort.writeBytes (new byte[] { (byte) PPI_UKUN_NOTIFY_A }, 1);
   554:         } else {  //!jSerialComm
   555:           //受信スレッドを開始する
   556:           if (ppiUkunThread == null) {
   557:             ppiUkunSending = false;
   558:             ppiUkunThread = new UkunThread ();
   559:             ppiUkunThread.start ();
   560:           }
   561:           //Notify(A)モードを送信する
   562:           try {
   563:             ppiUkunOldOutputStream.write (PPI_UKUN_NOTIFY_A & 0xff);
   564:           } catch (IOException ioe) {
   565:             ioe.printStackTrace ();
   566:           }
   567:         }  //if jSerialComm/!
   568:       }
   569:       if (PPI_UKUN_DEBUG) {
   570:         System.out.println ("ppiUkunNotifyAMode end");
   571:       }
   572:     }
   573:   }  //ppiUkunNotifyAMode
   574: 
   575:   //ppiUkunNotifyBMode ()
   576:   //  Notify(B)モード
   577:   public static void ppiUkunNotifyBMode () {
   578:     if (PPI_UKUN_ON) {
   579:       if (PPI_UKUN_DEBUG) {
   580:         System.out.println ("ppiUkunNotifyBMode start");
   581:       }
   582:       if (!ppiUkunRequestedConnection &&
   583:           !ppiUkunCurrentConnection) {  //接続していない
   584:         //Notify(B)モード
   585:         ppiUkunRequestedMode = PPI_UKUN_NOTIFY_B;
   586:         ppiUkunCurrentMode = PPI_UKUN_NOTIFY_B;
   587:         if (PPI_UKUN_DEBUG) {
   588:           System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   589:         }
   590:         ppiUkunAdjustMenu ();
   591:       } else if (ppiUkunRequestedConnection &&
   592:                  ppiUkunCurrentConnection &&  //接続している
   593:                  ((ppiUkunRequestedMode == PPI_UKUN_NOTIFY_A &&
   594:                    ppiUkunCurrentMode == PPI_UKUN_NOTIFY_A) ||  //Notify(A)モード
   595:                   (ppiUkunRequestedMode == PPI_UKUN_COMMAND &&
   596:                    ppiUkunCurrentMode == PPI_UKUN_COMMAND))) {  //Commandモード
   597:         //遷移中
   598:         ppiUkunRequestedMode = PPI_UKUN_NOTIFY_B;
   599:         ppiUkunCurrentMode = PPI_UKUN_TRANSITION;
   600:         if (PPI_UKUN_DEBUG) {
   601:           System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   602:         }
   603:         ppiUkunAdjustMenu ();
   604:         if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
   605:           //シリアルポートデータリスナーを登録する
   606:           if (ppiUkunDataListener == null) {
   607:             ppiUkunSending = false;
   608:             ppiUkunDataListener = new UkunDataListener ();
   609:             ppiUkunSerialPort.addDataListener (ppiUkunDataListener);
   610:           }
   611:           //Notify(B)モードを送信する
   612:           ppiUkunSerialPort.writeBytes (new byte[] { (byte) PPI_UKUN_NOTIFY_B }, 1);
   613:         } else {  //!jSerialComm
   614:           //受信スレッドを開始する
   615:           if (ppiUkunThread == null) {
   616:             ppiUkunSending = false;
   617:             ppiUkunThread = new UkunThread ();
   618:             ppiUkunThread.start ();
   619:           }
   620:           //Notify(B)モードを送信する
   621:           try {
   622:             ppiUkunOldOutputStream.write (PPI_UKUN_NOTIFY_B & 0xff);
   623:           } catch (IOException ioe) {
   624:             ioe.printStackTrace ();
   625:           }
   626:         }  //if jSerialComm/!
   627:       }
   628:       if (PPI_UKUN_DEBUG) {
   629:         System.out.println ("ppiUkunNotifyBMode end");
   630:       }
   631:     }
   632:   }  //ppiUkunNotifyBMode
   633: 
   634:   //ppiUkunCommandMode ()
   635:   //  Commandモード
   636:   public static void ppiUkunCommandMode () {
   637:     if (PPI_UKUN_ON) {
   638:       if (PPI_UKUN_DEBUG) {
   639:         System.out.println ("ppiUkunCommandMode start");
   640:       }
   641:       if (!ppiUkunRequestedConnection &&
   642:           !ppiUkunCurrentConnection) {  //接続していない
   643:         //Commandモード
   644:         ppiUkunRequestedMode = PPI_UKUN_COMMAND;
   645:         ppiUkunCurrentMode = PPI_UKUN_COMMAND;
   646:         if (PPI_UKUN_DEBUG) {
   647:           System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   648:         }
   649:         ppiUkunAdjustMenu ();
   650:       } else if (ppiUkunRequestedConnection &&
   651:                  ppiUkunCurrentConnection &&  //接続している
   652:                  ((ppiUkunRequestedMode == PPI_UKUN_NOTIFY_A &&
   653:                    ppiUkunCurrentMode == PPI_UKUN_NOTIFY_A) ||  //Notify(A)モード
   654:                   (ppiUkunRequestedMode == PPI_UKUN_NOTIFY_B &&
   655:                    ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B))) {  //Notify(B)モード
   656:         //遷移中
   657:         ppiUkunRequestedMode = PPI_UKUN_COMMAND;
   658:         ppiUkunCurrentMode = PPI_UKUN_TRANSITION;
   659:         if (PPI_UKUN_DEBUG) {
   660:           System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   661:         }
   662:         ppiUkunAdjustMenu ();
   663:         if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
   664:           //シリアルポートデータリスナーを登録する
   665:           if (ppiUkunDataListener == null) {
   666:             ppiUkunSending = false;
   667:             ppiUkunDataListener = new UkunDataListener ();
   668:             ppiUkunSerialPort.addDataListener (ppiUkunDataListener);
   669:           }
   670:           //Commandモードを送信する
   671:           ppiUkunSerialPort.writeBytes (new byte[] { (byte) PPI_UKUN_COMMAND }, 1);
   672:         } else {  //!jSerialComm
   673:           //受信スレッドを開始する
   674:           if (ppiUkunThread == null) {
   675:             ppiUkunSending = false;
   676:             ppiUkunThread = new UkunThread ();
   677:             ppiUkunThread.start ();
   678:           }
   679:           //Commandモードを送信する
   680:           try {
   681:             ppiUkunOldOutputStream.write (PPI_UKUN_COMMAND);
   682:           } catch (IOException ioe) {
   683:             ioe.printStackTrace ();
   684:           }
   685:         }  //if jSerialComm/!
   686:       }
   687:       if (PPI_UKUN_DEBUG) {
   688:         System.out.println ("ppiUkunCommandMode end");
   689:       }
   690:     }
   691:   }  //ppiUkunCommandMode
   692: 
   693:   //ppiUkunMenuConnect ()
   694:   //  メニューからの接続
   695:   public static void ppiUkunMenuConnect () {
   696:     if (PPI_UKUN_ON) {
   697:       XEiJ.tmrTimer.schedule (new TimerTask () {
   698:         @Override public void run () {
   699:           ppiUkunConnect ();
   700:         }
   701:       }, 0L);
   702:     }
   703:   }  //ppiUkunMenuConnect
   704: 
   705:   //ppiUkunMenuDisconnect ()
   706:   //  メニューからの切断
   707:   public static void ppiUkunMenuDisconnect () {
   708:     if (PPI_UKUN_ON) {
   709:       XEiJ.tmrTimer.schedule (new TimerTask () {
   710:         @Override public void run () {
   711:           ppiUkunDisconnect ();
   712:         }
   713:       }, 0L);
   714:     }
   715:   }  //ppiUkunMenuDisconnect
   716: 
   717:   //ppiUkunMenuNotifyAMode ()
   718:   //  メニューからのNotify(A)モード
   719:   public static void ppiUkunMenuNotifyAMode () {
   720:     if (PPI_UKUN_ON) {
   721:       XEiJ.tmrTimer.schedule (new TimerTask () {
   722:         @Override public void run () {
   723:           ppiUkunNotifyAMode ();
   724:         }
   725:       }, 0L);
   726:     }
   727:   }  //ppiUkunMenuNotifyAMode
   728: 
   729:   //ppiUkunMenuNotifyBMode ()
   730:   //  メニューからのNotify(B)モード
   731:   public static void ppiUkunMenuNotifyBMode () {
   732:     if (PPI_UKUN_ON) {
   733:       XEiJ.tmrTimer.schedule (new TimerTask () {
   734:         @Override public void run () {
   735:           ppiUkunNotifyBMode ();
   736:         }
   737:       }, 0L);
   738:     }
   739:   }  //ppiUkunMenuNotifyBMode
   740: 
   741:   //ppiUkunMenuCommandMode ()
   742:   //  メニューからのCommandモード
   743:   public static void ppiUkunMenuCommandMode () {
   744:     if (PPI_UKUN_ON) {
   745:       XEiJ.tmrTimer.schedule (new TimerTask () {
   746:         @Override public void run () {
   747:           ppiUkunCommandMode ();
   748:         }
   749:       }, 0L);
   750:     }
   751:   }  //ppiUkunMenuCommandMode
   752: 
   753:   //ppiUkunAdjustMenu ()
   754:   //  メニューを調整する
   755:   public static void ppiUkunAdjustMenu () {
   756:     if (PPI_UKUN_ON) {
   757:       if (ppiUkunConnectCheckbox != null) {
   758:         //接続チェックボックスと要求された接続を合わせる
   759:         if (ppiUkunConnectCheckbox.isSelected () != ppiUkunRequestedConnection) {
   760:           ppiUkunConnectCheckbox.setSelected (ppiUkunRequestedConnection);
   761:         }
   762:         //  遷移中は操作禁止
   763:         if (ppiUkunConnectCheckbox.isEnabled () != (ppiUkunRequestedConnection == ppiUkunCurrentConnection)) {
   764:           ppiUkunConnectCheckbox.setEnabled (ppiUkunRequestedConnection == ppiUkunCurrentConnection);
   765:         }
   766:       }
   767:       if (ppiUkunNotifyAModeRadioButton != null) {
   768:         //Notify(A)モードラジオボタンと要求されたモードを合わせる
   769:         if (ppiUkunNotifyAModeRadioButton.isSelected () != (ppiUkunRequestedMode == PPI_UKUN_NOTIFY_A)) {
   770:           ppiUkunNotifyAModeRadioButton.setSelected (ppiUkunRequestedMode == PPI_UKUN_NOTIFY_A);
   771:         }
   772:         //  接続中かつ遷移中は操作禁止
   773:         if (ppiUkunNotifyAModeRadioButton.isEnabled () != !(ppiUkunCurrentConnection && ppiUkunRequestedMode != ppiUkunCurrentMode)) {
   774:           ppiUkunNotifyAModeRadioButton.setEnabled (!(ppiUkunCurrentConnection && ppiUkunRequestedMode != ppiUkunCurrentMode));
   775:         }
   776:       }
   777:       if (ppiUkunNotifyBModeRadioButton != null) {
   778:         //Notify(B)モードラジオボタンと要求されたモードを合わせる
   779:         if (ppiUkunNotifyBModeRadioButton.isSelected () != (ppiUkunRequestedMode == PPI_UKUN_NOTIFY_B)) {
   780:           ppiUkunNotifyBModeRadioButton.setSelected (ppiUkunRequestedMode == PPI_UKUN_NOTIFY_B);
   781:         }
   782:         //  接続中かつ遷移中は操作禁止
   783:         if (ppiUkunNotifyBModeRadioButton.isEnabled () != !(ppiUkunCurrentConnection && ppiUkunRequestedMode != ppiUkunCurrentMode)) {
   784:           ppiUkunNotifyBModeRadioButton.setEnabled (!(ppiUkunCurrentConnection && ppiUkunRequestedMode != ppiUkunCurrentMode));
   785:         }
   786:       }
   787:       if (ppiUkunCommandModeRadioButton != null) {
   788:         //Commandモードラジオボタンと要求されたモードを合わせる
   789:         if (ppiUkunCommandModeRadioButton.isSelected () != (ppiUkunRequestedMode == PPI_UKUN_COMMAND)) {
   790:           ppiUkunCommandModeRadioButton.setSelected (ppiUkunRequestedMode == PPI_UKUN_COMMAND);
   791:         }
   792:         //  接続中かつ遷移中は操作禁止
   793:         if (ppiUkunCommandModeRadioButton.isEnabled () != !(ppiUkunCurrentConnection && ppiUkunRequestedMode != ppiUkunCurrentMode)) {
   794:           ppiUkunCommandModeRadioButton.setEnabled (!(ppiUkunCurrentConnection && ppiUkunRequestedMode != ppiUkunCurrentMode));
   795:         }
   796:       }
   797:       if (PPI_UKUN_JSERIALCOMM) {
   798:         if (ppiUkunJSerialCommCheckbox != null) {
   799:           //jSerialCommチェックボックスと現在の状態を合わせる
   800:           if (ppiUkunJSerialCommCheckbox.isSelected () != ppiUkunJSerialCommOn) {
   801:             ppiUkunJSerialCommCheckbox.setSelected (ppiUkunJSerialCommOn);
   802:           }
   803:           //  Windows以外と接続中は操作禁止
   804:           if (ppiUkunJSerialCommCheckbox.isEnabled () != (XEiJ.prgIsWindows && !ppiUkunCurrentConnection)) {
   805:             ppiUkunJSerialCommCheckbox.setEnabled (XEiJ.prgIsWindows && !ppiUkunCurrentConnection);
   806:           }
   807:         }
   808:       }
   809:     }
   810:   }  //ppiUkunAdjustMenu
   811: 
   812:   //class UkunDataListener
   813:   //  シリアルポートデータリスナー
   814:   public static class UkunDataListener implements SerialPortDataListener {
   815:     @Override public int getListeningEvents () {
   816:       return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
   817:     }
   818:     @Override public void serialEvent (SerialPortEvent spe) {
   819:       if (spe.getEventType () != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
   820:         return;
   821:       }
   822:       byte[] bytes = new byte[1];
   823:       while (ppiUkunSerialPort.bytesAvailable () != 0 &&
   824:              ppiUkunSerialPort.readBytes (bytes, 1) != 0) {  //1バイトずつ受信する
   825:         int data = bytes[0] & 0xff;
   826:         if (PPI_UKUN_DEBUG) {
   827:           System.out.printf ("UkunDataListener received 0x%02x\n", data);
   828:         }
   829:         //変化通知
   830:         //  0b1..0_....  portA  0b1..1_....
   831:         //  0b1..1_....  portB  0b1..1_....
   832:         //  0b0000_....  portC  0b...._0000
   833:         //完了通知
   834:         //  0b0011_1111  completed
   835:         //モード通知
   836:         //  0b0011_0001  Notify mode
   837:         //  0b0011_0010  Command mode
   838:         if ((data & 0b1001_0000) == 0b1000_0000) {  //0b1..0_.... portA
   839:           ppiUkunPortA = 0b1001_0000 | data;  //0b1..1_....
   840:         } else if ((data & 0b1001_0000) == 0b1001_0000) {  //0b1..1_.... portB
   841:           ppiUkunPortB = data;  //0b1..1_....
   842:         } else if (data <= 0b0000_1111) {  //0b0000_.... portC
   843:           ppiUkunPortC = data << 4;  //0b...._0000
   844:         } else if (data == 0b0011_1111) {  //completed
   845:           ppiUkunSending = false;
   846:         } else if (data == 0b0011_0001) {  //Notify mode
   847:           if (ppiUkunRequestedMode == PPI_UKUN_NOTIFY_B) {
   848:             System.out.printf (Multilingual.mlnJapanese ?
   849:                                "じょいぽーと U 君 (%s) は Notify(B) モードで初期化されました\n" :
   850:                                "JoyPortUkun (%s) has been initialized in Notify(B) mode\n",
   851:                                ppiUkunPortName);
   852:             //ppiUkunRequestedMode = PPI_UKUN_NOTIFY_B;
   853:             ppiUkunCurrentMode = PPI_UKUN_NOTIFY_B;
   854:           } else {
   855:             System.out.printf (Multilingual.mlnJapanese ?
   856:                                "じょいぽーと U 君 (%s) は Notify(A) モードで初期化されました\n" :
   857:                                "JoyPortUkun (%s) has been initialized in Notify(A) mode\n",
   858:                                ppiUkunPortName);
   859:             ppiUkunRequestedMode = PPI_UKUN_NOTIFY_A;
   860:             ppiUkunCurrentMode = PPI_UKUN_NOTIFY_A;
   861:           }
   862:           if (PPI_UKUN_DEBUG) {
   863:             System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   864:           }
   865:           ppiUkunAdjustMenu ();
   866:         } else if (data == 0b0011_0010) {  //Command mode
   867:           ppiUkunRequestedMode = PPI_UKUN_COMMAND;
   868:           ppiUkunCurrentMode = PPI_UKUN_COMMAND;
   869:           if (PPI_UKUN_DEBUG) {
   870:             System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   871:           }
   872:           ppiUkunAdjustMenu ();
   873:           System.out.printf (Multilingual.mlnJapanese ?
   874:                              "じょいぽーと U 君 (%s) は Command モードで初期化されました\n" :
   875:                              "JoyPortUkun (%s) has been initialized in Command mode\n",
   876:                              ppiUkunPortName);
   877:           //シリアルポートデータリスナーを解除する
   878:           ppiUkunSerialPort.removeDataListener ();
   879:           ppiUkunDataListener = null;
   880:           break;
   881:         } else {
   882:           System.out.printf ("UkunDataListener received unknown data 0x%02x\n", data);
   883:           //シリアルポートデータリスナーを解除する
   884:           ppiUkunSerialPort.removeDataListener ();
   885:           ppiUkunDataListener = null;
   886:           break;
   887:         }
   888:       }  //while
   889:     }  //serialEvent
   890:   }  //class UkunDataListener
   891: 
   892:   //class UkunThread
   893:   //  受信スレッド
   894:   public static class UkunThread extends Thread {
   895:     @Override public void run () {
   896:       if (PPI_UKUN_DEBUG) {
   897:         System.out.println ("UkunThread start");
   898:       }
   899:       try {
   900:         while (ppiUkunCurrentConnection) {
   901:           //受信する
   902:           //  受信するかキャンセルされるまでブロックする
   903:           int data = ppiUkunOldInputStream.read ();
   904:           if (PPI_UKUN_DEBUG) {
   905:             System.out.printf ("UkunThread received 0x%02x\n", data);
   906:           }
   907:           //変化通知
   908:           //  0b1..0_....  portA  0b1..1_....
   909:           //  0b1..1_....  portB  0b1..1_....
   910:           //  0b0000_....  portC  0b...._0000
   911:           //完了通知
   912:           //  0b0011_1111  completed
   913:           //モード通知
   914:           //  0b0011_0001  Notify mode
   915:           //  0b0011_0010  Command mode
   916:           if ((data & 0b1001_0000) == 0b1000_0000) {  //0b1..0_.... portA
   917:             ppiUkunPortA = 0b1001_0000 | data;  //0b1..1_....
   918:           } else if ((data & 0b1001_0000) == 0b1001_0000) {  //0b1..1_.... portB
   919:             ppiUkunPortB = data;  //0b1..1_....
   920:           } else if (data <= 0b0000_1111) {  //0b0000_.... portC
   921:             ppiUkunPortC = data << 4;  //0b...._0000
   922:           } else if (data == 0b0011_1111) {  //completed
   923:             ppiUkunSending = false;
   924:           } else if (data == 0b0011_0001) {  //Notify mode
   925:             if (ppiUkunRequestedMode == PPI_UKUN_NOTIFY_B) {
   926:               System.out.printf (Multilingual.mlnJapanese ?
   927:                                  "じょいぽーと U 君 (%s) は Notify(B) モードで初期化されました\n" :
   928:                                  "JoyPortUkun (%s) has been initialized in Notify(B) mode\n",
   929:                                  ppiUkunPortName);
   930:               //ppiUkunRequestedMode = PPI_UKUN_NOTIFY_B;
   931:               ppiUkunCurrentMode = PPI_UKUN_NOTIFY_B;
   932:             } else {
   933:               System.out.printf (Multilingual.mlnJapanese ?
   934:                                  "じょいぽーと U 君 (%s) は Notify(A) モードで初期化されました\n" :
   935:                                  "JoyPortUkun (%s) has been initialized in Notify(A) mode\n",
   936:                                  ppiUkunPortName);
   937:               ppiUkunRequestedMode = PPI_UKUN_NOTIFY_A;
   938:               ppiUkunCurrentMode = PPI_UKUN_NOTIFY_A;
   939:             }
   940:             if (PPI_UKUN_DEBUG) {
   941:               System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   942:             }
   943:             ppiUkunAdjustMenu ();
   944:           } else if (data == 0b0011_0010) {  //Command mode
   945:             ppiUkunRequestedMode = PPI_UKUN_COMMAND;
   946:             ppiUkunCurrentMode = PPI_UKUN_COMMAND;
   947:             if (PPI_UKUN_DEBUG) {
   948:               System.out.printf ("UkunCurrentMode 0x%04x\n", ppiUkunCurrentMode);
   949:             }
   950:             ppiUkunAdjustMenu ();
   951:             System.out.printf (Multilingual.mlnJapanese ?
   952:                                "じょいぽーと U 君 (%s) は Command モードで初期化されました\n" :
   953:                                "JoyPortUkun (%s) has been initialized in Command mode\n",
   954:                                ppiUkunPortName);
   955:             //受信スレッドを終了する
   956:             break;
   957:           } else if (data < 0) {  //キャンセルされた
   958:             //受信スレッドを終了する
   959:             break;
   960:           } else {
   961:             System.out.printf ("UkunThread received unknown data 0x%02x\n", data);
   962:             //受信スレッドを終了する
   963:             break;
   964:           }
   965:         }  //while
   966:       } catch (IOException ioe) {
   967:         ioe.printStackTrace ();
   968:       }
   969:       ppiUkunThread = null;
   970:       if (PPI_UKUN_DEBUG) {
   971:         System.out.println ("UkunThread end");
   972:       }
   973:     }
   974:   }  //class UkunThread
   975: 
   976:   //ppiUkunNanoStart ()
   977:   //  計測開始
   978:   public static void ppiUkunNanoStart () {
   979:     if (PPI_UKUN_ON && PPI_UKUN_NANO) {
   980:       XEiJ.tmrTimer.schedule (new TimerTask () {
   981:         @Override public void run () {
   982:           if (!ppiUkunNanoMeasuring) {
   983:             ppiUkunNanoMeasuring = true;
   984:             Arrays.fill (ppiUkunNanoBestArray, Long.MAX_VALUE);
   985:             Arrays.fill (ppiUkunNanoWorstArray, 0L);
   986:             Arrays.fill (ppiUkunNanoTotalArray, 0L);
   987:             Arrays.fill (ppiUkunNanoCountArray, 0L);
   988:           }
   989:         }
   990:       }, 0L);
   991:     }
   992:   }
   993: 
   994:   //ppiUkunNanoEnd ()
   995:   //  計測終了
   996:   public static void ppiUkunNanoEnd () {
   997:     if (PPI_UKUN_ON && PPI_UKUN_NANO) {
   998:       XEiJ.tmrTimer.schedule (new TimerTask () {
   999:         @Override public void run () {
  1000:           if (ppiUkunNanoMeasuring) {
  1001:             ppiUkunNanoMeasuring = false;
  1002:             System.out.println ("Nano timer");
  1003:             for (int i = 0; i < 8; i++) {
  1004:               long best = ppiUkunNanoBestArray[i];
  1005:               long worst = ppiUkunNanoWorstArray[i];
  1006:               long total = ppiUkunNanoTotalArray[i];
  1007:               long count = ppiUkunNanoCountArray[i];
  1008:               if (count != 0L) {
  1009:                 System.out.printf ("  %s %s best=%d(ns) average=%d(ns) worst=%d(ns) total=%d(ns) count=%d\n",
  1010:                                    i < 2 ? "PORT A" : i < 4 ? "PORT B" : i < 6 ? "PORT C" : "CONTROL WORD",
  1011:                                    (i & 1) == 0 ? "READ" : "WRITE",
  1012:                                    best,
  1013:                                    total / count,
  1014:                                    worst,
  1015:                                    total,
  1016:                                    count);
  1017:               }
  1018:             }
  1019:           }
  1020:         }
  1021:       }, 0L);
  1022:     }
  1023:   }
  1024: 
  1025: 
  1026:   //ppiStart ()
  1027:   public static void ppiStart () {
  1028:     if (RestorableFrame.rfmGetOpened (Settings.SGS_PPI_FRAME_KEY)) {
  1029:       ppiOpen ();
  1030:     }
  1031:     if (PPI_UKUN_ON) {
  1032:       if (ppiUkunRequestedConnection) {
  1033:         ppiUkunConnect ();
  1034:       }
  1035:     }
  1036:   }
  1037: 
  1038:   //ppiOpen ()
  1039:   //  ジョイスティックの設定ウインドウを開く
  1040:   public static void ppiOpen () {
  1041:     if (ppiFrame == null) {
  1042:       ppiMakeFrame ();
  1043:     }
  1044:     ppiFrame.setVisible (true);
  1045:   }
  1046: 
  1047:   //ppiMakeFrame ()
  1048:   //  ジョイスティックポートの設定ウインドウを作る
  1049:   //  ここでは開かない
  1050:   public static void ppiMakeFrame () {
  1051: 
  1052:     //アクションリスナー
  1053:     ActionListener actionListener = new ActionListener () {
  1054:       @Override public void actionPerformed (ActionEvent ae) {
  1055:         Object source = ae.getSource ();
  1056:         String command = ae.getActionCommand ();
  1057:         switch (command) {
  1058:         case "Consider part of keyboard as joystick":
  1059:           //キーボードの一部をジョイスティックとみなす
  1060:           ppiJoyKey = ((JCheckBox) ae.getSource ()).isSelected ();
  1061:           break;
  1062:         case "Enabled only while the port is read repeatedly":
  1063:           //ポートが繰り返し読み出されている間だけ有効
  1064:           ppiJoyAuto = ((JCheckBox) ae.getSource ()).isSelected ();
  1065:           break;
  1066:         case "Remove key input data processed as a joystick operation":
  1067:           //ジョイスティック操作として処理されたキー入力データを取り除く
  1068:           ppiJoyBlock = ((JCheckBox) ae.getSource ()).isSelected ();
  1069:           break;
  1070:           //
  1071:         case "JoyPortUkun":  //じょいぽーと U 君
  1072:           if (PPI_UKUN_ON) {
  1073:             if (((JCheckBox) ae.getSource ()).isSelected ()) {  //接続
  1074:               ppiUkunMenuConnect ();
  1075:             } else {  //切断
  1076:               ppiUkunMenuDisconnect ();
  1077:             }
  1078:           }
  1079:           break;
  1080:         case "Notify(A) mode":  //Notify(A) モード
  1081:           if (PPI_UKUN_ON) {
  1082:             ppiUkunMenuNotifyAMode ();
  1083:           }
  1084:           break;
  1085:         case "Notify(B) mode":  //Notify(B) モード
  1086:           if (PPI_UKUN_ON) {
  1087:             ppiUkunMenuNotifyBMode ();
  1088:           }
  1089:           break;
  1090:         case "Command mode":  //Command モード
  1091:           if (PPI_UKUN_ON) {
  1092:             ppiUkunMenuCommandMode ();
  1093:           }
  1094:           break;
  1095:         case "jSerialComm":
  1096:           if (PPI_UKUN_ON && PPI_UKUN_JSERIALCOMM && !ppiUkunCurrentConnection) {  //接続中は操作禁止
  1097:             ppiUkunJSerialCommOn = !XEiJ.prgIsWindows || ((JCheckBox) ae.getSource ()).isSelected ();  //Windows以外は常にtrue
  1098:             ppiUkunAdjustMenu ();
  1099:           }
  1100:           break;
  1101:         case "Output interval adjustment":  //出力間隔調整
  1102:           if (PPI_UKUN_ON && PPI_UKUN_INTERVAL) {
  1103:             boolean on = ((JCheckBox) ae.getSource ()).isSelected ();
  1104:             if (ppiUkunIntervalOn != on) {
  1105:               if (on) {
  1106:                 XEiJ.tmrTimer.schedule (new TimerTask () {
  1107:                   @Override public void run () {
  1108:                     ppiUkunLastVirtualTime = XEiJ.mpuClockTime;
  1109:                     ppiUkunLastRealTime = System.nanoTime ();
  1110:                     ppiUkunIntervalOn = true;
  1111:                   }
  1112:                 }, 0L);                
  1113:               } else {
  1114:                 XEiJ.tmrTimer.schedule (new TimerTask () {
  1115:                   @Override public void run () {
  1116:                     ppiUkunIntervalOn = false;
  1117:                   }
  1118:                 }, 0L);                
  1119:               }
  1120:             }
  1121:           }
  1122:           break;
  1123:         case "Nano timer":  //ナノタイマー
  1124:           if (PPI_UKUN_ON && PPI_UKUN_NANO) {
  1125:             if (((JCheckBox) ae.getSource ()).isSelected ()) {  //計測開始
  1126:               ppiUkunNanoStart ();
  1127:             } else {  //計測終了
  1128:               ppiUkunNanoEnd ();
  1129:             }
  1130:           }
  1131:           break;
  1132:         case "XInput":
  1133:           if (PPI_XINPUT_ON) {
  1134:             if (((JCheckBox) ae.getSource ()).isSelected ()) {  //on
  1135:               if (!ppiXInputOn) {  //off→on
  1136:                 ppiXInputOn = true;
  1137:                 ppiXInputStart ();
  1138:               }
  1139:             } else {  //off
  1140:               if (ppiXInputOn) {  //on→off
  1141:                 ppiXInputOn = false;
  1142:                 ppiXInputEnd ();
  1143:               }
  1144:             }
  1145:           }
  1146:         }
  1147:       }
  1148:     };
  1149: 
  1150:     //ジョイスティックポートのメニュー
  1151:     //       0   1  2
  1152:     //   0  ポート  接続
  1153:     //   1   1   2
  1154:     //   2  -------------------------
  1155:     //   3  RB  RB  なし
  1156:     //   4  RB  RB  ノーマル2ボタンパッド#1
  1157:     //   5  RB  RB  ノーマル2ボタンパッド#2
  1158:     //   6  RB  RB  メガドラ3ボタンパッド#1
  1159:     //   7  RB  RB  メガドラ3ボタンパッド#2
  1160:     //   8  RB  RB  メガドラ6ボタンパッド#1
  1161:     //   9  RB  RB  メガドラ6ボタンパッド#2
  1162:     //  10  RB  RB  サイバースティック(デジタルモード)#1
  1163:     //  12  RB  RB  サイバースティック(デジタルモード)#2
  1164:     //  13  RB  RB  サイバースティック(アナログモード)#1
  1165:     //  14  RB  RB  サイバースティック(アナログモード)#2
  1166:     //  15  RB  RB  白窓君#1
  1167:     //  16  RB  RB  白窓君#2
  1168:     ButtonGroup port1Group = new ButtonGroup ();
  1169:     ButtonGroup port2Group = new ButtonGroup ();
  1170:     ActionListener port1Listener = new ActionListener () {
  1171:       @Override public void actionPerformed (ActionEvent ae) {
  1172:         Joystick joyStick = ppiJoysticks[Integer.parseInt (ae.getActionCommand ())];
  1173:         if (ppiJoystick1 != joyStick) {
  1174:           ppiJoystick1.reset ();
  1175:           ppiJoystick1 = joyStick;
  1176:         }
  1177:         ppiConfigurationScrollPane.setViewportView (joyStick.getConfigurationPanel ());
  1178:       }
  1179:     };
  1180:     ActionListener port2Listener = new ActionListener () {
  1181:       @Override public void actionPerformed (ActionEvent ae) {
  1182:         Joystick joyStick = ppiJoysticks[Integer.parseInt (ae.getActionCommand ())];
  1183:         if (ppiJoystick2 != joyStick) {
  1184:           ppiJoystick2.reset ();
  1185:           ppiJoystick2 = joyStick;
  1186:         }
  1187:         ppiConfigurationScrollPane.setViewportView (joyStick.getConfigurationPanel ());
  1188:       }
  1189:     };
  1190:     ArrayList<Object> cellList = new ArrayList<Object> ();
  1191:     cellList.add (Multilingual.mlnText (ComponentFactory.createLabel ("Port"), "ja", "ポート"));  //(0,0)-(1,0)
  1192:     cellList.add (Multilingual.mlnText (ComponentFactory.createLabel ("Connect to"), "ja", "接続"));  //(2,0)-(2,1)
  1193:     cellList.add ("1");  //(0,1)
  1194:     cellList.add ("2");  //(1,1)
  1195:     cellList.add (ComponentFactory.createHorizontalSeparator ());  //(0,2)-(2,2)
  1196:     for (int i = 0; i < ppiJoysticks.length; i++) {
  1197:       Joystick joyStick = ppiJoysticks[i];
  1198:       cellList.add (ComponentFactory.setText (ComponentFactory.createRadioButton (
  1199:         port1Group, joyStick == ppiJoystick1, String.valueOf (i), port1Listener), ""));  //(0,3+i)
  1200:       cellList.add (ComponentFactory.setText (ComponentFactory.createRadioButton (
  1201:         port2Group, joyStick == ppiJoystick2, String.valueOf (i), port2Listener), ""));  //(1,3+i)
  1202:       cellList.add (Multilingual.mlnText (ComponentFactory.createLabel (joyStick.getNameEn ()), "ja", joyStick.getNameJa ()));  //(3,3)
  1203:     }
  1204:     JScrollPane portMenuPanel = new JScrollPane (
  1205:       ComponentFactory.createGridPanel (
  1206:         3, 3 + ppiJoysticks.length,
  1207:         "paddingLeft=3,paddingRight=3,center",   //gridStyles
  1208:         "",  //colStyles
  1209:         "italic;italic;colSpan=3,widen",  //rowStyles
  1210:         "colSpan=2;rowSpan=2",  //cellStyles
  1211:         cellList.toArray (new Object[0])));
  1212: 
  1213:     //個々のジョイスティックの設定パネル
  1214:     ppiConfigurationScrollPane = new JScrollPane ((((ppiJoystick1 instanceof DummyPad) &&
  1215:                                                     !(ppiJoystick2 instanceof DummyPad)) ||  //2だけ接続されている
  1216:                                                    (!(ppiJoystick1 instanceof Shiromadokun) &&
  1217:                                                     (ppiJoystick2 instanceof Shiromadokun))  //2だけ白窓君
  1218:                                                    ? ppiJoystick2 : ppiJoystick1).getConfigurationPanel ());
  1219: 
  1220:     //ウインドウ
  1221:     ButtonGroup ukunGroup = new ButtonGroup ();
  1222:     ppiFrame = Multilingual.mlnTitle (
  1223:       ComponentFactory.createRestorableSubFrame (
  1224:         Settings.SGS_PPI_FRAME_KEY,
  1225:         "Joystick port settings",
  1226:         null,
  1227:         ComponentFactory.setEmptyBorder (
  1228:           ComponentFactory.createVerticalBox (
  1229:             //
  1230:             ppiUkunConnectCheckbox =
  1231:             !(PPI_UKUN_ON && (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded)) ? null :
  1232:             Multilingual.mlnText (
  1233:               ComponentFactory.setEnabled (
  1234:                 ComponentFactory.createCheckBox (ppiUkunRequestedConnection, "JoyPortUkun", actionListener),
  1235:                 ppiUkunRequestedConnection == ppiUkunCurrentConnection),
  1236:               "ja", "じょいぽーと U 君"),
  1237:             ppiUkunNotifyAModeRadioButton =
  1238:             !(PPI_UKUN_ON && (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded)) ? null :
  1239:             Multilingual.mlnText (
  1240:               ComponentFactory.setEnabled (
  1241:                 ComponentFactory.createRadioButton (ukunGroup, ppiUkunRequestedMode == PPI_UKUN_NOTIFY_A, "Notify(A) mode", actionListener),
  1242:                 !(ppiUkunCurrentConnection && ppiUkunRequestedMode != ppiUkunCurrentMode)),
  1243:               "ja", "Notify(A) モード"),
  1244:             ppiUkunNotifyBModeRadioButton =
  1245:             !(PPI_UKUN_ON && (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded)) ? null :
  1246:             Multilingual.mlnText (
  1247:               ComponentFactory.setEnabled (
  1248:                 ComponentFactory.createRadioButton (ukunGroup, ppiUkunRequestedMode == PPI_UKUN_NOTIFY_B, "Notify(B) mode", actionListener),
  1249:                 !(ppiUkunCurrentConnection && ppiUkunRequestedMode != ppiUkunCurrentMode)),
  1250:               "ja", "Notify(B) モード"),
  1251:             ppiUkunCommandModeRadioButton =
  1252:             !(PPI_UKUN_ON && (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded)) ? null :
  1253:             Multilingual.mlnText (
  1254:               ComponentFactory.setEnabled (
  1255:                 ComponentFactory.createRadioButton (ukunGroup, ppiUkunRequestedMode == PPI_UKUN_COMMAND, "Command mode", actionListener),
  1256:                 !(ppiUkunCurrentConnection && ppiUkunRequestedMode != ppiUkunCurrentMode)),
  1257:               "ja", "Command モード"),
  1258:             ppiUkunJSerialCommCheckbox =
  1259:             !(PPI_UKUN_ON && (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded)) ? null :
  1260:             ComponentFactory.setEnabled (
  1261:               ComponentFactory.createCheckBox (ppiUkunJSerialCommOn, "jSerialComm", actionListener),
  1262:               XEiJ.prgIsWindows && !ppiUkunCurrentConnection),
  1263:             !(PPI_UKUN_ON && (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded)) ? null :
  1264:             Multilingual.mlnText (
  1265:               ComponentFactory.createCheckBox (ppiUkunIntervalOn, "Output interval adjustment", actionListener),
  1266:               "ja", "出力間隔調整"),
  1267:             !(PPI_UKUN_ON && (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded) && PPI_UKUN_NANO) ? null :
  1268:             Multilingual.mlnText (
  1269:               ComponentFactory.createCheckBox (ppiUkunNanoMeasuring, "Nano timer", actionListener),
  1270:               "ja", "ナノタイマー"),
  1271:             !(PPI_UKUN_ON && (PPI_UKUN_JSERIALCOMM || XEiJ.prgWindllLoaded)) ? null : ComponentFactory.createHorizontalSeparator (),
  1272:             //
  1273:             !(PPI_XINPUT_ON && XEiJ.prgWindllLoaded) ? null :
  1274:             ComponentFactory.createCheckBox (ppiXInputOn, "XInput", actionListener),
  1275:             !(PPI_XINPUT_ON && XEiJ.prgWindllLoaded) ? null : ComponentFactory.createHorizontalSeparator (),
  1276:             //
  1277:             Multilingual.mlnText (
  1278:               ComponentFactory.createCheckBox (
  1279:                 ppiJoyKey,
  1280:                 "Consider part of keyboard as joystick",
  1281:                 actionListener),
  1282:               "ja", "キーボードの一部をジョイスティックとみなす"),
  1283:             Multilingual.mlnText (
  1284:               ComponentFactory.createCheckBox (
  1285:                 ppiJoyAuto,
  1286:                 "Enabled only while the port is read repeatedly",
  1287:                 actionListener),
  1288:               "ja", "ポートが繰り返し読み出されている間だけ有効"),
  1289:             Multilingual.mlnText (
  1290:               ComponentFactory.createCheckBox (
  1291:                 ppiJoyBlock,
  1292:                 "Remove key input data processed as a joystick operation",
  1293:                 actionListener),
  1294:               "ja", "ジョイスティック操作として処理されたキー入力データを取り除く"),
  1295:             Box.createVerticalStrut (5),
  1296:             ComponentFactory.createHorizontalSplitPane (
  1297:               portMenuPanel,
  1298:               ppiConfigurationScrollPane)),
  1299:           5, 5, 5, 5)),
  1300:       "ja", "ジョイスティックポート設定");
  1301:   }  //ppiMakeFrame()
  1302: 
  1303:   //consume = ppiInput (ke, pressed)
  1304:   //  JOYKEYの処理
  1305:   //  consume  true=入力をキーボードに渡さない
  1306:   public static boolean ppiInput (KeyEvent ke, boolean pressed) {
  1307:     boolean consume = false;
  1308:     if (ppiJoyKey && (!ppiJoyAuto || XEiJ.mpuClockTime < ppiLastAccessTime + PPI_CONTINUOUS_ACCESS_SPAN)) {
  1309:       if (ppiJoystick1.input (ke, pressed) ||
  1310:           ppiJoystick2.input (ke, pressed)) {
  1311:         //押されたときだけキーボード入力を取り除く
  1312:         //  特に自動有効化のときは、押されている間に有効になって離されたデータだけ取り除かれると、
  1313:         //  キーボード側は押されたままになっていると判断してリピートが止まらなくなる
  1314:         consume = pressed && ppiJoyBlock;
  1315:       }
  1316:     }
  1317:     return consume;
  1318:   }
  1319: 
  1320: 
  1321:   //ppiUkunAdjustInterval ()
  1322:   //  出力間隔調整
  1323:   //  PPIへの書き込みの間隔を仮想時間と実時間でそれぞれ計測し、
  1324:   //  500usを上限として、
  1325:   //  仮想時間の間隔の方が短ければウェイトを、
  1326:   //  実時間の間隔の方が短ければ空ループを追加することで、
  1327:   //  仮想時間の間隔と実時間の間隔のずれを軽減する。
  1328:   //  空ループを追加するとき負荷率が高くなる。
  1329:   public static void ppiUkunAdjustInterval () {
  1330:     long virtualTime = XEiJ.mpuClockTime;  //仮想時間。ピコ秒
  1331:     long realTime = System.nanoTime ();  //実時間。ナノ秒
  1332:     long virtualInterval = virtualTime - ppiUkunLastVirtualTime;  //仮想時間の間隔
  1333:     long realInterval = realTime - ppiUkunLastRealTime;  //実時間の間隔
  1334:     ppiUkunLastVirtualTime = virtualTime;
  1335:     ppiUkunLastRealTime = realTime;
  1336:     realInterval *= XEiJ.TMR_FREQ / 1000000000L;  //ナノ秒→ピコ秒
  1337:     if (virtualInterval <= realInterval) {  //仮想時間の間隔の方が短い。ウェイトを追加する
  1338:       long adjustmentTime = realInterval - virtualInterval;
  1339:       adjustmentTime = Math.min (PPI_UKUN_INTERVAL_LIMIT, adjustmentTime);
  1340:       XEiJ.mpuClockTime += adjustmentTime;
  1341:     } else {  //実時間の間隔の方が短い。空ループを追加する
  1342:       long adjustmentTime = virtualInterval - realInterval;
  1343:       adjustmentTime = Math.min (PPI_UKUN_INTERVAL_LIMIT, adjustmentTime);
  1344:       adjustmentTime /= XEiJ.TMR_FREQ / 1000000000L;  //ピコ秒→ナノ秒
  1345:       if (adjustmentTime != 0L) {
  1346:         if (true) {  //空ループを追加する
  1347:           while (System.nanoTime () < realTime + adjustmentTime) {
  1348:           }
  1349:         } else {  //スリープを追加する
  1350:           try {
  1351:             Thread.sleep (adjustmentTime / 1000000L, (int) (adjustmentTime % 1000000L));
  1352:           } catch (InterruptedException ie) {
  1353:           }
  1354:         }
  1355:       }
  1356:     }
  1357:   }  //ppiUkunAdjustInterval
  1358: 
  1359: 
  1360:   //  リード
  1361:   //  FM音源レジスタのアクセスウェイトのためのPPIの空読みは、ジョイスティックのデータを得ることが目的ではないので、
  1362:   //  ジョイスティックポートが連続的に読み出されているとみなさない
  1363:   public static int ppiReadByte (int a) {
  1364:     int d;
  1365:     //
  1366:     long t;
  1367:     if (PPI_UKUN_NANO) {
  1368:       t = System.nanoTime ();
  1369:     }
  1370:     //
  1371:     switch (a & 7) {
  1372:       //
  1373:     case PPI_PORT_A & 7:
  1374:       if (PPI_UKUN_ON &&
  1375:           ppiUkunCurrentConnection) {  //接続している
  1376:         if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B) {  //Notify(B)モード
  1377:           if (ppiUkunSending) {
  1378:             XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1379:             do {
  1380:             } while (ppiUkunSending);
  1381:           }
  1382:         } else if (ppiUkunCurrentMode == PPI_UKUN_COMMAND) {  //Commandモード
  1383:           XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1384:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1385:             byte[] b = new byte[] { (byte) 0b0011_1010 };  //リードポートA
  1386:             ppiUkunSerialPort.writeBytes (b, 1);
  1387:             ppiUkunSerialPort.readBytes (b, 1);
  1388:             ppiUkunPortA = b[0] & 0xff;
  1389:           } else {  //!jSerialComm
  1390:             try {
  1391:               ppiUkunOldOutputStream.write (0b0011_1010);  //リードポートA
  1392:               ppiUkunPortA = ppiUkunOldInputStream.read ();
  1393:             } catch (IOException ioe) {
  1394:               ioe.printStackTrace ();
  1395:               ppiUkunDisconnect ();
  1396:             }
  1397:           }  //if jSerialComm/!
  1398:         }
  1399:         d = ppiUkunPortA;
  1400:       } else {  //接続していない
  1401:         if (XEiJ.regOC >> 6 != 0b0100_101_000) {  //TST.B以外
  1402:           ppiLastAccessTime = XEiJ.mpuClockTime;
  1403:         }
  1404:         d = ppiJoystick1.readByte () & 0xff;
  1405:       }
  1406:       break;
  1407:       //
  1408:     case PPI_PORT_B & 7:
  1409:       if (PPI_UKUN_ON &&
  1410:           ppiUkunCurrentConnection) {  //接続している
  1411:         if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B) {  //Notify(B)モード
  1412:           if (ppiUkunSending) {
  1413:             XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1414:             do {
  1415:             } while (ppiUkunSending);
  1416:           }
  1417:         } else if (ppiUkunCurrentMode == PPI_UKUN_COMMAND) {  //Commandモード
  1418:           XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1419:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1420:             byte[] b = new byte[] { (byte) 0b0011_1011 };  //リードポートB
  1421:             ppiUkunSerialPort.writeBytes (b, 1);
  1422:             ppiUkunSerialPort.readBytes (b, 1);
  1423:             ppiUkunPortB = b[0] & 0xff;
  1424:           } else {  //!jSerialComm
  1425:             try {
  1426:               ppiUkunOldOutputStream.write (0b0011_1011);  //リードポートB
  1427:               ppiUkunPortB = ppiUkunOldInputStream.read ();
  1428:             } catch (IOException ioe) {
  1429:               ioe.printStackTrace ();
  1430:               ppiUkunDisconnect ();
  1431:             }
  1432:           }  //if jSerialComm/!
  1433:         }
  1434:         d = ppiUkunPortB;
  1435:       } else {  //接続していない
  1436:         if (XEiJ.regOC >> 6 != 0b0100_101_000) {  //TST.B以外
  1437:           ppiLastAccessTime = XEiJ.mpuClockTime;
  1438:         }
  1439:         d = ppiJoystick2.readByte () & 0xff;
  1440:       }
  1441:       break;
  1442:       //
  1443:     case PPI_PORT_C & 7:
  1444:       if (PPI_UKUN_ON &&
  1445:           ppiUkunCurrentConnection) {  //接続している
  1446:         XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1447:         if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B) {  //Notify(B)モード
  1448:           if (ppiUkunSending) {
  1449:             XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1450:             do {
  1451:             } while (ppiUkunSending);
  1452:           }
  1453:         } else if (ppiUkunCurrentMode == PPI_UKUN_COMMAND) {  //Commandモード
  1454:           XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1455:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1456:             byte[] b = new byte[] { (byte) 0b0011_1100 };  //リードポートC
  1457:             ppiUkunSerialPort.writeBytes (b, 1);
  1458:             ppiUkunSerialPort.readBytes (b, 1);
  1459:             ppiUkunPortC = b[0] & 0xf0;
  1460:           } else {  //!jSerialComm
  1461:             try {
  1462:               ppiUkunOldOutputStream.write (0b0011_1100);  //リードポートC
  1463:               ppiUkunPortC = ppiUkunOldInputStream.read () & 0xf0;
  1464:             } catch (IOException ioe) {
  1465:               ioe.printStackTrace ();
  1466:               ppiUkunDisconnect ();
  1467:             }
  1468:           }  //if jSerialComm/!
  1469:         }
  1470:         d = (ppiUkunPortC |  //上位4ビット
  1471:              (ppiPortCData & 0x0f));  //下位4ビット
  1472:       } else {  //接続していない
  1473:         d = ppiPortCData;
  1474:       }
  1475:       break;
  1476:     default:
  1477:       d = 0xff;
  1478:     }  //switch a&7
  1479:     //
  1480:     if (PPI_UKUN_NANO) {
  1481:       t = System.nanoTime () - t;
  1482:       int i = a & 6;  //0,2,4,6
  1483:       if (t < ppiUkunNanoBestArray[i]) {
  1484:         ppiUkunNanoBestArray[i] = t;
  1485:       }
  1486:       if (ppiUkunNanoWorstArray[i] < t) {
  1487:         ppiUkunNanoWorstArray[i] = t;
  1488:       }
  1489:       ppiUkunNanoTotalArray[i] += t;
  1490:       ppiUkunNanoCountArray[i]++;
  1491:     }
  1492:     //
  1493:     return d;
  1494:   }  //ppiReadByte
  1495: 
  1496:   //  ライト
  1497:   public static void ppiWriteByte (int a, int d) {
  1498:     d &= 0xff;
  1499:     //
  1500:     long t;
  1501:     if (PPI_UKUN_NANO) {
  1502:       t = System.nanoTime ();
  1503:     }
  1504:     //
  1505:     switch (a & 7) {
  1506:       //
  1507:     case PPI_PORT_A & 7:
  1508:       if (PPI_UKUN_ON &&
  1509:           ppiUkunCurrentConnection) {  //接続している
  1510:         if (PPI_UKUN_INTERVAL && ppiUkunIntervalOn) {
  1511:           ppiUkunAdjustInterval ();
  1512:         } else {
  1513:           XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1514:         }
  1515:         if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_A) {  //Notify(A)モード
  1516:           ppiUkunSending = true;
  1517:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1518:             ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b1000_0000 | (d & 0b0110_1111)) }, 1);  //ライトポートA
  1519:           } else {  //!jSerialComm
  1520:             try {
  1521:               ppiUkunOldOutputStream.write (0b1000_0000 | (d & 0b0110_1111));  //ライトポートA
  1522:             } catch (IOException ioe) {
  1523:               ioe.printStackTrace ();
  1524:               ppiUkunDisconnect ();
  1525:             }
  1526:           }  //if jSerialComm/!
  1527:           while (ppiUkunSending) {
  1528:           }
  1529:         } else if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B) {  //Notify(B)モード
  1530:           while (ppiUkunSending) {
  1531:           }
  1532:           ppiUkunSending = true;
  1533:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1534:             ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b1000_0000 | (d & 0b0110_1111)) }, 1);  //ライトポートA
  1535:           } else {  //!jSerialComm
  1536:             try {
  1537:               ppiUkunOldOutputStream.write (0b1000_0000 | (d & 0b0110_1111));  //ライトポートA
  1538:             } catch (IOException ioe) {
  1539:               ioe.printStackTrace ();
  1540:               ppiUkunDisconnect ();
  1541:             }
  1542:           }  //if jSerialComm/!
  1543:         } else if (ppiUkunCurrentMode == PPI_UKUN_COMMAND) {  //Commandモード
  1544:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1545:             ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b1000_0000 | (d & 0b0110_1111)) }, 1);  //ライトポートA
  1546:           } else {  //!jSerialComm
  1547:             try {
  1548:               ppiUkunOldOutputStream.write (0b1000_0000 | (d & 0b0110_1111));  //ライトポートA
  1549:             } catch (IOException ioe) {
  1550:               ioe.printStackTrace ();
  1551:               ppiUkunDisconnect ();
  1552:             }
  1553:           }  //if jSerialComm/!
  1554:         }
  1555:       } else {  //接続していない
  1556:         ppiJoystick1.writeByte (d);
  1557:       }
  1558:       break;
  1559:       //
  1560:     case PPI_PORT_B & 7:
  1561:       if (PPI_UKUN_ON &&
  1562:           ppiUkunCurrentConnection) {  //接続している
  1563:         if (PPI_UKUN_INTERVAL && ppiUkunIntervalOn) {
  1564:           ppiUkunAdjustInterval ();
  1565:         } else {
  1566:           XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1567:         }
  1568:         if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_A) {  //Notify(A)モード
  1569:           ppiUkunSending = true;
  1570:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1571:             ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b1001_0000 | (d & 0b0110_1111)) }, 1);  //ライトポートB
  1572:           } else {  //!jSerialComm
  1573:             try {
  1574:               ppiUkunOldOutputStream.write (0b1001_0000 | (d & 0b0110_1111));  //ライトポートB
  1575:             } catch (IOException ioe) {
  1576:               ioe.printStackTrace ();
  1577:               ppiUkunDisconnect ();
  1578:             }
  1579:           }  //if jSerialComm/!
  1580:           while (ppiUkunSending) {
  1581:           }
  1582:         } else if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B) {  //Notify(B)モード
  1583:           while (ppiUkunSending) {
  1584:           }
  1585:           ppiUkunSending = true;
  1586:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1587:             ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b1001_0000 | (d & 0b0110_1111)) }, 1);  //ライトポートB
  1588:           } else {  //!jSerialComm
  1589:             try {
  1590:               ppiUkunOldOutputStream.write (0b1001_0000 | (d & 0b0110_1111));  //ライトポートB
  1591:             } catch (IOException ioe) {
  1592:               ioe.printStackTrace ();
  1593:               ppiUkunDisconnect ();
  1594:             }
  1595:           }  //if jSerialComm/!
  1596:         } else if (ppiUkunCurrentMode == PPI_UKUN_COMMAND) {  //Commandモード
  1597:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1598:             ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b1001_0000 | (d & 0b0110_1111)) }, 1);  //ライトポートB
  1599:           } else {  //!jSerialComm
  1600:             try {
  1601:               ppiUkunOldOutputStream.write (0b1001_0000 | (d & 0b0110_1111));  //ライトポートB
  1602:             } catch (IOException ioe) {
  1603:               ioe.printStackTrace ();
  1604:               ppiUkunDisconnect ();
  1605:             }
  1606:           }  //if jSerialComm/!
  1607:         }
  1608:       } else {  //接続していない
  1609:         ppiJoystick2.writeByte (d);
  1610:       }
  1611:       break;
  1612:       //
  1613:     case PPI_PORT_C & 7:
  1614:       ppiPortCData = d;
  1615:       //下位4ビット
  1616:       ADPCM.pcmSetPan (d);  //パン
  1617:       ADPCM.pcmDivider = d >> 2 & 3;  //分周比。0=1/1024,1=1/768,2=1/512,3=inhibited
  1618:       ADPCM.pcmUpdateRepeatInterval ();
  1619:       //上位4ビット
  1620:       if (PPI_UKUN_ON &&
  1621:           ppiUkunCurrentConnection) {  //接続している
  1622:         if (PPI_UKUN_INTERVAL && ppiUkunIntervalOn) {
  1623:           ppiUkunAdjustInterval ();
  1624:         } else {
  1625:           XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1626:         }
  1627:         if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_A) {  //Notify(A)モード
  1628:           ppiUkunSending = true;
  1629:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1630:             ppiUkunSerialPort.writeBytes (new byte[] { (byte) (d >> 4) }, 1);  //ライトポートC
  1631:           } else {  //!jSerialComm
  1632:             try {
  1633:               ppiUkunOldOutputStream.write (d >> 4);  //ライトポートC
  1634:             } catch (IOException ioe) {
  1635:               ioe.printStackTrace ();
  1636:               ppiUkunDisconnect ();
  1637:             }
  1638:           }  //if jSerialComm/!
  1639:           while (ppiUkunSending) {
  1640:           }
  1641:         } else if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B) {  //Notify(B)モード
  1642:           while (ppiUkunSending) {
  1643:           }
  1644:           ppiUkunSending = true;
  1645:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1646:             ppiUkunSerialPort.writeBytes (new byte[] { (byte) (d >> 4) }, 1);  //ライトポートC
  1647:           } else {  //!jSerialComm
  1648:             try {
  1649:               ppiUkunOldOutputStream.write (d >> 4);  //ライトポートC
  1650:             } catch (IOException ioe) {
  1651:               ioe.printStackTrace ();
  1652:               ppiUkunDisconnect ();
  1653:             }
  1654:           }  //if jSerialComm/!
  1655:         } else if (ppiUkunCurrentMode == PPI_UKUN_COMMAND) {  //Commandモード
  1656:           if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1657:             ppiUkunSerialPort.writeBytes (new byte[] { (byte) (d >> 4) }, 1);  //ライトポートC
  1658:           } else {  //!jSerialComm
  1659:             try {
  1660:               ppiUkunOldOutputStream.write (d >> 4);  //ライトポートC
  1661:             } catch (IOException ioe) {
  1662:               ioe.printStackTrace ();
  1663:               ppiUkunDisconnect ();
  1664:             }
  1665:           }  //if jSerialComm/!
  1666:         }
  1667:       } else {  //接続していない
  1668:         ppiJoystick1.setPin8 (d >> 4 & 1);
  1669:         ppiJoystick2.setPin8 (d >> 5 & 1);
  1670:         ppiJoystick1.setPin6 ((d >> 6 & 1) ^ 1);
  1671:         ppiJoystick1.setPin7 ((d >> 7 & 1) ^ 1);
  1672:       }
  1673:       break;
  1674:       //
  1675:     case PPI_CONTROL & 7:
  1676:       if ((d & 0b1000_0000) == 0b0000_0000) {  //0b0..._nnnx
  1677:         int n = (d >> 1) & 7;  //ビット番号
  1678:         int x = d & 1;  //データ
  1679:         ppiPortCData = ppiPortCData & ~(1 << n) | x << n;
  1680:         if (n < 4) {  //下位4ビット
  1681:           switch (n) {
  1682:           case 0:
  1683:           case 1:
  1684:             ADPCM.pcmSetPan (ppiPortCData & 3);  //パン
  1685:             break;
  1686:           case 2:
  1687:           case 3:
  1688:             ADPCM.pcmDivider = ppiPortCData >> 2 & 3;  //分周比。0=1/1024,1=1/768,2=1/512,3=inhibited
  1689:             ADPCM.pcmUpdateRepeatInterval ();
  1690:             break;
  1691:           }
  1692:         } else {  //上位4ビット
  1693:           if (PPI_UKUN_ON &&
  1694:               ppiUkunCurrentConnection) {  //接続している
  1695:             if (PPI_UKUN_INTERVAL && ppiUkunIntervalOn) {
  1696:               ppiUkunAdjustInterval ();
  1697:             } else {
  1698:               XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1699:             }
  1700:             if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_A) {  //Notify(A)モード
  1701:               ppiUkunSending = true;
  1702:               if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1703:                 ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b0001_0000 | (d & 0b0000_1111)) }, 1);  //ライトコントロールワード
  1704:               } else {  //!jSerialComm
  1705:                 try {
  1706:                   ppiUkunOldOutputStream.write (0b0001_0000 | (d & 0b0000_1111));  //ライトコントロールワード
  1707:                 } catch (IOException ioe) {
  1708:                   ioe.printStackTrace ();
  1709:                   ppiUkunDisconnect ();
  1710:                 }
  1711:               }  //if jSerialComm/!
  1712:               while (ppiUkunSending) {
  1713:               }
  1714:             } else if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B) {  //Notify(B)モード
  1715:               while (ppiUkunSending) {
  1716:               }
  1717:               ppiUkunSending = true;
  1718:               if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1719:                 ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b0001_0000 | (d & 0b0000_1111)) }, 1);  //ライトコントロールワード
  1720:               } else {  //!jSerialComm
  1721:                 try {
  1722:                   ppiUkunOldOutputStream.write (0b0001_0000 | (d & 0b0000_1111));  //ライトコントロールワード
  1723:                 } catch (IOException ioe) {
  1724:                   ioe.printStackTrace ();
  1725:                   ppiUkunDisconnect ();
  1726:                 }
  1727:               }  //if jSerialComm/!
  1728:             } else if (ppiUkunCurrentMode == PPI_UKUN_COMMAND) {  //Commandモード
  1729:               if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1730:                 ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b0001_0000 | (d & 0b0000_1111)) }, 1);  //ライトコントロールワード
  1731:               } else {  //!jSerialComm
  1732:                 try {
  1733:                   ppiUkunOldOutputStream.write (0b0001_0000 | (d & 0b0000_1111));  //ライトコントロールワード
  1734:                 } catch (IOException ioe) {
  1735:                   ioe.printStackTrace ();
  1736:                   ppiUkunDisconnect ();
  1737:                 }
  1738:               }  //if jSerialComm/!
  1739:             }
  1740:           } else {  //接続していない
  1741:             switch (n) {
  1742:             case 4:
  1743:               ppiJoystick1.setPin8 (x);
  1744:               break;
  1745:             case 5:
  1746:               ppiJoystick2.setPin8 (x);
  1747:               break;
  1748:             case 6:
  1749:               ppiJoystick1.setPin6 (x ^ 1);
  1750:               break;
  1751:             case 7:
  1752:               ppiJoystick1.setPin7 (x ^ 1);
  1753:               break;
  1754:             }
  1755:           }
  1756:         }  //if 下位/上位
  1757:       } else if ((d & 0b1000_0100) == 0b1000_0000){  //0b1..._.0..
  1758:         if (PPI_UKUN_ON &&
  1759:             ppiUkunCurrentConnection) {  //接続している
  1760:           if (PPI_UKUN_INTERVAL && ppiUkunIntervalOn) {
  1761:             ppiUkunAdjustInterval ();
  1762:           } else {
  1763:             XEiJ.mpuClockTime += PPI_UKUN_WAIT;
  1764:           }
  1765:           if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_A) {  //Notify(A)モード
  1766:             ppiUkunSending = true;
  1767:             if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1768:               ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b0100_0000 |
  1769:                                                                  (d & 0b0111_1000) >> 1 |
  1770:                                                                  (d & 0b0000_0011)) }, 1);  //ライトコントロールワード
  1771:             } else {  //!jSerialComm
  1772:               try {
  1773:                 ppiUkunOldOutputStream.write (0b0100_0000 |
  1774:                                               (d & 0b0111_1000) >> 1 |
  1775:                                               (d & 0b0000_0011));  //ライトコントロールワード
  1776:               } catch (IOException ioe) {
  1777:                 ioe.printStackTrace ();
  1778:                 ppiUkunDisconnect ();
  1779:               }
  1780:             }  //if jSerialComm/!
  1781:             while (ppiUkunSending) {
  1782:             }
  1783:           } else if (ppiUkunCurrentMode == PPI_UKUN_NOTIFY_B) {  //Notify(B)モード
  1784:             while (ppiUkunSending) {
  1785:             }
  1786:             ppiUkunSending = true;
  1787:             if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1788:               ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b0100_0000 |
  1789:                                                                  (d & 0b0111_1000) >> 1 |
  1790:                                                                  (d & 0b0000_0011)) }, 1);  //ライトコントロールワード
  1791:             } else {  //!jSerialComm
  1792:               try {
  1793:                 ppiUkunOldOutputStream.write (0b0100_0000 |
  1794:                                               (d & 0b0111_1000) >> 1 |
  1795:                                               (d & 0b0000_0011));  //ライトコントロールワード
  1796:               } catch (IOException ioe) {
  1797:                 ioe.printStackTrace ();
  1798:                 ppiUkunDisconnect ();
  1799:               }
  1800:             }  //if jSerialComm/!
  1801:           } else if (ppiUkunCurrentMode == PPI_UKUN_COMMAND) {  //Commandモード
  1802:             if (PPI_UKUN_JSERIALCOMM && ppiUkunJSerialCommOn) {  //jSerialComm
  1803:               ppiUkunSerialPort.writeBytes (new byte[] { (byte) (0b0100_0000 |
  1804:                                                                  (d & 0b0111_1000) >> 1 |
  1805:                                                                  (d & 0b0000_0011)) }, 1);  //ライトコントロールワード
  1806:             } else {  //!jSerialComm
  1807:               try {
  1808:                 ppiUkunOldOutputStream.write (0b0100_0000 |
  1809:                                               (d & 0b0111_1000) >> 1 |
  1810:                                               (d & 0b0000_0011));  //ライトコントロールワード
  1811:               } catch (IOException ioe) {
  1812:                 ioe.printStackTrace ();
  1813:                 ppiUkunDisconnect ();
  1814:               }
  1815:             }  //if jSerialComm/!
  1816:           }
  1817:         } else {  //接続していない
  1818:           //!!! 未対応
  1819:         }
  1820:       } else {  //0b1..._.1..
  1821:         //!!! 非対応
  1822:       }
  1823:     }  //switch a&7
  1824:     //
  1825:     if (PPI_UKUN_NANO) {
  1826:       t = System.nanoTime () - t;
  1827:       int i = (a & 6) | 1;  //1,3,5,7
  1828:       if (t < ppiUkunNanoBestArray[i]) {
  1829:         ppiUkunNanoBestArray[i] = t;
  1830:       }
  1831:       if (ppiUkunNanoWorstArray[i] < t) {
  1832:         ppiUkunNanoWorstArray[i] = t;
  1833:       }
  1834:       ppiUkunNanoTotalArray[i] += t;
  1835:       ppiUkunNanoCountArray[i]++;
  1836:     }
  1837:     //
  1838:   }  //ppiWriteByte
  1839: 
  1840: }  //class PPI