Commit d789114d authored by Melanie Jakob's avatar Melanie Jakob

Neuerstellung TMA-Maschine

parent 6fd48e57
/bin
\ No newline at end of file
......@@ -78,6 +78,10 @@ public class Cell extends JLabel implements MouseListener
defineType( "String", Color.cyan );
defineType( "Register", Color.blue, Color.white, 70, stdHeight, NOPOINTER );
defineType( "Pointer", Color.yellow, Color.black, stdWidth, stdHeight, POINTER );
defineType( "RQ", Color.blue, Color.white, 30, stdHeight, NOPOINTER);
defineType("RQ2", Color.blue, Color.white, 20, stdHeight, NOPOINTER);
defineType("JTable", Color.blue, Color.white, 50, stdHeight, NOPOINTER);
}
/**
......@@ -416,7 +420,7 @@ public class Cell extends JLabel implements MouseListener
/**
Aktualisiert den Zeiger der Zelle.
*/
void updatePointer()
public void updatePointer()
{
if ( isPointer )
if( pointer!=null ) pointer.update();
......
......@@ -239,4 +239,8 @@ public class CellGroup extends ArrayList
{
setLocation( location.x, location.y );
}
public void removeandsetlocation(int place){
}
}
\ No newline at end of file
......@@ -3,7 +3,11 @@
package vam.base;
import java.util.*;
import javax.swing.*;
import vam.machines.tma.ReadyQueue;
import java.awt.*;
class CellState {
......@@ -56,6 +60,8 @@ class State {
private History history;
private LinkedList regValues;
private LinkedList rqBooleans;
private LinkedList rqValues;
private HashMap mem;
private java.util.Stack trailCopy;
......@@ -78,6 +84,22 @@ class State {
regValues.add(new Integer(reg.value));
}
//store values of the ready queue
rqValues = new LinkedList();
ListIterator itb = history.rq.listIterator();
while(itb.hasNext()) {
ReadyQueue rq = (ReadyQueue)itb.next();
rqValues.add(new Integer(rq.get()));
}
//store values of the ready queue
rqBooleans = new LinkedList();
ListIterator itb2 = history.rq.listIterator();
while(itb2.hasNext()) {
ReadyQueue rq = (ReadyQueue)itb2.next();
rqBooleans.add(new Boolean(rq.isBelegt()));
}
mem = new HashMap();
Iterator it2 = history.vm.memory.memory.keySet().iterator();
while(it2.hasNext()) {
......@@ -121,6 +143,11 @@ class State {
ListIterator it = history.regs.listIterator();
ListIterator it2 = regValues.listIterator();
ListIterator itb = history.rq.listIterator();
ListIterator it2b = rqValues.listIterator();
ListIterator it3b = rqBooleans.listIterator();
while(it.hasNext()) {
Register reg = (Register)it.next();
......@@ -131,6 +158,29 @@ class State {
reg.updatePointer();
}
while(itb.hasNext()) {
ReadyQueue red = (ReadyQueue)itb.next();
Integer v = (Integer)it2b.next();
Boolean b = (Boolean)it3b.next();
history.vm.addVisualization(red);
if(b == true){
red.set(v.intValue());
red.setBelegt(b);
red.updatePointer();}
else {red.setValue("");
red.setBelegt(b);}
//red.set(v.intValue());
//red.setBelegt(b);
//red.updatePointer();
/*if(b == true){
red.set(v.intValue());
red.setBelegt(b);
red.updatePointer();}
else {red.setValue("");
red.setBelegt(b);}*/
}
history.vm.memory.addressForNewHeapObjects = addressForNewHeapObjects;
history.vm.memory.newHeapObjectLocation = newHeapObjectLocation;
......@@ -149,6 +199,7 @@ class State {
public class History {
public ViewMachine vm;
public LinkedList regs;
public LinkedList rq;
public java.util.Stack trail;
private java.util.Stack history;
......@@ -156,6 +207,7 @@ public class History {
this.vm = vm;
history = new java.util.Stack();
regs = new LinkedList();
rq = new LinkedList();
trail = null;
}
......@@ -178,6 +230,11 @@ public class History {
public void register(Register reg) {
regs.add(reg);
}
public void readyqueue(ReadyQueue red){
rq.add(red);
}
public void registerTrail(java.util.Stack trail) {
this.trail = trail;
......
package vam.base;
import java.util.HashMap;
import javax.swing.*;
import java.awt.*;
import vam.machines.tma.*;
import vam.machines.tma.Thread;
/**
Speicherverwaltung.
......@@ -16,6 +21,8 @@ public class Memory
// Laufzeitumgebeung
ViewMachine vm;
public int ct; // von mir fr neue Berechnung von Location
// (CMa)-Heap
public int addressForNewHeapObjects;
public Point newHeapObjectLocation;
......@@ -621,7 +628,14 @@ public class Memory
Point calculateLocation( int address )
{ // berechnet die Koordinaten fr eine Zelle zurck
Point p = new Point(0,0);
//bei der tma Maschine wird die Berechnung pro Stack vorgenommen
String ext = ViewMachine.ext;
if (ext.equals("tma")){
p = calculateLocationTma(address);
}
else {
// Stack
if ( 0<=address && address<=500 )
{
......@@ -632,7 +646,7 @@ public class Memory
p.setLocation( trans.x,trans.y );
}
else p.setLocation(100,vm.view.getHeight()-30);
p.translate( (address/cellsPerColumn)*(Cell.stdWidth+30),-(address % cellsPerColumn)*Cell.stdHeight );
p.translate( ((address-ct*100)/cellsPerColumn)*(Cell.stdWidth+30),-((address-ct*100) % cellsPerColumn)*Cell.stdHeight );
}
// CMa Heap
else if ( 500<address && address<=1000 )
......@@ -655,9 +669,69 @@ public class Memory
if ( newHeapObjectLocation.y < 10 ) newHeapObjectLocation.setLocation(newHeapObjectLocation.x+160,600);
p.setLocation(newHeapObjectLocation);
}
}
return p;
}
/**
Berechnet Koordinaten zur Darstellung einer Adresse fr die TMA Maschine.
@param address Adresse
@return Koordinaten
*/
Point calculateLocationTma( int address ){
Point p = new Point(50,0);
ct = vam.machines.tma.MainCommand.CT.get();;
int differenz = 100; //Platz zwischen den Stacks
int startwidth = 150; //Anfangspunkt des ersten Stacks
Thread thread = Threadtable.list.get(ct);
int bound = vm.getStackBound();
/*if(thread.getStack().getSize() >= bound )
{
vm.finalizeStackVisualization(true, address, 1);
}
else{
*/// Stack
if ( 0<=address && address<=700 )
{
int cellsPerColumn = (vm.view.getHeight()-30)/Cell.stdHeight;
if ( exists(0) )
{
Point trans = getCell(0).getLocation();
if (address < MainCommand.getStackspace()){
p.setLocation( trans.x*(ct+1),trans.y );
}
else {p.setLocation( startwidth+differenz*(ct),trans.y );}
}
else p.setLocation(startwidth+differenz*(ct),vm.view.getHeight()-30);
p.translate( ((address-ct*differenz)/cellsPerColumn)*(Cell.stdWidth+30),-((address-ct*differenz) % cellsPerColumn)*Cell.stdHeight );
}
// CMa Heap
else
{
if ( exists(1200) )
{ // Keller-Zellen auch bereineander nach interaktiver Verschiebung
Point trans = new Point(vm.view.getWidth()-100, 30);
p.setLocation( trans.x,trans.y );
}
int top = 1200;
int count1 = address;
int count2 = 0;
int cellsPerColumn = (vm.view.getHeight()-30)/Cell.stdHeight;
p.setLocation(vm.view.getWidth()-100, 0);
while (top-count1 >= cellsPerColumn){
count1 = count1 + cellsPerColumn;
}
p.translate( -(count2)*(Cell.stdWidth+30), (1200-count1)*Cell.stdHeight );
}
return p;
}
/**
Liefert Koordinaten einer Adresse.
@param address Adresse
......
package vam.base;
import java.util.HashMap;
import javax.swing.*;
import java.awt.*;
......@@ -10,6 +12,8 @@ Speicherverwaltung.
*/
public class Memory2 extends Memory
{
public Memory2( ViewMachine vm )
{
super(vm);
......@@ -30,6 +34,11 @@ public class Memory2 extends Memory
*/
public void store( int sourceAddress, int destinationAddress, int number, boolean copy, boolean ascending, String type )
{
String ext = ViewMachine.ext;
if (ext.equals("tma")){
super.store(sourceAddress,destinationAddress,number,copy,ascending,type);
}
else{
// Adress-Fehler abfangen
if (number<1) return;
......@@ -149,8 +158,9 @@ public class Memory2 extends Memory
else
setCell( destAddress, c[i],type );
}
}
}
// ***************
// * Operationen *
......@@ -270,6 +280,12 @@ public class Memory2 extends Memory
Point calculateLocation( int address )
{ // berechnet die Koordinaten fr eine Zelle zurck
Point p = new Point(0,0);
String ext = ViewMachine.ext;
if (ext.equals("tma")){
p=super.calculateLocation(address);
}
else{
// Stack
if ( 0<=address && address<=500 )
......@@ -312,9 +328,10 @@ public class Memory2 extends Memory
if ( newHeapObjectLocation.y < 10 ) newHeapObjectLocation.setLocation(newHeapObjectLocation.x+160,600);
p.setLocation(newHeapObjectLocation);
}
}
return p;
}
......@@ -355,7 +372,11 @@ public class Memory2 extends Memory
cell.highlight(false);
}
if(address >=1 && address <= 500) {
int a = address;
while(a >= 100){
a = a-100;
}
if(address >=1 && address <= 500 && a>=1) {
// es handelt sich wirklich um den Stack
// und es sind mindestens zwei Zellen da
if (vm.hiddenStackCells > 0) {
......@@ -378,6 +399,12 @@ public class Memory2 extends Memory
*/
public void setCell( int address, Cell cell, boolean visible, String type )
{
/*String ext = ViewMachine.ext;
if (ext.equals("tma")){
super.setCell(address, cell, visible, type);
}
else{*/
if ( getCell(address) != cell )
{
free( address );
......@@ -387,8 +414,11 @@ public class Memory2 extends Memory
cell.setAddress(address);
if( type!=null ) cell.setType( type );
if(address >=1 && address <= 500) {
int a = address;
while(a >= 100){
a = a-100;
}
if(address >=1 && address <= 500 && a >= 1) {
// es handelt sich wirklich um den Stack
// und es sind mindestens zwei Zellen da
if (cell.getLocation().equals(getCell(address-1).getLocation())) {
......@@ -405,6 +435,7 @@ public class Memory2 extends Memory
vm.sync();
cell.highlight( false );
}
}
// AD : setzt newHeapObjectLocation auf initialen Wert zurueck
......
package vam.base;
import java.awt.Point;
import javax.swing.*;
/**
......@@ -33,7 +34,11 @@ public class Register extends Cell
vm.history.register(this);
}
/**
public Register(String name2, String string, Point nextLocation2, boolean b) {
super( name2, string, nextLocation2, true );
}
/**
Entfernt alle Register.
*/
static public void reset()
......
......@@ -3,9 +3,13 @@ package vam.base;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import vam.machines.tma.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
......@@ -24,7 +28,7 @@ public class ViewMachine implements ActionListener
// GUI stuff
public JFrame frame;
JLayeredPane view;
JButton runButton, stepButton, backButton, compactButton;
JButton runButton, stepButton, backButton, yieldButton, compactButton;
// Options
int optionMoveStep = 8;
......@@ -73,7 +77,7 @@ public class ViewMachine implements ActionListener
public Memory memory;
ArrayList codeMem;
public vam.util.Stack stack;
public vam.util.Stack2 stack;
// changed by Alex Dann
// needed by WiM for "call f/n" instruction
......@@ -81,9 +85,12 @@ public class ViewMachine implements ActionListener
public HashMap comments;
public HashMap breakPoints;
private Cell comment;
private int stackBound;
public int stackBound;
private Cell dotsCell;
public int hiddenStackCells;
// von mir
static public String ext;
public boolean yield = false;
/**
Generiert Fenster und Laufzeitumgebung.
......@@ -107,7 +114,7 @@ public class ViewMachine implements ActionListener
comment = null;
compactHeap = false;
stackBound = 25;
stackBound = 0;
hiddenStackCells = 0;
// Animationstimer
......@@ -209,7 +216,7 @@ public class ViewMachine implements ActionListener
// changed by Alex Dann : it's public now
public static void sleep( int msec )
{
try { Thread.sleep( msec );
try { java.lang.Thread.sleep( msec );
} catch (InterruptedException e) {}
}
......@@ -405,6 +412,20 @@ public class ViewMachine implements ActionListener
});
backButton.setEnabled(false);
toolBar.add( backButton );
yieldButton = new JButton("Yield");
yieldButton.setPreferredSize( new Dimension(85,30) );
yieldButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
yield = true;
if (frozen) runAnimation();
}
});
toolBar.add( yieldButton );
toolBar.add(new JToolBar.Separator());
......@@ -1259,7 +1280,7 @@ public class ViewMachine implements ActionListener
boolean breakPoint = false;
while(true)
{
{
while( 0<= PC.get() && PC.get() < codeMem.size() )
{
goBackFromEnd = false;
......@@ -1314,16 +1335,31 @@ public class ViewMachine implements ActionListener
executing = true;
history.save();
}
highlightCodeLine( PC.get() );
if(yield){
Command c = createCommand("vam.machines.tma","YIELD");
codeMem.add(PC.get(), c);
}
else{highlightCodeLine( PC.get() );}
codePane.repaint();
IR = (Command)codeMem.get(PC.get());
ViewMachine.output( PC.get()+": "+IR.toString() );
PC.set(PC.get()+1);
if (yield){
if(PC.get()>0){
PC.set(PC.get()-1);}
}
else{
PC.set(PC.get()+1);}
IR.execute();
if (yield){
codeMem.remove(PC.get());
if(!ReadyQueue.array[0].isBelegt()){
PC.set(PC.get()+1);
}
yield=false;
}
sync();
if(compactHeap == true)
......@@ -1345,7 +1381,8 @@ public class ViewMachine implements ActionListener
if( PC.get() < 0 ) break;
if( reset ) break;
}
running = false;
runButton.setText("Restart");
......@@ -1395,6 +1432,11 @@ public class ViewMachine implements ActionListener
comment = null;
view.removeAll();
Register.reset();
vam.machines.tma.JTable.reset();
//von mir
Threadtable.list.clear();
Threadtable.reset();
ReadyQueue.reset();
repaintView();
visualizationList.clear(); // AD : sonts bleibt das System manchmal haengen (nach einem restart)
......@@ -1490,21 +1532,22 @@ public class ViewMachine implements ActionListener
// WARNUNG : beim Verschieben nach unten wird num = 1 vorausgesetzt
// num wird nur beim Verschieben nach oben benutzt, und zwar
// falls mehrere Zellen vom Stack gleichzeitig verschwinden
vam.machines.tma.Thread thread = Threadtable.list.get(MainCommand.CT.get());
int ct = MainCommand.CT.get();
int stackSize = stack.getSize();
Vector actions = new Vector();
if(down) {
// stack bis auf oberste Zelle nach unten bewegen
for(int i =0; i < stackBound - 1; i++) {
Cell c = memory.getCell(addr-i);
Point new_loc = new Point(100, (int)c.getLocation().getY());
Point new_loc = new Point(100+(ct*100), (int)c.getLocation().getY());
new_loc.translate(0, Cell.stdHeight*num);
if(new_loc.getY() > view.getHeight() - 30) {
new_loc = new Point(100, (int)view.getHeight() - 30);
new_loc = new Point(100+(ct*100), (int)view.getHeight() - 30);
}
actions.add(new MoveAction(c, new_loc));
}
hiddenStackCells++;
}
else {
// stack nach oben bewegen
......@@ -1522,7 +1565,7 @@ public class ViewMachine implements ActionListener
}
hiddenStackCells -= num;
thread.hiddenStackCells -= num;
}
for(int i = 0; i < actions.size(); i++) {
......@@ -1559,7 +1602,7 @@ public class ViewMachine implements ActionListener
// AD : Liefert den Namen der Machine in Abh. von Datei-Endung
private String extensionToMachine(String name) {
String ext = name.substring(name.lastIndexOf(".")+1).toLowerCase();
ext = name.substring(name.lastIndexOf(".")+1).toLowerCase();
String machine = machineBase;
if(ext.equals("cma")) {
......@@ -1574,6 +1617,10 @@ public class ViewMachine implements ActionListener
else if(ext.equals("jvm")) {
machine = "vam.machines.minijvm";
}
// von mir
else if(ext.equals("tma")) {
machine = "vam.machines.tma";
}
else if(ext.equals("wim")) {
machine = "vam.machines.wim";
}
......
......@@ -19,7 +19,7 @@ public class NEW extends MainCommand
int number = memory.getIntValue(SP());
for( int i=0;i< number; i++ )
{
memory.setValue( NP()+i,"" );
memory.setValue( NP()+i,"");
heapGroup.add( memory.getCell( NP()+i ) );
}
memory.setIntValue( SP(), NP() );
......
package vam.machines.tma;
public class ADD extends MainCommand
{
public ADD()
{
super("ADD");
}
public void execute()
{
int op2 = memory.getIntValue( SP() );
int op1 = memory.getIntValue( SP()-1 );
Thread thread = Threadtable.list.get(MainCommand.CT.get());
thread.getStack().binaryOperation( "ADD", op1 + op2 );
}
}
package vam.machines.tma;
public class ALLOC extends MainCommand
{
int number;
public ALLOC( int number )
{
super("ALLOC "+number);
this.number = number;
}
public void execute()
{
Thread thread = Threadtable.list.get(MainCommand.CT.get());
thread.getStack().increase( number );
}
}
package vam.machines.tma;
public class AND extends MainCommand
{
public AND()
{
super("AND");
}
public void execute()
{
int op2 = memory.getIntValue( SP() );
int op1 = memory.getIntValue( SP()-1 );
Thread thread = Threadtable.list.get(MainCommand.CT.get());
thread.getStack().binaryOperation( "AND", op1 & op2 );
}
}
package vam.machines.tma;
import java.awt.*;
import vam.base.Cell;
public class CALL extends MainCommand
{
int number;
public CALL()
{
super("CALL");
}
public void execute()
{
int newPC = memory.getIntValue(SP());
Cell register = MainCommand.viewMachine.PC.copy();
register.setVisible(true);
register.highlight(true);
Cell cell = memory.getCell(SP()).copy();
cell.setVisible(true);
cell.highlight(true);
memory.getCell(SP()).setVisible(false);
// exchange
Point registerPosition = register.getLocation();
register.moveTo(memory.getCell(SP()));
cell.moveTo(registerPosition);
Cell.syncAll();
memory.setValue(SP(),PC());
stack.highlight( 1,Color.magenta ); // Rücksprungadresse retten
FP(SP()); // setzen des neuen Frame Pointer
PC(newPC); // Funktion anspringen
//memory.getCell(SP()).setVisible(true);
memory.getCell(SP()).fadeIn();
cell.fadeOut();
register.fadeOut();
Cell.syncAll();
cell.remove();
register.remove();
// stack.push()