📄 cpu.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public
class CPU
extends Frame
implements Runnable, ActionListener
{
String currentRTL = new String();
boolean blank=false;
boolean some=false;
int m =0;
int n =0;
int o =0;
int m2 =0;
int n2 =0;
int o2 =0;
int m3 =0;
int n3 =0;
int o3 =0;
int m4 =0;
int n4 =0;
int o4 =0;
int m5 =0;
int n5 =0;
int o5 =0;
int m6 =0;
int n6 =0;
int o6 =0;
int m7 =0;
int n7 =0;
int o7 =0;
int m8 =0;
int n8 =0;
int o8 =0;
int m9 =0;
int n9 =0;
int o9 =0;
int m10 =0;
int n10 =0;
int o10 =0;
int m11 =0;
int n11 =0;
int o11 =0;
int m12 =0;
int n12 =0;
int o12 =0;
int m13 =0;
int n13 =0;
int o13 =0;
int m14 =0;
int n14 =0;
int o14 =0;
int m15 =0;
int n15 =0;
int o15 =0;
int m16 =0;
int n16 =0;
int o16 =0;
int m17 =0;
int n17 =0;
int o17 =0;
int m18 =0;
int n18 =0;
int o18 =0;
int m19 =0;
int n19 =0;
int o19 =0;
int m20 =0;
int n20 =0;
int o20 =0;
int m21 =0;
int n21 =0;
int o21 =0;
int m23 =0;
int n23 =0;
int o23 =0;
int m24 =0;
int n24 =0;
int o24 =0;
int dot1=0;
int dot2=0;
int dot3=0;
int dot4=0;
public void clear()
{
dot1=0;
dot2=0;
dot3=0;
dot4=0;
m =0;
n =0;
o =0;
m2 =0;
n2 =0;
o2 =0;
m3 =0;
n3 =0;
o3 =0;
m4 =0;
n4 =0;
o4 =0;
m5 =0;
n5 =0;
o5 =0;
m6 =0;
n6 =0;
o6 =0;
m7 =0;
n7 =0;
o7 =0;
m8 =0;
n8 =0;
o8 =0;
m9 =0;
n9 =0;
o9 =0;
m10 =0;
n10 =0;
o10 =0;
m11 =0;
n11 =0;
o11 =0;
m12 =0;
n12 =0;
o12 =0;
m13 =0;
n13 =0;
o13 =0;
m14 =0;
n14 =0;
o14 =0;
m15 =0;
n15 =0;
o15 =0;
m16 =0;
n16 =0;
o16 =0;
m17 =0;
n17 =0;
o17 =0;
m18 =0;
n18 =0;
o18 =0;
m19 =0;
n19 =0;
o19 =0;
m20 =0;
n20 =0;
o20 =0;
m21 =0;
n21 =0;
o21 =0;
m23 =0;
n23 =0;
o23 =0;
m24 =0;
n24 =0;
o24 =0;
currentRTL="";
}
CPU( Box parent, String windowTitle )
{
super( windowTitle );
theBox = ( Box ) parent;
setMenu();
// cpuCanvasScrollPane =
// new ScrollPane()
// {
// public Dimension getPreferredSize()
// {
// Dimension preferredSize = cpuCanvas.getPreferredSize();
// preferredSize.height += getHScrollbarHeight();
// preferredSize.width += getVScrollbarWidth();
// return ( preferredSize );
// }
// };
// cpuCanvasScrollPane.add( cpuCanvas );
// add( cpuCanvasScrollPane );
add( cpuCanvas );
memoryBox = new Memory( this, "Memory Display" );
aluBox = new Alu( this, "ALU Display" );
controlUnit = new CU( this, "Control Unit Display" );
mcu = new McodeControlUnit( this, "Control Unit Display" );
bp = new Breakpoints( theBox, this, "Breakpoints Display" );
reset();
addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
closeWindow();
}
}
);
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension cpuFrameSize = getSize();
Dimension memoryBoxSize = memoryBox.getSize();
Point memoryBoxLocation = new Point( Math.min( screenSize.width -
memoryBoxSize.width, cpuFrameSize.width ) , 0 );
memoryBox.setLocation( memoryBoxLocation );
Dimension aluBoxSize = aluBox.getSize();
Point aluBoxLocation = new Point( Math.min( screenSize.width -
aluBoxSize.width, cpuFrameSize.width ), Math.min( screenSize.
height, cpuFrameSize.height ) - aluBoxSize.height );
aluBox.setLocation( aluBoxLocation );
Dimension controlUnitSize = controlUnit.getSize();
Point controlUnitLocation = new Point( Math.min( screenSize.width -
controlUnitSize.width, cpuFrameSize.width ), Math.min(
memoryBoxSize.height, cpuFrameSize.height - controlUnitSize.
height ) );
controlUnit.setLocation( controlUnitLocation );
Dimension mcuSize = mcu.getSize();
Point mcuLocation = new Point( Math.min( screenSize.width - mcuSize.
width, cpuFrameSize.width ), Math.min( memoryBoxSize.height,
cpuFrameSize.height - mcuSize.height ) );
mcu.setLocation( mcuLocation );
Dimension bpSize = bp.getSize();
Point bpLocation = new Point( 0, cpuFrameSize.height - bpSize.
height );
bp.setLocation( bpLocation );
}
public void setMenu()
{
MenuBar mbar = new MenuBar();
setMenuBar( mbar );
runMenu = new Menu( "Run" );
newSimulationMenuItem = new MenuItem( "Start New Simulation", new
MenuShortcut( 'N' ,false) );
newSimulationMenuItem.addActionListener( this );
startCPUMenuItem = new MenuItem( "Start CPU (Go)", new MenuShortcut(
'G' ,false) );
startCPUMenuItem.addActionListener( this );
stopCPUMenuItem = new MenuItem( "Stop CPU", new MenuShortcut(
'S' ,false) );
stopCPUMenuItem.addActionListener( this );
resetCPUMenuItem = new MenuItem( "Reset CPU", new MenuShortcut(
'R', false ) );
resetCPUMenuItem.addActionListener( this );
stepInstructionMenuItem = new MenuItem( "Step Through Instruction",
new MenuShortcut( 'I' ,false) );
stepInstructionMenuItem.addActionListener( this );
stepCycleMenuItem = new MenuItem( "Step Through Cycle", new
MenuShortcut( 'C' ,false) );
stepCycleMenuItem.addActionListener( this );
closeWindowMenuItem = new MenuItem( "Close Window" );
closeWindowMenuItem.addActionListener( this );
runMenu.add( newSimulationMenuItem );
runMenu.addSeparator();
runMenu.add( startCPUMenuItem );
runMenu.add( stopCPUMenuItem );
runMenu.add( resetCPUMenuItem );
runMenu.addSeparator();
runMenu.add( stepInstructionMenuItem );
runMenu.add( stepCycleMenuItem );
runMenu.addSeparator();
runMenu.add( closeWindowMenuItem );
mbar.add( runMenu );
editMenu = new Menu( "Edit" );
editARMenuItem = new MenuItem( "Address Register (AR)" );
editARMenuItem.addActionListener( this );
editPCMenuItem = new MenuItem( "Program Counter (PC)" );
editPCMenuItem.addActionListener( this );
editDRMenuItem = new MenuItem( "Data Register (DR)" );
editDRMenuItem.addActionListener( this );
// editTRMenuItem = new MenuItem( "Temporary Register (TR)" );
// editTRMenuItem.addActionListener( this );
editIRMenuItem = new MenuItem( "Instruction Register (IR)" );
editIRMenuItem.addActionListener( this );
// editRMenuItem = new MenuItem( "General Purpose Register (R)" );
// editRMenuItem.addActionListener( this );
editACMenuItem = new MenuItem( "Accumulator (AC)" );
editACMenuItem.addActionListener( this );
// editZMenuItem = new MenuItem( "Zero Flag (Z)" );
// editZMenuItem.addActionListener( this );
clearRegistersMenuItem = new MenuItem( "Clear Registers" );
clearRegistersMenuItem.addActionListener( this );
editMenu.add( editARMenuItem );
editMenu.add( editPCMenuItem );
editMenu.add( editDRMenuItem );
// editMenu.add( editTRMenuItem );
editMenu.add( editIRMenuItem );
// editMenu.add( editRMenuItem );
editMenu.add( editACMenuItem );
// editMenu.add( editZMenuItem );
editMenu.addSeparator();
editMenu.add( clearRegistersMenuItem );
mbar.add( editMenu );
viewMenu = new Menu( "View" );
skipAnimation = false;
animationMenuItem = new CheckboxMenuItem( "Animation", true );
animationMenuItem.addItemListener(
new ItemListener()
{
public void itemStateChanged( ItemEvent e )
{
if ( animationMenuItem.getState() )
{
skipAnimation = false;
mcu.setSkipAnimation( false );
}
else
{
skipAnimation = true;
mcu.setSkipAnimation( true );
}
}
}
);
viewMenu.add( animationMenuItem );
viewMenu.addSeparator();
traceViewMenuItem = new CheckboxMenuItem("View Trace by Cycle", true);
traceViewMenuItem.addItemListener(
new ItemListener()
{
public void itemStateChanged( ItemEvent e )
{
if ( traceViewMenuItem.getState() )
{
some = false;
//traceViewMenuItem.setLabel("View Trace by Instruction");
}
else
{
some = true;
//traceViewMenuItem.setLabel("View Trace by Cycle");
}
}
}
);
viewMenu.add(traceViewMenuItem);
viewMenu.addSeparator();
binViewMenuItem = new CheckboxMenuItem("View Numbers in Binary", true);
binViewMenuItem.addItemListener(
new ItemListener()
{
public void itemStateChanged( ItemEvent e )
{
if ( binViewMenuItem.getState() )
{
bin=true;
canvasRepaint();
}
else
{
bin=false;
canvasRepaint();
}
}
}
);
viewMenu.add(binViewMenuItem);
mbar.add( viewMenu );
}
public void restart()
{
clear();
restartAnimation();
controlUnit.clear();
mcu.reset();
irChanged = false;
memoryBox.repaint();
controlUnit.repaint();
mcu.canvasRepaint();
cpuCanvas.repaint();
}
public void restartAnimation()
{
stopAnimation();
counter1 = 0;
counter2 = 0;
stallAnimationCounter = false;
atBeginBreak = true;
atCycleBegin = true;
jumpDecisionMade = false;
jumpDecisionClockDisabled = false;
}
public void reset()
{
clearRegisters();
restart();
}
public void clearRegisters()
{
theBox.AR = "000000";
theBox.PC = "000000";
theBox.DR = "00000000";
// theBox.TR = "00000000";
theBox.IR = "00";
// theBox.R = "00000000";
theBox.AC = "00000000";
// theBox.Z = "0";
theBox.ARint = 0;
theBox.PCint = 0;
theBox.DRint = 0;
// theBox.TRint = 0;
theBox.IRint = 0;
// theBox.Rint = 0;
theBox.ACint = 0;
// theBox.Zint = 0;
simulationStartNumber = 0;
registerAR = 0;
registerPC = 0;
registerDR = 0;
// registerTR = 0;
registerIR = 0;
// registerR = 0;
registerAC = 0;
// flagZ = 0;
}
public void startAnimation()
{
disableComponents();
if ( runner == null )
{
runner = new Thread( this );
canvasRepaint( 0 );
runner.start();
}
}
public void stopAnimation()
{
runner = null;
canCycleBreak = false;
cycleBreakNow = false;
canInstructionBreak = false;
instructionBreakNow = false;
enableComponents();
canvasRepaint();
controlUnit.repaint();
mcu.canvasRepaint();
memoryBox.repaint();
}
public void setStallAnimationCounter( boolean stall )
{
stallAnimationCounter = stall;
}
public boolean getStallAnimationCounter()
{
return ( stallAnimationCounter );
}
public void actionPerformed( ActionEvent e )
{
Object eventSource = e.getSource();
if ( eventSource == newSimulationMenuItem )
{
restart();
editRegisterDialog = new EditRegisterDialog( this,
"Edit Start Address", true, EditRegisterDialog.REG_SA, true );
Dimension screenSize = Toolkit.getDefaultToolkit().
getScreenSize();
Dimension editRegisterDialogSize = editRegisterDialog.getSize();
Point editRegisterDialogLocation = new Point( ( screenSize.width -
editRegisterDialogSize.width ) / 2, ( screenSize.height -
editRegisterDialogSize.height ) / 2 );
editRegisterDialog.setLocation( editRegisterDialogLocation );
editRegisterDialog.setVisible( true );
simulationStartNumber = ( int ) theBox.PCint;
TraceResults.traceBox.setText(" ");
TraceResults.traceBox.append("Trace of Program:" + newline + newline);
if(some){
TraceResults.traceBox.append(newline);
TraceResults.traceBox.append(" AR PC DR IR AC" + newline);
}
startAnimation();
hasStarted = true;
}
else if ( eventSource == startCPUMenuItem )
{
if ( runner == null )
{
startAnimation();
}
}
else if ( eventSource == stopCPUMenuItem )
{
stopAnimation();
}
else if ( eventSource == resetCPUMenuItem )
{
reset();
}
else if ( eventSource == closeWindowMenuItem )
{
closeWindow();
}
else if ( eventSource == editARMenuItem )
{
editRegisterDialog = new EditRegisterDialog( this,
"Edit Register", true, EditRegisterDialog.REG_AR );
Dimension screenSize = Toolkit.getDefaultToolkit().
getScreenSize();
Dimension editRegisterDialogSize = editRegisterDialog.getSize();
Point editRegisterDialogLocation = new Point( ( screenSize.width -
editRegisterDialogSize.width ) / 2, ( screenSize.height -
editRegisterDialogSize.height ) / 2 );
editRegisterDialog.setLocation( editRegisterDialogLocation );
editRegisterDialog.setVisible( true );
}
else if ( eventSource == editPCMenuItem )
{
editRegisterDialog = new EditRegisterDialog( this,
"Edit Register", true, EditRegisterDialog.REG_PC );
Dimension screenSize = Toolkit.getDefaultToolkit().
getScreenSize();
Dimension editRegisterDialogSize = editRegisterDialog.getSize();
Point editRegisterDialogLocation = new Point( ( screenSize.width -
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -