Line Path Bus public class Line String name
Line クラス // Path, Busの親クラスにする public class Line { String name; public Line(String name) { this. name = name; } public long get. Value() { return 0 L; } public String to. String() { return name; } }
Path クラスの拡張 public class Path extends Line { : public Path() { super(“anon”); } public Path(String name) { super(name); } public long get. Value() { if (sig. get. Value()) return 1 L; else return 0 L; } : }
Bus クラスの拡張 public class Bus extends Line { : public Bus(int num) { super(“anon”); init(num); } public Bus(int num, String name) { super(name); init(num); } private void init(int num) { paths = new Path[num]; for (int i = 0; i < num; i++) paths[i] = new Path(name + ". " + i); } : }
Circuit クラス(1/5) // 回路の親クラスにする public abstract class Circuit { String name; Line[] lines; static int run. Depth = 1; static int debug. Depth = 0; // 回路名と接続されている全ての配線を登録 public void register. Lines(String name, Line[] lines) { this. name = name; this. lines = lines; } :
Circuit クラス(2/5) : // 接続されている配線名を表示 public void print. Lines() { if (run. Depth > debug. Depth) return; System. out. print("#" + run. Depth + " " + name + ": "); for (int i = 0; i < lines. length; i++) System. out. print(" " + lines[i]); System. out. println(""); } :
Circuit クラス(3/5) : // 接続されている配線内の信号を表示 public void print. Values() { if (run. Depth > debug. Depth) return; System. out. print("#" + run. Depth + " " + name + ": "); for (int i = 0; i < lines. length; i++) System. out. print(" " + Long. to. Hex. String(lines[i]. get. Value())); System. out. println(""); } :
Circuit クラス(4/5) : // runメソッドの中で他のrunメソッドを呼ぶ前後で実行 public static void begin. Sub. Circuit() { run. Depth++; } public static void end. Sub. Circuit() { run. Depth--; } // デバッグする回路の深さを指定する public static void set. Debug. Depth(int depth) { debug. Depth = depth; } :
Circuit クラス(5/5) : // 自動的にデバッグ情報が出力されるように // これまでのrunメソッドを置き換える public void run() { print. Lines(); begin. Sub. Circuit(); run. Logic(); end. Sub. Circuit(); print. Values(); } // これまでのrunメソッドの代わり public abstract void run. Logic(); }
回路の変更 // Circuitを継承 public class MUX extends Circuit { public ANDGate(Path ctl, Path in 1, Path in 2, Path out 1) { // 入出力配線をLineの配列にして登録 Line[] lines = new Line[] { ctl, in 1, in 2, out 1 }; register. Lines(“MUX”, lines); // MUXの処理 } // runメソッドの名前をrun. Logicに変更 public void run. Logic() { // MUXの処理(そのまま) } } 回路の使い方はこれまでと同じ(runメソッドを呼ぶ)
デバッグレベルの指定 public class MIPS extends Circuit { public MIPS() { i. Mem = new Inst. Memory(. . . ); reg. File = new Register. File(. . . ); alu = new ALU 32(. . . ); } public void run. Logic() { // デバッグする回路の深さを指定 // 1にするとALU 32回路への入出力のみを表示 // 2にするとALU 32の内部回路の入出力も表示 // 0にすると何も表示しない Circuit. set. Debug. Depth(1); : i. Mem. run(); reg. File. run(); alu. run(); } }
表示例 デバッグレベル=1 #1 Inst. Mem: pc inst ←配線名 #1 Inst. Mem: 4000000 12 a 4020 ←値 : #1 Reg. File: reg. Write read 1 read 2 write 1 wdata rdata 1 rdata 2 #1 Reg. File: 1 9 a 8 100 200 100 : #1 ALU 32: op 2 op 1 op 0 rdata 1 rdata 2 result zero #1 ALU 32: 0 1 0 200 100 300 0 デバッグレベルを大きくすると実行が終わらなくなる場合もあるので注意
- Slides: 14