DisassembleList.java
     1: //========================================================================================
     2: //  DisassembleList.java
     3: //    en:Disassemble list
     4: //    ja:逆アセンブルリスト
     5: //  Copyright (C) 2003-2025 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.*;  //BasicStroke,BorderLayout,BoxLayout,Color,Component,Container,Cursor,Desktop,Dimension,Font,FlowLayout,Frame,Graphics,Graphics2D,GraphicsDevice,GraphicsEnvironment,GridLayout,Image,Insets,Paint,Point,Rectangle,RenderingHints,Robot,Shape,Stroke,TexturePaint,Toolkit
    16: import java.awt.event.*;  //ActionEvent,ActionListener,ComponentAdapter,ComponentEvent,ComponentListener,FocusAdapter,FocusEvent,FocusListener,InputEvent,KeyAdapter,KeyEvent,KeyListener,MouseAdapter,MouseEvent,MouseListener,MouseMotionAdapter,MouseWheelEvent,WindowAdapter,WindowEvent,WindowListener,WindowStateListener
    17: import java.lang.*;  //Boolean,Character,Class,Comparable,Double,Exception,Float,IllegalArgumentException,Integer,Long,Math,Number,Object,Runnable,SecurityException,String,StringBuilder,System
    18: import java.util.*;  //ArrayList,Arrays,Calendar,GregorianCalendar,HashMap,Map,Map.Entry,TimeZone,Timer,TimerTask,TreeMap
    19: import javax.swing.*;  //AbstractSpinnerModel,Box,ButtonGroup,DefaultListModel,ImageIcon,JApplet,JButton,JCheckBox,JCheckBoxMenuItem,JDialog,JFileChooser,JFrame,JLabel,JList,JMenu,JMenuBar,JMenuItem,JPanel,JRadioButton,JScrollPane,JSpinner,JTextArea,JTextField,JTextPane,JViewport,ScrollPaneConstants,SpinnerListModel,SpinnerNumberModel,SwingConstants,SwingUtilities,UIManager,UIDefaults,UnsupportedLookAndFeelException
    20: import javax.swing.event.*;  //CaretEvent,CaretListener,ChangeEvent,ChangeListener,DocumentEvent,DocumentListener,ListSelectionListener
    21: 
    22: public class DisassembleList {
    23: 
    24:   public static final int DDP_ITEM_SIZE = 0x00000002;  //行の最小サイズ
    25:   public static final int DDP_PAGE_SIZE = 0x00000400;  //ページのサイズ
    26:   public static final int DDP_ITEM_MASK = -DDP_ITEM_SIZE;
    27:   public static final int DDP_PAGE_MASK = -DDP_PAGE_SIZE;
    28:   public static final int DDP_MAX_ITEMS = DDP_PAGE_SIZE / DDP_ITEM_SIZE + 2;  //ページの最大項目数。先頭と末尾の番兵を含む
    29: 
    30:   public static final char[] DDP_MOVEQD0_BASE = (
    31:     //         11111111112222222222333333333344444444445555555555666666666677777777778
    32:     //12345678901234567890123456789012345678901234567890123456789012345678901234567890
    33:     "moveq.l #$xx,d0").toCharArray ();
    34:   public static final char[] DDP_DCW_BASE = (
    35:     //         11111111112222222222333333333344444444445555555555666666666677777777778
    36:     //12345678901234567890123456789012345678901234567890123456789012345678901234567890
    37:     ".dc.w   $xxxx").toCharArray ();
    38: 
    39:   public static int ddpItemCount;  //ページに含まれる項目の数。先頭と末尾の番兵を含む。0=構築前または再構築要求
    40:   public static int ddpItemIndex;  //キャレットがある項目の番号
    41:   public static int ddpItemAddress;  //キャレットがある項目の先頭アドレス
    42:   public static int ddpPageAddress;  //ページの先頭アドレス
    43:   public static final int[] ddpAddressArray = new int[DDP_MAX_ITEMS];  //項目の先頭アドレスの配列。先頭は前のページの末尾、末尾は次のページの先頭。スピナーのヒント
    44:   public static final int[] ddpSplitArray = new int[DDP_MAX_ITEMS];  //項目を区切る位置の配列。先頭は0
    45:   public static final int[] ddpCaretArray = new int[DDP_MAX_ITEMS];  //項目が選択されたときキャレットを移動させる位置の配列。行の手前にヘッダやラベルなどを挿入しないときはddpSplitArrayと同じ
    46:   public static final boolean[] ddpDCWArray = new boolean[DDP_MAX_ITEMS];  //項目毎の.dc.wで出力したかどうかのフラグの配列。true=数ワード後にある表示しなければならないアドレスを跨がないために逆アセンブルせず.dc.wで出力した。この行がクリックされたとき逆アセンブルし直す
    47: 
    48:   public static JFrame ddpFrame;  //ウインドウ
    49:   public static ScrollTextArea ddpBoard;  //スクロールテキストエリア
    50:   public static JTextArea ddpTextArea;  //テキストエリア
    51: 
    52:   public static Hex8Spinner ddpSpinner;  //スピナー
    53: 
    54:   public static int ddpPopupAddress;  //クリックされた行のアドレス
    55: 
    56:   public static boolean ddpBacktraceOn;  //true=バックトレース
    57:   public static long ddpBacktraceRecord;  //現在選択されている分岐レコードの通し番号。-1L=未選択
    58:   public static SpinnerNumberModel ddpBacktraceModel;  //バックトレーススピナーのモデル。値はLong
    59:   public static JSpinner ddpBacktraceSpinner;  //バックトレーススピナー
    60:   public static JCheckBox ddpBacktraceCheckBox;  //バックトレースチェックボックス
    61: 
    62:   public static String ddpStoppedBy;  //停止理由
    63:   public static int ddpStoppedAddress;  //停止位置
    64: 
    65:   public static int ddpSupervisorMode;  //0=ユーザモード,0以外=スーパーバイザモード
    66:   public static JCheckBox ddpSupervisorCheckBox;  //ユーザ/スーパーバイザチェックボックス
    67: 
    68:   public static int ddpProgramMode;  //アドレスとオフセット。0=アドレスのみ,1=オフセットのみ,2=両方
    69:   public static RotaryButton ddpProgramButton;  //アドレスとオフセットボタン
    70:   public static JComboBox<String> ddpProgramBox;  //プログラム名コンボボックス
    71:   public static int ddpProgramAge;
    72: 
    73: 
    74:   //ddpInit ()
    75:   //  初期化
    76:   public static void ddpInit () {
    77: 
    78:     ddpItemCount = 0;  //構築前
    79:     ddpItemIndex = 0;
    80:     ddpItemAddress = -1;
    81:     ddpPageAddress = 0;
    82:     ddpSupervisorMode = 1;
    83:     //ddpAddressArray = new int[DDP_MAX_ITEMS];
    84:     //ddpSplitArray = new int[DDP_MAX_ITEMS];
    85:     //ddpCaretArray = new int[DDP_MAX_ITEMS];
    86:     //ddpDCWArray = new boolean[DDP_MAX_ITEMS];
    87: 
    88:     ddpFrame = null;
    89: 
    90:   }  //ddpInit()
    91: 
    92:   //ddpStart ()
    93:   public static void ddpStart () {
    94:     if (RestorableFrame.rfmGetOpened (Settings.SGS_DDP_FRAME_KEY)) {
    95:       ddpOpen (-1, -1, true);
    96:     }
    97:   }  //ddpStart()
    98: 
    99:   //ddpOpen (address, supervisor, forceUpdate)
   100:   //  逆アセンブルリストウインドウを開く
   101:   //  既に開いているときは手前に持ってくる
   102:   public static void ddpOpen (int address, int supervisor, boolean forceUpdate) {
   103:     if (ddpFrame == null) {
   104:       ddpMake ();
   105:     }
   106:     ddpFrame.setVisible (true);
   107:     ddpBacktraceRecord = -1L;  //分岐レコードの選択を解除する
   108:     LabeledAddress.lblClear ();
   109:     ddpUpdate (address, supervisor, forceUpdate);
   110:     XEiJ.pnlExitFullScreen (false);
   111:     XEiJ.dbgVisibleMask |= XEiJ.DBG_DDP_VISIBLE_MASK;
   112:   }  //ddpOpen(int,int,boolean)
   113: 
   114:   //ddpMake ()
   115:   //  逆アセンブルリストウインドウを作る
   116:   public static void ddpMake () {
   117: 
   118:     //スクロールテキストエリア
   119:     ddpBoard = ComponentFactory.setPreferredSize (
   120:       ComponentFactory.setFont (new ScrollTextArea (), LnF.lnfMonospacedFont),
   121:       730, 400);
   122:     ddpBoard.setMargin (new Insets (2, 4, 2, 4));
   123:     ddpBoard.setHighlightCursorOn (true);
   124:     ddpTextArea = ddpBoard.getTextArea ();
   125:     ddpTextArea.setEditable (false);
   126:     ddpTextArea.addMouseWheelListener ((mwe) -> {
   127:       int n = mwe.getWheelRotation ();
   128:       JViewport v = ddpBoard.getViewport ();
   129:       Point p = v.getViewPosition ();
   130:       v.setViewPosition (new Point (p.x,
   131:                                     Math.max (0,
   132:                                               Math.min (ddpTextArea.getSize ().height - v.getExtentSize ().height,
   133:                                                         p.y + n * ddpTextArea.getFont ().getSize () * 5))));
   134:     });
   135: 
   136:     //スピナー
   137:     ddpSpinner = ComponentFactory.createHex8Spinner (ddpPageAddress, DDP_ITEM_MASK, true, new ChangeListener () {
   138:       //スピナーのチェンジリスナー
   139:       //  スピナーが操作されたとき、そのアドレスの行にテキストエリアのキャレットを移動させる
   140:       //  ページの範囲外になったときはテキストエリアを再構築する
   141:       //  ページの構築中に呼び出されたときは何もしない
   142:       @Override public void stateChanged (ChangeEvent ce) {
   143:         if (XEiJ.dbgEventMask == 0) {  //テキストは構築済みでsetTextの中ではない
   144:           ddpUpdate (ddpSpinner.getAbsoluteValue (), ddpSupervisorMode, false);
   145:         }
   146:       }
   147:     });
   148: 
   149:     //テキストエリアのキャレットリスナー
   150:     //  テキストエリアがクリックされてキャレットが動いたとき、その行のアドレスをスピナーに設定する
   151:     //  クリックでテキストエリアに移ってしまったフォーカスをスピナーに戻す
   152:     //  ページの構築中に呼び出されたときは何もしない
   153:     //    setText→キャレットリスナー→スピナーのチェンジリスナー→setTextとなるとsetTextの二重呼び出しでエラーが出る
   154:     ComponentFactory.addListener (
   155:       ddpTextArea,
   156:       new CaretListener () {
   157:         @Override public void caretUpdate (CaretEvent ce) {
   158:           if (XEiJ.dbgEventMask == 0) {  //テキストは構築済みでsetTextの中ではない
   159:             int p = ce.getDot ();  //キャレットの位置
   160:             if (p == ce.getMark ()) {  //選択範囲がない
   161:               int i = Arrays.binarySearch (ddpSplitArray, 1, ddpItemCount, p + 1);  //項目の先頭のときも次の項目を検索してから1つ戻る
   162:               i = (i >> 31 ^ i) - 1;  //キャレットがある位置を含む項目の番号
   163:               ddpSpinner.setHintIndex (i);
   164:               //ddpProgramBox.setSelectedIndex (LabeledAddress.lblGetIndex (ddpAddressArray[i]) + 1);
   165:             }
   166:           }
   167:         }
   168:       });
   169: 
   170:     //テキストエリアのマウスリスナー
   171:     ComponentFactory.addListener (
   172:       ddpTextArea,
   173:       new MouseAdapter () {
   174:         @Override public void mousePressed (MouseEvent me) {
   175:           if (XEiJ.mpuTask == null && me.isPopupTrigger ()) {
   176:             XEiJ.dbgShowPopup (me, ddpTextArea, true);
   177:           }
   178:         }
   179:         @Override public void mouseReleased (MouseEvent me) {
   180:           if (XEiJ.mpuTask == null && me.isPopupTrigger ()) {
   181:             XEiJ.dbgShowPopup (me, ddpTextArea, true);
   182:           }
   183:         }
   184:       });
   185: 
   186:     //ボタンのアクションリスナー
   187:     ActionListener listener = new ActionListener () {
   188:       @Override public void actionPerformed (ActionEvent ae) {
   189:         Object source = ae.getSource ();
   190:         switch (ae.getActionCommand ()) {
   191:         case "Backtrace":
   192:           if (BranchLog.BLG_ON) {
   193:             ddpBacktraceOn = ((JCheckBox) ae.getSource ()).isSelected ();
   194:             if (XEiJ.dbgEventMask == 0) {
   195:               ddpUpdate (ddpAddressArray[ddpItemIndex], ddpSupervisorMode, true);
   196:             }
   197:           }
   198:           break;
   199:         case "User/Supervisor":  //ユーザ/スーパーバイザ
   200:           if (XEiJ.dbgEventMask == 0) {
   201:             ddpUpdate (ddpAddressArray[ddpItemIndex], ((JCheckBox) ae.getSource ()).isSelected () ? 1 : 0, true);
   202:           }
   203:           break;
   204:         case "Address and/or offset":  //アドレスとオフセット
   205:           ddpProgramMode = ddpProgramButton.getIndex ();
   206:           if (XEiJ.dbgEventMask == 0) {
   207:             ddpUpdate (0, ddpSupervisorMode, true);
   208:           }
   209:           break;
   210:         case "Program name":  //プログラム名
   211:           {
   212:             int index = ddpProgramBox.getSelectedIndex ();
   213:             if (0 <= index) {
   214:               if (index == 0) {
   215:                 ddpSpinner.setOffset (0);
   216:               } else {
   217:                 if (ddpProgramAge == LabeledAddress.lblProgramAge) {  //念の為
   218:                   ddpSpinner.setOffset (LabeledAddress.lblProgramHead[index - 1]);
   219:                 }
   220:               }
   221:             }
   222:           }
   223:           break;
   224:         }
   225:       }
   226:     };
   227: 
   228:     //アドレスとオフセットボタン
   229:     ddpProgramMode = 0;
   230:     ddpProgramButton = Multilingual.mlnToolTipText (
   231:       ComponentFactory.createRotaryButton (
   232:         "Address and/or offset",
   233:         listener,
   234:         ddpProgramMode,
   235:         XEiJ.createImage (
   236:           20, 14,
   237:           "22222222222222222222" +
   238:           "2..................2" +
   239:           "2........22........2" +
   240:           "2......222222......2" +
   241:           "2.....22222222.....2" +
   242:           "2.....22.22........2" +
   243:           "2.....2222222......2" +
   244:           "2......2222222.....2" +
   245:           "2........22.22.....2" +
   246:           "2.....22222222.....2" +
   247:           "2......222222......2" +
   248:           "2........22........2" +
   249:           "2..................2" +
   250:           "22222222222222222222",
   251:           LnF.lnfRGB[0],
   252:           LnF.lnfRGB[6],
   253:           LnF.lnfRGB[12]),
   254:         XEiJ.createImage (
   255:           20, 14,
   256:           "22222222222222222222" +
   257:           "2..................2" +
   258:           "2......22..........2" +
   259:           "2......22..........2" +
   260:           "2......22..........2" +
   261:           "2......22..........2" +
   262:           "2......22..........2" +
   263:           "2......22..........2" +
   264:           "2......22..........2" +
   265:           "2......22..........2" +
   266:           "2......2222222.....2" +
   267:           "2......2222222.....2" +
   268:           "2..................2" +
   269:           "22222222222222222222",
   270:           LnF.lnfRGB[0],
   271:           LnF.lnfRGB[6],
   272:           LnF.lnfRGB[12]),
   273:         XEiJ.createImage (
   274:           20, 14,
   275:           "22222222222222222222" +
   276:           "2..................2" +
   277:           "2....22....22......2" +
   278:           "2..222222..22......2" +
   279:           "2.22222222.22......2" +
   280:           "2.22.22....22......2" +
   281:           "2.2222222..22......2" +
   282:           "2..2222222.22......2" +
   283:           "2....22.22.22......2" +
   284:           "2.22222222.22......2" +
   285:           "2..222222..2222222.2" +
   286:           "2....22....2222222.2" +
   287:           "2..................2" +
   288:           "22222222222222222222",
   289:           LnF.lnfRGB[0],
   290:           LnF.lnfRGB[6],
   291:           LnF.lnfRGB[12])
   292:         ),
   293:       "ja", "アドレスとオフセット");
   294: 
   295:     //プログラム名コンボボックス
   296:     ddpProgramBox = Multilingual.mlnToolTipText (
   297:       ComponentFactory.createComboBox (0, "Program name", listener, 16, ""),
   298:       "ja", "プログラム名");
   299:     ddpProgramBox.setMaximumRowCount (16);
   300:     ddpProgramAge = -1;
   301: 
   302:     //バックトレース
   303:     if (BranchLog.BLG_ON) {
   304:       ddpBacktraceOn = false;
   305:       ddpBacktraceRecord = -1L;  //未選択
   306: 
   307:       //バックトレーススピナー
   308:       ddpBacktraceModel = new ReverseLongModel (0L, 0L, 0L, 1L);
   309:       ddpBacktraceSpinner = ComponentFactory.createNumberSpinner (ddpBacktraceModel, 10, new ChangeListener () {
   310:         @Override public void stateChanged (ChangeEvent ce) {
   311:           if (XEiJ.dbgEventMask == 0 && XEiJ.mpuTask == null) {  //MPU停止中
   312:             long record = ddpBacktraceModel.getNumber ().longValue ();
   313:             int i = (char) record << BranchLog.BLG_RECORD_SHIFT;
   314:             if (//ddpBacktraceRecord < 0L ||  //現在選択されている分岐レコードがない
   315:                 ddpBacktraceRecord < record) {  //後ろへ移動する
   316:               ddpBacktraceRecord = record;
   317:               ddpUpdate (BranchLog.blgArray[i] & ~1, BranchLog.blgArray[i] & 1, false);  //分岐レコードの先頭
   318:             } else if (record < ddpBacktraceRecord) {  //手前へ移動する
   319:               ddpBacktraceRecord = record;
   320:               ddpUpdate (BranchLog.blgArray[i + 1] & ~1, BranchLog.blgArray[i] & 1, false);  //分岐レコードの末尾
   321:             }
   322:           }
   323:         }
   324:       });
   325: 
   326:       //バックトレースチェックボックス
   327:       ddpBacktraceCheckBox =
   328:         Multilingual.mlnToolTipText (
   329:           ComponentFactory.createIconCheckBox (
   330:             ddpBacktraceOn,
   331:             XEiJ.createImage (
   332:               20, 14,
   333:               "22222222222222222222" +
   334:               "2..................2" +
   335:               "2.......1..........2" +
   336:               "2......1.1.........2" +
   337:               "2.....1...1........2" +
   338:               "2....111.111.......2" +
   339:               "2......1.1.........2" +
   340:               "2......1.111111....2" +
   341:               "2......1......1....2" +
   342:               "2......111111.1....2" +
   343:               "2...........1.1....2" +
   344:               "2...........111....2" +
   345:               "2..................2" +
   346:               "22222222222222222222",
   347:               LnF.lnfRGB[0],
   348:               LnF.lnfRGB[6],
   349:               LnF.lnfRGB[12]),
   350:             XEiJ.createImage (
   351:               20, 14,
   352:               "22222222222222222222" +
   353:               "2..................2" +
   354:               "2.......1..........2" +
   355:               "2......1.1.........2" +
   356:               "2.....1...1........2" +
   357:               "2....111.111.......2" +
   358:               "2......1.1.........2" +
   359:               "2......1.111111....2" +
   360:               "2......1......1....2" +
   361:               "2......111111.1....2" +
   362:               "2...........1.1....2" +
   363:               "2...........111....2" +
   364:               "2..................2" +
   365:               "22222222222222222222",
   366:               LnF.lnfRGB[0],
   367:               LnF.lnfRGB[12],
   368:               LnF.lnfRGB[12]),
   369:             "Backtrace", listener),
   370:           "ja", "バックトレース");
   371:     }
   372: 
   373:     //スーパーバイザチェックボックス
   374:     ddpSupervisorCheckBox =
   375:       Multilingual.mlnToolTipText (
   376:         ComponentFactory.createIconCheckBox (
   377:           ddpSupervisorMode != 0,
   378:           XEiJ.createImage (
   379:             20, 14,
   380:             "22222222222222222222" +
   381:             "2..................2" +
   382:             "2..................2" +
   383:             "2.....11....11.....2" +
   384:             "2.....11....11.....2" +
   385:             "2.....11....11.....2" +
   386:             "2.....11....11.....2" +
   387:             "2.....11....11.....2" +
   388:             "2.....11....11.....2" +
   389:             "2.....11111111.....2" +
   390:             "2.....11111111.....2" +
   391:             "2..................2" +
   392:             "2..................2" +
   393:             "22222222222222222222",
   394:             LnF.lnfRGB[0],
   395:             LnF.lnfRGB[12],
   396:             LnF.lnfRGB[12]),
   397:           XEiJ.createImage (
   398:             20, 14,
   399:             "22222222222222222222" +
   400:             "2..................2" +
   401:             "2..................2" +
   402:             "2.....11111111.....2" +
   403:             "2.....11111111.....2" +
   404:             "2.....11...........2" +
   405:             "2.....11111111.....2" +
   406:             "2.....11111111.....2" +
   407:             "2...........11.....2" +
   408:             "2.....11111111.....2" +
   409:             "2.....11111111.....2" +
   410:             "2..................2" +
   411:             "2..................2" +
   412:             "22222222222222222222",
   413:             LnF.lnfRGB[0],
   414:             LnF.lnfRGB[12],
   415:             LnF.lnfRGB[12]),
   416:           "User/Supervisor", listener),
   417:         "ja", "ユーザ/スーパーバイザ");
   418: 
   419:     //ウインドウ
   420:     ddpFrame = Multilingual.mlnTitle (
   421:       ComponentFactory.createRestorableSubFrame (
   422:         Settings.SGS_DDP_FRAME_KEY,
   423:         "Disassemble list",
   424:         null,
   425:         ComponentFactory.createBorderPanel (
   426:           ddpBoard,
   427:           ComponentFactory.createHorizontalBox (
   428:             ddpProgramButton,
   429:             ddpProgramBox,
   430:             ddpSpinner,
   431:             ddpSupervisorCheckBox,
   432:             Box.createHorizontalStrut (12),
   433:             (BranchLog.BLG_ON ?
   434:              ComponentFactory.createHorizontalBox (
   435:                ddpBacktraceCheckBox,
   436:                ddpBacktraceSpinner,
   437:                Box.createHorizontalStrut (12)) :
   438:              null),
   439:             Box.createHorizontalGlue (),
   440:             XEiJ.mpuMakeOriIllegalCheckBox (),  //ORI.B #$00,D0を不当命令とみなすチェックボックス
   441:             XEiJ.mpuMakeStopOnErrorCheckBox (),  //エラーで停止するチェックボックス
   442:             XEiJ.mpuMakeStopAtStartCheckBox (),  //実行開始位置で停止するチェックボックス
   443:             Box.createHorizontalStrut (12),
   444:             XEiJ.mpuMakeBreakButton (),  //停止ボタン
   445:             XEiJ.mpuMakeTraceButton (),  //トレース実行ボタン
   446:             XEiJ.mpuMakeTrace10Button (),  //トレース10回ボタン
   447:             XEiJ.mpuMakeTrace100Button (),  //トレース100回ボタン
   448:             XEiJ.mpuMakeStepButton (),  //ステップ実行ボタン
   449:             XEiJ.mpuMakeStep10Button (),  //ステップ10回ボタン
   450:             XEiJ.mpuMakeStep100Button (),  //ステップ100回ボタン
   451:             XEiJ.mpuMakeReturnButton (),  //ステップアンティルリターンボタン
   452:             XEiJ.mpuMakeRunButton ()  //実行ボタン
   453:             )
   454:           )
   455:         ),
   456:       "ja", "逆アセンブルリスト");
   457:     ComponentFactory.addListener (
   458:       ddpFrame,
   459:       new WindowAdapter () {
   460:         @Override public void windowClosing (WindowEvent we) {
   461:           XEiJ.dbgVisibleMask &= ~XEiJ.DBG_DDP_VISIBLE_MASK;
   462:         }
   463:       });
   464: 
   465:     ddpStoppedBy = null;
   466:     ddpStoppedAddress = -1;
   467: 
   468:   }  //ddpMake()
   469: 
   470:   //ddpUpdate (address, supervisor, forceUpdate)
   471:   //  逆アセンブルリストウインドウを更新する
   472:   //  address  -1=pcまたはpc0を表示,0=前回と同じアドレスを表示
   473:   public static void ddpUpdate (int address, int supervisor, boolean forceUpdate) {
   474: 
   475:     XEiJ.dbgEventMask++;  //構築開始
   476: 
   477:     if (address == -1) {  //pcまたはpc0を表示
   478:       ddpStoppedAddress = address = ddpStoppedBy == null ? XEiJ.regPC : XEiJ.regPC0;
   479:       forceUpdate = true;
   480:     } else if (address == 0) {  //前回と同じアドレスを表示。同じアドレスで再構築したいときに使う
   481:       address = ddpItemAddress;
   482:     }
   483: 
   484:     if (supervisor == -1) {
   485:       supervisor = XEiJ.regSRS;
   486:       forceUpdate = true;
   487:     }
   488: 
   489:     if ((ddpSupervisorMode != 0) != (supervisor != 0)) {  //ユーザ/スーパーバイザが一致しない
   490:       ddpSupervisorMode = supervisor;
   491:       forceUpdate = true;
   492:       if (ddpSupervisorCheckBox.isSelected () != (supervisor != 0)) {
   493:         ddpSupervisorCheckBox.setSelected (supervisor != 0);
   494:       }
   495:     }
   496: 
   497:     if (forceUpdate) {  //再構築要求
   498:       ddpItemCount = 0;
   499:     }
   500: 
   501:     address &= DDP_ITEM_MASK;  //目的のアドレスを含む項目の先頭アドレス
   502: 
   503:     //バックトレース
   504:     if (BranchLog.BLG_ON) {
   505:       if (XEiJ.mpuTask == null) {  //MPU停止中
   506:         long newestRecord = BranchLog.blgNewestRecord;  //最新のレコードの番号
   507:         long oldestRecord = Math.max (0L, newestRecord - 65535);  //最古のレコードの番号
   508:         if (//ddpBacktraceRecord < 0L ||  //レコードが選択されていない
   509:             ddpBacktraceRecord < oldestRecord || newestRecord < ddpBacktraceRecord) {  //選択されているレコードが存在しない
   510:           ddpBacktraceRecord = newestRecord;  //最新のレコードを選択する
   511:           ddpBacktraceModel.setMaximum (Long.valueOf (newestRecord));
   512:           ddpBacktraceModel.setValue (Long.valueOf (newestRecord));
   513:         }
   514:         if (ddpBacktraceOn) {  //バックトレースモードのとき
   515:           int i = (char) ddpBacktraceRecord << BranchLog.BLG_RECORD_SHIFT;
   516:           if (address >>> 1 < BranchLog.blgArray[i] >>> 1) {  //現在選択されているレコードよりも前
   517:             if (oldestRecord < ddpBacktraceRecord) {  //直前にレコードがある
   518:               ddpBacktraceRecord--;  //直前のレコードを選択する
   519:               ddpBacktraceModel.setValue (Long.valueOf (ddpBacktraceRecord));
   520:               address = BranchLog.blgArray[((char) ddpBacktraceRecord << BranchLog.BLG_RECORD_SHIFT) + 1] & ~1;  //直前のレコードの末尾に移動する
   521:             }
   522:           } else if (BranchLog.blgArray[i + 1] >>> 1 < address >>> 1) {  //現在選択されているレコードよりも後
   523:             if (ddpBacktraceRecord < newestRecord) {  //直後にレコードがある
   524:               ddpBacktraceRecord++;  //直後のレコードを選択する
   525:               ddpBacktraceModel.setValue (Long.valueOf (ddpBacktraceRecord));
   526:               address = BranchLog.blgArray[(char) ddpBacktraceRecord << BranchLog.BLG_RECORD_SHIFT] & ~1;  //直後のレコードの先頭に移動する
   527:             }
   528:           }
   529:         }
   530:       }
   531:     }
   532: 
   533:     if (ddpItemCount != 0) {  //構築前または再構築要求のいずれでもない
   534:       int i = Arrays.binarySearch (ddpAddressArray, 1, ddpItemCount, address + 1);  //項目の先頭のときも次の項目を検索してから1つ戻る
   535:       i = (i >> 31 ^ i) - 1;  //目的のアドレスを含む項目の番号
   536:       if (0 < i && i < ddpItemCount - 1 &&  //ページの内側
   537:           ddpAddressArray[i] == address &&  //項目の先頭
   538:           !ddpDCWArray[i]) {  //.dc.wで出力されていない
   539: 
   540:         //再構築しない
   541: 
   542:         ddpItemAddress = address;
   543:         if (ddpItemIndex != i) {  //キャレットがある項目を変更する必要がある
   544:           ddpItemIndex = i;
   545:           ddpTextArea.setCaretPosition (ddpCaretArray[i]);
   546:         }
   547: 
   548:         //!
   549:         //バックトレースモードのとき分岐レコードの範囲が変わったときはハイライト表示を更新する
   550: 
   551:         XEiJ.dbgEventMask--;  //構築終了
   552:         return;
   553:       }
   554:     }
   555: 
   556:     //再構築する
   557:     ddpItemAddress = address;
   558: 
   559:     //構築前または再構築要求または先頭または末尾の番兵が選択された
   560:     //  0x00000000の境界を跨ぐとき反対側を指すことがあるので先頭と末尾の番兵を区別しない
   561:     ddpPageAddress = address & DDP_PAGE_MASK;  //ページの先頭アドレス
   562:     int pageEndAddress = ddpPageAddress + DDP_PAGE_SIZE;  //ページの末尾アドレス。0になることがある
   563: 
   564:     //幅を決める
   565:     int dataBytes = 10;  //1行あたりのデータのバイト数
   566:     int addressWidth;  //アドレスの幅
   567:     int codeWidth;  //コードの幅
   568:     if (ddpProgramMode == 0) {  //アドレスのみ
   569:       //          1111111111222222222233333333334444444444555555555566666666667777777777
   570:       //01234567890123456789012345678901234567890123456789012345678901234567890123456789
   571:       //aaaaaaaa  dddddddddddddddddddd  cccccccccccccccccccccccccccccccccccc..........
   572:       //          +0+1+2+3+4+5+6+7+8+9                                              ▲
   573:       //          +0+1+2+3+4+5+6+7+8+9                                              ▼
   574:       addressWidth = 8;
   575:       codeWidth = 36;
   576:     } else if (ddpProgramMode == 1) {  //オフセットのみ
   577:       //          1111111111222222222233333333334444444444555555555566666666667777777777
   578:       //01234567890123456789012345678901234567890123456789012345678901234567890123456789
   579:       //Loooooo  dddddddddddddddddddd  ccccccccccccccccccccccccccccccccccccc..........
   580:       //         +0+1+2+3+4+5+6+7+8+9                                               ▲
   581:       //         +0+1+2+3+4+5+6+7+8+9                                               ▼
   582:       addressWidth = 7;
   583:       codeWidth = 37;
   584:     } else {  //両方
   585:       //          111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999
   586:       //0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
   587:       //aaaaaaaa:Loooooo  dddddddddddddddddddd  cccccccccccccccccccccccccccccccccccccccccccccccc..........
   588:       //                  +0+1+2+3+4+5+6+7+8+9                                                           ▲
   589:       //                  +0+1+2+3+4+5+6+7+8+9                                                           ▼
   590:       addressWidth = 16;
   591:       codeWidth = 48;
   592:     }
   593:     String header = (new StringBuilder ().
   594:                      append (XEiJ.DBG_SPACES, 0, addressWidth + 2).
   595:                      append ("+0+1+2+3+4+5+6+7+8+9").
   596:                      append (XEiJ.DBG_SPACES, 0, 2 + codeWidth + dataBytes - 2).
   597:                      append ("▲\n")).toString ();
   598:     String footer = (new StringBuilder ().
   599:                      append (XEiJ.DBG_SPACES, 0, addressWidth + 2).
   600:                      append ("+0+1+2+3+4+5+6+7+8+9").
   601:                      append (XEiJ.DBG_SPACES, 0, 2 + codeWidth + dataBytes - 2).
   602:                      append ("▼")).toString ();
   603: 
   604:     //先頭の番兵
   605:     ddpAddressArray[0] = ddpPageAddress - DDP_ITEM_SIZE;  //昇順を維持するためマスクしない
   606:     ddpSplitArray[0] = 0;
   607:     ddpCaretArray[0] = 0;
   608:     StringBuilder sb = new StringBuilder (header);
   609:     int itemCount = 1;  //項目数
   610:     int itemAddress = ddpPageAddress;  //項目の先頭アドレス
   611:     int dcwAddress = pageEndAddress;  //.dc.wで出力する範囲
   612:     int dcwEndAddress = pageEndAddress;
   613:     boolean prevBranchFlag = false;  //true=直前が完全分岐命令だった
   614: 
   615:     //ラベル
   616:     if (LabeledAddress.lblProgramCount == 0) {
   617:       LabeledAddress.lblUpdateProgram ();
   618:     }
   619:     if (ddpProgramAge != LabeledAddress.lblProgramAge) {  //更新が必要
   620:       ddpProgramAge = LabeledAddress.lblProgramAge;
   621:       ddpProgramBox.removeAllItems ();
   622:       ddpProgramBox.addItem ("raw address");
   623:       for (int i = 0; i < LabeledAddress.lblProgramCount; i++) {
   624:         ddpProgramBox.addItem (XEiJ.fmtHex8 (new StringBuilder (), LabeledAddress.lblProgramHead[i]).append (' ').append (LabeledAddress.lblProgramName[i]).toString ());
   625:       }
   626:     }
   627: 
   628:     TreeMap<Integer,InstructionBreakPoint.InstructionBreakRecord> pointTable;
   629:     if (InstructionBreakPoint.IBP_ON) {
   630:       pointTable = InstructionBreakPoint.ibpPointTable;
   631:     }
   632: 
   633:   itemLoop:
   634:     do {
   635:       int itemEndAddress;  //項目の末尾アドレス
   636:       String code;  //逆アセンブル結果
   637: 
   638:       //逆アセンブルする
   639:       //  以下のアドレスを跨いでしまうときは逆アセンブルせず1ワードずつ.dc.wまたはmoveqで出力する
   640:       //    目的のアドレス
   641:       //    命令ブレークポイント
   642:       //  途中の行をクリックすることで途中から逆アセンブルし直せるようにするため、複数ワードあっても1行にまとめない
   643:       //  途中に逆アセンブルできる命令があっても、跨いではいけないアドレスまですべて1ワードずつ出力する
   644:       if (dcwAddress <= itemAddress && itemAddress < dcwEndAddress) {  //.dc.wで出力中
   645:         Disassembler.disStatus = 0;  //念のため
   646:         int oc = MC68060.mmuPeekWordZeroCode (itemAddress, supervisor);
   647:         if ((oc & 0xfe00) == 0x7000 && MC68060.mmuPeekWordZeroCode (itemAddress + 2, supervisor) == 0x4e4f) {  //moveq.l #$xx,d0;trap#15
   648:           //pcがIOCSコールのtrap#15を指しているときmoveqが.dc.wになってしまうのを避ける
   649:           XEiJ.fmtHex2 (DDP_MOVEQD0_BASE, 10, oc);
   650:           code = String.valueOf (DDP_MOVEQD0_BASE);
   651:         } else {
   652:           XEiJ.fmtHex4 (DDP_DCW_BASE, 9, oc);
   653:           code = String.valueOf (DDP_DCW_BASE);
   654:         }
   655:         itemEndAddress = itemAddress + 2;
   656:         ddpDCWArray[itemCount] = true;
   657:       } else {  //.dc.wで出力中ではない
   658:         LabeledAddress.lblGetIndex (itemAddress);
   659:         code = Disassembler.disDisassemble (new StringBuilder (), itemAddress, supervisor,
   660:                                             LabeledAddress.lblLastGetHead, LabeledAddress.lblLastGetTail, ddpProgramMode).toString ();  //逆アセンブルする
   661:         for (int t = itemAddress + 2; t < Disassembler.disPC; t += 2) {
   662:           if (t == address ||  //目的のアドレスを跨いでしまった
   663:               InstructionBreakPoint.IBP_ON && pointTable.containsKey (t)) {  //命令ブレークポイントを跨いでしまった
   664:             //!
   665:             //バックトレースモードのとき選択されている分岐レコードの先頭も跨がないようにする
   666:             //  IOCSの_B_READと_B_WRITEでmoveq.l #<data>,d0だけ変更してtrap#15に飛び込んでいるところなど
   667:             dcwAddress = itemAddress;  //.dc.wで出力し直す
   668:             dcwEndAddress = t;
   669:             continue itemLoop;
   670:           }
   671:         }
   672:         itemEndAddress = Disassembler.disPC;
   673:         ddpDCWArray[itemCount] = false;
   674:       }
   675: 
   676:       //完全分岐命令の下に隙間を空けて読みやすくする
   677:       if (prevBranchFlag) {
   678:         sb.append ('\n');
   679:       }
   680: 
   681:       //項目の開始
   682:       if (itemAddress == address) {
   683:         ddpItemIndex = itemCount;  //目的のアドレスを含む項目の番号
   684:       }
   685:       ddpAddressArray[itemCount] = itemAddress;  //項目の先頭アドレス
   686:       ddpSplitArray[itemCount] = sb.length ();  //項目を区切る位置
   687: 
   688:       if (prevBranchFlag) {
   689:         //ラベル
   690:         if (true) {
   691:           int i = sb.length ();
   692:           LabeledAddress.lblSearch (sb, itemAddress);
   693:           if (i < sb.length ()) {
   694:             sb.append ('\n');
   695:           }
   696:         }
   697:       }
   698: 
   699:       //停止理由
   700:       if (itemAddress == ddpStoppedAddress && ddpStoppedBy != null) {
   701:         sb.append (ddpStoppedBy).append ('\n');
   702:       }
   703: 
   704:       ddpCaretArray[itemCount] = sb.length ();  //項目が選択されたときキャレットを移動させる位置
   705: 
   706:       //1行目
   707:       int lineAddress = itemAddress;  //行の開始アドレス
   708:       int lineEndAddress = Math.min (lineAddress + dataBytes, itemEndAddress);  //行の終了アドレス
   709:       {
   710:         //アドレス
   711:         //  アドレスのみまたは両方のとき
   712:         //    アドレスを表示する
   713:         if (ddpProgramMode == 0 ||  //アドレスのみまたは
   714:             ddpProgramMode == 2) {  //両方のとき
   715:           XEiJ.fmtHex8 (sb, lineAddress);  //アドレスを表示する
   716:         }
   717:         //オフセット
   718:         //  オフセットのみまたは両方のとき
   719:         //    プログラムがありかつ先頭が0でないとき
   720:         //      両方のとき
   721:         //        ':'を表示する
   722:         //      'L'とオフセットを表示する
   723:         //    プログラムがないまたは先頭が0のとき
   724:         //      オフセットのみのとき
   725:         //        アドレスを表示する
   726:         //      両方のとき
   727:         //        空白を表示する
   728:         if (ddpProgramMode == 1 ||  //オフセットのみまたは
   729:             ddpProgramMode == 2) {  //両方のとき
   730:           int index = LabeledAddress.lblGetIndex (lineAddress);
   731:           if (index != -1 &&  //プログラムがありかつ
   732:               LabeledAddress.lblLastGetHead != 0) {  //先頭が0でないとき
   733:             if (ddpProgramMode == 2) {  //両方のとき
   734:               sb.append (':');  //':'を表示する
   735:             }
   736:             XEiJ.fmtHex6 (sb.append ('L'), lineAddress - LabeledAddress.lblLastGetHead);  //'L'とオフセットを表示する
   737:           } else {  //プログラムがないまたは先頭が0のとき
   738:             if (ddpProgramMode == 1) {  //オフセットのみのとき
   739:               XEiJ.fmtHex8 (sb, lineAddress);  //アドレスを表示する
   740:             } else {  //両方のとき
   741:               //          Lxxxxxx
   742:               sb.append ("       ");  //空白を表示する
   743:             }
   744:           }
   745:         }
   746:         sb.append ("  ");
   747:       }
   748:       //データ
   749:       for (int a = lineAddress; a < lineEndAddress; a += 2) {
   750:         XEiJ.fmtHex4 (sb, MC68060.mmuPeekWordZeroCode (a, supervisor));
   751:       }
   752:       sb.append (XEiJ.DBG_SPACES, 0, 2 * Math.max (0, lineAddress + dataBytes - lineEndAddress) + 2);
   753:       //逆アセンブル結果
   754:       sb.append (code).append (XEiJ.DBG_SPACES, 0, Math.max (1, codeWidth - code.length ()));
   755:       //キャラクタ
   756:       InstructionBreakPoint.InstructionBreakRecord r = InstructionBreakPoint.IBP_ON ? pointTable.get (itemAddress) : null;
   757:       if (r != null) {  //命令ブレークポイントがある
   758:         if (r.ibrWaitInstruction != null) {  //待機ポイント
   759:           sb.append ("----");
   760:         }
   761:         if (r.ibrThreshold < 0) {  //インスタント
   762:           sb.append ("******");
   763:         } else if (r.ibrThreshold != 0x7fffffff) {
   764:           sb.append (r.ibrValue).append ('/').append (r.ibrThreshold);
   765:         }
   766:       } else {  //命令ブレークポイントがない
   767:         for (int a = lineAddress; a < lineEndAddress; a++) {
   768:           int h = MC68060.mmuPeekByteZeroCode (a, supervisor);
   769:           int c;
   770:           if (0x81 <= h && h <= 0x9f || 0xe0 <= h && h <= 0xef) {  //SJISの2バイトコードの1バイト目
   771:             int l = MC68060.mmuPeekByteZeroCode (a + 1, supervisor);  //これは範囲外になる場合がある
   772:             if (0x40 <= l && l != 0x7f && l <= 0xfc) {  //SJISの2バイトコードの2バイト目
   773:               c = CharacterCode.chrSJISToChar[h << 8 | l];  //2バイトで変換する
   774:               if (c == 0) {  //対応する文字がない
   775:                 c = '※';
   776:               }
   777:               a++;
   778:             } else {  //SJISの2バイトコードの2バイト目ではない
   779:               c = '.';  //SJISの2バイトコードの1バイト目ではなかった
   780:             }
   781:           } else {  //SJISの2バイトコードの1バイト目ではない
   782:             c = CharacterCode.chrSJISToChar[h];  //1バイトで変換する
   783:             if (c < 0x20 || c == 0x7f) {  //対応する文字がないまたは制御コード
   784:               c = '.';
   785:             }
   786:           }
   787:           sb.append ((char) c);
   788:         }  //for a
   789:       }
   790:       sb.append ('\n');
   791: 
   792:       //2行目以降
   793:       while (lineEndAddress < itemEndAddress) {
   794:         lineAddress = lineEndAddress;  //行の開始アドレス
   795:         lineEndAddress = Math.min (lineAddress + dataBytes, itemEndAddress);  //行の終了アドレス
   796:         //アドレス
   797:         //  アドレスのみまたは両方のとき
   798:         //    アドレスを表示する
   799:         if (ddpProgramMode == 0 ||  //アドレスのみまたは
   800:             ddpProgramMode == 2) {  //両方のとき
   801:           XEiJ.fmtHex8 (sb, lineAddress);  //アドレスを表示する
   802:         }
   803:         //オフセット
   804:         //  オフセットのみまたは両方のとき
   805:         //    プログラムがありかつ先頭が0でないとき
   806:         //      両方のとき
   807:         //        ':'を表示する
   808:         //      'L'とオフセットを表示する
   809:         //    プログラムがないまたは先頭が0のとき
   810:         //      オフセットのみのとき
   811:         //        アドレスを表示する
   812:         //      両方のとき
   813:         //        空白を表示する
   814:         if (ddpProgramMode == 1 ||  //オフセットのみまたは
   815:             ddpProgramMode == 2) {  //両方のとき
   816:           int index = LabeledAddress.lblGetIndex (lineAddress);
   817:           if (index != -1 &&  //プログラムがありかつ
   818:               LabeledAddress.lblLastGetHead != 0) {  //先頭が0でないとき
   819:             if (ddpProgramMode == 2) {  //両方のとき
   820:               sb.append (':');  //':'を表示する
   821:             }
   822:             XEiJ.fmtHex6 (sb.append ('L'), lineAddress - LabeledAddress.lblLastGetHead);  //'L'とオフセットを表示する
   823:           } else {  //プログラムがないまたは先頭が0のとき
   824:             if (ddpProgramMode == 1) {  //オフセットのみのとき
   825:               XEiJ.fmtHex8 (sb, lineAddress);  //アドレスを表示する
   826:             } else {  //両方のとき
   827:               //          Lxxxxxx
   828:               sb.append ("       ");  //空白を表示する
   829:             }
   830:           }
   831:         }
   832:         sb.append ("  ");
   833:         //データ
   834:         for (int a = lineAddress; a < lineEndAddress; a += 2) {
   835:           XEiJ.fmtHex4 (sb, MC68060.mmuPeekWordZeroCode (a, supervisor));
   836:         }
   837:         sb.append (XEiJ.DBG_SPACES, 0, 2 * Math.max (0, lineAddress + dataBytes - lineEndAddress) + 2 + codeWidth);
   838:         //キャラクタ
   839:         for (int a = lineAddress; a < lineEndAddress; a++) {
   840:           int h = MC68060.mmuPeekByteZeroCode (a, supervisor);
   841:           int c;
   842:           if (0x81 <= h && h <= 0x9f || 0xe0 <= h && h <= 0xef) {  //SJISの2バイトコードの1バイト目
   843:             int l = MC68060.mmuPeekByteZeroCode (a + 1, supervisor);  //これは範囲外になる場合がある
   844:             if (0x40 <= l && l != 0x7f && l <= 0xfc) {  //SJISの2バイトコードの2バイト目
   845:               c = CharacterCode.chrSJISToChar[h << 8 | l];  //2バイトで変換する
   846:               if (c == 0) {  //対応する文字がない
   847:                 c = '※';
   848:               }
   849:               a++;
   850:             } else {  //SJISの2バイトコードの2バイト目ではない
   851:               c = '.';  //SJISの2バイトコードの1バイト目ではなかった
   852:             }
   853:           } else {  //SJISの2バイトコードの1バイト目ではない
   854:             c = CharacterCode.chrSJISToChar[h];  //1バイトで変換する
   855:             if (c < 0x20 || c == 0x7f) {  //対応する文字がないまたは制御コード
   856:               c = '.';
   857:             }
   858:           }
   859:           sb.append ((char) c);
   860:         }  //for a
   861:         sb.append ('\n');
   862:       }
   863: 
   864:       //項目の終了
   865:       itemCount++;
   866:       itemAddress = itemEndAddress;
   867: 
   868:       //完全分岐命令の下に隙間を空けて読みやすくする
   869:       prevBranchFlag = (Disassembler.disStatus & Disassembler.DIS_ALWAYS_BRANCH) != 0;
   870: 
   871:     } while (itemAddress < pageEndAddress);
   872: 
   873:     //末尾の番兵
   874:     ddpAddressArray[itemCount] = itemAddress;  //昇順を維持するためマスクしない
   875:     ddpSplitArray[itemCount] = sb.length ();
   876:     ddpCaretArray[itemCount] = sb.length ();
   877:     sb.append (footer);
   878:     itemCount++;
   879:     ddpItemCount = itemCount;
   880: 
   881:     //テキスト
   882:     ddpTextArea.setText (sb.toString ());
   883:     ddpTextArea.setCaretPosition (ddpCaretArray[ddpItemIndex]);
   884: 
   885:     //!
   886:     //バックトレースモードのとき選択されている分岐レコードの範囲をハイライト表示する
   887: 
   888:     //スピナー
   889:     ddpSpinner.setHintArray (ddpAddressArray, itemCount);
   890:     ddpSpinner.setHintIndex (ddpItemIndex);
   891: 
   892:     XEiJ.dbgEventMask--;  //構築終了
   893: 
   894:   }  //ddpUpdate(int,int,boolean)
   895: 
   896: }  //class DisassembleList
   897: 
   898: 
   899: