
前回の続き。連載第3回を読みながらレジスタの動作確認プログラムを書いたりとか,そんな感じ。
エラー処理をちゃんと書いてないので,変な値をレジスタにセットしようとすると落ちます。
ま,動けばいいやw
あと,ざっくり連載を最後まで読んだけど,結局これCPU作って無いじゃん!
残りはtextfieldから値を取って四則演算するだけでしょ?
つーことで,このネタはここまでにしておこう。。
IntelCpuCls
- VB.NETなんて知らないのに,本物を真似しながら作るのはしんどかった。
- あと,なんか冗長になってしまった。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class IntelCpuCls extends JFrame implements ActionListener{
RegisterCls eax = new RegisterCls();
RegisterCls ebx = new RegisterCls();
RegisterCls ecx = new RegisterCls();
RegisterCls edx = new RegisterCls();
JLabel jl_eax_title = new JLabel("EAXレジスタ");
JLabel jl_ebx_title = new JLabel("EBXレジスタ");
JLabel jl_ecx_title = new JLabel("ECXレジスタ");
JLabel jl_edx_title = new JLabel("EDXレジスタ");
JLabel jl_eax = new JLabel("EAX");
JLabel jl_ebx = new JLabel("EBX");
JLabel jl_ecx = new JLabel("ECX");
JLabel jl_edx = new JLabel("EDX");
JLabel jl_ax = new JLabel("AX");
JLabel jl_bx = new JLabel("BX");
JLabel jl_cx = new JLabel("CX");
JLabel jl_dx = new JLabel("DX");
JLabel jl_ah = new JLabel("AH");
JLabel jl_bh = new JLabel("BH");
JLabel jl_ch = new JLabel("CH");
JLabel jl_dh = new JLabel("DH");
JLabel jl_al = new JLabel("AL");
JLabel jl_bl = new JLabel("BL");
JLabel jl_cl = new JLabel("CL");
JLabel jl_dl = new JLabel("DL");
JLabel jl_eax_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_ebx_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_ecx_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_edx_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_ax_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_bx_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_cx_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_dx_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_ah_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_bh_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_ch_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_dh_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_al_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_bl_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_cl_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_dl_value = new JLabel("0",JLabel.RIGHT);
JLabel jl_reg = new JLabel("レジスタ");
JComboBox jc_reg;
JTextField jt_reg = new JTextField();
JButton jb_reg = new JButton("Set!");
//コンストラクタ
public IntelCpuCls(){
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(400,250);
this.setTitle("動作確認");
this.getContentPane().setLayout(null);
this.mySetBounds(jl_eax_title,0,0,100,20);
this.mySetBounds(jl_ebx_title,0,100,100,20);
this.mySetBounds(jl_ecx_title,200,0,100,20);
this.mySetBounds(jl_edx_title,200,100,100,20);
this.mySetBounds(jl_eax,0,20,100,20);
this.mySetBounds(jl_ebx,0,120,100,20);
this.mySetBounds(jl_ecx,200,20,100,20);
this.mySetBounds(jl_edx,200,120,100,20);
this.mySetBounds(jl_ax,0,40,100,20);
this.mySetBounds(jl_bx,0,140,100,20);
this.mySetBounds(jl_cx,200,40,100,20);
this.mySetBounds(jl_dx,200,140,100,20);
this.mySetBounds(jl_ah,0,60,100,20);
this.mySetBounds(jl_bh,0,160,100,20);
this.mySetBounds(jl_ch,200,60,100,20);
this.mySetBounds(jl_dh,200,160,100,20);
this.mySetBounds(jl_al,0,80,100,20);
this.mySetBounds(jl_bl,0,180,100,20);
this.mySetBounds(jl_cl,200,80,100,20);
this.mySetBounds(jl_dl,200,180,100,20);
this.mySetBounds(jl_eax_value,50,20,100,20);
this.mySetBounds(jl_ebx_value,50,120,100,20);
this.mySetBounds(jl_ecx_value,250,20,100,20);
this.mySetBounds(jl_edx_value,250,120,100,20);
this.mySetBounds(jl_ax_value,50,40,100,20);
this.mySetBounds(jl_bx_value,50,140,100,20);
this.mySetBounds(jl_cx_value,250,40,100,20);
this.mySetBounds(jl_dx_value,250,140,100,20);
this.mySetBounds(jl_ah_value,50,60,100,20);
this.mySetBounds(jl_bh_value,50,160,100,20);
this.mySetBounds(jl_ch_value,250,60,100,20);
this.mySetBounds(jl_dh_value,250,160,100,20);
this.mySetBounds(jl_al_value,50,80,100,20);
this.mySetBounds(jl_bl_value,50,180,100,20);
this.mySetBounds(jl_cl_value,250,80,100,20);
this.mySetBounds(jl_dl_value,250,180,100,20);
this.mySetBounds(jl_reg,0,200,60,20);
String[] s = new String[4];
s[0] = "EAX";
s[1] = "EBX";
s[2] = "ECX";
s[3] = "EDX";
jc_reg = new JComboBox(s);
this.mySetBounds(jc_reg,60,200,50,20);
this.mySetBounds(jt_reg,120,200,150,20);
this.mySetBounds(jb_reg,280,200,100,20);
jb_reg.addActionListener(this);
this.setVisible(true);
}
//部品配置用メソッド
private void mySetBounds(Component obj,int x,int y,int w,int h){
this.getContentPane().add(obj);
obj.setBounds(x,y,w,h);
}
public void actionPerformed(ActionEvent e){
Object o = e.getSource();
if(o == jb_reg){
if(jc_reg.getSelectedItem().equals("EAX")){
try{
eax.setValue32(Long.parseLong(jt_reg.getText(),16));
jl_eax_value.setText(Long.toHexString(eax.getValue32()));
jl_ax_value.setText(Long.toHexString(eax.getValue16()));
jl_ah_value.setText(Long.toHexString(eax.getValueHighByte()));
jl_al_value.setText(Long.toHexString(eax.getValueLowByte()));
}
catch(NumberFormatException ex){
System.out.println(ex);
}
}
else if(jc_reg.getSelectedItem().equals("EBX")){
try{
ebx.setValue32(Long.parseLong(jt_reg.getText(),16));
jl_ebx_value.setText(Long.toHexString(ebx.getValue32()));
jl_bx_value.setText(Long.toHexString(ebx.getValue16()));
jl_bh_value.setText(Long.toHexString(ebx.getValueHighByte()));
jl_bl_value.setText(Long.toHexString(ebx.getValueLowByte()));
}
catch(NumberFormatException ex){
System.out.println(ex);
}
}
else if(jc_reg.getSelectedItem().equals("ECX")){
try{
ecx.setValue32(Long.parseLong(jt_reg.getText(),16));
jl_ecx_value.setText(Long.toHexString(ecx.getValue32()));
jl_cx_value.setText(Long.toHexString(ecx.getValue16()));
jl_ch_value.setText(Long.toHexString(ecx.getValueHighByte()));
jl_cl_value.setText(Long.toHexString(ecx.getValueLowByte()));
}
catch(NumberFormatException ex){
System.out.println(ex);
}
}
else if(jc_reg.getSelectedItem().equals("EDX")){
try{
edx.setValue32(Long.parseLong(jt_reg.getText(),16));
jl_edx_value.setText(Long.toHexString(edx.getValue32()));
jl_dx_value.setText(Long.toHexString(edx.getValue16()));
jl_dh_value.setText(Long.toHexString(edx.getValueHighByte()));
jl_dl_value.setText(Long.toHexString(edx.getValueLowByte()));
}
catch(NumberFormatException ex){
System.out.println(ex);
}
}
}
}
public void fetch(){
}
public void decode(){
}
public void exequte(){
}
public static void main(String[] args){
new IntelCpuCls();
}
RegisterCls
- Javaだとunsignedが使えないみたいなので,全部longでやりとりするようにした。
- 今後ちょいちょい変更していくかも。。
public class RegisterCls{
//共有する値
private long reg = 0;
//32bitレジスタ
public long getValue32(){
return reg;
}
public void setValue32(long value32){
long mask = 0xFFFFFFFF;
reg = value32 & mask;
}
//16bitレジスタ
public long getValue16(){
long mask = 0x0000FFFF;
return reg & mask;
}
public void setValue16(long value16){
long mask1 = 0xFFFF0000;
long mask2 = 0x0000FFFF;
reg = (reg & mask1) + (value16 & mask2);
}
//16bitレジスタの左半分(high)
public long getValueHighByte(){
long mask = 0x0000FF00;
return ((reg & mask) >> 8);
}
public void setValueHighByte(long value8){
long mask1 = 0xFFFF00FF;
long mask2 = 0x000000FF;
reg = (reg & mask1) + ((value8 & mask2) << 8);
}
//16bitレジスタの右半分(low)
public long getValueLowByte(){
long mask = 0x000000FF;
return reg & mask;
}
public void setValueLowByte(long value8){
long mask1 = 0xFFFFFF00;
long mask2 = 0x000000FF;
reg = (reg & mask1) + (value8 & mask2);
}
}

コメントする