|
|
@ -17,23 +17,11 @@ import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
import java.util.TimerTask;
|
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
|
|
|
|
|
|
|
|
public class CpuEntity extends Peripheral implements Runnable{
|
|
|
|
public class CpuEntity extends Peripheral{
|
|
|
|
|
|
|
|
public int i = 0;
|
|
|
|
|
|
|
|
|
|
|
|
static long interruptTimer = 50L;
|
|
|
|
|
|
|
|
//Adjust as needed to match original clock speed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TimerTask cpuTask = new TimerTask(){
|
|
|
|
|
|
|
|
public void run(){
|
|
|
|
|
|
|
|
for(int i = 0; i < 50; i++)
|
|
|
|
|
|
|
|
step();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Timer timer = new Timer();
|
|
|
|
|
|
|
|
boolean notTicked = true;
|
|
|
|
boolean notTicked = true;
|
|
|
|
private boolean running = false;
|
|
|
|
public RCCpu cpu;
|
|
|
|
private Semaphore waiInterrupt = new Semaphore(2);
|
|
|
|
|
|
|
|
private RCCpu cpu;
|
|
|
|
|
|
|
|
private RCBus rcbus;
|
|
|
|
private RCBus rcbus;
|
|
|
|
private Path bootloader;
|
|
|
|
private Path bootloader;
|
|
|
|
|
|
|
|
|
|
|
@ -54,7 +42,7 @@ public class CpuEntity extends Peripheral implements Runnable{
|
|
|
|
be.cpu = new RCCpu(world, pos);
|
|
|
|
be.cpu = new RCCpu(world, pos);
|
|
|
|
be.setRCBus(new RCBus(new RedBus()));
|
|
|
|
be.setRCBus(new RCBus(new RedBus()));
|
|
|
|
be.cpu.setBus(be.getRCBus());
|
|
|
|
be.cpu.setBus(be.getRCBus());
|
|
|
|
be.cpu.setBus(new Bus(be.getRCBus().getRedBus()));
|
|
|
|
be.cpu.setBus(be.getRCBus());
|
|
|
|
Memory ram = new Memory(0x0000, 0x2000);
|
|
|
|
Memory ram = new Memory(0x0000, 0x2000);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
ram.loadFromFile(be.bootloader, 0x400, 0x100);
|
|
|
|
ram.loadFromFile(be.bootloader, 0x400, 0x100);
|
|
|
@ -64,12 +52,12 @@ public class CpuEntity extends Peripheral implements Runnable{
|
|
|
|
be.cpu.getBus().addDevice(ram);
|
|
|
|
be.cpu.getBus().addDevice(ram);
|
|
|
|
be.reset();
|
|
|
|
be.reset();
|
|
|
|
be.notTicked = false;
|
|
|
|
be.notTicked = false;
|
|
|
|
be.start();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(int i = 0; i < 50; i++)
|
|
|
|
|
|
|
|
be.step();
|
|
|
|
public void start(){
|
|
|
|
if(be.cpu.isWaitingForInterrupt()){
|
|
|
|
timer.schedule(cpuTask, new Date(), 2L);
|
|
|
|
be.cpu.setInterrupt(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -102,47 +90,19 @@ public class CpuEntity extends Peripheral implements Runnable{
|
|
|
|
this.defaultMonitorId = id;
|
|
|
|
this.defaultMonitorId = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isRunning() {
|
|
|
|
|
|
|
|
return this.running;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
this.running = true;
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
|
|
step();
|
|
|
|
|
|
|
|
} while (this.running);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void stop() {
|
|
|
|
|
|
|
|
this.running = false;
|
|
|
|
|
|
|
|
while (this.waiInterrupt.availablePermits() <= 0) {
|
|
|
|
|
|
|
|
this.waiInterrupt.release();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.waiInterrupt.drainPermits();
|
|
|
|
|
|
|
|
this.waiInterrupt.release(2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void reset() {
|
|
|
|
public void reset() {
|
|
|
|
stop();
|
|
|
|
|
|
|
|
this.cpu.reset();
|
|
|
|
this.cpu.reset();
|
|
|
|
this.getRCBus().write(0, this.defaultDriveId, getWorld(), this.getPos());
|
|
|
|
this.getRCBus().write(0, this.defaultDriveId, getWorld(), this.getPos());
|
|
|
|
this.getRCBus().write(1, this.defaultMonitorId, getWorld(), this.getPos());
|
|
|
|
this.getRCBus().write(1, this.defaultMonitorId, getWorld(), this.getPos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void step() {
|
|
|
|
public void step() {
|
|
|
|
this.waiInterrupt.acquireUninterruptibly();
|
|
|
|
i++;
|
|
|
|
this.cpu.step();
|
|
|
|
if(!this.cpu.isWaitingForInterrupt()) {
|
|
|
|
this.getRCBus().update();
|
|
|
|
this.cpu.step();
|
|
|
|
if (this.cpu.isStopped()) {
|
|
|
|
this.getBus().update();
|
|
|
|
this.stop();
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.cpu.isWaitingForInterrupt()) {
|
|
|
|
|
|
|
|
this.waiInterrupt.acquireUninterruptibly();
|
|
|
|
|
|
|
|
this.cpu.assertIrq();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.waiInterrupt.availablePermits() < 2) {
|
|
|
|
|
|
|
|
this.waiInterrupt.release();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|